blob: 2afb7922b06270448150791aef56c406a3427210 [file] [log] [blame]
Chris Masone20d96d2007-03-22 12:13:20 -04001#include <linux/module.h>
2#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04003#include <linux/blkdev.h>
Chris Mason87cbda52007-03-28 19:44:27 -04004#include <linux/crypto.h>
5#include <linux/scatterlist.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05006#include "ctree.h"
7#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04008#include "transaction.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05009
Chris Masond98237b2007-03-28 13:57:48 -040010
Chris Masone20d96d2007-03-22 12:13:20 -040011static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -050012{
Chris Masone20d96d2007-03-22 12:13:20 -040013 struct btrfs_node *node = btrfs_buffer_node(buf);
Chris Masond98237b2007-03-28 13:57:48 -040014 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
Chris Mason9a8dd152007-02-23 08:38:36 -050015 BUG();
Chris Masond98237b2007-03-28 13:57:48 -040016 }
Chris Masone20d96d2007-03-22 12:13:20 -040017 if (root->node && btrfs_header_parentid(&node->header) !=
Chris Masondf2ce342007-03-23 11:00:45 -040018 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
Chris Mason7f5c1512007-03-23 15:56:19 -040019 BUG();
Chris Masondf2ce342007-03-23 11:00:45 -040020 }
Chris Mason9a8dd152007-02-23 08:38:36 -050021 return 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050022}
23
Chris Masond98237b2007-03-28 13:57:48 -040024struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
Chris Masoned2ff2c2007-03-01 18:59:40 -050025{
Chris Masond98237b2007-03-28 13:57:48 -040026 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
27 int blockbits = root->fs_info->sb->s_blocksize_bits;
28 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
29 struct page *page;
30 struct buffer_head *bh;
31 struct buffer_head *head;
32 struct buffer_head *ret = NULL;
33
34 page = find_lock_page(mapping, index);
35 if (!page)
36 return NULL;
37
38 if (!page_has_buffers(page))
39 goto out_unlock;
40
41 head = page_buffers(page);
42 bh = head;
43 do {
44 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
45 ret = bh;
46 get_bh(bh);
47 goto out_unlock;
48 }
49 bh = bh->b_this_page;
50 } while (bh != head);
51out_unlock:
52 unlock_page(page);
53 page_cache_release(page);
54 return ret;
Chris Masoned2ff2c2007-03-01 18:59:40 -050055}
56
Chris Masond98237b2007-03-28 13:57:48 -040057struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
58 u64 blocknr)
Chris Masoneb60cea2007-02-02 09:18:22 -050059{
Chris Masond98237b2007-03-28 13:57:48 -040060 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
61 int blockbits = root->fs_info->sb->s_blocksize_bits;
62 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
63 struct page *page;
64 struct buffer_head *bh;
65 struct buffer_head *head;
66 struct buffer_head *ret = NULL;
67 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
68 page = grab_cache_page(mapping, index);
69 if (!page)
70 return NULL;
71
72 wait_on_page_writeback(page);
73 if (!page_has_buffers(page))
74 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
75 head = page_buffers(page);
76 bh = head;
77 do {
78 if (!buffer_mapped(bh)) {
79 bh->b_bdev = root->fs_info->sb->s_bdev;
80 bh->b_blocknr = first_block;
81 set_buffer_mapped(bh);
82 }
83 if (bh->b_blocknr == blocknr) {
84 ret = bh;
85 get_bh(bh);
86 goto out_unlock;
87 }
88 bh = bh->b_this_page;
89 first_block++;
90 } while (bh != head);
91out_unlock:
92 unlock_page(page);
93 page_cache_release(page);
94 return ret;
Chris Masone20d96d2007-03-22 12:13:20 -040095}
Chris Mason123abc82007-03-14 14:14:43 -040096
Chris Masond98237b2007-03-28 13:57:48 -040097static sector_t max_block(struct block_device *bdev)
98{
99 sector_t retval = ~((sector_t)0);
100 loff_t sz = i_size_read(bdev->bd_inode);
101
102 if (sz) {
103 unsigned int size = block_size(bdev);
104 unsigned int sizebits = blksize_bits(size);
105 retval = (sz >> sizebits);
106 }
107 return retval;
108}
109
110static int btree_get_block(struct inode *inode, sector_t iblock,
111 struct buffer_head *bh, int create)
112{
113 if (iblock >= max_block(inode->i_sb->s_bdev)) {
114 if (create)
115 return -EIO;
116
117 /*
118 * for reads, we're just trying to fill a partial page.
119 * return a hole, they will have to call get_block again
120 * before they can fill it, and they will get -EIO at that
121 * time
122 */
123 return 0;
124 }
125 bh->b_bdev = inode->i_sb->s_bdev;
126 bh->b_blocknr = iblock;
127 set_buffer_mapped(bh);
128 return 0;
129}
130
Chris Mason87cbda52007-03-28 19:44:27 -0400131static int csum_tree_block(struct btrfs_root * root, struct buffer_head *bh,
132 int verify)
133{
134 struct btrfs_node *node = btrfs_buffer_node(bh);
135 struct scatterlist sg;
136 struct crypto_hash *tfm = root->fs_info->hash_tfm;
137 struct hash_desc desc;
138 int ret;
139 char result[32];
140
141 desc.tfm = tfm;
142 desc.flags = 0;
143 sg_init_one(&sg, bh->b_data + 32, bh->b_size - 32);
144 spin_lock(&root->fs_info->hash_lock);
145 ret = crypto_hash_digest(&desc, &sg, bh->b_size - 32, result);
146 spin_unlock(&root->fs_info->hash_lock);
147 if (ret) {
148 printk("sha256 digest failed\n");
149 }
150 if (verify) {
151 if (memcmp(node->header.csum, result, sizeof(result)))
152 printk("csum verify failed on %Lu\n", bh->b_blocknr);
153 return -EINVAL;
154 } else
155 memcpy(node->header.csum, result, sizeof(node->header.csum));
156 return 0;
157}
158
Chris Masond98237b2007-03-28 13:57:48 -0400159static int btree_writepage(struct page *page, struct writeback_control *wbc)
160{
Chris Mason87cbda52007-03-28 19:44:27 -0400161 struct buffer_head *bh;
162 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
163 struct buffer_head *head;
164
165 if (!page_has_buffers(page)) {
166 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
167 (1 << BH_Dirty)|(1 << BH_Uptodate));
168 }
169 head = page_buffers(page);
170 bh = head;
171 do {
172 if (buffer_dirty(bh))
173 csum_tree_block(root, bh, 0);
174 bh = bh->b_this_page;
175 } while (bh != head);
Chris Masond98237b2007-03-28 13:57:48 -0400176 return block_write_full_page(page, btree_get_block, wbc);
177}
178
179static int btree_readpage(struct file * file, struct page * page)
180{
181 return block_read_full_page(page, btree_get_block);
182}
183
184static struct address_space_operations btree_aops = {
185 .readpage = btree_readpage,
186 .writepage = btree_writepage,
187 .sync_page = block_sync_page,
188};
189
Chris Masone20d96d2007-03-22 12:13:20 -0400190struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
191{
Chris Masond98237b2007-03-28 13:57:48 -0400192 struct buffer_head *bh = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400193
Chris Masond98237b2007-03-28 13:57:48 -0400194 bh = btrfs_find_create_tree_block(root, blocknr);
195 if (!bh)
196 return bh;
197 lock_buffer(bh);
198 if (!buffer_uptodate(bh)) {
199 get_bh(bh);
200 bh->b_end_io = end_buffer_read_sync;
201 submit_bh(READ, bh);
202 wait_on_buffer(bh);
203 if (!buffer_uptodate(bh))
204 goto fail;
Chris Mason87cbda52007-03-28 19:44:27 -0400205 csum_tree_block(root, bh, 1);
Chris Masond98237b2007-03-28 13:57:48 -0400206 } else {
207 unlock_buffer(bh);
208 }
209 if (check_tree_block(root, bh))
Chris Masoncfaa7292007-02-21 17:04:57 -0500210 BUG();
Chris Masond98237b2007-03-28 13:57:48 -0400211 return bh;
212fail:
213 brelse(bh);
214 return NULL;
215
Chris Masoneb60cea2007-02-02 09:18:22 -0500216}
217
Chris Masone089f052007-03-16 16:20:31 -0400218int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400219 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500220{
Chris Masone20d96d2007-03-22 12:13:20 -0400221 mark_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500222 return 0;
223}
224
Chris Masone089f052007-03-16 16:20:31 -0400225int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400226 struct buffer_head *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -0500227{
Chris Masone20d96d2007-03-22 12:13:20 -0400228 clear_buffer_dirty(buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500229 return 0;
230}
231
Chris Mason123abc82007-03-14 14:14:43 -0400232static int __setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400233 struct btrfs_root *root,
234 struct btrfs_fs_info *fs_info,
Chris Masone20d96d2007-03-22 12:13:20 -0400235 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -0500236{
Chris Masoncfaa7292007-02-21 17:04:57 -0500237 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -0500238 root->commit_root = NULL;
Chris Mason123abc82007-03-14 14:14:43 -0400239 root->blocksize = btrfs_super_blocksize(super);
240 root->ref_cows = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -0400241 root->fs_info = fs_info;
Chris Mason3768f362007-03-13 16:47:54 -0400242 memset(&root->root_key, 0, sizeof(root->root_key));
243 memset(&root->root_item, 0, sizeof(root->root_item));
244 return 0;
245}
246
Chris Mason123abc82007-03-14 14:14:43 -0400247static int find_and_setup_root(struct btrfs_super_block *super,
Chris Mason9f5fae22007-03-20 14:38:32 -0400248 struct btrfs_root *tree_root,
249 struct btrfs_fs_info *fs_info,
250 u64 objectid,
Chris Masone20d96d2007-03-22 12:13:20 -0400251 struct btrfs_root *root)
Chris Mason3768f362007-03-13 16:47:54 -0400252{
253 int ret;
254
Chris Masone20d96d2007-03-22 12:13:20 -0400255 __setup_root(super, root, fs_info, objectid);
Chris Mason3768f362007-03-13 16:47:54 -0400256 ret = btrfs_find_last_root(tree_root, objectid,
257 &root->root_item, &root->root_key);
258 BUG_ON(ret);
259
260 root->node = read_tree_block(root,
261 btrfs_root_blocknr(&root->root_item));
Chris Mason3768f362007-03-13 16:47:54 -0400262 BUG_ON(!root->node);
Chris Masond97e63b2007-02-20 16:40:44 -0500263 return 0;
264}
265
Chris Masone20d96d2007-03-22 12:13:20 -0400266struct btrfs_root *open_ctree(struct super_block *sb,
267 struct buffer_head *sb_buffer,
268 struct btrfs_super_block *disk_super)
Chris Masoneb60cea2007-02-02 09:18:22 -0500269{
Chris Masone20d96d2007-03-22 12:13:20 -0400270 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
271 GFP_NOFS);
272 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
273 GFP_NOFS);
274 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
275 GFP_NOFS);
276 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
277 GFP_NOFS);
278 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
279 GFP_NOFS);
Chris Masoneb60cea2007-02-02 09:18:22 -0500280 int ret;
281
Chris Mason87cbda52007-03-28 19:44:27 -0400282 if (!btrfs_super_root(disk_super)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400283 return NULL;
Chris Mason87cbda52007-03-28 19:44:27 -0400284 }
Chris Mason8ef97622007-03-26 10:15:30 -0400285 init_bit_radix(&fs_info->pinned_radix);
286 init_bit_radix(&fs_info->pending_del_radix);
Chris Masond98237b2007-03-28 13:57:48 -0400287 sb_set_blocksize(sb, sb_buffer->b_size);
Chris Mason9f5fae22007-03-20 14:38:32 -0400288 fs_info->running_transaction = NULL;
289 fs_info->fs_root = root;
290 fs_info->tree_root = tree_root;
291 fs_info->extent_root = extent_root;
292 fs_info->inode_root = inode_root;
293 fs_info->last_inode_alloc = 0;
294 fs_info->last_inode_alloc_dirid = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400295 fs_info->disk_super = disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400296 fs_info->sb = sb;
Chris Masond98237b2007-03-28 13:57:48 -0400297 fs_info->btree_inode = new_inode(sb);
298 fs_info->btree_inode->i_ino = 1;
299 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
300 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
301 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
Chris Mason87cbda52007-03-28 19:44:27 -0400302 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
303 if (!fs_info->hash_tfm) {
304 printk("failed to allocate sha256 hash\n");
305 return NULL;
306 }
307 spin_lock_init(&fs_info->hash_lock);
Chris Masond98237b2007-03-28 13:57:48 -0400308
Chris Mason79154b12007-03-22 15:59:16 -0400309 mutex_init(&fs_info->trans_mutex);
Chris Masond561c022007-03-23 19:47:49 -0400310 mutex_init(&fs_info->fs_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400311 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
312 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
Chris Mason3768f362007-03-13 16:47:54 -0400313
Chris Masone20d96d2007-03-22 12:13:20 -0400314 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Masond98237b2007-03-28 13:57:48 -0400315
316 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
317
Chris Mason87cbda52007-03-28 19:44:27 -0400318 if (!fs_info->sb_buffer) {
319printk("failed2\n");
Chris Masond98237b2007-03-28 13:57:48 -0400320 return NULL;
Chris Mason87cbda52007-03-28 19:44:27 -0400321 }
Chris Masond98237b2007-03-28 13:57:48 -0400322 brelse(sb_buffer);
323 sb_buffer = NULL;
324 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
325 fs_info->disk_super = disk_super;
326
Chris Masone20d96d2007-03-22 12:13:20 -0400327 tree_root->node = read_tree_block(tree_root,
328 btrfs_super_root(disk_super));
Chris Mason3768f362007-03-13 16:47:54 -0400329 BUG_ON(!tree_root->node);
330
Chris Masone20d96d2007-03-22 12:13:20 -0400331 ret = find_and_setup_root(disk_super, tree_root, fs_info,
332 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
Chris Mason3768f362007-03-13 16:47:54 -0400333 BUG_ON(ret);
334
Chris Masone20d96d2007-03-22 12:13:20 -0400335 ret = find_and_setup_root(disk_super, tree_root, fs_info,
336 BTRFS_INODE_MAP_OBJECTID, inode_root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400337 BUG_ON(ret);
338
Chris Masone20d96d2007-03-22 12:13:20 -0400339 ret = find_and_setup_root(disk_super, tree_root, fs_info,
340 BTRFS_FS_TREE_OBJECTID, root);
Chris Mason3768f362007-03-13 16:47:54 -0400341 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500342 root->commit_root = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400343 get_bh(root->node);
Chris Mason3768f362007-03-13 16:47:54 -0400344 root->ref_cows = 1;
Chris Mason293ffd52007-03-20 15:57:25 -0400345 root->fs_info->generation = root->root_key.offset + 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500346 return root;
347}
348
Chris Masone089f052007-03-16 16:20:31 -0400349int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason79154b12007-03-22 15:59:16 -0400350 *root)
Chris Masoncfaa7292007-02-21 17:04:57 -0500351{
Chris Masond5719762007-03-23 10:01:08 -0400352 struct buffer_head *bh = root->fs_info->sb_buffer;
353 btrfs_set_super_root(root->fs_info->disk_super,
354 root->fs_info->tree_root->node->b_blocknr);
355 lock_buffer(bh);
356 clear_buffer_dirty(bh);
Chris Mason87cbda52007-03-28 19:44:27 -0400357 csum_tree_block(root, bh, 0);
Chris Masond5719762007-03-23 10:01:08 -0400358 bh->b_end_io = end_buffer_write_sync;
359 get_bh(bh);
360 submit_bh(WRITE, bh);
361 wait_on_buffer(bh);
362 if (!buffer_uptodate(bh)) {
363 WARN_ON(1);
364 return -EIO;
Chris Masoncfaa7292007-02-21 17:04:57 -0500365 }
366 return 0;
367}
368
Chris Masone20d96d2007-03-22 12:13:20 -0400369int close_ctree(struct btrfs_root *root)
Chris Masoneb60cea2007-02-02 09:18:22 -0500370{
Chris Mason3768f362007-03-13 16:47:54 -0400371 int ret;
Chris Masone089f052007-03-16 16:20:31 -0400372 struct btrfs_trans_handle *trans;
373
Chris Mason79154b12007-03-22 15:59:16 -0400374 trans = btrfs_start_transaction(root, 1);
375 btrfs_commit_transaction(trans, root);
376 /* run commit again to drop the original snapshot */
377 trans = btrfs_start_transaction(root, 1);
378 btrfs_commit_transaction(trans, root);
379 ret = btrfs_write_and_wait_transaction(NULL, root);
Chris Mason9f5fae22007-03-20 14:38:32 -0400380 BUG_ON(ret);
Chris Mason79154b12007-03-22 15:59:16 -0400381 write_ctree_super(NULL, root);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500382
Chris Masoneb60cea2007-02-02 09:18:22 -0500383 if (root->node)
Chris Mason234b63a2007-03-13 10:46:10 -0400384 btrfs_block_release(root, root->node);
Chris Mason9f5fae22007-03-20 14:38:32 -0400385 if (root->fs_info->extent_root->node)
386 btrfs_block_release(root->fs_info->extent_root,
387 root->fs_info->extent_root->node);
388 if (root->fs_info->inode_root->node)
389 btrfs_block_release(root->fs_info->inode_root,
390 root->fs_info->inode_root->node);
391 if (root->fs_info->tree_root->node)
392 btrfs_block_release(root->fs_info->tree_root,
393 root->fs_info->tree_root->node);
Chris Mason234b63a2007-03-13 10:46:10 -0400394 btrfs_block_release(root, root->commit_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400395 btrfs_block_release(root, root->fs_info->sb_buffer);
Chris Mason87cbda52007-03-28 19:44:27 -0400396 crypto_free_hash(root->fs_info->hash_tfm);
Chris Masond98237b2007-03-28 13:57:48 -0400397 iput(root->fs_info->btree_inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400398 kfree(root->fs_info->extent_root);
399 kfree(root->fs_info->inode_root);
400 kfree(root->fs_info->tree_root);
401 kfree(root->fs_info);
402 kfree(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500403 return 0;
404}
405
Chris Masone20d96d2007-03-22 12:13:20 -0400406void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
Chris Masoneb60cea2007-02-02 09:18:22 -0500407{
Chris Masone20d96d2007-03-22 12:13:20 -0400408 brelse(buf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500409}
410