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.c108
1 files changed, 89 insertions, 19 deletions
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index b72b068183ec..17be3dedacba 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -22,6 +22,7 @@
22#include <linux/writeback.h> 22#include <linux/writeback.h>
23#include <linux/pagemap.h> 23#include <linux/pagemap.h>
24#include <linux/blkdev.h> 24#include <linux/blkdev.h>
25#include <linux/uuid.h>
25#include "ctree.h" 26#include "ctree.h"
26#include "disk-io.h" 27#include "disk-io.h"
27#include "transaction.h" 28#include "transaction.h"
@@ -38,7 +39,6 @@ void put_transaction(struct btrfs_transaction *transaction)
38 if (atomic_dec_and_test(&transaction->use_count)) { 39 if (atomic_dec_and_test(&transaction->use_count)) {
39 BUG_ON(!list_empty(&transaction->list)); 40 BUG_ON(!list_empty(&transaction->list));
40 WARN_ON(transaction->delayed_refs.root.rb_node); 41 WARN_ON(transaction->delayed_refs.root.rb_node);
41 WARN_ON(!list_empty(&transaction->delayed_refs.seq_head));
42 memset(transaction, 0, sizeof(*transaction)); 42 memset(transaction, 0, sizeof(*transaction));
43 kmem_cache_free(btrfs_transaction_cachep, transaction); 43 kmem_cache_free(btrfs_transaction_cachep, transaction);
44 } 44 }
@@ -100,8 +100,8 @@ loop:
100 kmem_cache_free(btrfs_transaction_cachep, cur_trans); 100 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
101 cur_trans = fs_info->running_transaction; 101 cur_trans = fs_info->running_transaction;
102 goto loop; 102 goto loop;
103 } else if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 103 } else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
104 spin_unlock(&root->fs_info->trans_lock); 104 spin_unlock(&fs_info->trans_lock);
105 kmem_cache_free(btrfs_transaction_cachep, cur_trans); 105 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
106 return -EROFS; 106 return -EROFS;
107 } 107 }
@@ -126,7 +126,6 @@ loop:
126 cur_trans->delayed_refs.num_heads = 0; 126 cur_trans->delayed_refs.num_heads = 0;
127 cur_trans->delayed_refs.flushing = 0; 127 cur_trans->delayed_refs.flushing = 0;
128 cur_trans->delayed_refs.run_delayed_start = 0; 128 cur_trans->delayed_refs.run_delayed_start = 0;
129 cur_trans->delayed_refs.seq = 1;
130 129
131 /* 130 /*
132 * although the tree mod log is per file system and not per transaction, 131 * although the tree mod log is per file system and not per transaction,
@@ -145,10 +144,8 @@ loop:
145 } 144 }
146 atomic_set(&fs_info->tree_mod_seq, 0); 145 atomic_set(&fs_info->tree_mod_seq, 0);
147 146
148 init_waitqueue_head(&cur_trans->delayed_refs.seq_wait);
149 spin_lock_init(&cur_trans->commit_lock); 147 spin_lock_init(&cur_trans->commit_lock);
150 spin_lock_init(&cur_trans->delayed_refs.lock); 148 spin_lock_init(&cur_trans->delayed_refs.lock);
151 INIT_LIST_HEAD(&cur_trans->delayed_refs.seq_head);
152 149
153 INIT_LIST_HEAD(&cur_trans->pending_snapshots); 150 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
154 list_add_tail(&cur_trans->list, &fs_info->trans_list); 151 list_add_tail(&cur_trans->list, &fs_info->trans_list);
@@ -299,6 +296,7 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
299 struct btrfs_transaction *cur_trans; 296 struct btrfs_transaction *cur_trans;
300 u64 num_bytes = 0; 297 u64 num_bytes = 0;
301 int ret; 298 int ret;
299 u64 qgroup_reserved = 0;
302 300
303 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 301 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
304 return ERR_PTR(-EROFS); 302 return ERR_PTR(-EROFS);
@@ -317,6 +315,14 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
317 * the appropriate flushing if need be. 315 * the appropriate flushing if need be.
318 */ 316 */
319 if (num_items > 0 && root != root->fs_info->chunk_root) { 317 if (num_items > 0 && root != root->fs_info->chunk_root) {
318 if (root->fs_info->quota_enabled &&
319 is_fstree(root->root_key.objectid)) {
320 qgroup_reserved = num_items * root->leafsize;
321 ret = btrfs_qgroup_reserve(root, qgroup_reserved);
322 if (ret)
323 return ERR_PTR(ret);
324 }
325
320 num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 326 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
321 ret = btrfs_block_rsv_add(root, 327 ret = btrfs_block_rsv_add(root,
322 &root->fs_info->trans_block_rsv, 328 &root->fs_info->trans_block_rsv,
@@ -329,6 +335,8 @@ again:
329 if (!h) 335 if (!h)
330 return ERR_PTR(-ENOMEM); 336 return ERR_PTR(-ENOMEM);
331 337
338 sb_start_intwrite(root->fs_info->sb);
339
332 if (may_wait_transaction(root, type)) 340 if (may_wait_transaction(root, type))
333 wait_current_trans(root); 341 wait_current_trans(root);
334 342
@@ -339,6 +347,7 @@ again:
339 } while (ret == -EBUSY); 347 } while (ret == -EBUSY);
340 348
341 if (ret < 0) { 349 if (ret < 0) {
350 sb_end_intwrite(root->fs_info->sb);
342 kmem_cache_free(btrfs_trans_handle_cachep, h); 351 kmem_cache_free(btrfs_trans_handle_cachep, h);
343 return ERR_PTR(ret); 352 return ERR_PTR(ret);
344 } 353 }
@@ -349,11 +358,16 @@ again:
349 h->transaction = cur_trans; 358 h->transaction = cur_trans;
350 h->blocks_used = 0; 359 h->blocks_used = 0;
351 h->bytes_reserved = 0; 360 h->bytes_reserved = 0;
361 h->root = root;
352 h->delayed_ref_updates = 0; 362 h->delayed_ref_updates = 0;
353 h->use_count = 1; 363 h->use_count = 1;
364 h->adding_csums = 0;
354 h->block_rsv = NULL; 365 h->block_rsv = NULL;
355 h->orig_rsv = NULL; 366 h->orig_rsv = NULL;
356 h->aborted = 0; 367 h->aborted = 0;
368 h->qgroup_reserved = qgroup_reserved;
369 h->delayed_ref_elem.seq = 0;
370 INIT_LIST_HEAD(&h->qgroup_ref_list);
357 371
358 smp_mb(); 372 smp_mb();
359 if (cur_trans->blocked && may_wait_transaction(root, type)) { 373 if (cur_trans->blocked && may_wait_transaction(root, type)) {
@@ -473,7 +487,6 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
473 struct btrfs_root *root) 487 struct btrfs_root *root)
474{ 488{
475 struct btrfs_transaction *cur_trans = trans->transaction; 489 struct btrfs_transaction *cur_trans = trans->transaction;
476 struct btrfs_block_rsv *rsv = trans->block_rsv;
477 int updates; 490 int updates;
478 int err; 491 int err;
479 492
@@ -481,12 +494,6 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
481 if (cur_trans->blocked || cur_trans->delayed_refs.flushing) 494 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
482 return 1; 495 return 1;
483 496
484 /*
485 * We need to do this in case we're deleting csums so the global block
486 * rsv get's used instead of the csum block rsv.
487 */
488 trans->block_rsv = NULL;
489
490 updates = trans->delayed_ref_updates; 497 updates = trans->delayed_ref_updates;
491 trans->delayed_ref_updates = 0; 498 trans->delayed_ref_updates = 0;
492 if (updates) { 499 if (updates) {
@@ -495,8 +502,6 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
495 return err; 502 return err;
496 } 503 }
497 504
498 trans->block_rsv = rsv;
499
500 return should_end_transaction(trans, root); 505 return should_end_transaction(trans, root);
501} 506}
502 507
@@ -513,8 +518,24 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
513 return 0; 518 return 0;
514 } 519 }
515 520
521 /*
522 * do the qgroup accounting as early as possible
523 */
524 err = btrfs_delayed_refs_qgroup_accounting(trans, info);
525
516 btrfs_trans_release_metadata(trans, root); 526 btrfs_trans_release_metadata(trans, root);
517 trans->block_rsv = NULL; 527 trans->block_rsv = NULL;
528 /*
529 * the same root has to be passed to start_transaction and
530 * end_transaction. Subvolume quota depends on this.
531 */
532 WARN_ON(trans->root != root);
533
534 if (trans->qgroup_reserved) {
535 btrfs_qgroup_free(root, trans->qgroup_reserved);
536 trans->qgroup_reserved = 0;
537 }
538
518 while (count < 2) { 539 while (count < 2) {
519 unsigned long cur = trans->delayed_ref_updates; 540 unsigned long cur = trans->delayed_ref_updates;
520 trans->delayed_ref_updates = 0; 541 trans->delayed_ref_updates = 0;
@@ -527,6 +548,10 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
527 } 548 }
528 count++; 549 count++;
529 } 550 }
551 btrfs_trans_release_metadata(trans, root);
552 trans->block_rsv = NULL;
553
554 sb_end_intwrite(root->fs_info->sb);
530 555
531 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 556 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
532 should_end_transaction(trans, root)) { 557 should_end_transaction(trans, root)) {
@@ -567,6 +592,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
567 root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 592 root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
568 err = -EIO; 593 err = -EIO;
569 } 594 }
595 assert_qgroups_uptodate(trans);
570 596
571 memset(trans, 0, sizeof(*trans)); 597 memset(trans, 0, sizeof(*trans));
572 kmem_cache_free(btrfs_trans_handle_cachep, trans); 598 kmem_cache_free(btrfs_trans_handle_cachep, trans);
@@ -785,6 +811,13 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
785 ret = btrfs_run_dev_stats(trans, root->fs_info); 811 ret = btrfs_run_dev_stats(trans, root->fs_info);
786 BUG_ON(ret); 812 BUG_ON(ret);
787 813
814 ret = btrfs_run_qgroups(trans, root->fs_info);
815 BUG_ON(ret);
816
817 /* run_qgroups might have added some more refs */
818 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
819 BUG_ON(ret);
820
788 while (!list_empty(&fs_info->dirty_cowonly_roots)) { 821 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
789 next = fs_info->dirty_cowonly_roots.next; 822 next = fs_info->dirty_cowonly_roots.next;
790 list_del_init(next); 823 list_del_init(next);
@@ -926,11 +959,13 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
926 struct dentry *dentry; 959 struct dentry *dentry;
927 struct extent_buffer *tmp; 960 struct extent_buffer *tmp;
928 struct extent_buffer *old; 961 struct extent_buffer *old;
962 struct timespec cur_time = CURRENT_TIME;
929 int ret; 963 int ret;
930 u64 to_reserve = 0; 964 u64 to_reserve = 0;
931 u64 index = 0; 965 u64 index = 0;
932 u64 objectid; 966 u64 objectid;
933 u64 root_flags; 967 u64 root_flags;
968 uuid_le new_uuid;
934 969
935 rsv = trans->block_rsv; 970 rsv = trans->block_rsv;
936 971
@@ -957,6 +992,14 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
957 } 992 }
958 } 993 }
959 994
995 ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
996 objectid, pending->inherit);
997 kfree(pending->inherit);
998 if (ret) {
999 pending->error = ret;
1000 goto fail;
1001 }
1002
960 key.objectid = objectid; 1003 key.objectid = objectid;
961 key.offset = (u64)-1; 1004 key.offset = (u64)-1;
962 key.type = BTRFS_ROOT_ITEM_KEY; 1005 key.type = BTRFS_ROOT_ITEM_KEY;
@@ -1016,6 +1059,20 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1016 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1059 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1017 btrfs_set_root_flags(new_root_item, root_flags); 1060 btrfs_set_root_flags(new_root_item, root_flags);
1018 1061
1062 btrfs_set_root_generation_v2(new_root_item,
1063 trans->transid);
1064 uuid_le_gen(&new_uuid);
1065 memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1066 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1067 BTRFS_UUID_SIZE);
1068 new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
1069 new_root_item->otime.nsec = cpu_to_le64(cur_time.tv_nsec);
1070 btrfs_set_root_otransid(new_root_item, trans->transid);
1071 memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1072 memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1073 btrfs_set_root_stransid(new_root_item, 0);
1074 btrfs_set_root_rtransid(new_root_item, 0);
1075
1019 old = btrfs_lock_root_node(root); 1076 old = btrfs_lock_root_node(root);
1020 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 1077 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
1021 if (ret) { 1078 if (ret) {
@@ -1269,9 +1326,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1269 1326
1270 btrfs_run_ordered_operations(root, 0); 1327 btrfs_run_ordered_operations(root, 0);
1271 1328
1272 btrfs_trans_release_metadata(trans, root);
1273 trans->block_rsv = NULL;
1274
1275 if (cur_trans->aborted) 1329 if (cur_trans->aborted)
1276 goto cleanup_transaction; 1330 goto cleanup_transaction;
1277 1331
@@ -1282,6 +1336,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1282 if (ret) 1336 if (ret)
1283 goto cleanup_transaction; 1337 goto cleanup_transaction;
1284 1338
1339 btrfs_trans_release_metadata(trans, root);
1340 trans->block_rsv = NULL;
1341
1285 cur_trans = trans->transaction; 1342 cur_trans = trans->transaction;
1286 1343
1287 /* 1344 /*
@@ -1330,7 +1387,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1330 spin_unlock(&root->fs_info->trans_lock); 1387 spin_unlock(&root->fs_info->trans_lock);
1331 } 1388 }
1332 1389
1333 if (now < cur_trans->start_time || now - cur_trans->start_time < 1) 1390 if (!btrfs_test_opt(root, SSD) &&
1391 (now < cur_trans->start_time || now - cur_trans->start_time < 1))
1334 should_grow = 1; 1392 should_grow = 1;
1335 1393
1336 do { 1394 do {
@@ -1352,6 +1410,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1352 goto cleanup_transaction; 1410 goto cleanup_transaction;
1353 1411
1354 /* 1412 /*
1413 * running the delayed items may have added new refs. account
1414 * them now so that they hinder processing of more delayed refs
1415 * as little as possible.
1416 */
1417 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1418
1419 /*
1355 * rename don't use btrfs_join_transaction, so, once we 1420 * rename don't use btrfs_join_transaction, so, once we
1356 * set the transaction to blocked above, we aren't going 1421 * set the transaction to blocked above, we aren't going
1357 * to get any new ordered operations. We can safely run 1422 * to get any new ordered operations. We can safely run
@@ -1463,6 +1528,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1463 root->fs_info->chunk_root->node); 1528 root->fs_info->chunk_root->node);
1464 switch_commit_root(root->fs_info->chunk_root); 1529 switch_commit_root(root->fs_info->chunk_root);
1465 1530
1531 assert_qgroups_uptodate(trans);
1466 update_super_roots(root); 1532 update_super_roots(root);
1467 1533
1468 if (!root->fs_info->log_root_recovering) { 1534 if (!root->fs_info->log_root_recovering) {
@@ -1517,6 +1583,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1517 put_transaction(cur_trans); 1583 put_transaction(cur_trans);
1518 put_transaction(cur_trans); 1584 put_transaction(cur_trans);
1519 1585
1586 sb_end_intwrite(root->fs_info->sb);
1587
1520 trace_btrfs_transaction_commit(root); 1588 trace_btrfs_transaction_commit(root);
1521 1589
1522 btrfs_scrub_continue(root); 1590 btrfs_scrub_continue(root);
@@ -1532,6 +1600,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1532 return ret; 1600 return ret;
1533 1601
1534cleanup_transaction: 1602cleanup_transaction:
1603 btrfs_trans_release_metadata(trans, root);
1604 trans->block_rsv = NULL;
1535 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n"); 1605 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1536// WARN_ON(1); 1606// WARN_ON(1);
1537 if (current->journal_info == trans) 1607 if (current->journal_info == trans)