aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/ctree.c30
-rw-r--r--fs/btrfs/delayed-inode.c151
-rw-r--r--fs/btrfs/delayed-inode.h2
-rw-r--r--fs/btrfs/disk-io.c30
-rw-r--r--fs/btrfs/extent-tree.c89
-rw-r--r--fs/btrfs/extent_io.c33
-rw-r--r--fs/btrfs/extent_io.h2
-rw-r--r--fs/btrfs/file-item.c6
-rw-r--r--fs/btrfs/file.c10
-rw-r--r--fs/btrfs/inode.c31
-rw-r--r--fs/btrfs/ioctl.c18
-rw-r--r--fs/btrfs/locking.h1
-rw-r--r--fs/btrfs/ordered-data.c2
-rw-r--r--fs/btrfs/qgroup.c13
-rw-r--r--fs/btrfs/relocation.c74
-rw-r--r--fs/btrfs/scrub.c3
-rw-r--r--fs/btrfs/send.c10
-rw-r--r--fs/btrfs/super.c1
-rw-r--r--fs/btrfs/transaction.c76
-rw-r--r--fs/btrfs/tree-log.c5
-rw-r--r--fs/btrfs/volumes.c33
21 files changed, 435 insertions, 185 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index ecd25a1b4e51..ca9d8f1a3bb6 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -651,6 +651,8 @@ tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
651 if (tree_mod_dont_log(fs_info, NULL)) 651 if (tree_mod_dont_log(fs_info, NULL))
652 return 0; 652 return 0;
653 653
654 __tree_mod_log_free_eb(fs_info, old_root);
655
654 ret = tree_mod_alloc(fs_info, flags, &tm); 656 ret = tree_mod_alloc(fs_info, flags, &tm);
655 if (ret < 0) 657 if (ret < 0)
656 goto out; 658 goto out;
@@ -736,7 +738,7 @@ tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
736static noinline void 738static noinline void
737tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, 739tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
738 struct extent_buffer *src, unsigned long dst_offset, 740 struct extent_buffer *src, unsigned long dst_offset,
739 unsigned long src_offset, int nr_items) 741 unsigned long src_offset, int nr_items, int log_removal)
740{ 742{
741 int ret; 743 int ret;
742 int i; 744 int i;
@@ -750,10 +752,12 @@ tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
750 } 752 }
751 753
752 for (i = 0; i < nr_items; i++) { 754 for (i = 0; i < nr_items; i++) {
753 ret = tree_mod_log_insert_key_locked(fs_info, src, 755 if (log_removal) {
754 i + src_offset, 756 ret = tree_mod_log_insert_key_locked(fs_info, src,
755 MOD_LOG_KEY_REMOVE); 757 i + src_offset,
756 BUG_ON(ret < 0); 758 MOD_LOG_KEY_REMOVE);
759 BUG_ON(ret < 0);
760 }
757 ret = tree_mod_log_insert_key_locked(fs_info, dst, 761 ret = tree_mod_log_insert_key_locked(fs_info, dst,
758 i + dst_offset, 762 i + dst_offset,
759 MOD_LOG_KEY_ADD); 763 MOD_LOG_KEY_ADD);
@@ -927,7 +931,6 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
927 ret = btrfs_dec_ref(trans, root, buf, 1, 1); 931 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
928 BUG_ON(ret); /* -ENOMEM */ 932 BUG_ON(ret); /* -ENOMEM */
929 } 933 }
930 tree_mod_log_free_eb(root->fs_info, buf);
931 clean_tree_block(trans, root, buf); 934 clean_tree_block(trans, root, buf);
932 *last_ref = 1; 935 *last_ref = 1;
933 } 936 }
@@ -1046,6 +1049,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1046 btrfs_set_node_ptr_generation(parent, parent_slot, 1049 btrfs_set_node_ptr_generation(parent, parent_slot,
1047 trans->transid); 1050 trans->transid);
1048 btrfs_mark_buffer_dirty(parent); 1051 btrfs_mark_buffer_dirty(parent);
1052 tree_mod_log_free_eb(root->fs_info, buf);
1049 btrfs_free_tree_block(trans, root, buf, parent_start, 1053 btrfs_free_tree_block(trans, root, buf, parent_start,
1050 last_ref); 1054 last_ref);
1051 } 1055 }
@@ -1750,7 +1754,6 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1750 goto enospc; 1754 goto enospc;
1751 } 1755 }
1752 1756
1753 tree_mod_log_free_eb(root->fs_info, root->node);
1754 tree_mod_log_set_root_pointer(root, child); 1757 tree_mod_log_set_root_pointer(root, child);
1755 rcu_assign_pointer(root->node, child); 1758 rcu_assign_pointer(root->node, child);
1756 1759
@@ -2995,7 +2998,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
2995 push_items = min(src_nritems - 8, push_items); 2998 push_items = min(src_nritems - 8, push_items);
2996 2999
2997 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, 3000 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2998 push_items); 3001 push_items, 1);
2999 copy_extent_buffer(dst, src, 3002 copy_extent_buffer(dst, src,
3000 btrfs_node_key_ptr_offset(dst_nritems), 3003 btrfs_node_key_ptr_offset(dst_nritems),
3001 btrfs_node_key_ptr_offset(0), 3004 btrfs_node_key_ptr_offset(0),
@@ -3066,7 +3069,7 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
3066 sizeof(struct btrfs_key_ptr)); 3069 sizeof(struct btrfs_key_ptr));
3067 3070
3068 tree_mod_log_eb_copy(root->fs_info, dst, src, 0, 3071 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3069 src_nritems - push_items, push_items); 3072 src_nritems - push_items, push_items, 1);
3070 copy_extent_buffer(dst, src, 3073 copy_extent_buffer(dst, src,
3071 btrfs_node_key_ptr_offset(0), 3074 btrfs_node_key_ptr_offset(0),
3072 btrfs_node_key_ptr_offset(src_nritems - push_items), 3075 btrfs_node_key_ptr_offset(src_nritems - push_items),
@@ -3218,12 +3221,18 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3218 int mid; 3221 int mid;
3219 int ret; 3222 int ret;
3220 u32 c_nritems; 3223 u32 c_nritems;
3224 int tree_mod_log_removal = 1;
3221 3225
3222 c = path->nodes[level]; 3226 c = path->nodes[level];
3223 WARN_ON(btrfs_header_generation(c) != trans->transid); 3227 WARN_ON(btrfs_header_generation(c) != trans->transid);
3224 if (c == root->node) { 3228 if (c == root->node) {
3225 /* trying to split the root, lets make a new one */ 3229 /* trying to split the root, lets make a new one */
3226 ret = insert_new_root(trans, root, path, level + 1); 3230 ret = insert_new_root(trans, root, path, level + 1);
3231 /*
3232 * removal of root nodes has been logged by
3233 * tree_mod_log_set_root_pointer due to locking
3234 */
3235 tree_mod_log_removal = 0;
3227 if (ret) 3236 if (ret)
3228 return ret; 3237 return ret;
3229 } else { 3238 } else {
@@ -3261,7 +3270,8 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3261 (unsigned long)btrfs_header_chunk_tree_uuid(split), 3270 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3262 BTRFS_UUID_SIZE); 3271 BTRFS_UUID_SIZE);
3263 3272
3264 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid); 3273 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid,
3274 tree_mod_log_removal);
3265 copy_extent_buffer(split, c, 3275 copy_extent_buffer(split, c,
3266 btrfs_node_key_ptr_offset(0), 3276 btrfs_node_key_ptr_offset(0),
3267 btrfs_node_key_ptr_offset(mid), 3277 btrfs_node_key_ptr_offset(mid),
diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c
index 0b278b117cbe..14fce27b4780 100644
--- a/fs/btrfs/delayed-inode.c
+++ b/fs/btrfs/delayed-inode.c
@@ -22,8 +22,9 @@
22#include "disk-io.h" 22#include "disk-io.h"
23#include "transaction.h" 23#include "transaction.h"
24 24
25#define BTRFS_DELAYED_WRITEBACK 400 25#define BTRFS_DELAYED_WRITEBACK 512
26#define BTRFS_DELAYED_BACKGROUND 100 26#define BTRFS_DELAYED_BACKGROUND 128
27#define BTRFS_DELAYED_BATCH 16
27 28
28static struct kmem_cache *delayed_node_cache; 29static struct kmem_cache *delayed_node_cache;
29 30
@@ -494,6 +495,15 @@ static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
494 BTRFS_DELAYED_DELETION_ITEM); 495 BTRFS_DELAYED_DELETION_ITEM);
495} 496}
496 497
498static void finish_one_item(struct btrfs_delayed_root *delayed_root)
499{
500 int seq = atomic_inc_return(&delayed_root->items_seq);
501 if ((atomic_dec_return(&delayed_root->items) <
502 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
503 waitqueue_active(&delayed_root->wait))
504 wake_up(&delayed_root->wait);
505}
506
497static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item) 507static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
498{ 508{
499 struct rb_root *root; 509 struct rb_root *root;
@@ -512,10 +522,8 @@ static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
512 522
513 rb_erase(&delayed_item->rb_node, root); 523 rb_erase(&delayed_item->rb_node, root);
514 delayed_item->delayed_node->count--; 524 delayed_item->delayed_node->count--;
515 if (atomic_dec_return(&delayed_root->items) < 525
516 BTRFS_DELAYED_BACKGROUND && 526 finish_one_item(delayed_root);
517 waitqueue_active(&delayed_root->wait))
518 wake_up(&delayed_root->wait);
519} 527}
520 528
521static void btrfs_release_delayed_item(struct btrfs_delayed_item *item) 529static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
@@ -1056,10 +1064,7 @@ static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
1056 delayed_node->count--; 1064 delayed_node->count--;
1057 1065
1058 delayed_root = delayed_node->root->fs_info->delayed_root; 1066 delayed_root = delayed_node->root->fs_info->delayed_root;
1059 if (atomic_dec_return(&delayed_root->items) < 1067 finish_one_item(delayed_root);
1060 BTRFS_DELAYED_BACKGROUND &&
1061 waitqueue_active(&delayed_root->wait))
1062 wake_up(&delayed_root->wait);
1063 } 1068 }
1064} 1069}
1065 1070
@@ -1304,35 +1309,44 @@ void btrfs_remove_delayed_node(struct inode *inode)
1304 btrfs_release_delayed_node(delayed_node); 1309 btrfs_release_delayed_node(delayed_node);
1305} 1310}
1306 1311
1307struct btrfs_async_delayed_node { 1312struct btrfs_async_delayed_work {
1308 struct btrfs_root *root; 1313 struct btrfs_delayed_root *delayed_root;
1309 struct btrfs_delayed_node *delayed_node; 1314 int nr;
1310 struct btrfs_work work; 1315 struct btrfs_work work;
1311}; 1316};
1312 1317
1313static void btrfs_async_run_delayed_node_done(struct btrfs_work *work) 1318static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1314{ 1319{
1315 struct btrfs_async_delayed_node *async_node; 1320 struct btrfs_async_delayed_work *async_work;
1321 struct btrfs_delayed_root *delayed_root;
1316 struct btrfs_trans_handle *trans; 1322 struct btrfs_trans_handle *trans;
1317 struct btrfs_path *path; 1323 struct btrfs_path *path;
1318 struct btrfs_delayed_node *delayed_node = NULL; 1324 struct btrfs_delayed_node *delayed_node = NULL;
1319 struct btrfs_root *root; 1325 struct btrfs_root *root;
1320 struct btrfs_block_rsv *block_rsv; 1326 struct btrfs_block_rsv *block_rsv;
1321 int need_requeue = 0; 1327 int total_done = 0;
1322 1328
1323 async_node = container_of(work, struct btrfs_async_delayed_node, work); 1329 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1330 delayed_root = async_work->delayed_root;
1324 1331
1325 path = btrfs_alloc_path(); 1332 path = btrfs_alloc_path();
1326 if (!path) 1333 if (!path)
1327 goto out; 1334 goto out;
1328 path->leave_spinning = 1;
1329 1335
1330 delayed_node = async_node->delayed_node; 1336again:
1337 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1338 goto free_path;
1339
1340 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1341 if (!delayed_node)
1342 goto free_path;
1343
1344 path->leave_spinning = 1;
1331 root = delayed_node->root; 1345 root = delayed_node->root;
1332 1346
1333 trans = btrfs_join_transaction(root); 1347 trans = btrfs_join_transaction(root);
1334 if (IS_ERR(trans)) 1348 if (IS_ERR(trans))
1335 goto free_path; 1349 goto release_path;
1336 1350
1337 block_rsv = trans->block_rsv; 1351 block_rsv = trans->block_rsv;
1338 trans->block_rsv = &root->fs_info->delayed_block_rsv; 1352 trans->block_rsv = &root->fs_info->delayed_block_rsv;
@@ -1363,57 +1377,47 @@ static void btrfs_async_run_delayed_node_done(struct btrfs_work *work)
1363 * Task1 will sleep until the transaction is commited. 1377 * Task1 will sleep until the transaction is commited.
1364 */ 1378 */
1365 mutex_lock(&delayed_node->mutex); 1379 mutex_lock(&delayed_node->mutex);
1366 if (delayed_node->count) 1380 btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node);
1367 need_requeue = 1;
1368 else
1369 btrfs_dequeue_delayed_node(root->fs_info->delayed_root,
1370 delayed_node);
1371 mutex_unlock(&delayed_node->mutex); 1381 mutex_unlock(&delayed_node->mutex);
1372 1382
1373 trans->block_rsv = block_rsv; 1383 trans->block_rsv = block_rsv;
1374 btrfs_end_transaction_dmeta(trans, root); 1384 btrfs_end_transaction_dmeta(trans, root);
1375 btrfs_btree_balance_dirty_nodelay(root); 1385 btrfs_btree_balance_dirty_nodelay(root);
1386
1387release_path:
1388 btrfs_release_path(path);
1389 total_done++;
1390
1391 btrfs_release_prepared_delayed_node(delayed_node);
1392 if (async_work->nr == 0 || total_done < async_work->nr)
1393 goto again;
1394
1376free_path: 1395free_path:
1377 btrfs_free_path(path); 1396 btrfs_free_path(path);
1378out: 1397out:
1379 if (need_requeue) 1398 wake_up(&delayed_root->wait);
1380 btrfs_requeue_work(&async_node->work); 1399 kfree(async_work);
1381 else {
1382 btrfs_release_prepared_delayed_node(delayed_node);
1383 kfree(async_node);
1384 }
1385} 1400}
1386 1401
1402
1387static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, 1403static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1388 struct btrfs_root *root, int all) 1404 struct btrfs_root *root, int nr)
1389{ 1405{
1390 struct btrfs_async_delayed_node *async_node; 1406 struct btrfs_async_delayed_work *async_work;
1391 struct btrfs_delayed_node *curr;
1392 int count = 0;
1393 1407
1394again: 1408 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1395 curr = btrfs_first_prepared_delayed_node(delayed_root);
1396 if (!curr)
1397 return 0; 1409 return 0;
1398 1410
1399 async_node = kmalloc(sizeof(*async_node), GFP_NOFS); 1411 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1400 if (!async_node) { 1412 if (!async_work)
1401 btrfs_release_prepared_delayed_node(curr);
1402 return -ENOMEM; 1413 return -ENOMEM;
1403 }
1404
1405 async_node->root = root;
1406 async_node->delayed_node = curr;
1407
1408 async_node->work.func = btrfs_async_run_delayed_node_done;
1409 async_node->work.flags = 0;
1410 1414
1411 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_node->work); 1415 async_work->delayed_root = delayed_root;
1412 count++; 1416 async_work->work.func = btrfs_async_run_delayed_root;
1413 1417 async_work->work.flags = 0;
1414 if (all || count < 4) 1418 async_work->nr = nr;
1415 goto again;
1416 1419
1420 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
1417 return 0; 1421 return 0;
1418} 1422}
1419 1423
@@ -1424,30 +1428,55 @@ void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1424 WARN_ON(btrfs_first_delayed_node(delayed_root)); 1428 WARN_ON(btrfs_first_delayed_node(delayed_root));
1425} 1429}
1426 1430
1431static int refs_newer(struct btrfs_delayed_root *delayed_root,
1432 int seq, int count)
1433{
1434 int val = atomic_read(&delayed_root->items_seq);
1435
1436 if (val < seq || val >= seq + count)
1437 return 1;
1438 return 0;
1439}
1440
1427void btrfs_balance_delayed_items(struct btrfs_root *root) 1441void btrfs_balance_delayed_items(struct btrfs_root *root)
1428{ 1442{
1429 struct btrfs_delayed_root *delayed_root; 1443 struct btrfs_delayed_root *delayed_root;
1444 int seq;
1430 1445
1431 delayed_root = btrfs_get_delayed_root(root); 1446 delayed_root = btrfs_get_delayed_root(root);
1432 1447
1433 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) 1448 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1434 return; 1449 return;
1435 1450
1451 seq = atomic_read(&delayed_root->items_seq);
1452
1436 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) { 1453 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1437 int ret; 1454 int ret;
1438 ret = btrfs_wq_run_delayed_node(delayed_root, root, 1); 1455 DEFINE_WAIT(__wait);
1456
1457 ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
1439 if (ret) 1458 if (ret)
1440 return; 1459 return;
1441 1460
1442 wait_event_interruptible_timeout( 1461 while (1) {
1443 delayed_root->wait, 1462 prepare_to_wait(&delayed_root->wait, &__wait,
1444 (atomic_read(&delayed_root->items) < 1463 TASK_INTERRUPTIBLE);
1445 BTRFS_DELAYED_BACKGROUND), 1464
1446 HZ); 1465 if (refs_newer(delayed_root, seq,
1447 return; 1466 BTRFS_DELAYED_BATCH) ||
1467 atomic_read(&delayed_root->items) <
1468 BTRFS_DELAYED_BACKGROUND) {
1469 break;
1470 }
1471 if (!signal_pending(current))
1472 schedule();
1473 else
1474 break;
1475 }
1476 finish_wait(&delayed_root->wait, &__wait);
1448 } 1477 }
1449 1478
1450 btrfs_wq_run_delayed_node(delayed_root, root, 0); 1479 btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
1451} 1480}
1452 1481
1453/* Will return 0 or -ENOMEM */ 1482/* Will return 0 or -ENOMEM */
diff --git a/fs/btrfs/delayed-inode.h b/fs/btrfs/delayed-inode.h
index 78b6ad0fc669..1d5c5f7abe3e 100644
--- a/fs/btrfs/delayed-inode.h
+++ b/fs/btrfs/delayed-inode.h
@@ -43,6 +43,7 @@ struct btrfs_delayed_root {
43 */ 43 */
44 struct list_head prepare_list; 44 struct list_head prepare_list;
45 atomic_t items; /* for delayed items */ 45 atomic_t items; /* for delayed items */
46 atomic_t items_seq; /* for delayed items */
46 int nodes; /* for delayed nodes */ 47 int nodes; /* for delayed nodes */
47 wait_queue_head_t wait; 48 wait_queue_head_t wait;
48}; 49};
@@ -86,6 +87,7 @@ static inline void btrfs_init_delayed_root(
86 struct btrfs_delayed_root *delayed_root) 87 struct btrfs_delayed_root *delayed_root)
87{ 88{
88 atomic_set(&delayed_root->items, 0); 89 atomic_set(&delayed_root->items, 0);
90 atomic_set(&delayed_root->items_seq, 0);
89 delayed_root->nodes = 0; 91 delayed_root->nodes = 0;
90 spin_lock_init(&delayed_root->lock); 92 spin_lock_init(&delayed_root->lock);
91 init_waitqueue_head(&delayed_root->wait); 93 init_waitqueue_head(&delayed_root->wait);
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 02369a3c162e..6d19a0a554aa 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -62,7 +62,7 @@ static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t,
62static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 62static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
63static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 63static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
64 struct btrfs_root *root); 64 struct btrfs_root *root);
65static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t); 65static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t);
66static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 66static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
67static int btrfs_destroy_marked_extents(struct btrfs_root *root, 67static int btrfs_destroy_marked_extents(struct btrfs_root *root,
68 struct extent_io_tree *dirty_pages, 68 struct extent_io_tree *dirty_pages,
@@ -1291,6 +1291,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1291 0, objectid, NULL, 0, 0, 0); 1291 0, objectid, NULL, 0, 0, 0);
1292 if (IS_ERR(leaf)) { 1292 if (IS_ERR(leaf)) {
1293 ret = PTR_ERR(leaf); 1293 ret = PTR_ERR(leaf);
1294 leaf = NULL;
1294 goto fail; 1295 goto fail;
1295 } 1296 }
1296 1297
@@ -1334,11 +1335,16 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1334 1335
1335 btrfs_tree_unlock(leaf); 1336 btrfs_tree_unlock(leaf);
1336 1337
1338 return root;
1339
1337fail: 1340fail:
1338 if (ret) 1341 if (leaf) {
1339 return ERR_PTR(ret); 1342 btrfs_tree_unlock(leaf);
1343 free_extent_buffer(leaf);
1344 }
1345 kfree(root);
1340 1346
1341 return root; 1347 return ERR_PTR(ret);
1342} 1348}
1343 1349
1344static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, 1350static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
@@ -3253,7 +3259,7 @@ void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3253 if (btrfs_root_refs(&root->root_item) == 0) 3259 if (btrfs_root_refs(&root->root_item) == 0)
3254 synchronize_srcu(&fs_info->subvol_srcu); 3260 synchronize_srcu(&fs_info->subvol_srcu);
3255 3261
3256 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 3262 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
3257 btrfs_free_log(NULL, root); 3263 btrfs_free_log(NULL, root);
3258 btrfs_free_log_root_tree(NULL, fs_info); 3264 btrfs_free_log_root_tree(NULL, fs_info);
3259 } 3265 }
@@ -3687,7 +3693,7 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3687 return ret; 3693 return ret;
3688} 3694}
3689 3695
3690static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t) 3696static void btrfs_evict_pending_snapshots(struct btrfs_transaction *t)
3691{ 3697{
3692 struct btrfs_pending_snapshot *snapshot; 3698 struct btrfs_pending_snapshot *snapshot;
3693 struct list_head splice; 3699 struct list_head splice;
@@ -3700,10 +3706,8 @@ static void btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
3700 snapshot = list_entry(splice.next, 3706 snapshot = list_entry(splice.next,
3701 struct btrfs_pending_snapshot, 3707 struct btrfs_pending_snapshot,
3702 list); 3708 list);
3703 3709 snapshot->error = -ECANCELED;
3704 list_del_init(&snapshot->list); 3710 list_del_init(&snapshot->list);
3705
3706 kfree(snapshot);
3707 } 3711 }
3708} 3712}
3709 3713
@@ -3840,6 +3844,8 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
3840 cur_trans->blocked = 1; 3844 cur_trans->blocked = 1;
3841 wake_up(&root->fs_info->transaction_blocked_wait); 3845 wake_up(&root->fs_info->transaction_blocked_wait);
3842 3846
3847 btrfs_evict_pending_snapshots(cur_trans);
3848
3843 cur_trans->blocked = 0; 3849 cur_trans->blocked = 0;
3844 wake_up(&root->fs_info->transaction_wait); 3850 wake_up(&root->fs_info->transaction_wait);
3845 3851
@@ -3849,8 +3855,6 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
3849 btrfs_destroy_delayed_inodes(root); 3855 btrfs_destroy_delayed_inodes(root);
3850 btrfs_assert_delayed_root_empty(root); 3856 btrfs_assert_delayed_root_empty(root);
3851 3857
3852 btrfs_destroy_pending_snapshots(cur_trans);
3853
3854 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 3858 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
3855 EXTENT_DIRTY); 3859 EXTENT_DIRTY);
3856 btrfs_destroy_pinned_extent(root, 3860 btrfs_destroy_pinned_extent(root,
@@ -3894,6 +3898,8 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
3894 if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) 3898 if (waitqueue_active(&root->fs_info->transaction_blocked_wait))
3895 wake_up(&root->fs_info->transaction_blocked_wait); 3899 wake_up(&root->fs_info->transaction_blocked_wait);
3896 3900
3901 btrfs_evict_pending_snapshots(t);
3902
3897 t->blocked = 0; 3903 t->blocked = 0;
3898 smp_mb(); 3904 smp_mb();
3899 if (waitqueue_active(&root->fs_info->transaction_wait)) 3905 if (waitqueue_active(&root->fs_info->transaction_wait))
@@ -3907,8 +3913,6 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
3907 btrfs_destroy_delayed_inodes(root); 3913 btrfs_destroy_delayed_inodes(root);
3908 btrfs_assert_delayed_root_empty(root); 3914 btrfs_assert_delayed_root_empty(root);
3909 3915
3910 btrfs_destroy_pending_snapshots(t);
3911
3912 btrfs_destroy_delalloc_inodes(root); 3916 btrfs_destroy_delalloc_inodes(root);
3913 3917
3914 spin_lock(&root->fs_info->trans_lock); 3918 spin_lock(&root->fs_info->trans_lock);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 3e074dab2d57..3d551231caba 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -257,7 +257,8 @@ static int exclude_super_stripes(struct btrfs_root *root,
257 cache->bytes_super += stripe_len; 257 cache->bytes_super += stripe_len;
258 ret = add_excluded_extent(root, cache->key.objectid, 258 ret = add_excluded_extent(root, cache->key.objectid,
259 stripe_len); 259 stripe_len);
260 BUG_ON(ret); /* -ENOMEM */ 260 if (ret)
261 return ret;
261 } 262 }
262 263
263 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 264 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
@@ -265,13 +266,17 @@ static int exclude_super_stripes(struct btrfs_root *root,
265 ret = btrfs_rmap_block(&root->fs_info->mapping_tree, 266 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
266 cache->key.objectid, bytenr, 267 cache->key.objectid, bytenr,
267 0, &logical, &nr, &stripe_len); 268 0, &logical, &nr, &stripe_len);
268 BUG_ON(ret); /* -ENOMEM */ 269 if (ret)
270 return ret;
269 271
270 while (nr--) { 272 while (nr--) {
271 cache->bytes_super += stripe_len; 273 cache->bytes_super += stripe_len;
272 ret = add_excluded_extent(root, logical[nr], 274 ret = add_excluded_extent(root, logical[nr],
273 stripe_len); 275 stripe_len);
274 BUG_ON(ret); /* -ENOMEM */ 276 if (ret) {
277 kfree(logical);
278 return ret;
279 }
275 } 280 }
276 281
277 kfree(logical); 282 kfree(logical);
@@ -1467,8 +1472,11 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1467 if (ret && !insert) { 1472 if (ret && !insert) {
1468 err = -ENOENT; 1473 err = -ENOENT;
1469 goto out; 1474 goto out;
1475 } else if (ret) {
1476 err = -EIO;
1477 WARN_ON(1);
1478 goto out;
1470 } 1479 }
1471 BUG_ON(ret); /* Corruption */
1472 1480
1473 leaf = path->nodes[0]; 1481 leaf = path->nodes[0];
1474 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 1482 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -4435,7 +4443,7 @@ static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4435 spin_lock(&sinfo->lock); 4443 spin_lock(&sinfo->lock);
4436 spin_lock(&block_rsv->lock); 4444 spin_lock(&block_rsv->lock);
4437 4445
4438 block_rsv->size = num_bytes; 4446 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
4439 4447
4440 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + 4448 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
4441 sinfo->bytes_reserved + sinfo->bytes_readonly + 4449 sinfo->bytes_reserved + sinfo->bytes_readonly +
@@ -4790,14 +4798,49 @@ out_fail:
4790 * If the inodes csum_bytes is the same as the original 4798 * If the inodes csum_bytes is the same as the original
4791 * csum_bytes then we know we haven't raced with any free()ers 4799 * csum_bytes then we know we haven't raced with any free()ers
4792 * so we can just reduce our inodes csum bytes and carry on. 4800 * so we can just reduce our inodes csum bytes and carry on.
4793 * Otherwise we have to do the normal free thing to account for
4794 * the case that the free side didn't free up its reserve
4795 * because of this outstanding reservation.
4796 */ 4801 */
4797 if (BTRFS_I(inode)->csum_bytes == csum_bytes) 4802 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
4798 calc_csum_metadata_size(inode, num_bytes, 0); 4803 calc_csum_metadata_size(inode, num_bytes, 0);
4799 else 4804 } else {
4800 to_free = calc_csum_metadata_size(inode, num_bytes, 0); 4805 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4806 u64 bytes;
4807
4808 /*
4809 * This is tricky, but first we need to figure out how much we
4810 * free'd from any free-ers that occured during this
4811 * reservation, so we reset ->csum_bytes to the csum_bytes
4812 * before we dropped our lock, and then call the free for the
4813 * number of bytes that were freed while we were trying our
4814 * reservation.
4815 */
4816 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4817 BTRFS_I(inode)->csum_bytes = csum_bytes;
4818 to_free = calc_csum_metadata_size(inode, bytes, 0);
4819
4820
4821 /*
4822 * Now we need to see how much we would have freed had we not
4823 * been making this reservation and our ->csum_bytes were not
4824 * artificially inflated.
4825 */
4826 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4827 bytes = csum_bytes - orig_csum_bytes;
4828 bytes = calc_csum_metadata_size(inode, bytes, 0);
4829
4830 /*
4831 * Now reset ->csum_bytes to what it should be. If bytes is
4832 * more than to_free then we would have free'd more space had we
4833 * not had an artificially high ->csum_bytes, so we need to free
4834 * the remainder. If bytes is the same or less then we don't
4835 * need to do anything, the other free-ers did the correct
4836 * thing.
4837 */
4838 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4839 if (bytes > to_free)
4840 to_free = bytes - to_free;
4841 else
4842 to_free = 0;
4843 }
4801 spin_unlock(&BTRFS_I(inode)->lock); 4844 spin_unlock(&BTRFS_I(inode)->lock);
4802 if (dropped) 4845 if (dropped)
4803 to_free += btrfs_calc_trans_metadata_size(root, dropped); 4846 to_free += btrfs_calc_trans_metadata_size(root, dropped);
@@ -7944,7 +7987,17 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7944 * info has super bytes accounted for, otherwise we'll think 7987 * info has super bytes accounted for, otherwise we'll think
7945 * we have more space than we actually do. 7988 * we have more space than we actually do.
7946 */ 7989 */
7947 exclude_super_stripes(root, cache); 7990 ret = exclude_super_stripes(root, cache);
7991 if (ret) {
7992 /*
7993 * We may have excluded something, so call this just in
7994 * case.
7995 */
7996 free_excluded_extents(root, cache);
7997 kfree(cache->free_space_ctl);
7998 kfree(cache);
7999 goto error;
8000 }
7948 8001
7949 /* 8002 /*
7950 * check for two cases, either we are full, and therefore 8003 * check for two cases, either we are full, and therefore
@@ -8086,7 +8139,17 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8086 8139
8087 cache->last_byte_to_unpin = (u64)-1; 8140 cache->last_byte_to_unpin = (u64)-1;
8088 cache->cached = BTRFS_CACHE_FINISHED; 8141 cache->cached = BTRFS_CACHE_FINISHED;
8089 exclude_super_stripes(root, cache); 8142 ret = exclude_super_stripes(root, cache);
8143 if (ret) {
8144 /*
8145 * We may have excluded something, so call this just in
8146 * case.
8147 */
8148 free_excluded_extents(root, cache);
8149 kfree(cache->free_space_ctl);
8150 kfree(cache);
8151 return ret;
8152 }
8090 8153
8091 add_new_free_space(cache, root->fs_info, chunk_offset, 8154 add_new_free_space(cache, root->fs_info, chunk_offset,
8092 chunk_offset + size); 8155 chunk_offset + size);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index f173c5af6461..cdee391fc7bf 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1257,6 +1257,39 @@ int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
1257 GFP_NOFS); 1257 GFP_NOFS);
1258} 1258}
1259 1259
1260int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1261{
1262 unsigned long index = start >> PAGE_CACHE_SHIFT;
1263 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1264 struct page *page;
1265
1266 while (index <= end_index) {
1267 page = find_get_page(inode->i_mapping, index);
1268 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1269 clear_page_dirty_for_io(page);
1270 page_cache_release(page);
1271 index++;
1272 }
1273 return 0;
1274}
1275
1276int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1277{
1278 unsigned long index = start >> PAGE_CACHE_SHIFT;
1279 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1280 struct page *page;
1281
1282 while (index <= end_index) {
1283 page = find_get_page(inode->i_mapping, index);
1284 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1285 account_page_redirty(page);
1286 __set_page_dirty_nobuffers(page);
1287 page_cache_release(page);
1288 index++;
1289 }
1290 return 0;
1291}
1292
1260/* 1293/*
1261 * helper function to set both pages and extents in the tree writeback 1294 * helper function to set both pages and extents in the tree writeback
1262 */ 1295 */
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 6068a1985560..258c92156857 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -325,6 +325,8 @@ int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
325 unsigned long *map_len); 325 unsigned long *map_len);
326int extent_range_uptodate(struct extent_io_tree *tree, 326int extent_range_uptodate(struct extent_io_tree *tree,
327 u64 start, u64 end); 327 u64 start, u64 end);
328int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
329int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
328int extent_clear_unlock_delalloc(struct inode *inode, 330int extent_clear_unlock_delalloc(struct inode *inode,
329 struct extent_io_tree *tree, 331 struct extent_io_tree *tree,
330 u64 start, u64 end, struct page *locked_page, 332 u64 start, u64 end, struct page *locked_page,
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index ec160202be3e..c4628a201cb3 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -118,9 +118,11 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
118 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 118 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
119 csums_in_item /= csum_size; 119 csums_in_item /= csum_size;
120 120
121 if (csum_offset >= csums_in_item) { 121 if (csum_offset == csums_in_item) {
122 ret = -EFBIG; 122 ret = -EFBIG;
123 goto fail; 123 goto fail;
124 } else if (csum_offset > csums_in_item) {
125 goto fail;
124 } 126 }
125 } 127 }
126 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 128 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
@@ -728,7 +730,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
728 return -ENOMEM; 730 return -ENOMEM;
729 731
730 sector_sum = sums->sums; 732 sector_sum = sums->sums;
731 trans->adding_csums = 1;
732again: 733again:
733 next_offset = (u64)-1; 734 next_offset = (u64)-1;
734 found_next = 0; 735 found_next = 0;
@@ -899,7 +900,6 @@ next_sector:
899 goto again; 900 goto again;
900 } 901 }
901out: 902out:
902 trans->adding_csums = 0;
903 btrfs_free_path(path); 903 btrfs_free_path(path);
904 return ret; 904 return ret;
905 905
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index af1d0605a5c1..ade03e6f7bd2 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -591,6 +591,7 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
591 } 591 }
592 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 592 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
593 clear_bit(EXTENT_FLAG_PINNED, &em->flags); 593 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
594 clear_bit(EXTENT_FLAG_LOGGING, &flags);
594 remove_extent_mapping(em_tree, em); 595 remove_extent_mapping(em_tree, em);
595 if (no_splits) 596 if (no_splits)
596 goto next; 597 goto next;
@@ -2141,6 +2142,7 @@ static long btrfs_fallocate(struct file *file, int mode,
2141{ 2142{
2142 struct inode *inode = file_inode(file); 2143 struct inode *inode = file_inode(file);
2143 struct extent_state *cached_state = NULL; 2144 struct extent_state *cached_state = NULL;
2145 struct btrfs_root *root = BTRFS_I(inode)->root;
2144 u64 cur_offset; 2146 u64 cur_offset;
2145 u64 last_byte; 2147 u64 last_byte;
2146 u64 alloc_start; 2148 u64 alloc_start;
@@ -2168,6 +2170,11 @@ static long btrfs_fallocate(struct file *file, int mode,
2168 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); 2170 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
2169 if (ret) 2171 if (ret)
2170 return ret; 2172 return ret;
2173 if (root->fs_info->quota_enabled) {
2174 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2175 if (ret)
2176 goto out_reserve_fail;
2177 }
2171 2178
2172 /* 2179 /*
2173 * wait for ordered IO before we have any locks. We'll loop again 2180 * wait for ordered IO before we have any locks. We'll loop again
@@ -2271,6 +2278,9 @@ static long btrfs_fallocate(struct file *file, int mode,
2271 &cached_state, GFP_NOFS); 2278 &cached_state, GFP_NOFS);
2272out: 2279out:
2273 mutex_unlock(&inode->i_mutex); 2280 mutex_unlock(&inode->i_mutex);
2281 if (root->fs_info->quota_enabled)
2282 btrfs_qgroup_free(root, alloc_end - alloc_start);
2283out_reserve_fail:
2274 /* Let go of our reservation. */ 2284 /* Let go of our reservation. */
2275 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); 2285 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
2276 return ret; 2286 return ret;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index c226daefd65d..09c58a35b429 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -353,6 +353,7 @@ static noinline int compress_file_range(struct inode *inode,
353 int i; 353 int i;
354 int will_compress; 354 int will_compress;
355 int compress_type = root->fs_info->compress_type; 355 int compress_type = root->fs_info->compress_type;
356 int redirty = 0;
356 357
357 /* if this is a small write inside eof, kick off a defrag */ 358 /* if this is a small write inside eof, kick off a defrag */
358 if ((end - start + 1) < 16 * 1024 && 359 if ((end - start + 1) < 16 * 1024 &&
@@ -415,6 +416,17 @@ again:
415 if (BTRFS_I(inode)->force_compress) 416 if (BTRFS_I(inode)->force_compress)
416 compress_type = BTRFS_I(inode)->force_compress; 417 compress_type = BTRFS_I(inode)->force_compress;
417 418
419 /*
420 * we need to call clear_page_dirty_for_io on each
421 * page in the range. Otherwise applications with the file
422 * mmap'd can wander in and change the page contents while
423 * we are compressing them.
424 *
425 * If the compression fails for any reason, we set the pages
426 * dirty again later on.
427 */
428 extent_range_clear_dirty_for_io(inode, start, end);
429 redirty = 1;
418 ret = btrfs_compress_pages(compress_type, 430 ret = btrfs_compress_pages(compress_type,
419 inode->i_mapping, start, 431 inode->i_mapping, start,
420 total_compressed, pages, 432 total_compressed, pages,
@@ -554,6 +566,8 @@ cleanup_and_bail_uncompressed:
554 __set_page_dirty_nobuffers(locked_page); 566 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */ 567 /* unlocked later on in the async handlers */
556 } 568 }
569 if (redirty)
570 extent_range_redirty_for_io(inode, start, end);
557 add_async_extent(async_cow, start, end - start + 1, 571 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE); 572 0, NULL, 0, BTRFS_COMPRESS_NONE);
559 *num_added += 1; 573 *num_added += 1;
@@ -1743,8 +1757,10 @@ static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1743 struct btrfs_ordered_sum *sum; 1757 struct btrfs_ordered_sum *sum;
1744 1758
1745 list_for_each_entry(sum, list, list) { 1759 list_for_each_entry(sum, list, list) {
1760 trans->adding_csums = 1;
1746 btrfs_csum_file_blocks(trans, 1761 btrfs_csum_file_blocks(trans,
1747 BTRFS_I(inode)->root->fs_info->csum_root, sum); 1762 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1763 trans->adding_csums = 0;
1748 } 1764 }
1749 return 0; 1765 return 0;
1750} 1766}
@@ -2312,6 +2328,7 @@ again:
2312 key.type = BTRFS_EXTENT_DATA_KEY; 2328 key.type = BTRFS_EXTENT_DATA_KEY;
2313 key.offset = start; 2329 key.offset = start;
2314 2330
2331 path->leave_spinning = 1;
2315 if (merge) { 2332 if (merge) {
2316 struct btrfs_file_extent_item *fi; 2333 struct btrfs_file_extent_item *fi;
2317 u64 extent_len; 2334 u64 extent_len;
@@ -2368,6 +2385,7 @@ again:
2368 2385
2369 btrfs_mark_buffer_dirty(leaf); 2386 btrfs_mark_buffer_dirty(leaf);
2370 inode_add_bytes(inode, len); 2387 inode_add_bytes(inode, len);
2388 btrfs_release_path(path);
2371 2389
2372 ret = btrfs_inc_extent_ref(trans, root, new->bytenr, 2390 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2373 new->disk_len, 0, 2391 new->disk_len, 0,
@@ -2381,6 +2399,7 @@ again:
2381 ret = 1; 2399 ret = 1;
2382out_free_path: 2400out_free_path:
2383 btrfs_release_path(path); 2401 btrfs_release_path(path);
2402 path->leave_spinning = 0;
2384 btrfs_end_transaction(trans, root); 2403 btrfs_end_transaction(trans, root);
2385out_unlock: 2404out_unlock:
2386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, 2405 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
@@ -3676,11 +3695,9 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3676 * 1 for the dir item 3695 * 1 for the dir item
3677 * 1 for the dir index 3696 * 1 for the dir index
3678 * 1 for the inode ref 3697 * 1 for the inode ref
3679 * 1 for the inode ref in the tree log
3680 * 2 for the dir entries in the log
3681 * 1 for the inode 3698 * 1 for the inode
3682 */ 3699 */
3683 trans = btrfs_start_transaction(root, 8); 3700 trans = btrfs_start_transaction(root, 5);
3684 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 3701 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3685 return trans; 3702 return trans;
3686 3703
@@ -8124,7 +8141,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8124 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items 8141 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8125 * should cover the worst case number of items we'll modify. 8142 * should cover the worst case number of items we'll modify.
8126 */ 8143 */
8127 trans = btrfs_start_transaction(root, 20); 8144 trans = btrfs_start_transaction(root, 11);
8128 if (IS_ERR(trans)) { 8145 if (IS_ERR(trans)) {
8129 ret = PTR_ERR(trans); 8146 ret = PTR_ERR(trans);
8130 goto out_notrans; 8147 goto out_notrans;
@@ -8502,6 +8519,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8502 struct btrfs_key ins; 8519 struct btrfs_key ins;
8503 u64 cur_offset = start; 8520 u64 cur_offset = start;
8504 u64 i_size; 8521 u64 i_size;
8522 u64 cur_bytes;
8505 int ret = 0; 8523 int ret = 0;
8506 bool own_trans = true; 8524 bool own_trans = true;
8507 8525
@@ -8516,8 +8534,9 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8516 } 8534 }
8517 } 8535 }
8518 8536
8519 ret = btrfs_reserve_extent(trans, root, 8537 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8520 min(num_bytes, 256ULL * 1024 * 1024), 8538 cur_bytes = max(cur_bytes, min_size);
8539 ret = btrfs_reserve_extent(trans, root, cur_bytes,
8521 min_size, 0, *alloc_hint, &ins, 1); 8540 min_size, 0, *alloc_hint, &ins, 1);
8522 if (ret) { 8541 if (ret) {
8523 if (own_trans) 8542 if (own_trans)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index c83086fdda05..2c02310ff2d9 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -527,6 +527,8 @@ fail:
527 if (async_transid) { 527 if (async_transid) {
528 *async_transid = trans->transid; 528 *async_transid = trans->transid;
529 err = btrfs_commit_transaction_async(trans, root, 1); 529 err = btrfs_commit_transaction_async(trans, root, 1);
530 if (err)
531 err = btrfs_commit_transaction(trans, root);
530 } else { 532 } else {
531 err = btrfs_commit_transaction(trans, root); 533 err = btrfs_commit_transaction(trans, root);
532 } 534 }
@@ -592,16 +594,14 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
592 *async_transid = trans->transid; 594 *async_transid = trans->transid;
593 ret = btrfs_commit_transaction_async(trans, 595 ret = btrfs_commit_transaction_async(trans,
594 root->fs_info->extent_root, 1); 596 root->fs_info->extent_root, 1);
597 if (ret)
598 ret = btrfs_commit_transaction(trans, root);
595 } else { 599 } else {
596 ret = btrfs_commit_transaction(trans, 600 ret = btrfs_commit_transaction(trans,
597 root->fs_info->extent_root); 601 root->fs_info->extent_root);
598 } 602 }
599 if (ret) { 603 if (ret)
600 /* cleanup_transaction has freed this for us */
601 if (trans->aborted)
602 pending_snapshot = NULL;
603 goto fail; 604 goto fail;
604 }
605 605
606 ret = pending_snapshot->error; 606 ret = pending_snapshot->error;
607 if (ret) 607 if (ret)
@@ -2245,13 +2245,6 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2245 if (ret) 2245 if (ret)
2246 return ret; 2246 return ret;
2247 2247
2248 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2249 1)) {
2250 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2251 mnt_drop_write_file(file);
2252 return -EINVAL;
2253 }
2254
2255 if (btrfs_root_readonly(root)) { 2248 if (btrfs_root_readonly(root)) {
2256 ret = -EROFS; 2249 ret = -EROFS;
2257 goto out; 2250 goto out;
@@ -2306,7 +2299,6 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2306 ret = -EINVAL; 2299 ret = -EINVAL;
2307 } 2300 }
2308out: 2301out:
2309 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2310 mnt_drop_write_file(file); 2302 mnt_drop_write_file(file);
2311 return ret; 2303 return ret;
2312} 2304}
diff --git a/fs/btrfs/locking.h b/fs/btrfs/locking.h
index ca52681e5f40..b81e0e9a4894 100644
--- a/fs/btrfs/locking.h
+++ b/fs/btrfs/locking.h
@@ -26,7 +26,6 @@
26 26
27void btrfs_tree_lock(struct extent_buffer *eb); 27void btrfs_tree_lock(struct extent_buffer *eb);
28void btrfs_tree_unlock(struct extent_buffer *eb); 28void btrfs_tree_unlock(struct extent_buffer *eb);
29int btrfs_try_spin_lock(struct extent_buffer *eb);
30 29
31void btrfs_tree_read_lock(struct extent_buffer *eb); 30void btrfs_tree_read_lock(struct extent_buffer *eb);
32void btrfs_tree_read_unlock(struct extent_buffer *eb); 31void btrfs_tree_read_unlock(struct extent_buffer *eb);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index dc08d77b717e..005c45db699e 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -557,6 +557,7 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput)
557 INIT_LIST_HEAD(&splice); 557 INIT_LIST_HEAD(&splice);
558 INIT_LIST_HEAD(&works); 558 INIT_LIST_HEAD(&works);
559 559
560 mutex_lock(&root->fs_info->ordered_operations_mutex);
560 spin_lock(&root->fs_info->ordered_extent_lock); 561 spin_lock(&root->fs_info->ordered_extent_lock);
561 list_splice_init(&root->fs_info->ordered_extents, &splice); 562 list_splice_init(&root->fs_info->ordered_extents, &splice);
562 while (!list_empty(&splice)) { 563 while (!list_empty(&splice)) {
@@ -600,6 +601,7 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput)
600 601
601 cond_resched(); 602 cond_resched();
602 } 603 }
604 mutex_unlock(&root->fs_info->ordered_operations_mutex);
603} 605}
604 606
605/* 607/*
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index aee4b1cc3d98..b44124dd2370 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -1153,7 +1153,7 @@ int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1153 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr, 1153 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr,
1154 sgn > 0 ? node->seq - 1 : node->seq, &roots); 1154 sgn > 0 ? node->seq - 1 : node->seq, &roots);
1155 if (ret < 0) 1155 if (ret < 0)
1156 goto out; 1156 return ret;
1157 1157
1158 spin_lock(&fs_info->qgroup_lock); 1158 spin_lock(&fs_info->qgroup_lock);
1159 quota_root = fs_info->quota_root; 1159 quota_root = fs_info->quota_root;
@@ -1275,7 +1275,6 @@ int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1275 ret = 0; 1275 ret = 0;
1276unlock: 1276unlock:
1277 spin_unlock(&fs_info->qgroup_lock); 1277 spin_unlock(&fs_info->qgroup_lock);
1278out:
1279 ulist_free(roots); 1278 ulist_free(roots);
1280 ulist_free(tmp); 1279 ulist_free(tmp);
1281 1280
@@ -1525,21 +1524,23 @@ int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1525 1524
1526 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) && 1525 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1527 qg->reserved + qg->rfer + num_bytes > 1526 qg->reserved + qg->rfer + num_bytes >
1528 qg->max_rfer) 1527 qg->max_rfer) {
1529 ret = -EDQUOT; 1528 ret = -EDQUOT;
1529 goto out;
1530 }
1530 1531
1531 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) && 1532 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1532 qg->reserved + qg->excl + num_bytes > 1533 qg->reserved + qg->excl + num_bytes >
1533 qg->max_excl) 1534 qg->max_excl) {
1534 ret = -EDQUOT; 1535 ret = -EDQUOT;
1536 goto out;
1537 }
1535 1538
1536 list_for_each_entry(glist, &qg->groups, next_group) { 1539 list_for_each_entry(glist, &qg->groups, next_group) {
1537 ulist_add(ulist, glist->group->qgroupid, 1540 ulist_add(ulist, glist->group->qgroupid,
1538 (uintptr_t)glist->group, GFP_ATOMIC); 1541 (uintptr_t)glist->group, GFP_ATOMIC);
1539 } 1542 }
1540 } 1543 }
1541 if (ret)
1542 goto out;
1543 1544
1544 /* 1545 /*
1545 * no limits exceeded, now record the reservation into all qgroups 1546 * no limits exceeded, now record the reservation into all qgroups
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 50695dc5e2ab..b67171e6d688 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1269,6 +1269,8 @@ static int __update_reloc_root(struct btrfs_root *root, int del)
1269 } 1269 }
1270 spin_unlock(&rc->reloc_root_tree.lock); 1270 spin_unlock(&rc->reloc_root_tree.lock);
1271 1271
1272 if (!node)
1273 return 0;
1272 BUG_ON((struct btrfs_root *)node->data != root); 1274 BUG_ON((struct btrfs_root *)node->data != root);
1273 1275
1274 if (!del) { 1276 if (!del) {
@@ -2238,13 +2240,28 @@ again:
2238} 2240}
2239 2241
2240static noinline_for_stack 2242static noinline_for_stack
2243void free_reloc_roots(struct list_head *list)
2244{
2245 struct btrfs_root *reloc_root;
2246
2247 while (!list_empty(list)) {
2248 reloc_root = list_entry(list->next, struct btrfs_root,
2249 root_list);
2250 __update_reloc_root(reloc_root, 1);
2251 free_extent_buffer(reloc_root->node);
2252 free_extent_buffer(reloc_root->commit_root);
2253 kfree(reloc_root);
2254 }
2255}
2256
2257static noinline_for_stack
2241int merge_reloc_roots(struct reloc_control *rc) 2258int merge_reloc_roots(struct reloc_control *rc)
2242{ 2259{
2243 struct btrfs_root *root; 2260 struct btrfs_root *root;
2244 struct btrfs_root *reloc_root; 2261 struct btrfs_root *reloc_root;
2245 LIST_HEAD(reloc_roots); 2262 LIST_HEAD(reloc_roots);
2246 int found = 0; 2263 int found = 0;
2247 int ret; 2264 int ret = 0;
2248again: 2265again:
2249 root = rc->extent_root; 2266 root = rc->extent_root;
2250 2267
@@ -2270,20 +2287,33 @@ again:
2270 BUG_ON(root->reloc_root != reloc_root); 2287 BUG_ON(root->reloc_root != reloc_root);
2271 2288
2272 ret = merge_reloc_root(rc, root); 2289 ret = merge_reloc_root(rc, root);
2273 BUG_ON(ret); 2290 if (ret)
2291 goto out;
2274 } else { 2292 } else {
2275 list_del_init(&reloc_root->root_list); 2293 list_del_init(&reloc_root->root_list);
2276 } 2294 }
2277 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1); 2295 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
2278 BUG_ON(ret < 0); 2296 if (ret < 0) {
2297 if (list_empty(&reloc_root->root_list))
2298 list_add_tail(&reloc_root->root_list,
2299 &reloc_roots);
2300 goto out;
2301 }
2279 } 2302 }
2280 2303
2281 if (found) { 2304 if (found) {
2282 found = 0; 2305 found = 0;
2283 goto again; 2306 goto again;
2284 } 2307 }
2308out:
2309 if (ret) {
2310 btrfs_std_error(root->fs_info, ret);
2311 if (!list_empty(&reloc_roots))
2312 free_reloc_roots(&reloc_roots);
2313 }
2314
2285 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); 2315 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2286 return 0; 2316 return ret;
2287} 2317}
2288 2318
2289static void free_block_list(struct rb_root *blocks) 2319static void free_block_list(struct rb_root *blocks)
@@ -2818,8 +2848,10 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2818 int err = 0; 2848 int err = 0;
2819 2849
2820 path = btrfs_alloc_path(); 2850 path = btrfs_alloc_path();
2821 if (!path) 2851 if (!path) {
2822 return -ENOMEM; 2852 err = -ENOMEM;
2853 goto out_path;
2854 }
2823 2855
2824 rb_node = rb_first(blocks); 2856 rb_node = rb_first(blocks);
2825 while (rb_node) { 2857 while (rb_node) {
@@ -2858,10 +2890,11 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2858 rb_node = rb_next(rb_node); 2890 rb_node = rb_next(rb_node);
2859 } 2891 }
2860out: 2892out:
2861 free_block_list(blocks);
2862 err = finish_pending_nodes(trans, rc, path, err); 2893 err = finish_pending_nodes(trans, rc, path, err);
2863 2894
2864 btrfs_free_path(path); 2895 btrfs_free_path(path);
2896out_path:
2897 free_block_list(blocks);
2865 return err; 2898 return err;
2866} 2899}
2867 2900
@@ -3698,7 +3731,15 @@ int prepare_to_relocate(struct reloc_control *rc)
3698 set_reloc_control(rc); 3731 set_reloc_control(rc);
3699 3732
3700 trans = btrfs_join_transaction(rc->extent_root); 3733 trans = btrfs_join_transaction(rc->extent_root);
3701 BUG_ON(IS_ERR(trans)); 3734 if (IS_ERR(trans)) {
3735 unset_reloc_control(rc);
3736 /*
3737 * extent tree is not a ref_cow tree and has no reloc_root to
3738 * cleanup. And callers are responsible to free the above
3739 * block rsv.
3740 */
3741 return PTR_ERR(trans);
3742 }
3702 btrfs_commit_transaction(trans, rc->extent_root); 3743 btrfs_commit_transaction(trans, rc->extent_root);
3703 return 0; 3744 return 0;
3704} 3745}
@@ -3730,7 +3771,11 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3730 while (1) { 3771 while (1) {
3731 progress++; 3772 progress++;
3732 trans = btrfs_start_transaction(rc->extent_root, 0); 3773 trans = btrfs_start_transaction(rc->extent_root, 0);
3733 BUG_ON(IS_ERR(trans)); 3774 if (IS_ERR(trans)) {
3775 err = PTR_ERR(trans);
3776 trans = NULL;
3777 break;
3778 }
3734restart: 3779restart:
3735 if (update_backref_cache(trans, &rc->backref_cache)) { 3780 if (update_backref_cache(trans, &rc->backref_cache)) {
3736 btrfs_end_transaction(trans, rc->extent_root); 3781 btrfs_end_transaction(trans, rc->extent_root);
@@ -4264,14 +4309,9 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4264out_free: 4309out_free:
4265 kfree(rc); 4310 kfree(rc);
4266out: 4311out:
4267 while (!list_empty(&reloc_roots)) { 4312 if (!list_empty(&reloc_roots))
4268 reloc_root = list_entry(reloc_roots.next, 4313 free_reloc_roots(&reloc_roots);
4269 struct btrfs_root, root_list); 4314
4270 list_del(&reloc_root->root_list);
4271 free_extent_buffer(reloc_root->node);
4272 free_extent_buffer(reloc_root->commit_root);
4273 kfree(reloc_root);
4274 }
4275 btrfs_free_path(path); 4315 btrfs_free_path(path);
4276 4316
4277 if (err == 0) { 4317 if (err == 0) {
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 53c3501fa4ca..85e072b956d5 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -542,7 +542,6 @@ static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
542 eb = path->nodes[0]; 542 eb = path->nodes[0];
543 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); 543 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
544 item_size = btrfs_item_size_nr(eb, path->slots[0]); 544 item_size = btrfs_item_size_nr(eb, path->slots[0]);
545 btrfs_release_path(path);
546 545
547 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 546 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
548 do { 547 do {
@@ -558,7 +557,9 @@ static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
558 ret < 0 ? -1 : ref_level, 557 ret < 0 ? -1 : ref_level,
559 ret < 0 ? -1 : ref_root); 558 ret < 0 ? -1 : ref_root);
560 } while (ret != 1); 559 } while (ret != 1);
560 btrfs_release_path(path);
561 } else { 561 } else {
562 btrfs_release_path(path);
562 swarn.path = path; 563 swarn.path = path;
563 swarn.dev = dev; 564 swarn.dev = dev;
564 iterate_extent_inodes(fs_info, found_key.objectid, 565 iterate_extent_inodes(fs_info, found_key.objectid,
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index f7a8b861058b..c85e7c6b4598 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -3945,12 +3945,10 @@ static int is_extent_unchanged(struct send_ctx *sctx,
3945 found_key.type != key.type) { 3945 found_key.type != key.type) {
3946 key.offset += right_len; 3946 key.offset += right_len;
3947 break; 3947 break;
3948 } else { 3948 }
3949 if (found_key.offset != key.offset + right_len) { 3949 if (found_key.offset != key.offset + right_len) {
3950 /* Should really not happen */ 3950 ret = 0;
3951 ret = -EIO; 3951 goto out;
3952 goto out;
3953 }
3954 } 3952 }
3955 key = found_key; 3953 key = found_key;
3956 } 3954 }
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 68a29a1ea068..f6b88595f858 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1558,6 +1558,7 @@ static struct file_system_type btrfs_fs_type = {
1558 .kill_sb = btrfs_kill_super, 1558 .kill_sb = btrfs_kill_super,
1559 .fs_flags = FS_REQUIRES_DEV, 1559 .fs_flags = FS_REQUIRES_DEV,
1560}; 1560};
1561MODULE_ALIAS_FS("btrfs");
1561 1562
1562/* 1563/*
1563 * used by btrfsctl to scan devices when no FS is mounted 1564 * used by btrfsctl to scan devices when no FS is mounted
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index e52da6fb1165..50767bbaad6c 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -625,14 +625,13 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
625 625
626 btrfs_trans_release_metadata(trans, root); 626 btrfs_trans_release_metadata(trans, root);
627 trans->block_rsv = NULL; 627 trans->block_rsv = NULL;
628 /*
629 * the same root has to be passed to start_transaction and
630 * end_transaction. Subvolume quota depends on this.
631 */
632 WARN_ON(trans->root != root);
633 628
634 if (trans->qgroup_reserved) { 629 if (trans->qgroup_reserved) {
635 btrfs_qgroup_free(root, trans->qgroup_reserved); 630 /*
631 * the same root has to be passed here between start_transaction
632 * and end_transaction. Subvolume quota depends on this.
633 */
634 btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
636 trans->qgroup_reserved = 0; 635 trans->qgroup_reserved = 0;
637 } 636 }
638 637
@@ -1052,7 +1051,12 @@ int btrfs_defrag_root(struct btrfs_root *root)
1052 1051
1053/* 1052/*
1054 * new snapshots need to be created at a very specific time in the 1053 * new snapshots need to be created at a very specific time in the
1055 * transaction commit. This does the actual creation 1054 * transaction commit. This does the actual creation.
1055 *
1056 * Note:
1057 * If the error which may affect the commitment of the current transaction
1058 * happens, we should return the error number. If the error which just affect
1059 * the creation of the pending snapshots, just return 0.
1056 */ 1060 */
1057static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 1061static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1058 struct btrfs_fs_info *fs_info, 1062 struct btrfs_fs_info *fs_info,
@@ -1071,7 +1075,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1071 struct extent_buffer *tmp; 1075 struct extent_buffer *tmp;
1072 struct extent_buffer *old; 1076 struct extent_buffer *old;
1073 struct timespec cur_time = CURRENT_TIME; 1077 struct timespec cur_time = CURRENT_TIME;
1074 int ret; 1078 int ret = 0;
1075 u64 to_reserve = 0; 1079 u64 to_reserve = 0;
1076 u64 index = 0; 1080 u64 index = 0;
1077 u64 objectid; 1081 u64 objectid;
@@ -1080,40 +1084,36 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1080 1084
1081 path = btrfs_alloc_path(); 1085 path = btrfs_alloc_path();
1082 if (!path) { 1086 if (!path) {
1083 ret = pending->error = -ENOMEM; 1087 pending->error = -ENOMEM;
1084 return ret; 1088 return 0;
1085 } 1089 }
1086 1090
1087 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 1091 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
1088 if (!new_root_item) { 1092 if (!new_root_item) {
1089 ret = pending->error = -ENOMEM; 1093 pending->error = -ENOMEM;
1090 goto root_item_alloc_fail; 1094 goto root_item_alloc_fail;
1091 } 1095 }
1092 1096
1093 ret = btrfs_find_free_objectid(tree_root, &objectid); 1097 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1094 if (ret) { 1098 if (pending->error)
1095 pending->error = ret;
1096 goto no_free_objectid; 1099 goto no_free_objectid;
1097 }
1098 1100
1099 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1101 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1100 1102
1101 if (to_reserve > 0) { 1103 if (to_reserve > 0) {
1102 ret = btrfs_block_rsv_add(root, &pending->block_rsv, 1104 pending->error = btrfs_block_rsv_add(root,
1103 to_reserve, 1105 &pending->block_rsv,
1104 BTRFS_RESERVE_NO_FLUSH); 1106 to_reserve,
1105 if (ret) { 1107 BTRFS_RESERVE_NO_FLUSH);
1106 pending->error = ret; 1108 if (pending->error)
1107 goto no_free_objectid; 1109 goto no_free_objectid;
1108 }
1109 } 1110 }
1110 1111
1111 ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid, 1112 pending->error = btrfs_qgroup_inherit(trans, fs_info,
1112 objectid, pending->inherit); 1113 root->root_key.objectid,
1113 if (ret) { 1114 objectid, pending->inherit);
1114 pending->error = ret; 1115 if (pending->error)
1115 goto no_free_objectid; 1116 goto no_free_objectid;
1116 }
1117 1117
1118 key.objectid = objectid; 1118 key.objectid = objectid;
1119 key.offset = (u64)-1; 1119 key.offset = (u64)-1;
@@ -1141,7 +1141,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1141 dentry->d_name.len, 0); 1141 dentry->d_name.len, 0);
1142 if (dir_item != NULL && !IS_ERR(dir_item)) { 1142 if (dir_item != NULL && !IS_ERR(dir_item)) {
1143 pending->error = -EEXIST; 1143 pending->error = -EEXIST;
1144 goto fail; 1144 goto dir_item_existed;
1145 } else if (IS_ERR(dir_item)) { 1145 } else if (IS_ERR(dir_item)) {
1146 ret = PTR_ERR(dir_item); 1146 ret = PTR_ERR(dir_item);
1147 btrfs_abort_transaction(trans, root, ret); 1147 btrfs_abort_transaction(trans, root, ret);
@@ -1272,6 +1272,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1272 if (ret) 1272 if (ret)
1273 btrfs_abort_transaction(trans, root, ret); 1273 btrfs_abort_transaction(trans, root, ret);
1274fail: 1274fail:
1275 pending->error = ret;
1276dir_item_existed:
1275 trans->block_rsv = rsv; 1277 trans->block_rsv = rsv;
1276 trans->bytes_reserved = 0; 1278 trans->bytes_reserved = 0;
1277no_free_objectid: 1279no_free_objectid:
@@ -1287,12 +1289,17 @@ root_item_alloc_fail:
1287static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 1289static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1288 struct btrfs_fs_info *fs_info) 1290 struct btrfs_fs_info *fs_info)
1289{ 1291{
1290 struct btrfs_pending_snapshot *pending; 1292 struct btrfs_pending_snapshot *pending, *next;
1291 struct list_head *head = &trans->transaction->pending_snapshots; 1293 struct list_head *head = &trans->transaction->pending_snapshots;
1294 int ret = 0;
1292 1295
1293 list_for_each_entry(pending, head, list) 1296 list_for_each_entry_safe(pending, next, head, list) {
1294 create_pending_snapshot(trans, fs_info, pending); 1297 list_del(&pending->list);
1295 return 0; 1298 ret = create_pending_snapshot(trans, fs_info, pending);
1299 if (ret)
1300 break;
1301 }
1302 return ret;
1296} 1303}
1297 1304
1298static void update_super_roots(struct btrfs_root *root) 1305static void update_super_roots(struct btrfs_root *root)
@@ -1448,6 +1455,13 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1448 btrfs_abort_transaction(trans, root, err); 1455 btrfs_abort_transaction(trans, root, err);
1449 1456
1450 spin_lock(&root->fs_info->trans_lock); 1457 spin_lock(&root->fs_info->trans_lock);
1458
1459 if (list_empty(&cur_trans->list)) {
1460 spin_unlock(&root->fs_info->trans_lock);
1461 btrfs_end_transaction(trans, root);
1462 return;
1463 }
1464
1451 list_del_init(&cur_trans->list); 1465 list_del_init(&cur_trans->list);
1452 if (cur_trans == root->fs_info->running_transaction) { 1466 if (cur_trans == root->fs_info->running_transaction) {
1453 root->fs_info->trans_no_join = 1; 1467 root->fs_info->trans_no_join = 1;
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index c7ef569eb22a..451fad96ecd1 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1382,7 +1382,10 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1382 1382
1383 btrfs_release_path(path); 1383 btrfs_release_path(path);
1384 if (ret == 0) { 1384 if (ret == 0) {
1385 btrfs_inc_nlink(inode); 1385 if (!inode->i_nlink)
1386 set_nlink(inode, 1);
1387 else
1388 btrfs_inc_nlink(inode);
1386 ret = btrfs_update_inode(trans, root, inode); 1389 ret = btrfs_update_inode(trans, root, inode);
1387 } else if (ret == -EEXIST) { 1390 } else if (ret == -EEXIST) {
1388 ret = 0; 1391 ret = 0;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 35bb2d4ed29f..2854c824ab64 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -684,6 +684,12 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
684 __btrfs_close_devices(fs_devices); 684 __btrfs_close_devices(fs_devices);
685 free_fs_devices(fs_devices); 685 free_fs_devices(fs_devices);
686 } 686 }
687 /*
688 * Wait for rcu kworkers under __btrfs_close_devices
689 * to finish all blkdev_puts so device is really
690 * free when umount is done.
691 */
692 rcu_barrier();
687 return ret; 693 return ret;
688} 694}
689 695
@@ -2379,7 +2385,11 @@ static int btrfs_relocate_chunk(struct btrfs_root *root,
2379 return ret; 2385 return ret;
2380 2386
2381 trans = btrfs_start_transaction(root, 0); 2387 trans = btrfs_start_transaction(root, 0);
2382 BUG_ON(IS_ERR(trans)); 2388 if (IS_ERR(trans)) {
2389 ret = PTR_ERR(trans);
2390 btrfs_std_error(root->fs_info, ret);
2391 return ret;
2392 }
2383 2393
2384 lock_chunks(root); 2394 lock_chunks(root);
2385 2395
@@ -3050,7 +3060,8 @@ static void __cancel_balance(struct btrfs_fs_info *fs_info)
3050 3060
3051 unset_balance_control(fs_info); 3061 unset_balance_control(fs_info);
3052 ret = del_balance_item(fs_info->tree_root); 3062 ret = del_balance_item(fs_info->tree_root);
3053 BUG_ON(ret); 3063 if (ret)
3064 btrfs_std_error(fs_info, ret);
3054 3065
3055 atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3066 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3056} 3067}
@@ -3230,6 +3241,11 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3230 update_ioctl_balance_args(fs_info, 0, bargs); 3241 update_ioctl_balance_args(fs_info, 0, bargs);
3231 } 3242 }
3232 3243
3244 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3245 balance_need_close(fs_info)) {
3246 __cancel_balance(fs_info);
3247 }
3248
3233 wake_up(&fs_info->balance_wait_q); 3249 wake_up(&fs_info->balance_wait_q);
3234 3250
3235 return ret; 3251 return ret;
@@ -4919,7 +4935,18 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4919 em = lookup_extent_mapping(em_tree, chunk_start, 1); 4935 em = lookup_extent_mapping(em_tree, chunk_start, 1);
4920 read_unlock(&em_tree->lock); 4936 read_unlock(&em_tree->lock);
4921 4937
4922 BUG_ON(!em || em->start != chunk_start); 4938 if (!em) {
4939 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4940 chunk_start);
4941 return -EIO;
4942 }
4943
4944 if (em->start != chunk_start) {
4945 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4946 em->start, chunk_start);
4947 free_extent_map(em);
4948 return -EIO;
4949 }
4923 map = (struct map_lookup *)em->bdev; 4950 map = (struct map_lookup *)em->bdev;
4924 4951
4925 length = em->len; 4952 length = em->len;