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); 662c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 6779154b1bSChris Mason } 6878fae27eSChris Mason } 6979154b1bSChris Mason 70817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root) 71817d52f8SJosef Bacik { 72817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 73817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 74817d52f8SJosef Bacik } 75817d52f8SJosef Bacik 760860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 770860adfdSMiao Xie unsigned int type) 780860adfdSMiao Xie { 790860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 800860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 810860adfdSMiao Xie } 820860adfdSMiao Xie 830860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 840860adfdSMiao Xie unsigned int type) 850860adfdSMiao Xie { 860860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 870860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 880860adfdSMiao Xie } 890860adfdSMiao Xie 900860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 910860adfdSMiao Xie unsigned int type) 920860adfdSMiao Xie { 930860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 940860adfdSMiao Xie } 950860adfdSMiao Xie 960860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 970860adfdSMiao Xie { 980860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 99178260b2SMiao Xie } 100178260b2SMiao Xie 101d352ac68SChris Mason /* 102d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 103d352ac68SChris Mason */ 1040860adfdSMiao Xie static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 10579154b1bSChris Mason { 10679154b1bSChris Mason struct btrfs_transaction *cur_trans; 10719ae4e81SJan Schmidt struct btrfs_fs_info *fs_info = root->fs_info; 108a4abeea4SJosef Bacik 10919ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 110d43317dcSChris Mason loop: 11149b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 11287533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 11319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 11449b25e05SJeff Mahoney return -EROFS; 11549b25e05SJeff Mahoney } 11649b25e05SJeff Mahoney 11719ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 118a4abeea4SJosef Bacik if (cur_trans) { 119871383beSDavid Sterba if (cur_trans->aborted) { 12019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 12149b25e05SJeff Mahoney return cur_trans->aborted; 122871383beSDavid Sterba } 1234a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 124178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 125178260b2SMiao Xie return -EBUSY; 126178260b2SMiao Xie } 127a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 128a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1290860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 13019ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 131a4abeea4SJosef Bacik return 0; 132a4abeea4SJosef Bacik } 13319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 134a4abeea4SJosef Bacik 135354aa0fbSMiao Xie /* 136354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 137354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 138354aa0fbSMiao Xie */ 139354aa0fbSMiao Xie if (type == TRANS_ATTACH) 140354aa0fbSMiao Xie return -ENOENT; 141354aa0fbSMiao Xie 1424a9d8bdeSMiao Xie /* 1434a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1444a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1454a9d8bdeSMiao Xie */ 1464a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1474a9d8bdeSMiao Xie 148a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 149db5b493aSTsutomu Itoh if (!cur_trans) 150db5b493aSTsutomu Itoh return -ENOMEM; 151d43317dcSChris Mason 15219ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 15319ae4e81SJan Schmidt if (fs_info->running_transaction) { 154d43317dcSChris Mason /* 155d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 1564a9d8bdeSMiao Xie * to redo the checks above 157d43317dcSChris Mason */ 158a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 159d43317dcSChris Mason goto loop; 16087533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 161e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 1627b8b92afSJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 1637b8b92afSJosef Bacik return -EROFS; 164a4abeea4SJosef Bacik } 165d43317dcSChris Mason 16613c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 1670860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 16879154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 16979154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 1704a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 171a4abeea4SJosef Bacik /* 172a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 173a4abeea4SJosef Bacik * commit the transaction. 174a4abeea4SJosef Bacik */ 175a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 17608607c1bSChris Mason cur_trans->start_time = get_seconds(); 17756bec294SChris Mason 1786bef4d31SEric Paris cur_trans->delayed_refs.root = RB_ROOT; 17956bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 180c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 181c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 18256bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 183c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 18420b297d6SJan Schmidt 18520b297d6SJan Schmidt /* 18620b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 18720b297d6SJan Schmidt * the log must never go across transaction boundaries. 18820b297d6SJan Schmidt */ 18920b297d6SJan Schmidt smp_mb(); 19031b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 19131b1a2bdSJulia Lawall WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when " 19220b297d6SJan Schmidt "creating a fresh transaction\n"); 19331b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 19431b1a2bdSJulia Lawall WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when " 19520b297d6SJan Schmidt "creating a fresh transaction\n"); 196fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 19720b297d6SJan Schmidt 19856bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 199bb721703SChris Mason atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0); 200bb721703SChris Mason atomic_set(&cur_trans->delayed_refs.ref_seq, 0); 201bb721703SChris Mason init_waitqueue_head(&cur_trans->delayed_refs.wait); 20256bec294SChris Mason 2033063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 204569e0f35SJosef Bacik INIT_LIST_HEAD(&cur_trans->ordered_operations); 20519ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 206d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 20719ae4e81SJan Schmidt fs_info->btree_inode->i_mapping); 20819ae4e81SJan Schmidt fs_info->generation++; 20919ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 21019ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 21149b25e05SJeff Mahoney cur_trans->aborted = 0; 21219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 21315ee9bc7SJosef Bacik 21479154b1bSChris Mason return 0; 21579154b1bSChris Mason } 21679154b1bSChris Mason 217d352ac68SChris Mason /* 218d397712bSChris Mason * this does all the record keeping required to make sure that a reference 219d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 220d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 221d397712bSChris Mason * when the transaction commits 222d352ac68SChris Mason */ 2237585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2245d4f98a2SYan Zheng struct btrfs_root *root) 2256702ed49SChris Mason { 2265d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 2276702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 2285d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 2295d4f98a2SYan Zheng 2307585717fSChris Mason /* 2317585717fSChris Mason * see below for in_trans_setup usage rules 2327585717fSChris Mason * we have the reloc mutex held now, so there 2337585717fSChris Mason * is only one writer in this function 2347585717fSChris Mason */ 2357585717fSChris Mason root->in_trans_setup = 1; 2367585717fSChris Mason 2377585717fSChris Mason /* make sure readers find in_trans_setup before 2387585717fSChris Mason * they find our root->last_trans update 2397585717fSChris Mason */ 2407585717fSChris Mason smp_wmb(); 2417585717fSChris Mason 242a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 243a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 244a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 245a4abeea4SJosef Bacik return 0; 246a4abeea4SJosef Bacik } 2476702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 2486702ed49SChris Mason (unsigned long)root->root_key.objectid, 2496702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 250a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 2517585717fSChris Mason root->last_trans = trans->transid; 2527585717fSChris Mason 2537585717fSChris Mason /* this is pretty tricky. We don't want to 2547585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 2557585717fSChris Mason * unless we're really doing the first setup for this root in 2567585717fSChris Mason * this transaction. 2577585717fSChris Mason * 2587585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 2597585717fSChris Mason * if we want to take the expensive mutex. 2607585717fSChris Mason * 2617585717fSChris Mason * But, we have to set root->last_trans before we 2627585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 2637585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 2647585717fSChris Mason * with root->in_trans_setup. When this is 1, we're still 2657585717fSChris Mason * fixing up the reloc trees and everyone must wait. 2667585717fSChris Mason * 2677585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 2687585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 2697585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 2707585717fSChris Mason * done before we pop in the zero below 2717585717fSChris Mason */ 2725d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 2737585717fSChris Mason smp_wmb(); 2747585717fSChris Mason root->in_trans_setup = 0; 2756702ed49SChris Mason } 2765d4f98a2SYan Zheng return 0; 2776702ed49SChris Mason } 2785d4f98a2SYan Zheng 2797585717fSChris Mason 2807585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 2817585717fSChris Mason struct btrfs_root *root) 2827585717fSChris Mason { 2837585717fSChris Mason if (!root->ref_cows) 2847585717fSChris Mason return 0; 2857585717fSChris Mason 2867585717fSChris Mason /* 2877585717fSChris Mason * see record_root_in_trans for comments about in_trans_setup usage 2887585717fSChris Mason * and barriers 2897585717fSChris Mason */ 2907585717fSChris Mason smp_rmb(); 2917585717fSChris Mason if (root->last_trans == trans->transid && 2927585717fSChris Mason !root->in_trans_setup) 2937585717fSChris Mason return 0; 2947585717fSChris Mason 2957585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 2967585717fSChris Mason record_root_in_trans(trans, root); 2977585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2987585717fSChris Mason 2997585717fSChris Mason return 0; 3007585717fSChris Mason } 3017585717fSChris Mason 3024a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3034a9d8bdeSMiao Xie { 3044a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 305*501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 306*501407aaSJosef Bacik !trans->aborted); 3074a9d8bdeSMiao Xie } 3084a9d8bdeSMiao Xie 309d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 310d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 311d352ac68SChris Mason * transaction might not be fully on disk. 312d352ac68SChris Mason */ 31337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 31479154b1bSChris Mason { 315f9295749SChris Mason struct btrfs_transaction *cur_trans; 31679154b1bSChris Mason 317a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 318f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 3194a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 32013c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 321a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 32272d63ed6SLi Zefan 32372d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 324*501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 325*501407aaSJosef Bacik cur_trans->aborted); 326f9295749SChris Mason put_transaction(cur_trans); 327a4abeea4SJosef Bacik } else { 328a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 329f9295749SChris Mason } 33037d1aeeeSChris Mason } 33137d1aeeeSChris Mason 332a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 33337d1aeeeSChris Mason { 334a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 335a4abeea4SJosef Bacik return 0; 336a4abeea4SJosef Bacik 337a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 338a22285a6SYan, Zheng return 1; 339a4abeea4SJosef Bacik 340a4abeea4SJosef Bacik if (type == TRANS_START && 341a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 342a4abeea4SJosef Bacik return 1; 343a4abeea4SJosef Bacik 344a22285a6SYan, Zheng return 0; 345a22285a6SYan, Zheng } 346a22285a6SYan, Zheng 34708e007d2SMiao Xie static struct btrfs_trans_handle * 3480860adfdSMiao Xie start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type, 34908e007d2SMiao Xie enum btrfs_reserve_flush_enum flush) 350a22285a6SYan, Zheng { 351a22285a6SYan, Zheng struct btrfs_trans_handle *h; 352a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 353b5009945SJosef Bacik u64 num_bytes = 0; 354a22285a6SYan, Zheng int ret; 355c5567237SArne Jansen u64 qgroup_reserved = 0; 356acce952bSliubo 35787533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 358acce952bSliubo return ERR_PTR(-EROFS); 3592a1eb461SJosef Bacik 3602a1eb461SJosef Bacik if (current->journal_info) { 3610860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 3622a1eb461SJosef Bacik h = current->journal_info; 3632a1eb461SJosef Bacik h->use_count++; 364b7d5b0a8SMiao Xie WARN_ON(h->use_count > 2); 3652a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 3662a1eb461SJosef Bacik h->block_rsv = NULL; 3672a1eb461SJosef Bacik goto got_it; 3682a1eb461SJosef Bacik } 369b5009945SJosef Bacik 370b5009945SJosef Bacik /* 371b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 372b5009945SJosef Bacik * the appropriate flushing if need be. 373b5009945SJosef Bacik */ 374b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 375c5567237SArne Jansen if (root->fs_info->quota_enabled && 376c5567237SArne Jansen is_fstree(root->root_key.objectid)) { 377c5567237SArne Jansen qgroup_reserved = num_items * root->leafsize; 378c5567237SArne Jansen ret = btrfs_qgroup_reserve(root, qgroup_reserved); 379c5567237SArne Jansen if (ret) 380c5567237SArne Jansen return ERR_PTR(ret); 381c5567237SArne Jansen } 382c5567237SArne Jansen 383b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 3844a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 385b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 38608e007d2SMiao Xie num_bytes, flush); 387b5009945SJosef Bacik if (ret) 388843fcf35SMiao Xie goto reserve_fail; 389b5009945SJosef Bacik } 390a22285a6SYan, Zheng again: 391a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 392843fcf35SMiao Xie if (!h) { 393843fcf35SMiao Xie ret = -ENOMEM; 394843fcf35SMiao Xie goto alloc_fail; 395843fcf35SMiao Xie } 396a22285a6SYan, Zheng 39798114659SJosef Bacik /* 39898114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 39998114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 40098114659SJosef Bacik * because we're already holding a ref. We need this because we could 40198114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 40298114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 403354aa0fbSMiao Xie * 404354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 405354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 40698114659SJosef Bacik */ 4070860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 408b2b5ef5cSJan Kara sb_start_intwrite(root->fs_info->sb); 409b2b5ef5cSJan Kara 410a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 41137d1aeeeSChris Mason wait_current_trans(root); 412a22285a6SYan, Zheng 413a4abeea4SJosef Bacik do { 414354aa0fbSMiao Xie ret = join_transaction(root, type); 415178260b2SMiao Xie if (ret == -EBUSY) { 416a4abeea4SJosef Bacik wait_current_trans(root); 417178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 418178260b2SMiao Xie ret = -ENOENT; 419178260b2SMiao Xie } 420a4abeea4SJosef Bacik } while (ret == -EBUSY); 421a4abeea4SJosef Bacik 422db5b493aSTsutomu Itoh if (ret < 0) { 423354aa0fbSMiao Xie /* We must get the transaction if we are JOIN_NOLOCK. */ 424354aa0fbSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 425843fcf35SMiao Xie goto join_fail; 426db5b493aSTsutomu Itoh } 4270f7d52f4SChris Mason 428a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 429a22285a6SYan, Zheng 430a22285a6SYan, Zheng h->transid = cur_trans->transid; 431a22285a6SYan, Zheng h->transaction = cur_trans; 43279154b1bSChris Mason h->blocks_used = 0; 433a22285a6SYan, Zheng h->bytes_reserved = 0; 434d13603efSArne Jansen h->root = root; 43556bec294SChris Mason h->delayed_ref_updates = 0; 4362a1eb461SJosef Bacik h->use_count = 1; 4370e721106SJosef Bacik h->adding_csums = 0; 438f0486c68SYan, Zheng h->block_rsv = NULL; 4392a1eb461SJosef Bacik h->orig_rsv = NULL; 44049b25e05SJeff Mahoney h->aborted = 0; 4414b824906SMiao Xie h->qgroup_reserved = 0; 442bed92eaeSArne Jansen h->delayed_ref_elem.seq = 0; 443a698d075SMiao Xie h->type = type; 444c6b305a8SJosef Bacik h->allocating_chunk = false; 445bed92eaeSArne Jansen INIT_LIST_HEAD(&h->qgroup_ref_list); 446ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 447b7ec40d7SChris Mason 448a22285a6SYan, Zheng smp_mb(); 4494a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 4504a9d8bdeSMiao Xie may_wait_transaction(root, type)) { 451a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 452a22285a6SYan, Zheng goto again; 453a22285a6SYan, Zheng } 4549ed74f2dSJosef Bacik 455b5009945SJosef Bacik if (num_bytes) { 4568c2a3ca2SJosef Bacik trace_btrfs_space_reservation(root->fs_info, "transaction", 4572bcc0328SLiu Bo h->transid, num_bytes, 1); 458b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 459b5009945SJosef Bacik h->bytes_reserved = num_bytes; 460a22285a6SYan, Zheng } 4614b824906SMiao Xie h->qgroup_reserved = qgroup_reserved; 462a22285a6SYan, Zheng 4632a1eb461SJosef Bacik got_it: 464a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 465a22285a6SYan, Zheng 466a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 467a22285a6SYan, Zheng current->journal_info = h; 46879154b1bSChris Mason return h; 469843fcf35SMiao Xie 470843fcf35SMiao Xie join_fail: 4710860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 472843fcf35SMiao Xie sb_end_intwrite(root->fs_info->sb); 473843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 474843fcf35SMiao Xie alloc_fail: 475843fcf35SMiao Xie if (num_bytes) 476843fcf35SMiao Xie btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 477843fcf35SMiao Xie num_bytes); 478843fcf35SMiao Xie reserve_fail: 479843fcf35SMiao Xie if (qgroup_reserved) 480843fcf35SMiao Xie btrfs_qgroup_free(root, qgroup_reserved); 481843fcf35SMiao Xie return ERR_PTR(ret); 48279154b1bSChris Mason } 48379154b1bSChris Mason 484f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 485a22285a6SYan, Zheng int num_items) 486f9295749SChris Mason { 48708e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 48808e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 489f9295749SChris Mason } 4908407aa46SMiao Xie 49108e007d2SMiao Xie struct btrfs_trans_handle *btrfs_start_transaction_lflush( 4928407aa46SMiao Xie struct btrfs_root *root, int num_items) 4938407aa46SMiao Xie { 49408e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 49508e007d2SMiao Xie BTRFS_RESERVE_FLUSH_LIMIT); 4968407aa46SMiao Xie } 4978407aa46SMiao Xie 4987a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 499f9295749SChris Mason { 5008407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN, 0); 501f9295749SChris Mason } 502f9295749SChris Mason 5037a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 5040af3d00bSJosef Bacik { 5058407aa46SMiao Xie return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0); 5060af3d00bSJosef Bacik } 5070af3d00bSJosef Bacik 5087a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 5099ca9ee09SSage Weil { 5108407aa46SMiao Xie return start_transaction(root, 0, TRANS_USERSPACE, 0); 5119ca9ee09SSage Weil } 5129ca9ee09SSage Weil 513d4edf39bSMiao Xie /* 514d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 515d4edf39bSMiao Xie * 516d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 517d4edf39bSMiao Xie * don't want to start a new one. 518d4edf39bSMiao Xie * 519d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 520d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 521d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 522d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 523d4edf39bSMiao Xie * invoke 524d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 525d4edf39bSMiao Xie */ 526354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 52760376ce4SJosef Bacik { 528354aa0fbSMiao Xie return start_transaction(root, 0, TRANS_ATTACH, 0); 52960376ce4SJosef Bacik } 53060376ce4SJosef Bacik 531d4edf39bSMiao Xie /* 532d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 533d4edf39bSMiao Xie * 534d4edf39bSMiao Xie * It is similar to the above function, the differentia is this one 535d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 536d4edf39bSMiao Xie * complete. 537d4edf39bSMiao Xie */ 538d4edf39bSMiao Xie struct btrfs_trans_handle * 539d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 540d4edf39bSMiao Xie { 541d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 542d4edf39bSMiao Xie 543d4edf39bSMiao Xie trans = start_transaction(root, 0, TRANS_ATTACH, 0); 544d4edf39bSMiao Xie if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 545d4edf39bSMiao Xie btrfs_wait_for_commit(root, 0); 546d4edf39bSMiao Xie 547d4edf39bSMiao Xie return trans; 548d4edf39bSMiao Xie } 549d4edf39bSMiao Xie 550d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 551b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 55289ce8a63SChris Mason struct btrfs_transaction *commit) 55389ce8a63SChris Mason { 5544a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 55589ce8a63SChris Mason } 55689ce8a63SChris Mason 55746204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 55846204592SSage Weil { 55946204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 5608cd2807fSMiao Xie int ret = 0; 56146204592SSage Weil 56246204592SSage Weil if (transid) { 56346204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 564a4abeea4SJosef Bacik goto out; 56546204592SSage Weil 5668cd2807fSMiao Xie ret = -EINVAL; 56746204592SSage Weil /* find specified transaction */ 568a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 56946204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 57046204592SSage Weil if (t->transid == transid) { 57146204592SSage Weil cur_trans = t; 572a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 5738cd2807fSMiao Xie ret = 0; 57446204592SSage Weil break; 57546204592SSage Weil } 5768cd2807fSMiao Xie if (t->transid > transid) { 5778cd2807fSMiao Xie ret = 0; 57846204592SSage Weil break; 57946204592SSage Weil } 5808cd2807fSMiao Xie } 581a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 5828cd2807fSMiao Xie /* The specified transaction doesn't exist */ 58346204592SSage Weil if (!cur_trans) 5848cd2807fSMiao Xie goto out; 58546204592SSage Weil } else { 58646204592SSage Weil /* find newest transaction that is committing | committed */ 587a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 58846204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 58946204592SSage Weil list) { 5904a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 5914a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 5923473f3c0SJosef Bacik break; 59346204592SSage Weil cur_trans = t; 594a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 59546204592SSage Weil break; 59646204592SSage Weil } 59746204592SSage Weil } 598a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 59946204592SSage Weil if (!cur_trans) 600a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 60146204592SSage Weil } 60246204592SSage Weil 60346204592SSage Weil wait_for_commit(root, cur_trans); 60446204592SSage Weil put_transaction(cur_trans); 605a4abeea4SJosef Bacik out: 60646204592SSage Weil return ret; 60746204592SSage Weil } 60846204592SSage Weil 60937d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 61037d1aeeeSChris Mason { 611a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 61237d1aeeeSChris Mason wait_current_trans(root); 61337d1aeeeSChris Mason } 61437d1aeeeSChris Mason 6158929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 6168929ecfaSYan, Zheng struct btrfs_root *root) 6178929ecfaSYan, Zheng { 6188929ecfaSYan, Zheng int ret; 61936ba022aSJosef Bacik 62036ba022aSJosef Bacik ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 6218929ecfaSYan, Zheng return ret ? 1 : 0; 6228929ecfaSYan, Zheng } 6238929ecfaSYan, Zheng 6248929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 6258929ecfaSYan, Zheng struct btrfs_root *root) 6268929ecfaSYan, Zheng { 6278929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 6288929ecfaSYan, Zheng int updates; 62949b25e05SJeff Mahoney int err; 6308929ecfaSYan, Zheng 631a4abeea4SJosef Bacik smp_mb(); 6324a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 6334a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 6348929ecfaSYan, Zheng return 1; 6358929ecfaSYan, Zheng 6368929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 6378929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 63849b25e05SJeff Mahoney if (updates) { 63949b25e05SJeff Mahoney err = btrfs_run_delayed_refs(trans, root, updates); 64049b25e05SJeff Mahoney if (err) /* Error code will also eval true */ 64149b25e05SJeff Mahoney return err; 64249b25e05SJeff Mahoney } 6438929ecfaSYan, Zheng 6448929ecfaSYan, Zheng return should_end_transaction(trans, root); 6458929ecfaSYan, Zheng } 6468929ecfaSYan, Zheng 64789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 648a698d075SMiao Xie struct btrfs_root *root, int throttle) 64979154b1bSChris Mason { 6508929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 651ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 652c3e69d58SChris Mason int count = 0; 653a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 6544edc2ca3SDave Jones int err = 0; 655d6e4a428SChris Mason 6562a1eb461SJosef Bacik if (--trans->use_count) { 6572a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 6582a1eb461SJosef Bacik return 0; 6592a1eb461SJosef Bacik } 6602a1eb461SJosef Bacik 661edf39272SJan Schmidt /* 662edf39272SJan Schmidt * do the qgroup accounting as early as possible 663edf39272SJan Schmidt */ 664edf39272SJan Schmidt err = btrfs_delayed_refs_qgroup_accounting(trans, info); 665edf39272SJan Schmidt 666b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 6674c13d758SJosef Bacik trans->block_rsv = NULL; 668c5567237SArne Jansen 669c5567237SArne Jansen if (trans->qgroup_reserved) { 6707c2ec3f0SLiu Bo /* 6717c2ec3f0SLiu Bo * the same root has to be passed here between start_transaction 6727c2ec3f0SLiu Bo * and end_transaction. Subvolume quota depends on this. 6737c2ec3f0SLiu Bo */ 6747c2ec3f0SLiu Bo btrfs_qgroup_free(trans->root, trans->qgroup_reserved); 675c5567237SArne Jansen trans->qgroup_reserved = 0; 676c5567237SArne Jansen } 677c5567237SArne Jansen 678ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 679ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 680ea658badSJosef Bacik 681bb721703SChris Mason while (count < 1) { 682c3e69d58SChris Mason unsigned long cur = trans->delayed_ref_updates; 683c3e69d58SChris Mason trans->delayed_ref_updates = 0; 684c3e69d58SChris Mason if (cur && 685c3e69d58SChris Mason trans->transaction->delayed_refs.num_heads_ready > 64) { 686c3e69d58SChris Mason trans->delayed_ref_updates = 0; 687c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 688c3e69d58SChris Mason } else { 689c3e69d58SChris Mason break; 690c3e69d58SChris Mason } 691c3e69d58SChris Mason count++; 69256bec294SChris Mason } 693bb721703SChris Mason 6940e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 6950e721106SJosef Bacik trans->block_rsv = NULL; 69656bec294SChris Mason 697ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 698ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 699ea658badSJosef Bacik 700a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 7014a9d8bdeSMiao Xie should_end_transaction(trans, root) && 7024a9d8bdeSMiao Xie ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 7034a9d8bdeSMiao Xie spin_lock(&info->trans_lock); 7044a9d8bdeSMiao Xie if (cur_trans->state == TRANS_STATE_RUNNING) 7054a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_BLOCKED; 7064a9d8bdeSMiao Xie spin_unlock(&info->trans_lock); 707a4abeea4SJosef Bacik } 7088929ecfaSYan, Zheng 7094a9d8bdeSMiao Xie if (lock && ACCESS_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 71081317fdeSJosef Bacik if (throttle) { 71181317fdeSJosef Bacik /* 71281317fdeSJosef Bacik * We may race with somebody else here so end up having 71381317fdeSJosef Bacik * to call end_transaction on ourselves again, so inc 71481317fdeSJosef Bacik * our use_count. 71581317fdeSJosef Bacik */ 71681317fdeSJosef Bacik trans->use_count++; 7178929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 71881317fdeSJosef Bacik } else { 7198929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 7208929ecfaSYan, Zheng } 72181317fdeSJosef Bacik } 7228929ecfaSYan, Zheng 7230860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 7246df7881aSJosef Bacik sb_end_intwrite(root->fs_info->sb); 7256df7881aSJosef Bacik 7268929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 72713c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 72813c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 7290860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 73089ce8a63SChris Mason 73199d16cbcSSage Weil smp_mb(); 73279154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 73379154b1bSChris Mason wake_up(&cur_trans->writer_wait); 73479154b1bSChris Mason put_transaction(cur_trans); 7359ed74f2dSJosef Bacik 7369ed74f2dSJosef Bacik if (current->journal_info == trans) 7379ed74f2dSJosef Bacik current->journal_info = NULL; 738ab78c84dSChris Mason 73924bbcf04SYan, Zheng if (throttle) 74024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 74124bbcf04SYan, Zheng 74249b25e05SJeff Mahoney if (trans->aborted || 74387533c47SMiao Xie test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 7444edc2ca3SDave Jones err = -EIO; 745edf39272SJan Schmidt assert_qgroups_uptodate(trans); 74649b25e05SJeff Mahoney 7474edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 7484edc2ca3SDave Jones return err; 74979154b1bSChris Mason } 75079154b1bSChris Mason 75189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 75289ce8a63SChris Mason struct btrfs_root *root) 75389ce8a63SChris Mason { 75498ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 0); 75589ce8a63SChris Mason } 75689ce8a63SChris Mason 75789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 75889ce8a63SChris Mason struct btrfs_root *root) 75989ce8a63SChris Mason { 76098ad43beSWang Shilong return __btrfs_end_transaction(trans, root, 1); 76116cdcec7SMiao Xie } 76216cdcec7SMiao Xie 76316cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans, 76416cdcec7SMiao Xie struct btrfs_root *root) 76516cdcec7SMiao Xie { 766a698d075SMiao Xie return __btrfs_end_transaction(trans, root, 1); 76789ce8a63SChris Mason } 76889ce8a63SChris Mason 769d352ac68SChris Mason /* 770d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 771d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 772690587d1SChris Mason * those extents are sent to disk but does not wait on them 773d352ac68SChris Mason */ 774690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 7758cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 77679154b1bSChris Mason { 777777e6bd7SChris Mason int err = 0; 7787c4452b9SChris Mason int werr = 0; 7791728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 780e6138876SJosef Bacik struct extent_state *cached_state = NULL; 781777e6bd7SChris Mason u64 start = 0; 7825f39d397SChris Mason u64 end; 7837c4452b9SChris Mason 7841728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 785e6138876SJosef Bacik mark, &cached_state)) { 786e6138876SJosef Bacik convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 787e6138876SJosef Bacik mark, &cached_state, GFP_NOFS); 788e6138876SJosef Bacik cached_state = NULL; 7891728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 7907c4452b9SChris Mason if (err) 7917c4452b9SChris Mason werr = err; 7921728366eSJosef Bacik cond_resched(); 7931728366eSJosef Bacik start = end + 1; 7947c4452b9SChris Mason } 795690587d1SChris Mason if (err) 796690587d1SChris Mason werr = err; 797690587d1SChris Mason return werr; 798690587d1SChris Mason } 799690587d1SChris Mason 800690587d1SChris Mason /* 801690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 802690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 803690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 804690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 805690587d1SChris Mason */ 806690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 8078cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 808690587d1SChris Mason { 809690587d1SChris Mason int err = 0; 810690587d1SChris Mason int werr = 0; 8111728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 812e6138876SJosef Bacik struct extent_state *cached_state = NULL; 813690587d1SChris Mason u64 start = 0; 814690587d1SChris Mason u64 end; 815690587d1SChris Mason 8161728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 817e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 818e6138876SJosef Bacik clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, 819e6138876SJosef Bacik 0, 0, &cached_state, GFP_NOFS); 8201728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 821777e6bd7SChris Mason if (err) 822777e6bd7SChris Mason werr = err; 823777e6bd7SChris Mason cond_resched(); 8241728366eSJosef Bacik start = end + 1; 825777e6bd7SChris Mason } 8267c4452b9SChris Mason if (err) 8277c4452b9SChris Mason werr = err; 8287c4452b9SChris Mason return werr; 82979154b1bSChris Mason } 83079154b1bSChris Mason 831690587d1SChris Mason /* 832690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 833690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 834690587d1SChris Mason * those extents are on disk for transaction or log commit 835690587d1SChris Mason */ 836690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 8378cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 838690587d1SChris Mason { 839690587d1SChris Mason int ret; 840690587d1SChris Mason int ret2; 841c6adc9ccSMiao Xie struct blk_plug plug; 842690587d1SChris Mason 843c6adc9ccSMiao Xie blk_start_plug(&plug); 8448cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 845c6adc9ccSMiao Xie blk_finish_plug(&plug); 8468cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 847bf0da8c1SChris Mason 848bf0da8c1SChris Mason if (ret) 849bf0da8c1SChris Mason return ret; 850bf0da8c1SChris Mason if (ret2) 851bf0da8c1SChris Mason return ret2; 852bf0da8c1SChris Mason return 0; 853690587d1SChris Mason } 854690587d1SChris Mason 855d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 856d0c803c4SChris Mason struct btrfs_root *root) 857d0c803c4SChris Mason { 858d0c803c4SChris Mason if (!trans || !trans->transaction) { 859d0c803c4SChris Mason struct inode *btree_inode; 860d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 861d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 862d0c803c4SChris Mason } 863d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 8648cef4e16SYan, Zheng &trans->transaction->dirty_pages, 8658cef4e16SYan, Zheng EXTENT_DIRTY); 866d0c803c4SChris Mason } 867d0c803c4SChris Mason 868d352ac68SChris Mason /* 869d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 870d352ac68SChris Mason * 871d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 872d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 873d352ac68SChris Mason * allocation tree. 874d352ac68SChris Mason * 875d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 876d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 877d352ac68SChris Mason */ 8780b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 87979154b1bSChris Mason struct btrfs_root *root) 88079154b1bSChris Mason { 88179154b1bSChris Mason int ret; 8820b86a832SChris Mason u64 old_root_bytenr; 88386b9f2ecSYan, Zheng u64 old_root_used; 8840b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 88579154b1bSChris Mason 88686b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 8870b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 88856bec294SChris Mason 88979154b1bSChris Mason while (1) { 8900b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 89186b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 89286b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 89379154b1bSChris Mason break; 89487ef2bb4SChris Mason 8955d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 89679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 8970b86a832SChris Mason &root->root_key, 8980b86a832SChris Mason &root->root_item); 89949b25e05SJeff Mahoney if (ret) 90049b25e05SJeff Mahoney return ret; 90156bec294SChris Mason 90286b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 9034a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 90449b25e05SJeff Mahoney if (ret) 90549b25e05SJeff Mahoney return ret; 9060b86a832SChris Mason } 907276e680dSYan Zheng 908276e680dSYan Zheng if (root != root->fs_info->extent_root) 909817d52f8SJosef Bacik switch_commit_root(root); 910276e680dSYan Zheng 9110b86a832SChris Mason return 0; 9120b86a832SChris Mason } 9130b86a832SChris Mason 914d352ac68SChris Mason /* 915d352ac68SChris Mason * update all the cowonly tree roots on disk 91649b25e05SJeff Mahoney * 91749b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 91849b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 91949b25e05SJeff Mahoney * to clean up the delayed refs. 920d352ac68SChris Mason */ 9215d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 9220b86a832SChris Mason struct btrfs_root *root) 9230b86a832SChris Mason { 9240b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 9250b86a832SChris Mason struct list_head *next; 92684234f3aSYan Zheng struct extent_buffer *eb; 92756bec294SChris Mason int ret; 92884234f3aSYan Zheng 92956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 93049b25e05SJeff Mahoney if (ret) 93149b25e05SJeff Mahoney return ret; 93287ef2bb4SChris Mason 93384234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 93449b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 93549b25e05SJeff Mahoney 0, &eb); 93684234f3aSYan Zheng btrfs_tree_unlock(eb); 93784234f3aSYan Zheng free_extent_buffer(eb); 9380b86a832SChris Mason 93949b25e05SJeff Mahoney if (ret) 94049b25e05SJeff Mahoney return ret; 94149b25e05SJeff Mahoney 94256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 94349b25e05SJeff Mahoney if (ret) 94449b25e05SJeff Mahoney return ret; 94587ef2bb4SChris Mason 946733f4fbbSStefan Behrens ret = btrfs_run_dev_stats(trans, root->fs_info); 9478dabb742SStefan Behrens WARN_ON(ret); 9488dabb742SStefan Behrens ret = btrfs_run_dev_replace(trans, root->fs_info); 9498dabb742SStefan Behrens WARN_ON(ret); 950733f4fbbSStefan Behrens 951546adb0dSJan Schmidt ret = btrfs_run_qgroups(trans, root->fs_info); 952546adb0dSJan Schmidt BUG_ON(ret); 953546adb0dSJan Schmidt 954546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 955546adb0dSJan Schmidt ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 956546adb0dSJan Schmidt BUG_ON(ret); 957546adb0dSJan Schmidt 9580b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 9590b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 9600b86a832SChris Mason list_del_init(next); 9610b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 96287ef2bb4SChris Mason 96349b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 96449b25e05SJeff Mahoney if (ret) 96549b25e05SJeff Mahoney return ret; 96679154b1bSChris Mason } 967276e680dSYan Zheng 968276e680dSYan Zheng down_write(&fs_info->extent_commit_sem); 969276e680dSYan Zheng switch_commit_root(fs_info->extent_root); 970276e680dSYan Zheng up_write(&fs_info->extent_commit_sem); 971276e680dSYan Zheng 9728dabb742SStefan Behrens btrfs_after_dev_replace_commit(fs_info); 9738dabb742SStefan Behrens 97479154b1bSChris Mason return 0; 97579154b1bSChris Mason } 97679154b1bSChris Mason 977d352ac68SChris Mason /* 978d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 979d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 980d352ac68SChris Mason * be deleted 981d352ac68SChris Mason */ 9825d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root) 9835eda7b5eSChris Mason { 984a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 9859d1a2a3aSDavid Sterba list_add_tail(&root->root_list, &root->fs_info->dead_roots); 986a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 9875eda7b5eSChris Mason return 0; 9885eda7b5eSChris Mason } 9895eda7b5eSChris Mason 990d352ac68SChris Mason /* 9915d4f98a2SYan Zheng * update all the cowonly tree roots on disk 992d352ac68SChris Mason */ 9935d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 9945d4f98a2SYan Zheng struct btrfs_root *root) 9950f7d52f4SChris Mason { 9960f7d52f4SChris Mason struct btrfs_root *gang[8]; 9975d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 9980f7d52f4SChris Mason int i; 9990f7d52f4SChris Mason int ret; 100054aa1f4dSChris Mason int err = 0; 100154aa1f4dSChris Mason 1002a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 10030f7d52f4SChris Mason while (1) { 10045d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 10055d4f98a2SYan Zheng (void **)gang, 0, 10060f7d52f4SChris Mason ARRAY_SIZE(gang), 10070f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 10080f7d52f4SChris Mason if (ret == 0) 10090f7d52f4SChris Mason break; 10100f7d52f4SChris Mason for (i = 0; i < ret; i++) { 10110f7d52f4SChris Mason root = gang[i]; 10125d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 10132619ba1fSChris Mason (unsigned long)root->root_key.objectid, 10140f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1015a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 101631153d81SYan Zheng 1017e02119d5SChris Mason btrfs_free_log(trans, root); 10185d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1019d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 1020e02119d5SChris Mason 102182d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 102282d5902dSLi Zefan 1023f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1024f1ebcc74SLiu Bo root->force_cow = 0; 1025f1ebcc74SLiu Bo smp_wmb(); 1026f1ebcc74SLiu Bo 1027978d910dSYan Zheng if (root->commit_root != root->node) { 1028581bb050SLi Zefan mutex_lock(&root->fs_commit_mutex); 1029817d52f8SJosef Bacik switch_commit_root(root); 1030581bb050SLi Zefan btrfs_unpin_free_ino(root); 1031581bb050SLi Zefan mutex_unlock(&root->fs_commit_mutex); 1032581bb050SLi Zefan 1033978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1034978d910dSYan Zheng root->node); 1035978d910dSYan Zheng } 103631153d81SYan Zheng 10375d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 10380f7d52f4SChris Mason &root->root_key, 10390f7d52f4SChris Mason &root->root_item); 1040a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 104154aa1f4dSChris Mason if (err) 104254aa1f4dSChris Mason break; 10430f7d52f4SChris Mason } 10449f3a7427SChris Mason } 1045a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 104654aa1f4dSChris Mason return err; 10470f7d52f4SChris Mason } 10480f7d52f4SChris Mason 1049d352ac68SChris Mason /* 1050de78b51aSEric Sandeen * defrag a given btree. 1051de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1052d352ac68SChris Mason */ 1053de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1054e9d0b13bSChris Mason { 1055e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1056e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 10578929ecfaSYan, Zheng int ret; 1058e9d0b13bSChris Mason 10598929ecfaSYan, Zheng if (xchg(&root->defrag_running, 1)) 1060e9d0b13bSChris Mason return 0; 10618929ecfaSYan, Zheng 10626b80053dSChris Mason while (1) { 10638929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 10648929ecfaSYan, Zheng if (IS_ERR(trans)) 10658929ecfaSYan, Zheng return PTR_ERR(trans); 10668929ecfaSYan, Zheng 1067de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 10688929ecfaSYan, Zheng 1069e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 1070b53d3f5dSLiu Bo btrfs_btree_balance_dirty(info->tree_root); 1071e9d0b13bSChris Mason cond_resched(); 1072e9d0b13bSChris Mason 10737841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 1074e9d0b13bSChris Mason break; 1075210549ebSDavid Sterba 1076210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1077210549ebSDavid Sterba printk(KERN_DEBUG "btrfs: defrag_root cancelled\n"); 1078210549ebSDavid Sterba ret = -EAGAIN; 1079210549ebSDavid Sterba break; 1080210549ebSDavid Sterba } 1081e9d0b13bSChris Mason } 1082e9d0b13bSChris Mason root->defrag_running = 0; 10838929ecfaSYan, Zheng return ret; 1084e9d0b13bSChris Mason } 1085e9d0b13bSChris Mason 1086d352ac68SChris Mason /* 1087d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1088aec8030aSMiao Xie * transaction commit. This does the actual creation. 1089aec8030aSMiao Xie * 1090aec8030aSMiao Xie * Note: 1091aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1092aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1093aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1094d352ac68SChris Mason */ 109580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 10963063d29fSChris Mason struct btrfs_fs_info *fs_info, 10973063d29fSChris Mason struct btrfs_pending_snapshot *pending) 10983063d29fSChris Mason { 10993063d29fSChris Mason struct btrfs_key key; 110080b6794dSChris Mason struct btrfs_root_item *new_root_item; 11013063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 11023063d29fSChris Mason struct btrfs_root *root = pending->root; 11036bdb72deSSage Weil struct btrfs_root *parent_root; 110498c9942aSLiu Bo struct btrfs_block_rsv *rsv; 11056bdb72deSSage Weil struct inode *parent_inode; 110642874b3dSMiao Xie struct btrfs_path *path; 110742874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1108a22285a6SYan, Zheng struct dentry *dentry; 11093063d29fSChris Mason struct extent_buffer *tmp; 1110925baeddSChris Mason struct extent_buffer *old; 11118ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 1112aec8030aSMiao Xie int ret = 0; 1113d68fc57bSYan, Zheng u64 to_reserve = 0; 11146bdb72deSSage Weil u64 index = 0; 1115a22285a6SYan, Zheng u64 objectid; 1116b83cc969SLi Zefan u64 root_flags; 11178ea05e3aSAlexander Block uuid_le new_uuid; 11183063d29fSChris Mason 111942874b3dSMiao Xie path = btrfs_alloc_path(); 112042874b3dSMiao Xie if (!path) { 1121aec8030aSMiao Xie pending->error = -ENOMEM; 1122aec8030aSMiao Xie return 0; 112342874b3dSMiao Xie } 112442874b3dSMiao Xie 112580b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 112680b6794dSChris Mason if (!new_root_item) { 1127aec8030aSMiao Xie pending->error = -ENOMEM; 11286fa9700eSMiao Xie goto root_item_alloc_fail; 112980b6794dSChris Mason } 1130a22285a6SYan, Zheng 1131aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1132aec8030aSMiao Xie if (pending->error) 11336fa9700eSMiao Xie goto no_free_objectid; 11343063d29fSChris Mason 11353fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 1136d68fc57bSYan, Zheng 1137d68fc57bSYan, Zheng if (to_reserve > 0) { 1138aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1139aec8030aSMiao Xie &pending->block_rsv, 114008e007d2SMiao Xie to_reserve, 114108e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1142aec8030aSMiao Xie if (pending->error) 11436fa9700eSMiao Xie goto no_free_objectid; 1144d68fc57bSYan, Zheng } 1145d68fc57bSYan, Zheng 1146aec8030aSMiao Xie pending->error = btrfs_qgroup_inherit(trans, fs_info, 1147aec8030aSMiao Xie root->root_key.objectid, 11486f72c7e2SArne Jansen objectid, pending->inherit); 1149aec8030aSMiao Xie if (pending->error) 11506fa9700eSMiao Xie goto no_free_objectid; 11516f72c7e2SArne Jansen 11523063d29fSChris Mason key.objectid = objectid; 1153a22285a6SYan, Zheng key.offset = (u64)-1; 1154a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 11553063d29fSChris Mason 11566fa9700eSMiao Xie rsv = trans->block_rsv; 1157a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 11582382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 11596bdb72deSSage Weil 1160a22285a6SYan, Zheng dentry = pending->dentry; 1161e9662f70SMiao Xie parent_inode = pending->dir; 1162a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 11637585717fSChris Mason record_root_in_trans(trans, parent_root); 1164a22285a6SYan, Zheng 11656bdb72deSSage Weil /* 11666bdb72deSSage Weil * insert the directory item 11676bdb72deSSage Weil */ 11686bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 116949b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 117042874b3dSMiao Xie 117142874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 117242874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 117342874b3dSMiao Xie btrfs_ino(parent_inode), 117442874b3dSMiao Xie dentry->d_name.name, 117542874b3dSMiao Xie dentry->d_name.len, 0); 117642874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1177fe66a05aSChris Mason pending->error = -EEXIST; 1178aec8030aSMiao Xie goto dir_item_existed; 117942874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 118042874b3dSMiao Xie ret = PTR_ERR(dir_item); 11818732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 11828732d44fSMiao Xie goto fail; 118379787eaaSJeff Mahoney } 118442874b3dSMiao Xie btrfs_release_path(path); 11856bdb72deSSage Weil 1186e999376fSChris Mason /* 1187e999376fSChris Mason * pull in the delayed directory update 1188e999376fSChris Mason * and the delayed inode item 1189e999376fSChris Mason * otherwise we corrupt the FS during 1190e999376fSChris Mason * snapshot 1191e999376fSChris Mason */ 1192e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 11938732d44fSMiao Xie if (ret) { /* Transaction aborted */ 11948732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 11958732d44fSMiao Xie goto fail; 11968732d44fSMiao Xie } 1197e999376fSChris Mason 11987585717fSChris Mason record_root_in_trans(trans, root); 11996bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 12006bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 120108fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 12026bdb72deSSage Weil 1203b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1204b83cc969SLi Zefan if (pending->readonly) 1205b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1206b83cc969SLi Zefan else 1207b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1208b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1209b83cc969SLi Zefan 12108ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 12118ea05e3aSAlexander Block trans->transid); 12128ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 12138ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 12148ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 12158ea05e3aSAlexander Block BTRFS_UUID_SIZE); 121670023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 121770023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 121870023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 12198ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 12208ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 12218ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 12228ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 122370023da2SStefan Behrens } 122470023da2SStefan Behrens new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec); 122570023da2SStefan Behrens new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec); 122670023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 12278ea05e3aSAlexander Block 1228925baeddSChris Mason old = btrfs_lock_root_node(root); 122949b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 123079787eaaSJeff Mahoney if (ret) { 123179787eaaSJeff Mahoney btrfs_tree_unlock(old); 123279787eaaSJeff Mahoney free_extent_buffer(old); 12338732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12348732d44fSMiao Xie goto fail; 123579787eaaSJeff Mahoney } 123649b25e05SJeff Mahoney 12375d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 12383063d29fSChris Mason 123949b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 124079787eaaSJeff Mahoney /* clean up in any case */ 1241925baeddSChris Mason btrfs_tree_unlock(old); 1242925baeddSChris Mason free_extent_buffer(old); 12438732d44fSMiao Xie if (ret) { 12448732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12458732d44fSMiao Xie goto fail; 12468732d44fSMiao Xie } 12473063d29fSChris Mason 1248f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 1249f1ebcc74SLiu Bo root->force_cow = 1; 1250f1ebcc74SLiu Bo smp_wmb(); 1251f1ebcc74SLiu Bo 12525d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1253a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1254a22285a6SYan, Zheng key.offset = trans->transid; 1255a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1256925baeddSChris Mason btrfs_tree_unlock(tmp); 12573063d29fSChris Mason free_extent_buffer(tmp); 12588732d44fSMiao Xie if (ret) { 12598732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12608732d44fSMiao Xie goto fail; 12618732d44fSMiao Xie } 12620660b5afSChris Mason 1263a22285a6SYan, Zheng /* 1264a22285a6SYan, Zheng * insert root back/forward references 1265a22285a6SYan, Zheng */ 1266a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 1267a22285a6SYan, Zheng parent_root->root_key.objectid, 126833345d01SLi Zefan btrfs_ino(parent_inode), index, 1269a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 12708732d44fSMiao Xie if (ret) { 12718732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12728732d44fSMiao Xie goto fail; 12738732d44fSMiao Xie } 1274a22285a6SYan, Zheng 1275a22285a6SYan, Zheng key.offset = (u64)-1; 1276a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 127779787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 127879787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 12798732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12808732d44fSMiao Xie goto fail; 128179787eaaSJeff Mahoney } 1282d68fc57bSYan, Zheng 128349b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 12848732d44fSMiao Xie if (ret) { 12858732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12868732d44fSMiao Xie goto fail; 12878732d44fSMiao Xie } 1288361048f5SMiao Xie 1289361048f5SMiao Xie ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 12908732d44fSMiao Xie if (ret) { 12918732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 12928732d44fSMiao Xie goto fail; 12938732d44fSMiao Xie } 129442874b3dSMiao Xie 129542874b3dSMiao Xie ret = btrfs_insert_dir_item(trans, parent_root, 129642874b3dSMiao Xie dentry->d_name.name, dentry->d_name.len, 129742874b3dSMiao Xie parent_inode, &key, 129842874b3dSMiao Xie BTRFS_FT_DIR, index); 129942874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 13009c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 13018732d44fSMiao Xie if (ret) { 13028732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13038732d44fSMiao Xie goto fail; 13048732d44fSMiao Xie } 130542874b3dSMiao Xie 130642874b3dSMiao Xie btrfs_i_size_write(parent_inode, parent_inode->i_size + 130742874b3dSMiao Xie dentry->d_name.len * 2); 130842874b3dSMiao Xie parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 1309be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 131042874b3dSMiao Xie if (ret) 13118732d44fSMiao Xie btrfs_abort_transaction(trans, root, ret); 13123063d29fSChris Mason fail: 1313aec8030aSMiao Xie pending->error = ret; 1314aec8030aSMiao Xie dir_item_existed: 131598c9942aSLiu Bo trans->block_rsv = rsv; 13162382c5ccSLiu Bo trans->bytes_reserved = 0; 13176fa9700eSMiao Xie no_free_objectid: 13186fa9700eSMiao Xie kfree(new_root_item); 13196fa9700eSMiao Xie root_item_alloc_fail: 132042874b3dSMiao Xie btrfs_free_path(path); 132149b25e05SJeff Mahoney return ret; 13223063d29fSChris Mason } 13233063d29fSChris Mason 1324d352ac68SChris Mason /* 1325d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1326d352ac68SChris Mason */ 132780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 13283063d29fSChris Mason struct btrfs_fs_info *fs_info) 13293063d29fSChris Mason { 1330aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 13313063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1332aec8030aSMiao Xie int ret = 0; 13333de4586cSChris Mason 1334aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1335aec8030aSMiao Xie list_del(&pending->list); 1336aec8030aSMiao Xie ret = create_pending_snapshot(trans, fs_info, pending); 1337aec8030aSMiao Xie if (ret) 1338aec8030aSMiao Xie break; 1339aec8030aSMiao Xie } 1340aec8030aSMiao Xie return ret; 13413de4586cSChris Mason } 13423de4586cSChris Mason 13435d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 13445d4f98a2SYan Zheng { 13455d4f98a2SYan Zheng struct btrfs_root_item *root_item; 13465d4f98a2SYan Zheng struct btrfs_super_block *super; 13475d4f98a2SYan Zheng 13486c41761fSDavid Sterba super = root->fs_info->super_copy; 13495d4f98a2SYan Zheng 13505d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 13515d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 13525d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 13535d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 13545d4f98a2SYan Zheng 13555d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 13565d4f98a2SYan Zheng super->root = root_item->bytenr; 13575d4f98a2SYan Zheng super->generation = root_item->generation; 13585d4f98a2SYan Zheng super->root_level = root_item->level; 135973bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 13600af3d00bSJosef Bacik super->cache_generation = root_item->generation; 13615d4f98a2SYan Zheng } 13625d4f98a2SYan Zheng 1363f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1364f36f3042SChris Mason { 13654a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1366f36f3042SChris Mason int ret = 0; 13674a9d8bdeSMiao Xie 1368a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 13694a9d8bdeSMiao Xie trans = info->running_transaction; 13704a9d8bdeSMiao Xie if (trans) 13714a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1372a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1373f36f3042SChris Mason return ret; 1374f36f3042SChris Mason } 1375f36f3042SChris Mason 13768929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 13778929ecfaSYan, Zheng { 13784a9d8bdeSMiao Xie struct btrfs_transaction *trans; 13798929ecfaSYan, Zheng int ret = 0; 13804a9d8bdeSMiao Xie 1381a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 13824a9d8bdeSMiao Xie trans = info->running_transaction; 13834a9d8bdeSMiao Xie if (trans) 13844a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1385a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 13868929ecfaSYan, Zheng return ret; 13878929ecfaSYan, Zheng } 13888929ecfaSYan, Zheng 1389bb9c12c9SSage Weil /* 1390bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1391bb9c12c9SSage Weil * transaction joins 1392bb9c12c9SSage Weil */ 1393bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1394bb9c12c9SSage Weil struct btrfs_transaction *trans) 1395bb9c12c9SSage Weil { 13964a9d8bdeSMiao Xie wait_event(root->fs_info->transaction_blocked_wait, 1397*501407aaSJosef Bacik trans->state >= TRANS_STATE_COMMIT_START || 1398*501407aaSJosef Bacik trans->aborted); 1399bb9c12c9SSage Weil } 1400bb9c12c9SSage Weil 1401bb9c12c9SSage Weil /* 1402bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1403bb9c12c9SSage Weil * caller holds ref. 1404bb9c12c9SSage Weil */ 1405bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1406bb9c12c9SSage Weil struct btrfs_transaction *trans) 1407bb9c12c9SSage Weil { 140872d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 1409*501407aaSJosef Bacik trans->state >= TRANS_STATE_UNBLOCKED || 1410*501407aaSJosef Bacik trans->aborted); 1411bb9c12c9SSage Weil } 1412bb9c12c9SSage Weil 1413bb9c12c9SSage Weil /* 1414bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1415bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1416bb9c12c9SSage Weil */ 1417bb9c12c9SSage Weil struct btrfs_async_commit { 1418bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1419bb9c12c9SSage Weil struct btrfs_root *root; 14207892b5afSMiao Xie struct work_struct work; 1421bb9c12c9SSage Weil }; 1422bb9c12c9SSage Weil 1423bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1424bb9c12c9SSage Weil { 1425bb9c12c9SSage Weil struct btrfs_async_commit *ac = 14267892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1427bb9c12c9SSage Weil 14286fc4e354SSage Weil /* 14296fc4e354SSage Weil * We've got freeze protection passed with the transaction. 14306fc4e354SSage Weil * Tell lockdep about it. 14316fc4e354SSage Weil */ 1432ff7c1d33SMiao Xie if (ac->newtrans->type < TRANS_JOIN_NOLOCK) 14336fc4e354SSage Weil rwsem_acquire_read( 14346fc4e354SSage Weil &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 14356fc4e354SSage Weil 0, 1, _THIS_IP_); 14366fc4e354SSage Weil 1437e209db7aSSage Weil current->journal_info = ac->newtrans; 1438e209db7aSSage Weil 1439bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1440bb9c12c9SSage Weil kfree(ac); 1441bb9c12c9SSage Weil } 1442bb9c12c9SSage Weil 1443bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1444bb9c12c9SSage Weil struct btrfs_root *root, 1445bb9c12c9SSage Weil int wait_for_unblock) 1446bb9c12c9SSage Weil { 1447bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1448bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1449bb9c12c9SSage Weil 1450bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1451db5b493aSTsutomu Itoh if (!ac) 1452db5b493aSTsutomu Itoh return -ENOMEM; 1453bb9c12c9SSage Weil 14547892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 1455bb9c12c9SSage Weil ac->root = root; 14567a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 14573612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 14583612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 14593612b495STsutomu Itoh kfree(ac); 14603612b495STsutomu Itoh return err; 14613612b495STsutomu Itoh } 1462bb9c12c9SSage Weil 1463bb9c12c9SSage Weil /* take transaction reference */ 1464bb9c12c9SSage Weil cur_trans = trans->transaction; 146513c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1466bb9c12c9SSage Weil 1467bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 14686fc4e354SSage Weil 14696fc4e354SSage Weil /* 14706fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 14716fc4e354SSage Weil * async commit thread will be the one to unlock it. 14726fc4e354SSage Weil */ 1473ff7c1d33SMiao Xie if (trans->type < TRANS_JOIN_NOLOCK) 1474ff7c1d33SMiao Xie rwsem_release( 1475ff7c1d33SMiao Xie &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1], 14766fc4e354SSage Weil 1, _THIS_IP_); 14776fc4e354SSage Weil 14787892b5afSMiao Xie schedule_work(&ac->work); 1479bb9c12c9SSage Weil 1480bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1481bb9c12c9SSage Weil if (wait_for_unblock) 1482bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1483bb9c12c9SSage Weil else 1484bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1485bb9c12c9SSage Weil 148638e88054SSage Weil if (current->journal_info == trans) 148738e88054SSage Weil current->journal_info = NULL; 148838e88054SSage Weil 148938e88054SSage Weil put_transaction(cur_trans); 1490bb9c12c9SSage Weil return 0; 1491bb9c12c9SSage Weil } 1492bb9c12c9SSage Weil 149349b25e05SJeff Mahoney 149449b25e05SJeff Mahoney static void cleanup_transaction(struct btrfs_trans_handle *trans, 14957b8b92afSJosef Bacik struct btrfs_root *root, int err) 149649b25e05SJeff Mahoney { 149749b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 1498f094ac32SLiu Bo DEFINE_WAIT(wait); 149949b25e05SJeff Mahoney 150049b25e05SJeff Mahoney WARN_ON(trans->use_count > 1); 150149b25e05SJeff Mahoney 15027b8b92afSJosef Bacik btrfs_abort_transaction(trans, root, err); 15037b8b92afSJosef Bacik 150449b25e05SJeff Mahoney spin_lock(&root->fs_info->trans_lock); 150566b6135bSLiu Bo 150625d8c284SMiao Xie /* 150725d8c284SMiao Xie * If the transaction is removed from the list, it means this 150825d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 150925d8c284SMiao Xie * to call the cleanup function. 151025d8c284SMiao Xie */ 151125d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 151266b6135bSLiu Bo 151349b25e05SJeff Mahoney list_del_init(&cur_trans->list); 1514d7096fc3SJosef Bacik if (cur_trans == root->fs_info->running_transaction) { 15154a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1516f094ac32SLiu Bo spin_unlock(&root->fs_info->trans_lock); 1517f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1518f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1519f094ac32SLiu Bo 1520f094ac32SLiu Bo spin_lock(&root->fs_info->trans_lock); 1521d7096fc3SJosef Bacik } 152249b25e05SJeff Mahoney spin_unlock(&root->fs_info->trans_lock); 152349b25e05SJeff Mahoney 152449b25e05SJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, root); 152549b25e05SJeff Mahoney 15264a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 15274a9d8bdeSMiao Xie if (cur_trans == root->fs_info->running_transaction) 15284a9d8bdeSMiao Xie root->fs_info->running_transaction = NULL; 15294a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 15304a9d8bdeSMiao Xie 153149b25e05SJeff Mahoney put_transaction(cur_trans); 153249b25e05SJeff Mahoney put_transaction(cur_trans); 153349b25e05SJeff Mahoney 153449b25e05SJeff Mahoney trace_btrfs_transaction_commit(root); 153549b25e05SJeff Mahoney 153649b25e05SJeff Mahoney btrfs_scrub_continue(root); 153749b25e05SJeff Mahoney 153849b25e05SJeff Mahoney if (current->journal_info == trans) 153949b25e05SJeff Mahoney current->journal_info = NULL; 154049b25e05SJeff Mahoney 154149b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 154249b25e05SJeff Mahoney } 154349b25e05SJeff Mahoney 1544ca469637SMiao Xie static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans, 1545ca469637SMiao Xie struct btrfs_root *root) 1546ca469637SMiao Xie { 1547ca469637SMiao Xie int ret; 1548ca469637SMiao Xie 1549ca469637SMiao Xie ret = btrfs_run_delayed_items(trans, root); 1550ca469637SMiao Xie if (ret) 1551ca469637SMiao Xie return ret; 1552ca469637SMiao Xie 1553ca469637SMiao Xie /* 1554ca469637SMiao Xie * running the delayed items may have added new refs. account 1555ca469637SMiao Xie * them now so that they hinder processing of more delayed refs 1556ca469637SMiao Xie * as little as possible. 1557ca469637SMiao Xie */ 1558ca469637SMiao Xie btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); 1559ca469637SMiao Xie 1560ca469637SMiao Xie /* 1561ca469637SMiao Xie * rename don't use btrfs_join_transaction, so, once we 1562ca469637SMiao Xie * set the transaction to blocked above, we aren't going 1563ca469637SMiao Xie * to get any new ordered operations. We can safely run 1564ca469637SMiao Xie * it here and no for sure that nothing new will be added 1565ca469637SMiao Xie * to the list 1566ca469637SMiao Xie */ 1567569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 1); 1568ca469637SMiao Xie 1569eebc6084SMiao Xie return ret; 1570ca469637SMiao Xie } 1571ca469637SMiao Xie 157282436617SMiao Xie static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info) 157382436617SMiao Xie { 157482436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 157582436617SMiao Xie return btrfs_start_all_delalloc_inodes(fs_info, 1); 157682436617SMiao Xie return 0; 157782436617SMiao Xie } 157882436617SMiao Xie 157982436617SMiao Xie static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info) 158082436617SMiao Xie { 158182436617SMiao Xie if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT)) 158282436617SMiao Xie btrfs_wait_all_ordered_extents(fs_info, 1); 158382436617SMiao Xie } 158482436617SMiao Xie 158579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 158679154b1bSChris Mason struct btrfs_root *root) 158779154b1bSChris Mason { 158849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 15898fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 159025287e0aSMiao Xie int ret; 159179154b1bSChris Mason 1592569e0f35SJosef Bacik ret = btrfs_run_ordered_operations(trans, root, 0); 159325287e0aSMiao Xie if (ret) { 159425287e0aSMiao Xie btrfs_abort_transaction(trans, root, ret); 1595e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1596e4a2bcacSJosef Bacik return ret; 159725287e0aSMiao Xie } 159825287e0aSMiao Xie 15998d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 16008d25a086SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 160125287e0aSMiao Xie ret = cur_trans->aborted; 1602e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1603e4a2bcacSJosef Bacik return ret; 160425287e0aSMiao Xie } 160549b25e05SJeff Mahoney 160656bec294SChris Mason /* make a pass through all the delayed refs we have so far 160756bec294SChris Mason * any runnings procs may add more while we are here 160856bec294SChris Mason */ 160956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1610e4a2bcacSJosef Bacik if (ret) { 1611e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1612e4a2bcacSJosef Bacik return ret; 1613e4a2bcacSJosef Bacik } 161456bec294SChris Mason 16150e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 16160e721106SJosef Bacik trans->block_rsv = NULL; 1617272d26d0SMiao Xie if (trans->qgroup_reserved) { 1618272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1619272d26d0SMiao Xie trans->qgroup_reserved = 0; 1620272d26d0SMiao Xie } 16210e721106SJosef Bacik 1622b7ec40d7SChris Mason cur_trans = trans->transaction; 162349b25e05SJeff Mahoney 162456bec294SChris Mason /* 162556bec294SChris Mason * set the flushing flag so procs in this transaction have to 162656bec294SChris Mason * start sending their work down. 162756bec294SChris Mason */ 1628b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 162956bec294SChris Mason 1630ea658badSJosef Bacik if (!list_empty(&trans->new_bgs)) 1631ea658badSJosef Bacik btrfs_create_pending_block_groups(trans, root); 1632ea658badSJosef Bacik 1633c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 1634e4a2bcacSJosef Bacik if (ret) { 1635e4a2bcacSJosef Bacik btrfs_end_transaction(trans, root); 1636e4a2bcacSJosef Bacik return ret; 1637e4a2bcacSJosef Bacik } 163856bec294SChris Mason 16394a9d8bdeSMiao Xie spin_lock(&root->fs_info->trans_lock); 16404a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 16414a9d8bdeSMiao Xie spin_unlock(&root->fs_info->trans_lock); 164213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 164349b25e05SJeff Mahoney ret = btrfs_end_transaction(trans, root); 1644ccd467d6SChris Mason 1645b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 164615ee9bc7SJosef Bacik 164779154b1bSChris Mason put_transaction(cur_trans); 164815ee9bc7SJosef Bacik 164949b25e05SJeff Mahoney return ret; 165079154b1bSChris Mason } 16514313b399SChris Mason 16524a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 1653bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1654bb9c12c9SSage Weil 1655ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1656ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1657ccd467d6SChris Mason struct btrfs_transaction, list); 16584a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 165913c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1660a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1661ccd467d6SChris Mason 1662ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1663ccd467d6SChris Mason 166415ee9bc7SJosef Bacik put_transaction(prev_trans); 1665a4abeea4SJosef Bacik } else { 1666a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1667ccd467d6SChris Mason } 1668a4abeea4SJosef Bacik } else { 1669a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1670ccd467d6SChris Mason } 167115ee9bc7SJosef Bacik 16720860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 16730860adfdSMiao Xie 167482436617SMiao Xie ret = btrfs_start_delalloc_flush(root->fs_info); 167582436617SMiao Xie if (ret) 167682436617SMiao Xie goto cleanup_transaction; 167782436617SMiao Xie 1678ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 167949b25e05SJeff Mahoney if (ret) 168049b25e05SJeff Mahoney goto cleanup_transaction; 168116cdcec7SMiao Xie 1682581227d0SMiao Xie wait_event(cur_trans->writer_wait, 1683581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 1684ed3b3d31SChris Mason 1685581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 1686ca469637SMiao Xie ret = btrfs_flush_all_pending_stuffs(trans, root); 1687ca469637SMiao Xie if (ret) 1688ca469637SMiao Xie goto cleanup_transaction; 1689ca469637SMiao Xie 169082436617SMiao Xie btrfs_wait_delalloc_flush(root->fs_info); 1691ed0ca140SJosef Bacik /* 1692ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1693ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 16944a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 1695ed0ca140SJosef Bacik */ 1696a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 16974a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 1698a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1699ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1700ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 170115ee9bc7SJosef Bacik 17022cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 17032cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 17042cba30f1SMiao Xie ret = cur_trans->aborted; 17052cba30f1SMiao Xie goto cleanup_transaction; 17062cba30f1SMiao Xie } 17077585717fSChris Mason /* 17087585717fSChris Mason * the reloc mutex makes sure that we stop 17097585717fSChris Mason * the balancing code from coming in and moving 17107585717fSChris Mason * extents around in the middle of the commit 17117585717fSChris Mason */ 17127585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 17137585717fSChris Mason 171442874b3dSMiao Xie /* 171542874b3dSMiao Xie * We needn't worry about the delayed items because we will 171642874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 171742874b3dSMiao Xie * core function of the snapshot creation. 171842874b3dSMiao Xie */ 171942874b3dSMiao Xie ret = create_pending_snapshots(trans, root->fs_info); 172049b25e05SJeff Mahoney if (ret) { 172149b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 172249b25e05SJeff Mahoney goto cleanup_transaction; 172349b25e05SJeff Mahoney } 17243063d29fSChris Mason 172542874b3dSMiao Xie /* 172642874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 172742874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 172842874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 172942874b3dSMiao Xie * them. 173042874b3dSMiao Xie * 173142874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 173242874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 173342874b3dSMiao Xie * the nodes and leaves. 173442874b3dSMiao Xie */ 173542874b3dSMiao Xie ret = btrfs_run_delayed_items(trans, root); 173649b25e05SJeff Mahoney if (ret) { 173749b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 173849b25e05SJeff Mahoney goto cleanup_transaction; 173949b25e05SJeff Mahoney } 174016cdcec7SMiao Xie 174156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 174249b25e05SJeff Mahoney if (ret) { 174349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->reloc_mutex); 174449b25e05SJeff Mahoney goto cleanup_transaction; 174549b25e05SJeff Mahoney } 174656bec294SChris Mason 1747e999376fSChris Mason /* 1748e999376fSChris Mason * make sure none of the code above managed to slip in a 1749e999376fSChris Mason * delayed item 1750e999376fSChris Mason */ 1751e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1752e999376fSChris Mason 17532c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1754dc17ff8fSChris Mason 1755a2de733cSArne Jansen btrfs_scrub_pause(root); 1756e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1757e02119d5SChris Mason * various roots consistent with each other. Every pointer 1758e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1759e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1760e02119d5SChris Mason * the tree logging code from jumping in and changing any 1761e02119d5SChris Mason * of the trees. 1762e02119d5SChris Mason * 1763e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1764e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1765e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1766e02119d5SChris Mason * from now until after the super is written, we avoid races 1767e02119d5SChris Mason * with the tree-log code. 1768e02119d5SChris Mason */ 1769e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 17701a40e23bSZheng Yan 17715d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 177249b25e05SJeff Mahoney if (ret) { 177349b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1774871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 177549b25e05SJeff Mahoney goto cleanup_transaction; 177649b25e05SJeff Mahoney } 177754aa1f4dSChris Mason 17785d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1779e02119d5SChris Mason * safe to free the root of tree log roots 1780e02119d5SChris Mason */ 1781e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1782e02119d5SChris Mason 17835d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 178449b25e05SJeff Mahoney if (ret) { 178549b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 1786871383beSDavid Sterba mutex_unlock(&root->fs_info->reloc_mutex); 178749b25e05SJeff Mahoney goto cleanup_transaction; 178849b25e05SJeff Mahoney } 178954aa1f4dSChris Mason 17902cba30f1SMiao Xie /* 17912cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 17922cba30f1SMiao Xie * update ->aborted, check it. 17932cba30f1SMiao Xie */ 17942cba30f1SMiao Xie if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 17952cba30f1SMiao Xie ret = cur_trans->aborted; 17962cba30f1SMiao Xie mutex_unlock(&root->fs_info->tree_log_mutex); 17972cba30f1SMiao Xie mutex_unlock(&root->fs_info->reloc_mutex); 17982cba30f1SMiao Xie goto cleanup_transaction; 17992cba30f1SMiao Xie } 18002cba30f1SMiao Xie 180111833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 180211833d66SYan Zheng 180378fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 18045f39d397SChris Mason 18055d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 18065d4f98a2SYan Zheng root->fs_info->tree_root->node); 1807817d52f8SJosef Bacik switch_commit_root(root->fs_info->tree_root); 18085d4f98a2SYan Zheng 18095d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 18105d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1811817d52f8SJosef Bacik switch_commit_root(root->fs_info->chunk_root); 18125d4f98a2SYan Zheng 1813edf39272SJan Schmidt assert_qgroups_uptodate(trans); 18145d4f98a2SYan Zheng update_super_roots(root); 1815e02119d5SChris Mason 1816e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 18176c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 18186c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 1819e02119d5SChris Mason } 1820e02119d5SChris Mason 18216c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 18226c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1823ccd467d6SChris Mason 1824a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 18254a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 1826a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1827a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 18287585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1829b7ec40d7SChris Mason 1830f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1831e6dcd2dcSChris Mason 183279154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 183349b25e05SJeff Mahoney if (ret) { 183449b25e05SJeff Mahoney btrfs_error(root->fs_info, ret, 183508748810SDavid Sterba "Error while writing out transaction"); 183649b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 183749b25e05SJeff Mahoney goto cleanup_transaction; 183849b25e05SJeff Mahoney } 183949b25e05SJeff Mahoney 184049b25e05SJeff Mahoney ret = write_ctree_super(trans, root, 0); 184149b25e05SJeff Mahoney if (ret) { 184249b25e05SJeff Mahoney mutex_unlock(&root->fs_info->tree_log_mutex); 184349b25e05SJeff Mahoney goto cleanup_transaction; 184449b25e05SJeff Mahoney } 18454313b399SChris Mason 1846e02119d5SChris Mason /* 1847e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1848e02119d5SChris Mason * to go about their business 1849e02119d5SChris Mason */ 1850e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1851e02119d5SChris Mason 185211833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 18534313b399SChris Mason 185415ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 18554a9d8bdeSMiao Xie /* 18564a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 18574a9d8bdeSMiao Xie * which can change it. 18584a9d8bdeSMiao Xie */ 18594a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 18602c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 18613de4586cSChris Mason 1862a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 186313c5a93eSJosef Bacik list_del_init(&cur_trans->list); 1864a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1865a4abeea4SJosef Bacik 186679154b1bSChris Mason put_transaction(cur_trans); 186778fae27eSChris Mason put_transaction(cur_trans); 186858176a96SJosef Bacik 18690860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 1870b2b5ef5cSJan Kara sb_end_intwrite(root->fs_info->sb); 1871b2b5ef5cSJan Kara 18721abe9b8aSliubo trace_btrfs_transaction_commit(root); 18731abe9b8aSliubo 1874a2de733cSArne Jansen btrfs_scrub_continue(root); 1875a2de733cSArne Jansen 18769ed74f2dSJosef Bacik if (current->journal_info == trans) 18779ed74f2dSJosef Bacik current->journal_info = NULL; 18789ed74f2dSJosef Bacik 18792c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 188024bbcf04SYan, Zheng 188124bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 188224bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 188324bbcf04SYan, Zheng 188479154b1bSChris Mason return ret; 188549b25e05SJeff Mahoney 188649b25e05SJeff Mahoney cleanup_transaction: 18870e721106SJosef Bacik btrfs_trans_release_metadata(trans, root); 18880e721106SJosef Bacik trans->block_rsv = NULL; 1889272d26d0SMiao Xie if (trans->qgroup_reserved) { 1890272d26d0SMiao Xie btrfs_qgroup_free(root, trans->qgroup_reserved); 1891272d26d0SMiao Xie trans->qgroup_reserved = 0; 1892272d26d0SMiao Xie } 1893c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 189449b25e05SJeff Mahoney if (current->journal_info == trans) 189549b25e05SJeff Mahoney current->journal_info = NULL; 18967b8b92afSJosef Bacik cleanup_transaction(trans, root, ret); 189749b25e05SJeff Mahoney 189849b25e05SJeff Mahoney return ret; 189979154b1bSChris Mason } 190079154b1bSChris Mason 1901d352ac68SChris Mason /* 19029d1a2a3aSDavid Sterba * return < 0 if error 19039d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 19049d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 19059d1a2a3aSDavid Sterba * 19069d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 19079d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 19089d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 19099d1a2a3aSDavid Sterba * few seconds later. 1910d352ac68SChris Mason */ 19119d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 1912e9d0b13bSChris Mason { 19139d1a2a3aSDavid Sterba int ret; 19145d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1915e9d0b13bSChris Mason 1916a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 19179d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 19189d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 19199d1a2a3aSDavid Sterba return 0; 19209d1a2a3aSDavid Sterba } 19219d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 19229d1a2a3aSDavid Sterba struct btrfs_root, root_list); 19239d1a2a3aSDavid Sterba list_del(&root->root_list); 1924a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 19255d4f98a2SYan Zheng 19269d1a2a3aSDavid Sterba pr_debug("btrfs: cleaner removing %llu\n", 19279d1a2a3aSDavid Sterba (unsigned long long)root->objectid); 192876dda93cSYan, Zheng 192916cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 193016cdcec7SMiao Xie 193176dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 193276dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 19332c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 193476dda93cSYan, Zheng else 19352c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 19369d1a2a3aSDavid Sterba /* 19379d1a2a3aSDavid Sterba * If we encounter a transaction abort during snapshot cleaning, we 19389d1a2a3aSDavid Sterba * don't want to crash here 19399d1a2a3aSDavid Sterba */ 19409d1a2a3aSDavid Sterba BUG_ON(ret < 0 && ret != -EAGAIN && ret != -EROFS); 19419d1a2a3aSDavid Sterba return 1; 1942e9d0b13bSChris Mason } 1943