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 { 6548407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 655f9295749SChris Mason } 656f9295749SChris Mason 6577a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6580af3d00bSJosef Bacik { 6598407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 6600af3d00bSJosef Bacik } 6610af3d00bSJosef Bacik 6627a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6639ca9ee09SSage Weil { 6648407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 6659ca9ee09SSage Weil } 6669ca9ee09SSage Weil 667d4edf39bSMiao Xie /* 668d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 669d4edf39bSMiao Xie * 670d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 671d4edf39bSMiao Xie * don't want to start a new one. 672d4edf39bSMiao Xie * 673d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 674d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 675d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 676d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 677d4edf39bSMiao Xie * invoke 678d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 679d4edf39bSMiao Xie */ 680354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 68160376ce4SJosef Bacik { 682354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 68360376ce4SJosef Bacik } 68460376ce4SJosef Bacik 685d4edf39bSMiao Xie /* 68690b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 687d4edf39bSMiao Xie * 688d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 689d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 690d4edf39bSMiao Xie * complete. 691d4edf39bSMiao Xie */ 692d4edf39bSMiao Xie struct btrfs_trans_handle * 693d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 694d4edf39bSMiao Xie { 695d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 696d4edf39bSMiao Xie 697d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 698d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 699d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 700d4edf39bSMiao Xie 701d4edf39bSMiao Xie return trans; 702d4edf39bSMiao Xie } 703d4edf39bSMiao Xie 704d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 705b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 70689ce8a63SChris Mason struct btrfs_transaction *commit) 70789ce8a63SChris Mason { 7084a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 70989ce8a63SChris Mason } 71089ce8a63SChris Mason 71146204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 71246204592SSage Weil { 71346204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7148cd2807fSMiao Xie int ret = 0; 71546204592SSage Weil 71646204592SSage Weil if (transid) { 71746204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 718a4abeea4SJosef Bacik goto out; 71946204592SSage Weil 72046204592SSage Weil /* find specified transaction */ 721a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 72246204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 72346204592SSage Weil if (t->transid == transid) { 72446204592SSage Weil cur_trans = t; 725a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 7268cd2807fSMiao Xie ret = 0; 72746204592SSage Weil break; 72846204592SSage Weil } 7298cd2807fSMiao Xie if (t->transid > transid) { 7308cd2807fSMiao Xie ret = 0; 73146204592SSage Weil break; 73246204592SSage Weil } 7338cd2807fSMiao Xie } 734a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 73542383020SSage Weil 73642383020SSage Weil /* 73742383020SSage Weil * The specified transaction doesn't exist, or we 73842383020SSage Weil * raced with btrfs_commit_transaction 73942383020SSage Weil */ 74042383020SSage Weil if (!cur_trans) { 74142383020SSage Weil if (transid > root->fs_info->last_trans_committed) 74242383020SSage Weil ret = -EINVAL; 7438cd2807fSMiao Xie goto out; 74442383020SSage Weil } 74546204592SSage Weil } else { 74646204592SSage Weil /* find newest transaction that is committing | committed */ 747a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 74846204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 74946204592SSage Weil list) { 7504a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7514a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7523473f3c0SJosef Bacik break; 75346204592SSage Weil cur_trans = t; 754a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 75546204592SSage Weil break; 75646204592SSage Weil } 75746204592SSage Weil } 758a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 75946204592SSage Weil if (!cur_trans) 760a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 76146204592SSage Weil } 76246204592SSage Weil 76346204592SSage Weil wait_for_commit(root, cur_trans); 764724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 765a4abeea4SJosef Bacik out: 76646204592SSage Weil return ret; 76746204592SSage Weil } 76846204592SSage Weil 76937d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 77037d1aeeeSChris Mason { 771a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 77237d1aeeeSChris Mason wait_current_trans(root); 77337d1aeeeSChris Mason } 77437d1aeeeSChris Mason 7758929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7768929ecfaSYan, Zheng struct btrfs_root *root) 7778929ecfaSYan, Zheng { 7781be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7790a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7801be41b78SJosef Bacik return 1; 78136ba022aSJosef Bacik 7821be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7838929ecfaSYan, Zheng } 7848929ecfaSYan, Zheng 7858929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7868929ecfaSYan, Zheng struct btrfs_root *root) 7878929ecfaSYan, Zheng { 7888929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7898929ecfaSYan, Zheng int updates; 79049b25e05SJeff Mahoney int err; 7918929ecfaSYan, Zheng 792a4abeea4SJosef Bacik smp_mb(); 7934a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7944a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7958929ecfaSYan, Zheng return 1; 7968929ecfaSYan, Zheng 7978929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 7988929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 79949b25e05SJeff Mahoney if (updates) { 80028ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 80149b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 80249b25e05SJeff Mahoney return err; 80349b25e05SJeff Mahoney } 8048929ecfaSYan, Zheng 8058929ecfaSYan, Zheng return should_end_transaction(trans, root); 8068929ecfaSYan, Zheng } 8078929ecfaSYan, Zheng 80889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 809a698d075SMiao Xie struct btrfs_root *root, int throttle) 81079154b1bSChris Mason { 8118929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 812ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 8131be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 814a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8154edc2ca3SDave Jones int err = 0; 816a79b7d4bSChris Mason int must_run_delayed_refs = 0; 817d6e4a428SChris Mason 8183bbb24b2SJosef Bacik if (trans->use_count > 1) { 8193bbb24b2SJosef Bacik trans->use_count--; 8202a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8212a1eb461SJosef Bacik return 0; 8222a1eb461SJosef Bacik } 8232a1eb461SJosef Bacik 824b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 8254c13d758SJosef Bacik trans->block_rsv = NULL; 826c5567237SArne Jansen 827ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 828ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 829ea658badSJosef Bacik 830c3e69d58SChris Mason trans->delayed_ref_updates = 0; 831a79b7d4bSChris Mason if (!trans->sync) { 832a79b7d4bSChris Mason must_run_delayed_refs = 833a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 8340a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 835a79b7d4bSChris Mason 836a79b7d4bSChris Mason /* 837a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 838a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 839a79b7d4bSChris Mason */ 840a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 841a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 842a79b7d4bSChris Mason must_run_delayed_refs = 2; 84356bec294SChris Mason } 844bb721703SChris Mason 8450e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8460e721106SJosef Bacik trans->block_rsv = NULL; 84756bec294SChris Mason 848ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 849ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 850ea658badSJosef Bacik 8514fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8524fbcdf66SFilipe Manana 853a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8544a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8554a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8564a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8574a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8584a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8594a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 860a4abeea4SJosef Bacik } 8618929ecfaSYan, Zheng 8624a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8633bbb24b2SJosef Bacik if (throttle) 8648929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8653bbb24b2SJosef Bacik else 8668929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8678929ecfaSYan, Zheng } 8688929ecfaSYan, Zheng 8690860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8706df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8716df7881aSJosef Bacik 8728929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 87313c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 87413c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8750860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 87689ce8a63SChris Mason 877a83342aaSDavid Sterba /* 878a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 879a83342aaSDavid Sterba */ 88099d16cbcSSage Weil smp_mb(); 88179154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 88279154b1bSChris Mason wake_up(&cur_trans->writer_wait); 883724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8849ed74f2dSJosef Bacik 8859ed74f2dSJosef Bacik if (current->journal_info == trans) 8869ed74f2dSJosef Bacik current->journal_info = NULL; 887ab78c84dSChris Mason 88824bbcf04SYan, Zheng if (throttle) 88924bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 89024bbcf04SYan, Zheng 89149b25e05SJeff Mahoney if (trans->aborted || 8924e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 8934e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8944edc2ca3SDave Jones err = -EIO; 8954e121c06SJosef Bacik } 896edf39272SJan Schmidt assert_qgroups_uptodate(trans); 89749b25e05SJeff Mahoney 8984edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 899a79b7d4bSChris Mason if (must_run_delayed_refs) { 900a79b7d4bSChris Mason btrfs_async_run_delayed_refs(root, cur, 901a79b7d4bSChris Mason must_run_delayed_refs == 1); 902a79b7d4bSChris Mason } 9034edc2ca3SDave Jones return err; 90479154b1bSChris Mason } 90579154b1bSChris Mason 90689ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 90789ce8a63SChris Mason struct btrfs_root *root) 90889ce8a63SChris Mason { 90998ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 91089ce8a63SChris Mason } 91189ce8a63SChris Mason 91289ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 91389ce8a63SChris Mason struct btrfs_root *root) 91489ce8a63SChris Mason { 91598ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 91616cdcec7SMiao Xie } 91716cdcec7SMiao Xie 918d352ac68SChris Mason /* 919d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 920d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 921690587d1SChris Mason * those extents are sent to disk but does not wait on them 922d352ac68SChris Mason */ 923690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 9248cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 92579154b1bSChris Mason { 926777e6bd7SChris Mason int err = 0; 9277c4452b9SChris Mason int werr = 0; 9281728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 929e6138876SJosef Bacik struct extent_state *cached_state = NULL; 930777e6bd7SChris Mason u64 start = 0; 9315f39d397SChris Mason u64 end; 9327c4452b9SChris Mason 9331728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 934e6138876SJosef Bacik mark, &cached_state)) { 935663dfbb0SFilipe Manana bool wait_writeback = false; 936663dfbb0SFilipe Manana 937663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 938663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 939e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 940663dfbb0SFilipe Manana /* 941663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 942663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 943663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 944663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 945663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 946663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 947663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 948663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 949663dfbb0SFilipe Manana * (and without errors). 950663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 951663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 952663dfbb0SFilipe Manana */ 953663dfbb0SFilipe Manana if (err == -ENOMEM) { 954663dfbb0SFilipe Manana err = 0; 955663dfbb0SFilipe Manana wait_writeback = true; 956663dfbb0SFilipe Manana } 957663dfbb0SFilipe Manana if (!err) 9581728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9597c4452b9SChris Mason if (err) 9607c4452b9SChris Mason werr = err; 961663dfbb0SFilipe Manana else if (wait_writeback) 962663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 963e38e2ed7SFilipe Manana free_extent_state(cached_state); 964663dfbb0SFilipe Manana cached_state = NULL; 9651728366eSJosef Bacik cond_resched(); 9661728366eSJosef Bacik start = end + 1; 9677c4452b9SChris Mason } 968690587d1SChris Mason return werr; 969690587d1SChris Mason } 970690587d1SChris Mason 971690587d1SChris Mason /* 972690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 973690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 974690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 975690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 976690587d1SChris Mason */ 977690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9788cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 979690587d1SChris Mason { 980690587d1SChris Mason int err = 0; 981690587d1SChris Mason int werr = 0; 9821728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 983e6138876SJosef Bacik struct extent_state *cached_state = NULL; 984690587d1SChris Mason u64 start = 0; 985690587d1SChris Mason u64 end; 986656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 987656f30dbSFilipe Manana bool errors = false; 988690587d1SChris Mason 9891728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 990e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 991663dfbb0SFilipe Manana /* 992663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 993663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 994663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 995663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 996663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 997663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 998663dfbb0SFilipe Manana */ 999663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1000663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1001e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1002663dfbb0SFilipe Manana if (err == -ENOMEM) 1003663dfbb0SFilipe Manana err = 0; 1004663dfbb0SFilipe Manana if (!err) 10051728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1006777e6bd7SChris Mason if (err) 1007777e6bd7SChris Mason werr = err; 1008e38e2ed7SFilipe Manana free_extent_state(cached_state); 1009e38e2ed7SFilipe Manana cached_state = NULL; 1010777e6bd7SChris Mason cond_resched(); 10111728366eSJosef Bacik start = end + 1; 1012777e6bd7SChris Mason } 10137c4452b9SChris Mason if (err) 10147c4452b9SChris Mason werr = err; 1015656f30dbSFilipe Manana 1016656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1017656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 1018656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 1019656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1020656f30dbSFilipe Manana errors = true; 1021656f30dbSFilipe Manana 1022656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 1023656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 1024656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1025656f30dbSFilipe Manana errors = true; 1026656f30dbSFilipe Manana } else { 1027656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 1028656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1029656f30dbSFilipe Manana errors = true; 1030656f30dbSFilipe Manana } 1031656f30dbSFilipe Manana 1032656f30dbSFilipe Manana if (errors && !werr) 1033656f30dbSFilipe Manana werr = -EIO; 1034656f30dbSFilipe Manana 10357c4452b9SChris Mason return werr; 103679154b1bSChris Mason } 103779154b1bSChris Mason 1038690587d1SChris Mason /* 1039690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1040690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1041690587d1SChris Mason * those extents are on disk for transaction or log commit 1042690587d1SChris Mason */ 1043171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10448cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1045690587d1SChris Mason { 1046690587d1SChris Mason int ret; 1047690587d1SChris Mason int ret2; 1048c6adc9ccSMiao Xie struct blk_plug plug; 1049690587d1SChris Mason 1050c6adc9ccSMiao Xie blk_start_plug(&plug); 10518cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1052c6adc9ccSMiao Xie blk_finish_plug(&plug); 10538cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1054bf0da8c1SChris Mason 1055bf0da8c1SChris Mason if (ret) 1056bf0da8c1SChris Mason return ret; 1057bf0da8c1SChris Mason if (ret2) 1058bf0da8c1SChris Mason return ret2; 1059bf0da8c1SChris Mason return 0; 1060690587d1SChris Mason } 1061690587d1SChris Mason 1062663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1063d0c803c4SChris Mason struct btrfs_root *root) 1064d0c803c4SChris Mason { 1065663dfbb0SFilipe Manana int ret; 1066663dfbb0SFilipe Manana 1067663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10688cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10698cef4e16SYan, Zheng EXTENT_DIRTY); 1070663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1071663dfbb0SFilipe Manana 1072663dfbb0SFilipe Manana return ret; 1073d0c803c4SChris Mason } 1074d0c803c4SChris Mason 1075d352ac68SChris Mason /* 1076d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1077d352ac68SChris Mason * 1078d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1079d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1080d352ac68SChris Mason * allocation tree. 1081d352ac68SChris Mason * 1082d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1083d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1084d352ac68SChris Mason */ 10850b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 108679154b1bSChris Mason struct btrfs_root *root) 108779154b1bSChris Mason { 108879154b1bSChris Mason int ret; 10890b86a832SChris Mason u64 old_root_bytenr; 109086b9f2ecSYan, Zheng u64 old_root_used; 10910b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 109279154b1bSChris Mason 109386b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 109456bec294SChris Mason 109579154b1bSChris Mason while (1) { 10960b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 109786b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1098ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 109979154b1bSChris Mason break; 110087ef2bb4SChris Mason 11015d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 110279154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11030b86a832SChris Mason &root->root_key, 11040b86a832SChris Mason &root->root_item); 110549b25e05SJeff Mahoney if (ret) 110649b25e05SJeff Mahoney return ret; 110756bec294SChris Mason 110886b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1109e7070be1SJosef Bacik } 1110276e680dSYan Zheng 11110b86a832SChris Mason return 0; 11120b86a832SChris Mason } 11130b86a832SChris Mason 1114d352ac68SChris Mason /* 1115d352ac68SChris Mason * update all the cowonly tree roots on disk 111649b25e05SJeff Mahoney * 111749b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 111849b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 111949b25e05SJeff Mahoney * to clean up the delayed refs. 1120d352ac68SChris Mason */ 11215d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11220b86a832SChris Mason struct btrfs_root *root) 11230b86a832SChris Mason { 11240b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1125ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11261bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11270b86a832SChris Mason struct list_head *next; 112884234f3aSYan Zheng struct extent_buffer *eb; 112956bec294SChris Mason int ret; 113084234f3aSYan Zheng 113184234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 113249b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 113349b25e05SJeff Mahoney 0, &eb); 113484234f3aSYan Zheng btrfs_tree_unlock(eb); 113584234f3aSYan Zheng free_extent_buffer(eb); 11360b86a832SChris Mason 113749b25e05SJeff Mahoney if (ret) 113849b25e05SJeff Mahoney return ret; 113949b25e05SJeff Mahoney 114056bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 114149b25e05SJeff Mahoney if (ret) 114249b25e05SJeff Mahoney return ret; 114387ef2bb4SChris Mason 1144733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1145c16ce190SJosef Bacik if (ret) 1146c16ce190SJosef Bacik return ret; 11478dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1148c16ce190SJosef Bacik if (ret) 1149c16ce190SJosef Bacik return ret; 1150546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1151c16ce190SJosef Bacik if (ret) 1152c16ce190SJosef Bacik return ret; 1153546adb0dSJan Schmidt 1154dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1155dcdf7f6dSJosef Bacik if (ret) 1156dcdf7f6dSJosef Bacik return ret; 1157dcdf7f6dSJosef Bacik 1158546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1159546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1160c16ce190SJosef Bacik if (ret) 1161c16ce190SJosef Bacik return ret; 1162ea526d18SJosef Bacik again: 11630b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11640b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11650b86a832SChris Mason list_del_init(next); 11660b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1167e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 116887ef2bb4SChris Mason 11699e351cc8SJosef Bacik if (root != fs_info->extent_root) 11709e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11719e351cc8SJosef Bacik &trans->transaction->switch_commits); 117249b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 117349b25e05SJeff Mahoney if (ret) 117449b25e05SJeff Mahoney return ret; 1175ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1176ea526d18SJosef Bacik if (ret) 1177ea526d18SJosef Bacik return ret; 117879154b1bSChris Mason } 1179276e680dSYan Zheng 11801bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1181ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1182ea526d18SJosef Bacik if (ret) 1183ea526d18SJosef Bacik return ret; 1184ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1185ea526d18SJosef Bacik if (ret) 1186ea526d18SJosef Bacik return ret; 1187ea526d18SJosef Bacik } 1188ea526d18SJosef Bacik 1189ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1190ea526d18SJosef Bacik goto again; 1191ea526d18SJosef Bacik 11929e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11939e351cc8SJosef Bacik &trans->transaction->switch_commits); 11948dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 11958dabb742SStefan Behrens 119679154b1bSChris Mason return 0; 119779154b1bSChris Mason } 119879154b1bSChris Mason 1199d352ac68SChris Mason /* 1200d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1201d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1202d352ac68SChris Mason * be deleted 1203d352ac68SChris Mason */ 1204cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12055eda7b5eSChris Mason { 1206a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1207cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12089d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1209a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 12105eda7b5eSChris Mason } 12115eda7b5eSChris Mason 1212d352ac68SChris Mason /* 12135d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1214d352ac68SChris Mason */ 12155d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12165d4f98a2SYan Zheng struct btrfs_root *root) 12170f7d52f4SChris Mason { 12180f7d52f4SChris Mason struct btrfs_root *gang[8]; 12195d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 12200f7d52f4SChris Mason int i; 12210f7d52f4SChris Mason int ret; 122254aa1f4dSChris Mason int err = 0; 122354aa1f4dSChris Mason 1224a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12250f7d52f4SChris Mason while (1) { 12265d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12275d4f98a2SYan Zheng (void **)gang, 0, 12280f7d52f4SChris Mason ARRAY_SIZE(gang), 12290f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12300f7d52f4SChris Mason if (ret == 0) 12310f7d52f4SChris Mason break; 12320f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12330f7d52f4SChris Mason root = gang[i]; 12345d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12352619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12360f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1237a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 123831153d81SYan Zheng 1239e02119d5SChris Mason btrfs_free_log(trans, root); 12405d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1241d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1242e02119d5SChris Mason 124382d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 124482d5902dSLi Zefan 1245f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 124627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1247c7548af6SChris Mason smp_mb__after_atomic(); 1248f1ebcc74SLiu Bo 1249978d910dSYan Zheng if (root->commit_root != root->node) { 12509e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12519e351cc8SJosef Bacik &trans->transaction->switch_commits); 1252978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1253978d910dSYan Zheng root->node); 1254978d910dSYan Zheng } 125531153d81SYan Zheng 12565d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12570f7d52f4SChris Mason &root->root_key, 12580f7d52f4SChris Mason &root->root_item); 1259a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 126054aa1f4dSChris Mason if (err) 126154aa1f4dSChris Mason break; 12627174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12630f7d52f4SChris Mason } 12649f3a7427SChris Mason } 1265a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 126654aa1f4dSChris Mason return err; 12670f7d52f4SChris Mason } 12680f7d52f4SChris Mason 1269d352ac68SChris Mason /* 1270de78b51aSEric Sandeen * defrag a given btree. 1271de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1272d352ac68SChris Mason */ 1273de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1274e9d0b13bSChris Mason { 1275e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1276e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12778929ecfaSYan, Zheng int ret; 1278e9d0b13bSChris Mason 127927cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1280e9d0b13bSChris Mason return 0; 12818929ecfaSYan, Zheng 12826b80053dSChris Mason while (1) { 12838929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12848929ecfaSYan, Zheng if (IS_ERR(trans)) 12858929ecfaSYan, Zheng return PTR_ERR(trans); 12868929ecfaSYan, Zheng 1287de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12888929ecfaSYan, Zheng 1289e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1290b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1291e9d0b13bSChris Mason cond_resched(); 1292e9d0b13bSChris Mason 12937841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1294e9d0b13bSChris Mason break; 1295210549ebSDavid Sterba 1296210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1297efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1298210549ebSDavid Sterba ret = -EAGAIN; 1299210549ebSDavid Sterba break; 1300210549ebSDavid Sterba } 1301e9d0b13bSChris Mason } 130227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13038929ecfaSYan, Zheng return ret; 1304e9d0b13bSChris Mason } 1305e9d0b13bSChris Mason 1306d352ac68SChris Mason /* 1307d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1308aec8030aSMiao Xie * transaction commit. This does the actual creation. 1309aec8030aSMiao Xie * 1310aec8030aSMiao Xie * Note: 1311aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1312aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1313aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1314d352ac68SChris Mason */ 131580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13163063d29fSChris Mason struct btrfs_fs_info *fs_info, 13173063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13183063d29fSChris Mason { 13193063d29fSChris Mason struct btrfs_key key; 132080b6794dSChris Mason struct btrfs_root_item *new_root_item; 13213063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13223063d29fSChris Mason struct btrfs_root *root = pending->root; 13236bdb72deSSage Weil struct btrfs_root *parent_root; 132498c9942aSLiu Bo struct btrfs_block_rsv *rsv; 13256bdb72deSSage Weil struct inode *parent_inode; 132642874b3dSMiao Xie struct btrfs_path *path; 132742874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1328a22285a6SYan, Zheng struct dentry *dentry; 13293063d29fSChris Mason struct extent_buffer *tmp; 1330925baeddSChris Mason struct extent_buffer *old; 13318ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1332aec8030aSMiao Xie int ret = 0; 1333d68fc57bSYan, Zheng u64 to_reserve = 0; 13346bdb72deSSage Weil u64 index = 0; 1335a22285a6SYan, Zheng u64 objectid; 1336b83cc969SLi Zefan u64 root_flags; 13378ea05e3aSAlexander Block uuid_le new_uuid; 13383063d29fSChris Mason 133942874b3dSMiao Xie path = btrfs_alloc_path(); 134042874b3dSMiao Xie if (!path) { 1341aec8030aSMiao Xie pending->error = -ENOMEM; 1342aec8030aSMiao Xie return 0; 134342874b3dSMiao Xie } 134442874b3dSMiao Xie 134580b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 134680b6794dSChris Mason if (!new_root_item) { 1347aec8030aSMiao Xie pending->error = -ENOMEM; 13486fa9700eSMiao Xie goto root_item_alloc_fail; 134980b6794dSChris Mason } 1350a22285a6SYan, Zheng 1351aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1352aec8030aSMiao Xie if (pending->error) 13536fa9700eSMiao Xie goto no_free_objectid; 13543063d29fSChris Mason 1355d6726335SQu Wenruo /* 1356d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1357d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1358d6726335SQu Wenruo */ 1359d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1360d6726335SQu Wenruo 1361147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1362d68fc57bSYan, Zheng 1363d68fc57bSYan, Zheng if (to_reserve > 0) { 1364aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1365aec8030aSMiao Xie &pending->block_rsv, 136608e007d2SMiao Xie to_reserve, 136708e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1368aec8030aSMiao Xie if (pending->error) 1369d6726335SQu Wenruo goto clear_skip_qgroup; 1370d68fc57bSYan, Zheng } 1371d68fc57bSYan, Zheng 13723063d29fSChris Mason key.objectid = objectid; 1373a22285a6SYan, Zheng key.offset = (u64)-1; 1374a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 13753063d29fSChris Mason 13766fa9700eSMiao Xie rsv = trans->block_rsv; 1377a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 13782382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 13796bdb72deSSage Weil 1380a22285a6SYan, Zheng dentry = pending->dentry; 1381e9662f70SMiao Xie parent_inode = pending->dir; 1382a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 13837585717fSChris Mason record_root_in_trans(trans, parent_root); 1384a22285a6SYan, Zheng 13856bdb72deSSage Weil /* 13866bdb72deSSage Weil * insert the directory item 13876bdb72deSSage Weil */ 13886bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 138949b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 139042874b3dSMiao Xie 139142874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 139242874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 139342874b3dSMiao Xie btrfs_ino(parent_inode), 139442874b3dSMiao Xie dentry->d_name.name, 139542874b3dSMiao Xie dentry->d_name.len, 0); 139642874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1397fe66a05aSChris Mason pending->error = -EEXIST; 1398aec8030aSMiao Xie goto dir_item_existed; 139942874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 140042874b3dSMiao Xie ret = PTR_ERR(dir_item); 14018732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14028732d44fSMiao Xie goto fail; 140379787eaaSJeff Mahoney } 140442874b3dSMiao Xie btrfs_release_path(path); 14056bdb72deSSage Weil 1406e999376fSChris Mason /* 1407e999376fSChris Mason * pull in the delayed directory update 1408e999376fSChris Mason * and the delayed inode item 1409e999376fSChris Mason * otherwise we corrupt the FS during 1410e999376fSChris Mason * snapshot 1411e999376fSChris Mason */ 1412e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 14138732d44fSMiao Xie if (ret) { /* Transaction aborted */ 14148732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14158732d44fSMiao Xie goto fail; 14168732d44fSMiao Xie } 1417e999376fSChris Mason 14187585717fSChris Mason record_root_in_trans(trans, root); 14196bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14206bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 142108fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14226bdb72deSSage Weil 1423b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1424b83cc969SLi Zefan if (pending->readonly) 1425b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1426b83cc969SLi Zefan else 1427b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1428b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1429b83cc969SLi Zefan 14308ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 14318ea05e3aSAlexander Block trans->transid); 14328ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 14338ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 14348ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 14358ea05e3aSAlexander Block BTRFS_UUID_SIZE); 143670023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 143770023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 143870023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14398ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14408ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 14418ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 14428ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 144370023da2SStefan Behrens } 14443cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 14453cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 144670023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 14478ea05e3aSAlexander Block 1448925baeddSChris Mason old = btrfs_lock_root_node(root); 144949b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 145079787eaaSJeff Mahoney if (ret) { 145179787eaaSJeff Mahoney btrfs_tree_unlock(old); 145279787eaaSJeff Mahoney free_extent_buffer(old); 14538732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14548732d44fSMiao Xie goto fail; 145579787eaaSJeff Mahoney } 145649b25e05SJeff Mahoney 14575d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 14583063d29fSChris Mason 145949b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 146079787eaaSJeff Mahoney /* clean up in any case */ 1461925baeddSChris Mason btrfs_tree_unlock(old); 1462925baeddSChris Mason free_extent_buffer(old); 14638732d44fSMiao Xie if (ret) { 14648732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14658732d44fSMiao Xie goto fail; 14668732d44fSMiao Xie } 1467f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 146827cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1469f1ebcc74SLiu Bo smp_wmb(); 1470f1ebcc74SLiu Bo 14715d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1472a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1473a22285a6SYan, Zheng key.offset = trans->transid; 1474a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1475925baeddSChris Mason btrfs_tree_unlock(tmp); 14763063d29fSChris Mason free_extent_buffer(tmp); 14778732d44fSMiao Xie if (ret) { 14788732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14798732d44fSMiao Xie goto fail; 14808732d44fSMiao Xie } 14810660b5afSChris Mason 1482a22285a6SYan, Zheng /* 1483a22285a6SYan, Zheng * insert root back/forward references 1484a22285a6SYan, Zheng */ 1485a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1486a22285a6SYan, Zheng parent_root->root_key.objectid, 148733345d01SLi Zefan btrfs_ino(parent_inode), index, 1488a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 14898732d44fSMiao Xie if (ret) { 14908732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14918732d44fSMiao Xie goto fail; 14928732d44fSMiao Xie } 1493a22285a6SYan, Zheng 1494a22285a6SYan, Zheng key.offset = (u64)-1; 1495a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 149679787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 149779787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 14988732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14998732d44fSMiao Xie goto fail; 150079787eaaSJeff Mahoney } 1501d68fc57bSYan, Zheng 150249b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15038732d44fSMiao Xie if (ret) { 15048732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15058732d44fSMiao Xie goto fail; 15068732d44fSMiao Xie } 1507361048f5SMiao Xie 1508361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 15098732d44fSMiao Xie if (ret) { 15108732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15118732d44fSMiao Xie goto fail; 15128732d44fSMiao Xie } 151342874b3dSMiao Xie 151442874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 151542874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 151642874b3dSMiao Xie parent_inode, &key, 151742874b3dSMiao Xie BTRFS_FT_DIR, index); 151842874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 15199c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 15208732d44fSMiao Xie if (ret) { 15218732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15228732d44fSMiao Xie goto fail; 15238732d44fSMiao Xie } 152442874b3dSMiao Xie 152542874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 152642874b3dSMiao Xie dentry->d_name.len * 2); 152742874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1528be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1529dd5f9615SStefan Behrens if (ret) { 15308732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1531dd5f9615SStefan Behrens goto fail; 1532dd5f9615SStefan Behrens } 1533dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1534dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1535dd5f9615SStefan Behrens if (ret) { 1536dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1537dd5f9615SStefan Behrens goto fail; 1538dd5f9615SStefan Behrens } 1539dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1540dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1541dd5f9615SStefan Behrens new_root_item->received_uuid, 1542dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1543dd5f9615SStefan Behrens objectid); 1544dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1545dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1546dd5f9615SStefan Behrens goto fail; 1547dd5f9615SStefan Behrens } 1548dd5f9615SStefan Behrens } 1549d6726335SQu Wenruo 1550d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1551d6726335SQu Wenruo if (ret) { 1552d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1553d6726335SQu Wenruo goto fail; 1554d6726335SQu Wenruo } 1555d6726335SQu Wenruo 1556d6726335SQu Wenruo /* 1557d6726335SQu Wenruo * account qgroup counters before qgroup_inherit() 1558d6726335SQu Wenruo */ 1559d6726335SQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 1560d6726335SQu Wenruo if (ret) 1561d6726335SQu Wenruo goto fail; 1562d6726335SQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 1563d6726335SQu Wenruo if (ret) 1564d6726335SQu Wenruo goto fail; 1565d6726335SQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 1566d6726335SQu Wenruo root->root_key.objectid, 1567d6726335SQu Wenruo objectid, pending->inherit); 1568d6726335SQu Wenruo if (ret) { 1569d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1570d6726335SQu Wenruo goto fail; 1571d6726335SQu Wenruo } 1572d6726335SQu Wenruo 15733063d29fSChris Mason fail: 1574aec8030aSMiao Xie pending->error = ret; 1575aec8030aSMiao Xie dir_item_existed: 157698c9942aSLiu Bo trans->block_rsv = rsv; 15772382c5ccSLiu Bo trans->bytes_reserved = 0; 1578d6726335SQu Wenruo clear_skip_qgroup: 1579d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 15806fa9700eSMiao Xie no_free_objectid: 15816fa9700eSMiao Xie kfree(new_root_item); 15826fa9700eSMiao Xie root_item_alloc_fail: 158342874b3dSMiao Xie btrfs_free_path(path); 158449b25e05SJeff Mahoney return ret; 15853063d29fSChris Mason } 15863063d29fSChris Mason 1587d352ac68SChris Mason /* 1588d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1589d352ac68SChris Mason */ 159080b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 15913063d29fSChris Mason struct btrfs_fs_info *fs_info) 15923063d29fSChris Mason { 1593aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 15943063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1595aec8030aSMiao Xie int ret = 0; 15963de4586cSChris Mason 1597aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1598aec8030aSMiao Xie list_del(&pending->list); 1599aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1600aec8030aSMiao Xie if (ret) 1601aec8030aSMiao Xie break; 1602aec8030aSMiao Xie } 1603aec8030aSMiao Xie return ret; 16043de4586cSChris Mason } 16053de4586cSChris Mason 16065d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 16075d4f98a2SYan Zheng { 16085d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16095d4f98a2SYan Zheng struct btrfs_super_block *super; 16105d4f98a2SYan Zheng 16116c41761fSDavid Sterba super = root->fs_info->super_copy; 16125d4f98a2SYan Zheng 16135d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 16145d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 16155d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 16165d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 16175d4f98a2SYan Zheng 16185d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 16195d4f98a2SYan Zheng super->root = root_item->bytenr; 16205d4f98a2SYan Zheng super->generation = root_item->generation; 16215d4f98a2SYan Zheng super->root_level = root_item->level; 162273bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 16230af3d00bSJosef Bacik super->cache_generation = root_item->generation; 162470f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 162526432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 16265d4f98a2SYan Zheng } 16275d4f98a2SYan Zheng 1628f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1629f36f3042SChris Mason { 16304a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1631f36f3042SChris Mason int ret = 0; 16324a9d8bdeSMiao Xie 1633a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16344a9d8bdeSMiao Xie trans = info->running_transaction; 16354a9d8bdeSMiao Xie if (trans) 16364a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1637a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1638f36f3042SChris Mason return ret; 1639f36f3042SChris Mason } 1640f36f3042SChris Mason 16418929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16428929ecfaSYan, Zheng { 16434a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16448929ecfaSYan, Zheng int ret = 0; 16454a9d8bdeSMiao Xie 1646a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16474a9d8bdeSMiao Xie trans = info->running_transaction; 16484a9d8bdeSMiao Xie if (trans) 16494a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1650a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 16518929ecfaSYan, Zheng return ret; 16528929ecfaSYan, Zheng } 16538929ecfaSYan, Zheng 1654bb9c12c9SSage Weil /* 1655bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1656bb9c12c9SSage Weil * transaction joins 1657bb9c12c9SSage Weil */ 1658bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1659bb9c12c9SSage Weil struct btrfs_transaction *trans) 1660bb9c12c9SSage Weil { 16614a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1662501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1663501407aaSJosef Bacik trans->aborted); 1664bb9c12c9SSage Weil } 1665bb9c12c9SSage Weil 1666bb9c12c9SSage Weil /* 1667bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1668bb9c12c9SSage Weil * caller holds ref. 1669bb9c12c9SSage Weil */ 1670bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1671bb9c12c9SSage Weil struct btrfs_transaction *trans) 1672bb9c12c9SSage Weil { 167372d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1674501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1675501407aaSJosef Bacik trans->aborted); 1676bb9c12c9SSage Weil } 1677bb9c12c9SSage Weil 1678bb9c12c9SSage Weil /* 1679bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1680bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1681bb9c12c9SSage Weil */ 1682bb9c12c9SSage Weil struct btrfs_async_commit { 1683bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1684bb9c12c9SSage Weil struct btrfs_root *root; 16857892b5afSMiao Xie struct work_struct work; 1686bb9c12c9SSage Weil }; 1687bb9c12c9SSage Weil 1688bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1689bb9c12c9SSage Weil { 1690bb9c12c9SSage Weil struct btrfs_async_commit *ac = 16917892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1692bb9c12c9SSage Weil 16936fc4e354SSage Weil /* 16946fc4e354SSage Weil * We've got freeze protection passed with the transaction. 16956fc4e354SSage Weil * Tell lockdep about it. 16966fc4e354SSage Weil */ 1697b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1698bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 16996fc4e354SSage Weil 1700e209db7aSSage Weil current->journal_info = ac->newtrans; 1701e209db7aSSage Weil 1702bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1703bb9c12c9SSage Weil kfree(ac); 1704bb9c12c9SSage Weil } 1705bb9c12c9SSage Weil 1706bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1707bb9c12c9SSage Weil struct btrfs_root *root, 1708bb9c12c9SSage Weil int wait_for_unblock) 1709bb9c12c9SSage Weil { 1710bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1711bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1712bb9c12c9SSage Weil 1713bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1714db5b493aSTsutomu Itoh if (!ac) 1715db5b493aSTsutomu Itoh return -ENOMEM; 1716bb9c12c9SSage Weil 17177892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1718bb9c12c9SSage Weil ac->root = root; 17197a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 17203612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17213612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17223612b495STsutomu Itoh kfree(ac); 17233612b495STsutomu Itoh return err; 17243612b495STsutomu Itoh } 1725bb9c12c9SSage Weil 1726bb9c12c9SSage Weil /* take transaction reference */ 1727bb9c12c9SSage Weil cur_trans = trans->transaction; 172813c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1729bb9c12c9SSage Weil 1730bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 17316fc4e354SSage Weil 17326fc4e354SSage Weil /* 17336fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17346fc4e354SSage Weil * async commit thread will be the one to unlock it. 17356fc4e354SSage Weil */ 1736b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1737bee9182dSOleg Nesterov __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 17386fc4e354SSage Weil 17397892b5afSMiao Xie schedule_work(&ac->work); 1740bb9c12c9SSage Weil 1741bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1742bb9c12c9SSage Weil if (wait_for_unblock) 1743bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1744bb9c12c9SSage Weil else 1745bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1746bb9c12c9SSage Weil 174738e88054SSage Weil if (current->journal_info == trans) 174838e88054SSage Weil current->journal_info = NULL; 174938e88054SSage Weil 1750724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1751bb9c12c9SSage Weil return 0; 1752bb9c12c9SSage Weil } 1753bb9c12c9SSage Weil 175449b25e05SJeff Mahoney 175549b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 17567b8b92afSJosef Bacik struct btrfs_root *root, int err) 175749b25e05SJeff Mahoney { 175849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1759f094ac32SLiu Bo DEFINE_WAIT(wait); 176049b25e05SJeff Mahoney 176149b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 176249b25e05SJeff Mahoney 17637b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 17647b8b92afSJosef Bacik 176549b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 176666b6135bSLiu Bo 176725d8c284SMiao Xie /* 176825d8c284SMiao Xie * If the transaction is removed from the list, it means this 176925d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 177025d8c284SMiao Xie * to call the cleanup function. 177125d8c284SMiao Xie */ 177225d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 177366b6135bSLiu Bo 177449b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1775d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 17764a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1777f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1778f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1779f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1780f094ac32SLiu Bo 1781f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1782d7096fc3SJosef Bacik } 178349b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 178449b25e05SJeff Mahoney 178549b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 178649b25e05SJeff Mahoney 17874a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 17884a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 17894a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 17904a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 17914a9d8bdeSMiao Xie 1792e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1793e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1794724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1795724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 179649b25e05SJeff Mahoney 179749b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 179849b25e05SJeff Mahoney 179949b25e05SJeff Mahoney if (current->journal_info == trans) 180049b25e05SJeff Mahoney current->journal_info = NULL; 1801c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 180249b25e05SJeff Mahoney 180349b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 180449b25e05SJeff Mahoney } 180549b25e05SJeff Mahoney 180682436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 180782436617SMiao Xie { 180882436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 18096c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 181082436617SMiao Xie return 0; 181182436617SMiao Xie } 181282436617SMiao Xie 181382436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 181482436617SMiao Xie { 181582436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1816b0244199SMiao Xie btrfs_wait_ordered_roots(fs_info, -1); 181782436617SMiao Xie } 181882436617SMiao Xie 181950d9aa99SJosef Bacik static inline void 1820161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 182150d9aa99SJosef Bacik { 1822161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1823161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 182450d9aa99SJosef Bacik } 182550d9aa99SJosef Bacik 182679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 182779154b1bSChris Mason struct btrfs_root *root) 182879154b1bSChris Mason { 182949b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 18308fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1831656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 183225287e0aSMiao Xie int ret; 183379154b1bSChris Mason 18348d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 18358d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 183625287e0aSMiao Xie ret = cur_trans->aborted; 1837e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1838e4a2bcacSJosef Bacik return ret; 183925287e0aSMiao Xie } 184049b25e05SJeff Mahoney 184156bec294SChris Mason /* make a pass through all the delayed refs we have so far 184256bec294SChris Mason * any runnings procs may add more while we are here 184356bec294SChris Mason */ 184456bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1845e4a2bcacSJosef Bacik if (ret) { 1846e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1847e4a2bcacSJosef Bacik return ret; 1848e4a2bcacSJosef Bacik } 184956bec294SChris Mason 18500e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18510e721106SJosef Bacik trans->block_rsv = NULL; 18520e721106SJosef Bacik 1853b7ec40d7SChris Mason cur_trans = trans->transaction; 185449b25e05SJeff Mahoney 185556bec294SChris Mason /* 185656bec294SChris Mason * set the flushing flag so procs in this transaction have to 185756bec294SChris Mason * start sending their work down. 185856bec294SChris Mason */ 1859b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 18601be41b78SJosef Bacik smp_wmb(); 186156bec294SChris Mason 1862ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1863ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1864ea658badSJosef Bacik 1865c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1866e4a2bcacSJosef Bacik if (ret) { 1867e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1868e4a2bcacSJosef Bacik return ret; 1869e4a2bcacSJosef Bacik } 187056bec294SChris Mason 18713204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 18721bbc621eSChris Mason int run_it = 0; 18731bbc621eSChris Mason 18741bbc621eSChris Mason /* this mutex is also taken before trying to set 18751bbc621eSChris Mason * block groups readonly. We need to make sure 18761bbc621eSChris Mason * that nobody has set a block group readonly 18771bbc621eSChris Mason * after a extents from that block group have been 18781bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 18791bbc621eSChris Mason * will wait for the transaction to commit if it 18803204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 18811bbc621eSChris Mason * 18823204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 18833204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 18841bbc621eSChris Mason * hurt to have more than one go through, but there's no 18851bbc621eSChris Mason * real advantage to it either. 18861bbc621eSChris Mason */ 18871bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 18883204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 18893204d33cSJosef Bacik &cur_trans->flags)) 18901bbc621eSChris Mason run_it = 1; 18911bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 18921bbc621eSChris Mason 18931bbc621eSChris Mason if (run_it) 18941bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 18951bbc621eSChris Mason } 18961bbc621eSChris Mason if (ret) { 18971bbc621eSChris Mason btrfs_end_transaction(trans, root); 18981bbc621eSChris Mason return ret; 18991bbc621eSChris Mason } 19001bbc621eSChris Mason 19014a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 19024a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 19034a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 190413c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 190549b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1906ccd467d6SChris Mason 1907b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 190815ee9bc7SJosef Bacik 1909b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 1910b4924a0fSLiu Bo ret = cur_trans->aborted; 1911b4924a0fSLiu Bo 1912724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 191315ee9bc7SJosef Bacik 191449b25e05SJeff Mahoney return ret; 191579154b1bSChris Mason } 19164313b399SChris Mason 19174a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1918bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1919bb9c12c9SSage Weil 1920ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1921ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1922ccd467d6SChris Mason struct btrfs_transaction, list); 19234a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 192413c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1925a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1926ccd467d6SChris Mason 1927ccd467d6SChris Mason wait_for_commit(root, prev_trans); 19281f9b8c8fSFilipe Manana ret = prev_trans->aborted; 1929ccd467d6SChris Mason 1930724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 19311f9b8c8fSFilipe Manana if (ret) 19321f9b8c8fSFilipe Manana goto cleanup_transaction; 1933a4abeea4SJosef Bacik } else { 1934a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1935ccd467d6SChris Mason } 1936a4abeea4SJosef Bacik } else { 1937a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1938ccd467d6SChris Mason } 193915ee9bc7SJosef Bacik 19400860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 19410860adfdSMiao Xie 194282436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 194382436617SMiao Xie if (ret) 194482436617SMiao Xie goto cleanup_transaction; 194582436617SMiao Xie 19468d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 194749b25e05SJeff Mahoney if (ret) 194849b25e05SJeff Mahoney goto cleanup_transaction; 194916cdcec7SMiao Xie 1950581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1951581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1952ed3b3d31SChris Mason 1953581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 19548d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 1955ca469637SMiao Xie if (ret) 1956ca469637SMiao Xie goto cleanup_transaction; 1957ca469637SMiao Xie 195882436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1959cb7ab021SWang Shilong 1960161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 196150d9aa99SJosef Bacik 1962cb7ab021SWang Shilong btrfs_scrub_pause(root); 1963ed0ca140SJosef Bacik /* 1964ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1965ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 19664a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1967ed0ca140SJosef Bacik */ 1968a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 19694a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1970a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1971ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1972ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 197315ee9bc7SJosef Bacik 19742cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 19752cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 19762cba30f1SMiao Xie ret = cur_trans->aborted; 19776cf7f77eSWang Shilong goto scrub_continue; 19782cba30f1SMiao Xie } 19797585717fSChris Mason /* 19807585717fSChris Mason * the reloc mutex makes sure that we stop 19817585717fSChris Mason * the balancing code from coming in and moving 19827585717fSChris Mason * extents around in the middle of the commit 19837585717fSChris Mason */ 19847585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 19857585717fSChris Mason 198642874b3dSMiao Xie /* 198742874b3dSMiao Xie * We needn't worry about the delayed items because we will 198842874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 198942874b3dSMiao Xie * core function of the snapshot creation. 199042874b3dSMiao Xie */ 199142874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 199249b25e05SJeff Mahoney if (ret) { 199349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 19946cf7f77eSWang Shilong goto scrub_continue; 199549b25e05SJeff Mahoney } 19963063d29fSChris Mason 199742874b3dSMiao Xie /* 199842874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 199942874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 200042874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 200142874b3dSMiao Xie * them. 200242874b3dSMiao Xie * 200342874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 200442874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 200542874b3dSMiao Xie * the nodes and leaves. 200642874b3dSMiao Xie */ 200742874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 200849b25e05SJeff Mahoney if (ret) { 200949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20106cf7f77eSWang Shilong goto scrub_continue; 201149b25e05SJeff Mahoney } 201216cdcec7SMiao Xie 201356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 201449b25e05SJeff Mahoney if (ret) { 201549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20166cf7f77eSWang Shilong goto scrub_continue; 201749b25e05SJeff Mahoney } 201856bec294SChris Mason 20190ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 20200ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 20210ed4792aSQu Wenruo if (ret) { 20220ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20230ed4792aSQu Wenruo goto scrub_continue; 20240ed4792aSQu Wenruo } 20250ed4792aSQu Wenruo 2026e999376fSChris Mason /* 2027e999376fSChris Mason * make sure none of the code above managed to slip in a 2028e999376fSChris Mason * delayed item 2029e999376fSChris Mason */ 2030e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 2031e999376fSChris Mason 20322c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2033dc17ff8fSChris Mason 2034e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2035e02119d5SChris Mason * various roots consistent with each other. Every pointer 2036e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2037e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2038e02119d5SChris Mason * the tree logging code from jumping in and changing any 2039e02119d5SChris Mason * of the trees. 2040e02119d5SChris Mason * 2041e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2042e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2043e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2044e02119d5SChris Mason * from now until after the super is written, we avoid races 2045e02119d5SChris Mason * with the tree-log code. 2046e02119d5SChris Mason */ 2047e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 20481a40e23bSZheng Yan 20495d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 205049b25e05SJeff Mahoney if (ret) { 205149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2052871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20536cf7f77eSWang Shilong goto scrub_continue; 205449b25e05SJeff Mahoney } 205554aa1f4dSChris Mason 20563818aea2SQu Wenruo /* 20577e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 20587e1876acSDavid Sterba * before the next transaction. 20593818aea2SQu Wenruo */ 2060572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 20613818aea2SQu Wenruo 20625d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2063e02119d5SChris Mason * safe to free the root of tree log roots 2064e02119d5SChris Mason */ 2065e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2066e02119d5SChris Mason 20670ed4792aSQu Wenruo /* 20680ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 20690ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 20700ed4792aSQu Wenruo */ 20710ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 20720ed4792aSQu Wenruo if (ret < 0) { 20730ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 20740ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 20750ed4792aSQu Wenruo goto scrub_continue; 20760ed4792aSQu Wenruo } 20770ed4792aSQu Wenruo 20785d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 207949b25e05SJeff Mahoney if (ret) { 208049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2081871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 20826cf7f77eSWang Shilong goto scrub_continue; 208349b25e05SJeff Mahoney } 208454aa1f4dSChris Mason 20852cba30f1SMiao Xie /* 20862cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 20872cba30f1SMiao Xie * update ->aborted, check it. 20882cba30f1SMiao Xie */ 20892cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20902cba30f1SMiao Xie ret = cur_trans->aborted; 20912cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 20922cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 20936cf7f77eSWang Shilong goto scrub_continue; 20942cba30f1SMiao Xie } 20952cba30f1SMiao Xie 209611833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 209711833d66SYan Zheng 209878fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 20995f39d397SChris Mason 21005d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 21015d4f98a2SYan Zheng root->fs_info->tree_root->node); 21029e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 21039e351cc8SJosef Bacik &cur_trans->switch_commits); 21045d4f98a2SYan Zheng 21055d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 21065d4f98a2SYan Zheng root->fs_info->chunk_root->node); 21079e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 21089e351cc8SJosef Bacik &cur_trans->switch_commits); 21099e351cc8SJosef Bacik 21109e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 21115d4f98a2SYan Zheng 2112edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2113ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 21141bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 21155d4f98a2SYan Zheng update_super_roots(root); 2116e02119d5SChris Mason 21176c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 21186c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 21196c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 21206c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2121ccd467d6SChris Mason 2122935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2123ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2124935e5cc9SMiao Xie 2125656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2126656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2127656f30dbSFilipe Manana 21284fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 21294fbcdf66SFilipe Manana 2130a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 21314a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2132a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2133a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 21347585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2135b7ec40d7SChris Mason 2136f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2137e6dcd2dcSChris Mason 213879154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 213949b25e05SJeff Mahoney if (ret) { 2140a4553fefSAnand Jain btrfs_std_error(root->fs_info, ret, 214108748810SDavid Sterba "Error while writing out transaction"); 214249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21436cf7f77eSWang Shilong goto scrub_continue; 214449b25e05SJeff Mahoney } 214549b25e05SJeff Mahoney 214649b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 214749b25e05SJeff Mahoney if (ret) { 214849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 21496cf7f77eSWang Shilong goto scrub_continue; 215049b25e05SJeff Mahoney } 21514313b399SChris Mason 2152e02119d5SChris Mason /* 2153e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2154e02119d5SChris Mason * to go about their business 2155e02119d5SChris Mason */ 2156e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2157e02119d5SChris Mason 215811833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 21594313b399SChris Mason 21603204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 216113212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 216213212b54SZhao Lei 216315ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 21644a9d8bdeSMiao Xie /* 21654a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 21664a9d8bdeSMiao Xie * which can change it. 21674a9d8bdeSMiao Xie */ 21684a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 21692c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 21703de4586cSChris Mason 2171a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 217213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2173a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2174a4abeea4SJosef Bacik 2175724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2176724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 217758176a96SJosef Bacik 21780860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2179b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2180b2b5ef5cSJan Kara 21811abe9b8aSliubo trace_btrfs_transaction_commit(root); 21821abe9b8aSliubo 2183a2de733cSArne Jansen btrfs_scrub_continue(root); 2184a2de733cSArne Jansen 21859ed74f2dSJosef Bacik if (current->journal_info == trans) 21869ed74f2dSJosef Bacik current->journal_info = NULL; 21879ed74f2dSJosef Bacik 21882c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 218924bbcf04SYan, Zheng 21908a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 21918a733013SZhao Lei current != root->fs_info->cleaner_kthread) 219224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 219324bbcf04SYan, Zheng 219479154b1bSChris Mason return ret; 219549b25e05SJeff Mahoney 21966cf7f77eSWang Shilong scrub_continue: 21976cf7f77eSWang Shilong btrfs_scrub_continue(root); 219849b25e05SJeff Mahoney cleanup_transaction: 21990e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 22004fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22010e721106SJosef Bacik trans->block_rsv = NULL; 2202c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 220349b25e05SJeff Mahoney if (current->journal_info == trans) 220449b25e05SJeff Mahoney current->journal_info = NULL; 22057b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 220649b25e05SJeff Mahoney 220749b25e05SJeff Mahoney return ret; 220879154b1bSChris Mason } 220979154b1bSChris Mason 2210d352ac68SChris Mason /* 22119d1a2a3aSDavid Sterba * return < 0 if error 22129d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 22139d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 22149d1a2a3aSDavid Sterba * 22159d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 22169d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 22179d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 22189d1a2a3aSDavid Sterba * few seconds later. 2219d352ac68SChris Mason */ 22209d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2221e9d0b13bSChris Mason { 22229d1a2a3aSDavid Sterba int ret; 22235d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2224e9d0b13bSChris Mason 2225a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 22269d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 22279d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 22289d1a2a3aSDavid Sterba return 0; 22299d1a2a3aSDavid Sterba } 22309d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 22319d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2232cfad392bSJosef Bacik list_del_init(&root->root_list); 2233a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 22345d4f98a2SYan Zheng 2235efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 223676dda93cSYan, Zheng 223716cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 223816cdcec7SMiao Xie 223976dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 224076dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 22412c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 224276dda93cSYan, Zheng else 22432c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 224432471dc2SDavid Sterba 22456596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2246e9d0b13bSChris Mason } 2247572d9ab7SDavid Sterba 2248572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2249572d9ab7SDavid Sterba { 2250572d9ab7SDavid Sterba unsigned long prev; 2251572d9ab7SDavid Sterba unsigned long bit; 2252572d9ab7SDavid Sterba 22536c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2254572d9ab7SDavid Sterba if (!prev) 2255572d9ab7SDavid Sterba return; 2256572d9ab7SDavid Sterba 22577e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 22587e1876acSDavid Sterba if (prev & bit) 22597e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22607e1876acSDavid Sterba prev &= ~bit; 22617e1876acSDavid Sterba 22627e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 22637e1876acSDavid Sterba if (prev & bit) 22647e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 22657e1876acSDavid Sterba prev &= ~bit; 22667e1876acSDavid Sterba 2267d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2268d51033d0SDavid Sterba if (prev & bit) 2269d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2270d51033d0SDavid Sterba prev &= ~bit; 2271d51033d0SDavid Sterba 2272572d9ab7SDavid Sterba if (prev) 2273572d9ab7SDavid Sterba btrfs_warn(fs_info, 2274572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2275572d9ab7SDavid Sterba } 2276