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" 3479154b1bSChris Mason 350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 360f7d52f4SChris Mason 374a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 384a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 394a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 404a9d8bdeSMiao Xie __TRANS_START), 414a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 424a9d8bdeSMiao Xie __TRANS_START | 434a9d8bdeSMiao Xie __TRANS_ATTACH), 444a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 454a9d8bdeSMiao Xie __TRANS_START | 464a9d8bdeSMiao Xie __TRANS_ATTACH | 474a9d8bdeSMiao Xie __TRANS_JOIN), 484a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 494a9d8bdeSMiao Xie __TRANS_START | 504a9d8bdeSMiao Xie __TRANS_ATTACH | 514a9d8bdeSMiao Xie __TRANS_JOIN | 524a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 534a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 544a9d8bdeSMiao Xie __TRANS_START | 554a9d8bdeSMiao Xie __TRANS_ATTACH | 564a9d8bdeSMiao Xie __TRANS_JOIN | 574a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 584a9d8bdeSMiao Xie }; 594a9d8bdeSMiao Xie 60724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 6179154b1bSChris Mason { 6213c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6313c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 64a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 65c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 666df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 676df9a95eSJosef Bacik struct extent_map *em; 686df9a95eSJosef Bacik 696df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 706df9a95eSJosef Bacik struct extent_map, list); 716df9a95eSJosef Bacik list_del_init(&em->list); 726df9a95eSJosef Bacik free_extent_map(em); 736df9a95eSJosef Bacik } 742c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7579154b1bSChris Mason } 7678fae27eSChris Mason } 7779154b1bSChris Mason 78*9e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 79*9e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 80817d52f8SJosef Bacik { 81*9e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 82*9e351cc8SJosef Bacik 83*9e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 84*9e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 85*9e351cc8SJosef Bacik dirty_list) { 86*9e351cc8SJosef Bacik list_del_init(&root->dirty_list); 87817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 88817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 89*9e351cc8SJosef Bacik if (is_fstree(root->objectid)) 90*9e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 91*9e351cc8SJosef Bacik } 92*9e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 93817d52f8SJosef Bacik } 94817d52f8SJosef Bacik 950860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 960860adfdSMiao Xie unsigned int type) 970860adfdSMiao Xie { 980860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 990860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1000860adfdSMiao Xie } 1010860adfdSMiao Xie 1020860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1030860adfdSMiao Xie unsigned int type) 1040860adfdSMiao Xie { 1050860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1060860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1070860adfdSMiao Xie } 1080860adfdSMiao Xie 1090860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1100860adfdSMiao Xie unsigned int type) 1110860adfdSMiao Xie { 1120860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1130860adfdSMiao Xie } 1140860adfdSMiao Xie 1150860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1160860adfdSMiao Xie { 1170860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 118178260b2SMiao Xie } 119178260b2SMiao Xie 120d352ac68SChris Mason /* 121d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 122d352ac68SChris Mason */ 1230860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 12479154b1bSChris Mason { 12579154b1bSChris Mason struct btrfs_transaction *cur_trans; 12619ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 127a4abeea4SJosef Bacik 12819ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 129d43317dcSChris Mason loop: 13049b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 13187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 13219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 13349b25e05SJeff Mahoney return -EROFS; 13449b25e05SJeff Mahoney } 13549b25e05SJeff Mahoney 13619ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 137a4abeea4SJosef Bacik if (cur_trans) { 138871383beSDavid Sterba if (cur_trans->aborted) { 13919ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 14049b25e05SJeff Mahoney return cur_trans->aborted; 141871383beSDavid Sterba } 1424a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 143178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 144178260b2SMiao Xie return -EBUSY; 145178260b2SMiao Xie } 146a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 147a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1480860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 14919ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 150a4abeea4SJosef Bacik return 0; 151a4abeea4SJosef Bacik } 15219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 153a4abeea4SJosef Bacik 154354aa0fbSMiao Xie /* 155354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 156354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 157354aa0fbSMiao Xie */ 158354aa0fbSMiao Xie if (type == TRANS_ATTACH) 159354aa0fbSMiao Xie return -ENOENT; 160354aa0fbSMiao Xie 1614a9d8bdeSMiao Xie /* 1624a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1634a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1644a9d8bdeSMiao Xie */ 1654a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1664a9d8bdeSMiao Xie 167a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 168db5b493aSTsutomu Itoh if (!cur_trans) 169db5b493aSTsutomu Itoh return -ENOMEM; 170d43317dcSChris Mason 17119ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 17219ae4e81SJan Schmidt if (fs_info->running_transaction) { 173d43317dcSChris Mason /* 174d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 1754a9d8bdeSMiao Xie * to redo the checks above 176d43317dcSChris Mason */ 177a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 178d43317dcSChris Mason goto loop; 17987533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 180e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 1817b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 1827b8b92afSJosef Bacik return -EROFS; 183a4abeea4SJosef Bacik } 184d43317dcSChris Mason 18513c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 1860860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 18779154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 18879154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 1894a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 190a4abeea4SJosef Bacik /* 191a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 192a4abeea4SJosef Bacik * commit the transaction. 193a4abeea4SJosef Bacik */ 194a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 19508607c1bSChris Mason cur_trans->start_time = get_seconds(); 19656bec294SChris Mason 197c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 198d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 199c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 200c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 20156bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 202c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 20320b297d6SJan Schmidt 20420b297d6SJan Schmidt /* 20520b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 20620b297d6SJan Schmidt * the log must never go across transaction boundaries. 20720b297d6SJan Schmidt */ 20820b297d6SJan Schmidt smp_mb(); 20931b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 210efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 21120b297d6SJan Schmidt "creating a fresh transaction\n"); 21231b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 213efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 21420b297d6SJan Schmidt "creating a fresh transaction\n"); 215fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 21620b297d6SJan Schmidt 21756bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 21856bec294SChris Mason 2193063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 220569e0f35SJosef Bacik INIT_LIST_HEAD(&cur_trans->ordered_operations); 2216df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 222*9e351cc8SJosef 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 { 2445d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 2456702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2465d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2475d4f98a2SYan Zheng 2487585717fSChris Mason /* 2497585717fSChris Mason * see below for in_trans_setup usage rules 2507585717fSChris Mason * we have the reloc mutex held now, so there 2517585717fSChris Mason * is only one writer in this function 2527585717fSChris Mason */ 2537585717fSChris Mason root->in_trans_setup = 1; 2547585717fSChris Mason 2557585717fSChris Mason /* make sure readers find in_trans_setup before 2567585717fSChris Mason * they find our root->last_trans update 2577585717fSChris Mason */ 2587585717fSChris Mason smp_wmb(); 2597585717fSChris Mason 260a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 261a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 262a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 263a4abeea4SJosef Bacik return 0; 264a4abeea4SJosef Bacik } 2656702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 2666702ed49SChris Mason (unsigned long)root->root_key.objectid, 2676702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 268a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 2697585717fSChris Mason root->last_trans = trans->transid; 2707585717fSChris Mason 2717585717fSChris Mason /* this is pretty tricky. We don't want to 2727585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 2737585717fSChris Mason * unless we're really doing the first setup for this root in 2747585717fSChris Mason * this transaction. 2757585717fSChris Mason * 2767585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 2777585717fSChris Mason * if we want to take the expensive mutex. 2787585717fSChris Mason * 2797585717fSChris Mason * But, we have to set root->last_trans before we 2807585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 2817585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 2827585717fSChris Mason * with root->in_trans_setup. When this is 1, we're still 2837585717fSChris Mason * fixing up the reloc trees and everyone must wait. 2847585717fSChris Mason * 2857585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 2867585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 2877585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 2887585717fSChris Mason * done before we pop in the zero below 2897585717fSChris Mason */ 2905d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 2917585717fSChris Mason smp_wmb(); 2927585717fSChris Mason root->in_trans_setup = 0; 2936702ed49SChris Mason } 2945d4f98a2SYan Zheng return 0; 2956702ed49SChris Mason } 2965d4f98a2SYan Zheng 2977585717fSChris Mason 2987585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 2997585717fSChris Mason struct btrfs_root *root) 3007585717fSChris Mason { 3017585717fSChris Mason if (!root->ref_cows) 3027585717fSChris Mason return 0; 3037585717fSChris Mason 3047585717fSChris Mason /* 3057585717fSChris Mason * see record_root_in_trans for comments about in_trans_setup usage 3067585717fSChris Mason * and barriers 3077585717fSChris Mason */ 3087585717fSChris Mason smp_rmb(); 3097585717fSChris Mason if (root->last_trans == trans->transid && 3107585717fSChris Mason !root->in_trans_setup) 3117585717fSChris Mason return 0; 3127585717fSChris Mason 3137585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3147585717fSChris Mason record_root_in_trans(trans, root); 3157585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3167585717fSChris Mason 3177585717fSChris Mason return 0; 3187585717fSChris Mason } 3197585717fSChris Mason 3204a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3214a9d8bdeSMiao Xie { 3224a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 323501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 324501407aaSJosef Bacik !trans->aborted); 3254a9d8bdeSMiao Xie } 3264a9d8bdeSMiao Xie 327d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 328d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 329d352ac68SChris Mason * transaction might not be fully on disk. 330d352ac68SChris Mason */ 33137d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 33279154b1bSChris Mason { 333f9295749SChris Mason struct btrfs_transaction *cur_trans; 33479154b1bSChris Mason 335a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 336f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3374a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 33813c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 339a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 34072d63ed6SLi Zefan 34172d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 342501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 343501407aaSJosef Bacik cur_trans->aborted); 344724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 345a4abeea4SJosef Bacik } else { 346a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 347f9295749SChris Mason } 34837d1aeeeSChris Mason } 34937d1aeeeSChris Mason 350a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 35137d1aeeeSChris Mason { 352a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 353a4abeea4SJosef Bacik return 0; 354a4abeea4SJosef Bacik 355a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 356a22285a6SYan, Zheng return 1; 357a4abeea4SJosef Bacik 358a4abeea4SJosef Bacik if (type == TRANS_START && 359a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 360a4abeea4SJosef Bacik return 1; 361a4abeea4SJosef Bacik 362a22285a6SYan, Zheng return 0; 363a22285a6SYan, Zheng } 364a22285a6SYan, Zheng 36520dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 36620dd2cbfSMiao Xie { 36720dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 36820dd2cbfSMiao Xie !root->ref_cows || 36920dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 37020dd2cbfSMiao Xie root->reloc_root) 37120dd2cbfSMiao Xie return false; 37220dd2cbfSMiao Xie 37320dd2cbfSMiao Xie return true; 37420dd2cbfSMiao Xie } 37520dd2cbfSMiao Xie 37608e007d2SMiao Xie static struct btrfs_trans_handle * 3770860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 37808e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 379a22285a6SYan, Zheng { 380a22285a6SYan, Zheng struct btrfs_trans_handle *h; 381a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 382b5009945SJosef Bacik u64 num_bytes = 0; 383c5567237SArne Jansen u64 qgroup_reserved = 0; 38420dd2cbfSMiao Xie bool reloc_reserved = false; 38520dd2cbfSMiao Xie int ret; 386acce952bSliubo 38787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 388acce952bSliubo return ERR_PTR(-EROFS); 3892a1eb461SJosef Bacik 390a26e8c9fSJosef Bacik if (current->journal_info && 391a26e8c9fSJosef Bacik current->journal_info != (void *)BTRFS_SEND_TRANS_STUB) { 3920860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 3932a1eb461SJosef Bacik h = current->journal_info; 3942a1eb461SJosef Bacik h->use_count++; 395b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 3962a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 3972a1eb461SJosef Bacik h->block_rsv = NULL; 3982a1eb461SJosef Bacik goto got_it; 3992a1eb461SJosef Bacik } 400b5009945SJosef Bacik 401b5009945SJosef Bacik /* 402b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 403b5009945SJosef Bacik * the appropriate flushing if need be. 404b5009945SJosef Bacik */ 405b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 406c5567237SArne Jansen if (root->fs_info->quota_enabled && 407c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 408c5567237SArne Jansen qgroup_reserved = num_items * root->leafsize; 409c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 410c5567237SArne Jansen if (ret) 411c5567237SArne Jansen return ERR_PTR(ret); 412c5567237SArne Jansen } 413c5567237SArne Jansen 414b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 41520dd2cbfSMiao Xie /* 41620dd2cbfSMiao Xie * Do the reservation for the relocation root creation 41720dd2cbfSMiao Xie */ 41820dd2cbfSMiao Xie if (unlikely(need_reserve_reloc_root(root))) { 41920dd2cbfSMiao Xie num_bytes += root->nodesize; 42020dd2cbfSMiao Xie reloc_reserved = true; 42120dd2cbfSMiao Xie } 42220dd2cbfSMiao Xie 4234a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 424b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 42508e007d2SMiao Xie num_bytes, flush); 426b5009945SJosef Bacik if (ret) 427843fcf35SMiao Xie goto reserve_fail; 428b5009945SJosef Bacik } 429a22285a6SYan, Zheng again: 430a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 431843fcf35SMiao Xie if (!h) { 432843fcf35SMiao Xie ret = -ENOMEM; 433843fcf35SMiao Xie goto alloc_fail; 434843fcf35SMiao Xie } 435a22285a6SYan, Zheng 43698114659SJosef Bacik /* 43798114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 43898114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 43998114659SJosef Bacik * because we're already holding a ref. We need this because we could 44098114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 44198114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 442354aa0fbSMiao Xie * 443354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 444354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 44598114659SJosef Bacik */ 4460860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 447b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 448b2b5ef5cSJan Kara 449a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 45037d1aeeeSChris Mason wait_current_trans(root); 451a22285a6SYan, Zheng 452a4abeea4SJosef Bacik do { 453354aa0fbSMiao Xie ret = join_transaction(root, type); 454178260b2SMiao Xie if (ret == -EBUSY) { 455a4abeea4SJosef Bacik wait_current_trans(root); 456178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 457178260b2SMiao Xie ret = -ENOENT; 458178260b2SMiao Xie } 459a4abeea4SJosef Bacik } while (ret == -EBUSY); 460a4abeea4SJosef Bacik 461db5b493aSTsutomu Itoh if (ret < 0) { 462354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 463354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 464843fcf35SMiao Xie goto join_fail; 465db5b493aSTsutomu Itoh } 4660f7d52f4SChris Mason 467a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 468a22285a6SYan, Zheng 469a22285a6SYan, Zheng h->transid = cur_trans->transid; 470a22285a6SYan, Zheng h->transaction = cur_trans; 47179154b1bSChris Mason h->blocks_used = 0; 472a22285a6SYan, Zheng h->bytes_reserved = 0; 473d13603efSArne Jansen h->root = root; 47456bec294SChris Mason h->delayed_ref_updates = 0; 4752a1eb461SJosef Bacik h->use_count = 1; 4760e721106SJosef Bacik h->adding_csums = 0; 477f0486c68SYan, Zheng h->block_rsv = NULL; 4782a1eb461SJosef Bacik h->orig_rsv = NULL; 47949b25e05SJeff Mahoney h->aborted = 0; 4804b824906SMiao Xie h->qgroup_reserved = 0; 481bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 482a698d075SMiao Xie h->type = type; 483c6b305a8SJosef Bacik h->allocating_chunk = false; 48420dd2cbfSMiao Xie h->reloc_reserved = false; 4855039eddcSJosef Bacik h->sync = false; 486bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 487ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 488b7ec40d7SChris Mason 489a22285a6SYan, Zheng smp_mb(); 4904a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 4914a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 492a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 493a22285a6SYan, Zheng goto again; 494a22285a6SYan, Zheng } 4959ed74f2dSJosef Bacik 496b5009945SJosef Bacik if (num_bytes) { 4978c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 4982bcc0328SLiu Bo h->transid, num_bytes, 1); 499b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 500b5009945SJosef Bacik h->bytes_reserved = num_bytes; 50120dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 502a22285a6SYan, Zheng } 5034b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 504a22285a6SYan, Zheng 5052a1eb461SJosef Bacik got_it: 506a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 507a22285a6SYan, Zheng 508a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 509a22285a6SYan, Zheng current->journal_info = h; 51079154b1bSChris Mason return h; 511843fcf35SMiao Xie 512843fcf35SMiao Xie join_fail: 5130860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 514843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 515843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 516843fcf35SMiao Xie alloc_fail: 517843fcf35SMiao Xie if (num_bytes) 518843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 519843fcf35SMiao Xie num_bytes); 520843fcf35SMiao Xie reserve_fail: 521843fcf35SMiao Xie if (qgroup_reserved) 522843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 523843fcf35SMiao Xie return ERR_PTR(ret); 52479154b1bSChris Mason } 52579154b1bSChris Mason 526f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 527a22285a6SYan, Zheng int num_items) 528f9295749SChris Mason { 52908e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 53008e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 531f9295749SChris Mason } 5328407aa46SMiao Xie 53308e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 5348407aa46SMiao Xie struct btrfs_root *root, int num_items) 5358407aa46SMiao Xie { 53608e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 53708e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 5388407aa46SMiao Xie } 5398407aa46SMiao Xie 5407a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 541f9295749SChris Mason { 5428407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 543f9295749SChris Mason } 544f9295749SChris Mason 5457a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5460af3d00bSJosef Bacik { 5478407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5480af3d00bSJosef Bacik } 5490af3d00bSJosef Bacik 5507a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5519ca9ee09SSage Weil { 5528407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5539ca9ee09SSage Weil } 5549ca9ee09SSage Weil 555d4edf39bSMiao Xie /* 556d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 557d4edf39bSMiao Xie * 558d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 559d4edf39bSMiao Xie * don't want to start a new one. 560d4edf39bSMiao Xie * 561d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 562d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 563d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 564d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 565d4edf39bSMiao Xie * invoke 566d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 567d4edf39bSMiao Xie */ 568354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 56960376ce4SJosef Bacik { 570354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 57160376ce4SJosef Bacik } 57260376ce4SJosef Bacik 573d4edf39bSMiao Xie /* 57490b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 575d4edf39bSMiao Xie * 576d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 577d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 578d4edf39bSMiao Xie * complete. 579d4edf39bSMiao Xie */ 580d4edf39bSMiao Xie struct btrfs_trans_handle * 581d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 582d4edf39bSMiao Xie { 583d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 584d4edf39bSMiao Xie 585d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 586d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 587d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 588d4edf39bSMiao Xie 589d4edf39bSMiao Xie return trans; 590d4edf39bSMiao Xie } 591d4edf39bSMiao Xie 592d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 593b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 59489ce8a63SChris Mason struct btrfs_transaction *commit) 59589ce8a63SChris Mason { 5964a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 59789ce8a63SChris Mason } 59889ce8a63SChris Mason 59946204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 60046204592SSage Weil { 60146204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6028cd2807fSMiao Xie int ret = 0; 60346204592SSage Weil 60446204592SSage Weil if (transid) { 60546204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 606a4abeea4SJosef Bacik goto out; 60746204592SSage Weil 6088cd2807fSMiao Xie ret = -EINVAL; 60946204592SSage Weil /* find specified transaction */ 610a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 61146204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 61246204592SSage Weil if (t->transid == transid) { 61346204592SSage Weil cur_trans = t; 614a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 6158cd2807fSMiao Xie ret = 0; 61646204592SSage Weil break; 61746204592SSage Weil } 6188cd2807fSMiao Xie if (t->transid > transid) { 6198cd2807fSMiao Xie ret = 0; 62046204592SSage Weil break; 62146204592SSage Weil } 6228cd2807fSMiao Xie } 623a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 6248cd2807fSMiao Xie /* The specified transaction doesn't exist */ 62546204592SSage Weil if (!cur_trans) 6268cd2807fSMiao Xie goto out; 62746204592SSage Weil } else { 62846204592SSage Weil /* find newest transaction that is committing | committed */ 629a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 63046204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 63146204592SSage Weil list) { 6324a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 6334a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 6343473f3c0SJosef Bacik break; 63546204592SSage Weil cur_trans = t; 636a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 63746204592SSage Weil break; 63846204592SSage Weil } 63946204592SSage Weil } 640a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 64146204592SSage Weil if (!cur_trans) 642a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 64346204592SSage Weil } 64446204592SSage Weil 64546204592SSage Weil wait_for_commit(root, cur_trans); 646724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 647a4abeea4SJosef Bacik out: 64846204592SSage Weil return ret; 64946204592SSage Weil } 65046204592SSage Weil 65137d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 65237d1aeeeSChris Mason { 653a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 65437d1aeeeSChris Mason wait_current_trans(root); 65537d1aeeeSChris Mason } 65637d1aeeeSChris Mason 6578929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 6588929ecfaSYan, Zheng struct btrfs_root *root) 6598929ecfaSYan, Zheng { 6601be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 6610a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 6621be41b78SJosef Bacik return 1; 66336ba022aSJosef Bacik 6641be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 6658929ecfaSYan, Zheng } 6668929ecfaSYan, Zheng 6678929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 6688929ecfaSYan, Zheng struct btrfs_root *root) 6698929ecfaSYan, Zheng { 6708929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 6718929ecfaSYan, Zheng int updates; 67249b25e05SJeff Mahoney int err; 6738929ecfaSYan, Zheng 674a4abeea4SJosef Bacik smp_mb(); 6754a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 6764a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 6778929ecfaSYan, Zheng return 1; 6788929ecfaSYan, Zheng 6798929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 6808929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 68149b25e05SJeff Mahoney if (updates) { 68249b25e05SJeff Mahoney err = btrfs_run_delayed_refs(trans, root, updates); 68349b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 68449b25e05SJeff Mahoney return err; 68549b25e05SJeff Mahoney } 6868929ecfaSYan, Zheng 6878929ecfaSYan, Zheng return should_end_transaction(trans, root); 6888929ecfaSYan, Zheng } 6898929ecfaSYan, Zheng 69089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 691a698d075SMiao Xie struct btrfs_root *root, int throttle) 69279154b1bSChris Mason { 6938929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 694ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 6951be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 696a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 6974edc2ca3SDave Jones int err = 0; 698d6e4a428SChris Mason 6993bbb24b2SJosef Bacik if (trans->use_count > 1) { 7003bbb24b2SJosef Bacik trans->use_count--; 7012a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 7022a1eb461SJosef Bacik return 0; 7032a1eb461SJosef Bacik } 7042a1eb461SJosef Bacik 705edf39272SJan Schmidt /* 706edf39272SJan Schmidt * do the qgroup accounting as early as possible 707edf39272SJan Schmidt */ 708edf39272SJan Schmidt err = btrfs_delayed_refs_qgroup_accounting(trans, info); 709edf39272SJan Schmidt 710b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 7114c13d758SJosef Bacik trans->block_rsv = NULL; 712c5567237SArne Jansen 713c5567237SArne Jansen if (trans->qgroup_reserved) { 7147c2ec3f0SLiu Bo /* 7157c2ec3f0SLiu Bo * the same root has to be passed here between start_transaction 7167c2ec3f0SLiu Bo * and end_transaction. Subvolume quota depends on this. 7177c2ec3f0SLiu Bo */ 7187c2ec3f0SLiu Bo btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 719c5567237SArne Jansen trans->qgroup_reserved = 0; 720c5567237SArne Jansen } 721c5567237SArne Jansen 722ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 723ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 724ea658badSJosef Bacik 725c3e69d58SChris Mason trans->delayed_ref_updates = 0; 7265039eddcSJosef Bacik if (!trans->sync && btrfs_should_throttle_delayed_refs(trans, root)) { 7270a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 728c3e69d58SChris Mason trans->delayed_ref_updates = 0; 729c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 73056bec294SChris Mason } 731bb721703SChris Mason 7320e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 7330e721106SJosef Bacik trans->block_rsv = NULL; 73456bec294SChris Mason 735ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 736ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 737ea658badSJosef Bacik 738a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 7394a9d8bdeSMiao Xie should_end_transaction(trans, root) && 7404a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 7414a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 7424a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 7434a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 7444a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 745a4abeea4SJosef Bacik } 7468929ecfaSYan, Zheng 7474a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 7483bbb24b2SJosef Bacik if (throttle) 7498929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 7503bbb24b2SJosef Bacik else 7518929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 7528929ecfaSYan, Zheng } 7538929ecfaSYan, Zheng 7540860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 7556df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 7566df7881aSJosef Bacik 7578929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 75813c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 75913c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 7600860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 76189ce8a63SChris Mason 76299d16cbcSSage Weil smp_mb(); 76379154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 76479154b1bSChris Mason wake_up(&cur_trans->writer_wait); 765724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 7669ed74f2dSJosef Bacik 7679ed74f2dSJosef Bacik if (current->journal_info == trans) 7689ed74f2dSJosef Bacik current->journal_info = NULL; 769ab78c84dSChris Mason 77024bbcf04SYan, Zheng if (throttle) 77124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 77224bbcf04SYan, Zheng 77349b25e05SJeff Mahoney if (trans->aborted || 7744e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 7754e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 7764edc2ca3SDave Jones err = -EIO; 7774e121c06SJosef Bacik } 778edf39272SJan Schmidt assert_qgroups_uptodate(trans); 77949b25e05SJeff Mahoney 7804edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 7814edc2ca3SDave Jones return err; 78279154b1bSChris Mason } 78379154b1bSChris Mason 78489ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 78589ce8a63SChris Mason struct btrfs_root *root) 78689ce8a63SChris Mason { 78798ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 78889ce8a63SChris Mason } 78989ce8a63SChris Mason 79089ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 79189ce8a63SChris Mason struct btrfs_root *root) 79289ce8a63SChris Mason { 79398ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 79416cdcec7SMiao Xie } 79516cdcec7SMiao Xie 796d352ac68SChris Mason /* 797d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 798d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 799690587d1SChris Mason * those extents are sent to disk but does not wait on them 800d352ac68SChris Mason */ 801690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 8028cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 80379154b1bSChris Mason { 804777e6bd7SChris Mason int err = 0; 8057c4452b9SChris Mason int werr = 0; 8061728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 807e6138876SJosef Bacik struct extent_state *cached_state = NULL; 808777e6bd7SChris Mason u64 start = 0; 8095f39d397SChris Mason u64 end; 8107c4452b9SChris Mason 8111728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 812e6138876SJosef Bacik mark, &cached_state)) { 813e6138876SJosef Bacik convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 814e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 815e6138876SJosef Bacik cached_state = NULL; 8161728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 8177c4452b9SChris Mason if (err) 8187c4452b9SChris Mason werr = err; 8191728366eSJosef Bacik cond_resched(); 8201728366eSJosef Bacik start = end + 1; 8217c4452b9SChris Mason } 822690587d1SChris Mason if (err) 823690587d1SChris Mason werr = err; 824690587d1SChris Mason return werr; 825690587d1SChris Mason } 826690587d1SChris Mason 827690587d1SChris Mason /* 828690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 829690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 830690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 831690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 832690587d1SChris Mason */ 833690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 8348cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 835690587d1SChris Mason { 836690587d1SChris Mason int err = 0; 837690587d1SChris Mason int werr = 0; 8381728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 839e6138876SJosef Bacik struct extent_state *cached_state = NULL; 840690587d1SChris Mason u64 start = 0; 841690587d1SChris Mason u64 end; 842690587d1SChris Mason 8431728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 844e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 845e6138876SJosef Bacik clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 846e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 8471728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 848777e6bd7SChris Mason if (err) 849777e6bd7SChris Mason werr = err; 850777e6bd7SChris Mason cond_resched(); 8511728366eSJosef Bacik start = end + 1; 852777e6bd7SChris Mason } 8537c4452b9SChris Mason if (err) 8547c4452b9SChris Mason werr = err; 8557c4452b9SChris Mason return werr; 85679154b1bSChris Mason } 85779154b1bSChris Mason 858690587d1SChris Mason /* 859690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 860690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 861690587d1SChris Mason * those extents are on disk for transaction or log commit 862690587d1SChris Mason */ 863171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 8648cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 865690587d1SChris Mason { 866690587d1SChris Mason int ret; 867690587d1SChris Mason int ret2; 868c6adc9ccSMiao Xie struct blk_plug plug; 869690587d1SChris Mason 870c6adc9ccSMiao Xie blk_start_plug(&plug); 8718cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 872c6adc9ccSMiao Xie blk_finish_plug(&plug); 8738cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 874bf0da8c1SChris Mason 875bf0da8c1SChris Mason if (ret) 876bf0da8c1SChris Mason return ret; 877bf0da8c1SChris Mason if (ret2) 878bf0da8c1SChris Mason return ret2; 879bf0da8c1SChris Mason return 0; 880690587d1SChris Mason } 881690587d1SChris Mason 882d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 883d0c803c4SChris Mason struct btrfs_root *root) 884d0c803c4SChris Mason { 885d0c803c4SChris Mason if (!trans || !trans->transaction) { 886d0c803c4SChris Mason struct inode *btree_inode; 887d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 888d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 889d0c803c4SChris Mason } 890d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 8918cef4e16SYan, Zheng &trans->transaction->dirty_pages, 8928cef4e16SYan, Zheng EXTENT_DIRTY); 893d0c803c4SChris Mason } 894d0c803c4SChris Mason 895d352ac68SChris Mason /* 896d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 897d352ac68SChris Mason * 898d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 899d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 900d352ac68SChris Mason * allocation tree. 901d352ac68SChris Mason * 902d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 903d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 904d352ac68SChris Mason */ 9050b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 90679154b1bSChris Mason struct btrfs_root *root) 90779154b1bSChris Mason { 90879154b1bSChris Mason int ret; 9090b86a832SChris Mason u64 old_root_bytenr; 91086b9f2ecSYan, Zheng u64 old_root_used; 9110b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 91279154b1bSChris Mason 91386b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9140b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 91556bec294SChris Mason 91679154b1bSChris Mason while (1) { 9170b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 91886b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 91986b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 92079154b1bSChris Mason break; 92187ef2bb4SChris Mason 9225d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 92379154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 9240b86a832SChris Mason &root->root_key, 9250b86a832SChris Mason &root->root_item); 92649b25e05SJeff Mahoney if (ret) 92749b25e05SJeff Mahoney return ret; 92856bec294SChris Mason 92986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9304a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 93149b25e05SJeff Mahoney if (ret) 93249b25e05SJeff Mahoney return ret; 9330b86a832SChris Mason } 934276e680dSYan Zheng 9350b86a832SChris Mason return 0; 9360b86a832SChris Mason } 9370b86a832SChris Mason 938d352ac68SChris Mason /* 939d352ac68SChris Mason * update all the cowonly tree roots on disk 94049b25e05SJeff Mahoney * 94149b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 94249b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 94349b25e05SJeff Mahoney * to clean up the delayed refs. 944d352ac68SChris Mason */ 9455d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 9460b86a832SChris Mason struct btrfs_root *root) 9470b86a832SChris Mason { 9480b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 9490b86a832SChris Mason struct list_head *next; 95084234f3aSYan Zheng struct extent_buffer *eb; 95156bec294SChris Mason int ret; 95284234f3aSYan Zheng 95356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 95449b25e05SJeff Mahoney if (ret) 95549b25e05SJeff Mahoney return ret; 95687ef2bb4SChris Mason 95784234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 95849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 95949b25e05SJeff Mahoney 0, &eb); 96084234f3aSYan Zheng btrfs_tree_unlock(eb); 96184234f3aSYan Zheng free_extent_buffer(eb); 9620b86a832SChris Mason 96349b25e05SJeff Mahoney if (ret) 96449b25e05SJeff Mahoney return ret; 96549b25e05SJeff Mahoney 96656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 96749b25e05SJeff Mahoney if (ret) 96849b25e05SJeff Mahoney return ret; 96987ef2bb4SChris Mason 970733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 971c16ce190SJosef Bacik if (ret) 972c16ce190SJosef Bacik return ret; 9738dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 974c16ce190SJosef Bacik if (ret) 975c16ce190SJosef Bacik return ret; 976546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 977c16ce190SJosef Bacik if (ret) 978c16ce190SJosef Bacik return ret; 979546adb0dSJan Schmidt 980546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 981546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 982c16ce190SJosef Bacik if (ret) 983c16ce190SJosef Bacik return ret; 984546adb0dSJan Schmidt 9850b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 9860b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 9870b86a832SChris Mason list_del_init(next); 9880b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 98987ef2bb4SChris Mason 990*9e351cc8SJosef Bacik if (root != fs_info->extent_root) 991*9e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 992*9e351cc8SJosef Bacik &trans->transaction->switch_commits); 99349b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 99449b25e05SJeff Mahoney if (ret) 99549b25e05SJeff Mahoney return ret; 99679154b1bSChris Mason } 997276e680dSYan Zheng 998*9e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 999*9e351cc8SJosef Bacik &trans->transaction->switch_commits); 10008dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 10018dabb742SStefan Behrens 100279154b1bSChris Mason return 0; 100379154b1bSChris Mason } 100479154b1bSChris Mason 1005d352ac68SChris Mason /* 1006d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1007d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1008d352ac68SChris Mason * be deleted 1009d352ac68SChris Mason */ 1010cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 10115eda7b5eSChris Mason { 1012a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1013cfad392bSJosef Bacik if (list_empty(&root->root_list)) 10149d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1015a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 10165eda7b5eSChris Mason } 10175eda7b5eSChris Mason 1018d352ac68SChris Mason /* 10195d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1020d352ac68SChris Mason */ 10215d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 10225d4f98a2SYan Zheng struct btrfs_root *root) 10230f7d52f4SChris Mason { 10240f7d52f4SChris Mason struct btrfs_root *gang[8]; 10255d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 10260f7d52f4SChris Mason int i; 10270f7d52f4SChris Mason int ret; 102854aa1f4dSChris Mason int err = 0; 102954aa1f4dSChris Mason 1030a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 10310f7d52f4SChris Mason while (1) { 10325d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 10335d4f98a2SYan Zheng (void **)gang, 0, 10340f7d52f4SChris Mason ARRAY_SIZE(gang), 10350f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 10360f7d52f4SChris Mason if (ret == 0) 10370f7d52f4SChris Mason break; 10380f7d52f4SChris Mason for (i = 0; i < ret; i++) { 10390f7d52f4SChris Mason root = gang[i]; 10405d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 10412619ba1fSChris Mason (unsigned long)root->root_key.objectid, 10420f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1043a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 104431153d81SYan Zheng 1045e02119d5SChris Mason btrfs_free_log(trans, root); 10465d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1047d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1048e02119d5SChris Mason 104982d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 105082d5902dSLi Zefan 1051f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1052f1ebcc74SLiu Bo root->force_cow = 0; 1053f1ebcc74SLiu Bo smp_wmb(); 1054f1ebcc74SLiu Bo 1055978d910dSYan Zheng if (root->commit_root != root->node) { 1056*9e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 1057*9e351cc8SJosef Bacik &trans->transaction->switch_commits); 1058978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1059978d910dSYan Zheng root->node); 1060978d910dSYan Zheng } 106131153d81SYan Zheng 10625d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 10630f7d52f4SChris Mason &root->root_key, 10640f7d52f4SChris Mason &root->root_item); 1065a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 106654aa1f4dSChris Mason if (err) 106754aa1f4dSChris Mason break; 10680f7d52f4SChris Mason } 10699f3a7427SChris Mason } 1070a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 107154aa1f4dSChris Mason return err; 10720f7d52f4SChris Mason } 10730f7d52f4SChris Mason 1074d352ac68SChris Mason /* 1075de78b51aSEric Sandeen * defrag a given btree. 1076de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1077d352ac68SChris Mason */ 1078de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1079e9d0b13bSChris Mason { 1080e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1081e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 10828929ecfaSYan, Zheng int ret; 1083e9d0b13bSChris Mason 10848929ecfaSYan, Zheng if (xchg(&root->defrag_running, 1)) 1085e9d0b13bSChris Mason return 0; 10868929ecfaSYan, Zheng 10876b80053dSChris Mason while (1) { 10888929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 10898929ecfaSYan, Zheng if (IS_ERR(trans)) 10908929ecfaSYan, Zheng return PTR_ERR(trans); 10918929ecfaSYan, Zheng 1092de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 10938929ecfaSYan, Zheng 1094e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1095b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1096e9d0b13bSChris Mason cond_resched(); 1097e9d0b13bSChris Mason 10987841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1099e9d0b13bSChris Mason break; 1100210549ebSDavid Sterba 1101210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1102efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1103210549ebSDavid Sterba ret = -EAGAIN; 1104210549ebSDavid Sterba break; 1105210549ebSDavid Sterba } 1106e9d0b13bSChris Mason } 1107e9d0b13bSChris Mason root->defrag_running = 0; 11088929ecfaSYan, Zheng return ret; 1109e9d0b13bSChris Mason } 1110e9d0b13bSChris Mason 1111d352ac68SChris Mason /* 1112d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1113aec8030aSMiao Xie * transaction commit. This does the actual creation. 1114aec8030aSMiao Xie * 1115aec8030aSMiao Xie * Note: 1116aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1117aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1118aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1119d352ac68SChris Mason */ 112080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 11213063d29fSChris Mason struct btrfs_fs_info *fs_info, 11223063d29fSChris Mason struct btrfs_pending_snapshot *pending) 11233063d29fSChris Mason { 11243063d29fSChris Mason struct btrfs_key key; 112580b6794dSChris Mason struct btrfs_root_item *new_root_item; 11263063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 11273063d29fSChris Mason struct btrfs_root *root = pending->root; 11286bdb72deSSage Weil struct btrfs_root *parent_root; 112998c9942aSLiu Bo struct btrfs_block_rsv *rsv; 11306bdb72deSSage Weil struct inode *parent_inode; 113142874b3dSMiao Xie struct btrfs_path *path; 113242874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1133a22285a6SYan, Zheng struct dentry *dentry; 11343063d29fSChris Mason struct extent_buffer *tmp; 1135925baeddSChris Mason struct extent_buffer *old; 11368ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1137aec8030aSMiao Xie int ret = 0; 1138d68fc57bSYan, Zheng u64 to_reserve = 0; 11396bdb72deSSage Weil u64 index = 0; 1140a22285a6SYan, Zheng u64 objectid; 1141b83cc969SLi Zefan u64 root_flags; 11428ea05e3aSAlexander Block uuid_le new_uuid; 11433063d29fSChris Mason 114442874b3dSMiao Xie path = btrfs_alloc_path(); 114542874b3dSMiao Xie if (!path) { 1146aec8030aSMiao Xie pending->error = -ENOMEM; 1147aec8030aSMiao Xie return 0; 114842874b3dSMiao Xie } 114942874b3dSMiao Xie 115080b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 115180b6794dSChris Mason if (!new_root_item) { 1152aec8030aSMiao Xie pending->error = -ENOMEM; 11536fa9700eSMiao Xie goto root_item_alloc_fail; 115480b6794dSChris Mason } 1155a22285a6SYan, Zheng 1156aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1157aec8030aSMiao Xie if (pending->error) 11586fa9700eSMiao Xie goto no_free_objectid; 11593063d29fSChris Mason 11603fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1161d68fc57bSYan, Zheng 1162d68fc57bSYan, Zheng if (to_reserve > 0) { 1163aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1164aec8030aSMiao Xie &pending->block_rsv, 116508e007d2SMiao Xie to_reserve, 116608e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1167aec8030aSMiao Xie if (pending->error) 11686fa9700eSMiao Xie goto no_free_objectid; 1169d68fc57bSYan, Zheng } 1170d68fc57bSYan, Zheng 1171aec8030aSMiao Xie pending->error = btrfs_qgroup_inherit(trans, fs_info, 1172aec8030aSMiao Xie root->root_key.objectid, 11736f72c7e2SArne Jansen objectid, pending->inherit); 1174aec8030aSMiao Xie if (pending->error) 11756fa9700eSMiao Xie goto no_free_objectid; 11766f72c7e2SArne Jansen 11773063d29fSChris Mason key.objectid = objectid; 1178a22285a6SYan, Zheng key.offset = (u64)-1; 1179a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 11803063d29fSChris Mason 11816fa9700eSMiao Xie rsv = trans->block_rsv; 1182a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 11832382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 11846bdb72deSSage Weil 1185a22285a6SYan, Zheng dentry = pending->dentry; 1186e9662f70SMiao Xie parent_inode = pending->dir; 1187a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 11887585717fSChris Mason record_root_in_trans(trans, parent_root); 1189a22285a6SYan, Zheng 11906bdb72deSSage Weil /* 11916bdb72deSSage Weil * insert the directory item 11926bdb72deSSage Weil */ 11936bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 119449b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 119542874b3dSMiao Xie 119642874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 119742874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 119842874b3dSMiao Xie btrfs_ino(parent_inode), 119942874b3dSMiao Xie dentry->d_name.name, 120042874b3dSMiao Xie dentry->d_name.len, 0); 120142874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1202fe66a05aSChris Mason pending->error = -EEXIST; 1203aec8030aSMiao Xie goto dir_item_existed; 120442874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 120542874b3dSMiao Xie ret = PTR_ERR(dir_item); 12068732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12078732d44fSMiao Xie goto fail; 120879787eaaSJeff Mahoney } 120942874b3dSMiao Xie btrfs_release_path(path); 12106bdb72deSSage Weil 1211e999376fSChris Mason /* 1212e999376fSChris Mason * pull in the delayed directory update 1213e999376fSChris Mason * and the delayed inode item 1214e999376fSChris Mason * otherwise we corrupt the FS during 1215e999376fSChris Mason * snapshot 1216e999376fSChris Mason */ 1217e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 12188732d44fSMiao Xie if (ret) { /* Transaction aborted */ 12198732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12208732d44fSMiao Xie goto fail; 12218732d44fSMiao Xie } 1222e999376fSChris Mason 12237585717fSChris Mason record_root_in_trans(trans, root); 12246bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 12256bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 122608fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 12276bdb72deSSage Weil 1228b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1229b83cc969SLi Zefan if (pending->readonly) 1230b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1231b83cc969SLi Zefan else 1232b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1233b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1234b83cc969SLi Zefan 12358ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 12368ea05e3aSAlexander Block trans->transid); 12378ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 12388ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 12398ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 12408ea05e3aSAlexander Block BTRFS_UUID_SIZE); 124170023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 124270023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 124370023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 12448ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 12458ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 12468ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 12478ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 124870023da2SStefan Behrens } 12493cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 12503cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 125170023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 12528ea05e3aSAlexander Block 1253925baeddSChris Mason old = btrfs_lock_root_node(root); 125449b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 125579787eaaSJeff Mahoney if (ret) { 125679787eaaSJeff Mahoney btrfs_tree_unlock(old); 125779787eaaSJeff Mahoney free_extent_buffer(old); 12588732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12598732d44fSMiao Xie goto fail; 126079787eaaSJeff Mahoney } 126149b25e05SJeff Mahoney 12625d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 12633063d29fSChris Mason 126449b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 126579787eaaSJeff Mahoney /* clean up in any case */ 1266925baeddSChris Mason btrfs_tree_unlock(old); 1267925baeddSChris Mason free_extent_buffer(old); 12688732d44fSMiao Xie if (ret) { 12698732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12708732d44fSMiao Xie goto fail; 12718732d44fSMiao Xie } 12723063d29fSChris Mason 1273f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1274f1ebcc74SLiu Bo root->force_cow = 1; 1275f1ebcc74SLiu Bo smp_wmb(); 1276f1ebcc74SLiu Bo 12775d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1278a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1279a22285a6SYan, Zheng key.offset = trans->transid; 1280a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1281925baeddSChris Mason btrfs_tree_unlock(tmp); 12823063d29fSChris Mason free_extent_buffer(tmp); 12838732d44fSMiao Xie if (ret) { 12848732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12858732d44fSMiao Xie goto fail; 12868732d44fSMiao Xie } 12870660b5afSChris Mason 1288a22285a6SYan, Zheng /* 1289a22285a6SYan, Zheng * insert root back/forward references 1290a22285a6SYan, Zheng */ 1291a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1292a22285a6SYan, Zheng parent_root->root_key.objectid, 129333345d01SLi Zefan btrfs_ino(parent_inode), index, 1294a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 12958732d44fSMiao Xie if (ret) { 12968732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12978732d44fSMiao Xie goto fail; 12988732d44fSMiao Xie } 1299a22285a6SYan, Zheng 1300a22285a6SYan, Zheng key.offset = (u64)-1; 1301a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 130279787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 130379787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 13048732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13058732d44fSMiao Xie goto fail; 130679787eaaSJeff Mahoney } 1307d68fc57bSYan, Zheng 130849b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 13098732d44fSMiao Xie if (ret) { 13108732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13118732d44fSMiao Xie goto fail; 13128732d44fSMiao Xie } 1313361048f5SMiao Xie 1314361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 13158732d44fSMiao Xie if (ret) { 13168732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13178732d44fSMiao Xie goto fail; 13188732d44fSMiao Xie } 131942874b3dSMiao Xie 132042874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 132142874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 132242874b3dSMiao Xie parent_inode, &key, 132342874b3dSMiao Xie BTRFS_FT_DIR, index); 132442874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 13259c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 13268732d44fSMiao Xie if (ret) { 13278732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13288732d44fSMiao Xie goto fail; 13298732d44fSMiao Xie } 133042874b3dSMiao Xie 133142874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 133242874b3dSMiao Xie dentry->d_name.len * 2); 133342874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1334be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1335dd5f9615SStefan Behrens if (ret) { 13368732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1337dd5f9615SStefan Behrens goto fail; 1338dd5f9615SStefan Behrens } 1339dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1340dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1341dd5f9615SStefan Behrens if (ret) { 1342dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1343dd5f9615SStefan Behrens goto fail; 1344dd5f9615SStefan Behrens } 1345dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1346dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1347dd5f9615SStefan Behrens new_root_item->received_uuid, 1348dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1349dd5f9615SStefan Behrens objectid); 1350dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1351dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1352dd5f9615SStefan Behrens goto fail; 1353dd5f9615SStefan Behrens } 1354dd5f9615SStefan Behrens } 13553063d29fSChris Mason fail: 1356aec8030aSMiao Xie pending->error = ret; 1357aec8030aSMiao Xie dir_item_existed: 135898c9942aSLiu Bo trans->block_rsv = rsv; 13592382c5ccSLiu Bo trans->bytes_reserved = 0; 13606fa9700eSMiao Xie no_free_objectid: 13616fa9700eSMiao Xie kfree(new_root_item); 13626fa9700eSMiao Xie root_item_alloc_fail: 136342874b3dSMiao Xie btrfs_free_path(path); 136449b25e05SJeff Mahoney return ret; 13653063d29fSChris Mason } 13663063d29fSChris Mason 1367d352ac68SChris Mason /* 1368d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1369d352ac68SChris Mason */ 137080b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 13713063d29fSChris Mason struct btrfs_fs_info *fs_info) 13723063d29fSChris Mason { 1373aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 13743063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1375aec8030aSMiao Xie int ret = 0; 13763de4586cSChris Mason 1377aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1378aec8030aSMiao Xie list_del(&pending->list); 1379aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1380aec8030aSMiao Xie if (ret) 1381aec8030aSMiao Xie break; 1382aec8030aSMiao Xie } 1383aec8030aSMiao Xie return ret; 13843de4586cSChris Mason } 13853de4586cSChris Mason 13865d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 13875d4f98a2SYan Zheng { 13885d4f98a2SYan Zheng struct btrfs_root_item *root_item; 13895d4f98a2SYan Zheng struct btrfs_super_block *super; 13905d4f98a2SYan Zheng 13916c41761fSDavid Sterba super = root->fs_info->super_copy; 13925d4f98a2SYan Zheng 13935d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 13945d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 13955d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 13965d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 13975d4f98a2SYan Zheng 13985d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 13995d4f98a2SYan Zheng super->root = root_item->bytenr; 14005d4f98a2SYan Zheng super->generation = root_item->generation; 14015d4f98a2SYan Zheng super->root_level = root_item->level; 140273bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 14030af3d00bSJosef Bacik super->cache_generation = root_item->generation; 140470f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 140526432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 14065d4f98a2SYan Zheng } 14075d4f98a2SYan Zheng 1408f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1409f36f3042SChris Mason { 14104a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1411f36f3042SChris Mason int ret = 0; 14124a9d8bdeSMiao Xie 1413a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 14144a9d8bdeSMiao Xie trans = info->running_transaction; 14154a9d8bdeSMiao Xie if (trans) 14164a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1417a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1418f36f3042SChris Mason return ret; 1419f36f3042SChris Mason } 1420f36f3042SChris Mason 14218929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 14228929ecfaSYan, Zheng { 14234a9d8bdeSMiao Xie struct btrfs_transaction *trans; 14248929ecfaSYan, Zheng int ret = 0; 14254a9d8bdeSMiao Xie 1426a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 14274a9d8bdeSMiao Xie trans = info->running_transaction; 14284a9d8bdeSMiao Xie if (trans) 14294a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1430a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 14318929ecfaSYan, Zheng return ret; 14328929ecfaSYan, Zheng } 14338929ecfaSYan, Zheng 1434bb9c12c9SSage Weil /* 1435bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1436bb9c12c9SSage Weil * transaction joins 1437bb9c12c9SSage Weil */ 1438bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1439bb9c12c9SSage Weil struct btrfs_transaction *trans) 1440bb9c12c9SSage Weil { 14414a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1442501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1443501407aaSJosef Bacik trans->aborted); 1444bb9c12c9SSage Weil } 1445bb9c12c9SSage Weil 1446bb9c12c9SSage Weil /* 1447bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1448bb9c12c9SSage Weil * caller holds ref. 1449bb9c12c9SSage Weil */ 1450bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1451bb9c12c9SSage Weil struct btrfs_transaction *trans) 1452bb9c12c9SSage Weil { 145372d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1454501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1455501407aaSJosef Bacik trans->aborted); 1456bb9c12c9SSage Weil } 1457bb9c12c9SSage Weil 1458bb9c12c9SSage Weil /* 1459bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1460bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1461bb9c12c9SSage Weil */ 1462bb9c12c9SSage Weil struct btrfs_async_commit { 1463bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1464bb9c12c9SSage Weil struct btrfs_root *root; 14657892b5afSMiao Xie struct work_struct work; 1466bb9c12c9SSage Weil }; 1467bb9c12c9SSage Weil 1468bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1469bb9c12c9SSage Weil { 1470bb9c12c9SSage Weil struct btrfs_async_commit *ac = 14717892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1472bb9c12c9SSage Weil 14736fc4e354SSage Weil /* 14746fc4e354SSage Weil * We've got freeze protection passed with the transaction. 14756fc4e354SSage Weil * Tell lockdep about it. 14766fc4e354SSage Weil */ 1477b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 14786fc4e354SSage Weil rwsem_acquire_read( 14796fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 14806fc4e354SSage Weil 0, 1, _THIS_IP_); 14816fc4e354SSage Weil 1482e209db7aSSage Weil current->journal_info = ac->newtrans; 1483e209db7aSSage Weil 1484bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1485bb9c12c9SSage Weil kfree(ac); 1486bb9c12c9SSage Weil } 1487bb9c12c9SSage Weil 1488bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1489bb9c12c9SSage Weil struct btrfs_root *root, 1490bb9c12c9SSage Weil int wait_for_unblock) 1491bb9c12c9SSage Weil { 1492bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1493bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1494bb9c12c9SSage Weil 1495bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1496db5b493aSTsutomu Itoh if (!ac) 1497db5b493aSTsutomu Itoh return -ENOMEM; 1498bb9c12c9SSage Weil 14997892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1500bb9c12c9SSage Weil ac->root = root; 15017a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 15023612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 15033612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 15043612b495STsutomu Itoh kfree(ac); 15053612b495STsutomu Itoh return err; 15063612b495STsutomu Itoh } 1507bb9c12c9SSage Weil 1508bb9c12c9SSage Weil /* take transaction reference */ 1509bb9c12c9SSage Weil cur_trans = trans->transaction; 151013c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1511bb9c12c9SSage Weil 1512bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 15136fc4e354SSage Weil 15146fc4e354SSage Weil /* 15156fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 15166fc4e354SSage Weil * async commit thread will be the one to unlock it. 15176fc4e354SSage Weil */ 1518b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1519ff7c1d33SMiao Xie rwsem_release( 1520ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 15216fc4e354SSage Weil 1, _THIS_IP_); 15226fc4e354SSage Weil 15237892b5afSMiao Xie schedule_work(&ac->work); 1524bb9c12c9SSage Weil 1525bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1526bb9c12c9SSage Weil if (wait_for_unblock) 1527bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1528bb9c12c9SSage Weil else 1529bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1530bb9c12c9SSage Weil 153138e88054SSage Weil if (current->journal_info == trans) 153238e88054SSage Weil current->journal_info = NULL; 153338e88054SSage Weil 1534724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1535bb9c12c9SSage Weil return 0; 1536bb9c12c9SSage Weil } 1537bb9c12c9SSage Weil 153849b25e05SJeff Mahoney 153949b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 15407b8b92afSJosef Bacik struct btrfs_root *root, int err) 154149b25e05SJeff Mahoney { 154249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1543f094ac32SLiu Bo DEFINE_WAIT(wait); 154449b25e05SJeff Mahoney 154549b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 154649b25e05SJeff Mahoney 15477b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 15487b8b92afSJosef Bacik 154949b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 155066b6135bSLiu Bo 155125d8c284SMiao Xie /* 155225d8c284SMiao Xie * If the transaction is removed from the list, it means this 155325d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 155425d8c284SMiao Xie * to call the cleanup function. 155525d8c284SMiao Xie */ 155625d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 155766b6135bSLiu Bo 155849b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1559d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 15604a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1561f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1562f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1563f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1564f094ac32SLiu Bo 1565f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1566d7096fc3SJosef Bacik } 156749b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 156849b25e05SJeff Mahoney 156949b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 157049b25e05SJeff Mahoney 15714a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 15724a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 15734a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 15744a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 15754a9d8bdeSMiao Xie 1576e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1577e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1578724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1579724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 158049b25e05SJeff Mahoney 158149b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 158249b25e05SJeff Mahoney 158349b25e05SJeff Mahoney if (current->journal_info == trans) 158449b25e05SJeff Mahoney current->journal_info = NULL; 1585c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 158649b25e05SJeff Mahoney 158749b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 158849b25e05SJeff Mahoney } 158949b25e05SJeff Mahoney 1590ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans, 1591ca469637SMiao Xie struct btrfs_root *root) 1592ca469637SMiao Xie { 1593ca469637SMiao Xie int ret; 1594ca469637SMiao Xie 1595ca469637SMiao Xie ret = btrfs_run_delayed_items(trans, root); 1596ca469637SMiao Xie /* 1597ca469637SMiao Xie * running the delayed items may have added new refs. account 1598ca469637SMiao Xie * them now so that they hinder processing of more delayed refs 1599ca469637SMiao Xie * as little as possible. 1600ca469637SMiao Xie */ 160180d94fb3SFilipe David Borba Manana if (ret) { 1602ca469637SMiao Xie btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); 160380d94fb3SFilipe David Borba Manana return ret; 160480d94fb3SFilipe David Borba Manana } 160580d94fb3SFilipe David Borba Manana 160680d94fb3SFilipe David Borba Manana ret = btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); 160780d94fb3SFilipe David Borba Manana if (ret) 160880d94fb3SFilipe David Borba Manana return ret; 1609ca469637SMiao Xie 1610ca469637SMiao Xie /* 1611ca469637SMiao Xie * rename don't use btrfs_join_transaction, so, once we 1612ca469637SMiao Xie * set the transaction to blocked above, we aren't going 1613ca469637SMiao Xie * to get any new ordered operations. We can safely run 1614ca469637SMiao Xie * it here and no for sure that nothing new will be added 1615ca469637SMiao Xie * to the list 1616ca469637SMiao Xie */ 1617569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 1); 1618ca469637SMiao Xie 1619eebc6084SMiao Xie return ret; 1620ca469637SMiao Xie } 1621ca469637SMiao Xie 162282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 162382436617SMiao Xie { 162482436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 16256c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 162682436617SMiao Xie return 0; 162782436617SMiao Xie } 162882436617SMiao Xie 162982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 163082436617SMiao Xie { 163182436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1632b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 163382436617SMiao Xie } 163482436617SMiao Xie 163579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 163679154b1bSChris Mason struct btrfs_root *root) 163779154b1bSChris Mason { 163849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 16398fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 164025287e0aSMiao Xie int ret; 164179154b1bSChris Mason 1642569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 0); 164325287e0aSMiao Xie if (ret) { 164425287e0aSMiao Xie btrfs_abort_transaction(trans, root, ret); 1645e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1646e4a2bcacSJosef Bacik return ret; 164725287e0aSMiao Xie } 164825287e0aSMiao Xie 16498d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 16508d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 165125287e0aSMiao Xie ret = cur_trans->aborted; 1652e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1653e4a2bcacSJosef Bacik return ret; 165425287e0aSMiao Xie } 165549b25e05SJeff Mahoney 165656bec294SChris Mason /* make a pass through all the delayed refs we have so far 165756bec294SChris Mason * any runnings procs may add more while we are here 165856bec294SChris Mason */ 165956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1660e4a2bcacSJosef Bacik if (ret) { 1661e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1662e4a2bcacSJosef Bacik return ret; 1663e4a2bcacSJosef Bacik } 166456bec294SChris Mason 16650e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 16660e721106SJosef Bacik trans->block_rsv = NULL; 1667272d26d0SMiao Xie if (trans->qgroup_reserved) { 1668272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1669272d26d0SMiao Xie trans->qgroup_reserved = 0; 1670272d26d0SMiao Xie } 16710e721106SJosef Bacik 1672b7ec40d7SChris Mason cur_trans = trans->transaction; 167349b25e05SJeff Mahoney 167456bec294SChris Mason /* 167556bec294SChris Mason * set the flushing flag so procs in this transaction have to 167656bec294SChris Mason * start sending their work down. 167756bec294SChris Mason */ 1678b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 16791be41b78SJosef Bacik smp_wmb(); 168056bec294SChris Mason 1681ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1682ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1683ea658badSJosef Bacik 1684c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1685e4a2bcacSJosef Bacik if (ret) { 1686e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1687e4a2bcacSJosef Bacik return ret; 1688e4a2bcacSJosef Bacik } 168956bec294SChris Mason 16904a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 16914a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 16924a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 169313c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 169449b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1695ccd467d6SChris Mason 1696b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 169715ee9bc7SJosef Bacik 1698724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 169915ee9bc7SJosef Bacik 170049b25e05SJeff Mahoney return ret; 170179154b1bSChris Mason } 17024313b399SChris Mason 17034a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1704bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1705bb9c12c9SSage Weil 1706ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1707ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1708ccd467d6SChris Mason struct btrfs_transaction, list); 17094a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 171013c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1711a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1712ccd467d6SChris Mason 1713ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1714ccd467d6SChris Mason 1715724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 1716a4abeea4SJosef Bacik } else { 1717a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1718ccd467d6SChris Mason } 1719a4abeea4SJosef Bacik } else { 1720a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1721ccd467d6SChris Mason } 172215ee9bc7SJosef Bacik 17230860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 17240860adfdSMiao Xie 172582436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 172682436617SMiao Xie if (ret) 172782436617SMiao Xie goto cleanup_transaction; 172882436617SMiao Xie 1729ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 173049b25e05SJeff Mahoney if (ret) 173149b25e05SJeff Mahoney goto cleanup_transaction; 173216cdcec7SMiao Xie 1733581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1734581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1735ed3b3d31SChris Mason 1736581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 1737ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 1738ca469637SMiao Xie if (ret) 1739ca469637SMiao Xie goto cleanup_transaction; 1740ca469637SMiao Xie 174182436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1742cb7ab021SWang Shilong 1743cb7ab021SWang Shilong btrfs_scrub_pause(root); 1744ed0ca140SJosef Bacik /* 1745ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1746ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 17474a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1748ed0ca140SJosef Bacik */ 1749a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 17504a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1751a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1752ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1753ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 175415ee9bc7SJosef Bacik 17552cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 17562cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 17572cba30f1SMiao Xie ret = cur_trans->aborted; 17586cf7f77eSWang Shilong goto scrub_continue; 17592cba30f1SMiao Xie } 17607585717fSChris Mason /* 17617585717fSChris Mason * the reloc mutex makes sure that we stop 17627585717fSChris Mason * the balancing code from coming in and moving 17637585717fSChris Mason * extents around in the middle of the commit 17647585717fSChris Mason */ 17657585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 17667585717fSChris Mason 176742874b3dSMiao Xie /* 176842874b3dSMiao Xie * We needn't worry about the delayed items because we will 176942874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 177042874b3dSMiao Xie * core function of the snapshot creation. 177142874b3dSMiao Xie */ 177242874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 177349b25e05SJeff Mahoney if (ret) { 177449b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 17756cf7f77eSWang Shilong goto scrub_continue; 177649b25e05SJeff Mahoney } 17773063d29fSChris Mason 177842874b3dSMiao Xie /* 177942874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 178042874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 178142874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 178242874b3dSMiao Xie * them. 178342874b3dSMiao Xie * 178442874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 178542874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 178642874b3dSMiao Xie * the nodes and leaves. 178742874b3dSMiao Xie */ 178842874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 178949b25e05SJeff Mahoney if (ret) { 179049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 17916cf7f77eSWang Shilong goto scrub_continue; 179249b25e05SJeff Mahoney } 179316cdcec7SMiao Xie 179456bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 179549b25e05SJeff Mahoney if (ret) { 179649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 17976cf7f77eSWang Shilong goto scrub_continue; 179849b25e05SJeff Mahoney } 179956bec294SChris Mason 1800e999376fSChris Mason /* 1801e999376fSChris Mason * make sure none of the code above managed to slip in a 1802e999376fSChris Mason * delayed item 1803e999376fSChris Mason */ 1804e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1805e999376fSChris Mason 18062c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1807dc17ff8fSChris Mason 1808e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1809e02119d5SChris Mason * various roots consistent with each other. Every pointer 1810e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1811e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1812e02119d5SChris Mason * the tree logging code from jumping in and changing any 1813e02119d5SChris Mason * of the trees. 1814e02119d5SChris Mason * 1815e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1816e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1817e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1818e02119d5SChris Mason * from now until after the super is written, we avoid races 1819e02119d5SChris Mason * with the tree-log code. 1820e02119d5SChris Mason */ 1821e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 18221a40e23bSZheng Yan 18235d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 182449b25e05SJeff Mahoney if (ret) { 182549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1826871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 18276cf7f77eSWang Shilong goto scrub_continue; 182849b25e05SJeff Mahoney } 182954aa1f4dSChris Mason 18303818aea2SQu Wenruo /* 18313818aea2SQu Wenruo * Since the transaction is done, we should set the inode map cache flag 18323818aea2SQu Wenruo * before any other comming transaction. 18333818aea2SQu Wenruo */ 18343818aea2SQu Wenruo if (btrfs_test_opt(root, CHANGE_INODE_CACHE)) 18353818aea2SQu Wenruo btrfs_set_opt(root->fs_info->mount_opt, INODE_MAP_CACHE); 18363818aea2SQu Wenruo else 18373818aea2SQu Wenruo btrfs_clear_opt(root->fs_info->mount_opt, INODE_MAP_CACHE); 18383818aea2SQu Wenruo 18395d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1840e02119d5SChris Mason * safe to free the root of tree log roots 1841e02119d5SChris Mason */ 1842e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1843e02119d5SChris Mason 18445d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 184549b25e05SJeff Mahoney if (ret) { 184649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1847871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 18486cf7f77eSWang Shilong goto scrub_continue; 184949b25e05SJeff Mahoney } 185054aa1f4dSChris Mason 18512cba30f1SMiao Xie /* 18522cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 18532cba30f1SMiao Xie * update ->aborted, check it. 18542cba30f1SMiao Xie */ 18552cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 18562cba30f1SMiao Xie ret = cur_trans->aborted; 18572cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 18582cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 18596cf7f77eSWang Shilong goto scrub_continue; 18602cba30f1SMiao Xie } 18612cba30f1SMiao Xie 186211833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 186311833d66SYan Zheng 186478fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 18655f39d397SChris Mason 18665d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 18675d4f98a2SYan Zheng root->fs_info->tree_root->node); 1868*9e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 1869*9e351cc8SJosef Bacik &cur_trans->switch_commits); 18705d4f98a2SYan Zheng 18715d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 18725d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1873*9e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 1874*9e351cc8SJosef Bacik &cur_trans->switch_commits); 1875*9e351cc8SJosef Bacik 1876*9e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 18775d4f98a2SYan Zheng 1878edf39272SJan Schmidt assert_qgroups_uptodate(trans); 18795d4f98a2SYan Zheng update_super_roots(root); 1880e02119d5SChris Mason 18816c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 18826c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 18836c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 18846c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1885ccd467d6SChris Mason 1886a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 18874a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 1888a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1889a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 18907585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1891b7ec40d7SChris Mason 1892f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1893e6dcd2dcSChris Mason 189479154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 189549b25e05SJeff Mahoney if (ret) { 189649b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 189708748810SDavid Sterba "Error while writing out transaction"); 189849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 18996cf7f77eSWang Shilong goto scrub_continue; 190049b25e05SJeff Mahoney } 190149b25e05SJeff Mahoney 190249b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 190349b25e05SJeff Mahoney if (ret) { 190449b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 19056cf7f77eSWang Shilong goto scrub_continue; 190649b25e05SJeff Mahoney } 19074313b399SChris Mason 1908e02119d5SChris Mason /* 1909e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1910e02119d5SChris Mason * to go about their business 1911e02119d5SChris Mason */ 1912e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1913e02119d5SChris Mason 191411833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 19154313b399SChris Mason 191615ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 19174a9d8bdeSMiao Xie /* 19184a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 19194a9d8bdeSMiao Xie * which can change it. 19204a9d8bdeSMiao Xie */ 19214a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 19222c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 19233de4586cSChris Mason 1924a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 192513c5a93eSJosef Bacik list_del_init(&cur_trans->list); 1926a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1927a4abeea4SJosef Bacik 1928724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1929724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 193058176a96SJosef Bacik 19310860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 1932b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 1933b2b5ef5cSJan Kara 19341abe9b8aSliubo trace_btrfs_transaction_commit(root); 19351abe9b8aSliubo 1936a2de733cSArne Jansen btrfs_scrub_continue(root); 1937a2de733cSArne Jansen 19389ed74f2dSJosef Bacik if (current->journal_info == trans) 19399ed74f2dSJosef Bacik current->journal_info = NULL; 19409ed74f2dSJosef Bacik 19412c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 194224bbcf04SYan, Zheng 194324bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 194424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 194524bbcf04SYan, Zheng 194679154b1bSChris Mason return ret; 194749b25e05SJeff Mahoney 19486cf7f77eSWang Shilong scrub_continue: 19496cf7f77eSWang Shilong btrfs_scrub_continue(root); 195049b25e05SJeff Mahoney cleanup_transaction: 19510e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 19520e721106SJosef Bacik trans->block_rsv = NULL; 1953272d26d0SMiao Xie if (trans->qgroup_reserved) { 1954272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1955272d26d0SMiao Xie trans->qgroup_reserved = 0; 1956272d26d0SMiao Xie } 1957c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 195849b25e05SJeff Mahoney if (current->journal_info == trans) 195949b25e05SJeff Mahoney current->journal_info = NULL; 19607b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 196149b25e05SJeff Mahoney 196249b25e05SJeff Mahoney return ret; 196379154b1bSChris Mason } 196479154b1bSChris Mason 1965d352ac68SChris Mason /* 19669d1a2a3aSDavid Sterba * return < 0 if error 19679d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 19689d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 19699d1a2a3aSDavid Sterba * 19709d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 19719d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 19729d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 19739d1a2a3aSDavid Sterba * few seconds later. 1974d352ac68SChris Mason */ 19759d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 1976e9d0b13bSChris Mason { 19779d1a2a3aSDavid Sterba int ret; 19785d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1979e9d0b13bSChris Mason 1980a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 19819d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 19829d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 19839d1a2a3aSDavid Sterba return 0; 19849d1a2a3aSDavid Sterba } 19859d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 19869d1a2a3aSDavid Sterba struct btrfs_root, root_list); 198718f687d5SWang Shilong /* 198818f687d5SWang Shilong * Make sure root is not involved in send, 198918f687d5SWang Shilong * if we fail with first root, we return 199018f687d5SWang Shilong * directly rather than continue. 199118f687d5SWang Shilong */ 199218f687d5SWang Shilong spin_lock(&root->root_item_lock); 199318f687d5SWang Shilong if (root->send_in_progress) { 199418f687d5SWang Shilong spin_unlock(&fs_info->trans_lock); 199518f687d5SWang Shilong spin_unlock(&root->root_item_lock); 199618f687d5SWang Shilong return 0; 199718f687d5SWang Shilong } 199818f687d5SWang Shilong spin_unlock(&root->root_item_lock); 199918f687d5SWang Shilong 2000cfad392bSJosef Bacik list_del_init(&root->root_list); 2001a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 20025d4f98a2SYan Zheng 2003efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 200476dda93cSYan, Zheng 200516cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 200616cdcec7SMiao Xie 200776dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 200876dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 20092c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 201076dda93cSYan, Zheng else 20112c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 20129d1a2a3aSDavid Sterba /* 20139d1a2a3aSDavid Sterba * If we encounter a transaction abort during snapshot cleaning, we 20149d1a2a3aSDavid Sterba * don't want to crash here 20159d1a2a3aSDavid Sterba */ 20166596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2017e9d0b13bSChris Mason } 2018