aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/transaction.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/transaction.c')
-rw-r--r--fs/btrfs/transaction.c261
1 files changed, 191 insertions, 70 deletions
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 87fac9a21ea5..9250b9c4f01e 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -40,7 +40,6 @@ void put_transaction(struct btrfs_transaction *transaction)
40 if (atomic_dec_and_test(&transaction->use_count)) { 40 if (atomic_dec_and_test(&transaction->use_count)) {
41 BUG_ON(!list_empty(&transaction->list)); 41 BUG_ON(!list_empty(&transaction->list));
42 WARN_ON(transaction->delayed_refs.root.rb_node); 42 WARN_ON(transaction->delayed_refs.root.rb_node);
43 memset(transaction, 0, sizeof(*transaction));
44 kmem_cache_free(btrfs_transaction_cachep, transaction); 43 kmem_cache_free(btrfs_transaction_cachep, transaction);
45 } 44 }
46} 45}
@@ -51,6 +50,14 @@ static noinline void switch_commit_root(struct btrfs_root *root)
51 root->commit_root = btrfs_root_node(root); 50 root->commit_root = btrfs_root_node(root);
52} 51}
53 52
53static inline int can_join_transaction(struct btrfs_transaction *trans,
54 int type)
55{
56 return !(trans->in_commit &&
57 type != TRANS_JOIN &&
58 type != TRANS_JOIN_NOLOCK);
59}
60
54/* 61/*
55 * either allocate a new transaction or hop into the existing one 62 * either allocate a new transaction or hop into the existing one
56 */ 63 */
@@ -62,7 +69,7 @@ static noinline int join_transaction(struct btrfs_root *root, int type)
62 spin_lock(&fs_info->trans_lock); 69 spin_lock(&fs_info->trans_lock);
63loop: 70loop:
64 /* The file system has been taken offline. No new transactions. */ 71 /* The file system has been taken offline. No new transactions. */
65 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 72 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
66 spin_unlock(&fs_info->trans_lock); 73 spin_unlock(&fs_info->trans_lock);
67 return -EROFS; 74 return -EROFS;
68 } 75 }
@@ -86,6 +93,10 @@ loop:
86 spin_unlock(&fs_info->trans_lock); 93 spin_unlock(&fs_info->trans_lock);
87 return cur_trans->aborted; 94 return cur_trans->aborted;
88 } 95 }
96 if (!can_join_transaction(cur_trans, type)) {
97 spin_unlock(&fs_info->trans_lock);
98 return -EBUSY;
99 }
89 atomic_inc(&cur_trans->use_count); 100 atomic_inc(&cur_trans->use_count);
90 atomic_inc(&cur_trans->num_writers); 101 atomic_inc(&cur_trans->num_writers);
91 cur_trans->num_joined++; 102 cur_trans->num_joined++;
@@ -112,9 +123,8 @@ loop:
112 * to redo the trans_no_join checks above 123 * to redo the trans_no_join checks above
113 */ 124 */
114 kmem_cache_free(btrfs_transaction_cachep, cur_trans); 125 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
115 cur_trans = fs_info->running_transaction;
116 goto loop; 126 goto loop;
117 } else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 127 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
118 spin_unlock(&fs_info->trans_lock); 128 spin_unlock(&fs_info->trans_lock);
119 kmem_cache_free(btrfs_transaction_cachep, cur_trans); 129 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
120 return -EROFS; 130 return -EROFS;
@@ -156,8 +166,12 @@ loop:
156 166
157 spin_lock_init(&cur_trans->commit_lock); 167 spin_lock_init(&cur_trans->commit_lock);
158 spin_lock_init(&cur_trans->delayed_refs.lock); 168 spin_lock_init(&cur_trans->delayed_refs.lock);
169 atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
170 atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
171 init_waitqueue_head(&cur_trans->delayed_refs.wait);
159 172
160 INIT_LIST_HEAD(&cur_trans->pending_snapshots); 173 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
174 INIT_LIST_HEAD(&cur_trans->ordered_operations);
161 list_add_tail(&cur_trans->list, &fs_info->trans_list); 175 list_add_tail(&cur_trans->list, &fs_info->trans_list);
162 extent_io_tree_init(&cur_trans->dirty_pages, 176 extent_io_tree_init(&cur_trans->dirty_pages,
163 fs_info->btree_inode->i_mapping); 177 fs_info->btree_inode->i_mapping);
@@ -302,7 +316,7 @@ start_transaction(struct btrfs_root *root, u64 num_items, int type,
302 int ret; 316 int ret;
303 u64 qgroup_reserved = 0; 317 u64 qgroup_reserved = 0;
304 318
305 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 319 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
306 return ERR_PTR(-EROFS); 320 return ERR_PTR(-EROFS);
307 321
308 if (current->journal_info) { 322 if (current->journal_info) {
@@ -333,12 +347,14 @@ start_transaction(struct btrfs_root *root, u64 num_items, int type,
333 &root->fs_info->trans_block_rsv, 347 &root->fs_info->trans_block_rsv,
334 num_bytes, flush); 348 num_bytes, flush);
335 if (ret) 349 if (ret)
336 return ERR_PTR(ret); 350 goto reserve_fail;
337 } 351 }
338again: 352again:
339 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 353 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
340 if (!h) 354 if (!h) {
341 return ERR_PTR(-ENOMEM); 355 ret = -ENOMEM;
356 goto alloc_fail;
357 }
342 358
343 /* 359 /*
344 * If we are JOIN_NOLOCK we're already committing a transaction and 360 * If we are JOIN_NOLOCK we're already committing a transaction and
@@ -358,18 +374,17 @@ again:
358 374
359 do { 375 do {
360 ret = join_transaction(root, type); 376 ret = join_transaction(root, type);
361 if (ret == -EBUSY) 377 if (ret == -EBUSY) {
362 wait_current_trans(root); 378 wait_current_trans(root);
379 if (unlikely(type == TRANS_ATTACH))
380 ret = -ENOENT;
381 }
363 } while (ret == -EBUSY); 382 } while (ret == -EBUSY);
364 383
365 if (ret < 0) { 384 if (ret < 0) {
366 /* We must get the transaction if we are JOIN_NOLOCK. */ 385 /* We must get the transaction if we are JOIN_NOLOCK. */
367 BUG_ON(type == TRANS_JOIN_NOLOCK); 386 BUG_ON(type == TRANS_JOIN_NOLOCK);
368 387 goto join_fail;
369 if (type < TRANS_JOIN_NOLOCK)
370 sb_end_intwrite(root->fs_info->sb);
371 kmem_cache_free(btrfs_trans_handle_cachep, h);
372 return ERR_PTR(ret);
373 } 388 }
374 389
375 cur_trans = root->fs_info->running_transaction; 390 cur_trans = root->fs_info->running_transaction;
@@ -385,9 +400,10 @@ again:
385 h->block_rsv = NULL; 400 h->block_rsv = NULL;
386 h->orig_rsv = NULL; 401 h->orig_rsv = NULL;
387 h->aborted = 0; 402 h->aborted = 0;
388 h->qgroup_reserved = qgroup_reserved; 403 h->qgroup_reserved = 0;
389 h->delayed_ref_elem.seq = 0; 404 h->delayed_ref_elem.seq = 0;
390 h->type = type; 405 h->type = type;
406 h->allocating_chunk = false;
391 INIT_LIST_HEAD(&h->qgroup_ref_list); 407 INIT_LIST_HEAD(&h->qgroup_ref_list);
392 INIT_LIST_HEAD(&h->new_bgs); 408 INIT_LIST_HEAD(&h->new_bgs);
393 409
@@ -403,6 +419,7 @@ again:
403 h->block_rsv = &root->fs_info->trans_block_rsv; 419 h->block_rsv = &root->fs_info->trans_block_rsv;
404 h->bytes_reserved = num_bytes; 420 h->bytes_reserved = num_bytes;
405 } 421 }
422 h->qgroup_reserved = qgroup_reserved;
406 423
407got_it: 424got_it:
408 btrfs_record_root_in_trans(h, root); 425 btrfs_record_root_in_trans(h, root);
@@ -410,6 +427,19 @@ got_it:
410 if (!current->journal_info && type != TRANS_USERSPACE) 427 if (!current->journal_info && type != TRANS_USERSPACE)
411 current->journal_info = h; 428 current->journal_info = h;
412 return h; 429 return h;
430
431join_fail:
432 if (type < TRANS_JOIN_NOLOCK)
433 sb_end_intwrite(root->fs_info->sb);
434 kmem_cache_free(btrfs_trans_handle_cachep, h);
435alloc_fail:
436 if (num_bytes)
437 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
438 num_bytes);
439reserve_fail:
440 if (qgroup_reserved)
441 btrfs_qgroup_free(root, qgroup_reserved);
442 return ERR_PTR(ret);
413} 443}
414 444
415struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 445struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
@@ -441,11 +471,43 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root
441 return start_transaction(root, 0, TRANS_USERSPACE, 0); 471 return start_transaction(root, 0, TRANS_USERSPACE, 0);
442} 472}
443 473
474/*
475 * btrfs_attach_transaction() - catch the running transaction
476 *
477 * It is used when we want to commit the current the transaction, but
478 * don't want to start a new one.
479 *
480 * Note: If this function return -ENOENT, it just means there is no
481 * running transaction. But it is possible that the inactive transaction
482 * is still in the memory, not fully on disk. If you hope there is no
483 * inactive transaction in the fs when -ENOENT is returned, you should
484 * invoke
485 * btrfs_attach_transaction_barrier()
486 */
444struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 487struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
445{ 488{
446 return start_transaction(root, 0, TRANS_ATTACH, 0); 489 return start_transaction(root, 0, TRANS_ATTACH, 0);
447} 490}
448 491
492/*
493 * btrfs_attach_transaction() - catch the running transaction
494 *
495 * It is similar to the above function, the differentia is this one
496 * will wait for all the inactive transactions until they fully
497 * complete.
498 */
499struct btrfs_trans_handle *
500btrfs_attach_transaction_barrier(struct btrfs_root *root)
501{
502 struct btrfs_trans_handle *trans;
503
504 trans = start_transaction(root, 0, TRANS_ATTACH, 0);
505 if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
506 btrfs_wait_for_commit(root, 0);
507
508 return trans;
509}
510
449/* wait for a transaction commit to be fully complete */ 511/* wait for a transaction commit to be fully complete */
450static noinline void wait_for_commit(struct btrfs_root *root, 512static noinline void wait_for_commit(struct btrfs_root *root,
451 struct btrfs_transaction *commit) 513 struct btrfs_transaction *commit)
@@ -577,7 +639,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
577 if (!list_empty(&trans->new_bgs)) 639 if (!list_empty(&trans->new_bgs))
578 btrfs_create_pending_block_groups(trans, root); 640 btrfs_create_pending_block_groups(trans, root);
579 641
580 while (count < 2) { 642 while (count < 1) {
581 unsigned long cur = trans->delayed_ref_updates; 643 unsigned long cur = trans->delayed_ref_updates;
582 trans->delayed_ref_updates = 0; 644 trans->delayed_ref_updates = 0;
583 if (cur && 645 if (cur &&
@@ -589,6 +651,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
589 } 651 }
590 count++; 652 count++;
591 } 653 }
654
592 btrfs_trans_release_metadata(trans, root); 655 btrfs_trans_release_metadata(trans, root);
593 trans->block_rsv = NULL; 656 trans->block_rsv = NULL;
594 657
@@ -634,12 +697,10 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
634 btrfs_run_delayed_iputs(root); 697 btrfs_run_delayed_iputs(root);
635 698
636 if (trans->aborted || 699 if (trans->aborted ||
637 root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 700 test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
638 err = -EIO; 701 err = -EIO;
639 }
640 assert_qgroups_uptodate(trans); 702 assert_qgroups_uptodate(trans);
641 703
642 memset(trans, 0, sizeof(*trans));
643 kmem_cache_free(btrfs_trans_handle_cachep, trans); 704 kmem_cache_free(btrfs_trans_handle_cachep, trans);
644 return err; 705 return err;
645} 706}
@@ -686,7 +747,9 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
686 struct extent_state *cached_state = NULL; 747 struct extent_state *cached_state = NULL;
687 u64 start = 0; 748 u64 start = 0;
688 u64 end; 749 u64 end;
750 struct blk_plug plug;
689 751
752 blk_start_plug(&plug);
690 while (!find_first_extent_bit(dirty_pages, start, &start, &end, 753 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
691 mark, &cached_state)) { 754 mark, &cached_state)) {
692 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 755 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
@@ -700,6 +763,7 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
700 } 763 }
701 if (err) 764 if (err)
702 werr = err; 765 werr = err;
766 blk_finish_plug(&plug);
703 return werr; 767 return werr;
704} 768}
705 769
@@ -950,10 +1014,10 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
950} 1014}
951 1015
952/* 1016/*
953 * defrag a given btree. If cacheonly == 1, this won't read from the disk, 1017 * defrag a given btree.
954 * otherwise every leaf in the btree is read and defragged. 1018 * Every leaf in the btree is read and defragged.
955 */ 1019 */
956int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 1020int btrfs_defrag_root(struct btrfs_root *root)
957{ 1021{
958 struct btrfs_fs_info *info = root->fs_info; 1022 struct btrfs_fs_info *info = root->fs_info;
959 struct btrfs_trans_handle *trans; 1023 struct btrfs_trans_handle *trans;
@@ -967,7 +1031,7 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
967 if (IS_ERR(trans)) 1031 if (IS_ERR(trans))
968 return PTR_ERR(trans); 1032 return PTR_ERR(trans);
969 1033
970 ret = btrfs_defrag_leaves(trans, root, cacheonly); 1034 ret = btrfs_defrag_leaves(trans, root);
971 1035
972 btrfs_end_transaction(trans, root); 1036 btrfs_end_transaction(trans, root);
973 btrfs_btree_balance_dirty(info->tree_root); 1037 btrfs_btree_balance_dirty(info->tree_root);
@@ -975,6 +1039,12 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
975 1039
976 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1040 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
977 break; 1041 break;
1042
1043 if (btrfs_defrag_cancelled(root->fs_info)) {
1044 printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1045 ret = -EAGAIN;
1046 break;
1047 }
978 } 1048 }
979 root->defrag_running = 0; 1049 root->defrag_running = 0;
980 return ret; 1050 return ret;
@@ -982,7 +1052,12 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
982 1052
983/* 1053/*
984 * new snapshots need to be created at a very specific time in the 1054 * new snapshots need to be created at a very specific time in the
985 * transaction commit. This does the actual creation 1055 * transaction commit. This does the actual creation.
1056 *
1057 * Note:
1058 * If the error which may affect the commitment of the current transaction
1059 * happens, we should return the error number. If the error which just affect
1060 * the creation of the pending snapshots, just return 0.
986 */ 1061 */
987static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 1062static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
988 struct btrfs_fs_info *fs_info, 1063 struct btrfs_fs_info *fs_info,
@@ -997,12 +1072,11 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
997 struct inode *parent_inode; 1072 struct inode *parent_inode;
998 struct btrfs_path *path; 1073 struct btrfs_path *path;
999 struct btrfs_dir_item *dir_item; 1074 struct btrfs_dir_item *dir_item;
1000 struct dentry *parent;
1001 struct dentry *dentry; 1075 struct dentry *dentry;
1002 struct extent_buffer *tmp; 1076 struct extent_buffer *tmp;
1003 struct extent_buffer *old; 1077 struct extent_buffer *old;
1004 struct timespec cur_time = CURRENT_TIME; 1078 struct timespec cur_time = CURRENT_TIME;
1005 int ret; 1079 int ret = 0;
1006 u64 to_reserve = 0; 1080 u64 to_reserve = 0;
1007 u64 index = 0; 1081 u64 index = 0;
1008 u64 objectid; 1082 u64 objectid;
@@ -1011,40 +1085,36 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1011 1085
1012 path = btrfs_alloc_path(); 1086 path = btrfs_alloc_path();
1013 if (!path) { 1087 if (!path) {
1014 ret = pending->error = -ENOMEM; 1088 pending->error = -ENOMEM;
1015 goto path_alloc_fail; 1089 return 0;
1016 } 1090 }
1017 1091
1018 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 1092 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
1019 if (!new_root_item) { 1093 if (!new_root_item) {
1020 ret = pending->error = -ENOMEM; 1094 pending->error = -ENOMEM;
1021 goto root_item_alloc_fail; 1095 goto root_item_alloc_fail;
1022 } 1096 }
1023 1097
1024 ret = btrfs_find_free_objectid(tree_root, &objectid); 1098 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1025 if (ret) { 1099 if (pending->error)
1026 pending->error = ret;
1027 goto no_free_objectid; 1100 goto no_free_objectid;
1028 }
1029 1101
1030 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1102 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
1031 1103
1032 if (to_reserve > 0) { 1104 if (to_reserve > 0) {
1033 ret = btrfs_block_rsv_add(root, &pending->block_rsv, 1105 pending->error = btrfs_block_rsv_add(root,
1034 to_reserve, 1106 &pending->block_rsv,
1035 BTRFS_RESERVE_NO_FLUSH); 1107 to_reserve,
1036 if (ret) { 1108 BTRFS_RESERVE_NO_FLUSH);
1037 pending->error = ret; 1109 if (pending->error)
1038 goto no_free_objectid; 1110 goto no_free_objectid;
1039 }
1040 } 1111 }
1041 1112
1042 ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid, 1113 pending->error = btrfs_qgroup_inherit(trans, fs_info,
1043 objectid, pending->inherit); 1114 root->root_key.objectid,
1044 if (ret) { 1115 objectid, pending->inherit);
1045 pending->error = ret; 1116 if (pending->error)
1046 goto no_free_objectid; 1117 goto no_free_objectid;
1047 }
1048 1118
1049 key.objectid = objectid; 1119 key.objectid = objectid;
1050 key.offset = (u64)-1; 1120 key.offset = (u64)-1;
@@ -1052,10 +1122,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1052 1122
1053 rsv = trans->block_rsv; 1123 rsv = trans->block_rsv;
1054 trans->block_rsv = &pending->block_rsv; 1124 trans->block_rsv = &pending->block_rsv;
1125 trans->bytes_reserved = trans->block_rsv->reserved;
1055 1126
1056 dentry = pending->dentry; 1127 dentry = pending->dentry;
1057 parent = dget_parent(dentry); 1128 parent_inode = pending->dir;
1058 parent_inode = parent->d_inode;
1059 parent_root = BTRFS_I(parent_inode)->root; 1129 parent_root = BTRFS_I(parent_inode)->root;
1060 record_root_in_trans(trans, parent_root); 1130 record_root_in_trans(trans, parent_root);
1061 1131
@@ -1072,7 +1142,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1072 dentry->d_name.len, 0); 1142 dentry->d_name.len, 0);
1073 if (dir_item != NULL && !IS_ERR(dir_item)) { 1143 if (dir_item != NULL && !IS_ERR(dir_item)) {
1074 pending->error = -EEXIST; 1144 pending->error = -EEXIST;
1075 goto fail; 1145 goto dir_item_existed;
1076 } else if (IS_ERR(dir_item)) { 1146 } else if (IS_ERR(dir_item)) {
1077 ret = PTR_ERR(dir_item); 1147 ret = PTR_ERR(dir_item);
1078 btrfs_abort_transaction(trans, root, ret); 1148 btrfs_abort_transaction(trans, root, ret);
@@ -1203,14 +1273,14 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1203 if (ret) 1273 if (ret)
1204 btrfs_abort_transaction(trans, root, ret); 1274 btrfs_abort_transaction(trans, root, ret);
1205fail: 1275fail:
1206 dput(parent); 1276 pending->error = ret;
1277dir_item_existed:
1207 trans->block_rsv = rsv; 1278 trans->block_rsv = rsv;
1279 trans->bytes_reserved = 0;
1208no_free_objectid: 1280no_free_objectid:
1209 kfree(new_root_item); 1281 kfree(new_root_item);
1210root_item_alloc_fail: 1282root_item_alloc_fail:
1211 btrfs_free_path(path); 1283 btrfs_free_path(path);
1212path_alloc_fail:
1213 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1214 return ret; 1284 return ret;
1215} 1285}
1216 1286
@@ -1220,12 +1290,17 @@ path_alloc_fail:
1220static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 1290static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1221 struct btrfs_fs_info *fs_info) 1291 struct btrfs_fs_info *fs_info)
1222{ 1292{
1223 struct btrfs_pending_snapshot *pending; 1293 struct btrfs_pending_snapshot *pending, *next;
1224 struct list_head *head = &trans->transaction->pending_snapshots; 1294 struct list_head *head = &trans->transaction->pending_snapshots;
1295 int ret = 0;
1225 1296
1226 list_for_each_entry(pending, head, list) 1297 list_for_each_entry_safe(pending, next, head, list) {
1227 create_pending_snapshot(trans, fs_info, pending); 1298 list_del(&pending->list);
1228 return 0; 1299 ret = create_pending_snapshot(trans, fs_info, pending);
1300 if (ret)
1301 break;
1302 }
1303 return ret;
1229} 1304}
1230 1305
1231static void update_super_roots(struct btrfs_root *root) 1306static void update_super_roots(struct btrfs_root *root)
@@ -1296,13 +1371,13 @@ static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1296struct btrfs_async_commit { 1371struct btrfs_async_commit {
1297 struct btrfs_trans_handle *newtrans; 1372 struct btrfs_trans_handle *newtrans;
1298 struct btrfs_root *root; 1373 struct btrfs_root *root;
1299 struct delayed_work work; 1374 struct work_struct work;
1300}; 1375};
1301 1376
1302static void do_async_commit(struct work_struct *work) 1377static void do_async_commit(struct work_struct *work)
1303{ 1378{
1304 struct btrfs_async_commit *ac = 1379 struct btrfs_async_commit *ac =
1305 container_of(work, struct btrfs_async_commit, work.work); 1380 container_of(work, struct btrfs_async_commit, work);
1306 1381
1307 /* 1382 /*
1308 * We've got freeze protection passed with the transaction. 1383 * We've got freeze protection passed with the transaction.
@@ -1330,7 +1405,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1330 if (!ac) 1405 if (!ac)
1331 return -ENOMEM; 1406 return -ENOMEM;
1332 1407
1333 INIT_DELAYED_WORK(&ac->work, do_async_commit); 1408 INIT_WORK(&ac->work, do_async_commit);
1334 ac->root = root; 1409 ac->root = root;
1335 ac->newtrans = btrfs_join_transaction(root); 1410 ac->newtrans = btrfs_join_transaction(root);
1336 if (IS_ERR(ac->newtrans)) { 1411 if (IS_ERR(ac->newtrans)) {
@@ -1354,7 +1429,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1354 &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 1429 &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1355 1, _THIS_IP_); 1430 1, _THIS_IP_);
1356 1431
1357 schedule_delayed_work(&ac->work, 0); 1432 schedule_work(&ac->work);
1358 1433
1359 /* wait for transaction to start and unblock */ 1434 /* wait for transaction to start and unblock */
1360 if (wait_for_unblock) 1435 if (wait_for_unblock)
@@ -1374,16 +1449,29 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root, int err) 1449 struct btrfs_root *root, int err)
1375{ 1450{
1376 struct btrfs_transaction *cur_trans = trans->transaction; 1451 struct btrfs_transaction *cur_trans = trans->transaction;
1452 DEFINE_WAIT(wait);
1377 1453
1378 WARN_ON(trans->use_count > 1); 1454 WARN_ON(trans->use_count > 1);
1379 1455
1380 btrfs_abort_transaction(trans, root, err); 1456 btrfs_abort_transaction(trans, root, err);
1381 1457
1382 spin_lock(&root->fs_info->trans_lock); 1458 spin_lock(&root->fs_info->trans_lock);
1459
1460 if (list_empty(&cur_trans->list)) {
1461 spin_unlock(&root->fs_info->trans_lock);
1462 btrfs_end_transaction(trans, root);
1463 return;
1464 }
1465
1383 list_del_init(&cur_trans->list); 1466 list_del_init(&cur_trans->list);
1384 if (cur_trans == root->fs_info->running_transaction) { 1467 if (cur_trans == root->fs_info->running_transaction) {
1468 root->fs_info->trans_no_join = 1;
1469 spin_unlock(&root->fs_info->trans_lock);
1470 wait_event(cur_trans->writer_wait,
1471 atomic_read(&cur_trans->num_writers) == 1);
1472
1473 spin_lock(&root->fs_info->trans_lock);
1385 root->fs_info->running_transaction = NULL; 1474 root->fs_info->running_transaction = NULL;
1386 root->fs_info->trans_no_join = 0;
1387 } 1475 }
1388 spin_unlock(&root->fs_info->trans_lock); 1476 spin_unlock(&root->fs_info->trans_lock);
1389 1477
@@ -1417,7 +1505,9 @@ static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1417 } 1505 }
1418 1506
1419 if (flush_on_commit || snap_pending) { 1507 if (flush_on_commit || snap_pending) {
1420 btrfs_start_delalloc_inodes(root, 1); 1508 ret = btrfs_start_delalloc_inodes(root, 1);
1509 if (ret)
1510 return ret;
1421 btrfs_wait_ordered_extents(root, 1); 1511 btrfs_wait_ordered_extents(root, 1);
1422 } 1512 }
1423 1513
@@ -1439,9 +1529,9 @@ static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1439 * it here and no for sure that nothing new will be added 1529 * it here and no for sure that nothing new will be added
1440 * to the list 1530 * to the list
1441 */ 1531 */
1442 btrfs_run_ordered_operations(root, 1); 1532 ret = btrfs_run_ordered_operations(trans, root, 1);
1443 1533
1444 return 0; 1534 return ret;
1445} 1535}
1446 1536
1447/* 1537/*
@@ -1462,26 +1552,35 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1462 int should_grow = 0; 1552 int should_grow = 0;
1463 unsigned long now = get_seconds(); 1553 unsigned long now = get_seconds();
1464 1554
1465 ret = btrfs_run_ordered_operations(root, 0); 1555 ret = btrfs_run_ordered_operations(trans, root, 0);
1466 if (ret) { 1556 if (ret) {
1467 btrfs_abort_transaction(trans, root, ret); 1557 btrfs_abort_transaction(trans, root, ret);
1468 goto cleanup_transaction; 1558 btrfs_end_transaction(trans, root);
1559 return ret;
1469 } 1560 }
1470 1561
1471 if (cur_trans->aborted) { 1562 /* Stop the commit early if ->aborted is set */
1563 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1472 ret = cur_trans->aborted; 1564 ret = cur_trans->aborted;
1473 goto cleanup_transaction; 1565 btrfs_end_transaction(trans, root);
1566 return ret;
1474 } 1567 }
1475 1568
1476 /* make a pass through all the delayed refs we have so far 1569 /* make a pass through all the delayed refs we have so far
1477 * any runnings procs may add more while we are here 1570 * any runnings procs may add more while we are here
1478 */ 1571 */
1479 ret = btrfs_run_delayed_refs(trans, root, 0); 1572 ret = btrfs_run_delayed_refs(trans, root, 0);
1480 if (ret) 1573 if (ret) {
1481 goto cleanup_transaction; 1574 btrfs_end_transaction(trans, root);
1575 return ret;
1576 }
1482 1577
1483 btrfs_trans_release_metadata(trans, root); 1578 btrfs_trans_release_metadata(trans, root);
1484 trans->block_rsv = NULL; 1579 trans->block_rsv = NULL;
1580 if (trans->qgroup_reserved) {
1581 btrfs_qgroup_free(root, trans->qgroup_reserved);
1582 trans->qgroup_reserved = 0;
1583 }
1485 1584
1486 cur_trans = trans->transaction; 1585 cur_trans = trans->transaction;
1487 1586
@@ -1495,8 +1594,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1495 btrfs_create_pending_block_groups(trans, root); 1594 btrfs_create_pending_block_groups(trans, root);
1496 1595
1497 ret = btrfs_run_delayed_refs(trans, root, 0); 1596 ret = btrfs_run_delayed_refs(trans, root, 0);
1498 if (ret) 1597 if (ret) {
1499 goto cleanup_transaction; 1598 btrfs_end_transaction(trans, root);
1599 return ret;
1600 }
1500 1601
1501 spin_lock(&cur_trans->commit_lock); 1602 spin_lock(&cur_trans->commit_lock);
1502 if (cur_trans->in_commit) { 1603 if (cur_trans->in_commit) {
@@ -1574,6 +1675,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1574 wait_event(cur_trans->writer_wait, 1675 wait_event(cur_trans->writer_wait,
1575 atomic_read(&cur_trans->num_writers) == 1); 1676 atomic_read(&cur_trans->num_writers) == 1);
1576 1677
1678 /* ->aborted might be set after the previous check, so check it */
1679 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1680 ret = cur_trans->aborted;
1681 goto cleanup_transaction;
1682 }
1577 /* 1683 /*
1578 * the reloc mutex makes sure that we stop 1684 * the reloc mutex makes sure that we stop
1579 * the balancing code from coming in and moving 1685 * the balancing code from coming in and moving
@@ -1657,6 +1763,17 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1657 goto cleanup_transaction; 1763 goto cleanup_transaction;
1658 } 1764 }
1659 1765
1766 /*
1767 * The tasks which save the space cache and inode cache may also
1768 * update ->aborted, check it.
1769 */
1770 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1771 ret = cur_trans->aborted;
1772 mutex_unlock(&root->fs_info->tree_log_mutex);
1773 mutex_unlock(&root->fs_info->reloc_mutex);
1774 goto cleanup_transaction;
1775 }
1776
1660 btrfs_prepare_extent_commit(trans, root); 1777 btrfs_prepare_extent_commit(trans, root);
1661 1778
1662 cur_trans = root->fs_info->running_transaction; 1779 cur_trans = root->fs_info->running_transaction;
@@ -1744,6 +1861,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1744cleanup_transaction: 1861cleanup_transaction:
1745 btrfs_trans_release_metadata(trans, root); 1862 btrfs_trans_release_metadata(trans, root);
1746 trans->block_rsv = NULL; 1863 trans->block_rsv = NULL;
1864 if (trans->qgroup_reserved) {
1865 btrfs_qgroup_free(root, trans->qgroup_reserved);
1866 trans->qgroup_reserved = 0;
1867 }
1747 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n"); 1868 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1748// WARN_ON(1); 1869// WARN_ON(1);
1749 if (current->journal_info == trans) 1870 if (current->journal_info == trans)