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 } 787785a663SFilipe Manana /* 797785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 807785a663SFilipe Manana * because the transaction was aborted and a commit did not 817785a663SFilipe Manana * happen (things failed before writing the new superblock 827785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 837785a663SFilipe Manana * discard the physical locations of the block groups. 847785a663SFilipe Manana */ 857785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 867785a663SFilipe Manana struct btrfs_block_group_cache *cache; 877785a663SFilipe Manana 887785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 897785a663SFilipe Manana struct btrfs_block_group_cache, 907785a663SFilipe Manana bg_list); 917785a663SFilipe Manana list_del_init(&cache->bg_list); 927785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 937785a663SFilipe Manana btrfs_put_block_group(cache); 947785a663SFilipe Manana } 952c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 9679154b1bSChris Mason } 9778fae27eSChris Mason } 9879154b1bSChris Mason 99663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 100663dfbb0SFilipe Manana { 101663dfbb0SFilipe Manana spin_lock(&tree->lock); 102b666a9cdSDavid Sterba /* 103b666a9cdSDavid Sterba * Do a single barrier for the waitqueue_active check here, the state 104b666a9cdSDavid Sterba * of the waitqueue should not change once clear_btree_io_tree is 105b666a9cdSDavid Sterba * called. 106b666a9cdSDavid Sterba */ 107b666a9cdSDavid Sterba smp_mb(); 108663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 109663dfbb0SFilipe Manana struct rb_node *node; 110663dfbb0SFilipe Manana struct extent_state *state; 111663dfbb0SFilipe Manana 112663dfbb0SFilipe Manana node = rb_first(&tree->state); 113663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 114663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 115663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 116663dfbb0SFilipe Manana /* 117663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 118663dfbb0SFilipe Manana * changes in the flags of extent states ever. 119663dfbb0SFilipe Manana */ 120663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 121663dfbb0SFilipe Manana free_extent_state(state); 122351810c1SDavid Sterba 123351810c1SDavid Sterba cond_resched_lock(&tree->lock); 124663dfbb0SFilipe Manana } 125663dfbb0SFilipe Manana spin_unlock(&tree->lock); 126663dfbb0SFilipe Manana } 127663dfbb0SFilipe Manana 1289e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 1299e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 130817d52f8SJosef Bacik { 1319e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1329e351cc8SJosef Bacik 1339e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1349e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1359e351cc8SJosef Bacik dirty_list) { 1369e351cc8SJosef Bacik list_del_init(&root->dirty_list); 137817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 138817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1399e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1409e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 141663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1429e351cc8SJosef Bacik } 1432b9dbef2SJosef Bacik 1442b9dbef2SJosef Bacik /* We can free old roots now. */ 1452b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1462b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1472b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1482b9dbef2SJosef Bacik struct btrfs_root, root_list); 1492b9dbef2SJosef Bacik list_del_init(&root->root_list); 1502b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1512b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1522b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1532b9dbef2SJosef Bacik } 1542b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1559e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 156817d52f8SJosef Bacik } 157817d52f8SJosef Bacik 1580860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1590860adfdSMiao Xie unsigned int type) 1600860adfdSMiao Xie { 1610860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1620860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1630860adfdSMiao Xie } 1640860adfdSMiao Xie 1650860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1660860adfdSMiao Xie unsigned int type) 1670860adfdSMiao Xie { 1680860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1690860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1700860adfdSMiao Xie } 1710860adfdSMiao Xie 1720860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1730860adfdSMiao Xie unsigned int type) 1740860adfdSMiao Xie { 1750860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1760860adfdSMiao Xie } 1770860adfdSMiao Xie 1780860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1790860adfdSMiao Xie { 1800860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 181178260b2SMiao Xie } 182178260b2SMiao Xie 183d352ac68SChris Mason /* 184d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 185d352ac68SChris Mason */ 1860860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 18779154b1bSChris Mason { 18879154b1bSChris Mason struct btrfs_transaction *cur_trans; 18919ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 190a4abeea4SJosef Bacik 19119ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 192d43317dcSChris Mason loop: 19349b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 19487533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 19519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 19649b25e05SJeff Mahoney return -EROFS; 19749b25e05SJeff Mahoney } 19849b25e05SJeff Mahoney 19919ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 200a4abeea4SJosef Bacik if (cur_trans) { 201871383beSDavid Sterba if (cur_trans->aborted) { 20219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 20349b25e05SJeff Mahoney return cur_trans->aborted; 204871383beSDavid Sterba } 2054a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 206178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 207178260b2SMiao Xie return -EBUSY; 208178260b2SMiao Xie } 209a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 210a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 2110860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 21219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 213a4abeea4SJosef Bacik return 0; 214a4abeea4SJosef Bacik } 21519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 216a4abeea4SJosef Bacik 217354aa0fbSMiao Xie /* 218354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 219354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 220354aa0fbSMiao Xie */ 221354aa0fbSMiao Xie if (type == TRANS_ATTACH) 222354aa0fbSMiao Xie return -ENOENT; 223354aa0fbSMiao Xie 2244a9d8bdeSMiao Xie /* 2254a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 2264a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 2274a9d8bdeSMiao Xie */ 2284a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2294a9d8bdeSMiao Xie 230a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 231db5b493aSTsutomu Itoh if (!cur_trans) 232db5b493aSTsutomu Itoh return -ENOMEM; 233d43317dcSChris Mason 23419ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 23519ae4e81SJan Schmidt if (fs_info->running_transaction) { 236d43317dcSChris Mason /* 237d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2384a9d8bdeSMiao Xie * to redo the checks above 239d43317dcSChris Mason */ 240a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 241d43317dcSChris Mason goto loop; 24287533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 243e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2447b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 2457b8b92afSJosef Bacik return -EROFS; 246a4abeea4SJosef Bacik } 247d43317dcSChris Mason 24813c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2490860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 25079154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 25179154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 252161c3549SJosef Bacik init_waitqueue_head(&cur_trans->pending_wait); 2534a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 254a4abeea4SJosef Bacik /* 255a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 256a4abeea4SJosef Bacik * commit the transaction. 257a4abeea4SJosef Bacik */ 258a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 259161c3549SJosef Bacik atomic_set(&cur_trans->pending_ordered, 0); 2603204d33cSJosef Bacik cur_trans->flags = 0; 26108607c1bSChris Mason cur_trans->start_time = get_seconds(); 26256bec294SChris Mason 263a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 264a099d0fdSAlexandru Moise 265c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2663368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 267d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 26820b297d6SJan Schmidt 26920b297d6SJan Schmidt /* 27020b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 27120b297d6SJan Schmidt * the log must never go across transaction boundaries. 27220b297d6SJan Schmidt */ 27320b297d6SJan Schmidt smp_mb(); 27431b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 275efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 27620b297d6SJan Schmidt "creating a fresh transaction\n"); 27731b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 278efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 27920b297d6SJan Schmidt "creating a fresh transaction\n"); 280fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 28120b297d6SJan Schmidt 28256bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 28356bec294SChris Mason 2843063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2856df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2869e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 287ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2881bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2892b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2901bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 291cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 292ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 293e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2942b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 29519ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 296d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 29719ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 29819ae4e81SJan Schmidt fs_info->generation++; 29919ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 30019ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 30149b25e05SJeff Mahoney cur_trans->aborted = 0; 30219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 30315ee9bc7SJosef Bacik 30479154b1bSChris Mason return 0; 30579154b1bSChris Mason } 30679154b1bSChris Mason 307d352ac68SChris Mason /* 308d397712bSChris Mason * this does all the record keeping required to make sure that a reference 309d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 310d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 311d397712bSChris Mason * when the transaction commits 312d352ac68SChris Mason */ 3137585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 3145d4f98a2SYan Zheng struct btrfs_root *root) 3156702ed49SChris Mason { 31627cdeb70SMiao Xie if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 31727cdeb70SMiao Xie root->last_trans < trans->transid) { 3186702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 3195d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 3205d4f98a2SYan Zheng 3217585717fSChris Mason /* 32227cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 3237585717fSChris Mason * we have the reloc mutex held now, so there 3247585717fSChris Mason * is only one writer in this function 3257585717fSChris Mason */ 32627cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3277585717fSChris Mason 32827cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 3297585717fSChris Mason * they find our root->last_trans update 3307585717fSChris Mason */ 3317585717fSChris Mason smp_wmb(); 3327585717fSChris Mason 333a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 334a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 335a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 336a4abeea4SJosef Bacik return 0; 337a4abeea4SJosef Bacik } 3386702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 3396702ed49SChris Mason (unsigned long)root->root_key.objectid, 3406702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 341a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3427585717fSChris Mason root->last_trans = trans->transid; 3437585717fSChris Mason 3447585717fSChris Mason /* this is pretty tricky. We don't want to 3457585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3467585717fSChris Mason * unless we're really doing the first setup for this root in 3477585717fSChris Mason * this transaction. 3487585717fSChris Mason * 3497585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3507585717fSChris Mason * if we want to take the expensive mutex. 3517585717fSChris Mason * 3527585717fSChris Mason * But, we have to set root->last_trans before we 3537585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3547585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 35527cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3567585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3577585717fSChris Mason * 3587585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3597585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3607585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3617585717fSChris Mason * done before we pop in the zero below 3627585717fSChris Mason */ 3635d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 364c7548af6SChris Mason smp_mb__before_atomic(); 36527cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3666702ed49SChris Mason } 3675d4f98a2SYan Zheng return 0; 3686702ed49SChris Mason } 3695d4f98a2SYan Zheng 3707585717fSChris Mason 3712b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3722b9dbef2SJosef Bacik struct btrfs_root *root) 3732b9dbef2SJosef Bacik { 3742b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3752b9dbef2SJosef Bacik 3762b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3772b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3782b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3792b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3802b9dbef2SJosef Bacik 3812b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3822b9dbef2SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 3832b9dbef2SJosef Bacik radix_tree_tag_clear(&root->fs_info->fs_roots_radix, 3842b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3852b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3862b9dbef2SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3872b9dbef2SJosef Bacik } 3882b9dbef2SJosef Bacik 3897585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3907585717fSChris Mason struct btrfs_root *root) 3917585717fSChris Mason { 39227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3937585717fSChris Mason return 0; 3947585717fSChris Mason 3957585717fSChris Mason /* 39627cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3977585717fSChris Mason * and barriers 3987585717fSChris Mason */ 3997585717fSChris Mason smp_rmb(); 4007585717fSChris Mason if (root->last_trans == trans->transid && 40127cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 4027585717fSChris Mason return 0; 4037585717fSChris Mason 4047585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 4057585717fSChris Mason record_root_in_trans(trans, root); 4067585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 4077585717fSChris Mason 4087585717fSChris Mason return 0; 4097585717fSChris Mason } 4107585717fSChris Mason 4114a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 4124a9d8bdeSMiao Xie { 4134a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 414501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 415501407aaSJosef Bacik !trans->aborted); 4164a9d8bdeSMiao Xie } 4174a9d8bdeSMiao Xie 418d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 419d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 420d352ac68SChris Mason * transaction might not be fully on disk. 421d352ac68SChris Mason */ 42237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 42379154b1bSChris Mason { 424f9295749SChris Mason struct btrfs_transaction *cur_trans; 42579154b1bSChris Mason 426a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 427f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 4284a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 42913c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 430a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 43172d63ed6SLi Zefan 43272d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 433501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 434501407aaSJosef Bacik cur_trans->aborted); 435724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 436a4abeea4SJosef Bacik } else { 437a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 438f9295749SChris Mason } 43937d1aeeeSChris Mason } 44037d1aeeeSChris Mason 441a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 44237d1aeeeSChris Mason { 443a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 444a4abeea4SJosef Bacik return 0; 445a4abeea4SJosef Bacik 446a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 447a22285a6SYan, Zheng return 1; 448a4abeea4SJosef Bacik 449a4abeea4SJosef Bacik if (type == TRANS_START && 450a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 451a4abeea4SJosef Bacik return 1; 452a4abeea4SJosef Bacik 453a22285a6SYan, Zheng return 0; 454a22285a6SYan, Zheng } 455a22285a6SYan, Zheng 45620dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 45720dd2cbfSMiao Xie { 45820dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 45927cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 46020dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 46120dd2cbfSMiao Xie root->reloc_root) 46220dd2cbfSMiao Xie return false; 46320dd2cbfSMiao Xie 46420dd2cbfSMiao Xie return true; 46520dd2cbfSMiao Xie } 46620dd2cbfSMiao Xie 46708e007d2SMiao Xie static struct btrfs_trans_handle * 4685aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 4695aed1dd8SAlexandru Moise unsigned int type, enum btrfs_reserve_flush_enum flush) 470a22285a6SYan, Zheng { 471a22285a6SYan, Zheng struct btrfs_trans_handle *h; 472a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 473b5009945SJosef Bacik u64 num_bytes = 0; 474c5567237SArne Jansen u64 qgroup_reserved = 0; 47520dd2cbfSMiao Xie bool reloc_reserved = false; 47620dd2cbfSMiao Xie int ret; 477acce952bSliubo 47846c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4792755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 48046c4e71eSFilipe Manana 48187533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 482acce952bSliubo return ERR_PTR(-EROFS); 4832a1eb461SJosef Bacik 48446c4e71eSFilipe Manana if (current->journal_info) { 4850860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4862a1eb461SJosef Bacik h = current->journal_info; 4872a1eb461SJosef Bacik h->use_count++; 488b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 4892a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4902a1eb461SJosef Bacik h->block_rsv = NULL; 4912a1eb461SJosef Bacik goto got_it; 4922a1eb461SJosef Bacik } 493b5009945SJosef Bacik 494b5009945SJosef Bacik /* 495b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 496b5009945SJosef Bacik * the appropriate flushing if need be. 497b5009945SJosef Bacik */ 498b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 499707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 5007174109cSQu Wenruo ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved); 501c5567237SArne Jansen if (ret) 502c5567237SArne Jansen return ERR_PTR(ret); 503c5567237SArne Jansen 504b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 50520dd2cbfSMiao Xie /* 50620dd2cbfSMiao Xie * Do the reservation for the relocation root creation 50720dd2cbfSMiao Xie */ 508ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 50920dd2cbfSMiao Xie num_bytes += root->nodesize; 51020dd2cbfSMiao Xie reloc_reserved = true; 51120dd2cbfSMiao Xie } 51220dd2cbfSMiao Xie 5134a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 514b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 51508e007d2SMiao Xie num_bytes, flush); 516b5009945SJosef Bacik if (ret) 517843fcf35SMiao Xie goto reserve_fail; 518b5009945SJosef Bacik } 519a22285a6SYan, Zheng again: 520f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 521843fcf35SMiao Xie if (!h) { 522843fcf35SMiao Xie ret = -ENOMEM; 523843fcf35SMiao Xie goto alloc_fail; 524843fcf35SMiao Xie } 525a22285a6SYan, Zheng 52698114659SJosef Bacik /* 52798114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 52898114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 52998114659SJosef Bacik * because we're already holding a ref. We need this because we could 53098114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53198114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 532354aa0fbSMiao Xie * 533354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 534354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 53598114659SJosef Bacik */ 5360860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 537b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 538b2b5ef5cSJan Kara 539a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 54037d1aeeeSChris Mason wait_current_trans(root); 541a22285a6SYan, Zheng 542a4abeea4SJosef Bacik do { 543354aa0fbSMiao Xie ret = join_transaction(root, type); 544178260b2SMiao Xie if (ret == -EBUSY) { 545a4abeea4SJosef Bacik wait_current_trans(root); 546178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 547178260b2SMiao Xie ret = -ENOENT; 548178260b2SMiao Xie } 549a4abeea4SJosef Bacik } while (ret == -EBUSY); 550a4abeea4SJosef Bacik 551db5b493aSTsutomu Itoh if (ret < 0) { 552354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 553354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 554843fcf35SMiao Xie goto join_fail; 555db5b493aSTsutomu Itoh } 5560f7d52f4SChris Mason 557a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 558a22285a6SYan, Zheng 559a22285a6SYan, Zheng h->transid = cur_trans->transid; 560a22285a6SYan, Zheng h->transaction = cur_trans; 561d13603efSArne Jansen h->root = root; 5622a1eb461SJosef Bacik h->use_count = 1; 5637174109cSQu Wenruo 564a698d075SMiao Xie h->type = type; 565d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 566bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 567ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 568b7ec40d7SChris Mason 569a22285a6SYan, Zheng smp_mb(); 5704a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5714a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 572abdd2e80SFilipe Manana current->journal_info = h; 573a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 574a22285a6SYan, Zheng goto again; 575a22285a6SYan, Zheng } 5769ed74f2dSJosef Bacik 577b5009945SJosef Bacik if (num_bytes) { 5788c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5792bcc0328SLiu Bo h->transid, num_bytes, 1); 580b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 581b5009945SJosef Bacik h->bytes_reserved = num_bytes; 58220dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 583a22285a6SYan, Zheng } 584a22285a6SYan, Zheng 5852a1eb461SJosef Bacik got_it: 586a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 587a22285a6SYan, Zheng 588a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 589a22285a6SYan, Zheng current->journal_info = h; 59079154b1bSChris Mason return h; 591843fcf35SMiao Xie 592843fcf35SMiao Xie join_fail: 5930860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 594843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 595843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 596843fcf35SMiao Xie alloc_fail: 597843fcf35SMiao Xie if (num_bytes) 598843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 599843fcf35SMiao Xie num_bytes); 600843fcf35SMiao Xie reserve_fail: 6017174109cSQu Wenruo btrfs_qgroup_free_meta(root, qgroup_reserved); 602843fcf35SMiao Xie return ERR_PTR(ret); 60379154b1bSChris Mason } 60479154b1bSChris Mason 605f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6065aed1dd8SAlexandru Moise unsigned int num_items) 607f9295749SChris Mason { 60808e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 60908e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 610f9295749SChris Mason } 6118eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6128eab77ffSFilipe Manana struct btrfs_root *root, 6138eab77ffSFilipe Manana unsigned int num_items, 6148eab77ffSFilipe Manana int min_factor) 6158eab77ffSFilipe Manana { 6168eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6178eab77ffSFilipe Manana u64 num_bytes; 6188eab77ffSFilipe Manana int ret; 6198eab77ffSFilipe Manana 6208eab77ffSFilipe Manana trans = btrfs_start_transaction(root, num_items); 6218eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6228eab77ffSFilipe Manana return trans; 6238eab77ffSFilipe Manana 6248eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6258eab77ffSFilipe Manana if (IS_ERR(trans)) 6268eab77ffSFilipe Manana return trans; 6278eab77ffSFilipe Manana 6288eab77ffSFilipe Manana num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 6298eab77ffSFilipe Manana ret = btrfs_cond_migrate_bytes(root->fs_info, 6308eab77ffSFilipe Manana &root->fs_info->trans_block_rsv, 6318eab77ffSFilipe Manana num_bytes, 6328eab77ffSFilipe Manana min_factor); 6338eab77ffSFilipe Manana if (ret) { 6348eab77ffSFilipe Manana btrfs_end_transaction(trans, root); 6358eab77ffSFilipe Manana return ERR_PTR(ret); 6368eab77ffSFilipe Manana } 6378eab77ffSFilipe Manana 6388eab77ffSFilipe Manana trans->block_rsv = &root->fs_info->trans_block_rsv; 6398eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 64088d3a5aaSJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 64188d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6428eab77ffSFilipe Manana 6438eab77ffSFilipe Manana return trans; 6448eab77ffSFilipe Manana } 6458407aa46SMiao Xie 64608e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6475aed1dd8SAlexandru Moise struct btrfs_root *root, 6485aed1dd8SAlexandru Moise unsigned int num_items) 6498407aa46SMiao Xie { 65008e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 65108e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 6528407aa46SMiao Xie } 6538407aa46SMiao Xie 6547a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 655f9295749SChris Mason { 656575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN, 657575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 658f9295749SChris Mason } 659f9295749SChris Mason 6607a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6610af3d00bSJosef Bacik { 662575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 663575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6640af3d00bSJosef Bacik } 6650af3d00bSJosef Bacik 6667a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6679ca9ee09SSage Weil { 668575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_USERSPACE, 669575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6709ca9ee09SSage Weil } 6719ca9ee09SSage Weil 672d4edf39bSMiao Xie /* 673d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 674d4edf39bSMiao Xie * 675d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 676d4edf39bSMiao Xie * don't want to start a new one. 677d4edf39bSMiao Xie * 678d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 679d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 680d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 681d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 682d4edf39bSMiao Xie * invoke 683d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 684d4edf39bSMiao Xie */ 685354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 68660376ce4SJosef Bacik { 687575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 688575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 68960376ce4SJosef Bacik } 69060376ce4SJosef Bacik 691d4edf39bSMiao Xie /* 69290b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 693d4edf39bSMiao Xie * 694d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 695d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 696d4edf39bSMiao Xie * complete. 697d4edf39bSMiao Xie */ 698d4edf39bSMiao Xie struct btrfs_trans_handle * 699d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 700d4edf39bSMiao Xie { 701d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 702d4edf39bSMiao Xie 703575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 704575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 705d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 706d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 707d4edf39bSMiao Xie 708d4edf39bSMiao Xie return trans; 709d4edf39bSMiao Xie } 710d4edf39bSMiao Xie 711d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 712b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 71389ce8a63SChris Mason struct btrfs_transaction *commit) 71489ce8a63SChris Mason { 7154a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 71689ce8a63SChris Mason } 71789ce8a63SChris Mason 71846204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 71946204592SSage Weil { 72046204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7218cd2807fSMiao Xie int ret = 0; 72246204592SSage Weil 72346204592SSage Weil if (transid) { 72446204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 725a4abeea4SJosef Bacik goto out; 72646204592SSage Weil 72746204592SSage Weil /* find specified transaction */ 728a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 72946204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 73046204592SSage Weil if (t->transid == transid) { 73146204592SSage Weil cur_trans = t; 732a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 7338cd2807fSMiao Xie ret = 0; 73446204592SSage Weil break; 73546204592SSage Weil } 7368cd2807fSMiao Xie if (t->transid > transid) { 7378cd2807fSMiao Xie ret = 0; 73846204592SSage Weil break; 73946204592SSage Weil } 7408cd2807fSMiao Xie } 741a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 74242383020SSage Weil 74342383020SSage Weil /* 74442383020SSage Weil * The specified transaction doesn't exist, or we 74542383020SSage Weil * raced with btrfs_commit_transaction 74642383020SSage Weil */ 74742383020SSage Weil if (!cur_trans) { 74842383020SSage Weil if (transid > root->fs_info->last_trans_committed) 74942383020SSage Weil ret = -EINVAL; 7508cd2807fSMiao Xie goto out; 75142383020SSage Weil } 75246204592SSage Weil } else { 75346204592SSage Weil /* find newest transaction that is committing | committed */ 754a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 75546204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 75646204592SSage Weil list) { 7574a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7584a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7593473f3c0SJosef Bacik break; 76046204592SSage Weil cur_trans = t; 761a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 76246204592SSage Weil break; 76346204592SSage Weil } 76446204592SSage Weil } 765a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 76646204592SSage Weil if (!cur_trans) 767a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 76846204592SSage Weil } 76946204592SSage Weil 77046204592SSage Weil wait_for_commit(root, cur_trans); 771724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 772a4abeea4SJosef Bacik out: 77346204592SSage Weil return ret; 77446204592SSage Weil } 77546204592SSage Weil 77637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 77737d1aeeeSChris Mason { 778a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 77937d1aeeeSChris Mason wait_current_trans(root); 78037d1aeeeSChris Mason } 78137d1aeeeSChris Mason 7828929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7838929ecfaSYan, Zheng struct btrfs_root *root) 7848929ecfaSYan, Zheng { 7851be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7860a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7871be41b78SJosef Bacik return 1; 78836ba022aSJosef Bacik 7891be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7908929ecfaSYan, Zheng } 7918929ecfaSYan, Zheng 7928929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7938929ecfaSYan, Zheng struct btrfs_root *root) 7948929ecfaSYan, Zheng { 7958929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7968929ecfaSYan, Zheng int updates; 79749b25e05SJeff Mahoney int err; 7988929ecfaSYan, Zheng 799a4abeea4SJosef Bacik smp_mb(); 8004a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8014a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8028929ecfaSYan, Zheng return 1; 8038929ecfaSYan, Zheng 8048929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 8058929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 80649b25e05SJeff Mahoney if (updates) { 80728ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 80849b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 80949b25e05SJeff Mahoney return err; 81049b25e05SJeff Mahoney } 8118929ecfaSYan, Zheng 8128929ecfaSYan, Zheng return should_end_transaction(trans, root); 8138929ecfaSYan, Zheng } 8148929ecfaSYan, Zheng 81589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 816a698d075SMiao Xie struct btrfs_root *root, int throttle) 81779154b1bSChris Mason { 8188929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 819ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 8201be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 821a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8224edc2ca3SDave Jones int err = 0; 823a79b7d4bSChris Mason int must_run_delayed_refs = 0; 824d6e4a428SChris Mason 8253bbb24b2SJosef Bacik if (trans->use_count > 1) { 8263bbb24b2SJosef Bacik trans->use_count--; 8272a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8282a1eb461SJosef Bacik return 0; 8292a1eb461SJosef Bacik } 8302a1eb461SJosef Bacik 831b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 8324c13d758SJosef Bacik trans->block_rsv = NULL; 833c5567237SArne Jansen 834ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 835ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 836ea658badSJosef Bacik 837c3e69d58SChris Mason trans->delayed_ref_updates = 0; 838a79b7d4bSChris Mason if (!trans->sync) { 839a79b7d4bSChris Mason must_run_delayed_refs = 840a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 8410a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 842a79b7d4bSChris Mason 843a79b7d4bSChris Mason /* 844a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 845a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 846a79b7d4bSChris Mason */ 847a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 848a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 849a79b7d4bSChris Mason must_run_delayed_refs = 2; 85056bec294SChris Mason } 851bb721703SChris Mason 8520e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8530e721106SJosef Bacik trans->block_rsv = NULL; 85456bec294SChris Mason 855ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 856ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 857ea658badSJosef Bacik 8584fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8594fbcdf66SFilipe Manana 860a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8614a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8624a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8634a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8644a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8654a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8664a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 867a4abeea4SJosef Bacik } 8688929ecfaSYan, Zheng 8694a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8703bbb24b2SJosef Bacik if (throttle) 8718929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8723bbb24b2SJosef Bacik else 8738929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8748929ecfaSYan, Zheng } 8758929ecfaSYan, Zheng 8760860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8776df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8786df7881aSJosef Bacik 8798929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 88013c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 88113c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8820860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 88389ce8a63SChris Mason 884a83342aaSDavid Sterba /* 885a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 886a83342aaSDavid Sterba */ 88799d16cbcSSage Weil smp_mb(); 88879154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 88979154b1bSChris Mason wake_up(&cur_trans->writer_wait); 890724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8919ed74f2dSJosef Bacik 8929ed74f2dSJosef Bacik if (current->journal_info == trans) 8939ed74f2dSJosef Bacik current->journal_info = NULL; 894ab78c84dSChris Mason 89524bbcf04SYan, Zheng if (throttle) 89624bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 89724bbcf04SYan, Zheng 89849b25e05SJeff Mahoney if (trans->aborted || 8994e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 9004e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 9014edc2ca3SDave Jones err = -EIO; 9024e121c06SJosef Bacik } 903edf39272SJan Schmidt assert_qgroups_uptodate(trans); 90449b25e05SJeff Mahoney 9054edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 906a79b7d4bSChris Mason if (must_run_delayed_refs) { 907a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 908a79b7d4bSChris Mason must_run_delayed_refs == 1); 909a79b7d4bSChris Mason } 9104edc2ca3SDave Jones return err; 91179154b1bSChris Mason } 91279154b1bSChris Mason 91389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 91489ce8a63SChris Mason struct btrfs_root *root) 91589ce8a63SChris Mason { 91698ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 91789ce8a63SChris Mason } 91889ce8a63SChris Mason 91989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 92089ce8a63SChris Mason struct btrfs_root *root) 92189ce8a63SChris Mason { 92298ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 92316cdcec7SMiao Xie } 92416cdcec7SMiao Xie 925d352ac68SChris Mason /* 926d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 927d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 928690587d1SChris Mason * those extents are sent to disk but does not wait on them 929d352ac68SChris Mason */ 930690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 9318cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 93279154b1bSChris Mason { 933777e6bd7SChris Mason int err = 0; 9347c4452b9SChris Mason int werr = 0; 9351728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 936e6138876SJosef Bacik struct extent_state *cached_state = NULL; 937777e6bd7SChris Mason u64 start = 0; 9385f39d397SChris Mason u64 end; 9397c4452b9SChris Mason 9401728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 941e6138876SJosef Bacik mark, &cached_state)) { 942663dfbb0SFilipe Manana bool wait_writeback = false; 943663dfbb0SFilipe Manana 944663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 945663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 946e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 947663dfbb0SFilipe Manana /* 948663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 949663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 950663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 951663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 952663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 953663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 954663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 955663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 956663dfbb0SFilipe Manana * (and without errors). 957663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 958663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 959663dfbb0SFilipe Manana */ 960663dfbb0SFilipe Manana if (err == -ENOMEM) { 961663dfbb0SFilipe Manana err = 0; 962663dfbb0SFilipe Manana wait_writeback = true; 963663dfbb0SFilipe Manana } 964663dfbb0SFilipe Manana if (!err) 9651728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9667c4452b9SChris Mason if (err) 9677c4452b9SChris Mason werr = err; 968663dfbb0SFilipe Manana else if (wait_writeback) 969663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 970e38e2ed7SFilipe Manana free_extent_state(cached_state); 971663dfbb0SFilipe Manana cached_state = NULL; 9721728366eSJosef Bacik cond_resched(); 9731728366eSJosef Bacik start = end + 1; 9747c4452b9SChris Mason } 975690587d1SChris Mason return werr; 976690587d1SChris Mason } 977690587d1SChris Mason 978690587d1SChris Mason /* 979690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 980690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 981690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 982690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 983690587d1SChris Mason */ 984690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9858cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 986690587d1SChris Mason { 987690587d1SChris Mason int err = 0; 988690587d1SChris Mason int werr = 0; 9891728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 990e6138876SJosef Bacik struct extent_state *cached_state = NULL; 991690587d1SChris Mason u64 start = 0; 992690587d1SChris Mason u64 end; 993656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 994656f30dbSFilipe Manana bool errors = false; 995690587d1SChris Mason 9961728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 997e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 998663dfbb0SFilipe Manana /* 999663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 1000663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 1001663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1002663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1003663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1004663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1005663dfbb0SFilipe Manana */ 1006663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1007663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1008e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1009663dfbb0SFilipe Manana if (err == -ENOMEM) 1010663dfbb0SFilipe Manana err = 0; 1011663dfbb0SFilipe Manana if (!err) 10121728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1013777e6bd7SChris Mason if (err) 1014777e6bd7SChris Mason werr = err; 1015e38e2ed7SFilipe Manana free_extent_state(cached_state); 1016e38e2ed7SFilipe Manana cached_state = NULL; 1017777e6bd7SChris Mason cond_resched(); 10181728366eSJosef Bacik start = end + 1; 1019777e6bd7SChris Mason } 10207c4452b9SChris Mason if (err) 10217c4452b9SChris Mason werr = err; 1022656f30dbSFilipe Manana 1023656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1024656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 1025656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 1026656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1027656f30dbSFilipe Manana errors = true; 1028656f30dbSFilipe Manana 1029656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 1030656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 1031656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1032656f30dbSFilipe Manana errors = true; 1033656f30dbSFilipe Manana } else { 1034656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 1035656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1036656f30dbSFilipe Manana errors = true; 1037656f30dbSFilipe Manana } 1038656f30dbSFilipe Manana 1039656f30dbSFilipe Manana if (errors && !werr) 1040656f30dbSFilipe Manana werr = -EIO; 1041656f30dbSFilipe Manana 10427c4452b9SChris Mason return werr; 104379154b1bSChris Mason } 104479154b1bSChris Mason 1045690587d1SChris Mason /* 1046690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1047690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1048690587d1SChris Mason * those extents are on disk for transaction or log commit 1049690587d1SChris Mason */ 1050171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10518cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1052690587d1SChris Mason { 1053690587d1SChris Mason int ret; 1054690587d1SChris Mason int ret2; 1055c6adc9ccSMiao Xie struct blk_plug plug; 1056690587d1SChris Mason 1057c6adc9ccSMiao Xie blk_start_plug(&plug); 10588cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1059c6adc9ccSMiao Xie blk_finish_plug(&plug); 10608cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1061bf0da8c1SChris Mason 1062bf0da8c1SChris Mason if (ret) 1063bf0da8c1SChris Mason return ret; 1064bf0da8c1SChris Mason if (ret2) 1065bf0da8c1SChris Mason return ret2; 1066bf0da8c1SChris Mason return 0; 1067690587d1SChris Mason } 1068690587d1SChris Mason 1069663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1070d0c803c4SChris Mason struct btrfs_root *root) 1071d0c803c4SChris Mason { 1072663dfbb0SFilipe Manana int ret; 1073663dfbb0SFilipe Manana 1074663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10758cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10768cef4e16SYan, Zheng EXTENT_DIRTY); 1077663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1078663dfbb0SFilipe Manana 1079663dfbb0SFilipe Manana return ret; 1080d0c803c4SChris Mason } 1081d0c803c4SChris Mason 1082d352ac68SChris Mason /* 1083d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1084d352ac68SChris Mason * 1085d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1086d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1087d352ac68SChris Mason * allocation tree. 1088d352ac68SChris Mason * 1089d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1090d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1091d352ac68SChris Mason */ 10920b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 109379154b1bSChris Mason struct btrfs_root *root) 109479154b1bSChris Mason { 109579154b1bSChris Mason int ret; 10960b86a832SChris Mason u64 old_root_bytenr; 109786b9f2ecSYan, Zheng u64 old_root_used; 10980b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 109979154b1bSChris Mason 110086b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 110156bec294SChris Mason 110279154b1bSChris Mason while (1) { 11030b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 110486b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1105ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 110679154b1bSChris Mason break; 110787ef2bb4SChris Mason 11085d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 110979154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11100b86a832SChris Mason &root->root_key, 11110b86a832SChris Mason &root->root_item); 111249b25e05SJeff Mahoney if (ret) 111349b25e05SJeff Mahoney return ret; 111456bec294SChris Mason 111586b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1116e7070be1SJosef Bacik } 1117276e680dSYan Zheng 11180b86a832SChris Mason return 0; 11190b86a832SChris Mason } 11200b86a832SChris Mason 1121d352ac68SChris Mason /* 1122d352ac68SChris Mason * update all the cowonly tree roots on disk 112349b25e05SJeff Mahoney * 112449b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 112549b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 112649b25e05SJeff Mahoney * to clean up the delayed refs. 1127d352ac68SChris Mason */ 11285d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11290b86a832SChris Mason struct btrfs_root *root) 11300b86a832SChris Mason { 11310b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1132ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11331bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11340b86a832SChris Mason struct list_head *next; 113584234f3aSYan Zheng struct extent_buffer *eb; 113656bec294SChris Mason int ret; 113784234f3aSYan Zheng 113884234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 113949b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 114049b25e05SJeff Mahoney 0, &eb); 114184234f3aSYan Zheng btrfs_tree_unlock(eb); 114284234f3aSYan Zheng free_extent_buffer(eb); 11430b86a832SChris Mason 114449b25e05SJeff Mahoney if (ret) 114549b25e05SJeff Mahoney return ret; 114649b25e05SJeff Mahoney 114756bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 114849b25e05SJeff Mahoney if (ret) 114949b25e05SJeff Mahoney return ret; 115087ef2bb4SChris Mason 1151733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1152c16ce190SJosef Bacik if (ret) 1153c16ce190SJosef Bacik return ret; 11548dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1155c16ce190SJosef Bacik if (ret) 1156c16ce190SJosef Bacik return ret; 1157546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1158c16ce190SJosef Bacik if (ret) 1159c16ce190SJosef Bacik return ret; 1160546adb0dSJan Schmidt 1161dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1162dcdf7f6dSJosef Bacik if (ret) 1163dcdf7f6dSJosef Bacik return ret; 1164dcdf7f6dSJosef Bacik 1165546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1166546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1167c16ce190SJosef Bacik if (ret) 1168c16ce190SJosef Bacik return ret; 1169ea526d18SJosef Bacik again: 11700b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11710b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11720b86a832SChris Mason list_del_init(next); 11730b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1174e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 117587ef2bb4SChris Mason 11769e351cc8SJosef Bacik if (root != fs_info->extent_root) 11779e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11789e351cc8SJosef Bacik &trans->transaction->switch_commits); 117949b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 118049b25e05SJeff Mahoney if (ret) 118149b25e05SJeff Mahoney return ret; 1182ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1183ea526d18SJosef Bacik if (ret) 1184ea526d18SJosef Bacik return ret; 118579154b1bSChris Mason } 1186276e680dSYan Zheng 11871bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1188ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1189ea526d18SJosef Bacik if (ret) 1190ea526d18SJosef Bacik return ret; 1191ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1192ea526d18SJosef Bacik if (ret) 1193ea526d18SJosef Bacik return ret; 1194ea526d18SJosef Bacik } 1195ea526d18SJosef Bacik 1196ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1197ea526d18SJosef Bacik goto again; 1198ea526d18SJosef Bacik 11999e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 12009e351cc8SJosef Bacik &trans->transaction->switch_commits); 12018dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12028dabb742SStefan Behrens 120379154b1bSChris Mason return 0; 120479154b1bSChris Mason } 120579154b1bSChris Mason 1206d352ac68SChris Mason /* 1207d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1208d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1209d352ac68SChris Mason * be deleted 1210d352ac68SChris Mason */ 1211cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12125eda7b5eSChris Mason { 1213a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1214cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12159d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1216a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 12175eda7b5eSChris Mason } 12185eda7b5eSChris Mason 1219d352ac68SChris Mason /* 12205d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1221d352ac68SChris Mason */ 12225d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12235d4f98a2SYan Zheng struct btrfs_root *root) 12240f7d52f4SChris Mason { 12250f7d52f4SChris Mason struct btrfs_root *gang[8]; 12265d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 12270f7d52f4SChris Mason int i; 12280f7d52f4SChris Mason int ret; 122954aa1f4dSChris Mason int err = 0; 123054aa1f4dSChris Mason 1231a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12320f7d52f4SChris Mason while (1) { 12335d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12345d4f98a2SYan Zheng (void **)gang, 0, 12350f7d52f4SChris Mason ARRAY_SIZE(gang), 12360f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12370f7d52f4SChris Mason if (ret == 0) 12380f7d52f4SChris Mason break; 12390f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12400f7d52f4SChris Mason root = gang[i]; 12415d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12422619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12430f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1244a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 124531153d81SYan Zheng 1246e02119d5SChris Mason btrfs_free_log(trans, root); 12475d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1248d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1249e02119d5SChris Mason 125082d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 125182d5902dSLi Zefan 1252f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 125327cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1254c7548af6SChris Mason smp_mb__after_atomic(); 1255f1ebcc74SLiu Bo 1256978d910dSYan Zheng if (root->commit_root != root->node) { 12579e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12589e351cc8SJosef Bacik &trans->transaction->switch_commits); 1259978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1260978d910dSYan Zheng root->node); 1261978d910dSYan Zheng } 126231153d81SYan Zheng 12635d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12640f7d52f4SChris Mason &root->root_key, 12650f7d52f4SChris Mason &root->root_item); 1266a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 126754aa1f4dSChris Mason if (err) 126854aa1f4dSChris Mason break; 12697174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12700f7d52f4SChris Mason } 12719f3a7427SChris Mason } 1272a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 127354aa1f4dSChris Mason return err; 12740f7d52f4SChris Mason } 12750f7d52f4SChris Mason 1276d352ac68SChris Mason /* 1277de78b51aSEric Sandeen * defrag a given btree. 1278de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1279d352ac68SChris Mason */ 1280de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1281e9d0b13bSChris Mason { 1282e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1283e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12848929ecfaSYan, Zheng int ret; 1285e9d0b13bSChris Mason 128627cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1287e9d0b13bSChris Mason return 0; 12888929ecfaSYan, Zheng 12896b80053dSChris Mason while (1) { 12908929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12918929ecfaSYan, Zheng if (IS_ERR(trans)) 12928929ecfaSYan, Zheng return PTR_ERR(trans); 12938929ecfaSYan, Zheng 1294de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12958929ecfaSYan, Zheng 1296e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1297b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1298e9d0b13bSChris Mason cond_resched(); 1299e9d0b13bSChris Mason 13007841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1301e9d0b13bSChris Mason break; 1302210549ebSDavid Sterba 1303210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1304efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1305210549ebSDavid Sterba ret = -EAGAIN; 1306210549ebSDavid Sterba break; 1307210549ebSDavid Sterba } 1308e9d0b13bSChris Mason } 130927cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13108929ecfaSYan, Zheng return ret; 1311e9d0b13bSChris Mason } 1312e9d0b13bSChris Mason 1313d352ac68SChris Mason /* 1314d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1315aec8030aSMiao Xie * transaction commit. This does the actual creation. 1316aec8030aSMiao Xie * 1317aec8030aSMiao Xie * Note: 1318aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1319aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1320aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1321d352ac68SChris Mason */ 132280b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13233063d29fSChris Mason struct btrfs_fs_info *fs_info, 13243063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13253063d29fSChris Mason { 13263063d29fSChris Mason struct btrfs_key key; 132780b6794dSChris Mason struct btrfs_root_item *new_root_item; 13283063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13293063d29fSChris Mason struct btrfs_root *root = pending->root; 13306bdb72deSSage Weil struct btrfs_root *parent_root; 133198c9942aSLiu Bo struct btrfs_block_rsv *rsv; 13326bdb72deSSage Weil struct inode *parent_inode; 133342874b3dSMiao Xie struct btrfs_path *path; 133442874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1335a22285a6SYan, Zheng struct dentry *dentry; 13363063d29fSChris Mason struct extent_buffer *tmp; 1337925baeddSChris Mason struct extent_buffer *old; 133804b285f3SDeepa Dinamani struct timespec cur_time; 1339aec8030aSMiao Xie int ret = 0; 1340d68fc57bSYan, Zheng u64 to_reserve = 0; 13416bdb72deSSage Weil u64 index = 0; 1342a22285a6SYan, Zheng u64 objectid; 1343b83cc969SLi Zefan u64 root_flags; 13448ea05e3aSAlexander Block uuid_le new_uuid; 13453063d29fSChris Mason 13468546b570SDavid Sterba ASSERT(pending->path); 13478546b570SDavid Sterba path = pending->path; 134842874b3dSMiao Xie 1349b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1350b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1351a22285a6SYan, Zheng 1352aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1353aec8030aSMiao Xie if (pending->error) 13546fa9700eSMiao Xie goto no_free_objectid; 13553063d29fSChris Mason 1356d6726335SQu Wenruo /* 1357d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1358d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1359d6726335SQu Wenruo */ 1360d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1361d6726335SQu Wenruo 1362147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1363d68fc57bSYan, Zheng 1364d68fc57bSYan, Zheng if (to_reserve > 0) { 1365aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1366aec8030aSMiao Xie &pending->block_rsv, 136708e007d2SMiao Xie to_reserve, 136808e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1369aec8030aSMiao Xie if (pending->error) 1370d6726335SQu Wenruo goto clear_skip_qgroup; 1371d68fc57bSYan, Zheng } 1372d68fc57bSYan, Zheng 13733063d29fSChris Mason key.objectid = objectid; 1374a22285a6SYan, Zheng key.offset = (u64)-1; 1375a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 13763063d29fSChris Mason 13776fa9700eSMiao Xie rsv = trans->block_rsv; 1378a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 13792382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 138088d3a5aaSJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 138188d3a5aaSJosef Bacik trans->transid, 138288d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1383a22285a6SYan, Zheng dentry = pending->dentry; 1384e9662f70SMiao Xie parent_inode = pending->dir; 1385a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 13867585717fSChris Mason record_root_in_trans(trans, parent_root); 1387a22285a6SYan, Zheng 138804b285f3SDeepa Dinamani cur_time = current_fs_time(parent_inode->i_sb); 138904b285f3SDeepa Dinamani 13906bdb72deSSage Weil /* 13916bdb72deSSage Weil * insert the directory item 13926bdb72deSSage Weil */ 13936bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 139449b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 139542874b3dSMiao Xie 139642874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 139742874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 139842874b3dSMiao Xie btrfs_ino(parent_inode), 139942874b3dSMiao Xie dentry->d_name.name, 140042874b3dSMiao Xie dentry->d_name.len, 0); 140142874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1402fe66a05aSChris Mason pending->error = -EEXIST; 1403aec8030aSMiao Xie goto dir_item_existed; 140442874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 140542874b3dSMiao Xie ret = PTR_ERR(dir_item); 14068732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14078732d44fSMiao Xie goto fail; 140879787eaaSJeff Mahoney } 140942874b3dSMiao Xie btrfs_release_path(path); 14106bdb72deSSage Weil 1411e999376fSChris Mason /* 1412e999376fSChris Mason * pull in the delayed directory update 1413e999376fSChris Mason * and the delayed inode item 1414e999376fSChris Mason * otherwise we corrupt the FS during 1415e999376fSChris Mason * snapshot 1416e999376fSChris Mason */ 1417e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 14188732d44fSMiao Xie if (ret) { /* Transaction aborted */ 14198732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14208732d44fSMiao Xie goto fail; 14218732d44fSMiao Xie } 1422e999376fSChris Mason 14237585717fSChris Mason record_root_in_trans(trans, root); 14246bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14256bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 142608fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14276bdb72deSSage Weil 1428b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1429b83cc969SLi Zefan if (pending->readonly) 1430b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1431b83cc969SLi Zefan else 1432b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1433b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1434b83cc969SLi Zefan 14358ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 14368ea05e3aSAlexander Block trans->transid); 14378ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 14388ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 14398ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 14408ea05e3aSAlexander Block BTRFS_UUID_SIZE); 144170023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 144270023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 144370023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14448ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14458ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 14468ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 14478ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 144870023da2SStefan Behrens } 14493cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 14503cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 145170023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 14528ea05e3aSAlexander Block 1453925baeddSChris Mason old = btrfs_lock_root_node(root); 145449b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 145579787eaaSJeff Mahoney if (ret) { 145679787eaaSJeff Mahoney btrfs_tree_unlock(old); 145779787eaaSJeff Mahoney free_extent_buffer(old); 14588732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14598732d44fSMiao Xie goto fail; 146079787eaaSJeff Mahoney } 146149b25e05SJeff Mahoney 14625d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 14633063d29fSChris Mason 146449b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 146579787eaaSJeff Mahoney /* clean up in any case */ 1466925baeddSChris Mason btrfs_tree_unlock(old); 1467925baeddSChris Mason free_extent_buffer(old); 14688732d44fSMiao Xie if (ret) { 14698732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14708732d44fSMiao Xie goto fail; 14718732d44fSMiao Xie } 1472f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 147327cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1474f1ebcc74SLiu Bo smp_wmb(); 1475f1ebcc74SLiu Bo 14765d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1477a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1478a22285a6SYan, Zheng key.offset = trans->transid; 1479a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1480925baeddSChris Mason btrfs_tree_unlock(tmp); 14813063d29fSChris Mason free_extent_buffer(tmp); 14828732d44fSMiao Xie if (ret) { 14838732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14848732d44fSMiao Xie goto fail; 14858732d44fSMiao Xie } 14860660b5afSChris Mason 1487a22285a6SYan, Zheng /* 1488a22285a6SYan, Zheng * insert root back/forward references 1489a22285a6SYan, Zheng */ 1490a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1491a22285a6SYan, Zheng parent_root->root_key.objectid, 149233345d01SLi Zefan btrfs_ino(parent_inode), index, 1493a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14948732d44fSMiao Xie if (ret) { 14958732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14968732d44fSMiao Xie goto fail; 14978732d44fSMiao Xie } 1498a22285a6SYan, Zheng 1499a22285a6SYan, Zheng key.offset = (u64)-1; 1500a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 150179787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 150279787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 15038732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15048732d44fSMiao Xie goto fail; 150579787eaaSJeff Mahoney } 1506d68fc57bSYan, Zheng 150749b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15088732d44fSMiao Xie if (ret) { 15098732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15108732d44fSMiao Xie goto fail; 15118732d44fSMiao Xie } 1512361048f5SMiao Xie 1513361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 15148732d44fSMiao Xie if (ret) { 15158732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15168732d44fSMiao Xie goto fail; 15178732d44fSMiao Xie } 151842874b3dSMiao Xie 151942874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 152042874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 152142874b3dSMiao Xie parent_inode, &key, 152242874b3dSMiao Xie BTRFS_FT_DIR, index); 152342874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 15249c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 15258732d44fSMiao Xie if (ret) { 15268732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15278732d44fSMiao Xie goto fail; 15288732d44fSMiao Xie } 152942874b3dSMiao Xie 153042874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 153142874b3dSMiao Xie dentry->d_name.len * 2); 153204b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 153304b285f3SDeepa Dinamani current_fs_time(parent_inode->i_sb); 1534be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1535dd5f9615SStefan Behrens if (ret) { 15368732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1537dd5f9615SStefan Behrens goto fail; 1538dd5f9615SStefan Behrens } 1539dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1540dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1541dd5f9615SStefan Behrens if (ret) { 1542dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1543dd5f9615SStefan Behrens goto fail; 1544dd5f9615SStefan Behrens } 1545dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1546dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1547dd5f9615SStefan Behrens new_root_item->received_uuid, 1548dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1549dd5f9615SStefan Behrens objectid); 1550dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1551dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1552dd5f9615SStefan Behrens goto fail; 1553dd5f9615SStefan Behrens } 1554dd5f9615SStefan Behrens } 1555d6726335SQu Wenruo 1556d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1557d6726335SQu Wenruo if (ret) { 1558d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1559d6726335SQu Wenruo goto fail; 1560d6726335SQu Wenruo } 1561d6726335SQu Wenruo 1562d6726335SQu Wenruo /* 1563d6726335SQu Wenruo * account qgroup counters before qgroup_inherit() 1564d6726335SQu Wenruo */ 1565d6726335SQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 1566d6726335SQu Wenruo if (ret) 1567d6726335SQu Wenruo goto fail; 1568d6726335SQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 1569d6726335SQu Wenruo if (ret) 1570d6726335SQu Wenruo goto fail; 1571d6726335SQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 1572d6726335SQu Wenruo root->root_key.objectid, 1573d6726335SQu Wenruo objectid, pending->inherit); 1574d6726335SQu Wenruo if (ret) { 1575d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1576d6726335SQu Wenruo goto fail; 1577d6726335SQu Wenruo } 1578d6726335SQu Wenruo 15793063d29fSChris Mason fail: 1580aec8030aSMiao Xie pending->error = ret; 1581aec8030aSMiao Xie dir_item_existed: 158298c9942aSLiu Bo trans->block_rsv = rsv; 15832382c5ccSLiu Bo trans->bytes_reserved = 0; 1584d6726335SQu Wenruo clear_skip_qgroup: 1585d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 15866fa9700eSMiao Xie no_free_objectid: 15876fa9700eSMiao Xie kfree(new_root_item); 1588b0c0ea63SDavid Sterba pending->root_item = NULL; 158942874b3dSMiao Xie btrfs_free_path(path); 15908546b570SDavid Sterba pending->path = NULL; 15918546b570SDavid Sterba 159249b25e05SJeff Mahoney return ret; 15933063d29fSChris Mason } 15943063d29fSChris Mason 1595d352ac68SChris Mason /* 1596d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1597d352ac68SChris Mason */ 159880b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 15993063d29fSChris Mason struct btrfs_fs_info *fs_info) 16003063d29fSChris Mason { 1601aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16023063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1603aec8030aSMiao Xie int ret = 0; 16043de4586cSChris Mason 1605aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1606aec8030aSMiao Xie list_del(&pending->list); 1607aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1608aec8030aSMiao Xie if (ret) 1609aec8030aSMiao Xie break; 1610aec8030aSMiao Xie } 1611aec8030aSMiao Xie return ret; 16123de4586cSChris Mason } 16133de4586cSChris Mason 16145d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 16155d4f98a2SYan Zheng { 16165d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16175d4f98a2SYan Zheng struct btrfs_super_block *super; 16185d4f98a2SYan Zheng 16196c41761fSDavid Sterba super = root->fs_info->super_copy; 16205d4f98a2SYan Zheng 16215d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 16225d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 16235d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 16245d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 16255d4f98a2SYan Zheng 16265d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 16275d4f98a2SYan Zheng super->root = root_item->bytenr; 16285d4f98a2SYan Zheng super->generation = root_item->generation; 16295d4f98a2SYan Zheng super->root_level = root_item->level; 163073bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 16310af3d00bSJosef Bacik super->cache_generation = root_item->generation; 163270f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 163326432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 16345d4f98a2SYan Zheng } 16355d4f98a2SYan Zheng 1636f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1637f36f3042SChris Mason { 16384a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1639f36f3042SChris Mason int ret = 0; 16404a9d8bdeSMiao Xie 1641a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16424a9d8bdeSMiao Xie trans = info->running_transaction; 16434a9d8bdeSMiao Xie if (trans) 16444a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1645a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1646f36f3042SChris Mason return ret; 1647f36f3042SChris Mason } 1648f36f3042SChris Mason 16498929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16508929ecfaSYan, Zheng { 16514a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16528929ecfaSYan, Zheng int ret = 0; 16534a9d8bdeSMiao Xie 1654a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16554a9d8bdeSMiao Xie trans = info->running_transaction; 16564a9d8bdeSMiao Xie if (trans) 16574a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1658a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 16598929ecfaSYan, Zheng return ret; 16608929ecfaSYan, Zheng } 16618929ecfaSYan, Zheng 1662bb9c12c9SSage Weil /* 1663bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1664bb9c12c9SSage Weil * transaction joins 1665bb9c12c9SSage Weil */ 1666bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1667bb9c12c9SSage Weil struct btrfs_transaction *trans) 1668bb9c12c9SSage Weil { 16694a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1670501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1671501407aaSJosef Bacik trans->aborted); 1672bb9c12c9SSage Weil } 1673bb9c12c9SSage Weil 1674bb9c12c9SSage Weil /* 1675bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1676bb9c12c9SSage Weil * caller holds ref. 1677bb9c12c9SSage Weil */ 1678bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1679bb9c12c9SSage Weil struct btrfs_transaction *trans) 1680bb9c12c9SSage Weil { 168172d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1682501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1683501407aaSJosef Bacik trans->aborted); 1684bb9c12c9SSage Weil } 1685bb9c12c9SSage Weil 1686bb9c12c9SSage Weil /* 1687bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1688bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1689bb9c12c9SSage Weil */ 1690bb9c12c9SSage Weil struct btrfs_async_commit { 1691bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1692bb9c12c9SSage Weil struct btrfs_root *root; 16937892b5afSMiao Xie struct work_struct work; 1694bb9c12c9SSage Weil }; 1695bb9c12c9SSage Weil 1696bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1697bb9c12c9SSage Weil { 1698bb9c12c9SSage Weil struct btrfs_async_commit *ac = 16997892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1700bb9c12c9SSage Weil 17016fc4e354SSage Weil /* 17026fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17036fc4e354SSage Weil * Tell lockdep about it. 17046fc4e354SSage Weil */ 1705b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1706bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 17076fc4e354SSage Weil 1708e209db7aSSage Weil current->journal_info = ac->newtrans; 1709e209db7aSSage Weil 1710bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1711bb9c12c9SSage Weil kfree(ac); 1712bb9c12c9SSage Weil } 1713bb9c12c9SSage Weil 1714bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1715bb9c12c9SSage Weil struct btrfs_root *root, 1716bb9c12c9SSage Weil int wait_for_unblock) 1717bb9c12c9SSage Weil { 1718bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1719bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1720bb9c12c9SSage Weil 1721bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1722db5b493aSTsutomu Itoh if (!ac) 1723db5b493aSTsutomu Itoh return -ENOMEM; 1724bb9c12c9SSage Weil 17257892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1726bb9c12c9SSage Weil ac->root = root; 17277a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 17283612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17293612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17303612b495STsutomu Itoh kfree(ac); 17313612b495STsutomu Itoh return err; 17323612b495STsutomu Itoh } 1733bb9c12c9SSage Weil 1734bb9c12c9SSage Weil /* take transaction reference */ 1735bb9c12c9SSage Weil cur_trans = trans->transaction; 173613c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1737bb9c12c9SSage Weil 1738bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 17396fc4e354SSage Weil 17406fc4e354SSage Weil /* 17416fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17426fc4e354SSage Weil * async commit thread will be the one to unlock it. 17436fc4e354SSage Weil */ 1744b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1745bee9182dSOleg Nesterov __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 17466fc4e354SSage Weil 17477892b5afSMiao Xie schedule_work(&ac->work); 1748bb9c12c9SSage Weil 1749bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1750bb9c12c9SSage Weil if (wait_for_unblock) 1751bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1752bb9c12c9SSage Weil else 1753bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1754bb9c12c9SSage Weil 175538e88054SSage Weil if (current->journal_info == trans) 175638e88054SSage Weil current->journal_info = NULL; 175738e88054SSage Weil 1758724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1759bb9c12c9SSage Weil return 0; 1760bb9c12c9SSage Weil } 1761bb9c12c9SSage Weil 176249b25e05SJeff Mahoney 176349b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 17647b8b92afSJosef Bacik struct btrfs_root *root, int err) 176549b25e05SJeff Mahoney { 176649b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1767f094ac32SLiu Bo DEFINE_WAIT(wait); 176849b25e05SJeff Mahoney 176949b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 177049b25e05SJeff Mahoney 17717b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 17727b8b92afSJosef Bacik 177349b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 177466b6135bSLiu Bo 177525d8c284SMiao Xie /* 177625d8c284SMiao Xie * If the transaction is removed from the list, it means this 177725d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 177825d8c284SMiao Xie * to call the cleanup function. 177925d8c284SMiao Xie */ 178025d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 178166b6135bSLiu Bo 178249b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1783d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 17844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1785f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1786f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1787f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1788f094ac32SLiu Bo 1789f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1790d7096fc3SJosef Bacik } 179149b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 179249b25e05SJeff Mahoney 179349b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 179449b25e05SJeff Mahoney 17954a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17964a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 17974a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 17984a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 17994a9d8bdeSMiao Xie 1800e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1801e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1802724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1803724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 180449b25e05SJeff Mahoney 180549b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 180649b25e05SJeff Mahoney 180749b25e05SJeff Mahoney if (current->journal_info == trans) 180849b25e05SJeff Mahoney current->journal_info = NULL; 1809c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 181049b25e05SJeff Mahoney 181149b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 181249b25e05SJeff Mahoney } 181349b25e05SJeff Mahoney 181482436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 181582436617SMiao Xie { 181682436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 18176c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 181882436617SMiao Xie return 0; 181982436617SMiao Xie } 182082436617SMiao Xie 182182436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 182282436617SMiao Xie { 182382436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1824b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 182582436617SMiao Xie } 182682436617SMiao Xie 182750d9aa99SJosef Bacik static inline void 1828161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 182950d9aa99SJosef Bacik { 1830161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1831161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 183250d9aa99SJosef Bacik } 183350d9aa99SJosef Bacik 183479154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 183579154b1bSChris Mason struct btrfs_root *root) 183679154b1bSChris Mason { 183749b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 18388fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1839656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 184025287e0aSMiao Xie int ret; 184179154b1bSChris Mason 18428d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 18438d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 184425287e0aSMiao Xie ret = cur_trans->aborted; 1845e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1846e4a2bcacSJosef Bacik return ret; 184725287e0aSMiao Xie } 184849b25e05SJeff Mahoney 184956bec294SChris Mason /* make a pass through all the delayed refs we have so far 185056bec294SChris Mason * any runnings procs may add more while we are here 185156bec294SChris Mason */ 185256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1853e4a2bcacSJosef Bacik if (ret) { 1854e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1855e4a2bcacSJosef Bacik return ret; 1856e4a2bcacSJosef Bacik } 185756bec294SChris Mason 18580e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18590e721106SJosef Bacik trans->block_rsv = NULL; 18600e721106SJosef Bacik 1861b7ec40d7SChris Mason cur_trans = trans->transaction; 186249b25e05SJeff Mahoney 186356bec294SChris Mason /* 186456bec294SChris Mason * set the flushing flag so procs in this transaction have to 186556bec294SChris Mason * start sending their work down. 186656bec294SChris Mason */ 1867b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 18681be41b78SJosef Bacik smp_wmb(); 186956bec294SChris Mason 1870ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1871ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1872ea658badSJosef Bacik 1873c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1874e4a2bcacSJosef Bacik if (ret) { 1875e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1876e4a2bcacSJosef Bacik return ret; 1877e4a2bcacSJosef Bacik } 187856bec294SChris Mason 18793204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 18801bbc621eSChris Mason int run_it = 0; 18811bbc621eSChris Mason 18821bbc621eSChris Mason /* this mutex is also taken before trying to set 18831bbc621eSChris Mason * block groups readonly. We need to make sure 18841bbc621eSChris Mason * that nobody has set a block group readonly 18851bbc621eSChris Mason * after a extents from that block group have been 18861bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 18871bbc621eSChris Mason * will wait for the transaction to commit if it 18883204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 18891bbc621eSChris Mason * 18903204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 18913204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 18921bbc621eSChris Mason * hurt to have more than one go through, but there's no 18931bbc621eSChris Mason * real advantage to it either. 18941bbc621eSChris Mason */ 18951bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 18963204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 18973204d33cSJosef Bacik &cur_trans->flags)) 18981bbc621eSChris Mason run_it = 1; 18991bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 19001bbc621eSChris Mason 19011bbc621eSChris Mason if (run_it) 19021bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 19031bbc621eSChris Mason } 19041bbc621eSChris Mason if (ret) { 19051bbc621eSChris Mason btrfs_end_transaction(trans, root); 19061bbc621eSChris Mason return ret; 19071bbc621eSChris Mason } 19081bbc621eSChris Mason 19094a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 19104a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 19114a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 191213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 191349b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1914ccd467d6SChris Mason 1915b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 191615ee9bc7SJosef Bacik 1917b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1918b4924a0fSLiu Bo ret = cur_trans->aborted; 1919b4924a0fSLiu Bo 1920724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 192115ee9bc7SJosef Bacik 192249b25e05SJeff Mahoney return ret; 192379154b1bSChris Mason } 19244313b399SChris Mason 19254a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1926bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1927bb9c12c9SSage Weil 1928ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1929ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1930ccd467d6SChris Mason struct btrfs_transaction, list); 19314a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 193213c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1933a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1934ccd467d6SChris Mason 1935ccd467d6SChris Mason wait_for_commit(root, prev_trans); 19361f9b8c8fSFilipe Manana ret = prev_trans->aborted; 1937ccd467d6SChris Mason 1938724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 19391f9b8c8fSFilipe Manana if (ret) 19401f9b8c8fSFilipe Manana goto cleanup_transaction; 1941a4abeea4SJosef Bacik } else { 1942a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1943ccd467d6SChris Mason } 1944a4abeea4SJosef Bacik } else { 1945a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1946ccd467d6SChris Mason } 194715ee9bc7SJosef Bacik 19480860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 19490860adfdSMiao Xie 195082436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 195182436617SMiao Xie if (ret) 195282436617SMiao Xie goto cleanup_transaction; 195382436617SMiao Xie 19548d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 195549b25e05SJeff Mahoney if (ret) 195649b25e05SJeff Mahoney goto cleanup_transaction; 195716cdcec7SMiao Xie 1958581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1959581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1960ed3b3d31SChris Mason 1961581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 19628d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1963ca469637SMiao Xie if (ret) 1964ca469637SMiao Xie goto cleanup_transaction; 1965ca469637SMiao Xie 196682436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1967cb7ab021SWang Shilong 1968161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 196950d9aa99SJosef Bacik 1970cb7ab021SWang Shilong btrfs_scrub_pause(root); 1971ed0ca140SJosef Bacik /* 1972ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1973ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 19744a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1975ed0ca140SJosef Bacik */ 1976a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19774a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1978a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1979ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1980ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 198115ee9bc7SJosef Bacik 19822cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 19832cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19842cba30f1SMiao Xie ret = cur_trans->aborted; 19856cf7f77eSWang Shilong goto scrub_continue; 19862cba30f1SMiao Xie } 19877585717fSChris Mason /* 19887585717fSChris Mason * the reloc mutex makes sure that we stop 19897585717fSChris Mason * the balancing code from coming in and moving 19907585717fSChris Mason * extents around in the middle of the commit 19917585717fSChris Mason */ 19927585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 19937585717fSChris Mason 199442874b3dSMiao Xie /* 199542874b3dSMiao Xie * We needn't worry about the delayed items because we will 199642874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 199742874b3dSMiao Xie * core function of the snapshot creation. 199842874b3dSMiao Xie */ 199942874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 200049b25e05SJeff Mahoney if (ret) { 200149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20026cf7f77eSWang Shilong goto scrub_continue; 200349b25e05SJeff Mahoney } 20043063d29fSChris Mason 200542874b3dSMiao Xie /* 200642874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 200742874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 200842874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 200942874b3dSMiao Xie * them. 201042874b3dSMiao Xie * 201142874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 201242874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 201342874b3dSMiao Xie * the nodes and leaves. 201442874b3dSMiao Xie */ 201542874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 201649b25e05SJeff Mahoney if (ret) { 201749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20186cf7f77eSWang Shilong goto scrub_continue; 201949b25e05SJeff Mahoney } 202016cdcec7SMiao Xie 202156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 202249b25e05SJeff Mahoney if (ret) { 202349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20246cf7f77eSWang Shilong goto scrub_continue; 202549b25e05SJeff Mahoney } 202656bec294SChris Mason 20270ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 20280ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 20290ed4792aSQu Wenruo if (ret) { 20300ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20310ed4792aSQu Wenruo goto scrub_continue; 20320ed4792aSQu Wenruo } 20330ed4792aSQu Wenruo 2034e999376fSChris Mason /* 2035e999376fSChris Mason * make sure none of the code above managed to slip in a 2036e999376fSChris Mason * delayed item 2037e999376fSChris Mason */ 2038e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 2039e999376fSChris Mason 20402c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2041dc17ff8fSChris Mason 2042e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2043e02119d5SChris Mason * various roots consistent with each other. Every pointer 2044e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2045e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2046e02119d5SChris Mason * the tree logging code from jumping in and changing any 2047e02119d5SChris Mason * of the trees. 2048e02119d5SChris Mason * 2049e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2050e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2051e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2052e02119d5SChris Mason * from now until after the super is written, we avoid races 2053e02119d5SChris Mason * with the tree-log code. 2054e02119d5SChris Mason */ 2055e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 20561a40e23bSZheng Yan 20575d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 205849b25e05SJeff Mahoney if (ret) { 205949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2060871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20616cf7f77eSWang Shilong goto scrub_continue; 206249b25e05SJeff Mahoney } 206354aa1f4dSChris Mason 20643818aea2SQu Wenruo /* 20657e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 20667e1876acSDavid Sterba * before the next transaction. 20673818aea2SQu Wenruo */ 2068572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 20693818aea2SQu Wenruo 20705d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2071e02119d5SChris Mason * safe to free the root of tree log roots 2072e02119d5SChris Mason */ 2073e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2074e02119d5SChris Mason 20750ed4792aSQu Wenruo /* 20760ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 20770ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 20780ed4792aSQu Wenruo */ 20790ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 20800ed4792aSQu Wenruo if (ret < 0) { 20810ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 20820ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20830ed4792aSQu Wenruo goto scrub_continue; 20840ed4792aSQu Wenruo } 20850ed4792aSQu Wenruo 20865d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 208749b25e05SJeff Mahoney if (ret) { 208849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2089871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20906cf7f77eSWang Shilong goto scrub_continue; 209149b25e05SJeff Mahoney } 209254aa1f4dSChris Mason 20932cba30f1SMiao Xie /* 20942cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 20952cba30f1SMiao Xie * update ->aborted, check it. 20962cba30f1SMiao Xie */ 20972cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20982cba30f1SMiao Xie ret = cur_trans->aborted; 20992cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 21002cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 21016cf7f77eSWang Shilong goto scrub_continue; 21022cba30f1SMiao Xie } 21032cba30f1SMiao Xie 210411833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 210511833d66SYan Zheng 210678fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 21075f39d397SChris Mason 21085d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 21095d4f98a2SYan Zheng root->fs_info->tree_root->node); 21109e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 21119e351cc8SJosef Bacik &cur_trans->switch_commits); 21125d4f98a2SYan Zheng 21135d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 21145d4f98a2SYan Zheng root->fs_info->chunk_root->node); 21159e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 21169e351cc8SJosef Bacik &cur_trans->switch_commits); 21179e351cc8SJosef Bacik 21189e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 21195d4f98a2SYan Zheng 2120edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2121ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 21221bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 21235d4f98a2SYan Zheng update_super_roots(root); 2124e02119d5SChris Mason 21256c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 21266c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 21276c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 21286c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2129ccd467d6SChris Mason 2130935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2131ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2132935e5cc9SMiao Xie 2133656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2134656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2135656f30dbSFilipe Manana 21364fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 21374fbcdf66SFilipe Manana 2138a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 21394a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2140a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2141a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 21427585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2143b7ec40d7SChris Mason 2144f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2145e6dcd2dcSChris Mason 214679154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 214749b25e05SJeff Mahoney if (ret) { 2148*34d97007SAnand Jain btrfs_handle_fs_error(root->fs_info, ret, 214908748810SDavid Sterba "Error while writing out transaction"); 215049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21516cf7f77eSWang Shilong goto scrub_continue; 215249b25e05SJeff Mahoney } 215349b25e05SJeff Mahoney 215449b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 215549b25e05SJeff Mahoney if (ret) { 215649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21576cf7f77eSWang Shilong goto scrub_continue; 215849b25e05SJeff Mahoney } 21594313b399SChris Mason 2160e02119d5SChris Mason /* 2161e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2162e02119d5SChris Mason * to go about their business 2163e02119d5SChris Mason */ 2164e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2165e02119d5SChris Mason 216611833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 21674313b399SChris Mason 21683204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 216913212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 217013212b54SZhao Lei 217115ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 21724a9d8bdeSMiao Xie /* 21734a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 21744a9d8bdeSMiao Xie * which can change it. 21754a9d8bdeSMiao Xie */ 21764a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 21772c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 21783de4586cSChris Mason 2179a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 218013c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2181a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2182a4abeea4SJosef Bacik 2183724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2184724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 218558176a96SJosef Bacik 21860860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2187b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2188b2b5ef5cSJan Kara 21891abe9b8aSliubo trace_btrfs_transaction_commit(root); 21901abe9b8aSliubo 2191a2de733cSArne Jansen btrfs_scrub_continue(root); 2192a2de733cSArne Jansen 21939ed74f2dSJosef Bacik if (current->journal_info == trans) 21949ed74f2dSJosef Bacik current->journal_info = NULL; 21959ed74f2dSJosef Bacik 21962c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 219724bbcf04SYan, Zheng 21988a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 21998a733013SZhao Lei current != root->fs_info->cleaner_kthread) 220024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 220124bbcf04SYan, Zheng 220279154b1bSChris Mason return ret; 220349b25e05SJeff Mahoney 22046cf7f77eSWang Shilong scrub_continue: 22056cf7f77eSWang Shilong btrfs_scrub_continue(root); 220649b25e05SJeff Mahoney cleanup_transaction: 22070e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 22084fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22090e721106SJosef Bacik trans->block_rsv = NULL; 2210c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 221149b25e05SJeff Mahoney if (current->journal_info == trans) 221249b25e05SJeff Mahoney current->journal_info = NULL; 22137b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 221449b25e05SJeff Mahoney 221549b25e05SJeff Mahoney return ret; 221679154b1bSChris Mason } 221779154b1bSChris Mason 2218d352ac68SChris Mason /* 22199d1a2a3aSDavid Sterba * return < 0 if error 22209d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 22219d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 22229d1a2a3aSDavid Sterba * 22239d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 22249d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 22259d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 22269d1a2a3aSDavid Sterba * few seconds later. 2227d352ac68SChris Mason */ 22289d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2229e9d0b13bSChris Mason { 22309d1a2a3aSDavid Sterba int ret; 22315d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2232e9d0b13bSChris Mason 2233a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 22349d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 22359d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 22369d1a2a3aSDavid Sterba return 0; 22379d1a2a3aSDavid Sterba } 22389d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 22399d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2240cfad392bSJosef Bacik list_del_init(&root->root_list); 2241a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 22425d4f98a2SYan Zheng 2243efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 224476dda93cSYan, Zheng 224516cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 224616cdcec7SMiao Xie 224776dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 224876dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 22492c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 225076dda93cSYan, Zheng else 22512c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 225232471dc2SDavid Sterba 22536596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2254e9d0b13bSChris Mason } 2255572d9ab7SDavid Sterba 2256572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2257572d9ab7SDavid Sterba { 2258572d9ab7SDavid Sterba unsigned long prev; 2259572d9ab7SDavid Sterba unsigned long bit; 2260572d9ab7SDavid Sterba 22616c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2262572d9ab7SDavid Sterba if (!prev) 2263572d9ab7SDavid Sterba return; 2264572d9ab7SDavid Sterba 22657e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 22667e1876acSDavid Sterba if (prev & bit) 22677e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22687e1876acSDavid Sterba prev &= ~bit; 22697e1876acSDavid Sterba 22707e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 22717e1876acSDavid Sterba if (prev & bit) 22727e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22737e1876acSDavid Sterba prev &= ~bit; 22747e1876acSDavid Sterba 2275d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2276d51033d0SDavid Sterba if (prev & bit) 2277d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2278d51033d0SDavid Sterba prev &= ~bit; 2279d51033d0SDavid Sterba 2280572d9ab7SDavid Sterba if (prev) 2281572d9ab7SDavid Sterba btrfs_warn(fs_info, 2282572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2283572d9ab7SDavid Sterba } 2284