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" 3479154b1bSChris Mason 350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 360f7d52f4SChris Mason 374a9d8bdeSMiao Xie static unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 384a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 394a9d8bdeSMiao Xie [TRANS_STATE_BLOCKED] = (__TRANS_USERSPACE | 404a9d8bdeSMiao Xie __TRANS_START), 414a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_START] = (__TRANS_USERSPACE | 424a9d8bdeSMiao Xie __TRANS_START | 434a9d8bdeSMiao Xie __TRANS_ATTACH), 444a9d8bdeSMiao Xie [TRANS_STATE_COMMIT_DOING] = (__TRANS_USERSPACE | 454a9d8bdeSMiao Xie __TRANS_START | 464a9d8bdeSMiao Xie __TRANS_ATTACH | 474a9d8bdeSMiao Xie __TRANS_JOIN), 484a9d8bdeSMiao Xie [TRANS_STATE_UNBLOCKED] = (__TRANS_USERSPACE | 494a9d8bdeSMiao Xie __TRANS_START | 504a9d8bdeSMiao Xie __TRANS_ATTACH | 514a9d8bdeSMiao Xie __TRANS_JOIN | 524a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 534a9d8bdeSMiao Xie [TRANS_STATE_COMPLETED] = (__TRANS_USERSPACE | 544a9d8bdeSMiao Xie __TRANS_START | 554a9d8bdeSMiao Xie __TRANS_ATTACH | 564a9d8bdeSMiao Xie __TRANS_JOIN | 574a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 584a9d8bdeSMiao Xie }; 594a9d8bdeSMiao Xie 6048a3b636SEric Sandeen static void put_transaction(struct btrfs_transaction *transaction) 6179154b1bSChris Mason { 6213c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 6313c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 64a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 6500f04b88SArne Jansen WARN_ON(transaction->delayed_refs.root.rb_node); 666df9a95eSJosef Bacik while (!list_empty(&transaction->pending_chunks)) { 676df9a95eSJosef Bacik struct extent_map *em; 686df9a95eSJosef Bacik 696df9a95eSJosef Bacik em = list_first_entry(&transaction->pending_chunks, 706df9a95eSJosef Bacik struct extent_map, list); 716df9a95eSJosef Bacik list_del_init(&em->list); 726df9a95eSJosef Bacik free_extent_map(em); 736df9a95eSJosef Bacik } 742c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 7579154b1bSChris Mason } 7678fae27eSChris Mason } 7779154b1bSChris Mason 78817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root) 79817d52f8SJosef Bacik { 80817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 81817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 82817d52f8SJosef Bacik } 83817d52f8SJosef Bacik 840860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 850860adfdSMiao Xie unsigned int type) 860860adfdSMiao Xie { 870860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 880860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 890860adfdSMiao Xie } 900860adfdSMiao Xie 910860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 920860adfdSMiao Xie unsigned int type) 930860adfdSMiao Xie { 940860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 950860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 960860adfdSMiao Xie } 970860adfdSMiao Xie 980860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 990860adfdSMiao Xie unsigned int type) 1000860adfdSMiao Xie { 1010860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1020860adfdSMiao Xie } 1030860adfdSMiao Xie 1040860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1050860adfdSMiao Xie { 1060860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 107178260b2SMiao Xie } 108178260b2SMiao Xie 109d352ac68SChris Mason /* 110d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 111d352ac68SChris Mason */ 1120860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 11379154b1bSChris Mason { 11479154b1bSChris Mason struct btrfs_transaction *cur_trans; 11519ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 116a4abeea4SJosef Bacik 11719ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 118d43317dcSChris Mason loop: 11949b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 12087533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 12119ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 12249b25e05SJeff Mahoney return -EROFS; 12349b25e05SJeff Mahoney } 12449b25e05SJeff Mahoney 12519ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 126a4abeea4SJosef Bacik if (cur_trans) { 127871383beSDavid Sterba if (cur_trans->aborted) { 12819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 12949b25e05SJeff Mahoney return cur_trans->aborted; 130871383beSDavid Sterba } 1314a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 132178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 133178260b2SMiao Xie return -EBUSY; 134178260b2SMiao Xie } 135a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 136a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1370860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 13819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 139a4abeea4SJosef Bacik return 0; 140a4abeea4SJosef Bacik } 14119ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 142a4abeea4SJosef Bacik 143354aa0fbSMiao Xie /* 144354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 145354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 146354aa0fbSMiao Xie */ 147354aa0fbSMiao Xie if (type == TRANS_ATTACH) 148354aa0fbSMiao Xie return -ENOENT; 149354aa0fbSMiao Xie 1504a9d8bdeSMiao Xie /* 1514a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1524a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1534a9d8bdeSMiao Xie */ 1544a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1554a9d8bdeSMiao Xie 156a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 157db5b493aSTsutomu Itoh if (!cur_trans) 158db5b493aSTsutomu Itoh return -ENOMEM; 159d43317dcSChris Mason 16019ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 16119ae4e81SJan Schmidt if (fs_info->running_transaction) { 162d43317dcSChris Mason /* 163d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 1644a9d8bdeSMiao Xie * to redo the checks above 165d43317dcSChris Mason */ 166a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 167d43317dcSChris Mason goto loop; 16887533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 169e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 1707b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 1717b8b92afSJosef Bacik return -EROFS; 172a4abeea4SJosef Bacik } 173d43317dcSChris Mason 17413c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 1750860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 17679154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 17779154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 1784a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 179a4abeea4SJosef Bacik /* 180a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 181a4abeea4SJosef Bacik * commit the transaction. 182a4abeea4SJosef Bacik */ 183a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 18408607c1bSChris Mason cur_trans->start_time = get_seconds(); 18556bec294SChris Mason 1866bef4d31SEric Paris cur_trans->delayed_refs.root = RB_ROOT; 18756bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 188c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 189c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 19056bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 191c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 19220b297d6SJan Schmidt 19320b297d6SJan Schmidt /* 19420b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 19520b297d6SJan Schmidt * the log must never go across transaction boundaries. 19620b297d6SJan Schmidt */ 19720b297d6SJan Schmidt smp_mb(); 19831b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 19931b1a2bdSJulia Lawall WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when " 20020b297d6SJan Schmidt "creating a fresh transaction\n"); 20131b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 20231b1a2bdSJulia Lawall WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when " 20320b297d6SJan Schmidt "creating a fresh transaction\n"); 204fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 20520b297d6SJan Schmidt 20656bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 207bb721703SChris Mason atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0); 208bb721703SChris Mason atomic_set(&cur_trans->delayed_refs.ref_seq, 0); 209bb721703SChris Mason init_waitqueue_head(&cur_trans->delayed_refs.wait); 21056bec294SChris Mason 2113063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 212569e0f35SJosef Bacik INIT_LIST_HEAD(&cur_trans->ordered_operations); 2136df9a95eSJosef Bacik INIT_LIST_HEAD(&cur_trans->pending_chunks); 21419ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 215d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 21619ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 21719ae4e81SJan Schmidt fs_info->generation++; 21819ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 21919ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 22049b25e05SJeff Mahoney cur_trans->aborted = 0; 22119ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 22215ee9bc7SJosef Bacik 22379154b1bSChris Mason return 0; 22479154b1bSChris Mason } 22579154b1bSChris Mason 226d352ac68SChris Mason /* 227d397712bSChris Mason * this does all the record keeping required to make sure that a reference 228d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 229d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 230d397712bSChris Mason * when the transaction commits 231d352ac68SChris Mason */ 2327585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2335d4f98a2SYan Zheng struct btrfs_root *root) 2346702ed49SChris Mason { 2355d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 2366702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2375d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2385d4f98a2SYan Zheng 2397585717fSChris Mason /* 2407585717fSChris Mason * see below for in_trans_setup usage rules 2417585717fSChris Mason * we have the reloc mutex held now, so there 2427585717fSChris Mason * is only one writer in this function 2437585717fSChris Mason */ 2447585717fSChris Mason root->in_trans_setup = 1; 2457585717fSChris Mason 2467585717fSChris Mason /* make sure readers find in_trans_setup before 2477585717fSChris Mason * they find our root->last_trans update 2487585717fSChris Mason */ 2497585717fSChris Mason smp_wmb(); 2507585717fSChris Mason 251a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 252a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 253a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 254a4abeea4SJosef Bacik return 0; 255a4abeea4SJosef Bacik } 2566702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 2576702ed49SChris Mason (unsigned long)root->root_key.objectid, 2586702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 259a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 2607585717fSChris Mason root->last_trans = trans->transid; 2617585717fSChris Mason 2627585717fSChris Mason /* this is pretty tricky. We don't want to 2637585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 2647585717fSChris Mason * unless we're really doing the first setup for this root in 2657585717fSChris Mason * this transaction. 2667585717fSChris Mason * 2677585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 2687585717fSChris Mason * if we want to take the expensive mutex. 2697585717fSChris Mason * 2707585717fSChris Mason * But, we have to set root->last_trans before we 2717585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 2727585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 2737585717fSChris Mason * with root->in_trans_setup. When this is 1, we're still 2747585717fSChris Mason * fixing up the reloc trees and everyone must wait. 2757585717fSChris Mason * 2767585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 2777585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 2787585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 2797585717fSChris Mason * done before we pop in the zero below 2807585717fSChris Mason */ 2815d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 2827585717fSChris Mason smp_wmb(); 2837585717fSChris Mason root->in_trans_setup = 0; 2846702ed49SChris Mason } 2855d4f98a2SYan Zheng return 0; 2866702ed49SChris Mason } 2875d4f98a2SYan Zheng 2887585717fSChris Mason 2897585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 2907585717fSChris Mason struct btrfs_root *root) 2917585717fSChris Mason { 2927585717fSChris Mason if (!root->ref_cows) 2937585717fSChris Mason return 0; 2947585717fSChris Mason 2957585717fSChris Mason /* 2967585717fSChris Mason * see record_root_in_trans for comments about in_trans_setup usage 2977585717fSChris Mason * and barriers 2987585717fSChris Mason */ 2997585717fSChris Mason smp_rmb(); 3007585717fSChris Mason if (root->last_trans == trans->transid && 3017585717fSChris Mason !root->in_trans_setup) 3027585717fSChris Mason return 0; 3037585717fSChris Mason 3047585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 3057585717fSChris Mason record_root_in_trans(trans, root); 3067585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 3077585717fSChris Mason 3087585717fSChris Mason return 0; 3097585717fSChris Mason } 3107585717fSChris Mason 3114a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3124a9d8bdeSMiao Xie { 3134a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 314501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 315501407aaSJosef Bacik !trans->aborted); 3164a9d8bdeSMiao Xie } 3174a9d8bdeSMiao Xie 318d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 319d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 320d352ac68SChris Mason * transaction might not be fully on disk. 321d352ac68SChris Mason */ 32237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 32379154b1bSChris Mason { 324f9295749SChris Mason struct btrfs_transaction *cur_trans; 32579154b1bSChris Mason 326a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 327f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3284a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 32913c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 330a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 33172d63ed6SLi Zefan 33272d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 333501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 334501407aaSJosef Bacik cur_trans->aborted); 335f9295749SChris Mason put_transaction(cur_trans); 336a4abeea4SJosef Bacik } else { 337a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 338f9295749SChris Mason } 33937d1aeeeSChris Mason } 34037d1aeeeSChris Mason 341a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 34237d1aeeeSChris Mason { 343a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 344a4abeea4SJosef Bacik return 0; 345a4abeea4SJosef Bacik 346a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 347a22285a6SYan, Zheng return 1; 348a4abeea4SJosef Bacik 349a4abeea4SJosef Bacik if (type == TRANS_START && 350a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 351a4abeea4SJosef Bacik return 1; 352a4abeea4SJosef Bacik 353a22285a6SYan, Zheng return 0; 354a22285a6SYan, Zheng } 355a22285a6SYan, Zheng 35608e007d2SMiao Xie static struct btrfs_trans_handle * 3570860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 35808e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 359a22285a6SYan, Zheng { 360a22285a6SYan, Zheng struct btrfs_trans_handle *h; 361a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 362b5009945SJosef Bacik u64 num_bytes = 0; 363a22285a6SYan, Zheng int ret; 364c5567237SArne Jansen u64 qgroup_reserved = 0; 365acce952bSliubo 36687533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 367acce952bSliubo return ERR_PTR(-EROFS); 3682a1eb461SJosef Bacik 3692a1eb461SJosef Bacik if (current->journal_info) { 3700860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 3712a1eb461SJosef Bacik h = current->journal_info; 3722a1eb461SJosef Bacik h->use_count++; 373b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 3742a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 3752a1eb461SJosef Bacik h->block_rsv = NULL; 3762a1eb461SJosef Bacik goto got_it; 3772a1eb461SJosef Bacik } 378b5009945SJosef Bacik 379b5009945SJosef Bacik /* 380b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 381b5009945SJosef Bacik * the appropriate flushing if need be. 382b5009945SJosef Bacik */ 383b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 384c5567237SArne Jansen if (root->fs_info->quota_enabled && 385c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 386c5567237SArne Jansen qgroup_reserved = num_items * root->leafsize; 387c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 388c5567237SArne Jansen if (ret) 389c5567237SArne Jansen return ERR_PTR(ret); 390c5567237SArne Jansen } 391c5567237SArne Jansen 392b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 3934a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 394b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 39508e007d2SMiao Xie num_bytes, flush); 396b5009945SJosef Bacik if (ret) 397843fcf35SMiao Xie goto reserve_fail; 398b5009945SJosef Bacik } 399a22285a6SYan, Zheng again: 400a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 401843fcf35SMiao Xie if (!h) { 402843fcf35SMiao Xie ret = -ENOMEM; 403843fcf35SMiao Xie goto alloc_fail; 404843fcf35SMiao Xie } 405a22285a6SYan, Zheng 40698114659SJosef Bacik /* 40798114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 40898114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 40998114659SJosef Bacik * because we're already holding a ref. We need this because we could 41098114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 41198114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 412354aa0fbSMiao Xie * 413354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 414354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 41598114659SJosef Bacik */ 4160860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 417b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 418b2b5ef5cSJan Kara 419a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 42037d1aeeeSChris Mason wait_current_trans(root); 421a22285a6SYan, Zheng 422a4abeea4SJosef Bacik do { 423354aa0fbSMiao Xie ret = join_transaction(root, type); 424178260b2SMiao Xie if (ret == -EBUSY) { 425a4abeea4SJosef Bacik wait_current_trans(root); 426178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 427178260b2SMiao Xie ret = -ENOENT; 428178260b2SMiao Xie } 429a4abeea4SJosef Bacik } while (ret == -EBUSY); 430a4abeea4SJosef Bacik 431db5b493aSTsutomu Itoh if (ret < 0) { 432354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 433354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 434843fcf35SMiao Xie goto join_fail; 435db5b493aSTsutomu Itoh } 4360f7d52f4SChris Mason 437a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 438a22285a6SYan, Zheng 439a22285a6SYan, Zheng h->transid = cur_trans->transid; 440a22285a6SYan, Zheng h->transaction = cur_trans; 44179154b1bSChris Mason h->blocks_used = 0; 442a22285a6SYan, Zheng h->bytes_reserved = 0; 443d13603efSArne Jansen h->root = root; 44456bec294SChris Mason h->delayed_ref_updates = 0; 4452a1eb461SJosef Bacik h->use_count = 1; 4460e721106SJosef Bacik h->adding_csums = 0; 447f0486c68SYan, Zheng h->block_rsv = NULL; 4482a1eb461SJosef Bacik h->orig_rsv = NULL; 44949b25e05SJeff Mahoney h->aborted = 0; 4504b824906SMiao Xie h->qgroup_reserved = 0; 451bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 452a698d075SMiao Xie h->type = type; 453c6b305a8SJosef Bacik h->allocating_chunk = false; 454bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 455ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 456b7ec40d7SChris Mason 457a22285a6SYan, Zheng smp_mb(); 4584a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 4594a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 460a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 461a22285a6SYan, Zheng goto again; 462a22285a6SYan, Zheng } 4639ed74f2dSJosef Bacik 464b5009945SJosef Bacik if (num_bytes) { 4658c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 4662bcc0328SLiu Bo h->transid, num_bytes, 1); 467b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 468b5009945SJosef Bacik h->bytes_reserved = num_bytes; 469a22285a6SYan, Zheng } 4704b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 471a22285a6SYan, Zheng 4722a1eb461SJosef Bacik got_it: 473a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 474a22285a6SYan, Zheng 475a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 476a22285a6SYan, Zheng current->journal_info = h; 47779154b1bSChris Mason return h; 478843fcf35SMiao Xie 479843fcf35SMiao Xie join_fail: 4800860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 481843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 482843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 483843fcf35SMiao Xie alloc_fail: 484843fcf35SMiao Xie if (num_bytes) 485843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 486843fcf35SMiao Xie num_bytes); 487843fcf35SMiao Xie reserve_fail: 488843fcf35SMiao Xie if (qgroup_reserved) 489843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 490843fcf35SMiao Xie return ERR_PTR(ret); 49179154b1bSChris Mason } 49279154b1bSChris Mason 493f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 494a22285a6SYan, Zheng int num_items) 495f9295749SChris Mason { 49608e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 49708e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 498f9295749SChris Mason } 4998407aa46SMiao Xie 50008e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 5018407aa46SMiao Xie struct btrfs_root *root, int num_items) 5028407aa46SMiao Xie { 50308e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 50408e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 5058407aa46SMiao Xie } 5068407aa46SMiao Xie 5077a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 508f9295749SChris Mason { 5098407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 510f9295749SChris Mason } 511f9295749SChris Mason 5127a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5130af3d00bSJosef Bacik { 5148407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5150af3d00bSJosef Bacik } 5160af3d00bSJosef Bacik 5177a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5189ca9ee09SSage Weil { 5198407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5209ca9ee09SSage Weil } 5219ca9ee09SSage Weil 522d4edf39bSMiao Xie /* 523d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 524d4edf39bSMiao Xie * 525d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 526d4edf39bSMiao Xie * don't want to start a new one. 527d4edf39bSMiao Xie * 528d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 529d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 530d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 531d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 532d4edf39bSMiao Xie * invoke 533d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 534d4edf39bSMiao Xie */ 535354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 53660376ce4SJosef Bacik { 537354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 53860376ce4SJosef Bacik } 53960376ce4SJosef Bacik 540d4edf39bSMiao Xie /* 54190b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 542d4edf39bSMiao Xie * 543d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 544d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 545d4edf39bSMiao Xie * complete. 546d4edf39bSMiao Xie */ 547d4edf39bSMiao Xie struct btrfs_trans_handle * 548d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 549d4edf39bSMiao Xie { 550d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 551d4edf39bSMiao Xie 552d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 553d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 554d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 555d4edf39bSMiao Xie 556d4edf39bSMiao Xie return trans; 557d4edf39bSMiao Xie } 558d4edf39bSMiao Xie 559d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 560b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 56189ce8a63SChris Mason struct btrfs_transaction *commit) 56289ce8a63SChris Mason { 5634a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 56489ce8a63SChris Mason } 56589ce8a63SChris Mason 56646204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 56746204592SSage Weil { 56846204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 5698cd2807fSMiao Xie int ret = 0; 57046204592SSage Weil 57146204592SSage Weil if (transid) { 57246204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 573a4abeea4SJosef Bacik goto out; 57446204592SSage Weil 5758cd2807fSMiao Xie ret = -EINVAL; 57646204592SSage Weil /* find specified transaction */ 577a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 57846204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 57946204592SSage Weil if (t->transid == transid) { 58046204592SSage Weil cur_trans = t; 581a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 5828cd2807fSMiao Xie ret = 0; 58346204592SSage Weil break; 58446204592SSage Weil } 5858cd2807fSMiao Xie if (t->transid > transid) { 5868cd2807fSMiao Xie ret = 0; 58746204592SSage Weil break; 58846204592SSage Weil } 5898cd2807fSMiao Xie } 590a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 5918cd2807fSMiao Xie /* The specified transaction doesn't exist */ 59246204592SSage Weil if (!cur_trans) 5938cd2807fSMiao Xie goto out; 59446204592SSage Weil } else { 59546204592SSage Weil /* find newest transaction that is committing | committed */ 596a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 59746204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 59846204592SSage Weil list) { 5994a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 6004a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 6013473f3c0SJosef Bacik break; 60246204592SSage Weil cur_trans = t; 603a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 60446204592SSage Weil break; 60546204592SSage Weil } 60646204592SSage Weil } 607a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 60846204592SSage Weil if (!cur_trans) 609a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 61046204592SSage Weil } 61146204592SSage Weil 61246204592SSage Weil wait_for_commit(root, cur_trans); 61346204592SSage Weil put_transaction(cur_trans); 614a4abeea4SJosef Bacik out: 61546204592SSage Weil return ret; 61646204592SSage Weil } 61746204592SSage Weil 61837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 61937d1aeeeSChris Mason { 620a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 62137d1aeeeSChris Mason wait_current_trans(root); 62237d1aeeeSChris Mason } 62337d1aeeeSChris Mason 6248929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 6258929ecfaSYan, Zheng struct btrfs_root *root) 6268929ecfaSYan, Zheng { 6271be41b78SJosef Bacik if (root->fs_info->global_block_rsv.space_info->full && 6281be41b78SJosef Bacik btrfs_should_throttle_delayed_refs(trans, root)) 6291be41b78SJosef Bacik return 1; 63036ba022aSJosef Bacik 6311be41b78SJosef Bacik return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 6328929ecfaSYan, Zheng } 6338929ecfaSYan, Zheng 6348929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 6358929ecfaSYan, Zheng struct btrfs_root *root) 6368929ecfaSYan, Zheng { 6378929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 6388929ecfaSYan, Zheng int updates; 63949b25e05SJeff Mahoney int err; 6408929ecfaSYan, Zheng 641a4abeea4SJosef Bacik smp_mb(); 6424a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 6434a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 6448929ecfaSYan, Zheng return 1; 6458929ecfaSYan, Zheng 6468929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 6478929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 64849b25e05SJeff Mahoney if (updates) { 64949b25e05SJeff Mahoney err = btrfs_run_delayed_refs(trans, root, updates); 65049b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 65149b25e05SJeff Mahoney return err; 65249b25e05SJeff Mahoney } 6538929ecfaSYan, Zheng 6548929ecfaSYan, Zheng return should_end_transaction(trans, root); 6558929ecfaSYan, Zheng } 6568929ecfaSYan, Zheng 65789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 658a698d075SMiao Xie struct btrfs_root *root, int throttle) 65979154b1bSChris Mason { 6608929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 661ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 6621be41b78SJosef Bacik unsigned long cur = trans->delayed_ref_updates; 663a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 6644edc2ca3SDave Jones int err = 0; 665d6e4a428SChris Mason 6662a1eb461SJosef Bacik if (--trans->use_count) { 6672a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 6682a1eb461SJosef Bacik return 0; 6692a1eb461SJosef Bacik } 6702a1eb461SJosef Bacik 671edf39272SJan Schmidt /* 672edf39272SJan Schmidt * do the qgroup accounting as early as possible 673edf39272SJan Schmidt */ 674edf39272SJan Schmidt err = btrfs_delayed_refs_qgroup_accounting(trans, info); 675edf39272SJan Schmidt 676b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 6774c13d758SJosef Bacik trans->block_rsv = NULL; 678c5567237SArne Jansen 679c5567237SArne Jansen if (trans->qgroup_reserved) { 6807c2ec3f0SLiu Bo /* 6817c2ec3f0SLiu Bo * the same root has to be passed here between start_transaction 6827c2ec3f0SLiu Bo * and end_transaction. Subvolume quota depends on this. 6837c2ec3f0SLiu Bo */ 6847c2ec3f0SLiu Bo btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 685c5567237SArne Jansen trans->qgroup_reserved = 0; 686c5567237SArne Jansen } 687c5567237SArne Jansen 688ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 689ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 690ea658badSJosef Bacik 691c3e69d58SChris Mason trans->delayed_ref_updates = 0; 6921be41b78SJosef Bacik if (btrfs_should_throttle_delayed_refs(trans, root)) { 6931be41b78SJosef Bacik cur = max_t(unsigned long, cur, 1); 694c3e69d58SChris Mason trans->delayed_ref_updates = 0; 695c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 69656bec294SChris Mason } 697bb721703SChris Mason 6980e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 6990e721106SJosef Bacik trans->block_rsv = NULL; 70056bec294SChris Mason 701ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 702ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 703ea658badSJosef Bacik 704a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 7054a9d8bdeSMiao Xie should_end_transaction(trans, root) && 7064a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 7074a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 7084a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 7094a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 7104a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 711a4abeea4SJosef Bacik } 7128929ecfaSYan, Zheng 7134a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 71481317fdeSJosef Bacik if (throttle) { 71581317fdeSJosef Bacik /* 71681317fdeSJosef Bacik * We may race with somebody else here so end up having 71781317fdeSJosef Bacik * to call end_transaction on ourselves again, so inc 71881317fdeSJosef Bacik * our use_count. 71981317fdeSJosef Bacik */ 72081317fdeSJosef Bacik trans->use_count++; 7218929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 72281317fdeSJosef Bacik } else { 7238929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 7248929ecfaSYan, Zheng } 72581317fdeSJosef Bacik } 7268929ecfaSYan, Zheng 7270860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 7286df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 7296df7881aSJosef Bacik 7308929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 73113c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 73213c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 7330860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 73489ce8a63SChris Mason 73599d16cbcSSage Weil smp_mb(); 73679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 73779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 73879154b1bSChris Mason put_transaction(cur_trans); 7399ed74f2dSJosef Bacik 7409ed74f2dSJosef Bacik if (current->journal_info == trans) 7419ed74f2dSJosef Bacik current->journal_info = NULL; 742ab78c84dSChris Mason 74324bbcf04SYan, Zheng if (throttle) 74424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 74524bbcf04SYan, Zheng 74649b25e05SJeff Mahoney if (trans->aborted || 7474e121c06SJosef Bacik test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 7484e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 7494edc2ca3SDave Jones err = -EIO; 7504e121c06SJosef Bacik } 751edf39272SJan Schmidt assert_qgroups_uptodate(trans); 75249b25e05SJeff Mahoney 7534edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 7544edc2ca3SDave Jones return err; 75579154b1bSChris Mason } 75679154b1bSChris Mason 75789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 75889ce8a63SChris Mason struct btrfs_root *root) 75989ce8a63SChris Mason { 76098ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 76189ce8a63SChris Mason } 76289ce8a63SChris Mason 76389ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 76489ce8a63SChris Mason struct btrfs_root *root) 76589ce8a63SChris Mason { 76698ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 76716cdcec7SMiao Xie } 76816cdcec7SMiao Xie 76916cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans, 77016cdcec7SMiao Xie struct btrfs_root *root) 77116cdcec7SMiao Xie { 772a698d075SMiao Xie return __btrfs_end_transaction(trans, root, 1); 77389ce8a63SChris Mason } 77489ce8a63SChris Mason 775d352ac68SChris Mason /* 776d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 777d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 778690587d1SChris Mason * those extents are sent to disk but does not wait on them 779d352ac68SChris Mason */ 780690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 7818cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 78279154b1bSChris Mason { 783777e6bd7SChris Mason int err = 0; 7847c4452b9SChris Mason int werr = 0; 7851728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 786e6138876SJosef Bacik struct extent_state *cached_state = NULL; 787777e6bd7SChris Mason u64 start = 0; 7885f39d397SChris Mason u64 end; 7897c4452b9SChris Mason 7901728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 791e6138876SJosef Bacik mark, &cached_state)) { 792e6138876SJosef Bacik convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 793e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 794e6138876SJosef Bacik cached_state = NULL; 7951728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 7967c4452b9SChris Mason if (err) 7977c4452b9SChris Mason werr = err; 7981728366eSJosef Bacik cond_resched(); 7991728366eSJosef Bacik start = end + 1; 8007c4452b9SChris Mason } 801690587d1SChris Mason if (err) 802690587d1SChris Mason werr = err; 803690587d1SChris Mason return werr; 804690587d1SChris Mason } 805690587d1SChris Mason 806690587d1SChris Mason /* 807690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 808690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 809690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 810690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 811690587d1SChris Mason */ 812690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 8138cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 814690587d1SChris Mason { 815690587d1SChris Mason int err = 0; 816690587d1SChris Mason int werr = 0; 8171728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 818e6138876SJosef Bacik struct extent_state *cached_state = NULL; 819690587d1SChris Mason u64 start = 0; 820690587d1SChris Mason u64 end; 821690587d1SChris Mason 8221728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 823e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 824e6138876SJosef Bacik clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 825e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 8261728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 827777e6bd7SChris Mason if (err) 828777e6bd7SChris Mason werr = err; 829777e6bd7SChris Mason cond_resched(); 8301728366eSJosef Bacik start = end + 1; 831777e6bd7SChris Mason } 8327c4452b9SChris Mason if (err) 8337c4452b9SChris Mason werr = err; 8347c4452b9SChris Mason return werr; 83579154b1bSChris Mason } 83679154b1bSChris Mason 837690587d1SChris Mason /* 838690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 839690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 840690587d1SChris Mason * those extents are on disk for transaction or log commit 841690587d1SChris Mason */ 842171170c1SSergei Trofimovich static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 8438cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 844690587d1SChris Mason { 845690587d1SChris Mason int ret; 846690587d1SChris Mason int ret2; 847c6adc9ccSMiao Xie struct blk_plug plug; 848690587d1SChris Mason 849c6adc9ccSMiao Xie blk_start_plug(&plug); 8508cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 851c6adc9ccSMiao Xie blk_finish_plug(&plug); 8528cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 853bf0da8c1SChris Mason 854bf0da8c1SChris Mason if (ret) 855bf0da8c1SChris Mason return ret; 856bf0da8c1SChris Mason if (ret2) 857bf0da8c1SChris Mason return ret2; 858bf0da8c1SChris Mason return 0; 859690587d1SChris Mason } 860690587d1SChris Mason 861d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 862d0c803c4SChris Mason struct btrfs_root *root) 863d0c803c4SChris Mason { 864d0c803c4SChris Mason if (!trans || !trans->transaction) { 865d0c803c4SChris Mason struct inode *btree_inode; 866d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 867d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 868d0c803c4SChris Mason } 869d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 8708cef4e16SYan, Zheng &trans->transaction->dirty_pages, 8718cef4e16SYan, Zheng EXTENT_DIRTY); 872d0c803c4SChris Mason } 873d0c803c4SChris Mason 874d352ac68SChris Mason /* 875d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 876d352ac68SChris Mason * 877d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 878d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 879d352ac68SChris Mason * allocation tree. 880d352ac68SChris Mason * 881d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 882d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 883d352ac68SChris Mason */ 8840b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 88579154b1bSChris Mason struct btrfs_root *root) 88679154b1bSChris Mason { 88779154b1bSChris Mason int ret; 8880b86a832SChris Mason u64 old_root_bytenr; 88986b9f2ecSYan, Zheng u64 old_root_used; 8900b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 89179154b1bSChris Mason 89286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 8930b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 89456bec294SChris Mason 89579154b1bSChris Mason while (1) { 8960b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 89786b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 89886b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 89979154b1bSChris Mason break; 90087ef2bb4SChris Mason 9015d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 90279154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 9030b86a832SChris Mason &root->root_key, 9040b86a832SChris Mason &root->root_item); 90549b25e05SJeff Mahoney if (ret) 90649b25e05SJeff Mahoney return ret; 90756bec294SChris Mason 90886b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9094a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 91049b25e05SJeff Mahoney if (ret) 91149b25e05SJeff Mahoney return ret; 9120b86a832SChris Mason } 913276e680dSYan Zheng 914276e680dSYan Zheng if (root != root->fs_info->extent_root) 915817d52f8SJosef Bacik switch_commit_root(root); 916276e680dSYan Zheng 9170b86a832SChris Mason return 0; 9180b86a832SChris Mason } 9190b86a832SChris Mason 920d352ac68SChris Mason /* 921d352ac68SChris Mason * update all the cowonly tree roots on disk 92249b25e05SJeff Mahoney * 92349b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 92449b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 92549b25e05SJeff Mahoney * to clean up the delayed refs. 926d352ac68SChris Mason */ 9275d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 9280b86a832SChris Mason struct btrfs_root *root) 9290b86a832SChris Mason { 9300b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 9310b86a832SChris Mason struct list_head *next; 93284234f3aSYan Zheng struct extent_buffer *eb; 93356bec294SChris Mason int ret; 93484234f3aSYan Zheng 93556bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 93649b25e05SJeff Mahoney if (ret) 93749b25e05SJeff Mahoney return ret; 93887ef2bb4SChris Mason 93984234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 94049b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 94149b25e05SJeff Mahoney 0, &eb); 94284234f3aSYan Zheng btrfs_tree_unlock(eb); 94384234f3aSYan Zheng free_extent_buffer(eb); 9440b86a832SChris Mason 94549b25e05SJeff Mahoney if (ret) 94649b25e05SJeff Mahoney return ret; 94749b25e05SJeff Mahoney 94856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 94949b25e05SJeff Mahoney if (ret) 95049b25e05SJeff Mahoney return ret; 95187ef2bb4SChris Mason 952733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 953*c16ce190SJosef Bacik if (ret) 954*c16ce190SJosef Bacik return ret; 9558dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 956*c16ce190SJosef Bacik if (ret) 957*c16ce190SJosef Bacik return ret; 958546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 959*c16ce190SJosef Bacik if (ret) 960*c16ce190SJosef Bacik return ret; 961546adb0dSJan Schmidt 962546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 963546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 964*c16ce190SJosef Bacik if (ret) 965*c16ce190SJosef Bacik return ret; 966546adb0dSJan Schmidt 9670b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 9680b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 9690b86a832SChris Mason list_del_init(next); 9700b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 97187ef2bb4SChris Mason 97249b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 97349b25e05SJeff Mahoney if (ret) 97449b25e05SJeff Mahoney return ret; 97579154b1bSChris Mason } 976276e680dSYan Zheng 977276e680dSYan Zheng down_write(&fs_info->extent_commit_sem); 978276e680dSYan Zheng switch_commit_root(fs_info->extent_root); 979276e680dSYan Zheng up_write(&fs_info->extent_commit_sem); 980276e680dSYan Zheng 9818dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 9828dabb742SStefan Behrens 98379154b1bSChris Mason return 0; 98479154b1bSChris Mason } 98579154b1bSChris Mason 986d352ac68SChris Mason /* 987d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 988d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 989d352ac68SChris Mason * be deleted 990d352ac68SChris Mason */ 991cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 9925eda7b5eSChris Mason { 993a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 994cfad392bSJosef Bacik if (list_empty(&root->root_list)) 9959d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 996a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 9975eda7b5eSChris Mason } 9985eda7b5eSChris Mason 999d352ac68SChris Mason /* 10005d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1001d352ac68SChris Mason */ 10025d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 10035d4f98a2SYan Zheng struct btrfs_root *root) 10040f7d52f4SChris Mason { 10050f7d52f4SChris Mason struct btrfs_root *gang[8]; 10065d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 10070f7d52f4SChris Mason int i; 10080f7d52f4SChris Mason int ret; 100954aa1f4dSChris Mason int err = 0; 101054aa1f4dSChris Mason 1011a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 10120f7d52f4SChris Mason while (1) { 10135d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 10145d4f98a2SYan Zheng (void **)gang, 0, 10150f7d52f4SChris Mason ARRAY_SIZE(gang), 10160f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 10170f7d52f4SChris Mason if (ret == 0) 10180f7d52f4SChris Mason break; 10190f7d52f4SChris Mason for (i = 0; i < ret; i++) { 10200f7d52f4SChris Mason root = gang[i]; 10215d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 10222619ba1fSChris Mason (unsigned long)root->root_key.objectid, 10230f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1024a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 102531153d81SYan Zheng 1026e02119d5SChris Mason btrfs_free_log(trans, root); 10275d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1028d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1029e02119d5SChris Mason 103082d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 103182d5902dSLi Zefan 1032f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1033f1ebcc74SLiu Bo root->force_cow = 0; 1034f1ebcc74SLiu Bo smp_wmb(); 1035f1ebcc74SLiu Bo 1036978d910dSYan Zheng if (root->commit_root != root->node) { 1037581bb050SLi Zefan mutex_lock(&root->fs_commit_mutex); 1038817d52f8SJosef Bacik switch_commit_root(root); 1039581bb050SLi Zefan btrfs_unpin_free_ino(root); 1040581bb050SLi Zefan mutex_unlock(&root->fs_commit_mutex); 1041581bb050SLi Zefan 1042978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1043978d910dSYan Zheng root->node); 1044978d910dSYan Zheng } 104531153d81SYan Zheng 10465d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 10470f7d52f4SChris Mason &root->root_key, 10480f7d52f4SChris Mason &root->root_item); 1049a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 105054aa1f4dSChris Mason if (err) 105154aa1f4dSChris Mason break; 10520f7d52f4SChris Mason } 10539f3a7427SChris Mason } 1054a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 105554aa1f4dSChris Mason return err; 10560f7d52f4SChris Mason } 10570f7d52f4SChris Mason 1058d352ac68SChris Mason /* 1059de78b51aSEric Sandeen * defrag a given btree. 1060de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1061d352ac68SChris Mason */ 1062de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1063e9d0b13bSChris Mason { 1064e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1065e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 10668929ecfaSYan, Zheng int ret; 1067e9d0b13bSChris Mason 10688929ecfaSYan, Zheng if (xchg(&root->defrag_running, 1)) 1069e9d0b13bSChris Mason return 0; 10708929ecfaSYan, Zheng 10716b80053dSChris Mason while (1) { 10728929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 10738929ecfaSYan, Zheng if (IS_ERR(trans)) 10748929ecfaSYan, Zheng return PTR_ERR(trans); 10758929ecfaSYan, Zheng 1076de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 10778929ecfaSYan, Zheng 1078e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1079b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1080e9d0b13bSChris Mason cond_resched(); 1081e9d0b13bSChris Mason 10827841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1083e9d0b13bSChris Mason break; 1084210549ebSDavid Sterba 1085210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1086210549ebSDavid Sterba printk(KERN_DEBUG "btrfs: defrag_root cancelled\n"); 1087210549ebSDavid Sterba ret = -EAGAIN; 1088210549ebSDavid Sterba break; 1089210549ebSDavid Sterba } 1090e9d0b13bSChris Mason } 1091e9d0b13bSChris Mason root->defrag_running = 0; 10928929ecfaSYan, Zheng return ret; 1093e9d0b13bSChris Mason } 1094e9d0b13bSChris Mason 1095d352ac68SChris Mason /* 1096d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1097aec8030aSMiao Xie * transaction commit. This does the actual creation. 1098aec8030aSMiao Xie * 1099aec8030aSMiao Xie * Note: 1100aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1101aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1102aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1103d352ac68SChris Mason */ 110480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 11053063d29fSChris Mason struct btrfs_fs_info *fs_info, 11063063d29fSChris Mason struct btrfs_pending_snapshot *pending) 11073063d29fSChris Mason { 11083063d29fSChris Mason struct btrfs_key key; 110980b6794dSChris Mason struct btrfs_root_item *new_root_item; 11103063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 11113063d29fSChris Mason struct btrfs_root *root = pending->root; 11126bdb72deSSage Weil struct btrfs_root *parent_root; 111398c9942aSLiu Bo struct btrfs_block_rsv *rsv; 11146bdb72deSSage Weil struct inode *parent_inode; 111542874b3dSMiao Xie struct btrfs_path *path; 111642874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1117a22285a6SYan, Zheng struct dentry *dentry; 11183063d29fSChris Mason struct extent_buffer *tmp; 1119925baeddSChris Mason struct extent_buffer *old; 11208ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1121aec8030aSMiao Xie int ret = 0; 1122d68fc57bSYan, Zheng u64 to_reserve = 0; 11236bdb72deSSage Weil u64 index = 0; 1124a22285a6SYan, Zheng u64 objectid; 1125b83cc969SLi Zefan u64 root_flags; 11268ea05e3aSAlexander Block uuid_le new_uuid; 11273063d29fSChris Mason 112842874b3dSMiao Xie path = btrfs_alloc_path(); 112942874b3dSMiao Xie if (!path) { 1130aec8030aSMiao Xie pending->error = -ENOMEM; 1131aec8030aSMiao Xie return 0; 113242874b3dSMiao Xie } 113342874b3dSMiao Xie 113480b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 113580b6794dSChris Mason if (!new_root_item) { 1136aec8030aSMiao Xie pending->error = -ENOMEM; 11376fa9700eSMiao Xie goto root_item_alloc_fail; 113880b6794dSChris Mason } 1139a22285a6SYan, Zheng 1140aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1141aec8030aSMiao Xie if (pending->error) 11426fa9700eSMiao Xie goto no_free_objectid; 11433063d29fSChris Mason 11443fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1145d68fc57bSYan, Zheng 1146d68fc57bSYan, Zheng if (to_reserve > 0) { 1147aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1148aec8030aSMiao Xie &pending->block_rsv, 114908e007d2SMiao Xie to_reserve, 115008e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1151aec8030aSMiao Xie if (pending->error) 11526fa9700eSMiao Xie goto no_free_objectid; 1153d68fc57bSYan, Zheng } 1154d68fc57bSYan, Zheng 1155aec8030aSMiao Xie pending->error = btrfs_qgroup_inherit(trans, fs_info, 1156aec8030aSMiao Xie root->root_key.objectid, 11576f72c7e2SArne Jansen objectid, pending->inherit); 1158aec8030aSMiao Xie if (pending->error) 11596fa9700eSMiao Xie goto no_free_objectid; 11606f72c7e2SArne Jansen 11613063d29fSChris Mason key.objectid = objectid; 1162a22285a6SYan, Zheng key.offset = (u64)-1; 1163a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 11643063d29fSChris Mason 11656fa9700eSMiao Xie rsv = trans->block_rsv; 1166a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 11672382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 11686bdb72deSSage Weil 1169a22285a6SYan, Zheng dentry = pending->dentry; 1170e9662f70SMiao Xie parent_inode = pending->dir; 1171a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 11727585717fSChris Mason record_root_in_trans(trans, parent_root); 1173a22285a6SYan, Zheng 11746bdb72deSSage Weil /* 11756bdb72deSSage Weil * insert the directory item 11766bdb72deSSage Weil */ 11776bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 117849b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 117942874b3dSMiao Xie 118042874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 118142874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 118242874b3dSMiao Xie btrfs_ino(parent_inode), 118342874b3dSMiao Xie dentry->d_name.name, 118442874b3dSMiao Xie dentry->d_name.len, 0); 118542874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1186fe66a05aSChris Mason pending->error = -EEXIST; 1187aec8030aSMiao Xie goto dir_item_existed; 118842874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 118942874b3dSMiao Xie ret = PTR_ERR(dir_item); 11908732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 11918732d44fSMiao Xie goto fail; 119279787eaaSJeff Mahoney } 119342874b3dSMiao Xie btrfs_release_path(path); 11946bdb72deSSage Weil 1195e999376fSChris Mason /* 1196e999376fSChris Mason * pull in the delayed directory update 1197e999376fSChris Mason * and the delayed inode item 1198e999376fSChris Mason * otherwise we corrupt the FS during 1199e999376fSChris Mason * snapshot 1200e999376fSChris Mason */ 1201e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 12028732d44fSMiao Xie if (ret) { /* Transaction aborted */ 12038732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12048732d44fSMiao Xie goto fail; 12058732d44fSMiao Xie } 1206e999376fSChris Mason 12077585717fSChris Mason record_root_in_trans(trans, root); 12086bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 12096bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 121008fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 12116bdb72deSSage Weil 1212b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1213b83cc969SLi Zefan if (pending->readonly) 1214b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1215b83cc969SLi Zefan else 1216b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1217b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1218b83cc969SLi Zefan 12198ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 12208ea05e3aSAlexander Block trans->transid); 12218ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 12228ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 12238ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 12248ea05e3aSAlexander Block BTRFS_UUID_SIZE); 122570023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 122670023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 122770023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 12288ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 12298ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 12308ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 12318ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 123270023da2SStefan Behrens } 12333cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 12343cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 123570023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 12368ea05e3aSAlexander Block 1237925baeddSChris Mason old = btrfs_lock_root_node(root); 123849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 123979787eaaSJeff Mahoney if (ret) { 124079787eaaSJeff Mahoney btrfs_tree_unlock(old); 124179787eaaSJeff Mahoney free_extent_buffer(old); 12428732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12438732d44fSMiao Xie goto fail; 124479787eaaSJeff Mahoney } 124549b25e05SJeff Mahoney 12465d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 12473063d29fSChris Mason 124849b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 124979787eaaSJeff Mahoney /* clean up in any case */ 1250925baeddSChris Mason btrfs_tree_unlock(old); 1251925baeddSChris Mason free_extent_buffer(old); 12528732d44fSMiao Xie if (ret) { 12538732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12548732d44fSMiao Xie goto fail; 12558732d44fSMiao Xie } 12563063d29fSChris Mason 1257f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1258f1ebcc74SLiu Bo root->force_cow = 1; 1259f1ebcc74SLiu Bo smp_wmb(); 1260f1ebcc74SLiu Bo 12615d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1262a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1263a22285a6SYan, Zheng key.offset = trans->transid; 1264a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1265925baeddSChris Mason btrfs_tree_unlock(tmp); 12663063d29fSChris Mason free_extent_buffer(tmp); 12678732d44fSMiao Xie if (ret) { 12688732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12698732d44fSMiao Xie goto fail; 12708732d44fSMiao Xie } 12710660b5afSChris Mason 1272a22285a6SYan, Zheng /* 1273a22285a6SYan, Zheng * insert root back/forward references 1274a22285a6SYan, Zheng */ 1275a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1276a22285a6SYan, Zheng parent_root->root_key.objectid, 127733345d01SLi Zefan btrfs_ino(parent_inode), index, 1278a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 12798732d44fSMiao Xie if (ret) { 12808732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12818732d44fSMiao Xie goto fail; 12828732d44fSMiao Xie } 1283a22285a6SYan, Zheng 1284a22285a6SYan, Zheng key.offset = (u64)-1; 1285a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 128679787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 128779787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 12888732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12898732d44fSMiao Xie goto fail; 129079787eaaSJeff Mahoney } 1291d68fc57bSYan, Zheng 129249b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 12938732d44fSMiao Xie if (ret) { 12948732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12958732d44fSMiao Xie goto fail; 12968732d44fSMiao Xie } 1297361048f5SMiao Xie 1298361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 12998732d44fSMiao Xie if (ret) { 13008732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13018732d44fSMiao Xie goto fail; 13028732d44fSMiao Xie } 130342874b3dSMiao Xie 130442874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 130542874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 130642874b3dSMiao Xie parent_inode, &key, 130742874b3dSMiao Xie BTRFS_FT_DIR, index); 130842874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 13099c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 13108732d44fSMiao Xie if (ret) { 13118732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13128732d44fSMiao Xie goto fail; 13138732d44fSMiao Xie } 131442874b3dSMiao Xie 131542874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 131642874b3dSMiao Xie dentry->d_name.len * 2); 131742874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1318be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1319dd5f9615SStefan Behrens if (ret) { 13208732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 1321dd5f9615SStefan Behrens goto fail; 1322dd5f9615SStefan Behrens } 1323dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, new_uuid.b, 1324dd5f9615SStefan Behrens BTRFS_UUID_KEY_SUBVOL, objectid); 1325dd5f9615SStefan Behrens if (ret) { 1326dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1327dd5f9615SStefan Behrens goto fail; 1328dd5f9615SStefan Behrens } 1329dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1330dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, 1331dd5f9615SStefan Behrens new_root_item->received_uuid, 1332dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1333dd5f9615SStefan Behrens objectid); 1334dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 1335dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 1336dd5f9615SStefan Behrens goto fail; 1337dd5f9615SStefan Behrens } 1338dd5f9615SStefan Behrens } 13393063d29fSChris Mason fail: 1340aec8030aSMiao Xie pending->error = ret; 1341aec8030aSMiao Xie dir_item_existed: 134298c9942aSLiu Bo trans->block_rsv = rsv; 13432382c5ccSLiu Bo trans->bytes_reserved = 0; 13446fa9700eSMiao Xie no_free_objectid: 13456fa9700eSMiao Xie kfree(new_root_item); 13466fa9700eSMiao Xie root_item_alloc_fail: 134742874b3dSMiao Xie btrfs_free_path(path); 134849b25e05SJeff Mahoney return ret; 13493063d29fSChris Mason } 13503063d29fSChris Mason 1351d352ac68SChris Mason /* 1352d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1353d352ac68SChris Mason */ 135480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 13553063d29fSChris Mason struct btrfs_fs_info *fs_info) 13563063d29fSChris Mason { 1357aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 13583063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1359aec8030aSMiao Xie int ret = 0; 13603de4586cSChris Mason 1361aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1362aec8030aSMiao Xie list_del(&pending->list); 1363aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1364aec8030aSMiao Xie if (ret) 1365aec8030aSMiao Xie break; 1366aec8030aSMiao Xie } 1367aec8030aSMiao Xie return ret; 13683de4586cSChris Mason } 13693de4586cSChris Mason 13705d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 13715d4f98a2SYan Zheng { 13725d4f98a2SYan Zheng struct btrfs_root_item *root_item; 13735d4f98a2SYan Zheng struct btrfs_super_block *super; 13745d4f98a2SYan Zheng 13756c41761fSDavid Sterba super = root->fs_info->super_copy; 13765d4f98a2SYan Zheng 13775d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 13785d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 13795d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 13805d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 13815d4f98a2SYan Zheng 13825d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 13835d4f98a2SYan Zheng super->root = root_item->bytenr; 13845d4f98a2SYan Zheng super->generation = root_item->generation; 13855d4f98a2SYan Zheng super->root_level = root_item->level; 138673bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 13870af3d00bSJosef Bacik super->cache_generation = root_item->generation; 138870f80175SStefan Behrens if (root->fs_info->update_uuid_tree_gen) 138926432799SStefan Behrens super->uuid_tree_generation = root_item->generation; 13905d4f98a2SYan Zheng } 13915d4f98a2SYan Zheng 1392f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1393f36f3042SChris Mason { 13944a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1395f36f3042SChris Mason int ret = 0; 13964a9d8bdeSMiao Xie 1397a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 13984a9d8bdeSMiao Xie trans = info->running_transaction; 13994a9d8bdeSMiao Xie if (trans) 14004a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1401a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1402f36f3042SChris Mason return ret; 1403f36f3042SChris Mason } 1404f36f3042SChris Mason 14058929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 14068929ecfaSYan, Zheng { 14074a9d8bdeSMiao Xie struct btrfs_transaction *trans; 14088929ecfaSYan, Zheng int ret = 0; 14094a9d8bdeSMiao Xie 1410a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 14114a9d8bdeSMiao Xie trans = info->running_transaction; 14124a9d8bdeSMiao Xie if (trans) 14134a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1414a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 14158929ecfaSYan, Zheng return ret; 14168929ecfaSYan, Zheng } 14178929ecfaSYan, Zheng 1418bb9c12c9SSage Weil /* 1419bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1420bb9c12c9SSage Weil * transaction joins 1421bb9c12c9SSage Weil */ 1422bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1423bb9c12c9SSage Weil struct btrfs_transaction *trans) 1424bb9c12c9SSage Weil { 14254a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1426501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1427501407aaSJosef Bacik trans->aborted); 1428bb9c12c9SSage Weil } 1429bb9c12c9SSage Weil 1430bb9c12c9SSage Weil /* 1431bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1432bb9c12c9SSage Weil * caller holds ref. 1433bb9c12c9SSage Weil */ 1434bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1435bb9c12c9SSage Weil struct btrfs_transaction *trans) 1436bb9c12c9SSage Weil { 143772d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1438501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1439501407aaSJosef Bacik trans->aborted); 1440bb9c12c9SSage Weil } 1441bb9c12c9SSage Weil 1442bb9c12c9SSage Weil /* 1443bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1444bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1445bb9c12c9SSage Weil */ 1446bb9c12c9SSage Weil struct btrfs_async_commit { 1447bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1448bb9c12c9SSage Weil struct btrfs_root *root; 14497892b5afSMiao Xie struct work_struct work; 1450bb9c12c9SSage Weil }; 1451bb9c12c9SSage Weil 1452bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1453bb9c12c9SSage Weil { 1454bb9c12c9SSage Weil struct btrfs_async_commit *ac = 14557892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1456bb9c12c9SSage Weil 14576fc4e354SSage Weil /* 14586fc4e354SSage Weil * We've got freeze protection passed with the transaction. 14596fc4e354SSage Weil * Tell lockdep about it. 14606fc4e354SSage Weil */ 1461ff7c1d33SMiao Xie if (ac->newtrans->type < TRANS_JOIN_NOLOCK) 14626fc4e354SSage Weil rwsem_acquire_read( 14636fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 14646fc4e354SSage Weil 0, 1, _THIS_IP_); 14656fc4e354SSage Weil 1466e209db7aSSage Weil current->journal_info = ac->newtrans; 1467e209db7aSSage Weil 1468bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1469bb9c12c9SSage Weil kfree(ac); 1470bb9c12c9SSage Weil } 1471bb9c12c9SSage Weil 1472bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1473bb9c12c9SSage Weil struct btrfs_root *root, 1474bb9c12c9SSage Weil int wait_for_unblock) 1475bb9c12c9SSage Weil { 1476bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1477bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1478bb9c12c9SSage Weil 1479bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1480db5b493aSTsutomu Itoh if (!ac) 1481db5b493aSTsutomu Itoh return -ENOMEM; 1482bb9c12c9SSage Weil 14837892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1484bb9c12c9SSage Weil ac->root = root; 14857a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 14863612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 14873612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 14883612b495STsutomu Itoh kfree(ac); 14893612b495STsutomu Itoh return err; 14903612b495STsutomu Itoh } 1491bb9c12c9SSage Weil 1492bb9c12c9SSage Weil /* take transaction reference */ 1493bb9c12c9SSage Weil cur_trans = trans->transaction; 149413c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1495bb9c12c9SSage Weil 1496bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 14976fc4e354SSage Weil 14986fc4e354SSage Weil /* 14996fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 15006fc4e354SSage Weil * async commit thread will be the one to unlock it. 15016fc4e354SSage Weil */ 1502ff7c1d33SMiao Xie if (trans->type < TRANS_JOIN_NOLOCK) 1503ff7c1d33SMiao Xie rwsem_release( 1504ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 15056fc4e354SSage Weil 1, _THIS_IP_); 15066fc4e354SSage Weil 15077892b5afSMiao Xie schedule_work(&ac->work); 1508bb9c12c9SSage Weil 1509bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1510bb9c12c9SSage Weil if (wait_for_unblock) 1511bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1512bb9c12c9SSage Weil else 1513bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1514bb9c12c9SSage Weil 151538e88054SSage Weil if (current->journal_info == trans) 151638e88054SSage Weil current->journal_info = NULL; 151738e88054SSage Weil 151838e88054SSage Weil put_transaction(cur_trans); 1519bb9c12c9SSage Weil return 0; 1520bb9c12c9SSage Weil } 1521bb9c12c9SSage Weil 152249b25e05SJeff Mahoney 152349b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 15247b8b92afSJosef Bacik struct btrfs_root *root, int err) 152549b25e05SJeff Mahoney { 152649b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1527f094ac32SLiu Bo DEFINE_WAIT(wait); 152849b25e05SJeff Mahoney 152949b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 153049b25e05SJeff Mahoney 15317b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 15327b8b92afSJosef Bacik 153349b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 153466b6135bSLiu Bo 153525d8c284SMiao Xie /* 153625d8c284SMiao Xie * If the transaction is removed from the list, it means this 153725d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 153825d8c284SMiao Xie * to call the cleanup function. 153925d8c284SMiao Xie */ 154025d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 154166b6135bSLiu Bo 154249b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1543d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 15444a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1545f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1546f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1547f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1548f094ac32SLiu Bo 1549f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1550d7096fc3SJosef Bacik } 155149b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 155249b25e05SJeff Mahoney 155349b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 155449b25e05SJeff Mahoney 15554a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 15564a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 15574a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 15584a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 15594a9d8bdeSMiao Xie 1560e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 1561e0228285SJosef Bacik sb_end_intwrite(root->fs_info->sb); 156249b25e05SJeff Mahoney put_transaction(cur_trans); 156349b25e05SJeff Mahoney put_transaction(cur_trans); 156449b25e05SJeff Mahoney 156549b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 156649b25e05SJeff Mahoney 156749b25e05SJeff Mahoney btrfs_scrub_continue(root); 156849b25e05SJeff Mahoney 156949b25e05SJeff Mahoney if (current->journal_info == trans) 157049b25e05SJeff Mahoney current->journal_info = NULL; 157149b25e05SJeff Mahoney 157249b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 157349b25e05SJeff Mahoney } 157449b25e05SJeff Mahoney 1575ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans, 1576ca469637SMiao Xie struct btrfs_root *root) 1577ca469637SMiao Xie { 1578ca469637SMiao Xie int ret; 1579ca469637SMiao Xie 1580ca469637SMiao Xie ret = btrfs_run_delayed_items(trans, root); 1581ca469637SMiao Xie if (ret) 1582ca469637SMiao Xie return ret; 1583ca469637SMiao Xie 1584ca469637SMiao Xie /* 1585ca469637SMiao Xie * running the delayed items may have added new refs. account 1586ca469637SMiao Xie * them now so that they hinder processing of more delayed refs 1587ca469637SMiao Xie * as little as possible. 1588ca469637SMiao Xie */ 1589ca469637SMiao Xie btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); 1590ca469637SMiao Xie 1591ca469637SMiao Xie /* 1592ca469637SMiao Xie * rename don't use btrfs_join_transaction, so, once we 1593ca469637SMiao Xie * set the transaction to blocked above, we aren't going 1594ca469637SMiao Xie * to get any new ordered operations. We can safely run 1595ca469637SMiao Xie * it here and no for sure that nothing new will be added 1596ca469637SMiao Xie * to the list 1597ca469637SMiao Xie */ 1598569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 1); 1599ca469637SMiao Xie 1600eebc6084SMiao Xie return ret; 1601ca469637SMiao Xie } 1602ca469637SMiao Xie 160382436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 160482436617SMiao Xie { 160582436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 160682436617SMiao Xie return btrfs_start_all_delalloc_inodes(fs_info, 1); 160782436617SMiao Xie return 0; 160882436617SMiao Xie } 160982436617SMiao Xie 161082436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 161182436617SMiao Xie { 161282436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 1613f0de181cSJosef Bacik btrfs_wait_all_ordered_extents(fs_info); 161482436617SMiao Xie } 161582436617SMiao Xie 161679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 161779154b1bSChris Mason struct btrfs_root *root) 161879154b1bSChris Mason { 161949b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 16208fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 162125287e0aSMiao Xie int ret; 162279154b1bSChris Mason 1623569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 0); 162425287e0aSMiao Xie if (ret) { 162525287e0aSMiao Xie btrfs_abort_transaction(trans, root, ret); 1626e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1627e4a2bcacSJosef Bacik return ret; 162825287e0aSMiao Xie } 162925287e0aSMiao Xie 16308d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 16318d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 163225287e0aSMiao Xie ret = cur_trans->aborted; 1633e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1634e4a2bcacSJosef Bacik return ret; 163525287e0aSMiao Xie } 163649b25e05SJeff Mahoney 163756bec294SChris Mason /* make a pass through all the delayed refs we have so far 163856bec294SChris Mason * any runnings procs may add more while we are here 163956bec294SChris Mason */ 164056bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1641e4a2bcacSJosef Bacik if (ret) { 1642e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1643e4a2bcacSJosef Bacik return ret; 1644e4a2bcacSJosef Bacik } 164556bec294SChris Mason 16460e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 16470e721106SJosef Bacik trans->block_rsv = NULL; 1648272d26d0SMiao Xie if (trans->qgroup_reserved) { 1649272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1650272d26d0SMiao Xie trans->qgroup_reserved = 0; 1651272d26d0SMiao Xie } 16520e721106SJosef Bacik 1653b7ec40d7SChris Mason cur_trans = trans->transaction; 165449b25e05SJeff Mahoney 165556bec294SChris Mason /* 165656bec294SChris Mason * set the flushing flag so procs in this transaction have to 165756bec294SChris Mason * start sending their work down. 165856bec294SChris Mason */ 1659b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 16601be41b78SJosef Bacik smp_wmb(); 166156bec294SChris Mason 1662ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1663ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1664ea658badSJosef Bacik 1665c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1666e4a2bcacSJosef Bacik if (ret) { 1667e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1668e4a2bcacSJosef Bacik return ret; 1669e4a2bcacSJosef Bacik } 167056bec294SChris Mason 16714a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 16724a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 16734a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 167413c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 167549b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1676ccd467d6SChris Mason 1677b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 167815ee9bc7SJosef Bacik 167979154b1bSChris Mason put_transaction(cur_trans); 168015ee9bc7SJosef Bacik 168149b25e05SJeff Mahoney return ret; 168279154b1bSChris Mason } 16834313b399SChris Mason 16844a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1685bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1686bb9c12c9SSage Weil 1687ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1688ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1689ccd467d6SChris Mason struct btrfs_transaction, list); 16904a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 169113c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1692a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1693ccd467d6SChris Mason 1694ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1695ccd467d6SChris Mason 169615ee9bc7SJosef Bacik put_transaction(prev_trans); 1697a4abeea4SJosef Bacik } else { 1698a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1699ccd467d6SChris Mason } 1700a4abeea4SJosef Bacik } else { 1701a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1702ccd467d6SChris Mason } 170315ee9bc7SJosef Bacik 17040860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 17050860adfdSMiao Xie 170682436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 170782436617SMiao Xie if (ret) 170882436617SMiao Xie goto cleanup_transaction; 170982436617SMiao Xie 1710ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 171149b25e05SJeff Mahoney if (ret) 171249b25e05SJeff Mahoney goto cleanup_transaction; 171316cdcec7SMiao Xie 1714581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1715581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1716ed3b3d31SChris Mason 1717581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 1718ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 1719ca469637SMiao Xie if (ret) 1720ca469637SMiao Xie goto cleanup_transaction; 1721ca469637SMiao Xie 172282436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1723ed0ca140SJosef Bacik /* 1724ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1725ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 17264a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1727ed0ca140SJosef Bacik */ 1728a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 17294a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1730a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1731ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1732ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 173315ee9bc7SJosef Bacik 17342cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 17352cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 17362cba30f1SMiao Xie ret = cur_trans->aborted; 17372cba30f1SMiao Xie goto cleanup_transaction; 17382cba30f1SMiao Xie } 17397585717fSChris Mason /* 17407585717fSChris Mason * the reloc mutex makes sure that we stop 17417585717fSChris Mason * the balancing code from coming in and moving 17427585717fSChris Mason * extents around in the middle of the commit 17437585717fSChris Mason */ 17447585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 17457585717fSChris Mason 174642874b3dSMiao Xie /* 174742874b3dSMiao Xie * We needn't worry about the delayed items because we will 174842874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 174942874b3dSMiao Xie * core function of the snapshot creation. 175042874b3dSMiao Xie */ 175142874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 175249b25e05SJeff Mahoney if (ret) { 175349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 175449b25e05SJeff Mahoney goto cleanup_transaction; 175549b25e05SJeff Mahoney } 17563063d29fSChris Mason 175742874b3dSMiao Xie /* 175842874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 175942874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 176042874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 176142874b3dSMiao Xie * them. 176242874b3dSMiao Xie * 176342874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 176442874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 176542874b3dSMiao Xie * the nodes and leaves. 176642874b3dSMiao Xie */ 176742874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 176849b25e05SJeff Mahoney if (ret) { 176949b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 177049b25e05SJeff Mahoney goto cleanup_transaction; 177149b25e05SJeff Mahoney } 177216cdcec7SMiao Xie 177356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 177449b25e05SJeff Mahoney if (ret) { 177549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 177649b25e05SJeff Mahoney goto cleanup_transaction; 177749b25e05SJeff Mahoney } 177856bec294SChris Mason 1779e999376fSChris Mason /* 1780e999376fSChris Mason * make sure none of the code above managed to slip in a 1781e999376fSChris Mason * delayed item 1782e999376fSChris Mason */ 1783e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1784e999376fSChris Mason 17852c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1786dc17ff8fSChris Mason 1787a2de733cSArne Jansen btrfs_scrub_pause(root); 1788e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1789e02119d5SChris Mason * various roots consistent with each other. Every pointer 1790e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1791e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1792e02119d5SChris Mason * the tree logging code from jumping in and changing any 1793e02119d5SChris Mason * of the trees. 1794e02119d5SChris Mason * 1795e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1796e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1797e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1798e02119d5SChris Mason * from now until after the super is written, we avoid races 1799e02119d5SChris Mason * with the tree-log code. 1800e02119d5SChris Mason */ 1801e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 18021a40e23bSZheng Yan 18035d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 180449b25e05SJeff Mahoney if (ret) { 180549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1806871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 180749b25e05SJeff Mahoney goto cleanup_transaction; 180849b25e05SJeff Mahoney } 180954aa1f4dSChris Mason 18105d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1811e02119d5SChris Mason * safe to free the root of tree log roots 1812e02119d5SChris Mason */ 1813e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1814e02119d5SChris Mason 18155d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 181649b25e05SJeff Mahoney if (ret) { 181749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1818871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 181949b25e05SJeff Mahoney goto cleanup_transaction; 182049b25e05SJeff Mahoney } 182154aa1f4dSChris Mason 18222cba30f1SMiao Xie /* 18232cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 18242cba30f1SMiao Xie * update ->aborted, check it. 18252cba30f1SMiao Xie */ 18262cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 18272cba30f1SMiao Xie ret = cur_trans->aborted; 18282cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 18292cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 18302cba30f1SMiao Xie goto cleanup_transaction; 18312cba30f1SMiao Xie } 18322cba30f1SMiao Xie 183311833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 183411833d66SYan Zheng 183578fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 18365f39d397SChris Mason 18375d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 18385d4f98a2SYan Zheng root->fs_info->tree_root->node); 1839817d52f8SJosef Bacik switch_commit_root(root->fs_info->tree_root); 18405d4f98a2SYan Zheng 18415d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 18425d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1843817d52f8SJosef Bacik switch_commit_root(root->fs_info->chunk_root); 18445d4f98a2SYan Zheng 1845edf39272SJan Schmidt assert_qgroups_uptodate(trans); 18465d4f98a2SYan Zheng update_super_roots(root); 1847e02119d5SChris Mason 18486c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 18496c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 18506c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 18516c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1852ccd467d6SChris Mason 1853a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 18544a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 1855a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1856a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 18577585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1858b7ec40d7SChris Mason 1859f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1860e6dcd2dcSChris Mason 186179154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 186249b25e05SJeff Mahoney if (ret) { 186349b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 186408748810SDavid Sterba "Error while writing out transaction"); 186549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 186649b25e05SJeff Mahoney goto cleanup_transaction; 186749b25e05SJeff Mahoney } 186849b25e05SJeff Mahoney 186949b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 187049b25e05SJeff Mahoney if (ret) { 187149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 187249b25e05SJeff Mahoney goto cleanup_transaction; 187349b25e05SJeff Mahoney } 18744313b399SChris Mason 1875e02119d5SChris Mason /* 1876e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1877e02119d5SChris Mason * to go about their business 1878e02119d5SChris Mason */ 1879e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1880e02119d5SChris Mason 188111833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 18824313b399SChris Mason 188315ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 18844a9d8bdeSMiao Xie /* 18854a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 18864a9d8bdeSMiao Xie * which can change it. 18874a9d8bdeSMiao Xie */ 18884a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 18892c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 18903de4586cSChris Mason 1891a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 189213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 1893a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1894a4abeea4SJosef Bacik 189579154b1bSChris Mason put_transaction(cur_trans); 189678fae27eSChris Mason put_transaction(cur_trans); 189758176a96SJosef Bacik 18980860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 1899b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 1900b2b5ef5cSJan Kara 19011abe9b8aSliubo trace_btrfs_transaction_commit(root); 19021abe9b8aSliubo 1903a2de733cSArne Jansen btrfs_scrub_continue(root); 1904a2de733cSArne Jansen 19059ed74f2dSJosef Bacik if (current->journal_info == trans) 19069ed74f2dSJosef Bacik current->journal_info = NULL; 19079ed74f2dSJosef Bacik 19082c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 190924bbcf04SYan, Zheng 191024bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 191124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 191224bbcf04SYan, Zheng 191379154b1bSChris Mason return ret; 191449b25e05SJeff Mahoney 191549b25e05SJeff Mahoney cleanup_transaction: 19160e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 19170e721106SJosef Bacik trans->block_rsv = NULL; 1918272d26d0SMiao Xie if (trans->qgroup_reserved) { 1919272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1920272d26d0SMiao Xie trans->qgroup_reserved = 0; 1921272d26d0SMiao Xie } 1922c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 192349b25e05SJeff Mahoney if (current->journal_info == trans) 192449b25e05SJeff Mahoney current->journal_info = NULL; 19257b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 192649b25e05SJeff Mahoney 192749b25e05SJeff Mahoney return ret; 192879154b1bSChris Mason } 192979154b1bSChris Mason 1930d352ac68SChris Mason /* 19319d1a2a3aSDavid Sterba * return < 0 if error 19329d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 19339d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 19349d1a2a3aSDavid Sterba * 19359d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 19369d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 19379d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 19389d1a2a3aSDavid Sterba * few seconds later. 1939d352ac68SChris Mason */ 19409d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 1941e9d0b13bSChris Mason { 19429d1a2a3aSDavid Sterba int ret; 19435d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1944e9d0b13bSChris Mason 1945a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 19469d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 19479d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 19489d1a2a3aSDavid Sterba return 0; 19499d1a2a3aSDavid Sterba } 19509d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 19519d1a2a3aSDavid Sterba struct btrfs_root, root_list); 1952cfad392bSJosef Bacik list_del_init(&root->root_list); 1953a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 19545d4f98a2SYan Zheng 1955c1c9ff7cSGeert Uytterhoeven pr_debug("btrfs: cleaner removing %llu\n", root->objectid); 195676dda93cSYan, Zheng 195716cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 195816cdcec7SMiao Xie 195976dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 196076dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 19612c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 196276dda93cSYan, Zheng else 19632c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 19649d1a2a3aSDavid Sterba /* 19659d1a2a3aSDavid Sterba * If we encounter a transaction abort during snapshot cleaning, we 19669d1a2a3aSDavid Sterba * don't want to crash here 19679d1a2a3aSDavid Sterba */ 19686596a928SJosef Bacik return (ret < 0) ? 0 : 1; 1969e9d0b13bSChris Mason } 1970