16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2679154b1bSChris Mason #include "ctree.h" 2779154b1bSChris Mason #include "disk-io.h" 2879154b1bSChris Mason #include "transaction.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31581bb050SLi Zefan #include "inode-map.h" 32733f4fbbSStefan Behrens #include "volumes.h" 338dabb742SStefan Behrens #include "dev-replace.h" 34fcebe456SJosef Bacik #include "qgroup.h" 3579154b1bSChris Mason 360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 370f7d52f4SChris Mason 384a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 394a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 404a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 414a9d8bdeSMiao Xie __TRANS_START), 424a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 434a9d8bdeSMiao Xie __TRANS_START | 444a9d8bdeSMiao Xie __TRANS_ATTACH), 454a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 464a9d8bdeSMiao Xie __TRANS_START | 474a9d8bdeSMiao Xie __TRANS_ATTACH | 484a9d8bdeSMiao Xie __TRANS_JOIN), 494a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 504a9d8bdeSMiao Xie __TRANS_START | 514a9d8bdeSMiao Xie __TRANS_ATTACH | 524a9d8bdeSMiao Xie __TRANS_JOIN | 534a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 544a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 554a9d8bdeSMiao Xie __TRANS_START | 564a9d8bdeSMiao Xie __TRANS_ATTACH | 574a9d8bdeSMiao Xie __TRANS_JOIN | 584a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 594a9d8bdeSMiao Xie }; 604a9d8bdeSMiao Xie 61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 6279154b1bSChris Mason { 6313c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6413c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 65a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 66c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 676df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 686df9a95eSJosef Bacik struct extent_map *em; 696df9a95eSJosef Bacik 706df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 716df9a95eSJosef Bacik struct extent_map, list); 726df9a95eSJosef Bacik list_del_init(&em->list); 736df9a95eSJosef Bacik free_extent_map(em); 746df9a95eSJosef Bacik } 752c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7679154b1bSChris Mason } 7778fae27eSChris Mason } 7879154b1bSChris Mason 79663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 80663dfbb0SFilipe Manana { 81663dfbb0SFilipe Manana spin_lock(&tree->lock); 82663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 83663dfbb0SFilipe Manana struct rb_node *node; 84663dfbb0SFilipe Manana struct extent_state *state; 85663dfbb0SFilipe Manana 86663dfbb0SFilipe Manana node = rb_first(&tree->state); 87663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 88663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 89663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 90663dfbb0SFilipe Manana /* 91663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 92663dfbb0SFilipe Manana * changes in the flags of extent states ever. 93663dfbb0SFilipe Manana */ 94663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 95663dfbb0SFilipe Manana free_extent_state(state); 96663dfbb0SFilipe Manana if (need_resched()) { 97663dfbb0SFilipe Manana spin_unlock(&tree->lock); 98663dfbb0SFilipe Manana cond_resched(); 99663dfbb0SFilipe Manana spin_lock(&tree->lock); 100663dfbb0SFilipe Manana } 101663dfbb0SFilipe Manana } 102663dfbb0SFilipe Manana spin_unlock(&tree->lock); 103663dfbb0SFilipe Manana } 104663dfbb0SFilipe Manana 1059e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 1069e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 107817d52f8SJosef Bacik { 1089e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1099e351cc8SJosef Bacik 1109e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1119e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1129e351cc8SJosef Bacik dirty_list) { 1139e351cc8SJosef Bacik list_del_init(&root->dirty_list); 114817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 115817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1169e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1179e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 118663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1199e351cc8SJosef Bacik } 1209e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 121817d52f8SJosef Bacik } 122817d52f8SJosef Bacik 1230860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1240860adfdSMiao Xie unsigned int type) 1250860adfdSMiao Xie { 1260860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1270860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1280860adfdSMiao Xie } 1290860adfdSMiao Xie 1300860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1310860adfdSMiao Xie unsigned int type) 1320860adfdSMiao Xie { 1330860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1340860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1350860adfdSMiao Xie } 1360860adfdSMiao Xie 1370860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1380860adfdSMiao Xie unsigned int type) 1390860adfdSMiao Xie { 1400860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1410860adfdSMiao Xie } 1420860adfdSMiao Xie 1430860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1440860adfdSMiao Xie { 1450860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 146178260b2SMiao Xie } 147178260b2SMiao Xie 148d352ac68SChris Mason /* 149d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 150d352ac68SChris Mason */ 1510860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 15279154b1bSChris Mason { 15379154b1bSChris Mason struct btrfs_transaction *cur_trans; 15419ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 155a4abeea4SJosef Bacik 15619ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 157d43317dcSChris Mason loop: 15849b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 15987533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 16019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16149b25e05SJeff Mahoney return -EROFS; 16249b25e05SJeff Mahoney } 16349b25e05SJeff Mahoney 16419ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 165a4abeea4SJosef Bacik if (cur_trans) { 166871383beSDavid Sterba if (cur_trans->aborted) { 16719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16849b25e05SJeff Mahoney return cur_trans->aborted; 169871383beSDavid Sterba } 1704a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 171178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 172178260b2SMiao Xie return -EBUSY; 173178260b2SMiao Xie } 174a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 175a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1760860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 17719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 178a4abeea4SJosef Bacik return 0; 179a4abeea4SJosef Bacik } 18019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 181a4abeea4SJosef Bacik 182354aa0fbSMiao Xie /* 183354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 184354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 185354aa0fbSMiao Xie */ 186354aa0fbSMiao Xie if (type == TRANS_ATTACH) 187354aa0fbSMiao Xie return -ENOENT; 188354aa0fbSMiao Xie 1894a9d8bdeSMiao Xie /* 1904a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1914a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1924a9d8bdeSMiao Xie */ 1934a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1944a9d8bdeSMiao Xie 195a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 196db5b493aSTsutomu Itoh if (!cur_trans) 197db5b493aSTsutomu Itoh return -ENOMEM; 198d43317dcSChris Mason 19919ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 20019ae4e81SJan Schmidt if (fs_info->running_transaction) { 201d43317dcSChris Mason /* 202d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2034a9d8bdeSMiao Xie * to redo the checks above 204d43317dcSChris Mason */ 205a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 206d43317dcSChris Mason goto loop; 20787533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 208e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2097b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 2107b8b92afSJosef Bacik return -EROFS; 211a4abeea4SJosef Bacik } 212d43317dcSChris Mason 21313c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2140860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 21579154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 21679154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 2174a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 218a4abeea4SJosef Bacik /* 219a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 220a4abeea4SJosef Bacik * commit the transaction. 221a4abeea4SJosef Bacik */ 222a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 22308607c1bSChris Mason cur_trans->start_time = get_seconds(); 22456bec294SChris Mason 225c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 226d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 227c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 228c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 22956bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 230c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 23120b297d6SJan Schmidt 23220b297d6SJan Schmidt /* 23320b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 23420b297d6SJan Schmidt * the log must never go across transaction boundaries. 23520b297d6SJan Schmidt */ 23620b297d6SJan Schmidt smp_mb(); 23731b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 238efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 23920b297d6SJan Schmidt "creating a fresh transaction\n"); 24031b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 241efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 24220b297d6SJan Schmidt "creating a fresh transaction\n"); 243fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 24420b297d6SJan Schmidt 24556bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 24656bec294SChris Mason 2473063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2486df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2499e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 25019ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 251d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 25219ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 25319ae4e81SJan Schmidt fs_info->generation++; 25419ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 25519ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 25649b25e05SJeff Mahoney cur_trans->aborted = 0; 25719ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 25815ee9bc7SJosef Bacik 25979154b1bSChris Mason return 0; 26079154b1bSChris Mason } 26179154b1bSChris Mason 262d352ac68SChris Mason /* 263d397712bSChris Mason * this does all the record keeping required to make sure that a reference 264d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 265d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 266d397712bSChris Mason * when the transaction commits 267d352ac68SChris Mason */ 2687585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2695d4f98a2SYan Zheng struct btrfs_root *root) 2706702ed49SChris Mason { 27127cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 27227cdeb70SMiao Xie root->last_trans < trans->transid) { 2736702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2745d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2755d4f98a2SYan Zheng 2767585717fSChris Mason /* 27727cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2787585717fSChris Mason * we have the reloc mutex held now, so there 2797585717fSChris Mason * is only one writer in this function 2807585717fSChris Mason */ 28127cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2827585717fSChris Mason 28327cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2847585717fSChris Mason * they find our root->last_trans update 2857585717fSChris Mason */ 2867585717fSChris Mason smp_wmb(); 2877585717fSChris Mason 288a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 289a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 290a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 291a4abeea4SJosef Bacik return 0; 292a4abeea4SJosef Bacik } 2936702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 2946702ed49SChris Mason (unsigned long)root->root_key.objectid, 2956702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 296a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 2977585717fSChris Mason root->last_trans = trans->transid; 2987585717fSChris Mason 2997585717fSChris Mason /* this is pretty tricky. We don't want to 3007585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3017585717fSChris Mason * unless we're really doing the first setup for this root in 3027585717fSChris Mason * this transaction. 3037585717fSChris Mason * 3047585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3057585717fSChris Mason * if we want to take the expensive mutex. 3067585717fSChris Mason * 3077585717fSChris Mason * But, we have to set root->last_trans before we 3087585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3097585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 31027cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3117585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3127585717fSChris Mason * 3137585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3147585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3157585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3167585717fSChris Mason * done before we pop in the zero below 3177585717fSChris Mason */ 3185d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 319c7548af6SChris Mason smp_mb__before_atomic(); 32027cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3216702ed49SChris Mason } 3225d4f98a2SYan Zheng return 0; 3236702ed49SChris Mason } 3245d4f98a2SYan Zheng 3257585717fSChris Mason 3267585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3277585717fSChris Mason struct btrfs_root *root) 3287585717fSChris Mason { 32927cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3307585717fSChris Mason return 0; 3317585717fSChris Mason 3327585717fSChris Mason /* 33327cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3347585717fSChris Mason * and barriers 3357585717fSChris Mason */ 3367585717fSChris Mason smp_rmb(); 3377585717fSChris Mason if (root->last_trans == trans->transid && 33827cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3397585717fSChris Mason return 0; 3407585717fSChris Mason 3417585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3427585717fSChris Mason record_root_in_trans(trans, root); 3437585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3447585717fSChris Mason 3457585717fSChris Mason return 0; 3467585717fSChris Mason } 3477585717fSChris Mason 3484a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3494a9d8bdeSMiao Xie { 3504a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 351501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 352501407aaSJosef Bacik !trans->aborted); 3534a9d8bdeSMiao Xie } 3544a9d8bdeSMiao Xie 355d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 356d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 357d352ac68SChris Mason * transaction might not be fully on disk. 358d352ac68SChris Mason */ 35937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 36079154b1bSChris Mason { 361f9295749SChris Mason struct btrfs_transaction *cur_trans; 36279154b1bSChris Mason 363a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 364f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3654a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 36613c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 367a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 36872d63ed6SLi Zefan 36972d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 370501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 371501407aaSJosef Bacik cur_trans->aborted); 372724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 373a4abeea4SJosef Bacik } else { 374a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 375f9295749SChris Mason } 37637d1aeeeSChris Mason } 37737d1aeeeSChris Mason 378a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 37937d1aeeeSChris Mason { 380a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 381a4abeea4SJosef Bacik return 0; 382a4abeea4SJosef Bacik 383a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 384a22285a6SYan, Zheng return 1; 385a4abeea4SJosef Bacik 386a4abeea4SJosef Bacik if (type == TRANS_START && 387a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 388a4abeea4SJosef Bacik return 1; 389a4abeea4SJosef Bacik 390a22285a6SYan, Zheng return 0; 391a22285a6SYan, Zheng } 392a22285a6SYan, Zheng 39320dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 39420dd2cbfSMiao Xie { 39520dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 39627cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 39720dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 39820dd2cbfSMiao Xie root->reloc_root) 39920dd2cbfSMiao Xie return false; 40020dd2cbfSMiao Xie 40120dd2cbfSMiao Xie return true; 40220dd2cbfSMiao Xie } 40320dd2cbfSMiao Xie 40408e007d2SMiao Xie static struct btrfs_trans_handle * 4050860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 40608e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 407a22285a6SYan, Zheng { 408a22285a6SYan, Zheng struct btrfs_trans_handle *h; 409a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 410b5009945SJosef Bacik u64 num_bytes = 0; 411c5567237SArne Jansen u64 qgroup_reserved = 0; 41220dd2cbfSMiao Xie bool reloc_reserved = false; 41320dd2cbfSMiao Xie int ret; 414acce952bSliubo 41546c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4162755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 41746c4e71eSFilipe Manana 41887533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 419acce952bSliubo return ERR_PTR(-EROFS); 4202a1eb461SJosef Bacik 42146c4e71eSFilipe Manana if (current->journal_info) { 4220860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4232a1eb461SJosef Bacik h = current->journal_info; 4242a1eb461SJosef Bacik h->use_count++; 425b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 4262a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4272a1eb461SJosef Bacik h->block_rsv = NULL; 4282a1eb461SJosef Bacik goto got_it; 4292a1eb461SJosef Bacik } 430b5009945SJosef Bacik 431b5009945SJosef Bacik /* 432b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 433b5009945SJosef Bacik * the appropriate flushing if need be. 434b5009945SJosef Bacik */ 435b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 436c5567237SArne Jansen if (root->fs_info->quota_enabled && 437c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 438707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 439c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 440c5567237SArne Jansen if (ret) 441c5567237SArne Jansen return ERR_PTR(ret); 442c5567237SArne Jansen } 443c5567237SArne Jansen 444b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 44520dd2cbfSMiao Xie /* 44620dd2cbfSMiao Xie * Do the reservation for the relocation root creation 44720dd2cbfSMiao Xie */ 448ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 44920dd2cbfSMiao Xie num_bytes += root->nodesize; 45020dd2cbfSMiao Xie reloc_reserved = true; 45120dd2cbfSMiao Xie } 45220dd2cbfSMiao Xie 4534a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 454b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 45508e007d2SMiao Xie num_bytes, flush); 456b5009945SJosef Bacik if (ret) 457843fcf35SMiao Xie goto reserve_fail; 458b5009945SJosef Bacik } 459a22285a6SYan, Zheng again: 460a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 461843fcf35SMiao Xie if (!h) { 462843fcf35SMiao Xie ret = -ENOMEM; 463843fcf35SMiao Xie goto alloc_fail; 464843fcf35SMiao Xie } 465a22285a6SYan, Zheng 46698114659SJosef Bacik /* 46798114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 46898114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 46998114659SJosef Bacik * because we're already holding a ref. We need this because we could 47098114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 47198114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 472354aa0fbSMiao Xie * 473354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 474354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 47598114659SJosef Bacik */ 4760860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 477b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 478b2b5ef5cSJan Kara 479a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 48037d1aeeeSChris Mason wait_current_trans(root); 481a22285a6SYan, Zheng 482a4abeea4SJosef Bacik do { 483354aa0fbSMiao Xie ret = join_transaction(root, type); 484178260b2SMiao Xie if (ret == -EBUSY) { 485a4abeea4SJosef Bacik wait_current_trans(root); 486178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 487178260b2SMiao Xie ret = -ENOENT; 488178260b2SMiao Xie } 489a4abeea4SJosef Bacik } while (ret == -EBUSY); 490a4abeea4SJosef Bacik 491db5b493aSTsutomu Itoh if (ret < 0) { 492354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 493354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 494843fcf35SMiao Xie goto join_fail; 495db5b493aSTsutomu Itoh } 4960f7d52f4SChris Mason 497a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 498a22285a6SYan, Zheng 499a22285a6SYan, Zheng h->transid = cur_trans->transid; 500a22285a6SYan, Zheng h->transaction = cur_trans; 50179154b1bSChris Mason h->blocks_used = 0; 502a22285a6SYan, Zheng h->bytes_reserved = 0; 503d13603efSArne Jansen h->root = root; 50456bec294SChris Mason h->delayed_ref_updates = 0; 5052a1eb461SJosef Bacik h->use_count = 1; 5060e721106SJosef Bacik h->adding_csums = 0; 507f0486c68SYan, Zheng h->block_rsv = NULL; 5082a1eb461SJosef Bacik h->orig_rsv = NULL; 50949b25e05SJeff Mahoney h->aborted = 0; 5104b824906SMiao Xie h->qgroup_reserved = 0; 511bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 512a698d075SMiao Xie h->type = type; 513c6b305a8SJosef Bacik h->allocating_chunk = false; 51420dd2cbfSMiao Xie h->reloc_reserved = false; 5155039eddcSJosef Bacik h->sync = false; 516bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 517ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 518b7ec40d7SChris Mason 519a22285a6SYan, Zheng smp_mb(); 5204a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5214a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 522abdd2e80SFilipe Manana current->journal_info = h; 523a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 524a22285a6SYan, Zheng goto again; 525a22285a6SYan, Zheng } 5269ed74f2dSJosef Bacik 527b5009945SJosef Bacik if (num_bytes) { 5288c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5292bcc0328SLiu Bo h->transid, num_bytes, 1); 530b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 531b5009945SJosef Bacik h->bytes_reserved = num_bytes; 53220dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 533a22285a6SYan, Zheng } 5344b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 535a22285a6SYan, Zheng 5362a1eb461SJosef Bacik got_it: 537a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 538a22285a6SYan, Zheng 539a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 540a22285a6SYan, Zheng current->journal_info = h; 54179154b1bSChris Mason return h; 542843fcf35SMiao Xie 543843fcf35SMiao Xie join_fail: 5440860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 545843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 546843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 547843fcf35SMiao Xie alloc_fail: 548843fcf35SMiao Xie if (num_bytes) 549843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 550843fcf35SMiao Xie num_bytes); 551843fcf35SMiao Xie reserve_fail: 552843fcf35SMiao Xie if (qgroup_reserved) 553843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 554843fcf35SMiao Xie return ERR_PTR(ret); 55579154b1bSChris Mason } 55679154b1bSChris Mason 557f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 558a22285a6SYan, Zheng int num_items) 559f9295749SChris Mason { 56008e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 56108e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 562f9295749SChris Mason } 5638407aa46SMiao Xie 56408e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 5658407aa46SMiao Xie struct btrfs_root *root, int num_items) 5668407aa46SMiao Xie { 56708e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 56808e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 5698407aa46SMiao Xie } 5708407aa46SMiao Xie 5717a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 572f9295749SChris Mason { 5738407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 574f9295749SChris Mason } 575f9295749SChris Mason 5767a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5770af3d00bSJosef Bacik { 5788407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5790af3d00bSJosef Bacik } 5800af3d00bSJosef Bacik 5817a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5829ca9ee09SSage Weil { 5838407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5849ca9ee09SSage Weil } 5859ca9ee09SSage Weil 586d4edf39bSMiao Xie /* 587d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 588d4edf39bSMiao Xie * 589d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 590d4edf39bSMiao Xie * don't want to start a new one. 591d4edf39bSMiao Xie * 592d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 593d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 594d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 595d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 596d4edf39bSMiao Xie * invoke 597d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 598d4edf39bSMiao Xie */ 599354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 60060376ce4SJosef Bacik { 601354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 60260376ce4SJosef Bacik } 60360376ce4SJosef Bacik 604d4edf39bSMiao Xie /* 60590b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 606d4edf39bSMiao Xie * 607d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 608d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 609d4edf39bSMiao Xie * complete. 610d4edf39bSMiao Xie */ 611d4edf39bSMiao Xie struct btrfs_trans_handle * 612d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 613d4edf39bSMiao Xie { 614d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 615d4edf39bSMiao Xie 616d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 617d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 618d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 619d4edf39bSMiao Xie 620d4edf39bSMiao Xie return trans; 621d4edf39bSMiao Xie } 622d4edf39bSMiao Xie 623d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 624b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 62589ce8a63SChris Mason struct btrfs_transaction *commit) 62689ce8a63SChris Mason { 6274a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 62889ce8a63SChris Mason } 62989ce8a63SChris Mason 63046204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 63146204592SSage Weil { 63246204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6338cd2807fSMiao Xie int ret = 0; 63446204592SSage Weil 63546204592SSage Weil if (transid) { 63646204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 637a4abeea4SJosef Bacik goto out; 63846204592SSage Weil 63946204592SSage Weil /* find specified transaction */ 640a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 64146204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 64246204592SSage Weil if (t->transid == transid) { 64346204592SSage Weil cur_trans = t; 644a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 6458cd2807fSMiao Xie ret = 0; 64646204592SSage Weil break; 64746204592SSage Weil } 6488cd2807fSMiao Xie if (t->transid > transid) { 6498cd2807fSMiao Xie ret = 0; 65046204592SSage Weil break; 65146204592SSage Weil } 6528cd2807fSMiao Xie } 653a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 65442383020SSage Weil 65542383020SSage Weil /* 65642383020SSage Weil * The specified transaction doesn't exist, or we 65742383020SSage Weil * raced with btrfs_commit_transaction 65842383020SSage Weil */ 65942383020SSage Weil if (!cur_trans) { 66042383020SSage Weil if (transid > root->fs_info->last_trans_committed) 66142383020SSage Weil ret = -EINVAL; 6628cd2807fSMiao Xie goto out; 66342383020SSage Weil } 66446204592SSage Weil } else { 66546204592SSage Weil /* find newest transaction that is committing | committed */ 666a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 66746204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 66846204592SSage Weil list) { 6694a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 6704a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 6713473f3c0SJosef Bacik break; 67246204592SSage Weil cur_trans = t; 673a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 67446204592SSage Weil break; 67546204592SSage Weil } 67646204592SSage Weil } 677a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 67846204592SSage Weil if (!cur_trans) 679a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 68046204592SSage Weil } 68146204592SSage Weil 68246204592SSage Weil wait_for_commit(root, cur_trans); 683724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 684a4abeea4SJosef Bacik out: 68546204592SSage Weil return ret; 68646204592SSage Weil } 68746204592SSage Weil 68837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 68937d1aeeeSChris Mason { 690a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 69137d1aeeeSChris Mason wait_current_trans(root); 69237d1aeeeSChris Mason } 69337d1aeeeSChris Mason 6948929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 6958929ecfaSYan, Zheng struct btrfs_root *root) 6968929ecfaSYan, Zheng { 6971be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 6980a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 6991be41b78SJosef Bacik return 1; 70036ba022aSJosef Bacik 7011be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7028929ecfaSYan, Zheng } 7038929ecfaSYan, Zheng 7048929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7058929ecfaSYan, Zheng struct btrfs_root *root) 7068929ecfaSYan, Zheng { 7078929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7088929ecfaSYan, Zheng int updates; 70949b25e05SJeff Mahoney int err; 7108929ecfaSYan, Zheng 711a4abeea4SJosef Bacik smp_mb(); 7124a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7134a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7148929ecfaSYan, Zheng return 1; 7158929ecfaSYan, Zheng 7168929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 7178929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 71849b25e05SJeff Mahoney if (updates) { 71949b25e05SJeff Mahoney err = btrfs_run_delayed_refs(trans, root, updates); 72049b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 72149b25e05SJeff Mahoney return err; 72249b25e05SJeff Mahoney } 7238929ecfaSYan, Zheng 7248929ecfaSYan, Zheng return should_end_transaction(trans, root); 7258929ecfaSYan, Zheng } 7268929ecfaSYan, Zheng 72789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 728a698d075SMiao Xie struct btrfs_root *root, int throttle) 72979154b1bSChris Mason { 7308929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 731ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 7321be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 733a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 7344edc2ca3SDave Jones int err = 0; 735a79b7d4bSChris Mason int must_run_delayed_refs = 0; 736d6e4a428SChris Mason 7373bbb24b2SJosef Bacik if (trans->use_count > 1) { 7383bbb24b2SJosef Bacik trans->use_count--; 7392a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 7402a1eb461SJosef Bacik return 0; 7412a1eb461SJosef Bacik } 7422a1eb461SJosef Bacik 743b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 7444c13d758SJosef Bacik trans->block_rsv = NULL; 745c5567237SArne Jansen 746ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 747ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 748ea658badSJosef Bacik 749c3e69d58SChris Mason trans->delayed_ref_updates = 0; 750a79b7d4bSChris Mason if (!trans->sync) { 751a79b7d4bSChris Mason must_run_delayed_refs = 752a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 7530a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 754a79b7d4bSChris Mason 755a79b7d4bSChris Mason /* 756a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 757a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 758a79b7d4bSChris Mason */ 759a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 760a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 761a79b7d4bSChris Mason must_run_delayed_refs = 2; 76256bec294SChris Mason } 763bb721703SChris Mason 764fcebe456SJosef Bacik if (trans->qgroup_reserved) { 765fcebe456SJosef Bacik /* 766fcebe456SJosef Bacik * the same root has to be passed here between start_transaction 767fcebe456SJosef Bacik * and end_transaction. Subvolume quota depends on this. 768fcebe456SJosef Bacik */ 769fcebe456SJosef Bacik btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 770fcebe456SJosef Bacik trans->qgroup_reserved = 0; 771fcebe456SJosef Bacik } 772fcebe456SJosef Bacik 7730e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 7740e721106SJosef Bacik trans->block_rsv = NULL; 77556bec294SChris Mason 776ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 777ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 778ea658badSJosef Bacik 779a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 7804a9d8bdeSMiao Xie should_end_transaction(trans, root) && 7814a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 7824a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 7834a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 7844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 7854a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 786a4abeea4SJosef Bacik } 7878929ecfaSYan, Zheng 7884a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 7893bbb24b2SJosef Bacik if (throttle) 7908929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 7913bbb24b2SJosef Bacik else 7928929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 7938929ecfaSYan, Zheng } 7948929ecfaSYan, Zheng 7950860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 7966df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 7976df7881aSJosef Bacik 7988929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 79913c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 80013c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8010860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 80289ce8a63SChris Mason 80399d16cbcSSage Weil smp_mb(); 80479154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 80579154b1bSChris Mason wake_up(&cur_trans->writer_wait); 806724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8079ed74f2dSJosef Bacik 8089ed74f2dSJosef Bacik if (current->journal_info == trans) 8099ed74f2dSJosef Bacik current->journal_info = NULL; 810ab78c84dSChris Mason 81124bbcf04SYan, Zheng if (throttle) 81224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 81324bbcf04SYan, Zheng 81449b25e05SJeff Mahoney if (trans->aborted || 8154e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 8164e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8174edc2ca3SDave Jones err = -EIO; 8184e121c06SJosef Bacik } 819edf39272SJan Schmidt assert_qgroups_uptodate(trans); 82049b25e05SJeff Mahoney 8214edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 822a79b7d4bSChris Mason if (must_run_delayed_refs) { 823a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 824a79b7d4bSChris Mason must_run_delayed_refs == 1); 825a79b7d4bSChris Mason } 8264edc2ca3SDave Jones return err; 82779154b1bSChris Mason } 82879154b1bSChris Mason 82989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 83089ce8a63SChris Mason struct btrfs_root *root) 83189ce8a63SChris Mason { 83298ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 83389ce8a63SChris Mason } 83489ce8a63SChris Mason 83589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 83689ce8a63SChris Mason struct btrfs_root *root) 83789ce8a63SChris Mason { 83898ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 83916cdcec7SMiao Xie } 84016cdcec7SMiao Xie 841d352ac68SChris Mason /* 842d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 843d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 844690587d1SChris Mason * those extents are sent to disk but does not wait on them 845d352ac68SChris Mason */ 846690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 8478cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 84879154b1bSChris Mason { 849777e6bd7SChris Mason int err = 0; 8507c4452b9SChris Mason int werr = 0; 8511728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 852e6138876SJosef Bacik struct extent_state *cached_state = NULL; 853777e6bd7SChris Mason u64 start = 0; 8545f39d397SChris Mason u64 end; 8557c4452b9SChris Mason 8561728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 857e6138876SJosef Bacik mark, &cached_state)) { 858663dfbb0SFilipe Manana bool wait_writeback = false; 859663dfbb0SFilipe Manana 860663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 861663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 862e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 863663dfbb0SFilipe Manana /* 864663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 865663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 866663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 867663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 868663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 869663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 870663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 871663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 872663dfbb0SFilipe Manana * (and without errors). 873663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 874663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 875663dfbb0SFilipe Manana */ 876663dfbb0SFilipe Manana if (err == -ENOMEM) { 877663dfbb0SFilipe Manana err = 0; 878663dfbb0SFilipe Manana wait_writeback = true; 879663dfbb0SFilipe Manana } 880663dfbb0SFilipe Manana if (!err) 8811728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 8827c4452b9SChris Mason if (err) 8837c4452b9SChris Mason werr = err; 884663dfbb0SFilipe Manana else if (wait_writeback) 885663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 886*e38e2ed7SFilipe Manana free_extent_state(cached_state); 887663dfbb0SFilipe Manana cached_state = NULL; 8881728366eSJosef Bacik cond_resched(); 8891728366eSJosef Bacik start = end + 1; 8907c4452b9SChris Mason } 891690587d1SChris Mason return werr; 892690587d1SChris Mason } 893690587d1SChris Mason 894690587d1SChris Mason /* 895690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 896690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 897690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 898690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 899690587d1SChris Mason */ 900690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9018cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 902690587d1SChris Mason { 903690587d1SChris Mason int err = 0; 904690587d1SChris Mason int werr = 0; 9051728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 906e6138876SJosef Bacik struct extent_state *cached_state = NULL; 907690587d1SChris Mason u64 start = 0; 908690587d1SChris Mason u64 end; 909656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 910656f30dbSFilipe Manana bool errors = false; 911690587d1SChris Mason 9121728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 913e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 914663dfbb0SFilipe Manana /* 915663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 916663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 917663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 918663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 919663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 920663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 921663dfbb0SFilipe Manana */ 922663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 923663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 924e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 925663dfbb0SFilipe Manana if (err == -ENOMEM) 926663dfbb0SFilipe Manana err = 0; 927663dfbb0SFilipe Manana if (!err) 9281728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 929777e6bd7SChris Mason if (err) 930777e6bd7SChris Mason werr = err; 931*e38e2ed7SFilipe Manana free_extent_state(cached_state); 932*e38e2ed7SFilipe Manana cached_state = NULL; 933777e6bd7SChris Mason cond_resched(); 9341728366eSJosef Bacik start = end + 1; 935777e6bd7SChris Mason } 9367c4452b9SChris Mason if (err) 9377c4452b9SChris Mason werr = err; 938656f30dbSFilipe Manana 939656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 940656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 941656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 942656f30dbSFilipe Manana &btree_ino->runtime_flags)) 943656f30dbSFilipe Manana errors = true; 944656f30dbSFilipe Manana 945656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 946656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 947656f30dbSFilipe Manana &btree_ino->runtime_flags)) 948656f30dbSFilipe Manana errors = true; 949656f30dbSFilipe Manana } else { 950656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 951656f30dbSFilipe Manana &btree_ino->runtime_flags)) 952656f30dbSFilipe Manana errors = true; 953656f30dbSFilipe Manana } 954656f30dbSFilipe Manana 955656f30dbSFilipe Manana if (errors && !werr) 956656f30dbSFilipe Manana werr = -EIO; 957656f30dbSFilipe Manana 9587c4452b9SChris Mason return werr; 95979154b1bSChris Mason } 96079154b1bSChris Mason 961690587d1SChris Mason /* 962690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 963690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 964690587d1SChris Mason * those extents are on disk for transaction or log commit 965690587d1SChris Mason */ 966171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 9678cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 968690587d1SChris Mason { 969690587d1SChris Mason int ret; 970690587d1SChris Mason int ret2; 971c6adc9ccSMiao Xie struct blk_plug plug; 972690587d1SChris Mason 973c6adc9ccSMiao Xie blk_start_plug(&plug); 9748cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 975c6adc9ccSMiao Xie blk_finish_plug(&plug); 9768cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 977bf0da8c1SChris Mason 978bf0da8c1SChris Mason if (ret) 979bf0da8c1SChris Mason return ret; 980bf0da8c1SChris Mason if (ret2) 981bf0da8c1SChris Mason return ret2; 982bf0da8c1SChris Mason return 0; 983690587d1SChris Mason } 984690587d1SChris Mason 985663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 986d0c803c4SChris Mason struct btrfs_root *root) 987d0c803c4SChris Mason { 988663dfbb0SFilipe Manana int ret; 989663dfbb0SFilipe Manana 990663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 9918cef4e16SYan, Zheng &trans->transaction->dirty_pages, 9928cef4e16SYan, Zheng EXTENT_DIRTY); 993663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 994663dfbb0SFilipe Manana 995663dfbb0SFilipe Manana return ret; 996d0c803c4SChris Mason } 997d0c803c4SChris Mason 998d352ac68SChris Mason /* 999d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1000d352ac68SChris Mason * 1001d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1002d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1003d352ac68SChris Mason * allocation tree. 1004d352ac68SChris Mason * 1005d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1006d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1007d352ac68SChris Mason */ 10080b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 100979154b1bSChris Mason struct btrfs_root *root) 101079154b1bSChris Mason { 101179154b1bSChris Mason int ret; 10120b86a832SChris Mason u64 old_root_bytenr; 101386b9f2ecSYan, Zheng u64 old_root_used; 10140b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 101579154b1bSChris Mason 101686b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 10170b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 101856bec294SChris Mason 101979154b1bSChris Mason while (1) { 10200b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 102186b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 102286b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 102379154b1bSChris Mason break; 102487ef2bb4SChris Mason 10255d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 102679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10270b86a832SChris Mason &root->root_key, 10280b86a832SChris Mason &root->root_item); 102949b25e05SJeff Mahoney if (ret) 103049b25e05SJeff Mahoney return ret; 103156bec294SChris Mason 103286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 10334a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 103449b25e05SJeff Mahoney if (ret) 103549b25e05SJeff Mahoney return ret; 10360b86a832SChris Mason } 1037276e680dSYan Zheng 10380b86a832SChris Mason return 0; 10390b86a832SChris Mason } 10400b86a832SChris Mason 1041d352ac68SChris Mason /* 1042d352ac68SChris Mason * update all the cowonly tree roots on disk 104349b25e05SJeff Mahoney * 104449b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 104549b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 104649b25e05SJeff Mahoney * to clean up the delayed refs. 1047d352ac68SChris Mason */ 10485d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 10490b86a832SChris Mason struct btrfs_root *root) 10500b86a832SChris Mason { 10510b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 10520b86a832SChris Mason struct list_head *next; 105384234f3aSYan Zheng struct extent_buffer *eb; 105456bec294SChris Mason int ret; 105584234f3aSYan Zheng 105656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 105749b25e05SJeff Mahoney if (ret) 105849b25e05SJeff Mahoney return ret; 105987ef2bb4SChris Mason 106084234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 106149b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 106249b25e05SJeff Mahoney 0, &eb); 106384234f3aSYan Zheng btrfs_tree_unlock(eb); 106484234f3aSYan Zheng free_extent_buffer(eb); 10650b86a832SChris Mason 106649b25e05SJeff Mahoney if (ret) 106749b25e05SJeff Mahoney return ret; 106849b25e05SJeff Mahoney 106956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 107049b25e05SJeff Mahoney if (ret) 107149b25e05SJeff Mahoney return ret; 107287ef2bb4SChris Mason 1073733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1074c16ce190SJosef Bacik if (ret) 1075c16ce190SJosef Bacik return ret; 10768dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1077c16ce190SJosef Bacik if (ret) 1078c16ce190SJosef Bacik return ret; 1079546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1080c16ce190SJosef Bacik if (ret) 1081c16ce190SJosef Bacik return ret; 1082546adb0dSJan Schmidt 1083546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1084546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1085c16ce190SJosef Bacik if (ret) 1086c16ce190SJosef Bacik return ret; 1087546adb0dSJan Schmidt 10880b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 10890b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 10900b86a832SChris Mason list_del_init(next); 10910b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 109287ef2bb4SChris Mason 10939e351cc8SJosef Bacik if (root != fs_info->extent_root) 10949e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 10959e351cc8SJosef Bacik &trans->transaction->switch_commits); 109649b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 109749b25e05SJeff Mahoney if (ret) 109849b25e05SJeff Mahoney return ret; 109979154b1bSChris Mason } 1100276e680dSYan Zheng 11019e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11029e351cc8SJosef Bacik &trans->transaction->switch_commits); 11038dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 11048dabb742SStefan Behrens 110579154b1bSChris Mason return 0; 110679154b1bSChris Mason } 110779154b1bSChris Mason 1108d352ac68SChris Mason /* 1109d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1110d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1111d352ac68SChris Mason * be deleted 1112d352ac68SChris Mason */ 1113cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11145eda7b5eSChris Mason { 1115a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1116cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11179d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1118a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 11195eda7b5eSChris Mason } 11205eda7b5eSChris Mason 1121d352ac68SChris Mason /* 11225d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1123d352ac68SChris Mason */ 11245d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 11255d4f98a2SYan Zheng struct btrfs_root *root) 11260f7d52f4SChris Mason { 11270f7d52f4SChris Mason struct btrfs_root *gang[8]; 11285d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 11290f7d52f4SChris Mason int i; 11300f7d52f4SChris Mason int ret; 113154aa1f4dSChris Mason int err = 0; 113254aa1f4dSChris Mason 1133a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 11340f7d52f4SChris Mason while (1) { 11355d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 11365d4f98a2SYan Zheng (void **)gang, 0, 11370f7d52f4SChris Mason ARRAY_SIZE(gang), 11380f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 11390f7d52f4SChris Mason if (ret == 0) 11400f7d52f4SChris Mason break; 11410f7d52f4SChris Mason for (i = 0; i < ret; i++) { 11420f7d52f4SChris Mason root = gang[i]; 11435d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 11442619ba1fSChris Mason (unsigned long)root->root_key.objectid, 11450f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1146a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 114731153d81SYan Zheng 1148e02119d5SChris Mason btrfs_free_log(trans, root); 11495d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1150d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1151e02119d5SChris Mason 115282d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 115382d5902dSLi Zefan 1154f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 115527cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1156c7548af6SChris Mason smp_mb__after_atomic(); 1157f1ebcc74SLiu Bo 1158978d910dSYan Zheng if (root->commit_root != root->node) { 11599e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11609e351cc8SJosef Bacik &trans->transaction->switch_commits); 1161978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1162978d910dSYan Zheng root->node); 1163978d910dSYan Zheng } 116431153d81SYan Zheng 11655d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 11660f7d52f4SChris Mason &root->root_key, 11670f7d52f4SChris Mason &root->root_item); 1168a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 116954aa1f4dSChris Mason if (err) 117054aa1f4dSChris Mason break; 11710f7d52f4SChris Mason } 11729f3a7427SChris Mason } 1173a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 117454aa1f4dSChris Mason return err; 11750f7d52f4SChris Mason } 11760f7d52f4SChris Mason 1177d352ac68SChris Mason /* 1178de78b51aSEric Sandeen * defrag a given btree. 1179de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1180d352ac68SChris Mason */ 1181de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1182e9d0b13bSChris Mason { 1183e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1184e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 11858929ecfaSYan, Zheng int ret; 1186e9d0b13bSChris Mason 118727cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1188e9d0b13bSChris Mason return 0; 11898929ecfaSYan, Zheng 11906b80053dSChris Mason while (1) { 11918929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 11928929ecfaSYan, Zheng if (IS_ERR(trans)) 11938929ecfaSYan, Zheng return PTR_ERR(trans); 11948929ecfaSYan, Zheng 1195de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 11968929ecfaSYan, Zheng 1197e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1198b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1199e9d0b13bSChris Mason cond_resched(); 1200e9d0b13bSChris Mason 12017841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1202e9d0b13bSChris Mason break; 1203210549ebSDavid Sterba 1204210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1205efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1206210549ebSDavid Sterba ret = -EAGAIN; 1207210549ebSDavid Sterba break; 1208210549ebSDavid Sterba } 1209e9d0b13bSChris Mason } 121027cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12118929ecfaSYan, Zheng return ret; 1212e9d0b13bSChris Mason } 1213e9d0b13bSChris Mason 1214d352ac68SChris Mason /* 1215d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1216aec8030aSMiao Xie * transaction commit. This does the actual creation. 1217aec8030aSMiao Xie * 1218aec8030aSMiao Xie * Note: 1219aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1220aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1221aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1222d352ac68SChris Mason */ 122380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 12243063d29fSChris Mason struct btrfs_fs_info *fs_info, 12253063d29fSChris Mason struct btrfs_pending_snapshot *pending) 12263063d29fSChris Mason { 12273063d29fSChris Mason struct btrfs_key key; 122880b6794dSChris Mason struct btrfs_root_item *new_root_item; 12293063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 12303063d29fSChris Mason struct btrfs_root *root = pending->root; 12316bdb72deSSage Weil struct btrfs_root *parent_root; 123298c9942aSLiu Bo struct btrfs_block_rsv *rsv; 12336bdb72deSSage Weil struct inode *parent_inode; 123442874b3dSMiao Xie struct btrfs_path *path; 123542874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1236a22285a6SYan, Zheng struct dentry *dentry; 12373063d29fSChris Mason struct extent_buffer *tmp; 1238925baeddSChris Mason struct extent_buffer *old; 12398ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1240aec8030aSMiao Xie int ret = 0; 1241d68fc57bSYan, Zheng u64 to_reserve = 0; 12426bdb72deSSage Weil u64 index = 0; 1243a22285a6SYan, Zheng u64 objectid; 1244b83cc969SLi Zefan u64 root_flags; 12458ea05e3aSAlexander Block uuid_le new_uuid; 12463063d29fSChris Mason 124742874b3dSMiao Xie path = btrfs_alloc_path(); 124842874b3dSMiao Xie if (!path) { 1249aec8030aSMiao Xie pending->error = -ENOMEM; 1250aec8030aSMiao Xie return 0; 125142874b3dSMiao Xie } 125242874b3dSMiao Xie 125380b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 125480b6794dSChris Mason if (!new_root_item) { 1255aec8030aSMiao Xie pending->error = -ENOMEM; 12566fa9700eSMiao Xie goto root_item_alloc_fail; 125780b6794dSChris Mason } 1258a22285a6SYan, Zheng 1259aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1260aec8030aSMiao Xie if (pending->error) 12616fa9700eSMiao Xie goto no_free_objectid; 12623063d29fSChris Mason 12633fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1264d68fc57bSYan, Zheng 1265d68fc57bSYan, Zheng if (to_reserve > 0) { 1266aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1267aec8030aSMiao Xie &pending->block_rsv, 126808e007d2SMiao Xie to_reserve, 126908e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1270aec8030aSMiao Xie if (pending->error) 12716fa9700eSMiao Xie goto no_free_objectid; 1272d68fc57bSYan, Zheng } 1273d68fc57bSYan, Zheng 12743063d29fSChris Mason key.objectid = objectid; 1275a22285a6SYan, Zheng key.offset = (u64)-1; 1276a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 12773063d29fSChris Mason 12786fa9700eSMiao Xie rsv = trans->block_rsv; 1279a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 12802382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 12816bdb72deSSage Weil 1282a22285a6SYan, Zheng dentry = pending->dentry; 1283e9662f70SMiao Xie parent_inode = pending->dir; 1284a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 12857585717fSChris Mason record_root_in_trans(trans, parent_root); 1286a22285a6SYan, Zheng 12876bdb72deSSage Weil /* 12886bdb72deSSage Weil * insert the directory item 12896bdb72deSSage Weil */ 12906bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 129149b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 129242874b3dSMiao Xie 129342874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 129442874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 129542874b3dSMiao Xie btrfs_ino(parent_inode), 129642874b3dSMiao Xie dentry->d_name.name, 129742874b3dSMiao Xie dentry->d_name.len, 0); 129842874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1299fe66a05aSChris Mason pending->error = -EEXIST; 1300aec8030aSMiao Xie goto dir_item_existed; 130142874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 130242874b3dSMiao Xie ret = PTR_ERR(dir_item); 13038732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13048732d44fSMiao Xie goto fail; 130579787eaaSJeff Mahoney } 130642874b3dSMiao Xie btrfs_release_path(path); 13076bdb72deSSage Weil 1308e999376fSChris Mason /* 1309e999376fSChris Mason * pull in the delayed directory update 1310e999376fSChris Mason * and the delayed inode item 1311e999376fSChris Mason * otherwise we corrupt the FS during 1312e999376fSChris Mason * snapshot 1313e999376fSChris Mason */ 1314e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 13158732d44fSMiao Xie if (ret) { /* Transaction aborted */ 13168732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13178732d44fSMiao Xie goto fail; 13188732d44fSMiao Xie } 1319e999376fSChris Mason 13207585717fSChris Mason record_root_in_trans(trans, root); 13216bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 13226bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 132308fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 13246bdb72deSSage Weil 1325b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1326b83cc969SLi Zefan if (pending->readonly) 1327b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1328b83cc969SLi Zefan else 1329b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1330b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1331b83cc969SLi Zefan 13328ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 13338ea05e3aSAlexander Block trans->transid); 13348ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 13358ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 13368ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 13378ea05e3aSAlexander Block BTRFS_UUID_SIZE); 133870023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 133970023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 134070023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 13418ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 13428ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 13438ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 13448ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 134570023da2SStefan Behrens } 13463cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 13473cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 134870023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 13498ea05e3aSAlexander Block 1350925baeddSChris Mason old = btrfs_lock_root_node(root); 135149b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 135279787eaaSJeff Mahoney if (ret) { 135379787eaaSJeff Mahoney btrfs_tree_unlock(old); 135479787eaaSJeff Mahoney free_extent_buffer(old); 13558732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13568732d44fSMiao Xie goto fail; 135779787eaaSJeff Mahoney } 135849b25e05SJeff Mahoney 13595d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 13603063d29fSChris Mason 136149b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 136279787eaaSJeff Mahoney /* clean up in any case */ 1363925baeddSChris Mason btrfs_tree_unlock(old); 1364925baeddSChris Mason free_extent_buffer(old); 13658732d44fSMiao Xie if (ret) { 13668732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13678732d44fSMiao Xie goto fail; 13688732d44fSMiao Xie } 13693063d29fSChris Mason 1370fcebe456SJosef Bacik /* 1371fcebe456SJosef Bacik * We need to flush delayed refs in order to make sure all of our quota 1372fcebe456SJosef Bacik * operations have been done before we call btrfs_qgroup_inherit. 1373fcebe456SJosef Bacik */ 1374fcebe456SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1375fcebe456SJosef Bacik if (ret) { 1376fcebe456SJosef Bacik btrfs_abort_transaction(trans, root, ret); 1377fcebe456SJosef Bacik goto fail; 1378fcebe456SJosef Bacik } 1379fcebe456SJosef Bacik 138047a306a7SEric Sandeen ret = btrfs_qgroup_inherit(trans, fs_info, 1381fcebe456SJosef Bacik root->root_key.objectid, 1382fcebe456SJosef Bacik objectid, pending->inherit); 138347a306a7SEric Sandeen if (ret) { 138447a306a7SEric Sandeen btrfs_abort_transaction(trans, root, ret); 138547a306a7SEric Sandeen goto fail; 138647a306a7SEric Sandeen } 1387fcebe456SJosef Bacik 1388f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 138927cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1390f1ebcc74SLiu Bo smp_wmb(); 1391f1ebcc74SLiu Bo 13925d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1393a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1394a22285a6SYan, Zheng key.offset = trans->transid; 1395a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1396925baeddSChris Mason btrfs_tree_unlock(tmp); 13973063d29fSChris Mason free_extent_buffer(tmp); 13988732d44fSMiao Xie if (ret) { 13998732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14008732d44fSMiao Xie goto fail; 14018732d44fSMiao Xie } 14020660b5afSChris Mason 1403a22285a6SYan, Zheng /* 1404a22285a6SYan, Zheng * insert root back/forward references 1405a22285a6SYan, Zheng */ 1406a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1407a22285a6SYan, Zheng parent_root->root_key.objectid, 140833345d01SLi Zefan btrfs_ino(parent_inode), index, 1409a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14108732d44fSMiao Xie if (ret) { 14118732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14128732d44fSMiao Xie goto fail; 14138732d44fSMiao Xie } 1414a22285a6SYan, Zheng 1415a22285a6SYan, Zheng key.offset = (u64)-1; 1416a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 141779787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 141879787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 14198732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14208732d44fSMiao Xie goto fail; 142179787eaaSJeff Mahoney } 1422d68fc57bSYan, Zheng 142349b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 14248732d44fSMiao Xie if (ret) { 14258732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14268732d44fSMiao Xie goto fail; 14278732d44fSMiao Xie } 1428361048f5SMiao Xie 1429361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 14308732d44fSMiao Xie if (ret) { 14318732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14328732d44fSMiao Xie goto fail; 14338732d44fSMiao Xie } 143442874b3dSMiao Xie 143542874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 143642874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 143742874b3dSMiao Xie parent_inode, &key, 143842874b3dSMiao Xie BTRFS_FT_DIR, index); 143942874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 14409c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 14418732d44fSMiao Xie if (ret) { 14428732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14438732d44fSMiao Xie goto fail; 14448732d44fSMiao Xie } 144542874b3dSMiao Xie 144642874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 144742874b3dSMiao Xie dentry->d_name.len * 2); 144842874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1449be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1450dd5f9615SStefan Behrens if (ret) { 14518732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1452dd5f9615SStefan Behrens goto fail; 1453dd5f9615SStefan Behrens } 1454dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1455dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1456dd5f9615SStefan Behrens if (ret) { 1457dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1458dd5f9615SStefan Behrens goto fail; 1459dd5f9615SStefan Behrens } 1460dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1461dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1462dd5f9615SStefan Behrens new_root_item->received_uuid, 1463dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1464dd5f9615SStefan Behrens objectid); 1465dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1466dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1467dd5f9615SStefan Behrens goto fail; 1468dd5f9615SStefan Behrens } 1469dd5f9615SStefan Behrens } 14703063d29fSChris Mason fail: 1471aec8030aSMiao Xie pending->error = ret; 1472aec8030aSMiao Xie dir_item_existed: 147398c9942aSLiu Bo trans->block_rsv = rsv; 14742382c5ccSLiu Bo trans->bytes_reserved = 0; 14756fa9700eSMiao Xie no_free_objectid: 14766fa9700eSMiao Xie kfree(new_root_item); 14776fa9700eSMiao Xie root_item_alloc_fail: 147842874b3dSMiao Xie btrfs_free_path(path); 147949b25e05SJeff Mahoney return ret; 14803063d29fSChris Mason } 14813063d29fSChris Mason 1482d352ac68SChris Mason /* 1483d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1484d352ac68SChris Mason */ 148580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 14863063d29fSChris Mason struct btrfs_fs_info *fs_info) 14873063d29fSChris Mason { 1488aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 14893063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1490aec8030aSMiao Xie int ret = 0; 14913de4586cSChris Mason 1492aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1493aec8030aSMiao Xie list_del(&pending->list); 1494aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1495aec8030aSMiao Xie if (ret) 1496aec8030aSMiao Xie break; 1497aec8030aSMiao Xie } 1498aec8030aSMiao Xie return ret; 14993de4586cSChris Mason } 15003de4586cSChris Mason 15015d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 15025d4f98a2SYan Zheng { 15035d4f98a2SYan Zheng struct btrfs_root_item *root_item; 15045d4f98a2SYan Zheng struct btrfs_super_block *super; 15055d4f98a2SYan Zheng 15066c41761fSDavid Sterba super = root->fs_info->super_copy; 15075d4f98a2SYan Zheng 15085d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 15095d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 15105d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 15115d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 15125d4f98a2SYan Zheng 15135d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 15145d4f98a2SYan Zheng super->root = root_item->bytenr; 15155d4f98a2SYan Zheng super->generation = root_item->generation; 15165d4f98a2SYan Zheng super->root_level = root_item->level; 151773bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 15180af3d00bSJosef Bacik super->cache_generation = root_item->generation; 151970f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 152026432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 15215d4f98a2SYan Zheng } 15225d4f98a2SYan Zheng 1523f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1524f36f3042SChris Mason { 15254a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1526f36f3042SChris Mason int ret = 0; 15274a9d8bdeSMiao Xie 1528a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 15294a9d8bdeSMiao Xie trans = info->running_transaction; 15304a9d8bdeSMiao Xie if (trans) 15314a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1532a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1533f36f3042SChris Mason return ret; 1534f36f3042SChris Mason } 1535f36f3042SChris Mason 15368929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 15378929ecfaSYan, Zheng { 15384a9d8bdeSMiao Xie struct btrfs_transaction *trans; 15398929ecfaSYan, Zheng int ret = 0; 15404a9d8bdeSMiao Xie 1541a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 15424a9d8bdeSMiao Xie trans = info->running_transaction; 15434a9d8bdeSMiao Xie if (trans) 15444a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1545a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 15468929ecfaSYan, Zheng return ret; 15478929ecfaSYan, Zheng } 15488929ecfaSYan, Zheng 1549bb9c12c9SSage Weil /* 1550bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1551bb9c12c9SSage Weil * transaction joins 1552bb9c12c9SSage Weil */ 1553bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1554bb9c12c9SSage Weil struct btrfs_transaction *trans) 1555bb9c12c9SSage Weil { 15564a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1557501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1558501407aaSJosef Bacik trans->aborted); 1559bb9c12c9SSage Weil } 1560bb9c12c9SSage Weil 1561bb9c12c9SSage Weil /* 1562bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1563bb9c12c9SSage Weil * caller holds ref. 1564bb9c12c9SSage Weil */ 1565bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1566bb9c12c9SSage Weil struct btrfs_transaction *trans) 1567bb9c12c9SSage Weil { 156872d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1569501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1570501407aaSJosef Bacik trans->aborted); 1571bb9c12c9SSage Weil } 1572bb9c12c9SSage Weil 1573bb9c12c9SSage Weil /* 1574bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1575bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1576bb9c12c9SSage Weil */ 1577bb9c12c9SSage Weil struct btrfs_async_commit { 1578bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1579bb9c12c9SSage Weil struct btrfs_root *root; 15807892b5afSMiao Xie struct work_struct work; 1581bb9c12c9SSage Weil }; 1582bb9c12c9SSage Weil 1583bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1584bb9c12c9SSage Weil { 1585bb9c12c9SSage Weil struct btrfs_async_commit *ac = 15867892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1587bb9c12c9SSage Weil 15886fc4e354SSage Weil /* 15896fc4e354SSage Weil * We've got freeze protection passed with the transaction. 15906fc4e354SSage Weil * Tell lockdep about it. 15916fc4e354SSage Weil */ 1592b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 15936fc4e354SSage Weil rwsem_acquire_read( 15946fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 15956fc4e354SSage Weil 0, 1, _THIS_IP_); 15966fc4e354SSage Weil 1597e209db7aSSage Weil current->journal_info = ac->newtrans; 1598e209db7aSSage Weil 1599bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1600bb9c12c9SSage Weil kfree(ac); 1601bb9c12c9SSage Weil } 1602bb9c12c9SSage Weil 1603bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1604bb9c12c9SSage Weil struct btrfs_root *root, 1605bb9c12c9SSage Weil int wait_for_unblock) 1606bb9c12c9SSage Weil { 1607bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1608bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1609bb9c12c9SSage Weil 1610bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1611db5b493aSTsutomu Itoh if (!ac) 1612db5b493aSTsutomu Itoh return -ENOMEM; 1613bb9c12c9SSage Weil 16147892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1615bb9c12c9SSage Weil ac->root = root; 16167a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 16173612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 16183612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 16193612b495STsutomu Itoh kfree(ac); 16203612b495STsutomu Itoh return err; 16213612b495STsutomu Itoh } 1622bb9c12c9SSage Weil 1623bb9c12c9SSage Weil /* take transaction reference */ 1624bb9c12c9SSage Weil cur_trans = trans->transaction; 162513c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1626bb9c12c9SSage Weil 1627bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 16286fc4e354SSage Weil 16296fc4e354SSage Weil /* 16306fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 16316fc4e354SSage Weil * async commit thread will be the one to unlock it. 16326fc4e354SSage Weil */ 1633b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1634ff7c1d33SMiao Xie rwsem_release( 1635ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 16366fc4e354SSage Weil 1, _THIS_IP_); 16376fc4e354SSage Weil 16387892b5afSMiao Xie schedule_work(&ac->work); 1639bb9c12c9SSage Weil 1640bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1641bb9c12c9SSage Weil if (wait_for_unblock) 1642bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1643bb9c12c9SSage Weil else 1644bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1645bb9c12c9SSage Weil 164638e88054SSage Weil if (current->journal_info == trans) 164738e88054SSage Weil current->journal_info = NULL; 164838e88054SSage Weil 1649724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1650bb9c12c9SSage Weil return 0; 1651bb9c12c9SSage Weil } 1652bb9c12c9SSage Weil 165349b25e05SJeff Mahoney 165449b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 16557b8b92afSJosef Bacik struct btrfs_root *root, int err) 165649b25e05SJeff Mahoney { 165749b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1658f094ac32SLiu Bo DEFINE_WAIT(wait); 165949b25e05SJeff Mahoney 166049b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 166149b25e05SJeff Mahoney 16627b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 16637b8b92afSJosef Bacik 166449b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 166566b6135bSLiu Bo 166625d8c284SMiao Xie /* 166725d8c284SMiao Xie * If the transaction is removed from the list, it means this 166825d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 166925d8c284SMiao Xie * to call the cleanup function. 167025d8c284SMiao Xie */ 167125d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 167266b6135bSLiu Bo 167349b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1674d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 16754a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1676f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1677f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1678f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1679f094ac32SLiu Bo 1680f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1681d7096fc3SJosef Bacik } 168249b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 168349b25e05SJeff Mahoney 168449b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 168549b25e05SJeff Mahoney 16864a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 16874a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 16884a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 16894a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 16904a9d8bdeSMiao Xie 1691e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1692e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1693724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1694724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 169549b25e05SJeff Mahoney 169649b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 169749b25e05SJeff Mahoney 169849b25e05SJeff Mahoney if (current->journal_info == trans) 169949b25e05SJeff Mahoney current->journal_info = NULL; 1700c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 170149b25e05SJeff Mahoney 170249b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 170349b25e05SJeff Mahoney } 170449b25e05SJeff Mahoney 170582436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 170682436617SMiao Xie { 170782436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 17086c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 170982436617SMiao Xie return 0; 171082436617SMiao Xie } 171182436617SMiao Xie 171282436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 171382436617SMiao Xie { 171482436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1715b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 171682436617SMiao Xie } 171782436617SMiao Xie 171879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 171979154b1bSChris Mason struct btrfs_root *root) 172079154b1bSChris Mason { 172149b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 17228fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1723656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 172425287e0aSMiao Xie int ret; 172579154b1bSChris Mason 17268d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 17278d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 172825287e0aSMiao Xie ret = cur_trans->aborted; 1729e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1730e4a2bcacSJosef Bacik return ret; 173125287e0aSMiao Xie } 173249b25e05SJeff Mahoney 173356bec294SChris Mason /* make a pass through all the delayed refs we have so far 173456bec294SChris Mason * any runnings procs may add more while we are here 173556bec294SChris Mason */ 173656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1737e4a2bcacSJosef Bacik if (ret) { 1738e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1739e4a2bcacSJosef Bacik return ret; 1740e4a2bcacSJosef Bacik } 174156bec294SChris Mason 17420e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 17430e721106SJosef Bacik trans->block_rsv = NULL; 1744272d26d0SMiao Xie if (trans->qgroup_reserved) { 1745272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1746272d26d0SMiao Xie trans->qgroup_reserved = 0; 1747272d26d0SMiao Xie } 17480e721106SJosef Bacik 1749b7ec40d7SChris Mason cur_trans = trans->transaction; 175049b25e05SJeff Mahoney 175156bec294SChris Mason /* 175256bec294SChris Mason * set the flushing flag so procs in this transaction have to 175356bec294SChris Mason * start sending their work down. 175456bec294SChris Mason */ 1755b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 17561be41b78SJosef Bacik smp_wmb(); 175756bec294SChris Mason 1758ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1759ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1760ea658badSJosef Bacik 1761c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1762e4a2bcacSJosef Bacik if (ret) { 1763e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1764e4a2bcacSJosef Bacik return ret; 1765e4a2bcacSJosef Bacik } 176656bec294SChris Mason 17674a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17684a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 17694a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 177013c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 177149b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1772ccd467d6SChris Mason 1773b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 177415ee9bc7SJosef Bacik 1775724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 177615ee9bc7SJosef Bacik 177749b25e05SJeff Mahoney return ret; 177879154b1bSChris Mason } 17794313b399SChris Mason 17804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1781bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1782bb9c12c9SSage Weil 1783ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1784ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1785ccd467d6SChris Mason struct btrfs_transaction, list); 17864a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 178713c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1788a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1789ccd467d6SChris Mason 1790ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1791ccd467d6SChris Mason 1792724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 1793a4abeea4SJosef Bacik } else { 1794a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1795ccd467d6SChris Mason } 1796a4abeea4SJosef Bacik } else { 1797a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1798ccd467d6SChris Mason } 179915ee9bc7SJosef Bacik 18000860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 18010860adfdSMiao Xie 180282436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 180382436617SMiao Xie if (ret) 180482436617SMiao Xie goto cleanup_transaction; 180582436617SMiao Xie 18068d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 180749b25e05SJeff Mahoney if (ret) 180849b25e05SJeff Mahoney goto cleanup_transaction; 180916cdcec7SMiao Xie 1810581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1811581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1812ed3b3d31SChris Mason 1813581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 18148d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1815ca469637SMiao Xie if (ret) 1816ca469637SMiao Xie goto cleanup_transaction; 1817ca469637SMiao Xie 181882436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1819cb7ab021SWang Shilong 1820cb7ab021SWang Shilong btrfs_scrub_pause(root); 1821ed0ca140SJosef Bacik /* 1822ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1823ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 18244a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1825ed0ca140SJosef Bacik */ 1826a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 18274a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1828a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1829ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1830ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 183115ee9bc7SJosef Bacik 18322cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 18332cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 18342cba30f1SMiao Xie ret = cur_trans->aborted; 18356cf7f77eSWang Shilong goto scrub_continue; 18362cba30f1SMiao Xie } 18377585717fSChris Mason /* 18387585717fSChris Mason * the reloc mutex makes sure that we stop 18397585717fSChris Mason * the balancing code from coming in and moving 18407585717fSChris Mason * extents around in the middle of the commit 18417585717fSChris Mason */ 18427585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 18437585717fSChris Mason 184442874b3dSMiao Xie /* 184542874b3dSMiao Xie * We needn't worry about the delayed items because we will 184642874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 184742874b3dSMiao Xie * core function of the snapshot creation. 184842874b3dSMiao Xie */ 184942874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 185049b25e05SJeff Mahoney if (ret) { 185149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 18526cf7f77eSWang Shilong goto scrub_continue; 185349b25e05SJeff Mahoney } 18543063d29fSChris Mason 185542874b3dSMiao Xie /* 185642874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 185742874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 185842874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 185942874b3dSMiao Xie * them. 186042874b3dSMiao Xie * 186142874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 186242874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 186342874b3dSMiao Xie * the nodes and leaves. 186442874b3dSMiao Xie */ 186542874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 186649b25e05SJeff Mahoney if (ret) { 186749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 18686cf7f77eSWang Shilong goto scrub_continue; 186949b25e05SJeff Mahoney } 187016cdcec7SMiao Xie 187156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 187249b25e05SJeff Mahoney if (ret) { 187349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 18746cf7f77eSWang Shilong goto scrub_continue; 187549b25e05SJeff Mahoney } 187656bec294SChris Mason 1877e999376fSChris Mason /* 1878e999376fSChris Mason * make sure none of the code above managed to slip in a 1879e999376fSChris Mason * delayed item 1880e999376fSChris Mason */ 1881e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1882e999376fSChris Mason 18832c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1884dc17ff8fSChris Mason 1885e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1886e02119d5SChris Mason * various roots consistent with each other. Every pointer 1887e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1888e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1889e02119d5SChris Mason * the tree logging code from jumping in and changing any 1890e02119d5SChris Mason * of the trees. 1891e02119d5SChris Mason * 1892e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1893e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1894e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1895e02119d5SChris Mason * from now until after the super is written, we avoid races 1896e02119d5SChris Mason * with the tree-log code. 1897e02119d5SChris Mason */ 1898e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 18991a40e23bSZheng Yan 19005d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 190149b25e05SJeff Mahoney if (ret) { 190249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1903871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 19046cf7f77eSWang Shilong goto scrub_continue; 190549b25e05SJeff Mahoney } 190654aa1f4dSChris Mason 19073818aea2SQu Wenruo /* 19083818aea2SQu Wenruo * Since the transaction is done, we should set the inode map cache flag 19093818aea2SQu Wenruo * before any other comming transaction. 19103818aea2SQu Wenruo */ 19113818aea2SQu Wenruo if (btrfs_test_opt(root, CHANGE_INODE_CACHE)) 19123818aea2SQu Wenruo btrfs_set_opt(root->fs_info->mount_opt, INODE_MAP_CACHE); 19133818aea2SQu Wenruo else 19143818aea2SQu Wenruo btrfs_clear_opt(root->fs_info->mount_opt, INODE_MAP_CACHE); 19153818aea2SQu Wenruo 19165d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1917e02119d5SChris Mason * safe to free the root of tree log roots 1918e02119d5SChris Mason */ 1919e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1920e02119d5SChris Mason 19215d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 192249b25e05SJeff Mahoney if (ret) { 192349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1924871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 19256cf7f77eSWang Shilong goto scrub_continue; 192649b25e05SJeff Mahoney } 192754aa1f4dSChris Mason 19282cba30f1SMiao Xie /* 19292cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 19302cba30f1SMiao Xie * update ->aborted, check it. 19312cba30f1SMiao Xie */ 19322cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19332cba30f1SMiao Xie ret = cur_trans->aborted; 19342cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 19352cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 19366cf7f77eSWang Shilong goto scrub_continue; 19372cba30f1SMiao Xie } 19382cba30f1SMiao Xie 193911833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 194011833d66SYan Zheng 194178fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 19425f39d397SChris Mason 19435d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 19445d4f98a2SYan Zheng root->fs_info->tree_root->node); 19459e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 19469e351cc8SJosef Bacik &cur_trans->switch_commits); 19475d4f98a2SYan Zheng 19485d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 19495d4f98a2SYan Zheng root->fs_info->chunk_root->node); 19509e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 19519e351cc8SJosef Bacik &cur_trans->switch_commits); 19529e351cc8SJosef Bacik 19539e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 19545d4f98a2SYan Zheng 1955edf39272SJan Schmidt assert_qgroups_uptodate(trans); 19565d4f98a2SYan Zheng update_super_roots(root); 1957e02119d5SChris Mason 19586c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 19596c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 19606c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 19616c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1962ccd467d6SChris Mason 1963935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 1964ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 1965935e5cc9SMiao Xie 1966656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 1967656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 1968656f30dbSFilipe Manana 1969a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19704a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 1971a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1972a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 19737585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1974b7ec40d7SChris Mason 1975f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1976e6dcd2dcSChris Mason 197779154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 197849b25e05SJeff Mahoney if (ret) { 197949b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 198008748810SDavid Sterba "Error while writing out transaction"); 198149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 19826cf7f77eSWang Shilong goto scrub_continue; 198349b25e05SJeff Mahoney } 198449b25e05SJeff Mahoney 198549b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 198649b25e05SJeff Mahoney if (ret) { 198749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 19886cf7f77eSWang Shilong goto scrub_continue; 198949b25e05SJeff Mahoney } 19904313b399SChris Mason 1991e02119d5SChris Mason /* 1992e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1993e02119d5SChris Mason * to go about their business 1994e02119d5SChris Mason */ 1995e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1996e02119d5SChris Mason 199711833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 19984313b399SChris Mason 199915ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 20004a9d8bdeSMiao Xie /* 20014a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 20024a9d8bdeSMiao Xie * which can change it. 20034a9d8bdeSMiao Xie */ 20044a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 20052c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 20063de4586cSChris Mason 2007a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 200813c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2009a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2010a4abeea4SJosef Bacik 2011724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2012724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 201358176a96SJosef Bacik 20140860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2015b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2016b2b5ef5cSJan Kara 20171abe9b8aSliubo trace_btrfs_transaction_commit(root); 20181abe9b8aSliubo 2019a2de733cSArne Jansen btrfs_scrub_continue(root); 2020a2de733cSArne Jansen 20219ed74f2dSJosef Bacik if (current->journal_info == trans) 20229ed74f2dSJosef Bacik current->journal_info = NULL; 20239ed74f2dSJosef Bacik 20242c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 202524bbcf04SYan, Zheng 202624bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 202724bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 202824bbcf04SYan, Zheng 202979154b1bSChris Mason return ret; 203049b25e05SJeff Mahoney 20316cf7f77eSWang Shilong scrub_continue: 20326cf7f77eSWang Shilong btrfs_scrub_continue(root); 203349b25e05SJeff Mahoney cleanup_transaction: 20340e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 20350e721106SJosef Bacik trans->block_rsv = NULL; 2036272d26d0SMiao Xie if (trans->qgroup_reserved) { 2037272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 2038272d26d0SMiao Xie trans->qgroup_reserved = 0; 2039272d26d0SMiao Xie } 2040c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 204149b25e05SJeff Mahoney if (current->journal_info == trans) 204249b25e05SJeff Mahoney current->journal_info = NULL; 20437b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 204449b25e05SJeff Mahoney 204549b25e05SJeff Mahoney return ret; 204679154b1bSChris Mason } 204779154b1bSChris Mason 2048d352ac68SChris Mason /* 20499d1a2a3aSDavid Sterba * return < 0 if error 20509d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 20519d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 20529d1a2a3aSDavid Sterba * 20539d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 20549d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 20559d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 20569d1a2a3aSDavid Sterba * few seconds later. 2057d352ac68SChris Mason */ 20589d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2059e9d0b13bSChris Mason { 20609d1a2a3aSDavid Sterba int ret; 20615d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2062e9d0b13bSChris Mason 2063a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 20649d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 20659d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 20669d1a2a3aSDavid Sterba return 0; 20679d1a2a3aSDavid Sterba } 20689d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 20699d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2070cfad392bSJosef Bacik list_del_init(&root->root_list); 2071a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 20725d4f98a2SYan Zheng 2073efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 207476dda93cSYan, Zheng 207516cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 207616cdcec7SMiao Xie 207776dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 207876dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 20792c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 208076dda93cSYan, Zheng else 20812c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 208232471dc2SDavid Sterba 20836596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2084e9d0b13bSChris Mason } 2085