1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 679154b1bSChris Mason #include <linux/fs.h> 75a0e3ad6STejun Heo #include <linux/slab.h> 834088780SChris Mason #include <linux/sched.h> 9d3c2fdcfSChris Mason #include <linux/writeback.h> 105f39d397SChris Mason #include <linux/pagemap.h> 115f2cc086SChris Mason #include <linux/blkdev.h> 128ea05e3aSAlexander Block #include <linux/uuid.h> 1379154b1bSChris Mason #include "ctree.h" 1479154b1bSChris Mason #include "disk-io.h" 1579154b1bSChris Mason #include "transaction.h" 16925baeddSChris Mason #include "locking.h" 17e02119d5SChris Mason #include "tree-log.h" 18581bb050SLi Zefan #include "inode-map.h" 19733f4fbbSStefan Behrens #include "volumes.h" 208dabb742SStefan Behrens #include "dev-replace.h" 21fcebe456SJosef Bacik #include "qgroup.h" 2279154b1bSChris Mason 230f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 240f7d52f4SChris Mason 25e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 264a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 27bcf3a3e7SNikolay Borisov [TRANS_STATE_BLOCKED] = __TRANS_START, 28bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), 29bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | 304a9d8bdeSMiao Xie __TRANS_ATTACH | 314a9d8bdeSMiao Xie __TRANS_JOIN), 32bcf3a3e7SNikolay Borisov [TRANS_STATE_UNBLOCKED] = (__TRANS_START | 334a9d8bdeSMiao Xie __TRANS_ATTACH | 344a9d8bdeSMiao Xie __TRANS_JOIN | 354a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 36bcf3a3e7SNikolay Borisov [TRANS_STATE_COMPLETED] = (__TRANS_START | 374a9d8bdeSMiao Xie __TRANS_ATTACH | 384a9d8bdeSMiao Xie __TRANS_JOIN | 394a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 404a9d8bdeSMiao Xie }; 414a9d8bdeSMiao Xie 42724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 4379154b1bSChris Mason { 449b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 459b64f57dSElena Reshetova if (refcount_dec_and_test(&transaction->use_count)) { 46a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 475c9d028bSLiu Bo WARN_ON(!RB_EMPTY_ROOT( 485c9d028bSLiu Bo &transaction->delayed_refs.href_root.rb_root)); 491262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 50ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 51ab8d0fc4SJeff Mahoney "pending csums is %llu", 521262133bSJosef Bacik transaction->delayed_refs.pending_csums); 537785a663SFilipe Manana /* 547785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 557785a663SFilipe Manana * because the transaction was aborted and a commit did not 567785a663SFilipe Manana * happen (things failed before writing the new superblock 577785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 587785a663SFilipe Manana * discard the physical locations of the block groups. 597785a663SFilipe Manana */ 607785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 617785a663SFilipe Manana struct btrfs_block_group_cache *cache; 627785a663SFilipe Manana 637785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 647785a663SFilipe Manana struct btrfs_block_group_cache, 657785a663SFilipe Manana bg_list); 667785a663SFilipe Manana list_del_init(&cache->bg_list); 677785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 687785a663SFilipe Manana btrfs_put_block_group(cache); 697785a663SFilipe Manana } 70bbbf7243SNikolay Borisov WARN_ON(!list_empty(&transaction->dev_update_list)); 714b5faeacSDavid Sterba kfree(transaction); 7279154b1bSChris Mason } 7378fae27eSChris Mason } 7479154b1bSChris Mason 7516916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans) 76817d52f8SJosef Bacik { 7716916a88SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 789e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 799e351cc8SJosef Bacik 809e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 819e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 829e351cc8SJosef Bacik dirty_list) { 839e351cc8SJosef Bacik list_del_init(&root->dirty_list); 84817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 85817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 864fd786e6SMisono Tomohiro if (is_fstree(root->root_key.objectid)) 879e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 8841e7acd3SNikolay Borisov extent_io_tree_release(&root->dirty_log_pages); 89370a11b8SQu Wenruo btrfs_qgroup_clean_swapped_blocks(root); 909e351cc8SJosef Bacik } 912b9dbef2SJosef Bacik 922b9dbef2SJosef Bacik /* We can free old roots now. */ 932b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 942b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 952b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 962b9dbef2SJosef Bacik struct btrfs_root, root_list); 972b9dbef2SJosef Bacik list_del_init(&root->root_list); 982b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 992b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1002b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1012b9dbef2SJosef Bacik } 1022b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1039e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 104817d52f8SJosef Bacik } 105817d52f8SJosef Bacik 1060860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1070860adfdSMiao Xie unsigned int type) 1080860adfdSMiao Xie { 1090860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1100860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1110860adfdSMiao Xie } 1120860adfdSMiao Xie 1130860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1140860adfdSMiao Xie unsigned int type) 1150860adfdSMiao Xie { 1160860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1170860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1180860adfdSMiao Xie } 1190860adfdSMiao Xie 1200860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1210860adfdSMiao Xie unsigned int type) 1220860adfdSMiao Xie { 1230860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1240860adfdSMiao Xie } 1250860adfdSMiao Xie 1260860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1270860adfdSMiao Xie { 1280860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 129178260b2SMiao Xie } 130178260b2SMiao Xie 131d352ac68SChris Mason /* 132d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 133d352ac68SChris Mason */ 1342ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1352ff7e61eSJeff Mahoney unsigned int type) 13679154b1bSChris Mason { 13779154b1bSChris Mason struct btrfs_transaction *cur_trans; 138a4abeea4SJosef Bacik 13919ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 140d43317dcSChris Mason loop: 14149b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 14287533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 14319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 14449b25e05SJeff Mahoney return -EROFS; 14549b25e05SJeff Mahoney } 14649b25e05SJeff Mahoney 14719ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 148a4abeea4SJosef Bacik if (cur_trans) { 149871383beSDavid Sterba if (cur_trans->aborted) { 15019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 15149b25e05SJeff Mahoney return cur_trans->aborted; 152871383beSDavid Sterba } 1534a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 154178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 155178260b2SMiao Xie return -EBUSY; 156178260b2SMiao Xie } 1579b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 158a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1590860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 16019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 161a4abeea4SJosef Bacik return 0; 162a4abeea4SJosef Bacik } 16319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 164a4abeea4SJosef Bacik 165354aa0fbSMiao Xie /* 166354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 167354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 168354aa0fbSMiao Xie */ 169354aa0fbSMiao Xie if (type == TRANS_ATTACH) 170354aa0fbSMiao Xie return -ENOENT; 171354aa0fbSMiao Xie 1724a9d8bdeSMiao Xie /* 1734a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1744a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1754a9d8bdeSMiao Xie */ 1764a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1774a9d8bdeSMiao Xie 1784b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS); 179db5b493aSTsutomu Itoh if (!cur_trans) 180db5b493aSTsutomu Itoh return -ENOMEM; 181d43317dcSChris Mason 18219ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 18319ae4e81SJan Schmidt if (fs_info->running_transaction) { 184d43317dcSChris Mason /* 185d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 1864a9d8bdeSMiao Xie * to redo the checks above 187d43317dcSChris Mason */ 1884b5faeacSDavid Sterba kfree(cur_trans); 189d43317dcSChris Mason goto loop; 19087533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 191e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 1924b5faeacSDavid Sterba kfree(cur_trans); 1937b8b92afSJosef Bacik return -EROFS; 194a4abeea4SJosef Bacik } 195d43317dcSChris Mason 196ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 19713c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 1980860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 19979154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 20079154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 2014a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 202a4abeea4SJosef Bacik /* 203a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 204a4abeea4SJosef Bacik * commit the transaction. 205a4abeea4SJosef Bacik */ 2069b64f57dSElena Reshetova refcount_set(&cur_trans->use_count, 2); 2073204d33cSJosef Bacik cur_trans->flags = 0; 208afd48513SArnd Bergmann cur_trans->start_time = ktime_get_seconds(); 20956bec294SChris Mason 210a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 211a099d0fdSAlexandru Moise 2125c9d028bSLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; 2133368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 214d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 21520b297d6SJan Schmidt 21620b297d6SJan Schmidt /* 21720b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 21820b297d6SJan Schmidt * the log must never go across transaction boundaries. 21920b297d6SJan Schmidt */ 22020b297d6SJan Schmidt smp_mb(); 22131b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2225d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 22331b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2245d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n"); 225fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 22620b297d6SJan Schmidt 22756bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 22856bec294SChris Mason 2293063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 230bbbf7243SNikolay Borisov INIT_LIST_HEAD(&cur_trans->dev_update_list); 2319e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 232ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2331bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2342b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2351bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 236cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 237ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 238e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2392b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 24019ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 241c258d6e3SQu Wenruo extent_io_tree_init(fs_info, &cur_trans->dirty_pages, 24243eb5f29SQu Wenruo IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); 24319ae4e81SJan Schmidt fs_info->generation++; 24419ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 24519ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 24649b25e05SJeff Mahoney cur_trans->aborted = 0; 24719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 24815ee9bc7SJosef Bacik 24979154b1bSChris Mason return 0; 25079154b1bSChris Mason } 25179154b1bSChris Mason 252d352ac68SChris Mason /* 253d397712bSChris Mason * this does all the record keeping required to make sure that a reference 254d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 255d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 256d397712bSChris Mason * when the transaction commits 257d352ac68SChris Mason */ 2587585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2596426c7adSQu Wenruo struct btrfs_root *root, 2606426c7adSQu Wenruo int force) 2616702ed49SChris Mason { 2620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2630b246afaSJeff Mahoney 2646426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2656426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 2660b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 2674d31778aSQu Wenruo WARN_ON(!force && root->commit_root != root->node); 2685d4f98a2SYan Zheng 2697585717fSChris Mason /* 27027cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2717585717fSChris Mason * we have the reloc mutex held now, so there 2727585717fSChris Mason * is only one writer in this function 2737585717fSChris Mason */ 27427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2757585717fSChris Mason 27627cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2777585717fSChris Mason * they find our root->last_trans update 2787585717fSChris Mason */ 2797585717fSChris Mason smp_wmb(); 2807585717fSChris Mason 2810b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 2826426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 2830b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 284a4abeea4SJosef Bacik return 0; 285a4abeea4SJosef Bacik } 2860b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 2876702ed49SChris Mason (unsigned long)root->root_key.objectid, 2886702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 2890b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 2907585717fSChris Mason root->last_trans = trans->transid; 2917585717fSChris Mason 2927585717fSChris Mason /* this is pretty tricky. We don't want to 2937585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 2947585717fSChris Mason * unless we're really doing the first setup for this root in 2957585717fSChris Mason * this transaction. 2967585717fSChris Mason * 2977585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 2987585717fSChris Mason * if we want to take the expensive mutex. 2997585717fSChris Mason * 3007585717fSChris Mason * But, we have to set root->last_trans before we 3017585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3027585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 30327cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3047585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3057585717fSChris Mason * 3067585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3077585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3087585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3097585717fSChris Mason * done before we pop in the zero below 3107585717fSChris Mason */ 3115d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 312c7548af6SChris Mason smp_mb__before_atomic(); 31327cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3146702ed49SChris Mason } 3155d4f98a2SYan Zheng return 0; 3166702ed49SChris Mason } 3175d4f98a2SYan Zheng 3187585717fSChris Mason 3192b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3202b9dbef2SJosef Bacik struct btrfs_root *root) 3212b9dbef2SJosef Bacik { 3220b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3232b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3242b9dbef2SJosef Bacik 3252b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3262b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3272b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3282b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3292b9dbef2SJosef Bacik 3302b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3310b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3320b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3332b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3342b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3350b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3362b9dbef2SJosef Bacik } 3372b9dbef2SJosef Bacik 3387585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3397585717fSChris Mason struct btrfs_root *root) 3407585717fSChris Mason { 3410b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3420b246afaSJeff Mahoney 34327cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3447585717fSChris Mason return 0; 3457585717fSChris Mason 3467585717fSChris Mason /* 34727cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3487585717fSChris Mason * and barriers 3497585717fSChris Mason */ 3507585717fSChris Mason smp_rmb(); 3517585717fSChris Mason if (root->last_trans == trans->transid && 35227cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3537585717fSChris Mason return 0; 3547585717fSChris Mason 3550b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 3566426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 3570b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 3587585717fSChris Mason 3597585717fSChris Mason return 0; 3607585717fSChris Mason } 3617585717fSChris Mason 3624a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3634a9d8bdeSMiao Xie { 3644a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 365501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 366501407aaSJosef Bacik !trans->aborted); 3674a9d8bdeSMiao Xie } 3684a9d8bdeSMiao Xie 369d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 370d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 371d352ac68SChris Mason * transaction might not be fully on disk. 372d352ac68SChris Mason */ 3732ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 37479154b1bSChris Mason { 375f9295749SChris Mason struct btrfs_transaction *cur_trans; 37679154b1bSChris Mason 3770b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 3780b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 3794a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 3809b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 3810b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 38272d63ed6SLi Zefan 3830b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 384501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 385501407aaSJosef Bacik cur_trans->aborted); 386724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 387a4abeea4SJosef Bacik } else { 3880b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 389f9295749SChris Mason } 39037d1aeeeSChris Mason } 39137d1aeeeSChris Mason 3922ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 39337d1aeeeSChris Mason { 3940b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 395a4abeea4SJosef Bacik return 0; 396a4abeea4SJosef Bacik 39792e2f7e3SNikolay Borisov if (type == TRANS_START) 398a4abeea4SJosef Bacik return 1; 399a4abeea4SJosef Bacik 400a22285a6SYan, Zheng return 0; 401a22285a6SYan, Zheng } 402a22285a6SYan, Zheng 40320dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 40420dd2cbfSMiao Xie { 4050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4060b246afaSJeff Mahoney 4070b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 40827cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 40920dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 41020dd2cbfSMiao Xie root->reloc_root) 41120dd2cbfSMiao Xie return false; 41220dd2cbfSMiao Xie 41320dd2cbfSMiao Xie return true; 41420dd2cbfSMiao Xie } 41520dd2cbfSMiao Xie 41608e007d2SMiao Xie static struct btrfs_trans_handle * 4175aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 418003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 419003d7c59SJeff Mahoney bool enforce_qgroups) 420a22285a6SYan, Zheng { 4210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 422ba2c4d4eSJosef Bacik struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; 423a22285a6SYan, Zheng struct btrfs_trans_handle *h; 424a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 425b5009945SJosef Bacik u64 num_bytes = 0; 426c5567237SArne Jansen u64 qgroup_reserved = 0; 42720dd2cbfSMiao Xie bool reloc_reserved = false; 42820dd2cbfSMiao Xie int ret; 429acce952bSliubo 43046c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4312755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 43246c4e71eSFilipe Manana 4330b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 434acce952bSliubo return ERR_PTR(-EROFS); 4352a1eb461SJosef Bacik 43646c4e71eSFilipe Manana if (current->journal_info) { 4370860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4382a1eb461SJosef Bacik h = current->journal_info; 439b50fff81SDavid Sterba refcount_inc(&h->use_count); 440b50fff81SDavid Sterba WARN_ON(refcount_read(&h->use_count) > 2); 4412a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4422a1eb461SJosef Bacik h->block_rsv = NULL; 4432a1eb461SJosef Bacik goto got_it; 4442a1eb461SJosef Bacik } 445b5009945SJosef Bacik 446b5009945SJosef Bacik /* 447b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 448b5009945SJosef Bacik * the appropriate flushing if need be. 449b5009945SJosef Bacik */ 450003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 451ba2c4d4eSJosef Bacik struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; 452ba2c4d4eSJosef Bacik u64 delayed_refs_bytes = 0; 453ba2c4d4eSJosef Bacik 4540b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 455733e03a0SQu Wenruo ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, 456003d7c59SJeff Mahoney enforce_qgroups); 457c5567237SArne Jansen if (ret) 458c5567237SArne Jansen return ERR_PTR(ret); 459c5567237SArne Jansen 460ba2c4d4eSJosef Bacik /* 461ba2c4d4eSJosef Bacik * We want to reserve all the bytes we may need all at once, so 462ba2c4d4eSJosef Bacik * we only do 1 enospc flushing cycle per transaction start. We 463ba2c4d4eSJosef Bacik * accomplish this by simply assuming we'll do 2 x num_items 464ba2c4d4eSJosef Bacik * worth of delayed refs updates in this trans handle, and 465ba2c4d4eSJosef Bacik * refill that amount for whatever is missing in the reserve. 466ba2c4d4eSJosef Bacik */ 4670b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 468ba2c4d4eSJosef Bacik if (delayed_refs_rsv->full == 0) { 469ba2c4d4eSJosef Bacik delayed_refs_bytes = num_bytes; 470ba2c4d4eSJosef Bacik num_bytes <<= 1; 471ba2c4d4eSJosef Bacik } 472ba2c4d4eSJosef Bacik 47320dd2cbfSMiao Xie /* 47420dd2cbfSMiao Xie * Do the reservation for the relocation root creation 47520dd2cbfSMiao Xie */ 476ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 4770b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 47820dd2cbfSMiao Xie reloc_reserved = true; 47920dd2cbfSMiao Xie } 48020dd2cbfSMiao Xie 481ba2c4d4eSJosef Bacik ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); 482ba2c4d4eSJosef Bacik if (ret) 483ba2c4d4eSJosef Bacik goto reserve_fail; 484ba2c4d4eSJosef Bacik if (delayed_refs_bytes) { 485ba2c4d4eSJosef Bacik btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, 486ba2c4d4eSJosef Bacik delayed_refs_bytes); 487ba2c4d4eSJosef Bacik num_bytes -= delayed_refs_bytes; 488ba2c4d4eSJosef Bacik } 489ba2c4d4eSJosef Bacik } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && 490ba2c4d4eSJosef Bacik !delayed_refs_rsv->full) { 491ba2c4d4eSJosef Bacik /* 492ba2c4d4eSJosef Bacik * Some people call with btrfs_start_transaction(root, 0) 493ba2c4d4eSJosef Bacik * because they can be throttled, but have some other mechanism 494ba2c4d4eSJosef Bacik * for reserving space. We still want these guys to refill the 495ba2c4d4eSJosef Bacik * delayed block_rsv so just add 1 items worth of reservation 496ba2c4d4eSJosef Bacik * here. 497ba2c4d4eSJosef Bacik */ 498ba2c4d4eSJosef Bacik ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); 499b5009945SJosef Bacik if (ret) 500843fcf35SMiao Xie goto reserve_fail; 501b5009945SJosef Bacik } 502a22285a6SYan, Zheng again: 503f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 504843fcf35SMiao Xie if (!h) { 505843fcf35SMiao Xie ret = -ENOMEM; 506843fcf35SMiao Xie goto alloc_fail; 507843fcf35SMiao Xie } 508a22285a6SYan, Zheng 50998114659SJosef Bacik /* 51098114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 51198114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 51298114659SJosef Bacik * because we're already holding a ref. We need this because we could 51398114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 51498114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 515354aa0fbSMiao Xie * 516354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 517354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 51898114659SJosef Bacik */ 5190860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5200b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 521b2b5ef5cSJan Kara 5222ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5232ff7e61eSJeff Mahoney wait_current_trans(fs_info); 524a22285a6SYan, Zheng 525a4abeea4SJosef Bacik do { 5262ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 527178260b2SMiao Xie if (ret == -EBUSY) { 5282ff7e61eSJeff Mahoney wait_current_trans(fs_info); 529178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 530178260b2SMiao Xie ret = -ENOENT; 531178260b2SMiao Xie } 532a4abeea4SJosef Bacik } while (ret == -EBUSY); 533a4abeea4SJosef Bacik 534a43f7f82SLiu Bo if (ret < 0) 535843fcf35SMiao Xie goto join_fail; 5360f7d52f4SChris Mason 5370b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 538a22285a6SYan, Zheng 539a22285a6SYan, Zheng h->transid = cur_trans->transid; 540a22285a6SYan, Zheng h->transaction = cur_trans; 541d13603efSArne Jansen h->root = root; 542b50fff81SDavid Sterba refcount_set(&h->use_count, 1); 54364b63580SJeff Mahoney h->fs_info = root->fs_info; 5447174109cSQu Wenruo 545a698d075SMiao Xie h->type = type; 546d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 547ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 548b7ec40d7SChris Mason 549a22285a6SYan, Zheng smp_mb(); 5504a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5512ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 552abdd2e80SFilipe Manana current->journal_info = h; 5533a45bb20SJeff Mahoney btrfs_commit_transaction(h); 554a22285a6SYan, Zheng goto again; 555a22285a6SYan, Zheng } 5569ed74f2dSJosef Bacik 557b5009945SJosef Bacik if (num_bytes) { 5580b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5592bcc0328SLiu Bo h->transid, num_bytes, 1); 5600b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 561b5009945SJosef Bacik h->bytes_reserved = num_bytes; 56220dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 563a22285a6SYan, Zheng } 564a22285a6SYan, Zheng 5652a1eb461SJosef Bacik got_it: 566a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 567a22285a6SYan, Zheng 568bcf3a3e7SNikolay Borisov if (!current->journal_info) 569a22285a6SYan, Zheng current->journal_info = h; 57079154b1bSChris Mason return h; 571843fcf35SMiao Xie 572843fcf35SMiao Xie join_fail: 5730860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5740b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 575843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 576843fcf35SMiao Xie alloc_fail: 577843fcf35SMiao Xie if (num_bytes) 5782ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 579843fcf35SMiao Xie num_bytes); 580843fcf35SMiao Xie reserve_fail: 581733e03a0SQu Wenruo btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); 582843fcf35SMiao Xie return ERR_PTR(ret); 58379154b1bSChris Mason } 58479154b1bSChris Mason 585f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 5865aed1dd8SAlexandru Moise unsigned int num_items) 587f9295749SChris Mason { 58808e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 589003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 590f9295749SChris Mason } 591003d7c59SJeff Mahoney 5928eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 5938eab77ffSFilipe Manana struct btrfs_root *root, 5948eab77ffSFilipe Manana unsigned int num_items, 5958eab77ffSFilipe Manana int min_factor) 5968eab77ffSFilipe Manana { 5970b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 5988eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 5998eab77ffSFilipe Manana u64 num_bytes; 6008eab77ffSFilipe Manana int ret; 6018eab77ffSFilipe Manana 602003d7c59SJeff Mahoney /* 603003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 604003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 605003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 606003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 607003d7c59SJeff Mahoney */ 608003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 609003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6108eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6118eab77ffSFilipe Manana return trans; 6128eab77ffSFilipe Manana 6138eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6148eab77ffSFilipe Manana if (IS_ERR(trans)) 6158eab77ffSFilipe Manana return trans; 6168eab77ffSFilipe Manana 6170b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6180b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6190b246afaSJeff Mahoney num_bytes, min_factor); 6208eab77ffSFilipe Manana if (ret) { 6213a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6228eab77ffSFilipe Manana return ERR_PTR(ret); 6238eab77ffSFilipe Manana } 6248eab77ffSFilipe Manana 6250b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6268eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6270b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 62888d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6298eab77ffSFilipe Manana 6308eab77ffSFilipe Manana return trans; 6318eab77ffSFilipe Manana } 6328407aa46SMiao Xie 6337a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 634f9295749SChris Mason { 635003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 636003d7c59SJeff Mahoney true); 637f9295749SChris Mason } 638f9295749SChris Mason 6397a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6400af3d00bSJosef Bacik { 641575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 642003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6430af3d00bSJosef Bacik } 6440af3d00bSJosef Bacik 645d4edf39bSMiao Xie /* 646d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 647d4edf39bSMiao Xie * 648d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 649d4edf39bSMiao Xie * don't want to start a new one. 650d4edf39bSMiao Xie * 651d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 652d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 653d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 654d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 655d4edf39bSMiao Xie * invoke 656d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 657d4edf39bSMiao Xie */ 658354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 65960376ce4SJosef Bacik { 660575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 661003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 66260376ce4SJosef Bacik } 66360376ce4SJosef Bacik 664d4edf39bSMiao Xie /* 66590b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 666d4edf39bSMiao Xie * 66752042d8eSAndrea Gelmini * It is similar to the above function, the difference is this one 668d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 669d4edf39bSMiao Xie * complete. 670d4edf39bSMiao Xie */ 671d4edf39bSMiao Xie struct btrfs_trans_handle * 672d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 673d4edf39bSMiao Xie { 674d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 675d4edf39bSMiao Xie 676575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 677003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6788d9e220cSAl Viro if (trans == ERR_PTR(-ENOENT)) 6792ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 680d4edf39bSMiao Xie 681d4edf39bSMiao Xie return trans; 682d4edf39bSMiao Xie } 683d4edf39bSMiao Xie 684d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 6852ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 68689ce8a63SChris Mason { 6874a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 68889ce8a63SChris Mason } 68989ce8a63SChris Mason 6902ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 69146204592SSage Weil { 69246204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6938cd2807fSMiao Xie int ret = 0; 69446204592SSage Weil 69546204592SSage Weil if (transid) { 6960b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 697a4abeea4SJosef Bacik goto out; 69846204592SSage Weil 69946204592SSage Weil /* find specified transaction */ 7000b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7010b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 70246204592SSage Weil if (t->transid == transid) { 70346204592SSage Weil cur_trans = t; 7049b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7058cd2807fSMiao Xie ret = 0; 70646204592SSage Weil break; 70746204592SSage Weil } 7088cd2807fSMiao Xie if (t->transid > transid) { 7098cd2807fSMiao Xie ret = 0; 71046204592SSage Weil break; 71146204592SSage Weil } 7128cd2807fSMiao Xie } 7130b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 71442383020SSage Weil 71542383020SSage Weil /* 71642383020SSage Weil * The specified transaction doesn't exist, or we 71742383020SSage Weil * raced with btrfs_commit_transaction 71842383020SSage Weil */ 71942383020SSage Weil if (!cur_trans) { 7200b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 72142383020SSage Weil ret = -EINVAL; 7228cd2807fSMiao Xie goto out; 72342383020SSage Weil } 72446204592SSage Weil } else { 72546204592SSage Weil /* find newest transaction that is committing | committed */ 7260b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7270b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 72846204592SSage Weil list) { 7294a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7304a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7313473f3c0SJosef Bacik break; 73246204592SSage Weil cur_trans = t; 7339b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 73446204592SSage Weil break; 73546204592SSage Weil } 73646204592SSage Weil } 7370b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 73846204592SSage Weil if (!cur_trans) 739a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 74046204592SSage Weil } 74146204592SSage Weil 7422ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 743724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 744a4abeea4SJosef Bacik out: 74546204592SSage Weil return ret; 74646204592SSage Weil } 74746204592SSage Weil 7482ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 74937d1aeeeSChris Mason { 7502ff7e61eSJeff Mahoney wait_current_trans(fs_info); 75137d1aeeeSChris Mason } 75237d1aeeeSChris Mason 7532ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7548929ecfaSYan, Zheng { 7552ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7560b246afaSJeff Mahoney 75764403612SJosef Bacik if (btrfs_check_space_for_delayed_refs(fs_info)) 7581be41b78SJosef Bacik return 1; 75936ba022aSJosef Bacik 7602ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 7618929ecfaSYan, Zheng } 7628929ecfaSYan, Zheng 7633a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 7648929ecfaSYan, Zheng { 7658929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7668929ecfaSYan, Zheng 767a4abeea4SJosef Bacik smp_mb(); 7684a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7694a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7708929ecfaSYan, Zheng return 1; 7718929ecfaSYan, Zheng 7722ff7e61eSJeff Mahoney return should_end_transaction(trans); 7738929ecfaSYan, Zheng } 7748929ecfaSYan, Zheng 775dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) 776dc60c525SNikolay Borisov 7770e34693fSNikolay Borisov { 778dc60c525SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 779dc60c525SNikolay Borisov 7800e34693fSNikolay Borisov if (!trans->block_rsv) { 7810e34693fSNikolay Borisov ASSERT(!trans->bytes_reserved); 7820e34693fSNikolay Borisov return; 7830e34693fSNikolay Borisov } 7840e34693fSNikolay Borisov 7850e34693fSNikolay Borisov if (!trans->bytes_reserved) 7860e34693fSNikolay Borisov return; 7870e34693fSNikolay Borisov 7880e34693fSNikolay Borisov ASSERT(trans->block_rsv == &fs_info->trans_block_rsv); 7890e34693fSNikolay Borisov trace_btrfs_space_reservation(fs_info, "transaction", 7900e34693fSNikolay Borisov trans->transid, trans->bytes_reserved, 0); 7910e34693fSNikolay Borisov btrfs_block_rsv_release(fs_info, trans->block_rsv, 7920e34693fSNikolay Borisov trans->bytes_reserved); 7930e34693fSNikolay Borisov trans->bytes_reserved = 0; 7940e34693fSNikolay Borisov } 7950e34693fSNikolay Borisov 79689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 7973a45bb20SJeff Mahoney int throttle) 79879154b1bSChris Mason { 7993a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8008929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 801a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8024edc2ca3SDave Jones int err = 0; 803d6e4a428SChris Mason 804b50fff81SDavid Sterba if (refcount_read(&trans->use_count) > 1) { 805b50fff81SDavid Sterba refcount_dec(&trans->use_count); 8062a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8072a1eb461SJosef Bacik return 0; 8082a1eb461SJosef Bacik } 8092a1eb461SJosef Bacik 810dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8114c13d758SJosef Bacik trans->block_rsv = NULL; 812c5567237SArne Jansen 8136c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 814ea658badSJosef Bacik 8154fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8164fbcdf66SFilipe Manana 81720c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8183bbb24b2SJosef Bacik if (throttle) 8193a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8203bbb24b2SJosef Bacik else 8218929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8228929ecfaSYan, Zheng } 8238929ecfaSYan, Zheng 8240860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8250b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8266df7881aSJosef Bacik 8278929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 82813c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 82913c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8300860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 83189ce8a63SChris Mason 832093258e6SDavid Sterba cond_wake_up(&cur_trans->writer_wait); 833724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8349ed74f2dSJosef Bacik 8359ed74f2dSJosef Bacik if (current->journal_info == trans) 8369ed74f2dSJosef Bacik current->journal_info = NULL; 837ab78c84dSChris Mason 83824bbcf04SYan, Zheng if (throttle) 8392ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 84024bbcf04SYan, Zheng 84149b25e05SJeff Mahoney if (trans->aborted || 8420b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 8434e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8444edc2ca3SDave Jones err = -EIO; 8454e121c06SJosef Bacik } 84649b25e05SJeff Mahoney 8474edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 8484edc2ca3SDave Jones return err; 84979154b1bSChris Mason } 85079154b1bSChris Mason 8513a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 85289ce8a63SChris Mason { 8533a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 85489ce8a63SChris Mason } 85589ce8a63SChris Mason 8563a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 85789ce8a63SChris Mason { 8583a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 85916cdcec7SMiao Xie } 86016cdcec7SMiao Xie 861d352ac68SChris Mason /* 862d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 863d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 864690587d1SChris Mason * those extents are sent to disk but does not wait on them 865d352ac68SChris Mason */ 8662ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 8678cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 86879154b1bSChris Mason { 869777e6bd7SChris Mason int err = 0; 8707c4452b9SChris Mason int werr = 0; 8710b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 872e6138876SJosef Bacik struct extent_state *cached_state = NULL; 873777e6bd7SChris Mason u64 start = 0; 8745f39d397SChris Mason u64 end; 8757c4452b9SChris Mason 8766300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 8771728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 878e6138876SJosef Bacik mark, &cached_state)) { 879663dfbb0SFilipe Manana bool wait_writeback = false; 880663dfbb0SFilipe Manana 881663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 882663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 883210aa277SDavid Sterba mark, &cached_state); 884663dfbb0SFilipe Manana /* 885663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 886663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 887663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 888663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 889bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 890bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 891bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 892bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 893bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 894663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 89541e7acd3SNikolay Borisov * the transaction (through extent_io_tree_release()). 896663dfbb0SFilipe Manana */ 897663dfbb0SFilipe Manana if (err == -ENOMEM) { 898663dfbb0SFilipe Manana err = 0; 899663dfbb0SFilipe Manana wait_writeback = true; 900663dfbb0SFilipe Manana } 901663dfbb0SFilipe Manana if (!err) 9021728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9037c4452b9SChris Mason if (err) 9047c4452b9SChris Mason werr = err; 905663dfbb0SFilipe Manana else if (wait_writeback) 906663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 907e38e2ed7SFilipe Manana free_extent_state(cached_state); 908663dfbb0SFilipe Manana cached_state = NULL; 9091728366eSJosef Bacik cond_resched(); 9101728366eSJosef Bacik start = end + 1; 9117c4452b9SChris Mason } 9126300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 913690587d1SChris Mason return werr; 914690587d1SChris Mason } 915690587d1SChris Mason 916690587d1SChris Mason /* 917690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 918690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 919690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 920690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 921690587d1SChris Mason */ 922bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 923bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 924690587d1SChris Mason { 925690587d1SChris Mason int err = 0; 926690587d1SChris Mason int werr = 0; 9270b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 928e6138876SJosef Bacik struct extent_state *cached_state = NULL; 929690587d1SChris Mason u64 start = 0; 930690587d1SChris Mason u64 end; 931690587d1SChris Mason 9321728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 933e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 934663dfbb0SFilipe Manana /* 935663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 936663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 937663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 938663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 939663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 94041e7acd3SNikolay Borisov * it's safe to do it (through extent_io_tree_release()). 941663dfbb0SFilipe Manana */ 942663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 943ae0f1625SDavid Sterba EXTENT_NEED_WAIT, 0, 0, &cached_state); 944663dfbb0SFilipe Manana if (err == -ENOMEM) 945663dfbb0SFilipe Manana err = 0; 946663dfbb0SFilipe Manana if (!err) 9471728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 948777e6bd7SChris Mason if (err) 949777e6bd7SChris Mason werr = err; 950e38e2ed7SFilipe Manana free_extent_state(cached_state); 951e38e2ed7SFilipe Manana cached_state = NULL; 952777e6bd7SChris Mason cond_resched(); 9531728366eSJosef Bacik start = end + 1; 954777e6bd7SChris Mason } 9557c4452b9SChris Mason if (err) 9567c4452b9SChris Mason werr = err; 957bf89d38fSJeff Mahoney return werr; 958bf89d38fSJeff Mahoney } 959656f30dbSFilipe Manana 960bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 961bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 962bf89d38fSJeff Mahoney { 963bf89d38fSJeff Mahoney bool errors = false; 964bf89d38fSJeff Mahoney int err; 965bf89d38fSJeff Mahoney 966bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 967bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 968bf89d38fSJeff Mahoney errors = true; 969bf89d38fSJeff Mahoney 970bf89d38fSJeff Mahoney if (errors && !err) 971bf89d38fSJeff Mahoney err = -EIO; 972bf89d38fSJeff Mahoney return err; 973bf89d38fSJeff Mahoney } 974bf89d38fSJeff Mahoney 975bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 976bf89d38fSJeff Mahoney { 977bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 978bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 979bf89d38fSJeff Mahoney bool errors = false; 980bf89d38fSJeff Mahoney int err; 981bf89d38fSJeff Mahoney 982bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 983bf89d38fSJeff Mahoney 984bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 985656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 9860b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 987656f30dbSFilipe Manana errors = true; 988656f30dbSFilipe Manana 989656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 9900b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 991656f30dbSFilipe Manana errors = true; 992656f30dbSFilipe Manana 993bf89d38fSJeff Mahoney if (errors && !err) 994bf89d38fSJeff Mahoney err = -EIO; 995bf89d38fSJeff Mahoney return err; 99679154b1bSChris Mason } 99779154b1bSChris Mason 998690587d1SChris Mason /* 999c9b577c0SNikolay Borisov * When btree blocks are allocated the corresponding extents are marked dirty. 1000c9b577c0SNikolay Borisov * This function ensures such extents are persisted on disk for transaction or 1001c9b577c0SNikolay Borisov * log commit. 1002c9b577c0SNikolay Borisov * 1003c9b577c0SNikolay Borisov * @trans: transaction whose dirty pages we'd like to write 1004690587d1SChris Mason */ 100570458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans) 1006d0c803c4SChris Mason { 1007663dfbb0SFilipe Manana int ret; 1008c9b577c0SNikolay Borisov int ret2; 1009c9b577c0SNikolay Borisov struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages; 101070458a58SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1011c9b577c0SNikolay Borisov struct blk_plug plug; 1012663dfbb0SFilipe Manana 1013c9b577c0SNikolay Borisov blk_start_plug(&plug); 1014c9b577c0SNikolay Borisov ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY); 1015c9b577c0SNikolay Borisov blk_finish_plug(&plug); 1016c9b577c0SNikolay Borisov ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1017c9b577c0SNikolay Borisov 101841e7acd3SNikolay Borisov extent_io_tree_release(&trans->transaction->dirty_pages); 1019663dfbb0SFilipe Manana 1020c9b577c0SNikolay Borisov if (ret) 1021663dfbb0SFilipe Manana return ret; 1022c9b577c0SNikolay Borisov else if (ret2) 1023c9b577c0SNikolay Borisov return ret2; 1024c9b577c0SNikolay Borisov else 1025c9b577c0SNikolay Borisov return 0; 1026d0c803c4SChris Mason } 1027d0c803c4SChris Mason 1028d352ac68SChris Mason /* 1029d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1030d352ac68SChris Mason * 1031d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1032d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1033d352ac68SChris Mason * allocation tree. 1034d352ac68SChris Mason * 1035d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1036d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1037d352ac68SChris Mason */ 10380b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 103979154b1bSChris Mason struct btrfs_root *root) 104079154b1bSChris Mason { 104179154b1bSChris Mason int ret; 10420b86a832SChris Mason u64 old_root_bytenr; 104386b9f2ecSYan, Zheng u64 old_root_used; 10440b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10450b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 104679154b1bSChris Mason 104786b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 104856bec294SChris Mason 104979154b1bSChris Mason while (1) { 10500b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 105186b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1052ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 105379154b1bSChris Mason break; 105487ef2bb4SChris Mason 10555d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 105679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10570b86a832SChris Mason &root->root_key, 10580b86a832SChris Mason &root->root_item); 105949b25e05SJeff Mahoney if (ret) 106049b25e05SJeff Mahoney return ret; 106156bec294SChris Mason 106286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1063e7070be1SJosef Bacik } 1064276e680dSYan Zheng 10650b86a832SChris Mason return 0; 10660b86a832SChris Mason } 10670b86a832SChris Mason 1068d352ac68SChris Mason /* 1069d352ac68SChris Mason * update all the cowonly tree roots on disk 107049b25e05SJeff Mahoney * 107149b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 107249b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 107349b25e05SJeff Mahoney * to clean up the delayed refs. 1074d352ac68SChris Mason */ 10759386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) 10760b86a832SChris Mason { 10779386d8bcSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1078ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 10791bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 10800b86a832SChris Mason struct list_head *next; 108184234f3aSYan Zheng struct extent_buffer *eb; 108256bec294SChris Mason int ret; 108384234f3aSYan Zheng 108484234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 108549b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 108649b25e05SJeff Mahoney 0, &eb); 108784234f3aSYan Zheng btrfs_tree_unlock(eb); 108884234f3aSYan Zheng free_extent_buffer(eb); 10890b86a832SChris Mason 109049b25e05SJeff Mahoney if (ret) 109149b25e05SJeff Mahoney return ret; 109249b25e05SJeff Mahoney 1093c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 109449b25e05SJeff Mahoney if (ret) 109549b25e05SJeff Mahoney return ret; 109687ef2bb4SChris Mason 10970b246afaSJeff Mahoney ret = btrfs_run_dev_stats(trans, fs_info); 1098c16ce190SJosef Bacik if (ret) 1099c16ce190SJosef Bacik return ret; 11000b246afaSJeff Mahoney ret = btrfs_run_dev_replace(trans, fs_info); 1101c16ce190SJosef Bacik if (ret) 1102c16ce190SJosef Bacik return ret; 1103280f8bd2SLu Fengqi ret = btrfs_run_qgroups(trans); 1104c16ce190SJosef Bacik if (ret) 1105c16ce190SJosef Bacik return ret; 1106546adb0dSJan Schmidt 1107bbebb3e0SDavid Sterba ret = btrfs_setup_space_cache(trans); 1108dcdf7f6dSJosef Bacik if (ret) 1109dcdf7f6dSJosef Bacik return ret; 1110dcdf7f6dSJosef Bacik 1111546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1112c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1113c16ce190SJosef Bacik if (ret) 1114c16ce190SJosef Bacik return ret; 1115ea526d18SJosef Bacik again: 11160b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11172ff7e61eSJeff Mahoney struct btrfs_root *root; 11180b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11190b86a832SChris Mason list_del_init(next); 11200b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1121e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 112287ef2bb4SChris Mason 11239e351cc8SJosef Bacik if (root != fs_info->extent_root) 11249e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11259e351cc8SJosef Bacik &trans->transaction->switch_commits); 112649b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 112749b25e05SJeff Mahoney if (ret) 112849b25e05SJeff Mahoney return ret; 1129c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1130ea526d18SJosef Bacik if (ret) 1131ea526d18SJosef Bacik return ret; 113279154b1bSChris Mason } 1133276e680dSYan Zheng 11341bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1135*5742d15fSDavid Sterba ret = btrfs_write_dirty_block_groups(trans); 1136ea526d18SJosef Bacik if (ret) 1137ea526d18SJosef Bacik return ret; 1138c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1139ea526d18SJosef Bacik if (ret) 1140ea526d18SJosef Bacik return ret; 1141ea526d18SJosef Bacik } 1142ea526d18SJosef Bacik 1143ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1144ea526d18SJosef Bacik goto again; 1145ea526d18SJosef Bacik 11469e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11479e351cc8SJosef Bacik &trans->transaction->switch_commits); 11489f6cbcbbSDavid Sterba 11499f6cbcbbSDavid Sterba /* Update dev-replace pointer once everything is committed */ 11509f6cbcbbSDavid Sterba fs_info->dev_replace.committed_cursor_left = 11519f6cbcbbSDavid Sterba fs_info->dev_replace.cursor_left_last_write_of_item; 11528dabb742SStefan Behrens 115379154b1bSChris Mason return 0; 115479154b1bSChris Mason } 115579154b1bSChris Mason 1156d352ac68SChris Mason /* 1157d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1158d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1159d352ac68SChris Mason * be deleted 1160d352ac68SChris Mason */ 1161cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11625eda7b5eSChris Mason { 11630b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11640b246afaSJeff Mahoney 11650b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1166cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11670b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 11680b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 11695eda7b5eSChris Mason } 11705eda7b5eSChris Mason 1171d352ac68SChris Mason /* 11725d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1173d352ac68SChris Mason */ 11747e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) 11750f7d52f4SChris Mason { 11767e4443d9SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 11770f7d52f4SChris Mason struct btrfs_root *gang[8]; 11780f7d52f4SChris Mason int i; 11790f7d52f4SChris Mason int ret; 118054aa1f4dSChris Mason int err = 0; 118154aa1f4dSChris Mason 1182a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 11830f7d52f4SChris Mason while (1) { 11845d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 11855d4f98a2SYan Zheng (void **)gang, 0, 11860f7d52f4SChris Mason ARRAY_SIZE(gang), 11870f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 11880f7d52f4SChris Mason if (ret == 0) 11890f7d52f4SChris Mason break; 11900f7d52f4SChris Mason for (i = 0; i < ret; i++) { 11915b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 11925d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 11932619ba1fSChris Mason (unsigned long)root->root_key.objectid, 11940f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1195a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 119631153d81SYan Zheng 1197e02119d5SChris Mason btrfs_free_log(trans, root); 11985d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1199e02119d5SChris Mason 120082d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 120182d5902dSLi Zefan 1202f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 120327cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1204c7548af6SChris Mason smp_mb__after_atomic(); 1205f1ebcc74SLiu Bo 1206978d910dSYan Zheng if (root->commit_root != root->node) { 12079e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12089e351cc8SJosef Bacik &trans->transaction->switch_commits); 1209978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1210978d910dSYan Zheng root->node); 1211978d910dSYan Zheng } 121231153d81SYan Zheng 12135d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12140f7d52f4SChris Mason &root->root_key, 12150f7d52f4SChris Mason &root->root_item); 1216a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 121754aa1f4dSChris Mason if (err) 121854aa1f4dSChris Mason break; 1219733e03a0SQu Wenruo btrfs_qgroup_free_meta_all_pertrans(root); 12200f7d52f4SChris Mason } 12219f3a7427SChris Mason } 1222a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 122354aa1f4dSChris Mason return err; 12240f7d52f4SChris Mason } 12250f7d52f4SChris Mason 1226d352ac68SChris Mason /* 1227de78b51aSEric Sandeen * defrag a given btree. 1228de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1229d352ac68SChris Mason */ 1230de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1231e9d0b13bSChris Mason { 1232e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1233e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12348929ecfaSYan, Zheng int ret; 1235e9d0b13bSChris Mason 123627cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1237e9d0b13bSChris Mason return 0; 12388929ecfaSYan, Zheng 12396b80053dSChris Mason while (1) { 12408929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12418929ecfaSYan, Zheng if (IS_ERR(trans)) 12428929ecfaSYan, Zheng return PTR_ERR(trans); 12438929ecfaSYan, Zheng 1244de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12458929ecfaSYan, Zheng 12463a45bb20SJeff Mahoney btrfs_end_transaction(trans); 12472ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1248e9d0b13bSChris Mason cond_resched(); 1249e9d0b13bSChris Mason 1250ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1251e9d0b13bSChris Mason break; 1252210549ebSDavid Sterba 1253ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1254ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1255210549ebSDavid Sterba ret = -EAGAIN; 1256210549ebSDavid Sterba break; 1257210549ebSDavid Sterba } 1258e9d0b13bSChris Mason } 125927cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12608929ecfaSYan, Zheng return ret; 1261e9d0b13bSChris Mason } 1262e9d0b13bSChris Mason 1263d352ac68SChris Mason /* 12646426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 12656426c7adSQu Wenruo * 12666426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 12676426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 12686426c7adSQu Wenruo * qgroup works. 12696426c7adSQu Wenruo */ 12706426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 12716426c7adSQu Wenruo struct btrfs_root *src, 12726426c7adSQu Wenruo struct btrfs_root *parent, 12736426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 12746426c7adSQu Wenruo u64 dst_objectid) 12756426c7adSQu Wenruo { 12766426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 12776426c7adSQu Wenruo int ret; 12786426c7adSQu Wenruo 12796426c7adSQu Wenruo /* 12806426c7adSQu Wenruo * Save some performance in the case that qgroups are not 12816426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 12826426c7adSQu Wenruo * kick in anyway. 12836426c7adSQu Wenruo */ 12849ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 12856426c7adSQu Wenruo return 0; 12866426c7adSQu Wenruo 12876426c7adSQu Wenruo /* 128852042d8eSAndrea Gelmini * Ensure dirty @src will be committed. Or, after coming 12894d31778aSQu Wenruo * commit_fs_roots() and switch_commit_roots(), any dirty but not 12904d31778aSQu Wenruo * recorded root will never be updated again, causing an outdated root 12914d31778aSQu Wenruo * item. 12924d31778aSQu Wenruo */ 12934d31778aSQu Wenruo record_root_in_trans(trans, src, 1); 12944d31778aSQu Wenruo 12954d31778aSQu Wenruo /* 12966426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 12976426c7adSQu Wenruo * comment for reason locking tree_log_mutex 12986426c7adSQu Wenruo */ 12996426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13006426c7adSQu Wenruo 13017e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 13026426c7adSQu Wenruo if (ret) 13036426c7adSQu Wenruo goto out; 1304460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 13056426c7adSQu Wenruo if (ret < 0) 13066426c7adSQu Wenruo goto out; 13076426c7adSQu Wenruo 13086426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 1309a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid, 13106426c7adSQu Wenruo inherit); 13116426c7adSQu Wenruo if (ret < 0) 13126426c7adSQu Wenruo goto out; 13136426c7adSQu Wenruo 13146426c7adSQu Wenruo /* 13156426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13166426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13176426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13186426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13196426c7adSQu Wenruo * 2) write all btree blocks onto disk 13206426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13216426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13226426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13236426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13246426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13256426c7adSQu Wenruo */ 13269386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 13276426c7adSQu Wenruo if (ret) 13286426c7adSQu Wenruo goto out; 132916916a88SNikolay Borisov switch_commit_roots(trans->transaction); 133070458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 13316426c7adSQu Wenruo if (ret) 1332f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13336426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13346426c7adSQu Wenruo 13356426c7adSQu Wenruo out: 13366426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 13376426c7adSQu Wenruo 13386426c7adSQu Wenruo /* 13396426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 13406426c7adSQu Wenruo * last_trans == cur_transid. 13416426c7adSQu Wenruo * Or it won't be committed again onto disk after later 13426426c7adSQu Wenruo * insert_dir_item() 13436426c7adSQu Wenruo */ 13446426c7adSQu Wenruo if (!ret) 13456426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 13466426c7adSQu Wenruo return ret; 13476426c7adSQu Wenruo } 13486426c7adSQu Wenruo 13496426c7adSQu Wenruo /* 1350d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1351aec8030aSMiao Xie * transaction commit. This does the actual creation. 1352aec8030aSMiao Xie * 1353aec8030aSMiao Xie * Note: 1354aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1355aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1356aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1357d352ac68SChris Mason */ 135880b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13593063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13603063d29fSChris Mason { 136108d50ca3SNikolay Borisov 136208d50ca3SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 13633063d29fSChris Mason struct btrfs_key key; 136480b6794dSChris Mason struct btrfs_root_item *new_root_item; 13653063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13663063d29fSChris Mason struct btrfs_root *root = pending->root; 13676bdb72deSSage Weil struct btrfs_root *parent_root; 136898c9942aSLiu Bo struct btrfs_block_rsv *rsv; 13696bdb72deSSage Weil struct inode *parent_inode; 137042874b3dSMiao Xie struct btrfs_path *path; 137142874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1372a22285a6SYan, Zheng struct dentry *dentry; 13733063d29fSChris Mason struct extent_buffer *tmp; 1374925baeddSChris Mason struct extent_buffer *old; 137595582b00SDeepa Dinamani struct timespec64 cur_time; 1376aec8030aSMiao Xie int ret = 0; 1377d68fc57bSYan, Zheng u64 to_reserve = 0; 13786bdb72deSSage Weil u64 index = 0; 1379a22285a6SYan, Zheng u64 objectid; 1380b83cc969SLi Zefan u64 root_flags; 13818ea05e3aSAlexander Block uuid_le new_uuid; 13823063d29fSChris Mason 13838546b570SDavid Sterba ASSERT(pending->path); 13848546b570SDavid Sterba path = pending->path; 138542874b3dSMiao Xie 1386b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1387b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1388a22285a6SYan, Zheng 1389aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1390aec8030aSMiao Xie if (pending->error) 13916fa9700eSMiao Xie goto no_free_objectid; 13923063d29fSChris Mason 1393d6726335SQu Wenruo /* 1394d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1395d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1396d6726335SQu Wenruo */ 1397d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1398d6726335SQu Wenruo 1399147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1400d68fc57bSYan, Zheng 1401d68fc57bSYan, Zheng if (to_reserve > 0) { 1402aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1403aec8030aSMiao Xie &pending->block_rsv, 140408e007d2SMiao Xie to_reserve, 140508e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1406aec8030aSMiao Xie if (pending->error) 1407d6726335SQu Wenruo goto clear_skip_qgroup; 1408d68fc57bSYan, Zheng } 1409d68fc57bSYan, Zheng 14103063d29fSChris Mason key.objectid = objectid; 1411a22285a6SYan, Zheng key.offset = (u64)-1; 1412a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14133063d29fSChris Mason 14146fa9700eSMiao Xie rsv = trans->block_rsv; 1415a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14162382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14170b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 141888d3a5aaSJosef Bacik trans->transid, 141988d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1420a22285a6SYan, Zheng dentry = pending->dentry; 1421e9662f70SMiao Xie parent_inode = pending->dir; 1422a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14236426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1424a22285a6SYan, Zheng 1425c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 142604b285f3SDeepa Dinamani 14276bdb72deSSage Weil /* 14286bdb72deSSage Weil * insert the directory item 14296bdb72deSSage Weil */ 1430877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 143149b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 143242874b3dSMiao Xie 143342874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 143442874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 14354a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 143642874b3dSMiao Xie dentry->d_name.name, 143742874b3dSMiao Xie dentry->d_name.len, 0); 143842874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1439fe66a05aSChris Mason pending->error = -EEXIST; 1440aec8030aSMiao Xie goto dir_item_existed; 144142874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 144242874b3dSMiao Xie ret = PTR_ERR(dir_item); 144366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14448732d44fSMiao Xie goto fail; 144579787eaaSJeff Mahoney } 144642874b3dSMiao Xie btrfs_release_path(path); 14476bdb72deSSage Weil 1448e999376fSChris Mason /* 1449e999376fSChris Mason * pull in the delayed directory update 1450e999376fSChris Mason * and the delayed inode item 1451e999376fSChris Mason * otherwise we corrupt the FS during 1452e999376fSChris Mason * snapshot 1453e999376fSChris Mason */ 1454e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 14558732d44fSMiao Xie if (ret) { /* Transaction aborted */ 145666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14578732d44fSMiao Xie goto fail; 14588732d44fSMiao Xie } 1459e999376fSChris Mason 14606426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 14616bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14626bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 146308fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14646bdb72deSSage Weil 1465b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1466b83cc969SLi Zefan if (pending->readonly) 1467b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1468b83cc969SLi Zefan else 1469b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1470b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1471b83cc969SLi Zefan 14728ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 14738ea05e3aSAlexander Block trans->transid); 14748ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 14758ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 14768ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 14778ea05e3aSAlexander Block BTRFS_UUID_SIZE); 147870023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 147970023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 148070023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14818ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14828ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 14838ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 14848ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 148570023da2SStefan Behrens } 14863cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 14873cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 148870023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 14898ea05e3aSAlexander Block 1490925baeddSChris Mason old = btrfs_lock_root_node(root); 149149b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 149279787eaaSJeff Mahoney if (ret) { 149379787eaaSJeff Mahoney btrfs_tree_unlock(old); 149479787eaaSJeff Mahoney free_extent_buffer(old); 149566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14968732d44fSMiao Xie goto fail; 149779787eaaSJeff Mahoney } 149849b25e05SJeff Mahoney 14998bead258SDavid Sterba btrfs_set_lock_blocking_write(old); 15003063d29fSChris Mason 150149b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 150279787eaaSJeff Mahoney /* clean up in any case */ 1503925baeddSChris Mason btrfs_tree_unlock(old); 1504925baeddSChris Mason free_extent_buffer(old); 15058732d44fSMiao Xie if (ret) { 150666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15078732d44fSMiao Xie goto fail; 15088732d44fSMiao Xie } 1509f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 151027cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1511f1ebcc74SLiu Bo smp_wmb(); 1512f1ebcc74SLiu Bo 15135d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1514a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1515a22285a6SYan, Zheng key.offset = trans->transid; 1516a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1517925baeddSChris Mason btrfs_tree_unlock(tmp); 15183063d29fSChris Mason free_extent_buffer(tmp); 15198732d44fSMiao Xie if (ret) { 152066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15218732d44fSMiao Xie goto fail; 15228732d44fSMiao Xie } 15230660b5afSChris Mason 1524a22285a6SYan, Zheng /* 1525a22285a6SYan, Zheng * insert root back/forward references 1526a22285a6SYan, Zheng */ 15276025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, 1528a22285a6SYan, Zheng parent_root->root_key.objectid, 15294a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1530a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15318732d44fSMiao Xie if (ret) { 153266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15338732d44fSMiao Xie goto fail; 15348732d44fSMiao Xie } 1535a22285a6SYan, Zheng 1536a22285a6SYan, Zheng key.offset = (u64)-1; 15370b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 153879787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 153979787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 154066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15418732d44fSMiao Xie goto fail; 154279787eaaSJeff Mahoney } 1543d68fc57bSYan, Zheng 154449b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15458732d44fSMiao Xie if (ret) { 154666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15478732d44fSMiao Xie goto fail; 15488732d44fSMiao Xie } 1549361048f5SMiao Xie 1550c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 15518732d44fSMiao Xie if (ret) { 155266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15538732d44fSMiao Xie goto fail; 15548732d44fSMiao Xie } 155542874b3dSMiao Xie 15566426c7adSQu Wenruo /* 15576426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 15586426c7adSQu Wenruo * snapshot hack to do fast snapshot. 15596426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 15606426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 15616426c7adSQu Wenruo */ 15626426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 15636426c7adSQu Wenruo pending->inherit, objectid); 15646426c7adSQu Wenruo if (ret < 0) 15656426c7adSQu Wenruo goto fail; 15666426c7adSQu Wenruo 1567684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, dentry->d_name.name, 1568684572dfSLu Fengqi dentry->d_name.len, BTRFS_I(parent_inode), 1569684572dfSLu Fengqi &key, BTRFS_FT_DIR, index); 157042874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 15719c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 15728732d44fSMiao Xie if (ret) { 157366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15748732d44fSMiao Xie goto fail; 15758732d44fSMiao Xie } 157642874b3dSMiao Xie 15776ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 157842874b3dSMiao Xie dentry->d_name.len * 2); 157904b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1580c2050a45SDeepa Dinamani current_time(parent_inode); 1581be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1582dd5f9615SStefan Behrens if (ret) { 158366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1584dd5f9615SStefan Behrens goto fail; 1585dd5f9615SStefan Behrens } 1586cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, 1587cdb345a8SLu Fengqi objectid); 1588dd5f9615SStefan Behrens if (ret) { 158966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1590dd5f9615SStefan Behrens goto fail; 1591dd5f9615SStefan Behrens } 1592dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1593cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid, 1594dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1595dd5f9615SStefan Behrens objectid); 1596dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 159766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1598dd5f9615SStefan Behrens goto fail; 1599dd5f9615SStefan Behrens } 1600dd5f9615SStefan Behrens } 1601d6726335SQu Wenruo 1602c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1603d6726335SQu Wenruo if (ret) { 160466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1605d6726335SQu Wenruo goto fail; 1606d6726335SQu Wenruo } 1607d6726335SQu Wenruo 16083063d29fSChris Mason fail: 1609aec8030aSMiao Xie pending->error = ret; 1610aec8030aSMiao Xie dir_item_existed: 161198c9942aSLiu Bo trans->block_rsv = rsv; 16122382c5ccSLiu Bo trans->bytes_reserved = 0; 1613d6726335SQu Wenruo clear_skip_qgroup: 1614d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16156fa9700eSMiao Xie no_free_objectid: 16166fa9700eSMiao Xie kfree(new_root_item); 1617b0c0ea63SDavid Sterba pending->root_item = NULL; 161842874b3dSMiao Xie btrfs_free_path(path); 16198546b570SDavid Sterba pending->path = NULL; 16208546b570SDavid Sterba 162149b25e05SJeff Mahoney return ret; 16223063d29fSChris Mason } 16233063d29fSChris Mason 1624d352ac68SChris Mason /* 1625d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1626d352ac68SChris Mason */ 162708d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans) 16283063d29fSChris Mason { 1629aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16303063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1631aec8030aSMiao Xie int ret = 0; 16323de4586cSChris Mason 1633aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1634aec8030aSMiao Xie list_del(&pending->list); 163508d50ca3SNikolay Borisov ret = create_pending_snapshot(trans, pending); 1636aec8030aSMiao Xie if (ret) 1637aec8030aSMiao Xie break; 1638aec8030aSMiao Xie } 1639aec8030aSMiao Xie return ret; 16403de4586cSChris Mason } 16413de4586cSChris Mason 16422ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 16435d4f98a2SYan Zheng { 16445d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16455d4f98a2SYan Zheng struct btrfs_super_block *super; 16465d4f98a2SYan Zheng 16470b246afaSJeff Mahoney super = fs_info->super_copy; 16485d4f98a2SYan Zheng 16490b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 1650093e037cSDavid Sterba super->chunk_root = root_item->bytenr; 1651093e037cSDavid Sterba super->chunk_root_generation = root_item->generation; 1652093e037cSDavid Sterba super->chunk_root_level = root_item->level; 16535d4f98a2SYan Zheng 16540b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 1655093e037cSDavid Sterba super->root = root_item->bytenr; 1656093e037cSDavid Sterba super->generation = root_item->generation; 1657093e037cSDavid Sterba super->root_level = root_item->level; 16580b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 1659093e037cSDavid Sterba super->cache_generation = root_item->generation; 16600b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 1661093e037cSDavid Sterba super->uuid_tree_generation = root_item->generation; 16625d4f98a2SYan Zheng } 16635d4f98a2SYan Zheng 1664f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1665f36f3042SChris Mason { 16664a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1667f36f3042SChris Mason int ret = 0; 16684a9d8bdeSMiao Xie 1669a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16704a9d8bdeSMiao Xie trans = info->running_transaction; 16714a9d8bdeSMiao Xie if (trans) 16724a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1673a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1674f36f3042SChris Mason return ret; 1675f36f3042SChris Mason } 1676f36f3042SChris Mason 16778929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16788929ecfaSYan, Zheng { 16794a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16808929ecfaSYan, Zheng int ret = 0; 16814a9d8bdeSMiao Xie 1682a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16834a9d8bdeSMiao Xie trans = info->running_transaction; 16844a9d8bdeSMiao Xie if (trans) 16854a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1686a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 16878929ecfaSYan, Zheng return ret; 16888929ecfaSYan, Zheng } 16898929ecfaSYan, Zheng 1690bb9c12c9SSage Weil /* 1691bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1692bb9c12c9SSage Weil * transaction joins 1693bb9c12c9SSage Weil */ 16942ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1695bb9c12c9SSage Weil struct btrfs_transaction *trans) 1696bb9c12c9SSage Weil { 16972ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 16982ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1699bb9c12c9SSage Weil } 1700bb9c12c9SSage Weil 1701bb9c12c9SSage Weil /* 1702bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1703bb9c12c9SSage Weil * caller holds ref. 1704bb9c12c9SSage Weil */ 17052ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17062ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1707bb9c12c9SSage Weil struct btrfs_transaction *trans) 1708bb9c12c9SSage Weil { 17092ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17102ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1711bb9c12c9SSage Weil } 1712bb9c12c9SSage Weil 1713bb9c12c9SSage Weil /* 1714bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1715bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1716bb9c12c9SSage Weil */ 1717bb9c12c9SSage Weil struct btrfs_async_commit { 1718bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17197892b5afSMiao Xie struct work_struct work; 1720bb9c12c9SSage Weil }; 1721bb9c12c9SSage Weil 1722bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1723bb9c12c9SSage Weil { 1724bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17257892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1726bb9c12c9SSage Weil 17276fc4e354SSage Weil /* 17286fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17296fc4e354SSage Weil * Tell lockdep about it. 17306fc4e354SSage Weil */ 1731b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17323a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 17336fc4e354SSage Weil 1734e209db7aSSage Weil current->journal_info = ac->newtrans; 1735e209db7aSSage Weil 17363a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1737bb9c12c9SSage Weil kfree(ac); 1738bb9c12c9SSage Weil } 1739bb9c12c9SSage Weil 1740bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1741bb9c12c9SSage Weil int wait_for_unblock) 1742bb9c12c9SSage Weil { 17433a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1744bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1745bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1746bb9c12c9SSage Weil 1747bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1748db5b493aSTsutomu Itoh if (!ac) 1749db5b493aSTsutomu Itoh return -ENOMEM; 1750bb9c12c9SSage Weil 17517892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 17523a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 17533612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17543612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17553612b495STsutomu Itoh kfree(ac); 17563612b495STsutomu Itoh return err; 17573612b495STsutomu Itoh } 1758bb9c12c9SSage Weil 1759bb9c12c9SSage Weil /* take transaction reference */ 1760bb9c12c9SSage Weil cur_trans = trans->transaction; 17619b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1762bb9c12c9SSage Weil 17633a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17646fc4e354SSage Weil 17656fc4e354SSage Weil /* 17666fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17676fc4e354SSage Weil * async commit thread will be the one to unlock it. 17686fc4e354SSage Weil */ 1769b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17700b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 17716fc4e354SSage Weil 17727892b5afSMiao Xie schedule_work(&ac->work); 1773bb9c12c9SSage Weil 1774bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1775bb9c12c9SSage Weil if (wait_for_unblock) 17762ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1777bb9c12c9SSage Weil else 17782ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1779bb9c12c9SSage Weil 178038e88054SSage Weil if (current->journal_info == trans) 178138e88054SSage Weil current->journal_info = NULL; 178238e88054SSage Weil 1783724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1784bb9c12c9SSage Weil return 0; 1785bb9c12c9SSage Weil } 1786bb9c12c9SSage Weil 178749b25e05SJeff Mahoney 178897cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) 178949b25e05SJeff Mahoney { 179097cb39bbSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 179149b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 179249b25e05SJeff Mahoney 1793b50fff81SDavid Sterba WARN_ON(refcount_read(&trans->use_count) > 1); 179449b25e05SJeff Mahoney 179566642832SJeff Mahoney btrfs_abort_transaction(trans, err); 17967b8b92afSJosef Bacik 17970b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 179866b6135bSLiu Bo 179925d8c284SMiao Xie /* 180025d8c284SMiao Xie * If the transaction is removed from the list, it means this 180125d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 180225d8c284SMiao Xie * to call the cleanup function. 180325d8c284SMiao Xie */ 180425d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 180566b6135bSLiu Bo 180649b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18070b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18084a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18090b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1810f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1811f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1812f094ac32SLiu Bo 18130b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1814d7096fc3SJosef Bacik } 18150b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 181649b25e05SJeff Mahoney 18172ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 181849b25e05SJeff Mahoney 18190b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18200b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18210b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18220b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18234a9d8bdeSMiao Xie 1824e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18250b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1826724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1827724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 182849b25e05SJeff Mahoney 182997cb39bbSNikolay Borisov trace_btrfs_transaction_commit(trans->root); 183049b25e05SJeff Mahoney 183149b25e05SJeff Mahoney if (current->journal_info == trans) 183249b25e05SJeff Mahoney current->journal_info = NULL; 18330b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 183449b25e05SJeff Mahoney 183549b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 183649b25e05SJeff Mahoney } 183749b25e05SJeff Mahoney 1838c7cc64a9SDavid Sterba /* 1839c7cc64a9SDavid Sterba * Release reserved delayed ref space of all pending block groups of the 1840c7cc64a9SDavid Sterba * transaction and remove them from the list 1841c7cc64a9SDavid Sterba */ 1842c7cc64a9SDavid Sterba static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) 1843c7cc64a9SDavid Sterba { 1844c7cc64a9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 1845c7cc64a9SDavid Sterba struct btrfs_block_group_cache *block_group, *tmp; 1846c7cc64a9SDavid Sterba 1847c7cc64a9SDavid Sterba list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { 1848c7cc64a9SDavid Sterba btrfs_delayed_refs_rsv_release(fs_info, 1); 1849c7cc64a9SDavid Sterba list_del_init(&block_group->bg_list); 1850c7cc64a9SDavid Sterba } 1851c7cc64a9SDavid Sterba } 1852c7cc64a9SDavid Sterba 1853609e804dSFilipe Manana static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) 185482436617SMiao Xie { 1855609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1856609e804dSFilipe Manana 1857ce8ea7ccSJosef Bacik /* 1858ce8ea7ccSJosef Bacik * We use writeback_inodes_sb here because if we used 1859ce8ea7ccSJosef Bacik * btrfs_start_delalloc_roots we would deadlock with fs freeze. 1860ce8ea7ccSJosef Bacik * Currently are holding the fs freeze lock, if we do an async flush 1861ce8ea7ccSJosef Bacik * we'll do btrfs_join_transaction() and deadlock because we need to 1862ce8ea7ccSJosef Bacik * wait for the fs freeze lock. Using the direct flushing we benefit 1863ce8ea7ccSJosef Bacik * from already being in a transaction and our join_transaction doesn't 1864ce8ea7ccSJosef Bacik * have to re-take the fs freeze lock. 1865ce8ea7ccSJosef Bacik */ 1866609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 1867ce8ea7ccSJosef Bacik writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); 1868609e804dSFilipe Manana } else { 1869609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1870609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1871609e804dSFilipe Manana 1872609e804dSFilipe Manana /* 1873609e804dSFilipe Manana * Flush dellaloc for any root that is going to be snapshotted. 1874609e804dSFilipe Manana * This is done to avoid a corrupted version of files, in the 1875609e804dSFilipe Manana * snapshots, that had both buffered and direct IO writes (even 1876609e804dSFilipe Manana * if they were done sequentially) due to an unordered update of 1877609e804dSFilipe Manana * the inode's size on disk. 1878609e804dSFilipe Manana */ 1879609e804dSFilipe Manana list_for_each_entry(pending, head, list) { 1880609e804dSFilipe Manana int ret; 1881609e804dSFilipe Manana 1882609e804dSFilipe Manana ret = btrfs_start_delalloc_snapshot(pending->root); 1883609e804dSFilipe Manana if (ret) 1884609e804dSFilipe Manana return ret; 1885609e804dSFilipe Manana } 1886609e804dSFilipe Manana } 188782436617SMiao Xie return 0; 188882436617SMiao Xie } 188982436617SMiao Xie 1890609e804dSFilipe Manana static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) 189182436617SMiao Xie { 1892609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1893609e804dSFilipe Manana 1894609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 18956374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1896609e804dSFilipe Manana } else { 1897609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1898609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1899609e804dSFilipe Manana 1900609e804dSFilipe Manana /* 1901609e804dSFilipe Manana * Wait for any dellaloc that we started previously for the roots 1902609e804dSFilipe Manana * that are going to be snapshotted. This is to avoid a corrupted 1903609e804dSFilipe Manana * version of files in the snapshots that had both buffered and 1904609e804dSFilipe Manana * direct IO writes (even if they were done sequentially). 1905609e804dSFilipe Manana */ 1906609e804dSFilipe Manana list_for_each_entry(pending, head, list) 1907609e804dSFilipe Manana btrfs_wait_ordered_extents(pending->root, 1908609e804dSFilipe Manana U64_MAX, 0, U64_MAX); 1909609e804dSFilipe Manana } 191082436617SMiao Xie } 191182436617SMiao Xie 19123a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 191379154b1bSChris Mason { 19143a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 191549b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19168fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 191725287e0aSMiao Xie int ret; 191879154b1bSChris Mason 19198d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 192020c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 192125287e0aSMiao Xie ret = cur_trans->aborted; 19223a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1923e4a2bcacSJosef Bacik return ret; 192425287e0aSMiao Xie } 192549b25e05SJeff Mahoney 1926f45c752bSJosef Bacik btrfs_trans_release_metadata(trans); 1927f45c752bSJosef Bacik trans->block_rsv = NULL; 1928f45c752bSJosef Bacik 192956bec294SChris Mason /* make a pass through all the delayed refs we have so far 193056bec294SChris Mason * any runnings procs may add more while we are here 193156bec294SChris Mason */ 1932c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1933e4a2bcacSJosef Bacik if (ret) { 19343a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1935e4a2bcacSJosef Bacik return ret; 1936e4a2bcacSJosef Bacik } 193756bec294SChris Mason 1938b7ec40d7SChris Mason cur_trans = trans->transaction; 193949b25e05SJeff Mahoney 194056bec294SChris Mason /* 194156bec294SChris Mason * set the flushing flag so procs in this transaction have to 194256bec294SChris Mason * start sending their work down. 194356bec294SChris Mason */ 1944b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19451be41b78SJosef Bacik smp_wmb(); 194656bec294SChris Mason 19476c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 1948ea658badSJosef Bacik 1949c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1950e4a2bcacSJosef Bacik if (ret) { 19513a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1952e4a2bcacSJosef Bacik return ret; 1953e4a2bcacSJosef Bacik } 195456bec294SChris Mason 19553204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19561bbc621eSChris Mason int run_it = 0; 19571bbc621eSChris Mason 19581bbc621eSChris Mason /* this mutex is also taken before trying to set 19591bbc621eSChris Mason * block groups readonly. We need to make sure 19601bbc621eSChris Mason * that nobody has set a block group readonly 19611bbc621eSChris Mason * after a extents from that block group have been 19621bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19631bbc621eSChris Mason * will wait for the transaction to commit if it 19643204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19651bbc621eSChris Mason * 19663204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19673204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19681bbc621eSChris Mason * hurt to have more than one go through, but there's no 19691bbc621eSChris Mason * real advantage to it either. 19701bbc621eSChris Mason */ 19710b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 19723204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 19733204d33cSJosef Bacik &cur_trans->flags)) 19741bbc621eSChris Mason run_it = 1; 19750b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 19761bbc621eSChris Mason 1977f9cacae3SNikolay Borisov if (run_it) { 197821217054SNikolay Borisov ret = btrfs_start_dirty_block_groups(trans); 19791bbc621eSChris Mason if (ret) { 19803a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19811bbc621eSChris Mason return ret; 19821bbc621eSChris Mason } 1983f9cacae3SNikolay Borisov } 1984f9cacae3SNikolay Borisov } 19851bbc621eSChris Mason 19860b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19874a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 19880b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 19899b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 19903a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 1991ccd467d6SChris Mason 19922ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 199315ee9bc7SJosef Bacik 1994b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1995b4924a0fSLiu Bo ret = cur_trans->aborted; 1996b4924a0fSLiu Bo 1997724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 199815ee9bc7SJosef Bacik 199949b25e05SJeff Mahoney return ret; 200079154b1bSChris Mason } 20014313b399SChris Mason 20024a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20030b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2004bb9c12c9SSage Weil 20050b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2006ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2007ccd467d6SChris Mason struct btrfs_transaction, list); 20084a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20099b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2011ccd467d6SChris Mason 20122ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20131f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2014ccd467d6SChris Mason 2015724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20161f9b8c8fSFilipe Manana if (ret) 20171f9b8c8fSFilipe Manana goto cleanup_transaction; 2018a4abeea4SJosef Bacik } else { 20190b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2020ccd467d6SChris Mason } 2021a4abeea4SJosef Bacik } else { 20220b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2023ccd467d6SChris Mason } 202415ee9bc7SJosef Bacik 20250860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20260860adfdSMiao Xie 2027609e804dSFilipe Manana ret = btrfs_start_delalloc_flush(trans); 202882436617SMiao Xie if (ret) 202982436617SMiao Xie goto cleanup_transaction; 203082436617SMiao Xie 2031e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 203249b25e05SJeff Mahoney if (ret) 203349b25e05SJeff Mahoney goto cleanup_transaction; 203416cdcec7SMiao Xie 2035581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2036581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2037ed3b3d31SChris Mason 2038581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 2039e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 2040ca469637SMiao Xie if (ret) 2041ca469637SMiao Xie goto cleanup_transaction; 2042ca469637SMiao Xie 2043609e804dSFilipe Manana btrfs_wait_delalloc_flush(trans); 2044cb7ab021SWang Shilong 20452ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2046ed0ca140SJosef Bacik /* 2047ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2048ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20494a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2050ed0ca140SJosef Bacik */ 20510b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20524a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20530b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2054ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2055ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 205615ee9bc7SJosef Bacik 20572cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 205820c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 20592cba30f1SMiao Xie ret = cur_trans->aborted; 20606cf7f77eSWang Shilong goto scrub_continue; 20612cba30f1SMiao Xie } 20627585717fSChris Mason /* 20637585717fSChris Mason * the reloc mutex makes sure that we stop 20647585717fSChris Mason * the balancing code from coming in and moving 20657585717fSChris Mason * extents around in the middle of the commit 20667585717fSChris Mason */ 20670b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 20687585717fSChris Mason 206942874b3dSMiao Xie /* 207042874b3dSMiao Xie * We needn't worry about the delayed items because we will 207142874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 207242874b3dSMiao Xie * core function of the snapshot creation. 207342874b3dSMiao Xie */ 207408d50ca3SNikolay Borisov ret = create_pending_snapshots(trans); 207549b25e05SJeff Mahoney if (ret) { 20760b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 20776cf7f77eSWang Shilong goto scrub_continue; 207849b25e05SJeff Mahoney } 20793063d29fSChris Mason 208042874b3dSMiao Xie /* 208142874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 208242874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 208342874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 208442874b3dSMiao Xie * them. 208542874b3dSMiao Xie * 208642874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 208742874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 208842874b3dSMiao Xie * the nodes and leaves. 208942874b3dSMiao Xie */ 2090e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 209149b25e05SJeff Mahoney if (ret) { 20920b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 20936cf7f77eSWang Shilong goto scrub_continue; 209449b25e05SJeff Mahoney } 209516cdcec7SMiao Xie 2096c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 209749b25e05SJeff Mahoney if (ret) { 20980b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 20996cf7f77eSWang Shilong goto scrub_continue; 210049b25e05SJeff Mahoney } 210156bec294SChris Mason 2102e999376fSChris Mason /* 2103e999376fSChris Mason * make sure none of the code above managed to slip in a 2104e999376fSChris Mason * delayed item 2105e999376fSChris Mason */ 2106ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2107e999376fSChris Mason 21082c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2109dc17ff8fSChris Mason 2110e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2111e02119d5SChris Mason * various roots consistent with each other. Every pointer 2112e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2113e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2114e02119d5SChris Mason * the tree logging code from jumping in and changing any 2115e02119d5SChris Mason * of the trees. 2116e02119d5SChris Mason * 2117e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2118e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2119e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2120e02119d5SChris Mason * from now until after the super is written, we avoid races 2121e02119d5SChris Mason * with the tree-log code. 2122e02119d5SChris Mason */ 21230b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21241a40e23bSZheng Yan 21257e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 212649b25e05SJeff Mahoney if (ret) { 21270b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21280b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21296cf7f77eSWang Shilong goto scrub_continue; 213049b25e05SJeff Mahoney } 213154aa1f4dSChris Mason 21323818aea2SQu Wenruo /* 21337e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21347e1876acSDavid Sterba * before the next transaction. 21353818aea2SQu Wenruo */ 21360b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21373818aea2SQu Wenruo 21385d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2139e02119d5SChris Mason * safe to free the root of tree log roots 2140e02119d5SChris Mason */ 21410b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2142e02119d5SChris Mason 21430ed4792aSQu Wenruo /* 214482bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 214582bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 214682bafb38SQu Wenruo */ 2147c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 214882bafb38SQu Wenruo if (ret) { 214982bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 215082bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 215182bafb38SQu Wenruo goto scrub_continue; 215282bafb38SQu Wenruo } 215382bafb38SQu Wenruo 215482bafb38SQu Wenruo /* 21550ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21560ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21570ed4792aSQu Wenruo */ 2158460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 21590ed4792aSQu Wenruo if (ret < 0) { 21600b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21610b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21620ed4792aSQu Wenruo goto scrub_continue; 21630ed4792aSQu Wenruo } 21640ed4792aSQu Wenruo 21659386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 216649b25e05SJeff Mahoney if (ret) { 21670b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21680b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21696cf7f77eSWang Shilong goto scrub_continue; 217049b25e05SJeff Mahoney } 217154aa1f4dSChris Mason 21722cba30f1SMiao Xie /* 21732cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 21742cba30f1SMiao Xie * update ->aborted, check it. 21752cba30f1SMiao Xie */ 217620c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 21772cba30f1SMiao Xie ret = cur_trans->aborted; 21780b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21790b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21806cf7f77eSWang Shilong goto scrub_continue; 21812cba30f1SMiao Xie } 21822cba30f1SMiao Xie 21838b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 218411833d66SYan Zheng 21850b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 21865f39d397SChris Mason 21870b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 21880b246afaSJeff Mahoney fs_info->tree_root->node); 21890b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 21909e351cc8SJosef Bacik &cur_trans->switch_commits); 21915d4f98a2SYan Zheng 21920b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 21930b246afaSJeff Mahoney fs_info->chunk_root->node); 21940b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 21959e351cc8SJosef Bacik &cur_trans->switch_commits); 21969e351cc8SJosef Bacik 219716916a88SNikolay Borisov switch_commit_roots(cur_trans); 21985d4f98a2SYan Zheng 2199ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22001bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22012ff7e61eSJeff Mahoney update_super_roots(fs_info); 2202e02119d5SChris Mason 22030b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22040b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22050b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22060b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2207ccd467d6SChris Mason 2208bbbf7243SNikolay Borisov btrfs_commit_device_sizes(cur_trans); 2209935e5cc9SMiao Xie 22100b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22110b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2212656f30dbSFilipe Manana 22134fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22144fbcdf66SFilipe Manana 22150b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22164a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22170b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22180b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22190b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2220b7ec40d7SChris Mason 22210b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2222e6dcd2dcSChris Mason 222370458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 222449b25e05SJeff Mahoney if (ret) { 22250b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 222608748810SDavid Sterba "Error while writing out transaction"); 22270b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22286cf7f77eSWang Shilong goto scrub_continue; 222949b25e05SJeff Mahoney } 223049b25e05SJeff Mahoney 2231eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 2232e02119d5SChris Mason /* 2233e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2234e02119d5SChris Mason * to go about their business 2235e02119d5SChris Mason */ 22360b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2237c1f32b7cSAnand Jain if (ret) 2238c1f32b7cSAnand Jain goto scrub_continue; 2239e02119d5SChris Mason 22405ead2dd0SNikolay Borisov btrfs_finish_extent_commit(trans); 22414313b399SChris Mason 22423204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22430b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 224413212b54SZhao Lei 22450b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22464a9d8bdeSMiao Xie /* 22474a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22484a9d8bdeSMiao Xie * which can change it. 22494a9d8bdeSMiao Xie */ 22504a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22512c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 2252a514d638SQu Wenruo clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); 22533de4586cSChris Mason 22540b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 225513c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22560b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2257a4abeea4SJosef Bacik 2258724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2259724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 226058176a96SJosef Bacik 22610860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 22620b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2263b2b5ef5cSJan Kara 22643a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 22651abe9b8aSliubo 22662ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2267a2de733cSArne Jansen 22689ed74f2dSJosef Bacik if (current->journal_info == trans) 22699ed74f2dSJosef Bacik current->journal_info = NULL; 22709ed74f2dSJosef Bacik 22712c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 227224bbcf04SYan, Zheng 227379154b1bSChris Mason return ret; 227449b25e05SJeff Mahoney 22756cf7f77eSWang Shilong scrub_continue: 22762ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 227749b25e05SJeff Mahoney cleanup_transaction: 2278dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 2279c7cc64a9SDavid Sterba btrfs_cleanup_pending_block_groups(trans); 22804fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22810e721106SJosef Bacik trans->block_rsv = NULL; 22820b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 228349b25e05SJeff Mahoney if (current->journal_info == trans) 228449b25e05SJeff Mahoney current->journal_info = NULL; 228597cb39bbSNikolay Borisov cleanup_transaction(trans, ret); 228649b25e05SJeff Mahoney 228749b25e05SJeff Mahoney return ret; 228879154b1bSChris Mason } 228979154b1bSChris Mason 2290d352ac68SChris Mason /* 22919d1a2a3aSDavid Sterba * return < 0 if error 22929d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 22939d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 22949d1a2a3aSDavid Sterba * 22959d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 22969d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 22979d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 22989d1a2a3aSDavid Sterba * few seconds later. 2299d352ac68SChris Mason */ 23009d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2301e9d0b13bSChris Mason { 23029d1a2a3aSDavid Sterba int ret; 23035d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2304e9d0b13bSChris Mason 2305a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23069d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23079d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23089d1a2a3aSDavid Sterba return 0; 23099d1a2a3aSDavid Sterba } 23109d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23119d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2312cfad392bSJosef Bacik list_del_init(&root->root_list); 2313a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23145d4f98a2SYan Zheng 23154fd786e6SMisono Tomohiro btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); 231676dda93cSYan, Zheng 231716cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 231816cdcec7SMiao Xie 231976dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 232076dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23212c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 232276dda93cSYan, Zheng else 23232c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 232432471dc2SDavid Sterba 23256596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2326e9d0b13bSChris Mason } 2327572d9ab7SDavid Sterba 2328572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2329572d9ab7SDavid Sterba { 2330572d9ab7SDavid Sterba unsigned long prev; 2331572d9ab7SDavid Sterba unsigned long bit; 2332572d9ab7SDavid Sterba 23336c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2334572d9ab7SDavid Sterba if (!prev) 2335572d9ab7SDavid Sterba return; 2336572d9ab7SDavid Sterba 23377e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23387e1876acSDavid Sterba if (prev & bit) 23397e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23407e1876acSDavid Sterba prev &= ~bit; 23417e1876acSDavid Sterba 23427e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23437e1876acSDavid Sterba if (prev & bit) 23447e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23457e1876acSDavid Sterba prev &= ~bit; 23467e1876acSDavid Sterba 2347d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2348d51033d0SDavid Sterba if (prev & bit) 2349d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2350d51033d0SDavid Sterba prev &= ~bit; 2351d51033d0SDavid Sterba 2352572d9ab7SDavid Sterba if (prev) 2353572d9ab7SDavid Sterba btrfs_warn(fs_info, 2354572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2355572d9ab7SDavid Sterba } 2356