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>
9 #include "transaction.h"
12 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
14 struct btrfs_node *node = btrfs_buffer_node(buf);
15 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
18 if (root->node && btrfs_header_parentid(&node->header) !=
19 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
25 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
27 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
28 int blockbits = root->fs_info->sb->s_blocksize_bits;
29 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
31 struct buffer_head *bh;
32 struct buffer_head *head;
33 struct buffer_head *ret = NULL;
35 page = find_lock_page(mapping, index);
39 if (!page_has_buffers(page))
42 head = page_buffers(page);
45 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
56 page_cache_release(page);
60 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
63 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
64 int blockbits = root->fs_info->sb->s_blocksize_bits;
65 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
67 struct buffer_head *bh;
68 struct buffer_head *head;
69 struct buffer_head *ret = NULL;
70 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
72 page = grab_cache_page(mapping, index);
76 if (!page_has_buffers(page))
77 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
78 head = page_buffers(page);
81 if (!buffer_mapped(bh)) {
82 bh->b_bdev = root->fs_info->sb->s_bdev;
83 bh->b_blocknr = first_block;
84 set_buffer_mapped(bh);
86 if (bh->b_blocknr == blocknr) {
98 page_cache_release(page);
102 static sector_t max_block(struct block_device *bdev)
104 sector_t retval = ~((sector_t)0);
105 loff_t sz = i_size_read(bdev->bd_inode);
108 unsigned int size = block_size(bdev);
109 unsigned int sizebits = blksize_bits(size);
110 retval = (sz >> sizebits);
115 static int btree_get_block(struct inode *inode, sector_t iblock,
116 struct buffer_head *bh, int create)
118 if (iblock >= max_block(inode->i_sb->s_bdev)) {
123 * for reads, we're just trying to fill a partial page.
124 * return a hole, they will have to call get_block again
125 * before they can fill it, and they will get -EIO at that
130 bh->b_bdev = inode->i_sb->s_bdev;
131 bh->b_blocknr = iblock;
132 set_buffer_mapped(bh);
136 int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
139 struct scatterlist sg;
140 struct crypto_hash *tfm = root->fs_info->hash_tfm;
141 struct hash_desc desc;
146 sg_init_one(&sg, data, len);
147 spin_lock(&root->fs_info->hash_lock);
148 ret = crypto_hash_digest(&desc, &sg, 1, result);
149 spin_unlock(&root->fs_info->hash_lock);
151 printk("sha256 digest failed\n");
155 static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
158 char result[BTRFS_CSUM_SIZE];
160 struct btrfs_node *node;
163 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
164 bh->b_size - BTRFS_CSUM_SIZE, result);
168 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
169 printk("checksum verify failed on %lu\n",
174 node = btrfs_buffer_node(bh);
175 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
180 static int btree_writepage(struct page *page, struct writeback_control *wbc)
183 struct buffer_head *bh;
184 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
185 struct buffer_head *head;
186 if (!page_has_buffers(page)) {
187 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
188 (1 << BH_Dirty)|(1 << BH_Uptodate));
190 head = page_buffers(page);
193 if (buffer_dirty(bh))
194 csum_tree_block(root, bh, 0);
195 bh = bh->b_this_page;
196 } while (bh != head);
198 return block_write_full_page(page, btree_get_block, wbc);
201 static int btree_readpage(struct file * file, struct page * page)
203 return block_read_full_page(page, btree_get_block);
206 static struct address_space_operations btree_aops = {
207 .readpage = btree_readpage,
208 .writepage = btree_writepage,
209 .sync_page = block_sync_page,
212 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
214 struct buffer_head *bh = NULL;
216 bh = btrfs_find_create_tree_block(root, blocknr);
220 if (!buffer_uptodate(bh)) {
222 bh->b_end_io = end_buffer_read_sync;
225 if (!buffer_uptodate(bh))
227 csum_tree_block(root, bh, 1);
231 if (check_tree_block(root, bh))
240 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
241 struct buffer_head *buf)
243 mark_buffer_dirty(buf);
247 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
248 struct buffer_head *buf)
250 clear_buffer_dirty(buf);
254 static int __setup_root(struct btrfs_super_block *super,
255 struct btrfs_root *root,
256 struct btrfs_fs_info *fs_info,
260 root->commit_root = NULL;
261 root->blocksize = btrfs_super_blocksize(super);
263 root->fs_info = fs_info;
264 memset(&root->root_key, 0, sizeof(root->root_key));
265 memset(&root->root_item, 0, sizeof(root->root_item));
269 static int find_and_setup_root(struct btrfs_super_block *super,
270 struct btrfs_root *tree_root,
271 struct btrfs_fs_info *fs_info,
273 struct btrfs_root *root)
277 __setup_root(super, root, fs_info, objectid);
278 ret = btrfs_find_last_root(tree_root, objectid,
279 &root->root_item, &root->root_key);
282 root->node = read_tree_block(root,
283 btrfs_root_blocknr(&root->root_item));
288 struct btrfs_root *open_ctree(struct super_block *sb,
289 struct buffer_head *sb_buffer,
290 struct btrfs_super_block *disk_super)
292 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
294 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
296 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
298 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
300 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
304 if (!btrfs_super_root(disk_super)) {
307 init_bit_radix(&fs_info->pinned_radix);
308 init_bit_radix(&fs_info->pending_del_radix);
309 sb_set_blocksize(sb, sb_buffer->b_size);
310 fs_info->running_transaction = NULL;
311 fs_info->fs_root = root;
312 fs_info->tree_root = tree_root;
313 fs_info->extent_root = extent_root;
314 fs_info->inode_root = inode_root;
315 fs_info->last_inode_alloc = 0;
316 fs_info->last_inode_alloc_dirid = 0;
317 fs_info->disk_super = disk_super;
319 fs_info->btree_inode = new_inode(sb);
320 fs_info->btree_inode->i_ino = 1;
321 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
322 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
323 insert_inode_hash(fs_info->btree_inode);
325 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
326 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
327 spin_lock_init(&fs_info->hash_lock);
329 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
330 printk("failed to allocate sha256 hash\n");
334 mutex_init(&fs_info->trans_mutex);
335 mutex_init(&fs_info->fs_mutex);
336 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
337 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
339 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
341 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
343 if (!fs_info->sb_buffer) {
349 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
350 fs_info->disk_super = disk_super;
352 tree_root->node = read_tree_block(tree_root,
353 btrfs_super_root(disk_super));
354 BUG_ON(!tree_root->node);
356 ret = find_and_setup_root(disk_super, tree_root, fs_info,
357 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
360 ret = find_and_setup_root(disk_super, tree_root, fs_info,
361 BTRFS_INODE_MAP_OBJECTID, inode_root);
364 ret = find_and_setup_root(disk_super, tree_root, fs_info,
365 BTRFS_FS_TREE_OBJECTID, root);
367 root->commit_root = root->node;
370 root->fs_info->generation = root->root_key.offset + 1;
374 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
377 struct buffer_head *bh = root->fs_info->sb_buffer;
378 btrfs_set_super_root(root->fs_info->disk_super,
379 root->fs_info->tree_root->node->b_blocknr);
381 clear_buffer_dirty(bh);
382 csum_tree_block(root, bh, 0);
383 bh->b_end_io = end_buffer_write_sync;
385 submit_bh(WRITE, bh);
387 if (!buffer_uptodate(bh)) {
394 int close_ctree(struct btrfs_root *root)
397 struct btrfs_trans_handle *trans;
399 trans = btrfs_start_transaction(root, 1);
400 btrfs_commit_transaction(trans, root);
401 /* run commit again to drop the original snapshot */
402 trans = btrfs_start_transaction(root, 1);
403 btrfs_commit_transaction(trans, root);
404 ret = btrfs_write_and_wait_transaction(NULL, root);
406 write_ctree_super(NULL, root);
409 btrfs_block_release(root, root->node);
410 if (root->fs_info->extent_root->node)
411 btrfs_block_release(root->fs_info->extent_root,
412 root->fs_info->extent_root->node);
413 if (root->fs_info->inode_root->node)
414 btrfs_block_release(root->fs_info->inode_root,
415 root->fs_info->inode_root->node);
416 if (root->fs_info->tree_root->node)
417 btrfs_block_release(root->fs_info->tree_root,
418 root->fs_info->tree_root->node);
419 btrfs_block_release(root, root->commit_root);
420 btrfs_block_release(root, root->fs_info->sb_buffer);
421 crypto_free_hash(root->fs_info->hash_tfm);
422 truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
423 iput(root->fs_info->btree_inode);
424 kfree(root->fs_info->extent_root);
425 kfree(root->fs_info->inode_root);
426 kfree(root->fs_info->tree_root);
427 kfree(root->fs_info);
432 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)