16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2679154b1bSChris Mason #include "ctree.h" 2779154b1bSChris Mason #include "disk-io.h" 2879154b1bSChris Mason #include "transaction.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31581bb050SLi Zefan #include "inode-map.h" 32733f4fbbSStefan Behrens #include "volumes.h" 338dabb742SStefan Behrens #include "dev-replace.h" 34fcebe456SJosef Bacik #include "qgroup.h" 3579154b1bSChris Mason 360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 370f7d52f4SChris Mason 38e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 394a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 404a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 414a9d8bdeSMiao Xie __TRANS_START), 424a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 434a9d8bdeSMiao Xie __TRANS_START | 444a9d8bdeSMiao Xie __TRANS_ATTACH), 454a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 464a9d8bdeSMiao Xie __TRANS_START | 474a9d8bdeSMiao Xie __TRANS_ATTACH | 484a9d8bdeSMiao Xie __TRANS_JOIN), 494a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 504a9d8bdeSMiao Xie __TRANS_START | 514a9d8bdeSMiao Xie __TRANS_ATTACH | 524a9d8bdeSMiao Xie __TRANS_JOIN | 534a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 544a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 554a9d8bdeSMiao Xie __TRANS_START | 564a9d8bdeSMiao Xie __TRANS_ATTACH | 574a9d8bdeSMiao Xie __TRANS_JOIN | 584a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 594a9d8bdeSMiao Xie }; 604a9d8bdeSMiao Xie 61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 6279154b1bSChris Mason { 6313c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6413c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 65a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 66c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 671262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 681262133bSJosef Bacik printk(KERN_ERR "pending csums is %llu\n", 691262133bSJosef Bacik transaction->delayed_refs.pending_csums); 706df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 716df9a95eSJosef Bacik struct extent_map *em; 726df9a95eSJosef Bacik 736df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 746df9a95eSJosef Bacik struct extent_map, list); 756df9a95eSJosef Bacik list_del_init(&em->list); 766df9a95eSJosef Bacik free_extent_map(em); 776df9a95eSJosef Bacik } 782c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7979154b1bSChris Mason } 8078fae27eSChris Mason } 8179154b1bSChris Mason 82663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 83663dfbb0SFilipe Manana { 84663dfbb0SFilipe Manana spin_lock(&tree->lock); 85b666a9cdSDavid Sterba /* 86b666a9cdSDavid Sterba * Do a single barrier for the waitqueue_active check here, the state 87b666a9cdSDavid Sterba * of the waitqueue should not change once clear_btree_io_tree is 88b666a9cdSDavid Sterba * called. 89b666a9cdSDavid Sterba */ 90b666a9cdSDavid Sterba smp_mb(); 91663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 92663dfbb0SFilipe Manana struct rb_node *node; 93663dfbb0SFilipe Manana struct extent_state *state; 94663dfbb0SFilipe Manana 95663dfbb0SFilipe Manana node = rb_first(&tree->state); 96663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 97663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 98663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 99663dfbb0SFilipe Manana /* 100663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 101663dfbb0SFilipe Manana * changes in the flags of extent states ever. 102663dfbb0SFilipe Manana */ 103663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 104663dfbb0SFilipe Manana free_extent_state(state); 105351810c1SDavid Sterba 106351810c1SDavid Sterba cond_resched_lock(&tree->lock); 107663dfbb0SFilipe Manana } 108663dfbb0SFilipe Manana spin_unlock(&tree->lock); 109663dfbb0SFilipe Manana } 110663dfbb0SFilipe Manana 1119e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 1129e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 113817d52f8SJosef Bacik { 1149e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1159e351cc8SJosef Bacik 1169e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1179e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1189e351cc8SJosef Bacik dirty_list) { 1199e351cc8SJosef Bacik list_del_init(&root->dirty_list); 120817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 121817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1229e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1239e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 124663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1259e351cc8SJosef Bacik } 1262b9dbef2SJosef Bacik 1272b9dbef2SJosef Bacik /* We can free old roots now. */ 1282b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1292b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1302b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1312b9dbef2SJosef Bacik struct btrfs_root, root_list); 1322b9dbef2SJosef Bacik list_del_init(&root->root_list); 1332b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1342b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1352b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1362b9dbef2SJosef Bacik } 1372b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1389e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 139817d52f8SJosef Bacik } 140817d52f8SJosef Bacik 1410860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1420860adfdSMiao Xie unsigned int type) 1430860adfdSMiao Xie { 1440860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1450860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1460860adfdSMiao Xie } 1470860adfdSMiao Xie 1480860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1490860adfdSMiao Xie unsigned int type) 1500860adfdSMiao Xie { 1510860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1520860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1530860adfdSMiao Xie } 1540860adfdSMiao Xie 1550860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1560860adfdSMiao Xie unsigned int type) 1570860adfdSMiao Xie { 1580860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1590860adfdSMiao Xie } 1600860adfdSMiao Xie 1610860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1620860adfdSMiao Xie { 1630860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 164178260b2SMiao Xie } 165178260b2SMiao Xie 166d352ac68SChris Mason /* 167d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 168d352ac68SChris Mason */ 1690860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 17079154b1bSChris Mason { 17179154b1bSChris Mason struct btrfs_transaction *cur_trans; 17219ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 173a4abeea4SJosef Bacik 17419ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 175d43317dcSChris Mason loop: 17649b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 17787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 17819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 17949b25e05SJeff Mahoney return -EROFS; 18049b25e05SJeff Mahoney } 18149b25e05SJeff Mahoney 18219ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 183a4abeea4SJosef Bacik if (cur_trans) { 184871383beSDavid Sterba if (cur_trans->aborted) { 18519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 18649b25e05SJeff Mahoney return cur_trans->aborted; 187871383beSDavid Sterba } 1884a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 189178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 190178260b2SMiao Xie return -EBUSY; 191178260b2SMiao Xie } 192a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 193a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1940860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 19519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 196a4abeea4SJosef Bacik return 0; 197a4abeea4SJosef Bacik } 19819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 199a4abeea4SJosef Bacik 200354aa0fbSMiao Xie /* 201354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 202354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 203354aa0fbSMiao Xie */ 204354aa0fbSMiao Xie if (type == TRANS_ATTACH) 205354aa0fbSMiao Xie return -ENOENT; 206354aa0fbSMiao Xie 2074a9d8bdeSMiao Xie /* 2084a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 2094a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 2104a9d8bdeSMiao Xie */ 2114a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2124a9d8bdeSMiao Xie 213a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 214db5b493aSTsutomu Itoh if (!cur_trans) 215db5b493aSTsutomu Itoh return -ENOMEM; 216d43317dcSChris Mason 21719ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 21819ae4e81SJan Schmidt if (fs_info->running_transaction) { 219d43317dcSChris Mason /* 220d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2214a9d8bdeSMiao Xie * to redo the checks above 222d43317dcSChris Mason */ 223a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 224d43317dcSChris Mason goto loop; 22587533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 226e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2277b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 2287b8b92afSJosef Bacik return -EROFS; 229a4abeea4SJosef Bacik } 230d43317dcSChris Mason 23113c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2320860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 23379154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 23479154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 235161c3549SJosef Bacik init_waitqueue_head(&cur_trans->pending_wait); 2364a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 237a4abeea4SJosef Bacik /* 238a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 239a4abeea4SJosef Bacik * commit the transaction. 240a4abeea4SJosef Bacik */ 241a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 242161c3549SJosef Bacik atomic_set(&cur_trans->pending_ordered, 0); 243*3204d33cSJosef Bacik cur_trans->flags = 0; 24408607c1bSChris Mason cur_trans->start_time = get_seconds(); 24556bec294SChris Mason 246a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 247a099d0fdSAlexandru Moise 248c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2493368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 250d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 25120b297d6SJan Schmidt 25220b297d6SJan Schmidt /* 25320b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 25420b297d6SJan Schmidt * the log must never go across transaction boundaries. 25520b297d6SJan Schmidt */ 25620b297d6SJan Schmidt smp_mb(); 25731b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 258efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 25920b297d6SJan Schmidt "creating a fresh transaction\n"); 26031b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 261efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 26220b297d6SJan Schmidt "creating a fresh transaction\n"); 263fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 26420b297d6SJan Schmidt 26556bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 26656bec294SChris Mason 2673063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2686df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2699e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 270ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2711bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2722b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2731bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 274cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 275ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 276e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 277e33e17eeSJeff Mahoney spin_lock_init(&cur_trans->deleted_bgs_lock); 2782b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 27919ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 280d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 28119ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 28219ae4e81SJan Schmidt fs_info->generation++; 28319ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 28419ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 28549b25e05SJeff Mahoney cur_trans->aborted = 0; 28619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 28715ee9bc7SJosef Bacik 28879154b1bSChris Mason return 0; 28979154b1bSChris Mason } 29079154b1bSChris Mason 291d352ac68SChris Mason /* 292d397712bSChris Mason * this does all the record keeping required to make sure that a reference 293d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 294d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 295d397712bSChris Mason * when the transaction commits 296d352ac68SChris Mason */ 2977585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2985d4f98a2SYan Zheng struct btrfs_root *root) 2996702ed49SChris Mason { 30027cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 30127cdeb70SMiao Xie root->last_trans < trans->transid) { 3026702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 3035d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 3045d4f98a2SYan Zheng 3057585717fSChris Mason /* 30627cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 3077585717fSChris Mason * we have the reloc mutex held now, so there 3087585717fSChris Mason * is only one writer in this function 3097585717fSChris Mason */ 31027cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3117585717fSChris Mason 31227cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 3137585717fSChris Mason * they find our root->last_trans update 3147585717fSChris Mason */ 3157585717fSChris Mason smp_wmb(); 3167585717fSChris Mason 317a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 318a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 319a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 320a4abeea4SJosef Bacik return 0; 321a4abeea4SJosef Bacik } 3226702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 3236702ed49SChris Mason (unsigned long)root->root_key.objectid, 3246702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 325a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3267585717fSChris Mason root->last_trans = trans->transid; 3277585717fSChris Mason 3287585717fSChris Mason /* this is pretty tricky. We don't want to 3297585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3307585717fSChris Mason * unless we're really doing the first setup for this root in 3317585717fSChris Mason * this transaction. 3327585717fSChris Mason * 3337585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3347585717fSChris Mason * if we want to take the expensive mutex. 3357585717fSChris Mason * 3367585717fSChris Mason * But, we have to set root->last_trans before we 3377585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3387585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 33927cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3407585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3417585717fSChris Mason * 3427585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3437585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3447585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3457585717fSChris Mason * done before we pop in the zero below 3467585717fSChris Mason */ 3475d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 348c7548af6SChris Mason smp_mb__before_atomic(); 34927cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3506702ed49SChris Mason } 3515d4f98a2SYan Zheng return 0; 3526702ed49SChris Mason } 3535d4f98a2SYan Zheng 3547585717fSChris Mason 3552b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3562b9dbef2SJosef Bacik struct btrfs_root *root) 3572b9dbef2SJosef Bacik { 3582b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3592b9dbef2SJosef Bacik 3602b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3612b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3622b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3632b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3642b9dbef2SJosef Bacik 3652b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3662b9dbef2SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 3672b9dbef2SJosef Bacik radix_tree_tag_clear(&root->fs_info->fs_roots_radix, 3682b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3692b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3702b9dbef2SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3712b9dbef2SJosef Bacik } 3722b9dbef2SJosef Bacik 3737585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3747585717fSChris Mason struct btrfs_root *root) 3757585717fSChris Mason { 37627cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3777585717fSChris Mason return 0; 3787585717fSChris Mason 3797585717fSChris Mason /* 38027cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3817585717fSChris Mason * and barriers 3827585717fSChris Mason */ 3837585717fSChris Mason smp_rmb(); 3847585717fSChris Mason if (root->last_trans == trans->transid && 38527cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3867585717fSChris Mason return 0; 3877585717fSChris Mason 3887585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3897585717fSChris Mason record_root_in_trans(trans, root); 3907585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3917585717fSChris Mason 3927585717fSChris Mason return 0; 3937585717fSChris Mason } 3947585717fSChris Mason 3954a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3964a9d8bdeSMiao Xie { 3974a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 398501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 399501407aaSJosef Bacik !trans->aborted); 4004a9d8bdeSMiao Xie } 4014a9d8bdeSMiao Xie 402d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 403d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 404d352ac68SChris Mason * transaction might not be fully on disk. 405d352ac68SChris Mason */ 40637d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 40779154b1bSChris Mason { 408f9295749SChris Mason struct btrfs_transaction *cur_trans; 40979154b1bSChris Mason 410a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 411f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 4124a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 41313c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 414a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 41572d63ed6SLi Zefan 41672d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 417501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 418501407aaSJosef Bacik cur_trans->aborted); 419724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 420a4abeea4SJosef Bacik } else { 421a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 422f9295749SChris Mason } 42337d1aeeeSChris Mason } 42437d1aeeeSChris Mason 425a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 42637d1aeeeSChris Mason { 427a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 428a4abeea4SJosef Bacik return 0; 429a4abeea4SJosef Bacik 430a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 431a22285a6SYan, Zheng return 1; 432a4abeea4SJosef Bacik 433a4abeea4SJosef Bacik if (type == TRANS_START && 434a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 435a4abeea4SJosef Bacik return 1; 436a4abeea4SJosef Bacik 437a22285a6SYan, Zheng return 0; 438a22285a6SYan, Zheng } 439a22285a6SYan, Zheng 44020dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 44120dd2cbfSMiao Xie { 44220dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 44327cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 44420dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 44520dd2cbfSMiao Xie root->reloc_root) 44620dd2cbfSMiao Xie return false; 44720dd2cbfSMiao Xie 44820dd2cbfSMiao Xie return true; 44920dd2cbfSMiao Xie } 45020dd2cbfSMiao Xie 45108e007d2SMiao Xie static struct btrfs_trans_handle * 4525aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 4535aed1dd8SAlexandru Moise unsigned int type, enum btrfs_reserve_flush_enum flush) 454a22285a6SYan, Zheng { 455a22285a6SYan, Zheng struct btrfs_trans_handle *h; 456a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 457b5009945SJosef Bacik u64 num_bytes = 0; 458c5567237SArne Jansen u64 qgroup_reserved = 0; 45920dd2cbfSMiao Xie bool reloc_reserved = false; 46020dd2cbfSMiao Xie int ret; 461acce952bSliubo 46246c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4632755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 46446c4e71eSFilipe Manana 46587533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 466acce952bSliubo return ERR_PTR(-EROFS); 4672a1eb461SJosef Bacik 46846c4e71eSFilipe Manana if (current->journal_info) { 4690860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4702a1eb461SJosef Bacik h = current->journal_info; 4712a1eb461SJosef Bacik h->use_count++; 472b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 4732a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4742a1eb461SJosef Bacik h->block_rsv = NULL; 4752a1eb461SJosef Bacik goto got_it; 4762a1eb461SJosef Bacik } 477b5009945SJosef Bacik 478b5009945SJosef Bacik /* 479b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 480b5009945SJosef Bacik * the appropriate flushing if need be. 481b5009945SJosef Bacik */ 482b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 483c5567237SArne Jansen if (root->fs_info->quota_enabled && 484c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 485707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 486c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 487c5567237SArne Jansen if (ret) 488c5567237SArne Jansen return ERR_PTR(ret); 489c5567237SArne Jansen } 490c5567237SArne Jansen 491b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 49220dd2cbfSMiao Xie /* 49320dd2cbfSMiao Xie * Do the reservation for the relocation root creation 49420dd2cbfSMiao Xie */ 495ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 49620dd2cbfSMiao Xie num_bytes += root->nodesize; 49720dd2cbfSMiao Xie reloc_reserved = true; 49820dd2cbfSMiao Xie } 49920dd2cbfSMiao Xie 5004a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 501b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 50208e007d2SMiao Xie num_bytes, flush); 503b5009945SJosef Bacik if (ret) 504843fcf35SMiao Xie goto reserve_fail; 505b5009945SJosef Bacik } 506a22285a6SYan, Zheng again: 507f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 508843fcf35SMiao Xie if (!h) { 509843fcf35SMiao Xie ret = -ENOMEM; 510843fcf35SMiao Xie goto alloc_fail; 511843fcf35SMiao Xie } 512a22285a6SYan, Zheng 51398114659SJosef Bacik /* 51498114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 51598114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 51698114659SJosef Bacik * because we're already holding a ref. We need this because we could 51798114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 51898114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 519354aa0fbSMiao Xie * 520354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 521354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 52298114659SJosef Bacik */ 5230860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 524b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 525b2b5ef5cSJan Kara 526a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 52737d1aeeeSChris Mason wait_current_trans(root); 528a22285a6SYan, Zheng 529a4abeea4SJosef Bacik do { 530354aa0fbSMiao Xie ret = join_transaction(root, type); 531178260b2SMiao Xie if (ret == -EBUSY) { 532a4abeea4SJosef Bacik wait_current_trans(root); 533178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 534178260b2SMiao Xie ret = -ENOENT; 535178260b2SMiao Xie } 536a4abeea4SJosef Bacik } while (ret == -EBUSY); 537a4abeea4SJosef Bacik 538db5b493aSTsutomu Itoh if (ret < 0) { 539354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 540354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 541843fcf35SMiao Xie goto join_fail; 542db5b493aSTsutomu Itoh } 5430f7d52f4SChris Mason 544a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 545a22285a6SYan, Zheng 546a22285a6SYan, Zheng h->transid = cur_trans->transid; 547a22285a6SYan, Zheng h->transaction = cur_trans; 548d13603efSArne Jansen h->root = root; 5492a1eb461SJosef Bacik h->use_count = 1; 550a698d075SMiao Xie h->type = type; 551d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 552bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 553ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 554b7ec40d7SChris Mason 555a22285a6SYan, Zheng smp_mb(); 5564a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5574a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 558abdd2e80SFilipe Manana current->journal_info = h; 559a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 560a22285a6SYan, Zheng goto again; 561a22285a6SYan, Zheng } 5629ed74f2dSJosef Bacik 563b5009945SJosef Bacik if (num_bytes) { 5648c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5652bcc0328SLiu Bo h->transid, num_bytes, 1); 566b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 567b5009945SJosef Bacik h->bytes_reserved = num_bytes; 56820dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 569a22285a6SYan, Zheng } 5704b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 571a22285a6SYan, Zheng 5722a1eb461SJosef Bacik got_it: 573a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 574a22285a6SYan, Zheng 575a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 576a22285a6SYan, Zheng current->journal_info = h; 57779154b1bSChris Mason return h; 578843fcf35SMiao Xie 579843fcf35SMiao Xie join_fail: 5800860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 581843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 582843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 583843fcf35SMiao Xie alloc_fail: 584843fcf35SMiao Xie if (num_bytes) 585843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 586843fcf35SMiao Xie num_bytes); 587843fcf35SMiao Xie reserve_fail: 588843fcf35SMiao Xie if (qgroup_reserved) 589843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 590843fcf35SMiao Xie return ERR_PTR(ret); 59179154b1bSChris Mason } 59279154b1bSChris Mason 593f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 5945aed1dd8SAlexandru Moise unsigned int num_items) 595f9295749SChris Mason { 59608e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 59708e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 598f9295749SChris Mason } 5998407aa46SMiao Xie 60008e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6015aed1dd8SAlexandru Moise struct btrfs_root *root, 6025aed1dd8SAlexandru Moise unsigned int num_items) 6038407aa46SMiao Xie { 60408e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 60508e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 6068407aa46SMiao Xie } 6078407aa46SMiao Xie 6087a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 609f9295749SChris Mason { 6108407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 611f9295749SChris Mason } 612f9295749SChris Mason 6137a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6140af3d00bSJosef Bacik { 6158407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 6160af3d00bSJosef Bacik } 6170af3d00bSJosef Bacik 6187a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6199ca9ee09SSage Weil { 6208407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 6219ca9ee09SSage Weil } 6229ca9ee09SSage Weil 623d4edf39bSMiao Xie /* 624d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 625d4edf39bSMiao Xie * 626d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 627d4edf39bSMiao Xie * don't want to start a new one. 628d4edf39bSMiao Xie * 629d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 630d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 631d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 632d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 633d4edf39bSMiao Xie * invoke 634d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 635d4edf39bSMiao Xie */ 636354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 63760376ce4SJosef Bacik { 638354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 63960376ce4SJosef Bacik } 64060376ce4SJosef Bacik 641d4edf39bSMiao Xie /* 64290b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 643d4edf39bSMiao Xie * 644d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 645d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 646d4edf39bSMiao Xie * complete. 647d4edf39bSMiao Xie */ 648d4edf39bSMiao Xie struct btrfs_trans_handle * 649d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 650d4edf39bSMiao Xie { 651d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 652d4edf39bSMiao Xie 653d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 654d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 655d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 656d4edf39bSMiao Xie 657d4edf39bSMiao Xie return trans; 658d4edf39bSMiao Xie } 659d4edf39bSMiao Xie 660d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 661b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 66289ce8a63SChris Mason struct btrfs_transaction *commit) 66389ce8a63SChris Mason { 6644a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 66589ce8a63SChris Mason } 66689ce8a63SChris Mason 66746204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 66846204592SSage Weil { 66946204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6708cd2807fSMiao Xie int ret = 0; 67146204592SSage Weil 67246204592SSage Weil if (transid) { 67346204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 674a4abeea4SJosef Bacik goto out; 67546204592SSage Weil 67646204592SSage Weil /* find specified transaction */ 677a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 67846204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 67946204592SSage Weil if (t->transid == transid) { 68046204592SSage Weil cur_trans = t; 681a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 6828cd2807fSMiao Xie ret = 0; 68346204592SSage Weil break; 68446204592SSage Weil } 6858cd2807fSMiao Xie if (t->transid > transid) { 6868cd2807fSMiao Xie ret = 0; 68746204592SSage Weil break; 68846204592SSage Weil } 6898cd2807fSMiao Xie } 690a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 69142383020SSage Weil 69242383020SSage Weil /* 69342383020SSage Weil * The specified transaction doesn't exist, or we 69442383020SSage Weil * raced with btrfs_commit_transaction 69542383020SSage Weil */ 69642383020SSage Weil if (!cur_trans) { 69742383020SSage Weil if (transid > root->fs_info->last_trans_committed) 69842383020SSage Weil ret = -EINVAL; 6998cd2807fSMiao Xie goto out; 70042383020SSage Weil } 70146204592SSage Weil } else { 70246204592SSage Weil /* find newest transaction that is committing | committed */ 703a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 70446204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 70546204592SSage Weil list) { 7064a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7074a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7083473f3c0SJosef Bacik break; 70946204592SSage Weil cur_trans = t; 710a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 71146204592SSage Weil break; 71246204592SSage Weil } 71346204592SSage Weil } 714a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 71546204592SSage Weil if (!cur_trans) 716a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 71746204592SSage Weil } 71846204592SSage Weil 71946204592SSage Weil wait_for_commit(root, cur_trans); 720724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 721a4abeea4SJosef Bacik out: 72246204592SSage Weil return ret; 72346204592SSage Weil } 72446204592SSage Weil 72537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 72637d1aeeeSChris Mason { 727a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 72837d1aeeeSChris Mason wait_current_trans(root); 72937d1aeeeSChris Mason } 73037d1aeeeSChris Mason 7318929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7328929ecfaSYan, Zheng struct btrfs_root *root) 7338929ecfaSYan, Zheng { 7341be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7350a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7361be41b78SJosef Bacik return 1; 73736ba022aSJosef Bacik 7381be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7398929ecfaSYan, Zheng } 7408929ecfaSYan, Zheng 7418929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7428929ecfaSYan, Zheng struct btrfs_root *root) 7438929ecfaSYan, Zheng { 7448929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7458929ecfaSYan, Zheng int updates; 74649b25e05SJeff Mahoney int err; 7478929ecfaSYan, Zheng 748a4abeea4SJosef Bacik smp_mb(); 7494a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7504a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7518929ecfaSYan, Zheng return 1; 7528929ecfaSYan, Zheng 7538929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 7548929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 75549b25e05SJeff Mahoney if (updates) { 75628ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 75749b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 75849b25e05SJeff Mahoney return err; 75949b25e05SJeff Mahoney } 7608929ecfaSYan, Zheng 7618929ecfaSYan, Zheng return should_end_transaction(trans, root); 7628929ecfaSYan, Zheng } 7638929ecfaSYan, Zheng 76489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 765a698d075SMiao Xie struct btrfs_root *root, int throttle) 76679154b1bSChris Mason { 7678929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 768ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 7691be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 770a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 7714edc2ca3SDave Jones int err = 0; 772a79b7d4bSChris Mason int must_run_delayed_refs = 0; 773d6e4a428SChris Mason 7743bbb24b2SJosef Bacik if (trans->use_count > 1) { 7753bbb24b2SJosef Bacik trans->use_count--; 7762a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 7772a1eb461SJosef Bacik return 0; 7782a1eb461SJosef Bacik } 7792a1eb461SJosef Bacik 780b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 7814c13d758SJosef Bacik trans->block_rsv = NULL; 782c5567237SArne Jansen 783ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 784ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 785ea658badSJosef Bacik 786c3e69d58SChris Mason trans->delayed_ref_updates = 0; 787a79b7d4bSChris Mason if (!trans->sync) { 788a79b7d4bSChris Mason must_run_delayed_refs = 789a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 7900a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 791a79b7d4bSChris Mason 792a79b7d4bSChris Mason /* 793a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 794a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 795a79b7d4bSChris Mason */ 796a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 797a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 798a79b7d4bSChris Mason must_run_delayed_refs = 2; 79956bec294SChris Mason } 800bb721703SChris Mason 801fcebe456SJosef Bacik if (trans->qgroup_reserved) { 802fcebe456SJosef Bacik /* 803fcebe456SJosef Bacik * the same root has to be passed here between start_transaction 804fcebe456SJosef Bacik * and end_transaction. Subvolume quota depends on this. 805fcebe456SJosef Bacik */ 806fcebe456SJosef Bacik btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 807fcebe456SJosef Bacik trans->qgroup_reserved = 0; 808fcebe456SJosef Bacik } 809fcebe456SJosef Bacik 8100e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8110e721106SJosef Bacik trans->block_rsv = NULL; 81256bec294SChris Mason 813ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 814ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 815ea658badSJosef Bacik 8164fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8174fbcdf66SFilipe Manana 818a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8194a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8204a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8214a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8224a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8234a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8244a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 825a4abeea4SJosef Bacik } 8268929ecfaSYan, Zheng 8274a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8283bbb24b2SJosef Bacik if (throttle) 8298929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8303bbb24b2SJosef Bacik else 8318929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8328929ecfaSYan, Zheng } 8338929ecfaSYan, Zheng 8340860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8356df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8366df7881aSJosef Bacik 8378929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 83813c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 83913c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8400860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 84189ce8a63SChris Mason 842a83342aaSDavid Sterba /* 843a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 844a83342aaSDavid Sterba */ 84599d16cbcSSage Weil smp_mb(); 84679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 84779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 848724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8499ed74f2dSJosef Bacik 8509ed74f2dSJosef Bacik if (current->journal_info == trans) 8519ed74f2dSJosef Bacik current->journal_info = NULL; 852ab78c84dSChris Mason 85324bbcf04SYan, Zheng if (throttle) 85424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 85524bbcf04SYan, Zheng 85649b25e05SJeff Mahoney if (trans->aborted || 8574e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 8584e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8594edc2ca3SDave Jones err = -EIO; 8604e121c06SJosef Bacik } 861edf39272SJan Schmidt assert_qgroups_uptodate(trans); 86249b25e05SJeff Mahoney 8634edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 864a79b7d4bSChris Mason if (must_run_delayed_refs) { 865a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 866a79b7d4bSChris Mason must_run_delayed_refs == 1); 867a79b7d4bSChris Mason } 8684edc2ca3SDave Jones return err; 86979154b1bSChris Mason } 87079154b1bSChris Mason 87189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 87289ce8a63SChris Mason struct btrfs_root *root) 87389ce8a63SChris Mason { 87498ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 87589ce8a63SChris Mason } 87689ce8a63SChris Mason 87789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 87889ce8a63SChris Mason struct btrfs_root *root) 87989ce8a63SChris Mason { 88098ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 88116cdcec7SMiao Xie } 88216cdcec7SMiao Xie 883d352ac68SChris Mason /* 884d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 885d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 886690587d1SChris Mason * those extents are sent to disk but does not wait on them 887d352ac68SChris Mason */ 888690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 8898cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 89079154b1bSChris Mason { 891777e6bd7SChris Mason int err = 0; 8927c4452b9SChris Mason int werr = 0; 8931728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 894e6138876SJosef Bacik struct extent_state *cached_state = NULL; 895777e6bd7SChris Mason u64 start = 0; 8965f39d397SChris Mason u64 end; 8977c4452b9SChris Mason 8981728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 899e6138876SJosef Bacik mark, &cached_state)) { 900663dfbb0SFilipe Manana bool wait_writeback = false; 901663dfbb0SFilipe Manana 902663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 903663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 904e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 905663dfbb0SFilipe Manana /* 906663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 907663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 908663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 909663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 910663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 911663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 912663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 913663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 914663dfbb0SFilipe Manana * (and without errors). 915663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 916663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 917663dfbb0SFilipe Manana */ 918663dfbb0SFilipe Manana if (err == -ENOMEM) { 919663dfbb0SFilipe Manana err = 0; 920663dfbb0SFilipe Manana wait_writeback = true; 921663dfbb0SFilipe Manana } 922663dfbb0SFilipe Manana if (!err) 9231728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9247c4452b9SChris Mason if (err) 9257c4452b9SChris Mason werr = err; 926663dfbb0SFilipe Manana else if (wait_writeback) 927663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 928e38e2ed7SFilipe Manana free_extent_state(cached_state); 929663dfbb0SFilipe Manana cached_state = NULL; 9301728366eSJosef Bacik cond_resched(); 9311728366eSJosef Bacik start = end + 1; 9327c4452b9SChris Mason } 933690587d1SChris Mason return werr; 934690587d1SChris Mason } 935690587d1SChris Mason 936690587d1SChris Mason /* 937690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 938690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 939690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 940690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 941690587d1SChris Mason */ 942690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9438cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 944690587d1SChris Mason { 945690587d1SChris Mason int err = 0; 946690587d1SChris Mason int werr = 0; 9471728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 948e6138876SJosef Bacik struct extent_state *cached_state = NULL; 949690587d1SChris Mason u64 start = 0; 950690587d1SChris Mason u64 end; 951656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 952656f30dbSFilipe Manana bool errors = false; 953690587d1SChris Mason 9541728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 955e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 956663dfbb0SFilipe Manana /* 957663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 958663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 959663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 960663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 961663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 962663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 963663dfbb0SFilipe Manana */ 964663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 965663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 966e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 967663dfbb0SFilipe Manana if (err == -ENOMEM) 968663dfbb0SFilipe Manana err = 0; 969663dfbb0SFilipe Manana if (!err) 9701728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 971777e6bd7SChris Mason if (err) 972777e6bd7SChris Mason werr = err; 973e38e2ed7SFilipe Manana free_extent_state(cached_state); 974e38e2ed7SFilipe Manana cached_state = NULL; 975777e6bd7SChris Mason cond_resched(); 9761728366eSJosef Bacik start = end + 1; 977777e6bd7SChris Mason } 9787c4452b9SChris Mason if (err) 9797c4452b9SChris Mason werr = err; 980656f30dbSFilipe Manana 981656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 982656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 983656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 984656f30dbSFilipe Manana &btree_ino->runtime_flags)) 985656f30dbSFilipe Manana errors = true; 986656f30dbSFilipe Manana 987656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 988656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 989656f30dbSFilipe Manana &btree_ino->runtime_flags)) 990656f30dbSFilipe Manana errors = true; 991656f30dbSFilipe Manana } else { 992656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 993656f30dbSFilipe Manana &btree_ino->runtime_flags)) 994656f30dbSFilipe Manana errors = true; 995656f30dbSFilipe Manana } 996656f30dbSFilipe Manana 997656f30dbSFilipe Manana if (errors && !werr) 998656f30dbSFilipe Manana werr = -EIO; 999656f30dbSFilipe Manana 10007c4452b9SChris Mason return werr; 100179154b1bSChris Mason } 100279154b1bSChris Mason 1003690587d1SChris Mason /* 1004690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1005690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1006690587d1SChris Mason * those extents are on disk for transaction or log commit 1007690587d1SChris Mason */ 1008171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10098cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1010690587d1SChris Mason { 1011690587d1SChris Mason int ret; 1012690587d1SChris Mason int ret2; 1013c6adc9ccSMiao Xie struct blk_plug plug; 1014690587d1SChris Mason 1015c6adc9ccSMiao Xie blk_start_plug(&plug); 10168cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1017c6adc9ccSMiao Xie blk_finish_plug(&plug); 10188cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1019bf0da8c1SChris Mason 1020bf0da8c1SChris Mason if (ret) 1021bf0da8c1SChris Mason return ret; 1022bf0da8c1SChris Mason if (ret2) 1023bf0da8c1SChris Mason return ret2; 1024bf0da8c1SChris Mason return 0; 1025690587d1SChris Mason } 1026690587d1SChris Mason 1027663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1028d0c803c4SChris Mason struct btrfs_root *root) 1029d0c803c4SChris Mason { 1030663dfbb0SFilipe Manana int ret; 1031663dfbb0SFilipe Manana 1032663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10338cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10348cef4e16SYan, Zheng EXTENT_DIRTY); 1035663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1036663dfbb0SFilipe Manana 1037663dfbb0SFilipe Manana return ret; 1038d0c803c4SChris Mason } 1039d0c803c4SChris Mason 1040d352ac68SChris Mason /* 1041d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1042d352ac68SChris Mason * 1043d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1044d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1045d352ac68SChris Mason * allocation tree. 1046d352ac68SChris Mason * 1047d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1048d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1049d352ac68SChris Mason */ 10500b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 105179154b1bSChris Mason struct btrfs_root *root) 105279154b1bSChris Mason { 105379154b1bSChris Mason int ret; 10540b86a832SChris Mason u64 old_root_bytenr; 105586b9f2ecSYan, Zheng u64 old_root_used; 10560b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 105779154b1bSChris Mason 105886b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 105956bec294SChris Mason 106079154b1bSChris Mason while (1) { 10610b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 106286b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1063ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 106479154b1bSChris Mason break; 106587ef2bb4SChris Mason 10665d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 106779154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10680b86a832SChris Mason &root->root_key, 10690b86a832SChris Mason &root->root_item); 107049b25e05SJeff Mahoney if (ret) 107149b25e05SJeff Mahoney return ret; 107256bec294SChris Mason 107386b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1074e7070be1SJosef Bacik } 1075276e680dSYan Zheng 10760b86a832SChris Mason return 0; 10770b86a832SChris Mason } 10780b86a832SChris Mason 1079d352ac68SChris Mason /* 1080d352ac68SChris Mason * update all the cowonly tree roots on disk 108149b25e05SJeff Mahoney * 108249b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 108349b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 108449b25e05SJeff Mahoney * to clean up the delayed refs. 1085d352ac68SChris Mason */ 10865d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 10870b86a832SChris Mason struct btrfs_root *root) 10880b86a832SChris Mason { 10890b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1090ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 10911bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 10920b86a832SChris Mason struct list_head *next; 109384234f3aSYan Zheng struct extent_buffer *eb; 109456bec294SChris Mason int ret; 109584234f3aSYan Zheng 109684234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 109749b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 109849b25e05SJeff Mahoney 0, &eb); 109984234f3aSYan Zheng btrfs_tree_unlock(eb); 110084234f3aSYan Zheng free_extent_buffer(eb); 11010b86a832SChris Mason 110249b25e05SJeff Mahoney if (ret) 110349b25e05SJeff Mahoney return ret; 110449b25e05SJeff Mahoney 110556bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 110649b25e05SJeff Mahoney if (ret) 110749b25e05SJeff Mahoney return ret; 110887ef2bb4SChris Mason 1109733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1110c16ce190SJosef Bacik if (ret) 1111c16ce190SJosef Bacik return ret; 11128dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1113c16ce190SJosef Bacik if (ret) 1114c16ce190SJosef Bacik return ret; 1115546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1116c16ce190SJosef Bacik if (ret) 1117c16ce190SJosef Bacik return ret; 1118546adb0dSJan Schmidt 1119dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1120dcdf7f6dSJosef Bacik if (ret) 1121dcdf7f6dSJosef Bacik return ret; 1122dcdf7f6dSJosef Bacik 1123546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1124546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1125c16ce190SJosef Bacik if (ret) 1126c16ce190SJosef Bacik return ret; 1127ea526d18SJosef Bacik again: 11280b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11290b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11300b86a832SChris Mason list_del_init(next); 11310b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1132e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 113387ef2bb4SChris Mason 11349e351cc8SJosef Bacik if (root != fs_info->extent_root) 11359e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11369e351cc8SJosef Bacik &trans->transaction->switch_commits); 113749b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 113849b25e05SJeff Mahoney if (ret) 113949b25e05SJeff Mahoney return ret; 1140ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1141ea526d18SJosef Bacik if (ret) 1142ea526d18SJosef Bacik return ret; 114379154b1bSChris Mason } 1144276e680dSYan Zheng 11451bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1146ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1147ea526d18SJosef Bacik if (ret) 1148ea526d18SJosef Bacik return ret; 1149ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1150ea526d18SJosef Bacik if (ret) 1151ea526d18SJosef Bacik return ret; 1152ea526d18SJosef Bacik } 1153ea526d18SJosef Bacik 1154ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1155ea526d18SJosef Bacik goto again; 1156ea526d18SJosef Bacik 11579e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11589e351cc8SJosef Bacik &trans->transaction->switch_commits); 11598dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 11608dabb742SStefan Behrens 116179154b1bSChris Mason return 0; 116279154b1bSChris Mason } 116379154b1bSChris Mason 1164d352ac68SChris Mason /* 1165d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1166d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1167d352ac68SChris Mason * be deleted 1168d352ac68SChris Mason */ 1169cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11705eda7b5eSChris Mason { 1171a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1172cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11739d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1174a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 11755eda7b5eSChris Mason } 11765eda7b5eSChris Mason 1177d352ac68SChris Mason /* 11785d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1179d352ac68SChris Mason */ 11805d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 11815d4f98a2SYan Zheng struct btrfs_root *root) 11820f7d52f4SChris Mason { 11830f7d52f4SChris Mason struct btrfs_root *gang[8]; 11845d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 11850f7d52f4SChris Mason int i; 11860f7d52f4SChris Mason int ret; 118754aa1f4dSChris Mason int err = 0; 118854aa1f4dSChris Mason 1189a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 11900f7d52f4SChris Mason while (1) { 11915d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 11925d4f98a2SYan Zheng (void **)gang, 0, 11930f7d52f4SChris Mason ARRAY_SIZE(gang), 11940f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 11950f7d52f4SChris Mason if (ret == 0) 11960f7d52f4SChris Mason break; 11970f7d52f4SChris Mason for (i = 0; i < ret; i++) { 11980f7d52f4SChris Mason root = gang[i]; 11995d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12002619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12010f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1202a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 120331153d81SYan Zheng 1204e02119d5SChris Mason btrfs_free_log(trans, root); 12055d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1206d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1207e02119d5SChris Mason 120882d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 120982d5902dSLi Zefan 1210f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 121127cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1212c7548af6SChris Mason smp_mb__after_atomic(); 1213f1ebcc74SLiu Bo 1214978d910dSYan Zheng if (root->commit_root != root->node) { 12159e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12169e351cc8SJosef Bacik &trans->transaction->switch_commits); 1217978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1218978d910dSYan Zheng root->node); 1219978d910dSYan Zheng } 122031153d81SYan Zheng 12215d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12220f7d52f4SChris Mason &root->root_key, 12230f7d52f4SChris Mason &root->root_item); 1224a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 122554aa1f4dSChris Mason if (err) 122654aa1f4dSChris Mason break; 12270f7d52f4SChris Mason } 12289f3a7427SChris Mason } 1229a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 123054aa1f4dSChris Mason return err; 12310f7d52f4SChris Mason } 12320f7d52f4SChris Mason 1233d352ac68SChris Mason /* 1234de78b51aSEric Sandeen * defrag a given btree. 1235de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1236d352ac68SChris Mason */ 1237de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1238e9d0b13bSChris Mason { 1239e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1240e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12418929ecfaSYan, Zheng int ret; 1242e9d0b13bSChris Mason 124327cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1244e9d0b13bSChris Mason return 0; 12458929ecfaSYan, Zheng 12466b80053dSChris Mason while (1) { 12478929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12488929ecfaSYan, Zheng if (IS_ERR(trans)) 12498929ecfaSYan, Zheng return PTR_ERR(trans); 12508929ecfaSYan, Zheng 1251de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12528929ecfaSYan, Zheng 1253e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1254b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1255e9d0b13bSChris Mason cond_resched(); 1256e9d0b13bSChris Mason 12577841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1258e9d0b13bSChris Mason break; 1259210549ebSDavid Sterba 1260210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1261efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1262210549ebSDavid Sterba ret = -EAGAIN; 1263210549ebSDavid Sterba break; 1264210549ebSDavid Sterba } 1265e9d0b13bSChris Mason } 126627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12678929ecfaSYan, Zheng return ret; 1268e9d0b13bSChris Mason } 1269e9d0b13bSChris Mason 1270d352ac68SChris Mason /* 1271d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1272aec8030aSMiao Xie * transaction commit. This does the actual creation. 1273aec8030aSMiao Xie * 1274aec8030aSMiao Xie * Note: 1275aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1276aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1277aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1278d352ac68SChris Mason */ 127980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 12803063d29fSChris Mason struct btrfs_fs_info *fs_info, 12813063d29fSChris Mason struct btrfs_pending_snapshot *pending) 12823063d29fSChris Mason { 12833063d29fSChris Mason struct btrfs_key key; 128480b6794dSChris Mason struct btrfs_root_item *new_root_item; 12853063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 12863063d29fSChris Mason struct btrfs_root *root = pending->root; 12876bdb72deSSage Weil struct btrfs_root *parent_root; 128898c9942aSLiu Bo struct btrfs_block_rsv *rsv; 12896bdb72deSSage Weil struct inode *parent_inode; 129042874b3dSMiao Xie struct btrfs_path *path; 129142874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1292a22285a6SYan, Zheng struct dentry *dentry; 12933063d29fSChris Mason struct extent_buffer *tmp; 1294925baeddSChris Mason struct extent_buffer *old; 12958ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1296aec8030aSMiao Xie int ret = 0; 1297d68fc57bSYan, Zheng u64 to_reserve = 0; 12986bdb72deSSage Weil u64 index = 0; 1299a22285a6SYan, Zheng u64 objectid; 1300b83cc969SLi Zefan u64 root_flags; 13018ea05e3aSAlexander Block uuid_le new_uuid; 13023063d29fSChris Mason 130342874b3dSMiao Xie path = btrfs_alloc_path(); 130442874b3dSMiao Xie if (!path) { 1305aec8030aSMiao Xie pending->error = -ENOMEM; 1306aec8030aSMiao Xie return 0; 130742874b3dSMiao Xie } 130842874b3dSMiao Xie 130980b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 131080b6794dSChris Mason if (!new_root_item) { 1311aec8030aSMiao Xie pending->error = -ENOMEM; 13126fa9700eSMiao Xie goto root_item_alloc_fail; 131380b6794dSChris Mason } 1314a22285a6SYan, Zheng 1315aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1316aec8030aSMiao Xie if (pending->error) 13176fa9700eSMiao Xie goto no_free_objectid; 13183063d29fSChris Mason 1319d6726335SQu Wenruo /* 1320d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1321d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1322d6726335SQu Wenruo */ 1323d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1324d6726335SQu Wenruo 1325147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1326d68fc57bSYan, Zheng 1327d68fc57bSYan, Zheng if (to_reserve > 0) { 1328aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1329aec8030aSMiao Xie &pending->block_rsv, 133008e007d2SMiao Xie to_reserve, 133108e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1332aec8030aSMiao Xie if (pending->error) 1333d6726335SQu Wenruo goto clear_skip_qgroup; 1334d68fc57bSYan, Zheng } 1335d68fc57bSYan, Zheng 13363063d29fSChris Mason key.objectid = objectid; 1337a22285a6SYan, Zheng key.offset = (u64)-1; 1338a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 13393063d29fSChris Mason 13406fa9700eSMiao Xie rsv = trans->block_rsv; 1341a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 13422382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 13436bdb72deSSage Weil 1344a22285a6SYan, Zheng dentry = pending->dentry; 1345e9662f70SMiao Xie parent_inode = pending->dir; 1346a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 13477585717fSChris Mason record_root_in_trans(trans, parent_root); 1348a22285a6SYan, Zheng 13496bdb72deSSage Weil /* 13506bdb72deSSage Weil * insert the directory item 13516bdb72deSSage Weil */ 13526bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 135349b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 135442874b3dSMiao Xie 135542874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 135642874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 135742874b3dSMiao Xie btrfs_ino(parent_inode), 135842874b3dSMiao Xie dentry->d_name.name, 135942874b3dSMiao Xie dentry->d_name.len, 0); 136042874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1361fe66a05aSChris Mason pending->error = -EEXIST; 1362aec8030aSMiao Xie goto dir_item_existed; 136342874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 136442874b3dSMiao Xie ret = PTR_ERR(dir_item); 13658732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13668732d44fSMiao Xie goto fail; 136779787eaaSJeff Mahoney } 136842874b3dSMiao Xie btrfs_release_path(path); 13696bdb72deSSage Weil 1370e999376fSChris Mason /* 1371e999376fSChris Mason * pull in the delayed directory update 1372e999376fSChris Mason * and the delayed inode item 1373e999376fSChris Mason * otherwise we corrupt the FS during 1374e999376fSChris Mason * snapshot 1375e999376fSChris Mason */ 1376e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 13778732d44fSMiao Xie if (ret) { /* Transaction aborted */ 13788732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13798732d44fSMiao Xie goto fail; 13808732d44fSMiao Xie } 1381e999376fSChris Mason 13827585717fSChris Mason record_root_in_trans(trans, root); 13836bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 13846bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 138508fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 13866bdb72deSSage Weil 1387b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1388b83cc969SLi Zefan if (pending->readonly) 1389b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1390b83cc969SLi Zefan else 1391b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1392b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1393b83cc969SLi Zefan 13948ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 13958ea05e3aSAlexander Block trans->transid); 13968ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 13978ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 13988ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 13998ea05e3aSAlexander Block BTRFS_UUID_SIZE); 140070023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 140170023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 140270023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14038ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14048ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 14058ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 14068ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 140770023da2SStefan Behrens } 14083cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 14093cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 141070023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 14118ea05e3aSAlexander Block 1412925baeddSChris Mason old = btrfs_lock_root_node(root); 141349b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 141479787eaaSJeff Mahoney if (ret) { 141579787eaaSJeff Mahoney btrfs_tree_unlock(old); 141679787eaaSJeff Mahoney free_extent_buffer(old); 14178732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14188732d44fSMiao Xie goto fail; 141979787eaaSJeff Mahoney } 142049b25e05SJeff Mahoney 14215d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 14223063d29fSChris Mason 142349b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 142479787eaaSJeff Mahoney /* clean up in any case */ 1425925baeddSChris Mason btrfs_tree_unlock(old); 1426925baeddSChris Mason free_extent_buffer(old); 14278732d44fSMiao Xie if (ret) { 14288732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14298732d44fSMiao Xie goto fail; 14308732d44fSMiao Xie } 1431f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 143227cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1433f1ebcc74SLiu Bo smp_wmb(); 1434f1ebcc74SLiu Bo 14355d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1436a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1437a22285a6SYan, Zheng key.offset = trans->transid; 1438a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1439925baeddSChris Mason btrfs_tree_unlock(tmp); 14403063d29fSChris Mason free_extent_buffer(tmp); 14418732d44fSMiao Xie if (ret) { 14428732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14438732d44fSMiao Xie goto fail; 14448732d44fSMiao Xie } 14450660b5afSChris Mason 1446a22285a6SYan, Zheng /* 1447a22285a6SYan, Zheng * insert root back/forward references 1448a22285a6SYan, Zheng */ 1449a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1450a22285a6SYan, Zheng parent_root->root_key.objectid, 145133345d01SLi Zefan btrfs_ino(parent_inode), index, 1452a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14538732d44fSMiao Xie if (ret) { 14548732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14558732d44fSMiao Xie goto fail; 14568732d44fSMiao Xie } 1457a22285a6SYan, Zheng 1458a22285a6SYan, Zheng key.offset = (u64)-1; 1459a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 146079787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 146179787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 14628732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14638732d44fSMiao Xie goto fail; 146479787eaaSJeff Mahoney } 1465d68fc57bSYan, Zheng 146649b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 14678732d44fSMiao Xie if (ret) { 14688732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14698732d44fSMiao Xie goto fail; 14708732d44fSMiao Xie } 1471361048f5SMiao Xie 1472361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 14738732d44fSMiao Xie if (ret) { 14748732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14758732d44fSMiao Xie goto fail; 14768732d44fSMiao Xie } 147742874b3dSMiao Xie 147842874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 147942874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 148042874b3dSMiao Xie parent_inode, &key, 148142874b3dSMiao Xie BTRFS_FT_DIR, index); 148242874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 14839c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 14848732d44fSMiao Xie if (ret) { 14858732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14868732d44fSMiao Xie goto fail; 14878732d44fSMiao Xie } 148842874b3dSMiao Xie 148942874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 149042874b3dSMiao Xie dentry->d_name.len * 2); 149142874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1492be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1493dd5f9615SStefan Behrens if (ret) { 14948732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1495dd5f9615SStefan Behrens goto fail; 1496dd5f9615SStefan Behrens } 1497dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1498dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1499dd5f9615SStefan Behrens if (ret) { 1500dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1501dd5f9615SStefan Behrens goto fail; 1502dd5f9615SStefan Behrens } 1503dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1504dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1505dd5f9615SStefan Behrens new_root_item->received_uuid, 1506dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1507dd5f9615SStefan Behrens objectid); 1508dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1509dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1510dd5f9615SStefan Behrens goto fail; 1511dd5f9615SStefan Behrens } 1512dd5f9615SStefan Behrens } 1513d6726335SQu Wenruo 1514d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1515d6726335SQu Wenruo if (ret) { 1516d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1517d6726335SQu Wenruo goto fail; 1518d6726335SQu Wenruo } 1519d6726335SQu Wenruo 1520d6726335SQu Wenruo /* 1521d6726335SQu Wenruo * account qgroup counters before qgroup_inherit() 1522d6726335SQu Wenruo */ 1523d6726335SQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 1524d6726335SQu Wenruo if (ret) 1525d6726335SQu Wenruo goto fail; 1526d6726335SQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 1527d6726335SQu Wenruo if (ret) 1528d6726335SQu Wenruo goto fail; 1529d6726335SQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 1530d6726335SQu Wenruo root->root_key.objectid, 1531d6726335SQu Wenruo objectid, pending->inherit); 1532d6726335SQu Wenruo if (ret) { 1533d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1534d6726335SQu Wenruo goto fail; 1535d6726335SQu Wenruo } 1536d6726335SQu Wenruo 15373063d29fSChris Mason fail: 1538aec8030aSMiao Xie pending->error = ret; 1539aec8030aSMiao Xie dir_item_existed: 154098c9942aSLiu Bo trans->block_rsv = rsv; 15412382c5ccSLiu Bo trans->bytes_reserved = 0; 1542d6726335SQu Wenruo clear_skip_qgroup: 1543d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 15446fa9700eSMiao Xie no_free_objectid: 15456fa9700eSMiao Xie kfree(new_root_item); 15466fa9700eSMiao Xie root_item_alloc_fail: 154742874b3dSMiao Xie btrfs_free_path(path); 154849b25e05SJeff Mahoney return ret; 15493063d29fSChris Mason } 15503063d29fSChris Mason 1551d352ac68SChris Mason /* 1552d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1553d352ac68SChris Mason */ 155480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 15553063d29fSChris Mason struct btrfs_fs_info *fs_info) 15563063d29fSChris Mason { 1557aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 15583063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1559aec8030aSMiao Xie int ret = 0; 15603de4586cSChris Mason 1561aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1562aec8030aSMiao Xie list_del(&pending->list); 1563aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1564aec8030aSMiao Xie if (ret) 1565aec8030aSMiao Xie break; 1566aec8030aSMiao Xie } 1567aec8030aSMiao Xie return ret; 15683de4586cSChris Mason } 15693de4586cSChris Mason 15705d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 15715d4f98a2SYan Zheng { 15725d4f98a2SYan Zheng struct btrfs_root_item *root_item; 15735d4f98a2SYan Zheng struct btrfs_super_block *super; 15745d4f98a2SYan Zheng 15756c41761fSDavid Sterba super = root->fs_info->super_copy; 15765d4f98a2SYan Zheng 15775d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 15785d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 15795d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 15805d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 15815d4f98a2SYan Zheng 15825d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 15835d4f98a2SYan Zheng super->root = root_item->bytenr; 15845d4f98a2SYan Zheng super->generation = root_item->generation; 15855d4f98a2SYan Zheng super->root_level = root_item->level; 158673bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 15870af3d00bSJosef Bacik super->cache_generation = root_item->generation; 158870f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 158926432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 15905d4f98a2SYan Zheng } 15915d4f98a2SYan Zheng 1592f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1593f36f3042SChris Mason { 15944a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1595f36f3042SChris Mason int ret = 0; 15964a9d8bdeSMiao Xie 1597a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 15984a9d8bdeSMiao Xie trans = info->running_transaction; 15994a9d8bdeSMiao Xie if (trans) 16004a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1601a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1602f36f3042SChris Mason return ret; 1603f36f3042SChris Mason } 1604f36f3042SChris Mason 16058929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16068929ecfaSYan, Zheng { 16074a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16088929ecfaSYan, Zheng int ret = 0; 16094a9d8bdeSMiao Xie 1610a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16114a9d8bdeSMiao Xie trans = info->running_transaction; 16124a9d8bdeSMiao Xie if (trans) 16134a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1614a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 16158929ecfaSYan, Zheng return ret; 16168929ecfaSYan, Zheng } 16178929ecfaSYan, Zheng 1618bb9c12c9SSage Weil /* 1619bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1620bb9c12c9SSage Weil * transaction joins 1621bb9c12c9SSage Weil */ 1622bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1623bb9c12c9SSage Weil struct btrfs_transaction *trans) 1624bb9c12c9SSage Weil { 16254a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1626501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1627501407aaSJosef Bacik trans->aborted); 1628bb9c12c9SSage Weil } 1629bb9c12c9SSage Weil 1630bb9c12c9SSage Weil /* 1631bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1632bb9c12c9SSage Weil * caller holds ref. 1633bb9c12c9SSage Weil */ 1634bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1635bb9c12c9SSage Weil struct btrfs_transaction *trans) 1636bb9c12c9SSage Weil { 163772d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1638501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1639501407aaSJosef Bacik trans->aborted); 1640bb9c12c9SSage Weil } 1641bb9c12c9SSage Weil 1642bb9c12c9SSage Weil /* 1643bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1644bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1645bb9c12c9SSage Weil */ 1646bb9c12c9SSage Weil struct btrfs_async_commit { 1647bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1648bb9c12c9SSage Weil struct btrfs_root *root; 16497892b5afSMiao Xie struct work_struct work; 1650bb9c12c9SSage Weil }; 1651bb9c12c9SSage Weil 1652bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1653bb9c12c9SSage Weil { 1654bb9c12c9SSage Weil struct btrfs_async_commit *ac = 16557892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1656bb9c12c9SSage Weil 16576fc4e354SSage Weil /* 16586fc4e354SSage Weil * We've got freeze protection passed with the transaction. 16596fc4e354SSage Weil * Tell lockdep about it. 16606fc4e354SSage Weil */ 1661b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1662bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 16636fc4e354SSage Weil 1664e209db7aSSage Weil current->journal_info = ac->newtrans; 1665e209db7aSSage Weil 1666bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1667bb9c12c9SSage Weil kfree(ac); 1668bb9c12c9SSage Weil } 1669bb9c12c9SSage Weil 1670bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1671bb9c12c9SSage Weil struct btrfs_root *root, 1672bb9c12c9SSage Weil int wait_for_unblock) 1673bb9c12c9SSage Weil { 1674bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1675bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1676bb9c12c9SSage Weil 1677bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1678db5b493aSTsutomu Itoh if (!ac) 1679db5b493aSTsutomu Itoh return -ENOMEM; 1680bb9c12c9SSage Weil 16817892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1682bb9c12c9SSage Weil ac->root = root; 16837a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 16843612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 16853612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 16863612b495STsutomu Itoh kfree(ac); 16873612b495STsutomu Itoh return err; 16883612b495STsutomu Itoh } 1689bb9c12c9SSage Weil 1690bb9c12c9SSage Weil /* take transaction reference */ 1691bb9c12c9SSage Weil cur_trans = trans->transaction; 169213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1693bb9c12c9SSage Weil 1694bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 16956fc4e354SSage Weil 16966fc4e354SSage Weil /* 16976fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 16986fc4e354SSage Weil * async commit thread will be the one to unlock it. 16996fc4e354SSage Weil */ 1700b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1701bee9182dSOleg Nesterov __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 17026fc4e354SSage Weil 17037892b5afSMiao Xie schedule_work(&ac->work); 1704bb9c12c9SSage Weil 1705bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1706bb9c12c9SSage Weil if (wait_for_unblock) 1707bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1708bb9c12c9SSage Weil else 1709bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1710bb9c12c9SSage Weil 171138e88054SSage Weil if (current->journal_info == trans) 171238e88054SSage Weil current->journal_info = NULL; 171338e88054SSage Weil 1714724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1715bb9c12c9SSage Weil return 0; 1716bb9c12c9SSage Weil } 1717bb9c12c9SSage Weil 171849b25e05SJeff Mahoney 171949b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 17207b8b92afSJosef Bacik struct btrfs_root *root, int err) 172149b25e05SJeff Mahoney { 172249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1723f094ac32SLiu Bo DEFINE_WAIT(wait); 172449b25e05SJeff Mahoney 172549b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 172649b25e05SJeff Mahoney 17277b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 17287b8b92afSJosef Bacik 172949b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 173066b6135bSLiu Bo 173125d8c284SMiao Xie /* 173225d8c284SMiao Xie * If the transaction is removed from the list, it means this 173325d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 173425d8c284SMiao Xie * to call the cleanup function. 173525d8c284SMiao Xie */ 173625d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 173766b6135bSLiu Bo 173849b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1739d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 17404a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1741f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1742f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1743f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1744f094ac32SLiu Bo 1745f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1746d7096fc3SJosef Bacik } 174749b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 174849b25e05SJeff Mahoney 174949b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 175049b25e05SJeff Mahoney 17514a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17524a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 17534a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 17544a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 17554a9d8bdeSMiao Xie 1756e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1757e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1758724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1759724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 176049b25e05SJeff Mahoney 176149b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 176249b25e05SJeff Mahoney 176349b25e05SJeff Mahoney if (current->journal_info == trans) 176449b25e05SJeff Mahoney current->journal_info = NULL; 1765c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 176649b25e05SJeff Mahoney 176749b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 176849b25e05SJeff Mahoney } 176949b25e05SJeff Mahoney 177082436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 177182436617SMiao Xie { 177282436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 17736c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 177482436617SMiao Xie return 0; 177582436617SMiao Xie } 177682436617SMiao Xie 177782436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 177882436617SMiao Xie { 177982436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1780b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 178182436617SMiao Xie } 178282436617SMiao Xie 178350d9aa99SJosef Bacik static inline void 1784161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 178550d9aa99SJosef Bacik { 1786161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1787161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 178850d9aa99SJosef Bacik } 178950d9aa99SJosef Bacik 179079154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 179179154b1bSChris Mason struct btrfs_root *root) 179279154b1bSChris Mason { 179349b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 17948fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1795656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 179625287e0aSMiao Xie int ret; 179779154b1bSChris Mason 17988d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 17998d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 180025287e0aSMiao Xie ret = cur_trans->aborted; 1801e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1802e4a2bcacSJosef Bacik return ret; 180325287e0aSMiao Xie } 180449b25e05SJeff Mahoney 180556bec294SChris Mason /* make a pass through all the delayed refs we have so far 180656bec294SChris Mason * any runnings procs may add more while we are here 180756bec294SChris Mason */ 180856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1809e4a2bcacSJosef Bacik if (ret) { 1810e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1811e4a2bcacSJosef Bacik return ret; 1812e4a2bcacSJosef Bacik } 181356bec294SChris Mason 18140e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18150e721106SJosef Bacik trans->block_rsv = NULL; 1816272d26d0SMiao Xie if (trans->qgroup_reserved) { 1817272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1818272d26d0SMiao Xie trans->qgroup_reserved = 0; 1819272d26d0SMiao Xie } 18200e721106SJosef Bacik 1821b7ec40d7SChris Mason cur_trans = trans->transaction; 182249b25e05SJeff Mahoney 182356bec294SChris Mason /* 182456bec294SChris Mason * set the flushing flag so procs in this transaction have to 182556bec294SChris Mason * start sending their work down. 182656bec294SChris Mason */ 1827b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 18281be41b78SJosef Bacik smp_wmb(); 182956bec294SChris Mason 1830ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1831ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1832ea658badSJosef Bacik 1833c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1834e4a2bcacSJosef Bacik if (ret) { 1835e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1836e4a2bcacSJosef Bacik return ret; 1837e4a2bcacSJosef Bacik } 183856bec294SChris Mason 1839*3204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 18401bbc621eSChris Mason int run_it = 0; 18411bbc621eSChris Mason 18421bbc621eSChris Mason /* this mutex is also taken before trying to set 18431bbc621eSChris Mason * block groups readonly. We need to make sure 18441bbc621eSChris Mason * that nobody has set a block group readonly 18451bbc621eSChris Mason * after a extents from that block group have been 18461bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 18471bbc621eSChris Mason * will wait for the transaction to commit if it 1848*3204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 18491bbc621eSChris Mason * 1850*3204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 1851*3204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 18521bbc621eSChris Mason * hurt to have more than one go through, but there's no 18531bbc621eSChris Mason * real advantage to it either. 18541bbc621eSChris Mason */ 18551bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 1856*3204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 1857*3204d33cSJosef Bacik &cur_trans->flags)) 18581bbc621eSChris Mason run_it = 1; 18591bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 18601bbc621eSChris Mason 18611bbc621eSChris Mason if (run_it) 18621bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 18631bbc621eSChris Mason } 18641bbc621eSChris Mason if (ret) { 18651bbc621eSChris Mason btrfs_end_transaction(trans, root); 18661bbc621eSChris Mason return ret; 18671bbc621eSChris Mason } 18681bbc621eSChris Mason 18694a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 18704a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 18714a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 187213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 187349b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1874ccd467d6SChris Mason 1875b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 187615ee9bc7SJosef Bacik 1877b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1878b4924a0fSLiu Bo ret = cur_trans->aborted; 1879b4924a0fSLiu Bo 1880724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 188115ee9bc7SJosef Bacik 188249b25e05SJeff Mahoney return ret; 188379154b1bSChris Mason } 18844313b399SChris Mason 18854a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1886bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1887bb9c12c9SSage Weil 1888ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1889ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1890ccd467d6SChris Mason struct btrfs_transaction, list); 18914a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 189213c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1893a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1894ccd467d6SChris Mason 1895ccd467d6SChris Mason wait_for_commit(root, prev_trans); 18961f9b8c8fSFilipe Manana ret = prev_trans->aborted; 1897ccd467d6SChris Mason 1898724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 18991f9b8c8fSFilipe Manana if (ret) 19001f9b8c8fSFilipe Manana goto cleanup_transaction; 1901a4abeea4SJosef Bacik } else { 1902a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1903ccd467d6SChris Mason } 1904a4abeea4SJosef Bacik } else { 1905a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1906ccd467d6SChris Mason } 190715ee9bc7SJosef Bacik 19080860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 19090860adfdSMiao Xie 191082436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 191182436617SMiao Xie if (ret) 191282436617SMiao Xie goto cleanup_transaction; 191382436617SMiao Xie 19148d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 191549b25e05SJeff Mahoney if (ret) 191649b25e05SJeff Mahoney goto cleanup_transaction; 191716cdcec7SMiao Xie 1918581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1919581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1920ed3b3d31SChris Mason 1921581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 19228d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1923ca469637SMiao Xie if (ret) 1924ca469637SMiao Xie goto cleanup_transaction; 1925ca469637SMiao Xie 192682436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1927cb7ab021SWang Shilong 1928161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 192950d9aa99SJosef Bacik 1930cb7ab021SWang Shilong btrfs_scrub_pause(root); 1931ed0ca140SJosef Bacik /* 1932ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1933ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 19344a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1935ed0ca140SJosef Bacik */ 1936a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19374a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1938a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1939ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1940ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 194115ee9bc7SJosef Bacik 19422cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 19432cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19442cba30f1SMiao Xie ret = cur_trans->aborted; 19456cf7f77eSWang Shilong goto scrub_continue; 19462cba30f1SMiao Xie } 19477585717fSChris Mason /* 19487585717fSChris Mason * the reloc mutex makes sure that we stop 19497585717fSChris Mason * the balancing code from coming in and moving 19507585717fSChris Mason * extents around in the middle of the commit 19517585717fSChris Mason */ 19527585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 19537585717fSChris Mason 195442874b3dSMiao Xie /* 195542874b3dSMiao Xie * We needn't worry about the delayed items because we will 195642874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 195742874b3dSMiao Xie * core function of the snapshot creation. 195842874b3dSMiao Xie */ 195942874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 196049b25e05SJeff Mahoney if (ret) { 196149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19626cf7f77eSWang Shilong goto scrub_continue; 196349b25e05SJeff Mahoney } 19643063d29fSChris Mason 196542874b3dSMiao Xie /* 196642874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 196742874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 196842874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 196942874b3dSMiao Xie * them. 197042874b3dSMiao Xie * 197142874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 197242874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 197342874b3dSMiao Xie * the nodes and leaves. 197442874b3dSMiao Xie */ 197542874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 197649b25e05SJeff Mahoney if (ret) { 197749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19786cf7f77eSWang Shilong goto scrub_continue; 197949b25e05SJeff Mahoney } 198016cdcec7SMiao Xie 198156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 198249b25e05SJeff Mahoney if (ret) { 198349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19846cf7f77eSWang Shilong goto scrub_continue; 198549b25e05SJeff Mahoney } 198656bec294SChris Mason 19870ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 19880ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 19890ed4792aSQu Wenruo if (ret) { 19900ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 19910ed4792aSQu Wenruo goto scrub_continue; 19920ed4792aSQu Wenruo } 19930ed4792aSQu Wenruo 1994e999376fSChris Mason /* 1995e999376fSChris Mason * make sure none of the code above managed to slip in a 1996e999376fSChris Mason * delayed item 1997e999376fSChris Mason */ 1998e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1999e999376fSChris Mason 20002c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2001dc17ff8fSChris Mason 2002e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2003e02119d5SChris Mason * various roots consistent with each other. Every pointer 2004e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2005e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2006e02119d5SChris Mason * the tree logging code from jumping in and changing any 2007e02119d5SChris Mason * of the trees. 2008e02119d5SChris Mason * 2009e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2010e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2011e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2012e02119d5SChris Mason * from now until after the super is written, we avoid races 2013e02119d5SChris Mason * with the tree-log code. 2014e02119d5SChris Mason */ 2015e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 20161a40e23bSZheng Yan 20175d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 201849b25e05SJeff Mahoney if (ret) { 201949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2020871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20216cf7f77eSWang Shilong goto scrub_continue; 202249b25e05SJeff Mahoney } 202354aa1f4dSChris Mason 20243818aea2SQu Wenruo /* 20257e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 20267e1876acSDavid Sterba * before the next transaction. 20273818aea2SQu Wenruo */ 2028572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 20293818aea2SQu Wenruo 20305d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2031e02119d5SChris Mason * safe to free the root of tree log roots 2032e02119d5SChris Mason */ 2033e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2034e02119d5SChris Mason 20350ed4792aSQu Wenruo /* 20360ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 20370ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 20380ed4792aSQu Wenruo */ 20390ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 20400ed4792aSQu Wenruo if (ret < 0) { 20410ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 20420ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20430ed4792aSQu Wenruo goto scrub_continue; 20440ed4792aSQu Wenruo } 20450ed4792aSQu Wenruo 20465d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 204749b25e05SJeff Mahoney if (ret) { 204849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2049871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20506cf7f77eSWang Shilong goto scrub_continue; 205149b25e05SJeff Mahoney } 205254aa1f4dSChris Mason 20532cba30f1SMiao Xie /* 20542cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 20552cba30f1SMiao Xie * update ->aborted, check it. 20562cba30f1SMiao Xie */ 20572cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20582cba30f1SMiao Xie ret = cur_trans->aborted; 20592cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 20602cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 20616cf7f77eSWang Shilong goto scrub_continue; 20622cba30f1SMiao Xie } 20632cba30f1SMiao Xie 206411833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 206511833d66SYan Zheng 206678fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 20675f39d397SChris Mason 20685d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 20695d4f98a2SYan Zheng root->fs_info->tree_root->node); 20709e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 20719e351cc8SJosef Bacik &cur_trans->switch_commits); 20725d4f98a2SYan Zheng 20735d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 20745d4f98a2SYan Zheng root->fs_info->chunk_root->node); 20759e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 20769e351cc8SJosef Bacik &cur_trans->switch_commits); 20779e351cc8SJosef Bacik 20789e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 20795d4f98a2SYan Zheng 2080edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2081ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 20821bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 20835d4f98a2SYan Zheng update_super_roots(root); 2084e02119d5SChris Mason 20856c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 20866c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 20876c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 20886c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2089ccd467d6SChris Mason 2090935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2091ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2092935e5cc9SMiao Xie 2093656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2094656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2095656f30dbSFilipe Manana 20964fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 20974fbcdf66SFilipe Manana 2098a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 20994a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2100a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2101a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 21027585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2103b7ec40d7SChris Mason 2104f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2105e6dcd2dcSChris Mason 210679154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 210749b25e05SJeff Mahoney if (ret) { 2108a4553fefSAnand Jain btrfs_std_error(root->fs_info, ret, 210908748810SDavid Sterba "Error while writing out transaction"); 211049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21116cf7f77eSWang Shilong goto scrub_continue; 211249b25e05SJeff Mahoney } 211349b25e05SJeff Mahoney 211449b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 211549b25e05SJeff Mahoney if (ret) { 211649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21176cf7f77eSWang Shilong goto scrub_continue; 211849b25e05SJeff Mahoney } 21194313b399SChris Mason 2120e02119d5SChris Mason /* 2121e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2122e02119d5SChris Mason * to go about their business 2123e02119d5SChris Mason */ 2124e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2125e02119d5SChris Mason 212611833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 21274313b399SChris Mason 2128*3204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 212913212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 213013212b54SZhao Lei 213115ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 21324a9d8bdeSMiao Xie /* 21334a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 21344a9d8bdeSMiao Xie * which can change it. 21354a9d8bdeSMiao Xie */ 21364a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 21372c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 21383de4586cSChris Mason 2139a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 214013c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2141a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2142a4abeea4SJosef Bacik 2143724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2144724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 214558176a96SJosef Bacik 21460860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2147b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2148b2b5ef5cSJan Kara 21491abe9b8aSliubo trace_btrfs_transaction_commit(root); 21501abe9b8aSliubo 2151a2de733cSArne Jansen btrfs_scrub_continue(root); 2152a2de733cSArne Jansen 21539ed74f2dSJosef Bacik if (current->journal_info == trans) 21549ed74f2dSJosef Bacik current->journal_info = NULL; 21559ed74f2dSJosef Bacik 21562c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 215724bbcf04SYan, Zheng 21588a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 21598a733013SZhao Lei current != root->fs_info->cleaner_kthread) 216024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 216124bbcf04SYan, Zheng 216279154b1bSChris Mason return ret; 216349b25e05SJeff Mahoney 21646cf7f77eSWang Shilong scrub_continue: 21656cf7f77eSWang Shilong btrfs_scrub_continue(root); 216649b25e05SJeff Mahoney cleanup_transaction: 21670e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 21684fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 21690e721106SJosef Bacik trans->block_rsv = NULL; 2170272d26d0SMiao Xie if (trans->qgroup_reserved) { 2171272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 2172272d26d0SMiao Xie trans->qgroup_reserved = 0; 2173272d26d0SMiao Xie } 2174c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 217549b25e05SJeff Mahoney if (current->journal_info == trans) 217649b25e05SJeff Mahoney current->journal_info = NULL; 21777b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 217849b25e05SJeff Mahoney 217949b25e05SJeff Mahoney return ret; 218079154b1bSChris Mason } 218179154b1bSChris Mason 2182d352ac68SChris Mason /* 21839d1a2a3aSDavid Sterba * return < 0 if error 21849d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 21859d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 21869d1a2a3aSDavid Sterba * 21879d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 21889d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 21899d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 21909d1a2a3aSDavid Sterba * few seconds later. 2191d352ac68SChris Mason */ 21929d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2193e9d0b13bSChris Mason { 21949d1a2a3aSDavid Sterba int ret; 21955d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2196e9d0b13bSChris Mason 2197a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 21989d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 21999d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 22009d1a2a3aSDavid Sterba return 0; 22019d1a2a3aSDavid Sterba } 22029d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 22039d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2204cfad392bSJosef Bacik list_del_init(&root->root_list); 2205a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 22065d4f98a2SYan Zheng 2207efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 220876dda93cSYan, Zheng 220916cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 221016cdcec7SMiao Xie 221176dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 221276dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 22132c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 221476dda93cSYan, Zheng else 22152c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 221632471dc2SDavid Sterba 22176596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2218e9d0b13bSChris Mason } 2219572d9ab7SDavid Sterba 2220572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2221572d9ab7SDavid Sterba { 2222572d9ab7SDavid Sterba unsigned long prev; 2223572d9ab7SDavid Sterba unsigned long bit; 2224572d9ab7SDavid Sterba 22256c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2226572d9ab7SDavid Sterba if (!prev) 2227572d9ab7SDavid Sterba return; 2228572d9ab7SDavid Sterba 22297e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 22307e1876acSDavid Sterba if (prev & bit) 22317e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22327e1876acSDavid Sterba prev &= ~bit; 22337e1876acSDavid Sterba 22347e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 22357e1876acSDavid Sterba if (prev & bit) 22367e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22377e1876acSDavid Sterba prev &= ~bit; 22387e1876acSDavid Sterba 2239d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2240d51033d0SDavid Sterba if (prev & bit) 2241d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2242d51033d0SDavid Sterba prev &= ~bit; 2243d51033d0SDavid Sterba 2244572d9ab7SDavid Sterba if (prev) 2245572d9ab7SDavid Sterba btrfs_warn(fs_info, 2246572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2247572d9ab7SDavid Sterba } 2248