16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2679154b1bSChris Mason #include "ctree.h" 2779154b1bSChris Mason #include "disk-io.h" 2879154b1bSChris Mason #include "transaction.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31581bb050SLi Zefan #include "inode-map.h" 32733f4fbbSStefan Behrens #include "volumes.h" 338dabb742SStefan Behrens #include "dev-replace.h" 34fcebe456SJosef Bacik #include "qgroup.h" 3579154b1bSChris Mason 360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 370f7d52f4SChris Mason 38e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 394a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 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)); 671262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 681262133bSJosef Bacik printk(KERN_ERR "pending csums is %llu\n", 691262133bSJosef Bacik transaction->delayed_refs.pending_csums); 706df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 716df9a95eSJosef Bacik struct extent_map *em; 726df9a95eSJosef Bacik 736df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 746df9a95eSJosef Bacik struct extent_map, list); 756df9a95eSJosef Bacik list_del_init(&em->list); 766df9a95eSJosef Bacik free_extent_map(em); 776df9a95eSJosef Bacik } 782c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7979154b1bSChris Mason } 8078fae27eSChris Mason } 8179154b1bSChris Mason 82663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 83663dfbb0SFilipe Manana { 84663dfbb0SFilipe Manana spin_lock(&tree->lock); 85663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 86663dfbb0SFilipe Manana struct rb_node *node; 87663dfbb0SFilipe Manana struct extent_state *state; 88663dfbb0SFilipe Manana 89663dfbb0SFilipe Manana node = rb_first(&tree->state); 90663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 91663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 92663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 93663dfbb0SFilipe Manana /* 94663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 95663dfbb0SFilipe Manana * changes in the flags of extent states ever. 96663dfbb0SFilipe Manana */ 97663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 98663dfbb0SFilipe Manana free_extent_state(state); 99351810c1SDavid Sterba 100351810c1SDavid Sterba cond_resched_lock(&tree->lock); 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); 22313212b54SZhao Lei cur_trans->have_free_bgs = 0; 22408607c1bSChris Mason cur_trans->start_time = get_seconds(); 2251bbc621eSChris Mason cur_trans->dirty_bg_run = 0; 22656bec294SChris Mason 227c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2283368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 229d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 230c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 2311262133bSJosef Bacik cur_trans->delayed_refs.pending_csums = 0; 232c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 23356bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 234c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 2359086db86SQu Wenruo cur_trans->delayed_refs.qgroup_to_skip = 0; 23620b297d6SJan Schmidt 23720b297d6SJan Schmidt /* 23820b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 23920b297d6SJan Schmidt * the log must never go across transaction boundaries. 24020b297d6SJan Schmidt */ 24120b297d6SJan Schmidt smp_mb(); 24231b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 243efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 24420b297d6SJan Schmidt "creating a fresh transaction\n"); 24531b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 246efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 24720b297d6SJan Schmidt "creating a fresh transaction\n"); 248fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 24920b297d6SJan Schmidt 25056bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 25156bec294SChris Mason 2523063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2536df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2549e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 25550d9aa99SJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_ordered); 256ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2571bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2581bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 259cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 260ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 261e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 262e33e17eeSJeff Mahoney spin_lock_init(&cur_trans->deleted_bgs_lock); 26319ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 264d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 26519ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 26619ae4e81SJan Schmidt fs_info->generation++; 26719ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 26819ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 26949b25e05SJeff Mahoney cur_trans->aborted = 0; 27019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 27115ee9bc7SJosef Bacik 27279154b1bSChris Mason return 0; 27379154b1bSChris Mason } 27479154b1bSChris Mason 275d352ac68SChris Mason /* 276d397712bSChris Mason * this does all the record keeping required to make sure that a reference 277d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 278d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 279d397712bSChris Mason * when the transaction commits 280d352ac68SChris Mason */ 2817585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2825d4f98a2SYan Zheng struct btrfs_root *root) 2836702ed49SChris Mason { 28427cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 28527cdeb70SMiao Xie root->last_trans < trans->transid) { 2866702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2875d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2885d4f98a2SYan Zheng 2897585717fSChris Mason /* 29027cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2917585717fSChris Mason * we have the reloc mutex held now, so there 2927585717fSChris Mason * is only one writer in this function 2937585717fSChris Mason */ 29427cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2957585717fSChris Mason 29627cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2977585717fSChris Mason * they find our root->last_trans update 2987585717fSChris Mason */ 2997585717fSChris Mason smp_wmb(); 3007585717fSChris Mason 301a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 302a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 303a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 304a4abeea4SJosef Bacik return 0; 305a4abeea4SJosef Bacik } 3066702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 3076702ed49SChris Mason (unsigned long)root->root_key.objectid, 3086702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 309a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3107585717fSChris Mason root->last_trans = trans->transid; 3117585717fSChris Mason 3127585717fSChris Mason /* this is pretty tricky. We don't want to 3137585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3147585717fSChris Mason * unless we're really doing the first setup for this root in 3157585717fSChris Mason * this transaction. 3167585717fSChris Mason * 3177585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3187585717fSChris Mason * if we want to take the expensive mutex. 3197585717fSChris Mason * 3207585717fSChris Mason * But, we have to set root->last_trans before we 3217585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3227585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 32327cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3247585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3257585717fSChris Mason * 3267585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3277585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3287585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3297585717fSChris Mason * done before we pop in the zero below 3307585717fSChris Mason */ 3315d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 332c7548af6SChris Mason smp_mb__before_atomic(); 33327cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3346702ed49SChris Mason } 3355d4f98a2SYan Zheng return 0; 3366702ed49SChris Mason } 3375d4f98a2SYan Zheng 3387585717fSChris Mason 3397585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3407585717fSChris Mason struct btrfs_root *root) 3417585717fSChris Mason { 34227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3437585717fSChris Mason return 0; 3447585717fSChris Mason 3457585717fSChris Mason /* 34627cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3477585717fSChris Mason * and barriers 3487585717fSChris Mason */ 3497585717fSChris Mason smp_rmb(); 3507585717fSChris Mason if (root->last_trans == trans->transid && 35127cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3527585717fSChris Mason return 0; 3537585717fSChris Mason 3547585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3557585717fSChris Mason record_root_in_trans(trans, root); 3567585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3577585717fSChris Mason 3587585717fSChris Mason return 0; 3597585717fSChris Mason } 3607585717fSChris Mason 3614a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3624a9d8bdeSMiao Xie { 3634a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 364501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 365501407aaSJosef Bacik !trans->aborted); 3664a9d8bdeSMiao Xie } 3674a9d8bdeSMiao Xie 368d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 369d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 370d352ac68SChris Mason * transaction might not be fully on disk. 371d352ac68SChris Mason */ 37237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 37379154b1bSChris Mason { 374f9295749SChris Mason struct btrfs_transaction *cur_trans; 37579154b1bSChris Mason 376a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 377f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3784a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 37913c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 380a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 38172d63ed6SLi Zefan 38272d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 383501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 384501407aaSJosef Bacik cur_trans->aborted); 385724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 386a4abeea4SJosef Bacik } else { 387a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 388f9295749SChris Mason } 38937d1aeeeSChris Mason } 39037d1aeeeSChris Mason 391a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 39237d1aeeeSChris Mason { 393a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 394a4abeea4SJosef Bacik return 0; 395a4abeea4SJosef Bacik 396a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 397a22285a6SYan, Zheng return 1; 398a4abeea4SJosef Bacik 399a4abeea4SJosef Bacik if (type == TRANS_START && 400a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 401a4abeea4SJosef Bacik return 1; 402a4abeea4SJosef Bacik 403a22285a6SYan, Zheng return 0; 404a22285a6SYan, Zheng } 405a22285a6SYan, Zheng 40620dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 40720dd2cbfSMiao Xie { 40820dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 40927cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 41020dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 41120dd2cbfSMiao Xie root->reloc_root) 41220dd2cbfSMiao Xie return false; 41320dd2cbfSMiao Xie 41420dd2cbfSMiao Xie return true; 41520dd2cbfSMiao Xie } 41620dd2cbfSMiao Xie 41708e007d2SMiao Xie static struct btrfs_trans_handle * 4180860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 41908e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 420a22285a6SYan, Zheng { 421a22285a6SYan, Zheng struct btrfs_trans_handle *h; 422a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 423b5009945SJosef Bacik u64 num_bytes = 0; 424c5567237SArne Jansen u64 qgroup_reserved = 0; 42520dd2cbfSMiao Xie bool reloc_reserved = false; 42620dd2cbfSMiao Xie int ret; 427acce952bSliubo 42846c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4292755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 43046c4e71eSFilipe Manana 43187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 432acce952bSliubo return ERR_PTR(-EROFS); 4332a1eb461SJosef Bacik 43446c4e71eSFilipe Manana if (current->journal_info) { 4350860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4362a1eb461SJosef Bacik h = current->journal_info; 4372a1eb461SJosef Bacik h->use_count++; 438b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 4392a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4402a1eb461SJosef Bacik h->block_rsv = NULL; 4412a1eb461SJosef Bacik goto got_it; 4422a1eb461SJosef Bacik } 443b5009945SJosef Bacik 444b5009945SJosef Bacik /* 445b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 446b5009945SJosef Bacik * the appropriate flushing if need be. 447b5009945SJosef Bacik */ 448b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 449c5567237SArne Jansen if (root->fs_info->quota_enabled && 450c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 451707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 452c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 453c5567237SArne Jansen if (ret) 454c5567237SArne Jansen return ERR_PTR(ret); 455c5567237SArne Jansen } 456c5567237SArne Jansen 457b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 45820dd2cbfSMiao Xie /* 45920dd2cbfSMiao Xie * Do the reservation for the relocation root creation 46020dd2cbfSMiao Xie */ 461ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 46220dd2cbfSMiao Xie num_bytes += root->nodesize; 46320dd2cbfSMiao Xie reloc_reserved = true; 46420dd2cbfSMiao Xie } 46520dd2cbfSMiao Xie 4664a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 467b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 46808e007d2SMiao Xie num_bytes, flush); 469b5009945SJosef Bacik if (ret) 470843fcf35SMiao Xie goto reserve_fail; 471b5009945SJosef Bacik } 472a22285a6SYan, Zheng again: 473a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 474843fcf35SMiao Xie if (!h) { 475843fcf35SMiao Xie ret = -ENOMEM; 476843fcf35SMiao Xie goto alloc_fail; 477843fcf35SMiao Xie } 478a22285a6SYan, Zheng 47998114659SJosef Bacik /* 48098114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 48198114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 48298114659SJosef Bacik * because we're already holding a ref. We need this because we could 48398114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 48498114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 485354aa0fbSMiao Xie * 486354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 487354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 48898114659SJosef Bacik */ 4890860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 490b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 491b2b5ef5cSJan Kara 492a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 49337d1aeeeSChris Mason wait_current_trans(root); 494a22285a6SYan, Zheng 495a4abeea4SJosef Bacik do { 496354aa0fbSMiao Xie ret = join_transaction(root, type); 497178260b2SMiao Xie if (ret == -EBUSY) { 498a4abeea4SJosef Bacik wait_current_trans(root); 499178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 500178260b2SMiao Xie ret = -ENOENT; 501178260b2SMiao Xie } 502a4abeea4SJosef Bacik } while (ret == -EBUSY); 503a4abeea4SJosef Bacik 504db5b493aSTsutomu Itoh if (ret < 0) { 505354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 506354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 507843fcf35SMiao Xie goto join_fail; 508db5b493aSTsutomu Itoh } 5090f7d52f4SChris Mason 510a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 511a22285a6SYan, Zheng 512a22285a6SYan, Zheng h->transid = cur_trans->transid; 513a22285a6SYan, Zheng h->transaction = cur_trans; 51479154b1bSChris Mason h->blocks_used = 0; 515a22285a6SYan, Zheng h->bytes_reserved = 0; 5164fbcdf66SFilipe Manana h->chunk_bytes_reserved = 0; 517d13603efSArne Jansen h->root = root; 51856bec294SChris Mason h->delayed_ref_updates = 0; 5192a1eb461SJosef Bacik h->use_count = 1; 5200e721106SJosef Bacik h->adding_csums = 0; 521f0486c68SYan, Zheng h->block_rsv = NULL; 5222a1eb461SJosef Bacik h->orig_rsv = NULL; 52349b25e05SJeff Mahoney h->aborted = 0; 5244b824906SMiao Xie h->qgroup_reserved = 0; 525bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 526a698d075SMiao Xie h->type = type; 527c6b305a8SJosef Bacik h->allocating_chunk = false; 52820dd2cbfSMiao Xie h->reloc_reserved = false; 5295039eddcSJosef Bacik h->sync = false; 530bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 531ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 53250d9aa99SJosef Bacik INIT_LIST_HEAD(&h->ordered); 533b7ec40d7SChris Mason 534a22285a6SYan, Zheng smp_mb(); 5354a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5364a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 537abdd2e80SFilipe Manana current->journal_info = h; 538a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 539a22285a6SYan, Zheng goto again; 540a22285a6SYan, Zheng } 5419ed74f2dSJosef Bacik 542b5009945SJosef Bacik if (num_bytes) { 5438c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5442bcc0328SLiu Bo h->transid, num_bytes, 1); 545b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 546b5009945SJosef Bacik h->bytes_reserved = num_bytes; 54720dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 548a22285a6SYan, Zheng } 5494b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 550a22285a6SYan, Zheng 5512a1eb461SJosef Bacik got_it: 552a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 553a22285a6SYan, Zheng 554a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 555a22285a6SYan, Zheng current->journal_info = h; 55679154b1bSChris Mason return h; 557843fcf35SMiao Xie 558843fcf35SMiao Xie join_fail: 5590860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 560843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 561843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 562843fcf35SMiao Xie alloc_fail: 563843fcf35SMiao Xie if (num_bytes) 564843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 565843fcf35SMiao Xie num_bytes); 566843fcf35SMiao Xie reserve_fail: 567843fcf35SMiao Xie if (qgroup_reserved) 568843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 569843fcf35SMiao Xie return ERR_PTR(ret); 57079154b1bSChris Mason } 57179154b1bSChris Mason 572f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 573a22285a6SYan, Zheng int num_items) 574f9295749SChris Mason { 57508e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 57608e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 577f9295749SChris Mason } 5788407aa46SMiao Xie 57908e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 5808407aa46SMiao Xie struct btrfs_root *root, int num_items) 5818407aa46SMiao Xie { 58208e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 58308e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 5848407aa46SMiao Xie } 5858407aa46SMiao Xie 5867a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 587f9295749SChris Mason { 5888407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 589f9295749SChris Mason } 590f9295749SChris Mason 5917a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5920af3d00bSJosef Bacik { 5938407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5940af3d00bSJosef Bacik } 5950af3d00bSJosef Bacik 5967a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5979ca9ee09SSage Weil { 5988407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5999ca9ee09SSage Weil } 6009ca9ee09SSage Weil 601d4edf39bSMiao Xie /* 602d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 603d4edf39bSMiao Xie * 604d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 605d4edf39bSMiao Xie * don't want to start a new one. 606d4edf39bSMiao Xie * 607d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 608d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 609d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 610d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 611d4edf39bSMiao Xie * invoke 612d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 613d4edf39bSMiao Xie */ 614354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 61560376ce4SJosef Bacik { 616354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 61760376ce4SJosef Bacik } 61860376ce4SJosef Bacik 619d4edf39bSMiao Xie /* 62090b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 621d4edf39bSMiao Xie * 622d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 623d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 624d4edf39bSMiao Xie * complete. 625d4edf39bSMiao Xie */ 626d4edf39bSMiao Xie struct btrfs_trans_handle * 627d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 628d4edf39bSMiao Xie { 629d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 630d4edf39bSMiao Xie 631d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 632d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 633d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 634d4edf39bSMiao Xie 635d4edf39bSMiao Xie return trans; 636d4edf39bSMiao Xie } 637d4edf39bSMiao Xie 638d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 639b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 64089ce8a63SChris Mason struct btrfs_transaction *commit) 64189ce8a63SChris Mason { 6424a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 64389ce8a63SChris Mason } 64489ce8a63SChris Mason 64546204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 64646204592SSage Weil { 64746204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 6488cd2807fSMiao Xie int ret = 0; 64946204592SSage Weil 65046204592SSage Weil if (transid) { 65146204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 652a4abeea4SJosef Bacik goto out; 65346204592SSage Weil 65446204592SSage Weil /* find specified transaction */ 655a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 65646204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 65746204592SSage Weil if (t->transid == transid) { 65846204592SSage Weil cur_trans = t; 659a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 6608cd2807fSMiao Xie ret = 0; 66146204592SSage Weil break; 66246204592SSage Weil } 6638cd2807fSMiao Xie if (t->transid > transid) { 6648cd2807fSMiao Xie ret = 0; 66546204592SSage Weil break; 66646204592SSage Weil } 6678cd2807fSMiao Xie } 668a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 66942383020SSage Weil 67042383020SSage Weil /* 67142383020SSage Weil * The specified transaction doesn't exist, or we 67242383020SSage Weil * raced with btrfs_commit_transaction 67342383020SSage Weil */ 67442383020SSage Weil if (!cur_trans) { 67542383020SSage Weil if (transid > root->fs_info->last_trans_committed) 67642383020SSage Weil ret = -EINVAL; 6778cd2807fSMiao Xie goto out; 67842383020SSage Weil } 67946204592SSage Weil } else { 68046204592SSage Weil /* find newest transaction that is committing | committed */ 681a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 68246204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 68346204592SSage Weil list) { 6844a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 6854a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 6863473f3c0SJosef Bacik break; 68746204592SSage Weil cur_trans = t; 688a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 68946204592SSage Weil break; 69046204592SSage Weil } 69146204592SSage Weil } 692a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 69346204592SSage Weil if (!cur_trans) 694a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 69546204592SSage Weil } 69646204592SSage Weil 69746204592SSage Weil wait_for_commit(root, cur_trans); 698724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 699a4abeea4SJosef Bacik out: 70046204592SSage Weil return ret; 70146204592SSage Weil } 70246204592SSage Weil 70337d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 70437d1aeeeSChris Mason { 705a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 70637d1aeeeSChris Mason wait_current_trans(root); 70737d1aeeeSChris Mason } 70837d1aeeeSChris Mason 7098929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7108929ecfaSYan, Zheng struct btrfs_root *root) 7118929ecfaSYan, Zheng { 7121be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7130a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7141be41b78SJosef Bacik return 1; 71536ba022aSJosef Bacik 7161be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7178929ecfaSYan, Zheng } 7188929ecfaSYan, Zheng 7198929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7208929ecfaSYan, Zheng struct btrfs_root *root) 7218929ecfaSYan, Zheng { 7228929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7238929ecfaSYan, Zheng int updates; 72449b25e05SJeff Mahoney int err; 7258929ecfaSYan, Zheng 726a4abeea4SJosef Bacik smp_mb(); 7274a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7284a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7298929ecfaSYan, Zheng return 1; 7308929ecfaSYan, Zheng 7318929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 7328929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 73349b25e05SJeff Mahoney if (updates) { 73428ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 73549b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 73649b25e05SJeff Mahoney return err; 73749b25e05SJeff Mahoney } 7388929ecfaSYan, Zheng 7398929ecfaSYan, Zheng return should_end_transaction(trans, root); 7408929ecfaSYan, Zheng } 7418929ecfaSYan, Zheng 74289ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 743a698d075SMiao Xie struct btrfs_root *root, int throttle) 74479154b1bSChris Mason { 7458929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 746ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 7471be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 748a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 7494edc2ca3SDave Jones int err = 0; 750a79b7d4bSChris Mason int must_run_delayed_refs = 0; 751d6e4a428SChris Mason 7523bbb24b2SJosef Bacik if (trans->use_count > 1) { 7533bbb24b2SJosef Bacik trans->use_count--; 7542a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 7552a1eb461SJosef Bacik return 0; 7562a1eb461SJosef Bacik } 7572a1eb461SJosef Bacik 758b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 7594c13d758SJosef Bacik trans->block_rsv = NULL; 760c5567237SArne Jansen 761ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 762ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 763ea658badSJosef Bacik 76450d9aa99SJosef Bacik if (!list_empty(&trans->ordered)) { 76550d9aa99SJosef Bacik spin_lock(&info->trans_lock); 766d3efe084SFilipe Manana list_splice_init(&trans->ordered, &cur_trans->pending_ordered); 76750d9aa99SJosef Bacik spin_unlock(&info->trans_lock); 76850d9aa99SJosef Bacik } 76950d9aa99SJosef Bacik 770c3e69d58SChris Mason trans->delayed_ref_updates = 0; 771a79b7d4bSChris Mason if (!trans->sync) { 772a79b7d4bSChris Mason must_run_delayed_refs = 773a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 7740a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 775a79b7d4bSChris Mason 776a79b7d4bSChris Mason /* 777a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 778a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 779a79b7d4bSChris Mason */ 780a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 781a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 782a79b7d4bSChris Mason must_run_delayed_refs = 2; 78356bec294SChris Mason } 784bb721703SChris Mason 785fcebe456SJosef Bacik if (trans->qgroup_reserved) { 786fcebe456SJosef Bacik /* 787fcebe456SJosef Bacik * the same root has to be passed here between start_transaction 788fcebe456SJosef Bacik * and end_transaction. Subvolume quota depends on this. 789fcebe456SJosef Bacik */ 790fcebe456SJosef Bacik btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 791fcebe456SJosef Bacik trans->qgroup_reserved = 0; 792fcebe456SJosef Bacik } 793fcebe456SJosef Bacik 7940e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 7950e721106SJosef Bacik trans->block_rsv = NULL; 79656bec294SChris Mason 797ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 798ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 799ea658badSJosef Bacik 8004fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8014fbcdf66SFilipe Manana 802a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8034a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8044a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8054a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8064a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8074a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8084a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 809a4abeea4SJosef Bacik } 8108929ecfaSYan, Zheng 8114a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8123bbb24b2SJosef Bacik if (throttle) 8138929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8143bbb24b2SJosef Bacik else 8158929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8168929ecfaSYan, Zheng } 8178929ecfaSYan, Zheng 8180860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8196df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8206df7881aSJosef Bacik 8218929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 82213c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 82313c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8240860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 82589ce8a63SChris Mason 82699d16cbcSSage Weil smp_mb(); 82779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 82879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 829724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8309ed74f2dSJosef Bacik 8319ed74f2dSJosef Bacik if (current->journal_info == trans) 8329ed74f2dSJosef Bacik current->journal_info = NULL; 833ab78c84dSChris Mason 83424bbcf04SYan, Zheng if (throttle) 83524bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 83624bbcf04SYan, Zheng 83749b25e05SJeff Mahoney if (trans->aborted || 8384e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 8394e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8404edc2ca3SDave Jones err = -EIO; 8414e121c06SJosef Bacik } 842edf39272SJan Schmidt assert_qgroups_uptodate(trans); 84349b25e05SJeff Mahoney 8444edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 845a79b7d4bSChris Mason if (must_run_delayed_refs) { 846a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 847a79b7d4bSChris Mason must_run_delayed_refs == 1); 848a79b7d4bSChris Mason } 8494edc2ca3SDave Jones return err; 85079154b1bSChris Mason } 85179154b1bSChris Mason 85289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 85389ce8a63SChris Mason struct btrfs_root *root) 85489ce8a63SChris Mason { 85598ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 85689ce8a63SChris Mason } 85789ce8a63SChris Mason 85889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 85989ce8a63SChris Mason struct btrfs_root *root) 86089ce8a63SChris Mason { 86198ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 86216cdcec7SMiao Xie } 86316cdcec7SMiao Xie 864d352ac68SChris Mason /* 865d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 866d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 867690587d1SChris Mason * those extents are sent to disk but does not wait on them 868d352ac68SChris Mason */ 869690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 8708cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 87179154b1bSChris Mason { 872777e6bd7SChris Mason int err = 0; 8737c4452b9SChris Mason int werr = 0; 8741728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 875e6138876SJosef Bacik struct extent_state *cached_state = NULL; 876777e6bd7SChris Mason u64 start = 0; 8775f39d397SChris Mason u64 end; 8787c4452b9SChris Mason 8791728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 880e6138876SJosef Bacik mark, &cached_state)) { 881663dfbb0SFilipe Manana bool wait_writeback = false; 882663dfbb0SFilipe Manana 883663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 884663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 885e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 886663dfbb0SFilipe Manana /* 887663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 888663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 889663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 890663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 891663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 892663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 893663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 894663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 895663dfbb0SFilipe Manana * (and without errors). 896663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 897663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 898663dfbb0SFilipe Manana */ 899663dfbb0SFilipe Manana if (err == -ENOMEM) { 900663dfbb0SFilipe Manana err = 0; 901663dfbb0SFilipe Manana wait_writeback = true; 902663dfbb0SFilipe Manana } 903663dfbb0SFilipe Manana if (!err) 9041728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9057c4452b9SChris Mason if (err) 9067c4452b9SChris Mason werr = err; 907663dfbb0SFilipe Manana else if (wait_writeback) 908663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 909e38e2ed7SFilipe Manana free_extent_state(cached_state); 910663dfbb0SFilipe Manana cached_state = NULL; 9111728366eSJosef Bacik cond_resched(); 9121728366eSJosef Bacik start = end + 1; 9137c4452b9SChris Mason } 914690587d1SChris Mason return werr; 915690587d1SChris Mason } 916690587d1SChris Mason 917690587d1SChris Mason /* 918690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 919690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 920690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 921690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 922690587d1SChris Mason */ 923690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9248cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 925690587d1SChris Mason { 926690587d1SChris Mason int err = 0; 927690587d1SChris Mason int werr = 0; 9281728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 929e6138876SJosef Bacik struct extent_state *cached_state = NULL; 930690587d1SChris Mason u64 start = 0; 931690587d1SChris Mason u64 end; 932656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 933656f30dbSFilipe Manana bool errors = false; 934690587d1SChris Mason 9351728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 936e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 937663dfbb0SFilipe Manana /* 938663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 939663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 940663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 941663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 942663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 943663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 944663dfbb0SFilipe Manana */ 945663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 946663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 947e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 948663dfbb0SFilipe Manana if (err == -ENOMEM) 949663dfbb0SFilipe Manana err = 0; 950663dfbb0SFilipe Manana if (!err) 9511728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 952777e6bd7SChris Mason if (err) 953777e6bd7SChris Mason werr = err; 954e38e2ed7SFilipe Manana free_extent_state(cached_state); 955e38e2ed7SFilipe Manana cached_state = NULL; 956777e6bd7SChris Mason cond_resched(); 9571728366eSJosef Bacik start = end + 1; 958777e6bd7SChris Mason } 9597c4452b9SChris Mason if (err) 9607c4452b9SChris Mason werr = err; 961656f30dbSFilipe Manana 962656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 963656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 964656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 965656f30dbSFilipe Manana &btree_ino->runtime_flags)) 966656f30dbSFilipe Manana errors = true; 967656f30dbSFilipe Manana 968656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 969656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 970656f30dbSFilipe Manana &btree_ino->runtime_flags)) 971656f30dbSFilipe Manana errors = true; 972656f30dbSFilipe Manana } else { 973656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 974656f30dbSFilipe Manana &btree_ino->runtime_flags)) 975656f30dbSFilipe Manana errors = true; 976656f30dbSFilipe Manana } 977656f30dbSFilipe Manana 978656f30dbSFilipe Manana if (errors && !werr) 979656f30dbSFilipe Manana werr = -EIO; 980656f30dbSFilipe Manana 9817c4452b9SChris Mason return werr; 98279154b1bSChris Mason } 98379154b1bSChris Mason 984690587d1SChris Mason /* 985690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 986690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 987690587d1SChris Mason * those extents are on disk for transaction or log commit 988690587d1SChris Mason */ 989171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 9908cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 991690587d1SChris Mason { 992690587d1SChris Mason int ret; 993690587d1SChris Mason int ret2; 994c6adc9ccSMiao Xie struct blk_plug plug; 995690587d1SChris Mason 996c6adc9ccSMiao Xie blk_start_plug(&plug); 9978cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 998c6adc9ccSMiao Xie blk_finish_plug(&plug); 9998cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1000bf0da8c1SChris Mason 1001bf0da8c1SChris Mason if (ret) 1002bf0da8c1SChris Mason return ret; 1003bf0da8c1SChris Mason if (ret2) 1004bf0da8c1SChris Mason return ret2; 1005bf0da8c1SChris Mason return 0; 1006690587d1SChris Mason } 1007690587d1SChris Mason 1008663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1009d0c803c4SChris Mason struct btrfs_root *root) 1010d0c803c4SChris Mason { 1011663dfbb0SFilipe Manana int ret; 1012663dfbb0SFilipe Manana 1013663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10148cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10158cef4e16SYan, Zheng EXTENT_DIRTY); 1016663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1017663dfbb0SFilipe Manana 1018663dfbb0SFilipe Manana return ret; 1019d0c803c4SChris Mason } 1020d0c803c4SChris Mason 1021d352ac68SChris Mason /* 1022d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1023d352ac68SChris Mason * 1024d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1025d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1026d352ac68SChris Mason * allocation tree. 1027d352ac68SChris Mason * 1028d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1029d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1030d352ac68SChris Mason */ 10310b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 103279154b1bSChris Mason struct btrfs_root *root) 103379154b1bSChris Mason { 103479154b1bSChris Mason int ret; 10350b86a832SChris Mason u64 old_root_bytenr; 103686b9f2ecSYan, Zheng u64 old_root_used; 10370b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 103879154b1bSChris Mason 103986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 104056bec294SChris Mason 104179154b1bSChris Mason while (1) { 10420b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 104386b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1044ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 104579154b1bSChris Mason break; 104687ef2bb4SChris Mason 10475d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 104879154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10490b86a832SChris Mason &root->root_key, 10500b86a832SChris Mason &root->root_item); 105149b25e05SJeff Mahoney if (ret) 105249b25e05SJeff Mahoney return ret; 105356bec294SChris Mason 105486b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1055e7070be1SJosef Bacik } 1056276e680dSYan Zheng 10570b86a832SChris Mason return 0; 10580b86a832SChris Mason } 10590b86a832SChris Mason 1060d352ac68SChris Mason /* 1061d352ac68SChris Mason * update all the cowonly tree roots on disk 106249b25e05SJeff Mahoney * 106349b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 106449b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 106549b25e05SJeff Mahoney * to clean up the delayed refs. 1066d352ac68SChris Mason */ 10675d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 10680b86a832SChris Mason struct btrfs_root *root) 10690b86a832SChris Mason { 10700b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1071ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 10721bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 10730b86a832SChris Mason struct list_head *next; 107484234f3aSYan Zheng struct extent_buffer *eb; 107556bec294SChris Mason int ret; 107684234f3aSYan Zheng 107784234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 107849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 107949b25e05SJeff Mahoney 0, &eb); 108084234f3aSYan Zheng btrfs_tree_unlock(eb); 108184234f3aSYan Zheng free_extent_buffer(eb); 10820b86a832SChris Mason 108349b25e05SJeff Mahoney if (ret) 108449b25e05SJeff Mahoney return ret; 108549b25e05SJeff Mahoney 108656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 108749b25e05SJeff Mahoney if (ret) 108849b25e05SJeff Mahoney return ret; 108987ef2bb4SChris Mason 1090733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1091c16ce190SJosef Bacik if (ret) 1092c16ce190SJosef Bacik return ret; 10938dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1094c16ce190SJosef Bacik if (ret) 1095c16ce190SJosef Bacik return ret; 1096546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1097c16ce190SJosef Bacik if (ret) 1098c16ce190SJosef Bacik return ret; 1099546adb0dSJan Schmidt 1100dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1101dcdf7f6dSJosef Bacik if (ret) 1102dcdf7f6dSJosef Bacik return ret; 1103dcdf7f6dSJosef Bacik 1104546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1105546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1106c16ce190SJosef Bacik if (ret) 1107c16ce190SJosef Bacik return ret; 1108ea526d18SJosef Bacik again: 11090b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11100b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11110b86a832SChris Mason list_del_init(next); 11120b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1113e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 111487ef2bb4SChris Mason 11159e351cc8SJosef Bacik if (root != fs_info->extent_root) 11169e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11179e351cc8SJosef Bacik &trans->transaction->switch_commits); 111849b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 111949b25e05SJeff Mahoney if (ret) 112049b25e05SJeff Mahoney return ret; 1121ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1122ea526d18SJosef Bacik if (ret) 1123ea526d18SJosef Bacik return ret; 112479154b1bSChris Mason } 1125276e680dSYan Zheng 11261bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1127ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1128ea526d18SJosef Bacik if (ret) 1129ea526d18SJosef Bacik return ret; 1130ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1131ea526d18SJosef Bacik if (ret) 1132ea526d18SJosef Bacik return ret; 1133ea526d18SJosef Bacik } 1134ea526d18SJosef Bacik 1135ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1136ea526d18SJosef Bacik goto again; 1137ea526d18SJosef Bacik 11389e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11399e351cc8SJosef Bacik &trans->transaction->switch_commits); 11408dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 11418dabb742SStefan Behrens 114279154b1bSChris Mason return 0; 114379154b1bSChris Mason } 114479154b1bSChris Mason 1145d352ac68SChris Mason /* 1146d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1147d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1148d352ac68SChris Mason * be deleted 1149d352ac68SChris Mason */ 1150cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11515eda7b5eSChris Mason { 1152a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1153cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11549d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1155a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 11565eda7b5eSChris Mason } 11575eda7b5eSChris Mason 1158d352ac68SChris Mason /* 11595d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1160d352ac68SChris Mason */ 11615d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 11625d4f98a2SYan Zheng struct btrfs_root *root) 11630f7d52f4SChris Mason { 11640f7d52f4SChris Mason struct btrfs_root *gang[8]; 11655d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 11660f7d52f4SChris Mason int i; 11670f7d52f4SChris Mason int ret; 116854aa1f4dSChris Mason int err = 0; 116954aa1f4dSChris Mason 1170a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 11710f7d52f4SChris Mason while (1) { 11725d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 11735d4f98a2SYan Zheng (void **)gang, 0, 11740f7d52f4SChris Mason ARRAY_SIZE(gang), 11750f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 11760f7d52f4SChris Mason if (ret == 0) 11770f7d52f4SChris Mason break; 11780f7d52f4SChris Mason for (i = 0; i < ret; i++) { 11790f7d52f4SChris Mason root = gang[i]; 11805d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 11812619ba1fSChris Mason (unsigned long)root->root_key.objectid, 11820f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1183a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 118431153d81SYan Zheng 1185e02119d5SChris Mason btrfs_free_log(trans, root); 11865d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1187d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1188e02119d5SChris Mason 118982d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 119082d5902dSLi Zefan 1191f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 119227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1193c7548af6SChris Mason smp_mb__after_atomic(); 1194f1ebcc74SLiu Bo 1195978d910dSYan Zheng if (root->commit_root != root->node) { 11969e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11979e351cc8SJosef Bacik &trans->transaction->switch_commits); 1198978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1199978d910dSYan Zheng root->node); 1200978d910dSYan Zheng } 120131153d81SYan Zheng 12025d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12030f7d52f4SChris Mason &root->root_key, 12040f7d52f4SChris Mason &root->root_item); 1205a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 120654aa1f4dSChris Mason if (err) 120754aa1f4dSChris Mason break; 12080f7d52f4SChris Mason } 12099f3a7427SChris Mason } 1210a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 121154aa1f4dSChris Mason return err; 12120f7d52f4SChris Mason } 12130f7d52f4SChris Mason 1214d352ac68SChris Mason /* 1215de78b51aSEric Sandeen * defrag a given btree. 1216de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1217d352ac68SChris Mason */ 1218de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1219e9d0b13bSChris Mason { 1220e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1221e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12228929ecfaSYan, Zheng int ret; 1223e9d0b13bSChris Mason 122427cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1225e9d0b13bSChris Mason return 0; 12268929ecfaSYan, Zheng 12276b80053dSChris Mason while (1) { 12288929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12298929ecfaSYan, Zheng if (IS_ERR(trans)) 12308929ecfaSYan, Zheng return PTR_ERR(trans); 12318929ecfaSYan, Zheng 1232de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12338929ecfaSYan, Zheng 1234e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1235b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1236e9d0b13bSChris Mason cond_resched(); 1237e9d0b13bSChris Mason 12387841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1239e9d0b13bSChris Mason break; 1240210549ebSDavid Sterba 1241210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1242efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1243210549ebSDavid Sterba ret = -EAGAIN; 1244210549ebSDavid Sterba break; 1245210549ebSDavid Sterba } 1246e9d0b13bSChris Mason } 124727cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12488929ecfaSYan, Zheng return ret; 1249e9d0b13bSChris Mason } 1250e9d0b13bSChris Mason 1251d352ac68SChris Mason /* 1252d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1253aec8030aSMiao Xie * transaction commit. This does the actual creation. 1254aec8030aSMiao Xie * 1255aec8030aSMiao Xie * Note: 1256aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1257aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1258aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1259d352ac68SChris Mason */ 126080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 12613063d29fSChris Mason struct btrfs_fs_info *fs_info, 12623063d29fSChris Mason struct btrfs_pending_snapshot *pending) 12633063d29fSChris Mason { 12643063d29fSChris Mason struct btrfs_key key; 126580b6794dSChris Mason struct btrfs_root_item *new_root_item; 12663063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 12673063d29fSChris Mason struct btrfs_root *root = pending->root; 12686bdb72deSSage Weil struct btrfs_root *parent_root; 126998c9942aSLiu Bo struct btrfs_block_rsv *rsv; 12706bdb72deSSage Weil struct inode *parent_inode; 127142874b3dSMiao Xie struct btrfs_path *path; 127242874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1273a22285a6SYan, Zheng struct dentry *dentry; 12743063d29fSChris Mason struct extent_buffer *tmp; 1275925baeddSChris Mason struct extent_buffer *old; 12768ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1277aec8030aSMiao Xie int ret = 0; 1278d68fc57bSYan, Zheng u64 to_reserve = 0; 12796bdb72deSSage Weil u64 index = 0; 1280a22285a6SYan, Zheng u64 objectid; 1281b83cc969SLi Zefan u64 root_flags; 12828ea05e3aSAlexander Block uuid_le new_uuid; 12833063d29fSChris Mason 128442874b3dSMiao Xie path = btrfs_alloc_path(); 128542874b3dSMiao Xie if (!path) { 1286aec8030aSMiao Xie pending->error = -ENOMEM; 1287aec8030aSMiao Xie return 0; 128842874b3dSMiao Xie } 128942874b3dSMiao Xie 129080b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 129180b6794dSChris Mason if (!new_root_item) { 1292aec8030aSMiao Xie pending->error = -ENOMEM; 12936fa9700eSMiao Xie goto root_item_alloc_fail; 129480b6794dSChris Mason } 1295a22285a6SYan, Zheng 1296aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1297aec8030aSMiao Xie if (pending->error) 12986fa9700eSMiao Xie goto no_free_objectid; 12993063d29fSChris Mason 1300d6726335SQu Wenruo /* 1301d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1302d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1303d6726335SQu Wenruo */ 1304d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1305d6726335SQu Wenruo 1306147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1307d68fc57bSYan, Zheng 1308d68fc57bSYan, Zheng if (to_reserve > 0) { 1309aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1310aec8030aSMiao Xie &pending->block_rsv, 131108e007d2SMiao Xie to_reserve, 131208e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1313aec8030aSMiao Xie if (pending->error) 1314d6726335SQu Wenruo goto clear_skip_qgroup; 1315d68fc57bSYan, Zheng } 1316d68fc57bSYan, Zheng 13173063d29fSChris Mason key.objectid = objectid; 1318a22285a6SYan, Zheng key.offset = (u64)-1; 1319a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 13203063d29fSChris Mason 13216fa9700eSMiao Xie rsv = trans->block_rsv; 1322a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 13232382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 13246bdb72deSSage Weil 1325a22285a6SYan, Zheng dentry = pending->dentry; 1326e9662f70SMiao Xie parent_inode = pending->dir; 1327a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 13287585717fSChris Mason record_root_in_trans(trans, parent_root); 1329a22285a6SYan, Zheng 13306bdb72deSSage Weil /* 13316bdb72deSSage Weil * insert the directory item 13326bdb72deSSage Weil */ 13336bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 133449b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 133542874b3dSMiao Xie 133642874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 133742874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 133842874b3dSMiao Xie btrfs_ino(parent_inode), 133942874b3dSMiao Xie dentry->d_name.name, 134042874b3dSMiao Xie dentry->d_name.len, 0); 134142874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1342fe66a05aSChris Mason pending->error = -EEXIST; 1343aec8030aSMiao Xie goto dir_item_existed; 134442874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 134542874b3dSMiao Xie ret = PTR_ERR(dir_item); 13468732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13478732d44fSMiao Xie goto fail; 134879787eaaSJeff Mahoney } 134942874b3dSMiao Xie btrfs_release_path(path); 13506bdb72deSSage Weil 1351e999376fSChris Mason /* 1352e999376fSChris Mason * pull in the delayed directory update 1353e999376fSChris Mason * and the delayed inode item 1354e999376fSChris Mason * otherwise we corrupt the FS during 1355e999376fSChris Mason * snapshot 1356e999376fSChris Mason */ 1357e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 13588732d44fSMiao Xie if (ret) { /* Transaction aborted */ 13598732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13608732d44fSMiao Xie goto fail; 13618732d44fSMiao Xie } 1362e999376fSChris Mason 13637585717fSChris Mason record_root_in_trans(trans, root); 13646bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 13656bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 136608fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 13676bdb72deSSage Weil 1368b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1369b83cc969SLi Zefan if (pending->readonly) 1370b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1371b83cc969SLi Zefan else 1372b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1373b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1374b83cc969SLi Zefan 13758ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 13768ea05e3aSAlexander Block trans->transid); 13778ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 13788ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 13798ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 13808ea05e3aSAlexander Block BTRFS_UUID_SIZE); 138170023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 138270023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 138370023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 13848ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 13858ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 13868ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 13878ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 138870023da2SStefan Behrens } 13893cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 13903cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 139170023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 13928ea05e3aSAlexander Block 1393925baeddSChris Mason old = btrfs_lock_root_node(root); 139449b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 139579787eaaSJeff Mahoney if (ret) { 139679787eaaSJeff Mahoney btrfs_tree_unlock(old); 139779787eaaSJeff Mahoney free_extent_buffer(old); 13988732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13998732d44fSMiao Xie goto fail; 140079787eaaSJeff Mahoney } 140149b25e05SJeff Mahoney 14025d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 14033063d29fSChris Mason 140449b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 140579787eaaSJeff Mahoney /* clean up in any case */ 1406925baeddSChris Mason btrfs_tree_unlock(old); 1407925baeddSChris Mason free_extent_buffer(old); 14088732d44fSMiao Xie if (ret) { 14098732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14108732d44fSMiao Xie goto fail; 14118732d44fSMiao Xie } 1412f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 141327cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1414f1ebcc74SLiu Bo smp_wmb(); 1415f1ebcc74SLiu Bo 14165d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1417a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1418a22285a6SYan, Zheng key.offset = trans->transid; 1419a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1420925baeddSChris Mason btrfs_tree_unlock(tmp); 14213063d29fSChris Mason free_extent_buffer(tmp); 14228732d44fSMiao Xie if (ret) { 14238732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14248732d44fSMiao Xie goto fail; 14258732d44fSMiao Xie } 14260660b5afSChris Mason 1427a22285a6SYan, Zheng /* 1428a22285a6SYan, Zheng * insert root back/forward references 1429a22285a6SYan, Zheng */ 1430a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1431a22285a6SYan, Zheng parent_root->root_key.objectid, 143233345d01SLi Zefan btrfs_ino(parent_inode), index, 1433a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14348732d44fSMiao Xie if (ret) { 14358732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14368732d44fSMiao Xie goto fail; 14378732d44fSMiao Xie } 1438a22285a6SYan, Zheng 1439a22285a6SYan, Zheng key.offset = (u64)-1; 1440a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 144179787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 144279787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 14438732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14448732d44fSMiao Xie goto fail; 144579787eaaSJeff Mahoney } 1446d68fc57bSYan, Zheng 144749b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 14488732d44fSMiao Xie if (ret) { 14498732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14508732d44fSMiao Xie goto fail; 14518732d44fSMiao Xie } 1452361048f5SMiao Xie 1453361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 14548732d44fSMiao Xie if (ret) { 14558732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14568732d44fSMiao Xie goto fail; 14578732d44fSMiao Xie } 145842874b3dSMiao Xie 145942874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 146042874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 146142874b3dSMiao Xie parent_inode, &key, 146242874b3dSMiao Xie BTRFS_FT_DIR, index); 146342874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 14649c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 14658732d44fSMiao Xie if (ret) { 14668732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14678732d44fSMiao Xie goto fail; 14688732d44fSMiao Xie } 146942874b3dSMiao Xie 147042874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 147142874b3dSMiao Xie dentry->d_name.len * 2); 147242874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1473be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1474dd5f9615SStefan Behrens if (ret) { 14758732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1476dd5f9615SStefan Behrens goto fail; 1477dd5f9615SStefan Behrens } 1478dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1479dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1480dd5f9615SStefan Behrens if (ret) { 1481dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1482dd5f9615SStefan Behrens goto fail; 1483dd5f9615SStefan Behrens } 1484dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1485dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1486dd5f9615SStefan Behrens new_root_item->received_uuid, 1487dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1488dd5f9615SStefan Behrens objectid); 1489dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1490dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1491dd5f9615SStefan Behrens goto fail; 1492dd5f9615SStefan Behrens } 1493dd5f9615SStefan Behrens } 1494d6726335SQu Wenruo 1495d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1496d6726335SQu Wenruo if (ret) { 1497d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1498d6726335SQu Wenruo goto fail; 1499d6726335SQu Wenruo } 1500d6726335SQu Wenruo 1501d6726335SQu Wenruo /* 1502d6726335SQu Wenruo * account qgroup counters before qgroup_inherit() 1503d6726335SQu Wenruo */ 1504d6726335SQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 1505d6726335SQu Wenruo if (ret) 1506d6726335SQu Wenruo goto fail; 1507d6726335SQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 1508d6726335SQu Wenruo if (ret) 1509d6726335SQu Wenruo goto fail; 1510d6726335SQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 1511d6726335SQu Wenruo root->root_key.objectid, 1512d6726335SQu Wenruo objectid, pending->inherit); 1513d6726335SQu Wenruo if (ret) { 1514d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1515d6726335SQu Wenruo goto fail; 1516d6726335SQu Wenruo } 1517d6726335SQu Wenruo 15183063d29fSChris Mason fail: 1519aec8030aSMiao Xie pending->error = ret; 1520aec8030aSMiao Xie dir_item_existed: 152198c9942aSLiu Bo trans->block_rsv = rsv; 15222382c5ccSLiu Bo trans->bytes_reserved = 0; 1523d6726335SQu Wenruo clear_skip_qgroup: 1524d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 15256fa9700eSMiao Xie no_free_objectid: 15266fa9700eSMiao Xie kfree(new_root_item); 15276fa9700eSMiao Xie root_item_alloc_fail: 152842874b3dSMiao Xie btrfs_free_path(path); 152949b25e05SJeff Mahoney return ret; 15303063d29fSChris Mason } 15313063d29fSChris Mason 1532d352ac68SChris Mason /* 1533d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1534d352ac68SChris Mason */ 153580b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 15363063d29fSChris Mason struct btrfs_fs_info *fs_info) 15373063d29fSChris Mason { 1538aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 15393063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1540aec8030aSMiao Xie int ret = 0; 15413de4586cSChris Mason 1542aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1543aec8030aSMiao Xie list_del(&pending->list); 1544aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1545aec8030aSMiao Xie if (ret) 1546aec8030aSMiao Xie break; 1547aec8030aSMiao Xie } 1548aec8030aSMiao Xie return ret; 15493de4586cSChris Mason } 15503de4586cSChris Mason 15515d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 15525d4f98a2SYan Zheng { 15535d4f98a2SYan Zheng struct btrfs_root_item *root_item; 15545d4f98a2SYan Zheng struct btrfs_super_block *super; 15555d4f98a2SYan Zheng 15566c41761fSDavid Sterba super = root->fs_info->super_copy; 15575d4f98a2SYan Zheng 15585d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 15595d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 15605d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 15615d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 15625d4f98a2SYan Zheng 15635d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 15645d4f98a2SYan Zheng super->root = root_item->bytenr; 15655d4f98a2SYan Zheng super->generation = root_item->generation; 15665d4f98a2SYan Zheng super->root_level = root_item->level; 156773bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 15680af3d00bSJosef Bacik super->cache_generation = root_item->generation; 156970f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 157026432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 15715d4f98a2SYan Zheng } 15725d4f98a2SYan Zheng 1573f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1574f36f3042SChris Mason { 15754a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1576f36f3042SChris Mason int ret = 0; 15774a9d8bdeSMiao Xie 1578a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 15794a9d8bdeSMiao Xie trans = info->running_transaction; 15804a9d8bdeSMiao Xie if (trans) 15814a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1582a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1583f36f3042SChris Mason return ret; 1584f36f3042SChris Mason } 1585f36f3042SChris Mason 15868929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 15878929ecfaSYan, Zheng { 15884a9d8bdeSMiao Xie struct btrfs_transaction *trans; 15898929ecfaSYan, Zheng int ret = 0; 15904a9d8bdeSMiao Xie 1591a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 15924a9d8bdeSMiao Xie trans = info->running_transaction; 15934a9d8bdeSMiao Xie if (trans) 15944a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1595a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 15968929ecfaSYan, Zheng return ret; 15978929ecfaSYan, Zheng } 15988929ecfaSYan, Zheng 1599bb9c12c9SSage Weil /* 1600bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1601bb9c12c9SSage Weil * transaction joins 1602bb9c12c9SSage Weil */ 1603bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1604bb9c12c9SSage Weil struct btrfs_transaction *trans) 1605bb9c12c9SSage Weil { 16064a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1607501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1608501407aaSJosef Bacik trans->aborted); 1609bb9c12c9SSage Weil } 1610bb9c12c9SSage Weil 1611bb9c12c9SSage Weil /* 1612bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1613bb9c12c9SSage Weil * caller holds ref. 1614bb9c12c9SSage Weil */ 1615bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1616bb9c12c9SSage Weil struct btrfs_transaction *trans) 1617bb9c12c9SSage Weil { 161872d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1619501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1620501407aaSJosef Bacik trans->aborted); 1621bb9c12c9SSage Weil } 1622bb9c12c9SSage Weil 1623bb9c12c9SSage Weil /* 1624bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1625bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1626bb9c12c9SSage Weil */ 1627bb9c12c9SSage Weil struct btrfs_async_commit { 1628bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1629bb9c12c9SSage Weil struct btrfs_root *root; 16307892b5afSMiao Xie struct work_struct work; 1631bb9c12c9SSage Weil }; 1632bb9c12c9SSage Weil 1633bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1634bb9c12c9SSage Weil { 1635bb9c12c9SSage Weil struct btrfs_async_commit *ac = 16367892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1637bb9c12c9SSage Weil 16386fc4e354SSage Weil /* 16396fc4e354SSage Weil * We've got freeze protection passed with the transaction. 16406fc4e354SSage Weil * Tell lockdep about it. 16416fc4e354SSage Weil */ 1642b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 16436fc4e354SSage Weil rwsem_acquire_read( 16446fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 16456fc4e354SSage Weil 0, 1, _THIS_IP_); 16466fc4e354SSage Weil 1647e209db7aSSage Weil current->journal_info = ac->newtrans; 1648e209db7aSSage Weil 1649bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1650bb9c12c9SSage Weil kfree(ac); 1651bb9c12c9SSage Weil } 1652bb9c12c9SSage Weil 1653bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1654bb9c12c9SSage Weil struct btrfs_root *root, 1655bb9c12c9SSage Weil int wait_for_unblock) 1656bb9c12c9SSage Weil { 1657bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1658bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1659bb9c12c9SSage Weil 1660bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1661db5b493aSTsutomu Itoh if (!ac) 1662db5b493aSTsutomu Itoh return -ENOMEM; 1663bb9c12c9SSage Weil 16647892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1665bb9c12c9SSage Weil ac->root = root; 16667a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 16673612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 16683612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 16693612b495STsutomu Itoh kfree(ac); 16703612b495STsutomu Itoh return err; 16713612b495STsutomu Itoh } 1672bb9c12c9SSage Weil 1673bb9c12c9SSage Weil /* take transaction reference */ 1674bb9c12c9SSage Weil cur_trans = trans->transaction; 167513c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1676bb9c12c9SSage Weil 1677bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 16786fc4e354SSage Weil 16796fc4e354SSage Weil /* 16806fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 16816fc4e354SSage Weil * async commit thread will be the one to unlock it. 16826fc4e354SSage Weil */ 1683b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1684ff7c1d33SMiao Xie rwsem_release( 1685ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 16866fc4e354SSage Weil 1, _THIS_IP_); 16876fc4e354SSage Weil 16887892b5afSMiao Xie schedule_work(&ac->work); 1689bb9c12c9SSage Weil 1690bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1691bb9c12c9SSage Weil if (wait_for_unblock) 1692bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1693bb9c12c9SSage Weil else 1694bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1695bb9c12c9SSage Weil 169638e88054SSage Weil if (current->journal_info == trans) 169738e88054SSage Weil current->journal_info = NULL; 169838e88054SSage Weil 1699724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1700bb9c12c9SSage Weil return 0; 1701bb9c12c9SSage Weil } 1702bb9c12c9SSage Weil 170349b25e05SJeff Mahoney 170449b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 17057b8b92afSJosef Bacik struct btrfs_root *root, int err) 170649b25e05SJeff Mahoney { 170749b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1708f094ac32SLiu Bo DEFINE_WAIT(wait); 170949b25e05SJeff Mahoney 171049b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 171149b25e05SJeff Mahoney 17127b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 17137b8b92afSJosef Bacik 171449b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 171566b6135bSLiu Bo 171625d8c284SMiao Xie /* 171725d8c284SMiao Xie * If the transaction is removed from the list, it means this 171825d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 171925d8c284SMiao Xie * to call the cleanup function. 172025d8c284SMiao Xie */ 172125d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 172266b6135bSLiu Bo 172349b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1724d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 17254a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1726f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1727f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1728f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1729f094ac32SLiu Bo 1730f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1731d7096fc3SJosef Bacik } 173249b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 173349b25e05SJeff Mahoney 173449b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 173549b25e05SJeff Mahoney 17364a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17374a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 17384a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 17394a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 17404a9d8bdeSMiao Xie 1741e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1742e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1743724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1744724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 174549b25e05SJeff Mahoney 174649b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 174749b25e05SJeff Mahoney 174849b25e05SJeff Mahoney if (current->journal_info == trans) 174949b25e05SJeff Mahoney current->journal_info = NULL; 1750c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 175149b25e05SJeff Mahoney 175249b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 175349b25e05SJeff Mahoney } 175449b25e05SJeff Mahoney 175582436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 175682436617SMiao Xie { 175782436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 17586c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 175982436617SMiao Xie return 0; 176082436617SMiao Xie } 176182436617SMiao Xie 176282436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 176382436617SMiao Xie { 176482436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1765b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 176682436617SMiao Xie } 176782436617SMiao Xie 176850d9aa99SJosef Bacik static inline void 176950d9aa99SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans, 177050d9aa99SJosef Bacik struct btrfs_fs_info *fs_info) 177150d9aa99SJosef Bacik { 177250d9aa99SJosef Bacik struct btrfs_ordered_extent *ordered; 177350d9aa99SJosef Bacik 177450d9aa99SJosef Bacik spin_lock(&fs_info->trans_lock); 177550d9aa99SJosef Bacik while (!list_empty(&cur_trans->pending_ordered)) { 177650d9aa99SJosef Bacik ordered = list_first_entry(&cur_trans->pending_ordered, 177750d9aa99SJosef Bacik struct btrfs_ordered_extent, 177850d9aa99SJosef Bacik trans_list); 177950d9aa99SJosef Bacik list_del_init(&ordered->trans_list); 178050d9aa99SJosef Bacik spin_unlock(&fs_info->trans_lock); 178150d9aa99SJosef Bacik 178250d9aa99SJosef Bacik wait_event(ordered->wait, test_bit(BTRFS_ORDERED_COMPLETE, 178350d9aa99SJosef Bacik &ordered->flags)); 178450d9aa99SJosef Bacik btrfs_put_ordered_extent(ordered); 178550d9aa99SJosef Bacik spin_lock(&fs_info->trans_lock); 178650d9aa99SJosef Bacik } 178750d9aa99SJosef Bacik spin_unlock(&fs_info->trans_lock); 178850d9aa99SJosef Bacik } 178950d9aa99SJosef Bacik 179079154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 179179154b1bSChris Mason struct btrfs_root *root) 179279154b1bSChris Mason { 179349b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 17948fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1795656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 179625287e0aSMiao Xie int ret; 179779154b1bSChris Mason 17988d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 17998d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 180025287e0aSMiao Xie ret = cur_trans->aborted; 1801e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1802e4a2bcacSJosef Bacik return ret; 180325287e0aSMiao Xie } 180449b25e05SJeff Mahoney 180556bec294SChris Mason /* make a pass through all the delayed refs we have so far 180656bec294SChris Mason * any runnings procs may add more while we are here 180756bec294SChris Mason */ 180856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1809e4a2bcacSJosef Bacik if (ret) { 1810e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1811e4a2bcacSJosef Bacik return ret; 1812e4a2bcacSJosef Bacik } 181356bec294SChris Mason 18140e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18150e721106SJosef Bacik trans->block_rsv = NULL; 1816272d26d0SMiao Xie if (trans->qgroup_reserved) { 1817272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1818272d26d0SMiao Xie trans->qgroup_reserved = 0; 1819272d26d0SMiao Xie } 18200e721106SJosef Bacik 1821b7ec40d7SChris Mason cur_trans = trans->transaction; 182249b25e05SJeff Mahoney 182356bec294SChris Mason /* 182456bec294SChris Mason * set the flushing flag so procs in this transaction have to 182556bec294SChris Mason * start sending their work down. 182656bec294SChris Mason */ 1827b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 18281be41b78SJosef Bacik smp_wmb(); 182956bec294SChris Mason 1830ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1831ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1832ea658badSJosef Bacik 1833c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1834e4a2bcacSJosef Bacik if (ret) { 1835e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1836e4a2bcacSJosef Bacik return ret; 1837e4a2bcacSJosef Bacik } 183856bec294SChris Mason 18391bbc621eSChris Mason if (!cur_trans->dirty_bg_run) { 18401bbc621eSChris Mason int run_it = 0; 18411bbc621eSChris Mason 18421bbc621eSChris Mason /* this mutex is also taken before trying to set 18431bbc621eSChris Mason * block groups readonly. We need to make sure 18441bbc621eSChris Mason * that nobody has set a block group readonly 18451bbc621eSChris Mason * after a extents from that block group have been 18461bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 18471bbc621eSChris Mason * will wait for the transaction to commit if it 18481bbc621eSChris Mason * finds dirty_bg_run = 1 18491bbc621eSChris Mason * 18501bbc621eSChris Mason * The dirty_bg_run flag is also used to make sure only 18511bbc621eSChris Mason * one process starts all the block group IO. It wouldn't 18521bbc621eSChris Mason * hurt to have more than one go through, but there's no 18531bbc621eSChris Mason * real advantage to it either. 18541bbc621eSChris Mason */ 18551bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 18561bbc621eSChris Mason if (!cur_trans->dirty_bg_run) { 18571bbc621eSChris Mason run_it = 1; 18581bbc621eSChris Mason cur_trans->dirty_bg_run = 1; 18591bbc621eSChris Mason } 18601bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 18611bbc621eSChris Mason 18621bbc621eSChris Mason if (run_it) 18631bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 18641bbc621eSChris Mason } 18651bbc621eSChris Mason if (ret) { 18661bbc621eSChris Mason btrfs_end_transaction(trans, root); 18671bbc621eSChris Mason return ret; 18681bbc621eSChris Mason } 18691bbc621eSChris Mason 18704a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 1871d3efe084SFilipe Manana list_splice_init(&trans->ordered, &cur_trans->pending_ordered); 18724a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 18734a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 187413c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 187549b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1876ccd467d6SChris Mason 1877b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 187815ee9bc7SJosef Bacik 1879b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1880b4924a0fSLiu Bo ret = cur_trans->aborted; 1881b4924a0fSLiu Bo 1882724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 188315ee9bc7SJosef Bacik 188449b25e05SJeff Mahoney return ret; 188579154b1bSChris Mason } 18864313b399SChris Mason 18874a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1888bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1889bb9c12c9SSage Weil 1890ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1891ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1892ccd467d6SChris Mason struct btrfs_transaction, list); 18934a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 189413c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1895a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1896ccd467d6SChris Mason 1897ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1898*1f9b8c8fSFilipe Manana ret = prev_trans->aborted; 1899ccd467d6SChris Mason 1900724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 1901*1f9b8c8fSFilipe Manana if (ret) 1902*1f9b8c8fSFilipe Manana goto cleanup_transaction; 1903a4abeea4SJosef Bacik } else { 1904a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1905ccd467d6SChris Mason } 1906a4abeea4SJosef Bacik } else { 1907a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1908ccd467d6SChris Mason } 190915ee9bc7SJosef Bacik 19100860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 19110860adfdSMiao Xie 191282436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 191382436617SMiao Xie if (ret) 191482436617SMiao Xie goto cleanup_transaction; 191582436617SMiao Xie 19168d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 191749b25e05SJeff Mahoney if (ret) 191849b25e05SJeff Mahoney goto cleanup_transaction; 191916cdcec7SMiao Xie 1920581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1921581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1922ed3b3d31SChris Mason 1923581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 19248d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1925ca469637SMiao Xie if (ret) 1926ca469637SMiao Xie goto cleanup_transaction; 1927ca469637SMiao Xie 192882436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1929cb7ab021SWang Shilong 193050d9aa99SJosef Bacik btrfs_wait_pending_ordered(cur_trans, root->fs_info); 193150d9aa99SJosef Bacik 1932cb7ab021SWang Shilong btrfs_scrub_pause(root); 1933ed0ca140SJosef Bacik /* 1934ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1935ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 19364a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1937ed0ca140SJosef Bacik */ 1938a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19394a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1940a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1941ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1942ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 194315ee9bc7SJosef Bacik 19442cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 19452cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19462cba30f1SMiao Xie ret = cur_trans->aborted; 19476cf7f77eSWang Shilong goto scrub_continue; 19482cba30f1SMiao Xie } 19497585717fSChris Mason /* 19507585717fSChris Mason * the reloc mutex makes sure that we stop 19517585717fSChris Mason * the balancing code from coming in and moving 19527585717fSChris Mason * extents around in the middle of the commit 19537585717fSChris Mason */ 19547585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 19557585717fSChris Mason 195642874b3dSMiao Xie /* 195742874b3dSMiao Xie * We needn't worry about the delayed items because we will 195842874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 195942874b3dSMiao Xie * core function of the snapshot creation. 196042874b3dSMiao Xie */ 196142874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 196249b25e05SJeff Mahoney if (ret) { 196349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19646cf7f77eSWang Shilong goto scrub_continue; 196549b25e05SJeff Mahoney } 19663063d29fSChris Mason 196742874b3dSMiao Xie /* 196842874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 196942874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 197042874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 197142874b3dSMiao Xie * them. 197242874b3dSMiao Xie * 197342874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 197442874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 197542874b3dSMiao Xie * the nodes and leaves. 197642874b3dSMiao Xie */ 197742874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 197849b25e05SJeff Mahoney if (ret) { 197949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19806cf7f77eSWang Shilong goto scrub_continue; 198149b25e05SJeff Mahoney } 198216cdcec7SMiao Xie 198356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 198449b25e05SJeff Mahoney if (ret) { 198549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19866cf7f77eSWang Shilong goto scrub_continue; 198749b25e05SJeff Mahoney } 198856bec294SChris Mason 19890ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 19900ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 19910ed4792aSQu Wenruo if (ret) { 19920ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 19930ed4792aSQu Wenruo goto scrub_continue; 19940ed4792aSQu Wenruo } 19950ed4792aSQu Wenruo 1996e999376fSChris Mason /* 1997e999376fSChris Mason * make sure none of the code above managed to slip in a 1998e999376fSChris Mason * delayed item 1999e999376fSChris Mason */ 2000e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 2001e999376fSChris Mason 20022c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2003dc17ff8fSChris Mason 2004e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2005e02119d5SChris Mason * various roots consistent with each other. Every pointer 2006e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2007e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2008e02119d5SChris Mason * the tree logging code from jumping in and changing any 2009e02119d5SChris Mason * of the trees. 2010e02119d5SChris Mason * 2011e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2012e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2013e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2014e02119d5SChris Mason * from now until after the super is written, we avoid races 2015e02119d5SChris Mason * with the tree-log code. 2016e02119d5SChris Mason */ 2017e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 20181a40e23bSZheng Yan 20195d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 202049b25e05SJeff Mahoney if (ret) { 202149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2022871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20236cf7f77eSWang Shilong goto scrub_continue; 202449b25e05SJeff Mahoney } 202554aa1f4dSChris Mason 20263818aea2SQu Wenruo /* 20277e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 20287e1876acSDavid Sterba * before the next transaction. 20293818aea2SQu Wenruo */ 2030572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 20313818aea2SQu Wenruo 20325d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2033e02119d5SChris Mason * safe to free the root of tree log roots 2034e02119d5SChris Mason */ 2035e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2036e02119d5SChris Mason 20370ed4792aSQu Wenruo /* 20380ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 20390ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 20400ed4792aSQu Wenruo */ 20410ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 20420ed4792aSQu Wenruo if (ret < 0) { 20430ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 20440ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20450ed4792aSQu Wenruo goto scrub_continue; 20460ed4792aSQu Wenruo } 20470ed4792aSQu Wenruo 20485d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 204949b25e05SJeff Mahoney if (ret) { 205049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2051871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20526cf7f77eSWang Shilong goto scrub_continue; 205349b25e05SJeff Mahoney } 205454aa1f4dSChris Mason 20552cba30f1SMiao Xie /* 20562cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 20572cba30f1SMiao Xie * update ->aborted, check it. 20582cba30f1SMiao Xie */ 20592cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20602cba30f1SMiao Xie ret = cur_trans->aborted; 20612cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 20622cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 20636cf7f77eSWang Shilong goto scrub_continue; 20642cba30f1SMiao Xie } 20652cba30f1SMiao Xie 206611833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 206711833d66SYan Zheng 206878fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 20695f39d397SChris Mason 20705d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 20715d4f98a2SYan Zheng root->fs_info->tree_root->node); 20729e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 20739e351cc8SJosef Bacik &cur_trans->switch_commits); 20745d4f98a2SYan Zheng 20755d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 20765d4f98a2SYan Zheng root->fs_info->chunk_root->node); 20779e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 20789e351cc8SJosef Bacik &cur_trans->switch_commits); 20799e351cc8SJosef Bacik 20809e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 20815d4f98a2SYan Zheng 2082edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2083ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 20841bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 20855d4f98a2SYan Zheng update_super_roots(root); 2086e02119d5SChris Mason 20876c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 20886c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 20896c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 20906c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2091ccd467d6SChris Mason 2092935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2093ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2094935e5cc9SMiao Xie 2095656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2096656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2097656f30dbSFilipe Manana 20984fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 20994fbcdf66SFilipe Manana 2100a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 21014a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2102a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2103a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 21047585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2105b7ec40d7SChris Mason 2106f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2107e6dcd2dcSChris Mason 210879154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 210949b25e05SJeff Mahoney if (ret) { 211049b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 211108748810SDavid Sterba "Error while writing out transaction"); 211249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21136cf7f77eSWang Shilong goto scrub_continue; 211449b25e05SJeff Mahoney } 211549b25e05SJeff Mahoney 211649b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 211749b25e05SJeff Mahoney if (ret) { 211849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21196cf7f77eSWang Shilong goto scrub_continue; 212049b25e05SJeff Mahoney } 21214313b399SChris Mason 2122e02119d5SChris Mason /* 2123e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2124e02119d5SChris Mason * to go about their business 2125e02119d5SChris Mason */ 2126e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2127e02119d5SChris Mason 212811833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 21294313b399SChris Mason 213013212b54SZhao Lei if (cur_trans->have_free_bgs) 213113212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 213213212b54SZhao Lei 213315ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 21344a9d8bdeSMiao Xie /* 21354a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 21364a9d8bdeSMiao Xie * which can change it. 21374a9d8bdeSMiao Xie */ 21384a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 21392c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 21403de4586cSChris Mason 2141a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 214213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2143a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2144a4abeea4SJosef Bacik 2145724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2146724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 214758176a96SJosef Bacik 21480860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2149b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2150b2b5ef5cSJan Kara 21511abe9b8aSliubo trace_btrfs_transaction_commit(root); 21521abe9b8aSliubo 2153a2de733cSArne Jansen btrfs_scrub_continue(root); 2154a2de733cSArne Jansen 21559ed74f2dSJosef Bacik if (current->journal_info == trans) 21569ed74f2dSJosef Bacik current->journal_info = NULL; 21579ed74f2dSJosef Bacik 21582c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 215924bbcf04SYan, Zheng 21608a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 21618a733013SZhao Lei current != root->fs_info->cleaner_kthread) 216224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 216324bbcf04SYan, Zheng 216479154b1bSChris Mason return ret; 216549b25e05SJeff Mahoney 21666cf7f77eSWang Shilong scrub_continue: 21676cf7f77eSWang Shilong btrfs_scrub_continue(root); 216849b25e05SJeff Mahoney cleanup_transaction: 21690e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 21704fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 21710e721106SJosef Bacik trans->block_rsv = NULL; 2172272d26d0SMiao Xie if (trans->qgroup_reserved) { 2173272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 2174272d26d0SMiao Xie trans->qgroup_reserved = 0; 2175272d26d0SMiao Xie } 2176c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 217749b25e05SJeff Mahoney if (current->journal_info == trans) 217849b25e05SJeff Mahoney current->journal_info = NULL; 21797b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 218049b25e05SJeff Mahoney 218149b25e05SJeff Mahoney return ret; 218279154b1bSChris Mason } 218379154b1bSChris Mason 2184d352ac68SChris Mason /* 21859d1a2a3aSDavid Sterba * return < 0 if error 21869d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 21879d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 21889d1a2a3aSDavid Sterba * 21899d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 21909d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 21919d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 21929d1a2a3aSDavid Sterba * few seconds later. 2193d352ac68SChris Mason */ 21949d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2195e9d0b13bSChris Mason { 21969d1a2a3aSDavid Sterba int ret; 21975d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2198e9d0b13bSChris Mason 2199a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 22009d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 22019d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 22029d1a2a3aSDavid Sterba return 0; 22039d1a2a3aSDavid Sterba } 22049d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 22059d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2206cfad392bSJosef Bacik list_del_init(&root->root_list); 2207a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 22085d4f98a2SYan Zheng 2209efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 221076dda93cSYan, Zheng 221116cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 221216cdcec7SMiao Xie 221376dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 221476dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 22152c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 221676dda93cSYan, Zheng else 22172c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 221832471dc2SDavid Sterba 22196596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2220e9d0b13bSChris Mason } 2221572d9ab7SDavid Sterba 2222572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2223572d9ab7SDavid Sterba { 2224572d9ab7SDavid Sterba unsigned long prev; 2225572d9ab7SDavid Sterba unsigned long bit; 2226572d9ab7SDavid Sterba 22276c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2228572d9ab7SDavid Sterba if (!prev) 2229572d9ab7SDavid Sterba return; 2230572d9ab7SDavid Sterba 22317e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 22327e1876acSDavid Sterba if (prev & bit) 22337e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22347e1876acSDavid Sterba prev &= ~bit; 22357e1876acSDavid Sterba 22367e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 22377e1876acSDavid Sterba if (prev & bit) 22387e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22397e1876acSDavid Sterba prev &= ~bit; 22407e1876acSDavid Sterba 2241d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2242d51033d0SDavid Sterba if (prev & bit) 2243d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2244d51033d0SDavid Sterba prev &= ~bit; 2245d51033d0SDavid Sterba 2246572d9ab7SDavid Sterba if (prev) 2247572d9ab7SDavid Sterba btrfs_warn(fs_info, 2248572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2249572d9ab7SDavid Sterba } 2250