aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c263
1 files changed, 207 insertions, 56 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 7ae51decf6d3..fadeba6a5db9 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -44,6 +44,7 @@
44#include "free-space-cache.h" 44#include "free-space-cache.h"
45#include "inode-map.h" 45#include "inode-map.h"
46#include "check-integrity.h" 46#include "check-integrity.h"
47#include "rcu-string.h"
47 48
48static struct extent_io_ops btree_extent_io_ops; 49static struct extent_io_ops btree_extent_io_ops;
49static void end_workqueue_fn(struct btrfs_work *work); 50static void end_workqueue_fn(struct btrfs_work *work);
@@ -406,7 +407,7 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,
406 break; 407 break;
407 } 408 }
408 409
409 if (failed && !ret) 410 if (failed && !ret && failed_mirror)
410 repair_eb_io_failure(root, eb, failed_mirror); 411 repair_eb_io_failure(root, eb, failed_mirror);
411 412
412 return ret; 413 return ret;
@@ -1113,7 +1114,7 @@ void clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1113 spin_unlock(&root->fs_info->delalloc_lock); 1114 spin_unlock(&root->fs_info->delalloc_lock);
1114 btrfs_panic(root->fs_info, -EOVERFLOW, 1115 btrfs_panic(root->fs_info, -EOVERFLOW,
1115 "Can't clear %lu bytes from " 1116 "Can't clear %lu bytes from "
1116 " dirty_mdatadata_bytes (%lu)", 1117 " dirty_mdatadata_bytes (%llu)",
1117 buf->len, 1118 buf->len,
1118 root->fs_info->dirty_metadata_bytes); 1119 root->fs_info->dirty_metadata_bytes);
1119 } 1120 }
@@ -1181,6 +1182,8 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
1181 root->defrag_running = 0; 1182 root->defrag_running = 0;
1182 root->root_key.objectid = objectid; 1183 root->root_key.objectid = objectid;
1183 root->anon_dev = 0; 1184 root->anon_dev = 0;
1185
1186 spin_lock_init(&root->root_times_lock);
1184} 1187}
1185 1188
1186static int __must_check find_and_setup_root(struct btrfs_root *tree_root, 1189static int __must_check find_and_setup_root(struct btrfs_root *tree_root,
@@ -1224,6 +1227,82 @@ static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info)
1224 return root; 1227 return root;
1225} 1228}
1226 1229
1230struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1231 struct btrfs_fs_info *fs_info,
1232 u64 objectid)
1233{
1234 struct extent_buffer *leaf;
1235 struct btrfs_root *tree_root = fs_info->tree_root;
1236 struct btrfs_root *root;
1237 struct btrfs_key key;
1238 int ret = 0;
1239 u64 bytenr;
1240
1241 root = btrfs_alloc_root(fs_info);
1242 if (!root)
1243 return ERR_PTR(-ENOMEM);
1244
1245 __setup_root(tree_root->nodesize, tree_root->leafsize,
1246 tree_root->sectorsize, tree_root->stripesize,
1247 root, fs_info, objectid);
1248 root->root_key.objectid = objectid;
1249 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1250 root->root_key.offset = 0;
1251
1252 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
1253 0, objectid, NULL, 0, 0, 0);
1254 if (IS_ERR(leaf)) {
1255 ret = PTR_ERR(leaf);
1256 goto fail;
1257 }
1258
1259 bytenr = leaf->start;
1260 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1261 btrfs_set_header_bytenr(leaf, leaf->start);
1262 btrfs_set_header_generation(leaf, trans->transid);
1263 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1264 btrfs_set_header_owner(leaf, objectid);
1265 root->node = leaf;
1266
1267 write_extent_buffer(leaf, fs_info->fsid,
1268 (unsigned long)btrfs_header_fsid(leaf),
1269 BTRFS_FSID_SIZE);
1270 write_extent_buffer(leaf, fs_info->chunk_tree_uuid,
1271 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
1272 BTRFS_UUID_SIZE);
1273 btrfs_mark_buffer_dirty(leaf);
1274
1275 root->commit_root = btrfs_root_node(root);
1276 root->track_dirty = 1;
1277
1278
1279 root->root_item.flags = 0;
1280 root->root_item.byte_limit = 0;
1281 btrfs_set_root_bytenr(&root->root_item, leaf->start);
1282 btrfs_set_root_generation(&root->root_item, trans->transid);
1283 btrfs_set_root_level(&root->root_item, 0);
1284 btrfs_set_root_refs(&root->root_item, 1);
1285 btrfs_set_root_used(&root->root_item, leaf->len);
1286 btrfs_set_root_last_snapshot(&root->root_item, 0);
1287 btrfs_set_root_dirid(&root->root_item, 0);
1288 root->root_item.drop_level = 0;
1289
1290 key.objectid = objectid;
1291 key.type = BTRFS_ROOT_ITEM_KEY;
1292 key.offset = 0;
1293 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
1294 if (ret)
1295 goto fail;
1296
1297 btrfs_tree_unlock(leaf);
1298
1299fail:
1300 if (ret)
1301 return ERR_PTR(ret);
1302
1303 return root;
1304}
1305
1227static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1306static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1228 struct btrfs_fs_info *fs_info) 1307 struct btrfs_fs_info *fs_info)
1229{ 1308{
@@ -1325,6 +1404,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1325 u64 generation; 1404 u64 generation;
1326 u32 blocksize; 1405 u32 blocksize;
1327 int ret = 0; 1406 int ret = 0;
1407 int slot;
1328 1408
1329 root = btrfs_alloc_root(fs_info); 1409 root = btrfs_alloc_root(fs_info);
1330 if (!root) 1410 if (!root)
@@ -1351,9 +1431,8 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1351 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 1431 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
1352 if (ret == 0) { 1432 if (ret == 0) {
1353 l = path->nodes[0]; 1433 l = path->nodes[0];
1354 read_extent_buffer(l, &root->root_item, 1434 slot = path->slots[0];
1355 btrfs_item_ptr_offset(l, path->slots[0]), 1435 btrfs_read_root_item(tree_root, l, slot, &root->root_item);
1356 sizeof(root->root_item));
1357 memcpy(&root->root_key, location, sizeof(*location)); 1436 memcpy(&root->root_key, location, sizeof(*location));
1358 } 1437 }
1359 btrfs_free_path(path); 1438 btrfs_free_path(path);
@@ -1395,6 +1474,9 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1395 return fs_info->dev_root; 1474 return fs_info->dev_root;
1396 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 1475 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1397 return fs_info->csum_root; 1476 return fs_info->csum_root;
1477 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1478 return fs_info->quota_root ? fs_info->quota_root :
1479 ERR_PTR(-ENOENT);
1398again: 1480again:
1399 spin_lock(&fs_info->fs_roots_radix_lock); 1481 spin_lock(&fs_info->fs_roots_radix_lock);
1400 root = radix_tree_lookup(&fs_info->fs_roots_radix, 1482 root = radix_tree_lookup(&fs_info->fs_roots_radix,
@@ -1822,6 +1904,10 @@ static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
1822 free_extent_buffer(info->extent_root->commit_root); 1904 free_extent_buffer(info->extent_root->commit_root);
1823 free_extent_buffer(info->csum_root->node); 1905 free_extent_buffer(info->csum_root->node);
1824 free_extent_buffer(info->csum_root->commit_root); 1906 free_extent_buffer(info->csum_root->commit_root);
1907 if (info->quota_root) {
1908 free_extent_buffer(info->quota_root->node);
1909 free_extent_buffer(info->quota_root->commit_root);
1910 }
1825 1911
1826 info->tree_root->node = NULL; 1912 info->tree_root->node = NULL;
1827 info->tree_root->commit_root = NULL; 1913 info->tree_root->commit_root = NULL;
@@ -1831,6 +1917,10 @@ static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
1831 info->extent_root->commit_root = NULL; 1917 info->extent_root->commit_root = NULL;
1832 info->csum_root->node = NULL; 1918 info->csum_root->node = NULL;
1833 info->csum_root->commit_root = NULL; 1919 info->csum_root->commit_root = NULL;
1920 if (info->quota_root) {
1921 info->quota_root->node = NULL;
1922 info->quota_root->commit_root = NULL;
1923 }
1834 1924
1835 if (chunk_root) { 1925 if (chunk_root) {
1836 free_extent_buffer(info->chunk_root->node); 1926 free_extent_buffer(info->chunk_root->node);
@@ -1861,6 +1951,7 @@ int open_ctree(struct super_block *sb,
1861 struct btrfs_root *csum_root; 1951 struct btrfs_root *csum_root;
1862 struct btrfs_root *chunk_root; 1952 struct btrfs_root *chunk_root;
1863 struct btrfs_root *dev_root; 1953 struct btrfs_root *dev_root;
1954 struct btrfs_root *quota_root;
1864 struct btrfs_root *log_tree_root; 1955 struct btrfs_root *log_tree_root;
1865 int ret; 1956 int ret;
1866 int err = -EINVAL; 1957 int err = -EINVAL;
@@ -1872,9 +1963,10 @@ int open_ctree(struct super_block *sb,
1872 csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); 1963 csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info);
1873 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); 1964 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info);
1874 dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); 1965 dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info);
1966 quota_root = fs_info->quota_root = btrfs_alloc_root(fs_info);
1875 1967
1876 if (!tree_root || !extent_root || !csum_root || 1968 if (!tree_root || !extent_root || !csum_root ||
1877 !chunk_root || !dev_root) { 1969 !chunk_root || !dev_root || !quota_root) {
1878 err = -ENOMEM; 1970 err = -ENOMEM;
1879 goto fail; 1971 goto fail;
1880 } 1972 }
@@ -1943,6 +2035,8 @@ int open_ctree(struct super_block *sb,
1943 fs_info->free_chunk_space = 0; 2035 fs_info->free_chunk_space = 0;
1944 fs_info->tree_mod_log = RB_ROOT; 2036 fs_info->tree_mod_log = RB_ROOT;
1945 2037
2038 init_waitqueue_head(&fs_info->tree_mod_seq_wait);
2039
1946 /* readahead state */ 2040 /* readahead state */
1947 INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT); 2041 INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_WAIT);
1948 spin_lock_init(&fs_info->reada_lock); 2042 spin_lock_init(&fs_info->reada_lock);
@@ -2031,6 +2125,13 @@ int open_ctree(struct super_block *sb,
2031 init_rwsem(&fs_info->cleanup_work_sem); 2125 init_rwsem(&fs_info->cleanup_work_sem);
2032 init_rwsem(&fs_info->subvol_sem); 2126 init_rwsem(&fs_info->subvol_sem);
2033 2127
2128 spin_lock_init(&fs_info->qgroup_lock);
2129 fs_info->qgroup_tree = RB_ROOT;
2130 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2131 fs_info->qgroup_seq = 1;
2132 fs_info->quota_enabled = 0;
2133 fs_info->pending_quota_state = 0;
2134
2034 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 2135 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2035 btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 2136 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2036 2137
@@ -2118,7 +2219,7 @@ int open_ctree(struct super_block *sb,
2118 2219
2119 features = btrfs_super_incompat_flags(disk_super); 2220 features = btrfs_super_incompat_flags(disk_super);
2120 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 2221 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
2121 if (tree_root->fs_info->compress_type & BTRFS_COMPRESS_LZO) 2222 if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
2122 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2223 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
2123 2224
2124 /* 2225 /*
@@ -2243,7 +2344,7 @@ int open_ctree(struct super_block *sb,
2243 ret |= btrfs_start_workers(&fs_info->caching_workers); 2344 ret |= btrfs_start_workers(&fs_info->caching_workers);
2244 ret |= btrfs_start_workers(&fs_info->readahead_workers); 2345 ret |= btrfs_start_workers(&fs_info->readahead_workers);
2245 if (ret) { 2346 if (ret) {
2246 ret = -ENOMEM; 2347 err = -ENOMEM;
2247 goto fail_sb_buffer; 2348 goto fail_sb_buffer;
2248 } 2349 }
2249 2350
@@ -2353,12 +2454,28 @@ retry_root_backup:
2353 BTRFS_CSUM_TREE_OBJECTID, csum_root); 2454 BTRFS_CSUM_TREE_OBJECTID, csum_root);
2354 if (ret) 2455 if (ret)
2355 goto recovery_tree_root; 2456 goto recovery_tree_root;
2356
2357 csum_root->track_dirty = 1; 2457 csum_root->track_dirty = 1;
2358 2458
2459 ret = find_and_setup_root(tree_root, fs_info,
2460 BTRFS_QUOTA_TREE_OBJECTID, quota_root);
2461 if (ret) {
2462 kfree(quota_root);
2463 quota_root = fs_info->quota_root = NULL;
2464 } else {
2465 quota_root->track_dirty = 1;
2466 fs_info->quota_enabled = 1;
2467 fs_info->pending_quota_state = 1;
2468 }
2469
2359 fs_info->generation = generation; 2470 fs_info->generation = generation;
2360 fs_info->last_trans_committed = generation; 2471 fs_info->last_trans_committed = generation;
2361 2472
2473 ret = btrfs_recover_balance(fs_info);
2474 if (ret) {
2475 printk(KERN_WARNING "btrfs: failed to recover balance\n");
2476 goto fail_block_groups;
2477 }
2478
2362 ret = btrfs_init_dev_stats(fs_info); 2479 ret = btrfs_init_dev_stats(fs_info);
2363 if (ret) { 2480 if (ret) {
2364 printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n", 2481 printk(KERN_ERR "btrfs: failed to init dev_stats: %d\n",
@@ -2409,6 +2526,9 @@ retry_root_backup:
2409 " integrity check module %s\n", sb->s_id); 2526 " integrity check module %s\n", sb->s_id);
2410 } 2527 }
2411#endif 2528#endif
2529 ret = btrfs_read_qgroup_config(fs_info);
2530 if (ret)
2531 goto fail_trans_kthread;
2412 2532
2413 /* do not make disk changes in broken FS */ 2533 /* do not make disk changes in broken FS */
2414 if (btrfs_super_log_root(disk_super) != 0 && 2534 if (btrfs_super_log_root(disk_super) != 0 &&
@@ -2419,7 +2539,7 @@ retry_root_backup:
2419 printk(KERN_WARNING "Btrfs log replay required " 2539 printk(KERN_WARNING "Btrfs log replay required "
2420 "on RO media\n"); 2540 "on RO media\n");
2421 err = -EIO; 2541 err = -EIO;
2422 goto fail_trans_kthread; 2542 goto fail_qgroup;
2423 } 2543 }
2424 blocksize = 2544 blocksize =
2425 btrfs_level_size(tree_root, 2545 btrfs_level_size(tree_root,
@@ -2428,7 +2548,7 @@ retry_root_backup:
2428 log_tree_root = btrfs_alloc_root(fs_info); 2548 log_tree_root = btrfs_alloc_root(fs_info);
2429 if (!log_tree_root) { 2549 if (!log_tree_root) {
2430 err = -ENOMEM; 2550 err = -ENOMEM;
2431 goto fail_trans_kthread; 2551 goto fail_qgroup;
2432 } 2552 }
2433 2553
2434 __setup_root(nodesize, leafsize, sectorsize, stripesize, 2554 __setup_root(nodesize, leafsize, sectorsize, stripesize,
@@ -2460,15 +2580,15 @@ retry_root_backup:
2460 2580
2461 if (!(sb->s_flags & MS_RDONLY)) { 2581 if (!(sb->s_flags & MS_RDONLY)) {
2462 ret = btrfs_cleanup_fs_roots(fs_info); 2582 ret = btrfs_cleanup_fs_roots(fs_info);
2463 if (ret) { 2583 if (ret)
2464 } 2584 goto fail_trans_kthread;
2465 2585
2466 ret = btrfs_recover_relocation(tree_root); 2586 ret = btrfs_recover_relocation(tree_root);
2467 if (ret < 0) { 2587 if (ret < 0) {
2468 printk(KERN_WARNING 2588 printk(KERN_WARNING
2469 "btrfs: failed to recover relocation\n"); 2589 "btrfs: failed to recover relocation\n");
2470 err = -EINVAL; 2590 err = -EINVAL;
2471 goto fail_trans_kthread; 2591 goto fail_qgroup;
2472 } 2592 }
2473 } 2593 }
2474 2594
@@ -2478,30 +2598,35 @@ retry_root_backup:
2478 2598
2479 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); 2599 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
2480 if (!fs_info->fs_root) 2600 if (!fs_info->fs_root)
2481 goto fail_trans_kthread; 2601 goto fail_qgroup;
2482 if (IS_ERR(fs_info->fs_root)) { 2602 if (IS_ERR(fs_info->fs_root)) {
2483 err = PTR_ERR(fs_info->fs_root); 2603 err = PTR_ERR(fs_info->fs_root);
2484 goto fail_trans_kthread; 2604 goto fail_qgroup;
2485 } 2605 }
2486 2606
2487 if (!(sb->s_flags & MS_RDONLY)) { 2607 if (sb->s_flags & MS_RDONLY)
2488 down_read(&fs_info->cleanup_work_sem); 2608 return 0;
2489 err = btrfs_orphan_cleanup(fs_info->fs_root);
2490 if (!err)
2491 err = btrfs_orphan_cleanup(fs_info->tree_root);
2492 up_read(&fs_info->cleanup_work_sem);
2493 2609
2494 if (!err) 2610 down_read(&fs_info->cleanup_work_sem);
2495 err = btrfs_recover_balance(fs_info->tree_root); 2611 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
2612 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
2613 up_read(&fs_info->cleanup_work_sem);
2614 close_ctree(tree_root);
2615 return ret;
2616 }
2617 up_read(&fs_info->cleanup_work_sem);
2496 2618
2497 if (err) { 2619 ret = btrfs_resume_balance_async(fs_info);
2498 close_ctree(tree_root); 2620 if (ret) {
2499 return err; 2621 printk(KERN_WARNING "btrfs: failed to resume balance\n");
2500 } 2622 close_ctree(tree_root);
2623 return ret;
2501 } 2624 }
2502 2625
2503 return 0; 2626 return 0;
2504 2627
2628fail_qgroup:
2629 btrfs_free_qgroup_config(fs_info);
2505fail_trans_kthread: 2630fail_trans_kthread:
2506 kthread_stop(fs_info->transaction_kthread); 2631 kthread_stop(fs_info->transaction_kthread);
2507fail_cleaner: 2632fail_cleaner:
@@ -2575,8 +2700,9 @@ static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
2575 struct btrfs_device *device = (struct btrfs_device *) 2700 struct btrfs_device *device = (struct btrfs_device *)
2576 bh->b_private; 2701 bh->b_private;
2577 2702
2578 printk_ratelimited(KERN_WARNING "lost page write due to " 2703 printk_ratelimited_in_rcu(KERN_WARNING "lost page write due to "
2579 "I/O error on %s\n", device->name); 2704 "I/O error on %s\n",
2705 rcu_str_deref(device->name));
2580 /* note, we dont' set_buffer_write_io_error because we have 2706 /* note, we dont' set_buffer_write_io_error because we have
2581 * our own ways of dealing with the IO errors 2707 * our own ways of dealing with the IO errors
2582 */ 2708 */
@@ -2749,8 +2875,8 @@ static int write_dev_flush(struct btrfs_device *device, int wait)
2749 wait_for_completion(&device->flush_wait); 2875 wait_for_completion(&device->flush_wait);
2750 2876
2751 if (bio_flagged(bio, BIO_EOPNOTSUPP)) { 2877 if (bio_flagged(bio, BIO_EOPNOTSUPP)) {
2752 printk("btrfs: disabling barriers on dev %s\n", 2878 printk_in_rcu("btrfs: disabling barriers on dev %s\n",
2753 device->name); 2879 rcu_str_deref(device->name));
2754 device->nobarriers = 1; 2880 device->nobarriers = 1;
2755 } 2881 }
2756 if (!bio_flagged(bio, BIO_UPTODATE)) { 2882 if (!bio_flagged(bio, BIO_UPTODATE)) {
@@ -3099,6 +3225,8 @@ int close_ctree(struct btrfs_root *root)
3099 fs_info->closing = 2; 3225 fs_info->closing = 2;
3100 smp_mb(); 3226 smp_mb();
3101 3227
3228 btrfs_free_qgroup_config(root->fs_info);
3229
3102 if (fs_info->delalloc_bytes) { 3230 if (fs_info->delalloc_bytes) {
3103 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n", 3231 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
3104 (unsigned long long)fs_info->delalloc_bytes); 3232 (unsigned long long)fs_info->delalloc_bytes);
@@ -3118,6 +3246,10 @@ int close_ctree(struct btrfs_root *root)
3118 free_extent_buffer(fs_info->dev_root->commit_root); 3246 free_extent_buffer(fs_info->dev_root->commit_root);
3119 free_extent_buffer(fs_info->csum_root->node); 3247 free_extent_buffer(fs_info->csum_root->node);
3120 free_extent_buffer(fs_info->csum_root->commit_root); 3248 free_extent_buffer(fs_info->csum_root->commit_root);
3249 if (fs_info->quota_root) {
3250 free_extent_buffer(fs_info->quota_root->node);
3251 free_extent_buffer(fs_info->quota_root->commit_root);
3252 }
3121 3253
3122 btrfs_free_block_groups(fs_info); 3254 btrfs_free_block_groups(fs_info);
3123 3255
@@ -3248,7 +3380,7 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
3248 return btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 3380 return btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
3249} 3381}
3250 3382
3251static int btree_lock_page_hook(struct page *page, void *data, 3383int btree_lock_page_hook(struct page *page, void *data,
3252 void (*flush_fn)(void *)) 3384 void (*flush_fn)(void *))
3253{ 3385{
3254 struct inode *inode = page->mapping->host; 3386 struct inode *inode = page->mapping->host;
@@ -3400,7 +3532,6 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3400 3532
3401 delayed_refs = &trans->delayed_refs; 3533 delayed_refs = &trans->delayed_refs;
3402 3534
3403again:
3404 spin_lock(&delayed_refs->lock); 3535 spin_lock(&delayed_refs->lock);
3405 if (delayed_refs->num_entries == 0) { 3536 if (delayed_refs->num_entries == 0) {
3406 spin_unlock(&delayed_refs->lock); 3537 spin_unlock(&delayed_refs->lock);
@@ -3408,31 +3539,37 @@ again:
3408 return ret; 3539 return ret;
3409 } 3540 }
3410 3541
3411 node = rb_first(&delayed_refs->root); 3542 while ((node = rb_first(&delayed_refs->root)) != NULL) {
3412 while (node) {
3413 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 3543 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3414 node = rb_next(node);
3415
3416 ref->in_tree = 0;
3417 rb_erase(&ref->rb_node, &delayed_refs->root);
3418 delayed_refs->num_entries--;
3419 3544
3420 atomic_set(&ref->refs, 1); 3545 atomic_set(&ref->refs, 1);
3421 if (btrfs_delayed_ref_is_head(ref)) { 3546 if (btrfs_delayed_ref_is_head(ref)) {
3422 struct btrfs_delayed_ref_head *head; 3547 struct btrfs_delayed_ref_head *head;
3423 3548
3424 head = btrfs_delayed_node_to_head(ref); 3549 head = btrfs_delayed_node_to_head(ref);
3425 spin_unlock(&delayed_refs->lock); 3550 if (!mutex_trylock(&head->mutex)) {
3426 mutex_lock(&head->mutex); 3551 atomic_inc(&ref->refs);
3552 spin_unlock(&delayed_refs->lock);
3553
3554 /* Need to wait for the delayed ref to run */
3555 mutex_lock(&head->mutex);
3556 mutex_unlock(&head->mutex);
3557 btrfs_put_delayed_ref(ref);
3558
3559 spin_lock(&delayed_refs->lock);
3560 continue;
3561 }
3562
3427 kfree(head->extent_op); 3563 kfree(head->extent_op);
3428 delayed_refs->num_heads--; 3564 delayed_refs->num_heads--;
3429 if (list_empty(&head->cluster)) 3565 if (list_empty(&head->cluster))
3430 delayed_refs->num_heads_ready--; 3566 delayed_refs->num_heads_ready--;
3431 list_del_init(&head->cluster); 3567 list_del_init(&head->cluster);
3432 mutex_unlock(&head->mutex);
3433 btrfs_put_delayed_ref(ref);
3434 goto again;
3435 } 3568 }
3569 ref->in_tree = 0;
3570 rb_erase(&ref->rb_node, &delayed_refs->root);
3571 delayed_refs->num_entries--;
3572
3436 spin_unlock(&delayed_refs->lock); 3573 spin_unlock(&delayed_refs->lock);
3437 btrfs_put_delayed_ref(ref); 3574 btrfs_put_delayed_ref(ref);
3438 3575
@@ -3520,11 +3657,9 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
3520 &(&BTRFS_I(page->mapping->host)->io_tree)->buffer, 3657 &(&BTRFS_I(page->mapping->host)->io_tree)->buffer,
3521 offset >> PAGE_CACHE_SHIFT); 3658 offset >> PAGE_CACHE_SHIFT);
3522 spin_unlock(&dirty_pages->buffer_lock); 3659 spin_unlock(&dirty_pages->buffer_lock);
3523 if (eb) { 3660 if (eb)
3524 ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY, 3661 ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY,
3525 &eb->bflags); 3662 &eb->bflags);
3526 atomic_set(&eb->refs, 1);
3527 }
3528 if (PageWriteback(page)) 3663 if (PageWriteback(page))
3529 end_page_writeback(page); 3664 end_page_writeback(page);
3530 3665
@@ -3538,8 +3673,8 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
3538 spin_unlock_irq(&page->mapping->tree_lock); 3673 spin_unlock_irq(&page->mapping->tree_lock);
3539 } 3674 }
3540 3675
3541 page->mapping->a_ops->invalidatepage(page, 0);
3542 unlock_page(page); 3676 unlock_page(page);
3677 page_cache_release(page);
3543 } 3678 }
3544 } 3679 }
3545 3680
@@ -3553,8 +3688,10 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
3553 u64 start; 3688 u64 start;
3554 u64 end; 3689 u64 end;
3555 int ret; 3690 int ret;
3691 bool loop = true;
3556 3692
3557 unpin = pinned_extents; 3693 unpin = pinned_extents;
3694again:
3558 while (1) { 3695 while (1) {
3559 ret = find_first_extent_bit(unpin, 0, &start, &end, 3696 ret = find_first_extent_bit(unpin, 0, &start, &end,
3560 EXTENT_DIRTY); 3697 EXTENT_DIRTY);
@@ -3572,6 +3709,15 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
3572 cond_resched(); 3709 cond_resched();
3573 } 3710 }
3574 3711
3712 if (loop) {
3713 if (unpin == &root->fs_info->freed_extents[0])
3714 unpin = &root->fs_info->freed_extents[1];
3715 else
3716 unpin = &root->fs_info->freed_extents[0];
3717 loop = false;
3718 goto again;
3719 }
3720
3575 return 0; 3721 return 0;
3576} 3722}
3577 3723
@@ -3585,21 +3731,23 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
3585 /* FIXME: cleanup wait for commit */ 3731 /* FIXME: cleanup wait for commit */
3586 cur_trans->in_commit = 1; 3732 cur_trans->in_commit = 1;
3587 cur_trans->blocked = 1; 3733 cur_trans->blocked = 1;
3588 if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3734 wake_up(&root->fs_info->transaction_blocked_wait);
3589 wake_up(&root->fs_info->transaction_blocked_wait);
3590 3735
3591 cur_trans->blocked = 0; 3736 cur_trans->blocked = 0;
3592 if (waitqueue_active(&root->fs_info->transaction_wait)) 3737 wake_up(&root->fs_info->transaction_wait);
3593 wake_up(&root->fs_info->transaction_wait);
3594 3738
3595 cur_trans->commit_done = 1; 3739 cur_trans->commit_done = 1;
3596 if (waitqueue_active(&cur_trans->commit_wait)) 3740 wake_up(&cur_trans->commit_wait);
3597 wake_up(&cur_trans->commit_wait); 3741
3742 btrfs_destroy_delayed_inodes(root);
3743 btrfs_assert_delayed_root_empty(root);
3598 3744
3599 btrfs_destroy_pending_snapshots(cur_trans); 3745 btrfs_destroy_pending_snapshots(cur_trans);
3600 3746
3601 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 3747 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
3602 EXTENT_DIRTY); 3748 EXTENT_DIRTY);
3749 btrfs_destroy_pinned_extent(root,
3750 root->fs_info->pinned_extents);
3603 3751
3604 /* 3752 /*
3605 memset(cur_trans, 0, sizeof(*cur_trans)); 3753 memset(cur_trans, 0, sizeof(*cur_trans));
@@ -3648,6 +3796,9 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
3648 if (waitqueue_active(&t->commit_wait)) 3796 if (waitqueue_active(&t->commit_wait))
3649 wake_up(&t->commit_wait); 3797 wake_up(&t->commit_wait);
3650 3798
3799 btrfs_destroy_delayed_inodes(root);
3800 btrfs_assert_delayed_root_empty(root);
3801
3651 btrfs_destroy_pending_snapshots(t); 3802 btrfs_destroy_pending_snapshots(t);
3652 3803
3653 btrfs_destroy_delalloc_inodes(root); 3804 btrfs_destroy_delalloc_inodes(root);