Btrfs: Add additional debugging for metadata checksum failures
[deliverable/linux.git] / fs / btrfs / disk-io.c
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
19 #include <linux/fs.h>
20 #include <linux/blkdev.h>
21 #include <linux/crc32c.h>
22 #include <linux/scatterlist.h>
23 #include <linux/swap.h>
24 #include <linux/radix-tree.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h> // for block_sync_page
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "volumes.h"
32 #include "print-tree.h"
33
34 #if 0
35 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
36 {
37 if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
38 printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf),
40 (unsigned long long)btrfs_header_blocknr(buf));
41 return 1;
42 }
43 return 0;
44 }
45 #endif
46
47 static struct extent_io_ops btree_extent_io_ops;
48
49 struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
50 size_t page_offset, u64 start, u64 len,
51 int create)
52 {
53 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
54 struct extent_map *em;
55 int ret;
56
57 again:
58 spin_lock(&em_tree->lock);
59 em = lookup_extent_mapping(em_tree, start, len);
60 spin_unlock(&em_tree->lock);
61 if (em) {
62 goto out;
63 }
64 em = alloc_extent_map(GFP_NOFS);
65 if (!em) {
66 em = ERR_PTR(-ENOMEM);
67 goto out;
68 }
69 em->start = 0;
70 em->len = i_size_read(inode);
71 em->block_start = 0;
72 em->bdev = inode->i_sb->s_bdev;
73
74 spin_lock(&em_tree->lock);
75 ret = add_extent_mapping(em_tree, em);
76 spin_unlock(&em_tree->lock);
77
78 if (ret == -EEXIST) {
79 free_extent_map(em);
80 em = NULL;
81 goto again;
82 } else if (ret) {
83 em = ERR_PTR(ret);
84 }
85 out:
86 return em;
87 }
88
89 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
90 {
91 return crc32c(seed, data, len);
92 }
93
94 void btrfs_csum_final(u32 crc, char *result)
95 {
96 *(__le32 *)result = ~cpu_to_le32(crc);
97 }
98
99 static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
100 int verify)
101 {
102 char result[BTRFS_CRC32_SIZE];
103 unsigned long len;
104 unsigned long cur_len;
105 unsigned long offset = BTRFS_CSUM_SIZE;
106 char *map_token = NULL;
107 char *kaddr;
108 unsigned long map_start;
109 unsigned long map_len;
110 int err;
111 u32 crc = ~(u32)0;
112
113 len = buf->len - offset;
114 while(len > 0) {
115 err = map_private_extent_buffer(buf, offset, 32,
116 &map_token, &kaddr,
117 &map_start, &map_len, KM_USER0);
118 if (err) {
119 printk("failed to map extent buffer! %lu\n",
120 offset);
121 return 1;
122 }
123 cur_len = min(len, map_len - (offset - map_start));
124 crc = btrfs_csum_data(root, kaddr + offset - map_start,
125 crc, cur_len);
126 len -= cur_len;
127 offset += cur_len;
128 unmap_extent_buffer(buf, map_token, KM_USER0);
129 }
130 btrfs_csum_final(crc, result);
131
132 if (verify) {
133 int from_this_trans = 0;
134
135 if (root->fs_info->running_transaction &&
136 btrfs_header_generation(buf) ==
137 root->fs_info->running_transaction->transid)
138 from_this_trans = 1;
139
140 /* FIXME, this is not good */
141 if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
142 u32 val;
143 u32 found = 0;
144 memcpy(&found, result, BTRFS_CRC32_SIZE);
145
146 read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
147 WARN_ON(1);
148 printk("btrfs: %s checksum verify failed on %llu "
149 "wanted %X found %X from_this_trans %d "
150 "level %d\n",
151 root->fs_info->sb->s_id,
152 buf->start, val, found, from_this_trans,
153 btrfs_header_level(buf));
154 return 1;
155 }
156 } else {
157 write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
158 }
159 return 0;
160 }
161
162
163 int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
164 {
165 struct extent_io_tree *tree;
166 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
167 u64 found_start;
168 int found_level;
169 unsigned long len;
170 struct extent_buffer *eb;
171 tree = &BTRFS_I(page->mapping->host)->io_tree;
172
173 if (page->private == EXTENT_PAGE_PRIVATE)
174 goto out;
175 if (!page->private)
176 goto out;
177 len = page->private >> 2;
178 if (len == 0) {
179 WARN_ON(1);
180 }
181 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
182 read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
183 btree_get_extent);
184 btrfs_clear_buffer_defrag(eb);
185 found_start = btrfs_header_bytenr(eb);
186 if (found_start != start) {
187 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
188 start, found_start, len);
189 WARN_ON(1);
190 goto err;
191 }
192 if (eb->first_page != page) {
193 printk("bad first page %lu %lu\n", eb->first_page->index,
194 page->index);
195 WARN_ON(1);
196 goto err;
197 }
198 if (!PageUptodate(page)) {
199 printk("csum not up to date page %lu\n", page->index);
200 WARN_ON(1);
201 goto err;
202 }
203 found_level = btrfs_header_level(eb);
204 spin_lock(&root->fs_info->hash_lock);
205 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
206 spin_unlock(&root->fs_info->hash_lock);
207 csum_tree_block(root, eb, 0);
208 err:
209 free_extent_buffer(eb);
210 out:
211 return 0;
212 }
213
214 static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
215 {
216 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
217
218 csum_dirty_buffer(root, page);
219 return 0;
220 }
221
222 static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
223 {
224 struct btrfs_root *root = BTRFS_I(inode)->root;
225 u64 offset;
226 offset = bio->bi_sector << 9;
227 if (offset == BTRFS_SUPER_INFO_OFFSET) {
228 bio->bi_bdev = root->fs_info->sb->s_bdev;
229 submit_bio(rw, bio);
230 return 0;
231 }
232 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
233 }
234
235 static int btree_writepage(struct page *page, struct writeback_control *wbc)
236 {
237 struct extent_io_tree *tree;
238 tree = &BTRFS_I(page->mapping->host)->io_tree;
239 return extent_write_full_page(tree, page, btree_get_extent, wbc);
240 }
241
242 static int btree_writepages(struct address_space *mapping,
243 struct writeback_control *wbc)
244 {
245 struct extent_io_tree *tree;
246 tree = &BTRFS_I(mapping->host)->io_tree;
247 if (wbc->sync_mode == WB_SYNC_NONE) {
248 u64 num_dirty;
249 u64 start = 0;
250 unsigned long thresh = 96 * 1024 * 1024;
251
252 if (wbc->for_kupdate)
253 return 0;
254
255 if (current_is_pdflush()) {
256 thresh = 96 * 1024 * 1024;
257 } else {
258 thresh = 8 * 1024 * 1024;
259 }
260 num_dirty = count_range_bits(tree, &start, (u64)-1,
261 thresh, EXTENT_DIRTY);
262 if (num_dirty < thresh) {
263 return 0;
264 }
265 }
266 return extent_writepages(tree, mapping, btree_get_extent, wbc);
267 }
268
269 int btree_readpage(struct file *file, struct page *page)
270 {
271 struct extent_io_tree *tree;
272 tree = &BTRFS_I(page->mapping->host)->io_tree;
273 return extent_read_full_page(tree, page, btree_get_extent);
274 }
275
276 static int btree_releasepage(struct page *page, gfp_t gfp_flags)
277 {
278 struct extent_io_tree *tree;
279 struct extent_map_tree *map;
280 int ret;
281
282 tree = &BTRFS_I(page->mapping->host)->io_tree;
283 map = &BTRFS_I(page->mapping->host)->extent_tree;
284 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
285 if (ret == 1) {
286 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
287 ClearPagePrivate(page);
288 set_page_private(page, 0);
289 page_cache_release(page);
290 }
291 return ret;
292 }
293
294 static void btree_invalidatepage(struct page *page, unsigned long offset)
295 {
296 struct extent_io_tree *tree;
297 tree = &BTRFS_I(page->mapping->host)->io_tree;
298 extent_invalidatepage(tree, page, offset);
299 btree_releasepage(page, GFP_NOFS);
300 }
301
302 #if 0
303 static int btree_writepage(struct page *page, struct writeback_control *wbc)
304 {
305 struct buffer_head *bh;
306 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
307 struct buffer_head *head;
308 if (!page_has_buffers(page)) {
309 create_empty_buffers(page, root->fs_info->sb->s_blocksize,
310 (1 << BH_Dirty)|(1 << BH_Uptodate));
311 }
312 head = page_buffers(page);
313 bh = head;
314 do {
315 if (buffer_dirty(bh))
316 csum_tree_block(root, bh, 0);
317 bh = bh->b_this_page;
318 } while (bh != head);
319 return block_write_full_page(page, btree_get_block, wbc);
320 }
321 #endif
322
323 static struct address_space_operations btree_aops = {
324 .readpage = btree_readpage,
325 .writepage = btree_writepage,
326 .writepages = btree_writepages,
327 .releasepage = btree_releasepage,
328 .invalidatepage = btree_invalidatepage,
329 .sync_page = block_sync_page,
330 };
331
332 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
333 {
334 struct extent_buffer *buf = NULL;
335 struct inode *btree_inode = root->fs_info->btree_inode;
336 int ret = 0;
337
338 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
339 if (!buf)
340 return 0;
341 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
342 buf, 0, 0, btree_get_extent);
343 free_extent_buffer(buf);
344 return ret;
345 }
346
347 static int close_all_devices(struct btrfs_fs_info *fs_info)
348 {
349 struct list_head *list;
350 struct list_head *next;
351 struct btrfs_device *device;
352
353 list = &fs_info->fs_devices->devices;
354 list_for_each(next, list) {
355 device = list_entry(next, struct btrfs_device, dev_list);
356 if (device->bdev && device->bdev != fs_info->sb->s_bdev)
357 close_bdev_excl(device->bdev);
358 device->bdev = NULL;
359 }
360 return 0;
361 }
362
363 int btrfs_verify_block_csum(struct btrfs_root *root,
364 struct extent_buffer *buf)
365 {
366 struct extent_io_tree *io_tree;
367 u64 end;
368 int ret;
369
370 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
371 if (buf->flags & EXTENT_CSUM)
372 return 0;
373
374 end = min_t(u64, buf->len, PAGE_CACHE_SIZE);
375 end = buf->start + end - 1;
376 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
377 buf->flags |= EXTENT_CSUM;
378 return 0;
379 }
380 lock_extent(io_tree, buf->start, end, GFP_NOFS);
381
382 if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
383 buf->flags |= EXTENT_CSUM;
384 ret = 0;
385 goto out_unlock;
386 }
387 WARN_ON(buf->flags & EXTENT_CSUM);
388
389 ret = csum_tree_block(root, buf, 1);
390 set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
391 buf->flags |= EXTENT_CSUM;
392
393 out_unlock:
394 unlock_extent(io_tree, buf->start, end, GFP_NOFS);
395 return ret;
396 }
397
398 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
399 u64 bytenr, u32 blocksize)
400 {
401 struct inode *btree_inode = root->fs_info->btree_inode;
402 struct extent_buffer *eb;
403 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
404 bytenr, blocksize, GFP_NOFS);
405 return eb;
406 }
407
408 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
409 u64 bytenr, u32 blocksize)
410 {
411 struct inode *btree_inode = root->fs_info->btree_inode;
412 struct extent_buffer *eb;
413
414 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
415 bytenr, blocksize, NULL, GFP_NOFS);
416 return eb;
417 }
418
419
420 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
421 u32 blocksize)
422 {
423 struct extent_buffer *buf = NULL;
424 struct inode *btree_inode = root->fs_info->btree_inode;
425 struct extent_io_tree *io_tree;
426 int ret;
427
428 io_tree = &BTRFS_I(btree_inode)->io_tree;
429
430 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
431 if (!buf)
432 return NULL;
433 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
434 btree_get_extent);
435
436 ret = btrfs_verify_block_csum(root, buf);
437 return buf;
438 }
439
440 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
441 struct extent_buffer *buf)
442 {
443 struct inode *btree_inode = root->fs_info->btree_inode;
444 if (btrfs_header_generation(buf) ==
445 root->fs_info->running_transaction->transid)
446 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
447 buf);
448 return 0;
449 }
450
451 int wait_on_tree_block_writeback(struct btrfs_root *root,
452 struct extent_buffer *buf)
453 {
454 struct inode *btree_inode = root->fs_info->btree_inode;
455 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
456 buf);
457 return 0;
458 }
459
460 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
461 u32 stripesize, struct btrfs_root *root,
462 struct btrfs_fs_info *fs_info,
463 u64 objectid)
464 {
465 root->node = NULL;
466 root->inode = NULL;
467 root->commit_root = NULL;
468 root->sectorsize = sectorsize;
469 root->nodesize = nodesize;
470 root->leafsize = leafsize;
471 root->stripesize = stripesize;
472 root->ref_cows = 0;
473 root->track_dirty = 0;
474
475 root->fs_info = fs_info;
476 root->objectid = objectid;
477 root->last_trans = 0;
478 root->highest_inode = 0;
479 root->last_inode_alloc = 0;
480 root->name = NULL;
481 root->in_sysfs = 0;
482
483 INIT_LIST_HEAD(&root->dirty_list);
484 memset(&root->root_key, 0, sizeof(root->root_key));
485 memset(&root->root_item, 0, sizeof(root->root_item));
486 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
487 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
488 init_completion(&root->kobj_unregister);
489 root->defrag_running = 0;
490 root->defrag_level = 0;
491 root->root_key.objectid = objectid;
492 return 0;
493 }
494
495 static int find_and_setup_root(struct btrfs_root *tree_root,
496 struct btrfs_fs_info *fs_info,
497 u64 objectid,
498 struct btrfs_root *root)
499 {
500 int ret;
501 u32 blocksize;
502
503 __setup_root(tree_root->nodesize, tree_root->leafsize,
504 tree_root->sectorsize, tree_root->stripesize,
505 root, fs_info, objectid);
506 ret = btrfs_find_last_root(tree_root, objectid,
507 &root->root_item, &root->root_key);
508 BUG_ON(ret);
509
510 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
511 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
512 blocksize);
513 BUG_ON(!root->node);
514 return 0;
515 }
516
517 struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
518 struct btrfs_key *location)
519 {
520 struct btrfs_root *root;
521 struct btrfs_root *tree_root = fs_info->tree_root;
522 struct btrfs_path *path;
523 struct extent_buffer *l;
524 u64 highest_inode;
525 u32 blocksize;
526 int ret = 0;
527
528 root = kzalloc(sizeof(*root), GFP_NOFS);
529 if (!root)
530 return ERR_PTR(-ENOMEM);
531 if (location->offset == (u64)-1) {
532 ret = find_and_setup_root(tree_root, fs_info,
533 location->objectid, root);
534 if (ret) {
535 kfree(root);
536 return ERR_PTR(ret);
537 }
538 goto insert;
539 }
540
541 __setup_root(tree_root->nodesize, tree_root->leafsize,
542 tree_root->sectorsize, tree_root->stripesize,
543 root, fs_info, location->objectid);
544
545 path = btrfs_alloc_path();
546 BUG_ON(!path);
547 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
548 if (ret != 0) {
549 if (ret > 0)
550 ret = -ENOENT;
551 goto out;
552 }
553 l = path->nodes[0];
554 read_extent_buffer(l, &root->root_item,
555 btrfs_item_ptr_offset(l, path->slots[0]),
556 sizeof(root->root_item));
557 memcpy(&root->root_key, location, sizeof(*location));
558 ret = 0;
559 out:
560 btrfs_release_path(root, path);
561 btrfs_free_path(path);
562 if (ret) {
563 kfree(root);
564 return ERR_PTR(ret);
565 }
566 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
567 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
568 blocksize);
569 BUG_ON(!root->node);
570 insert:
571 root->ref_cows = 1;
572 ret = btrfs_find_highest_inode(root, &highest_inode);
573 if (ret == 0) {
574 root->highest_inode = highest_inode;
575 root->last_inode_alloc = highest_inode;
576 }
577 return root;
578 }
579
580 struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
581 u64 root_objectid)
582 {
583 struct btrfs_root *root;
584
585 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
586 return fs_info->tree_root;
587 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
588 return fs_info->extent_root;
589
590 root = radix_tree_lookup(&fs_info->fs_roots_radix,
591 (unsigned long)root_objectid);
592 return root;
593 }
594
595 struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
596 struct btrfs_key *location)
597 {
598 struct btrfs_root *root;
599 int ret;
600
601 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
602 return fs_info->tree_root;
603 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
604 return fs_info->extent_root;
605
606 root = radix_tree_lookup(&fs_info->fs_roots_radix,
607 (unsigned long)location->objectid);
608 if (root)
609 return root;
610
611 root = btrfs_read_fs_root_no_radix(fs_info, location);
612 if (IS_ERR(root))
613 return root;
614 ret = radix_tree_insert(&fs_info->fs_roots_radix,
615 (unsigned long)root->root_key.objectid,
616 root);
617 if (ret) {
618 free_extent_buffer(root->node);
619 kfree(root);
620 return ERR_PTR(ret);
621 }
622 ret = btrfs_find_dead_roots(fs_info->tree_root,
623 root->root_key.objectid, root);
624 BUG_ON(ret);
625
626 return root;
627 }
628
629 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
630 struct btrfs_key *location,
631 const char *name, int namelen)
632 {
633 struct btrfs_root *root;
634 int ret;
635
636 root = btrfs_read_fs_root_no_name(fs_info, location);
637 if (!root)
638 return NULL;
639
640 if (root->in_sysfs)
641 return root;
642
643 ret = btrfs_set_root_name(root, name, namelen);
644 if (ret) {
645 free_extent_buffer(root->node);
646 kfree(root);
647 return ERR_PTR(ret);
648 }
649
650 ret = btrfs_sysfs_add_root(root);
651 if (ret) {
652 free_extent_buffer(root->node);
653 kfree(root->name);
654 kfree(root);
655 return ERR_PTR(ret);
656 }
657 root->in_sysfs = 1;
658 return root;
659 }
660 #if 0
661 static int add_hasher(struct btrfs_fs_info *info, char *type) {
662 struct btrfs_hasher *hasher;
663
664 hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
665 if (!hasher)
666 return -ENOMEM;
667 hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
668 if (!hasher->hash_tfm) {
669 kfree(hasher);
670 return -EINVAL;
671 }
672 spin_lock(&info->hash_lock);
673 list_add(&hasher->list, &info->hashers);
674 spin_unlock(&info->hash_lock);
675 return 0;
676 }
677 #endif
678
679 static int btrfs_congested_fn(void *congested_data, int bdi_bits)
680 {
681 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
682 int ret = 0;
683 struct list_head *cur;
684 struct btrfs_device *device;
685 struct backing_dev_info *bdi;
686
687 list_for_each(cur, &info->fs_devices->devices) {
688 device = list_entry(cur, struct btrfs_device, dev_list);
689 bdi = blk_get_backing_dev_info(device->bdev);
690 if (bdi && bdi_congested(bdi, bdi_bits)) {
691 ret = 1;
692 break;
693 }
694 }
695 return ret;
696 }
697
698 void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
699 {
700 struct list_head *cur;
701 struct btrfs_device *device;
702 struct btrfs_fs_info *info;
703
704 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
705 list_for_each(cur, &info->fs_devices->devices) {
706 device = list_entry(cur, struct btrfs_device, dev_list);
707 bdi = blk_get_backing_dev_info(device->bdev);
708 if (bdi->unplug_io_fn) {
709 bdi->unplug_io_fn(bdi, page);
710 }
711 }
712 }
713
714 static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
715 {
716 bdi_init(bdi);
717 bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
718 bdi->state = 0;
719 bdi->capabilities = default_backing_dev_info.capabilities;
720 bdi->unplug_io_fn = btrfs_unplug_io_fn;
721 bdi->unplug_io_data = info;
722 bdi->congested_fn = btrfs_congested_fn;
723 bdi->congested_data = info;
724 return 0;
725 }
726
727 struct btrfs_root *open_ctree(struct super_block *sb,
728 struct btrfs_fs_devices *fs_devices)
729 {
730 u32 sectorsize;
731 u32 nodesize;
732 u32 leafsize;
733 u32 blocksize;
734 u32 stripesize;
735 struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
736 GFP_NOFS);
737 struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
738 GFP_NOFS);
739 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
740 GFP_NOFS);
741 struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
742 GFP_NOFS);
743 struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
744 GFP_NOFS);
745 int ret;
746 int err = -EINVAL;
747 struct btrfs_super_block *disk_super;
748
749 if (!extent_root || !tree_root || !fs_info) {
750 err = -ENOMEM;
751 goto fail;
752 }
753 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
754 INIT_LIST_HEAD(&fs_info->trans_list);
755 INIT_LIST_HEAD(&fs_info->dead_roots);
756 INIT_LIST_HEAD(&fs_info->hashers);
757 spin_lock_init(&fs_info->hash_lock);
758 spin_lock_init(&fs_info->delalloc_lock);
759 spin_lock_init(&fs_info->new_trans_lock);
760
761 init_completion(&fs_info->kobj_unregister);
762 sb_set_blocksize(sb, 4096);
763 fs_info->tree_root = tree_root;
764 fs_info->extent_root = extent_root;
765 fs_info->chunk_root = chunk_root;
766 fs_info->dev_root = dev_root;
767 fs_info->fs_devices = fs_devices;
768 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
769 INIT_LIST_HEAD(&fs_info->space_info);
770 btrfs_mapping_init(&fs_info->mapping_tree);
771 fs_info->sb = sb;
772 fs_info->max_extent = (u64)-1;
773 fs_info->max_inline = 8192 * 1024;
774 setup_bdi(fs_info, &fs_info->bdi);
775 fs_info->btree_inode = new_inode(sb);
776 fs_info->btree_inode->i_ino = 1;
777 fs_info->btree_inode->i_nlink = 1;
778 fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
779 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
780 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
781
782 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
783 fs_info->btree_inode->i_mapping,
784 GFP_NOFS);
785 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
786 GFP_NOFS);
787
788 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
789
790 extent_io_tree_init(&fs_info->free_space_cache,
791 fs_info->btree_inode->i_mapping, GFP_NOFS);
792 extent_io_tree_init(&fs_info->block_group_cache,
793 fs_info->btree_inode->i_mapping, GFP_NOFS);
794 extent_io_tree_init(&fs_info->pinned_extents,
795 fs_info->btree_inode->i_mapping, GFP_NOFS);
796 extent_io_tree_init(&fs_info->pending_del,
797 fs_info->btree_inode->i_mapping, GFP_NOFS);
798 extent_io_tree_init(&fs_info->extent_ins,
799 fs_info->btree_inode->i_mapping, GFP_NOFS);
800 fs_info->do_barriers = 1;
801
802 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
803 INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
804 #else
805 INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
806 #endif
807 BTRFS_I(fs_info->btree_inode)->root = tree_root;
808 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
809 sizeof(struct btrfs_key));
810 insert_inode_hash(fs_info->btree_inode);
811 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
812
813 mutex_init(&fs_info->trans_mutex);
814 mutex_init(&fs_info->fs_mutex);
815
816 #if 0
817 ret = add_hasher(fs_info, "crc32c");
818 if (ret) {
819 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
820 err = -ENOMEM;
821 goto fail_iput;
822 }
823 #endif
824 __setup_root(4096, 4096, 4096, 4096, tree_root,
825 fs_info, BTRFS_ROOT_TREE_OBJECTID);
826
827 fs_info->sb_buffer = read_tree_block(tree_root,
828 BTRFS_SUPER_INFO_OFFSET,
829 4096);
830
831 if (!fs_info->sb_buffer)
832 goto fail_iput;
833
834 read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
835 sizeof(fs_info->super_copy));
836
837 read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
838 (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
839 BTRFS_FSID_SIZE);
840
841 disk_super = &fs_info->super_copy;
842 if (!btrfs_super_root(disk_super))
843 goto fail_sb_buffer;
844
845 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
846 printk("Btrfs: wanted %llu devices, but found %llu\n",
847 (unsigned long long)btrfs_super_num_devices(disk_super),
848 (unsigned long long)fs_devices->num_devices);
849 goto fail_sb_buffer;
850 }
851 nodesize = btrfs_super_nodesize(disk_super);
852 leafsize = btrfs_super_leafsize(disk_super);
853 sectorsize = btrfs_super_sectorsize(disk_super);
854 stripesize = btrfs_super_stripesize(disk_super);
855 tree_root->nodesize = nodesize;
856 tree_root->leafsize = leafsize;
857 tree_root->sectorsize = sectorsize;
858 tree_root->stripesize = stripesize;
859 sb_set_blocksize(sb, sectorsize);
860
861 i_size_write(fs_info->btree_inode,
862 btrfs_super_total_bytes(disk_super));
863
864 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
865 sizeof(disk_super->magic))) {
866 printk("btrfs: valid FS not found on %s\n", sb->s_id);
867 goto fail_sb_buffer;
868 }
869
870 mutex_lock(&fs_info->fs_mutex);
871
872 ret = btrfs_read_sys_array(tree_root);
873 BUG_ON(ret);
874
875 blocksize = btrfs_level_size(tree_root,
876 btrfs_super_chunk_root_level(disk_super));
877
878 __setup_root(nodesize, leafsize, sectorsize, stripesize,
879 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
880
881 chunk_root->node = read_tree_block(chunk_root,
882 btrfs_super_chunk_root(disk_super),
883 blocksize);
884 BUG_ON(!chunk_root->node);
885
886 ret = btrfs_read_chunk_tree(chunk_root);
887 BUG_ON(ret);
888
889 blocksize = btrfs_level_size(tree_root,
890 btrfs_super_root_level(disk_super));
891
892
893 tree_root->node = read_tree_block(tree_root,
894 btrfs_super_root(disk_super),
895 blocksize);
896 if (!tree_root->node)
897 goto fail_sb_buffer;
898
899
900 ret = find_and_setup_root(tree_root, fs_info,
901 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
902 if (ret)
903 goto fail_tree_root;
904 extent_root->track_dirty = 1;
905
906 ret = find_and_setup_root(tree_root, fs_info,
907 BTRFS_DEV_TREE_OBJECTID, dev_root);
908 dev_root->track_dirty = 1;
909
910 if (ret)
911 goto fail_extent_root;
912
913 btrfs_read_block_groups(extent_root);
914
915 fs_info->generation = btrfs_super_generation(disk_super) + 1;
916 fs_info->data_alloc_profile = (u64)-1;
917 fs_info->metadata_alloc_profile = (u64)-1;
918 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
919
920 mutex_unlock(&fs_info->fs_mutex);
921 return tree_root;
922
923 fail_extent_root:
924 free_extent_buffer(extent_root->node);
925 fail_tree_root:
926 mutex_unlock(&fs_info->fs_mutex);
927 free_extent_buffer(tree_root->node);
928 fail_sb_buffer:
929 free_extent_buffer(fs_info->sb_buffer);
930 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
931 fail_iput:
932 iput(fs_info->btree_inode);
933 fail:
934 close_all_devices(fs_info);
935 kfree(extent_root);
936 kfree(tree_root);
937 bdi_destroy(&fs_info->bdi);
938 kfree(fs_info);
939 return ERR_PTR(err);
940 }
941
942 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
943 *root)
944 {
945 int ret;
946 struct extent_buffer *super = root->fs_info->sb_buffer;
947 struct inode *btree_inode = root->fs_info->btree_inode;
948 struct super_block *sb = root->fs_info->sb;
949
950 if (!btrfs_test_opt(root, NOBARRIER))
951 blkdev_issue_flush(sb->s_bdev, NULL);
952 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
953 ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
954 super->start, super->len);
955 if (!btrfs_test_opt(root, NOBARRIER))
956 blkdev_issue_flush(sb->s_bdev, NULL);
957 return ret;
958 }
959
960 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
961 {
962 radix_tree_delete(&fs_info->fs_roots_radix,
963 (unsigned long)root->root_key.objectid);
964 if (root->in_sysfs)
965 btrfs_sysfs_del_root(root);
966 if (root->inode)
967 iput(root->inode);
968 if (root->node)
969 free_extent_buffer(root->node);
970 if (root->commit_root)
971 free_extent_buffer(root->commit_root);
972 if (root->name)
973 kfree(root->name);
974 kfree(root);
975 return 0;
976 }
977
978 static int del_fs_roots(struct btrfs_fs_info *fs_info)
979 {
980 int ret;
981 struct btrfs_root *gang[8];
982 int i;
983
984 while(1) {
985 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
986 (void **)gang, 0,
987 ARRAY_SIZE(gang));
988 if (!ret)
989 break;
990 for (i = 0; i < ret; i++)
991 btrfs_free_fs_root(fs_info, gang[i]);
992 }
993 return 0;
994 }
995
996 int close_ctree(struct btrfs_root *root)
997 {
998 int ret;
999 struct btrfs_trans_handle *trans;
1000 struct btrfs_fs_info *fs_info = root->fs_info;
1001
1002 fs_info->closing = 1;
1003 btrfs_transaction_flush_work(root);
1004 mutex_lock(&fs_info->fs_mutex);
1005 btrfs_defrag_dirty_roots(root->fs_info);
1006 trans = btrfs_start_transaction(root, 1);
1007 ret = btrfs_commit_transaction(trans, root);
1008 /* run commit again to drop the original snapshot */
1009 trans = btrfs_start_transaction(root, 1);
1010 btrfs_commit_transaction(trans, root);
1011 ret = btrfs_write_and_wait_transaction(NULL, root);
1012 BUG_ON(ret);
1013 write_ctree_super(NULL, root);
1014 mutex_unlock(&fs_info->fs_mutex);
1015
1016 if (fs_info->delalloc_bytes) {
1017 printk("btrfs: at unmount delalloc count %Lu\n",
1018 fs_info->delalloc_bytes);
1019 }
1020 if (fs_info->extent_root->node)
1021 free_extent_buffer(fs_info->extent_root->node);
1022
1023 if (fs_info->tree_root->node)
1024 free_extent_buffer(fs_info->tree_root->node);
1025
1026 if (root->fs_info->chunk_root->node);
1027 free_extent_buffer(root->fs_info->chunk_root->node);
1028
1029 if (root->fs_info->dev_root->node);
1030 free_extent_buffer(root->fs_info->dev_root->node);
1031
1032 free_extent_buffer(fs_info->sb_buffer);
1033
1034 btrfs_free_block_groups(root->fs_info);
1035 del_fs_roots(fs_info);
1036
1037 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1038
1039 extent_io_tree_empty_lru(&fs_info->free_space_cache);
1040 extent_io_tree_empty_lru(&fs_info->block_group_cache);
1041 extent_io_tree_empty_lru(&fs_info->pinned_extents);
1042 extent_io_tree_empty_lru(&fs_info->pending_del);
1043 extent_io_tree_empty_lru(&fs_info->extent_ins);
1044 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1045
1046 truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
1047
1048 iput(fs_info->btree_inode);
1049 #if 0
1050 while(!list_empty(&fs_info->hashers)) {
1051 struct btrfs_hasher *hasher;
1052 hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
1053 hashers);
1054 list_del(&hasher->hashers);
1055 crypto_free_hash(&fs_info->hash_tfm);
1056 kfree(hasher);
1057 }
1058 #endif
1059 close_all_devices(fs_info);
1060 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1061 bdi_destroy(&fs_info->bdi);
1062
1063 kfree(fs_info->extent_root);
1064 kfree(fs_info->tree_root);
1065 kfree(fs_info->chunk_root);
1066 kfree(fs_info->dev_root);
1067 return 0;
1068 }
1069
1070 int btrfs_buffer_uptodate(struct extent_buffer *buf)
1071 {
1072 struct inode *btree_inode = buf->first_page->mapping->host;
1073 return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
1074 }
1075
1076 int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
1077 {
1078 struct inode *btree_inode = buf->first_page->mapping->host;
1079 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
1080 buf);
1081 }
1082
1083 void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
1084 {
1085 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1086 u64 transid = btrfs_header_generation(buf);
1087 struct inode *btree_inode = root->fs_info->btree_inode;
1088
1089 if (transid != root->fs_info->generation) {
1090 printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
1091 (unsigned long long)buf->start,
1092 transid, root->fs_info->generation);
1093 WARN_ON(1);
1094 }
1095 set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
1096 }
1097
1098 void btrfs_throttle(struct btrfs_root *root)
1099 {
1100 struct backing_dev_info *bdi;
1101
1102 bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
1103 if (root->fs_info->throttles && bdi_write_congested(bdi)) {
1104 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1105 congestion_wait(WRITE, HZ/20);
1106 #else
1107 blk_congestion_wait(WRITE, HZ/20);
1108 #endif
1109 }
1110 }
1111
1112 void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
1113 {
1114 balance_dirty_pages_ratelimited_nr(
1115 root->fs_info->btree_inode->i_mapping, 1);
1116 }
1117
1118 void btrfs_set_buffer_defrag(struct extent_buffer *buf)
1119 {
1120 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1121 struct inode *btree_inode = root->fs_info->btree_inode;
1122 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1123 buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
1124 }
1125
1126 void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
1127 {
1128 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1129 struct inode *btree_inode = root->fs_info->btree_inode;
1130 set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
1131 buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
1132 GFP_NOFS);
1133 }
1134
1135 int btrfs_buffer_defrag(struct extent_buffer *buf)
1136 {
1137 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1138 struct inode *btree_inode = root->fs_info->btree_inode;
1139 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1140 buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
1141 }
1142
1143 int btrfs_buffer_defrag_done(struct extent_buffer *buf)
1144 {
1145 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1146 struct inode *btree_inode = root->fs_info->btree_inode;
1147 return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
1148 buf->start, buf->start + buf->len - 1,
1149 EXTENT_DEFRAG_DONE, 0);
1150 }
1151
1152 int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
1153 {
1154 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1155 struct inode *btree_inode = root->fs_info->btree_inode;
1156 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1157 buf->start, buf->start + buf->len - 1,
1158 EXTENT_DEFRAG_DONE, GFP_NOFS);
1159 }
1160
1161 int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
1162 {
1163 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1164 struct inode *btree_inode = root->fs_info->btree_inode;
1165 return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
1166 buf->start, buf->start + buf->len - 1,
1167 EXTENT_DEFRAG, GFP_NOFS);
1168 }
1169
1170 int btrfs_read_buffer(struct extent_buffer *buf)
1171 {
1172 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
1173 struct inode *btree_inode = root->fs_info->btree_inode;
1174 return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
1175 buf, 0, 1, btree_get_extent);
1176 }
1177
1178 static struct extent_io_ops btree_extent_io_ops = {
1179 .writepage_io_hook = btree_writepage_io_hook,
1180 .submit_bio_hook = btree_submit_bio_hook,
1181 /* note we're sharing with inode.c for the merge bio hook */
1182 .merge_bio_hook = btrfs_merge_bio_hook,
1183 };
This page took 0.060872 seconds and 5 git commands to generate.