2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/module.h>
21 #include <linux/blkdev.h>
22 #include <linux/crypto.h>
23 #include <linux/scatterlist.h>
24 #include <linux/swap.h>
25 #include <linux/radix-tree.h>
26 #include <linux/writeback.h>
29 #include "transaction.h"
30 #include "btrfs_inode.h"
32 u64 bh_blocknr(struct buffer_head *bh)
37 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
39 struct btrfs_node *node = btrfs_buffer_node(buf);
40 if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) {
41 printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n",
42 (unsigned long long)bh_blocknr(buf),
43 (unsigned long long)btrfs_header_blocknr(&node->header));
49 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
51 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
52 int blockbits = root->fs_info->sb->s_blocksize_bits;
53 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
55 struct buffer_head *bh;
56 struct buffer_head *head;
57 struct buffer_head *ret = NULL;
60 page = find_lock_page(mapping, index);
64 if (!page_has_buffers(page))
67 head = page_buffers(page);
70 if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) {
79 page_cache_release(page);
83 int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh,
89 set_buffer_mapped(bh);
91 map_bh(bh, root->fs_info->sb, logical);
96 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
99 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
100 int blockbits = root->fs_info->sb->s_blocksize_bits;
101 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
103 struct buffer_head *bh;
104 struct buffer_head *head;
105 struct buffer_head *ret = NULL;
107 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
109 page = find_or_create_page(mapping, index, GFP_NOFS);
113 if (!page_has_buffers(page))
114 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
115 head = page_buffers(page);
118 if (!buffer_mapped(bh)) {
119 err = btrfs_map_bh_to_logical(root, bh, first_block);
122 if (bh_blocknr(bh) == blocknr) {
127 bh = bh->b_this_page;
129 } while (bh != head);
134 page_cache_release(page);
138 static int btree_get_block(struct inode *inode, sector_t iblock,
139 struct buffer_head *bh, int create)
142 struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root;
143 err = btrfs_map_bh_to_logical(root, bh, iblock);
147 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
150 struct scatterlist sg;
151 struct crypto_hash *tfm = root->fs_info->hash_tfm;
152 struct hash_desc desc;
157 sg_init_one(&sg, data, len);
158 spin_lock_irq(&root->fs_info->hash_lock);
159 ret = crypto_hash_digest(&desc, &sg, 1, result);
160 spin_unlock_irq(&root->fs_info->hash_lock);
162 printk("digest failed\n");
166 static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
169 char result[BTRFS_CRC32_SIZE];
171 struct btrfs_node *node;
173 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
174 bh->b_size - BTRFS_CSUM_SIZE, result);
178 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
179 printk("btrfs: %s checksum verify failed on %llu\n",
180 root->fs_info->sb->s_id,
181 (unsigned long long)bh_blocknr(bh));
185 node = btrfs_buffer_node(bh);
186 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
191 static int btree_writepage(struct page *page, struct writeback_control *wbc)
193 struct buffer_head *bh;
194 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
195 struct buffer_head *head;
196 if (!page_has_buffers(page)) {
197 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
198 (1 << BH_Dirty)|(1 << BH_Uptodate));
200 head = page_buffers(page);
203 if (buffer_dirty(bh))
204 csum_tree_block(root, bh, 0);
205 bh = bh->b_this_page;
206 } while (bh != head);
207 return block_write_full_page(page, btree_get_block, wbc);
210 static int btree_readpage(struct file * file, struct page * page)
212 return block_read_full_page(page, btree_get_block);
215 static struct address_space_operations btree_aops = {
216 .readpage = btree_readpage,
217 .writepage = btree_writepage,
218 .sync_page = block_sync_page,
221 int readahead_tree_block(struct btrfs_root *root, u64 blocknr)
223 struct buffer_head *bh = NULL;
226 bh = btrfs_find_create_tree_block(root, blocknr);
229 if (buffer_uptodate(bh)) {
233 if (test_set_buffer_locked(bh)) {
237 if (!buffer_uptodate(bh)) {
239 bh->b_end_io = end_buffer_read_sync;
250 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
252 struct buffer_head *bh = NULL;
254 bh = btrfs_find_create_tree_block(root, blocknr);
257 if (buffer_uptodate(bh))
260 if (!buffer_uptodate(bh)) {
262 bh->b_end_io = end_buffer_read_sync;
265 if (!buffer_uptodate(bh))
271 if (!buffer_checked(bh)) {
272 csum_tree_block(root, bh, 1);
273 set_buffer_checked(bh);
275 if (check_tree_block(root, bh))
283 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
284 struct buffer_head *buf)
286 WARN_ON(atomic_read(&buf->b_count) == 0);
287 mark_buffer_dirty(buf);
291 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
292 struct buffer_head *buf)
294 WARN_ON(atomic_read(&buf->b_count) == 0);
295 clear_buffer_dirty(buf);
299 static int __setup_root(int blocksize,
300 struct btrfs_root *root,
301 struct btrfs_fs_info *fs_info,
306 root->commit_root = NULL;
307 root->blocksize = blocksize;
309 root->fs_info = fs_info;
310 root->objectid = objectid;
311 root->last_trans = 0;
312 root->highest_inode = 0;
313 root->last_inode_alloc = 0;
314 memset(&root->root_key, 0, sizeof(root->root_key));
315 memset(&root->root_item, 0, sizeof(root->root_item));
316 root->root_key.objectid = objectid;
320 static int find_and_setup_root(int blocksize,
321 struct btrfs_root *tree_root,
322 struct btrfs_fs_info *fs_info,
324 struct btrfs_root *root)
328 __setup_root(blocksize, root, fs_info, objectid);
329 ret = btrfs_find_last_root(tree_root, objectid,
330 &root->root_item, &root->root_key);
333 root->node = read_tree_block(root,
334 btrfs_root_blocknr(&root->root_item));
339 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
340 struct btrfs_key *location)
342 struct btrfs_root *root;
343 struct btrfs_root *tree_root = fs_info->tree_root;
344 struct btrfs_path *path;
345 struct btrfs_leaf *l;
349 root = radix_tree_lookup(&fs_info->fs_roots_radix,
350 (unsigned long)location->objectid);
353 root = kmalloc(sizeof(*root), GFP_NOFS);
355 return ERR_PTR(-ENOMEM);
356 if (location->offset == (u64)-1) {
357 ret = find_and_setup_root(fs_info->sb->s_blocksize,
358 fs_info->tree_root, fs_info,
359 location->objectid, root);
367 __setup_root(fs_info->sb->s_blocksize, root, fs_info,
370 path = btrfs_alloc_path();
372 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
378 l = btrfs_buffer_leaf(path->nodes[0]);
379 memcpy(&root->root_item,
380 btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item),
381 sizeof(root->root_item));
382 memcpy(&root->root_key, location, sizeof(*location));
385 btrfs_release_path(root, path);
386 btrfs_free_path(path);
391 root->node = read_tree_block(root,
392 btrfs_root_blocknr(&root->root_item));
396 ret = radix_tree_insert(&fs_info->fs_roots_radix,
397 (unsigned long)root->root_key.objectid,
404 ret = btrfs_find_highest_inode(root, &highest_inode);
406 root->highest_inode = highest_inode;
407 root->last_inode_alloc = highest_inode;
412 struct btrfs_root *open_ctree(struct super_block *sb)
414 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
416 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
418 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
422 struct btrfs_super_block *disk_super;
424 if (!extent_root || !tree_root || !fs_info) {
428 init_bit_radix(&fs_info->pinned_radix);
429 init_bit_radix(&fs_info->pending_del_radix);
430 init_bit_radix(&fs_info->extent_map_radix);
431 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
432 INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL);
433 INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL);
434 INIT_LIST_HEAD(&fs_info->trans_list);
435 INIT_LIST_HEAD(&fs_info->dead_roots);
436 sb_set_blocksize(sb, 4096);
437 fs_info->running_transaction = NULL;
438 fs_info->tree_root = tree_root;
439 fs_info->extent_root = extent_root;
441 fs_info->btree_inode = new_inode(sb);
442 fs_info->btree_inode->i_ino = 1;
443 fs_info->btree_inode->i_nlink = 1;
444 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
445 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
446 fs_info->do_barriers = 1;
447 fs_info->extent_tree_insert_nr = 0;
448 fs_info->extent_tree_prealloc_nr = 0;
449 fs_info->closing = 0;
451 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
452 BTRFS_I(fs_info->btree_inode)->root = tree_root;
453 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
454 sizeof(struct btrfs_key));
455 insert_inode_hash(fs_info->btree_inode);
456 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
457 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
458 spin_lock_init(&fs_info->hash_lock);
460 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
461 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
465 mutex_init(&fs_info->trans_mutex);
466 mutex_init(&fs_info->fs_mutex);
468 __setup_root(sb->s_blocksize, tree_root,
469 fs_info, BTRFS_ROOT_TREE_OBJECTID);
471 fs_info->sb_buffer = read_tree_block(tree_root,
472 BTRFS_SUPER_INFO_OFFSET /
475 if (!fs_info->sb_buffer)
477 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
479 if (!btrfs_super_root(disk_super))
482 i_size_write(fs_info->btree_inode,
483 btrfs_super_total_blocks(disk_super) <<
484 fs_info->btree_inode->i_blkbits);
486 fs_info->disk_super = disk_super;
488 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
489 sizeof(disk_super->magic))) {
490 printk("btrfs: valid FS not found on %s\n", sb->s_id);
493 tree_root->node = read_tree_block(tree_root,
494 btrfs_super_root(disk_super));
495 if (!tree_root->node)
498 mutex_lock(&fs_info->fs_mutex);
499 ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
500 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
502 mutex_unlock(&fs_info->fs_mutex);
506 btrfs_read_block_groups(extent_root);
508 fs_info->generation = btrfs_super_generation(disk_super) + 1;
509 mutex_unlock(&fs_info->fs_mutex);
513 btrfs_block_release(tree_root, tree_root->node);
515 btrfs_block_release(tree_root, fs_info->sb_buffer);
517 iput(fs_info->btree_inode);
525 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
529 struct buffer_head *bh = root->fs_info->sb_buffer;
531 btrfs_set_super_root(root->fs_info->disk_super,
532 bh_blocknr(root->fs_info->tree_root->node));
534 WARN_ON(atomic_read(&bh->b_count) < 1);
535 clear_buffer_dirty(bh);
536 csum_tree_block(root, bh, 0);
537 bh->b_end_io = end_buffer_write_sync;
539 if (root->fs_info->do_barriers)
540 ret = submit_bh(WRITE_BARRIER, bh);
542 ret = submit_bh(WRITE, bh);
543 if (ret == -EOPNOTSUPP) {
546 set_buffer_uptodate(bh);
547 root->fs_info->do_barriers = 0;
548 ret = submit_bh(WRITE, bh);
551 if (!buffer_uptodate(bh)) {
558 static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
560 radix_tree_delete(&fs_info->fs_roots_radix,
561 (unsigned long)root->root_key.objectid);
566 if (root->commit_root)
567 brelse(root->commit_root);
572 static int del_fs_roots(struct btrfs_fs_info *fs_info)
575 struct btrfs_root *gang[8];
579 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
584 for (i = 0; i < ret; i++)
585 free_fs_root(fs_info, gang[i]);
590 int close_ctree(struct btrfs_root *root)
593 struct btrfs_trans_handle *trans;
594 struct btrfs_fs_info *fs_info = root->fs_info;
596 fs_info->closing = 1;
597 btrfs_transaction_flush_work(root);
598 mutex_lock(&fs_info->fs_mutex);
599 trans = btrfs_start_transaction(root, 1);
600 btrfs_commit_transaction(trans, root);
601 /* run commit again to drop the original snapshot */
602 trans = btrfs_start_transaction(root, 1);
603 btrfs_commit_transaction(trans, root);
604 ret = btrfs_write_and_wait_transaction(NULL, root);
606 write_ctree_super(NULL, root);
607 mutex_unlock(&fs_info->fs_mutex);
609 if (fs_info->extent_root->node)
610 btrfs_block_release(fs_info->extent_root,
611 fs_info->extent_root->node);
612 if (fs_info->tree_root->node)
613 btrfs_block_release(fs_info->tree_root,
614 fs_info->tree_root->node);
615 btrfs_block_release(root, fs_info->sb_buffer);
616 crypto_free_hash(fs_info->hash_tfm);
617 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
618 iput(fs_info->btree_inode);
620 btrfs_free_block_groups(root->fs_info);
621 del_fs_roots(fs_info);
622 kfree(fs_info->extent_root);
623 kfree(fs_info->tree_root);
627 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
632 void btrfs_btree_balance_dirty(struct btrfs_root *root)
634 balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping);