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"
14 u64 bh_blocknr(struct buffer_head *bh)
19 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
21 struct btrfs_node *node = btrfs_buffer_node(buf);
22 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
23 printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n",
24 bh_blocknr(buf), btrfs_header_blocknr(&node->header));
30 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
32 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
33 int blockbits = root->fs_info->sb->s_blocksize_bits;
34 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
36 struct buffer_head *bh;
37 struct buffer_head *head;
38 struct buffer_head *ret = NULL;
41 page = find_lock_page(mapping, index);
45 if (!page_has_buffers(page))
48 head = page_buffers(page);
51 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
60 page_cache_release(page);
64 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
70 set_buffer_mapped(bh);
72 map_bh(bh, root->fs_info->sb, logical);
77 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
80 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
81 int blockbits = root->fs_info->sb->s_blocksize_bits;
82 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
84 struct buffer_head *bh;
85 struct buffer_head *head;
86 struct buffer_head *ret = NULL;
88 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
90 page = grab_cache_page(mapping, index);
94 if (!page_has_buffers(page))
95 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
96 head = page_buffers(page);
99 if (!buffer_mapped(bh)) {
100 err = btrfs_map_bh_to_logical(root, bh, first_block);
103 if (bh_blocknr(bh) == blocknr) {
108 bh = bh->b_this_page;
110 } while (bh != head);
115 page_cache_release(page);
119 static int btree_get_block(struct inode *inode, sector_t iblock,
120 struct buffer_head *bh, int create)
123 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
124 err = btrfs_map_bh_to_logical(root, bh, iblock);
128 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
131 struct scatterlist sg;
132 struct crypto_hash *tfm = root->fs_info->hash_tfm;
133 struct hash_desc desc;
138 sg_init_one(&sg, data, len);
139 spin_lock(&root->fs_info->hash_lock);
140 ret = crypto_hash_digest(&desc, &sg, 1, result);
141 spin_unlock(&root->fs_info->hash_lock);
143 printk("digest failed\n");
147 static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
150 char result[BTRFS_CRC32_SIZE];
152 struct btrfs_node *node;
154 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
155 bh->b_size - BTRFS_CSUM_SIZE, result);
159 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
160 printk("checksum verify failed on %Lu\n",
165 node = btrfs_buffer_node(bh);
166 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
171 static int btree_writepage(struct page *page, struct writeback_control *wbc)
173 struct buffer_head *bh;
174 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
175 struct buffer_head *head;
176 if (!page_has_buffers(page)) {
177 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
178 (1 << BH_Dirty)|(1 << BH_Uptodate));
180 head = page_buffers(page);
183 if (buffer_dirty(bh))
184 csum_tree_block(root, bh, 0);
185 bh = bh->b_this_page;
186 } while (bh != head);
187 return block_write_full_page(page, btree_get_block, wbc);
190 static int btree_readpage(struct file * file, struct page * page)
192 return block_read_full_page(page, btree_get_block);
195 static struct address_space_operations btree_aops = {
196 .readpage = btree_readpage,
197 .writepage = btree_writepage,
198 .sync_page = block_sync_page,
201 int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
203 struct buffer_head *bh = NULL;
206 bh = btrfs_find_create_tree_block(root, blocknr);
209 if (buffer_uptodate(bh)) {
213 if (test_set_buffer_locked(bh)) {
217 if (!buffer_uptodate(bh)) {
219 bh->b_end_io = end_buffer_read_sync;
230 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
232 struct buffer_head *bh = NULL;
234 bh = btrfs_find_create_tree_block(root, blocknr);
237 if (buffer_uptodate(bh))
240 if (!buffer_uptodate(bh)) {
242 bh->b_end_io = end_buffer_read_sync;
245 if (!buffer_uptodate(bh))
251 if (!buffer_checked(bh)) {
252 csum_tree_block(root, bh, 1);
253 set_buffer_checked(bh);
255 if (check_tree_block(root, bh))
263 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
264 struct buffer_head *buf)
266 WARN_ON(atomic_read(&buf->b_count) == 0);
267 mark_buffer_dirty(buf);
271 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
272 struct buffer_head *buf)
274 WARN_ON(atomic_read(&buf->b_count) == 0);
275 clear_buffer_dirty(buf);
279 static int __setup_root(int blocksize,
280 struct btrfs_root *root,
281 struct btrfs_fs_info *fs_info,
286 root->commit_root = NULL;
287 root->blocksize = blocksize;
289 root->fs_info = fs_info;
290 root->objectid = objectid;
291 root->last_trans = 0;
292 root->highest_inode = 0;
293 root->last_inode_alloc = 0;
294 memset(&root->root_key, 0, sizeof(root->root_key));
295 memset(&root->root_item, 0, sizeof(root->root_item));
296 root->root_key.objectid = objectid;
300 static int find_and_setup_root(int blocksize,
301 struct btrfs_root *tree_root,
302 struct btrfs_fs_info *fs_info,
304 struct btrfs_root *root)
308 __setup_root(blocksize, root, fs_info, objectid);
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 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
320 struct btrfs_key *location)
322 struct btrfs_root *root;
323 struct btrfs_root *tree_root = fs_info->tree_root;
324 struct btrfs_path *path;
325 struct btrfs_leaf *l;
329 root = radix_tree_lookup(&fs_info->fs_roots_radix,
330 (unsigned long)location->objectid);
333 root = kmalloc(sizeof(*root), GFP_NOFS);
335 return ERR_PTR(-ENOMEM);
336 if (location->offset == (u64)-1) {
337 ret = find_and_setup_root(fs_info->sb->s_blocksize,
338 fs_info->tree_root, fs_info,
339 location->objectid, root);
347 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
350 path = btrfs_alloc_path();
352 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
358 l = btrfs_buffer_leaf(path->nodes[0]);
359 memcpy(&root->root_item,
360 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
361 sizeof(root->root_item));
362 memcpy(&root->root_key, location, sizeof(*location));
365 btrfs_release_path(root, path);
366 btrfs_free_path(path);
371 root->node = read_tree_block(root,
372 btrfs_root_blocknr(&root->root_item));
376 ret = radix_tree_insert(&fs_info->fs_roots_radix,
377 (unsigned long)root->root_key.objectid,
384 ret = btrfs_find_highest_inode(root, &highest_inode);
386 root->highest_inode = highest_inode;
387 root->last_inode_alloc = highest_inode;
392 struct btrfs_root *open_ctree(struct super_block *sb)
394 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
396 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
398 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
401 struct btrfs_super_block *disk_super;
403 init_bit_radix(&fs_info->pinned_radix);
404 init_bit_radix(&fs_info->pending_del_radix);
405 init_bit_radix(&fs_info->extent_map_radix);
406 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
407 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
408 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
409 INIT_LIST_HEAD(&fs_info->trans_list);
410 INIT_LIST_HEAD(&fs_info->dead_roots);
411 sb_set_blocksize(sb, 4096);
412 fs_info->running_transaction = NULL;
413 fs_info->tree_root = tree_root;
414 fs_info->extent_root = extent_root;
416 fs_info->btree_inode = new_inode(sb);
417 fs_info->btree_inode->i_ino = 1;
418 fs_info->btree_inode->i_nlink = 1;
419 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
420 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
421 fs_info->do_barriers = 1;
422 fs_info->extent_tree_insert_nr = 0;
423 fs_info->extent_tree_prealloc_nr = 0;
424 fs_info->closing = 0;
426 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
427 BTRFS_I(fs_info->btree_inode)->root = tree_root;
428 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
429 sizeof(struct btrfs_key));
430 insert_inode_hash(fs_info->btree_inode);
431 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
432 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
433 spin_lock_init(&fs_info->hash_lock);
434 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
435 printk("failed to allocate digest hash\n");
438 mutex_init(&fs_info->trans_mutex);
439 mutex_init(&fs_info->fs_mutex);
441 __setup_root(sb->s_blocksize, tree_root,
442 fs_info, BTRFS_ROOT_TREE_OBJECTID);
444 fs_info->sb_buffer = read_tree_block(tree_root,
445 BTRFS_SUPER_INFO_OFFSET /
448 if (!fs_info->sb_buffer)
450 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
451 if (!btrfs_super_root(disk_super))
454 i_size_write(fs_info->btree_inode,
455 btrfs_super_total_blocks(disk_super) <<
456 fs_info->btree_inode->i_blkbits);
458 fs_info->disk_super = disk_super;
459 tree_root->node = read_tree_block(tree_root,
460 btrfs_super_root(disk_super));
461 BUG_ON(!tree_root->node);
463 mutex_lock(&fs_info->fs_mutex);
464 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
465 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
468 btrfs_read_block_groups(extent_root);
470 fs_info->generation = btrfs_super_generation(disk_super) + 1;
471 mutex_unlock(&fs_info->fs_mutex);
475 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
479 struct buffer_head *bh = root->fs_info->sb_buffer;
481 btrfs_set_super_root(root->fs_info->disk_super,
482 bh_blocknr(root->fs_info->tree_root->node));
484 WARN_ON(atomic_read(&bh->b_count) < 1);
485 clear_buffer_dirty(bh);
486 csum_tree_block(root, bh, 0);
487 bh->b_end_io = end_buffer_write_sync;
489 if (root->fs_info->do_barriers)
490 ret = submit_bh(WRITE_BARRIER, bh);
492 ret = submit_bh(WRITE, bh);
493 if (ret == -EOPNOTSUPP) {
494 set_buffer_uptodate(bh);
495 root->fs_info->do_barriers = 0;
496 ret = submit_bh(WRITE, bh);
499 if (!buffer_uptodate(bh)) {
506 static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
508 radix_tree_delete(&fs_info->fs_roots_radix,
509 (unsigned long)root->root_key.objectid);
514 if (root->commit_root)
515 brelse(root->commit_root);
520 static int del_fs_roots(struct btrfs_fs_info *fs_info)
523 struct btrfs_root *gang[8];
527 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
532 for (i = 0; i < ret; i++)
533 free_fs_root(fs_info, gang[i]);
538 int close_ctree(struct btrfs_root *root)
541 struct btrfs_trans_handle *trans;
542 struct btrfs_fs_info *fs_info = root->fs_info;
544 fs_info->closing = 1;
545 btrfs_transaction_flush_work(root);
546 mutex_lock(&fs_info->fs_mutex);
547 trans = btrfs_start_transaction(root, 1);
548 btrfs_commit_transaction(trans, root);
549 /* run commit again to drop the original snapshot */
550 trans = btrfs_start_transaction(root, 1);
551 btrfs_commit_transaction(trans, root);
552 ret = btrfs_write_and_wait_transaction(NULL, root);
554 write_ctree_super(NULL, root);
555 mutex_unlock(&fs_info->fs_mutex);
557 if (fs_info->extent_root->node)
558 btrfs_block_release(fs_info->extent_root,
559 fs_info->extent_root->node);
560 if (fs_info->tree_root->node)
561 btrfs_block_release(fs_info->tree_root,
562 fs_info->tree_root->node);
563 btrfs_block_release(root, fs_info->sb_buffer);
564 crypto_free_hash(fs_info->hash_tfm);
565 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
566 iput(fs_info->btree_inode);
568 btrfs_free_block_groups(root->fs_info);
569 del_fs_roots(fs_info);
570 kfree(fs_info->extent_root);
571 kfree(fs_info->tree_root);
575 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
580 void btrfs_btree_balance_dirty(struct btrfs_root *root)
582 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);