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, 40bcf3a3e7SNikolay Borisov [TRANS_STATE_BLOCKED] = __TRANS_START, 41bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), 42bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | 434a9d8bdeSMiao Xie __TRANS_ATTACH | 444a9d8bdeSMiao Xie __TRANS_JOIN), 45bcf3a3e7SNikolay Borisov [TRANS_STATE_UNBLOCKED] = (__TRANS_START | 464a9d8bdeSMiao Xie __TRANS_ATTACH | 474a9d8bdeSMiao Xie __TRANS_JOIN | 484a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 49bcf3a3e7SNikolay Borisov [TRANS_STATE_COMPLETED] = (__TRANS_START | 504a9d8bdeSMiao Xie __TRANS_ATTACH | 514a9d8bdeSMiao Xie __TRANS_JOIN | 524a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 534a9d8bdeSMiao Xie }; 544a9d8bdeSMiao Xie 55724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 5679154b1bSChris Mason { 579b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 589b64f57dSElena Reshetova if (refcount_dec_and_test(&transaction->use_count)) { 59a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 60c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 611262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 62ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 63ab8d0fc4SJeff Mahoney "pending csums is %llu", 641262133bSJosef Bacik transaction->delayed_refs.pending_csums); 656df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 666df9a95eSJosef Bacik struct extent_map *em; 676df9a95eSJosef Bacik 686df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 696df9a95eSJosef Bacik struct extent_map, list); 706df9a95eSJosef Bacik list_del_init(&em->list); 716df9a95eSJosef Bacik free_extent_map(em); 726df9a95eSJosef Bacik } 737785a663SFilipe Manana /* 747785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 757785a663SFilipe Manana * because the transaction was aborted and a commit did not 767785a663SFilipe Manana * happen (things failed before writing the new superblock 777785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 787785a663SFilipe Manana * discard the physical locations of the block groups. 797785a663SFilipe Manana */ 807785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 817785a663SFilipe Manana struct btrfs_block_group_cache *cache; 827785a663SFilipe Manana 837785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 847785a663SFilipe Manana struct btrfs_block_group_cache, 857785a663SFilipe Manana bg_list); 867785a663SFilipe Manana list_del_init(&cache->bg_list); 877785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 887785a663SFilipe Manana btrfs_put_block_group(cache); 897785a663SFilipe Manana } 904b5faeacSDavid Sterba kfree(transaction); 9179154b1bSChris Mason } 9278fae27eSChris Mason } 9379154b1bSChris Mason 94663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 95663dfbb0SFilipe Manana { 96663dfbb0SFilipe Manana spin_lock(&tree->lock); 97b666a9cdSDavid Sterba /* 98b666a9cdSDavid Sterba * Do a single barrier for the waitqueue_active check here, the state 99b666a9cdSDavid Sterba * of the waitqueue should not change once clear_btree_io_tree is 100b666a9cdSDavid Sterba * called. 101b666a9cdSDavid Sterba */ 102b666a9cdSDavid Sterba smp_mb(); 103663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 104663dfbb0SFilipe Manana struct rb_node *node; 105663dfbb0SFilipe Manana struct extent_state *state; 106663dfbb0SFilipe Manana 107663dfbb0SFilipe Manana node = rb_first(&tree->state); 108663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 109663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 110663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 111663dfbb0SFilipe Manana /* 112663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 113663dfbb0SFilipe Manana * changes in the flags of extent states ever. 114663dfbb0SFilipe Manana */ 115663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 116663dfbb0SFilipe Manana free_extent_state(state); 117351810c1SDavid Sterba 118351810c1SDavid Sterba cond_resched_lock(&tree->lock); 119663dfbb0SFilipe Manana } 120663dfbb0SFilipe Manana spin_unlock(&tree->lock); 121663dfbb0SFilipe Manana } 122663dfbb0SFilipe Manana 12316916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans) 124817d52f8SJosef Bacik { 12516916a88SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1269e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1279e351cc8SJosef Bacik 1289e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1299e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1309e351cc8SJosef Bacik dirty_list) { 1319e351cc8SJosef Bacik list_del_init(&root->dirty_list); 132817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 133817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1349e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1359e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 136663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1379e351cc8SJosef Bacik } 1382b9dbef2SJosef Bacik 1392b9dbef2SJosef Bacik /* We can free old roots now. */ 1402b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1412b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1422b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1432b9dbef2SJosef Bacik struct btrfs_root, root_list); 1442b9dbef2SJosef Bacik list_del_init(&root->root_list); 1452b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1462b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1472b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1482b9dbef2SJosef Bacik } 1492b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1509e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 151817d52f8SJosef Bacik } 152817d52f8SJosef Bacik 1530860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1540860adfdSMiao Xie unsigned int type) 1550860adfdSMiao Xie { 1560860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1570860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1580860adfdSMiao Xie } 1590860adfdSMiao Xie 1600860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1610860adfdSMiao Xie unsigned int type) 1620860adfdSMiao Xie { 1630860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1640860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1650860adfdSMiao Xie } 1660860adfdSMiao Xie 1670860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1680860adfdSMiao Xie unsigned int type) 1690860adfdSMiao Xie { 1700860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1710860adfdSMiao Xie } 1720860adfdSMiao Xie 1730860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1740860adfdSMiao Xie { 1750860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 176178260b2SMiao Xie } 177178260b2SMiao Xie 178d352ac68SChris Mason /* 179d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 180d352ac68SChris Mason */ 1812ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1822ff7e61eSJeff Mahoney unsigned int type) 18379154b1bSChris Mason { 18479154b1bSChris Mason struct btrfs_transaction *cur_trans; 185a4abeea4SJosef Bacik 18619ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 187d43317dcSChris Mason loop: 18849b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 18987533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 19019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 19149b25e05SJeff Mahoney return -EROFS; 19249b25e05SJeff Mahoney } 19349b25e05SJeff Mahoney 19419ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 195a4abeea4SJosef Bacik if (cur_trans) { 196871383beSDavid Sterba if (cur_trans->aborted) { 19719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 19849b25e05SJeff Mahoney return cur_trans->aborted; 199871383beSDavid Sterba } 2004a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 201178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 202178260b2SMiao Xie return -EBUSY; 203178260b2SMiao Xie } 2049b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 205a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 2060860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 20719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 208a4abeea4SJosef Bacik return 0; 209a4abeea4SJosef Bacik } 21019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 211a4abeea4SJosef Bacik 212354aa0fbSMiao Xie /* 213354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 214354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 215354aa0fbSMiao Xie */ 216354aa0fbSMiao Xie if (type == TRANS_ATTACH) 217354aa0fbSMiao Xie return -ENOENT; 218354aa0fbSMiao Xie 2194a9d8bdeSMiao Xie /* 2204a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 2214a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 2224a9d8bdeSMiao Xie */ 2234a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2244a9d8bdeSMiao Xie 2254b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS); 226db5b493aSTsutomu Itoh if (!cur_trans) 227db5b493aSTsutomu Itoh return -ENOMEM; 228d43317dcSChris Mason 22919ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 23019ae4e81SJan Schmidt if (fs_info->running_transaction) { 231d43317dcSChris Mason /* 232d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2334a9d8bdeSMiao Xie * to redo the checks above 234d43317dcSChris Mason */ 2354b5faeacSDavid Sterba kfree(cur_trans); 236d43317dcSChris Mason goto loop; 23787533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 238e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2394b5faeacSDavid Sterba kfree(cur_trans); 2407b8b92afSJosef Bacik return -EROFS; 241a4abeea4SJosef Bacik } 242d43317dcSChris Mason 243ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 24413c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2450860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 24679154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 24779154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 248161c3549SJosef Bacik init_waitqueue_head(&cur_trans->pending_wait); 2494a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 250a4abeea4SJosef Bacik /* 251a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 252a4abeea4SJosef Bacik * commit the transaction. 253a4abeea4SJosef Bacik */ 2549b64f57dSElena Reshetova refcount_set(&cur_trans->use_count, 2); 255161c3549SJosef Bacik atomic_set(&cur_trans->pending_ordered, 0); 2563204d33cSJosef Bacik cur_trans->flags = 0; 25708607c1bSChris Mason cur_trans->start_time = get_seconds(); 25856bec294SChris Mason 259a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 260a099d0fdSAlexandru Moise 261c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2623368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 263d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 26420b297d6SJan Schmidt 26520b297d6SJan Schmidt /* 26620b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 26720b297d6SJan Schmidt * the log must never go across transaction boundaries. 26820b297d6SJan Schmidt */ 26920b297d6SJan Schmidt smp_mb(); 27031b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2715d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 27231b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2735d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n"); 274fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 27520b297d6SJan Schmidt 27656bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 27756bec294SChris Mason 2783063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2796df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2809e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 281ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2821bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2832b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2841bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 285cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 286ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 287e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2882b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 28919ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 290d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 291c6100a4bSJosef Bacik fs_info->btree_inode); 29219ae4e81SJan Schmidt fs_info->generation++; 29319ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 29419ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 29549b25e05SJeff Mahoney cur_trans->aborted = 0; 29619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 29715ee9bc7SJosef Bacik 29879154b1bSChris Mason return 0; 29979154b1bSChris Mason } 30079154b1bSChris Mason 301d352ac68SChris Mason /* 302d397712bSChris Mason * this does all the record keeping required to make sure that a reference 303d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 304d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 305d397712bSChris Mason * when the transaction commits 306d352ac68SChris Mason */ 3077585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 3086426c7adSQu Wenruo struct btrfs_root *root, 3096426c7adSQu Wenruo int force) 3106702ed49SChris Mason { 3110b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3120b246afaSJeff Mahoney 3136426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 3146426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 3150b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 3164d31778aSQu Wenruo WARN_ON(!force && root->commit_root != root->node); 3175d4f98a2SYan Zheng 3187585717fSChris Mason /* 31927cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 3207585717fSChris Mason * we have the reloc mutex held now, so there 3217585717fSChris Mason * is only one writer in this function 3227585717fSChris Mason */ 32327cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3247585717fSChris Mason 32527cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 3267585717fSChris Mason * they find our root->last_trans update 3277585717fSChris Mason */ 3287585717fSChris Mason smp_wmb(); 3297585717fSChris Mason 3300b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3316426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 3320b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 333a4abeea4SJosef Bacik return 0; 334a4abeea4SJosef Bacik } 3350b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 3366702ed49SChris Mason (unsigned long)root->root_key.objectid, 3376702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 3380b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3397585717fSChris Mason root->last_trans = trans->transid; 3407585717fSChris Mason 3417585717fSChris Mason /* this is pretty tricky. We don't want to 3427585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3437585717fSChris Mason * unless we're really doing the first setup for this root in 3447585717fSChris Mason * this transaction. 3457585717fSChris Mason * 3467585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3477585717fSChris Mason * if we want to take the expensive mutex. 3487585717fSChris Mason * 3497585717fSChris Mason * But, we have to set root->last_trans before we 3507585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3517585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 35227cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3537585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3547585717fSChris Mason * 3557585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3567585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3577585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3587585717fSChris Mason * done before we pop in the zero below 3597585717fSChris Mason */ 3605d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 361c7548af6SChris Mason smp_mb__before_atomic(); 36227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3636702ed49SChris Mason } 3645d4f98a2SYan Zheng return 0; 3656702ed49SChris Mason } 3665d4f98a2SYan Zheng 3677585717fSChris Mason 3682b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3692b9dbef2SJosef Bacik struct btrfs_root *root) 3702b9dbef2SJosef Bacik { 3710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3722b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3732b9dbef2SJosef Bacik 3742b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3752b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3762b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3772b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3782b9dbef2SJosef Bacik 3792b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3800b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3810b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3822b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3832b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3840b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3852b9dbef2SJosef Bacik } 3862b9dbef2SJosef Bacik 3877585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3887585717fSChris Mason struct btrfs_root *root) 3897585717fSChris Mason { 3900b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3910b246afaSJeff Mahoney 39227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3937585717fSChris Mason return 0; 3947585717fSChris Mason 3957585717fSChris Mason /* 39627cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3977585717fSChris Mason * and barriers 3987585717fSChris Mason */ 3997585717fSChris Mason smp_rmb(); 4007585717fSChris Mason if (root->last_trans == trans->transid && 40127cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 4027585717fSChris Mason return 0; 4037585717fSChris Mason 4040b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 4056426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 4060b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 4077585717fSChris Mason 4087585717fSChris Mason return 0; 4097585717fSChris Mason } 4107585717fSChris Mason 4114a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 4124a9d8bdeSMiao Xie { 4134a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 414501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 415501407aaSJosef Bacik !trans->aborted); 4164a9d8bdeSMiao Xie } 4174a9d8bdeSMiao Xie 418d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 419d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 420d352ac68SChris Mason * transaction might not be fully on disk. 421d352ac68SChris Mason */ 4222ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 42379154b1bSChris Mason { 424f9295749SChris Mason struct btrfs_transaction *cur_trans; 42579154b1bSChris Mason 4260b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4270b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 4284a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 4299b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 4300b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 43172d63ed6SLi Zefan 4320b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 433501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 434501407aaSJosef Bacik cur_trans->aborted); 435724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 436a4abeea4SJosef Bacik } else { 4370b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 438f9295749SChris Mason } 43937d1aeeeSChris Mason } 44037d1aeeeSChris Mason 4412ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 44237d1aeeeSChris Mason { 4430b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 444a4abeea4SJosef Bacik return 0; 445a4abeea4SJosef Bacik 44692e2f7e3SNikolay Borisov if (type == TRANS_START) 447a4abeea4SJosef Bacik return 1; 448a4abeea4SJosef Bacik 449a22285a6SYan, Zheng return 0; 450a22285a6SYan, Zheng } 451a22285a6SYan, Zheng 45220dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 45320dd2cbfSMiao Xie { 4540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4550b246afaSJeff Mahoney 4560b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 45727cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 45820dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 45920dd2cbfSMiao Xie root->reloc_root) 46020dd2cbfSMiao Xie return false; 46120dd2cbfSMiao Xie 46220dd2cbfSMiao Xie return true; 46320dd2cbfSMiao Xie } 46420dd2cbfSMiao Xie 46508e007d2SMiao Xie static struct btrfs_trans_handle * 4665aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 467003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 468003d7c59SJeff Mahoney bool enforce_qgroups) 469a22285a6SYan, Zheng { 4700b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4710b246afaSJeff Mahoney 472a22285a6SYan, Zheng struct btrfs_trans_handle *h; 473a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 474b5009945SJosef Bacik u64 num_bytes = 0; 475c5567237SArne Jansen u64 qgroup_reserved = 0; 47620dd2cbfSMiao Xie bool reloc_reserved = false; 47720dd2cbfSMiao Xie int ret; 478acce952bSliubo 47946c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4802755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 48146c4e71eSFilipe Manana 4820b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 483acce952bSliubo return ERR_PTR(-EROFS); 4842a1eb461SJosef Bacik 48546c4e71eSFilipe Manana if (current->journal_info) { 4860860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4872a1eb461SJosef Bacik h = current->journal_info; 488b50fff81SDavid Sterba refcount_inc(&h->use_count); 489b50fff81SDavid Sterba WARN_ON(refcount_read(&h->use_count) > 2); 4902a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4912a1eb461SJosef Bacik h->block_rsv = NULL; 4922a1eb461SJosef Bacik goto got_it; 4932a1eb461SJosef Bacik } 494b5009945SJosef Bacik 495b5009945SJosef Bacik /* 496b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 497b5009945SJosef Bacik * the appropriate flushing if need be. 498b5009945SJosef Bacik */ 499003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 5000b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 501003d7c59SJeff Mahoney ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved, 502003d7c59SJeff Mahoney enforce_qgroups); 503c5567237SArne Jansen if (ret) 504c5567237SArne Jansen return ERR_PTR(ret); 505c5567237SArne Jansen 5060b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 50720dd2cbfSMiao Xie /* 50820dd2cbfSMiao Xie * Do the reservation for the relocation root creation 50920dd2cbfSMiao Xie */ 510ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 5110b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 51220dd2cbfSMiao Xie reloc_reserved = true; 51320dd2cbfSMiao Xie } 51420dd2cbfSMiao Xie 5150b246afaSJeff Mahoney ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, 51608e007d2SMiao Xie num_bytes, flush); 517b5009945SJosef Bacik if (ret) 518843fcf35SMiao Xie goto reserve_fail; 519b5009945SJosef Bacik } 520a22285a6SYan, Zheng again: 521f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 522843fcf35SMiao Xie if (!h) { 523843fcf35SMiao Xie ret = -ENOMEM; 524843fcf35SMiao Xie goto alloc_fail; 525843fcf35SMiao Xie } 526a22285a6SYan, Zheng 52798114659SJosef Bacik /* 52898114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 52998114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 53098114659SJosef Bacik * because we're already holding a ref. We need this because we could 53198114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53298114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 533354aa0fbSMiao Xie * 534354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 535354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 53698114659SJosef Bacik */ 5370860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5380b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 539b2b5ef5cSJan Kara 5402ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5412ff7e61eSJeff Mahoney wait_current_trans(fs_info); 542a22285a6SYan, Zheng 543a4abeea4SJosef Bacik do { 5442ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 545178260b2SMiao Xie if (ret == -EBUSY) { 5462ff7e61eSJeff Mahoney wait_current_trans(fs_info); 547178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 548178260b2SMiao Xie ret = -ENOENT; 549178260b2SMiao Xie } 550a4abeea4SJosef Bacik } while (ret == -EBUSY); 551a4abeea4SJosef Bacik 552a43f7f82SLiu Bo if (ret < 0) 553843fcf35SMiao Xie goto join_fail; 5540f7d52f4SChris Mason 5550b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 556a22285a6SYan, Zheng 557a22285a6SYan, Zheng h->transid = cur_trans->transid; 558a22285a6SYan, Zheng h->transaction = cur_trans; 559d13603efSArne Jansen h->root = root; 560b50fff81SDavid Sterba refcount_set(&h->use_count, 1); 56164b63580SJeff Mahoney h->fs_info = root->fs_info; 5627174109cSQu Wenruo 563a698d075SMiao Xie h->type = type; 564d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 565ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 566b7ec40d7SChris Mason 567a22285a6SYan, Zheng smp_mb(); 5684a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5692ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 570abdd2e80SFilipe Manana current->journal_info = h; 5713a45bb20SJeff Mahoney btrfs_commit_transaction(h); 572a22285a6SYan, Zheng goto again; 573a22285a6SYan, Zheng } 5749ed74f2dSJosef Bacik 575b5009945SJosef Bacik if (num_bytes) { 5760b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5772bcc0328SLiu Bo h->transid, num_bytes, 1); 5780b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 579b5009945SJosef Bacik h->bytes_reserved = num_bytes; 58020dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 581a22285a6SYan, Zheng } 582a22285a6SYan, Zheng 5832a1eb461SJosef Bacik got_it: 584a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 585a22285a6SYan, Zheng 586bcf3a3e7SNikolay Borisov if (!current->journal_info) 587a22285a6SYan, Zheng current->journal_info = h; 58879154b1bSChris Mason return h; 589843fcf35SMiao Xie 590843fcf35SMiao Xie join_fail: 5910860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5920b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 593843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 594843fcf35SMiao Xie alloc_fail: 595843fcf35SMiao Xie if (num_bytes) 5962ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 597843fcf35SMiao Xie num_bytes); 598843fcf35SMiao Xie reserve_fail: 5997174109cSQu Wenruo btrfs_qgroup_free_meta(root, qgroup_reserved); 600843fcf35SMiao Xie return ERR_PTR(ret); 60179154b1bSChris Mason } 60279154b1bSChris Mason 603f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6045aed1dd8SAlexandru Moise unsigned int num_items) 605f9295749SChris Mason { 60608e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 607003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 608f9295749SChris Mason } 609003d7c59SJeff Mahoney 6108eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6118eab77ffSFilipe Manana struct btrfs_root *root, 6128eab77ffSFilipe Manana unsigned int num_items, 6138eab77ffSFilipe Manana int min_factor) 6148eab77ffSFilipe Manana { 6150b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6168eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6178eab77ffSFilipe Manana u64 num_bytes; 6188eab77ffSFilipe Manana int ret; 6198eab77ffSFilipe Manana 620003d7c59SJeff Mahoney /* 621003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 622003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 623003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 624003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 625003d7c59SJeff Mahoney */ 626003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 627003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6288eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6298eab77ffSFilipe Manana return trans; 6308eab77ffSFilipe Manana 6318eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6328eab77ffSFilipe Manana if (IS_ERR(trans)) 6338eab77ffSFilipe Manana return trans; 6348eab77ffSFilipe Manana 6350b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6360b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6370b246afaSJeff Mahoney num_bytes, min_factor); 6388eab77ffSFilipe Manana if (ret) { 6393a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6408eab77ffSFilipe Manana return ERR_PTR(ret); 6418eab77ffSFilipe Manana } 6428eab77ffSFilipe Manana 6430b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6448eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6450b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 64688d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6478eab77ffSFilipe Manana 6488eab77ffSFilipe Manana return trans; 6498eab77ffSFilipe Manana } 6508407aa46SMiao Xie 6517a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 652f9295749SChris Mason { 653003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 654003d7c59SJeff Mahoney true); 655f9295749SChris Mason } 656f9295749SChris Mason 6577a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6580af3d00bSJosef Bacik { 659575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 660003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6610af3d00bSJosef Bacik } 6620af3d00bSJosef Bacik 663d4edf39bSMiao Xie /* 664d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 665d4edf39bSMiao Xie * 666d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 667d4edf39bSMiao Xie * don't want to start a new one. 668d4edf39bSMiao Xie * 669d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 670d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 671d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 672d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 673d4edf39bSMiao Xie * invoke 674d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 675d4edf39bSMiao Xie */ 676354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 67760376ce4SJosef Bacik { 678575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 679003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 68060376ce4SJosef Bacik } 68160376ce4SJosef Bacik 682d4edf39bSMiao Xie /* 68390b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 684d4edf39bSMiao Xie * 685d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 686d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 687d4edf39bSMiao Xie * complete. 688d4edf39bSMiao Xie */ 689d4edf39bSMiao Xie struct btrfs_trans_handle * 690d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 691d4edf39bSMiao Xie { 692d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 693d4edf39bSMiao Xie 694575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 695003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 696d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 6972ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 698d4edf39bSMiao Xie 699d4edf39bSMiao Xie return trans; 700d4edf39bSMiao Xie } 701d4edf39bSMiao Xie 702d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 7032ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 70489ce8a63SChris Mason { 7054a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 70689ce8a63SChris Mason } 70789ce8a63SChris Mason 7082ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 70946204592SSage Weil { 71046204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7118cd2807fSMiao Xie int ret = 0; 71246204592SSage Weil 71346204592SSage Weil if (transid) { 7140b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 715a4abeea4SJosef Bacik goto out; 71646204592SSage Weil 71746204592SSage Weil /* find specified transaction */ 7180b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7190b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 72046204592SSage Weil if (t->transid == transid) { 72146204592SSage Weil cur_trans = t; 7229b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7238cd2807fSMiao Xie ret = 0; 72446204592SSage Weil break; 72546204592SSage Weil } 7268cd2807fSMiao Xie if (t->transid > transid) { 7278cd2807fSMiao Xie ret = 0; 72846204592SSage Weil break; 72946204592SSage Weil } 7308cd2807fSMiao Xie } 7310b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 73242383020SSage Weil 73342383020SSage Weil /* 73442383020SSage Weil * The specified transaction doesn't exist, or we 73542383020SSage Weil * raced with btrfs_commit_transaction 73642383020SSage Weil */ 73742383020SSage Weil if (!cur_trans) { 7380b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 73942383020SSage Weil ret = -EINVAL; 7408cd2807fSMiao Xie goto out; 74142383020SSage Weil } 74246204592SSage Weil } else { 74346204592SSage Weil /* find newest transaction that is committing | committed */ 7440b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7450b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 74646204592SSage Weil list) { 7474a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7484a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7493473f3c0SJosef Bacik break; 75046204592SSage Weil cur_trans = t; 7519b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 75246204592SSage Weil break; 75346204592SSage Weil } 75446204592SSage Weil } 7550b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 75646204592SSage Weil if (!cur_trans) 757a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 75846204592SSage Weil } 75946204592SSage Weil 7602ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 761724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 762a4abeea4SJosef Bacik out: 76346204592SSage Weil return ret; 76446204592SSage Weil } 76546204592SSage Weil 7662ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 76737d1aeeeSChris Mason { 7682ff7e61eSJeff Mahoney wait_current_trans(fs_info); 76937d1aeeeSChris Mason } 77037d1aeeeSChris Mason 7712ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7728929ecfaSYan, Zheng { 7732ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7740b246afaSJeff Mahoney 7757c777430SJosef Bacik if (btrfs_check_space_for_delayed_refs(trans, fs_info)) 7761be41b78SJosef Bacik return 1; 77736ba022aSJosef Bacik 7782ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 7798929ecfaSYan, Zheng } 7808929ecfaSYan, Zheng 7813a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 7828929ecfaSYan, Zheng { 7838929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7848929ecfaSYan, Zheng int updates; 78549b25e05SJeff Mahoney int err; 7868929ecfaSYan, Zheng 787a4abeea4SJosef Bacik smp_mb(); 7884a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7894a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7908929ecfaSYan, Zheng return 1; 7918929ecfaSYan, Zheng 7928929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 7938929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 79449b25e05SJeff Mahoney if (updates) { 795c79a70b1SNikolay Borisov err = btrfs_run_delayed_refs(trans, updates * 2); 79649b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 79749b25e05SJeff Mahoney return err; 79849b25e05SJeff Mahoney } 7998929ecfaSYan, Zheng 8002ff7e61eSJeff Mahoney return should_end_transaction(trans); 8018929ecfaSYan, Zheng } 8028929ecfaSYan, Zheng 803dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) 804dc60c525SNikolay Borisov 8050e34693fSNikolay Borisov { 806dc60c525SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 807dc60c525SNikolay Borisov 8080e34693fSNikolay Borisov if (!trans->block_rsv) { 8090e34693fSNikolay Borisov ASSERT(!trans->bytes_reserved); 8100e34693fSNikolay Borisov return; 8110e34693fSNikolay Borisov } 8120e34693fSNikolay Borisov 8130e34693fSNikolay Borisov if (!trans->bytes_reserved) 8140e34693fSNikolay Borisov return; 8150e34693fSNikolay Borisov 8160e34693fSNikolay Borisov ASSERT(trans->block_rsv == &fs_info->trans_block_rsv); 8170e34693fSNikolay Borisov trace_btrfs_space_reservation(fs_info, "transaction", 8180e34693fSNikolay Borisov trans->transid, trans->bytes_reserved, 0); 8190e34693fSNikolay Borisov btrfs_block_rsv_release(fs_info, trans->block_rsv, 8200e34693fSNikolay Borisov trans->bytes_reserved); 8210e34693fSNikolay Borisov trans->bytes_reserved = 0; 8220e34693fSNikolay Borisov } 8230e34693fSNikolay Borisov 82489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 8253a45bb20SJeff Mahoney int throttle) 82679154b1bSChris Mason { 8273a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8288929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 82931b9655fSJosef Bacik u64 transid = trans->transid; 8301be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 831a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8324edc2ca3SDave Jones int err = 0; 833a79b7d4bSChris Mason int must_run_delayed_refs = 0; 834d6e4a428SChris Mason 835b50fff81SDavid Sterba if (refcount_read(&trans->use_count) > 1) { 836b50fff81SDavid Sterba refcount_dec(&trans->use_count); 8372a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8382a1eb461SJosef Bacik return 0; 8392a1eb461SJosef Bacik } 8402a1eb461SJosef Bacik 841dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8424c13d758SJosef Bacik trans->block_rsv = NULL; 843c5567237SArne Jansen 844ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 8456c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 846ea658badSJosef Bacik 847c3e69d58SChris Mason trans->delayed_ref_updates = 0; 848a79b7d4bSChris Mason if (!trans->sync) { 849a79b7d4bSChris Mason must_run_delayed_refs = 8502ff7e61eSJeff Mahoney btrfs_should_throttle_delayed_refs(trans, info); 8510a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 852a79b7d4bSChris Mason 853a79b7d4bSChris Mason /* 854a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 855a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 856a79b7d4bSChris Mason */ 857a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 858a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 859a79b7d4bSChris Mason must_run_delayed_refs = 2; 86056bec294SChris Mason } 861bb721703SChris Mason 862dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8630e721106SJosef Bacik trans->block_rsv = NULL; 86456bec294SChris Mason 865ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 8666c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 867ea658badSJosef Bacik 8684fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8694fbcdf66SFilipe Manana 87092e2f7e3SNikolay Borisov if (lock && should_end_transaction(trans) && 87120c7bcecSSeraphime Kirkovski READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8724a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8734a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8744a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8754a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 876a4abeea4SJosef Bacik } 8778929ecfaSYan, Zheng 87820c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8793bbb24b2SJosef Bacik if (throttle) 8803a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8813bbb24b2SJosef Bacik else 8828929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8838929ecfaSYan, Zheng } 8848929ecfaSYan, Zheng 8850860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8860b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8876df7881aSJosef Bacik 8888929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 88913c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 89013c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8910860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 89289ce8a63SChris Mason 893a83342aaSDavid Sterba /* 894a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 895a83342aaSDavid Sterba */ 89699d16cbcSSage Weil smp_mb(); 89779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 89879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 899724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 9009ed74f2dSJosef Bacik 9019ed74f2dSJosef Bacik if (current->journal_info == trans) 9029ed74f2dSJosef Bacik current->journal_info = NULL; 903ab78c84dSChris Mason 90424bbcf04SYan, Zheng if (throttle) 9052ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 90624bbcf04SYan, Zheng 90749b25e05SJeff Mahoney if (trans->aborted || 9080b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 9094e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 9104edc2ca3SDave Jones err = -EIO; 9114e121c06SJosef Bacik } 91249b25e05SJeff Mahoney 9134edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 914a79b7d4bSChris Mason if (must_run_delayed_refs) { 9152ff7e61eSJeff Mahoney btrfs_async_run_delayed_refs(info, cur, transid, 916a79b7d4bSChris Mason must_run_delayed_refs == 1); 917a79b7d4bSChris Mason } 9184edc2ca3SDave Jones return err; 91979154b1bSChris Mason } 92079154b1bSChris Mason 9213a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 92289ce8a63SChris Mason { 9233a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 92489ce8a63SChris Mason } 92589ce8a63SChris Mason 9263a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 92789ce8a63SChris Mason { 9283a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 92916cdcec7SMiao Xie } 93016cdcec7SMiao Xie 931d352ac68SChris Mason /* 932d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 933d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 934690587d1SChris Mason * those extents are sent to disk but does not wait on them 935d352ac68SChris Mason */ 9362ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 9378cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 93879154b1bSChris Mason { 939777e6bd7SChris Mason int err = 0; 9407c4452b9SChris Mason int werr = 0; 9410b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 942e6138876SJosef Bacik struct extent_state *cached_state = NULL; 943777e6bd7SChris Mason u64 start = 0; 9445f39d397SChris Mason u64 end; 9457c4452b9SChris Mason 9466300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 9471728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 948e6138876SJosef Bacik mark, &cached_state)) { 949663dfbb0SFilipe Manana bool wait_writeback = false; 950663dfbb0SFilipe Manana 951663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 952663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 953210aa277SDavid Sterba mark, &cached_state); 954663dfbb0SFilipe Manana /* 955663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 956663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 957663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 958663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 959bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 960bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 961bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 962bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 963bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 964663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 965663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 966663dfbb0SFilipe Manana */ 967663dfbb0SFilipe Manana if (err == -ENOMEM) { 968663dfbb0SFilipe Manana err = 0; 969663dfbb0SFilipe Manana wait_writeback = true; 970663dfbb0SFilipe Manana } 971663dfbb0SFilipe Manana if (!err) 9721728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9737c4452b9SChris Mason if (err) 9747c4452b9SChris Mason werr = err; 975663dfbb0SFilipe Manana else if (wait_writeback) 976663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 977e38e2ed7SFilipe Manana free_extent_state(cached_state); 978663dfbb0SFilipe Manana cached_state = NULL; 9791728366eSJosef Bacik cond_resched(); 9801728366eSJosef Bacik start = end + 1; 9817c4452b9SChris Mason } 9826300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 983690587d1SChris Mason return werr; 984690587d1SChris Mason } 985690587d1SChris Mason 986690587d1SChris Mason /* 987690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 988690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 989690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 990690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 991690587d1SChris Mason */ 992bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 993bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 994690587d1SChris Mason { 995690587d1SChris Mason int err = 0; 996690587d1SChris Mason int werr = 0; 9970b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 998e6138876SJosef Bacik struct extent_state *cached_state = NULL; 999690587d1SChris Mason u64 start = 0; 1000690587d1SChris Mason u64 end; 1001690587d1SChris Mason 10021728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 1003e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 1004663dfbb0SFilipe Manana /* 1005663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 1006663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 1007663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1008663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1009663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1010663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1011663dfbb0SFilipe Manana */ 1012663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1013ae0f1625SDavid Sterba EXTENT_NEED_WAIT, 0, 0, &cached_state); 1014663dfbb0SFilipe Manana if (err == -ENOMEM) 1015663dfbb0SFilipe Manana err = 0; 1016663dfbb0SFilipe Manana if (!err) 10171728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1018777e6bd7SChris Mason if (err) 1019777e6bd7SChris Mason werr = err; 1020e38e2ed7SFilipe Manana free_extent_state(cached_state); 1021e38e2ed7SFilipe Manana cached_state = NULL; 1022777e6bd7SChris Mason cond_resched(); 10231728366eSJosef Bacik start = end + 1; 1024777e6bd7SChris Mason } 10257c4452b9SChris Mason if (err) 10267c4452b9SChris Mason werr = err; 1027bf89d38fSJeff Mahoney return werr; 1028bf89d38fSJeff Mahoney } 1029656f30dbSFilipe Manana 1030bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 1031bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 1032bf89d38fSJeff Mahoney { 1033bf89d38fSJeff Mahoney bool errors = false; 1034bf89d38fSJeff Mahoney int err; 1035bf89d38fSJeff Mahoney 1036bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1037bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 1038bf89d38fSJeff Mahoney errors = true; 1039bf89d38fSJeff Mahoney 1040bf89d38fSJeff Mahoney if (errors && !err) 1041bf89d38fSJeff Mahoney err = -EIO; 1042bf89d38fSJeff Mahoney return err; 1043bf89d38fSJeff Mahoney } 1044bf89d38fSJeff Mahoney 1045bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 1046bf89d38fSJeff Mahoney { 1047bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 1048bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 1049bf89d38fSJeff Mahoney bool errors = false; 1050bf89d38fSJeff Mahoney int err; 1051bf89d38fSJeff Mahoney 1052bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 1053bf89d38fSJeff Mahoney 1054bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1055656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10560b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1057656f30dbSFilipe Manana errors = true; 1058656f30dbSFilipe Manana 1059656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10600b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1061656f30dbSFilipe Manana errors = true; 1062656f30dbSFilipe Manana 1063bf89d38fSJeff Mahoney if (errors && !err) 1064bf89d38fSJeff Mahoney err = -EIO; 1065bf89d38fSJeff Mahoney return err; 106679154b1bSChris Mason } 106779154b1bSChris Mason 1068690587d1SChris Mason /* 1069c9b577c0SNikolay Borisov * When btree blocks are allocated the corresponding extents are marked dirty. 1070c9b577c0SNikolay Borisov * This function ensures such extents are persisted on disk for transaction or 1071c9b577c0SNikolay Borisov * log commit. 1072c9b577c0SNikolay Borisov * 1073c9b577c0SNikolay Borisov * @trans: transaction whose dirty pages we'd like to write 1074690587d1SChris Mason */ 107570458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans) 1076d0c803c4SChris Mason { 1077663dfbb0SFilipe Manana int ret; 1078c9b577c0SNikolay Borisov int ret2; 1079c9b577c0SNikolay Borisov struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages; 108070458a58SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1081c9b577c0SNikolay Borisov struct blk_plug plug; 1082663dfbb0SFilipe Manana 1083c9b577c0SNikolay Borisov blk_start_plug(&plug); 1084c9b577c0SNikolay Borisov ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY); 1085c9b577c0SNikolay Borisov blk_finish_plug(&plug); 1086c9b577c0SNikolay Borisov ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1087c9b577c0SNikolay Borisov 1088663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1089663dfbb0SFilipe Manana 1090c9b577c0SNikolay Borisov if (ret) 1091663dfbb0SFilipe Manana return ret; 1092c9b577c0SNikolay Borisov else if (ret2) 1093c9b577c0SNikolay Borisov return ret2; 1094c9b577c0SNikolay Borisov else 1095c9b577c0SNikolay Borisov return 0; 1096d0c803c4SChris Mason } 1097d0c803c4SChris Mason 1098d352ac68SChris Mason /* 1099d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1100d352ac68SChris Mason * 1101d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1102d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1103d352ac68SChris Mason * allocation tree. 1104d352ac68SChris Mason * 1105d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1106d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1107d352ac68SChris Mason */ 11080b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 110979154b1bSChris Mason struct btrfs_root *root) 111079154b1bSChris Mason { 111179154b1bSChris Mason int ret; 11120b86a832SChris Mason u64 old_root_bytenr; 111386b9f2ecSYan, Zheng u64 old_root_used; 11140b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11150b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 111679154b1bSChris Mason 111786b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 111856bec294SChris Mason 111979154b1bSChris Mason while (1) { 11200b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 112186b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1122ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 112379154b1bSChris Mason break; 112487ef2bb4SChris Mason 11255d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 112679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11270b86a832SChris Mason &root->root_key, 11280b86a832SChris Mason &root->root_item); 112949b25e05SJeff Mahoney if (ret) 113049b25e05SJeff Mahoney return ret; 113156bec294SChris Mason 113286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1133e7070be1SJosef Bacik } 1134276e680dSYan Zheng 11350b86a832SChris Mason return 0; 11360b86a832SChris Mason } 11370b86a832SChris Mason 1138d352ac68SChris Mason /* 1139d352ac68SChris Mason * update all the cowonly tree roots on disk 114049b25e05SJeff Mahoney * 114149b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 114249b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 114349b25e05SJeff Mahoney * to clean up the delayed refs. 1144d352ac68SChris Mason */ 11459386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) 11460b86a832SChris Mason { 11479386d8bcSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1148ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11491bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11500b86a832SChris Mason struct list_head *next; 115184234f3aSYan Zheng struct extent_buffer *eb; 115256bec294SChris Mason int ret; 115384234f3aSYan Zheng 115484234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 115549b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 115649b25e05SJeff Mahoney 0, &eb); 115784234f3aSYan Zheng btrfs_tree_unlock(eb); 115884234f3aSYan Zheng free_extent_buffer(eb); 11590b86a832SChris Mason 116049b25e05SJeff Mahoney if (ret) 116149b25e05SJeff Mahoney return ret; 116249b25e05SJeff Mahoney 1163c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 116449b25e05SJeff Mahoney if (ret) 116549b25e05SJeff Mahoney return ret; 116687ef2bb4SChris Mason 11670b246afaSJeff Mahoney ret = btrfs_run_dev_stats(trans, fs_info); 1168c16ce190SJosef Bacik if (ret) 1169c16ce190SJosef Bacik return ret; 11700b246afaSJeff Mahoney ret = btrfs_run_dev_replace(trans, fs_info); 1171c16ce190SJosef Bacik if (ret) 1172c16ce190SJosef Bacik return ret; 11730b246afaSJeff Mahoney ret = btrfs_run_qgroups(trans, fs_info); 1174c16ce190SJosef Bacik if (ret) 1175c16ce190SJosef Bacik return ret; 1176546adb0dSJan Schmidt 11772ff7e61eSJeff Mahoney ret = btrfs_setup_space_cache(trans, fs_info); 1178dcdf7f6dSJosef Bacik if (ret) 1179dcdf7f6dSJosef Bacik return ret; 1180dcdf7f6dSJosef Bacik 1181546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1182c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1183c16ce190SJosef Bacik if (ret) 1184c16ce190SJosef Bacik return ret; 1185ea526d18SJosef Bacik again: 11860b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11872ff7e61eSJeff Mahoney struct btrfs_root *root; 11880b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11890b86a832SChris Mason list_del_init(next); 11900b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1191e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 119287ef2bb4SChris Mason 11939e351cc8SJosef Bacik if (root != fs_info->extent_root) 11949e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11959e351cc8SJosef Bacik &trans->transaction->switch_commits); 119649b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 119749b25e05SJeff Mahoney if (ret) 119849b25e05SJeff Mahoney return ret; 1199c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1200ea526d18SJosef Bacik if (ret) 1201ea526d18SJosef Bacik return ret; 120279154b1bSChris Mason } 1203276e680dSYan Zheng 12041bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 12052ff7e61eSJeff Mahoney ret = btrfs_write_dirty_block_groups(trans, fs_info); 1206ea526d18SJosef Bacik if (ret) 1207ea526d18SJosef Bacik return ret; 1208c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1209ea526d18SJosef Bacik if (ret) 1210ea526d18SJosef Bacik return ret; 1211ea526d18SJosef Bacik } 1212ea526d18SJosef Bacik 1213ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1214ea526d18SJosef Bacik goto again; 1215ea526d18SJosef Bacik 12169e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 12179e351cc8SJosef Bacik &trans->transaction->switch_commits); 12188dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12198dabb742SStefan Behrens 122079154b1bSChris Mason return 0; 122179154b1bSChris Mason } 122279154b1bSChris Mason 1223d352ac68SChris Mason /* 1224d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1225d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1226d352ac68SChris Mason * be deleted 1227d352ac68SChris Mason */ 1228cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12295eda7b5eSChris Mason { 12300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12310b246afaSJeff Mahoney 12320b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1233cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12340b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 12350b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 12365eda7b5eSChris Mason } 12375eda7b5eSChris Mason 1238d352ac68SChris Mason /* 12395d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1240d352ac68SChris Mason */ 12417e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) 12420f7d52f4SChris Mason { 12437e4443d9SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 12440f7d52f4SChris Mason struct btrfs_root *gang[8]; 12450f7d52f4SChris Mason int i; 12460f7d52f4SChris Mason int ret; 124754aa1f4dSChris Mason int err = 0; 124854aa1f4dSChris Mason 1249a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12500f7d52f4SChris Mason while (1) { 12515d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12525d4f98a2SYan Zheng (void **)gang, 0, 12530f7d52f4SChris Mason ARRAY_SIZE(gang), 12540f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12550f7d52f4SChris Mason if (ret == 0) 12560f7d52f4SChris Mason break; 12570f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12585b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12595d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12602619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12610f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1262a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 126331153d81SYan Zheng 1264e02119d5SChris Mason btrfs_free_log(trans, root); 12655d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1266d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1267e02119d5SChris Mason 126882d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 126982d5902dSLi Zefan 1270f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 127127cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1272c7548af6SChris Mason smp_mb__after_atomic(); 1273f1ebcc74SLiu Bo 1274978d910dSYan Zheng if (root->commit_root != root->node) { 12759e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12769e351cc8SJosef Bacik &trans->transaction->switch_commits); 1277978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1278978d910dSYan Zheng root->node); 1279978d910dSYan Zheng } 128031153d81SYan Zheng 12815d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12820f7d52f4SChris Mason &root->root_key, 12830f7d52f4SChris Mason &root->root_item); 1284a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 128554aa1f4dSChris Mason if (err) 128654aa1f4dSChris Mason break; 12877174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12880f7d52f4SChris Mason } 12899f3a7427SChris Mason } 1290a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 129154aa1f4dSChris Mason return err; 12920f7d52f4SChris Mason } 12930f7d52f4SChris Mason 1294d352ac68SChris Mason /* 1295de78b51aSEric Sandeen * defrag a given btree. 1296de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1297d352ac68SChris Mason */ 1298de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1299e9d0b13bSChris Mason { 1300e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1301e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 13028929ecfaSYan, Zheng int ret; 1303e9d0b13bSChris Mason 130427cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1305e9d0b13bSChris Mason return 0; 13068929ecfaSYan, Zheng 13076b80053dSChris Mason while (1) { 13088929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 13098929ecfaSYan, Zheng if (IS_ERR(trans)) 13108929ecfaSYan, Zheng return PTR_ERR(trans); 13118929ecfaSYan, Zheng 1312de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 13138929ecfaSYan, Zheng 13143a45bb20SJeff Mahoney btrfs_end_transaction(trans); 13152ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1316e9d0b13bSChris Mason cond_resched(); 1317e9d0b13bSChris Mason 1318ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1319e9d0b13bSChris Mason break; 1320210549ebSDavid Sterba 1321ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1322ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1323210549ebSDavid Sterba ret = -EAGAIN; 1324210549ebSDavid Sterba break; 1325210549ebSDavid Sterba } 1326e9d0b13bSChris Mason } 132727cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13288929ecfaSYan, Zheng return ret; 1329e9d0b13bSChris Mason } 1330e9d0b13bSChris Mason 1331d352ac68SChris Mason /* 13326426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 13336426c7adSQu Wenruo * 13346426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 13356426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13366426c7adSQu Wenruo * qgroup works. 13376426c7adSQu Wenruo */ 13386426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13396426c7adSQu Wenruo struct btrfs_root *src, 13406426c7adSQu Wenruo struct btrfs_root *parent, 13416426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13426426c7adSQu Wenruo u64 dst_objectid) 13436426c7adSQu Wenruo { 13446426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13456426c7adSQu Wenruo int ret; 13466426c7adSQu Wenruo 13476426c7adSQu Wenruo /* 13486426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13496426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13506426c7adSQu Wenruo * kick in anyway. 13516426c7adSQu Wenruo */ 13529ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 13536426c7adSQu Wenruo return 0; 13546426c7adSQu Wenruo 13556426c7adSQu Wenruo /* 13564d31778aSQu Wenruo * Ensure dirty @src will be commited. Or, after comming 13574d31778aSQu Wenruo * commit_fs_roots() and switch_commit_roots(), any dirty but not 13584d31778aSQu Wenruo * recorded root will never be updated again, causing an outdated root 13594d31778aSQu Wenruo * item. 13604d31778aSQu Wenruo */ 13614d31778aSQu Wenruo record_root_in_trans(trans, src, 1); 13624d31778aSQu Wenruo 13634d31778aSQu Wenruo /* 13646426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13656426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13666426c7adSQu Wenruo */ 13676426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13686426c7adSQu Wenruo 13697e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 13706426c7adSQu Wenruo if (ret) 13716426c7adSQu Wenruo goto out; 1372*460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 13736426c7adSQu Wenruo if (ret < 0) 13746426c7adSQu Wenruo goto out; 13756426c7adSQu Wenruo 13766426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 13776426c7adSQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 13786426c7adSQu Wenruo src->root_key.objectid, dst_objectid, 13796426c7adSQu Wenruo inherit); 13806426c7adSQu Wenruo if (ret < 0) 13816426c7adSQu Wenruo goto out; 13826426c7adSQu Wenruo 13836426c7adSQu Wenruo /* 13846426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13856426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13866426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13876426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13886426c7adSQu Wenruo * 2) write all btree blocks onto disk 13896426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13906426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13916426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13926426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13936426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13946426c7adSQu Wenruo */ 13959386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 13966426c7adSQu Wenruo if (ret) 13976426c7adSQu Wenruo goto out; 139816916a88SNikolay Borisov switch_commit_roots(trans->transaction); 139970458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 14006426c7adSQu Wenruo if (ret) 1401f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 14026426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 14036426c7adSQu Wenruo 14046426c7adSQu Wenruo out: 14056426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 14066426c7adSQu Wenruo 14076426c7adSQu Wenruo /* 14086426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 14096426c7adSQu Wenruo * last_trans == cur_transid. 14106426c7adSQu Wenruo * Or it won't be committed again onto disk after later 14116426c7adSQu Wenruo * insert_dir_item() 14126426c7adSQu Wenruo */ 14136426c7adSQu Wenruo if (!ret) 14146426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 14156426c7adSQu Wenruo return ret; 14166426c7adSQu Wenruo } 14176426c7adSQu Wenruo 14186426c7adSQu Wenruo /* 1419d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1420aec8030aSMiao Xie * transaction commit. This does the actual creation. 1421aec8030aSMiao Xie * 1422aec8030aSMiao Xie * Note: 1423aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1424aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1425aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1426d352ac68SChris Mason */ 142780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 14283063d29fSChris Mason struct btrfs_pending_snapshot *pending) 14293063d29fSChris Mason { 143008d50ca3SNikolay Borisov 143108d50ca3SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 14323063d29fSChris Mason struct btrfs_key key; 143380b6794dSChris Mason struct btrfs_root_item *new_root_item; 14343063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 14353063d29fSChris Mason struct btrfs_root *root = pending->root; 14366bdb72deSSage Weil struct btrfs_root *parent_root; 143798c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14386bdb72deSSage Weil struct inode *parent_inode; 143942874b3dSMiao Xie struct btrfs_path *path; 144042874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1441a22285a6SYan, Zheng struct dentry *dentry; 14423063d29fSChris Mason struct extent_buffer *tmp; 1443925baeddSChris Mason struct extent_buffer *old; 144404b285f3SDeepa Dinamani struct timespec cur_time; 1445aec8030aSMiao Xie int ret = 0; 1446d68fc57bSYan, Zheng u64 to_reserve = 0; 14476bdb72deSSage Weil u64 index = 0; 1448a22285a6SYan, Zheng u64 objectid; 1449b83cc969SLi Zefan u64 root_flags; 14508ea05e3aSAlexander Block uuid_le new_uuid; 14513063d29fSChris Mason 14528546b570SDavid Sterba ASSERT(pending->path); 14538546b570SDavid Sterba path = pending->path; 145442874b3dSMiao Xie 1455b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1456b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1457a22285a6SYan, Zheng 1458aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1459aec8030aSMiao Xie if (pending->error) 14606fa9700eSMiao Xie goto no_free_objectid; 14613063d29fSChris Mason 1462d6726335SQu Wenruo /* 1463d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1464d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1465d6726335SQu Wenruo */ 1466d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1467d6726335SQu Wenruo 1468147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1469d68fc57bSYan, Zheng 1470d68fc57bSYan, Zheng if (to_reserve > 0) { 1471aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1472aec8030aSMiao Xie &pending->block_rsv, 147308e007d2SMiao Xie to_reserve, 147408e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1475aec8030aSMiao Xie if (pending->error) 1476d6726335SQu Wenruo goto clear_skip_qgroup; 1477d68fc57bSYan, Zheng } 1478d68fc57bSYan, Zheng 14793063d29fSChris Mason key.objectid = objectid; 1480a22285a6SYan, Zheng key.offset = (u64)-1; 1481a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14823063d29fSChris Mason 14836fa9700eSMiao Xie rsv = trans->block_rsv; 1484a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14852382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14860b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 148788d3a5aaSJosef Bacik trans->transid, 148888d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1489a22285a6SYan, Zheng dentry = pending->dentry; 1490e9662f70SMiao Xie parent_inode = pending->dir; 1491a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14926426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1493a22285a6SYan, Zheng 1494c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 149504b285f3SDeepa Dinamani 14966bdb72deSSage Weil /* 14976bdb72deSSage Weil * insert the directory item 14986bdb72deSSage Weil */ 1499877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 150049b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 150142874b3dSMiao Xie 150242874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 150342874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 15044a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 150542874b3dSMiao Xie dentry->d_name.name, 150642874b3dSMiao Xie dentry->d_name.len, 0); 150742874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1508fe66a05aSChris Mason pending->error = -EEXIST; 1509aec8030aSMiao Xie goto dir_item_existed; 151042874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 151142874b3dSMiao Xie ret = PTR_ERR(dir_item); 151266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15138732d44fSMiao Xie goto fail; 151479787eaaSJeff Mahoney } 151542874b3dSMiao Xie btrfs_release_path(path); 15166bdb72deSSage Weil 1517e999376fSChris Mason /* 1518e999376fSChris Mason * pull in the delayed directory update 1519e999376fSChris Mason * and the delayed inode item 1520e999376fSChris Mason * otherwise we corrupt the FS during 1521e999376fSChris Mason * snapshot 1522e999376fSChris Mason */ 1523e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 15248732d44fSMiao Xie if (ret) { /* Transaction aborted */ 152566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15268732d44fSMiao Xie goto fail; 15278732d44fSMiao Xie } 1528e999376fSChris Mason 15296426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 15306bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 15316bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 153208fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 15336bdb72deSSage Weil 1534b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1535b83cc969SLi Zefan if (pending->readonly) 1536b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1537b83cc969SLi Zefan else 1538b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1539b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1540b83cc969SLi Zefan 15418ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15428ea05e3aSAlexander Block trans->transid); 15438ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15448ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15458ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15468ea05e3aSAlexander Block BTRFS_UUID_SIZE); 154770023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 154870023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 154970023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15508ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15518ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15528ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15538ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 155470023da2SStefan Behrens } 15553cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15563cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 155770023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15588ea05e3aSAlexander Block 1559925baeddSChris Mason old = btrfs_lock_root_node(root); 156049b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 156179787eaaSJeff Mahoney if (ret) { 156279787eaaSJeff Mahoney btrfs_tree_unlock(old); 156379787eaaSJeff Mahoney free_extent_buffer(old); 156466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15658732d44fSMiao Xie goto fail; 156679787eaaSJeff Mahoney } 156749b25e05SJeff Mahoney 15685d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 15693063d29fSChris Mason 157049b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 157179787eaaSJeff Mahoney /* clean up in any case */ 1572925baeddSChris Mason btrfs_tree_unlock(old); 1573925baeddSChris Mason free_extent_buffer(old); 15748732d44fSMiao Xie if (ret) { 157566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15768732d44fSMiao Xie goto fail; 15778732d44fSMiao Xie } 1578f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 157927cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1580f1ebcc74SLiu Bo smp_wmb(); 1581f1ebcc74SLiu Bo 15825d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1583a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1584a22285a6SYan, Zheng key.offset = trans->transid; 1585a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1586925baeddSChris Mason btrfs_tree_unlock(tmp); 15873063d29fSChris Mason free_extent_buffer(tmp); 15888732d44fSMiao Xie if (ret) { 158966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15908732d44fSMiao Xie goto fail; 15918732d44fSMiao Xie } 15920660b5afSChris Mason 1593a22285a6SYan, Zheng /* 1594a22285a6SYan, Zheng * insert root back/forward references 1595a22285a6SYan, Zheng */ 15966bccf3abSJeff Mahoney ret = btrfs_add_root_ref(trans, fs_info, objectid, 1597a22285a6SYan, Zheng parent_root->root_key.objectid, 15984a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1599a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 16008732d44fSMiao Xie if (ret) { 160166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16028732d44fSMiao Xie goto fail; 16038732d44fSMiao Xie } 1604a22285a6SYan, Zheng 1605a22285a6SYan, Zheng key.offset = (u64)-1; 16060b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 160779787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 160879787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 160966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16108732d44fSMiao Xie goto fail; 161179787eaaSJeff Mahoney } 1612d68fc57bSYan, Zheng 161349b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 16148732d44fSMiao Xie if (ret) { 161566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16168732d44fSMiao Xie goto fail; 16178732d44fSMiao Xie } 1618361048f5SMiao Xie 1619c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 16208732d44fSMiao Xie if (ret) { 162166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16228732d44fSMiao Xie goto fail; 16238732d44fSMiao Xie } 162442874b3dSMiao Xie 16256426c7adSQu Wenruo /* 16266426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 16276426c7adSQu Wenruo * snapshot hack to do fast snapshot. 16286426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 16296426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 16306426c7adSQu Wenruo */ 16316426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 16326426c7adSQu Wenruo pending->inherit, objectid); 16336426c7adSQu Wenruo if (ret < 0) 16346426c7adSQu Wenruo goto fail; 16356426c7adSQu Wenruo 163642874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 163742874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 16388e7611cfSNikolay Borisov BTRFS_I(parent_inode), &key, 163942874b3dSMiao Xie BTRFS_FT_DIR, index); 164042874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16419c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16428732d44fSMiao Xie if (ret) { 164366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16448732d44fSMiao Xie goto fail; 16458732d44fSMiao Xie } 164642874b3dSMiao Xie 16476ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 164842874b3dSMiao Xie dentry->d_name.len * 2); 164904b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1650c2050a45SDeepa Dinamani current_time(parent_inode); 1651be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1652dd5f9615SStefan Behrens if (ret) { 165366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1654dd5f9615SStefan Behrens goto fail; 1655dd5f9615SStefan Behrens } 16566bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, new_uuid.b, 1657dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1658dd5f9615SStefan Behrens if (ret) { 165966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1660dd5f9615SStefan Behrens goto fail; 1661dd5f9615SStefan Behrens } 1662dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 16636bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, 1664dd5f9615SStefan Behrens new_root_item->received_uuid, 1665dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1666dd5f9615SStefan Behrens objectid); 1667dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 166866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1669dd5f9615SStefan Behrens goto fail; 1670dd5f9615SStefan Behrens } 1671dd5f9615SStefan Behrens } 1672d6726335SQu Wenruo 1673c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1674d6726335SQu Wenruo if (ret) { 167566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1676d6726335SQu Wenruo goto fail; 1677d6726335SQu Wenruo } 1678d6726335SQu Wenruo 16793063d29fSChris Mason fail: 1680aec8030aSMiao Xie pending->error = ret; 1681aec8030aSMiao Xie dir_item_existed: 168298c9942aSLiu Bo trans->block_rsv = rsv; 16832382c5ccSLiu Bo trans->bytes_reserved = 0; 1684d6726335SQu Wenruo clear_skip_qgroup: 1685d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16866fa9700eSMiao Xie no_free_objectid: 16876fa9700eSMiao Xie kfree(new_root_item); 1688b0c0ea63SDavid Sterba pending->root_item = NULL; 168942874b3dSMiao Xie btrfs_free_path(path); 16908546b570SDavid Sterba pending->path = NULL; 16918546b570SDavid Sterba 169249b25e05SJeff Mahoney return ret; 16933063d29fSChris Mason } 16943063d29fSChris Mason 1695d352ac68SChris Mason /* 1696d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1697d352ac68SChris Mason */ 169808d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans) 16993063d29fSChris Mason { 1700aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 17013063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1702aec8030aSMiao Xie int ret = 0; 17033de4586cSChris Mason 1704aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1705aec8030aSMiao Xie list_del(&pending->list); 170608d50ca3SNikolay Borisov ret = create_pending_snapshot(trans, pending); 1707aec8030aSMiao Xie if (ret) 1708aec8030aSMiao Xie break; 1709aec8030aSMiao Xie } 1710aec8030aSMiao Xie return ret; 17113de4586cSChris Mason } 17123de4586cSChris Mason 17132ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 17145d4f98a2SYan Zheng { 17155d4f98a2SYan Zheng struct btrfs_root_item *root_item; 17165d4f98a2SYan Zheng struct btrfs_super_block *super; 17175d4f98a2SYan Zheng 17180b246afaSJeff Mahoney super = fs_info->super_copy; 17195d4f98a2SYan Zheng 17200b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 1721093e037cSDavid Sterba super->chunk_root = root_item->bytenr; 1722093e037cSDavid Sterba super->chunk_root_generation = root_item->generation; 1723093e037cSDavid Sterba super->chunk_root_level = root_item->level; 17245d4f98a2SYan Zheng 17250b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 1726093e037cSDavid Sterba super->root = root_item->bytenr; 1727093e037cSDavid Sterba super->generation = root_item->generation; 1728093e037cSDavid Sterba super->root_level = root_item->level; 17290b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 1730093e037cSDavid Sterba super->cache_generation = root_item->generation; 17310b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 1732093e037cSDavid Sterba super->uuid_tree_generation = root_item->generation; 17335d4f98a2SYan Zheng } 17345d4f98a2SYan Zheng 1735f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1736f36f3042SChris Mason { 17374a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1738f36f3042SChris Mason int ret = 0; 17394a9d8bdeSMiao Xie 1740a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17414a9d8bdeSMiao Xie trans = info->running_transaction; 17424a9d8bdeSMiao Xie if (trans) 17434a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1744a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1745f36f3042SChris Mason return ret; 1746f36f3042SChris Mason } 1747f36f3042SChris Mason 17488929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17498929ecfaSYan, Zheng { 17504a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17518929ecfaSYan, Zheng int ret = 0; 17524a9d8bdeSMiao Xie 1753a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17544a9d8bdeSMiao Xie trans = info->running_transaction; 17554a9d8bdeSMiao Xie if (trans) 17564a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1757a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17588929ecfaSYan, Zheng return ret; 17598929ecfaSYan, Zheng } 17608929ecfaSYan, Zheng 1761bb9c12c9SSage Weil /* 1762bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1763bb9c12c9SSage Weil * transaction joins 1764bb9c12c9SSage Weil */ 17652ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1766bb9c12c9SSage Weil struct btrfs_transaction *trans) 1767bb9c12c9SSage Weil { 17682ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 17692ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1770bb9c12c9SSage Weil } 1771bb9c12c9SSage Weil 1772bb9c12c9SSage Weil /* 1773bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1774bb9c12c9SSage Weil * caller holds ref. 1775bb9c12c9SSage Weil */ 17762ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17772ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1778bb9c12c9SSage Weil struct btrfs_transaction *trans) 1779bb9c12c9SSage Weil { 17802ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17812ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1782bb9c12c9SSage Weil } 1783bb9c12c9SSage Weil 1784bb9c12c9SSage Weil /* 1785bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1786bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1787bb9c12c9SSage Weil */ 1788bb9c12c9SSage Weil struct btrfs_async_commit { 1789bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17907892b5afSMiao Xie struct work_struct work; 1791bb9c12c9SSage Weil }; 1792bb9c12c9SSage Weil 1793bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1794bb9c12c9SSage Weil { 1795bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17967892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1797bb9c12c9SSage Weil 17986fc4e354SSage Weil /* 17996fc4e354SSage Weil * We've got freeze protection passed with the transaction. 18006fc4e354SSage Weil * Tell lockdep about it. 18016fc4e354SSage Weil */ 1802b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18033a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 18046fc4e354SSage Weil 1805e209db7aSSage Weil current->journal_info = ac->newtrans; 1806e209db7aSSage Weil 18073a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1808bb9c12c9SSage Weil kfree(ac); 1809bb9c12c9SSage Weil } 1810bb9c12c9SSage Weil 1811bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1812bb9c12c9SSage Weil int wait_for_unblock) 1813bb9c12c9SSage Weil { 18143a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1815bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1816bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1817bb9c12c9SSage Weil 1818bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1819db5b493aSTsutomu Itoh if (!ac) 1820db5b493aSTsutomu Itoh return -ENOMEM; 1821bb9c12c9SSage Weil 18227892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 18233a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 18243612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 18253612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 18263612b495STsutomu Itoh kfree(ac); 18273612b495STsutomu Itoh return err; 18283612b495STsutomu Itoh } 1829bb9c12c9SSage Weil 1830bb9c12c9SSage Weil /* take transaction reference */ 1831bb9c12c9SSage Weil cur_trans = trans->transaction; 18329b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1833bb9c12c9SSage Weil 18343a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18356fc4e354SSage Weil 18366fc4e354SSage Weil /* 18376fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 18386fc4e354SSage Weil * async commit thread will be the one to unlock it. 18396fc4e354SSage Weil */ 1840b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18410b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 18426fc4e354SSage Weil 18437892b5afSMiao Xie schedule_work(&ac->work); 1844bb9c12c9SSage Weil 1845bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1846bb9c12c9SSage Weil if (wait_for_unblock) 18472ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1848bb9c12c9SSage Weil else 18492ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1850bb9c12c9SSage Weil 185138e88054SSage Weil if (current->journal_info == trans) 185238e88054SSage Weil current->journal_info = NULL; 185338e88054SSage Weil 1854724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1855bb9c12c9SSage Weil return 0; 1856bb9c12c9SSage Weil } 1857bb9c12c9SSage Weil 185849b25e05SJeff Mahoney 185997cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) 186049b25e05SJeff Mahoney { 186197cb39bbSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 186249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1863f094ac32SLiu Bo DEFINE_WAIT(wait); 186449b25e05SJeff Mahoney 1865b50fff81SDavid Sterba WARN_ON(refcount_read(&trans->use_count) > 1); 186649b25e05SJeff Mahoney 186766642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18687b8b92afSJosef Bacik 18690b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 187066b6135bSLiu Bo 187125d8c284SMiao Xie /* 187225d8c284SMiao Xie * If the transaction is removed from the list, it means this 187325d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 187425d8c284SMiao Xie * to call the cleanup function. 187525d8c284SMiao Xie */ 187625d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 187766b6135bSLiu Bo 187849b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18790b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18810b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1882f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1883f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1884f094ac32SLiu Bo 18850b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1886d7096fc3SJosef Bacik } 18870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 188849b25e05SJeff Mahoney 18892ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 189049b25e05SJeff Mahoney 18910b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18920b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18930b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18940b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18954a9d8bdeSMiao Xie 1896e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18970b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1898724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1899724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 190049b25e05SJeff Mahoney 190197cb39bbSNikolay Borisov trace_btrfs_transaction_commit(trans->root); 190249b25e05SJeff Mahoney 190349b25e05SJeff Mahoney if (current->journal_info == trans) 190449b25e05SJeff Mahoney current->journal_info = NULL; 19050b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 190649b25e05SJeff Mahoney 190749b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 190849b25e05SJeff Mahoney } 190949b25e05SJeff Mahoney 191082436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 191182436617SMiao Xie { 1912ce8ea7ccSJosef Bacik /* 1913ce8ea7ccSJosef Bacik * We use writeback_inodes_sb here because if we used 1914ce8ea7ccSJosef Bacik * btrfs_start_delalloc_roots we would deadlock with fs freeze. 1915ce8ea7ccSJosef Bacik * Currently are holding the fs freeze lock, if we do an async flush 1916ce8ea7ccSJosef Bacik * we'll do btrfs_join_transaction() and deadlock because we need to 1917ce8ea7ccSJosef Bacik * wait for the fs freeze lock. Using the direct flushing we benefit 1918ce8ea7ccSJosef Bacik * from already being in a transaction and our join_transaction doesn't 1919ce8ea7ccSJosef Bacik * have to re-take the fs freeze lock. 1920ce8ea7ccSJosef Bacik */ 19213cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 1922ce8ea7ccSJosef Bacik writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); 192382436617SMiao Xie return 0; 192482436617SMiao Xie } 192582436617SMiao Xie 192682436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 192782436617SMiao Xie { 19283cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 19296374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 193082436617SMiao Xie } 193182436617SMiao Xie 193250d9aa99SJosef Bacik static inline void 1933161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 193450d9aa99SJosef Bacik { 1935161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1936161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 193750d9aa99SJosef Bacik } 193850d9aa99SJosef Bacik 19393a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 194079154b1bSChris Mason { 19413a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 194249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19438fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 194425287e0aSMiao Xie int ret; 194579154b1bSChris Mason 19468d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 194720c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 194825287e0aSMiao Xie ret = cur_trans->aborted; 19493a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1950e4a2bcacSJosef Bacik return ret; 195125287e0aSMiao Xie } 195249b25e05SJeff Mahoney 195356bec294SChris Mason /* make a pass through all the delayed refs we have so far 195456bec294SChris Mason * any runnings procs may add more while we are here 195556bec294SChris Mason */ 1956c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1957e4a2bcacSJosef Bacik if (ret) { 19583a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1959e4a2bcacSJosef Bacik return ret; 1960e4a2bcacSJosef Bacik } 196156bec294SChris Mason 1962dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 19630e721106SJosef Bacik trans->block_rsv = NULL; 19640e721106SJosef Bacik 1965b7ec40d7SChris Mason cur_trans = trans->transaction; 196649b25e05SJeff Mahoney 196756bec294SChris Mason /* 196856bec294SChris Mason * set the flushing flag so procs in this transaction have to 196956bec294SChris Mason * start sending their work down. 197056bec294SChris Mason */ 1971b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19721be41b78SJosef Bacik smp_wmb(); 197356bec294SChris Mason 1974ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 19756c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 1976ea658badSJosef Bacik 1977c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1978e4a2bcacSJosef Bacik if (ret) { 19793a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1980e4a2bcacSJosef Bacik return ret; 1981e4a2bcacSJosef Bacik } 198256bec294SChris Mason 19833204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19841bbc621eSChris Mason int run_it = 0; 19851bbc621eSChris Mason 19861bbc621eSChris Mason /* this mutex is also taken before trying to set 19871bbc621eSChris Mason * block groups readonly. We need to make sure 19881bbc621eSChris Mason * that nobody has set a block group readonly 19891bbc621eSChris Mason * after a extents from that block group have been 19901bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19911bbc621eSChris Mason * will wait for the transaction to commit if it 19923204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19931bbc621eSChris Mason * 19943204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19953204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19961bbc621eSChris Mason * hurt to have more than one go through, but there's no 19971bbc621eSChris Mason * real advantage to it either. 19981bbc621eSChris Mason */ 19990b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 20003204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 20013204d33cSJosef Bacik &cur_trans->flags)) 20021bbc621eSChris Mason run_it = 1; 20030b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 20041bbc621eSChris Mason 2005f9cacae3SNikolay Borisov if (run_it) { 200621217054SNikolay Borisov ret = btrfs_start_dirty_block_groups(trans); 20071bbc621eSChris Mason if (ret) { 20083a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20091bbc621eSChris Mason return ret; 20101bbc621eSChris Mason } 2011f9cacae3SNikolay Borisov } 2012f9cacae3SNikolay Borisov } 20131bbc621eSChris Mason 20140b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20154a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20160b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 20179b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 20183a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 2019ccd467d6SChris Mason 20202ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 202115ee9bc7SJosef Bacik 2022b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2023b4924a0fSLiu Bo ret = cur_trans->aborted; 2024b4924a0fSLiu Bo 2025724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 202615ee9bc7SJosef Bacik 202749b25e05SJeff Mahoney return ret; 202879154b1bSChris Mason } 20294313b399SChris Mason 20304a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20310b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2032bb9c12c9SSage Weil 20330b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2034ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2035ccd467d6SChris Mason struct btrfs_transaction, list); 20364a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20379b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20380b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2039ccd467d6SChris Mason 20402ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20411f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2042ccd467d6SChris Mason 2043724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20441f9b8c8fSFilipe Manana if (ret) 20451f9b8c8fSFilipe Manana goto cleanup_transaction; 2046a4abeea4SJosef Bacik } else { 20470b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2048ccd467d6SChris Mason } 2049a4abeea4SJosef Bacik } else { 20500b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2051ccd467d6SChris Mason } 205215ee9bc7SJosef Bacik 20530860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20540860adfdSMiao Xie 20550b246afaSJeff Mahoney ret = btrfs_start_delalloc_flush(fs_info); 205682436617SMiao Xie if (ret) 205782436617SMiao Xie goto cleanup_transaction; 205882436617SMiao Xie 2059e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 206049b25e05SJeff Mahoney if (ret) 206149b25e05SJeff Mahoney goto cleanup_transaction; 206216cdcec7SMiao Xie 2063581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2064581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2065ed3b3d31SChris Mason 2066581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 2067e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 2068ca469637SMiao Xie if (ret) 2069ca469637SMiao Xie goto cleanup_transaction; 2070ca469637SMiao Xie 20710b246afaSJeff Mahoney btrfs_wait_delalloc_flush(fs_info); 2072cb7ab021SWang Shilong 2073161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 207450d9aa99SJosef Bacik 20752ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2076ed0ca140SJosef Bacik /* 2077ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2078ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20794a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2080ed0ca140SJosef Bacik */ 20810b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20824a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20830b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2084ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2085ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 208615ee9bc7SJosef Bacik 20872cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 208820c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 20892cba30f1SMiao Xie ret = cur_trans->aborted; 20906cf7f77eSWang Shilong goto scrub_continue; 20912cba30f1SMiao Xie } 20927585717fSChris Mason /* 20937585717fSChris Mason * the reloc mutex makes sure that we stop 20947585717fSChris Mason * the balancing code from coming in and moving 20957585717fSChris Mason * extents around in the middle of the commit 20967585717fSChris Mason */ 20970b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 20987585717fSChris Mason 209942874b3dSMiao Xie /* 210042874b3dSMiao Xie * We needn't worry about the delayed items because we will 210142874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 210242874b3dSMiao Xie * core function of the snapshot creation. 210342874b3dSMiao Xie */ 210408d50ca3SNikolay Borisov ret = create_pending_snapshots(trans); 210549b25e05SJeff Mahoney if (ret) { 21060b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21076cf7f77eSWang Shilong goto scrub_continue; 210849b25e05SJeff Mahoney } 21093063d29fSChris Mason 211042874b3dSMiao Xie /* 211142874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 211242874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 211342874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 211442874b3dSMiao Xie * them. 211542874b3dSMiao Xie * 211642874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 211742874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 211842874b3dSMiao Xie * the nodes and leaves. 211942874b3dSMiao Xie */ 2120e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 212149b25e05SJeff Mahoney if (ret) { 21220b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21236cf7f77eSWang Shilong goto scrub_continue; 212449b25e05SJeff Mahoney } 212516cdcec7SMiao Xie 2126c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 212749b25e05SJeff Mahoney if (ret) { 21280b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21296cf7f77eSWang Shilong goto scrub_continue; 213049b25e05SJeff Mahoney } 213156bec294SChris Mason 2132e999376fSChris Mason /* 2133e999376fSChris Mason * make sure none of the code above managed to slip in a 2134e999376fSChris Mason * delayed item 2135e999376fSChris Mason */ 2136ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2137e999376fSChris Mason 21382c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2139dc17ff8fSChris Mason 2140e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2141e02119d5SChris Mason * various roots consistent with each other. Every pointer 2142e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2143e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2144e02119d5SChris Mason * the tree logging code from jumping in and changing any 2145e02119d5SChris Mason * of the trees. 2146e02119d5SChris Mason * 2147e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2148e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2149e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2150e02119d5SChris Mason * from now until after the super is written, we avoid races 2151e02119d5SChris Mason * with the tree-log code. 2152e02119d5SChris Mason */ 21530b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21541a40e23bSZheng Yan 21557e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 215649b25e05SJeff Mahoney if (ret) { 21570b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21580b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21596cf7f77eSWang Shilong goto scrub_continue; 216049b25e05SJeff Mahoney } 216154aa1f4dSChris Mason 21623818aea2SQu Wenruo /* 21637e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21647e1876acSDavid Sterba * before the next transaction. 21653818aea2SQu Wenruo */ 21660b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21673818aea2SQu Wenruo 21685d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2169e02119d5SChris Mason * safe to free the root of tree log roots 2170e02119d5SChris Mason */ 21710b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2172e02119d5SChris Mason 21730ed4792aSQu Wenruo /* 217482bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 217582bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 217682bafb38SQu Wenruo */ 2177c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 217882bafb38SQu Wenruo if (ret) { 217982bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 218082bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 218182bafb38SQu Wenruo goto scrub_continue; 218282bafb38SQu Wenruo } 218382bafb38SQu Wenruo 218482bafb38SQu Wenruo /* 21850ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21860ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21870ed4792aSQu Wenruo */ 2188*460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 21890ed4792aSQu Wenruo if (ret < 0) { 21900b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21910b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21920ed4792aSQu Wenruo goto scrub_continue; 21930ed4792aSQu Wenruo } 21940ed4792aSQu Wenruo 21959386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 219649b25e05SJeff Mahoney if (ret) { 21970b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21980b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21996cf7f77eSWang Shilong goto scrub_continue; 220049b25e05SJeff Mahoney } 220154aa1f4dSChris Mason 22022cba30f1SMiao Xie /* 22032cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 22042cba30f1SMiao Xie * update ->aborted, check it. 22052cba30f1SMiao Xie */ 220620c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 22072cba30f1SMiao Xie ret = cur_trans->aborted; 22080b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22090b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22106cf7f77eSWang Shilong goto scrub_continue; 22112cba30f1SMiao Xie } 22122cba30f1SMiao Xie 22138b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 221411833d66SYan Zheng 22150b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22165f39d397SChris Mason 22170b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22180b246afaSJeff Mahoney fs_info->tree_root->node); 22190b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22209e351cc8SJosef Bacik &cur_trans->switch_commits); 22215d4f98a2SYan Zheng 22220b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22230b246afaSJeff Mahoney fs_info->chunk_root->node); 22240b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22259e351cc8SJosef Bacik &cur_trans->switch_commits); 22269e351cc8SJosef Bacik 222716916a88SNikolay Borisov switch_commit_roots(cur_trans); 22285d4f98a2SYan Zheng 2229ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22301bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22312ff7e61eSJeff Mahoney update_super_roots(fs_info); 2232e02119d5SChris Mason 22330b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22340b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22350b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22360b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2237ccd467d6SChris Mason 22380b246afaSJeff Mahoney btrfs_update_commit_device_size(fs_info); 2239e9b919b1SNikolay Borisov btrfs_update_commit_device_bytes_used(cur_trans); 2240935e5cc9SMiao Xie 22410b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22420b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2243656f30dbSFilipe Manana 22444fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22454fbcdf66SFilipe Manana 22460b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22474a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22480b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22490b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22500b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2251b7ec40d7SChris Mason 22520b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2253e6dcd2dcSChris Mason 225470458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 225549b25e05SJeff Mahoney if (ret) { 22560b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 225708748810SDavid Sterba "Error while writing out transaction"); 22580b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22596cf7f77eSWang Shilong goto scrub_continue; 226049b25e05SJeff Mahoney } 226149b25e05SJeff Mahoney 2262eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 2263e02119d5SChris Mason /* 2264e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2265e02119d5SChris Mason * to go about their business 2266e02119d5SChris Mason */ 22670b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2268c1f32b7cSAnand Jain if (ret) 2269c1f32b7cSAnand Jain goto scrub_continue; 2270e02119d5SChris Mason 22712ff7e61eSJeff Mahoney btrfs_finish_extent_commit(trans, fs_info); 22724313b399SChris Mason 22733204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22740b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 227513212b54SZhao Lei 22760b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22774a9d8bdeSMiao Xie /* 22784a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22794a9d8bdeSMiao Xie * which can change it. 22804a9d8bdeSMiao Xie */ 22814a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22822c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 22833de4586cSChris Mason 22840b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 228513c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22860b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2287a4abeea4SJosef Bacik 2288724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2289724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 229058176a96SJosef Bacik 22910860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 22920b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2293b2b5ef5cSJan Kara 22943a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 22951abe9b8aSliubo 22962ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2297a2de733cSArne Jansen 22989ed74f2dSJosef Bacik if (current->journal_info == trans) 22999ed74f2dSJosef Bacik current->journal_info = NULL; 23009ed74f2dSJosef Bacik 23012c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 230224bbcf04SYan, Zheng 23039e7cc91aSWang Xiaoguang /* 23049e7cc91aSWang Xiaoguang * If fs has been frozen, we can not handle delayed iputs, otherwise 23059e7cc91aSWang Xiaoguang * it'll result in deadlock about SB_FREEZE_FS. 23069e7cc91aSWang Xiaoguang */ 23070b246afaSJeff Mahoney if (current != fs_info->transaction_kthread && 2308fac03c8dSDavid Sterba current != fs_info->cleaner_kthread && 2309fac03c8dSDavid Sterba !test_bit(BTRFS_FS_FROZEN, &fs_info->flags)) 23102ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 231124bbcf04SYan, Zheng 231279154b1bSChris Mason return ret; 231349b25e05SJeff Mahoney 23146cf7f77eSWang Shilong scrub_continue: 23152ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 231649b25e05SJeff Mahoney cleanup_transaction: 2317dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 23184fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 23190e721106SJosef Bacik trans->block_rsv = NULL; 23200b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 232149b25e05SJeff Mahoney if (current->journal_info == trans) 232249b25e05SJeff Mahoney current->journal_info = NULL; 232397cb39bbSNikolay Borisov cleanup_transaction(trans, ret); 232449b25e05SJeff Mahoney 232549b25e05SJeff Mahoney return ret; 232679154b1bSChris Mason } 232779154b1bSChris Mason 2328d352ac68SChris Mason /* 23299d1a2a3aSDavid Sterba * return < 0 if error 23309d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23319d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23329d1a2a3aSDavid Sterba * 23339d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23349d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23359d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23369d1a2a3aSDavid Sterba * few seconds later. 2337d352ac68SChris Mason */ 23389d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2339e9d0b13bSChris Mason { 23409d1a2a3aSDavid Sterba int ret; 23415d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2342e9d0b13bSChris Mason 2343a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23449d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23459d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23469d1a2a3aSDavid Sterba return 0; 23479d1a2a3aSDavid Sterba } 23489d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23499d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2350cfad392bSJosef Bacik list_del_init(&root->root_list); 2351a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23525d4f98a2SYan Zheng 2353ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); 235476dda93cSYan, Zheng 235516cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 235616cdcec7SMiao Xie 235776dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 235876dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23592c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 236076dda93cSYan, Zheng else 23612c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 236232471dc2SDavid Sterba 23636596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2364e9d0b13bSChris Mason } 2365572d9ab7SDavid Sterba 2366572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2367572d9ab7SDavid Sterba { 2368572d9ab7SDavid Sterba unsigned long prev; 2369572d9ab7SDavid Sterba unsigned long bit; 2370572d9ab7SDavid Sterba 23716c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2372572d9ab7SDavid Sterba if (!prev) 2373572d9ab7SDavid Sterba return; 2374572d9ab7SDavid Sterba 23757e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23767e1876acSDavid Sterba if (prev & bit) 23777e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23787e1876acSDavid Sterba prev &= ~bit; 23797e1876acSDavid Sterba 23807e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23817e1876acSDavid Sterba if (prev & bit) 23827e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23837e1876acSDavid Sterba prev &= ~bit; 23847e1876acSDavid Sterba 2385d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2386d51033d0SDavid Sterba if (prev & bit) 2387d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2388d51033d0SDavid Sterba prev &= ~bit; 2389d51033d0SDavid Sterba 2390572d9ab7SDavid Sterba if (prev) 2391572d9ab7SDavid Sterba btrfs_warn(fs_info, 2392572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2393572d9ab7SDavid Sterba } 2394