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;
24 int btrfs_insert_dev_radix(struct btrfs_root
*root
,
30 struct dev_lookup
*lookup
;
33 lookup
= malloc(sizeof(*lookup
));
36 lookup
->block_start
= block_start
;
37 lookup
->num_blocks
= num_blocks
;
39 lookup
->device_id
= device_id
;
40 printf("inserting into dev radix %Lu %Lu\n", block_start
, num_blocks
);
42 ret
= radix_tree_insert(&root
->fs_info
->dev_radix
, block_start
+
43 num_blocks
- 1, lookup
);
47 int btrfs_map_bh_to_logical(struct btrfs_root
*root
, struct btrfs_buffer
*bh
,
50 struct dev_lookup
*lookup
[2];
54 root
= root
->fs_info
->dev_root
;
55 ret
= radix_tree_gang_lookup(&root
->fs_info
->dev_radix
,
57 (unsigned long)logical
,
59 if (ret
== 0 || lookup
[0]->block_start
> logical
||
60 lookup
[0]->block_start
+ lookup
[0]->num_blocks
<= logical
) {
64 bh
->fd
= lookup
[0]->fd
;
65 bh
->dev_blocknr
= logical
- lookup
[0]->block_start
;
71 static int check_tree_block(struct btrfs_root
*root
, struct btrfs_buffer
*buf
)
73 if (buf
->blocknr
!= btrfs_header_blocknr(&buf
->node
.header
))
75 if (memcmp(root
->fs_info
->disk_super
->fsid
, buf
->node
.header
.fsid
,
76 sizeof(buf
->node
.header
.fsid
)))
81 static int free_some_buffers(struct btrfs_root
*root
)
83 struct list_head
*node
, *next
;
84 struct btrfs_buffer
*b
;
85 if (root
->fs_info
->cache_size
< cache_max
)
87 list_for_each_safe(node
, next
, &root
->fs_info
->cache
) {
88 b
= list_entry(node
, struct btrfs_buffer
, cache
);
90 BUG_ON(!list_empty(&b
->dirty
));
91 list_del_init(&b
->cache
);
92 btrfs_block_release(root
, b
);
93 if (root
->fs_info
->cache_size
< cache_max
)
100 struct btrfs_buffer
*alloc_tree_block(struct btrfs_root
*root
, u64 blocknr
)
102 struct btrfs_buffer
*buf
;
105 buf
= malloc(sizeof(struct btrfs_buffer
) + root
->blocksize
);
109 buf
->blocknr
= blocknr
;
111 INIT_LIST_HEAD(&buf
->dirty
);
112 free_some_buffers(root
);
113 radix_tree_preload(GFP_KERNEL
);
114 ret
= radix_tree_insert(&root
->fs_info
->cache_radix
, blocknr
, buf
);
115 radix_tree_preload_end();
116 list_add_tail(&buf
->cache
, &root
->fs_info
->cache
);
117 root
->fs_info
->cache_size
++;
125 struct btrfs_buffer
*find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
127 struct btrfs_buffer
*buf
;
128 buf
= radix_tree_lookup(&root
->fs_info
->cache_radix
, blocknr
);
132 buf
= alloc_tree_block(root
, blocknr
);
141 struct btrfs_buffer
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
143 struct btrfs_buffer
*buf
;
145 buf
= radix_tree_lookup(&root
->fs_info
->cache_radix
, blocknr
);
149 buf
= alloc_tree_block(root
, blocknr
);
152 btrfs_map_bh_to_logical(root
, buf
, blocknr
);
153 ret
= pread(buf
->fd
, &buf
->node
, root
->blocksize
,
154 buf
->dev_blocknr
* root
->blocksize
);
155 if (ret
!= root
->blocksize
) {
160 if (check_tree_block(root
, buf
))
165 int dirty_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
166 struct btrfs_buffer
*buf
)
168 if (!list_empty(&buf
->dirty
))
170 list_add_tail(&buf
->dirty
, &root
->fs_info
->trans
);
175 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
176 struct btrfs_buffer
*buf
)
178 if (!list_empty(&buf
->dirty
)) {
179 list_del_init(&buf
->dirty
);
180 btrfs_block_release(root
, buf
);
185 int write_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
186 struct btrfs_buffer
*buf
)
190 if (buf
->blocknr
!= btrfs_header_blocknr(&buf
->node
.header
))
192 btrfs_map_bh_to_logical(root
, buf
, buf
->blocknr
);
193 ret
= pwrite(buf
->fd
, &buf
->node
, root
->blocksize
,
194 buf
->dev_blocknr
* root
->blocksize
);
195 if (ret
!= root
->blocksize
)
200 static int __commit_transaction(struct btrfs_trans_handle
*trans
, struct
203 struct btrfs_buffer
*b
;
206 while(!list_empty(&root
->fs_info
->trans
)) {
207 b
= list_entry(root
->fs_info
->trans
.next
, struct btrfs_buffer
,
209 list_del_init(&b
->dirty
);
210 wret
= write_tree_block(trans
, root
, b
);
213 btrfs_block_release(root
, b
);
218 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
219 struct btrfs_fs_info
*fs_info
)
222 u64 old_extent_block
;
223 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
224 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
226 if (btrfs_super_device_root(fs_info
->disk_super
) !=
227 fs_info
->dev_root
->node
->blocknr
) {
228 btrfs_set_super_device_root(fs_info
->disk_super
,
229 fs_info
->dev_root
->node
->blocknr
);
232 old_extent_block
= btrfs_root_blocknr(&extent_root
->root_item
);
233 if (old_extent_block
== extent_root
->node
->blocknr
)
235 btrfs_set_root_blocknr(&extent_root
->root_item
,
236 extent_root
->node
->blocknr
);
237 ret
= btrfs_update_root(trans
, tree_root
,
238 &extent_root
->root_key
,
239 &extent_root
->root_item
);
245 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
, struct
246 btrfs_root
*root
, struct btrfs_super_block
*s
)
249 struct btrfs_buffer
*snap
= root
->commit_root
;
250 struct btrfs_key snap_key
;
252 if (root
->commit_root
== root
->node
)
255 memcpy(&snap_key
, &root
->root_key
, sizeof(snap_key
));
256 root
->root_key
.offset
++;
258 btrfs_set_root_blocknr(&root
->root_item
, root
->node
->blocknr
);
259 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
260 &root
->root_key
, &root
->root_item
);
263 ret
= commit_tree_roots(trans
, root
->fs_info
);
266 ret
= __commit_transaction(trans
, root
);
269 write_ctree_super(trans
, root
, s
);
270 btrfs_finish_extent_commit(trans
, root
->fs_info
->extent_root
);
271 btrfs_finish_extent_commit(trans
, root
->fs_info
->tree_root
);
273 root
->commit_root
= root
->node
;
275 ret
= btrfs_drop_snapshot(trans
, root
, snap
);
278 ret
= btrfs_del_root(trans
, root
->fs_info
->tree_root
, &snap_key
);
280 root
->fs_info
->generation
= root
->root_key
.offset
+ 1;
285 static int __setup_root(struct btrfs_super_block
*super
,
286 struct btrfs_root
*root
,
287 struct btrfs_fs_info
*fs_info
,
288 u64 objectid
, int fp
)
291 root
->commit_root
= NULL
;
292 root
->blocksize
= btrfs_super_blocksize(super
);
294 root
->fs_info
= fs_info
;
295 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
296 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
300 static int find_and_setup_root(struct btrfs_super_block
*super
,
301 struct btrfs_root
*tree_root
,
302 struct btrfs_fs_info
*fs_info
,
304 struct btrfs_root
*root
, int fp
)
308 __setup_root(super
, root
, fs_info
, objectid
, fp
);
309 ret
= btrfs_find_last_root(tree_root
, objectid
,
310 &root
->root_item
, &root
->root_key
);
313 root
->node
= read_tree_block(root
,
314 btrfs_root_blocknr(&root
->root_item
));
319 int btrfs_open_disk(struct btrfs_root
*root
, u64 device_id
,
320 u64 block_start
, u64 num_blocks
,
321 char *filename
, int name_len
)
327 null_filename
= malloc(name_len
+ 1);
330 memcpy(null_filename
, filename
, name_len
);
331 null_filename
[name_len
] = '\0';
333 fd
= open(null_filename
, O_RDWR
);
338 ret
= btrfs_insert_dev_radix(root
, fd
, device_id
,
339 block_start
, num_blocks
);
347 static int read_device_info(struct btrfs_root
*root
)
349 struct btrfs_path path
;
351 struct btrfs_key key
;
352 struct btrfs_leaf
*leaf
;
353 struct btrfs_device_item
*dev_item
;
357 root
= root
->fs_info
->dev_root
;
359 btrfs_init_path(&path
);
363 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
365 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
366 leaf
= &path
.nodes
[0]->leaf
;
367 nritems
= btrfs_header_nritems(&leaf
->header
);
369 slot
= path
.slots
[0];
370 if (slot
>= nritems
) {
371 ret
= btrfs_next_leaf(root
, &path
);
374 leaf
= &path
.nodes
[0]->leaf
;
375 nritems
= btrfs_header_nritems(&leaf
->header
);
376 slot
= path
.slots
[0];
378 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
379 if (btrfs_key_type(&key
) != BTRFS_DEV_ITEM_KEY
) {
383 dev_item
= btrfs_item_ptr(leaf
, slot
, struct btrfs_device_item
);
384 if (btrfs_device_id(dev_item
) !=
385 btrfs_super_device_id(root
->fs_info
->disk_super
)) {
386 printf("found key %Lu %Lu\n", key
.objectid
, key
.offset
);
387 ret
= btrfs_open_disk(root
, btrfs_device_id(dev_item
),
388 key
.objectid
, key
.offset
,
389 (char *)(dev_item
+ 1),
390 btrfs_device_pathlen(dev_item
));
395 btrfs_release_path(root
, &path
);
399 struct btrfs_root
*open_ctree(char *filename
, struct btrfs_super_block
*super
)
403 fp
= open(filename
, O_CREAT
| O_RDWR
, 0600);
407 return open_ctree_fd(fp
, super
);
410 struct btrfs_root
*open_ctree_fd(int fp
, struct btrfs_super_block
*super
)
412 struct btrfs_root
*root
= malloc(sizeof(struct btrfs_root
));
413 struct btrfs_root
*extent_root
= malloc(sizeof(struct btrfs_root
));
414 struct btrfs_root
*tree_root
= malloc(sizeof(struct btrfs_root
));
415 struct btrfs_root
*dev_root
= malloc(sizeof(struct btrfs_root
));
416 struct btrfs_fs_info
*fs_info
= malloc(sizeof(*fs_info
));
417 struct dev_lookup
*dev_lookup
;
420 INIT_RADIX_TREE(&fs_info
->cache_radix
, GFP_KERNEL
);
421 INIT_RADIX_TREE(&fs_info
->pinned_radix
, GFP_KERNEL
);
422 INIT_RADIX_TREE(&fs_info
->dev_radix
, GFP_KERNEL
);
423 INIT_LIST_HEAD(&fs_info
->trans
);
424 INIT_LIST_HEAD(&fs_info
->cache
);
425 fs_info
->cache_size
= 0;
427 fs_info
->running_transaction
= NULL
;
428 fs_info
->fs_root
= root
;
429 fs_info
->tree_root
= tree_root
;
430 fs_info
->extent_root
= extent_root
;
431 fs_info
->dev_root
= dev_root
;
432 fs_info
->last_inode_alloc
= 0;
433 fs_info
->last_inode_alloc_dirid
= 0;
434 fs_info
->disk_super
= super
;
435 memset(&fs_info
->current_insert
, 0, sizeof(fs_info
->current_insert
));
436 memset(&fs_info
->last_insert
, 0, sizeof(fs_info
->last_insert
));
438 ret
= pread(fp
, super
, sizeof(struct btrfs_super_block
),
439 BTRFS_SUPER_INFO_OFFSET
);
440 if (ret
== 0 || btrfs_super_root(super
) == 0) {
445 __setup_root(super
, dev_root
, fs_info
, BTRFS_DEV_TREE_OBJECTID
, fp
);
447 dev_lookup
= malloc(sizeof(*dev_lookup
));
449 dev_lookup
->device_id
= btrfs_super_device_id(super
);
450 dev_lookup
->block_start
= btrfs_super_device_block_start(super
);
451 dev_lookup
->num_blocks
= btrfs_super_device_num_blocks(super
);
452 ret
= radix_tree_insert(&fs_info
->dev_radix
,
453 dev_lookup
->block_start
+
454 dev_lookup
->num_blocks
- 1, dev_lookup
);
457 dev_root
->node
= read_tree_block(dev_root
,
458 btrfs_super_device_root(super
));
460 ret
= read_device_info(dev_root
);
463 __setup_root(super
, tree_root
, fs_info
, BTRFS_ROOT_TREE_OBJECTID
, fp
);
464 tree_root
->node
= read_tree_block(tree_root
, btrfs_super_root(super
));
465 BUG_ON(!tree_root
->node
);
467 ret
= find_and_setup_root(super
, tree_root
, fs_info
,
468 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
, fp
);
471 ret
= find_and_setup_root(super
, tree_root
, fs_info
,
472 BTRFS_FS_TREE_OBJECTID
, root
, fp
);
475 root
->commit_root
= root
->node
;
478 root
->fs_info
->generation
= root
->root_key
.offset
+ 1;
482 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
483 *root
, struct btrfs_super_block
*s
)
486 btrfs_set_super_root(s
, root
->fs_info
->tree_root
->node
->blocknr
);
487 ret
= pwrite(root
->fs_info
->fp
, s
, sizeof(*s
),
488 BTRFS_SUPER_INFO_OFFSET
);
489 if (ret
!= sizeof(*s
)) {
490 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
496 static int drop_cache(struct btrfs_root
*root
)
498 while(!list_empty(&root
->fs_info
->cache
)) {
499 struct btrfs_buffer
*b
= list_entry(root
->fs_info
->cache
.next
,
502 list_del_init(&b
->cache
);
503 btrfs_block_release(root
, b
);
508 static int free_dev_radix(struct btrfs_fs_info
*fs_info
)
510 struct dev_lookup
*lookup
[8];
514 ret
= radix_tree_gang_lookup(&fs_info
->dev_radix
,
519 for (i
= 0; i
< ret
; i
++) {
520 if (lookup
[i
]->device_id
!=
521 btrfs_super_device_id(fs_info
->disk_super
))
522 close(lookup
[i
]->fd
);
523 radix_tree_delete(&fs_info
->dev_radix
,
524 lookup
[i
]->block_start
+
525 lookup
[i
]->num_blocks
- 1);
532 int close_ctree(struct btrfs_root
*root
, struct btrfs_super_block
*s
)
535 struct btrfs_trans_handle
*trans
;
537 trans
= root
->fs_info
->running_transaction
;
538 btrfs_commit_transaction(trans
, root
, s
);
539 ret
= commit_tree_roots(trans
, root
->fs_info
);
541 ret
= __commit_transaction(trans
, root
);
543 write_ctree_super(trans
, root
, s
);
545 BUG_ON(!list_empty(&root
->fs_info
->trans
));
547 free_dev_radix(root
->fs_info
);
548 close(root
->fs_info
->fp
);
550 btrfs_block_release(root
, root
->node
);
551 if (root
->fs_info
->extent_root
->node
)
552 btrfs_block_release(root
->fs_info
->extent_root
,
553 root
->fs_info
->extent_root
->node
);
554 if (root
->fs_info
->tree_root
->node
)
555 btrfs_block_release(root
->fs_info
->tree_root
,
556 root
->fs_info
->tree_root
->node
);
557 if (root
->fs_info
->dev_root
->node
)
558 btrfs_block_release(root
->fs_info
->dev_root
,
559 root
->fs_info
->dev_root
->node
);
560 btrfs_block_release(root
, root
->commit_root
);
562 printf("on close %d blocks are allocated\n", allocated_blocks
);
566 void btrfs_block_release(struct btrfs_root
*root
, struct btrfs_buffer
*buf
)
571 if (buf
->count
== 0) {
572 BUG_ON(!list_empty(&buf
->cache
));
573 BUG_ON(!list_empty(&buf
->dirty
));
574 if (!radix_tree_lookup(&root
->fs_info
->cache_radix
,
577 radix_tree_delete(&root
->fs_info
->cache_radix
, buf
->blocknr
);
578 memset(buf
, 0, sizeof(*buf
));
580 BUG_ON(allocated_blocks
== 0);
582 BUG_ON(root
->fs_info
->cache_size
== 0);
583 root
->fs_info
->cache_size
--;