16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2679154b1bSChris Mason #include "ctree.h" 2779154b1bSChris Mason #include "disk-io.h" 2879154b1bSChris Mason #include "transaction.h" 29925baeddSChris Mason #include "locking.h" 30e02119d5SChris Mason #include "tree-log.h" 31581bb050SLi Zefan #include "inode-map.h" 32733f4fbbSStefan Behrens #include "volumes.h" 338dabb742SStefan Behrens #include "dev-replace.h" 34fcebe456SJosef Bacik #include "qgroup.h" 3579154b1bSChris Mason 360f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 370f7d52f4SChris Mason 38e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 394a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 404a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 414a9d8bdeSMiao Xie __TRANS_START), 424a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 434a9d8bdeSMiao Xie __TRANS_START | 444a9d8bdeSMiao Xie __TRANS_ATTACH), 454a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 464a9d8bdeSMiao Xie __TRANS_START | 474a9d8bdeSMiao Xie __TRANS_ATTACH | 484a9d8bdeSMiao Xie __TRANS_JOIN), 494a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 504a9d8bdeSMiao Xie __TRANS_START | 514a9d8bdeSMiao Xie __TRANS_ATTACH | 524a9d8bdeSMiao Xie __TRANS_JOIN | 534a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 544a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 554a9d8bdeSMiao Xie __TRANS_START | 564a9d8bdeSMiao Xie __TRANS_ATTACH | 574a9d8bdeSMiao Xie __TRANS_JOIN | 584a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 594a9d8bdeSMiao Xie }; 604a9d8bdeSMiao Xie 61724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 6279154b1bSChris Mason { 6313c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6413c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 65a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 66c46effa6SLiu Bo WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root)); 671262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 681262133bSJosef Bacik printk(KERN_ERR "pending csums is %llu\n", 691262133bSJosef Bacik transaction->delayed_refs.pending_csums); 706df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 716df9a95eSJosef Bacik struct extent_map *em; 726df9a95eSJosef Bacik 736df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 746df9a95eSJosef Bacik struct extent_map, list); 756df9a95eSJosef Bacik list_del_init(&em->list); 766df9a95eSJosef Bacik free_extent_map(em); 776df9a95eSJosef Bacik } 787785a663SFilipe Manana /* 797785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 807785a663SFilipe Manana * because the transaction was aborted and a commit did not 817785a663SFilipe Manana * happen (things failed before writing the new superblock 827785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 837785a663SFilipe Manana * discard the physical locations of the block groups. 847785a663SFilipe Manana */ 857785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 867785a663SFilipe Manana struct btrfs_block_group_cache *cache; 877785a663SFilipe Manana 887785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 897785a663SFilipe Manana struct btrfs_block_group_cache, 907785a663SFilipe Manana bg_list); 917785a663SFilipe Manana list_del_init(&cache->bg_list); 927785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 937785a663SFilipe Manana btrfs_put_block_group(cache); 947785a663SFilipe Manana } 952c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 9679154b1bSChris Mason } 9778fae27eSChris Mason } 9879154b1bSChris Mason 99663dfbb0SFilipe Manana static void clear_btree_io_tree(struct extent_io_tree *tree) 100663dfbb0SFilipe Manana { 101663dfbb0SFilipe Manana spin_lock(&tree->lock); 102b666a9cdSDavid Sterba /* 103b666a9cdSDavid Sterba * Do a single barrier for the waitqueue_active check here, the state 104b666a9cdSDavid Sterba * of the waitqueue should not change once clear_btree_io_tree is 105b666a9cdSDavid Sterba * called. 106b666a9cdSDavid Sterba */ 107b666a9cdSDavid Sterba smp_mb(); 108663dfbb0SFilipe Manana while (!RB_EMPTY_ROOT(&tree->state)) { 109663dfbb0SFilipe Manana struct rb_node *node; 110663dfbb0SFilipe Manana struct extent_state *state; 111663dfbb0SFilipe Manana 112663dfbb0SFilipe Manana node = rb_first(&tree->state); 113663dfbb0SFilipe Manana state = rb_entry(node, struct extent_state, rb_node); 114663dfbb0SFilipe Manana rb_erase(&state->rb_node, &tree->state); 115663dfbb0SFilipe Manana RB_CLEAR_NODE(&state->rb_node); 116663dfbb0SFilipe Manana /* 117663dfbb0SFilipe Manana * btree io trees aren't supposed to have tasks waiting for 118663dfbb0SFilipe Manana * changes in the flags of extent states ever. 119663dfbb0SFilipe Manana */ 120663dfbb0SFilipe Manana ASSERT(!waitqueue_active(&state->wq)); 121663dfbb0SFilipe Manana free_extent_state(state); 122351810c1SDavid Sterba 123351810c1SDavid Sterba cond_resched_lock(&tree->lock); 124663dfbb0SFilipe Manana } 125663dfbb0SFilipe Manana spin_unlock(&tree->lock); 126663dfbb0SFilipe Manana } 127663dfbb0SFilipe Manana 1289e351cc8SJosef Bacik static noinline void switch_commit_roots(struct btrfs_transaction *trans, 1299e351cc8SJosef Bacik struct btrfs_fs_info *fs_info) 130817d52f8SJosef Bacik { 1319e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 1329e351cc8SJosef Bacik 1339e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 1349e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 1359e351cc8SJosef Bacik dirty_list) { 1369e351cc8SJosef Bacik list_del_init(&root->dirty_list); 137817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 138817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 1399e351cc8SJosef Bacik if (is_fstree(root->objectid)) 1409e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 141663dfbb0SFilipe Manana clear_btree_io_tree(&root->dirty_log_pages); 1429e351cc8SJosef Bacik } 1432b9dbef2SJosef Bacik 1442b9dbef2SJosef Bacik /* We can free old roots now. */ 1452b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1462b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1472b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1482b9dbef2SJosef Bacik struct btrfs_root, root_list); 1492b9dbef2SJosef Bacik list_del_init(&root->root_list); 1502b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1512b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1522b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1532b9dbef2SJosef Bacik } 1542b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1559e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 156817d52f8SJosef Bacik } 157817d52f8SJosef Bacik 1580860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1590860adfdSMiao Xie unsigned int type) 1600860adfdSMiao Xie { 1610860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1620860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1630860adfdSMiao Xie } 1640860adfdSMiao Xie 1650860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1660860adfdSMiao Xie unsigned int type) 1670860adfdSMiao Xie { 1680860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1690860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1700860adfdSMiao Xie } 1710860adfdSMiao Xie 1720860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1730860adfdSMiao Xie unsigned int type) 1740860adfdSMiao Xie { 1750860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1760860adfdSMiao Xie } 1770860adfdSMiao Xie 1780860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1790860adfdSMiao Xie { 1800860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 181178260b2SMiao Xie } 182178260b2SMiao Xie 183d352ac68SChris Mason /* 184d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 185d352ac68SChris Mason */ 1860860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 18779154b1bSChris Mason { 18879154b1bSChris Mason struct btrfs_transaction *cur_trans; 18919ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 190a4abeea4SJosef Bacik 19119ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 192d43317dcSChris Mason loop: 19349b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 19487533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 19519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 19649b25e05SJeff Mahoney return -EROFS; 19749b25e05SJeff Mahoney } 19849b25e05SJeff Mahoney 19919ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 200a4abeea4SJosef Bacik if (cur_trans) { 201871383beSDavid Sterba if (cur_trans->aborted) { 20219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 20349b25e05SJeff Mahoney return cur_trans->aborted; 204871383beSDavid Sterba } 2054a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 206178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 207178260b2SMiao Xie return -EBUSY; 208178260b2SMiao Xie } 209a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 210a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 2110860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 21219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 213a4abeea4SJosef Bacik return 0; 214a4abeea4SJosef Bacik } 21519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 216a4abeea4SJosef Bacik 217354aa0fbSMiao Xie /* 218354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 219354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 220354aa0fbSMiao Xie */ 221354aa0fbSMiao Xie if (type == TRANS_ATTACH) 222354aa0fbSMiao Xie return -ENOENT; 223354aa0fbSMiao Xie 2244a9d8bdeSMiao Xie /* 2254a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 2264a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 2274a9d8bdeSMiao Xie */ 2284a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2294a9d8bdeSMiao Xie 230a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 231db5b493aSTsutomu Itoh if (!cur_trans) 232db5b493aSTsutomu Itoh return -ENOMEM; 233d43317dcSChris Mason 23419ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 23519ae4e81SJan Schmidt if (fs_info->running_transaction) { 236d43317dcSChris Mason /* 237d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2384a9d8bdeSMiao Xie * to redo the checks above 239d43317dcSChris Mason */ 240a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 241d43317dcSChris Mason goto loop; 24287533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 243e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2447b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 2457b8b92afSJosef Bacik return -EROFS; 246a4abeea4SJosef Bacik } 247d43317dcSChris Mason 24813c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2490860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 25079154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 25179154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 252161c3549SJosef Bacik init_waitqueue_head(&cur_trans->pending_wait); 2534a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 254a4abeea4SJosef Bacik /* 255a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 256a4abeea4SJosef Bacik * commit the transaction. 257a4abeea4SJosef Bacik */ 258a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 259161c3549SJosef Bacik atomic_set(&cur_trans->pending_ordered, 0); 2603204d33cSJosef Bacik cur_trans->flags = 0; 26108607c1bSChris Mason cur_trans->start_time = get_seconds(); 26256bec294SChris Mason 263a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 264a099d0fdSAlexandru Moise 265c46effa6SLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT; 2663368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 267d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 26820b297d6SJan Schmidt 26920b297d6SJan Schmidt /* 27020b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 27120b297d6SJan Schmidt * the log must never go across transaction boundaries. 27220b297d6SJan Schmidt */ 27320b297d6SJan Schmidt smp_mb(); 27431b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 275efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when " 27620b297d6SJan Schmidt "creating a fresh transaction\n"); 27731b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 278efe120a0SFrank Holton WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when " 27920b297d6SJan Schmidt "creating a fresh transaction\n"); 280fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 28120b297d6SJan Schmidt 28256bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 28356bec294SChris Mason 2843063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 2856df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 2869e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 287ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2881bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2892b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2901bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 291cb723e49SJosef Bacik cur_trans->num_dirty_bgs = 0; 292ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 293e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2942b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 29519ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 296d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 29719ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 29819ae4e81SJan Schmidt fs_info->generation++; 29919ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 30019ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 30149b25e05SJeff Mahoney cur_trans->aborted = 0; 30219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 30315ee9bc7SJosef Bacik 30479154b1bSChris Mason return 0; 30579154b1bSChris Mason } 30679154b1bSChris Mason 307d352ac68SChris Mason /* 308d397712bSChris Mason * this does all the record keeping required to make sure that a reference 309d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 310d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 311d397712bSChris Mason * when the transaction commits 312d352ac68SChris Mason */ 3137585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 3146426c7adSQu Wenruo struct btrfs_root *root, 3156426c7adSQu Wenruo int force) 3166702ed49SChris Mason { 3176426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 3186426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 3196702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 3205d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 3215d4f98a2SYan Zheng 3227585717fSChris Mason /* 32327cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 3247585717fSChris Mason * we have the reloc mutex held now, so there 3257585717fSChris Mason * is only one writer in this function 3267585717fSChris Mason */ 32727cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3287585717fSChris Mason 32927cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 3307585717fSChris Mason * they find our root->last_trans update 3317585717fSChris Mason */ 3327585717fSChris Mason smp_wmb(); 3337585717fSChris Mason 334a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 3356426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 336a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 337a4abeea4SJosef Bacik return 0; 338a4abeea4SJosef Bacik } 3396702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 3406702ed49SChris Mason (unsigned long)root->root_key.objectid, 3416702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 342a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3437585717fSChris Mason root->last_trans = trans->transid; 3447585717fSChris Mason 3457585717fSChris Mason /* this is pretty tricky. We don't want to 3467585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3477585717fSChris Mason * unless we're really doing the first setup for this root in 3487585717fSChris Mason * this transaction. 3497585717fSChris Mason * 3507585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3517585717fSChris Mason * if we want to take the expensive mutex. 3527585717fSChris Mason * 3537585717fSChris Mason * But, we have to set root->last_trans before we 3547585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3557585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 35627cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3577585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3587585717fSChris Mason * 3597585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3607585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3617585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3627585717fSChris Mason * done before we pop in the zero below 3637585717fSChris Mason */ 3645d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 365c7548af6SChris Mason smp_mb__before_atomic(); 36627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3676702ed49SChris Mason } 3685d4f98a2SYan Zheng return 0; 3696702ed49SChris Mason } 3705d4f98a2SYan Zheng 3717585717fSChris Mason 3722b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3732b9dbef2SJosef Bacik struct btrfs_root *root) 3742b9dbef2SJosef Bacik { 3752b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3762b9dbef2SJosef Bacik 3772b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3782b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3792b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3802b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3812b9dbef2SJosef Bacik 3822b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3832b9dbef2SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 3842b9dbef2SJosef Bacik radix_tree_tag_clear(&root->fs_info->fs_roots_radix, 3852b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3862b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3872b9dbef2SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 3882b9dbef2SJosef Bacik } 3892b9dbef2SJosef Bacik 3907585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3917585717fSChris Mason struct btrfs_root *root) 3927585717fSChris Mason { 39327cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3947585717fSChris Mason return 0; 3957585717fSChris Mason 3967585717fSChris Mason /* 39727cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3987585717fSChris Mason * and barriers 3997585717fSChris Mason */ 4007585717fSChris Mason smp_rmb(); 4017585717fSChris Mason if (root->last_trans == trans->transid && 40227cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 4037585717fSChris Mason return 0; 4047585717fSChris Mason 4057585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 4066426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 4077585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 4087585717fSChris Mason 4097585717fSChris Mason return 0; 4107585717fSChris Mason } 4117585717fSChris Mason 4124a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 4134a9d8bdeSMiao Xie { 4144a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 415501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 416501407aaSJosef Bacik !trans->aborted); 4174a9d8bdeSMiao Xie } 4184a9d8bdeSMiao Xie 419d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 420d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 421d352ac68SChris Mason * transaction might not be fully on disk. 422d352ac68SChris Mason */ 42337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 42479154b1bSChris Mason { 425f9295749SChris Mason struct btrfs_transaction *cur_trans; 42679154b1bSChris Mason 427a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 428f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 4294a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 43013c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 431a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 43272d63ed6SLi Zefan 43372d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 434501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 435501407aaSJosef Bacik cur_trans->aborted); 436724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 437a4abeea4SJosef Bacik } else { 438a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 439f9295749SChris Mason } 44037d1aeeeSChris Mason } 44137d1aeeeSChris Mason 442a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 44337d1aeeeSChris Mason { 444a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 445a4abeea4SJosef Bacik return 0; 446a4abeea4SJosef Bacik 447a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 448a22285a6SYan, Zheng return 1; 449a4abeea4SJosef Bacik 450a4abeea4SJosef Bacik if (type == TRANS_START && 451a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 452a4abeea4SJosef Bacik return 1; 453a4abeea4SJosef Bacik 454a22285a6SYan, Zheng return 0; 455a22285a6SYan, Zheng } 456a22285a6SYan, Zheng 45720dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 45820dd2cbfSMiao Xie { 45920dd2cbfSMiao Xie if (!root->fs_info->reloc_ctl || 46027cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 46120dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 46220dd2cbfSMiao Xie root->reloc_root) 46320dd2cbfSMiao Xie return false; 46420dd2cbfSMiao Xie 46520dd2cbfSMiao Xie return true; 46620dd2cbfSMiao Xie } 46720dd2cbfSMiao Xie 46808e007d2SMiao Xie static struct btrfs_trans_handle * 4695aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 4705aed1dd8SAlexandru Moise unsigned int type, enum btrfs_reserve_flush_enum flush) 471a22285a6SYan, Zheng { 472a22285a6SYan, Zheng struct btrfs_trans_handle *h; 473a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 474b5009945SJosef Bacik u64 num_bytes = 0; 475c5567237SArne Jansen u64 qgroup_reserved = 0; 47620dd2cbfSMiao Xie bool reloc_reserved = false; 47720dd2cbfSMiao Xie int ret; 478acce952bSliubo 47946c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4802755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 48146c4e71eSFilipe Manana 48287533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 483acce952bSliubo return ERR_PTR(-EROFS); 4842a1eb461SJosef Bacik 48546c4e71eSFilipe Manana if (current->journal_info) { 4860860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4872a1eb461SJosef Bacik h = current->journal_info; 4882a1eb461SJosef Bacik h->use_count++; 489b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 4902a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4912a1eb461SJosef Bacik h->block_rsv = NULL; 4922a1eb461SJosef Bacik goto got_it; 4932a1eb461SJosef Bacik } 494b5009945SJosef Bacik 495b5009945SJosef Bacik /* 496b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 497b5009945SJosef Bacik * the appropriate flushing if need be. 498b5009945SJosef Bacik */ 499b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 500707e8a07SDavid Sterba qgroup_reserved = num_items * root->nodesize; 5017174109cSQu Wenruo ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved); 502c5567237SArne Jansen if (ret) 503c5567237SArne Jansen return ERR_PTR(ret); 504c5567237SArne Jansen 505b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 50620dd2cbfSMiao Xie /* 50720dd2cbfSMiao Xie * Do the reservation for the relocation root creation 50820dd2cbfSMiao Xie */ 509ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 51020dd2cbfSMiao Xie num_bytes += root->nodesize; 51120dd2cbfSMiao Xie reloc_reserved = true; 51220dd2cbfSMiao Xie } 51320dd2cbfSMiao Xie 5144a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 515b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 51608e007d2SMiao Xie num_bytes, flush); 517b5009945SJosef Bacik if (ret) 518843fcf35SMiao Xie goto reserve_fail; 519b5009945SJosef Bacik } 520a22285a6SYan, Zheng again: 521f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 522843fcf35SMiao Xie if (!h) { 523843fcf35SMiao Xie ret = -ENOMEM; 524843fcf35SMiao Xie goto alloc_fail; 525843fcf35SMiao Xie } 526a22285a6SYan, Zheng 52798114659SJosef Bacik /* 52898114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 52998114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 53098114659SJosef Bacik * because we're already holding a ref. We need this because we could 53198114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53298114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 533354aa0fbSMiao Xie * 534354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 535354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 53698114659SJosef Bacik */ 5370860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 538b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 539b2b5ef5cSJan Kara 540a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 54137d1aeeeSChris Mason wait_current_trans(root); 542a22285a6SYan, Zheng 543a4abeea4SJosef Bacik do { 544354aa0fbSMiao Xie ret = join_transaction(root, type); 545178260b2SMiao Xie if (ret == -EBUSY) { 546a4abeea4SJosef Bacik wait_current_trans(root); 547178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 548178260b2SMiao Xie ret = -ENOENT; 549178260b2SMiao Xie } 550a4abeea4SJosef Bacik } while (ret == -EBUSY); 551a4abeea4SJosef Bacik 552db5b493aSTsutomu Itoh if (ret < 0) { 553354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 554354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 555843fcf35SMiao Xie goto join_fail; 556db5b493aSTsutomu Itoh } 5570f7d52f4SChris Mason 558a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 559a22285a6SYan, Zheng 560a22285a6SYan, Zheng h->transid = cur_trans->transid; 561a22285a6SYan, Zheng h->transaction = cur_trans; 562d13603efSArne Jansen h->root = root; 5632a1eb461SJosef Bacik h->use_count = 1; 5647174109cSQu Wenruo 565a698d075SMiao Xie h->type = type; 566d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 567bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 568ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 569b7ec40d7SChris Mason 570a22285a6SYan, Zheng smp_mb(); 5714a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5724a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 573abdd2e80SFilipe Manana current->journal_info = h; 574a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 575a22285a6SYan, Zheng goto again; 576a22285a6SYan, Zheng } 5779ed74f2dSJosef Bacik 578b5009945SJosef Bacik if (num_bytes) { 5798c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 5802bcc0328SLiu Bo h->transid, num_bytes, 1); 581b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 582b5009945SJosef Bacik h->bytes_reserved = num_bytes; 58320dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 584a22285a6SYan, Zheng } 585a22285a6SYan, Zheng 5862a1eb461SJosef Bacik got_it: 587a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 588a22285a6SYan, Zheng 589a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 590a22285a6SYan, Zheng current->journal_info = h; 59179154b1bSChris Mason return h; 592843fcf35SMiao Xie 593843fcf35SMiao Xie join_fail: 5940860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 595843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 596843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 597843fcf35SMiao Xie alloc_fail: 598843fcf35SMiao Xie if (num_bytes) 599843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 600843fcf35SMiao Xie num_bytes); 601843fcf35SMiao Xie reserve_fail: 6027174109cSQu Wenruo btrfs_qgroup_free_meta(root, qgroup_reserved); 603843fcf35SMiao Xie return ERR_PTR(ret); 60479154b1bSChris Mason } 60579154b1bSChris Mason 606f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6075aed1dd8SAlexandru Moise unsigned int num_items) 608f9295749SChris Mason { 60908e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 61008e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 611f9295749SChris Mason } 6128eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6138eab77ffSFilipe Manana struct btrfs_root *root, 6148eab77ffSFilipe Manana unsigned int num_items, 6158eab77ffSFilipe Manana int min_factor) 6168eab77ffSFilipe Manana { 6178eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6188eab77ffSFilipe Manana u64 num_bytes; 6198eab77ffSFilipe Manana int ret; 6208eab77ffSFilipe Manana 6218eab77ffSFilipe Manana trans = btrfs_start_transaction(root, num_items); 6228eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6238eab77ffSFilipe Manana return trans; 6248eab77ffSFilipe Manana 6258eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6268eab77ffSFilipe Manana if (IS_ERR(trans)) 6278eab77ffSFilipe Manana return trans; 6288eab77ffSFilipe Manana 6298eab77ffSFilipe Manana num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 6308eab77ffSFilipe Manana ret = btrfs_cond_migrate_bytes(root->fs_info, 6318eab77ffSFilipe Manana &root->fs_info->trans_block_rsv, 6328eab77ffSFilipe Manana num_bytes, 6338eab77ffSFilipe Manana min_factor); 6348eab77ffSFilipe Manana if (ret) { 6358eab77ffSFilipe Manana btrfs_end_transaction(trans, root); 6368eab77ffSFilipe Manana return ERR_PTR(ret); 6378eab77ffSFilipe Manana } 6388eab77ffSFilipe Manana 6398eab77ffSFilipe Manana trans->block_rsv = &root->fs_info->trans_block_rsv; 6408eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 64188d3a5aaSJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 64288d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6438eab77ffSFilipe Manana 6448eab77ffSFilipe Manana return trans; 6458eab77ffSFilipe Manana } 6468407aa46SMiao Xie 64708e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 6485aed1dd8SAlexandru Moise struct btrfs_root *root, 6495aed1dd8SAlexandru Moise unsigned int num_items) 6508407aa46SMiao Xie { 65108e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 65208e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 6538407aa46SMiao Xie } 6548407aa46SMiao Xie 6557a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 656f9295749SChris Mason { 657575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN, 658575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 659f9295749SChris Mason } 660f9295749SChris Mason 6617a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6620af3d00bSJosef Bacik { 663575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 664575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6650af3d00bSJosef Bacik } 6660af3d00bSJosef Bacik 6677a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 6689ca9ee09SSage Weil { 669575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_USERSPACE, 670575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 6719ca9ee09SSage Weil } 6729ca9ee09SSage Weil 673d4edf39bSMiao Xie /* 674d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 675d4edf39bSMiao Xie * 676d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 677d4edf39bSMiao Xie * don't want to start a new one. 678d4edf39bSMiao Xie * 679d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 680d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 681d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 682d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 683d4edf39bSMiao Xie * invoke 684d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 685d4edf39bSMiao Xie */ 686354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 68760376ce4SJosef Bacik { 688575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 689575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 69060376ce4SJosef Bacik } 69160376ce4SJosef Bacik 692d4edf39bSMiao Xie /* 69390b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 694d4edf39bSMiao Xie * 695d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 696d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 697d4edf39bSMiao Xie * complete. 698d4edf39bSMiao Xie */ 699d4edf39bSMiao Xie struct btrfs_trans_handle * 700d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 701d4edf39bSMiao Xie { 702d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 703d4edf39bSMiao Xie 704575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 705575a75d6SAlexandru Moise BTRFS_RESERVE_NO_FLUSH); 706d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 707d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 708d4edf39bSMiao Xie 709d4edf39bSMiao Xie return trans; 710d4edf39bSMiao Xie } 711d4edf39bSMiao Xie 712d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 713b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 71489ce8a63SChris Mason struct btrfs_transaction *commit) 71589ce8a63SChris Mason { 7164a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 71789ce8a63SChris Mason } 71889ce8a63SChris Mason 71946204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 72046204592SSage Weil { 72146204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7228cd2807fSMiao Xie int ret = 0; 72346204592SSage Weil 72446204592SSage Weil if (transid) { 72546204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 726a4abeea4SJosef Bacik goto out; 72746204592SSage Weil 72846204592SSage Weil /* find specified transaction */ 729a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 73046204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 73146204592SSage Weil if (t->transid == transid) { 73246204592SSage Weil cur_trans = t; 733a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 7348cd2807fSMiao Xie ret = 0; 73546204592SSage Weil break; 73646204592SSage Weil } 7378cd2807fSMiao Xie if (t->transid > transid) { 7388cd2807fSMiao Xie ret = 0; 73946204592SSage Weil break; 74046204592SSage Weil } 7418cd2807fSMiao Xie } 742a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 74342383020SSage Weil 74442383020SSage Weil /* 74542383020SSage Weil * The specified transaction doesn't exist, or we 74642383020SSage Weil * raced with btrfs_commit_transaction 74742383020SSage Weil */ 74842383020SSage Weil if (!cur_trans) { 74942383020SSage Weil if (transid > root->fs_info->last_trans_committed) 75042383020SSage Weil ret = -EINVAL; 7518cd2807fSMiao Xie goto out; 75242383020SSage Weil } 75346204592SSage Weil } else { 75446204592SSage Weil /* find newest transaction that is committing | committed */ 755a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 75646204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 75746204592SSage Weil list) { 7584a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7594a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7603473f3c0SJosef Bacik break; 76146204592SSage Weil cur_trans = t; 762a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 76346204592SSage Weil break; 76446204592SSage Weil } 76546204592SSage Weil } 766a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 76746204592SSage Weil if (!cur_trans) 768a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 76946204592SSage Weil } 77046204592SSage Weil 77146204592SSage Weil wait_for_commit(root, cur_trans); 772724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 773a4abeea4SJosef Bacik out: 77446204592SSage Weil return ret; 77546204592SSage Weil } 77646204592SSage Weil 77737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 77837d1aeeeSChris Mason { 779a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 78037d1aeeeSChris Mason wait_current_trans(root); 78137d1aeeeSChris Mason } 78237d1aeeeSChris Mason 7838929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 7848929ecfaSYan, Zheng struct btrfs_root *root) 7858929ecfaSYan, Zheng { 7861be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 7870a2b2a84SJosef Bacik btrfs_check_space_for_delayed_refs(trans, root)) 7881be41b78SJosef Bacik return 1; 78936ba022aSJosef Bacik 7901be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 7918929ecfaSYan, Zheng } 7928929ecfaSYan, Zheng 7938929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 7948929ecfaSYan, Zheng struct btrfs_root *root) 7958929ecfaSYan, Zheng { 7968929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7978929ecfaSYan, Zheng int updates; 79849b25e05SJeff Mahoney int err; 7998929ecfaSYan, Zheng 800a4abeea4SJosef Bacik smp_mb(); 8014a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8024a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8038929ecfaSYan, Zheng return 1; 8048929ecfaSYan, Zheng 8058929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 8068929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 80749b25e05SJeff Mahoney if (updates) { 80828ed1345SChris Mason err = btrfs_run_delayed_refs(trans, root, updates * 2); 80949b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 81049b25e05SJeff Mahoney return err; 81149b25e05SJeff Mahoney } 8128929ecfaSYan, Zheng 8138929ecfaSYan, Zheng return should_end_transaction(trans, root); 8148929ecfaSYan, Zheng } 8158929ecfaSYan, Zheng 81689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 817a698d075SMiao Xie struct btrfs_root *root, int throttle) 81879154b1bSChris Mason { 8198929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 820ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 821*31b9655fSJosef Bacik u64 transid = trans->transid; 8221be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 823a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8244edc2ca3SDave Jones int err = 0; 825a79b7d4bSChris Mason int must_run_delayed_refs = 0; 826d6e4a428SChris Mason 8273bbb24b2SJosef Bacik if (trans->use_count > 1) { 8283bbb24b2SJosef Bacik trans->use_count--; 8292a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8302a1eb461SJosef Bacik return 0; 8312a1eb461SJosef Bacik } 8322a1eb461SJosef Bacik 833b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 8344c13d758SJosef Bacik trans->block_rsv = NULL; 835c5567237SArne Jansen 836ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 837ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 838ea658badSJosef Bacik 839c3e69d58SChris Mason trans->delayed_ref_updates = 0; 840a79b7d4bSChris Mason if (!trans->sync) { 841a79b7d4bSChris Mason must_run_delayed_refs = 842a79b7d4bSChris Mason btrfs_should_throttle_delayed_refs(trans, root); 8430a2b2a84SJosef Bacik cur = max_t(unsigned long, cur, 32); 844a79b7d4bSChris Mason 845a79b7d4bSChris Mason /* 846a79b7d4bSChris Mason * don't make the caller wait if they are from a NOLOCK 847a79b7d4bSChris Mason * or ATTACH transaction, it will deadlock with commit 848a79b7d4bSChris Mason */ 849a79b7d4bSChris Mason if (must_run_delayed_refs == 1 && 850a79b7d4bSChris Mason (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH))) 851a79b7d4bSChris Mason must_run_delayed_refs = 2; 85256bec294SChris Mason } 853bb721703SChris Mason 8540e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 8550e721106SJosef Bacik trans->block_rsv = NULL; 85656bec294SChris Mason 857ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 858ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 859ea658badSJosef Bacik 8604fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8614fbcdf66SFilipe Manana 862a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 8634a9d8bdeSMiao Xie should_end_transaction(trans, root) && 8644a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 8654a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 8664a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 8674a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 8684a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 869a4abeea4SJosef Bacik } 8708929ecfaSYan, Zheng 8714a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8723bbb24b2SJosef Bacik if (throttle) 8738929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 8743bbb24b2SJosef Bacik else 8758929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8768929ecfaSYan, Zheng } 8778929ecfaSYan, Zheng 8780860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8796df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 8806df7881aSJosef Bacik 8818929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 88213c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 88313c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8840860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 88589ce8a63SChris Mason 886a83342aaSDavid Sterba /* 887a83342aaSDavid Sterba * Make sure counter is updated before we wake up waiters. 888a83342aaSDavid Sterba */ 88999d16cbcSSage Weil smp_mb(); 89079154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 89179154b1bSChris Mason wake_up(&cur_trans->writer_wait); 892724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8939ed74f2dSJosef Bacik 8949ed74f2dSJosef Bacik if (current->journal_info == trans) 8959ed74f2dSJosef Bacik current->journal_info = NULL; 896ab78c84dSChris Mason 89724bbcf04SYan, Zheng if (throttle) 89824bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 89924bbcf04SYan, Zheng 90049b25e05SJeff Mahoney if (trans->aborted || 9014e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 9024e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 9034edc2ca3SDave Jones err = -EIO; 9044e121c06SJosef Bacik } 905edf39272SJan Schmidt assert_qgroups_uptodate(trans); 90649b25e05SJeff Mahoney 9074edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 908a79b7d4bSChris Mason if (must_run_delayed_refs) { 909*31b9655fSJosef Bacik btrfs_async_run_delayed_refs(root, cur, transid, 910a79b7d4bSChris Mason must_run_delayed_refs == 1); 911a79b7d4bSChris Mason } 9124edc2ca3SDave Jones return err; 91379154b1bSChris Mason } 91479154b1bSChris Mason 91589ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 91689ce8a63SChris Mason struct btrfs_root *root) 91789ce8a63SChris Mason { 91898ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 91989ce8a63SChris Mason } 92089ce8a63SChris Mason 92189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 92289ce8a63SChris Mason struct btrfs_root *root) 92389ce8a63SChris Mason { 92498ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 92516cdcec7SMiao Xie } 92616cdcec7SMiao Xie 927d352ac68SChris Mason /* 928d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 929d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 930690587d1SChris Mason * those extents are sent to disk but does not wait on them 931d352ac68SChris Mason */ 932690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 9338cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 93479154b1bSChris Mason { 935777e6bd7SChris Mason int err = 0; 9367c4452b9SChris Mason int werr = 0; 9371728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 938e6138876SJosef Bacik struct extent_state *cached_state = NULL; 939777e6bd7SChris Mason u64 start = 0; 9405f39d397SChris Mason u64 end; 9417c4452b9SChris Mason 9421728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 943e6138876SJosef Bacik mark, &cached_state)) { 944663dfbb0SFilipe Manana bool wait_writeback = false; 945663dfbb0SFilipe Manana 946663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 947663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 948210aa277SDavid Sterba mark, &cached_state); 949663dfbb0SFilipe Manana /* 950663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 951663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 952663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 953663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 954663dfbb0SFilipe Manana * to btrfs_wait_marked_extents() would not know that writeback 955663dfbb0SFilipe Manana * for this range started and therefore wouldn't wait for it to 956663dfbb0SFilipe Manana * finish - we don't want to commit a superblock that points to 957663dfbb0SFilipe Manana * btree nodes/leafs for which writeback hasn't finished yet 958663dfbb0SFilipe Manana * (and without errors). 959663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 960663dfbb0SFilipe Manana * the transaction (through clear_btree_io_tree()). 961663dfbb0SFilipe Manana */ 962663dfbb0SFilipe Manana if (err == -ENOMEM) { 963663dfbb0SFilipe Manana err = 0; 964663dfbb0SFilipe Manana wait_writeback = true; 965663dfbb0SFilipe Manana } 966663dfbb0SFilipe Manana if (!err) 9671728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9687c4452b9SChris Mason if (err) 9697c4452b9SChris Mason werr = err; 970663dfbb0SFilipe Manana else if (wait_writeback) 971663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 972e38e2ed7SFilipe Manana free_extent_state(cached_state); 973663dfbb0SFilipe Manana cached_state = NULL; 9741728366eSJosef Bacik cond_resched(); 9751728366eSJosef Bacik start = end + 1; 9767c4452b9SChris Mason } 977690587d1SChris Mason return werr; 978690587d1SChris Mason } 979690587d1SChris Mason 980690587d1SChris Mason /* 981690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 982690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 983690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 984690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 985690587d1SChris Mason */ 986690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 9878cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 988690587d1SChris Mason { 989690587d1SChris Mason int err = 0; 990690587d1SChris Mason int werr = 0; 9911728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 992e6138876SJosef Bacik struct extent_state *cached_state = NULL; 993690587d1SChris Mason u64 start = 0; 994690587d1SChris Mason u64 end; 995656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 996656f30dbSFilipe Manana bool errors = false; 997690587d1SChris Mason 9981728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 999e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 1000663dfbb0SFilipe Manana /* 1001663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 1002663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 1003663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 1004663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 1005663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 1006663dfbb0SFilipe Manana * it's safe to do it (through clear_btree_io_tree()). 1007663dfbb0SFilipe Manana */ 1008663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 1009663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 1010e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 1011663dfbb0SFilipe Manana if (err == -ENOMEM) 1012663dfbb0SFilipe Manana err = 0; 1013663dfbb0SFilipe Manana if (!err) 10141728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 1015777e6bd7SChris Mason if (err) 1016777e6bd7SChris Mason werr = err; 1017e38e2ed7SFilipe Manana free_extent_state(cached_state); 1018e38e2ed7SFilipe Manana cached_state = NULL; 1019777e6bd7SChris Mason cond_resched(); 10201728366eSJosef Bacik start = end + 1; 1021777e6bd7SChris Mason } 10227c4452b9SChris Mason if (err) 10237c4452b9SChris Mason werr = err; 1024656f30dbSFilipe Manana 1025656f30dbSFilipe Manana if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1026656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 1027656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, 1028656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1029656f30dbSFilipe Manana errors = true; 1030656f30dbSFilipe Manana 1031656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 1032656f30dbSFilipe Manana test_and_clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, 1033656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1034656f30dbSFilipe Manana errors = true; 1035656f30dbSFilipe Manana } else { 1036656f30dbSFilipe Manana if (test_and_clear_bit(BTRFS_INODE_BTREE_ERR, 1037656f30dbSFilipe Manana &btree_ino->runtime_flags)) 1038656f30dbSFilipe Manana errors = true; 1039656f30dbSFilipe Manana } 1040656f30dbSFilipe Manana 1041656f30dbSFilipe Manana if (errors && !werr) 1042656f30dbSFilipe Manana werr = -EIO; 1043656f30dbSFilipe Manana 10447c4452b9SChris Mason return werr; 104579154b1bSChris Mason } 104679154b1bSChris Mason 1047690587d1SChris Mason /* 1048690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 1049690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 1050690587d1SChris Mason * those extents are on disk for transaction or log commit 1051690587d1SChris Mason */ 1052171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 10538cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 1054690587d1SChris Mason { 1055690587d1SChris Mason int ret; 1056690587d1SChris Mason int ret2; 1057c6adc9ccSMiao Xie struct blk_plug plug; 1058690587d1SChris Mason 1059c6adc9ccSMiao Xie blk_start_plug(&plug); 10608cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1061c6adc9ccSMiao Xie blk_finish_plug(&plug); 10628cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1063bf0da8c1SChris Mason 1064bf0da8c1SChris Mason if (ret) 1065bf0da8c1SChris Mason return ret; 1066bf0da8c1SChris Mason if (ret2) 1067bf0da8c1SChris Mason return ret2; 1068bf0da8c1SChris Mason return 0; 1069690587d1SChris Mason } 1070690587d1SChris Mason 1071663dfbb0SFilipe Manana static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1072d0c803c4SChris Mason struct btrfs_root *root) 1073d0c803c4SChris Mason { 1074663dfbb0SFilipe Manana int ret; 1075663dfbb0SFilipe Manana 1076663dfbb0SFilipe Manana ret = btrfs_write_and_wait_marked_extents(root, 10778cef4e16SYan, Zheng &trans->transaction->dirty_pages, 10788cef4e16SYan, Zheng EXTENT_DIRTY); 1079663dfbb0SFilipe Manana clear_btree_io_tree(&trans->transaction->dirty_pages); 1080663dfbb0SFilipe Manana 1081663dfbb0SFilipe Manana return ret; 1082d0c803c4SChris Mason } 1083d0c803c4SChris Mason 1084d352ac68SChris Mason /* 1085d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1086d352ac68SChris Mason * 1087d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1088d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1089d352ac68SChris Mason * allocation tree. 1090d352ac68SChris Mason * 1091d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1092d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1093d352ac68SChris Mason */ 10940b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 109579154b1bSChris Mason struct btrfs_root *root) 109679154b1bSChris Mason { 109779154b1bSChris Mason int ret; 10980b86a832SChris Mason u64 old_root_bytenr; 109986b9f2ecSYan, Zheng u64 old_root_used; 11000b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 110179154b1bSChris Mason 110286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 110356bec294SChris Mason 110479154b1bSChris Mason while (1) { 11050b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 110686b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1107ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 110879154b1bSChris Mason break; 110987ef2bb4SChris Mason 11105d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 111179154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 11120b86a832SChris Mason &root->root_key, 11130b86a832SChris Mason &root->root_item); 111449b25e05SJeff Mahoney if (ret) 111549b25e05SJeff Mahoney return ret; 111656bec294SChris Mason 111786b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1118e7070be1SJosef Bacik } 1119276e680dSYan Zheng 11200b86a832SChris Mason return 0; 11210b86a832SChris Mason } 11220b86a832SChris Mason 1123d352ac68SChris Mason /* 1124d352ac68SChris Mason * update all the cowonly tree roots on disk 112549b25e05SJeff Mahoney * 112649b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 112749b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 112849b25e05SJeff Mahoney * to clean up the delayed refs. 1129d352ac68SChris Mason */ 11305d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 11310b86a832SChris Mason struct btrfs_root *root) 11320b86a832SChris Mason { 11330b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 1134ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11351bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11360b86a832SChris Mason struct list_head *next; 113784234f3aSYan Zheng struct extent_buffer *eb; 113856bec294SChris Mason int ret; 113984234f3aSYan Zheng 114084234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 114149b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 114249b25e05SJeff Mahoney 0, &eb); 114384234f3aSYan Zheng btrfs_tree_unlock(eb); 114484234f3aSYan Zheng free_extent_buffer(eb); 11450b86a832SChris Mason 114649b25e05SJeff Mahoney if (ret) 114749b25e05SJeff Mahoney return ret; 114849b25e05SJeff Mahoney 114956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 115049b25e05SJeff Mahoney if (ret) 115149b25e05SJeff Mahoney return ret; 115287ef2bb4SChris Mason 1153733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 1154c16ce190SJosef Bacik if (ret) 1155c16ce190SJosef Bacik return ret; 11568dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 1157c16ce190SJosef Bacik if (ret) 1158c16ce190SJosef Bacik return ret; 1159546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 1160c16ce190SJosef Bacik if (ret) 1161c16ce190SJosef Bacik return ret; 1162546adb0dSJan Schmidt 1163dcdf7f6dSJosef Bacik ret = btrfs_setup_space_cache(trans, root); 1164dcdf7f6dSJosef Bacik if (ret) 1165dcdf7f6dSJosef Bacik return ret; 1166dcdf7f6dSJosef Bacik 1167546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1168546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1169c16ce190SJosef Bacik if (ret) 1170c16ce190SJosef Bacik return ret; 1171ea526d18SJosef Bacik again: 11720b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11730b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11740b86a832SChris Mason list_del_init(next); 11750b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1176e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 117787ef2bb4SChris Mason 11789e351cc8SJosef Bacik if (root != fs_info->extent_root) 11799e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11809e351cc8SJosef Bacik &trans->transaction->switch_commits); 118149b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 118249b25e05SJeff Mahoney if (ret) 118349b25e05SJeff Mahoney return ret; 1184ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1185ea526d18SJosef Bacik if (ret) 1186ea526d18SJosef Bacik return ret; 118779154b1bSChris Mason } 1188276e680dSYan Zheng 11891bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1190ea526d18SJosef Bacik ret = btrfs_write_dirty_block_groups(trans, root); 1191ea526d18SJosef Bacik if (ret) 1192ea526d18SJosef Bacik return ret; 1193ea526d18SJosef Bacik ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1194ea526d18SJosef Bacik if (ret) 1195ea526d18SJosef Bacik return ret; 1196ea526d18SJosef Bacik } 1197ea526d18SJosef Bacik 1198ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1199ea526d18SJosef Bacik goto again; 1200ea526d18SJosef Bacik 12019e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 12029e351cc8SJosef Bacik &trans->transaction->switch_commits); 12038dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 12048dabb742SStefan Behrens 120579154b1bSChris Mason return 0; 120679154b1bSChris Mason } 120779154b1bSChris Mason 1208d352ac68SChris Mason /* 1209d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1210d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1211d352ac68SChris Mason * be deleted 1212d352ac68SChris Mason */ 1213cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 12145eda7b5eSChris Mason { 1215a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1216cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12179d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1218a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 12195eda7b5eSChris Mason } 12205eda7b5eSChris Mason 1221d352ac68SChris Mason /* 12225d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1223d352ac68SChris Mason */ 12245d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 12255d4f98a2SYan Zheng struct btrfs_root *root) 12260f7d52f4SChris Mason { 12270f7d52f4SChris Mason struct btrfs_root *gang[8]; 12285d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 12290f7d52f4SChris Mason int i; 12300f7d52f4SChris Mason int ret; 123154aa1f4dSChris Mason int err = 0; 123254aa1f4dSChris Mason 1233a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12340f7d52f4SChris Mason while (1) { 12355d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12365d4f98a2SYan Zheng (void **)gang, 0, 12370f7d52f4SChris Mason ARRAY_SIZE(gang), 12380f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12390f7d52f4SChris Mason if (ret == 0) 12400f7d52f4SChris Mason break; 12410f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12420f7d52f4SChris Mason root = gang[i]; 12435d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12442619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12450f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1246a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 124731153d81SYan Zheng 1248e02119d5SChris Mason btrfs_free_log(trans, root); 12495d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1250d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1251e02119d5SChris Mason 125282d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 125382d5902dSLi Zefan 1254f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 125527cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1256c7548af6SChris Mason smp_mb__after_atomic(); 1257f1ebcc74SLiu Bo 1258978d910dSYan Zheng if (root->commit_root != root->node) { 12599e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12609e351cc8SJosef Bacik &trans->transaction->switch_commits); 1261978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1262978d910dSYan Zheng root->node); 1263978d910dSYan Zheng } 126431153d81SYan Zheng 12655d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12660f7d52f4SChris Mason &root->root_key, 12670f7d52f4SChris Mason &root->root_item); 1268a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 126954aa1f4dSChris Mason if (err) 127054aa1f4dSChris Mason break; 12717174109cSQu Wenruo btrfs_qgroup_free_meta_all(root); 12720f7d52f4SChris Mason } 12739f3a7427SChris Mason } 1274a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 127554aa1f4dSChris Mason return err; 12760f7d52f4SChris Mason } 12770f7d52f4SChris Mason 1278d352ac68SChris Mason /* 1279de78b51aSEric Sandeen * defrag a given btree. 1280de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1281d352ac68SChris Mason */ 1282de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1283e9d0b13bSChris Mason { 1284e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1285e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12868929ecfaSYan, Zheng int ret; 1287e9d0b13bSChris Mason 128827cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1289e9d0b13bSChris Mason return 0; 12908929ecfaSYan, Zheng 12916b80053dSChris Mason while (1) { 12928929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12938929ecfaSYan, Zheng if (IS_ERR(trans)) 12948929ecfaSYan, Zheng return PTR_ERR(trans); 12958929ecfaSYan, Zheng 1296de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12978929ecfaSYan, Zheng 1298e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1299b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1300e9d0b13bSChris Mason cond_resched(); 1301e9d0b13bSChris Mason 13027841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1303e9d0b13bSChris Mason break; 1304210549ebSDavid Sterba 1305210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1306efe120a0SFrank Holton pr_debug("BTRFS: defrag_root cancelled\n"); 1307210549ebSDavid Sterba ret = -EAGAIN; 1308210549ebSDavid Sterba break; 1309210549ebSDavid Sterba } 1310e9d0b13bSChris Mason } 131127cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 13128929ecfaSYan, Zheng return ret; 1313e9d0b13bSChris Mason } 1314e9d0b13bSChris Mason 1315d352ac68SChris Mason /* 13166426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 13176426c7adSQu Wenruo * 13186426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 13196426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13206426c7adSQu Wenruo * qgroup works. 13216426c7adSQu Wenruo */ 13226426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13236426c7adSQu Wenruo struct btrfs_root *src, 13246426c7adSQu Wenruo struct btrfs_root *parent, 13256426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13266426c7adSQu Wenruo u64 dst_objectid) 13276426c7adSQu Wenruo { 13286426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13296426c7adSQu Wenruo int ret; 13306426c7adSQu Wenruo 13316426c7adSQu Wenruo /* 13326426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13336426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13346426c7adSQu Wenruo * kick in anyway. 13356426c7adSQu Wenruo */ 13366426c7adSQu Wenruo mutex_lock(&fs_info->qgroup_ioctl_lock); 13376426c7adSQu Wenruo if (!fs_info->quota_enabled) { 13386426c7adSQu Wenruo mutex_unlock(&fs_info->qgroup_ioctl_lock); 13396426c7adSQu Wenruo return 0; 13406426c7adSQu Wenruo } 13416426c7adSQu Wenruo mutex_unlock(&fs_info->qgroup_ioctl_lock); 13426426c7adSQu Wenruo 13436426c7adSQu Wenruo /* 13446426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13456426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13466426c7adSQu Wenruo */ 13476426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13486426c7adSQu Wenruo 13496426c7adSQu Wenruo ret = commit_fs_roots(trans, src); 13506426c7adSQu Wenruo if (ret) 13516426c7adSQu Wenruo goto out; 13526426c7adSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, fs_info); 13536426c7adSQu Wenruo if (ret < 0) 13546426c7adSQu Wenruo goto out; 13556426c7adSQu Wenruo ret = btrfs_qgroup_account_extents(trans, fs_info); 13566426c7adSQu Wenruo if (ret < 0) 13576426c7adSQu Wenruo goto out; 13586426c7adSQu Wenruo 13596426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 13606426c7adSQu Wenruo ret = btrfs_qgroup_inherit(trans, fs_info, 13616426c7adSQu Wenruo src->root_key.objectid, dst_objectid, 13626426c7adSQu Wenruo inherit); 13636426c7adSQu Wenruo if (ret < 0) 13646426c7adSQu Wenruo goto out; 13656426c7adSQu Wenruo 13666426c7adSQu Wenruo /* 13676426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13686426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13696426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13706426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13716426c7adSQu Wenruo * 2) write all btree blocks onto disk 13726426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13736426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13746426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13756426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13766426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13776426c7adSQu Wenruo */ 13786426c7adSQu Wenruo ret = commit_cowonly_roots(trans, src); 13796426c7adSQu Wenruo if (ret) 13806426c7adSQu Wenruo goto out; 13816426c7adSQu Wenruo switch_commit_roots(trans->transaction, fs_info); 13826426c7adSQu Wenruo ret = btrfs_write_and_wait_transaction(trans, src); 13836426c7adSQu Wenruo if (ret) 1384f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13856426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13866426c7adSQu Wenruo 13876426c7adSQu Wenruo out: 13886426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 13896426c7adSQu Wenruo 13906426c7adSQu Wenruo /* 13916426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 13926426c7adSQu Wenruo * last_trans == cur_transid. 13936426c7adSQu Wenruo * Or it won't be committed again onto disk after later 13946426c7adSQu Wenruo * insert_dir_item() 13956426c7adSQu Wenruo */ 13966426c7adSQu Wenruo if (!ret) 13976426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 13986426c7adSQu Wenruo return ret; 13996426c7adSQu Wenruo } 14006426c7adSQu Wenruo 14016426c7adSQu Wenruo /* 1402d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1403aec8030aSMiao Xie * transaction commit. This does the actual creation. 1404aec8030aSMiao Xie * 1405aec8030aSMiao Xie * Note: 1406aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1407aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1408aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1409d352ac68SChris Mason */ 141080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 14113063d29fSChris Mason struct btrfs_fs_info *fs_info, 14123063d29fSChris Mason struct btrfs_pending_snapshot *pending) 14133063d29fSChris Mason { 14143063d29fSChris Mason struct btrfs_key key; 141580b6794dSChris Mason struct btrfs_root_item *new_root_item; 14163063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 14173063d29fSChris Mason struct btrfs_root *root = pending->root; 14186bdb72deSSage Weil struct btrfs_root *parent_root; 141998c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14206bdb72deSSage Weil struct inode *parent_inode; 142142874b3dSMiao Xie struct btrfs_path *path; 142242874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1423a22285a6SYan, Zheng struct dentry *dentry; 14243063d29fSChris Mason struct extent_buffer *tmp; 1425925baeddSChris Mason struct extent_buffer *old; 142604b285f3SDeepa Dinamani struct timespec cur_time; 1427aec8030aSMiao Xie int ret = 0; 1428d68fc57bSYan, Zheng u64 to_reserve = 0; 14296bdb72deSSage Weil u64 index = 0; 1430a22285a6SYan, Zheng u64 objectid; 1431b83cc969SLi Zefan u64 root_flags; 14328ea05e3aSAlexander Block uuid_le new_uuid; 14333063d29fSChris Mason 14348546b570SDavid Sterba ASSERT(pending->path); 14358546b570SDavid Sterba path = pending->path; 143642874b3dSMiao Xie 1437b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1438b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1439a22285a6SYan, Zheng 1440aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1441aec8030aSMiao Xie if (pending->error) 14426fa9700eSMiao Xie goto no_free_objectid; 14433063d29fSChris Mason 1444d6726335SQu Wenruo /* 1445d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1446d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1447d6726335SQu Wenruo */ 1448d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1449d6726335SQu Wenruo 1450147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1451d68fc57bSYan, Zheng 1452d68fc57bSYan, Zheng if (to_reserve > 0) { 1453aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1454aec8030aSMiao Xie &pending->block_rsv, 145508e007d2SMiao Xie to_reserve, 145608e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1457aec8030aSMiao Xie if (pending->error) 1458d6726335SQu Wenruo goto clear_skip_qgroup; 1459d68fc57bSYan, Zheng } 1460d68fc57bSYan, Zheng 14613063d29fSChris Mason key.objectid = objectid; 1462a22285a6SYan, Zheng key.offset = (u64)-1; 1463a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14643063d29fSChris Mason 14656fa9700eSMiao Xie rsv = trans->block_rsv; 1466a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14672382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 146888d3a5aaSJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 146988d3a5aaSJosef Bacik trans->transid, 147088d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1471a22285a6SYan, Zheng dentry = pending->dentry; 1472e9662f70SMiao Xie parent_inode = pending->dir; 1473a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14746426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1475a22285a6SYan, Zheng 147604b285f3SDeepa Dinamani cur_time = current_fs_time(parent_inode->i_sb); 147704b285f3SDeepa Dinamani 14786bdb72deSSage Weil /* 14796bdb72deSSage Weil * insert the directory item 14806bdb72deSSage Weil */ 14816bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 148249b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 148342874b3dSMiao Xie 148442874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 148542874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 148642874b3dSMiao Xie btrfs_ino(parent_inode), 148742874b3dSMiao Xie dentry->d_name.name, 148842874b3dSMiao Xie dentry->d_name.len, 0); 148942874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1490fe66a05aSChris Mason pending->error = -EEXIST; 1491aec8030aSMiao Xie goto dir_item_existed; 149242874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 149342874b3dSMiao Xie ret = PTR_ERR(dir_item); 14948732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 14958732d44fSMiao Xie goto fail; 149679787eaaSJeff Mahoney } 149742874b3dSMiao Xie btrfs_release_path(path); 14986bdb72deSSage Weil 1499e999376fSChris Mason /* 1500e999376fSChris Mason * pull in the delayed directory update 1501e999376fSChris Mason * and the delayed inode item 1502e999376fSChris Mason * otherwise we corrupt the FS during 1503e999376fSChris Mason * snapshot 1504e999376fSChris Mason */ 1505e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 15068732d44fSMiao Xie if (ret) { /* Transaction aborted */ 15078732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15088732d44fSMiao Xie goto fail; 15098732d44fSMiao Xie } 1510e999376fSChris Mason 15116426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 15126bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 15136bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 151408fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 15156bdb72deSSage Weil 1516b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1517b83cc969SLi Zefan if (pending->readonly) 1518b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1519b83cc969SLi Zefan else 1520b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1521b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1522b83cc969SLi Zefan 15238ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15248ea05e3aSAlexander Block trans->transid); 15258ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15268ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15278ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15288ea05e3aSAlexander Block BTRFS_UUID_SIZE); 152970023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 153070023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 153170023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15328ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15338ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15348ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15358ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 153670023da2SStefan Behrens } 15373cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15383cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 153970023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15408ea05e3aSAlexander Block 1541925baeddSChris Mason old = btrfs_lock_root_node(root); 154249b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 154379787eaaSJeff Mahoney if (ret) { 154479787eaaSJeff Mahoney btrfs_tree_unlock(old); 154579787eaaSJeff Mahoney free_extent_buffer(old); 15468732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15478732d44fSMiao Xie goto fail; 154879787eaaSJeff Mahoney } 154949b25e05SJeff Mahoney 15505d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 15513063d29fSChris Mason 155249b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 155379787eaaSJeff Mahoney /* clean up in any case */ 1554925baeddSChris Mason btrfs_tree_unlock(old); 1555925baeddSChris Mason free_extent_buffer(old); 15568732d44fSMiao Xie if (ret) { 15578732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15588732d44fSMiao Xie goto fail; 15598732d44fSMiao Xie } 1560f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 156127cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1562f1ebcc74SLiu Bo smp_wmb(); 1563f1ebcc74SLiu Bo 15645d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1565a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1566a22285a6SYan, Zheng key.offset = trans->transid; 1567a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1568925baeddSChris Mason btrfs_tree_unlock(tmp); 15693063d29fSChris Mason free_extent_buffer(tmp); 15708732d44fSMiao Xie if (ret) { 15718732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15728732d44fSMiao Xie goto fail; 15738732d44fSMiao Xie } 15740660b5afSChris Mason 1575a22285a6SYan, Zheng /* 1576a22285a6SYan, Zheng * insert root back/forward references 1577a22285a6SYan, Zheng */ 1578a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1579a22285a6SYan, Zheng parent_root->root_key.objectid, 158033345d01SLi Zefan btrfs_ino(parent_inode), index, 1581a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15828732d44fSMiao Xie if (ret) { 15838732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15848732d44fSMiao Xie goto fail; 15858732d44fSMiao Xie } 1586a22285a6SYan, Zheng 1587a22285a6SYan, Zheng key.offset = (u64)-1; 1588a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 158979787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 159079787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 15918732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15928732d44fSMiao Xie goto fail; 159379787eaaSJeff Mahoney } 1594d68fc57bSYan, Zheng 159549b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15968732d44fSMiao Xie if (ret) { 15978732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 15988732d44fSMiao Xie goto fail; 15998732d44fSMiao Xie } 1600361048f5SMiao Xie 1601361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 16028732d44fSMiao Xie if (ret) { 16038732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 16048732d44fSMiao Xie goto fail; 16058732d44fSMiao Xie } 160642874b3dSMiao Xie 16076426c7adSQu Wenruo /* 16086426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 16096426c7adSQu Wenruo * snapshot hack to do fast snapshot. 16106426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 16116426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 16126426c7adSQu Wenruo */ 16136426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 16146426c7adSQu Wenruo pending->inherit, objectid); 16156426c7adSQu Wenruo if (ret < 0) 16166426c7adSQu Wenruo goto fail; 16176426c7adSQu Wenruo 161842874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 161942874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 162042874b3dSMiao Xie parent_inode, &key, 162142874b3dSMiao Xie BTRFS_FT_DIR, index); 162242874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16239c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16248732d44fSMiao Xie if (ret) { 16258732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 16268732d44fSMiao Xie goto fail; 16278732d44fSMiao Xie } 162842874b3dSMiao Xie 162942874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 163042874b3dSMiao Xie dentry->d_name.len * 2); 163104b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 163204b285f3SDeepa Dinamani current_fs_time(parent_inode->i_sb); 1633be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1634dd5f9615SStefan Behrens if (ret) { 16358732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1636dd5f9615SStefan Behrens goto fail; 1637dd5f9615SStefan Behrens } 1638dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1639dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1640dd5f9615SStefan Behrens if (ret) { 1641dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1642dd5f9615SStefan Behrens goto fail; 1643dd5f9615SStefan Behrens } 1644dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1645dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1646dd5f9615SStefan Behrens new_root_item->received_uuid, 1647dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1648dd5f9615SStefan Behrens objectid); 1649dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1650dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1651dd5f9615SStefan Behrens goto fail; 1652dd5f9615SStefan Behrens } 1653dd5f9615SStefan Behrens } 1654d6726335SQu Wenruo 1655d6726335SQu Wenruo ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1656d6726335SQu Wenruo if (ret) { 1657d6726335SQu Wenruo btrfs_abort_transaction(trans, root, ret); 1658d6726335SQu Wenruo goto fail; 1659d6726335SQu Wenruo } 1660d6726335SQu Wenruo 16613063d29fSChris Mason fail: 1662aec8030aSMiao Xie pending->error = ret; 1663aec8030aSMiao Xie dir_item_existed: 166498c9942aSLiu Bo trans->block_rsv = rsv; 16652382c5ccSLiu Bo trans->bytes_reserved = 0; 1666d6726335SQu Wenruo clear_skip_qgroup: 1667d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16686fa9700eSMiao Xie no_free_objectid: 16696fa9700eSMiao Xie kfree(new_root_item); 1670b0c0ea63SDavid Sterba pending->root_item = NULL; 167142874b3dSMiao Xie btrfs_free_path(path); 16728546b570SDavid Sterba pending->path = NULL; 16738546b570SDavid Sterba 167449b25e05SJeff Mahoney return ret; 16753063d29fSChris Mason } 16763063d29fSChris Mason 1677d352ac68SChris Mason /* 1678d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1679d352ac68SChris Mason */ 168080b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 16813063d29fSChris Mason struct btrfs_fs_info *fs_info) 16823063d29fSChris Mason { 1683aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16843063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1685aec8030aSMiao Xie int ret = 0; 16863de4586cSChris Mason 1687aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1688aec8030aSMiao Xie list_del(&pending->list); 1689aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1690aec8030aSMiao Xie if (ret) 1691aec8030aSMiao Xie break; 1692aec8030aSMiao Xie } 1693aec8030aSMiao Xie return ret; 16943de4586cSChris Mason } 16953de4586cSChris Mason 16965d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 16975d4f98a2SYan Zheng { 16985d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16995d4f98a2SYan Zheng struct btrfs_super_block *super; 17005d4f98a2SYan Zheng 17016c41761fSDavid Sterba super = root->fs_info->super_copy; 17025d4f98a2SYan Zheng 17035d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 17045d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 17055d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 17065d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 17075d4f98a2SYan Zheng 17085d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 17095d4f98a2SYan Zheng super->root = root_item->bytenr; 17105d4f98a2SYan Zheng super->generation = root_item->generation; 17115d4f98a2SYan Zheng super->root_level = root_item->level; 171273bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 17130af3d00bSJosef Bacik super->cache_generation = root_item->generation; 171470f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 171526432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 17165d4f98a2SYan Zheng } 17175d4f98a2SYan Zheng 1718f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1719f36f3042SChris Mason { 17204a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1721f36f3042SChris Mason int ret = 0; 17224a9d8bdeSMiao Xie 1723a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17244a9d8bdeSMiao Xie trans = info->running_transaction; 17254a9d8bdeSMiao Xie if (trans) 17264a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1727a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1728f36f3042SChris Mason return ret; 1729f36f3042SChris Mason } 1730f36f3042SChris Mason 17318929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17328929ecfaSYan, Zheng { 17334a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17348929ecfaSYan, Zheng int ret = 0; 17354a9d8bdeSMiao Xie 1736a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17374a9d8bdeSMiao Xie trans = info->running_transaction; 17384a9d8bdeSMiao Xie if (trans) 17394a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1740a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17418929ecfaSYan, Zheng return ret; 17428929ecfaSYan, Zheng } 17438929ecfaSYan, Zheng 1744bb9c12c9SSage Weil /* 1745bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1746bb9c12c9SSage Weil * transaction joins 1747bb9c12c9SSage Weil */ 1748bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1749bb9c12c9SSage Weil struct btrfs_transaction *trans) 1750bb9c12c9SSage Weil { 17514a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1752501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1753501407aaSJosef Bacik trans->aborted); 1754bb9c12c9SSage Weil } 1755bb9c12c9SSage Weil 1756bb9c12c9SSage Weil /* 1757bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1758bb9c12c9SSage Weil * caller holds ref. 1759bb9c12c9SSage Weil */ 1760bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1761bb9c12c9SSage Weil struct btrfs_transaction *trans) 1762bb9c12c9SSage Weil { 176372d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1764501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1765501407aaSJosef Bacik trans->aborted); 1766bb9c12c9SSage Weil } 1767bb9c12c9SSage Weil 1768bb9c12c9SSage Weil /* 1769bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1770bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1771bb9c12c9SSage Weil */ 1772bb9c12c9SSage Weil struct btrfs_async_commit { 1773bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1774bb9c12c9SSage Weil struct btrfs_root *root; 17757892b5afSMiao Xie struct work_struct work; 1776bb9c12c9SSage Weil }; 1777bb9c12c9SSage Weil 1778bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1779bb9c12c9SSage Weil { 1780bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17817892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1782bb9c12c9SSage Weil 17836fc4e354SSage Weil /* 17846fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17856fc4e354SSage Weil * Tell lockdep about it. 17866fc4e354SSage Weil */ 1787b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1788bee9182dSOleg Nesterov __sb_writers_acquired(ac->root->fs_info->sb, SB_FREEZE_FS); 17896fc4e354SSage Weil 1790e209db7aSSage Weil current->journal_info = ac->newtrans; 1791e209db7aSSage Weil 1792bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1793bb9c12c9SSage Weil kfree(ac); 1794bb9c12c9SSage Weil } 1795bb9c12c9SSage Weil 1796bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1797bb9c12c9SSage Weil struct btrfs_root *root, 1798bb9c12c9SSage Weil int wait_for_unblock) 1799bb9c12c9SSage Weil { 1800bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1801bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1802bb9c12c9SSage Weil 1803bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1804db5b493aSTsutomu Itoh if (!ac) 1805db5b493aSTsutomu Itoh return -ENOMEM; 1806bb9c12c9SSage Weil 18077892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1808bb9c12c9SSage Weil ac->root = root; 18097a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 18103612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 18113612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 18123612b495STsutomu Itoh kfree(ac); 18133612b495STsutomu Itoh return err; 18143612b495STsutomu Itoh } 1815bb9c12c9SSage Weil 1816bb9c12c9SSage Weil /* take transaction reference */ 1817bb9c12c9SSage Weil cur_trans = trans->transaction; 181813c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1819bb9c12c9SSage Weil 1820bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 18216fc4e354SSage Weil 18226fc4e354SSage Weil /* 18236fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 18246fc4e354SSage Weil * async commit thread will be the one to unlock it. 18256fc4e354SSage Weil */ 1826b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 1827bee9182dSOleg Nesterov __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 18286fc4e354SSage Weil 18297892b5afSMiao Xie schedule_work(&ac->work); 1830bb9c12c9SSage Weil 1831bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1832bb9c12c9SSage Weil if (wait_for_unblock) 1833bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1834bb9c12c9SSage Weil else 1835bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1836bb9c12c9SSage Weil 183738e88054SSage Weil if (current->journal_info == trans) 183838e88054SSage Weil current->journal_info = NULL; 183938e88054SSage Weil 1840724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1841bb9c12c9SSage Weil return 0; 1842bb9c12c9SSage Weil } 1843bb9c12c9SSage Weil 184449b25e05SJeff Mahoney 184549b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 18467b8b92afSJosef Bacik struct btrfs_root *root, int err) 184749b25e05SJeff Mahoney { 184849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1849f094ac32SLiu Bo DEFINE_WAIT(wait); 185049b25e05SJeff Mahoney 185149b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 185249b25e05SJeff Mahoney 18537b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 18547b8b92afSJosef Bacik 185549b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 185666b6135bSLiu Bo 185725d8c284SMiao Xie /* 185825d8c284SMiao Xie * If the transaction is removed from the list, it means this 185925d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 186025d8c284SMiao Xie * to call the cleanup function. 186125d8c284SMiao Xie */ 186225d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 186366b6135bSLiu Bo 186449b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1865d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 18664a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1867f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1868f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1869f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1870f094ac32SLiu Bo 1871f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1872d7096fc3SJosef Bacik } 187349b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 187449b25e05SJeff Mahoney 187549b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 187649b25e05SJeff Mahoney 18774a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 18784a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 18794a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 18804a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 18814a9d8bdeSMiao Xie 1882e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1883e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 1884724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1885724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 188649b25e05SJeff Mahoney 188749b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 188849b25e05SJeff Mahoney 188949b25e05SJeff Mahoney if (current->journal_info == trans) 189049b25e05SJeff Mahoney current->journal_info = NULL; 1891c0af8f0bSWang Shilong btrfs_scrub_cancel(root->fs_info); 189249b25e05SJeff Mahoney 189349b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 189449b25e05SJeff Mahoney } 189549b25e05SJeff Mahoney 189682436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 189782436617SMiao Xie { 189882436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 18996c255e67SMiao Xie return btrfs_start_delalloc_roots(fs_info, 1, -1); 190082436617SMiao Xie return 0; 190182436617SMiao Xie } 190282436617SMiao Xie 190382436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 190482436617SMiao Xie { 190582436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1906578def7cSFilipe Manana btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); 190782436617SMiao Xie } 190882436617SMiao Xie 190950d9aa99SJosef Bacik static inline void 1910161c3549SJosef Bacik btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans) 191150d9aa99SJosef Bacik { 1912161c3549SJosef Bacik wait_event(cur_trans->pending_wait, 1913161c3549SJosef Bacik atomic_read(&cur_trans->pending_ordered) == 0); 191450d9aa99SJosef Bacik } 191550d9aa99SJosef Bacik 191679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 191779154b1bSChris Mason struct btrfs_root *root) 191879154b1bSChris Mason { 191949b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19208fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 1921656f30dbSFilipe Manana struct btrfs_inode *btree_ino = BTRFS_I(root->fs_info->btree_inode); 192225287e0aSMiao Xie int ret; 192379154b1bSChris Mason 19248d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 19258d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 192625287e0aSMiao Xie ret = cur_trans->aborted; 1927e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1928e4a2bcacSJosef Bacik return ret; 192925287e0aSMiao Xie } 193049b25e05SJeff Mahoney 193156bec294SChris Mason /* make a pass through all the delayed refs we have so far 193256bec294SChris Mason * any runnings procs may add more while we are here 193356bec294SChris Mason */ 193456bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1935e4a2bcacSJosef Bacik if (ret) { 1936e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1937e4a2bcacSJosef Bacik return ret; 1938e4a2bcacSJosef Bacik } 193956bec294SChris Mason 19400e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 19410e721106SJosef Bacik trans->block_rsv = NULL; 19420e721106SJosef Bacik 1943b7ec40d7SChris Mason cur_trans = trans->transaction; 194449b25e05SJeff Mahoney 194556bec294SChris Mason /* 194656bec294SChris Mason * set the flushing flag so procs in this transaction have to 194756bec294SChris Mason * start sending their work down. 194856bec294SChris Mason */ 1949b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19501be41b78SJosef Bacik smp_wmb(); 195156bec294SChris Mason 1952ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1953ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1954ea658badSJosef Bacik 1955c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1956e4a2bcacSJosef Bacik if (ret) { 1957e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1958e4a2bcacSJosef Bacik return ret; 1959e4a2bcacSJosef Bacik } 196056bec294SChris Mason 19613204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19621bbc621eSChris Mason int run_it = 0; 19631bbc621eSChris Mason 19641bbc621eSChris Mason /* this mutex is also taken before trying to set 19651bbc621eSChris Mason * block groups readonly. We need to make sure 19661bbc621eSChris Mason * that nobody has set a block group readonly 19671bbc621eSChris Mason * after a extents from that block group have been 19681bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19691bbc621eSChris Mason * will wait for the transaction to commit if it 19703204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19711bbc621eSChris Mason * 19723204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19733204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19741bbc621eSChris Mason * hurt to have more than one go through, but there's no 19751bbc621eSChris Mason * real advantage to it either. 19761bbc621eSChris Mason */ 19771bbc621eSChris Mason mutex_lock(&root->fs_info->ro_block_group_mutex); 19783204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 19793204d33cSJosef Bacik &cur_trans->flags)) 19801bbc621eSChris Mason run_it = 1; 19811bbc621eSChris Mason mutex_unlock(&root->fs_info->ro_block_group_mutex); 19821bbc621eSChris Mason 19831bbc621eSChris Mason if (run_it) 19841bbc621eSChris Mason ret = btrfs_start_dirty_block_groups(trans, root); 19851bbc621eSChris Mason } 19861bbc621eSChris Mason if (ret) { 19871bbc621eSChris Mason btrfs_end_transaction(trans, root); 19881bbc621eSChris Mason return ret; 19891bbc621eSChris Mason } 19901bbc621eSChris Mason 19914a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 19924a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 19934a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 199413c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 199549b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1996ccd467d6SChris Mason 1997b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 199815ee9bc7SJosef Bacik 1999b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2000b4924a0fSLiu Bo ret = cur_trans->aborted; 2001b4924a0fSLiu Bo 2002724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 200315ee9bc7SJosef Bacik 200449b25e05SJeff Mahoney return ret; 200579154b1bSChris Mason } 20064313b399SChris Mason 20074a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 2008bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 2009bb9c12c9SSage Weil 2010ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 2011ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2012ccd467d6SChris Mason struct btrfs_transaction, list); 20134a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 201413c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 2015a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2016ccd467d6SChris Mason 2017ccd467d6SChris Mason wait_for_commit(root, prev_trans); 20181f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2019ccd467d6SChris Mason 2020724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20211f9b8c8fSFilipe Manana if (ret) 20221f9b8c8fSFilipe Manana goto cleanup_transaction; 2023a4abeea4SJosef Bacik } else { 2024a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2025ccd467d6SChris Mason } 2026a4abeea4SJosef Bacik } else { 2027a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2028ccd467d6SChris Mason } 202915ee9bc7SJosef Bacik 20300860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20310860adfdSMiao Xie 203282436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 203382436617SMiao Xie if (ret) 203482436617SMiao Xie goto cleanup_transaction; 203582436617SMiao Xie 20368d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 203749b25e05SJeff Mahoney if (ret) 203849b25e05SJeff Mahoney goto cleanup_transaction; 203916cdcec7SMiao Xie 2040581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2041581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2042ed3b3d31SChris Mason 2043581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 20448d875f95SChris Mason ret = btrfs_run_delayed_items(trans, root); 2045ca469637SMiao Xie if (ret) 2046ca469637SMiao Xie goto cleanup_transaction; 2047ca469637SMiao Xie 204882436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 2049cb7ab021SWang Shilong 2050161c3549SJosef Bacik btrfs_wait_pending_ordered(cur_trans); 205150d9aa99SJosef Bacik 2052cb7ab021SWang Shilong btrfs_scrub_pause(root); 2053ed0ca140SJosef Bacik /* 2054ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2055ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20564a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2057ed0ca140SJosef Bacik */ 2058a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 20594a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 2060a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2061ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2062ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 206315ee9bc7SJosef Bacik 20642cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 20652cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 20662cba30f1SMiao Xie ret = cur_trans->aborted; 20676cf7f77eSWang Shilong goto scrub_continue; 20682cba30f1SMiao Xie } 20697585717fSChris Mason /* 20707585717fSChris Mason * the reloc mutex makes sure that we stop 20717585717fSChris Mason * the balancing code from coming in and moving 20727585717fSChris Mason * extents around in the middle of the commit 20737585717fSChris Mason */ 20747585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 20757585717fSChris Mason 207642874b3dSMiao Xie /* 207742874b3dSMiao Xie * We needn't worry about the delayed items because we will 207842874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 207942874b3dSMiao Xie * core function of the snapshot creation. 208042874b3dSMiao Xie */ 208142874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 208249b25e05SJeff Mahoney if (ret) { 208349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 20846cf7f77eSWang Shilong goto scrub_continue; 208549b25e05SJeff Mahoney } 20863063d29fSChris Mason 208742874b3dSMiao Xie /* 208842874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 208942874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 209042874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 209142874b3dSMiao Xie * them. 209242874b3dSMiao Xie * 209342874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 209442874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 209542874b3dSMiao Xie * the nodes and leaves. 209642874b3dSMiao Xie */ 209742874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 209849b25e05SJeff Mahoney if (ret) { 209949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 21006cf7f77eSWang Shilong goto scrub_continue; 210149b25e05SJeff Mahoney } 210216cdcec7SMiao Xie 210356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 210449b25e05SJeff Mahoney if (ret) { 210549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 21066cf7f77eSWang Shilong goto scrub_continue; 210749b25e05SJeff Mahoney } 210856bec294SChris Mason 21090ed4792aSQu Wenruo /* Reocrd old roots for later qgroup accounting */ 21100ed4792aSQu Wenruo ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 21110ed4792aSQu Wenruo if (ret) { 21120ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 21130ed4792aSQu Wenruo goto scrub_continue; 21140ed4792aSQu Wenruo } 21150ed4792aSQu Wenruo 2116e999376fSChris Mason /* 2117e999376fSChris Mason * make sure none of the code above managed to slip in a 2118e999376fSChris Mason * delayed item 2119e999376fSChris Mason */ 2120e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 2121e999376fSChris Mason 21222c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2123dc17ff8fSChris Mason 2124e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2125e02119d5SChris Mason * various roots consistent with each other. Every pointer 2126e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2127e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2128e02119d5SChris Mason * the tree logging code from jumping in and changing any 2129e02119d5SChris Mason * of the trees. 2130e02119d5SChris Mason * 2131e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2132e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2133e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2134e02119d5SChris Mason * from now until after the super is written, we avoid races 2135e02119d5SChris Mason * with the tree-log code. 2136e02119d5SChris Mason */ 2137e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 21381a40e23bSZheng Yan 21395d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 214049b25e05SJeff Mahoney if (ret) { 214149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2142871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 21436cf7f77eSWang Shilong goto scrub_continue; 214449b25e05SJeff Mahoney } 214554aa1f4dSChris Mason 21463818aea2SQu Wenruo /* 21477e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21487e1876acSDavid Sterba * before the next transaction. 21493818aea2SQu Wenruo */ 2150572d9ab7SDavid Sterba btrfs_apply_pending_changes(root->fs_info); 21513818aea2SQu Wenruo 21525d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2153e02119d5SChris Mason * safe to free the root of tree log roots 2154e02119d5SChris Mason */ 2155e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 2156e02119d5SChris Mason 21570ed4792aSQu Wenruo /* 21580ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21590ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21600ed4792aSQu Wenruo */ 21610ed4792aSQu Wenruo ret = btrfs_qgroup_account_extents(trans, root->fs_info); 21620ed4792aSQu Wenruo if (ret < 0) { 21630ed4792aSQu Wenruo mutex_unlock(&root->fs_info->tree_log_mutex); 21640ed4792aSQu Wenruo mutex_unlock(&root->fs_info->reloc_mutex); 21650ed4792aSQu Wenruo goto scrub_continue; 21660ed4792aSQu Wenruo } 21670ed4792aSQu Wenruo 21685d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 216949b25e05SJeff Mahoney if (ret) { 217049b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 2171871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 21726cf7f77eSWang Shilong goto scrub_continue; 217349b25e05SJeff Mahoney } 217454aa1f4dSChris Mason 21752cba30f1SMiao Xie /* 21762cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 21772cba30f1SMiao Xie * update ->aborted, check it. 21782cba30f1SMiao Xie */ 21792cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 21802cba30f1SMiao Xie ret = cur_trans->aborted; 21812cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 21822cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 21836cf7f77eSWang Shilong goto scrub_continue; 21842cba30f1SMiao Xie } 21852cba30f1SMiao Xie 218611833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 218711833d66SYan Zheng 218878fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 21895f39d397SChris Mason 21905d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 21915d4f98a2SYan Zheng root->fs_info->tree_root->node); 21929e351cc8SJosef Bacik list_add_tail(&root->fs_info->tree_root->dirty_list, 21939e351cc8SJosef Bacik &cur_trans->switch_commits); 21945d4f98a2SYan Zheng 21955d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 21965d4f98a2SYan Zheng root->fs_info->chunk_root->node); 21979e351cc8SJosef Bacik list_add_tail(&root->fs_info->chunk_root->dirty_list, 21989e351cc8SJosef Bacik &cur_trans->switch_commits); 21999e351cc8SJosef Bacik 22009e351cc8SJosef Bacik switch_commit_roots(cur_trans, root->fs_info); 22015d4f98a2SYan Zheng 2202edf39272SJan Schmidt assert_qgroups_uptodate(trans); 2203ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22041bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22055d4f98a2SYan Zheng update_super_roots(root); 2206e02119d5SChris Mason 22076c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 22086c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 22096c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 22106c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 2211ccd467d6SChris Mason 2212935e5cc9SMiao Xie btrfs_update_commit_device_size(root->fs_info); 2213ce7213c7SMiao Xie btrfs_update_commit_device_bytes_used(root, cur_trans); 2214935e5cc9SMiao Xie 2215656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags); 2216656f30dbSFilipe Manana clear_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags); 2217656f30dbSFilipe Manana 22184fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22194fbcdf66SFilipe Manana 2220a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 22214a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 2222a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 2223a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 22247585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2225b7ec40d7SChris Mason 2226f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 2227e6dcd2dcSChris Mason 222879154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 222949b25e05SJeff Mahoney if (ret) { 223034d97007SAnand Jain btrfs_handle_fs_error(root->fs_info, ret, 223108748810SDavid Sterba "Error while writing out transaction"); 223249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 22336cf7f77eSWang Shilong goto scrub_continue; 223449b25e05SJeff Mahoney } 223549b25e05SJeff Mahoney 223649b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 223749b25e05SJeff Mahoney if (ret) { 223849b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 22396cf7f77eSWang Shilong goto scrub_continue; 224049b25e05SJeff Mahoney } 22414313b399SChris Mason 2242e02119d5SChris Mason /* 2243e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2244e02119d5SChris Mason * to go about their business 2245e02119d5SChris Mason */ 2246e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 2247e02119d5SChris Mason 224811833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 22494313b399SChris Mason 22503204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 225113212b54SZhao Lei btrfs_clear_space_info_full(root->fs_info); 225213212b54SZhao Lei 225315ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 22544a9d8bdeSMiao Xie /* 22554a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22564a9d8bdeSMiao Xie * which can change it. 22574a9d8bdeSMiao Xie */ 22584a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22592c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 22603de4586cSChris Mason 2261a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 226213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 2263a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 2264a4abeea4SJosef Bacik 2265724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2266724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 226758176a96SJosef Bacik 22680860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 2269b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 2270b2b5ef5cSJan Kara 22711abe9b8aSliubo trace_btrfs_transaction_commit(root); 22721abe9b8aSliubo 2273a2de733cSArne Jansen btrfs_scrub_continue(root); 2274a2de733cSArne Jansen 22759ed74f2dSJosef Bacik if (current->journal_info == trans) 22769ed74f2dSJosef Bacik current->journal_info = NULL; 22779ed74f2dSJosef Bacik 22782c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 227924bbcf04SYan, Zheng 22808a733013SZhao Lei if (current != root->fs_info->transaction_kthread && 22818a733013SZhao Lei current != root->fs_info->cleaner_kthread) 228224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 228324bbcf04SYan, Zheng 228479154b1bSChris Mason return ret; 228549b25e05SJeff Mahoney 22866cf7f77eSWang Shilong scrub_continue: 22876cf7f77eSWang Shilong btrfs_scrub_continue(root); 228849b25e05SJeff Mahoney cleanup_transaction: 22890e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 22904fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22910e721106SJosef Bacik trans->block_rsv = NULL; 2292c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 229349b25e05SJeff Mahoney if (current->journal_info == trans) 229449b25e05SJeff Mahoney current->journal_info = NULL; 22957b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 229649b25e05SJeff Mahoney 229749b25e05SJeff Mahoney return ret; 229879154b1bSChris Mason } 229979154b1bSChris Mason 2300d352ac68SChris Mason /* 23019d1a2a3aSDavid Sterba * return < 0 if error 23029d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23039d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23049d1a2a3aSDavid Sterba * 23059d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23069d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23079d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23089d1a2a3aSDavid Sterba * few seconds later. 2309d352ac68SChris Mason */ 23109d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2311e9d0b13bSChris Mason { 23129d1a2a3aSDavid Sterba int ret; 23135d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2314e9d0b13bSChris Mason 2315a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23169d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23179d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23189d1a2a3aSDavid Sterba return 0; 23199d1a2a3aSDavid Sterba } 23209d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23219d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2322cfad392bSJosef Bacik list_del_init(&root->root_list); 2323a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23245d4f98a2SYan Zheng 2325efe120a0SFrank Holton pr_debug("BTRFS: cleaner removing %llu\n", root->objectid); 232676dda93cSYan, Zheng 232716cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 232816cdcec7SMiao Xie 232976dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 233076dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23312c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 233276dda93cSYan, Zheng else 23332c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 233432471dc2SDavid Sterba 23356596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2336e9d0b13bSChris Mason } 2337572d9ab7SDavid Sterba 2338572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2339572d9ab7SDavid Sterba { 2340572d9ab7SDavid Sterba unsigned long prev; 2341572d9ab7SDavid Sterba unsigned long bit; 2342572d9ab7SDavid Sterba 23436c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2344572d9ab7SDavid Sterba if (!prev) 2345572d9ab7SDavid Sterba return; 2346572d9ab7SDavid Sterba 23477e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23487e1876acSDavid Sterba if (prev & bit) 23497e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23507e1876acSDavid Sterba prev &= ~bit; 23517e1876acSDavid Sterba 23527e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23537e1876acSDavid Sterba if (prev & bit) 23547e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23557e1876acSDavid Sterba prev &= ~bit; 23567e1876acSDavid Sterba 2357d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2358d51033d0SDavid Sterba if (prev & bit) 2359d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2360d51033d0SDavid Sterba prev &= ~bit; 2361d51033d0SDavid Sterba 2362572d9ab7SDavid Sterba if (prev) 2363572d9ab7SDavid Sterba btrfs_warn(fs_info, 2364572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2365572d9ab7SDavid Sterba } 2366