Btrfs: corruption hunt continues
[linux-block.git] / fs / btrfs / disk-io.c
CommitLineData
e20d96d6
CM
1#include <linux/module.h>
2#include <linux/fs.h>
d98237b3 3#include <linux/blkdev.h>
87cbda5c
CM
4#include <linux/crypto.h>
5#include <linux/scatterlist.h>
22b0ebda 6#include <linux/swap.h>
eb60ceac
CM
7#include "ctree.h"
8#include "disk-io.h"
e089f05c 9#include "transaction.h"
eb60ceac 10
d6025579
CM
11#define PATTERN 0xDEADBEEFUL
12static inline void check_pattern(struct buffer_head *buf)
13{
14 if (buf->b_private != (void *)PATTERN)
15 WARN_ON(1);
16}
17
18static inline void set_pattern(struct buffer_head *buf)
19{
20 buf->b_private = (void *)PATTERN;
21}
d98237b3 22
e20d96d6 23static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 24{
e20d96d6 25 struct btrfs_node *node = btrfs_buffer_node(buf);
d98237b3 26 if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
9a8dd150 27 BUG();
d98237b3 28 }
e20d96d6 29 if (root->node && btrfs_header_parentid(&node->header) !=
df2ce34c 30 btrfs_header_parentid(btrfs_buffer_header(root->node))) {
7f5c1516 31 BUG();
df2ce34c 32 }
9a8dd150 33 return 0;
eb60ceac
CM
34}
35
d98237b3
CM
36struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
37{
38 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
39 int blockbits = root->fs_info->sb->s_blocksize_bits;
40 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
41 struct page *page;
42 struct buffer_head *bh;
43 struct buffer_head *head;
44 struct buffer_head *ret = NULL;
45
46 page = find_lock_page(mapping, index);
47 if (!page)
48 return NULL;
49
50 if (!page_has_buffers(page))
51 goto out_unlock;
52
53 head = page_buffers(page);
54 bh = head;
55 do {
56 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
57 ret = bh;
58 get_bh(bh);
59 goto out_unlock;
60 }
61 bh = bh->b_this_page;
62 } while (bh != head);
63out_unlock:
64 unlock_page(page);
d6025579 65 if (ret) {
22b0ebda 66 touch_buffer(ret);
d6025579
CM
67 check_pattern(ret);
68 }
d98237b3
CM
69 page_cache_release(page);
70 return ret;
71}
72
73struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
74 u64 blocknr)
75{
76 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
77 int blockbits = root->fs_info->sb->s_blocksize_bits;
78 unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
79 struct page *page;
80 struct buffer_head *bh;
81 struct buffer_head *head;
82 struct buffer_head *ret = NULL;
83 u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
22b0ebda 84
d98237b3
CM
85 page = grab_cache_page(mapping, index);
86 if (!page)
87 return NULL;
88
d98237b3
CM
89 if (!page_has_buffers(page))
90 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
91 head = page_buffers(page);
92 bh = head;
93 do {
94 if (!buffer_mapped(bh)) {
95 bh->b_bdev = root->fs_info->sb->s_bdev;
96 bh->b_blocknr = first_block;
97 set_buffer_mapped(bh);
d6025579 98 set_pattern(bh);
d98237b3
CM
99 }
100 if (bh->b_blocknr == blocknr) {
101 ret = bh;
102 get_bh(bh);
103 goto out_unlock;
104 }
105 bh = bh->b_this_page;
106 first_block++;
107 } while (bh != head);
108out_unlock:
109 unlock_page(page);
22b0ebda
CM
110 if (ret)
111 touch_buffer(ret);
d98237b3
CM
112 page_cache_release(page);
113 return ret;
114}
115
116static sector_t max_block(struct block_device *bdev)
117{
118 sector_t retval = ~((sector_t)0);
119 loff_t sz = i_size_read(bdev->bd_inode);
120
121 if (sz) {
122 unsigned int size = block_size(bdev);
123 unsigned int sizebits = blksize_bits(size);
124 retval = (sz >> sizebits);
125 }
126 return retval;
127}
128
129static int btree_get_block(struct inode *inode, sector_t iblock,
130 struct buffer_head *bh, int create)
131{
132 if (iblock >= max_block(inode->i_sb->s_bdev)) {
133 if (create)
134 return -EIO;
135
136 /*
137 * for reads, we're just trying to fill a partial page.
138 * return a hole, they will have to call get_block again
139 * before they can fill it, and they will get -EIO at that
140 * time
141 */
142 return 0;
143 }
144 bh->b_bdev = inode->i_sb->s_bdev;
145 bh->b_blocknr = iblock;
146 set_buffer_mapped(bh);
147 return 0;
148}
149
f254e52c
CM
150int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
151 char *result)
87cbda5c 152{
87cbda5c
CM
153 struct scatterlist sg;
154 struct crypto_hash *tfm = root->fs_info->hash_tfm;
155 struct hash_desc desc;
156 int ret;
87cbda5c
CM
157
158 desc.tfm = tfm;
159 desc.flags = 0;
f254e52c 160 sg_init_one(&sg, data, len);
87cbda5c 161 spin_lock(&root->fs_info->hash_lock);
22b0ebda 162 ret = crypto_hash_digest(&desc, &sg, 1, result);
87cbda5c
CM
163 spin_unlock(&root->fs_info->hash_lock);
164 if (ret) {
165 printk("sha256 digest failed\n");
166 }
f254e52c
CM
167 return ret;
168}
169static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
170 int verify)
171{
172 char result[BTRFS_CSUM_SIZE];
173 int ret;
174 struct btrfs_node *node;
175
22b0ebda 176 return 0;
f254e52c
CM
177 ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE,
178 bh->b_size - BTRFS_CSUM_SIZE, result);
179 if (ret)
180 return ret;
87cbda5c 181 if (verify) {
f254e52c
CM
182 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) {
183 printk("checksum verify failed on %lu\n",
184 bh->b_blocknr);
185 return 1;
186 }
187 } else {
188 node = btrfs_buffer_node(bh);
22b0ebda 189 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE);
f254e52c 190 }
87cbda5c
CM
191 return 0;
192}
193
d98237b3 194static int btree_writepage(struct page *page, struct writeback_control *wbc)
ed2ff2cb 195{
22b0ebda 196#if 0
87cbda5c
CM
197 struct buffer_head *bh;
198 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
199 struct buffer_head *head;
87cbda5c
CM
200 if (!page_has_buffers(page)) {
201 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
202 (1 << BH_Dirty)|(1 << BH_Uptodate));
203 }
204 head = page_buffers(page);
205 bh = head;
206 do {
207 if (buffer_dirty(bh))
208 csum_tree_block(root, bh, 0);
209 bh = bh->b_this_page;
210 } while (bh != head);
22b0ebda 211#endif
d98237b3 212 return block_write_full_page(page, btree_get_block, wbc);
ed2ff2cb
CM
213}
214
d98237b3 215static int btree_readpage(struct file * file, struct page * page)
eb60ceac 216{
d98237b3 217 return block_read_full_page(page, btree_get_block);
eb60ceac
CM
218}
219
d98237b3
CM
220static struct address_space_operations btree_aops = {
221 .readpage = btree_readpage,
222 .writepage = btree_writepage,
223 .sync_page = block_sync_page,
224};
225
e20d96d6 226struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
eb60ceac 227{
d98237b3 228 struct buffer_head *bh = NULL;
eb60ceac 229
d98237b3
CM
230 bh = btrfs_find_create_tree_block(root, blocknr);
231 if (!bh)
232 return bh;
233 lock_buffer(bh);
234 if (!buffer_uptodate(bh)) {
235 get_bh(bh);
236 bh->b_end_io = end_buffer_read_sync;
237 submit_bh(READ, bh);
238 wait_on_buffer(bh);
239 if (!buffer_uptodate(bh))
240 goto fail;
87cbda5c 241 csum_tree_block(root, bh, 1);
d6025579 242 set_pattern(bh);
d98237b3
CM
243 } else {
244 unlock_buffer(bh);
245 }
246 if (check_tree_block(root, bh))
cfaa7295 247 BUG();
d98237b3
CM
248 return bh;
249fail:
250 brelse(bh);
251 return NULL;
252
eb60ceac
CM
253}
254
e089f05c 255int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 256 struct buffer_head *buf)
ed2ff2cb 257{
d6025579 258 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 259 mark_buffer_dirty(buf);
ed2ff2cb
CM
260 return 0;
261}
262
e089f05c 263int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
e20d96d6 264 struct buffer_head *buf)
ed2ff2cb 265{
d6025579 266 WARN_ON(atomic_read(&buf->b_count) == 0);
e20d96d6 267 clear_buffer_dirty(buf);
ed2ff2cb
CM
268 return 0;
269}
270
123abc88 271static int __setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
272 struct btrfs_root *root,
273 struct btrfs_fs_info *fs_info,
e20d96d6 274 u64 objectid)
d97e63b6 275{
cfaa7295 276 root->node = NULL;
a28ec197 277 root->commit_root = NULL;
123abc88
CM
278 root->blocksize = btrfs_super_blocksize(super);
279 root->ref_cows = 0;
9f5fae2f 280 root->fs_info = fs_info;
3768f368
CM
281 memset(&root->root_key, 0, sizeof(root->root_key));
282 memset(&root->root_item, 0, sizeof(root->root_item));
283 return 0;
284}
285
123abc88 286static int find_and_setup_root(struct btrfs_super_block *super,
9f5fae2f
CM
287 struct btrfs_root *tree_root,
288 struct btrfs_fs_info *fs_info,
289 u64 objectid,
e20d96d6 290 struct btrfs_root *root)
3768f368
CM
291{
292 int ret;
293
e20d96d6 294 __setup_root(super, root, fs_info, objectid);
3768f368
CM
295 ret = btrfs_find_last_root(tree_root, objectid,
296 &root->root_item, &root->root_key);
297 BUG_ON(ret);
298
299 root->node = read_tree_block(root,
300 btrfs_root_blocknr(&root->root_item));
3768f368 301 BUG_ON(!root->node);
d97e63b6
CM
302 return 0;
303}
304
e20d96d6
CM
305struct btrfs_root *open_ctree(struct super_block *sb,
306 struct buffer_head *sb_buffer,
307 struct btrfs_super_block *disk_super)
2e635a27 308{
e20d96d6
CM
309 struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
310 GFP_NOFS);
311 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
312 GFP_NOFS);
313 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
314 GFP_NOFS);
315 struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
316 GFP_NOFS);
317 struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
318 GFP_NOFS);
eb60ceac
CM
319 int ret;
320
87cbda5c 321 if (!btrfs_super_root(disk_super)) {
e20d96d6 322 return NULL;
87cbda5c 323 }
8ef97622
CM
324 init_bit_radix(&fs_info->pinned_radix);
325 init_bit_radix(&fs_info->pending_del_radix);
d98237b3 326 sb_set_blocksize(sb, sb_buffer->b_size);
9f5fae2f
CM
327 fs_info->running_transaction = NULL;
328 fs_info->fs_root = root;
329 fs_info->tree_root = tree_root;
330 fs_info->extent_root = extent_root;
331 fs_info->inode_root = inode_root;
332 fs_info->last_inode_alloc = 0;
333 fs_info->last_inode_alloc_dirid = 0;
e20d96d6 334 fs_info->disk_super = disk_super;
e20d96d6 335 fs_info->sb = sb;
d98237b3
CM
336 fs_info->btree_inode = new_inode(sb);
337 fs_info->btree_inode->i_ino = 1;
338 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
339 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
22b0ebda
CM
340 insert_inode_hash(fs_info->btree_inode);
341
d98237b3 342 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
87cbda5c 343 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
30ae8467
CM
344 spin_lock_init(&fs_info->hash_lock);
345
346 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
87cbda5c
CM
347 printk("failed to allocate sha256 hash\n");
348 return NULL;
349 }
d98237b3 350
79154b1b 351 mutex_init(&fs_info->trans_mutex);
d561c025 352 mutex_init(&fs_info->fs_mutex);
9f5fae2f
CM
353 memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
354 memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
3768f368 355
e20d96d6 356 __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
d98237b3
CM
357
358 fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
359
87cbda5c
CM
360 if (!fs_info->sb_buffer) {
361printk("failed2\n");
d98237b3 362 return NULL;
87cbda5c 363 }
d98237b3
CM
364 brelse(sb_buffer);
365 sb_buffer = NULL;
366 disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
367 fs_info->disk_super = disk_super;
368
e20d96d6
CM
369 tree_root->node = read_tree_block(tree_root,
370 btrfs_super_root(disk_super));
3768f368
CM
371 BUG_ON(!tree_root->node);
372
e20d96d6
CM
373 ret = find_and_setup_root(disk_super, tree_root, fs_info,
374 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
3768f368
CM
375 BUG_ON(ret);
376
e20d96d6
CM
377 ret = find_and_setup_root(disk_super, tree_root, fs_info,
378 BTRFS_INODE_MAP_OBJECTID, inode_root);
9f5fae2f
CM
379 BUG_ON(ret);
380
e20d96d6
CM
381 ret = find_and_setup_root(disk_super, tree_root, fs_info,
382 BTRFS_FS_TREE_OBJECTID, root);
3768f368 383 BUG_ON(ret);
a28ec197 384 root->commit_root = root->node;
e20d96d6 385 get_bh(root->node);
3768f368 386 root->ref_cows = 1;
293ffd5f 387 root->fs_info->generation = root->root_key.offset + 1;
eb60ceac
CM
388 return root;
389}
390
e089f05c 391int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
79154b1b 392 *root)
eb60ceac 393{
d5719762
CM
394 struct buffer_head *bh = root->fs_info->sb_buffer;
395 btrfs_set_super_root(root->fs_info->disk_super,
396 root->fs_info->tree_root->node->b_blocknr);
397 lock_buffer(bh);
398 clear_buffer_dirty(bh);
87cbda5c 399 csum_tree_block(root, bh, 0);
d5719762
CM
400 bh->b_end_io = end_buffer_write_sync;
401 get_bh(bh);
402 submit_bh(WRITE, bh);
403 wait_on_buffer(bh);
404 if (!buffer_uptodate(bh)) {
405 WARN_ON(1);
406 return -EIO;
cfaa7295
CM
407 }
408 return 0;
409}
410
e20d96d6 411int close_ctree(struct btrfs_root *root)
cfaa7295 412{
3768f368 413 int ret;
e089f05c
CM
414 struct btrfs_trans_handle *trans;
415
79154b1b
CM
416 trans = btrfs_start_transaction(root, 1);
417 btrfs_commit_transaction(trans, root);
418 /* run commit again to drop the original snapshot */
419 trans = btrfs_start_transaction(root, 1);
420 btrfs_commit_transaction(trans, root);
421 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 422 BUG_ON(ret);
79154b1b 423 write_ctree_super(NULL, root);
ed2ff2cb 424
cfaa7295 425 if (root->node)
234b63a0 426 btrfs_block_release(root, root->node);
9f5fae2f
CM
427 if (root->fs_info->extent_root->node)
428 btrfs_block_release(root->fs_info->extent_root,
429 root->fs_info->extent_root->node);
430 if (root->fs_info->inode_root->node)
431 btrfs_block_release(root->fs_info->inode_root,
432 root->fs_info->inode_root->node);
433 if (root->fs_info->tree_root->node)
434 btrfs_block_release(root->fs_info->tree_root,
435 root->fs_info->tree_root->node);
234b63a0 436 btrfs_block_release(root, root->commit_root);
e20d96d6 437 btrfs_block_release(root, root->fs_info->sb_buffer);
87cbda5c 438 crypto_free_hash(root->fs_info->hash_tfm);
30ae8467 439 truncate_inode_pages(root->fs_info->btree_inode->i_mapping, 0);
d98237b3 440 iput(root->fs_info->btree_inode);
e20d96d6
CM
441 kfree(root->fs_info->extent_root);
442 kfree(root->fs_info->inode_root);
443 kfree(root->fs_info->tree_root);
444 kfree(root->fs_info);
445 kfree(root);
eb60ceac
CM
446 return 0;
447}
448
e20d96d6 449void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
eb60ceac 450{
d6025579 451 check_pattern(buf);
e20d96d6 452 brelse(buf);
eb60ceac
CM
453}
454