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) 68ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 69ab8d0fc4SJeff Mahoney "pending csums is %llu", 701262133bSJosef Bacik transaction->delayed_refs.pending_csums); 716df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 726df9a95eSJosef Bacik struct extent_map *em; 736df9a95eSJosef Bacik 746df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 756df9a95eSJosef Bacik struct extent_map, list); 766df9a95eSJosef Bacik list_del_init(&em->list); 776df9a95eSJosef Bacik free_extent_map(em); 786df9a95eSJosef Bacik } 797785a663SFilipe Manana /* 807785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 817785a663SFilipe Manana * because the transaction was aborted and a commit did not 827785a663SFilipe Manana * happen (things failed before writing the new superblock 837785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 847785a663SFilipe Manana * discard the physical locations of the block groups. 857785a663SFilipe Manana */ 867785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 877785a663SFilipe Manana struct btrfs_block_group_cache *cache; 887785a663SFilipe Manana 897785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 907785a663SFilipe Manana struct btrfs_block_group_cache, 917785a663SFilipe Manana bg_list); 927785a663SFilipe Manana list_del_init(&cache->bg_list); 937785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 947785a663SFilipe Manana btrfs_put_block_group(cache); 957785a663SFilipe Manana } 962c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 9779154b1bSChris Mason } 9878fae27eSChris Mason } 9979154b1bSChris Mason 100663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 101663dfbb0SFilipe Manana { 102663dfbb0SFilipe Manana spin_lock(&tree->lock); 103b666a9cdSDavid Sterba /* 104b666a9cdSDavid Sterba * Do a single barrier for the waitqueue_active check here, the state 105b666a9cdSDavid Sterba * of the waitqueue should not change once clear_btree_io_tree is 106b666a9cdSDavid Sterba * called. 107b666a9cdSDavid Sterba */ 108b666a9cdSDavid Sterba smp_mb(); 109663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 110663dfbb0SFilipe Manana struct rb_node *node; 111663dfbb0SFilipe Manana struct extent_state *state; 112663dfbb0SFilipe Manana 113663dfbb0SFilipe Manana node = rb_first(&tree->state); 114663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 115663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 116663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 117663dfbb0SFilipe Manana /* 118663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 119663dfbb0SFilipe Manana * changes in the flags of extent states ever. 120663dfbb0SFilipe Manana */ 121663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 122663dfbb0SFilipe Manana free_extent_state(state); 123351810c1SDavid Sterba 124351810c1SDavid Sterba cond_resched_lock(&tree->lock); 125663dfbb0SFilipe Manana } 126663dfbb0SFilipe Manana spin_unlock(&tree->lock); 127663dfbb0SFilipe Manana } 128663dfbb0SFilipe Manana 1299e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 1309e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 131817d52f8SJosef Bacik { 1329e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1339e351cc8SJosef Bacik 1349e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1359e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1369e351cc8SJosef Bacik dirty_list) { 1379e351cc8SJosef Bacik list_del_init(&root->dirty_list); 138817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 139817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1409e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1419e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 142663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1439e351cc8SJosef Bacik } 1442b9dbef2SJosef Bacik 1452b9dbef2SJosef Bacik /* We can free old roots now. */ 1462b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1472b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1482b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1492b9dbef2SJosef Bacik struct btrfs_root, root_list); 1502b9dbef2SJosef Bacik list_del_init(&root->root_list); 1512b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1522b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1532b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1542b9dbef2SJosef Bacik } 1552b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1569e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 157817d52f8SJosef Bacik } 158817d52f8SJosef Bacik 1590860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1600860adfdSMiao Xie unsigned int type) 1610860adfdSMiao Xie { 1620860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1630860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1640860adfdSMiao Xie } 1650860adfdSMiao Xie 1660860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1670860adfdSMiao Xie unsigned int type) 1680860adfdSMiao Xie { 1690860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1700860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1710860adfdSMiao Xie } 1720860adfdSMiao Xie 1730860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1740860adfdSMiao Xie unsigned int type) 1750860adfdSMiao Xie { 1760860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1770860adfdSMiao Xie } 1780860adfdSMiao Xie 1790860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1800860adfdSMiao Xie { 1810860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 182178260b2SMiao Xie } 183178260b2SMiao Xie 184d352ac68SChris Mason /* 185d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 186d352ac68SChris Mason */ 1870860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 18879154b1bSChris Mason { 18979154b1bSChris Mason struct btrfs_transaction *cur_trans; 19019ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 191a4abeea4SJosef Bacik 19219ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 193d43317dcSChris Mason loop: 19449b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 19587533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 19619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 19749b25e05SJeff Mahoney return -EROFS; 19849b25e05SJeff Mahoney } 19949b25e05SJeff Mahoney 20019ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 201a4abeea4SJosef Bacik if (cur_trans) { 202871383beSDavid Sterba if (cur_trans->aborted) { 20319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 20449b25e05SJeff Mahoney return cur_trans->aborted; 205871383beSDavid Sterba } 2064a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 207178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 208178260b2SMiao Xie return -EBUSY; 209178260b2SMiao Xie } 210a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 211a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 2120860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 21319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 214a4abeea4SJosef Bacik return 0; 215a4abeea4SJosef Bacik } 21619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 217a4abeea4SJosef Bacik 218354aa0fbSMiao Xie /* 219354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 220354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 221354aa0fbSMiao Xie */ 222354aa0fbSMiao Xie if (type == TRANS_ATTACH) 223354aa0fbSMiao Xie return -ENOENT; 224354aa0fbSMiao Xie 2254a9d8bdeSMiao Xie /* 2264a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 2274a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 2284a9d8bdeSMiao Xie */ 2294a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2304a9d8bdeSMiao Xie 231a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 232db5b493aSTsutomu Itoh if (!cur_trans) 233db5b493aSTsutomu Itoh return -ENOMEM; 234d43317dcSChris Mason 23519ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 23619ae4e81SJan Schmidt if (fs_info->running_transaction) { 237d43317dcSChris Mason /* 238d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2394a9d8bdeSMiao Xie * to redo the checks above 240d43317dcSChris Mason */ 241a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 242d43317dcSChris Mason goto loop; 24387533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 244e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2457b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 2467b8b92afSJosef Bacik return -EROFS; 247a4abeea4SJosef Bacik } 248d43317dcSChris Mason 249ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 25013c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2510860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 25279154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 25379154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 254161c3549SJosef Bacik init_waitqueue_head(&cur_trans->pending_wait); 2554a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 256a4abeea4SJosef Bacik /* 257a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 258a4abeea4SJosef Bacik * commit the transaction. 259a4abeea4SJosef Bacik */ 260a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 261161c3549SJosef Bacik atomic_set(&cur_trans->pending_ordered, 0); 2623204d33cSJosef Bacik cur_trans->flags = 0; 26308607c1bSChris Mason cur_trans->start_time = get_seconds(); 26456bec294SChris Mason 265a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 266a099d0fdSAlexandru Moise 267c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2683368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 269d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 27020b297d6SJan Schmidt 27120b297d6SJan Schmidt /* 27220b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 27320b297d6SJan Schmidt * the log must never go across transaction boundaries. 27420b297d6SJan Schmidt */ 27520b297d6SJan Schmidt smp_mb(); 27631b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2775d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 27831b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2795d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when 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, 3146426c7adSQu Wenruo struct btrfs_root *root, 3156426c7adSQu Wenruo int force) 3166702ed49SChris Mason { 3170b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3180b246afaSJeff Mahoney 3196426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 3206426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 3210b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 3225d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 3235d4f98a2SYan Zheng 3247585717fSChris Mason /* 32527cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 3267585717fSChris Mason * we have the reloc mutex held now, so there 3277585717fSChris Mason * is only one writer in this function 3287585717fSChris Mason */ 32927cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3307585717fSChris Mason 33127cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 3327585717fSChris Mason * they find our root->last_trans update 3337585717fSChris Mason */ 3347585717fSChris Mason smp_wmb(); 3357585717fSChris Mason 3360b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3376426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 3380b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 339a4abeea4SJosef Bacik return 0; 340a4abeea4SJosef Bacik } 3410b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 3426702ed49SChris Mason (unsigned long)root->root_key.objectid, 3436702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 3440b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3457585717fSChris Mason root->last_trans = trans->transid; 3467585717fSChris Mason 3477585717fSChris Mason /* this is pretty tricky. We don't want to 3487585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3497585717fSChris Mason * unless we're really doing the first setup for this root in 3507585717fSChris Mason * this transaction. 3517585717fSChris Mason * 3527585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3537585717fSChris Mason * if we want to take the expensive mutex. 3547585717fSChris Mason * 3557585717fSChris Mason * But, we have to set root->last_trans before we 3567585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3577585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 35827cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3597585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3607585717fSChris Mason * 3617585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3627585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3637585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3647585717fSChris Mason * done before we pop in the zero below 3657585717fSChris Mason */ 3665d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 367c7548af6SChris Mason smp_mb__before_atomic(); 36827cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3696702ed49SChris Mason } 3705d4f98a2SYan Zheng return 0; 3716702ed49SChris Mason } 3725d4f98a2SYan Zheng 3737585717fSChris Mason 3742b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3752b9dbef2SJosef Bacik struct btrfs_root *root) 3762b9dbef2SJosef Bacik { 3770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3782b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3792b9dbef2SJosef Bacik 3802b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3812b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3822b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3832b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3842b9dbef2SJosef Bacik 3852b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3860b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3870b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3882b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3892b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3900b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3912b9dbef2SJosef Bacik } 3922b9dbef2SJosef Bacik 3937585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3947585717fSChris Mason struct btrfs_root *root) 3957585717fSChris Mason { 3960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3970b246afaSJeff Mahoney 39827cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3997585717fSChris Mason return 0; 4007585717fSChris Mason 4017585717fSChris Mason /* 40227cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 4037585717fSChris Mason * and barriers 4047585717fSChris Mason */ 4057585717fSChris Mason smp_rmb(); 4067585717fSChris Mason if (root->last_trans == trans->transid && 40727cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 4087585717fSChris Mason return 0; 4097585717fSChris Mason 4100b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 4116426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 4120b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 4137585717fSChris Mason 4147585717fSChris Mason return 0; 4157585717fSChris Mason } 4167585717fSChris Mason 4174a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 4184a9d8bdeSMiao Xie { 4194a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 420501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 421501407aaSJosef Bacik !trans->aborted); 4224a9d8bdeSMiao Xie } 4234a9d8bdeSMiao Xie 424d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 425d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 426d352ac68SChris Mason * transaction might not be fully on disk. 427d352ac68SChris Mason */ 42837d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 42979154b1bSChris Mason { 4300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 431f9295749SChris Mason struct btrfs_transaction *cur_trans; 43279154b1bSChris Mason 4330b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4340b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 4354a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 43613c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 4370b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 43872d63ed6SLi Zefan 4390b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 440501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 441501407aaSJosef Bacik cur_trans->aborted); 442724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 443a4abeea4SJosef Bacik } else { 4440b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 445f9295749SChris Mason } 44637d1aeeeSChris Mason } 44737d1aeeeSChris Mason 448a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 44937d1aeeeSChris Mason { 4500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4510b246afaSJeff Mahoney 4520b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 453a4abeea4SJosef Bacik return 0; 454a4abeea4SJosef Bacik 455a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 456a22285a6SYan, Zheng return 1; 457a4abeea4SJosef Bacik 458a4abeea4SJosef Bacik if (type == TRANS_START && 4590b246afaSJeff Mahoney !atomic_read(&fs_info->open_ioctl_trans)) 460a4abeea4SJosef Bacik return 1; 461a4abeea4SJosef Bacik 462a22285a6SYan, Zheng return 0; 463a22285a6SYan, Zheng } 464a22285a6SYan, Zheng 46520dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 46620dd2cbfSMiao Xie { 4670b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4680b246afaSJeff Mahoney 4690b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 47027cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 47120dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 47220dd2cbfSMiao Xie root->reloc_root) 47320dd2cbfSMiao Xie return false; 47420dd2cbfSMiao Xie 47520dd2cbfSMiao Xie return true; 47620dd2cbfSMiao Xie } 47720dd2cbfSMiao Xie 47808e007d2SMiao Xie static struct btrfs_trans_handle * 4795aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 4805aed1dd8SAlexandru Moise unsigned int type, enum btrfs_reserve_flush_enum flush) 481a22285a6SYan, Zheng { 4820b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4830b246afaSJeff Mahoney 484a22285a6SYan, Zheng struct btrfs_trans_handle *h; 485a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 486b5009945SJosef Bacik u64 num_bytes = 0; 487c5567237SArne Jansen u64 qgroup_reserved = 0; 48820dd2cbfSMiao Xie bool reloc_reserved = false; 48920dd2cbfSMiao Xie int ret; 490acce952bSliubo 49146c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4922755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 49346c4e71eSFilipe Manana 4940b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 495acce952bSliubo return ERR_PTR(-EROFS); 4962a1eb461SJosef Bacik 49746c4e71eSFilipe Manana if (current->journal_info) { 4980860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4992a1eb461SJosef Bacik h = current->journal_info; 5002a1eb461SJosef Bacik h->use_count++; 501b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 5022a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 5032a1eb461SJosef Bacik h->block_rsv = NULL; 5042a1eb461SJosef Bacik goto got_it; 5052a1eb461SJosef Bacik } 506b5009945SJosef Bacik 507b5009945SJosef Bacik /* 508b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 509b5009945SJosef Bacik * the appropriate flushing if need be. 510b5009945SJosef Bacik */ 5110b246afaSJeff Mahoney if (num_items > 0 && root != fs_info->chunk_root) { 5120b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 5137174109cSQu Wenruo ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved); 514c5567237SArne Jansen if (ret) 515c5567237SArne Jansen return ERR_PTR(ret); 516c5567237SArne Jansen 5170b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 51820dd2cbfSMiao Xie /* 51920dd2cbfSMiao Xie * Do the reservation for the relocation root creation 52020dd2cbfSMiao Xie */ 521ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 5220b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 52320dd2cbfSMiao Xie reloc_reserved = true; 52420dd2cbfSMiao Xie } 52520dd2cbfSMiao Xie 5260b246afaSJeff Mahoney ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, 52708e007d2SMiao Xie num_bytes, flush); 528b5009945SJosef Bacik if (ret) 529843fcf35SMiao Xie goto reserve_fail; 530b5009945SJosef Bacik } 531a22285a6SYan, Zheng again: 532f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 533843fcf35SMiao Xie if (!h) { 534843fcf35SMiao Xie ret = -ENOMEM; 535843fcf35SMiao Xie goto alloc_fail; 536843fcf35SMiao Xie } 537a22285a6SYan, Zheng 53898114659SJosef Bacik /* 53998114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 54098114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 54198114659SJosef Bacik * because we're already holding a ref. We need this because we could 54298114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 54398114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 544354aa0fbSMiao Xie * 545354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 546354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 54798114659SJosef Bacik */ 5480860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5490b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 550b2b5ef5cSJan Kara 551a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 55237d1aeeeSChris Mason wait_current_trans(root); 553a22285a6SYan, Zheng 554a4abeea4SJosef Bacik do { 555354aa0fbSMiao Xie ret = join_transaction(root, type); 556178260b2SMiao Xie if (ret == -EBUSY) { 557a4abeea4SJosef Bacik wait_current_trans(root); 558178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 559178260b2SMiao Xie ret = -ENOENT; 560178260b2SMiao Xie } 561a4abeea4SJosef Bacik } while (ret == -EBUSY); 562a4abeea4SJosef Bacik 563a43f7f82SLiu Bo if (ret < 0) 564843fcf35SMiao Xie goto join_fail; 5650f7d52f4SChris Mason 5660b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 567a22285a6SYan, Zheng 568a22285a6SYan, Zheng h->transid = cur_trans->transid; 569a22285a6SYan, Zheng h->transaction = cur_trans; 570d13603efSArne Jansen h->root = root; 5712a1eb461SJosef Bacik h->use_count = 1; 57264b63580SJeff Mahoney h->fs_info = root->fs_info; 5737174109cSQu Wenruo 574a698d075SMiao Xie h->type = type; 575d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 576bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 577ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 578b7ec40d7SChris Mason 579a22285a6SYan, Zheng smp_mb(); 5804a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5814a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 582abdd2e80SFilipe Manana current->journal_info = h; 583a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 584a22285a6SYan, Zheng goto again; 585a22285a6SYan, Zheng } 5869ed74f2dSJosef Bacik 587b5009945SJosef Bacik if (num_bytes) { 5880b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5892bcc0328SLiu Bo h->transid, num_bytes, 1); 5900b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 591b5009945SJosef Bacik h->bytes_reserved = num_bytes; 59220dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 593a22285a6SYan, Zheng } 594a22285a6SYan, Zheng 5952a1eb461SJosef Bacik got_it: 596a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 597a22285a6SYan, Zheng 598a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 599a22285a6SYan, Zheng current->journal_info = h; 60079154b1bSChris Mason return h; 601843fcf35SMiao Xie 602843fcf35SMiao Xie join_fail: 6030860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 6040b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 605843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 606843fcf35SMiao Xie alloc_fail: 607843fcf35SMiao Xie if (num_bytes) 6080b246afaSJeff Mahoney btrfs_block_rsv_release(root, &fs_info->trans_block_rsv, 609843fcf35SMiao Xie num_bytes); 610843fcf35SMiao Xie reserve_fail: 6117174109cSQu Wenruo btrfs_qgroup_free_meta(root, qgroup_reserved); 612843fcf35SMiao Xie return ERR_PTR(ret); 61379154b1bSChris Mason } 61479154b1bSChris Mason 615f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6165aed1dd8SAlexandru Moise unsigned int num_items) 617f9295749SChris Mason { 61808e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 61908e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 620f9295749SChris Mason } 6218eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6228eab77ffSFilipe Manana struct btrfs_root *root, 6238eab77ffSFilipe Manana unsigned int num_items, 6248eab77ffSFilipe Manana int min_factor) 6258eab77ffSFilipe Manana { 6260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6278eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6288eab77ffSFilipe Manana u64 num_bytes; 6298eab77ffSFilipe Manana int ret; 6308eab77ffSFilipe Manana 6318eab77ffSFilipe Manana trans = btrfs_start_transaction(root, num_items); 6328eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6338eab77ffSFilipe Manana return trans; 6348eab77ffSFilipe Manana 6358eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6368eab77ffSFilipe Manana if (IS_ERR(trans)) 6378eab77ffSFilipe Manana return trans; 6388eab77ffSFilipe Manana 6390b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6400b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6410b246afaSJeff Mahoney num_bytes, min_factor); 6428eab77ffSFilipe Manana if (ret) { 6438eab77ffSFilipe Manana btrfs_end_transaction(trans, root); 6448eab77ffSFilipe Manana return ERR_PTR(ret); 6458eab77ffSFilipe Manana } 6468eab77ffSFilipe Manana 6470b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6488eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6490b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 65088d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6518eab77ffSFilipe Manana 6528eab77ffSFilipe Manana return trans; 6538eab77ffSFilipe Manana } 6548407aa46SMiao Xie 65508e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6565aed1dd8SAlexandru Moise struct btrfs_root *root, 6575aed1dd8SAlexandru Moise unsigned int num_items) 6588407aa46SMiao Xie { 65908e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 66008e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 6618407aa46SMiao Xie } 6628407aa46SMiao Xie 6637a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 664f9295749SChris Mason { 665575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN, 666575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 667f9295749SChris Mason } 668f9295749SChris Mason 6697a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6700af3d00bSJosef Bacik { 671575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 672575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6730af3d00bSJosef Bacik } 6740af3d00bSJosef Bacik 6757a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6769ca9ee09SSage Weil { 677575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_USERSPACE, 678575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6799ca9ee09SSage Weil } 6809ca9ee09SSage Weil 681d4edf39bSMiao Xie /* 682d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 683d4edf39bSMiao Xie * 684d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 685d4edf39bSMiao Xie * don't want to start a new one. 686d4edf39bSMiao Xie * 687d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 688d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 689d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 690d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 691d4edf39bSMiao Xie * invoke 692d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 693d4edf39bSMiao Xie */ 694354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 69560376ce4SJosef Bacik { 696575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 697575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 69860376ce4SJosef Bacik } 69960376ce4SJosef Bacik 700d4edf39bSMiao Xie /* 70190b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 702d4edf39bSMiao Xie * 703d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 704d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 705d4edf39bSMiao Xie * complete. 706d4edf39bSMiao Xie */ 707d4edf39bSMiao Xie struct btrfs_trans_handle * 708d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 709d4edf39bSMiao Xie { 710d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 711d4edf39bSMiao Xie 712575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 713575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 714d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 715d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 716d4edf39bSMiao Xie 717d4edf39bSMiao Xie return trans; 718d4edf39bSMiao Xie } 719d4edf39bSMiao Xie 720d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 721b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 72289ce8a63SChris Mason struct btrfs_transaction *commit) 72389ce8a63SChris Mason { 7244a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 72589ce8a63SChris Mason } 72689ce8a63SChris Mason 72746204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 72846204592SSage Weil { 7290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 73046204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7318cd2807fSMiao Xie int ret = 0; 73246204592SSage Weil 73346204592SSage Weil if (transid) { 7340b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 735a4abeea4SJosef Bacik goto out; 73646204592SSage Weil 73746204592SSage Weil /* find specified transaction */ 7380b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7390b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 74046204592SSage Weil if (t->transid == transid) { 74146204592SSage Weil cur_trans = t; 742a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 7438cd2807fSMiao Xie ret = 0; 74446204592SSage Weil break; 74546204592SSage Weil } 7468cd2807fSMiao Xie if (t->transid > transid) { 7478cd2807fSMiao Xie ret = 0; 74846204592SSage Weil break; 74946204592SSage Weil } 7508cd2807fSMiao Xie } 7510b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 75242383020SSage Weil 75342383020SSage Weil /* 75442383020SSage Weil * The specified transaction doesn't exist, or we 75542383020SSage Weil * raced with btrfs_commit_transaction 75642383020SSage Weil */ 75742383020SSage Weil if (!cur_trans) { 7580b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 75942383020SSage Weil ret = -EINVAL; 7608cd2807fSMiao Xie goto out; 76142383020SSage Weil } 76246204592SSage Weil } else { 76346204592SSage Weil /* find newest transaction that is committing | committed */ 7640b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7650b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 76646204592SSage Weil list) { 7674a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7684a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7693473f3c0SJosef Bacik break; 77046204592SSage Weil cur_trans = t; 771a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 77246204592SSage Weil break; 77346204592SSage Weil } 77446204592SSage Weil } 7750b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 77646204592SSage Weil if (!cur_trans) 777a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 77846204592SSage Weil } 77946204592SSage Weil 78046204592SSage Weil wait_for_commit(root, cur_trans); 781724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 782a4abeea4SJosef Bacik out: 78346204592SSage Weil return ret; 78446204592SSage Weil } 78546204592SSage Weil 78637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 78737d1aeeeSChris Mason { 7880b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 7890b246afaSJeff Mahoney 7900b246afaSJeff Mahoney if (!atomic_read(&fs_info->open_ioctl_trans)) 79137d1aeeeSChris Mason wait_current_trans(root); 79237d1aeeeSChris Mason } 79337d1aeeeSChris Mason 7948929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7958929ecfaSYan, Zheng struct btrfs_root *root) 7968929ecfaSYan, Zheng { 7970b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 7980b246afaSJeff Mahoney 7990b246afaSJeff Mahoney if (fs_info->global_block_rsv.space_info->full && 8000a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 8011be41b78SJosef Bacik return 1; 80236ba022aSJosef Bacik 8030b246afaSJeff Mahoney return !!btrfs_block_rsv_check(root, &fs_info->global_block_rsv, 5); 8048929ecfaSYan, Zheng } 8058929ecfaSYan, Zheng 8068929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 8078929ecfaSYan, Zheng struct btrfs_root *root) 8088929ecfaSYan, Zheng { 8098929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 8108929ecfaSYan, Zheng int updates; 81149b25e05SJeff Mahoney int err; 8128929ecfaSYan, Zheng 813a4abeea4SJosef Bacik smp_mb(); 8144a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8154a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8168929ecfaSYan, Zheng return 1; 8178929ecfaSYan, Zheng 8188929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 8198929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 82049b25e05SJeff Mahoney if (updates) { 82128ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 82249b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 82349b25e05SJeff Mahoney return err; 82449b25e05SJeff Mahoney } 8258929ecfaSYan, Zheng 8268929ecfaSYan, Zheng return should_end_transaction(trans, root); 8278929ecfaSYan, Zheng } 8288929ecfaSYan, Zheng 82989ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 830a698d075SMiao Xie struct btrfs_root *root, int throttle) 83179154b1bSChris Mason { 8328929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 833ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 83431b9655fSJosef Bacik u64 transid = trans->transid; 8351be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 836a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8374edc2ca3SDave Jones int err = 0; 838a79b7d4bSChris Mason int must_run_delayed_refs = 0; 839d6e4a428SChris Mason 8403bbb24b2SJosef Bacik if (trans->use_count > 1) { 8413bbb24b2SJosef Bacik trans->use_count--; 8422a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8432a1eb461SJosef Bacik return 0; 8442a1eb461SJosef Bacik } 8452a1eb461SJosef Bacik 846b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 8474c13d758SJosef Bacik trans->block_rsv = NULL; 848c5567237SArne Jansen 849ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 850ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 851ea658badSJosef Bacik 852c3e69d58SChris Mason trans->delayed_ref_updates = 0; 853a79b7d4bSChris Mason if (!trans->sync) { 854a79b7d4bSChris Mason must_run_delayed_refs = 855a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 8560a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 857a79b7d4bSChris Mason 858a79b7d4bSChris Mason /* 859a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 860a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 861a79b7d4bSChris Mason */ 862a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 863a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 864a79b7d4bSChris Mason must_run_delayed_refs = 2; 86556bec294SChris Mason } 866bb721703SChris Mason 8670e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8680e721106SJosef Bacik trans->block_rsv = NULL; 86956bec294SChris Mason 870ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 871ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 872ea658badSJosef Bacik 8734fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8744fbcdf66SFilipe Manana 8750b246afaSJeff Mahoney if (lock && !atomic_read(&info->open_ioctl_trans) && 8764a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8774a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8784a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8794a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8814a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 882a4abeea4SJosef Bacik } 8838929ecfaSYan, Zheng 8844a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8853bbb24b2SJosef Bacik if (throttle) 8868929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8873bbb24b2SJosef Bacik else 8888929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8898929ecfaSYan, Zheng } 8908929ecfaSYan, Zheng 8910860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8920b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8936df7881aSJosef Bacik 8948929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 89513c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 89613c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8970860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 89889ce8a63SChris Mason 899a83342aaSDavid Sterba /* 900a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 901a83342aaSDavid Sterba */ 90299d16cbcSSage Weil smp_mb(); 90379154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 90479154b1bSChris Mason wake_up(&cur_trans->writer_wait); 905724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 9069ed74f2dSJosef Bacik 9079ed74f2dSJosef Bacik if (current->journal_info == trans) 9089ed74f2dSJosef Bacik current->journal_info = NULL; 909ab78c84dSChris Mason 91024bbcf04SYan, Zheng if (throttle) 91124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 91224bbcf04SYan, Zheng 91349b25e05SJeff Mahoney if (trans->aborted || 9140b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 9154e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 9164edc2ca3SDave Jones err = -EIO; 9174e121c06SJosef Bacik } 918edf39272SJan Schmidt assert_qgroups_uptodate(trans); 91949b25e05SJeff Mahoney 9204edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 921a79b7d4bSChris Mason if (must_run_delayed_refs) { 92231b9655fSJosef Bacik btrfs_async_run_delayed_refs(root, cur, transid, 923a79b7d4bSChris Mason must_run_delayed_refs == 1); 924a79b7d4bSChris Mason } 9254edc2ca3SDave Jones return err; 92679154b1bSChris Mason } 92779154b1bSChris Mason 92889ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 92989ce8a63SChris Mason struct btrfs_root *root) 93089ce8a63SChris Mason { 93198ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 93289ce8a63SChris Mason } 93389ce8a63SChris Mason 93489ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 93589ce8a63SChris Mason struct btrfs_root *root) 93689ce8a63SChris Mason { 93798ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 93816cdcec7SMiao Xie } 93916cdcec7SMiao Xie 940d352ac68SChris Mason /* 941d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 942d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 943690587d1SChris Mason * those extents are sent to disk but does not wait on them 944d352ac68SChris Mason */ 945690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 9468cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 94779154b1bSChris Mason { 948777e6bd7SChris Mason int err = 0; 9497c4452b9SChris Mason int werr = 0; 9500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 9510b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 952e6138876SJosef Bacik struct extent_state *cached_state = NULL; 953777e6bd7SChris Mason u64 start = 0; 9545f39d397SChris Mason u64 end; 9557c4452b9SChris Mason 9561728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 957e6138876SJosef Bacik mark, &cached_state)) { 958663dfbb0SFilipe Manana bool wait_writeback = false; 959663dfbb0SFilipe Manana 960663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 961663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 962210aa277SDavid Sterba mark, &cached_state); 963663dfbb0SFilipe Manana /* 964663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 965663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 966663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 967663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 968663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 969663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 970663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 971663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 972663dfbb0SFilipe Manana * (and without errors). 973663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 974663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 975663dfbb0SFilipe Manana */ 976663dfbb0SFilipe Manana if (err == -ENOMEM) { 977663dfbb0SFilipe Manana err = 0; 978663dfbb0SFilipe Manana wait_writeback = true; 979663dfbb0SFilipe Manana } 980663dfbb0SFilipe Manana if (!err) 9811728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9827c4452b9SChris Mason if (err) 9837c4452b9SChris Mason werr = err; 984663dfbb0SFilipe Manana else if (wait_writeback) 985663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 986e38e2ed7SFilipe Manana free_extent_state(cached_state); 987663dfbb0SFilipe Manana cached_state = NULL; 9881728366eSJosef Bacik cond_resched(); 9891728366eSJosef Bacik start = end + 1; 9907c4452b9SChris Mason } 991690587d1SChris Mason return werr; 992690587d1SChris Mason } 993690587d1SChris Mason 994690587d1SChris Mason /* 995690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 996690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 997690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 998690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 999690587d1SChris Mason */ 1000690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 10018cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1002690587d1SChris Mason { 1003690587d1SChris Mason int err = 0; 1004690587d1SChris Mason int werr = 0; 10050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10060b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 1007e6138876SJosef Bacik struct extent_state *cached_state = NULL; 1008690587d1SChris Mason u64 start = 0; 1009690587d1SChris Mason u64 end; 1010656f30dbSFilipe Manana bool errors = false; 1011690587d1SChris Mason 10121728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 1013e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 1014663dfbb0SFilipe Manana /* 1015663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 1016663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 1017663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1018663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1019663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1020663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1021663dfbb0SFilipe Manana */ 1022663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1023663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1024e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1025663dfbb0SFilipe Manana if (err == -ENOMEM) 1026663dfbb0SFilipe Manana err = 0; 1027663dfbb0SFilipe Manana if (!err) 10281728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1029777e6bd7SChris Mason if (err) 1030777e6bd7SChris Mason werr = err; 1031e38e2ed7SFilipe Manana free_extent_state(cached_state); 1032e38e2ed7SFilipe Manana cached_state = NULL; 1033777e6bd7SChris Mason cond_resched(); 10341728366eSJosef Bacik start = end + 1; 1035777e6bd7SChris Mason } 10367c4452b9SChris Mason if (err) 10377c4452b9SChris Mason werr = err; 1038656f30dbSFilipe Manana 1039656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1040656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10410b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1042656f30dbSFilipe Manana errors = true; 1043656f30dbSFilipe Manana 1044656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10450b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1046656f30dbSFilipe Manana errors = true; 1047656f30dbSFilipe Manana } else { 10480b246afaSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 1049656f30dbSFilipe Manana errors = true; 1050656f30dbSFilipe Manana } 1051656f30dbSFilipe Manana 1052656f30dbSFilipe Manana if (errors && !werr) 1053656f30dbSFilipe Manana werr = -EIO; 1054656f30dbSFilipe Manana 10557c4452b9SChris Mason return werr; 105679154b1bSChris Mason } 105779154b1bSChris Mason 1058690587d1SChris Mason /* 1059690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1060690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1061690587d1SChris Mason * those extents are on disk for transaction or log commit 1062690587d1SChris Mason */ 1063171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10648cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1065690587d1SChris Mason { 1066690587d1SChris Mason int ret; 1067690587d1SChris Mason int ret2; 1068c6adc9ccSMiao Xie struct blk_plug plug; 1069690587d1SChris Mason 1070c6adc9ccSMiao Xie blk_start_plug(&plug); 10718cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1072c6adc9ccSMiao Xie blk_finish_plug(&plug); 10738cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1074bf0da8c1SChris Mason 1075bf0da8c1SChris Mason if (ret) 1076bf0da8c1SChris Mason return ret; 1077bf0da8c1SChris Mason if (ret2) 1078bf0da8c1SChris Mason return ret2; 1079bf0da8c1SChris Mason return 0; 1080690587d1SChris Mason } 1081690587d1SChris Mason 1082663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1083d0c803c4SChris Mason struct btrfs_root *root) 1084d0c803c4SChris Mason { 1085663dfbb0SFilipe Manana int ret; 1086663dfbb0SFilipe Manana 1087663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10888cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10898cef4e16SYan, Zheng EXTENT_DIRTY); 1090663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1091663dfbb0SFilipe Manana 1092663dfbb0SFilipe Manana return ret; 1093d0c803c4SChris Mason } 1094d0c803c4SChris Mason 1095d352ac68SChris Mason /* 1096d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1097d352ac68SChris Mason * 1098d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1099d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1100d352ac68SChris Mason * allocation tree. 1101d352ac68SChris Mason * 1102d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1103d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1104d352ac68SChris Mason */ 11050b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 110679154b1bSChris Mason struct btrfs_root *root) 110779154b1bSChris Mason { 110879154b1bSChris Mason int ret; 11090b86a832SChris Mason u64 old_root_bytenr; 111086b9f2ecSYan, Zheng u64 old_root_used; 11110b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11120b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 111379154b1bSChris Mason 111486b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 111556bec294SChris Mason 111679154b1bSChris Mason while (1) { 11170b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 111886b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1119ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 112079154b1bSChris Mason break; 112187ef2bb4SChris Mason 11225d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 112379154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11240b86a832SChris Mason &root->root_key, 11250b86a832SChris Mason &root->root_item); 112649b25e05SJeff Mahoney if (ret) 112749b25e05SJeff Mahoney return ret; 112856bec294SChris Mason 112986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1130e7070be1SJosef Bacik } 1131276e680dSYan Zheng 11320b86a832SChris Mason return 0; 11330b86a832SChris Mason } 11340b86a832SChris Mason 1135d352ac68SChris Mason /* 1136d352ac68SChris Mason * update all the cowonly tree roots on disk 113749b25e05SJeff Mahoney * 113849b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 113949b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 114049b25e05SJeff Mahoney * to clean up the delayed refs. 1141d352ac68SChris Mason */ 11425d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11430b86a832SChris Mason struct btrfs_root *root) 11440b86a832SChris Mason { 11450b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1146ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11471bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11480b86a832SChris Mason struct list_head *next; 114984234f3aSYan Zheng struct extent_buffer *eb; 115056bec294SChris Mason int ret; 115184234f3aSYan Zheng 115284234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 115349b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 115449b25e05SJeff Mahoney 0, &eb); 115584234f3aSYan Zheng btrfs_tree_unlock(eb); 115684234f3aSYan Zheng free_extent_buffer(eb); 11570b86a832SChris Mason 115849b25e05SJeff Mahoney if (ret) 115949b25e05SJeff Mahoney return ret; 116049b25e05SJeff Mahoney 116156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 116249b25e05SJeff Mahoney if (ret) 116349b25e05SJeff Mahoney return ret; 116487ef2bb4SChris Mason 11650b246afaSJeff Mahoney ret = btrfs_run_dev_stats(trans, fs_info); 1166c16ce190SJosef Bacik if (ret) 1167c16ce190SJosef Bacik return ret; 11680b246afaSJeff Mahoney ret = btrfs_run_dev_replace(trans, fs_info); 1169c16ce190SJosef Bacik if (ret) 1170c16ce190SJosef Bacik return ret; 11710b246afaSJeff Mahoney ret = btrfs_run_qgroups(trans, fs_info); 1172c16ce190SJosef Bacik if (ret) 1173c16ce190SJosef Bacik return ret; 1174546adb0dSJan Schmidt 1175dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1176dcdf7f6dSJosef Bacik if (ret) 1177dcdf7f6dSJosef Bacik return ret; 1178dcdf7f6dSJosef Bacik 1179546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1180546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1181c16ce190SJosef Bacik if (ret) 1182c16ce190SJosef Bacik return ret; 1183ea526d18SJosef Bacik again: 11840b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11850b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11860b86a832SChris Mason list_del_init(next); 11870b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1188e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 118987ef2bb4SChris Mason 11909e351cc8SJosef Bacik if (root != fs_info->extent_root) 11919e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11929e351cc8SJosef Bacik &trans->transaction->switch_commits); 119349b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 119449b25e05SJeff Mahoney if (ret) 119549b25e05SJeff Mahoney return ret; 1196ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1197ea526d18SJosef Bacik if (ret) 1198ea526d18SJosef Bacik return ret; 119979154b1bSChris Mason } 1200276e680dSYan Zheng 12011bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1202ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1203ea526d18SJosef Bacik if (ret) 1204ea526d18SJosef Bacik return ret; 1205ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1206ea526d18SJosef Bacik if (ret) 1207ea526d18SJosef Bacik return ret; 1208ea526d18SJosef Bacik } 1209ea526d18SJosef Bacik 1210ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1211ea526d18SJosef Bacik goto again; 1212ea526d18SJosef Bacik 12139e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 12149e351cc8SJosef Bacik &trans->transaction->switch_commits); 12158dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12168dabb742SStefan Behrens 121779154b1bSChris Mason return 0; 121879154b1bSChris Mason } 121979154b1bSChris Mason 1220d352ac68SChris Mason /* 1221d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1222d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1223d352ac68SChris Mason * be deleted 1224d352ac68SChris Mason */ 1225cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12265eda7b5eSChris Mason { 12270b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12280b246afaSJeff Mahoney 12290b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1230cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12310b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 12320b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 12335eda7b5eSChris Mason } 12345eda7b5eSChris Mason 1235d352ac68SChris Mason /* 12365d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1237d352ac68SChris Mason */ 12385d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12395b4aacefSJeff Mahoney struct btrfs_fs_info *fs_info) 12400f7d52f4SChris Mason { 12410f7d52f4SChris Mason struct btrfs_root *gang[8]; 12420f7d52f4SChris Mason int i; 12430f7d52f4SChris Mason int ret; 124454aa1f4dSChris Mason int err = 0; 124554aa1f4dSChris Mason 1246a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12470f7d52f4SChris Mason while (1) { 12485d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12495d4f98a2SYan Zheng (void **)gang, 0, 12500f7d52f4SChris Mason ARRAY_SIZE(gang), 12510f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12520f7d52f4SChris Mason if (ret == 0) 12530f7d52f4SChris Mason break; 12540f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12555b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12565d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12572619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12580f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1259a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 126031153d81SYan Zheng 1261e02119d5SChris Mason btrfs_free_log(trans, root); 12625d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1263d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1264e02119d5SChris Mason 126582d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 126682d5902dSLi Zefan 1267f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 126827cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1269c7548af6SChris Mason smp_mb__after_atomic(); 1270f1ebcc74SLiu Bo 1271978d910dSYan Zheng if (root->commit_root != root->node) { 12729e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12739e351cc8SJosef Bacik &trans->transaction->switch_commits); 1274978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1275978d910dSYan Zheng root->node); 1276978d910dSYan Zheng } 127731153d81SYan Zheng 12785d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12790f7d52f4SChris Mason &root->root_key, 12800f7d52f4SChris Mason &root->root_item); 1281a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 128254aa1f4dSChris Mason if (err) 128354aa1f4dSChris Mason break; 12847174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12850f7d52f4SChris Mason } 12869f3a7427SChris Mason } 1287a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 128854aa1f4dSChris Mason return err; 12890f7d52f4SChris Mason } 12900f7d52f4SChris Mason 1291d352ac68SChris Mason /* 1292de78b51aSEric Sandeen * defrag a given btree. 1293de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1294d352ac68SChris Mason */ 1295de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1296e9d0b13bSChris Mason { 1297e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1298e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12998929ecfaSYan, Zheng int ret; 1300e9d0b13bSChris Mason 130127cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1302e9d0b13bSChris Mason return 0; 13038929ecfaSYan, Zheng 13046b80053dSChris Mason while (1) { 13058929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 13068929ecfaSYan, Zheng if (IS_ERR(trans)) 13078929ecfaSYan, Zheng return PTR_ERR(trans); 13088929ecfaSYan, Zheng 1309de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 13108929ecfaSYan, Zheng 1311e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1312b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1313e9d0b13bSChris Mason cond_resched(); 1314e9d0b13bSChris Mason 1315ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1316e9d0b13bSChris Mason break; 1317210549ebSDavid Sterba 1318ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1319ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1320210549ebSDavid Sterba ret = -EAGAIN; 1321210549ebSDavid Sterba break; 1322210549ebSDavid Sterba } 1323e9d0b13bSChris Mason } 132427cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13258929ecfaSYan, Zheng return ret; 1326e9d0b13bSChris Mason } 1327e9d0b13bSChris Mason 1328d352ac68SChris Mason /* 13296426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 13306426c7adSQu Wenruo * 13316426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 13326426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13336426c7adSQu Wenruo * qgroup works. 13346426c7adSQu Wenruo */ 13356426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13366426c7adSQu Wenruo struct btrfs_root *src, 13376426c7adSQu Wenruo struct btrfs_root *parent, 13386426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13396426c7adSQu Wenruo u64 dst_objectid) 13406426c7adSQu Wenruo { 13416426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13426426c7adSQu Wenruo int ret; 13436426c7adSQu Wenruo 13446426c7adSQu Wenruo /* 13456426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13466426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13476426c7adSQu Wenruo * kick in anyway. 13486426c7adSQu Wenruo */ 13496426c7adSQu Wenruo mutex_lock(&fs_info->qgroup_ioctl_lock); 1350afcdd129SJosef Bacik if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { 13516426c7adSQu Wenruo mutex_unlock(&fs_info->qgroup_ioctl_lock); 13526426c7adSQu Wenruo return 0; 13536426c7adSQu Wenruo } 13546426c7adSQu Wenruo mutex_unlock(&fs_info->qgroup_ioctl_lock); 13556426c7adSQu Wenruo 13566426c7adSQu Wenruo /* 13576426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13586426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13596426c7adSQu Wenruo */ 13606426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13616426c7adSQu Wenruo 13625b4aacefSJeff Mahoney ret = commit_fs_roots(trans, fs_info); 13636426c7adSQu Wenruo if (ret) 13646426c7adSQu Wenruo goto out; 13656426c7adSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 13666426c7adSQu Wenruo if (ret < 0) 13676426c7adSQu Wenruo goto out; 13686426c7adSQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 13696426c7adSQu Wenruo if (ret < 0) 13706426c7adSQu Wenruo goto out; 13716426c7adSQu Wenruo 13726426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 13736426c7adSQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 13746426c7adSQu Wenruo src->root_key.objectid, dst_objectid, 13756426c7adSQu Wenruo inherit); 13766426c7adSQu Wenruo if (ret < 0) 13776426c7adSQu Wenruo goto out; 13786426c7adSQu Wenruo 13796426c7adSQu Wenruo /* 13806426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13816426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13826426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13836426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13846426c7adSQu Wenruo * 2) write all btree blocks onto disk 13856426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13866426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13876426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13886426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13896426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13906426c7adSQu Wenruo */ 13916426c7adSQu Wenruo ret = commit_cowonly_roots(trans, src); 13926426c7adSQu Wenruo if (ret) 13936426c7adSQu Wenruo goto out; 13946426c7adSQu Wenruo switch_commit_roots(trans->transaction, fs_info); 13956426c7adSQu Wenruo ret = btrfs_write_and_wait_transaction(trans, src); 13966426c7adSQu Wenruo if (ret) 1397f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13986426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13996426c7adSQu Wenruo 14006426c7adSQu Wenruo out: 14016426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 14026426c7adSQu Wenruo 14036426c7adSQu Wenruo /* 14046426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 14056426c7adSQu Wenruo * last_trans == cur_transid. 14066426c7adSQu Wenruo * Or it won't be committed again onto disk after later 14076426c7adSQu Wenruo * insert_dir_item() 14086426c7adSQu Wenruo */ 14096426c7adSQu Wenruo if (!ret) 14106426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 14116426c7adSQu Wenruo return ret; 14126426c7adSQu Wenruo } 14136426c7adSQu Wenruo 14146426c7adSQu Wenruo /* 1415d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1416aec8030aSMiao Xie * transaction commit. This does the actual creation. 1417aec8030aSMiao Xie * 1418aec8030aSMiao Xie * Note: 1419aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1420aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1421aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1422d352ac68SChris Mason */ 142380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 14243063d29fSChris Mason struct btrfs_fs_info *fs_info, 14253063d29fSChris Mason struct btrfs_pending_snapshot *pending) 14263063d29fSChris Mason { 14273063d29fSChris Mason struct btrfs_key key; 142880b6794dSChris Mason struct btrfs_root_item *new_root_item; 14293063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 14303063d29fSChris Mason struct btrfs_root *root = pending->root; 14316bdb72deSSage Weil struct btrfs_root *parent_root; 143298c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14336bdb72deSSage Weil struct inode *parent_inode; 143442874b3dSMiao Xie struct btrfs_path *path; 143542874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1436a22285a6SYan, Zheng struct dentry *dentry; 14373063d29fSChris Mason struct extent_buffer *tmp; 1438925baeddSChris Mason struct extent_buffer *old; 143904b285f3SDeepa Dinamani struct timespec cur_time; 1440aec8030aSMiao Xie int ret = 0; 1441d68fc57bSYan, Zheng u64 to_reserve = 0; 14426bdb72deSSage Weil u64 index = 0; 1443a22285a6SYan, Zheng u64 objectid; 1444b83cc969SLi Zefan u64 root_flags; 14458ea05e3aSAlexander Block uuid_le new_uuid; 14463063d29fSChris Mason 14478546b570SDavid Sterba ASSERT(pending->path); 14488546b570SDavid Sterba path = pending->path; 144942874b3dSMiao Xie 1450b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1451b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1452a22285a6SYan, Zheng 1453aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1454aec8030aSMiao Xie if (pending->error) 14556fa9700eSMiao Xie goto no_free_objectid; 14563063d29fSChris Mason 1457d6726335SQu Wenruo /* 1458d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1459d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1460d6726335SQu Wenruo */ 1461d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1462d6726335SQu Wenruo 1463147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1464d68fc57bSYan, Zheng 1465d68fc57bSYan, Zheng if (to_reserve > 0) { 1466aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1467aec8030aSMiao Xie &pending->block_rsv, 146808e007d2SMiao Xie to_reserve, 146908e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1470aec8030aSMiao Xie if (pending->error) 1471d6726335SQu Wenruo goto clear_skip_qgroup; 1472d68fc57bSYan, Zheng } 1473d68fc57bSYan, Zheng 14743063d29fSChris Mason key.objectid = objectid; 1475a22285a6SYan, Zheng key.offset = (u64)-1; 1476a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14773063d29fSChris Mason 14786fa9700eSMiao Xie rsv = trans->block_rsv; 1479a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14802382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14810b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 148288d3a5aaSJosef Bacik trans->transid, 148388d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1484a22285a6SYan, Zheng dentry = pending->dentry; 1485e9662f70SMiao Xie parent_inode = pending->dir; 1486a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14876426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1488a22285a6SYan, Zheng 1489c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 149004b285f3SDeepa Dinamani 14916bdb72deSSage Weil /* 14926bdb72deSSage Weil * insert the directory item 14936bdb72deSSage Weil */ 14946bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 149549b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 149642874b3dSMiao Xie 149742874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 149842874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 149942874b3dSMiao Xie btrfs_ino(parent_inode), 150042874b3dSMiao Xie dentry->d_name.name, 150142874b3dSMiao Xie dentry->d_name.len, 0); 150242874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1503fe66a05aSChris Mason pending->error = -EEXIST; 1504aec8030aSMiao Xie goto dir_item_existed; 150542874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 150642874b3dSMiao Xie ret = PTR_ERR(dir_item); 150766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15088732d44fSMiao Xie goto fail; 150979787eaaSJeff Mahoney } 151042874b3dSMiao Xie btrfs_release_path(path); 15116bdb72deSSage Weil 1512e999376fSChris Mason /* 1513e999376fSChris Mason * pull in the delayed directory update 1514e999376fSChris Mason * and the delayed inode item 1515e999376fSChris Mason * otherwise we corrupt the FS during 1516e999376fSChris Mason * snapshot 1517e999376fSChris Mason */ 1518e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 15198732d44fSMiao Xie if (ret) { /* Transaction aborted */ 152066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15218732d44fSMiao Xie goto fail; 15228732d44fSMiao Xie } 1523e999376fSChris Mason 15246426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 15256bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 15266bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 152708fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 15286bdb72deSSage Weil 1529b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1530b83cc969SLi Zefan if (pending->readonly) 1531b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1532b83cc969SLi Zefan else 1533b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1534b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1535b83cc969SLi Zefan 15368ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15378ea05e3aSAlexander Block trans->transid); 15388ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15398ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15408ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15418ea05e3aSAlexander Block BTRFS_UUID_SIZE); 154270023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 154370023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 154470023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15458ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15468ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15478ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15488ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 154970023da2SStefan Behrens } 15503cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15513cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 155270023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15538ea05e3aSAlexander Block 1554925baeddSChris Mason old = btrfs_lock_root_node(root); 155549b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 155679787eaaSJeff Mahoney if (ret) { 155779787eaaSJeff Mahoney btrfs_tree_unlock(old); 155879787eaaSJeff Mahoney free_extent_buffer(old); 155966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15608732d44fSMiao Xie goto fail; 156179787eaaSJeff Mahoney } 156249b25e05SJeff Mahoney 15635d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 15643063d29fSChris Mason 156549b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 156679787eaaSJeff Mahoney /* clean up in any case */ 1567925baeddSChris Mason btrfs_tree_unlock(old); 1568925baeddSChris Mason free_extent_buffer(old); 15698732d44fSMiao Xie if (ret) { 157066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15718732d44fSMiao Xie goto fail; 15728732d44fSMiao Xie } 1573f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 157427cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1575f1ebcc74SLiu Bo smp_wmb(); 1576f1ebcc74SLiu Bo 15775d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1578a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1579a22285a6SYan, Zheng key.offset = trans->transid; 1580a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1581925baeddSChris Mason btrfs_tree_unlock(tmp); 15823063d29fSChris Mason free_extent_buffer(tmp); 15838732d44fSMiao Xie if (ret) { 158466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15858732d44fSMiao Xie goto fail; 15868732d44fSMiao Xie } 15870660b5afSChris Mason 1588a22285a6SYan, Zheng /* 1589a22285a6SYan, Zheng * insert root back/forward references 1590a22285a6SYan, Zheng */ 15916bccf3abSJeff Mahoney ret = btrfs_add_root_ref(trans, fs_info, objectid, 1592a22285a6SYan, Zheng parent_root->root_key.objectid, 159333345d01SLi Zefan btrfs_ino(parent_inode), index, 1594a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15958732d44fSMiao Xie if (ret) { 159666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15978732d44fSMiao Xie goto fail; 15988732d44fSMiao Xie } 1599a22285a6SYan, Zheng 1600a22285a6SYan, Zheng key.offset = (u64)-1; 16010b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 160279787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 160379787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 160466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16058732d44fSMiao Xie goto fail; 160679787eaaSJeff Mahoney } 1607d68fc57bSYan, Zheng 160849b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 16098732d44fSMiao Xie if (ret) { 161066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16118732d44fSMiao Xie goto fail; 16128732d44fSMiao Xie } 1613361048f5SMiao Xie 1614361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 16158732d44fSMiao Xie if (ret) { 161666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16178732d44fSMiao Xie goto fail; 16188732d44fSMiao Xie } 161942874b3dSMiao Xie 16206426c7adSQu Wenruo /* 16216426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 16226426c7adSQu Wenruo * snapshot hack to do fast snapshot. 16236426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 16246426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 16256426c7adSQu Wenruo */ 16266426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 16276426c7adSQu Wenruo pending->inherit, objectid); 16286426c7adSQu Wenruo if (ret < 0) 16296426c7adSQu Wenruo goto fail; 16306426c7adSQu Wenruo 163142874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 163242874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 163342874b3dSMiao Xie parent_inode, &key, 163442874b3dSMiao Xie BTRFS_FT_DIR, index); 163542874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16369c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16378732d44fSMiao Xie if (ret) { 163866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16398732d44fSMiao Xie goto fail; 16408732d44fSMiao Xie } 164142874b3dSMiao Xie 164242874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 164342874b3dSMiao Xie dentry->d_name.len * 2); 164404b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1645c2050a45SDeepa Dinamani current_time(parent_inode); 1646be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1647dd5f9615SStefan Behrens if (ret) { 164866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1649dd5f9615SStefan Behrens goto fail; 1650dd5f9615SStefan Behrens } 16516bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, new_uuid.b, 1652dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1653dd5f9615SStefan Behrens if (ret) { 165466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1655dd5f9615SStefan Behrens goto fail; 1656dd5f9615SStefan Behrens } 1657dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 16586bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, 1659dd5f9615SStefan Behrens new_root_item->received_uuid, 1660dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1661dd5f9615SStefan Behrens objectid); 1662dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 166366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1664dd5f9615SStefan Behrens goto fail; 1665dd5f9615SStefan Behrens } 1666dd5f9615SStefan Behrens } 1667d6726335SQu Wenruo 1668d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1669d6726335SQu Wenruo if (ret) { 167066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1671d6726335SQu Wenruo goto fail; 1672d6726335SQu Wenruo } 1673d6726335SQu Wenruo 16743063d29fSChris Mason fail: 1675aec8030aSMiao Xie pending->error = ret; 1676aec8030aSMiao Xie dir_item_existed: 167798c9942aSLiu Bo trans->block_rsv = rsv; 16782382c5ccSLiu Bo trans->bytes_reserved = 0; 1679d6726335SQu Wenruo clear_skip_qgroup: 1680d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16816fa9700eSMiao Xie no_free_objectid: 16826fa9700eSMiao Xie kfree(new_root_item); 1683b0c0ea63SDavid Sterba pending->root_item = NULL; 168442874b3dSMiao Xie btrfs_free_path(path); 16858546b570SDavid Sterba pending->path = NULL; 16868546b570SDavid Sterba 168749b25e05SJeff Mahoney return ret; 16883063d29fSChris Mason } 16893063d29fSChris Mason 1690d352ac68SChris Mason /* 1691d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1692d352ac68SChris Mason */ 169380b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 16943063d29fSChris Mason struct btrfs_fs_info *fs_info) 16953063d29fSChris Mason { 1696aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16973063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1698aec8030aSMiao Xie int ret = 0; 16993de4586cSChris Mason 1700aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1701aec8030aSMiao Xie list_del(&pending->list); 1702aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1703aec8030aSMiao Xie if (ret) 1704aec8030aSMiao Xie break; 1705aec8030aSMiao Xie } 1706aec8030aSMiao Xie return ret; 17073de4586cSChris Mason } 17083de4586cSChris Mason 17095d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 17105d4f98a2SYan Zheng { 17110b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 17125d4f98a2SYan Zheng struct btrfs_root_item *root_item; 17135d4f98a2SYan Zheng struct btrfs_super_block *super; 17145d4f98a2SYan Zheng 17150b246afaSJeff Mahoney super = fs_info->super_copy; 17165d4f98a2SYan Zheng 17170b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 17185d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 17195d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 17205d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 17215d4f98a2SYan Zheng 17220b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 17235d4f98a2SYan Zheng super->root = root_item->bytenr; 17245d4f98a2SYan Zheng super->generation = root_item->generation; 17255d4f98a2SYan Zheng super->root_level = root_item->level; 17260b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 17270af3d00bSJosef Bacik super->cache_generation = root_item->generation; 17280b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 172926432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 17305d4f98a2SYan Zheng } 17315d4f98a2SYan Zheng 1732f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1733f36f3042SChris Mason { 17344a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1735f36f3042SChris Mason int ret = 0; 17364a9d8bdeSMiao Xie 1737a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17384a9d8bdeSMiao Xie trans = info->running_transaction; 17394a9d8bdeSMiao Xie if (trans) 17404a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1741a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1742f36f3042SChris Mason return ret; 1743f36f3042SChris Mason } 1744f36f3042SChris Mason 17458929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17468929ecfaSYan, Zheng { 17474a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17488929ecfaSYan, Zheng int ret = 0; 17494a9d8bdeSMiao Xie 1750a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17514a9d8bdeSMiao Xie trans = info->running_transaction; 17524a9d8bdeSMiao Xie if (trans) 17534a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1754a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17558929ecfaSYan, Zheng return ret; 17568929ecfaSYan, Zheng } 17578929ecfaSYan, Zheng 1758bb9c12c9SSage Weil /* 1759bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1760bb9c12c9SSage Weil * transaction joins 1761bb9c12c9SSage Weil */ 1762bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1763bb9c12c9SSage Weil struct btrfs_transaction *trans) 1764bb9c12c9SSage Weil { 17654a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1766501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1767501407aaSJosef Bacik trans->aborted); 1768bb9c12c9SSage Weil } 1769bb9c12c9SSage Weil 1770bb9c12c9SSage Weil /* 1771bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1772bb9c12c9SSage Weil * caller holds ref. 1773bb9c12c9SSage Weil */ 1774bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1775bb9c12c9SSage Weil struct btrfs_transaction *trans) 1776bb9c12c9SSage Weil { 177772d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1778501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1779501407aaSJosef Bacik trans->aborted); 1780bb9c12c9SSage Weil } 1781bb9c12c9SSage Weil 1782bb9c12c9SSage Weil /* 1783bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1784bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1785bb9c12c9SSage Weil */ 1786bb9c12c9SSage Weil struct btrfs_async_commit { 1787bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1788bb9c12c9SSage Weil struct btrfs_root *root; 17897892b5afSMiao Xie struct work_struct work; 1790bb9c12c9SSage Weil }; 1791bb9c12c9SSage Weil 1792bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1793bb9c12c9SSage Weil { 1794bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17957892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1796bb9c12c9SSage Weil 17976fc4e354SSage Weil /* 17986fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17996fc4e354SSage Weil * Tell lockdep about it. 18006fc4e354SSage Weil */ 1801b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1802bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 18036fc4e354SSage Weil 1804e209db7aSSage Weil current->journal_info = ac->newtrans; 1805e209db7aSSage Weil 1806bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1807bb9c12c9SSage Weil kfree(ac); 1808bb9c12c9SSage Weil } 1809bb9c12c9SSage Weil 1810bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1811bb9c12c9SSage Weil struct btrfs_root *root, 1812bb9c12c9SSage Weil int wait_for_unblock) 1813bb9c12c9SSage Weil { 18140b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 1815bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1816bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1817bb9c12c9SSage Weil 1818bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1819db5b493aSTsutomu Itoh if (!ac) 1820db5b493aSTsutomu Itoh return -ENOMEM; 1821bb9c12c9SSage Weil 18227892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1823bb9c12c9SSage Weil ac->root = root; 18247a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 18253612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 18263612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 18273612b495STsutomu Itoh kfree(ac); 18283612b495STsutomu Itoh return err; 18293612b495STsutomu Itoh } 1830bb9c12c9SSage Weil 1831bb9c12c9SSage Weil /* take transaction reference */ 1832bb9c12c9SSage Weil cur_trans = trans->transaction; 183313c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1834bb9c12c9SSage Weil 1835bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 18366fc4e354SSage Weil 18376fc4e354SSage Weil /* 18386fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 18396fc4e354SSage Weil * async commit thread will be the one to unlock it. 18406fc4e354SSage Weil */ 1841b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18420b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 18436fc4e354SSage Weil 18447892b5afSMiao Xie schedule_work(&ac->work); 1845bb9c12c9SSage Weil 1846bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1847bb9c12c9SSage Weil if (wait_for_unblock) 1848bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1849bb9c12c9SSage Weil else 1850bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1851bb9c12c9SSage Weil 185238e88054SSage Weil if (current->journal_info == trans) 185338e88054SSage Weil current->journal_info = NULL; 185438e88054SSage Weil 1855724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1856bb9c12c9SSage Weil return 0; 1857bb9c12c9SSage Weil } 1858bb9c12c9SSage Weil 185949b25e05SJeff Mahoney 186049b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 18617b8b92afSJosef Bacik struct btrfs_root *root, int err) 186249b25e05SJeff Mahoney { 18630b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 186449b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1865f094ac32SLiu Bo DEFINE_WAIT(wait); 186649b25e05SJeff Mahoney 186749b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 186849b25e05SJeff Mahoney 186966642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18707b8b92afSJosef Bacik 18710b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 187266b6135bSLiu Bo 187325d8c284SMiao Xie /* 187425d8c284SMiao Xie * If the transaction is removed from the list, it means this 187525d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 187625d8c284SMiao Xie * to call the cleanup function. 187725d8c284SMiao Xie */ 187825d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 187966b6135bSLiu Bo 188049b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18810b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18824a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18830b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1884f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1885f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1886f094ac32SLiu Bo 18870b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1888d7096fc3SJosef Bacik } 18890b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 189049b25e05SJeff Mahoney 189149b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 189249b25e05SJeff Mahoney 18930b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18940b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18950b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18960b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18974a9d8bdeSMiao Xie 1898e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18990b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1900724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1901724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 190249b25e05SJeff Mahoney 190349b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 190449b25e05SJeff Mahoney 190549b25e05SJeff Mahoney if (current->journal_info == trans) 190649b25e05SJeff Mahoney current->journal_info = NULL; 19070b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 190849b25e05SJeff Mahoney 190949b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 191049b25e05SJeff Mahoney } 191149b25e05SJeff Mahoney 191282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 191382436617SMiao Xie { 19143cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 19156c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 191682436617SMiao Xie return 0; 191782436617SMiao Xie } 191882436617SMiao Xie 191982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 192082436617SMiao Xie { 19213cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 1922578def7cSFilipe Manana btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); 192382436617SMiao Xie } 192482436617SMiao Xie 192550d9aa99SJosef Bacik static inline void 1926161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 192750d9aa99SJosef Bacik { 1928161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1929161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 193050d9aa99SJosef Bacik } 193150d9aa99SJosef Bacik 193279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 193379154b1bSChris Mason struct btrfs_root *root) 193479154b1bSChris Mason { 19350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 193649b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19378fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 193825287e0aSMiao Xie int ret; 193979154b1bSChris Mason 19408d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 19418d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 194225287e0aSMiao Xie ret = cur_trans->aborted; 1943e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1944e4a2bcacSJosef Bacik return ret; 194525287e0aSMiao Xie } 194649b25e05SJeff Mahoney 194756bec294SChris Mason /* make a pass through all the delayed refs we have so far 194856bec294SChris Mason * any runnings procs may add more while we are here 194956bec294SChris Mason */ 195056bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1951e4a2bcacSJosef Bacik if (ret) { 1952e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1953e4a2bcacSJosef Bacik return ret; 1954e4a2bcacSJosef Bacik } 195556bec294SChris Mason 19560e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 19570e721106SJosef Bacik trans->block_rsv = NULL; 19580e721106SJosef Bacik 1959b7ec40d7SChris Mason cur_trans = trans->transaction; 196049b25e05SJeff Mahoney 196156bec294SChris Mason /* 196256bec294SChris Mason * set the flushing flag so procs in this transaction have to 196356bec294SChris Mason * start sending their work down. 196456bec294SChris Mason */ 1965b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19661be41b78SJosef Bacik smp_wmb(); 196756bec294SChris Mason 1968ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1969ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1970ea658badSJosef Bacik 1971c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1972e4a2bcacSJosef Bacik if (ret) { 1973e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1974e4a2bcacSJosef Bacik return ret; 1975e4a2bcacSJosef Bacik } 197656bec294SChris Mason 19773204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19781bbc621eSChris Mason int run_it = 0; 19791bbc621eSChris Mason 19801bbc621eSChris Mason /* this mutex is also taken before trying to set 19811bbc621eSChris Mason * block groups readonly. We need to make sure 19821bbc621eSChris Mason * that nobody has set a block group readonly 19831bbc621eSChris Mason * after a extents from that block group have been 19841bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19851bbc621eSChris Mason * will wait for the transaction to commit if it 19863204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19871bbc621eSChris Mason * 19883204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19893204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19901bbc621eSChris Mason * hurt to have more than one go through, but there's no 19911bbc621eSChris Mason * real advantage to it either. 19921bbc621eSChris Mason */ 19930b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 19943204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 19953204d33cSJosef Bacik &cur_trans->flags)) 19961bbc621eSChris Mason run_it = 1; 19970b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 19981bbc621eSChris Mason 19991bbc621eSChris Mason if (run_it) 20001bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 20011bbc621eSChris Mason } 20021bbc621eSChris Mason if (ret) { 20031bbc621eSChris Mason btrfs_end_transaction(trans, root); 20041bbc621eSChris Mason return ret; 20051bbc621eSChris Mason } 20061bbc621eSChris Mason 20070b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20084a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20090b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 201013c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 201149b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 2012ccd467d6SChris Mason 2013b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 201415ee9bc7SJosef Bacik 2015b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2016b4924a0fSLiu Bo ret = cur_trans->aborted; 2017b4924a0fSLiu Bo 2018724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 201915ee9bc7SJosef Bacik 202049b25e05SJeff Mahoney return ret; 202179154b1bSChris Mason } 20224313b399SChris Mason 20234a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20240b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2025bb9c12c9SSage Weil 20260b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2027ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2028ccd467d6SChris Mason struct btrfs_transaction, list); 20294a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 203013c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 20310b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2032ccd467d6SChris Mason 2033ccd467d6SChris Mason wait_for_commit(root, prev_trans); 20341f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2035ccd467d6SChris Mason 2036724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20371f9b8c8fSFilipe Manana if (ret) 20381f9b8c8fSFilipe Manana goto cleanup_transaction; 2039a4abeea4SJosef Bacik } else { 20400b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2041ccd467d6SChris Mason } 2042a4abeea4SJosef Bacik } else { 20430b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2044ccd467d6SChris Mason } 204515ee9bc7SJosef Bacik 20460860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20470860adfdSMiao Xie 20480b246afaSJeff Mahoney ret = btrfs_start_delalloc_flush(fs_info); 204982436617SMiao Xie if (ret) 205082436617SMiao Xie goto cleanup_transaction; 205182436617SMiao Xie 20528d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 205349b25e05SJeff Mahoney if (ret) 205449b25e05SJeff Mahoney goto cleanup_transaction; 205516cdcec7SMiao Xie 2056581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2057581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2058ed3b3d31SChris Mason 2059581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 20608d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 2061ca469637SMiao Xie if (ret) 2062ca469637SMiao Xie goto cleanup_transaction; 2063ca469637SMiao Xie 20640b246afaSJeff Mahoney btrfs_wait_delalloc_flush(fs_info); 2065cb7ab021SWang Shilong 2066161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 206750d9aa99SJosef Bacik 2068cb7ab021SWang Shilong btrfs_scrub_pause(root); 2069ed0ca140SJosef Bacik /* 2070ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2071ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20724a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2073ed0ca140SJosef Bacik */ 20740b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20754a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20760b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2077ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2078ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 207915ee9bc7SJosef Bacik 20802cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 20812cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20822cba30f1SMiao Xie ret = cur_trans->aborted; 20836cf7f77eSWang Shilong goto scrub_continue; 20842cba30f1SMiao Xie } 20857585717fSChris Mason /* 20867585717fSChris Mason * the reloc mutex makes sure that we stop 20877585717fSChris Mason * the balancing code from coming in and moving 20887585717fSChris Mason * extents around in the middle of the commit 20897585717fSChris Mason */ 20900b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 20917585717fSChris Mason 209242874b3dSMiao Xie /* 209342874b3dSMiao Xie * We needn't worry about the delayed items because we will 209442874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 209542874b3dSMiao Xie * core function of the snapshot creation. 209642874b3dSMiao Xie */ 20970b246afaSJeff Mahoney ret = create_pending_snapshots(trans, fs_info); 209849b25e05SJeff Mahoney if (ret) { 20990b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21006cf7f77eSWang Shilong goto scrub_continue; 210149b25e05SJeff Mahoney } 21023063d29fSChris Mason 210342874b3dSMiao Xie /* 210442874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 210542874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 210642874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 210742874b3dSMiao Xie * them. 210842874b3dSMiao Xie * 210942874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 211042874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 211142874b3dSMiao Xie * the nodes and leaves. 211242874b3dSMiao Xie */ 211342874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 211449b25e05SJeff Mahoney if (ret) { 21150b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21166cf7f77eSWang Shilong goto scrub_continue; 211749b25e05SJeff Mahoney } 211816cdcec7SMiao Xie 211956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 212049b25e05SJeff Mahoney if (ret) { 21210b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21226cf7f77eSWang Shilong goto scrub_continue; 212349b25e05SJeff Mahoney } 212456bec294SChris Mason 21250ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 21260b246afaSJeff Mahoney ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 21270ed4792aSQu Wenruo if (ret) { 21280b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21290ed4792aSQu Wenruo goto scrub_continue; 21300ed4792aSQu Wenruo } 21310ed4792aSQu Wenruo 2132e999376fSChris Mason /* 2133e999376fSChris Mason * make sure none of the code above managed to slip in a 2134e999376fSChris Mason * delayed item 2135e999376fSChris Mason */ 2136*ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2137e999376fSChris Mason 21382c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2139dc17ff8fSChris Mason 2140e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2141e02119d5SChris Mason * various roots consistent with each other. Every pointer 2142e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2143e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2144e02119d5SChris Mason * the tree logging code from jumping in and changing any 2145e02119d5SChris Mason * of the trees. 2146e02119d5SChris Mason * 2147e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2148e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2149e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2150e02119d5SChris Mason * from now until after the super is written, we avoid races 2151e02119d5SChris Mason * with the tree-log code. 2152e02119d5SChris Mason */ 21530b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21541a40e23bSZheng Yan 21550b246afaSJeff Mahoney ret = commit_fs_roots(trans, fs_info); 215649b25e05SJeff Mahoney if (ret) { 21570b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21580b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21596cf7f77eSWang Shilong goto scrub_continue; 216049b25e05SJeff Mahoney } 216154aa1f4dSChris Mason 21623818aea2SQu Wenruo /* 21637e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21647e1876acSDavid Sterba * before the next transaction. 21653818aea2SQu Wenruo */ 21660b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21673818aea2SQu Wenruo 21685d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2169e02119d5SChris Mason * safe to free the root of tree log roots 2170e02119d5SChris Mason */ 21710b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2172e02119d5SChris Mason 21730ed4792aSQu Wenruo /* 21740ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21750ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21760ed4792aSQu Wenruo */ 21770b246afaSJeff Mahoney ret = btrfs_qgroup_account_extents(trans, fs_info); 21780ed4792aSQu Wenruo if (ret < 0) { 21790b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21800b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21810ed4792aSQu Wenruo goto scrub_continue; 21820ed4792aSQu Wenruo } 21830ed4792aSQu Wenruo 21845d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 218549b25e05SJeff Mahoney if (ret) { 21860b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21870b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21886cf7f77eSWang Shilong goto scrub_continue; 218949b25e05SJeff Mahoney } 219054aa1f4dSChris Mason 21912cba30f1SMiao Xie /* 21922cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 21932cba30f1SMiao Xie * update ->aborted, check it. 21942cba30f1SMiao Xie */ 21952cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 21962cba30f1SMiao Xie ret = cur_trans->aborted; 21970b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21980b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21996cf7f77eSWang Shilong goto scrub_continue; 22002cba30f1SMiao Xie } 22012cba30f1SMiao Xie 220211833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 220311833d66SYan Zheng 22040b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22055f39d397SChris Mason 22060b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22070b246afaSJeff Mahoney fs_info->tree_root->node); 22080b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22099e351cc8SJosef Bacik &cur_trans->switch_commits); 22105d4f98a2SYan Zheng 22110b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22120b246afaSJeff Mahoney fs_info->chunk_root->node); 22130b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22149e351cc8SJosef Bacik &cur_trans->switch_commits); 22159e351cc8SJosef Bacik 22160b246afaSJeff Mahoney switch_commit_roots(cur_trans, fs_info); 22175d4f98a2SYan Zheng 2218edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2219ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22201bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22215d4f98a2SYan Zheng update_super_roots(root); 2222e02119d5SChris Mason 22230b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22240b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22250b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22260b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2227ccd467d6SChris Mason 22280b246afaSJeff Mahoney btrfs_update_commit_device_size(fs_info); 2229ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2230935e5cc9SMiao Xie 22310b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22320b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2233656f30dbSFilipe Manana 22344fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22354fbcdf66SFilipe Manana 22360b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22374a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22380b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22390b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22400b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2241b7ec40d7SChris Mason 22420b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2243e6dcd2dcSChris Mason 224479154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 224549b25e05SJeff Mahoney if (ret) { 22460b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 224708748810SDavid Sterba "Error while writing out transaction"); 22480b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22496cf7f77eSWang Shilong goto scrub_continue; 225049b25e05SJeff Mahoney } 225149b25e05SJeff Mahoney 225249b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 225349b25e05SJeff Mahoney if (ret) { 22540b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22556cf7f77eSWang Shilong goto scrub_continue; 225649b25e05SJeff Mahoney } 22574313b399SChris Mason 2258e02119d5SChris Mason /* 2259e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2260e02119d5SChris Mason * to go about their business 2261e02119d5SChris Mason */ 22620b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2263e02119d5SChris Mason 226411833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 22654313b399SChris Mason 22663204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22670b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 226813212b54SZhao Lei 22690b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22704a9d8bdeSMiao Xie /* 22714a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22724a9d8bdeSMiao Xie * which can change it. 22734a9d8bdeSMiao Xie */ 22744a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22752c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 22763de4586cSChris Mason 22770b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 227813c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2280a4abeea4SJosef Bacik 2281724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2282724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 228358176a96SJosef Bacik 22840860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 22850b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2286b2b5ef5cSJan Kara 22871abe9b8aSliubo trace_btrfs_transaction_commit(root); 22881abe9b8aSliubo 2289a2de733cSArne Jansen btrfs_scrub_continue(root); 2290a2de733cSArne Jansen 22919ed74f2dSJosef Bacik if (current->journal_info == trans) 22929ed74f2dSJosef Bacik current->journal_info = NULL; 22939ed74f2dSJosef Bacik 22942c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 229524bbcf04SYan, Zheng 22969e7cc91aSWang Xiaoguang /* 22979e7cc91aSWang Xiaoguang * If fs has been frozen, we can not handle delayed iputs, otherwise 22989e7cc91aSWang Xiaoguang * it'll result in deadlock about SB_FREEZE_FS. 22999e7cc91aSWang Xiaoguang */ 23000b246afaSJeff Mahoney if (current != fs_info->transaction_kthread && 23010b246afaSJeff Mahoney current != fs_info->cleaner_kthread && !fs_info->fs_frozen) 230224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 230324bbcf04SYan, Zheng 230479154b1bSChris Mason return ret; 230549b25e05SJeff Mahoney 23066cf7f77eSWang Shilong scrub_continue: 23076cf7f77eSWang Shilong btrfs_scrub_continue(root); 230849b25e05SJeff Mahoney cleanup_transaction: 23090e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 23104fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 23110e721106SJosef Bacik trans->block_rsv = NULL; 23120b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 231349b25e05SJeff Mahoney if (current->journal_info == trans) 231449b25e05SJeff Mahoney current->journal_info = NULL; 23157b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 231649b25e05SJeff Mahoney 231749b25e05SJeff Mahoney return ret; 231879154b1bSChris Mason } 231979154b1bSChris Mason 2320d352ac68SChris Mason /* 23219d1a2a3aSDavid Sterba * return < 0 if error 23229d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23239d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23249d1a2a3aSDavid Sterba * 23259d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23269d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23279d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23289d1a2a3aSDavid Sterba * few seconds later. 2329d352ac68SChris Mason */ 23309d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2331e9d0b13bSChris Mason { 23329d1a2a3aSDavid Sterba int ret; 23335d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2334e9d0b13bSChris Mason 2335a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23369d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23379d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23389d1a2a3aSDavid Sterba return 0; 23399d1a2a3aSDavid Sterba } 23409d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23419d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2342cfad392bSJosef Bacik list_del_init(&root->root_list); 2343a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23445d4f98a2SYan Zheng 2345ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); 234676dda93cSYan, Zheng 234716cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 234816cdcec7SMiao Xie 234976dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 235076dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23512c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 235276dda93cSYan, Zheng else 23532c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 235432471dc2SDavid Sterba 23556596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2356e9d0b13bSChris Mason } 2357572d9ab7SDavid Sterba 2358572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2359572d9ab7SDavid Sterba { 2360572d9ab7SDavid Sterba unsigned long prev; 2361572d9ab7SDavid Sterba unsigned long bit; 2362572d9ab7SDavid Sterba 23636c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2364572d9ab7SDavid Sterba if (!prev) 2365572d9ab7SDavid Sterba return; 2366572d9ab7SDavid Sterba 23677e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23687e1876acSDavid Sterba if (prev & bit) 23697e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23707e1876acSDavid Sterba prev &= ~bit; 23717e1876acSDavid Sterba 23727e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23737e1876acSDavid Sterba if (prev & bit) 23747e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23757e1876acSDavid Sterba prev &= ~bit; 23767e1876acSDavid Sterba 2377d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2378d51033d0SDavid Sterba if (prev & bit) 2379d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2380d51033d0SDavid Sterba prev &= ~bit; 2381d51033d0SDavid Sterba 2382572d9ab7SDavid Sterba if (prev) 2383572d9ab7SDavid Sterba btrfs_warn(fs_info, 2384572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2385572d9ab7SDavid Sterba } 2386