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; 6408eab77ffSFilipe Manana 6418eab77ffSFilipe Manana return trans; 6428eab77ffSFilipe Manana } 6438407aa46SMiao Xie 64408e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6455aed1dd8SAlexandru Moise struct btrfs_root *root, 6465aed1dd8SAlexandru Moise unsigned int num_items) 6478407aa46SMiao Xie { 64808e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 64908e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 6508407aa46SMiao Xie } 6518407aa46SMiao Xie 6527a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 653f9295749SChris Mason { 654575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN, 655575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 656f9295749SChris Mason } 657f9295749SChris Mason 6587a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6590af3d00bSJosef Bacik { 660575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 661575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6620af3d00bSJosef Bacik } 6630af3d00bSJosef Bacik 6647a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6659ca9ee09SSage Weil { 666575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_USERSPACE, 667575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6689ca9ee09SSage Weil } 6699ca9ee09SSage Weil 670d4edf39bSMiao Xie /* 671d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 672d4edf39bSMiao Xie * 673d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 674d4edf39bSMiao Xie * don't want to start a new one. 675d4edf39bSMiao Xie * 676d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 677d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 678d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 679d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 680d4edf39bSMiao Xie * invoke 681d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 682d4edf39bSMiao Xie */ 683354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 68460376ce4SJosef Bacik { 685575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 686575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 68760376ce4SJosef Bacik } 68860376ce4SJosef Bacik 689d4edf39bSMiao Xie /* 69090b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 691d4edf39bSMiao Xie * 692d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 693d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 694d4edf39bSMiao Xie * complete. 695d4edf39bSMiao Xie */ 696d4edf39bSMiao Xie struct btrfs_trans_handle * 697d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 698d4edf39bSMiao Xie { 699d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 700d4edf39bSMiao Xie 701575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 702575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 703d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 704d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 705d4edf39bSMiao Xie 706d4edf39bSMiao Xie return trans; 707d4edf39bSMiao Xie } 708d4edf39bSMiao Xie 709d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 710b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 71189ce8a63SChris Mason struct btrfs_transaction *commit) 71289ce8a63SChris Mason { 7134a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 71489ce8a63SChris Mason } 71589ce8a63SChris Mason 71646204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 71746204592SSage Weil { 71846204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7198cd2807fSMiao Xie int ret = 0; 72046204592SSage Weil 72146204592SSage Weil if (transid) { 72246204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 723a4abeea4SJosef Bacik goto out; 72446204592SSage Weil 72546204592SSage Weil /* find specified transaction */ 726a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 72746204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 72846204592SSage Weil if (t->transid == transid) { 72946204592SSage Weil cur_trans = t; 730a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 7318cd2807fSMiao Xie ret = 0; 73246204592SSage Weil break; 73346204592SSage Weil } 7348cd2807fSMiao Xie if (t->transid > transid) { 7358cd2807fSMiao Xie ret = 0; 73646204592SSage Weil break; 73746204592SSage Weil } 7388cd2807fSMiao Xie } 739a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 74042383020SSage Weil 74142383020SSage Weil /* 74242383020SSage Weil * The specified transaction doesn't exist, or we 74342383020SSage Weil * raced with btrfs_commit_transaction 74442383020SSage Weil */ 74542383020SSage Weil if (!cur_trans) { 74642383020SSage Weil if (transid > root->fs_info->last_trans_committed) 74742383020SSage Weil ret = -EINVAL; 7488cd2807fSMiao Xie goto out; 74942383020SSage Weil } 75046204592SSage Weil } else { 75146204592SSage Weil /* find newest transaction that is committing | committed */ 752a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 75346204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 75446204592SSage Weil list) { 7554a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7564a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7573473f3c0SJosef Bacik break; 75846204592SSage Weil cur_trans = t; 759a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 76046204592SSage Weil break; 76146204592SSage Weil } 76246204592SSage Weil } 763a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 76446204592SSage Weil if (!cur_trans) 765a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 76646204592SSage Weil } 76746204592SSage Weil 76846204592SSage Weil wait_for_commit(root, cur_trans); 769724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 770a4abeea4SJosef Bacik out: 77146204592SSage Weil return ret; 77246204592SSage Weil } 77346204592SSage Weil 77437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 77537d1aeeeSChris Mason { 776a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 77737d1aeeeSChris Mason wait_current_trans(root); 77837d1aeeeSChris Mason } 77937d1aeeeSChris Mason 7808929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7818929ecfaSYan, Zheng struct btrfs_root *root) 7828929ecfaSYan, Zheng { 7831be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7840a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7851be41b78SJosef Bacik return 1; 78636ba022aSJosef Bacik 7871be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7888929ecfaSYan, Zheng } 7898929ecfaSYan, Zheng 7908929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7918929ecfaSYan, Zheng struct btrfs_root *root) 7928929ecfaSYan, Zheng { 7938929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7948929ecfaSYan, Zheng int updates; 79549b25e05SJeff Mahoney int err; 7968929ecfaSYan, Zheng 797a4abeea4SJosef Bacik smp_mb(); 7984a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7994a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8008929ecfaSYan, Zheng return 1; 8018929ecfaSYan, Zheng 8028929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 8038929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 80449b25e05SJeff Mahoney if (updates) { 80528ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 80649b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 80749b25e05SJeff Mahoney return err; 80849b25e05SJeff Mahoney } 8098929ecfaSYan, Zheng 8108929ecfaSYan, Zheng return should_end_transaction(trans, root); 8118929ecfaSYan, Zheng } 8128929ecfaSYan, Zheng 81389ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 814a698d075SMiao Xie struct btrfs_root *root, int throttle) 81579154b1bSChris Mason { 8168929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 817ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 8181be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 819a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8204edc2ca3SDave Jones int err = 0; 821a79b7d4bSChris Mason int must_run_delayed_refs = 0; 822d6e4a428SChris Mason 8233bbb24b2SJosef Bacik if (trans->use_count > 1) { 8243bbb24b2SJosef Bacik trans->use_count--; 8252a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8262a1eb461SJosef Bacik return 0; 8272a1eb461SJosef Bacik } 8282a1eb461SJosef Bacik 829b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 8304c13d758SJosef Bacik trans->block_rsv = NULL; 831c5567237SArne Jansen 832ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 833ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 834ea658badSJosef Bacik 835c3e69d58SChris Mason trans->delayed_ref_updates = 0; 836a79b7d4bSChris Mason if (!trans->sync) { 837a79b7d4bSChris Mason must_run_delayed_refs = 838a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 8390a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 840a79b7d4bSChris Mason 841a79b7d4bSChris Mason /* 842a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 843a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 844a79b7d4bSChris Mason */ 845a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 846a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 847a79b7d4bSChris Mason must_run_delayed_refs = 2; 84856bec294SChris Mason } 849bb721703SChris Mason 8500e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8510e721106SJosef Bacik trans->block_rsv = NULL; 85256bec294SChris Mason 853ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 854ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 855ea658badSJosef Bacik 8564fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8574fbcdf66SFilipe Manana 858a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8594a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8604a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8614a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8624a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8634a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8644a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 865a4abeea4SJosef Bacik } 8668929ecfaSYan, Zheng 8674a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8683bbb24b2SJosef Bacik if (throttle) 8698929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8703bbb24b2SJosef Bacik else 8718929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8728929ecfaSYan, Zheng } 8738929ecfaSYan, Zheng 8740860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8756df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8766df7881aSJosef Bacik 8778929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 87813c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 87913c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8800860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 88189ce8a63SChris Mason 882a83342aaSDavid Sterba /* 883a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 884a83342aaSDavid Sterba */ 88599d16cbcSSage Weil smp_mb(); 88679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 88779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 888724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8899ed74f2dSJosef Bacik 8909ed74f2dSJosef Bacik if (current->journal_info == trans) 8919ed74f2dSJosef Bacik current->journal_info = NULL; 892ab78c84dSChris Mason 89324bbcf04SYan, Zheng if (throttle) 89424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 89524bbcf04SYan, Zheng 89649b25e05SJeff Mahoney if (trans->aborted || 8974e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 8984e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8994edc2ca3SDave Jones err = -EIO; 9004e121c06SJosef Bacik } 901edf39272SJan Schmidt assert_qgroups_uptodate(trans); 90249b25e05SJeff Mahoney 9034edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 904a79b7d4bSChris Mason if (must_run_delayed_refs) { 905a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 906a79b7d4bSChris Mason must_run_delayed_refs == 1); 907a79b7d4bSChris Mason } 9084edc2ca3SDave Jones return err; 90979154b1bSChris Mason } 91079154b1bSChris Mason 91189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 91289ce8a63SChris Mason struct btrfs_root *root) 91389ce8a63SChris Mason { 91498ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 91589ce8a63SChris Mason } 91689ce8a63SChris Mason 91789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 91889ce8a63SChris Mason struct btrfs_root *root) 91989ce8a63SChris Mason { 92098ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 92116cdcec7SMiao Xie } 92216cdcec7SMiao Xie 923d352ac68SChris Mason /* 924d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 925d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 926690587d1SChris Mason * those extents are sent to disk but does not wait on them 927d352ac68SChris Mason */ 928690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 9298cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 93079154b1bSChris Mason { 931777e6bd7SChris Mason int err = 0; 9327c4452b9SChris Mason int werr = 0; 9331728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 934e6138876SJosef Bacik struct extent_state *cached_state = NULL; 935777e6bd7SChris Mason u64 start = 0; 9365f39d397SChris Mason u64 end; 9377c4452b9SChris Mason 9381728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 939e6138876SJosef Bacik mark, &cached_state)) { 940663dfbb0SFilipe Manana bool wait_writeback = false; 941663dfbb0SFilipe Manana 942663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 943663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 944e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 945663dfbb0SFilipe Manana /* 946663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 947663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 948663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 949663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 950663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 951663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 952663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 953663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 954663dfbb0SFilipe Manana * (and without errors). 955663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 956663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 957663dfbb0SFilipe Manana */ 958663dfbb0SFilipe Manana if (err == -ENOMEM) { 959663dfbb0SFilipe Manana err = 0; 960663dfbb0SFilipe Manana wait_writeback = true; 961663dfbb0SFilipe Manana } 962663dfbb0SFilipe Manana if (!err) 9631728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9647c4452b9SChris Mason if (err) 9657c4452b9SChris Mason werr = err; 966663dfbb0SFilipe Manana else if (wait_writeback) 967663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 968e38e2ed7SFilipe Manana free_extent_state(cached_state); 969663dfbb0SFilipe Manana cached_state = NULL; 9701728366eSJosef Bacik cond_resched(); 9711728366eSJosef Bacik start = end + 1; 9727c4452b9SChris Mason } 973690587d1SChris Mason return werr; 974690587d1SChris Mason } 975690587d1SChris Mason 976690587d1SChris Mason /* 977690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 978690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 979690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 980690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 981690587d1SChris Mason */ 982690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9838cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 984690587d1SChris Mason { 985690587d1SChris Mason int err = 0; 986690587d1SChris Mason int werr = 0; 9871728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 988e6138876SJosef Bacik struct extent_state *cached_state = NULL; 989690587d1SChris Mason u64 start = 0; 990690587d1SChris Mason u64 end; 991656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 992656f30dbSFilipe Manana bool errors = false; 993690587d1SChris Mason 9941728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 995e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 996663dfbb0SFilipe Manana /* 997663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 998663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 999663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1000663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1001663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1002663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1003663dfbb0SFilipe Manana */ 1004663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1005663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1006e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1007663dfbb0SFilipe Manana if (err == -ENOMEM) 1008663dfbb0SFilipe Manana err = 0; 1009663dfbb0SFilipe Manana if (!err) 10101728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1011777e6bd7SChris Mason if (err) 1012777e6bd7SChris Mason werr = err; 1013e38e2ed7SFilipe Manana free_extent_state(cached_state); 1014e38e2ed7SFilipe Manana cached_state = NULL; 1015777e6bd7SChris Mason cond_resched(); 10161728366eSJosef Bacik start = end + 1; 1017777e6bd7SChris Mason } 10187c4452b9SChris Mason if (err) 10197c4452b9SChris Mason werr = err; 1020656f30dbSFilipe Manana 1021656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1022656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 1023656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 1024656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1025656f30dbSFilipe Manana errors = true; 1026656f30dbSFilipe Manana 1027656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 1028656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 1029656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1030656f30dbSFilipe Manana errors = true; 1031656f30dbSFilipe Manana } else { 1032656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 1033656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1034656f30dbSFilipe Manana errors = true; 1035656f30dbSFilipe Manana } 1036656f30dbSFilipe Manana 1037656f30dbSFilipe Manana if (errors && !werr) 1038656f30dbSFilipe Manana werr = -EIO; 1039656f30dbSFilipe Manana 10407c4452b9SChris Mason return werr; 104179154b1bSChris Mason } 104279154b1bSChris Mason 1043690587d1SChris Mason /* 1044690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1045690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1046690587d1SChris Mason * those extents are on disk for transaction or log commit 1047690587d1SChris Mason */ 1048171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10498cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1050690587d1SChris Mason { 1051690587d1SChris Mason int ret; 1052690587d1SChris Mason int ret2; 1053c6adc9ccSMiao Xie struct blk_plug plug; 1054690587d1SChris Mason 1055c6adc9ccSMiao Xie blk_start_plug(&plug); 10568cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1057c6adc9ccSMiao Xie blk_finish_plug(&plug); 10588cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1059bf0da8c1SChris Mason 1060bf0da8c1SChris Mason if (ret) 1061bf0da8c1SChris Mason return ret; 1062bf0da8c1SChris Mason if (ret2) 1063bf0da8c1SChris Mason return ret2; 1064bf0da8c1SChris Mason return 0; 1065690587d1SChris Mason } 1066690587d1SChris Mason 1067663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1068d0c803c4SChris Mason struct btrfs_root *root) 1069d0c803c4SChris Mason { 1070663dfbb0SFilipe Manana int ret; 1071663dfbb0SFilipe Manana 1072663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10738cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10748cef4e16SYan, Zheng EXTENT_DIRTY); 1075663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1076663dfbb0SFilipe Manana 1077663dfbb0SFilipe Manana return ret; 1078d0c803c4SChris Mason } 1079d0c803c4SChris Mason 1080d352ac68SChris Mason /* 1081d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1082d352ac68SChris Mason * 1083d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1084d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1085d352ac68SChris Mason * allocation tree. 1086d352ac68SChris Mason * 1087d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1088d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1089d352ac68SChris Mason */ 10900b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 109179154b1bSChris Mason struct btrfs_root *root) 109279154b1bSChris Mason { 109379154b1bSChris Mason int ret; 10940b86a832SChris Mason u64 old_root_bytenr; 109586b9f2ecSYan, Zheng u64 old_root_used; 10960b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 109779154b1bSChris Mason 109886b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 109956bec294SChris Mason 110079154b1bSChris Mason while (1) { 11010b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 110286b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1103ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 110479154b1bSChris Mason break; 110587ef2bb4SChris Mason 11065d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 110779154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11080b86a832SChris Mason &root->root_key, 11090b86a832SChris Mason &root->root_item); 111049b25e05SJeff Mahoney if (ret) 111149b25e05SJeff Mahoney return ret; 111256bec294SChris Mason 111386b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1114e7070be1SJosef Bacik } 1115276e680dSYan Zheng 11160b86a832SChris Mason return 0; 11170b86a832SChris Mason } 11180b86a832SChris Mason 1119d352ac68SChris Mason /* 1120d352ac68SChris Mason * update all the cowonly tree roots on disk 112149b25e05SJeff Mahoney * 112249b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 112349b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 112449b25e05SJeff Mahoney * to clean up the delayed refs. 1125d352ac68SChris Mason */ 11265d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11270b86a832SChris Mason struct btrfs_root *root) 11280b86a832SChris Mason { 11290b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1130ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11311bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11320b86a832SChris Mason struct list_head *next; 113384234f3aSYan Zheng struct extent_buffer *eb; 113456bec294SChris Mason int ret; 113584234f3aSYan Zheng 113684234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 113749b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 113849b25e05SJeff Mahoney 0, &eb); 113984234f3aSYan Zheng btrfs_tree_unlock(eb); 114084234f3aSYan Zheng free_extent_buffer(eb); 11410b86a832SChris Mason 114249b25e05SJeff Mahoney if (ret) 114349b25e05SJeff Mahoney return ret; 114449b25e05SJeff Mahoney 114556bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 114649b25e05SJeff Mahoney if (ret) 114749b25e05SJeff Mahoney return ret; 114887ef2bb4SChris Mason 1149733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1150c16ce190SJosef Bacik if (ret) 1151c16ce190SJosef Bacik return ret; 11528dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1153c16ce190SJosef Bacik if (ret) 1154c16ce190SJosef Bacik return ret; 1155546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1156c16ce190SJosef Bacik if (ret) 1157c16ce190SJosef Bacik return ret; 1158546adb0dSJan Schmidt 1159dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1160dcdf7f6dSJosef Bacik if (ret) 1161dcdf7f6dSJosef Bacik return ret; 1162dcdf7f6dSJosef Bacik 1163546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1164546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1165c16ce190SJosef Bacik if (ret) 1166c16ce190SJosef Bacik return ret; 1167ea526d18SJosef Bacik again: 11680b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11690b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11700b86a832SChris Mason list_del_init(next); 11710b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1172e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 117387ef2bb4SChris Mason 11749e351cc8SJosef Bacik if (root != fs_info->extent_root) 11759e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11769e351cc8SJosef Bacik &trans->transaction->switch_commits); 117749b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 117849b25e05SJeff Mahoney if (ret) 117949b25e05SJeff Mahoney return ret; 1180ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1181ea526d18SJosef Bacik if (ret) 1182ea526d18SJosef Bacik return ret; 118379154b1bSChris Mason } 1184276e680dSYan Zheng 11851bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1186ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1187ea526d18SJosef Bacik if (ret) 1188ea526d18SJosef Bacik return ret; 1189ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1190ea526d18SJosef Bacik if (ret) 1191ea526d18SJosef Bacik return ret; 1192ea526d18SJosef Bacik } 1193ea526d18SJosef Bacik 1194ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1195ea526d18SJosef Bacik goto again; 1196ea526d18SJosef Bacik 11979e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11989e351cc8SJosef Bacik &trans->transaction->switch_commits); 11998dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12008dabb742SStefan Behrens 120179154b1bSChris Mason return 0; 120279154b1bSChris Mason } 120379154b1bSChris Mason 1204d352ac68SChris Mason /* 1205d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1206d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1207d352ac68SChris Mason * be deleted 1208d352ac68SChris Mason */ 1209cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12105eda7b5eSChris Mason { 1211a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1212cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12139d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1214a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 12155eda7b5eSChris Mason } 12165eda7b5eSChris Mason 1217d352ac68SChris Mason /* 12185d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1219d352ac68SChris Mason */ 12205d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12215d4f98a2SYan Zheng struct btrfs_root *root) 12220f7d52f4SChris Mason { 12230f7d52f4SChris Mason struct btrfs_root *gang[8]; 12245d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 12250f7d52f4SChris Mason int i; 12260f7d52f4SChris Mason int ret; 122754aa1f4dSChris Mason int err = 0; 122854aa1f4dSChris Mason 1229a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12300f7d52f4SChris Mason while (1) { 12315d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12325d4f98a2SYan Zheng (void **)gang, 0, 12330f7d52f4SChris Mason ARRAY_SIZE(gang), 12340f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12350f7d52f4SChris Mason if (ret == 0) 12360f7d52f4SChris Mason break; 12370f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12380f7d52f4SChris Mason root = gang[i]; 12395d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12402619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12410f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1242a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 124331153d81SYan Zheng 1244e02119d5SChris Mason btrfs_free_log(trans, root); 12455d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1246d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1247e02119d5SChris Mason 124882d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 124982d5902dSLi Zefan 1250f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 125127cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1252c7548af6SChris Mason smp_mb__after_atomic(); 1253f1ebcc74SLiu Bo 1254978d910dSYan Zheng if (root->commit_root != root->node) { 12559e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12569e351cc8SJosef Bacik &trans->transaction->switch_commits); 1257978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1258978d910dSYan Zheng root->node); 1259978d910dSYan Zheng } 126031153d81SYan Zheng 12615d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12620f7d52f4SChris Mason &root->root_key, 12630f7d52f4SChris Mason &root->root_item); 1264a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 126554aa1f4dSChris Mason if (err) 126654aa1f4dSChris Mason break; 12677174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12680f7d52f4SChris Mason } 12699f3a7427SChris Mason } 1270a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 127154aa1f4dSChris Mason return err; 12720f7d52f4SChris Mason } 12730f7d52f4SChris Mason 1274d352ac68SChris Mason /* 1275de78b51aSEric Sandeen * defrag a given btree. 1276de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1277d352ac68SChris Mason */ 1278de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1279e9d0b13bSChris Mason { 1280e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1281e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12828929ecfaSYan, Zheng int ret; 1283e9d0b13bSChris Mason 128427cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1285e9d0b13bSChris Mason return 0; 12868929ecfaSYan, Zheng 12876b80053dSChris Mason while (1) { 12888929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12898929ecfaSYan, Zheng if (IS_ERR(trans)) 12908929ecfaSYan, Zheng return PTR_ERR(trans); 12918929ecfaSYan, Zheng 1292de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12938929ecfaSYan, Zheng 1294e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1295b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1296e9d0b13bSChris Mason cond_resched(); 1297e9d0b13bSChris Mason 12987841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1299e9d0b13bSChris Mason break; 1300210549ebSDavid Sterba 1301210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1302efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1303210549ebSDavid Sterba ret = -EAGAIN; 1304210549ebSDavid Sterba break; 1305210549ebSDavid Sterba } 1306e9d0b13bSChris Mason } 130727cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13088929ecfaSYan, Zheng return ret; 1309e9d0b13bSChris Mason } 1310e9d0b13bSChris Mason 1311d352ac68SChris Mason /* 1312d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1313aec8030aSMiao Xie * transaction commit. This does the actual creation. 1314aec8030aSMiao Xie * 1315aec8030aSMiao Xie * Note: 1316aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1317aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1318aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1319d352ac68SChris Mason */ 132080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13213063d29fSChris Mason struct btrfs_fs_info *fs_info, 13223063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13233063d29fSChris Mason { 13243063d29fSChris Mason struct btrfs_key key; 132580b6794dSChris Mason struct btrfs_root_item *new_root_item; 13263063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13273063d29fSChris Mason struct btrfs_root *root = pending->root; 13286bdb72deSSage Weil struct btrfs_root *parent_root; 132998c9942aSLiu Bo struct btrfs_block_rsv *rsv; 13306bdb72deSSage Weil struct inode *parent_inode; 133142874b3dSMiao Xie struct btrfs_path *path; 133242874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1333a22285a6SYan, Zheng struct dentry *dentry; 13343063d29fSChris Mason struct extent_buffer *tmp; 1335925baeddSChris Mason struct extent_buffer *old; 1336*04b285f3SDeepa Dinamani struct timespec cur_time; 1337aec8030aSMiao Xie int ret = 0; 1338d68fc57bSYan, Zheng u64 to_reserve = 0; 13396bdb72deSSage Weil u64 index = 0; 1340a22285a6SYan, Zheng u64 objectid; 1341b83cc969SLi Zefan u64 root_flags; 13428ea05e3aSAlexander Block uuid_le new_uuid; 13433063d29fSChris Mason 13448546b570SDavid Sterba ASSERT(pending->path); 13458546b570SDavid Sterba path = pending->path; 134642874b3dSMiao Xie 1347b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1348b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1349a22285a6SYan, Zheng 1350aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1351aec8030aSMiao Xie if (pending->error) 13526fa9700eSMiao Xie goto no_free_objectid; 13533063d29fSChris Mason 1354d6726335SQu Wenruo /* 1355d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1356d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1357d6726335SQu Wenruo */ 1358d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1359d6726335SQu Wenruo 1360147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1361d68fc57bSYan, Zheng 1362d68fc57bSYan, Zheng if (to_reserve > 0) { 1363aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1364aec8030aSMiao Xie &pending->block_rsv, 136508e007d2SMiao Xie to_reserve, 136608e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1367aec8030aSMiao Xie if (pending->error) 1368d6726335SQu Wenruo goto clear_skip_qgroup; 1369d68fc57bSYan, Zheng } 1370d68fc57bSYan, Zheng 13713063d29fSChris Mason key.objectid = objectid; 1372a22285a6SYan, Zheng key.offset = (u64)-1; 1373a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 13743063d29fSChris Mason 13756fa9700eSMiao Xie rsv = trans->block_rsv; 1376a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 13772382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 13786bdb72deSSage Weil 1379a22285a6SYan, Zheng dentry = pending->dentry; 1380e9662f70SMiao Xie parent_inode = pending->dir; 1381a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 13827585717fSChris Mason record_root_in_trans(trans, parent_root); 1383a22285a6SYan, Zheng 1384*04b285f3SDeepa Dinamani cur_time = current_fs_time(parent_inode->i_sb); 1385*04b285f3SDeepa Dinamani 13866bdb72deSSage Weil /* 13876bdb72deSSage Weil * insert the directory item 13886bdb72deSSage Weil */ 13896bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 139049b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 139142874b3dSMiao Xie 139242874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 139342874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 139442874b3dSMiao Xie btrfs_ino(parent_inode), 139542874b3dSMiao Xie dentry->d_name.name, 139642874b3dSMiao Xie dentry->d_name.len, 0); 139742874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1398fe66a05aSChris Mason pending->error = -EEXIST; 1399aec8030aSMiao Xie goto dir_item_existed; 140042874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 140142874b3dSMiao Xie ret = PTR_ERR(dir_item); 14028732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14038732d44fSMiao Xie goto fail; 140479787eaaSJeff Mahoney } 140542874b3dSMiao Xie btrfs_release_path(path); 14066bdb72deSSage Weil 1407e999376fSChris Mason /* 1408e999376fSChris Mason * pull in the delayed directory update 1409e999376fSChris Mason * and the delayed inode item 1410e999376fSChris Mason * otherwise we corrupt the FS during 1411e999376fSChris Mason * snapshot 1412e999376fSChris Mason */ 1413e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 14148732d44fSMiao Xie if (ret) { /* Transaction aborted */ 14158732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14168732d44fSMiao Xie goto fail; 14178732d44fSMiao Xie } 1418e999376fSChris Mason 14197585717fSChris Mason record_root_in_trans(trans, root); 14206bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14216bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 142208fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14236bdb72deSSage Weil 1424b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1425b83cc969SLi Zefan if (pending->readonly) 1426b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1427b83cc969SLi Zefan else 1428b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1429b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1430b83cc969SLi Zefan 14318ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 14328ea05e3aSAlexander Block trans->transid); 14338ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 14348ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 14358ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 14368ea05e3aSAlexander Block BTRFS_UUID_SIZE); 143770023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 143870023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 143970023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14408ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14418ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 14428ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 14438ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 144470023da2SStefan Behrens } 14453cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 14463cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 144770023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 14488ea05e3aSAlexander Block 1449925baeddSChris Mason old = btrfs_lock_root_node(root); 145049b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 145179787eaaSJeff Mahoney if (ret) { 145279787eaaSJeff Mahoney btrfs_tree_unlock(old); 145379787eaaSJeff Mahoney free_extent_buffer(old); 14548732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14558732d44fSMiao Xie goto fail; 145679787eaaSJeff Mahoney } 145749b25e05SJeff Mahoney 14585d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 14593063d29fSChris Mason 146049b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 146179787eaaSJeff Mahoney /* clean up in any case */ 1462925baeddSChris Mason btrfs_tree_unlock(old); 1463925baeddSChris Mason free_extent_buffer(old); 14648732d44fSMiao Xie if (ret) { 14658732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14668732d44fSMiao Xie goto fail; 14678732d44fSMiao Xie } 1468f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 146927cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1470f1ebcc74SLiu Bo smp_wmb(); 1471f1ebcc74SLiu Bo 14725d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1473a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1474a22285a6SYan, Zheng key.offset = trans->transid; 1475a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1476925baeddSChris Mason btrfs_tree_unlock(tmp); 14773063d29fSChris Mason free_extent_buffer(tmp); 14788732d44fSMiao Xie if (ret) { 14798732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14808732d44fSMiao Xie goto fail; 14818732d44fSMiao Xie } 14820660b5afSChris Mason 1483a22285a6SYan, Zheng /* 1484a22285a6SYan, Zheng * insert root back/forward references 1485a22285a6SYan, Zheng */ 1486a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1487a22285a6SYan, Zheng parent_root->root_key.objectid, 148833345d01SLi Zefan btrfs_ino(parent_inode), index, 1489a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14908732d44fSMiao Xie if (ret) { 14918732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14928732d44fSMiao Xie goto fail; 14938732d44fSMiao Xie } 1494a22285a6SYan, Zheng 1495a22285a6SYan, Zheng key.offset = (u64)-1; 1496a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 149779787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 149879787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 14998732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15008732d44fSMiao Xie goto fail; 150179787eaaSJeff Mahoney } 1502d68fc57bSYan, Zheng 150349b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15048732d44fSMiao Xie if (ret) { 15058732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15068732d44fSMiao Xie goto fail; 15078732d44fSMiao Xie } 1508361048f5SMiao Xie 1509361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 15108732d44fSMiao Xie if (ret) { 15118732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15128732d44fSMiao Xie goto fail; 15138732d44fSMiao Xie } 151442874b3dSMiao Xie 151542874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 151642874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 151742874b3dSMiao Xie parent_inode, &key, 151842874b3dSMiao Xie BTRFS_FT_DIR, index); 151942874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 15209c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 15218732d44fSMiao Xie if (ret) { 15228732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15238732d44fSMiao Xie goto fail; 15248732d44fSMiao Xie } 152542874b3dSMiao Xie 152642874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 152742874b3dSMiao Xie dentry->d_name.len * 2); 1528*04b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1529*04b285f3SDeepa Dinamani current_fs_time(parent_inode->i_sb); 1530be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1531dd5f9615SStefan Behrens if (ret) { 15328732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1533dd5f9615SStefan Behrens goto fail; 1534dd5f9615SStefan Behrens } 1535dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1536dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1537dd5f9615SStefan Behrens if (ret) { 1538dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1539dd5f9615SStefan Behrens goto fail; 1540dd5f9615SStefan Behrens } 1541dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1542dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1543dd5f9615SStefan Behrens new_root_item->received_uuid, 1544dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1545dd5f9615SStefan Behrens objectid); 1546dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1547dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1548dd5f9615SStefan Behrens goto fail; 1549dd5f9615SStefan Behrens } 1550dd5f9615SStefan Behrens } 1551d6726335SQu Wenruo 1552d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1553d6726335SQu Wenruo if (ret) { 1554d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1555d6726335SQu Wenruo goto fail; 1556d6726335SQu Wenruo } 1557d6726335SQu Wenruo 1558d6726335SQu Wenruo /* 1559d6726335SQu Wenruo * account qgroup counters before qgroup_inherit() 1560d6726335SQu Wenruo */ 1561d6726335SQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 1562d6726335SQu Wenruo if (ret) 1563d6726335SQu Wenruo goto fail; 1564d6726335SQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 1565d6726335SQu Wenruo if (ret) 1566d6726335SQu Wenruo goto fail; 1567d6726335SQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 1568d6726335SQu Wenruo root->root_key.objectid, 1569d6726335SQu Wenruo objectid, pending->inherit); 1570d6726335SQu Wenruo if (ret) { 1571d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1572d6726335SQu Wenruo goto fail; 1573d6726335SQu Wenruo } 1574d6726335SQu Wenruo 15753063d29fSChris Mason fail: 1576aec8030aSMiao Xie pending->error = ret; 1577aec8030aSMiao Xie dir_item_existed: 157898c9942aSLiu Bo trans->block_rsv = rsv; 15792382c5ccSLiu Bo trans->bytes_reserved = 0; 1580d6726335SQu Wenruo clear_skip_qgroup: 1581d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 15826fa9700eSMiao Xie no_free_objectid: 15836fa9700eSMiao Xie kfree(new_root_item); 1584b0c0ea63SDavid Sterba pending->root_item = NULL; 158542874b3dSMiao Xie btrfs_free_path(path); 15868546b570SDavid Sterba pending->path = NULL; 15878546b570SDavid Sterba 158849b25e05SJeff Mahoney return ret; 15893063d29fSChris Mason } 15903063d29fSChris Mason 1591d352ac68SChris Mason /* 1592d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1593d352ac68SChris Mason */ 159480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 15953063d29fSChris Mason struct btrfs_fs_info *fs_info) 15963063d29fSChris Mason { 1597aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 15983063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1599aec8030aSMiao Xie int ret = 0; 16003de4586cSChris Mason 1601aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1602aec8030aSMiao Xie list_del(&pending->list); 1603aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1604aec8030aSMiao Xie if (ret) 1605aec8030aSMiao Xie break; 1606aec8030aSMiao Xie } 1607aec8030aSMiao Xie return ret; 16083de4586cSChris Mason } 16093de4586cSChris Mason 16105d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 16115d4f98a2SYan Zheng { 16125d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16135d4f98a2SYan Zheng struct btrfs_super_block *super; 16145d4f98a2SYan Zheng 16156c41761fSDavid Sterba super = root->fs_info->super_copy; 16165d4f98a2SYan Zheng 16175d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 16185d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 16195d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 16205d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 16215d4f98a2SYan Zheng 16225d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 16235d4f98a2SYan Zheng super->root = root_item->bytenr; 16245d4f98a2SYan Zheng super->generation = root_item->generation; 16255d4f98a2SYan Zheng super->root_level = root_item->level; 162673bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 16270af3d00bSJosef Bacik super->cache_generation = root_item->generation; 162870f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 162926432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 16305d4f98a2SYan Zheng } 16315d4f98a2SYan Zheng 1632f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1633f36f3042SChris Mason { 16344a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1635f36f3042SChris Mason int ret = 0; 16364a9d8bdeSMiao Xie 1637a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16384a9d8bdeSMiao Xie trans = info->running_transaction; 16394a9d8bdeSMiao Xie if (trans) 16404a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1641a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1642f36f3042SChris Mason return ret; 1643f36f3042SChris Mason } 1644f36f3042SChris Mason 16458929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16468929ecfaSYan, Zheng { 16474a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16488929ecfaSYan, Zheng int ret = 0; 16494a9d8bdeSMiao Xie 1650a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16514a9d8bdeSMiao Xie trans = info->running_transaction; 16524a9d8bdeSMiao Xie if (trans) 16534a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1654a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 16558929ecfaSYan, Zheng return ret; 16568929ecfaSYan, Zheng } 16578929ecfaSYan, Zheng 1658bb9c12c9SSage Weil /* 1659bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1660bb9c12c9SSage Weil * transaction joins 1661bb9c12c9SSage Weil */ 1662bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1663bb9c12c9SSage Weil struct btrfs_transaction *trans) 1664bb9c12c9SSage Weil { 16654a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1666501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1667501407aaSJosef Bacik trans->aborted); 1668bb9c12c9SSage Weil } 1669bb9c12c9SSage Weil 1670bb9c12c9SSage Weil /* 1671bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1672bb9c12c9SSage Weil * caller holds ref. 1673bb9c12c9SSage Weil */ 1674bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1675bb9c12c9SSage Weil struct btrfs_transaction *trans) 1676bb9c12c9SSage Weil { 167772d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1678501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1679501407aaSJosef Bacik trans->aborted); 1680bb9c12c9SSage Weil } 1681bb9c12c9SSage Weil 1682bb9c12c9SSage Weil /* 1683bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1684bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1685bb9c12c9SSage Weil */ 1686bb9c12c9SSage Weil struct btrfs_async_commit { 1687bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1688bb9c12c9SSage Weil struct btrfs_root *root; 16897892b5afSMiao Xie struct work_struct work; 1690bb9c12c9SSage Weil }; 1691bb9c12c9SSage Weil 1692bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1693bb9c12c9SSage Weil { 1694bb9c12c9SSage Weil struct btrfs_async_commit *ac = 16957892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1696bb9c12c9SSage Weil 16976fc4e354SSage Weil /* 16986fc4e354SSage Weil * We've got freeze protection passed with the transaction. 16996fc4e354SSage Weil * Tell lockdep about it. 17006fc4e354SSage Weil */ 1701b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1702bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 17036fc4e354SSage Weil 1704e209db7aSSage Weil current->journal_info = ac->newtrans; 1705e209db7aSSage Weil 1706bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1707bb9c12c9SSage Weil kfree(ac); 1708bb9c12c9SSage Weil } 1709bb9c12c9SSage Weil 1710bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1711bb9c12c9SSage Weil struct btrfs_root *root, 1712bb9c12c9SSage Weil int wait_for_unblock) 1713bb9c12c9SSage Weil { 1714bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1715bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1716bb9c12c9SSage Weil 1717bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1718db5b493aSTsutomu Itoh if (!ac) 1719db5b493aSTsutomu Itoh return -ENOMEM; 1720bb9c12c9SSage Weil 17217892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1722bb9c12c9SSage Weil ac->root = root; 17237a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 17243612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17253612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17263612b495STsutomu Itoh kfree(ac); 17273612b495STsutomu Itoh return err; 17283612b495STsutomu Itoh } 1729bb9c12c9SSage Weil 1730bb9c12c9SSage Weil /* take transaction reference */ 1731bb9c12c9SSage Weil cur_trans = trans->transaction; 173213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1733bb9c12c9SSage Weil 1734bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 17356fc4e354SSage Weil 17366fc4e354SSage Weil /* 17376fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17386fc4e354SSage Weil * async commit thread will be the one to unlock it. 17396fc4e354SSage Weil */ 1740b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1741bee9182dSOleg Nesterov __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 17426fc4e354SSage Weil 17437892b5afSMiao Xie schedule_work(&ac->work); 1744bb9c12c9SSage Weil 1745bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1746bb9c12c9SSage Weil if (wait_for_unblock) 1747bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1748bb9c12c9SSage Weil else 1749bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1750bb9c12c9SSage Weil 175138e88054SSage Weil if (current->journal_info == trans) 175238e88054SSage Weil current->journal_info = NULL; 175338e88054SSage Weil 1754724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1755bb9c12c9SSage Weil return 0; 1756bb9c12c9SSage Weil } 1757bb9c12c9SSage Weil 175849b25e05SJeff Mahoney 175949b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 17607b8b92afSJosef Bacik struct btrfs_root *root, int err) 176149b25e05SJeff Mahoney { 176249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1763f094ac32SLiu Bo DEFINE_WAIT(wait); 176449b25e05SJeff Mahoney 176549b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 176649b25e05SJeff Mahoney 17677b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 17687b8b92afSJosef Bacik 176949b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 177066b6135bSLiu Bo 177125d8c284SMiao Xie /* 177225d8c284SMiao Xie * If the transaction is removed from the list, it means this 177325d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 177425d8c284SMiao Xie * to call the cleanup function. 177525d8c284SMiao Xie */ 177625d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 177766b6135bSLiu Bo 177849b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1779d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 17804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1781f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1782f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1783f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1784f094ac32SLiu Bo 1785f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1786d7096fc3SJosef Bacik } 178749b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 178849b25e05SJeff Mahoney 178949b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 179049b25e05SJeff Mahoney 17914a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17924a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 17934a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 17944a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 17954a9d8bdeSMiao Xie 1796e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1797e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1798724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1799724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 180049b25e05SJeff Mahoney 180149b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 180249b25e05SJeff Mahoney 180349b25e05SJeff Mahoney if (current->journal_info == trans) 180449b25e05SJeff Mahoney current->journal_info = NULL; 1805c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 180649b25e05SJeff Mahoney 180749b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 180849b25e05SJeff Mahoney } 180949b25e05SJeff Mahoney 181082436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 181182436617SMiao Xie { 181282436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 18136c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 181482436617SMiao Xie return 0; 181582436617SMiao Xie } 181682436617SMiao Xie 181782436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 181882436617SMiao Xie { 181982436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1820b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 182182436617SMiao Xie } 182282436617SMiao Xie 182350d9aa99SJosef Bacik static inline void 1824161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 182550d9aa99SJosef Bacik { 1826161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1827161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 182850d9aa99SJosef Bacik } 182950d9aa99SJosef Bacik 183079154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 183179154b1bSChris Mason struct btrfs_root *root) 183279154b1bSChris Mason { 183349b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 18348fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1835656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 183625287e0aSMiao Xie int ret; 183779154b1bSChris Mason 18388d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 18398d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 184025287e0aSMiao Xie ret = cur_trans->aborted; 1841e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1842e4a2bcacSJosef Bacik return ret; 184325287e0aSMiao Xie } 184449b25e05SJeff Mahoney 184556bec294SChris Mason /* make a pass through all the delayed refs we have so far 184656bec294SChris Mason * any runnings procs may add more while we are here 184756bec294SChris Mason */ 184856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1849e4a2bcacSJosef Bacik if (ret) { 1850e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1851e4a2bcacSJosef Bacik return ret; 1852e4a2bcacSJosef Bacik } 185356bec294SChris Mason 18540e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18550e721106SJosef Bacik trans->block_rsv = NULL; 18560e721106SJosef Bacik 1857b7ec40d7SChris Mason cur_trans = trans->transaction; 185849b25e05SJeff Mahoney 185956bec294SChris Mason /* 186056bec294SChris Mason * set the flushing flag so procs in this transaction have to 186156bec294SChris Mason * start sending their work down. 186256bec294SChris Mason */ 1863b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 18641be41b78SJosef Bacik smp_wmb(); 186556bec294SChris Mason 1866ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1867ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1868ea658badSJosef Bacik 1869c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1870e4a2bcacSJosef Bacik if (ret) { 1871e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1872e4a2bcacSJosef Bacik return ret; 1873e4a2bcacSJosef Bacik } 187456bec294SChris Mason 18753204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 18761bbc621eSChris Mason int run_it = 0; 18771bbc621eSChris Mason 18781bbc621eSChris Mason /* this mutex is also taken before trying to set 18791bbc621eSChris Mason * block groups readonly. We need to make sure 18801bbc621eSChris Mason * that nobody has set a block group readonly 18811bbc621eSChris Mason * after a extents from that block group have been 18821bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 18831bbc621eSChris Mason * will wait for the transaction to commit if it 18843204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 18851bbc621eSChris Mason * 18863204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 18873204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 18881bbc621eSChris Mason * hurt to have more than one go through, but there's no 18891bbc621eSChris Mason * real advantage to it either. 18901bbc621eSChris Mason */ 18911bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 18923204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 18933204d33cSJosef Bacik &cur_trans->flags)) 18941bbc621eSChris Mason run_it = 1; 18951bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 18961bbc621eSChris Mason 18971bbc621eSChris Mason if (run_it) 18981bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 18991bbc621eSChris Mason } 19001bbc621eSChris Mason if (ret) { 19011bbc621eSChris Mason btrfs_end_transaction(trans, root); 19021bbc621eSChris Mason return ret; 19031bbc621eSChris Mason } 19041bbc621eSChris Mason 19054a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 19064a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 19074a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 190813c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 190949b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1910ccd467d6SChris Mason 1911b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 191215ee9bc7SJosef Bacik 1913b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1914b4924a0fSLiu Bo ret = cur_trans->aborted; 1915b4924a0fSLiu Bo 1916724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 191715ee9bc7SJosef Bacik 191849b25e05SJeff Mahoney return ret; 191979154b1bSChris Mason } 19204313b399SChris Mason 19214a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1922bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1923bb9c12c9SSage Weil 1924ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1925ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1926ccd467d6SChris Mason struct btrfs_transaction, list); 19274a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 192813c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1929a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1930ccd467d6SChris Mason 1931ccd467d6SChris Mason wait_for_commit(root, prev_trans); 19321f9b8c8fSFilipe Manana ret = prev_trans->aborted; 1933ccd467d6SChris Mason 1934724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 19351f9b8c8fSFilipe Manana if (ret) 19361f9b8c8fSFilipe Manana goto cleanup_transaction; 1937a4abeea4SJosef Bacik } else { 1938a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1939ccd467d6SChris Mason } 1940a4abeea4SJosef Bacik } else { 1941a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1942ccd467d6SChris Mason } 194315ee9bc7SJosef Bacik 19440860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 19450860adfdSMiao Xie 194682436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 194782436617SMiao Xie if (ret) 194882436617SMiao Xie goto cleanup_transaction; 194982436617SMiao Xie 19508d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 195149b25e05SJeff Mahoney if (ret) 195249b25e05SJeff Mahoney goto cleanup_transaction; 195316cdcec7SMiao Xie 1954581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1955581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1956ed3b3d31SChris Mason 1957581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 19588d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1959ca469637SMiao Xie if (ret) 1960ca469637SMiao Xie goto cleanup_transaction; 1961ca469637SMiao Xie 196282436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1963cb7ab021SWang Shilong 1964161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 196550d9aa99SJosef Bacik 1966cb7ab021SWang Shilong btrfs_scrub_pause(root); 1967ed0ca140SJosef Bacik /* 1968ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1969ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 19704a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1971ed0ca140SJosef Bacik */ 1972a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19734a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1974a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1975ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1976ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 197715ee9bc7SJosef Bacik 19782cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 19792cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19802cba30f1SMiao Xie ret = cur_trans->aborted; 19816cf7f77eSWang Shilong goto scrub_continue; 19822cba30f1SMiao Xie } 19837585717fSChris Mason /* 19847585717fSChris Mason * the reloc mutex makes sure that we stop 19857585717fSChris Mason * the balancing code from coming in and moving 19867585717fSChris Mason * extents around in the middle of the commit 19877585717fSChris Mason */ 19887585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 19897585717fSChris Mason 199042874b3dSMiao Xie /* 199142874b3dSMiao Xie * We needn't worry about the delayed items because we will 199242874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 199342874b3dSMiao Xie * core function of the snapshot creation. 199442874b3dSMiao Xie */ 199542874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 199649b25e05SJeff Mahoney if (ret) { 199749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19986cf7f77eSWang Shilong goto scrub_continue; 199949b25e05SJeff Mahoney } 20003063d29fSChris Mason 200142874b3dSMiao Xie /* 200242874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 200342874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 200442874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 200542874b3dSMiao Xie * them. 200642874b3dSMiao Xie * 200742874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 200842874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 200942874b3dSMiao Xie * the nodes and leaves. 201042874b3dSMiao Xie */ 201142874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 201249b25e05SJeff Mahoney if (ret) { 201349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20146cf7f77eSWang Shilong goto scrub_continue; 201549b25e05SJeff Mahoney } 201616cdcec7SMiao Xie 201756bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 201849b25e05SJeff Mahoney if (ret) { 201949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20206cf7f77eSWang Shilong goto scrub_continue; 202149b25e05SJeff Mahoney } 202256bec294SChris Mason 20230ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 20240ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 20250ed4792aSQu Wenruo if (ret) { 20260ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20270ed4792aSQu Wenruo goto scrub_continue; 20280ed4792aSQu Wenruo } 20290ed4792aSQu Wenruo 2030e999376fSChris Mason /* 2031e999376fSChris Mason * make sure none of the code above managed to slip in a 2032e999376fSChris Mason * delayed item 2033e999376fSChris Mason */ 2034e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 2035e999376fSChris Mason 20362c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2037dc17ff8fSChris Mason 2038e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2039e02119d5SChris Mason * various roots consistent with each other. Every pointer 2040e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2041e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2042e02119d5SChris Mason * the tree logging code from jumping in and changing any 2043e02119d5SChris Mason * of the trees. 2044e02119d5SChris Mason * 2045e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2046e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2047e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2048e02119d5SChris Mason * from now until after the super is written, we avoid races 2049e02119d5SChris Mason * with the tree-log code. 2050e02119d5SChris Mason */ 2051e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 20521a40e23bSZheng Yan 20535d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 205449b25e05SJeff Mahoney if (ret) { 205549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2056871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20576cf7f77eSWang Shilong goto scrub_continue; 205849b25e05SJeff Mahoney } 205954aa1f4dSChris Mason 20603818aea2SQu Wenruo /* 20617e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 20627e1876acSDavid Sterba * before the next transaction. 20633818aea2SQu Wenruo */ 2064572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 20653818aea2SQu Wenruo 20665d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2067e02119d5SChris Mason * safe to free the root of tree log roots 2068e02119d5SChris Mason */ 2069e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2070e02119d5SChris Mason 20710ed4792aSQu Wenruo /* 20720ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 20730ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 20740ed4792aSQu Wenruo */ 20750ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 20760ed4792aSQu Wenruo if (ret < 0) { 20770ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 20780ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20790ed4792aSQu Wenruo goto scrub_continue; 20800ed4792aSQu Wenruo } 20810ed4792aSQu Wenruo 20825d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 208349b25e05SJeff Mahoney if (ret) { 208449b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2085871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20866cf7f77eSWang Shilong goto scrub_continue; 208749b25e05SJeff Mahoney } 208854aa1f4dSChris Mason 20892cba30f1SMiao Xie /* 20902cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 20912cba30f1SMiao Xie * update ->aborted, check it. 20922cba30f1SMiao Xie */ 20932cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20942cba30f1SMiao Xie ret = cur_trans->aborted; 20952cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 20962cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 20976cf7f77eSWang Shilong goto scrub_continue; 20982cba30f1SMiao Xie } 20992cba30f1SMiao Xie 210011833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 210111833d66SYan Zheng 210278fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 21035f39d397SChris Mason 21045d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 21055d4f98a2SYan Zheng root->fs_info->tree_root->node); 21069e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 21079e351cc8SJosef Bacik &cur_trans->switch_commits); 21085d4f98a2SYan Zheng 21095d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 21105d4f98a2SYan Zheng root->fs_info->chunk_root->node); 21119e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 21129e351cc8SJosef Bacik &cur_trans->switch_commits); 21139e351cc8SJosef Bacik 21149e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 21155d4f98a2SYan Zheng 2116edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2117ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 21181bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 21195d4f98a2SYan Zheng update_super_roots(root); 2120e02119d5SChris Mason 21216c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 21226c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 21236c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 21246c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2125ccd467d6SChris Mason 2126935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2127ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2128935e5cc9SMiao Xie 2129656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2130656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2131656f30dbSFilipe Manana 21324fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 21334fbcdf66SFilipe Manana 2134a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 21354a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2136a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2137a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 21387585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2139b7ec40d7SChris Mason 2140f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2141e6dcd2dcSChris Mason 214279154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 214349b25e05SJeff Mahoney if (ret) { 2144a4553fefSAnand Jain btrfs_std_error(root->fs_info, ret, 214508748810SDavid Sterba "Error while writing out transaction"); 214649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21476cf7f77eSWang Shilong goto scrub_continue; 214849b25e05SJeff Mahoney } 214949b25e05SJeff Mahoney 215049b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 215149b25e05SJeff Mahoney if (ret) { 215249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21536cf7f77eSWang Shilong goto scrub_continue; 215449b25e05SJeff Mahoney } 21554313b399SChris Mason 2156e02119d5SChris Mason /* 2157e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2158e02119d5SChris Mason * to go about their business 2159e02119d5SChris Mason */ 2160e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2161e02119d5SChris Mason 216211833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 21634313b399SChris Mason 21643204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 216513212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 216613212b54SZhao Lei 216715ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 21684a9d8bdeSMiao Xie /* 21694a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 21704a9d8bdeSMiao Xie * which can change it. 21714a9d8bdeSMiao Xie */ 21724a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 21732c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 21743de4586cSChris Mason 2175a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 217613c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2177a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2178a4abeea4SJosef Bacik 2179724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2180724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 218158176a96SJosef Bacik 21820860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2183b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2184b2b5ef5cSJan Kara 21851abe9b8aSliubo trace_btrfs_transaction_commit(root); 21861abe9b8aSliubo 2187a2de733cSArne Jansen btrfs_scrub_continue(root); 2188a2de733cSArne Jansen 21899ed74f2dSJosef Bacik if (current->journal_info == trans) 21909ed74f2dSJosef Bacik current->journal_info = NULL; 21919ed74f2dSJosef Bacik 21922c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 219324bbcf04SYan, Zheng 21948a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 21958a733013SZhao Lei current != root->fs_info->cleaner_kthread) 219624bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 219724bbcf04SYan, Zheng 219879154b1bSChris Mason return ret; 219949b25e05SJeff Mahoney 22006cf7f77eSWang Shilong scrub_continue: 22016cf7f77eSWang Shilong btrfs_scrub_continue(root); 220249b25e05SJeff Mahoney cleanup_transaction: 22030e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 22044fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22050e721106SJosef Bacik trans->block_rsv = NULL; 2206c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 220749b25e05SJeff Mahoney if (current->journal_info == trans) 220849b25e05SJeff Mahoney current->journal_info = NULL; 22097b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 221049b25e05SJeff Mahoney 221149b25e05SJeff Mahoney return ret; 221279154b1bSChris Mason } 221379154b1bSChris Mason 2214d352ac68SChris Mason /* 22159d1a2a3aSDavid Sterba * return < 0 if error 22169d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 22179d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 22189d1a2a3aSDavid Sterba * 22199d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 22209d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 22219d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 22229d1a2a3aSDavid Sterba * few seconds later. 2223d352ac68SChris Mason */ 22249d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2225e9d0b13bSChris Mason { 22269d1a2a3aSDavid Sterba int ret; 22275d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2228e9d0b13bSChris Mason 2229a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 22309d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 22319d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 22329d1a2a3aSDavid Sterba return 0; 22339d1a2a3aSDavid Sterba } 22349d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 22359d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2236cfad392bSJosef Bacik list_del_init(&root->root_list); 2237a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 22385d4f98a2SYan Zheng 2239efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 224076dda93cSYan, Zheng 224116cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 224216cdcec7SMiao Xie 224376dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 224476dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 22452c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 224676dda93cSYan, Zheng else 22472c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 224832471dc2SDavid Sterba 22496596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2250e9d0b13bSChris Mason } 2251572d9ab7SDavid Sterba 2252572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2253572d9ab7SDavid Sterba { 2254572d9ab7SDavid Sterba unsigned long prev; 2255572d9ab7SDavid Sterba unsigned long bit; 2256572d9ab7SDavid Sterba 22576c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2258572d9ab7SDavid Sterba if (!prev) 2259572d9ab7SDavid Sterba return; 2260572d9ab7SDavid Sterba 22617e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 22627e1876acSDavid Sterba if (prev & bit) 22637e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22647e1876acSDavid Sterba prev &= ~bit; 22657e1876acSDavid Sterba 22667e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 22677e1876acSDavid Sterba if (prev & bit) 22687e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22697e1876acSDavid Sterba prev &= ~bit; 22707e1876acSDavid Sterba 2271d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2272d51033d0SDavid Sterba if (prev & bit) 2273d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2274d51033d0SDavid Sterba prev &= ~bit; 2275d51033d0SDavid Sterba 2276572d9ab7SDavid Sterba if (prev) 2277572d9ab7SDavid Sterba btrfs_warn(fs_info, 2278572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2279572d9ab7SDavid Sterba } 2280