1 #include <linux/module.h>
3 #include <linux/blkdev.h>
4 #include <linux/crypto.h>
5 #include <linux/scatterlist.h>
6 #include <linux/swap.h>
7 #include <linux/radix-tree.h>
8 #include <linux/writeback.h>
11 #include "transaction.h"
12 #include "btrfs_inode.h"
18 struct block_device
*bdev
;
21 int btrfs_insert_dev_radix(struct btrfs_root
*root
,
22 struct block_device
*bdev
,
27 struct dev_lookup
*lookup
;
30 lookup
= kmalloc(sizeof(*lookup
), GFP_NOFS
);
33 lookup
->block_start
= block_start
;
34 lookup
->num_blocks
= num_blocks
;
36 lookup
->device_id
= device_id
;
38 ret
= radix_tree_insert(&root
->fs_info
->dev_radix
, block_start
+
39 num_blocks
- 1, lookup
);
43 u64
bh_blocknr(struct buffer_head
*bh
)
45 int blkbits
= bh
->b_page
->mapping
->host
->i_blkbits
;
46 u64 blocknr
= bh
->b_page
->index
<< (PAGE_CACHE_SHIFT
- blkbits
);
49 if (PageHighMem(bh
->b_page
))
50 offset
= (unsigned long)bh
->b_data
;
52 offset
= bh
->b_data
- (char *)page_address(bh
->b_page
);
53 blocknr
+= offset
>> (PAGE_CACHE_SHIFT
- blkbits
);
57 static int check_tree_block(struct btrfs_root
*root
, struct buffer_head
*buf
)
59 struct btrfs_node
*node
= btrfs_buffer_node(buf
);
60 if (bh_blocknr(buf
) != btrfs_header_blocknr(&node
->header
)) {
61 printk(KERN_CRIT
"bh_blocknr(buf) is %Lu, header is %Lu\n",
62 bh_blocknr(buf
), btrfs_header_blocknr(&node
->header
));
68 struct buffer_head
*btrfs_find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
70 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
71 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
72 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
74 struct buffer_head
*bh
;
75 struct buffer_head
*head
;
76 struct buffer_head
*ret
= NULL
;
79 page
= find_lock_page(mapping
, index
);
83 if (!page_has_buffers(page
))
86 head
= page_buffers(page
);
89 if (buffer_mapped(bh
) && bh_blocknr(bh
) == blocknr
) {
98 page_cache_release(page
);
102 int btrfs_map_bh_to_logical(struct btrfs_root
*root
, struct buffer_head
*bh
,
105 struct dev_lookup
*lookup
[2];
112 set_buffer_mapped(bh
);
115 root
= root
->fs_info
->dev_root
;
116 ret
= radix_tree_gang_lookup(&root
->fs_info
->dev_radix
,
118 (unsigned long)logical
,
120 if (ret
== 0 || lookup
[0]->block_start
> logical
||
121 lookup
[0]->block_start
+ lookup
[0]->num_blocks
<= logical
) {
125 bh
->b_bdev
= lookup
[0]->bdev
;
126 bh
->b_blocknr
= logical
- lookup
[0]->block_start
;
127 set_buffer_mapped(bh
);
133 struct buffer_head
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
136 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
137 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
138 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
140 struct buffer_head
*bh
;
141 struct buffer_head
*head
;
142 struct buffer_head
*ret
= NULL
;
144 u64 first_block
= index
<< (PAGE_CACHE_SHIFT
- blockbits
);
146 page
= grab_cache_page(mapping
, index
);
150 if (!page_has_buffers(page
))
151 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
, 0);
152 head
= page_buffers(page
);
155 if (!buffer_mapped(bh
)) {
156 err
= btrfs_map_bh_to_logical(root
, bh
, first_block
);
159 if (bh_blocknr(bh
) == blocknr
) {
164 bh
= bh
->b_this_page
;
166 } while (bh
!= head
);
171 page_cache_release(page
);
175 static int btree_get_block(struct inode
*inode
, sector_t iblock
,
176 struct buffer_head
*bh
, int create
)
179 struct btrfs_root
*root
= BTRFS_I(bh
->b_page
->mapping
->host
)->root
;
180 err
= btrfs_map_bh_to_logical(root
, bh
, iblock
);
184 int btrfs_csum_data(struct btrfs_root
* root
, char *data
, size_t len
,
187 struct scatterlist sg
;
188 struct crypto_hash
*tfm
= root
->fs_info
->hash_tfm
;
189 struct hash_desc desc
;
194 sg_init_one(&sg
, data
, len
);
195 spin_lock(&root
->fs_info
->hash_lock
);
196 ret
= crypto_hash_digest(&desc
, &sg
, 1, result
);
197 spin_unlock(&root
->fs_info
->hash_lock
);
199 printk("sha256 digest failed\n");
203 static int csum_tree_block(struct btrfs_root
*root
, struct buffer_head
*bh
,
206 char result
[BTRFS_CSUM_SIZE
];
208 struct btrfs_node
*node
;
210 ret
= btrfs_csum_data(root
, bh
->b_data
+ BTRFS_CSUM_SIZE
,
211 bh
->b_size
- BTRFS_CSUM_SIZE
, result
);
215 if (memcmp(bh
->b_data
, result
, BTRFS_CSUM_SIZE
)) {
216 printk("checksum verify failed on %Lu\n",
221 node
= btrfs_buffer_node(bh
);
222 memcpy(node
->header
.csum
, result
, BTRFS_CSUM_SIZE
);
227 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
229 struct buffer_head
*bh
;
230 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
231 struct buffer_head
*head
;
232 if (!page_has_buffers(page
)) {
233 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
234 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
236 head
= page_buffers(page
);
239 if (buffer_dirty(bh
))
240 csum_tree_block(root
, bh
, 0);
241 bh
= bh
->b_this_page
;
242 } while (bh
!= head
);
243 return block_write_full_page(page
, btree_get_block
, wbc
);
246 static int btree_readpage(struct file
* file
, struct page
* page
)
248 return block_read_full_page(page
, btree_get_block
);
251 static struct address_space_operations btree_aops
= {
252 .readpage
= btree_readpage
,
253 .writepage
= btree_writepage
,
254 .sync_page
= block_sync_page
,
257 int readahead_tree_block(struct btrfs_root
*root
, u64 blocknr
)
259 struct buffer_head
*bh
= NULL
;
261 bh
= btrfs_find_create_tree_block(root
, blocknr
);
264 if (buffer_uptodate(bh
))
266 if (test_set_buffer_locked(bh
))
268 if (!buffer_uptodate(bh
)) {
270 bh
->b_end_io
= end_buffer_read_sync
;
280 struct buffer_head
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
282 struct buffer_head
*bh
= NULL
;
284 bh
= btrfs_find_create_tree_block(root
, blocknr
);
287 if (buffer_uptodate(bh
))
290 if (!buffer_uptodate(bh
)) {
292 bh
->b_end_io
= end_buffer_read_sync
;
295 if (!buffer_uptodate(bh
))
301 if (!buffer_checked(bh
)) {
302 csum_tree_block(root
, bh
, 1);
303 set_buffer_checked(bh
);
305 if (check_tree_block(root
, bh
))
313 int dirty_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
314 struct buffer_head
*buf
)
316 WARN_ON(atomic_read(&buf
->b_count
) == 0);
317 mark_buffer_dirty(buf
);
321 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
322 struct buffer_head
*buf
)
324 WARN_ON(atomic_read(&buf
->b_count
) == 0);
325 clear_buffer_dirty(buf
);
329 static int __setup_root(int blocksize
,
330 struct btrfs_root
*root
,
331 struct btrfs_fs_info
*fs_info
,
336 root
->commit_root
= NULL
;
337 root
->blocksize
= blocksize
;
339 root
->fs_info
= fs_info
;
340 root
->objectid
= objectid
;
341 root
->last_trans
= 0;
342 root
->highest_inode
= 0;
343 root
->last_inode_alloc
= 0;
344 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
345 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
346 root
->root_key
.objectid
= objectid
;
350 static int find_and_setup_root(int blocksize
,
351 struct btrfs_root
*tree_root
,
352 struct btrfs_fs_info
*fs_info
,
354 struct btrfs_root
*root
)
358 __setup_root(blocksize
, root
, fs_info
, objectid
);
359 ret
= btrfs_find_last_root(tree_root
, objectid
,
360 &root
->root_item
, &root
->root_key
);
363 root
->node
= read_tree_block(root
,
364 btrfs_root_blocknr(&root
->root_item
));
369 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
370 struct btrfs_key
*location
)
372 struct btrfs_root
*root
;
373 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
374 struct btrfs_path
*path
;
375 struct btrfs_leaf
*l
;
379 printk("read_fs_root looking for %Lu %Lu %u\n", location
->objectid
, location
->offset
, location
->flags
);
380 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
381 (unsigned long)location
->objectid
);
383 printk("found %p in cache\n", root
);
386 root
= kmalloc(sizeof(*root
), GFP_NOFS
);
389 return ERR_PTR(-ENOMEM
);
391 if (location
->offset
== (u64
)-1) {
392 ret
= find_and_setup_root(fs_info
->sb
->s_blocksize
,
393 fs_info
->tree_root
, fs_info
,
394 location
->objectid
, root
);
403 __setup_root(fs_info
->sb
->s_blocksize
, root
, fs_info
,
406 path
= btrfs_alloc_path();
408 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
410 printk("internal search_slot gives us %d\n", ret
);
415 l
= btrfs_buffer_leaf(path
->nodes
[0]);
416 memcpy(&root
->root_item
,
417 btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_root_item
),
418 sizeof(root
->root_item
));
419 memcpy(&root
->root_key
, location
, sizeof(*location
));
422 btrfs_release_path(root
, path
);
423 btrfs_free_path(path
);
428 root
->node
= read_tree_block(root
,
429 btrfs_root_blocknr(&root
->root_item
));
432 printk("inserting %p\n", root
);
434 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
435 (unsigned long)root
->root_key
.objectid
,
438 printk("radix_tree_insert gives us %d\n", ret
);
443 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
445 root
->highest_inode
= highest_inode
;
446 root
->last_inode_alloc
= highest_inode
;
447 printk("highest inode is %Lu\n", highest_inode
);
449 printk("all worked\n");
453 static int btrfs_open_disk(struct btrfs_root
*root
, u64 device_id
,
454 u64 block_start
, u64 num_blocks
,
455 char *filename
, int name_len
)
458 struct block_device
*bdev
;
461 null_filename
= kmalloc(name_len
+ 1, GFP_NOFS
);
464 memcpy(null_filename
, filename
, name_len
);
465 null_filename
[name_len
] = '\0';
467 bdev
= open_bdev_excl(null_filename
, O_RDWR
, root
->fs_info
->sb
);
472 set_blocksize(bdev
, root
->fs_info
->sb
->s_blocksize
);
473 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
,
474 block_start
, num_blocks
);
478 kfree(null_filename
);
482 static int read_device_info(struct btrfs_root
*root
)
484 struct btrfs_path
*path
;
486 struct btrfs_key key
;
487 struct btrfs_leaf
*leaf
;
488 struct btrfs_device_item
*dev_item
;
492 root
= root
->fs_info
->dev_root
;
494 path
= btrfs_alloc_path();
500 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
502 mutex_lock(&root
->fs_info
->fs_mutex
);
503 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
504 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
505 nritems
= btrfs_header_nritems(&leaf
->header
);
507 slot
= path
->slots
[0];
508 if (slot
>= nritems
) {
509 ret
= btrfs_next_leaf(root
, path
);
512 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
513 nritems
= btrfs_header_nritems(&leaf
->header
);
514 slot
= path
->slots
[0];
516 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
517 if (btrfs_key_type(&key
) != BTRFS_DEV_ITEM_KEY
) {
521 dev_item
= btrfs_item_ptr(leaf
, slot
, struct btrfs_device_item
);
522 printk("found key %Lu %Lu\n", key
.objectid
, key
.offset
);
523 if (btrfs_device_id(dev_item
) !=
524 btrfs_super_device_id(root
->fs_info
->disk_super
)) {
525 ret
= btrfs_open_disk(root
, btrfs_device_id(dev_item
),
526 key
.objectid
, key
.offset
,
527 (char *)(dev_item
+ 1),
528 btrfs_device_pathlen(dev_item
));
533 btrfs_free_path(path
);
534 mutex_unlock(&root
->fs_info
->fs_mutex
);
538 struct btrfs_root
*open_ctree(struct super_block
*sb
)
540 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
542 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
544 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
546 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
549 struct btrfs_super_block
*disk_super
;
550 struct dev_lookup
*dev_lookup
;
552 init_bit_radix(&fs_info
->pinned_radix
);
553 init_bit_radix(&fs_info
->pending_del_radix
);
554 init_bit_radix(&fs_info
->extent_map_radix
);
555 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
556 INIT_RADIX_TREE(&fs_info
->dev_radix
, GFP_NOFS
);
557 INIT_RADIX_TREE(&fs_info
->block_group_radix
, GFP_KERNEL
);
558 INIT_RADIX_TREE(&fs_info
->block_group_data_radix
, GFP_KERNEL
);
559 INIT_LIST_HEAD(&fs_info
->trans_list
);
560 sb_set_blocksize(sb
, 4096);
561 fs_info
->running_transaction
= NULL
;
562 fs_info
->tree_root
= tree_root
;
563 fs_info
->extent_root
= extent_root
;
564 fs_info
->dev_root
= dev_root
;
566 fs_info
->btree_inode
= new_inode(sb
);
567 fs_info
->btree_inode
->i_ino
= 1;
568 fs_info
->btree_inode
->i_nlink
= 1;
569 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
570 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
571 fs_info
->do_barriers
= 1;
572 fs_info
->extent_tree_insert_nr
= 0;
573 fs_info
->extent_tree_prealloc_nr
= 0;
574 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
575 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
576 sizeof(struct btrfs_key
));
577 insert_inode_hash(fs_info
->btree_inode
);
578 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
579 fs_info
->hash_tfm
= crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC
);
580 spin_lock_init(&fs_info
->hash_lock
);
581 if (!fs_info
->hash_tfm
|| IS_ERR(fs_info
->hash_tfm
)) {
582 printk("failed to allocate sha256 hash\n");
585 mutex_init(&fs_info
->trans_mutex
);
586 mutex_init(&fs_info
->fs_mutex
);
588 __setup_root(sb
->s_blocksize
, dev_root
,
589 fs_info
, BTRFS_DEV_TREE_OBJECTID
);
591 __setup_root(sb
->s_blocksize
, tree_root
,
592 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
594 dev_lookup
= kmalloc(sizeof(*dev_lookup
), GFP_NOFS
);
595 dev_lookup
->block_start
= 0;
596 dev_lookup
->num_blocks
= (u32
)-2;
597 dev_lookup
->bdev
= sb
->s_bdev
;
598 dev_lookup
->device_id
= 0;
599 ret
= radix_tree_insert(&fs_info
->dev_radix
, (u32
)-2, dev_lookup
);
601 fs_info
->sb_buffer
= read_tree_block(tree_root
,
602 BTRFS_SUPER_INFO_OFFSET
/
605 if (!fs_info
->sb_buffer
)
607 disk_super
= (struct btrfs_super_block
*)fs_info
->sb_buffer
->b_data
;
608 if (!btrfs_super_root(disk_super
))
611 i_size_write(fs_info
->btree_inode
,
612 btrfs_super_total_blocks(disk_super
) <<
613 fs_info
->btree_inode
->i_blkbits
);
615 radix_tree_delete(&fs_info
->dev_radix
, (u32
)-2);
616 dev_lookup
->block_start
= btrfs_super_device_block_start(disk_super
);
617 dev_lookup
->num_blocks
= btrfs_super_device_num_blocks(disk_super
);
618 dev_lookup
->device_id
= btrfs_super_device_id(disk_super
);
620 ret
= radix_tree_insert(&fs_info
->dev_radix
,
621 dev_lookup
->block_start
+
622 dev_lookup
->num_blocks
- 1, dev_lookup
);
625 fs_info
->disk_super
= disk_super
;
627 dev_root
->node
= read_tree_block(tree_root
,
628 btrfs_super_device_root(disk_super
));
630 ret
= read_device_info(dev_root
);
633 tree_root
->node
= read_tree_block(tree_root
,
634 btrfs_super_root(disk_super
));
635 BUG_ON(!tree_root
->node
);
637 mutex_lock(&fs_info
->fs_mutex
);
638 ret
= find_and_setup_root(sb
->s_blocksize
, tree_root
, fs_info
,
639 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
642 btrfs_read_block_groups(extent_root
);
644 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
645 memset(&fs_info
->kobj
, 0, sizeof(fs_info
->kobj
));
646 kobj_set_kset_s(fs_info
, btrfs_subsys
);
647 kobject_set_name(&fs_info
->kobj
, "%s", sb
->s_id
);
648 kobject_register(&fs_info
->kobj
);
649 mutex_unlock(&fs_info
->fs_mutex
);
653 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
657 struct buffer_head
*bh
= root
->fs_info
->sb_buffer
;
659 btrfs_set_super_root(root
->fs_info
->disk_super
,
660 bh_blocknr(root
->fs_info
->tree_root
->node
));
662 WARN_ON(atomic_read(&bh
->b_count
) < 1);
663 clear_buffer_dirty(bh
);
664 csum_tree_block(root
, bh
, 0);
665 bh
->b_end_io
= end_buffer_write_sync
;
667 if (root
->fs_info
->do_barriers
)
668 ret
= submit_bh(WRITE_BARRIER
, bh
);
670 ret
= submit_bh(WRITE
, bh
);
671 if (ret
== -EOPNOTSUPP
) {
672 set_buffer_uptodate(bh
);
673 root
->fs_info
->do_barriers
= 0;
674 ret
= submit_bh(WRITE
, bh
);
677 if (!buffer_uptodate(bh
)) {
684 static int free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
686 radix_tree_delete(&fs_info
->fs_roots_radix
,
687 (unsigned long)root
->root_key
.objectid
);
692 if (root
->commit_root
)
693 brelse(root
->commit_root
);
698 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
701 struct btrfs_root
*gang
[8];
705 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
710 for (i
= 0; i
< ret
; i
++)
711 free_fs_root(fs_info
, gang
[i
]);
716 static int free_dev_radix(struct btrfs_fs_info
*fs_info
)
718 struct dev_lookup
*lookup
[8];
719 struct block_device
*super_bdev
= fs_info
->sb
->s_bdev
;
723 ret
= radix_tree_gang_lookup(&fs_info
->dev_radix
,
728 for (i
= 0; i
< ret
; i
++) {
729 if (lookup
[i
]->bdev
!= super_bdev
)
730 close_bdev_excl(lookup
[i
]->bdev
);
731 radix_tree_delete(&fs_info
->dev_radix
,
732 lookup
[i
]->block_start
+
733 lookup
[i
]->num_blocks
- 1);
740 int close_ctree(struct btrfs_root
*root
)
743 struct btrfs_trans_handle
*trans
;
744 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
746 mutex_lock(&fs_info
->fs_mutex
);
747 trans
= btrfs_start_transaction(root
, 1);
748 btrfs_commit_transaction(trans
, root
);
749 /* run commit again to drop the original snapshot */
750 trans
= btrfs_start_transaction(root
, 1);
751 btrfs_commit_transaction(trans
, root
);
752 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
754 write_ctree_super(NULL
, root
);
755 mutex_unlock(&fs_info
->fs_mutex
);
757 if (fs_info
->extent_root
->node
)
758 btrfs_block_release(fs_info
->extent_root
,
759 fs_info
->extent_root
->node
);
760 if (fs_info
->dev_root
->node
)
761 btrfs_block_release(fs_info
->dev_root
,
762 fs_info
->dev_root
->node
);
763 if (fs_info
->tree_root
->node
)
764 btrfs_block_release(fs_info
->tree_root
,
765 fs_info
->tree_root
->node
);
766 btrfs_block_release(root
, fs_info
->sb_buffer
);
767 crypto_free_hash(fs_info
->hash_tfm
);
768 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
769 iput(fs_info
->btree_inode
);
771 free_dev_radix(fs_info
);
772 btrfs_free_block_groups(root
->fs_info
);
773 del_fs_roots(fs_info
);
774 kfree(fs_info
->extent_root
);
775 kfree(fs_info
->tree_root
);
776 kobject_unregister(&fs_info
->kobj
);
780 void btrfs_block_release(struct btrfs_root
*root
, struct buffer_head
*buf
)
785 void btrfs_btree_balance_dirty(struct btrfs_root
*root
)
787 balance_dirty_pages_ratelimited(root
->fs_info
->btree_inode
->i_mapping
);