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>
10 #include "transaction.h"
11 #include "btrfs_inode.h"
13 static int check_tree_block(struct btrfs_root
*root
, struct buffer_head
*buf
)
15 struct btrfs_node
*node
= btrfs_buffer_node(buf
);
16 if (buf
->b_blocknr
!= btrfs_header_blocknr(&node
->header
)) {
22 struct buffer_head
*btrfs_find_tree_block(struct btrfs_root
*root
, u64 blocknr
)
24 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
25 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
26 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
28 struct buffer_head
*bh
;
29 struct buffer_head
*head
;
30 struct buffer_head
*ret
= NULL
;
33 page
= find_lock_page(mapping
, index
);
37 if (!page_has_buffers(page
))
40 head
= page_buffers(page
);
43 if (buffer_mapped(bh
) && bh
->b_blocknr
== blocknr
) {
55 page_cache_release(page
);
59 struct buffer_head
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
62 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
63 int blockbits
= root
->fs_info
->sb
->s_blocksize_bits
;
64 unsigned long index
= blocknr
>> (PAGE_CACHE_SHIFT
- blockbits
);
66 struct buffer_head
*bh
;
67 struct buffer_head
*head
;
68 struct buffer_head
*ret
= NULL
;
69 u64 first_block
= index
<< (PAGE_CACHE_SHIFT
- blockbits
);
71 page
= grab_cache_page(mapping
, index
);
75 if (!page_has_buffers(page
))
76 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
, 0);
77 head
= page_buffers(page
);
80 if (!buffer_mapped(bh
)) {
81 bh
->b_bdev
= root
->fs_info
->sb
->s_bdev
;
82 bh
->b_blocknr
= first_block
;
83 set_buffer_mapped(bh
);
85 if (bh
->b_blocknr
== blocknr
) {
97 page_cache_release(page
);
101 static sector_t
max_block(struct block_device
*bdev
)
103 sector_t retval
= ~((sector_t
)0);
104 loff_t sz
= i_size_read(bdev
->bd_inode
);
107 unsigned int size
= block_size(bdev
);
108 unsigned int sizebits
= blksize_bits(size
);
109 retval
= (sz
>> sizebits
);
114 static int btree_get_block(struct inode
*inode
, sector_t iblock
,
115 struct buffer_head
*bh
, int create
)
117 if (iblock
>= max_block(inode
->i_sb
->s_bdev
)) {
122 * for reads, we're just trying to fill a partial page.
123 * return a hole, they will have to call get_block again
124 * before they can fill it, and they will get -EIO at that
129 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
130 bh
->b_blocknr
= iblock
;
131 set_buffer_mapped(bh
);
135 int btrfs_csum_data(struct btrfs_root
* root
, char *data
, size_t len
,
138 struct scatterlist sg
;
139 struct crypto_hash
*tfm
= root
->fs_info
->hash_tfm
;
140 struct hash_desc desc
;
145 sg_init_one(&sg
, data
, len
);
146 spin_lock(&root
->fs_info
->hash_lock
);
147 ret
= crypto_hash_digest(&desc
, &sg
, 1, result
);
148 spin_unlock(&root
->fs_info
->hash_lock
);
150 printk("sha256 digest failed\n");
154 static int csum_tree_block(struct btrfs_root
*root
, struct buffer_head
*bh
,
157 char result
[BTRFS_CSUM_SIZE
];
159 struct btrfs_node
*node
;
161 ret
= btrfs_csum_data(root
, bh
->b_data
+ BTRFS_CSUM_SIZE
,
162 bh
->b_size
- BTRFS_CSUM_SIZE
, result
);
166 if (memcmp(bh
->b_data
, result
, BTRFS_CSUM_SIZE
)) {
167 printk("checksum verify failed on %lu\n",
172 node
= btrfs_buffer_node(bh
);
173 memcpy(node
->header
.csum
, result
, BTRFS_CSUM_SIZE
);
178 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
180 struct buffer_head
*bh
;
181 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
182 struct buffer_head
*head
;
183 if (!page_has_buffers(page
)) {
184 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
185 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
187 head
= page_buffers(page
);
190 if (buffer_dirty(bh
))
191 csum_tree_block(root
, bh
, 0);
192 bh
= bh
->b_this_page
;
193 } while (bh
!= head
);
194 return block_write_full_page(page
, btree_get_block
, wbc
);
197 static int btree_readpage(struct file
* file
, struct page
* page
)
199 return block_read_full_page(page
, btree_get_block
);
202 static struct address_space_operations btree_aops
= {
203 .readpage
= btree_readpage
,
204 .writepage
= btree_writepage
,
205 .sync_page
= block_sync_page
,
208 struct buffer_head
*read_tree_block(struct btrfs_root
*root
, u64 blocknr
)
210 struct buffer_head
*bh
= NULL
;
212 bh
= btrfs_find_create_tree_block(root
, blocknr
);
215 if (buffer_uptodate(bh
))
218 if (!buffer_uptodate(bh
)) {
220 bh
->b_end_io
= end_buffer_read_sync
;
223 if (!buffer_uptodate(bh
))
225 csum_tree_block(root
, bh
, 1);
230 if (check_tree_block(root
, bh
))
238 int dirty_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
239 struct buffer_head
*buf
)
241 WARN_ON(atomic_read(&buf
->b_count
) == 0);
242 mark_buffer_dirty(buf
);
246 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
247 struct buffer_head
*buf
)
249 WARN_ON(atomic_read(&buf
->b_count
) == 0);
250 clear_buffer_dirty(buf
);
254 static int __setup_root(int blocksize
,
255 struct btrfs_root
*root
,
256 struct btrfs_fs_info
*fs_info
,
261 root
->commit_root
= NULL
;
262 root
->blocksize
= blocksize
;
264 root
->fs_info
= fs_info
;
265 root
->objectid
= objectid
;
266 root
->last_trans
= 0;
267 root
->highest_inode
= 0;
268 root
->last_inode_alloc
= 0;
269 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
270 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
274 static int find_and_setup_root(int blocksize
,
275 struct btrfs_root
*tree_root
,
276 struct btrfs_fs_info
*fs_info
,
278 struct btrfs_root
*root
)
282 __setup_root(blocksize
, root
, fs_info
, objectid
);
283 ret
= btrfs_find_last_root(tree_root
, objectid
,
284 &root
->root_item
, &root
->root_key
);
287 root
->node
= read_tree_block(root
,
288 btrfs_root_blocknr(&root
->root_item
));
293 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
294 struct btrfs_key
*location
)
296 struct btrfs_root
*root
;
297 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
298 struct btrfs_path
*path
;
299 struct btrfs_leaf
*l
;
303 printk("read_fs_root looking for %Lu %Lu %u\n", location
->objectid
, location
->offset
, location
->flags
);
304 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
305 (unsigned long)location
->objectid
);
307 printk("found %p in cache\n", root
);
310 root
= kmalloc(sizeof(*root
), GFP_NOFS
);
313 return ERR_PTR(-ENOMEM
);
315 if (location
->offset
== (u64
)-1) {
316 ret
= find_and_setup_root(fs_info
->sb
->s_blocksize
,
317 fs_info
->tree_root
, fs_info
,
318 location
->objectid
, root
);
327 __setup_root(fs_info
->sb
->s_blocksize
, root
, fs_info
,
330 path
= btrfs_alloc_path();
332 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
334 printk("internal search_slot gives us %d\n", ret
);
339 l
= btrfs_buffer_leaf(path
->nodes
[0]);
340 memcpy(&root
->root_item
,
341 btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_root_item
),
342 sizeof(root
->root_item
));
343 memcpy(&root
->root_key
, location
, sizeof(*location
));
346 btrfs_release_path(root
, path
);
347 btrfs_free_path(path
);
352 root
->node
= read_tree_block(root
,
353 btrfs_root_blocknr(&root
->root_item
));
356 printk("inserting %p\n", root
);
358 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
359 (unsigned long)root
->root_key
.objectid
,
362 printk("radix_tree_insert gives us %d\n", ret
);
367 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
369 root
->highest_inode
= highest_inode
;
370 root
->last_inode_alloc
= highest_inode
;
371 printk("highest inode is %Lu\n", highest_inode
);
373 printk("all worked\n");
377 struct btrfs_root
*open_ctree(struct super_block
*sb
)
379 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
381 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
383 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
385 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
388 struct btrfs_super_block
*disk_super
;
390 init_bit_radix(&fs_info
->pinned_radix
);
391 init_bit_radix(&fs_info
->pending_del_radix
);
392 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
393 sb_set_blocksize(sb
, 4096);
394 fs_info
->running_transaction
= NULL
;
395 fs_info
->tree_root
= tree_root
;
396 fs_info
->extent_root
= extent_root
;
397 fs_info
->dev_root
= dev_root
;
399 fs_info
->btree_inode
= new_inode(sb
);
400 fs_info
->btree_inode
->i_ino
= 1;
401 fs_info
->btree_inode
->i_nlink
= 1;
402 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
403 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
404 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
405 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
406 sizeof(struct btrfs_key
));
407 insert_inode_hash(fs_info
->btree_inode
);
408 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
409 fs_info
->hash_tfm
= crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC
);
410 spin_lock_init(&fs_info
->hash_lock
);
411 if (!fs_info
->hash_tfm
|| IS_ERR(fs_info
->hash_tfm
)) {
412 printk("failed to allocate sha256 hash\n");
415 mutex_init(&fs_info
->trans_mutex
);
416 mutex_init(&fs_info
->fs_mutex
);
417 memset(&fs_info
->current_insert
, 0, sizeof(fs_info
->current_insert
));
418 memset(&fs_info
->last_insert
, 0, sizeof(fs_info
->last_insert
));
420 __setup_root(sb
->s_blocksize
, dev_root
,
421 fs_info
, BTRFS_DEV_TREE_OBJECTID
);
423 __setup_root(sb
->s_blocksize
, tree_root
,
424 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
425 fs_info
->sb_buffer
= read_tree_block(tree_root
,
426 BTRFS_SUPER_INFO_OFFSET
/
429 if (!fs_info
->sb_buffer
)
431 disk_super
= (struct btrfs_super_block
*)fs_info
->sb_buffer
->b_data
;
432 if (!btrfs_super_root(disk_super
))
435 fs_info
->disk_super
= disk_super
;
436 dev_root
->node
= read_tree_block(tree_root
,
437 btrfs_super_device_root(disk_super
));
438 tree_root
->node
= read_tree_block(tree_root
,
439 btrfs_super_root(disk_super
));
440 BUG_ON(!tree_root
->node
);
442 mutex_lock(&fs_info
->fs_mutex
);
443 ret
= find_and_setup_root(sb
->s_blocksize
, tree_root
, fs_info
,
444 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
447 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
448 memset(&fs_info
->kobj
, 0, sizeof(fs_info
->kobj
));
449 kobj_set_kset_s(fs_info
, btrfs_subsys
);
450 kobject_set_name(&fs_info
->kobj
, "%s", sb
->s_id
);
451 kobject_register(&fs_info
->kobj
);
452 mutex_unlock(&fs_info
->fs_mutex
);
456 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
459 struct buffer_head
*bh
= root
->fs_info
->sb_buffer
;
461 btrfs_set_super_root(root
->fs_info
->disk_super
,
462 root
->fs_info
->tree_root
->node
->b_blocknr
);
464 WARN_ON(atomic_read(&bh
->b_count
) < 1);
465 clear_buffer_dirty(bh
);
466 csum_tree_block(root
, bh
, 0);
467 bh
->b_end_io
= end_buffer_write_sync
;
469 submit_bh(WRITE
, bh
);
471 if (!buffer_uptodate(bh
)) {
478 static int free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
480 radix_tree_delete(&fs_info
->fs_roots_radix
,
481 (unsigned long)root
->root_key
.objectid
);
486 if (root
->commit_root
)
487 brelse(root
->commit_root
);
492 int del_fs_roots(struct btrfs_fs_info
*fs_info
)
495 struct btrfs_root
*gang
[8];
499 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
504 for (i
= 0; i
< ret
; i
++)
505 free_fs_root(fs_info
, gang
[i
]);
510 int close_ctree(struct btrfs_root
*root
)
513 struct btrfs_trans_handle
*trans
;
514 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
516 mutex_lock(&fs_info
->fs_mutex
);
517 trans
= btrfs_start_transaction(root
, 1);
518 btrfs_commit_transaction(trans
, root
);
519 /* run commit again to drop the original snapshot */
520 trans
= btrfs_start_transaction(root
, 1);
521 btrfs_commit_transaction(trans
, root
);
522 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
524 write_ctree_super(NULL
, root
);
525 mutex_unlock(&fs_info
->fs_mutex
);
527 if (fs_info
->extent_root
->node
)
528 btrfs_block_release(fs_info
->extent_root
,
529 fs_info
->extent_root
->node
);
530 if (fs_info
->dev_root
->node
)
531 btrfs_block_release(fs_info
->dev_root
,
532 fs_info
->dev_root
->node
);
533 if (fs_info
->tree_root
->node
)
534 btrfs_block_release(fs_info
->tree_root
,
535 fs_info
->tree_root
->node
);
536 btrfs_block_release(root
, fs_info
->sb_buffer
);
537 crypto_free_hash(fs_info
->hash_tfm
);
538 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
539 iput(fs_info
->btree_inode
);
540 del_fs_roots(fs_info
);
541 kfree(fs_info
->extent_root
);
542 kfree(fs_info
->tree_root
);
543 kobject_unregister(&fs_info
->kobj
);
547 void btrfs_block_release(struct btrfs_root
*root
, struct buffer_head
*buf
)