aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
authorMichal Marek <mmarek@suse.cz>2010-10-27 18:15:57 -0400
committerMichal Marek <mmarek@suse.cz>2010-10-27 18:15:57 -0400
commitb74b953b998bcc2db91b694446f3a2619ec32de6 (patch)
tree6ce24caabd730f6ae9287ed0676ec32e6ff31e9d /fs/btrfs/disk-io.c
parentabb438526201c6a79949ad45375c051b6681c253 (diff)
parentf6f94e2ab1b33f0082ac22d71f66385a60d8157f (diff)
Merge commit 'v2.6.36' into kbuild/misc
Update to be able to fix a recent change to scripts/basic/docproc.c (commit eda603f).
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c231
1 files changed, 114 insertions, 117 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 87b25543d7d1..64f10082f048 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -27,6 +27,7 @@
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>
30#include "compat.h" 31#include "compat.h"
31#include "ctree.h" 32#include "ctree.h"
32#include "disk-io.h" 33#include "disk-io.h"
@@ -43,8 +44,6 @@ static struct extent_io_ops btree_extent_io_ops;
43static void end_workqueue_fn(struct btrfs_work *work); 44static void end_workqueue_fn(struct btrfs_work *work);
44static void free_fs_root(struct btrfs_root *root); 45static void free_fs_root(struct btrfs_root *root);
45 46
46static atomic_t btrfs_bdi_num = ATOMIC_INIT(0);
47
48/* 47/*
49 * end_io_wq structs are used to do processing in task context when an IO is 48 * end_io_wq structs are used to do processing in task context when an IO is
50 * complete. This is used during reads to verify checksums, and it is used 49 * complete. This is used during reads to verify checksums, and it is used
@@ -75,6 +74,11 @@ struct async_submit_bio {
75 int rw; 74 int rw;
76 int mirror_num; 75 int mirror_num;
77 unsigned long bio_flags; 76 unsigned long bio_flags;
77 /*
78 * bio_offset is optional, can be used if the pages in the bio
79 * can't tell us where in the file the bio should go
80 */
81 u64 bio_offset;
78 struct btrfs_work work; 82 struct btrfs_work work;
79}; 83};
80 84
@@ -263,13 +267,15 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
263static int verify_parent_transid(struct extent_io_tree *io_tree, 267static int verify_parent_transid(struct extent_io_tree *io_tree,
264 struct extent_buffer *eb, u64 parent_transid) 268 struct extent_buffer *eb, u64 parent_transid)
265{ 269{
270 struct extent_state *cached_state = NULL;
266 int ret; 271 int ret;
267 272
268 if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 273 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
269 return 0; 274 return 0;
270 275
271 lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS); 276 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
272 if (extent_buffer_uptodate(io_tree, eb) && 277 0, &cached_state, GFP_NOFS);
278 if (extent_buffer_uptodate(io_tree, eb, cached_state) &&
273 btrfs_header_generation(eb) == parent_transid) { 279 btrfs_header_generation(eb) == parent_transid) {
274 ret = 0; 280 ret = 0;
275 goto out; 281 goto out;
@@ -282,10 +288,10 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
282 (unsigned long long)btrfs_header_generation(eb)); 288 (unsigned long long)btrfs_header_generation(eb));
283 } 289 }
284 ret = 1; 290 ret = 1;
285 clear_extent_buffer_uptodate(io_tree, eb); 291 clear_extent_buffer_uptodate(io_tree, eb, &cached_state);
286out: 292out:
287 unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, 293 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
288 GFP_NOFS); 294 &cached_state, GFP_NOFS);
289 return ret; 295 return ret;
290} 296}
291 297
@@ -474,7 +480,7 @@ static void end_workqueue_bio(struct bio *bio, int err)
474 end_io_wq->work.func = end_workqueue_fn; 480 end_io_wq->work.func = end_workqueue_fn;
475 end_io_wq->work.flags = 0; 481 end_io_wq->work.flags = 0;
476 482
477 if (bio->bi_rw & (1 << BIO_RW)) { 483 if (bio->bi_rw & REQ_WRITE) {
478 if (end_io_wq->metadata) 484 if (end_io_wq->metadata)
479 btrfs_queue_worker(&fs_info->endio_meta_write_workers, 485 btrfs_queue_worker(&fs_info->endio_meta_write_workers,
480 &end_io_wq->work); 486 &end_io_wq->work);
@@ -533,7 +539,8 @@ static void run_one_async_start(struct btrfs_work *work)
533 async = container_of(work, struct async_submit_bio, work); 539 async = container_of(work, struct async_submit_bio, work);
534 fs_info = BTRFS_I(async->inode)->root->fs_info; 540 fs_info = BTRFS_I(async->inode)->root->fs_info;
535 async->submit_bio_start(async->inode, async->rw, async->bio, 541 async->submit_bio_start(async->inode, async->rw, async->bio,
536 async->mirror_num, async->bio_flags); 542 async->mirror_num, async->bio_flags,
543 async->bio_offset);
537} 544}
538 545
539static void run_one_async_done(struct btrfs_work *work) 546static void run_one_async_done(struct btrfs_work *work)
@@ -555,7 +562,8 @@ static void run_one_async_done(struct btrfs_work *work)
555 wake_up(&fs_info->async_submit_wait); 562 wake_up(&fs_info->async_submit_wait);
556 563
557 async->submit_bio_done(async->inode, async->rw, async->bio, 564 async->submit_bio_done(async->inode, async->rw, async->bio,
558 async->mirror_num, async->bio_flags); 565 async->mirror_num, async->bio_flags,
566 async->bio_offset);
559} 567}
560 568
561static void run_one_async_free(struct btrfs_work *work) 569static void run_one_async_free(struct btrfs_work *work)
@@ -569,6 +577,7 @@ static void run_one_async_free(struct btrfs_work *work)
569int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode, 577int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
570 int rw, struct bio *bio, int mirror_num, 578 int rw, struct bio *bio, int mirror_num,
571 unsigned long bio_flags, 579 unsigned long bio_flags,
580 u64 bio_offset,
572 extent_submit_bio_hook_t *submit_bio_start, 581 extent_submit_bio_hook_t *submit_bio_start,
573 extent_submit_bio_hook_t *submit_bio_done) 582 extent_submit_bio_hook_t *submit_bio_done)
574{ 583{
@@ -591,10 +600,11 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
591 600
592 async->work.flags = 0; 601 async->work.flags = 0;
593 async->bio_flags = bio_flags; 602 async->bio_flags = bio_flags;
603 async->bio_offset = bio_offset;
594 604
595 atomic_inc(&fs_info->nr_async_submits); 605 atomic_inc(&fs_info->nr_async_submits);
596 606
597 if (rw & (1 << BIO_RW_SYNCIO)) 607 if (rw & REQ_SYNC)
598 btrfs_set_work_high_prio(&async->work); 608 btrfs_set_work_high_prio(&async->work);
599 609
600 btrfs_queue_worker(&fs_info->workers, &async->work); 610 btrfs_queue_worker(&fs_info->workers, &async->work);
@@ -626,7 +636,8 @@ static int btree_csum_one_bio(struct bio *bio)
626 636
627static int __btree_submit_bio_start(struct inode *inode, int rw, 637static int __btree_submit_bio_start(struct inode *inode, int rw,
628 struct bio *bio, int mirror_num, 638 struct bio *bio, int mirror_num,
629 unsigned long bio_flags) 639 unsigned long bio_flags,
640 u64 bio_offset)
630{ 641{
631 /* 642 /*
632 * when we're called for a write, we're already in the async 643 * when we're called for a write, we're already in the async
@@ -637,7 +648,8 @@ static int __btree_submit_bio_start(struct inode *inode, int rw,
637} 648}
638 649
639static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 650static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
640 int mirror_num, unsigned long bio_flags) 651 int mirror_num, unsigned long bio_flags,
652 u64 bio_offset)
641{ 653{
642 /* 654 /*
643 * when we're called for a write, we're already in the async 655 * when we're called for a write, we're already in the async
@@ -647,7 +659,8 @@ static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
647} 659}
648 660
649static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, 661static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
650 int mirror_num, unsigned long bio_flags) 662 int mirror_num, unsigned long bio_flags,
663 u64 bio_offset)
651{ 664{
652 int ret; 665 int ret;
653 666
@@ -655,7 +668,7 @@ static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
655 bio, 1); 668 bio, 1);
656 BUG_ON(ret); 669 BUG_ON(ret);
657 670
658 if (!(rw & (1 << BIO_RW))) { 671 if (!(rw & REQ_WRITE)) {
659 /* 672 /*
660 * called for a read, do the setup so that checksum validation 673 * called for a read, do the setup so that checksum validation
661 * can happen in the async kernel threads 674 * can happen in the async kernel threads
@@ -670,6 +683,7 @@ static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
670 */ 683 */
671 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 684 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
672 inode, rw, bio, mirror_num, 0, 685 inode, rw, bio, mirror_num, 0,
686 bio_offset,
673 __btree_submit_bio_start, 687 __btree_submit_bio_start,
674 __btree_submit_bio_done); 688 __btree_submit_bio_done);
675} 689}
@@ -893,7 +907,8 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
893 root->ref_cows = 0; 907 root->ref_cows = 0;
894 root->track_dirty = 0; 908 root->track_dirty = 0;
895 root->in_radix = 0; 909 root->in_radix = 0;
896 root->clean_orphans = 0; 910 root->orphan_item_inserted = 0;
911 root->orphan_cleanup_state = 0;
897 912
898 root->fs_info = fs_info; 913 root->fs_info = fs_info;
899 root->objectid = objectid; 914 root->objectid = objectid;
@@ -901,14 +916,17 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
901 root->highest_objectid = 0; 916 root->highest_objectid = 0;
902 root->name = NULL; 917 root->name = NULL;
903 root->in_sysfs = 0; 918 root->in_sysfs = 0;
904 root->inode_tree.rb_node = NULL; 919 root->inode_tree = RB_ROOT;
920 root->block_rsv = NULL;
921 root->orphan_block_rsv = NULL;
905 922
906 INIT_LIST_HEAD(&root->dirty_list); 923 INIT_LIST_HEAD(&root->dirty_list);
907 INIT_LIST_HEAD(&root->orphan_list); 924 INIT_LIST_HEAD(&root->orphan_list);
908 INIT_LIST_HEAD(&root->root_list); 925 INIT_LIST_HEAD(&root->root_list);
909 spin_lock_init(&root->node_lock); 926 spin_lock_init(&root->node_lock);
910 spin_lock_init(&root->list_lock); 927 spin_lock_init(&root->orphan_lock);
911 spin_lock_init(&root->inode_lock); 928 spin_lock_init(&root->inode_lock);
929 spin_lock_init(&root->accounting_lock);
912 mutex_init(&root->objectid_mutex); 930 mutex_init(&root->objectid_mutex);
913 mutex_init(&root->log_mutex); 931 mutex_init(&root->log_mutex);
914 init_waitqueue_head(&root->log_writer_wait); 932 init_waitqueue_head(&root->log_writer_wait);
@@ -967,42 +985,6 @@ static int find_and_setup_root(struct btrfs_root *tree_root,
967 return 0; 985 return 0;
968} 986}
969 987
970int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
971 struct btrfs_fs_info *fs_info)
972{
973 struct extent_buffer *eb;
974 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
975 u64 start = 0;
976 u64 end = 0;
977 int ret;
978
979 if (!log_root_tree)
980 return 0;
981
982 while (1) {
983 ret = find_first_extent_bit(&log_root_tree->dirty_log_pages,
984 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
985 if (ret)
986 break;
987
988 clear_extent_bits(&log_root_tree->dirty_log_pages, start, end,
989 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
990 }
991 eb = fs_info->log_root_tree->node;
992
993 WARN_ON(btrfs_header_level(eb) != 0);
994 WARN_ON(btrfs_header_nritems(eb) != 0);
995
996 ret = btrfs_free_reserved_extent(fs_info->tree_root,
997 eb->start, eb->len);
998 BUG_ON(ret);
999
1000 free_extent_buffer(eb);
1001 kfree(fs_info->log_root_tree);
1002 fs_info->log_root_tree = NULL;
1003 return 0;
1004}
1005
1006static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 988static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1007 struct btrfs_fs_info *fs_info) 989 struct btrfs_fs_info *fs_info)
1008{ 990{
@@ -1190,19 +1172,23 @@ again:
1190 if (root) 1172 if (root)
1191 return root; 1173 return root;
1192 1174
1193 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1194 if (ret == 0)
1195 ret = -ENOENT;
1196 if (ret < 0)
1197 return ERR_PTR(ret);
1198
1199 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 1175 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
1200 if (IS_ERR(root)) 1176 if (IS_ERR(root))
1201 return root; 1177 return root;
1202 1178
1203 WARN_ON(btrfs_root_refs(&root->root_item) == 0);
1204 set_anon_super(&root->anon_super, NULL); 1179 set_anon_super(&root->anon_super, NULL);
1205 1180
1181 if (btrfs_root_refs(&root->root_item) == 0) {
1182 ret = -ENOENT;
1183 goto fail;
1184 }
1185
1186 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1187 if (ret < 0)
1188 goto fail;
1189 if (ret == 0)
1190 root->orphan_item_inserted = 1;
1191
1206 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); 1192 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1207 if (ret) 1193 if (ret)
1208 goto fail; 1194 goto fail;
@@ -1211,10 +1197,9 @@ again:
1211 ret = radix_tree_insert(&fs_info->fs_roots_radix, 1197 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1212 (unsigned long)root->root_key.objectid, 1198 (unsigned long)root->root_key.objectid,
1213 root); 1199 root);
1214 if (ret == 0) { 1200 if (ret == 0)
1215 root->in_radix = 1; 1201 root->in_radix = 1;
1216 root->clean_orphans = 1; 1202
1217 }
1218 spin_unlock(&fs_info->fs_roots_radix_lock); 1203 spin_unlock(&fs_info->fs_roots_radix_lock);
1219 radix_tree_preload_end(); 1204 radix_tree_preload_end();
1220 if (ret) { 1205 if (ret) {
@@ -1372,19 +1357,11 @@ static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1372{ 1357{
1373 int err; 1358 int err;
1374 1359
1375 bdi->name = "btrfs";
1376 bdi->capabilities = BDI_CAP_MAP_COPY; 1360 bdi->capabilities = BDI_CAP_MAP_COPY;
1377 err = bdi_init(bdi); 1361 err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
1378 if (err) 1362 if (err)
1379 return err; 1363 return err;
1380 1364
1381 err = bdi_register(bdi, NULL, "btrfs-%d",
1382 atomic_inc_return(&btrfs_bdi_num));
1383 if (err) {
1384 bdi_destroy(bdi);
1385 return err;
1386 }
1387
1388 bdi->ra_pages = default_backing_dev_info.ra_pages; 1365 bdi->ra_pages = default_backing_dev_info.ra_pages;
1389 bdi->unplug_io_fn = btrfs_unplug_io_fn; 1366 bdi->unplug_io_fn = btrfs_unplug_io_fn;
1390 bdi->unplug_io_data = info; 1367 bdi->unplug_io_data = info;
@@ -1450,7 +1427,7 @@ static void end_workqueue_fn(struct btrfs_work *work)
1450 * ram and up to date before trying to verify things. For 1427 * ram and up to date before trying to verify things. For
1451 * blocksize <= pagesize, it is basically a noop 1428 * blocksize <= pagesize, it is basically a noop
1452 */ 1429 */
1453 if (!(bio->bi_rw & (1 << BIO_RW)) && end_io_wq->metadata && 1430 if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata &&
1454 !bio_ready_for_csum(bio)) { 1431 !bio_ready_for_csum(bio)) {
1455 btrfs_queue_worker(&fs_info->endio_meta_workers, 1432 btrfs_queue_worker(&fs_info->endio_meta_workers,
1456 &end_io_wq->work); 1433 &end_io_wq->work);
@@ -1468,10 +1445,6 @@ static int cleaner_kthread(void *arg)
1468 struct btrfs_root *root = arg; 1445 struct btrfs_root *root = arg;
1469 1446
1470 do { 1447 do {
1471 smp_mb();
1472 if (root->fs_info->closing)
1473 break;
1474
1475 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1448 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1476 1449
1477 if (!(root->fs_info->sb->s_flags & MS_RDONLY) && 1450 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
@@ -1484,11 +1457,9 @@ static int cleaner_kthread(void *arg)
1484 if (freezing(current)) { 1457 if (freezing(current)) {
1485 refrigerator(); 1458 refrigerator();
1486 } else { 1459 } else {
1487 smp_mb();
1488 if (root->fs_info->closing)
1489 break;
1490 set_current_state(TASK_INTERRUPTIBLE); 1460 set_current_state(TASK_INTERRUPTIBLE);
1491 schedule(); 1461 if (!kthread_should_stop())
1462 schedule();
1492 __set_current_state(TASK_RUNNING); 1463 __set_current_state(TASK_RUNNING);
1493 } 1464 }
1494 } while (!kthread_should_stop()); 1465 } while (!kthread_should_stop());
@@ -1500,36 +1471,40 @@ static int transaction_kthread(void *arg)
1500 struct btrfs_root *root = arg; 1471 struct btrfs_root *root = arg;
1501 struct btrfs_trans_handle *trans; 1472 struct btrfs_trans_handle *trans;
1502 struct btrfs_transaction *cur; 1473 struct btrfs_transaction *cur;
1474 u64 transid;
1503 unsigned long now; 1475 unsigned long now;
1504 unsigned long delay; 1476 unsigned long delay;
1505 int ret; 1477 int ret;
1506 1478
1507 do { 1479 do {
1508 smp_mb();
1509 if (root->fs_info->closing)
1510 break;
1511
1512 delay = HZ * 30; 1480 delay = HZ * 30;
1513 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1481 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1514 mutex_lock(&root->fs_info->transaction_kthread_mutex); 1482 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1515 1483
1516 mutex_lock(&root->fs_info->trans_mutex); 1484 spin_lock(&root->fs_info->new_trans_lock);
1517 cur = root->fs_info->running_transaction; 1485 cur = root->fs_info->running_transaction;
1518 if (!cur) { 1486 if (!cur) {
1519 mutex_unlock(&root->fs_info->trans_mutex); 1487 spin_unlock(&root->fs_info->new_trans_lock);
1520 goto sleep; 1488 goto sleep;
1521 } 1489 }
1522 1490
1523 now = get_seconds(); 1491 now = get_seconds();
1524 if (now < cur->start_time || now - cur->start_time < 30) { 1492 if (!cur->blocked &&
1525 mutex_unlock(&root->fs_info->trans_mutex); 1493 (now < cur->start_time || now - cur->start_time < 30)) {
1494 spin_unlock(&root->fs_info->new_trans_lock);
1526 delay = HZ * 5; 1495 delay = HZ * 5;
1527 goto sleep; 1496 goto sleep;
1528 } 1497 }
1529 mutex_unlock(&root->fs_info->trans_mutex); 1498 transid = cur->transid;
1530 trans = btrfs_start_transaction(root, 1); 1499 spin_unlock(&root->fs_info->new_trans_lock);
1531 ret = btrfs_commit_transaction(trans, root);
1532 1500
1501 trans = btrfs_join_transaction(root, 1);
1502 if (transid == trans->transid) {
1503 ret = btrfs_commit_transaction(trans, root);
1504 BUG_ON(ret);
1505 } else {
1506 btrfs_end_transaction(trans, root);
1507 }
1533sleep: 1508sleep:
1534 wake_up_process(root->fs_info->cleaner_kthread); 1509 wake_up_process(root->fs_info->cleaner_kthread);
1535 mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1510 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
@@ -1537,10 +1512,10 @@ sleep:
1537 if (freezing(current)) { 1512 if (freezing(current)) {
1538 refrigerator(); 1513 refrigerator();
1539 } else { 1514 } else {
1540 if (root->fs_info->closing)
1541 break;
1542 set_current_state(TASK_INTERRUPTIBLE); 1515 set_current_state(TASK_INTERRUPTIBLE);
1543 schedule_timeout(delay); 1516 if (!kthread_should_stop() &&
1517 !btrfs_transaction_blocked(root->fs_info))
1518 schedule_timeout(delay);
1544 __set_current_state(TASK_RUNNING); 1519 __set_current_state(TASK_RUNNING);
1545 } 1520 }
1546 } while (!kthread_should_stop()); 1521 } while (!kthread_should_stop());
@@ -1627,12 +1602,18 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1627 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 1602 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
1628 INIT_LIST_HEAD(&fs_info->space_info); 1603 INIT_LIST_HEAD(&fs_info->space_info);
1629 btrfs_mapping_init(&fs_info->mapping_tree); 1604 btrfs_mapping_init(&fs_info->mapping_tree);
1605 btrfs_init_block_rsv(&fs_info->global_block_rsv);
1606 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
1607 btrfs_init_block_rsv(&fs_info->trans_block_rsv);
1608 btrfs_init_block_rsv(&fs_info->chunk_block_rsv);
1609 btrfs_init_block_rsv(&fs_info->empty_block_rsv);
1610 INIT_LIST_HEAD(&fs_info->durable_block_rsv_list);
1611 mutex_init(&fs_info->durable_block_rsv_mutex);
1630 atomic_set(&fs_info->nr_async_submits, 0); 1612 atomic_set(&fs_info->nr_async_submits, 0);
1631 atomic_set(&fs_info->async_delalloc_pages, 0); 1613 atomic_set(&fs_info->async_delalloc_pages, 0);
1632 atomic_set(&fs_info->async_submit_draining, 0); 1614 atomic_set(&fs_info->async_submit_draining, 0);
1633 atomic_set(&fs_info->nr_async_bios, 0); 1615 atomic_set(&fs_info->nr_async_bios, 0);
1634 fs_info->sb = sb; 1616 fs_info->sb = sb;
1635 fs_info->max_extent = (u64)-1;
1636 fs_info->max_inline = 8192 * 1024; 1617 fs_info->max_inline = 8192 * 1024;
1637 fs_info->metadata_ratio = 0; 1618 fs_info->metadata_ratio = 0;
1638 1619
@@ -1673,7 +1654,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1673 insert_inode_hash(fs_info->btree_inode); 1654 insert_inode_hash(fs_info->btree_inode);
1674 1655
1675 spin_lock_init(&fs_info->block_group_cache_lock); 1656 spin_lock_init(&fs_info->block_group_cache_lock);
1676 fs_info->block_group_cache_tree.rb_node = NULL; 1657 fs_info->block_group_cache_tree = RB_ROOT;
1677 1658
1678 extent_io_tree_init(&fs_info->freed_extents[0], 1659 extent_io_tree_init(&fs_info->freed_extents[0],
1679 fs_info->btree_inode->i_mapping, GFP_NOFS); 1660 fs_info->btree_inode->i_mapping, GFP_NOFS);
@@ -1767,9 +1748,6 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1767 min_t(u64, fs_devices->num_devices, 1748 min_t(u64, fs_devices->num_devices,
1768 fs_info->thread_pool_size), 1749 fs_info->thread_pool_size),
1769 &fs_info->generic_worker); 1750 &fs_info->generic_worker);
1770 btrfs_init_workers(&fs_info->enospc_workers, "enospc",
1771 fs_info->thread_pool_size,
1772 &fs_info->generic_worker);
1773 1751
1774 /* a higher idle thresh on the submit workers makes it much more 1752 /* a higher idle thresh on the submit workers makes it much more
1775 * likely that bios will be send down in a sane order to the 1753 * likely that bios will be send down in a sane order to the
@@ -1817,7 +1795,6 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1817 btrfs_start_workers(&fs_info->endio_meta_workers, 1); 1795 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1818 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1); 1796 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1819 btrfs_start_workers(&fs_info->endio_write_workers, 1); 1797 btrfs_start_workers(&fs_info->endio_write_workers, 1);
1820 btrfs_start_workers(&fs_info->enospc_workers, 1);
1821 1798
1822 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 1799 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1823 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 1800 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
@@ -1920,17 +1897,22 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1920 1897
1921 csum_root->track_dirty = 1; 1898 csum_root->track_dirty = 1;
1922 1899
1923 btrfs_read_block_groups(extent_root);
1924
1925 fs_info->generation = generation; 1900 fs_info->generation = generation;
1926 fs_info->last_trans_committed = generation; 1901 fs_info->last_trans_committed = generation;
1927 fs_info->data_alloc_profile = (u64)-1; 1902 fs_info->data_alloc_profile = (u64)-1;
1928 fs_info->metadata_alloc_profile = (u64)-1; 1903 fs_info->metadata_alloc_profile = (u64)-1;
1929 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1904 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1905
1906 ret = btrfs_read_block_groups(extent_root);
1907 if (ret) {
1908 printk(KERN_ERR "Failed to read block groups: %d\n", ret);
1909 goto fail_block_groups;
1910 }
1911
1930 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root, 1912 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
1931 "btrfs-cleaner"); 1913 "btrfs-cleaner");
1932 if (IS_ERR(fs_info->cleaner_kthread)) 1914 if (IS_ERR(fs_info->cleaner_kthread))
1933 goto fail_csum_root; 1915 goto fail_block_groups;
1934 1916
1935 fs_info->transaction_kthread = kthread_run(transaction_kthread, 1917 fs_info->transaction_kthread = kthread_run(transaction_kthread,
1936 tree_root, 1918 tree_root,
@@ -1959,8 +1941,11 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1959 btrfs_level_size(tree_root, 1941 btrfs_level_size(tree_root,
1960 btrfs_super_log_root_level(disk_super)); 1942 btrfs_super_log_root_level(disk_super));
1961 1943
1962 log_tree_root = kzalloc(sizeof(struct btrfs_root), 1944 log_tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
1963 GFP_NOFS); 1945 if (!log_tree_root) {
1946 err = -ENOMEM;
1947 goto fail_trans_kthread;
1948 }
1964 1949
1965 __setup_root(nodesize, leafsize, sectorsize, stripesize, 1950 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1966 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 1951 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
@@ -1981,8 +1966,16 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1981 BUG_ON(ret); 1966 BUG_ON(ret);
1982 1967
1983 if (!(sb->s_flags & MS_RDONLY)) { 1968 if (!(sb->s_flags & MS_RDONLY)) {
1984 ret = btrfs_recover_relocation(tree_root); 1969 ret = btrfs_cleanup_fs_roots(fs_info);
1985 BUG_ON(ret); 1970 BUG_ON(ret);
1971
1972 ret = btrfs_recover_relocation(tree_root);
1973 if (ret < 0) {
1974 printk(KERN_WARNING
1975 "btrfs: failed to recover relocation\n");
1976 err = -EINVAL;
1977 goto fail_trans_kthread;
1978 }
1986 } 1979 }
1987 1980
1988 location.objectid = BTRFS_FS_TREE_OBJECTID; 1981 location.objectid = BTRFS_FS_TREE_OBJECTID;
@@ -1992,6 +1985,10 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1992 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 1985 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
1993 if (!fs_info->fs_root) 1986 if (!fs_info->fs_root)
1994 goto fail_trans_kthread; 1987 goto fail_trans_kthread;
1988 if (IS_ERR(fs_info->fs_root)) {
1989 err = PTR_ERR(fs_info->fs_root);
1990 goto fail_trans_kthread;
1991 }
1995 1992
1996 if (!(sb->s_flags & MS_RDONLY)) { 1993 if (!(sb->s_flags & MS_RDONLY)) {
1997 down_read(&fs_info->cleanup_work_sem); 1994 down_read(&fs_info->cleanup_work_sem);
@@ -2013,7 +2010,8 @@ fail_cleaner:
2013 filemap_write_and_wait(fs_info->btree_inode->i_mapping); 2010 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
2014 invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2011 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2015 2012
2016fail_csum_root: 2013fail_block_groups:
2014 btrfs_free_block_groups(fs_info);
2017 free_extent_buffer(csum_root->node); 2015 free_extent_buffer(csum_root->node);
2018 free_extent_buffer(csum_root->commit_root); 2016 free_extent_buffer(csum_root->commit_root);
2019fail_dev_root: 2017fail_dev_root:
@@ -2038,7 +2036,6 @@ fail_sb_buffer:
2038 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2036 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2039 btrfs_stop_workers(&fs_info->endio_write_workers); 2037 btrfs_stop_workers(&fs_info->endio_write_workers);
2040 btrfs_stop_workers(&fs_info->submit_workers); 2038 btrfs_stop_workers(&fs_info->submit_workers);
2041 btrfs_stop_workers(&fs_info->enospc_workers);
2042fail_iput: 2039fail_iput:
2043 invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2040 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2044 iput(fs_info->btree_inode); 2041 iput(fs_info->btree_inode);
@@ -2403,11 +2400,11 @@ int btrfs_commit_super(struct btrfs_root *root)
2403 down_write(&root->fs_info->cleanup_work_sem); 2400 down_write(&root->fs_info->cleanup_work_sem);
2404 up_write(&root->fs_info->cleanup_work_sem); 2401 up_write(&root->fs_info->cleanup_work_sem);
2405 2402
2406 trans = btrfs_start_transaction(root, 1); 2403 trans = btrfs_join_transaction(root, 1);
2407 ret = btrfs_commit_transaction(trans, root); 2404 ret = btrfs_commit_transaction(trans, root);
2408 BUG_ON(ret); 2405 BUG_ON(ret);
2409 /* run commit again to drop the original snapshot */ 2406 /* run commit again to drop the original snapshot */
2410 trans = btrfs_start_transaction(root, 1); 2407 trans = btrfs_join_transaction(root, 1);
2411 btrfs_commit_transaction(trans, root); 2408 btrfs_commit_transaction(trans, root);
2412 ret = btrfs_write_and_wait_transaction(NULL, root); 2409 ret = btrfs_write_and_wait_transaction(NULL, root);
2413 BUG_ON(ret); 2410 BUG_ON(ret);
@@ -2424,15 +2421,15 @@ int close_ctree(struct btrfs_root *root)
2424 fs_info->closing = 1; 2421 fs_info->closing = 1;
2425 smp_mb(); 2422 smp_mb();
2426 2423
2427 kthread_stop(root->fs_info->transaction_kthread);
2428 kthread_stop(root->fs_info->cleaner_kthread);
2429
2430 if (!(fs_info->sb->s_flags & MS_RDONLY)) { 2424 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
2431 ret = btrfs_commit_super(root); 2425 ret = btrfs_commit_super(root);
2432 if (ret) 2426 if (ret)
2433 printk(KERN_ERR "btrfs: commit super ret %d\n", ret); 2427 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2434 } 2428 }
2435 2429
2430 kthread_stop(root->fs_info->transaction_kthread);
2431 kthread_stop(root->fs_info->cleaner_kthread);
2432
2436 fs_info->closing = 2; 2433 fs_info->closing = 2;
2437 smp_mb(); 2434 smp_mb();
2438 2435
@@ -2471,7 +2468,6 @@ int close_ctree(struct btrfs_root *root)
2471 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2468 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2472 btrfs_stop_workers(&fs_info->endio_write_workers); 2469 btrfs_stop_workers(&fs_info->endio_write_workers);
2473 btrfs_stop_workers(&fs_info->submit_workers); 2470 btrfs_stop_workers(&fs_info->submit_workers);
2474 btrfs_stop_workers(&fs_info->enospc_workers);
2475 2471
2476 btrfs_close_devices(fs_info->fs_devices); 2472 btrfs_close_devices(fs_info->fs_devices);
2477 btrfs_mapping_tree_free(&fs_info->mapping_tree); 2473 btrfs_mapping_tree_free(&fs_info->mapping_tree);
@@ -2492,7 +2488,8 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
2492 int ret; 2488 int ret;
2493 struct inode *btree_inode = buf->first_page->mapping->host; 2489 struct inode *btree_inode = buf->first_page->mapping->host;
2494 2490
2495 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); 2491 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf,
2492 NULL);
2496 if (!ret) 2493 if (!ret)
2497 return ret; 2494 return ret;
2498 2495