16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2679154b1bSChris Mason #include "ctree.h" 2779154b1bSChris Mason #include "disk-io.h" 2879154b1bSChris Mason #include "transaction.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31581bb050SLi Zefan #include "inode-map.h" 32733f4fbbSStefan Behrens #include "volumes.h" 338dabb742SStefan Behrens #include "dev-replace.h" 34fcebe456SJosef Bacik #include "qgroup.h" 3579154b1bSChris Mason 360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 370f7d52f4SChris Mason 384a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 394a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 404a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 414a9d8bdeSMiao Xie __TRANS_START), 424a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 434a9d8bdeSMiao Xie __TRANS_START | 444a9d8bdeSMiao Xie __TRANS_ATTACH), 454a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 464a9d8bdeSMiao Xie __TRANS_START | 474a9d8bdeSMiao Xie __TRANS_ATTACH | 484a9d8bdeSMiao Xie __TRANS_JOIN), 494a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 504a9d8bdeSMiao Xie __TRANS_START | 514a9d8bdeSMiao Xie __TRANS_ATTACH | 524a9d8bdeSMiao Xie __TRANS_JOIN | 534a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 544a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 554a9d8bdeSMiao Xie __TRANS_START | 564a9d8bdeSMiao Xie __TRANS_ATTACH | 574a9d8bdeSMiao Xie __TRANS_JOIN | 584a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 594a9d8bdeSMiao Xie }; 604a9d8bdeSMiao Xie 61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 6279154b1bSChris Mason { 6313c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6413c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 65a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 66c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 676df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 686df9a95eSJosef Bacik struct extent_map *em; 696df9a95eSJosef Bacik 706df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 716df9a95eSJosef Bacik struct extent_map, list); 726df9a95eSJosef Bacik list_del_init(&em->list); 736df9a95eSJosef Bacik free_extent_map(em); 746df9a95eSJosef Bacik } 752c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7679154b1bSChris Mason } 7778fae27eSChris Mason } 7879154b1bSChris Mason 799e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 809e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 81817d52f8SJosef Bacik { 829e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 839e351cc8SJosef Bacik 849e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 859e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 869e351cc8SJosef Bacik dirty_list) { 879e351cc8SJosef Bacik list_del_init(&root->dirty_list); 88817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 89817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 909e351cc8SJosef Bacik if (is_fstree(root->objectid)) 919e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 929e351cc8SJosef Bacik } 939e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 94817d52f8SJosef Bacik } 95817d52f8SJosef Bacik 960860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 970860adfdSMiao Xie unsigned int type) 980860adfdSMiao Xie { 990860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1000860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1010860adfdSMiao Xie } 1020860adfdSMiao Xie 1030860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1040860adfdSMiao Xie unsigned int type) 1050860adfdSMiao Xie { 1060860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1070860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1080860adfdSMiao Xie } 1090860adfdSMiao Xie 1100860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1110860adfdSMiao Xie unsigned int type) 1120860adfdSMiao Xie { 1130860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1140860adfdSMiao Xie } 1150860adfdSMiao Xie 1160860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1170860adfdSMiao Xie { 1180860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 119178260b2SMiao Xie } 120178260b2SMiao Xie 121d352ac68SChris Mason /* 122d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 123d352ac68SChris Mason */ 1240860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 12579154b1bSChris Mason { 12679154b1bSChris Mason struct btrfs_transaction *cur_trans; 12719ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 128a4abeea4SJosef Bacik 12919ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 130d43317dcSChris Mason loop: 13149b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 13287533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 13319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 13449b25e05SJeff Mahoney return -EROFS; 13549b25e05SJeff Mahoney } 13649b25e05SJeff Mahoney 13719ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 138a4abeea4SJosef Bacik if (cur_trans) { 139871383beSDavid Sterba if (cur_trans->aborted) { 14019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 14149b25e05SJeff Mahoney return cur_trans->aborted; 142871383beSDavid Sterba } 1434a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 144178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 145178260b2SMiao Xie return -EBUSY; 146178260b2SMiao Xie } 147a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 148a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1490860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 15019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 151a4abeea4SJosef Bacik return 0; 152a4abeea4SJosef Bacik } 15319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 154a4abeea4SJosef Bacik 155354aa0fbSMiao Xie /* 156354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 157354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 158354aa0fbSMiao Xie */ 159354aa0fbSMiao Xie if (type == TRANS_ATTACH) 160354aa0fbSMiao Xie return -ENOENT; 161354aa0fbSMiao Xie 1624a9d8bdeSMiao Xie /* 1634a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1644a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1654a9d8bdeSMiao Xie */ 1664a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1674a9d8bdeSMiao Xie 168a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 169db5b493aSTsutomu Itoh if (!cur_trans) 170db5b493aSTsutomu Itoh return -ENOMEM; 171d43317dcSChris Mason 17219ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 17319ae4e81SJan Schmidt if (fs_info->running_transaction) { 174d43317dcSChris Mason /* 175d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 1764a9d8bdeSMiao Xie * to redo the checks above 177d43317dcSChris Mason */ 178a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 179d43317dcSChris Mason goto loop; 18087533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 181e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 1827b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 1837b8b92afSJosef Bacik return -EROFS; 184a4abeea4SJosef Bacik } 185d43317dcSChris Mason 18613c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 1870860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 18879154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 18979154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 1904a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 191a4abeea4SJosef Bacik /* 192a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 193a4abeea4SJosef Bacik * commit the transaction. 194a4abeea4SJosef Bacik */ 195a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 19608607c1bSChris Mason cur_trans->start_time = get_seconds(); 19756bec294SChris Mason 198c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 199d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 200c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 201c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 20256bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 203c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 20420b297d6SJan Schmidt 20520b297d6SJan Schmidt /* 20620b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 20720b297d6SJan Schmidt * the log must never go across transaction boundaries. 20820b297d6SJan Schmidt */ 20920b297d6SJan Schmidt smp_mb(); 21031b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 211efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 21220b297d6SJan Schmidt "creating a fresh transaction\n"); 21331b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 214efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 21520b297d6SJan Schmidt "creating a fresh transaction\n"); 216fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 21720b297d6SJan Schmidt 21856bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 21956bec294SChris Mason 2203063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2216df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2229e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 22319ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 224d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 22519ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 22619ae4e81SJan Schmidt fs_info->generation++; 22719ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 22819ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 22949b25e05SJeff Mahoney cur_trans->aborted = 0; 23019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 23115ee9bc7SJosef Bacik 23279154b1bSChris Mason return 0; 23379154b1bSChris Mason } 23479154b1bSChris Mason 235d352ac68SChris Mason /* 236d397712bSChris Mason * this does all the record keeping required to make sure that a reference 237d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 238d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 239d397712bSChris Mason * when the transaction commits 240d352ac68SChris Mason */ 2417585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2425d4f98a2SYan Zheng struct btrfs_root *root) 2436702ed49SChris Mason { 24427cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 24527cdeb70SMiao Xie root->last_trans < trans->transid) { 2466702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2475d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2485d4f98a2SYan Zheng 2497585717fSChris Mason /* 25027cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2517585717fSChris Mason * we have the reloc mutex held now, so there 2527585717fSChris Mason * is only one writer in this function 2537585717fSChris Mason */ 25427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2557585717fSChris Mason 25627cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2577585717fSChris Mason * they find our root->last_trans update 2587585717fSChris Mason */ 2597585717fSChris Mason smp_wmb(); 2607585717fSChris Mason 261a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 262a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 263a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 264a4abeea4SJosef Bacik return 0; 265a4abeea4SJosef Bacik } 2666702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 2676702ed49SChris Mason (unsigned long)root->root_key.objectid, 2686702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 269a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 2707585717fSChris Mason root->last_trans = trans->transid; 2717585717fSChris Mason 2727585717fSChris Mason /* this is pretty tricky. We don't want to 2737585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 2747585717fSChris Mason * unless we're really doing the first setup for this root in 2757585717fSChris Mason * this transaction. 2767585717fSChris Mason * 2777585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 2787585717fSChris Mason * if we want to take the expensive mutex. 2797585717fSChris Mason * 2807585717fSChris Mason * But, we have to set root->last_trans before we 2817585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 2827585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 28327cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 2847585717fSChris Mason * fixing up the reloc trees and everyone must wait. 2857585717fSChris Mason * 2867585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 2877585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 2887585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 2897585717fSChris Mason * done before we pop in the zero below 2907585717fSChris Mason */ 2915d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 292c7548af6SChris Mason smp_mb__before_atomic(); 29327cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2946702ed49SChris Mason } 2955d4f98a2SYan Zheng return 0; 2966702ed49SChris Mason } 2975d4f98a2SYan Zheng 2987585717fSChris Mason 2997585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3007585717fSChris Mason struct btrfs_root *root) 3017585717fSChris Mason { 30227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3037585717fSChris Mason return 0; 3047585717fSChris Mason 3057585717fSChris Mason /* 30627cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3077585717fSChris Mason * and barriers 3087585717fSChris Mason */ 3097585717fSChris Mason smp_rmb(); 3107585717fSChris Mason if (root->last_trans == trans->transid && 31127cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3127585717fSChris Mason return 0; 3137585717fSChris Mason 3147585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3157585717fSChris Mason record_root_in_trans(trans, root); 3167585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3177585717fSChris Mason 3187585717fSChris Mason return 0; 3197585717fSChris Mason } 3207585717fSChris Mason 3214a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3224a9d8bdeSMiao Xie { 3234a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 324501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 325501407aaSJosef Bacik !trans->aborted); 3264a9d8bdeSMiao Xie } 3274a9d8bdeSMiao Xie 328d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 329d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 330d352ac68SChris Mason * transaction might not be fully on disk. 331d352ac68SChris Mason */ 33237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 33379154b1bSChris Mason { 334f9295749SChris Mason struct btrfs_transaction *cur_trans; 33579154b1bSChris Mason 336a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 337f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3384a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 33913c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 340a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 34172d63ed6SLi Zefan 34272d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 343501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 344501407aaSJosef Bacik cur_trans->aborted); 345724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 346a4abeea4SJosef Bacik } else { 347a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 348f9295749SChris Mason } 34937d1aeeeSChris Mason } 35037d1aeeeSChris Mason 351a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 35237d1aeeeSChris Mason { 353a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 354a4abeea4SJosef Bacik return 0; 355a4abeea4SJosef Bacik 356a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 357a22285a6SYan, Zheng return 1; 358a4abeea4SJosef Bacik 359a4abeea4SJosef Bacik if (type == TRANS_START && 360a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 361a4abeea4SJosef Bacik return 1; 362a4abeea4SJosef Bacik 363a22285a6SYan, Zheng return 0; 364a22285a6SYan, Zheng } 365a22285a6SYan, Zheng 36620dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 36720dd2cbfSMiao Xie { 36820dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 36927cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 37020dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 37120dd2cbfSMiao Xie root->reloc_root) 37220dd2cbfSMiao Xie return false; 37320dd2cbfSMiao Xie 37420dd2cbfSMiao Xie return true; 37520dd2cbfSMiao Xie } 37620dd2cbfSMiao Xie 37708e007d2SMiao Xie static struct btrfs_trans_handle * 3780860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 37908e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 380a22285a6SYan, Zheng { 381a22285a6SYan, Zheng struct btrfs_trans_handle *h; 382a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 383b5009945SJosef Bacik u64 num_bytes = 0; 384c5567237SArne Jansen u64 qgroup_reserved = 0; 38520dd2cbfSMiao Xie bool reloc_reserved = false; 38620dd2cbfSMiao Xie int ret; 387acce952bSliubo 38846c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 3892755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 39046c4e71eSFilipe Manana 39187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 392acce952bSliubo return ERR_PTR(-EROFS); 3932a1eb461SJosef Bacik 39446c4e71eSFilipe Manana if (current->journal_info) { 3950860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 3962a1eb461SJosef Bacik h = current->journal_info; 3972a1eb461SJosef Bacik h->use_count++; 398b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 3992a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4002a1eb461SJosef Bacik h->block_rsv = NULL; 4012a1eb461SJosef Bacik goto got_it; 4022a1eb461SJosef Bacik } 403b5009945SJosef Bacik 404b5009945SJosef Bacik /* 405b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 406b5009945SJosef Bacik * the appropriate flushing if need be. 407b5009945SJosef Bacik */ 408b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 409c5567237SArne Jansen if (root->fs_info->quota_enabled && 410c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 411707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 412c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 413c5567237SArne Jansen if (ret) 414c5567237SArne Jansen return ERR_PTR(ret); 415c5567237SArne Jansen } 416c5567237SArne Jansen 417b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 41820dd2cbfSMiao Xie /* 41920dd2cbfSMiao Xie * Do the reservation for the relocation root creation 42020dd2cbfSMiao Xie */ 421ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 42220dd2cbfSMiao Xie num_bytes += root->nodesize; 42320dd2cbfSMiao Xie reloc_reserved = true; 42420dd2cbfSMiao Xie } 42520dd2cbfSMiao Xie 4264a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 427b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 42808e007d2SMiao Xie num_bytes, flush); 429b5009945SJosef Bacik if (ret) 430843fcf35SMiao Xie goto reserve_fail; 431b5009945SJosef Bacik } 432a22285a6SYan, Zheng again: 433a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 434843fcf35SMiao Xie if (!h) { 435843fcf35SMiao Xie ret = -ENOMEM; 436843fcf35SMiao Xie goto alloc_fail; 437843fcf35SMiao Xie } 438a22285a6SYan, Zheng 43998114659SJosef Bacik /* 44098114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 44198114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 44298114659SJosef Bacik * because we're already holding a ref. We need this because we could 44398114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 44498114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 445354aa0fbSMiao Xie * 446354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 447354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 44898114659SJosef Bacik */ 4490860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 450b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 451b2b5ef5cSJan Kara 452a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 45337d1aeeeSChris Mason wait_current_trans(root); 454a22285a6SYan, Zheng 455a4abeea4SJosef Bacik do { 456354aa0fbSMiao Xie ret = join_transaction(root, type); 457178260b2SMiao Xie if (ret == -EBUSY) { 458a4abeea4SJosef Bacik wait_current_trans(root); 459178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 460178260b2SMiao Xie ret = -ENOENT; 461178260b2SMiao Xie } 462a4abeea4SJosef Bacik } while (ret == -EBUSY); 463a4abeea4SJosef Bacik 464db5b493aSTsutomu Itoh if (ret < 0) { 465354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 466354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 467843fcf35SMiao Xie goto join_fail; 468db5b493aSTsutomu Itoh } 4690f7d52f4SChris Mason 470a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 471a22285a6SYan, Zheng 472a22285a6SYan, Zheng h->transid = cur_trans->transid; 473a22285a6SYan, Zheng h->transaction = cur_trans; 47479154b1bSChris Mason h->blocks_used = 0; 475a22285a6SYan, Zheng h->bytes_reserved = 0; 476d13603efSArne Jansen h->root = root; 47756bec294SChris Mason h->delayed_ref_updates = 0; 4782a1eb461SJosef Bacik h->use_count = 1; 4790e721106SJosef Bacik h->adding_csums = 0; 480f0486c68SYan, Zheng h->block_rsv = NULL; 4812a1eb461SJosef Bacik h->orig_rsv = NULL; 48249b25e05SJeff Mahoney h->aborted = 0; 4834b824906SMiao Xie h->qgroup_reserved = 0; 484bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 485a698d075SMiao Xie h->type = type; 486c6b305a8SJosef Bacik h->allocating_chunk = false; 48720dd2cbfSMiao Xie h->reloc_reserved = false; 4885039eddcSJosef Bacik h->sync = false; 489bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 490ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 491b7ec40d7SChris Mason 492a22285a6SYan, Zheng smp_mb(); 4934a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 4944a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 495abdd2e80SFilipe Manana current->journal_info = h; 496a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 497a22285a6SYan, Zheng goto again; 498a22285a6SYan, Zheng } 4999ed74f2dSJosef Bacik 500b5009945SJosef Bacik if (num_bytes) { 5018c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5022bcc0328SLiu Bo h->transid, num_bytes, 1); 503b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 504b5009945SJosef Bacik h->bytes_reserved = num_bytes; 50520dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 506a22285a6SYan, Zheng } 5074b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 508a22285a6SYan, Zheng 5092a1eb461SJosef Bacik got_it: 510a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 511a22285a6SYan, Zheng 512a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 513a22285a6SYan, Zheng current->journal_info = h; 51479154b1bSChris Mason return h; 515843fcf35SMiao Xie 516843fcf35SMiao Xie join_fail: 5170860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 518843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 519843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 520843fcf35SMiao Xie alloc_fail: 521843fcf35SMiao Xie if (num_bytes) 522843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 523843fcf35SMiao Xie num_bytes); 524843fcf35SMiao Xie reserve_fail: 525843fcf35SMiao Xie if (qgroup_reserved) 526843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 527843fcf35SMiao Xie return ERR_PTR(ret); 52879154b1bSChris Mason } 52979154b1bSChris Mason 530f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 531a22285a6SYan, Zheng int num_items) 532f9295749SChris Mason { 53308e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 53408e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 535f9295749SChris Mason } 5368407aa46SMiao Xie 53708e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 5388407aa46SMiao Xie struct btrfs_root *root, int num_items) 5398407aa46SMiao Xie { 54008e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 54108e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 5428407aa46SMiao Xie } 5438407aa46SMiao Xie 5447a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 545f9295749SChris Mason { 5468407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 547f9295749SChris Mason } 548f9295749SChris Mason 5497a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5500af3d00bSJosef Bacik { 5518407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5520af3d00bSJosef Bacik } 5530af3d00bSJosef Bacik 5547a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5559ca9ee09SSage Weil { 5568407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5579ca9ee09SSage Weil } 5589ca9ee09SSage Weil 559d4edf39bSMiao Xie /* 560d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 561d4edf39bSMiao Xie * 562d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 563d4edf39bSMiao Xie * don't want to start a new one. 564d4edf39bSMiao Xie * 565d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 566d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 567d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 568d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 569d4edf39bSMiao Xie * invoke 570d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 571d4edf39bSMiao Xie */ 572354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 57360376ce4SJosef Bacik { 574354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 57560376ce4SJosef Bacik } 57660376ce4SJosef Bacik 577d4edf39bSMiao Xie /* 57890b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 579d4edf39bSMiao Xie * 580d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 581d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 582d4edf39bSMiao Xie * complete. 583d4edf39bSMiao Xie */ 584d4edf39bSMiao Xie struct btrfs_trans_handle * 585d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 586d4edf39bSMiao Xie { 587d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 588d4edf39bSMiao Xie 589d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 590d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 591d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 592d4edf39bSMiao Xie 593d4edf39bSMiao Xie return trans; 594d4edf39bSMiao Xie } 595d4edf39bSMiao Xie 596d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 597b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 59889ce8a63SChris Mason struct btrfs_transaction *commit) 59989ce8a63SChris Mason { 6004a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 60189ce8a63SChris Mason } 60289ce8a63SChris Mason 60346204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 60446204592SSage Weil { 60546204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6068cd2807fSMiao Xie int ret = 0; 60746204592SSage Weil 60846204592SSage Weil if (transid) { 60946204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 610a4abeea4SJosef Bacik goto out; 61146204592SSage Weil 61246204592SSage Weil /* find specified transaction */ 613a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 61446204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 61546204592SSage Weil if (t->transid == transid) { 61646204592SSage Weil cur_trans = t; 617a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 6188cd2807fSMiao Xie ret = 0; 61946204592SSage Weil break; 62046204592SSage Weil } 6218cd2807fSMiao Xie if (t->transid > transid) { 6228cd2807fSMiao Xie ret = 0; 62346204592SSage Weil break; 62446204592SSage Weil } 6258cd2807fSMiao Xie } 626a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 62742383020SSage Weil 62842383020SSage Weil /* 62942383020SSage Weil * The specified transaction doesn't exist, or we 63042383020SSage Weil * raced with btrfs_commit_transaction 63142383020SSage Weil */ 63242383020SSage Weil if (!cur_trans) { 63342383020SSage Weil if (transid > root->fs_info->last_trans_committed) 63442383020SSage Weil ret = -EINVAL; 6358cd2807fSMiao Xie goto out; 63642383020SSage Weil } 63746204592SSage Weil } else { 63846204592SSage Weil /* find newest transaction that is committing | committed */ 639a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 64046204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 64146204592SSage Weil list) { 6424a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 6434a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 6443473f3c0SJosef Bacik break; 64546204592SSage Weil cur_trans = t; 646a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 64746204592SSage Weil break; 64846204592SSage Weil } 64946204592SSage Weil } 650a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 65146204592SSage Weil if (!cur_trans) 652a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 65346204592SSage Weil } 65446204592SSage Weil 65546204592SSage Weil wait_for_commit(root, cur_trans); 656724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 657a4abeea4SJosef Bacik out: 65846204592SSage Weil return ret; 65946204592SSage Weil } 66046204592SSage Weil 66137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 66237d1aeeeSChris Mason { 663a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 66437d1aeeeSChris Mason wait_current_trans(root); 66537d1aeeeSChris Mason } 66637d1aeeeSChris Mason 6678929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 6688929ecfaSYan, Zheng struct btrfs_root *root) 6698929ecfaSYan, Zheng { 6701be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 6710a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 6721be41b78SJosef Bacik return 1; 67336ba022aSJosef Bacik 6741be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 6758929ecfaSYan, Zheng } 6768929ecfaSYan, Zheng 6778929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 6788929ecfaSYan, Zheng struct btrfs_root *root) 6798929ecfaSYan, Zheng { 6808929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 6818929ecfaSYan, Zheng int updates; 68249b25e05SJeff Mahoney int err; 6838929ecfaSYan, Zheng 684a4abeea4SJosef Bacik smp_mb(); 6854a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 6864a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 6878929ecfaSYan, Zheng return 1; 6888929ecfaSYan, Zheng 6898929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 6908929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 69149b25e05SJeff Mahoney if (updates) { 69249b25e05SJeff Mahoney err = btrfs_run_delayed_refs(trans, root, updates); 69349b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 69449b25e05SJeff Mahoney return err; 69549b25e05SJeff Mahoney } 6968929ecfaSYan, Zheng 6978929ecfaSYan, Zheng return should_end_transaction(trans, root); 6988929ecfaSYan, Zheng } 6998929ecfaSYan, Zheng 70089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 701a698d075SMiao Xie struct btrfs_root *root, int throttle) 70279154b1bSChris Mason { 7038929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 704ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 7051be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 706a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 7074edc2ca3SDave Jones int err = 0; 708a79b7d4bSChris Mason int must_run_delayed_refs = 0; 709d6e4a428SChris Mason 7103bbb24b2SJosef Bacik if (trans->use_count > 1) { 7113bbb24b2SJosef Bacik trans->use_count--; 7122a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 7132a1eb461SJosef Bacik return 0; 7142a1eb461SJosef Bacik } 7152a1eb461SJosef Bacik 716b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 7174c13d758SJosef Bacik trans->block_rsv = NULL; 718c5567237SArne Jansen 719ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 720ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 721ea658badSJosef Bacik 722c3e69d58SChris Mason trans->delayed_ref_updates = 0; 723a79b7d4bSChris Mason if (!trans->sync) { 724a79b7d4bSChris Mason must_run_delayed_refs = 725a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 7260a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 727a79b7d4bSChris Mason 728a79b7d4bSChris Mason /* 729a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 730a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 731a79b7d4bSChris Mason */ 732a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 733a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 734a79b7d4bSChris Mason must_run_delayed_refs = 2; 73556bec294SChris Mason } 736bb721703SChris Mason 737fcebe456SJosef Bacik if (trans->qgroup_reserved) { 738fcebe456SJosef Bacik /* 739fcebe456SJosef Bacik * the same root has to be passed here between start_transaction 740fcebe456SJosef Bacik * and end_transaction. Subvolume quota depends on this. 741fcebe456SJosef Bacik */ 742fcebe456SJosef Bacik btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 743fcebe456SJosef Bacik trans->qgroup_reserved = 0; 744fcebe456SJosef Bacik } 745fcebe456SJosef Bacik 7460e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 7470e721106SJosef Bacik trans->block_rsv = NULL; 74856bec294SChris Mason 749ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 750ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 751ea658badSJosef Bacik 752a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 7534a9d8bdeSMiao Xie should_end_transaction(trans, root) && 7544a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 7554a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 7564a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 7574a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 7584a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 759a4abeea4SJosef Bacik } 7608929ecfaSYan, Zheng 7614a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 7623bbb24b2SJosef Bacik if (throttle) 7638929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 7643bbb24b2SJosef Bacik else 7658929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 7668929ecfaSYan, Zheng } 7678929ecfaSYan, Zheng 7680860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 7696df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 7706df7881aSJosef Bacik 7718929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 77213c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 77313c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 7740860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 77589ce8a63SChris Mason 77699d16cbcSSage Weil smp_mb(); 77779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 77879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 779724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 7809ed74f2dSJosef Bacik 7819ed74f2dSJosef Bacik if (current->journal_info == trans) 7829ed74f2dSJosef Bacik current->journal_info = NULL; 783ab78c84dSChris Mason 78424bbcf04SYan, Zheng if (throttle) 78524bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 78624bbcf04SYan, Zheng 78749b25e05SJeff Mahoney if (trans->aborted || 7884e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 7894e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 7904edc2ca3SDave Jones err = -EIO; 7914e121c06SJosef Bacik } 792edf39272SJan Schmidt assert_qgroups_uptodate(trans); 79349b25e05SJeff Mahoney 7944edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 795a79b7d4bSChris Mason if (must_run_delayed_refs) { 796a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 797a79b7d4bSChris Mason must_run_delayed_refs == 1); 798a79b7d4bSChris Mason } 7994edc2ca3SDave Jones return err; 80079154b1bSChris Mason } 80179154b1bSChris Mason 80289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 80389ce8a63SChris Mason struct btrfs_root *root) 80489ce8a63SChris Mason { 80598ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 80689ce8a63SChris Mason } 80789ce8a63SChris Mason 80889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 80989ce8a63SChris Mason struct btrfs_root *root) 81089ce8a63SChris Mason { 81198ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 81216cdcec7SMiao Xie } 81316cdcec7SMiao Xie 814d352ac68SChris Mason /* 815d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 816d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 817690587d1SChris Mason * those extents are sent to disk but does not wait on them 818d352ac68SChris Mason */ 819690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 8208cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 82179154b1bSChris Mason { 822777e6bd7SChris Mason int err = 0; 8237c4452b9SChris Mason int werr = 0; 8241728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 825e6138876SJosef Bacik struct extent_state *cached_state = NULL; 826777e6bd7SChris Mason u64 start = 0; 8275f39d397SChris Mason u64 end; 8287c4452b9SChris Mason 8291728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 830e6138876SJosef Bacik mark, &cached_state)) { 831e6138876SJosef Bacik convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 832e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 833e6138876SJosef Bacik cached_state = NULL; 8341728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 8357c4452b9SChris Mason if (err) 8367c4452b9SChris Mason werr = err; 8371728366eSJosef Bacik cond_resched(); 8381728366eSJosef Bacik start = end + 1; 8397c4452b9SChris Mason } 840690587d1SChris Mason if (err) 841690587d1SChris Mason werr = err; 842690587d1SChris Mason return werr; 843690587d1SChris Mason } 844690587d1SChris Mason 845690587d1SChris Mason /* 846690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 847690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 848690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 849690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 850690587d1SChris Mason */ 851690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 8528cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 853690587d1SChris Mason { 854690587d1SChris Mason int err = 0; 855690587d1SChris Mason int werr = 0; 8561728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 857e6138876SJosef Bacik struct extent_state *cached_state = NULL; 858690587d1SChris Mason u64 start = 0; 859690587d1SChris Mason u64 end; 860656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 861656f30dbSFilipe Manana bool errors = false; 862690587d1SChris Mason 8631728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 864e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 865e6138876SJosef Bacik clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 866e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 8671728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 868777e6bd7SChris Mason if (err) 869777e6bd7SChris Mason werr = err; 870777e6bd7SChris Mason cond_resched(); 8711728366eSJosef Bacik start = end + 1; 872777e6bd7SChris Mason } 8737c4452b9SChris Mason if (err) 8747c4452b9SChris Mason werr = err; 875656f30dbSFilipe Manana 876656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 877656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 878656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 879656f30dbSFilipe Manana &btree_ino->runtime_flags)) 880656f30dbSFilipe Manana errors = true; 881656f30dbSFilipe Manana 882656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 883656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 884656f30dbSFilipe Manana &btree_ino->runtime_flags)) 885656f30dbSFilipe Manana errors = true; 886656f30dbSFilipe Manana } else { 887656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 888656f30dbSFilipe Manana &btree_ino->runtime_flags)) 889656f30dbSFilipe Manana errors = true; 890656f30dbSFilipe Manana } 891656f30dbSFilipe Manana 892656f30dbSFilipe Manana if (errors && !werr) 893656f30dbSFilipe Manana werr = -EIO; 894656f30dbSFilipe Manana 8957c4452b9SChris Mason return werr; 89679154b1bSChris Mason } 89779154b1bSChris Mason 898690587d1SChris Mason /* 899690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 900690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 901690587d1SChris Mason * those extents are on disk for transaction or log commit 902690587d1SChris Mason */ 903171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 9048cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 905690587d1SChris Mason { 906690587d1SChris Mason int ret; 907690587d1SChris Mason int ret2; 908c6adc9ccSMiao Xie struct blk_plug plug; 909690587d1SChris Mason 910c6adc9ccSMiao Xie blk_start_plug(&plug); 9118cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 912c6adc9ccSMiao Xie blk_finish_plug(&plug); 9138cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 914bf0da8c1SChris Mason 915bf0da8c1SChris Mason if (ret) 916bf0da8c1SChris Mason return ret; 917bf0da8c1SChris Mason if (ret2) 918bf0da8c1SChris Mason return ret2; 919bf0da8c1SChris Mason return 0; 920690587d1SChris Mason } 921690587d1SChris Mason 922d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 923d0c803c4SChris Mason struct btrfs_root *root) 924d0c803c4SChris Mason { 925d0c803c4SChris Mason if (!trans || !trans->transaction) { 926d0c803c4SChris Mason struct inode *btree_inode; 927d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 928d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 929d0c803c4SChris Mason } 930d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 9318cef4e16SYan, Zheng &trans->transaction->dirty_pages, 9328cef4e16SYan, Zheng EXTENT_DIRTY); 933d0c803c4SChris Mason } 934d0c803c4SChris Mason 935d352ac68SChris Mason /* 936d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 937d352ac68SChris Mason * 938d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 939d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 940d352ac68SChris Mason * allocation tree. 941d352ac68SChris Mason * 942d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 943d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 944d352ac68SChris Mason */ 9450b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 94679154b1bSChris Mason struct btrfs_root *root) 94779154b1bSChris Mason { 94879154b1bSChris Mason int ret; 9490b86a832SChris Mason u64 old_root_bytenr; 95086b9f2ecSYan, Zheng u64 old_root_used; 9510b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 95279154b1bSChris Mason 95386b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9540b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 95556bec294SChris Mason 95679154b1bSChris Mason while (1) { 9570b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 95886b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 95986b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 96079154b1bSChris Mason break; 96187ef2bb4SChris Mason 9625d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 96379154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 9640b86a832SChris Mason &root->root_key, 9650b86a832SChris Mason &root->root_item); 96649b25e05SJeff Mahoney if (ret) 96749b25e05SJeff Mahoney return ret; 96856bec294SChris Mason 96986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9704a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 97149b25e05SJeff Mahoney if (ret) 97249b25e05SJeff Mahoney return ret; 9730b86a832SChris Mason } 974276e680dSYan Zheng 9750b86a832SChris Mason return 0; 9760b86a832SChris Mason } 9770b86a832SChris Mason 978d352ac68SChris Mason /* 979d352ac68SChris Mason * update all the cowonly tree roots on disk 98049b25e05SJeff Mahoney * 98149b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 98249b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 98349b25e05SJeff Mahoney * to clean up the delayed refs. 984d352ac68SChris Mason */ 9855d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 9860b86a832SChris Mason struct btrfs_root *root) 9870b86a832SChris Mason { 9880b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 9890b86a832SChris Mason struct list_head *next; 99084234f3aSYan Zheng struct extent_buffer *eb; 99156bec294SChris Mason int ret; 99284234f3aSYan Zheng 99356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 99449b25e05SJeff Mahoney if (ret) 99549b25e05SJeff Mahoney return ret; 99687ef2bb4SChris Mason 99784234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 99849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 99949b25e05SJeff Mahoney 0, &eb); 100084234f3aSYan Zheng btrfs_tree_unlock(eb); 100184234f3aSYan Zheng free_extent_buffer(eb); 10020b86a832SChris Mason 100349b25e05SJeff Mahoney if (ret) 100449b25e05SJeff Mahoney return ret; 100549b25e05SJeff Mahoney 100656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 100749b25e05SJeff Mahoney if (ret) 100849b25e05SJeff Mahoney return ret; 100987ef2bb4SChris Mason 1010733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1011c16ce190SJosef Bacik if (ret) 1012c16ce190SJosef Bacik return ret; 10138dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1014c16ce190SJosef Bacik if (ret) 1015c16ce190SJosef Bacik return ret; 1016546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1017c16ce190SJosef Bacik if (ret) 1018c16ce190SJosef Bacik return ret; 1019546adb0dSJan Schmidt 1020546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1021546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1022c16ce190SJosef Bacik if (ret) 1023c16ce190SJosef Bacik return ret; 1024546adb0dSJan Schmidt 10250b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 10260b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 10270b86a832SChris Mason list_del_init(next); 10280b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 102987ef2bb4SChris Mason 10309e351cc8SJosef Bacik if (root != fs_info->extent_root) 10319e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 10329e351cc8SJosef Bacik &trans->transaction->switch_commits); 103349b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 103449b25e05SJeff Mahoney if (ret) 103549b25e05SJeff Mahoney return ret; 103679154b1bSChris Mason } 1037276e680dSYan Zheng 10389e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 10399e351cc8SJosef Bacik &trans->transaction->switch_commits); 10408dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 10418dabb742SStefan Behrens 104279154b1bSChris Mason return 0; 104379154b1bSChris Mason } 104479154b1bSChris Mason 1045d352ac68SChris Mason /* 1046d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1047d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1048d352ac68SChris Mason * be deleted 1049d352ac68SChris Mason */ 1050cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 10515eda7b5eSChris Mason { 1052a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1053cfad392bSJosef Bacik if (list_empty(&root->root_list)) 10549d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1055a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 10565eda7b5eSChris Mason } 10575eda7b5eSChris Mason 1058d352ac68SChris Mason /* 10595d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1060d352ac68SChris Mason */ 10615d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 10625d4f98a2SYan Zheng struct btrfs_root *root) 10630f7d52f4SChris Mason { 10640f7d52f4SChris Mason struct btrfs_root *gang[8]; 10655d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 10660f7d52f4SChris Mason int i; 10670f7d52f4SChris Mason int ret; 106854aa1f4dSChris Mason int err = 0; 106954aa1f4dSChris Mason 1070a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 10710f7d52f4SChris Mason while (1) { 10725d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 10735d4f98a2SYan Zheng (void **)gang, 0, 10740f7d52f4SChris Mason ARRAY_SIZE(gang), 10750f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 10760f7d52f4SChris Mason if (ret == 0) 10770f7d52f4SChris Mason break; 10780f7d52f4SChris Mason for (i = 0; i < ret; i++) { 10790f7d52f4SChris Mason root = gang[i]; 10805d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 10812619ba1fSChris Mason (unsigned long)root->root_key.objectid, 10820f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1083a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 108431153d81SYan Zheng 1085e02119d5SChris Mason btrfs_free_log(trans, root); 10865d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1087d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1088e02119d5SChris Mason 108982d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 109082d5902dSLi Zefan 1091f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 109227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1093c7548af6SChris Mason smp_mb__after_atomic(); 1094f1ebcc74SLiu Bo 1095978d910dSYan Zheng if (root->commit_root != root->node) { 10969e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 10979e351cc8SJosef Bacik &trans->transaction->switch_commits); 1098978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1099978d910dSYan Zheng root->node); 1100978d910dSYan Zheng } 110131153d81SYan Zheng 11025d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 11030f7d52f4SChris Mason &root->root_key, 11040f7d52f4SChris Mason &root->root_item); 1105a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 110654aa1f4dSChris Mason if (err) 110754aa1f4dSChris Mason break; 11080f7d52f4SChris Mason } 11099f3a7427SChris Mason } 1110a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 111154aa1f4dSChris Mason return err; 11120f7d52f4SChris Mason } 11130f7d52f4SChris Mason 1114d352ac68SChris Mason /* 1115de78b51aSEric Sandeen * defrag a given btree. 1116de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1117d352ac68SChris Mason */ 1118de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1119e9d0b13bSChris Mason { 1120e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1121e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 11228929ecfaSYan, Zheng int ret; 1123e9d0b13bSChris Mason 112427cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1125e9d0b13bSChris Mason return 0; 11268929ecfaSYan, Zheng 11276b80053dSChris Mason while (1) { 11288929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 11298929ecfaSYan, Zheng if (IS_ERR(trans)) 11308929ecfaSYan, Zheng return PTR_ERR(trans); 11318929ecfaSYan, Zheng 1132de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 11338929ecfaSYan, Zheng 1134e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1135b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1136e9d0b13bSChris Mason cond_resched(); 1137e9d0b13bSChris Mason 11387841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1139e9d0b13bSChris Mason break; 1140210549ebSDavid Sterba 1141210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1142efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1143210549ebSDavid Sterba ret = -EAGAIN; 1144210549ebSDavid Sterba break; 1145210549ebSDavid Sterba } 1146e9d0b13bSChris Mason } 114727cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 11488929ecfaSYan, Zheng return ret; 1149e9d0b13bSChris Mason } 1150e9d0b13bSChris Mason 1151d352ac68SChris Mason /* 1152d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1153aec8030aSMiao Xie * transaction commit. This does the actual creation. 1154aec8030aSMiao Xie * 1155aec8030aSMiao Xie * Note: 1156aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1157aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1158aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1159d352ac68SChris Mason */ 116080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 11613063d29fSChris Mason struct btrfs_fs_info *fs_info, 11623063d29fSChris Mason struct btrfs_pending_snapshot *pending) 11633063d29fSChris Mason { 11643063d29fSChris Mason struct btrfs_key key; 116580b6794dSChris Mason struct btrfs_root_item *new_root_item; 11663063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 11673063d29fSChris Mason struct btrfs_root *root = pending->root; 11686bdb72deSSage Weil struct btrfs_root *parent_root; 116998c9942aSLiu Bo struct btrfs_block_rsv *rsv; 11706bdb72deSSage Weil struct inode *parent_inode; 117142874b3dSMiao Xie struct btrfs_path *path; 117242874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1173a22285a6SYan, Zheng struct dentry *dentry; 11743063d29fSChris Mason struct extent_buffer *tmp; 1175925baeddSChris Mason struct extent_buffer *old; 11768ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1177aec8030aSMiao Xie int ret = 0; 1178d68fc57bSYan, Zheng u64 to_reserve = 0; 11796bdb72deSSage Weil u64 index = 0; 1180a22285a6SYan, Zheng u64 objectid; 1181b83cc969SLi Zefan u64 root_flags; 11828ea05e3aSAlexander Block uuid_le new_uuid; 11833063d29fSChris Mason 118442874b3dSMiao Xie path = btrfs_alloc_path(); 118542874b3dSMiao Xie if (!path) { 1186aec8030aSMiao Xie pending->error = -ENOMEM; 1187aec8030aSMiao Xie return 0; 118842874b3dSMiao Xie } 118942874b3dSMiao Xie 119080b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 119180b6794dSChris Mason if (!new_root_item) { 1192aec8030aSMiao Xie pending->error = -ENOMEM; 11936fa9700eSMiao Xie goto root_item_alloc_fail; 119480b6794dSChris Mason } 1195a22285a6SYan, Zheng 1196aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1197aec8030aSMiao Xie if (pending->error) 11986fa9700eSMiao Xie goto no_free_objectid; 11993063d29fSChris Mason 12003fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1201d68fc57bSYan, Zheng 1202d68fc57bSYan, Zheng if (to_reserve > 0) { 1203aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1204aec8030aSMiao Xie &pending->block_rsv, 120508e007d2SMiao Xie to_reserve, 120608e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1207aec8030aSMiao Xie if (pending->error) 12086fa9700eSMiao Xie goto no_free_objectid; 1209d68fc57bSYan, Zheng } 1210d68fc57bSYan, Zheng 12113063d29fSChris Mason key.objectid = objectid; 1212a22285a6SYan, Zheng key.offset = (u64)-1; 1213a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 12143063d29fSChris Mason 12156fa9700eSMiao Xie rsv = trans->block_rsv; 1216a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 12172382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 12186bdb72deSSage Weil 1219a22285a6SYan, Zheng dentry = pending->dentry; 1220e9662f70SMiao Xie parent_inode = pending->dir; 1221a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 12227585717fSChris Mason record_root_in_trans(trans, parent_root); 1223a22285a6SYan, Zheng 12246bdb72deSSage Weil /* 12256bdb72deSSage Weil * insert the directory item 12266bdb72deSSage Weil */ 12276bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 122849b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 122942874b3dSMiao Xie 123042874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 123142874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 123242874b3dSMiao Xie btrfs_ino(parent_inode), 123342874b3dSMiao Xie dentry->d_name.name, 123442874b3dSMiao Xie dentry->d_name.len, 0); 123542874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1236fe66a05aSChris Mason pending->error = -EEXIST; 1237aec8030aSMiao Xie goto dir_item_existed; 123842874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 123942874b3dSMiao Xie ret = PTR_ERR(dir_item); 12408732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12418732d44fSMiao Xie goto fail; 124279787eaaSJeff Mahoney } 124342874b3dSMiao Xie btrfs_release_path(path); 12446bdb72deSSage Weil 1245e999376fSChris Mason /* 1246e999376fSChris Mason * pull in the delayed directory update 1247e999376fSChris Mason * and the delayed inode item 1248e999376fSChris Mason * otherwise we corrupt the FS during 1249e999376fSChris Mason * snapshot 1250e999376fSChris Mason */ 1251e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 12528732d44fSMiao Xie if (ret) { /* Transaction aborted */ 12538732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12548732d44fSMiao Xie goto fail; 12558732d44fSMiao Xie } 1256e999376fSChris Mason 12577585717fSChris Mason record_root_in_trans(trans, root); 12586bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 12596bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 126008fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 12616bdb72deSSage Weil 1262b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1263b83cc969SLi Zefan if (pending->readonly) 1264b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1265b83cc969SLi Zefan else 1266b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1267b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1268b83cc969SLi Zefan 12698ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 12708ea05e3aSAlexander Block trans->transid); 12718ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 12728ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 12738ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 12748ea05e3aSAlexander Block BTRFS_UUID_SIZE); 127570023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 127670023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 127770023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 12788ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 12798ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 12808ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 12818ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 128270023da2SStefan Behrens } 12833cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 12843cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 128570023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 12868ea05e3aSAlexander Block 1287925baeddSChris Mason old = btrfs_lock_root_node(root); 128849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 128979787eaaSJeff Mahoney if (ret) { 129079787eaaSJeff Mahoney btrfs_tree_unlock(old); 129179787eaaSJeff Mahoney free_extent_buffer(old); 12928732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12938732d44fSMiao Xie goto fail; 129479787eaaSJeff Mahoney } 129549b25e05SJeff Mahoney 12965d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 12973063d29fSChris Mason 129849b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 129979787eaaSJeff Mahoney /* clean up in any case */ 1300925baeddSChris Mason btrfs_tree_unlock(old); 1301925baeddSChris Mason free_extent_buffer(old); 13028732d44fSMiao Xie if (ret) { 13038732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13048732d44fSMiao Xie goto fail; 13058732d44fSMiao Xie } 13063063d29fSChris Mason 1307fcebe456SJosef Bacik /* 1308fcebe456SJosef Bacik * We need to flush delayed refs in order to make sure all of our quota 1309fcebe456SJosef Bacik * operations have been done before we call btrfs_qgroup_inherit. 1310fcebe456SJosef Bacik */ 1311fcebe456SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1312fcebe456SJosef Bacik if (ret) { 1313fcebe456SJosef Bacik btrfs_abort_transaction(trans, root, ret); 1314fcebe456SJosef Bacik goto fail; 1315fcebe456SJosef Bacik } 1316fcebe456SJosef Bacik 131747a306a7SEric Sandeen ret = btrfs_qgroup_inherit(trans, fs_info, 1318fcebe456SJosef Bacik root->root_key.objectid, 1319fcebe456SJosef Bacik objectid, pending->inherit); 132047a306a7SEric Sandeen if (ret) { 132147a306a7SEric Sandeen btrfs_abort_transaction(trans, root, ret); 132247a306a7SEric Sandeen goto fail; 132347a306a7SEric Sandeen } 1324fcebe456SJosef Bacik 1325f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 132627cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1327f1ebcc74SLiu Bo smp_wmb(); 1328f1ebcc74SLiu Bo 13295d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1330a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1331a22285a6SYan, Zheng key.offset = trans->transid; 1332a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1333925baeddSChris Mason btrfs_tree_unlock(tmp); 13343063d29fSChris Mason free_extent_buffer(tmp); 13358732d44fSMiao Xie if (ret) { 13368732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13378732d44fSMiao Xie goto fail; 13388732d44fSMiao Xie } 13390660b5afSChris Mason 1340a22285a6SYan, Zheng /* 1341a22285a6SYan, Zheng * insert root back/forward references 1342a22285a6SYan, Zheng */ 1343a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1344a22285a6SYan, Zheng parent_root->root_key.objectid, 134533345d01SLi Zefan btrfs_ino(parent_inode), index, 1346a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 13478732d44fSMiao Xie if (ret) { 13488732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13498732d44fSMiao Xie goto fail; 13508732d44fSMiao Xie } 1351a22285a6SYan, Zheng 1352a22285a6SYan, Zheng key.offset = (u64)-1; 1353a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 135479787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 135579787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 13568732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13578732d44fSMiao Xie goto fail; 135879787eaaSJeff Mahoney } 1359d68fc57bSYan, Zheng 136049b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 13618732d44fSMiao Xie if (ret) { 13628732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13638732d44fSMiao Xie goto fail; 13648732d44fSMiao Xie } 1365361048f5SMiao Xie 1366361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 13678732d44fSMiao Xie if (ret) { 13688732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13698732d44fSMiao Xie goto fail; 13708732d44fSMiao Xie } 137142874b3dSMiao Xie 137242874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 137342874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 137442874b3dSMiao Xie parent_inode, &key, 137542874b3dSMiao Xie BTRFS_FT_DIR, index); 137642874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 13779c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 13788732d44fSMiao Xie if (ret) { 13798732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13808732d44fSMiao Xie goto fail; 13818732d44fSMiao Xie } 138242874b3dSMiao Xie 138342874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 138442874b3dSMiao Xie dentry->d_name.len * 2); 138542874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1386be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1387dd5f9615SStefan Behrens if (ret) { 13888732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1389dd5f9615SStefan Behrens goto fail; 1390dd5f9615SStefan Behrens } 1391dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1392dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1393dd5f9615SStefan Behrens if (ret) { 1394dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1395dd5f9615SStefan Behrens goto fail; 1396dd5f9615SStefan Behrens } 1397dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1398dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1399dd5f9615SStefan Behrens new_root_item->received_uuid, 1400dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1401dd5f9615SStefan Behrens objectid); 1402dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1403dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1404dd5f9615SStefan Behrens goto fail; 1405dd5f9615SStefan Behrens } 1406dd5f9615SStefan Behrens } 14073063d29fSChris Mason fail: 1408aec8030aSMiao Xie pending->error = ret; 1409aec8030aSMiao Xie dir_item_existed: 141098c9942aSLiu Bo trans->block_rsv = rsv; 14112382c5ccSLiu Bo trans->bytes_reserved = 0; 14126fa9700eSMiao Xie no_free_objectid: 14136fa9700eSMiao Xie kfree(new_root_item); 14146fa9700eSMiao Xie root_item_alloc_fail: 141542874b3dSMiao Xie btrfs_free_path(path); 141649b25e05SJeff Mahoney return ret; 14173063d29fSChris Mason } 14183063d29fSChris Mason 1419d352ac68SChris Mason /* 1420d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1421d352ac68SChris Mason */ 142280b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 14233063d29fSChris Mason struct btrfs_fs_info *fs_info) 14243063d29fSChris Mason { 1425aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 14263063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1427aec8030aSMiao Xie int ret = 0; 14283de4586cSChris Mason 1429aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1430aec8030aSMiao Xie list_del(&pending->list); 1431aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1432aec8030aSMiao Xie if (ret) 1433aec8030aSMiao Xie break; 1434aec8030aSMiao Xie } 1435aec8030aSMiao Xie return ret; 14363de4586cSChris Mason } 14373de4586cSChris Mason 14385d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 14395d4f98a2SYan Zheng { 14405d4f98a2SYan Zheng struct btrfs_root_item *root_item; 14415d4f98a2SYan Zheng struct btrfs_super_block *super; 14425d4f98a2SYan Zheng 14436c41761fSDavid Sterba super = root->fs_info->super_copy; 14445d4f98a2SYan Zheng 14455d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 14465d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 14475d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 14485d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 14495d4f98a2SYan Zheng 14505d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 14515d4f98a2SYan Zheng super->root = root_item->bytenr; 14525d4f98a2SYan Zheng super->generation = root_item->generation; 14535d4f98a2SYan Zheng super->root_level = root_item->level; 145473bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 14550af3d00bSJosef Bacik super->cache_generation = root_item->generation; 145670f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 145726432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 14585d4f98a2SYan Zheng } 14595d4f98a2SYan Zheng 1460f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1461f36f3042SChris Mason { 14624a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1463f36f3042SChris Mason int ret = 0; 14644a9d8bdeSMiao Xie 1465a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 14664a9d8bdeSMiao Xie trans = info->running_transaction; 14674a9d8bdeSMiao Xie if (trans) 14684a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1469a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1470f36f3042SChris Mason return ret; 1471f36f3042SChris Mason } 1472f36f3042SChris Mason 14738929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 14748929ecfaSYan, Zheng { 14754a9d8bdeSMiao Xie struct btrfs_transaction *trans; 14768929ecfaSYan, Zheng int ret = 0; 14774a9d8bdeSMiao Xie 1478a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 14794a9d8bdeSMiao Xie trans = info->running_transaction; 14804a9d8bdeSMiao Xie if (trans) 14814a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1482a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 14838929ecfaSYan, Zheng return ret; 14848929ecfaSYan, Zheng } 14858929ecfaSYan, Zheng 1486bb9c12c9SSage Weil /* 1487bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1488bb9c12c9SSage Weil * transaction joins 1489bb9c12c9SSage Weil */ 1490bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1491bb9c12c9SSage Weil struct btrfs_transaction *trans) 1492bb9c12c9SSage Weil { 14934a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1494501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1495501407aaSJosef Bacik trans->aborted); 1496bb9c12c9SSage Weil } 1497bb9c12c9SSage Weil 1498bb9c12c9SSage Weil /* 1499bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1500bb9c12c9SSage Weil * caller holds ref. 1501bb9c12c9SSage Weil */ 1502bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1503bb9c12c9SSage Weil struct btrfs_transaction *trans) 1504bb9c12c9SSage Weil { 150572d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1506501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1507501407aaSJosef Bacik trans->aborted); 1508bb9c12c9SSage Weil } 1509bb9c12c9SSage Weil 1510bb9c12c9SSage Weil /* 1511bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1512bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1513bb9c12c9SSage Weil */ 1514bb9c12c9SSage Weil struct btrfs_async_commit { 1515bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1516bb9c12c9SSage Weil struct btrfs_root *root; 15177892b5afSMiao Xie struct work_struct work; 1518bb9c12c9SSage Weil }; 1519bb9c12c9SSage Weil 1520bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1521bb9c12c9SSage Weil { 1522bb9c12c9SSage Weil struct btrfs_async_commit *ac = 15237892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1524bb9c12c9SSage Weil 15256fc4e354SSage Weil /* 15266fc4e354SSage Weil * We've got freeze protection passed with the transaction. 15276fc4e354SSage Weil * Tell lockdep about it. 15286fc4e354SSage Weil */ 1529b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 15306fc4e354SSage Weil rwsem_acquire_read( 15316fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 15326fc4e354SSage Weil 0, 1, _THIS_IP_); 15336fc4e354SSage Weil 1534e209db7aSSage Weil current->journal_info = ac->newtrans; 1535e209db7aSSage Weil 1536bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1537bb9c12c9SSage Weil kfree(ac); 1538bb9c12c9SSage Weil } 1539bb9c12c9SSage Weil 1540bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1541bb9c12c9SSage Weil struct btrfs_root *root, 1542bb9c12c9SSage Weil int wait_for_unblock) 1543bb9c12c9SSage Weil { 1544bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1545bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1546bb9c12c9SSage Weil 1547bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1548db5b493aSTsutomu Itoh if (!ac) 1549db5b493aSTsutomu Itoh return -ENOMEM; 1550bb9c12c9SSage Weil 15517892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1552bb9c12c9SSage Weil ac->root = root; 15537a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 15543612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 15553612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 15563612b495STsutomu Itoh kfree(ac); 15573612b495STsutomu Itoh return err; 15583612b495STsutomu Itoh } 1559bb9c12c9SSage Weil 1560bb9c12c9SSage Weil /* take transaction reference */ 1561bb9c12c9SSage Weil cur_trans = trans->transaction; 156213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1563bb9c12c9SSage Weil 1564bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 15656fc4e354SSage Weil 15666fc4e354SSage Weil /* 15676fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 15686fc4e354SSage Weil * async commit thread will be the one to unlock it. 15696fc4e354SSage Weil */ 1570b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1571ff7c1d33SMiao Xie rwsem_release( 1572ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 15736fc4e354SSage Weil 1, _THIS_IP_); 15746fc4e354SSage Weil 15757892b5afSMiao Xie schedule_work(&ac->work); 1576bb9c12c9SSage Weil 1577bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1578bb9c12c9SSage Weil if (wait_for_unblock) 1579bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1580bb9c12c9SSage Weil else 1581bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1582bb9c12c9SSage Weil 158338e88054SSage Weil if (current->journal_info == trans) 158438e88054SSage Weil current->journal_info = NULL; 158538e88054SSage Weil 1586724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1587bb9c12c9SSage Weil return 0; 1588bb9c12c9SSage Weil } 1589bb9c12c9SSage Weil 159049b25e05SJeff Mahoney 159149b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 15927b8b92afSJosef Bacik struct btrfs_root *root, int err) 159349b25e05SJeff Mahoney { 159449b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1595f094ac32SLiu Bo DEFINE_WAIT(wait); 159649b25e05SJeff Mahoney 159749b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 159849b25e05SJeff Mahoney 15997b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 16007b8b92afSJosef Bacik 160149b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 160266b6135bSLiu Bo 160325d8c284SMiao Xie /* 160425d8c284SMiao Xie * If the transaction is removed from the list, it means this 160525d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 160625d8c284SMiao Xie * to call the cleanup function. 160725d8c284SMiao Xie */ 160825d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 160966b6135bSLiu Bo 161049b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1611d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 16124a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1613f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1614f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1615f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1616f094ac32SLiu Bo 1617f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1618d7096fc3SJosef Bacik } 161949b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 162049b25e05SJeff Mahoney 162149b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 162249b25e05SJeff Mahoney 16234a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 16244a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 16254a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 16264a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 16274a9d8bdeSMiao Xie 1628e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1629e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1630724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1631724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 163249b25e05SJeff Mahoney 163349b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 163449b25e05SJeff Mahoney 163549b25e05SJeff Mahoney if (current->journal_info == trans) 163649b25e05SJeff Mahoney current->journal_info = NULL; 1637c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 163849b25e05SJeff Mahoney 163949b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 164049b25e05SJeff Mahoney } 164149b25e05SJeff Mahoney 164282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 164382436617SMiao Xie { 164482436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 16456c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 164682436617SMiao Xie return 0; 164782436617SMiao Xie } 164882436617SMiao Xie 164982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 165082436617SMiao Xie { 165182436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1652b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 165382436617SMiao Xie } 165482436617SMiao Xie 165579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 165679154b1bSChris Mason struct btrfs_root *root) 165779154b1bSChris Mason { 165849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 16598fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1660656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 166125287e0aSMiao Xie int ret; 166279154b1bSChris Mason 16638d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 16648d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 166525287e0aSMiao Xie ret = cur_trans->aborted; 1666e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1667e4a2bcacSJosef Bacik return ret; 166825287e0aSMiao Xie } 166949b25e05SJeff Mahoney 167056bec294SChris Mason /* make a pass through all the delayed refs we have so far 167156bec294SChris Mason * any runnings procs may add more while we are here 167256bec294SChris Mason */ 167356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1674e4a2bcacSJosef Bacik if (ret) { 1675e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1676e4a2bcacSJosef Bacik return ret; 1677e4a2bcacSJosef Bacik } 167856bec294SChris Mason 16790e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 16800e721106SJosef Bacik trans->block_rsv = NULL; 1681272d26d0SMiao Xie if (trans->qgroup_reserved) { 1682272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1683272d26d0SMiao Xie trans->qgroup_reserved = 0; 1684272d26d0SMiao Xie } 16850e721106SJosef Bacik 1686b7ec40d7SChris Mason cur_trans = trans->transaction; 168749b25e05SJeff Mahoney 168856bec294SChris Mason /* 168956bec294SChris Mason * set the flushing flag so procs in this transaction have to 169056bec294SChris Mason * start sending their work down. 169156bec294SChris Mason */ 1692b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 16931be41b78SJosef Bacik smp_wmb(); 169456bec294SChris Mason 1695ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1696ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1697ea658badSJosef Bacik 1698c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1699e4a2bcacSJosef Bacik if (ret) { 1700e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1701e4a2bcacSJosef Bacik return ret; 1702e4a2bcacSJosef Bacik } 170356bec294SChris Mason 17044a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17054a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 17064a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 170713c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 170849b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1709ccd467d6SChris Mason 1710b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 171115ee9bc7SJosef Bacik 1712724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 171315ee9bc7SJosef Bacik 171449b25e05SJeff Mahoney return ret; 171579154b1bSChris Mason } 17164313b399SChris Mason 17174a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1718bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1719bb9c12c9SSage Weil 1720ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1721ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1722ccd467d6SChris Mason struct btrfs_transaction, list); 17234a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 172413c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1725a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1726ccd467d6SChris Mason 1727ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1728ccd467d6SChris Mason 1729724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 1730a4abeea4SJosef Bacik } else { 1731a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1732ccd467d6SChris Mason } 1733a4abeea4SJosef Bacik } else { 1734a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1735ccd467d6SChris Mason } 173615ee9bc7SJosef Bacik 17370860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 17380860adfdSMiao Xie 173982436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 174082436617SMiao Xie if (ret) 174182436617SMiao Xie goto cleanup_transaction; 174282436617SMiao Xie 17438d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 174449b25e05SJeff Mahoney if (ret) 174549b25e05SJeff Mahoney goto cleanup_transaction; 174616cdcec7SMiao Xie 1747581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1748581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1749ed3b3d31SChris Mason 1750581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 17518d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1752ca469637SMiao Xie if (ret) 1753ca469637SMiao Xie goto cleanup_transaction; 1754ca469637SMiao Xie 175582436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1756cb7ab021SWang Shilong 1757cb7ab021SWang Shilong btrfs_scrub_pause(root); 1758ed0ca140SJosef Bacik /* 1759ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1760ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 17614a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1762ed0ca140SJosef Bacik */ 1763a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 17644a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1765a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1766ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1767ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 176815ee9bc7SJosef Bacik 17692cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 17702cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 17712cba30f1SMiao Xie ret = cur_trans->aborted; 17726cf7f77eSWang Shilong goto scrub_continue; 17732cba30f1SMiao Xie } 17747585717fSChris Mason /* 17757585717fSChris Mason * the reloc mutex makes sure that we stop 17767585717fSChris Mason * the balancing code from coming in and moving 17777585717fSChris Mason * extents around in the middle of the commit 17787585717fSChris Mason */ 17797585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 17807585717fSChris Mason 178142874b3dSMiao Xie /* 178242874b3dSMiao Xie * We needn't worry about the delayed items because we will 178342874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 178442874b3dSMiao Xie * core function of the snapshot creation. 178542874b3dSMiao Xie */ 178642874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 178749b25e05SJeff Mahoney if (ret) { 178849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 17896cf7f77eSWang Shilong goto scrub_continue; 179049b25e05SJeff Mahoney } 17913063d29fSChris Mason 179242874b3dSMiao Xie /* 179342874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 179442874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 179542874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 179642874b3dSMiao Xie * them. 179742874b3dSMiao Xie * 179842874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 179942874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 180042874b3dSMiao Xie * the nodes and leaves. 180142874b3dSMiao Xie */ 180242874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 180349b25e05SJeff Mahoney if (ret) { 180449b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 18056cf7f77eSWang Shilong goto scrub_continue; 180649b25e05SJeff Mahoney } 180716cdcec7SMiao Xie 180856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 180949b25e05SJeff Mahoney if (ret) { 181049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 18116cf7f77eSWang Shilong goto scrub_continue; 181249b25e05SJeff Mahoney } 181356bec294SChris Mason 1814e999376fSChris Mason /* 1815e999376fSChris Mason * make sure none of the code above managed to slip in a 1816e999376fSChris Mason * delayed item 1817e999376fSChris Mason */ 1818e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1819e999376fSChris Mason 18202c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1821dc17ff8fSChris Mason 1822e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1823e02119d5SChris Mason * various roots consistent with each other. Every pointer 1824e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1825e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1826e02119d5SChris Mason * the tree logging code from jumping in and changing any 1827e02119d5SChris Mason * of the trees. 1828e02119d5SChris Mason * 1829e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1830e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1831e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1832e02119d5SChris Mason * from now until after the super is written, we avoid races 1833e02119d5SChris Mason * with the tree-log code. 1834e02119d5SChris Mason */ 1835e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 18361a40e23bSZheng Yan 18375d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 183849b25e05SJeff Mahoney if (ret) { 183949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1840871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 18416cf7f77eSWang Shilong goto scrub_continue; 184249b25e05SJeff Mahoney } 184354aa1f4dSChris Mason 18443818aea2SQu Wenruo /* 18457e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 18467e1876acSDavid Sterba * before the next transaction. 18473818aea2SQu Wenruo */ 1848572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 1849572d9ab7SDavid Sterba 18505d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1851e02119d5SChris Mason * safe to free the root of tree log roots 1852e02119d5SChris Mason */ 1853e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1854e02119d5SChris Mason 18555d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 185649b25e05SJeff Mahoney if (ret) { 185749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1858871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 18596cf7f77eSWang Shilong goto scrub_continue; 186049b25e05SJeff Mahoney } 186154aa1f4dSChris Mason 18622cba30f1SMiao Xie /* 18632cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 18642cba30f1SMiao Xie * update ->aborted, check it. 18652cba30f1SMiao Xie */ 18662cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 18672cba30f1SMiao Xie ret = cur_trans->aborted; 18682cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 18692cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 18706cf7f77eSWang Shilong goto scrub_continue; 18712cba30f1SMiao Xie } 18722cba30f1SMiao Xie 187311833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 187411833d66SYan Zheng 187578fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 18765f39d397SChris Mason 18775d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 18785d4f98a2SYan Zheng root->fs_info->tree_root->node); 18799e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 18809e351cc8SJosef Bacik &cur_trans->switch_commits); 18815d4f98a2SYan Zheng 18825d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 18835d4f98a2SYan Zheng root->fs_info->chunk_root->node); 18849e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 18859e351cc8SJosef Bacik &cur_trans->switch_commits); 18869e351cc8SJosef Bacik 18879e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 18885d4f98a2SYan Zheng 1889edf39272SJan Schmidt assert_qgroups_uptodate(trans); 18905d4f98a2SYan Zheng update_super_roots(root); 1891e02119d5SChris Mason 18926c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 18936c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 18946c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 18956c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1896ccd467d6SChris Mason 1897935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 1898ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 1899935e5cc9SMiao Xie 1900656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 1901656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 1902656f30dbSFilipe Manana 1903a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19044a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 1905a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1906a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 19077585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1908b7ec40d7SChris Mason 1909f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1910e6dcd2dcSChris Mason 191179154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 191249b25e05SJeff Mahoney if (ret) { 191349b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 191408748810SDavid Sterba "Error while writing out transaction"); 191549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 19166cf7f77eSWang Shilong goto scrub_continue; 191749b25e05SJeff Mahoney } 191849b25e05SJeff Mahoney 191949b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 192049b25e05SJeff Mahoney if (ret) { 192149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 19226cf7f77eSWang Shilong goto scrub_continue; 192349b25e05SJeff Mahoney } 19244313b399SChris Mason 1925e02119d5SChris Mason /* 1926e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1927e02119d5SChris Mason * to go about their business 1928e02119d5SChris Mason */ 1929e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1930e02119d5SChris Mason 193111833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 19324313b399SChris Mason 193315ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 19344a9d8bdeSMiao Xie /* 19354a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 19364a9d8bdeSMiao Xie * which can change it. 19374a9d8bdeSMiao Xie */ 19384a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 19392c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 19403de4586cSChris Mason 1941a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 194213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 1943a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1944a4abeea4SJosef Bacik 1945724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1946724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 194758176a96SJosef Bacik 19480860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 1949b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 1950b2b5ef5cSJan Kara 19511abe9b8aSliubo trace_btrfs_transaction_commit(root); 19521abe9b8aSliubo 1953a2de733cSArne Jansen btrfs_scrub_continue(root); 1954a2de733cSArne Jansen 19559ed74f2dSJosef Bacik if (current->journal_info == trans) 19569ed74f2dSJosef Bacik current->journal_info = NULL; 19579ed74f2dSJosef Bacik 19582c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 195924bbcf04SYan, Zheng 196024bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 196124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 196224bbcf04SYan, Zheng 196379154b1bSChris Mason return ret; 196449b25e05SJeff Mahoney 19656cf7f77eSWang Shilong scrub_continue: 19666cf7f77eSWang Shilong btrfs_scrub_continue(root); 196749b25e05SJeff Mahoney cleanup_transaction: 19680e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 19690e721106SJosef Bacik trans->block_rsv = NULL; 1970272d26d0SMiao Xie if (trans->qgroup_reserved) { 1971272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1972272d26d0SMiao Xie trans->qgroup_reserved = 0; 1973272d26d0SMiao Xie } 1974c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 197549b25e05SJeff Mahoney if (current->journal_info == trans) 197649b25e05SJeff Mahoney current->journal_info = NULL; 19777b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 197849b25e05SJeff Mahoney 197949b25e05SJeff Mahoney return ret; 198079154b1bSChris Mason } 198179154b1bSChris Mason 1982d352ac68SChris Mason /* 19839d1a2a3aSDavid Sterba * return < 0 if error 19849d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 19859d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 19869d1a2a3aSDavid Sterba * 19879d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 19889d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 19899d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 19909d1a2a3aSDavid Sterba * few seconds later. 1991d352ac68SChris Mason */ 19929d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 1993e9d0b13bSChris Mason { 19949d1a2a3aSDavid Sterba int ret; 19955d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1996e9d0b13bSChris Mason 1997a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 19989d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 19999d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 20009d1a2a3aSDavid Sterba return 0; 20019d1a2a3aSDavid Sterba } 20029d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 20039d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2004cfad392bSJosef Bacik list_del_init(&root->root_list); 2005a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 20065d4f98a2SYan Zheng 2007efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 200876dda93cSYan, Zheng 200916cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 201016cdcec7SMiao Xie 201176dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 201276dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 20132c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 201476dda93cSYan, Zheng else 20152c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 201632471dc2SDavid Sterba 20176596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2018e9d0b13bSChris Mason } 2019572d9ab7SDavid Sterba 2020572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2021572d9ab7SDavid Sterba { 2022572d9ab7SDavid Sterba unsigned long prev; 2023572d9ab7SDavid Sterba unsigned long bit; 2024572d9ab7SDavid Sterba 2025572d9ab7SDavid Sterba prev = cmpxchg(&fs_info->pending_changes, 0, 0); 2026572d9ab7SDavid Sterba if (!prev) 2027572d9ab7SDavid Sterba return; 2028572d9ab7SDavid Sterba 20297e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 20307e1876acSDavid Sterba if (prev & bit) 20317e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 20327e1876acSDavid Sterba prev &= ~bit; 20337e1876acSDavid Sterba 20347e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 20357e1876acSDavid Sterba if (prev & bit) 20367e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 20377e1876acSDavid Sterba prev &= ~bit; 20387e1876acSDavid Sterba 2039*d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2040*d51033d0SDavid Sterba if (prev & bit) 2041*d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2042*d51033d0SDavid Sterba prev &= ~bit; 2043*d51033d0SDavid Sterba 2044572d9ab7SDavid Sterba if (prev) 2045572d9ab7SDavid Sterba btrfs_warn(fs_info, 2046572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2047572d9ab7SDavid Sterba } 2048