aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
authorPaul Mundt <lethal@linux-sh.org>2011-01-13 01:06:28 -0500
committerPaul Mundt <lethal@linux-sh.org>2011-01-13 01:06:28 -0500
commitf43dc23d5ea91fca257be02138a255f02d98e806 (patch)
treeb29722f6e965316e90ac97abf79923ced250dc21 /fs/btrfs/disk-io.c
parentf8e53553f452dcbf67cb89c8cba63a1cd6eb4cc0 (diff)
parent4162cf64973df51fc885825bc9ca4d055891c49f (diff)
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into common/serial-rework
Conflicts: arch/sh/kernel/cpu/sh2/setup-sh7619.c arch/sh/kernel/cpu/sh2a/setup-mxg.c arch/sh/kernel/cpu/sh2a/setup-sh7201.c arch/sh/kernel/cpu/sh2a/setup-sh7203.c arch/sh/kernel/cpu/sh2a/setup-sh7206.c arch/sh/kernel/cpu/sh3/setup-sh7705.c arch/sh/kernel/cpu/sh3/setup-sh770x.c arch/sh/kernel/cpu/sh3/setup-sh7710.c arch/sh/kernel/cpu/sh3/setup-sh7720.c arch/sh/kernel/cpu/sh4/setup-sh4-202.c arch/sh/kernel/cpu/sh4/setup-sh7750.c arch/sh/kernel/cpu/sh4/setup-sh7760.c arch/sh/kernel/cpu/sh4a/setup-sh7343.c arch/sh/kernel/cpu/sh4a/setup-sh7366.c arch/sh/kernel/cpu/sh4a/setup-sh7722.c arch/sh/kernel/cpu/sh4a/setup-sh7723.c arch/sh/kernel/cpu/sh4a/setup-sh7724.c arch/sh/kernel/cpu/sh4a/setup-sh7763.c arch/sh/kernel/cpu/sh4a/setup-sh7770.c arch/sh/kernel/cpu/sh4a/setup-sh7780.c arch/sh/kernel/cpu/sh4a/setup-sh7785.c arch/sh/kernel/cpu/sh4a/setup-sh7786.c arch/sh/kernel/cpu/sh4a/setup-shx3.c arch/sh/kernel/cpu/sh5/setup-sh5.c drivers/serial/sh-sci.c drivers/serial/sh-sci.h include/linux/serial_sci.h
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c596
1 files changed, 366 insertions, 230 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index d28d29c95f7c..51d2e4de34eb 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -27,6 +27,8 @@
27#include <linux/kthread.h> 27#include <linux/kthread.h>
28#include <linux/freezer.h> 28#include <linux/freezer.h>
29#include <linux/crc32c.h> 29#include <linux/crc32c.h>
30#include <linux/slab.h>
31#include <linux/migrate.h>
30#include "compat.h" 32#include "compat.h"
31#include "ctree.h" 33#include "ctree.h"
32#include "disk-io.h" 34#include "disk-io.h"
@@ -41,8 +43,7 @@
41 43
42static struct extent_io_ops btree_extent_io_ops; 44static struct extent_io_ops btree_extent_io_ops;
43static void end_workqueue_fn(struct btrfs_work *work); 45static void end_workqueue_fn(struct btrfs_work *work);
44 46static void free_fs_root(struct btrfs_root *root);
45static atomic_t btrfs_bdi_num = ATOMIC_INIT(0);
46 47
47/* 48/*
48 * end_io_wq structs are used to do processing in task context when an IO is 49 * end_io_wq structs are used to do processing in task context when an IO is
@@ -74,6 +75,11 @@ struct async_submit_bio {
74 int rw; 75 int rw;
75 int mirror_num; 76 int mirror_num;
76 unsigned long bio_flags; 77 unsigned long bio_flags;
78 /*
79 * bio_offset is optional, can be used if the pages in the bio
80 * can't tell us where in the file the bio should go
81 */
82 u64 bio_offset;
77 struct btrfs_work work; 83 struct btrfs_work work;
78}; 84};
79 85
@@ -123,15 +129,15 @@ static struct extent_map *btree_get_extent(struct inode *inode,
123 struct extent_map *em; 129 struct extent_map *em;
124 int ret; 130 int ret;
125 131
126 spin_lock(&em_tree->lock); 132 read_lock(&em_tree->lock);
127 em = lookup_extent_mapping(em_tree, start, len); 133 em = lookup_extent_mapping(em_tree, start, len);
128 if (em) { 134 if (em) {
129 em->bdev = 135 em->bdev =
130 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 136 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
131 spin_unlock(&em_tree->lock); 137 read_unlock(&em_tree->lock);
132 goto out; 138 goto out;
133 } 139 }
134 spin_unlock(&em_tree->lock); 140 read_unlock(&em_tree->lock);
135 141
136 em = alloc_extent_map(GFP_NOFS); 142 em = alloc_extent_map(GFP_NOFS);
137 if (!em) { 143 if (!em) {
@@ -144,7 +150,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
144 em->block_start = 0; 150 em->block_start = 0;
145 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 151 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
146 152
147 spin_lock(&em_tree->lock); 153 write_lock(&em_tree->lock);
148 ret = add_extent_mapping(em_tree, em); 154 ret = add_extent_mapping(em_tree, em);
149 if (ret == -EEXIST) { 155 if (ret == -EEXIST) {
150 u64 failed_start = em->start; 156 u64 failed_start = em->start;
@@ -163,7 +169,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
163 free_extent_map(em); 169 free_extent_map(em);
164 em = NULL; 170 em = NULL;
165 } 171 }
166 spin_unlock(&em_tree->lock); 172 write_unlock(&em_tree->lock);
167 173
168 if (ret) 174 if (ret)
169 em = ERR_PTR(ret); 175 em = ERR_PTR(ret);
@@ -262,13 +268,15 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
262static int verify_parent_transid(struct extent_io_tree *io_tree, 268static int verify_parent_transid(struct extent_io_tree *io_tree,
263 struct extent_buffer *eb, u64 parent_transid) 269 struct extent_buffer *eb, u64 parent_transid)
264{ 270{
271 struct extent_state *cached_state = NULL;
265 int ret; 272 int ret;
266 273
267 if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 274 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
268 return 0; 275 return 0;
269 276
270 lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS); 277 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
271 if (extent_buffer_uptodate(io_tree, eb) && 278 0, &cached_state, GFP_NOFS);
279 if (extent_buffer_uptodate(io_tree, eb, cached_state) &&
272 btrfs_header_generation(eb) == parent_transid) { 280 btrfs_header_generation(eb) == parent_transid) {
273 ret = 0; 281 ret = 0;
274 goto out; 282 goto out;
@@ -281,10 +289,10 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
281 (unsigned long long)btrfs_header_generation(eb)); 289 (unsigned long long)btrfs_header_generation(eb));
282 } 290 }
283 ret = 1; 291 ret = 1;
284 clear_extent_buffer_uptodate(io_tree, eb); 292 clear_extent_buffer_uptodate(io_tree, eb, &cached_state);
285out: 293out:
286 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, 294 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
287 GFP_NOFS); 295 &cached_state, GFP_NOFS);
288 return ret; 296 return ret;
289} 297}
290 298
@@ -331,7 +339,6 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
331 struct extent_io_tree *tree; 339 struct extent_io_tree *tree;
332 u64 start = (u64)page->index << PAGE_CACHE_SHIFT; 340 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
333 u64 found_start; 341 u64 found_start;
334 int found_level;
335 unsigned long len; 342 unsigned long len;
336 struct extent_buffer *eb; 343 struct extent_buffer *eb;
337 int ret; 344 int ret;
@@ -349,6 +356,8 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
349 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE, 356 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
350 btrfs_header_generation(eb)); 357 btrfs_header_generation(eb));
351 BUG_ON(ret); 358 BUG_ON(ret);
359 WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN));
360
352 found_start = btrfs_header_bytenr(eb); 361 found_start = btrfs_header_bytenr(eb);
353 if (found_start != start) { 362 if (found_start != start) {
354 WARN_ON(1); 363 WARN_ON(1);
@@ -362,8 +371,6 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
362 WARN_ON(1); 371 WARN_ON(1);
363 goto err; 372 goto err;
364 } 373 }
365 found_level = btrfs_header_level(eb);
366
367 csum_tree_block(root, eb, 0); 374 csum_tree_block(root, eb, 0);
368err: 375err:
369 free_extent_buffer(eb); 376 free_extent_buffer(eb);
@@ -473,10 +480,13 @@ static void end_workqueue_bio(struct bio *bio, int err)
473 end_io_wq->work.func = end_workqueue_fn; 480 end_io_wq->work.func = end_workqueue_fn;
474 end_io_wq->work.flags = 0; 481 end_io_wq->work.flags = 0;
475 482
476 if (bio->bi_rw & (1 << BIO_RW)) { 483 if (bio->bi_rw & REQ_WRITE) {
477 if (end_io_wq->metadata) 484 if (end_io_wq->metadata == 1)
478 btrfs_queue_worker(&fs_info->endio_meta_write_workers, 485 btrfs_queue_worker(&fs_info->endio_meta_write_workers,
479 &end_io_wq->work); 486 &end_io_wq->work);
487 else if (end_io_wq->metadata == 2)
488 btrfs_queue_worker(&fs_info->endio_freespace_worker,
489 &end_io_wq->work);
480 else 490 else
481 btrfs_queue_worker(&fs_info->endio_write_workers, 491 btrfs_queue_worker(&fs_info->endio_write_workers,
482 &end_io_wq->work); 492 &end_io_wq->work);
@@ -490,6 +500,13 @@ static void end_workqueue_bio(struct bio *bio, int err)
490 } 500 }
491} 501}
492 502
503/*
504 * For the metadata arg you want
505 *
506 * 0 - if data
507 * 1 - if normal metadta
508 * 2 - if writing to the free space cache area
509 */
493int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio, 510int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
494 int metadata) 511 int metadata)
495{ 512{
@@ -526,13 +543,12 @@ int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
526 543
527static void run_one_async_start(struct btrfs_work *work) 544static void run_one_async_start(struct btrfs_work *work)
528{ 545{
529 struct btrfs_fs_info *fs_info;
530 struct async_submit_bio *async; 546 struct async_submit_bio *async;
531 547
532 async = container_of(work, struct async_submit_bio, work); 548 async = container_of(work, struct async_submit_bio, work);
533 fs_info = BTRFS_I(async->inode)->root->fs_info;
534 async->submit_bio_start(async->inode, async->rw, async->bio, 549 async->submit_bio_start(async->inode, async->rw, async->bio,
535 async->mirror_num, async->bio_flags); 550 async->mirror_num, async->bio_flags,
551 async->bio_offset);
536} 552}
537 553
538static void run_one_async_done(struct btrfs_work *work) 554static void run_one_async_done(struct btrfs_work *work)
@@ -554,7 +570,8 @@ static void run_one_async_done(struct btrfs_work *work)
554 wake_up(&fs_info->async_submit_wait); 570 wake_up(&fs_info->async_submit_wait);
555 571
556 async->submit_bio_done(async->inode, async->rw, async->bio, 572 async->submit_bio_done(async->inode, async->rw, async->bio,
557 async->mirror_num, async->bio_flags); 573 async->mirror_num, async->bio_flags,
574 async->bio_offset);
558} 575}
559 576
560static void run_one_async_free(struct btrfs_work *work) 577static void run_one_async_free(struct btrfs_work *work)
@@ -568,6 +585,7 @@ static void run_one_async_free(struct btrfs_work *work)
568int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 585int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
569 int rw, struct bio *bio, int mirror_num, 586 int rw, struct bio *bio, int mirror_num,
570 unsigned long bio_flags, 587 unsigned long bio_flags,
588 u64 bio_offset,
571 extent_submit_bio_hook_t *submit_bio_start, 589 extent_submit_bio_hook_t *submit_bio_start,
572 extent_submit_bio_hook_t *submit_bio_done) 590 extent_submit_bio_hook_t *submit_bio_done)
573{ 591{
@@ -590,10 +608,11 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
590 608
591 async->work.flags = 0; 609 async->work.flags = 0;
592 async->bio_flags = bio_flags; 610 async->bio_flags = bio_flags;
611 async->bio_offset = bio_offset;
593 612
594 atomic_inc(&fs_info->nr_async_submits); 613 atomic_inc(&fs_info->nr_async_submits);
595 614
596 if (rw & (1 << BIO_RW_SYNCIO)) 615 if (rw & REQ_SYNC)
597 btrfs_set_work_high_prio(&async->work); 616 btrfs_set_work_high_prio(&async->work);
598 617
599 btrfs_queue_worker(&fs_info->workers, &async->work); 618 btrfs_queue_worker(&fs_info->workers, &async->work);
@@ -625,7 +644,8 @@ static int btree_csum_one_bio(struct bio *bio)
625 644
626static int __btree_submit_bio_start(struct inode *inode, int rw, 645static int __btree_submit_bio_start(struct inode *inode, int rw,
627 struct bio *bio, int mirror_num, 646 struct bio *bio, int mirror_num,
628 unsigned long bio_flags) 647 unsigned long bio_flags,
648 u64 bio_offset)
629{ 649{
630 /* 650 /*
631 * when we're called for a write, we're already in the async 651 * when we're called for a write, we're already in the async
@@ -636,7 +656,8 @@ static int __btree_submit_bio_start(struct inode *inode, int rw,
636} 656}
637 657
638static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 658static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
639 int mirror_num, unsigned long bio_flags) 659 int mirror_num, unsigned long bio_flags,
660 u64 bio_offset)
640{ 661{
641 /* 662 /*
642 * when we're called for a write, we're already in the async 663 * when we're called for a write, we're already in the async
@@ -646,7 +667,8 @@ static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
646} 667}
647 668
648static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 669static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
649 int mirror_num, unsigned long bio_flags) 670 int mirror_num, unsigned long bio_flags,
671 u64 bio_offset)
650{ 672{
651 int ret; 673 int ret;
652 674
@@ -654,7 +676,7 @@ static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
654 bio, 1); 676 bio, 1);
655 BUG_ON(ret); 677 BUG_ON(ret);
656 678
657 if (!(rw & (1 << BIO_RW))) { 679 if (!(rw & REQ_WRITE)) {
658 /* 680 /*
659 * called for a read, do the setup so that checksum validation 681 * called for a read, do the setup so that checksum validation
660 * can happen in the async kernel threads 682 * can happen in the async kernel threads
@@ -669,10 +691,32 @@ static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
669 */ 691 */
670 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 692 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
671 inode, rw, bio, mirror_num, 0, 693 inode, rw, bio, mirror_num, 0,
694 bio_offset,
672 __btree_submit_bio_start, 695 __btree_submit_bio_start,
673 __btree_submit_bio_done); 696 __btree_submit_bio_done);
674} 697}
675 698
699#ifdef CONFIG_MIGRATION
700static int btree_migratepage(struct address_space *mapping,
701 struct page *newpage, struct page *page)
702{
703 /*
704 * we can't safely write a btree page from here,
705 * we haven't done the locking hook
706 */
707 if (PageDirty(page))
708 return -EAGAIN;
709 /*
710 * Buffers may be managed in a filesystem specific way.
711 * We must have no buffers or drop them.
712 */
713 if (page_has_private(page) &&
714 !try_to_release_page(page, GFP_KERNEL))
715 return -EAGAIN;
716 return migrate_page(mapping, newpage, page);
717}
718#endif
719
676static int btree_writepage(struct page *page, struct writeback_control *wbc) 720static int btree_writepage(struct page *page, struct writeback_control *wbc)
677{ 721{
678 struct extent_io_tree *tree; 722 struct extent_io_tree *tree;
@@ -687,8 +731,7 @@ static int btree_writepage(struct page *page, struct writeback_control *wbc)
687 } 731 }
688 732
689 redirty_page_for_writepage(wbc, page); 733 redirty_page_for_writepage(wbc, page);
690 eb = btrfs_find_tree_block(root, page_offset(page), 734 eb = btrfs_find_tree_block(root, page_offset(page), PAGE_CACHE_SIZE);
691 PAGE_CACHE_SIZE);
692 WARN_ON(!eb); 735 WARN_ON(!eb);
693 736
694 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); 737 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
@@ -772,13 +815,16 @@ static void btree_invalidatepage(struct page *page, unsigned long offset)
772 } 815 }
773} 816}
774 817
775static struct address_space_operations btree_aops = { 818static const struct address_space_operations btree_aops = {
776 .readpage = btree_readpage, 819 .readpage = btree_readpage,
777 .writepage = btree_writepage, 820 .writepage = btree_writepage,
778 .writepages = btree_writepages, 821 .writepages = btree_writepages,
779 .releasepage = btree_releasepage, 822 .releasepage = btree_releasepage,
780 .invalidatepage = btree_invalidatepage, 823 .invalidatepage = btree_invalidatepage,
781 .sync_page = block_sync_page, 824 .sync_page = block_sync_page,
825#ifdef CONFIG_MIGRATION
826 .migratepage = btree_migratepage,
827#endif
782}; 828};
783 829
784int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 830int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
@@ -821,26 +867,22 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
821 867
822int btrfs_write_tree_block(struct extent_buffer *buf) 868int btrfs_write_tree_block(struct extent_buffer *buf)
823{ 869{
824 return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start, 870 return filemap_fdatawrite_range(buf->first_page->mapping, buf->start,
825 buf->start + buf->len - 1, WB_SYNC_ALL); 871 buf->start + buf->len - 1);
826} 872}
827 873
828int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 874int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
829{ 875{
830 return btrfs_wait_on_page_writeback_range(buf->first_page->mapping, 876 return filemap_fdatawait_range(buf->first_page->mapping,
831 buf->start, buf->start + buf->len - 1); 877 buf->start, buf->start + buf->len - 1);
832} 878}
833 879
834struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 880struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
835 u32 blocksize, u64 parent_transid) 881 u32 blocksize, u64 parent_transid)
836{ 882{
837 struct extent_buffer *buf = NULL; 883 struct extent_buffer *buf = NULL;
838 struct inode *btree_inode = root->fs_info->btree_inode;
839 struct extent_io_tree *io_tree;
840 int ret; 884 int ret;
841 885
842 io_tree = &BTRFS_I(btree_inode)->io_tree;
843
844 buf = btrfs_find_create_tree_block(root, bytenr, blocksize); 886 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
845 if (!buf) 887 if (!buf)
846 return NULL; 888 return NULL;
@@ -891,22 +933,27 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
891 root->stripesize = stripesize; 933 root->stripesize = stripesize;
892 root->ref_cows = 0; 934 root->ref_cows = 0;
893 root->track_dirty = 0; 935 root->track_dirty = 0;
936 root->in_radix = 0;
937 root->orphan_item_inserted = 0;
938 root->orphan_cleanup_state = 0;
894 939
895 root->fs_info = fs_info; 940 root->fs_info = fs_info;
896 root->objectid = objectid; 941 root->objectid = objectid;
897 root->last_trans = 0; 942 root->last_trans = 0;
898 root->highest_inode = 0; 943 root->highest_objectid = 0;
899 root->last_inode_alloc = 0;
900 root->name = NULL; 944 root->name = NULL;
901 root->in_sysfs = 0; 945 root->in_sysfs = 0;
902 root->inode_tree.rb_node = NULL; 946 root->inode_tree = RB_ROOT;
947 root->block_rsv = NULL;
948 root->orphan_block_rsv = NULL;
903 949
904 INIT_LIST_HEAD(&root->dirty_list); 950 INIT_LIST_HEAD(&root->dirty_list);
905 INIT_LIST_HEAD(&root->orphan_list); 951 INIT_LIST_HEAD(&root->orphan_list);
906 INIT_LIST_HEAD(&root->root_list); 952 INIT_LIST_HEAD(&root->root_list);
907 spin_lock_init(&root->node_lock); 953 spin_lock_init(&root->node_lock);
908 spin_lock_init(&root->list_lock); 954 spin_lock_init(&root->orphan_lock);
909 spin_lock_init(&root->inode_lock); 955 spin_lock_init(&root->inode_lock);
956 spin_lock_init(&root->accounting_lock);
910 mutex_init(&root->objectid_mutex); 957 mutex_init(&root->objectid_mutex);
911 mutex_init(&root->log_mutex); 958 mutex_init(&root->log_mutex);
912 init_waitqueue_head(&root->log_writer_wait); 959 init_waitqueue_head(&root->log_writer_wait);
@@ -917,6 +964,7 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
917 atomic_set(&root->log_writers, 0); 964 atomic_set(&root->log_writers, 0);
918 root->log_batch = 0; 965 root->log_batch = 0;
919 root->log_transid = 0; 966 root->log_transid = 0;
967 root->last_log_commit = 0;
920 extent_io_tree_init(&root->dirty_log_pages, 968 extent_io_tree_init(&root->dirty_log_pages,
921 fs_info->btree_inode->i_mapping, GFP_NOFS); 969 fs_info->btree_inode->i_mapping, GFP_NOFS);
922 970
@@ -927,7 +975,6 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
927 root->defrag_trans_start = fs_info->generation; 975 root->defrag_trans_start = fs_info->generation;
928 init_completion(&root->kobj_unregister); 976 init_completion(&root->kobj_unregister);
929 root->defrag_running = 0; 977 root->defrag_running = 0;
930 root->defrag_level = 0;
931 root->root_key.objectid = objectid; 978 root->root_key.objectid = objectid;
932 root->anon_super.s_root = NULL; 979 root->anon_super.s_root = NULL;
933 root->anon_super.s_dev = 0; 980 root->anon_super.s_dev = 0;
@@ -952,50 +999,19 @@ static int find_and_setup_root(struct btrfs_root *tree_root,
952 root, fs_info, objectid); 999 root, fs_info, objectid);
953 ret = btrfs_find_last_root(tree_root, objectid, 1000 ret = btrfs_find_last_root(tree_root, objectid,
954 &root->root_item, &root->root_key); 1001 &root->root_item, &root->root_key);
1002 if (ret > 0)
1003 return -ENOENT;
955 BUG_ON(ret); 1004 BUG_ON(ret);
956 1005
957 generation = btrfs_root_generation(&root->root_item); 1006 generation = btrfs_root_generation(&root->root_item);
958 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1007 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
959 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1008 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
960 blocksize, generation); 1009 blocksize, generation);
961 root->commit_root = btrfs_root_node(root); 1010 if (!root->node || !btrfs_buffer_uptodate(root->node, generation)) {
962 BUG_ON(!root->node); 1011 free_extent_buffer(root->node);
963 return 0; 1012 return -EIO;
964}
965
966int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
967 struct btrfs_fs_info *fs_info)
968{
969 struct extent_buffer *eb;
970 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
971 u64 start = 0;
972 u64 end = 0;
973 int ret;
974
975 if (!log_root_tree)
976 return 0;
977
978 while (1) {
979 ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
980 0, &start, &end, EXTENT_DIRTY);
981 if (ret)
982 break;
983
984 clear_extent_dirty(&log_root_tree->dirty_log_pages,
985 start, end, GFP_NOFS);
986 } 1013 }
987 eb = fs_info->log_root_tree->node; 1014 root->commit_root = btrfs_root_node(root);
988
989 WARN_ON(btrfs_header_level(eb) != 0);
990 WARN_ON(btrfs_header_nritems(eb) != 0);
991
992 ret = btrfs_free_reserved_extent(fs_info->tree_root,
993 eb->start, eb->len);
994 BUG_ON(ret);
995
996 free_extent_buffer(eb);
997 kfree(fs_info->log_root_tree);
998 fs_info->log_root_tree = NULL;
999 return 0; 1015 return 0;
1000} 1016}
1001 1017
@@ -1085,6 +1101,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1085 WARN_ON(root->log_root); 1101 WARN_ON(root->log_root);
1086 root->log_root = log_root; 1102 root->log_root = log_root;
1087 root->log_transid = 0; 1103 root->log_transid = 0;
1104 root->last_log_commit = 0;
1088 return 0; 1105 return 0;
1089} 1106}
1090 1107
@@ -1095,7 +1112,6 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1095 struct btrfs_fs_info *fs_info = tree_root->fs_info; 1112 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1096 struct btrfs_path *path; 1113 struct btrfs_path *path;
1097 struct extent_buffer *l; 1114 struct extent_buffer *l;
1098 u64 highest_inode;
1099 u64 generation; 1115 u64 generation;
1100 u32 blocksize; 1116 u32 blocksize;
1101 int ret = 0; 1117 int ret = 0;
@@ -1110,7 +1126,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1110 kfree(root); 1126 kfree(root);
1111 return ERR_PTR(ret); 1127 return ERR_PTR(ret);
1112 } 1128 }
1113 goto insert; 1129 goto out;
1114 } 1130 }
1115 1131
1116 __setup_root(tree_root->nodesize, tree_root->leafsize, 1132 __setup_root(tree_root->nodesize, tree_root->leafsize,
@@ -1120,39 +1136,30 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1120 path = btrfs_alloc_path(); 1136 path = btrfs_alloc_path();
1121 BUG_ON(!path); 1137 BUG_ON(!path);
1122 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 1138 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
1123 if (ret != 0) { 1139 if (ret == 0) {
1124 if (ret > 0) 1140 l = path->nodes[0];
1125 ret = -ENOENT; 1141 read_extent_buffer(l, &root->root_item,
1126 goto out; 1142 btrfs_item_ptr_offset(l, path->slots[0]),
1143 sizeof(root->root_item));
1144 memcpy(&root->root_key, location, sizeof(*location));
1127 } 1145 }
1128 l = path->nodes[0];
1129 read_extent_buffer(l, &root->root_item,
1130 btrfs_item_ptr_offset(l, path->slots[0]),
1131 sizeof(root->root_item));
1132 memcpy(&root->root_key, location, sizeof(*location));
1133 ret = 0;
1134out:
1135 btrfs_release_path(root, path);
1136 btrfs_free_path(path); 1146 btrfs_free_path(path);
1137 if (ret) { 1147 if (ret) {
1138 kfree(root); 1148 if (ret > 0)
1149 ret = -ENOENT;
1139 return ERR_PTR(ret); 1150 return ERR_PTR(ret);
1140 } 1151 }
1152
1141 generation = btrfs_root_generation(&root->root_item); 1153 generation = btrfs_root_generation(&root->root_item);
1142 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1154 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1143 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1155 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
1144 blocksize, generation); 1156 blocksize, generation);
1145 root->commit_root = btrfs_root_node(root); 1157 root->commit_root = btrfs_root_node(root);
1146 BUG_ON(!root->node); 1158 BUG_ON(!root->node);
1147insert: 1159out:
1148 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 1160 if (location->objectid != BTRFS_TREE_LOG_OBJECTID)
1149 root->ref_cows = 1; 1161 root->ref_cows = 1;
1150 ret = btrfs_find_highest_inode(root, &highest_inode); 1162
1151 if (ret == 0) {
1152 root->highest_inode = highest_inode;
1153 root->last_inode_alloc = highest_inode;
1154 }
1155 }
1156 return root; 1163 return root;
1157} 1164}
1158 1165
@@ -1187,9 +1194,11 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1187 return fs_info->dev_root; 1194 return fs_info->dev_root;
1188 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 1195 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1189 return fs_info->csum_root; 1196 return fs_info->csum_root;
1190 1197again:
1198 spin_lock(&fs_info->fs_roots_radix_lock);
1191 root = radix_tree_lookup(&fs_info->fs_roots_radix, 1199 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1192 (unsigned long)location->objectid); 1200 (unsigned long)location->objectid);
1201 spin_unlock(&fs_info->fs_roots_radix_lock);
1193 if (root) 1202 if (root)
1194 return root; 1203 return root;
1195 1204
@@ -1199,27 +1208,53 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1199 1208
1200 set_anon_super(&root->anon_super, NULL); 1209 set_anon_super(&root->anon_super, NULL);
1201 1210
1211 if (btrfs_root_refs(&root->root_item) == 0) {
1212 ret = -ENOENT;
1213 goto fail;
1214 }
1215
1216 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1217 if (ret < 0)
1218 goto fail;
1219 if (ret == 0)
1220 root->orphan_item_inserted = 1;
1221
1222 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1223 if (ret)
1224 goto fail;
1225
1226 spin_lock(&fs_info->fs_roots_radix_lock);
1202 ret = radix_tree_insert(&fs_info->fs_roots_radix, 1227 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1203 (unsigned long)root->root_key.objectid, 1228 (unsigned long)root->root_key.objectid,
1204 root); 1229 root);
1230 if (ret == 0)
1231 root->in_radix = 1;
1232
1233 spin_unlock(&fs_info->fs_roots_radix_lock);
1234 radix_tree_preload_end();
1205 if (ret) { 1235 if (ret) {
1206 free_extent_buffer(root->node); 1236 if (ret == -EEXIST) {
1207 kfree(root); 1237 free_fs_root(root);
1208 return ERR_PTR(ret); 1238 goto again;
1209 } 1239 }
1210 if (!(fs_info->sb->s_flags & MS_RDONLY)) { 1240 goto fail;
1211 ret = btrfs_find_dead_roots(fs_info->tree_root,
1212 root->root_key.objectid);
1213 BUG_ON(ret);
1214 btrfs_orphan_cleanup(root);
1215 } 1241 }
1242
1243 ret = btrfs_find_dead_roots(fs_info->tree_root,
1244 root->root_key.objectid);
1245 WARN_ON(ret);
1216 return root; 1246 return root;
1247fail:
1248 free_fs_root(root);
1249 return ERR_PTR(ret);
1217} 1250}
1218 1251
1219struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, 1252struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1220 struct btrfs_key *location, 1253 struct btrfs_key *location,
1221 const char *name, int namelen) 1254 const char *name, int namelen)
1222{ 1255{
1256 return btrfs_read_fs_root_no_name(fs_info, location);
1257#if 0
1223 struct btrfs_root *root; 1258 struct btrfs_root *root;
1224 int ret; 1259 int ret;
1225 1260
@@ -1236,7 +1271,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1236 kfree(root); 1271 kfree(root);
1237 return ERR_PTR(ret); 1272 return ERR_PTR(ret);
1238 } 1273 }
1239#if 0 1274
1240 ret = btrfs_sysfs_add_root(root); 1275 ret = btrfs_sysfs_add_root(root);
1241 if (ret) { 1276 if (ret) {
1242 free_extent_buffer(root->node); 1277 free_extent_buffer(root->node);
@@ -1244,9 +1279,9 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1244 kfree(root); 1279 kfree(root);
1245 return ERR_PTR(ret); 1280 return ERR_PTR(ret);
1246 } 1281 }
1247#endif
1248 root->in_sysfs = 1; 1282 root->in_sysfs = 1;
1249 return root; 1283 return root;
1284#endif
1250} 1285}
1251 1286
1252static int btrfs_congested_fn(void *congested_data, int bdi_bits) 1287static int btrfs_congested_fn(void *congested_data, int bdi_bits)
@@ -1325,9 +1360,9 @@ static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1325 offset = page_offset(page); 1360 offset = page_offset(page);
1326 1361
1327 em_tree = &BTRFS_I(inode)->extent_tree; 1362 em_tree = &BTRFS_I(inode)->extent_tree;
1328 spin_lock(&em_tree->lock); 1363 read_lock(&em_tree->lock);
1329 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 1364 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
1330 spin_unlock(&em_tree->lock); 1365 read_unlock(&em_tree->lock);
1331 if (!em) { 1366 if (!em) {
1332 __unplug_io_fn(bdi, page); 1367 __unplug_io_fn(bdi, page);
1333 return; 1368 return;
@@ -1353,12 +1388,7 @@ static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1353 int err; 1388 int err;
1354 1389
1355 bdi->capabilities = BDI_CAP_MAP_COPY; 1390 bdi->capabilities = BDI_CAP_MAP_COPY;
1356 err = bdi_init(bdi); 1391 err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
1357 if (err)
1358 return err;
1359
1360 err = bdi_register(bdi, NULL, "btrfs-%d",
1361 atomic_inc_return(&btrfs_bdi_num));
1362 if (err) 1392 if (err)
1363 return err; 1393 return err;
1364 1394
@@ -1377,7 +1407,6 @@ static int bio_ready_for_csum(struct bio *bio)
1377 u64 start = 0; 1407 u64 start = 0;
1378 struct page *page; 1408 struct page *page;
1379 struct extent_io_tree *io_tree = NULL; 1409 struct extent_io_tree *io_tree = NULL;
1380 struct btrfs_fs_info *info = NULL;
1381 struct bio_vec *bvec; 1410 struct bio_vec *bvec;
1382 int i; 1411 int i;
1383 int ret; 1412 int ret;
@@ -1396,7 +1425,6 @@ static int bio_ready_for_csum(struct bio *bio)
1396 buf_len = page->private >> 2; 1425 buf_len = page->private >> 2;
1397 start = page_offset(page) + bvec->bv_offset; 1426 start = page_offset(page) + bvec->bv_offset;
1398 io_tree = &BTRFS_I(page->mapping->host)->io_tree; 1427 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
1399 info = BTRFS_I(page->mapping->host)->root->fs_info;
1400 } 1428 }
1401 /* are we fully contained in this bio? */ 1429 /* are we fully contained in this bio? */
1402 if (buf_len <= length) 1430 if (buf_len <= length)
@@ -1427,7 +1455,7 @@ static void end_workqueue_fn(struct btrfs_work *work)
1427 * ram and up to date before trying to verify things. For 1455 * ram and up to date before trying to verify things. For
1428 * blocksize <= pagesize, it is basically a noop 1456 * blocksize <= pagesize, it is basically a noop
1429 */ 1457 */
1430 if (!(bio->bi_rw & (1 << BIO_RW)) && end_io_wq->metadata && 1458 if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata &&
1431 !bio_ready_for_csum(bio)) { 1459 !bio_ready_for_csum(bio)) {
1432 btrfs_queue_worker(&fs_info->endio_meta_workers, 1460 btrfs_queue_worker(&fs_info->endio_meta_workers,
1433 &end_io_wq->work); 1461 &end_io_wq->work);
@@ -1445,23 +1473,21 @@ static int cleaner_kthread(void *arg)
1445 struct btrfs_root *root = arg; 1473 struct btrfs_root *root = arg;
1446 1474
1447 do { 1475 do {
1448 smp_mb();
1449 if (root->fs_info->closing)
1450 break;
1451
1452 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1476 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1453 mutex_lock(&root->fs_info->cleaner_mutex); 1477
1454 btrfs_clean_old_snapshots(root); 1478 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
1455 mutex_unlock(&root->fs_info->cleaner_mutex); 1479 mutex_trylock(&root->fs_info->cleaner_mutex)) {
1480 btrfs_run_delayed_iputs(root);
1481 btrfs_clean_old_snapshots(root);
1482 mutex_unlock(&root->fs_info->cleaner_mutex);
1483 }
1456 1484
1457 if (freezing(current)) { 1485 if (freezing(current)) {
1458 refrigerator(); 1486 refrigerator();
1459 } else { 1487 } else {
1460 smp_mb();
1461 if (root->fs_info->closing)
1462 break;
1463 set_current_state(TASK_INTERRUPTIBLE); 1488 set_current_state(TASK_INTERRUPTIBLE);
1464 schedule(); 1489 if (!kthread_should_stop())
1490 schedule();
1465 __set_current_state(TASK_RUNNING); 1491 __set_current_state(TASK_RUNNING);
1466 } 1492 }
1467 } while (!kthread_should_stop()); 1493 } while (!kthread_should_stop());
@@ -1473,36 +1499,40 @@ static int transaction_kthread(void *arg)
1473 struct btrfs_root *root = arg; 1499 struct btrfs_root *root = arg;
1474 struct btrfs_trans_handle *trans; 1500 struct btrfs_trans_handle *trans;
1475 struct btrfs_transaction *cur; 1501 struct btrfs_transaction *cur;
1502 u64 transid;
1476 unsigned long now; 1503 unsigned long now;
1477 unsigned long delay; 1504 unsigned long delay;
1478 int ret; 1505 int ret;
1479 1506
1480 do { 1507 do {
1481 smp_mb();
1482 if (root->fs_info->closing)
1483 break;
1484
1485 delay = HZ * 30; 1508 delay = HZ * 30;
1486 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1509 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1487 mutex_lock(&root->fs_info->transaction_kthread_mutex); 1510 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1488 1511
1489 mutex_lock(&root->fs_info->trans_mutex); 1512 spin_lock(&root->fs_info->new_trans_lock);
1490 cur = root->fs_info->running_transaction; 1513 cur = root->fs_info->running_transaction;
1491 if (!cur) { 1514 if (!cur) {
1492 mutex_unlock(&root->fs_info->trans_mutex); 1515 spin_unlock(&root->fs_info->new_trans_lock);
1493 goto sleep; 1516 goto sleep;
1494 } 1517 }
1495 1518
1496 now = get_seconds(); 1519 now = get_seconds();
1497 if (now < cur->start_time || now - cur->start_time < 30) { 1520 if (!cur->blocked &&
1498 mutex_unlock(&root->fs_info->trans_mutex); 1521 (now < cur->start_time || now - cur->start_time < 30)) {
1522 spin_unlock(&root->fs_info->new_trans_lock);
1499 delay = HZ * 5; 1523 delay = HZ * 5;
1500 goto sleep; 1524 goto sleep;
1501 } 1525 }
1502 mutex_unlock(&root->fs_info->trans_mutex); 1526 transid = cur->transid;
1503 trans = btrfs_start_transaction(root, 1); 1527 spin_unlock(&root->fs_info->new_trans_lock);
1504 ret = btrfs_commit_transaction(trans, root);
1505 1528
1529 trans = btrfs_join_transaction(root, 1);
1530 if (transid == trans->transid) {
1531 ret = btrfs_commit_transaction(trans, root);
1532 BUG_ON(ret);
1533 } else {
1534 btrfs_end_transaction(trans, root);
1535 }
1506sleep: 1536sleep:
1507 wake_up_process(root->fs_info->cleaner_kthread); 1537 wake_up_process(root->fs_info->cleaner_kthread);
1508 mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1538 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
@@ -1510,10 +1540,10 @@ sleep:
1510 if (freezing(current)) { 1540 if (freezing(current)) {
1511 refrigerator(); 1541 refrigerator();
1512 } else { 1542 } else {
1513 if (root->fs_info->closing)
1514 break;
1515 set_current_state(TASK_INTERRUPTIBLE); 1543 set_current_state(TASK_INTERRUPTIBLE);
1516 schedule_timeout(delay); 1544 if (!kthread_should_stop() &&
1545 !btrfs_transaction_blocked(root->fs_info))
1546 schedule_timeout(delay);
1517 __set_current_state(TASK_RUNNING); 1547 __set_current_state(TASK_RUNNING);
1518 } 1548 }
1519 } while (!kthread_should_stop()); 1549 } while (!kthread_should_stop());
@@ -1537,10 +1567,8 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1537 GFP_NOFS); 1567 GFP_NOFS);
1538 struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root), 1568 struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
1539 GFP_NOFS); 1569 GFP_NOFS);
1540 struct btrfs_root *tree_root = kzalloc(sizeof(struct btrfs_root), 1570 struct btrfs_root *tree_root = btrfs_sb(sb);
1541 GFP_NOFS); 1571 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1542 struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
1543 GFP_NOFS);
1544 struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root), 1572 struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
1545 GFP_NOFS); 1573 GFP_NOFS);
1546 struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root), 1574 struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
@@ -1557,15 +1585,38 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1557 err = -ENOMEM; 1585 err = -ENOMEM;
1558 goto fail; 1586 goto fail;
1559 } 1587 }
1560 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); 1588
1589 ret = init_srcu_struct(&fs_info->subvol_srcu);
1590 if (ret) {
1591 err = ret;
1592 goto fail;
1593 }
1594
1595 ret = setup_bdi(fs_info, &fs_info->bdi);
1596 if (ret) {
1597 err = ret;
1598 goto fail_srcu;
1599 }
1600
1601 fs_info->btree_inode = new_inode(sb);
1602 if (!fs_info->btree_inode) {
1603 err = -ENOMEM;
1604 goto fail_bdi;
1605 }
1606
1607 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
1561 INIT_LIST_HEAD(&fs_info->trans_list); 1608 INIT_LIST_HEAD(&fs_info->trans_list);
1562 INIT_LIST_HEAD(&fs_info->dead_roots); 1609 INIT_LIST_HEAD(&fs_info->dead_roots);
1610 INIT_LIST_HEAD(&fs_info->delayed_iputs);
1563 INIT_LIST_HEAD(&fs_info->hashers); 1611 INIT_LIST_HEAD(&fs_info->hashers);
1564 INIT_LIST_HEAD(&fs_info->delalloc_inodes); 1612 INIT_LIST_HEAD(&fs_info->delalloc_inodes);
1565 INIT_LIST_HEAD(&fs_info->ordered_operations); 1613 INIT_LIST_HEAD(&fs_info->ordered_operations);
1614 INIT_LIST_HEAD(&fs_info->caching_block_groups);
1566 spin_lock_init(&fs_info->delalloc_lock); 1615 spin_lock_init(&fs_info->delalloc_lock);
1567 spin_lock_init(&fs_info->new_trans_lock); 1616 spin_lock_init(&fs_info->new_trans_lock);
1568 spin_lock_init(&fs_info->ref_cache_lock); 1617 spin_lock_init(&fs_info->ref_cache_lock);
1618 spin_lock_init(&fs_info->fs_roots_radix_lock);
1619 spin_lock_init(&fs_info->delayed_iput_lock);
1569 1620
1570 init_completion(&fs_info->kobj_unregister); 1621 init_completion(&fs_info->kobj_unregister);
1571 fs_info->tree_root = tree_root; 1622 fs_info->tree_root = tree_root;
@@ -1577,19 +1628,20 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1577 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 1628 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1578 INIT_LIST_HEAD(&fs_info->space_info); 1629 INIT_LIST_HEAD(&fs_info->space_info);
1579 btrfs_mapping_init(&fs_info->mapping_tree); 1630 btrfs_mapping_init(&fs_info->mapping_tree);
1631 btrfs_init_block_rsv(&fs_info->global_block_rsv);
1632 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
1633 btrfs_init_block_rsv(&fs_info->trans_block_rsv);
1634 btrfs_init_block_rsv(&fs_info->chunk_block_rsv);
1635 btrfs_init_block_rsv(&fs_info->empty_block_rsv);
1636 INIT_LIST_HEAD(&fs_info->durable_block_rsv_list);
1637 mutex_init(&fs_info->durable_block_rsv_mutex);
1580 atomic_set(&fs_info->nr_async_submits, 0); 1638 atomic_set(&fs_info->nr_async_submits, 0);
1581 atomic_set(&fs_info->async_delalloc_pages, 0); 1639 atomic_set(&fs_info->async_delalloc_pages, 0);
1582 atomic_set(&fs_info->async_submit_draining, 0); 1640 atomic_set(&fs_info->async_submit_draining, 0);
1583 atomic_set(&fs_info->nr_async_bios, 0); 1641 atomic_set(&fs_info->nr_async_bios, 0);
1584 fs_info->sb = sb; 1642 fs_info->sb = sb;
1585 fs_info->max_extent = (u64)-1;
1586 fs_info->max_inline = 8192 * 1024; 1643 fs_info->max_inline = 8192 * 1024;
1587 if (setup_bdi(fs_info, &fs_info->bdi)) 1644 fs_info->metadata_ratio = 0;
1588 goto fail_bdi;
1589 fs_info->btree_inode = new_inode(sb);
1590 fs_info->btree_inode->i_ino = 1;
1591 fs_info->btree_inode->i_nlink = 1;
1592 fs_info->metadata_ratio = 8;
1593 1645
1594 fs_info->thread_pool_size = min_t(unsigned long, 1646 fs_info->thread_pool_size = min_t(unsigned long,
1595 num_online_cpus() + 2, 8); 1647 num_online_cpus() + 2, 8);
@@ -1599,7 +1651,10 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1599 1651
1600 sb->s_blocksize = 4096; 1652 sb->s_blocksize = 4096;
1601 sb->s_blocksize_bits = blksize_bits(4096); 1653 sb->s_blocksize_bits = blksize_bits(4096);
1654 sb->s_bdi = &fs_info->bdi;
1602 1655
1656 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
1657 fs_info->btree_inode->i_nlink = 1;
1603 /* 1658 /*
1604 * we set the i_size on the btree inode to the max possible int. 1659 * we set the i_size on the btree inode to the max possible int.
1605 * the real end of the address space is determined by all of 1660 * the real end of the address space is determined by all of
@@ -1618,39 +1673,45 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1618 1673
1619 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 1674 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1620 1675
1676 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1677 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1678 sizeof(struct btrfs_key));
1679 BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
1680 insert_inode_hash(fs_info->btree_inode);
1681
1621 spin_lock_init(&fs_info->block_group_cache_lock); 1682 spin_lock_init(&fs_info->block_group_cache_lock);
1622 fs_info->block_group_cache_tree.rb_node = NULL; 1683 fs_info->block_group_cache_tree = RB_ROOT;
1623 1684
1624 extent_io_tree_init(&fs_info->pinned_extents, 1685 extent_io_tree_init(&fs_info->freed_extents[0],
1686 fs_info->btree_inode->i_mapping, GFP_NOFS);
1687 extent_io_tree_init(&fs_info->freed_extents[1],
1625 fs_info->btree_inode->i_mapping, GFP_NOFS); 1688 fs_info->btree_inode->i_mapping, GFP_NOFS);
1689 fs_info->pinned_extents = &fs_info->freed_extents[0];
1626 fs_info->do_barriers = 1; 1690 fs_info->do_barriers = 1;
1627 1691
1628 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1629 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1630 sizeof(struct btrfs_key));
1631 insert_inode_hash(fs_info->btree_inode);
1632 1692
1633 mutex_init(&fs_info->trans_mutex); 1693 mutex_init(&fs_info->trans_mutex);
1634 mutex_init(&fs_info->ordered_operations_mutex); 1694 mutex_init(&fs_info->ordered_operations_mutex);
1635 mutex_init(&fs_info->tree_log_mutex); 1695 mutex_init(&fs_info->tree_log_mutex);
1636 mutex_init(&fs_info->drop_mutex);
1637 mutex_init(&fs_info->chunk_mutex); 1696 mutex_init(&fs_info->chunk_mutex);
1638 mutex_init(&fs_info->transaction_kthread_mutex); 1697 mutex_init(&fs_info->transaction_kthread_mutex);
1639 mutex_init(&fs_info->cleaner_mutex); 1698 mutex_init(&fs_info->cleaner_mutex);
1640 mutex_init(&fs_info->volume_mutex); 1699 mutex_init(&fs_info->volume_mutex);
1641 mutex_init(&fs_info->tree_reloc_mutex); 1700 init_rwsem(&fs_info->extent_commit_sem);
1701 init_rwsem(&fs_info->cleanup_work_sem);
1702 init_rwsem(&fs_info->subvol_sem);
1642 1703
1643 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 1704 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
1644 btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 1705 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
1645 1706
1646 init_waitqueue_head(&fs_info->transaction_throttle); 1707 init_waitqueue_head(&fs_info->transaction_throttle);
1647 init_waitqueue_head(&fs_info->transaction_wait); 1708 init_waitqueue_head(&fs_info->transaction_wait);
1709 init_waitqueue_head(&fs_info->transaction_blocked_wait);
1648 init_waitqueue_head(&fs_info->async_submit_wait); 1710 init_waitqueue_head(&fs_info->async_submit_wait);
1649 1711
1650 __setup_root(4096, 4096, 4096, 4096, tree_root, 1712 __setup_root(4096, 4096, 4096, 4096, tree_root,
1651 fs_info, BTRFS_ROOT_TREE_OBJECTID); 1713 fs_info, BTRFS_ROOT_TREE_OBJECTID);
1652 1714
1653
1654 bh = btrfs_read_dev_super(fs_devices->latest_bdev); 1715 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
1655 if (!bh) 1716 if (!bh)
1656 goto fail_iput; 1717 goto fail_iput;
@@ -1698,20 +1759,21 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1698 goto fail_iput; 1759 goto fail_iput;
1699 } 1760 }
1700 1761
1701 /* 1762 btrfs_init_workers(&fs_info->generic_worker,
1702 * we need to start all the end_io workers up front because the 1763 "genwork", 1, NULL);
1703 * queue work function gets called at interrupt time, and so it 1764
1704 * cannot dynamically grow.
1705 */
1706 btrfs_init_workers(&fs_info->workers, "worker", 1765 btrfs_init_workers(&fs_info->workers, "worker",
1707 fs_info->thread_pool_size); 1766 fs_info->thread_pool_size,
1767 &fs_info->generic_worker);
1708 1768
1709 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 1769 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
1710 fs_info->thread_pool_size); 1770 fs_info->thread_pool_size,
1771 &fs_info->generic_worker);
1711 1772
1712 btrfs_init_workers(&fs_info->submit_workers, "submit", 1773 btrfs_init_workers(&fs_info->submit_workers, "submit",
1713 min_t(u64, fs_devices->num_devices, 1774 min_t(u64, fs_devices->num_devices,
1714 fs_info->thread_pool_size)); 1775 fs_info->thread_pool_size),
1776 &fs_info->generic_worker);
1715 1777
1716 /* a higher idle thresh on the submit workers makes it much more 1778 /* a higher idle thresh on the submit workers makes it much more
1717 * likely that bios will be send down in a sane order to the 1779 * likely that bios will be send down in a sane order to the
@@ -1725,15 +1787,22 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1725 fs_info->delalloc_workers.idle_thresh = 2; 1787 fs_info->delalloc_workers.idle_thresh = 2;
1726 fs_info->delalloc_workers.ordered = 1; 1788 fs_info->delalloc_workers.ordered = 1;
1727 1789
1728 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1); 1790 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1,
1791 &fs_info->generic_worker);
1729 btrfs_init_workers(&fs_info->endio_workers, "endio", 1792 btrfs_init_workers(&fs_info->endio_workers, "endio",
1730 fs_info->thread_pool_size); 1793 fs_info->thread_pool_size,
1794 &fs_info->generic_worker);
1731 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 1795 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
1732 fs_info->thread_pool_size); 1796 fs_info->thread_pool_size,
1797 &fs_info->generic_worker);
1733 btrfs_init_workers(&fs_info->endio_meta_write_workers, 1798 btrfs_init_workers(&fs_info->endio_meta_write_workers,
1734 "endio-meta-write", fs_info->thread_pool_size); 1799 "endio-meta-write", fs_info->thread_pool_size,
1800 &fs_info->generic_worker);
1735 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 1801 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
1736 fs_info->thread_pool_size); 1802 fs_info->thread_pool_size,
1803 &fs_info->generic_worker);
1804 btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write",
1805 1, &fs_info->generic_worker);
1737 1806
1738 /* 1807 /*
1739 * endios are largely parallel and should have a very 1808 * endios are largely parallel and should have a very
@@ -1742,20 +1811,19 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1742 fs_info->endio_workers.idle_thresh = 4; 1811 fs_info->endio_workers.idle_thresh = 4;
1743 fs_info->endio_meta_workers.idle_thresh = 4; 1812 fs_info->endio_meta_workers.idle_thresh = 4;
1744 1813
1745 fs_info->endio_write_workers.idle_thresh = 64; 1814 fs_info->endio_write_workers.idle_thresh = 2;
1746 fs_info->endio_meta_write_workers.idle_thresh = 64; 1815 fs_info->endio_meta_write_workers.idle_thresh = 2;
1747 1816
1748 btrfs_start_workers(&fs_info->workers, 1); 1817 btrfs_start_workers(&fs_info->workers, 1);
1818 btrfs_start_workers(&fs_info->generic_worker, 1);
1749 btrfs_start_workers(&fs_info->submit_workers, 1); 1819 btrfs_start_workers(&fs_info->submit_workers, 1);
1750 btrfs_start_workers(&fs_info->delalloc_workers, 1); 1820 btrfs_start_workers(&fs_info->delalloc_workers, 1);
1751 btrfs_start_workers(&fs_info->fixup_workers, 1); 1821 btrfs_start_workers(&fs_info->fixup_workers, 1);
1752 btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size); 1822 btrfs_start_workers(&fs_info->endio_workers, 1);
1753 btrfs_start_workers(&fs_info->endio_meta_workers, 1823 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1754 fs_info->thread_pool_size); 1824 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1755 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1825 btrfs_start_workers(&fs_info->endio_write_workers, 1);
1756 fs_info->thread_pool_size); 1826 btrfs_start_workers(&fs_info->endio_freespace_worker, 1);
1757 btrfs_start_workers(&fs_info->endio_write_workers,
1758 fs_info->thread_pool_size);
1759 1827
1760 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 1828 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1761 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 1829 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
@@ -1799,6 +1867,11 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1799 btrfs_super_chunk_root(disk_super), 1867 btrfs_super_chunk_root(disk_super),
1800 blocksize, generation); 1868 blocksize, generation);
1801 BUG_ON(!chunk_root->node); 1869 BUG_ON(!chunk_root->node);
1870 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
1871 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n",
1872 sb->s_id);
1873 goto fail_chunk_root;
1874 }
1802 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node); 1875 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
1803 chunk_root->commit_root = btrfs_root_node(chunk_root); 1876 chunk_root->commit_root = btrfs_root_node(chunk_root);
1804 1877
@@ -1826,6 +1899,11 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1826 blocksize, generation); 1899 blocksize, generation);
1827 if (!tree_root->node) 1900 if (!tree_root->node)
1828 goto fail_chunk_root; 1901 goto fail_chunk_root;
1902 if (!test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) {
1903 printk(KERN_WARNING "btrfs: failed to read tree root on %s\n",
1904 sb->s_id);
1905 goto fail_tree_root;
1906 }
1829 btrfs_set_root_node(&tree_root->root_item, tree_root->node); 1907 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
1830 tree_root->commit_root = btrfs_root_node(tree_root); 1908 tree_root->commit_root = btrfs_root_node(tree_root);
1831 1909
@@ -1848,17 +1926,22 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1848 1926
1849 csum_root->track_dirty = 1; 1927 csum_root->track_dirty = 1;
1850 1928
1851 btrfs_read_block_groups(extent_root);
1852
1853 fs_info->generation = generation; 1929 fs_info->generation = generation;
1854 fs_info->last_trans_committed = generation; 1930 fs_info->last_trans_committed = generation;
1855 fs_info->data_alloc_profile = (u64)-1; 1931 fs_info->data_alloc_profile = (u64)-1;
1856 fs_info->metadata_alloc_profile = (u64)-1; 1932 fs_info->metadata_alloc_profile = (u64)-1;
1857 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1933 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1934
1935 ret = btrfs_read_block_groups(extent_root);
1936 if (ret) {
1937 printk(KERN_ERR "Failed to read block groups: %d\n", ret);
1938 goto fail_block_groups;
1939 }
1940
1858 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 1941 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
1859 "btrfs-cleaner"); 1942 "btrfs-cleaner");
1860 if (IS_ERR(fs_info->cleaner_kthread)) 1943 if (IS_ERR(fs_info->cleaner_kthread))
1861 goto fail_csum_root; 1944 goto fail_block_groups;
1862 1945
1863 fs_info->transaction_kthread = kthread_run(transaction_kthread, 1946 fs_info->transaction_kthread = kthread_run(transaction_kthread,
1864 tree_root, 1947 tree_root,
@@ -1887,8 +1970,11 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1887 btrfs_level_size(tree_root, 1970 btrfs_level_size(tree_root,
1888 btrfs_super_log_root_level(disk_super)); 1971 btrfs_super_log_root_level(disk_super));
1889 1972
1890 log_tree_root = kzalloc(sizeof(struct btrfs_root), 1973 log_tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
1891 GFP_NOFS); 1974 if (!log_tree_root) {
1975 err = -ENOMEM;
1976 goto fail_trans_kthread;
1977 }
1892 1978
1893 __setup_root(nodesize, leafsize, sectorsize, stripesize, 1979 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1894 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1980 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
@@ -1905,9 +1991,20 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1905 } 1991 }
1906 } 1992 }
1907 1993
1994 ret = btrfs_find_orphan_roots(tree_root);
1995 BUG_ON(ret);
1996
1908 if (!(sb->s_flags & MS_RDONLY)) { 1997 if (!(sb->s_flags & MS_RDONLY)) {
1909 ret = btrfs_recover_relocation(tree_root); 1998 ret = btrfs_cleanup_fs_roots(fs_info);
1910 BUG_ON(ret); 1999 BUG_ON(ret);
2000
2001 ret = btrfs_recover_relocation(tree_root);
2002 if (ret < 0) {
2003 printk(KERN_WARNING
2004 "btrfs: failed to recover relocation\n");
2005 err = -EINVAL;
2006 goto fail_trans_kthread;
2007 }
1911 } 2008 }
1912 2009
1913 location.objectid = BTRFS_FS_TREE_OBJECTID; 2010 location.objectid = BTRFS_FS_TREE_OBJECTID;
@@ -1917,6 +2014,17 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1917 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 2014 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
1918 if (!fs_info->fs_root) 2015 if (!fs_info->fs_root)
1919 goto fail_trans_kthread; 2016 goto fail_trans_kthread;
2017 if (IS_ERR(fs_info->fs_root)) {
2018 err = PTR_ERR(fs_info->fs_root);
2019 goto fail_trans_kthread;
2020 }
2021
2022 if (!(sb->s_flags & MS_RDONLY)) {
2023 down_read(&fs_info->cleanup_work_sem);
2024 btrfs_orphan_cleanup(fs_info->fs_root);
2025 btrfs_orphan_cleanup(fs_info->tree_root);
2026 up_read(&fs_info->cleanup_work_sem);
2027 }
1920 2028
1921 return tree_root; 2029 return tree_root;
1922 2030
@@ -1932,7 +2040,8 @@ fail_cleaner:
1932 filemap_write_and_wait(fs_info->btree_inode->i_mapping); 2040 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1933 invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2041 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
1934 2042
1935fail_csum_root: 2043fail_block_groups:
2044 btrfs_free_block_groups(fs_info);
1936 free_extent_buffer(csum_root->node); 2045 free_extent_buffer(csum_root->node);
1937 free_extent_buffer(csum_root->commit_root); 2046 free_extent_buffer(csum_root->commit_root);
1938fail_dev_root: 2047fail_dev_root:
@@ -1948,6 +2057,7 @@ fail_chunk_root:
1948 free_extent_buffer(chunk_root->node); 2057 free_extent_buffer(chunk_root->node);
1949 free_extent_buffer(chunk_root->commit_root); 2058 free_extent_buffer(chunk_root->commit_root);
1950fail_sb_buffer: 2059fail_sb_buffer:
2060 btrfs_stop_workers(&fs_info->generic_worker);
1951 btrfs_stop_workers(&fs_info->fixup_workers); 2061 btrfs_stop_workers(&fs_info->fixup_workers);
1952 btrfs_stop_workers(&fs_info->delalloc_workers); 2062 btrfs_stop_workers(&fs_info->delalloc_workers);
1953 btrfs_stop_workers(&fs_info->workers); 2063 btrfs_stop_workers(&fs_info->workers);
@@ -1955,6 +2065,7 @@ fail_sb_buffer:
1955 btrfs_stop_workers(&fs_info->endio_meta_workers); 2065 btrfs_stop_workers(&fs_info->endio_meta_workers);
1956 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2066 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
1957 btrfs_stop_workers(&fs_info->endio_write_workers); 2067 btrfs_stop_workers(&fs_info->endio_write_workers);
2068 btrfs_stop_workers(&fs_info->endio_freespace_worker);
1958 btrfs_stop_workers(&fs_info->submit_workers); 2069 btrfs_stop_workers(&fs_info->submit_workers);
1959fail_iput: 2070fail_iput:
1960 invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2071 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
@@ -1964,6 +2075,8 @@ fail_iput:
1964 btrfs_mapping_tree_free(&fs_info->mapping_tree); 2075 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1965fail_bdi: 2076fail_bdi:
1966 bdi_destroy(&fs_info->bdi); 2077 bdi_destroy(&fs_info->bdi);
2078fail_srcu:
2079 cleanup_srcu_struct(&fs_info->subvol_srcu);
1967fail: 2080fail:
1968 kfree(extent_root); 2081 kfree(extent_root);
1969 kfree(tree_root); 2082 kfree(tree_root);
@@ -1981,7 +2094,7 @@ static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
1981 if (uptodate) { 2094 if (uptodate) {
1982 set_buffer_uptodate(bh); 2095 set_buffer_uptodate(bh);
1983 } else { 2096 } else {
1984 if (!buffer_eopnotsupp(bh) && printk_ratelimit()) { 2097 if (printk_ratelimit()) {
1985 printk(KERN_WARNING "lost page write due to " 2098 printk(KERN_WARNING "lost page write due to "
1986 "I/O error on %s\n", 2099 "I/O error on %s\n",
1987 bdevname(bh->b_bdev, b)); 2100 bdevname(bh->b_bdev, b));
@@ -2118,21 +2231,10 @@ static int write_dev_supers(struct btrfs_device *device,
2118 bh->b_end_io = btrfs_end_buffer_write_sync; 2231 bh->b_end_io = btrfs_end_buffer_write_sync;
2119 } 2232 }
2120 2233
2121 if (i == last_barrier && do_barriers && device->barriers) { 2234 if (i == last_barrier && do_barriers)
2122 ret = submit_bh(WRITE_BARRIER, bh); 2235 ret = submit_bh(WRITE_FLUSH_FUA, bh);
2123 if (ret == -EOPNOTSUPP) { 2236 else
2124 printk("btrfs: disabling barriers on dev %s\n",
2125 device->name);
2126 set_buffer_uptodate(bh);
2127 device->barriers = 0;
2128 /* one reference for submit_bh */
2129 get_bh(bh);
2130 lock_buffer(bh);
2131 ret = submit_bh(WRITE_SYNC, bh);
2132 }
2133 } else {
2134 ret = submit_bh(WRITE_SYNC, bh); 2237 ret = submit_bh(WRITE_SYNC, bh);
2135 }
2136 2238
2137 if (ret) 2239 if (ret)
2138 errors++; 2240 errors++;
@@ -2223,20 +2325,29 @@ int write_ctree_super(struct btrfs_trans_handle *trans,
2223 2325
2224int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 2326int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2225{ 2327{
2226 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 2328 spin_lock(&fs_info->fs_roots_radix_lock);
2227 radix_tree_delete(&fs_info->fs_roots_radix, 2329 radix_tree_delete(&fs_info->fs_roots_radix,
2228 (unsigned long)root->root_key.objectid); 2330 (unsigned long)root->root_key.objectid);
2331 spin_unlock(&fs_info->fs_roots_radix_lock);
2332
2333 if (btrfs_root_refs(&root->root_item) == 0)
2334 synchronize_srcu(&fs_info->subvol_srcu);
2335
2336 free_fs_root(root);
2337 return 0;
2338}
2339
2340static void free_fs_root(struct btrfs_root *root)
2341{
2342 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2229 if (root->anon_super.s_dev) { 2343 if (root->anon_super.s_dev) {
2230 down_write(&root->anon_super.s_umount); 2344 down_write(&root->anon_super.s_umount);
2231 kill_anon_super(&root->anon_super); 2345 kill_anon_super(&root->anon_super);
2232 } 2346 }
2233 if (root->node) 2347 free_extent_buffer(root->node);
2234 free_extent_buffer(root->node); 2348 free_extent_buffer(root->commit_root);
2235 if (root->commit_root)
2236 free_extent_buffer(root->commit_root);
2237 kfree(root->name); 2349 kfree(root->name);
2238 kfree(root); 2350 kfree(root);
2239 return 0;
2240} 2351}
2241 2352
2242static int del_fs_roots(struct btrfs_fs_info *fs_info) 2353static int del_fs_roots(struct btrfs_fs_info *fs_info)
@@ -2245,6 +2356,20 @@ static int del_fs_roots(struct btrfs_fs_info *fs_info)
2245 struct btrfs_root *gang[8]; 2356 struct btrfs_root *gang[8];
2246 int i; 2357 int i;
2247 2358
2359 while (!list_empty(&fs_info->dead_roots)) {
2360 gang[0] = list_entry(fs_info->dead_roots.next,
2361 struct btrfs_root, root_list);
2362 list_del(&gang[0]->root_list);
2363
2364 if (gang[0]->in_radix) {
2365 btrfs_free_fs_root(fs_info, gang[0]);
2366 } else {
2367 free_extent_buffer(gang[0]->node);
2368 free_extent_buffer(gang[0]->commit_root);
2369 kfree(gang[0]);
2370 }
2371 }
2372
2248 while (1) { 2373 while (1) {
2249 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2374 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2250 (void **)gang, 0, 2375 (void **)gang, 0,
@@ -2274,9 +2399,6 @@ int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2274 root_objectid = gang[ret - 1]->root_key.objectid + 1; 2399 root_objectid = gang[ret - 1]->root_key.objectid + 1;
2275 for (i = 0; i < ret; i++) { 2400 for (i = 0; i < ret; i++) {
2276 root_objectid = gang[i]->root_key.objectid; 2401 root_objectid = gang[i]->root_key.objectid;
2277 ret = btrfs_find_dead_roots(fs_info->tree_root,
2278 root_objectid);
2279 BUG_ON(ret);
2280 btrfs_orphan_cleanup(gang[i]); 2402 btrfs_orphan_cleanup(gang[i]);
2281 } 2403 }
2282 root_objectid++; 2404 root_objectid++;
@@ -2290,13 +2412,19 @@ int btrfs_commit_super(struct btrfs_root *root)
2290 int ret; 2412 int ret;
2291 2413
2292 mutex_lock(&root->fs_info->cleaner_mutex); 2414 mutex_lock(&root->fs_info->cleaner_mutex);
2415 btrfs_run_delayed_iputs(root);
2293 btrfs_clean_old_snapshots(root); 2416 btrfs_clean_old_snapshots(root);
2294 mutex_unlock(&root->fs_info->cleaner_mutex); 2417 mutex_unlock(&root->fs_info->cleaner_mutex);
2295 trans = btrfs_start_transaction(root, 1); 2418
2419 /* wait until ongoing cleanup work done */
2420 down_write(&root->fs_info->cleanup_work_sem);
2421 up_write(&root->fs_info->cleanup_work_sem);
2422
2423 trans = btrfs_join_transaction(root, 1);
2296 ret = btrfs_commit_transaction(trans, root); 2424 ret = btrfs_commit_transaction(trans, root);
2297 BUG_ON(ret); 2425 BUG_ON(ret);
2298 /* run commit again to drop the original snapshot */ 2426 /* run commit again to drop the original snapshot */
2299 trans = btrfs_start_transaction(root, 1); 2427 trans = btrfs_join_transaction(root, 1);
2300 btrfs_commit_transaction(trans, root); 2428 btrfs_commit_transaction(trans, root);
2301 ret = btrfs_write_and_wait_transaction(NULL, root); 2429 ret = btrfs_write_and_wait_transaction(NULL, root);
2302 BUG_ON(ret); 2430 BUG_ON(ret);
@@ -2313,15 +2441,19 @@ int close_ctree(struct btrfs_root *root)
2313 fs_info->closing = 1; 2441 fs_info->closing = 1;
2314 smp_mb(); 2442 smp_mb();
2315 2443
2316 kthread_stop(root->fs_info->transaction_kthread); 2444 btrfs_put_block_group_cache(fs_info);
2317 kthread_stop(root->fs_info->cleaner_kthread);
2318
2319 if (!(fs_info->sb->s_flags & MS_RDONLY)) { 2445 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
2320 ret = btrfs_commit_super(root); 2446 ret = btrfs_commit_super(root);
2321 if (ret) 2447 if (ret)
2322 printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 2448 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2323 } 2449 }
2324 2450
2451 kthread_stop(root->fs_info->transaction_kthread);
2452 kthread_stop(root->fs_info->cleaner_kthread);
2453
2454 fs_info->closing = 2;
2455 smp_mb();
2456
2325 if (fs_info->delalloc_bytes) { 2457 if (fs_info->delalloc_bytes) {
2326 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n", 2458 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
2327 (unsigned long long)fs_info->delalloc_bytes); 2459 (unsigned long long)fs_info->delalloc_bytes);
@@ -2348,6 +2480,7 @@ int close_ctree(struct btrfs_root *root)
2348 2480
2349 iput(fs_info->btree_inode); 2481 iput(fs_info->btree_inode);
2350 2482
2483 btrfs_stop_workers(&fs_info->generic_worker);
2351 btrfs_stop_workers(&fs_info->fixup_workers); 2484 btrfs_stop_workers(&fs_info->fixup_workers);
2352 btrfs_stop_workers(&fs_info->delalloc_workers); 2485 btrfs_stop_workers(&fs_info->delalloc_workers);
2353 btrfs_stop_workers(&fs_info->workers); 2486 btrfs_stop_workers(&fs_info->workers);
@@ -2355,12 +2488,14 @@ int close_ctree(struct btrfs_root *root)
2355 btrfs_stop_workers(&fs_info->endio_meta_workers); 2488 btrfs_stop_workers(&fs_info->endio_meta_workers);
2356 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2489 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2357 btrfs_stop_workers(&fs_info->endio_write_workers); 2490 btrfs_stop_workers(&fs_info->endio_write_workers);
2491 btrfs_stop_workers(&fs_info->endio_freespace_worker);
2358 btrfs_stop_workers(&fs_info->submit_workers); 2492 btrfs_stop_workers(&fs_info->submit_workers);
2359 2493
2360 btrfs_close_devices(fs_info->fs_devices); 2494 btrfs_close_devices(fs_info->fs_devices);
2361 btrfs_mapping_tree_free(&fs_info->mapping_tree); 2495 btrfs_mapping_tree_free(&fs_info->mapping_tree);
2362 2496
2363 bdi_destroy(&fs_info->bdi); 2497 bdi_destroy(&fs_info->bdi);
2498 cleanup_srcu_struct(&fs_info->subvol_srcu);
2364 2499
2365 kfree(fs_info->extent_root); 2500 kfree(fs_info->extent_root);
2366 kfree(fs_info->tree_root); 2501 kfree(fs_info->tree_root);
@@ -2375,7 +2510,8 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
2375 int ret; 2510 int ret;
2376 struct inode *btree_inode = buf->first_page->mapping->host; 2511 struct inode *btree_inode = buf->first_page->mapping->host;
2377 2512
2378 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); 2513 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf,
2514 NULL);
2379 if (!ret) 2515 if (!ret)
2380 return ret; 2516 return ret;
2381 2517