1 #define _XOPEN_SOURCE 500
8 #include "kerncompat.h"
9 #include "radix-tree.h"
12 #include "transaction.h"
14 static int allocated_blocks
= 0;
15 int cache_max
= 10000;
17 static int check_tree_block(struct btrfs_root
*root
, struct btrfs_buffer
*buf
)
19 if (buf
->blocknr
!= btrfs_header_blocknr(&buf
->node
.header
))
21 if (root
->node
&& btrfs_header_parentid(&buf
->node
.header
) !=
22 btrfs_header_parentid(&root
->node
->node
.header
))
27 static int free_some_buffers(struct btrfs_root
*root
)
29 struct list_head
*node
, *next
;
30 struct btrfs_buffer
*b
;
31 if (root
->cache_size
< cache_max
)
33 list_for_each_safe(node
, next
, &root
->cache
) {
34 b
= list_entry(node
, struct btrfs_buffer
, cache
);
36 BUG_ON(!list_empty(&b
->dirty
));
37 list_del_init(&b
->cache
);
38 btrfs_block_release(root
, b
);
39 if (root
->cache_size
< cache_max
)
46 struct btrfs_buffer
*alloc_tree_block(struct btrfs_root
*root
, u64 blocknr
)
48 struct btrfs_buffer
*buf
;
51 buf
= malloc(sizeof(struct btrfs_buffer
) + root
->blocksize
);
55 buf
->blocknr
= blocknr
;
57 INIT_LIST_HEAD(&buf
->dirty
);
58 free_some_buffers(root
);
59 radix_tree_preload(GFP_KERNEL
);
60 ret
= radix_tree_insert(&root
->cache_radix
, blocknr
, buf
);
61 radix_tree_preload_end();
62 list_add_tail(&buf
->cache
, &root
->cache
);
71 struct btrfs_buffer
*find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
73 struct btrfs_buffer
*buf
;
74 buf
= radix_tree_lookup(&root
->cache_radix
, blocknr
);
78 buf
= alloc_tree_block(root
, blocknr
);
87 struct btrfs_buffer
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
89 loff_t offset
= blocknr
* root
->blocksize
;
90 struct btrfs_buffer
*buf
;
93 buf
= radix_tree_lookup(&root
->cache_radix
, blocknr
);
97 buf
= alloc_tree_block(root
, blocknr
);
100 ret
= pread(root
->fp
, &buf
->node
, root
->blocksize
, offset
);
101 if (ret
!= root
->blocksize
) {
106 if (check_tree_block(root
, buf
))
111 int dirty_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
112 struct btrfs_buffer
*buf
)
114 if (!list_empty(&buf
->dirty
))
116 list_add_tail(&buf
->dirty
, &root
->trans
);
121 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
122 struct btrfs_buffer
*buf
)
124 if (!list_empty(&buf
->dirty
)) {
125 list_del_init(&buf
->dirty
);
126 btrfs_block_release(root
, buf
);
131 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
132 struct btrfs_buffer
*buf
)
134 u64 blocknr
= buf
->blocknr
;
135 loff_t offset
= blocknr
* root
->blocksize
;
138 if (buf
->blocknr
!= btrfs_header_blocknr(&buf
->node
.header
))
140 ret
= pwrite(root
->fp
, &buf
->node
, root
->blocksize
, offset
);
141 if (ret
!= root
->blocksize
)
146 static int __commit_transaction(struct btrfs_trans_handle
*trans
, struct
149 struct btrfs_buffer
*b
;
152 while(!list_empty(&root
->trans
)) {
153 b
= list_entry(root
->trans
.next
, struct btrfs_buffer
, dirty
);
154 list_del_init(&b
->dirty
);
155 wret
= write_tree_block(trans
, root
, b
);
158 btrfs_block_release(root
, b
);
163 static int commit_extent_and_tree_roots(struct btrfs_trans_handle
*trans
,
164 struct btrfs_root
*tree_root
, struct
165 btrfs_root
*extent_root
)
168 u64 old_extent_block
;
171 old_extent_block
= btrfs_root_blocknr(&extent_root
->root_item
);
172 if (old_extent_block
== extent_root
->node
->blocknr
)
174 btrfs_set_root_blocknr(&extent_root
->root_item
,
175 extent_root
->node
->blocknr
);
176 ret
= btrfs_update_root(trans
, tree_root
,
177 &extent_root
->root_key
,
178 &extent_root
->root_item
);
181 __commit_transaction(trans
, extent_root
);
182 __commit_transaction(trans
, tree_root
);
186 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
, struct
187 btrfs_root
*root
, struct btrfs_super_block
*s
)
190 struct btrfs_buffer
*snap
= root
->commit_root
;
191 struct btrfs_key snap_key
;
193 ret
= __commit_transaction(trans
, root
);
196 if (root
->commit_root
== root
->node
)
199 memcpy(&snap_key
, &root
->root_key
, sizeof(snap_key
));
200 root
->root_key
.offset
++;
202 btrfs_set_root_blocknr(&root
->root_item
, root
->node
->blocknr
);
203 ret
= btrfs_insert_root(trans
, root
->tree_root
, &root
->root_key
,
207 ret
= commit_extent_and_tree_roots(trans
, root
->tree_root
,
211 write_ctree_super(trans
, root
, s
);
212 btrfs_finish_extent_commit(trans
, root
->extent_root
);
213 btrfs_finish_extent_commit(trans
, root
->tree_root
);
215 root
->commit_root
= root
->node
;
217 ret
= btrfs_drop_snapshot(trans
, root
, snap
);
220 ret
= btrfs_del_root(trans
, root
->tree_root
, &snap_key
);
226 static int __setup_root(struct btrfs_super_block
*super
,
227 struct btrfs_root
*root
, u64 objectid
, int fp
)
229 INIT_LIST_HEAD(&root
->trans
);
230 INIT_LIST_HEAD(&root
->cache
);
231 root
->cache_size
= 0;
234 root
->commit_root
= NULL
;
235 root
->blocksize
= btrfs_super_blocksize(super
);
237 memset(&root
->current_insert
, 0, sizeof(root
->current_insert
));
238 memset(&root
->last_insert
, 0, sizeof(root
->last_insert
));
239 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
240 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
244 static int find_and_setup_root(struct btrfs_super_block
*super
,
245 struct btrfs_root
*tree_root
, u64 objectid
,
246 struct btrfs_root
*root
, int fp
)
250 __setup_root(super
, root
, objectid
, fp
);
251 ret
= btrfs_find_last_root(tree_root
, objectid
,
252 &root
->root_item
, &root
->root_key
);
255 root
->node
= read_tree_block(root
,
256 btrfs_root_blocknr(&root
->root_item
));
261 struct btrfs_root
*open_ctree(char *filename
, struct btrfs_super_block
*super
)
263 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
264 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
265 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
269 root
->extent_root
= extent_root
;
270 root
->tree_root
= tree_root
;
272 extent_root
->extent_root
= extent_root
;
273 extent_root
->tree_root
= tree_root
;
275 tree_root
->extent_root
= extent_root
;
276 tree_root
->tree_root
= tree_root
;
278 fp
= open(filename
, O_CREAT
| O_RDWR
, 0600);
283 INIT_RADIX_TREE(&root
->cache_radix
, GFP_KERNEL
);
284 INIT_RADIX_TREE(&root
->pinned_radix
, GFP_KERNEL
);
285 INIT_RADIX_TREE(&extent_root
->pinned_radix
, GFP_KERNEL
);
286 INIT_RADIX_TREE(&extent_root
->cache_radix
, GFP_KERNEL
);
287 INIT_RADIX_TREE(&tree_root
->pinned_radix
, GFP_KERNEL
);
288 INIT_RADIX_TREE(&tree_root
->cache_radix
, GFP_KERNEL
);
290 ret
= pread(fp
, super
, sizeof(struct btrfs_super_block
),
291 BTRFS_SUPER_INFO_OFFSET
);
292 if (ret
== 0 || btrfs_super_root(super
) == 0) {
293 printf("making new FS!\n");
294 ret
= mkfs(fp
, 0, 1024);
297 ret
= pread(fp
, super
, sizeof(struct btrfs_super_block
),
298 BTRFS_SUPER_INFO_OFFSET
);
299 if (ret
!= sizeof(struct btrfs_super_block
))
304 __setup_root(super
, tree_root
, BTRFS_ROOT_TREE_OBJECTID
, fp
);
305 tree_root
->node
= read_tree_block(tree_root
, btrfs_super_root(super
));
306 BUG_ON(!tree_root
->node
);
308 ret
= find_and_setup_root(super
, tree_root
, BTRFS_EXTENT_TREE_OBJECTID
,
312 ret
= find_and_setup_root(super
, tree_root
, BTRFS_FS_TREE_OBJECTID
,
316 root
->commit_root
= root
->node
;
322 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
323 *root
, struct btrfs_super_block
*s
)
326 btrfs_set_super_root(s
, root
->tree_root
->node
->blocknr
);
327 ret
= pwrite(root
->fp
, s
, sizeof(*s
),
328 BTRFS_SUPER_INFO_OFFSET
);
329 if (ret
!= sizeof(*s
)) {
330 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
336 static int drop_cache(struct btrfs_root
*root
)
338 while(!list_empty(&root
->cache
)) {
339 struct btrfs_buffer
*b
= list_entry(root
->cache
.next
,
340 struct btrfs_buffer
, cache
);
341 list_del_init(&b
->cache
);
342 btrfs_block_release(root
, b
);
346 int close_ctree(struct btrfs_root
*root
, struct btrfs_super_block
*s
)
349 struct btrfs_trans_handle
*trans
;
351 trans
= root
->running_transaction
;
352 btrfs_commit_transaction(trans
, root
, s
);
353 ret
= commit_extent_and_tree_roots(trans
, root
->tree_root
,
356 write_ctree_super(trans
, root
, s
);
357 drop_cache(root
->extent_root
);
358 drop_cache(root
->tree_root
);
360 BUG_ON(!list_empty(&root
->trans
));
361 BUG_ON(!list_empty(&root
->extent_root
->trans
));
362 BUG_ON(!list_empty(&root
->tree_root
->trans
));
366 btrfs_block_release(root
, root
->node
);
367 if (root
->extent_root
->node
)
368 btrfs_block_release(root
->extent_root
, root
->extent_root
->node
);
369 if (root
->tree_root
->node
)
370 btrfs_block_release(root
->tree_root
, root
->tree_root
->node
);
371 btrfs_block_release(root
, root
->commit_root
);
373 printf("on close %d blocks are allocated\n", allocated_blocks
);
377 void btrfs_block_release(struct btrfs_root
*root
, struct btrfs_buffer
*buf
)
382 if (buf
->count
== 0) {
383 BUG_ON(!list_empty(&buf
->cache
));
384 BUG_ON(!list_empty(&buf
->dirty
));
385 if (!radix_tree_lookup(&root
->cache_radix
, buf
->blocknr
))
387 radix_tree_delete(&root
->cache_radix
, buf
->blocknr
);
388 memset(buf
, 0, sizeof(*buf
));
390 BUG_ON(allocated_blocks
== 0);
392 BUG_ON(root
->cache_size
== 0);