blob: 997cc578a1852f7ee4379394c61955b16a2b0917 [file] [log] [blame]
Chris Masoneb60cea2007-02-02 09:18:22 -05001#define _XOPEN_SOURCE 500
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include "kerncompat.h"
9#include "radix-tree.h"
10#include "ctree.h"
11#include "disk-io.h"
12
13static int allocated_blocks = 0;
Chris Masoned2ff2c2007-03-01 18:59:40 -050014int cache_max = 10000;
Chris Masoneb60cea2007-02-02 09:18:22 -050015
Chris Mason9a8dd152007-02-23 08:38:36 -050016static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050017{
Chris Mason9a8dd152007-02-23 08:38:36 -050018 if (buf->blocknr != buf->node.header.blocknr)
19 BUG();
20 if (root->node && buf->node.header.parentid != root->node->node.header.parentid)
21 BUG();
22 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050023}
24
Chris Masoned2ff2c2007-03-01 18:59:40 -050025static int free_some_buffers(struct ctree_root *root)
26{
27 struct list_head *node, *next;
28 struct tree_buffer *b;
29 if (root->cache_size < cache_max)
30 return 0;
31 list_for_each_safe(node, next, &root->cache) {
32 b = list_entry(node, struct tree_buffer, cache);
33 if (b->count == 1) {
34 BUG_ON(!list_empty(&b->dirty));
35 list_del_init(&b->cache);
36 tree_block_release(root, b);
37 if (root->cache_size < cache_max)
Chris Mason77ce6842007-03-02 10:06:43 -050038 break;
Chris Masoned2ff2c2007-03-01 18:59:40 -050039 }
40 }
41 return 0;
42}
43
Chris Masoneb60cea2007-02-02 09:18:22 -050044struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
45{
46 struct tree_buffer *buf;
47 int ret;
48 buf = malloc(sizeof(struct tree_buffer));
49 if (!buf)
50 return buf;
51 allocated_blocks++;
52 buf->blocknr = blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -050053 buf->count = 2;
54 INIT_LIST_HEAD(&buf->dirty);
55 free_some_buffers(root);
Chris Masoneb60cea2007-02-02 09:18:22 -050056 radix_tree_preload(GFP_KERNEL);
57 ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
58 radix_tree_preload_end();
Chris Masoned2ff2c2007-03-01 18:59:40 -050059 list_add_tail(&buf->cache, &root->cache);
60 root->cache_size++;
Chris Masoneb60cea2007-02-02 09:18:22 -050061 if (ret) {
62 free(buf);
63 return NULL;
64 }
65 return buf;
66}
67
Chris Mason9a8dd152007-02-23 08:38:36 -050068struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050069{
Chris Mason9a8dd152007-02-23 08:38:36 -050070 struct tree_buffer *buf;
71 buf = radix_tree_lookup(&root->cache_radix, blocknr);
72 if (buf) {
73 buf->count++;
74 } else {
75 buf = alloc_tree_block(root, blocknr);
76 if (!buf) {
77 BUG();
78 return NULL;
79 }
Chris Masoneb60cea2007-02-02 09:18:22 -050080 }
Chris Masoneb60cea2007-02-02 09:18:22 -050081 return buf;
82}
83
84struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
85{
Chris Masond97e63b2007-02-20 16:40:44 -050086 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -050087 struct tree_buffer *buf;
88 int ret;
89
90 buf = radix_tree_lookup(&root->cache_radix, blocknr);
91 if (buf) {
92 buf->count++;
Chris Mason9a8dd152007-02-23 08:38:36 -050093 } else {
94 buf = alloc_tree_block(root, blocknr);
95 if (!buf)
96 return NULL;
97 ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
98 if (ret != CTREE_BLOCKSIZE) {
99 free(buf);
100 return NULL;
101 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500102 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500103 if (check_tree_block(root, buf))
Chris Masoncfaa7292007-02-21 17:04:57 -0500104 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500105 return buf;
106}
107
Chris Masoned2ff2c2007-03-01 18:59:40 -0500108int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf)
109{
110 if (!list_empty(&buf->dirty))
111 return 0;
112 list_add_tail(&buf->dirty, &root->trans);
113 buf->count++;
114 return 0;
115}
116
117int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf)
118{
119 if (!list_empty(&buf->dirty)) {
120 list_del_init(&buf->dirty);
121 tree_block_release(root, buf);
122 }
123 return 0;
124}
125
Chris Masoneb60cea2007-02-02 09:18:22 -0500126int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
127{
128 u64 blocknr = buf->blocknr;
Chris Masond97e63b2007-02-20 16:40:44 -0500129 loff_t offset = blocknr * CTREE_BLOCKSIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500130 int ret;
131
132 if (buf->blocknr != buf->node.header.blocknr)
133 BUG();
134 ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
135 if (ret != CTREE_BLOCKSIZE)
136 return ret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 return 0;
138}
139
Chris Masoned2ff2c2007-03-01 18:59:40 -0500140static int __commit_transaction(struct ctree_root *root)
141{
142 struct tree_buffer *b;
143 int ret = 0;
144 int wret;
145 while(!list_empty(&root->trans)) {
146 b = list_entry(root->trans.next, struct tree_buffer, dirty);
147 list_del_init(&b->dirty);
148 wret = write_tree_block(root, b);
149 if (wret)
150 ret = wret;
151 tree_block_release(root, b);
152 }
153 return ret;
154}
155
Chris Masona28ec192007-03-06 20:08:01 -0500156int commit_transaction(struct ctree_root *root, struct ctree_super_block *s)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500157{
Chris Masona28ec192007-03-06 20:08:01 -0500158 int ret = 0;
159
Chris Masoned2ff2c2007-03-01 18:59:40 -0500160 ret = __commit_transaction(root);
161 if (!ret && root != root->extent_root)
162 ret = __commit_transaction(root->extent_root);
163 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500164 if (root->commit_root != root->node) {
165 struct tree_buffer *snap = root->commit_root;
166 root->commit_root = root->node;
167 root->node->count++;
168 ret = btrfs_drop_snapshot(root, snap);
169 BUG_ON(ret);
170 tree_block_release(root, snap);
171 }
172 write_ctree_super(root, s);
173 btrfs_finish_extent_commit(root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500174 return ret;
175}
176
Chris Masond97e63b2007-02-20 16:40:44 -0500177static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
178 struct ctree_root_info *info, int fp)
179{
Chris Masoned2ff2c2007-03-01 18:59:40 -0500180 INIT_LIST_HEAD(&root->trans);
181 INIT_LIST_HEAD(&root->cache);
Chris Masona28ec192007-03-06 20:08:01 -0500182 root->cache_size = 0;
Chris Masond97e63b2007-02-20 16:40:44 -0500183 root->fp = fp;
Chris Masoncfaa7292007-02-21 17:04:57 -0500184 root->node = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -0500185 root->extent_root = extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500186 root->commit_root = NULL;
187 root->node = read_tree_block(root, info->tree_root);
188 memset(&root->current_insert, 0, sizeof(root->current_insert));
Chris Mason0579da42007-03-07 16:15:30 -0500189 memset(&root->last_insert, 0, sizeof(root->last_insert));
Chris Masond97e63b2007-02-20 16:40:44 -0500190 return 0;
191}
192
Chris Masoncfaa7292007-02-21 17:04:57 -0500193struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500194{
195 struct ctree_root *root = malloc(sizeof(struct ctree_root));
Chris Masond97e63b2007-02-20 16:40:44 -0500196 struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
Chris Masoneb60cea2007-02-02 09:18:22 -0500197 int fp;
Chris Masoneb60cea2007-02-02 09:18:22 -0500198 int ret;
199
Chris Masonc6730242007-02-26 10:46:55 -0500200 fp = open(filename, O_CREAT | O_RDWR, 0600);
Chris Masoneb60cea2007-02-02 09:18:22 -0500201 if (fp < 0) {
202 free(root);
203 return NULL;
204 }
Chris Mason9a8dd152007-02-23 08:38:36 -0500205 INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
Chris Masona28ec192007-03-06 20:08:01 -0500206 INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
207 INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
Chris Mason9a8dd152007-02-23 08:38:36 -0500208 INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
Chris Masoncfaa7292007-02-21 17:04:57 -0500209 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500210 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
Chris Mason5c680ed2007-02-22 11:39:13 -0500211 if (ret == 0 || super->root_info.tree_root == 0) {
212 printf("making new FS!\n");
Chris Masond97e63b2007-02-20 16:40:44 -0500213 ret = mkfs(fp);
214 if (ret)
215 return NULL;
Chris Masoncfaa7292007-02-21 17:04:57 -0500216 ret = pread(fp, super, sizeof(struct ctree_super_block),
Chris Masond97e63b2007-02-20 16:40:44 -0500217 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
218 if (ret != sizeof(struct ctree_super_block))
219 return NULL;
220 }
221 BUG_ON(ret < 0);
Chris Masoncfaa7292007-02-21 17:04:57 -0500222 __setup_root(root, extent_root, &super->root_info, fp);
223 __setup_root(extent_root, extent_root, &super->extent_info, fp);
Chris Masona28ec192007-03-06 20:08:01 -0500224 root->commit_root = root->node;
225 root->node->count++;
Chris Masoneb60cea2007-02-02 09:18:22 -0500226 return root;
227}
228
Chris Masoncfaa7292007-02-21 17:04:57 -0500229static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
230{
231 info->tree_root = root->node->blocknr;
Chris Masoncfaa7292007-02-21 17:04:57 -0500232 return 0;
233}
234
235int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
236{
237 int ret;
238 __update_root(root, &s->root_info);
239 __update_root(root->extent_root, &s->extent_info);
240 ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
241 if (ret != sizeof(*s)) {
242 fprintf(stderr, "failed to write new super block err %d\n", ret);
243 return ret;
244 }
245 return 0;
246}
247
Chris Masoned2ff2c2007-03-01 18:59:40 -0500248static int drop_cache(struct ctree_root *root)
249{
250 while(!list_empty(&root->cache)) {
251 struct tree_buffer *b = list_entry(root->cache.next,
252 struct tree_buffer, cache);
253 list_del_init(&b->cache);
254 tree_block_release(root, b);
255 }
256 return 0;
257}
Chris Masona28ec192007-03-06 20:08:01 -0500258int close_ctree(struct ctree_root *root, struct ctree_super_block *s)
Chris Masoneb60cea2007-02-02 09:18:22 -0500259{
Chris Masona28ec192007-03-06 20:08:01 -0500260 commit_transaction(root, s);
261 __commit_transaction(root->extent_root);
262 write_ctree_super(root, s);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500263 drop_cache(root->extent_root);
264 drop_cache(root);
265 BUG_ON(!list_empty(&root->trans));
266 BUG_ON(!list_empty(&root->extent_root->trans));
267
Chris Masoneb60cea2007-02-02 09:18:22 -0500268 close(root->fp);
269 if (root->node)
270 tree_block_release(root, root->node);
Chris Masoncfaa7292007-02-21 17:04:57 -0500271 if (root->extent_root->node)
272 tree_block_release(root->extent_root, root->extent_root->node);
Chris Masona28ec192007-03-06 20:08:01 -0500273 tree_block_release(root, root->commit_root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500274 free(root);
275 printf("on close %d blocks are allocated\n", allocated_blocks);
276 return 0;
277}
278
Chris Masoneb60cea2007-02-02 09:18:22 -0500279void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
280{
Chris Masoneb60cea2007-02-02 09:18:22 -0500281 buf->count--;
Chris Masoncfaa7292007-02-21 17:04:57 -0500282 if (buf->count < 0)
283 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500284 if (buf->count == 0) {
Chris Mason02217ed2007-03-02 16:08:05 -0500285 BUG_ON(!list_empty(&buf->cache));
286 BUG_ON(!list_empty(&buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500287 if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
288 BUG();
289 radix_tree_delete(&root->cache_radix, buf->blocknr);
290 memset(buf, 0, sizeof(*buf));
291 free(buf);
292 BUG_ON(allocated_blocks == 0);
293 allocated_blocks--;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500294 BUG_ON(root->cache_size == 0);
295 root->cache_size--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500296 }
297}
298