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 { 639b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 649b64f57dSElena Reshetova if (refcount_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 } 964b5faeacSDavid Sterba kfree(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 */ 1872ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1882ff7e61eSJeff Mahoney unsigned int type) 18979154b1bSChris Mason { 19079154b1bSChris Mason struct btrfs_transaction *cur_trans; 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 } 2109b64f57dSElena Reshetova refcount_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 2314b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), 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 */ 2414b5faeacSDavid Sterba kfree(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); 2454b5faeacSDavid Sterba kfree(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 */ 2609b64f57dSElena Reshetova refcount_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, 297c6100a4bSJosef Bacik fs_info->btree_inode); 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 */ 4282ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 42979154b1bSChris Mason { 430f9295749SChris Mason struct btrfs_transaction *cur_trans; 43179154b1bSChris Mason 4320b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4330b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 4344a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 4359b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 4360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 43772d63ed6SLi Zefan 4380b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 439501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 440501407aaSJosef Bacik cur_trans->aborted); 441724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 442a4abeea4SJosef Bacik } else { 4430b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 444f9295749SChris Mason } 44537d1aeeeSChris Mason } 44637d1aeeeSChris Mason 4472ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 44837d1aeeeSChris Mason { 4490b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 450a4abeea4SJosef Bacik return 0; 451a4abeea4SJosef Bacik 452a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 453a22285a6SYan, Zheng return 1; 454a4abeea4SJosef Bacik 455a4abeea4SJosef Bacik if (type == TRANS_START && 4560b246afaSJeff Mahoney !atomic_read(&fs_info->open_ioctl_trans)) 457a4abeea4SJosef Bacik return 1; 458a4abeea4SJosef Bacik 459a22285a6SYan, Zheng return 0; 460a22285a6SYan, Zheng } 461a22285a6SYan, Zheng 46220dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 46320dd2cbfSMiao Xie { 4640b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4650b246afaSJeff Mahoney 4660b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 46727cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 46820dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 46920dd2cbfSMiao Xie root->reloc_root) 47020dd2cbfSMiao Xie return false; 47120dd2cbfSMiao Xie 47220dd2cbfSMiao Xie return true; 47320dd2cbfSMiao Xie } 47420dd2cbfSMiao Xie 47508e007d2SMiao Xie static struct btrfs_trans_handle * 4765aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 477003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 478003d7c59SJeff Mahoney bool enforce_qgroups) 479a22285a6SYan, Zheng { 4800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4810b246afaSJeff Mahoney 482a22285a6SYan, Zheng struct btrfs_trans_handle *h; 483a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 484b5009945SJosef Bacik u64 num_bytes = 0; 485c5567237SArne Jansen u64 qgroup_reserved = 0; 48620dd2cbfSMiao Xie bool reloc_reserved = false; 48720dd2cbfSMiao Xie int ret; 488acce952bSliubo 48946c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4902755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 49146c4e71eSFilipe Manana 4920b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 493acce952bSliubo return ERR_PTR(-EROFS); 4942a1eb461SJosef Bacik 49546c4e71eSFilipe Manana if (current->journal_info) { 4960860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4972a1eb461SJosef Bacik h = current->journal_info; 4982a1eb461SJosef Bacik h->use_count++; 499b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 5002a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 5012a1eb461SJosef Bacik h->block_rsv = NULL; 5022a1eb461SJosef Bacik goto got_it; 5032a1eb461SJosef Bacik } 504b5009945SJosef Bacik 505b5009945SJosef Bacik /* 506b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 507b5009945SJosef Bacik * the appropriate flushing if need be. 508b5009945SJosef Bacik */ 509003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 5100b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 511003d7c59SJeff Mahoney ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved, 512003d7c59SJeff Mahoney enforce_qgroups); 513c5567237SArne Jansen if (ret) 514c5567237SArne Jansen return ERR_PTR(ret); 515c5567237SArne Jansen 5160b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 51720dd2cbfSMiao Xie /* 51820dd2cbfSMiao Xie * Do the reservation for the relocation root creation 51920dd2cbfSMiao Xie */ 520ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 5210b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 52220dd2cbfSMiao Xie reloc_reserved = true; 52320dd2cbfSMiao Xie } 52420dd2cbfSMiao Xie 5250b246afaSJeff Mahoney ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, 52608e007d2SMiao Xie num_bytes, flush); 527b5009945SJosef Bacik if (ret) 528843fcf35SMiao Xie goto reserve_fail; 529b5009945SJosef Bacik } 530a22285a6SYan, Zheng again: 531f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 532843fcf35SMiao Xie if (!h) { 533843fcf35SMiao Xie ret = -ENOMEM; 534843fcf35SMiao Xie goto alloc_fail; 535843fcf35SMiao Xie } 536a22285a6SYan, Zheng 53798114659SJosef Bacik /* 53898114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 53998114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 54098114659SJosef Bacik * because we're already holding a ref. We need this because we could 54198114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 54298114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 543354aa0fbSMiao Xie * 544354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 545354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 54698114659SJosef Bacik */ 5470860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5480b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 549b2b5ef5cSJan Kara 5502ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5512ff7e61eSJeff Mahoney wait_current_trans(fs_info); 552a22285a6SYan, Zheng 553a4abeea4SJosef Bacik do { 5542ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 555178260b2SMiao Xie if (ret == -EBUSY) { 5562ff7e61eSJeff Mahoney wait_current_trans(fs_info); 557178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 558178260b2SMiao Xie ret = -ENOENT; 559178260b2SMiao Xie } 560a4abeea4SJosef Bacik } while (ret == -EBUSY); 561a4abeea4SJosef Bacik 562a43f7f82SLiu Bo if (ret < 0) 563843fcf35SMiao Xie goto join_fail; 5640f7d52f4SChris Mason 5650b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 566a22285a6SYan, Zheng 567a22285a6SYan, Zheng h->transid = cur_trans->transid; 568a22285a6SYan, Zheng h->transaction = cur_trans; 569d13603efSArne Jansen h->root = root; 5702a1eb461SJosef Bacik h->use_count = 1; 57164b63580SJeff Mahoney h->fs_info = root->fs_info; 5727174109cSQu Wenruo 573a698d075SMiao Xie h->type = type; 574d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 575ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 576b7ec40d7SChris Mason 577a22285a6SYan, Zheng smp_mb(); 5784a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5792ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 580abdd2e80SFilipe Manana current->journal_info = h; 5813a45bb20SJeff Mahoney btrfs_commit_transaction(h); 582a22285a6SYan, Zheng goto again; 583a22285a6SYan, Zheng } 5849ed74f2dSJosef Bacik 585b5009945SJosef Bacik if (num_bytes) { 5860b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5872bcc0328SLiu Bo h->transid, num_bytes, 1); 5880b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 589b5009945SJosef Bacik h->bytes_reserved = num_bytes; 59020dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 591a22285a6SYan, Zheng } 592a22285a6SYan, Zheng 5932a1eb461SJosef Bacik got_it: 594a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 595a22285a6SYan, Zheng 596a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 597a22285a6SYan, Zheng current->journal_info = h; 59879154b1bSChris Mason return h; 599843fcf35SMiao Xie 600843fcf35SMiao Xie join_fail: 6010860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 6020b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 603843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 604843fcf35SMiao Xie alloc_fail: 605843fcf35SMiao Xie if (num_bytes) 6062ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 607843fcf35SMiao Xie num_bytes); 608843fcf35SMiao Xie reserve_fail: 6097174109cSQu Wenruo btrfs_qgroup_free_meta(root, qgroup_reserved); 610843fcf35SMiao Xie return ERR_PTR(ret); 61179154b1bSChris Mason } 61279154b1bSChris Mason 613f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6145aed1dd8SAlexandru Moise unsigned int num_items) 615f9295749SChris Mason { 61608e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 617003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 618f9295749SChris Mason } 619003d7c59SJeff Mahoney 6208eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6218eab77ffSFilipe Manana struct btrfs_root *root, 6228eab77ffSFilipe Manana unsigned int num_items, 6238eab77ffSFilipe Manana int min_factor) 6248eab77ffSFilipe Manana { 6250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6268eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6278eab77ffSFilipe Manana u64 num_bytes; 6288eab77ffSFilipe Manana int ret; 6298eab77ffSFilipe Manana 630003d7c59SJeff Mahoney /* 631003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 632003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 633003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 634003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 635003d7c59SJeff Mahoney */ 636003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 637003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6388eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6398eab77ffSFilipe Manana return trans; 6408eab77ffSFilipe Manana 6418eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6428eab77ffSFilipe Manana if (IS_ERR(trans)) 6438eab77ffSFilipe Manana return trans; 6448eab77ffSFilipe Manana 6450b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6460b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6470b246afaSJeff Mahoney num_bytes, min_factor); 6488eab77ffSFilipe Manana if (ret) { 6493a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6508eab77ffSFilipe Manana return ERR_PTR(ret); 6518eab77ffSFilipe Manana } 6528eab77ffSFilipe Manana 6530b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6548eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6550b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 65688d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6578eab77ffSFilipe Manana 6588eab77ffSFilipe Manana return trans; 6598eab77ffSFilipe Manana } 6608407aa46SMiao Xie 66108e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6625aed1dd8SAlexandru Moise struct btrfs_root *root, 6635aed1dd8SAlexandru Moise unsigned int num_items) 6648407aa46SMiao Xie { 66508e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 666003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_LIMIT, true); 6678407aa46SMiao Xie } 6688407aa46SMiao Xie 6697a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 670f9295749SChris Mason { 671003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 672003d7c59SJeff Mahoney true); 673f9295749SChris Mason } 674f9295749SChris Mason 6757a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6760af3d00bSJosef Bacik { 677575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 678003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6790af3d00bSJosef Bacik } 6800af3d00bSJosef Bacik 6817a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6829ca9ee09SSage Weil { 683575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_USERSPACE, 684003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6859ca9ee09SSage Weil } 6869ca9ee09SSage Weil 687d4edf39bSMiao Xie /* 688d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 689d4edf39bSMiao Xie * 690d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 691d4edf39bSMiao Xie * don't want to start a new one. 692d4edf39bSMiao Xie * 693d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 694d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 695d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 696d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 697d4edf39bSMiao Xie * invoke 698d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 699d4edf39bSMiao Xie */ 700354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 70160376ce4SJosef Bacik { 702575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 703003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 70460376ce4SJosef Bacik } 70560376ce4SJosef Bacik 706d4edf39bSMiao Xie /* 70790b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 708d4edf39bSMiao Xie * 709d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 710d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 711d4edf39bSMiao Xie * complete. 712d4edf39bSMiao Xie */ 713d4edf39bSMiao Xie struct btrfs_trans_handle * 714d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 715d4edf39bSMiao Xie { 716d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 717d4edf39bSMiao Xie 718575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 719003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 720d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 7212ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 722d4edf39bSMiao Xie 723d4edf39bSMiao Xie return trans; 724d4edf39bSMiao Xie } 725d4edf39bSMiao Xie 726d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 7272ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 72889ce8a63SChris Mason { 7294a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 73089ce8a63SChris Mason } 73189ce8a63SChris Mason 7322ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 73346204592SSage Weil { 73446204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7358cd2807fSMiao Xie int ret = 0; 73646204592SSage Weil 73746204592SSage Weil if (transid) { 7380b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 739a4abeea4SJosef Bacik goto out; 74046204592SSage Weil 74146204592SSage Weil /* find specified transaction */ 7420b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7430b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 74446204592SSage Weil if (t->transid == transid) { 74546204592SSage Weil cur_trans = t; 7469b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7478cd2807fSMiao Xie ret = 0; 74846204592SSage Weil break; 74946204592SSage Weil } 7508cd2807fSMiao Xie if (t->transid > transid) { 7518cd2807fSMiao Xie ret = 0; 75246204592SSage Weil break; 75346204592SSage Weil } 7548cd2807fSMiao Xie } 7550b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 75642383020SSage Weil 75742383020SSage Weil /* 75842383020SSage Weil * The specified transaction doesn't exist, or we 75942383020SSage Weil * raced with btrfs_commit_transaction 76042383020SSage Weil */ 76142383020SSage Weil if (!cur_trans) { 7620b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 76342383020SSage Weil ret = -EINVAL; 7648cd2807fSMiao Xie goto out; 76542383020SSage Weil } 76646204592SSage Weil } else { 76746204592SSage Weil /* find newest transaction that is committing | committed */ 7680b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7690b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 77046204592SSage Weil list) { 7714a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7724a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7733473f3c0SJosef Bacik break; 77446204592SSage Weil cur_trans = t; 7759b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 77646204592SSage Weil break; 77746204592SSage Weil } 77846204592SSage Weil } 7790b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 78046204592SSage Weil if (!cur_trans) 781a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 78246204592SSage Weil } 78346204592SSage Weil 7842ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 785724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 786a4abeea4SJosef Bacik out: 78746204592SSage Weil return ret; 78846204592SSage Weil } 78946204592SSage Weil 7902ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 79137d1aeeeSChris Mason { 7920b246afaSJeff Mahoney if (!atomic_read(&fs_info->open_ioctl_trans)) 7932ff7e61eSJeff Mahoney wait_current_trans(fs_info); 79437d1aeeeSChris Mason } 79537d1aeeeSChris Mason 7962ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7978929ecfaSYan, Zheng { 7982ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7990b246afaSJeff Mahoney 8000b246afaSJeff Mahoney if (fs_info->global_block_rsv.space_info->full && 8012ff7e61eSJeff Mahoney btrfs_check_space_for_delayed_refs(trans, fs_info)) 8021be41b78SJosef Bacik return 1; 80336ba022aSJosef Bacik 8042ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 8058929ecfaSYan, Zheng } 8068929ecfaSYan, Zheng 8073a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 8088929ecfaSYan, Zheng { 8098929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 8103a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 8118929ecfaSYan, Zheng int updates; 81249b25e05SJeff Mahoney int err; 8138929ecfaSYan, Zheng 814a4abeea4SJosef Bacik smp_mb(); 8154a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8164a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8178929ecfaSYan, Zheng return 1; 8188929ecfaSYan, Zheng 8198929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 8208929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 82149b25e05SJeff Mahoney if (updates) { 8222ff7e61eSJeff Mahoney err = btrfs_run_delayed_refs(trans, fs_info, updates * 2); 82349b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 82449b25e05SJeff Mahoney return err; 82549b25e05SJeff Mahoney } 8268929ecfaSYan, Zheng 8272ff7e61eSJeff Mahoney return should_end_transaction(trans); 8288929ecfaSYan, Zheng } 8298929ecfaSYan, Zheng 83089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 8313a45bb20SJeff Mahoney int throttle) 83279154b1bSChris Mason { 8333a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8348929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 83531b9655fSJosef Bacik u64 transid = trans->transid; 8361be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 837a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8384edc2ca3SDave Jones int err = 0; 839a79b7d4bSChris Mason int must_run_delayed_refs = 0; 840d6e4a428SChris Mason 8413bbb24b2SJosef Bacik if (trans->use_count > 1) { 8423bbb24b2SJosef Bacik trans->use_count--; 8432a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8442a1eb461SJosef Bacik return 0; 8452a1eb461SJosef Bacik } 8462a1eb461SJosef Bacik 8472ff7e61eSJeff Mahoney btrfs_trans_release_metadata(trans, info); 8484c13d758SJosef Bacik trans->block_rsv = NULL; 849c5567237SArne Jansen 850ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 8512ff7e61eSJeff Mahoney btrfs_create_pending_block_groups(trans, info); 852ea658badSJosef Bacik 853c3e69d58SChris Mason trans->delayed_ref_updates = 0; 854a79b7d4bSChris Mason if (!trans->sync) { 855a79b7d4bSChris Mason must_run_delayed_refs = 8562ff7e61eSJeff Mahoney btrfs_should_throttle_delayed_refs(trans, info); 8570a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 858a79b7d4bSChris Mason 859a79b7d4bSChris Mason /* 860a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 861a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 862a79b7d4bSChris Mason */ 863a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 864a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 865a79b7d4bSChris Mason must_run_delayed_refs = 2; 86656bec294SChris Mason } 867bb721703SChris Mason 8682ff7e61eSJeff Mahoney btrfs_trans_release_metadata(trans, info); 8690e721106SJosef Bacik trans->block_rsv = NULL; 87056bec294SChris Mason 871ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 8722ff7e61eSJeff Mahoney btrfs_create_pending_block_groups(trans, info); 873ea658badSJosef Bacik 8744fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8754fbcdf66SFilipe Manana 8760b246afaSJeff Mahoney if (lock && !atomic_read(&info->open_ioctl_trans) && 8772ff7e61eSJeff Mahoney should_end_transaction(trans) && 87820c7bcecSSeraphime Kirkovski READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8794a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8804a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8814a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8824a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 883a4abeea4SJosef Bacik } 8848929ecfaSYan, Zheng 88520c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8863bbb24b2SJosef Bacik if (throttle) 8873a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8883bbb24b2SJosef Bacik else 8898929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8908929ecfaSYan, Zheng } 8918929ecfaSYan, Zheng 8920860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8930b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8946df7881aSJosef Bacik 8958929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 89613c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 89713c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8980860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 89989ce8a63SChris Mason 900a83342aaSDavid Sterba /* 901a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 902a83342aaSDavid Sterba */ 90399d16cbcSSage Weil smp_mb(); 90479154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 90579154b1bSChris Mason wake_up(&cur_trans->writer_wait); 906724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 9079ed74f2dSJosef Bacik 9089ed74f2dSJosef Bacik if (current->journal_info == trans) 9099ed74f2dSJosef Bacik current->journal_info = NULL; 910ab78c84dSChris Mason 91124bbcf04SYan, Zheng if (throttle) 9122ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 91324bbcf04SYan, Zheng 91449b25e05SJeff Mahoney if (trans->aborted || 9150b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 9164e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 9174edc2ca3SDave Jones err = -EIO; 9184e121c06SJosef Bacik } 91949b25e05SJeff Mahoney 9204edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 921a79b7d4bSChris Mason if (must_run_delayed_refs) { 9222ff7e61eSJeff Mahoney btrfs_async_run_delayed_refs(info, cur, transid, 923a79b7d4bSChris Mason must_run_delayed_refs == 1); 924a79b7d4bSChris Mason } 9254edc2ca3SDave Jones return err; 92679154b1bSChris Mason } 92779154b1bSChris Mason 9283a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 92989ce8a63SChris Mason { 9303a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 93189ce8a63SChris Mason } 93289ce8a63SChris Mason 9333a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 93489ce8a63SChris Mason { 9353a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 93616cdcec7SMiao Xie } 93716cdcec7SMiao Xie 938d352ac68SChris Mason /* 939d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 940d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 941690587d1SChris Mason * those extents are sent to disk but does not wait on them 942d352ac68SChris Mason */ 9432ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 9448cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 94579154b1bSChris Mason { 946777e6bd7SChris Mason int err = 0; 9477c4452b9SChris Mason int werr = 0; 9480b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 949e6138876SJosef Bacik struct extent_state *cached_state = NULL; 950777e6bd7SChris Mason u64 start = 0; 9515f39d397SChris Mason u64 end; 9527c4452b9SChris Mason 953*6300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 9541728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 955e6138876SJosef Bacik mark, &cached_state)) { 956663dfbb0SFilipe Manana bool wait_writeback = false; 957663dfbb0SFilipe Manana 958663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 959663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 960210aa277SDavid Sterba mark, &cached_state); 961663dfbb0SFilipe Manana /* 962663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 963663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 964663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 965663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 966bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 967bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 968bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 969bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 970bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 971663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 972663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 973663dfbb0SFilipe Manana */ 974663dfbb0SFilipe Manana if (err == -ENOMEM) { 975663dfbb0SFilipe Manana err = 0; 976663dfbb0SFilipe Manana wait_writeback = true; 977663dfbb0SFilipe Manana } 978663dfbb0SFilipe Manana if (!err) 9791728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9807c4452b9SChris Mason if (err) 9817c4452b9SChris Mason werr = err; 982663dfbb0SFilipe Manana else if (wait_writeback) 983663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 984e38e2ed7SFilipe Manana free_extent_state(cached_state); 985663dfbb0SFilipe Manana cached_state = NULL; 9861728366eSJosef Bacik cond_resched(); 9871728366eSJosef Bacik start = end + 1; 9887c4452b9SChris Mason } 989*6300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 990690587d1SChris Mason return werr; 991690587d1SChris Mason } 992690587d1SChris Mason 993690587d1SChris Mason /* 994690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 995690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 996690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 997690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 998690587d1SChris Mason */ 999bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 1000bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 1001690587d1SChris Mason { 1002690587d1SChris Mason int err = 0; 1003690587d1SChris Mason int werr = 0; 10040b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 1005e6138876SJosef Bacik struct extent_state *cached_state = NULL; 1006690587d1SChris Mason u64 start = 0; 1007690587d1SChris Mason u64 end; 1008690587d1SChris Mason 10091728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 1010e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 1011663dfbb0SFilipe Manana /* 1012663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 1013663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 1014663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1015663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1016663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1017663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1018663dfbb0SFilipe Manana */ 1019663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1020663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1021e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1022663dfbb0SFilipe Manana if (err == -ENOMEM) 1023663dfbb0SFilipe Manana err = 0; 1024663dfbb0SFilipe Manana if (!err) 10251728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1026777e6bd7SChris Mason if (err) 1027777e6bd7SChris Mason werr = err; 1028e38e2ed7SFilipe Manana free_extent_state(cached_state); 1029e38e2ed7SFilipe Manana cached_state = NULL; 1030777e6bd7SChris Mason cond_resched(); 10311728366eSJosef Bacik start = end + 1; 1032777e6bd7SChris Mason } 10337c4452b9SChris Mason if (err) 10347c4452b9SChris Mason werr = err; 1035bf89d38fSJeff Mahoney return werr; 1036bf89d38fSJeff Mahoney } 1037656f30dbSFilipe Manana 1038bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 1039bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 1040bf89d38fSJeff Mahoney { 1041bf89d38fSJeff Mahoney bool errors = false; 1042bf89d38fSJeff Mahoney int err; 1043bf89d38fSJeff Mahoney 1044bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1045bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 1046bf89d38fSJeff Mahoney errors = true; 1047bf89d38fSJeff Mahoney 1048bf89d38fSJeff Mahoney if (errors && !err) 1049bf89d38fSJeff Mahoney err = -EIO; 1050bf89d38fSJeff Mahoney return err; 1051bf89d38fSJeff Mahoney } 1052bf89d38fSJeff Mahoney 1053bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 1054bf89d38fSJeff Mahoney { 1055bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 1056bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 1057bf89d38fSJeff Mahoney bool errors = false; 1058bf89d38fSJeff Mahoney int err; 1059bf89d38fSJeff Mahoney 1060bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 1061bf89d38fSJeff Mahoney 1062bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1063656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10640b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1065656f30dbSFilipe Manana errors = true; 1066656f30dbSFilipe Manana 1067656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10680b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1069656f30dbSFilipe Manana errors = true; 1070656f30dbSFilipe Manana 1071bf89d38fSJeff Mahoney if (errors && !err) 1072bf89d38fSJeff Mahoney err = -EIO; 1073bf89d38fSJeff Mahoney return err; 107479154b1bSChris Mason } 107579154b1bSChris Mason 1076690587d1SChris Mason /* 1077690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1078690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1079690587d1SChris Mason * those extents are on disk for transaction or log commit 1080690587d1SChris Mason */ 1081bf89d38fSJeff Mahoney static int btrfs_write_and_wait_marked_extents(struct btrfs_fs_info *fs_info, 10828cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1083690587d1SChris Mason { 1084690587d1SChris Mason int ret; 1085690587d1SChris Mason int ret2; 1086c6adc9ccSMiao Xie struct blk_plug plug; 1087690587d1SChris Mason 1088c6adc9ccSMiao Xie blk_start_plug(&plug); 1089bf89d38fSJeff Mahoney ret = btrfs_write_marked_extents(fs_info, dirty_pages, mark); 1090c6adc9ccSMiao Xie blk_finish_plug(&plug); 1091bf89d38fSJeff Mahoney ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1092bf0da8c1SChris Mason 1093bf0da8c1SChris Mason if (ret) 1094bf0da8c1SChris Mason return ret; 1095bf0da8c1SChris Mason if (ret2) 1096bf0da8c1SChris Mason return ret2; 1097bf0da8c1SChris Mason return 0; 1098690587d1SChris Mason } 1099690587d1SChris Mason 1100663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1101bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info) 1102d0c803c4SChris Mason { 1103663dfbb0SFilipe Manana int ret; 1104663dfbb0SFilipe Manana 1105bf89d38fSJeff Mahoney ret = btrfs_write_and_wait_marked_extents(fs_info, 11068cef4e16SYan, Zheng &trans->transaction->dirty_pages, 11078cef4e16SYan, Zheng EXTENT_DIRTY); 1108663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1109663dfbb0SFilipe Manana 1110663dfbb0SFilipe Manana return ret; 1111d0c803c4SChris Mason } 1112d0c803c4SChris Mason 1113d352ac68SChris Mason /* 1114d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1115d352ac68SChris Mason * 1116d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1117d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1118d352ac68SChris Mason * allocation tree. 1119d352ac68SChris Mason * 1120d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1121d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1122d352ac68SChris Mason */ 11230b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 112479154b1bSChris Mason struct btrfs_root *root) 112579154b1bSChris Mason { 112679154b1bSChris Mason int ret; 11270b86a832SChris Mason u64 old_root_bytenr; 112886b9f2ecSYan, Zheng u64 old_root_used; 11290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11300b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 113179154b1bSChris Mason 113286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 113356bec294SChris Mason 113479154b1bSChris Mason while (1) { 11350b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 113686b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1137ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 113879154b1bSChris Mason break; 113987ef2bb4SChris Mason 11405d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 114179154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11420b86a832SChris Mason &root->root_key, 11430b86a832SChris Mason &root->root_item); 114449b25e05SJeff Mahoney if (ret) 114549b25e05SJeff Mahoney return ret; 114656bec294SChris Mason 114786b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1148e7070be1SJosef Bacik } 1149276e680dSYan Zheng 11500b86a832SChris Mason return 0; 11510b86a832SChris Mason } 11520b86a832SChris Mason 1153d352ac68SChris Mason /* 1154d352ac68SChris Mason * update all the cowonly tree roots on disk 115549b25e05SJeff Mahoney * 115649b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 115749b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 115849b25e05SJeff Mahoney * to clean up the delayed refs. 1159d352ac68SChris Mason */ 11605d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11612ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info) 11620b86a832SChris Mason { 1163ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11641bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11650b86a832SChris Mason struct list_head *next; 116684234f3aSYan Zheng struct extent_buffer *eb; 116756bec294SChris Mason int ret; 116884234f3aSYan Zheng 116984234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 117049b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 117149b25e05SJeff Mahoney 0, &eb); 117284234f3aSYan Zheng btrfs_tree_unlock(eb); 117384234f3aSYan Zheng free_extent_buffer(eb); 11740b86a832SChris Mason 117549b25e05SJeff Mahoney if (ret) 117649b25e05SJeff Mahoney return ret; 117749b25e05SJeff Mahoney 11782ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 117949b25e05SJeff Mahoney if (ret) 118049b25e05SJeff Mahoney return ret; 118187ef2bb4SChris Mason 11820b246afaSJeff Mahoney ret = btrfs_run_dev_stats(trans, fs_info); 1183c16ce190SJosef Bacik if (ret) 1184c16ce190SJosef Bacik return ret; 11850b246afaSJeff Mahoney ret = btrfs_run_dev_replace(trans, fs_info); 1186c16ce190SJosef Bacik if (ret) 1187c16ce190SJosef Bacik return ret; 11880b246afaSJeff Mahoney ret = btrfs_run_qgroups(trans, fs_info); 1189c16ce190SJosef Bacik if (ret) 1190c16ce190SJosef Bacik return ret; 1191546adb0dSJan Schmidt 11922ff7e61eSJeff Mahoney ret = btrfs_setup_space_cache(trans, fs_info); 1193dcdf7f6dSJosef Bacik if (ret) 1194dcdf7f6dSJosef Bacik return ret; 1195dcdf7f6dSJosef Bacik 1196546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 11972ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 1198c16ce190SJosef Bacik if (ret) 1199c16ce190SJosef Bacik return ret; 1200ea526d18SJosef Bacik again: 12010b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 12022ff7e61eSJeff Mahoney struct btrfs_root *root; 12030b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 12040b86a832SChris Mason list_del_init(next); 12050b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1206e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 120787ef2bb4SChris Mason 12089e351cc8SJosef Bacik if (root != fs_info->extent_root) 12099e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12109e351cc8SJosef Bacik &trans->transaction->switch_commits); 121149b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 121249b25e05SJeff Mahoney if (ret) 121349b25e05SJeff Mahoney return ret; 12142ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 1215ea526d18SJosef Bacik if (ret) 1216ea526d18SJosef Bacik return ret; 121779154b1bSChris Mason } 1218276e680dSYan Zheng 12191bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 12202ff7e61eSJeff Mahoney ret = btrfs_write_dirty_block_groups(trans, fs_info); 1221ea526d18SJosef Bacik if (ret) 1222ea526d18SJosef Bacik return ret; 12232ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 1224ea526d18SJosef Bacik if (ret) 1225ea526d18SJosef Bacik return ret; 1226ea526d18SJosef Bacik } 1227ea526d18SJosef Bacik 1228ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1229ea526d18SJosef Bacik goto again; 1230ea526d18SJosef Bacik 12319e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 12329e351cc8SJosef Bacik &trans->transaction->switch_commits); 12338dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12348dabb742SStefan Behrens 123579154b1bSChris Mason return 0; 123679154b1bSChris Mason } 123779154b1bSChris Mason 1238d352ac68SChris Mason /* 1239d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1240d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1241d352ac68SChris Mason * be deleted 1242d352ac68SChris Mason */ 1243cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12445eda7b5eSChris Mason { 12450b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 12460b246afaSJeff Mahoney 12470b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1248cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12490b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 12500b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 12515eda7b5eSChris Mason } 12525eda7b5eSChris Mason 1253d352ac68SChris Mason /* 12545d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1255d352ac68SChris Mason */ 12565d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12575b4aacefSJeff Mahoney struct btrfs_fs_info *fs_info) 12580f7d52f4SChris Mason { 12590f7d52f4SChris Mason struct btrfs_root *gang[8]; 12600f7d52f4SChris Mason int i; 12610f7d52f4SChris Mason int ret; 126254aa1f4dSChris Mason int err = 0; 126354aa1f4dSChris Mason 1264a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12650f7d52f4SChris Mason while (1) { 12665d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12675d4f98a2SYan Zheng (void **)gang, 0, 12680f7d52f4SChris Mason ARRAY_SIZE(gang), 12690f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12700f7d52f4SChris Mason if (ret == 0) 12710f7d52f4SChris Mason break; 12720f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12735b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12745d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12752619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12760f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1277a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 127831153d81SYan Zheng 1279e02119d5SChris Mason btrfs_free_log(trans, root); 12805d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1281d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1282e02119d5SChris Mason 128382d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 128482d5902dSLi Zefan 1285f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 128627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1287c7548af6SChris Mason smp_mb__after_atomic(); 1288f1ebcc74SLiu Bo 1289978d910dSYan Zheng if (root->commit_root != root->node) { 12909e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12919e351cc8SJosef Bacik &trans->transaction->switch_commits); 1292978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1293978d910dSYan Zheng root->node); 1294978d910dSYan Zheng } 129531153d81SYan Zheng 12965d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12970f7d52f4SChris Mason &root->root_key, 12980f7d52f4SChris Mason &root->root_item); 1299a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 130054aa1f4dSChris Mason if (err) 130154aa1f4dSChris Mason break; 13027174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 13030f7d52f4SChris Mason } 13049f3a7427SChris Mason } 1305a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 130654aa1f4dSChris Mason return err; 13070f7d52f4SChris Mason } 13080f7d52f4SChris Mason 1309d352ac68SChris Mason /* 1310de78b51aSEric Sandeen * defrag a given btree. 1311de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1312d352ac68SChris Mason */ 1313de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1314e9d0b13bSChris Mason { 1315e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1316e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 13178929ecfaSYan, Zheng int ret; 1318e9d0b13bSChris Mason 131927cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1320e9d0b13bSChris Mason return 0; 13218929ecfaSYan, Zheng 13226b80053dSChris Mason while (1) { 13238929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 13248929ecfaSYan, Zheng if (IS_ERR(trans)) 13258929ecfaSYan, Zheng return PTR_ERR(trans); 13268929ecfaSYan, Zheng 1327de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 13288929ecfaSYan, Zheng 13293a45bb20SJeff Mahoney btrfs_end_transaction(trans); 13302ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1331e9d0b13bSChris Mason cond_resched(); 1332e9d0b13bSChris Mason 1333ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1334e9d0b13bSChris Mason break; 1335210549ebSDavid Sterba 1336ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1337ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1338210549ebSDavid Sterba ret = -EAGAIN; 1339210549ebSDavid Sterba break; 1340210549ebSDavid Sterba } 1341e9d0b13bSChris Mason } 134227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13438929ecfaSYan, Zheng return ret; 1344e9d0b13bSChris Mason } 1345e9d0b13bSChris Mason 1346d352ac68SChris Mason /* 13476426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 13486426c7adSQu Wenruo * 13496426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 13506426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13516426c7adSQu Wenruo * qgroup works. 13526426c7adSQu Wenruo */ 13536426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13546426c7adSQu Wenruo struct btrfs_root *src, 13556426c7adSQu Wenruo struct btrfs_root *parent, 13566426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13576426c7adSQu Wenruo u64 dst_objectid) 13586426c7adSQu Wenruo { 13596426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13606426c7adSQu Wenruo int ret; 13616426c7adSQu Wenruo 13626426c7adSQu Wenruo /* 13636426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13646426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13656426c7adSQu Wenruo * kick in anyway. 13666426c7adSQu Wenruo */ 13679ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 13686426c7adSQu Wenruo return 0; 13696426c7adSQu Wenruo 13706426c7adSQu Wenruo /* 13716426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13726426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13736426c7adSQu Wenruo */ 13746426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13756426c7adSQu Wenruo 13765b4aacefSJeff Mahoney ret = commit_fs_roots(trans, fs_info); 13776426c7adSQu Wenruo if (ret) 13786426c7adSQu Wenruo goto out; 13796426c7adSQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 13806426c7adSQu Wenruo if (ret < 0) 13816426c7adSQu Wenruo goto out; 13826426c7adSQu Wenruo 13836426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 13846426c7adSQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 13856426c7adSQu Wenruo src->root_key.objectid, dst_objectid, 13866426c7adSQu Wenruo inherit); 13876426c7adSQu Wenruo if (ret < 0) 13886426c7adSQu Wenruo goto out; 13896426c7adSQu Wenruo 13906426c7adSQu Wenruo /* 13916426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13926426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13936426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13946426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13956426c7adSQu Wenruo * 2) write all btree blocks onto disk 13966426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13976426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13986426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13996426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 14006426c7adSQu Wenruo * And we don't write super to avoid half committed status. 14016426c7adSQu Wenruo */ 14022ff7e61eSJeff Mahoney ret = commit_cowonly_roots(trans, fs_info); 14036426c7adSQu Wenruo if (ret) 14046426c7adSQu Wenruo goto out; 14056426c7adSQu Wenruo switch_commit_roots(trans->transaction, fs_info); 1406bf89d38fSJeff Mahoney ret = btrfs_write_and_wait_transaction(trans, fs_info); 14076426c7adSQu Wenruo if (ret) 1408f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 14096426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 14106426c7adSQu Wenruo 14116426c7adSQu Wenruo out: 14126426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 14136426c7adSQu Wenruo 14146426c7adSQu Wenruo /* 14156426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 14166426c7adSQu Wenruo * last_trans == cur_transid. 14176426c7adSQu Wenruo * Or it won't be committed again onto disk after later 14186426c7adSQu Wenruo * insert_dir_item() 14196426c7adSQu Wenruo */ 14206426c7adSQu Wenruo if (!ret) 14216426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 14226426c7adSQu Wenruo return ret; 14236426c7adSQu Wenruo } 14246426c7adSQu Wenruo 14256426c7adSQu Wenruo /* 1426d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1427aec8030aSMiao Xie * transaction commit. This does the actual creation. 1428aec8030aSMiao Xie * 1429aec8030aSMiao Xie * Note: 1430aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1431aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1432aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1433d352ac68SChris Mason */ 143480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 14353063d29fSChris Mason struct btrfs_fs_info *fs_info, 14363063d29fSChris Mason struct btrfs_pending_snapshot *pending) 14373063d29fSChris Mason { 14383063d29fSChris Mason struct btrfs_key key; 143980b6794dSChris Mason struct btrfs_root_item *new_root_item; 14403063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 14413063d29fSChris Mason struct btrfs_root *root = pending->root; 14426bdb72deSSage Weil struct btrfs_root *parent_root; 144398c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14446bdb72deSSage Weil struct inode *parent_inode; 144542874b3dSMiao Xie struct btrfs_path *path; 144642874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1447a22285a6SYan, Zheng struct dentry *dentry; 14483063d29fSChris Mason struct extent_buffer *tmp; 1449925baeddSChris Mason struct extent_buffer *old; 145004b285f3SDeepa Dinamani struct timespec cur_time; 1451aec8030aSMiao Xie int ret = 0; 1452d68fc57bSYan, Zheng u64 to_reserve = 0; 14536bdb72deSSage Weil u64 index = 0; 1454a22285a6SYan, Zheng u64 objectid; 1455b83cc969SLi Zefan u64 root_flags; 14568ea05e3aSAlexander Block uuid_le new_uuid; 14573063d29fSChris Mason 14588546b570SDavid Sterba ASSERT(pending->path); 14598546b570SDavid Sterba path = pending->path; 146042874b3dSMiao Xie 1461b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1462b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1463a22285a6SYan, Zheng 1464aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1465aec8030aSMiao Xie if (pending->error) 14666fa9700eSMiao Xie goto no_free_objectid; 14673063d29fSChris Mason 1468d6726335SQu Wenruo /* 1469d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1470d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1471d6726335SQu Wenruo */ 1472d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1473d6726335SQu Wenruo 1474147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1475d68fc57bSYan, Zheng 1476d68fc57bSYan, Zheng if (to_reserve > 0) { 1477aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1478aec8030aSMiao Xie &pending->block_rsv, 147908e007d2SMiao Xie to_reserve, 148008e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1481aec8030aSMiao Xie if (pending->error) 1482d6726335SQu Wenruo goto clear_skip_qgroup; 1483d68fc57bSYan, Zheng } 1484d68fc57bSYan, Zheng 14853063d29fSChris Mason key.objectid = objectid; 1486a22285a6SYan, Zheng key.offset = (u64)-1; 1487a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14883063d29fSChris Mason 14896fa9700eSMiao Xie rsv = trans->block_rsv; 1490a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14912382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14920b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 149388d3a5aaSJosef Bacik trans->transid, 149488d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1495a22285a6SYan, Zheng dentry = pending->dentry; 1496e9662f70SMiao Xie parent_inode = pending->dir; 1497a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14986426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1499a22285a6SYan, Zheng 1500c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 150104b285f3SDeepa Dinamani 15026bdb72deSSage Weil /* 15036bdb72deSSage Weil * insert the directory item 15046bdb72deSSage Weil */ 1505877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 150649b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 150742874b3dSMiao Xie 150842874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 150942874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 15104a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 151142874b3dSMiao Xie dentry->d_name.name, 151242874b3dSMiao Xie dentry->d_name.len, 0); 151342874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1514fe66a05aSChris Mason pending->error = -EEXIST; 1515aec8030aSMiao Xie goto dir_item_existed; 151642874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 151742874b3dSMiao Xie ret = PTR_ERR(dir_item); 151866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15198732d44fSMiao Xie goto fail; 152079787eaaSJeff Mahoney } 152142874b3dSMiao Xie btrfs_release_path(path); 15226bdb72deSSage Weil 1523e999376fSChris Mason /* 1524e999376fSChris Mason * pull in the delayed directory update 1525e999376fSChris Mason * and the delayed inode item 1526e999376fSChris Mason * otherwise we corrupt the FS during 1527e999376fSChris Mason * snapshot 1528e999376fSChris Mason */ 15292ff7e61eSJeff Mahoney ret = btrfs_run_delayed_items(trans, fs_info); 15308732d44fSMiao Xie if (ret) { /* Transaction aborted */ 153166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15328732d44fSMiao Xie goto fail; 15338732d44fSMiao Xie } 1534e999376fSChris Mason 15356426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 15366bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 15376bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 153808fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 15396bdb72deSSage Weil 1540b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1541b83cc969SLi Zefan if (pending->readonly) 1542b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1543b83cc969SLi Zefan else 1544b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1545b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1546b83cc969SLi Zefan 15478ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15488ea05e3aSAlexander Block trans->transid); 15498ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15508ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15518ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15528ea05e3aSAlexander Block BTRFS_UUID_SIZE); 155370023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 155470023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 155570023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15568ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15578ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15588ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15598ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 156070023da2SStefan Behrens } 15613cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15623cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 156370023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15648ea05e3aSAlexander Block 1565925baeddSChris Mason old = btrfs_lock_root_node(root); 156649b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 156779787eaaSJeff Mahoney if (ret) { 156879787eaaSJeff Mahoney btrfs_tree_unlock(old); 156979787eaaSJeff Mahoney free_extent_buffer(old); 157066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15718732d44fSMiao Xie goto fail; 157279787eaaSJeff Mahoney } 157349b25e05SJeff Mahoney 15745d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 15753063d29fSChris Mason 157649b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 157779787eaaSJeff Mahoney /* clean up in any case */ 1578925baeddSChris Mason btrfs_tree_unlock(old); 1579925baeddSChris Mason free_extent_buffer(old); 15808732d44fSMiao Xie if (ret) { 158166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15828732d44fSMiao Xie goto fail; 15838732d44fSMiao Xie } 1584f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 158527cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1586f1ebcc74SLiu Bo smp_wmb(); 1587f1ebcc74SLiu Bo 15885d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1589a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1590a22285a6SYan, Zheng key.offset = trans->transid; 1591a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1592925baeddSChris Mason btrfs_tree_unlock(tmp); 15933063d29fSChris Mason free_extent_buffer(tmp); 15948732d44fSMiao Xie if (ret) { 159566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15968732d44fSMiao Xie goto fail; 15978732d44fSMiao Xie } 15980660b5afSChris Mason 1599a22285a6SYan, Zheng /* 1600a22285a6SYan, Zheng * insert root back/forward references 1601a22285a6SYan, Zheng */ 16026bccf3abSJeff Mahoney ret = btrfs_add_root_ref(trans, fs_info, objectid, 1603a22285a6SYan, Zheng parent_root->root_key.objectid, 16044a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1605a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 16068732d44fSMiao Xie if (ret) { 160766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16088732d44fSMiao Xie goto fail; 16098732d44fSMiao Xie } 1610a22285a6SYan, Zheng 1611a22285a6SYan, Zheng key.offset = (u64)-1; 16120b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 161379787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 161479787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 161566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16168732d44fSMiao Xie goto fail; 161779787eaaSJeff Mahoney } 1618d68fc57bSYan, Zheng 161949b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 16208732d44fSMiao Xie if (ret) { 162166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16228732d44fSMiao Xie goto fail; 16238732d44fSMiao Xie } 1624361048f5SMiao Xie 16252ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 16268732d44fSMiao Xie if (ret) { 162766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16288732d44fSMiao Xie goto fail; 16298732d44fSMiao Xie } 163042874b3dSMiao Xie 16316426c7adSQu Wenruo /* 16326426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 16336426c7adSQu Wenruo * snapshot hack to do fast snapshot. 16346426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 16356426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 16366426c7adSQu Wenruo */ 16376426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 16386426c7adSQu Wenruo pending->inherit, objectid); 16396426c7adSQu Wenruo if (ret < 0) 16406426c7adSQu Wenruo goto fail; 16416426c7adSQu Wenruo 164242874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 164342874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 16448e7611cfSNikolay Borisov BTRFS_I(parent_inode), &key, 164542874b3dSMiao Xie BTRFS_FT_DIR, index); 164642874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16479c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16488732d44fSMiao Xie if (ret) { 164966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16508732d44fSMiao Xie goto fail; 16518732d44fSMiao Xie } 165242874b3dSMiao Xie 16536ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 165442874b3dSMiao Xie dentry->d_name.len * 2); 165504b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1656c2050a45SDeepa Dinamani current_time(parent_inode); 1657be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1658dd5f9615SStefan Behrens if (ret) { 165966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1660dd5f9615SStefan Behrens goto fail; 1661dd5f9615SStefan Behrens } 16626bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, new_uuid.b, 1663dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1664dd5f9615SStefan Behrens if (ret) { 166566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1666dd5f9615SStefan Behrens goto fail; 1667dd5f9615SStefan Behrens } 1668dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 16696bccf3abSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, 1670dd5f9615SStefan Behrens new_root_item->received_uuid, 1671dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1672dd5f9615SStefan Behrens objectid); 1673dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 167466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1675dd5f9615SStefan Behrens goto fail; 1676dd5f9615SStefan Behrens } 1677dd5f9615SStefan Behrens } 1678d6726335SQu Wenruo 16792ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 1680d6726335SQu Wenruo if (ret) { 168166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1682d6726335SQu Wenruo goto fail; 1683d6726335SQu Wenruo } 1684d6726335SQu Wenruo 16853063d29fSChris Mason fail: 1686aec8030aSMiao Xie pending->error = ret; 1687aec8030aSMiao Xie dir_item_existed: 168898c9942aSLiu Bo trans->block_rsv = rsv; 16892382c5ccSLiu Bo trans->bytes_reserved = 0; 1690d6726335SQu Wenruo clear_skip_qgroup: 1691d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16926fa9700eSMiao Xie no_free_objectid: 16936fa9700eSMiao Xie kfree(new_root_item); 1694b0c0ea63SDavid Sterba pending->root_item = NULL; 169542874b3dSMiao Xie btrfs_free_path(path); 16968546b570SDavid Sterba pending->path = NULL; 16978546b570SDavid Sterba 169849b25e05SJeff Mahoney return ret; 16993063d29fSChris Mason } 17003063d29fSChris Mason 1701d352ac68SChris Mason /* 1702d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1703d352ac68SChris Mason */ 170480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 17053063d29fSChris Mason struct btrfs_fs_info *fs_info) 17063063d29fSChris Mason { 1707aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 17083063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1709aec8030aSMiao Xie int ret = 0; 17103de4586cSChris Mason 1711aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1712aec8030aSMiao Xie list_del(&pending->list); 1713aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1714aec8030aSMiao Xie if (ret) 1715aec8030aSMiao Xie break; 1716aec8030aSMiao Xie } 1717aec8030aSMiao Xie return ret; 17183de4586cSChris Mason } 17193de4586cSChris Mason 17202ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 17215d4f98a2SYan Zheng { 17225d4f98a2SYan Zheng struct btrfs_root_item *root_item; 17235d4f98a2SYan Zheng struct btrfs_super_block *super; 17245d4f98a2SYan Zheng 17250b246afaSJeff Mahoney super = fs_info->super_copy; 17265d4f98a2SYan Zheng 17270b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 17285d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 17295d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 17305d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 17315d4f98a2SYan Zheng 17320b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 17335d4f98a2SYan Zheng super->root = root_item->bytenr; 17345d4f98a2SYan Zheng super->generation = root_item->generation; 17355d4f98a2SYan Zheng super->root_level = root_item->level; 17360b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 17370af3d00bSJosef Bacik super->cache_generation = root_item->generation; 17380b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 173926432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 17405d4f98a2SYan Zheng } 17415d4f98a2SYan Zheng 1742f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1743f36f3042SChris Mason { 17444a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1745f36f3042SChris Mason int ret = 0; 17464a9d8bdeSMiao Xie 1747a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17484a9d8bdeSMiao Xie trans = info->running_transaction; 17494a9d8bdeSMiao Xie if (trans) 17504a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1751a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1752f36f3042SChris Mason return ret; 1753f36f3042SChris Mason } 1754f36f3042SChris Mason 17558929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17568929ecfaSYan, Zheng { 17574a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17588929ecfaSYan, Zheng int ret = 0; 17594a9d8bdeSMiao Xie 1760a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17614a9d8bdeSMiao Xie trans = info->running_transaction; 17624a9d8bdeSMiao Xie if (trans) 17634a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1764a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17658929ecfaSYan, Zheng return ret; 17668929ecfaSYan, Zheng } 17678929ecfaSYan, Zheng 1768bb9c12c9SSage Weil /* 1769bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1770bb9c12c9SSage Weil * transaction joins 1771bb9c12c9SSage Weil */ 17722ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1773bb9c12c9SSage Weil struct btrfs_transaction *trans) 1774bb9c12c9SSage Weil { 17752ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 17762ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1777bb9c12c9SSage Weil } 1778bb9c12c9SSage Weil 1779bb9c12c9SSage Weil /* 1780bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1781bb9c12c9SSage Weil * caller holds ref. 1782bb9c12c9SSage Weil */ 17832ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17842ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1785bb9c12c9SSage Weil struct btrfs_transaction *trans) 1786bb9c12c9SSage Weil { 17872ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17882ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1789bb9c12c9SSage Weil } 1790bb9c12c9SSage Weil 1791bb9c12c9SSage Weil /* 1792bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1793bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1794bb9c12c9SSage Weil */ 1795bb9c12c9SSage Weil struct btrfs_async_commit { 1796bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17977892b5afSMiao Xie struct work_struct work; 1798bb9c12c9SSage Weil }; 1799bb9c12c9SSage Weil 1800bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1801bb9c12c9SSage Weil { 1802bb9c12c9SSage Weil struct btrfs_async_commit *ac = 18037892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1804bb9c12c9SSage Weil 18056fc4e354SSage Weil /* 18066fc4e354SSage Weil * We've got freeze protection passed with the transaction. 18076fc4e354SSage Weil * Tell lockdep about it. 18086fc4e354SSage Weil */ 1809b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18103a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 18116fc4e354SSage Weil 1812e209db7aSSage Weil current->journal_info = ac->newtrans; 1813e209db7aSSage Weil 18143a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1815bb9c12c9SSage Weil kfree(ac); 1816bb9c12c9SSage Weil } 1817bb9c12c9SSage Weil 1818bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1819bb9c12c9SSage Weil int wait_for_unblock) 1820bb9c12c9SSage Weil { 18213a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1822bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1823bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1824bb9c12c9SSage Weil 1825bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1826db5b493aSTsutomu Itoh if (!ac) 1827db5b493aSTsutomu Itoh return -ENOMEM; 1828bb9c12c9SSage Weil 18297892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 18303a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 18313612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 18323612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 18333612b495STsutomu Itoh kfree(ac); 18343612b495STsutomu Itoh return err; 18353612b495STsutomu Itoh } 1836bb9c12c9SSage Weil 1837bb9c12c9SSage Weil /* take transaction reference */ 1838bb9c12c9SSage Weil cur_trans = trans->transaction; 18399b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1840bb9c12c9SSage Weil 18413a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18426fc4e354SSage Weil 18436fc4e354SSage Weil /* 18446fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 18456fc4e354SSage Weil * async commit thread will be the one to unlock it. 18466fc4e354SSage Weil */ 1847b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18480b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 18496fc4e354SSage Weil 18507892b5afSMiao Xie schedule_work(&ac->work); 1851bb9c12c9SSage Weil 1852bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1853bb9c12c9SSage Weil if (wait_for_unblock) 18542ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1855bb9c12c9SSage Weil else 18562ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1857bb9c12c9SSage Weil 185838e88054SSage Weil if (current->journal_info == trans) 185938e88054SSage Weil current->journal_info = NULL; 186038e88054SSage Weil 1861724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1862bb9c12c9SSage Weil return 0; 1863bb9c12c9SSage Weil } 1864bb9c12c9SSage Weil 186549b25e05SJeff Mahoney 186649b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 18677b8b92afSJosef Bacik struct btrfs_root *root, int err) 186849b25e05SJeff Mahoney { 18690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 187049b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1871f094ac32SLiu Bo DEFINE_WAIT(wait); 187249b25e05SJeff Mahoney 187349b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 187449b25e05SJeff Mahoney 187566642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18767b8b92afSJosef Bacik 18770b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 187866b6135bSLiu Bo 187925d8c284SMiao Xie /* 188025d8c284SMiao Xie * If the transaction is removed from the list, it means this 188125d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 188225d8c284SMiao Xie * to call the cleanup function. 188325d8c284SMiao Xie */ 188425d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 188566b6135bSLiu Bo 188649b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18870b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18884a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18890b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1890f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1891f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1892f094ac32SLiu Bo 18930b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1894d7096fc3SJosef Bacik } 18950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 189649b25e05SJeff Mahoney 18972ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 189849b25e05SJeff Mahoney 18990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 19000b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 19010b246afaSJeff Mahoney fs_info->running_transaction = NULL; 19020b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 19034a9d8bdeSMiao Xie 1904e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 19050b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1906724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1907724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 190849b25e05SJeff Mahoney 190949b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 191049b25e05SJeff Mahoney 191149b25e05SJeff Mahoney if (current->journal_info == trans) 191249b25e05SJeff Mahoney current->journal_info = NULL; 19130b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 191449b25e05SJeff Mahoney 191549b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 191649b25e05SJeff Mahoney } 191749b25e05SJeff Mahoney 191882436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 191982436617SMiao Xie { 19203cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 19216c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 192282436617SMiao Xie return 0; 192382436617SMiao Xie } 192482436617SMiao Xie 192582436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 192682436617SMiao Xie { 19273cdde224SJeff Mahoney if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) 19286374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 192982436617SMiao Xie } 193082436617SMiao Xie 193150d9aa99SJosef Bacik static inline void 1932161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 193350d9aa99SJosef Bacik { 1934161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1935161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 193650d9aa99SJosef Bacik } 193750d9aa99SJosef Bacik 19383a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 193979154b1bSChris Mason { 19403a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 194149b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19428fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 194325287e0aSMiao Xie int ret; 194479154b1bSChris Mason 19458d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 194620c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 194725287e0aSMiao Xie ret = cur_trans->aborted; 19483a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1949e4a2bcacSJosef Bacik return ret; 195025287e0aSMiao Xie } 195149b25e05SJeff Mahoney 195256bec294SChris Mason /* make a pass through all the delayed refs we have so far 195356bec294SChris Mason * any runnings procs may add more while we are here 195456bec294SChris Mason */ 19552ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, 0); 1956e4a2bcacSJosef Bacik if (ret) { 19573a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1958e4a2bcacSJosef Bacik return ret; 1959e4a2bcacSJosef Bacik } 196056bec294SChris Mason 19612ff7e61eSJeff Mahoney btrfs_trans_release_metadata(trans, fs_info); 19620e721106SJosef Bacik trans->block_rsv = NULL; 19630e721106SJosef Bacik 1964b7ec40d7SChris Mason cur_trans = trans->transaction; 196549b25e05SJeff Mahoney 196656bec294SChris Mason /* 196756bec294SChris Mason * set the flushing flag so procs in this transaction have to 196856bec294SChris Mason * start sending their work down. 196956bec294SChris Mason */ 1970b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19711be41b78SJosef Bacik smp_wmb(); 197256bec294SChris Mason 1973ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 19742ff7e61eSJeff Mahoney btrfs_create_pending_block_groups(trans, fs_info); 1975ea658badSJosef Bacik 19762ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, 0); 1977e4a2bcacSJosef Bacik if (ret) { 19783a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1979e4a2bcacSJosef Bacik return ret; 1980e4a2bcacSJosef Bacik } 198156bec294SChris Mason 19823204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19831bbc621eSChris Mason int run_it = 0; 19841bbc621eSChris Mason 19851bbc621eSChris Mason /* this mutex is also taken before trying to set 19861bbc621eSChris Mason * block groups readonly. We need to make sure 19871bbc621eSChris Mason * that nobody has set a block group readonly 19881bbc621eSChris Mason * after a extents from that block group have been 19891bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19901bbc621eSChris Mason * will wait for the transaction to commit if it 19913204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19921bbc621eSChris Mason * 19933204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19943204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19951bbc621eSChris Mason * hurt to have more than one go through, but there's no 19961bbc621eSChris Mason * real advantage to it either. 19971bbc621eSChris Mason */ 19980b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 19993204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 20003204d33cSJosef Bacik &cur_trans->flags)) 20011bbc621eSChris Mason run_it = 1; 20020b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 20031bbc621eSChris Mason 20041bbc621eSChris Mason if (run_it) 20052ff7e61eSJeff Mahoney ret = btrfs_start_dirty_block_groups(trans, fs_info); 20061bbc621eSChris Mason } 20071bbc621eSChris Mason if (ret) { 20083a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20091bbc621eSChris Mason return ret; 20101bbc621eSChris Mason } 20111bbc621eSChris Mason 20120b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20134a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20140b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 20159b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 20163a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 2017ccd467d6SChris Mason 20182ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 201915ee9bc7SJosef Bacik 2020b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2021b4924a0fSLiu Bo ret = cur_trans->aborted; 2022b4924a0fSLiu Bo 2023724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 202415ee9bc7SJosef Bacik 202549b25e05SJeff Mahoney return ret; 202679154b1bSChris Mason } 20274313b399SChris Mason 20284a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20290b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2030bb9c12c9SSage Weil 20310b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2032ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2033ccd467d6SChris Mason struct btrfs_transaction, list); 20344a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20359b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2037ccd467d6SChris Mason 20382ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20391f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2040ccd467d6SChris Mason 2041724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20421f9b8c8fSFilipe Manana if (ret) 20431f9b8c8fSFilipe Manana goto cleanup_transaction; 2044a4abeea4SJosef Bacik } else { 20450b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2046ccd467d6SChris Mason } 2047a4abeea4SJosef Bacik } else { 20480b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2049ccd467d6SChris Mason } 205015ee9bc7SJosef Bacik 20510860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20520860adfdSMiao Xie 20530b246afaSJeff Mahoney ret = btrfs_start_delalloc_flush(fs_info); 205482436617SMiao Xie if (ret) 205582436617SMiao Xie goto cleanup_transaction; 205682436617SMiao Xie 20572ff7e61eSJeff Mahoney ret = btrfs_run_delayed_items(trans, fs_info); 205849b25e05SJeff Mahoney if (ret) 205949b25e05SJeff Mahoney goto cleanup_transaction; 206016cdcec7SMiao Xie 2061581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2062581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2063ed3b3d31SChris Mason 2064581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 20652ff7e61eSJeff Mahoney ret = btrfs_run_delayed_items(trans, fs_info); 2066ca469637SMiao Xie if (ret) 2067ca469637SMiao Xie goto cleanup_transaction; 2068ca469637SMiao Xie 20690b246afaSJeff Mahoney btrfs_wait_delalloc_flush(fs_info); 2070cb7ab021SWang Shilong 2071161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 207250d9aa99SJosef Bacik 20732ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2074ed0ca140SJosef Bacik /* 2075ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2076ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20774a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2078ed0ca140SJosef Bacik */ 20790b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20804a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20810b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2082ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2083ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 208415ee9bc7SJosef Bacik 20852cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 208620c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 20872cba30f1SMiao Xie ret = cur_trans->aborted; 20886cf7f77eSWang Shilong goto scrub_continue; 20892cba30f1SMiao Xie } 20907585717fSChris Mason /* 20917585717fSChris Mason * the reloc mutex makes sure that we stop 20927585717fSChris Mason * the balancing code from coming in and moving 20937585717fSChris Mason * extents around in the middle of the commit 20947585717fSChris Mason */ 20950b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 20967585717fSChris Mason 209742874b3dSMiao Xie /* 209842874b3dSMiao Xie * We needn't worry about the delayed items because we will 209942874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 210042874b3dSMiao Xie * core function of the snapshot creation. 210142874b3dSMiao Xie */ 21020b246afaSJeff Mahoney ret = create_pending_snapshots(trans, fs_info); 210349b25e05SJeff Mahoney if (ret) { 21040b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21056cf7f77eSWang Shilong goto scrub_continue; 210649b25e05SJeff Mahoney } 21073063d29fSChris Mason 210842874b3dSMiao Xie /* 210942874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 211042874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 211142874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 211242874b3dSMiao Xie * them. 211342874b3dSMiao Xie * 211442874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 211542874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 211642874b3dSMiao Xie * the nodes and leaves. 211742874b3dSMiao Xie */ 21182ff7e61eSJeff Mahoney ret = btrfs_run_delayed_items(trans, fs_info); 211949b25e05SJeff Mahoney if (ret) { 21200b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21216cf7f77eSWang Shilong goto scrub_continue; 212249b25e05SJeff Mahoney } 212316cdcec7SMiao Xie 21242ff7e61eSJeff Mahoney ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 212549b25e05SJeff Mahoney if (ret) { 21260b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21276cf7f77eSWang Shilong goto scrub_continue; 212849b25e05SJeff Mahoney } 212956bec294SChris Mason 2130e999376fSChris Mason /* 2131e999376fSChris Mason * make sure none of the code above managed to slip in a 2132e999376fSChris Mason * delayed item 2133e999376fSChris Mason */ 2134ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2135e999376fSChris Mason 21362c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2137dc17ff8fSChris Mason 2138e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2139e02119d5SChris Mason * various roots consistent with each other. Every pointer 2140e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2141e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2142e02119d5SChris Mason * the tree logging code from jumping in and changing any 2143e02119d5SChris Mason * of the trees. 2144e02119d5SChris Mason * 2145e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2146e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2147e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2148e02119d5SChris Mason * from now until after the super is written, we avoid races 2149e02119d5SChris Mason * with the tree-log code. 2150e02119d5SChris Mason */ 21510b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21521a40e23bSZheng Yan 21530b246afaSJeff Mahoney ret = commit_fs_roots(trans, fs_info); 215449b25e05SJeff Mahoney if (ret) { 21550b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21560b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21576cf7f77eSWang Shilong goto scrub_continue; 215849b25e05SJeff Mahoney } 215954aa1f4dSChris Mason 21603818aea2SQu Wenruo /* 21617e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21627e1876acSDavid Sterba * before the next transaction. 21633818aea2SQu Wenruo */ 21640b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21653818aea2SQu Wenruo 21665d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2167e02119d5SChris Mason * safe to free the root of tree log roots 2168e02119d5SChris Mason */ 21690b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2170e02119d5SChris Mason 21710ed4792aSQu Wenruo /* 217282bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 217382bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 217482bafb38SQu Wenruo */ 217582bafb38SQu Wenruo ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1); 217682bafb38SQu Wenruo if (ret) { 217782bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 217882bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 217982bafb38SQu Wenruo goto scrub_continue; 218082bafb38SQu Wenruo } 218182bafb38SQu Wenruo 218282bafb38SQu Wenruo /* 21830ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21840ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21850ed4792aSQu Wenruo */ 21860b246afaSJeff Mahoney ret = btrfs_qgroup_account_extents(trans, fs_info); 21870ed4792aSQu Wenruo if (ret < 0) { 21880b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21890b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21900ed4792aSQu Wenruo goto scrub_continue; 21910ed4792aSQu Wenruo } 21920ed4792aSQu Wenruo 21932ff7e61eSJeff Mahoney ret = commit_cowonly_roots(trans, fs_info); 219449b25e05SJeff Mahoney if (ret) { 21950b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21960b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21976cf7f77eSWang Shilong goto scrub_continue; 219849b25e05SJeff Mahoney } 219954aa1f4dSChris Mason 22002cba30f1SMiao Xie /* 22012cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 22022cba30f1SMiao Xie * update ->aborted, check it. 22032cba30f1SMiao Xie */ 220420c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 22052cba30f1SMiao Xie ret = cur_trans->aborted; 22060b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22070b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22086cf7f77eSWang Shilong goto scrub_continue; 22092cba30f1SMiao Xie } 22102cba30f1SMiao Xie 22118b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 221211833d66SYan Zheng 22130b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22145f39d397SChris Mason 22150b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22160b246afaSJeff Mahoney fs_info->tree_root->node); 22170b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22189e351cc8SJosef Bacik &cur_trans->switch_commits); 22195d4f98a2SYan Zheng 22200b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22210b246afaSJeff Mahoney fs_info->chunk_root->node); 22220b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22239e351cc8SJosef Bacik &cur_trans->switch_commits); 22249e351cc8SJosef Bacik 22250b246afaSJeff Mahoney switch_commit_roots(cur_trans, fs_info); 22265d4f98a2SYan Zheng 2227ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22281bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22292ff7e61eSJeff Mahoney update_super_roots(fs_info); 2230e02119d5SChris Mason 22310b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22320b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22330b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22340b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2235ccd467d6SChris Mason 22360b246afaSJeff Mahoney btrfs_update_commit_device_size(fs_info); 22372ff7e61eSJeff Mahoney btrfs_update_commit_device_bytes_used(fs_info, cur_trans); 2238935e5cc9SMiao Xie 22390b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22400b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2241656f30dbSFilipe Manana 22424fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22434fbcdf66SFilipe Manana 22440b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22454a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22460b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22470b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22480b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2249b7ec40d7SChris Mason 22500b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2251e6dcd2dcSChris Mason 2252bf89d38fSJeff Mahoney ret = btrfs_write_and_wait_transaction(trans, fs_info); 225349b25e05SJeff Mahoney if (ret) { 22540b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 225508748810SDavid Sterba "Error while writing out transaction"); 22560b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22576cf7f77eSWang Shilong goto scrub_continue; 225849b25e05SJeff Mahoney } 225949b25e05SJeff Mahoney 2260eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 226149b25e05SJeff Mahoney if (ret) { 22620b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22636cf7f77eSWang Shilong goto scrub_continue; 226449b25e05SJeff Mahoney } 22654313b399SChris Mason 2266e02119d5SChris Mason /* 2267e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2268e02119d5SChris Mason * to go about their business 2269e02119d5SChris Mason */ 22700b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2271e02119d5SChris Mason 22722ff7e61eSJeff Mahoney btrfs_finish_extent_commit(trans, fs_info); 22734313b399SChris Mason 22743204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22750b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 227613212b54SZhao Lei 22770b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22784a9d8bdeSMiao Xie /* 22794a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22804a9d8bdeSMiao Xie * which can change it. 22814a9d8bdeSMiao Xie */ 22824a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22832c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 22843de4586cSChris Mason 22850b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 228613c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22870b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2288a4abeea4SJosef Bacik 2289724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2290724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 229158176a96SJosef Bacik 22920860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 22930b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2294b2b5ef5cSJan Kara 22953a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 22961abe9b8aSliubo 22972ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2298a2de733cSArne Jansen 22999ed74f2dSJosef Bacik if (current->journal_info == trans) 23009ed74f2dSJosef Bacik current->journal_info = NULL; 23019ed74f2dSJosef Bacik 23022c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 230324bbcf04SYan, Zheng 23049e7cc91aSWang Xiaoguang /* 23059e7cc91aSWang Xiaoguang * If fs has been frozen, we can not handle delayed iputs, otherwise 23069e7cc91aSWang Xiaoguang * it'll result in deadlock about SB_FREEZE_FS. 23079e7cc91aSWang Xiaoguang */ 23080b246afaSJeff Mahoney if (current != fs_info->transaction_kthread && 2309fac03c8dSDavid Sterba current != fs_info->cleaner_kthread && 2310fac03c8dSDavid Sterba !test_bit(BTRFS_FS_FROZEN, &fs_info->flags)) 23112ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(fs_info); 231224bbcf04SYan, Zheng 231379154b1bSChris Mason return ret; 231449b25e05SJeff Mahoney 23156cf7f77eSWang Shilong scrub_continue: 23162ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 231749b25e05SJeff Mahoney cleanup_transaction: 23182ff7e61eSJeff Mahoney btrfs_trans_release_metadata(trans, fs_info); 23194fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 23200e721106SJosef Bacik trans->block_rsv = NULL; 23210b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 232249b25e05SJeff Mahoney if (current->journal_info == trans) 232349b25e05SJeff Mahoney current->journal_info = NULL; 23243a45bb20SJeff Mahoney cleanup_transaction(trans, trans->root, ret); 232549b25e05SJeff Mahoney 232649b25e05SJeff Mahoney return ret; 232779154b1bSChris Mason } 232879154b1bSChris Mason 2329d352ac68SChris Mason /* 23309d1a2a3aSDavid Sterba * return < 0 if error 23319d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23329d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23339d1a2a3aSDavid Sterba * 23349d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23359d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23369d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23379d1a2a3aSDavid Sterba * few seconds later. 2338d352ac68SChris Mason */ 23399d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2340e9d0b13bSChris Mason { 23419d1a2a3aSDavid Sterba int ret; 23425d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2343e9d0b13bSChris Mason 2344a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23459d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23469d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23479d1a2a3aSDavid Sterba return 0; 23489d1a2a3aSDavid Sterba } 23499d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23509d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2351cfad392bSJosef Bacik list_del_init(&root->root_list); 2352a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23535d4f98a2SYan Zheng 2354ab8d0fc4SJeff Mahoney btrfs_debug(fs_info, "cleaner removing %llu", root->objectid); 235576dda93cSYan, Zheng 235616cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 235716cdcec7SMiao Xie 235876dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 235976dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23602c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 236176dda93cSYan, Zheng else 23622c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 236332471dc2SDavid Sterba 23646596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2365e9d0b13bSChris Mason } 2366572d9ab7SDavid Sterba 2367572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2368572d9ab7SDavid Sterba { 2369572d9ab7SDavid Sterba unsigned long prev; 2370572d9ab7SDavid Sterba unsigned long bit; 2371572d9ab7SDavid Sterba 23726c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2373572d9ab7SDavid Sterba if (!prev) 2374572d9ab7SDavid Sterba return; 2375572d9ab7SDavid Sterba 23767e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23777e1876acSDavid Sterba if (prev & bit) 23787e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23797e1876acSDavid Sterba prev &= ~bit; 23807e1876acSDavid Sterba 23817e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23827e1876acSDavid Sterba if (prev & bit) 23837e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23847e1876acSDavid Sterba prev &= ~bit; 23857e1876acSDavid Sterba 2386d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2387d51033d0SDavid Sterba if (prev & bit) 2388d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2389d51033d0SDavid Sterba prev &= ~bit; 2390d51033d0SDavid Sterba 2391572d9ab7SDavid Sterba if (prev) 2392572d9ab7SDavid Sterba btrfs_warn(fs_info, 2393572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2394572d9ab7SDavid Sterba } 2395