1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 679154b1bSChris Mason #include <linux/fs.h> 75a0e3ad6STejun Heo #include <linux/slab.h> 834088780SChris Mason #include <linux/sched.h> 9d3c2fdcfSChris Mason #include <linux/writeback.h> 105f39d397SChris Mason #include <linux/pagemap.h> 115f2cc086SChris Mason #include <linux/blkdev.h> 128ea05e3aSAlexander Block #include <linux/uuid.h> 1379154b1bSChris Mason #include "ctree.h" 1479154b1bSChris Mason #include "disk-io.h" 1579154b1bSChris Mason #include "transaction.h" 16925baeddSChris Mason #include "locking.h" 17e02119d5SChris Mason #include "tree-log.h" 18581bb050SLi Zefan #include "inode-map.h" 19733f4fbbSStefan Behrens #include "volumes.h" 208dabb742SStefan Behrens #include "dev-replace.h" 21fcebe456SJosef Bacik #include "qgroup.h" 2279154b1bSChris Mason 230f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 240f7d52f4SChris Mason 25e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 264a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 27bcf3a3e7SNikolay Borisov [TRANS_STATE_BLOCKED] = __TRANS_START, 28bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), 29bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | 304a9d8bdeSMiao Xie __TRANS_ATTACH | 314a9d8bdeSMiao Xie __TRANS_JOIN), 32bcf3a3e7SNikolay Borisov [TRANS_STATE_UNBLOCKED] = (__TRANS_START | 334a9d8bdeSMiao Xie __TRANS_ATTACH | 344a9d8bdeSMiao Xie __TRANS_JOIN | 354a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 36bcf3a3e7SNikolay Borisov [TRANS_STATE_COMPLETED] = (__TRANS_START | 374a9d8bdeSMiao Xie __TRANS_ATTACH | 384a9d8bdeSMiao Xie __TRANS_JOIN | 394a9d8bdeSMiao Xie __TRANS_JOIN_NOLOCK), 404a9d8bdeSMiao Xie }; 414a9d8bdeSMiao Xie 42724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 4379154b1bSChris Mason { 449b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 459b64f57dSElena Reshetova if (refcount_dec_and_test(&transaction->use_count)) { 46a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 475c9d028bSLiu Bo WARN_ON(!RB_EMPTY_ROOT( 485c9d028bSLiu Bo &transaction->delayed_refs.href_root.rb_root)); 491262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 50ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 51ab8d0fc4SJeff Mahoney "pending csums is %llu", 521262133bSJosef Bacik transaction->delayed_refs.pending_csums); 537785a663SFilipe Manana /* 547785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 557785a663SFilipe Manana * because the transaction was aborted and a commit did not 567785a663SFilipe Manana * happen (things failed before writing the new superblock 577785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 587785a663SFilipe Manana * discard the physical locations of the block groups. 597785a663SFilipe Manana */ 607785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 617785a663SFilipe Manana struct btrfs_block_group_cache *cache; 627785a663SFilipe Manana 637785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 647785a663SFilipe Manana struct btrfs_block_group_cache, 657785a663SFilipe Manana bg_list); 667785a663SFilipe Manana list_del_init(&cache->bg_list); 677785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 687785a663SFilipe Manana btrfs_put_block_group(cache); 697785a663SFilipe Manana } 70bbbf7243SNikolay Borisov WARN_ON(!list_empty(&transaction->dev_update_list)); 714b5faeacSDavid Sterba kfree(transaction); 7279154b1bSChris Mason } 7378fae27eSChris Mason } 7479154b1bSChris Mason 7516916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans) 76817d52f8SJosef Bacik { 7716916a88SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 789e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 799e351cc8SJosef Bacik 809e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 819e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 829e351cc8SJosef Bacik dirty_list) { 839e351cc8SJosef Bacik list_del_init(&root->dirty_list); 84817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 85817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 864fd786e6SMisono Tomohiro if (is_fstree(root->root_key.objectid)) 879e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 8841e7acd3SNikolay Borisov extent_io_tree_release(&root->dirty_log_pages); 89370a11b8SQu Wenruo btrfs_qgroup_clean_swapped_blocks(root); 909e351cc8SJosef Bacik } 912b9dbef2SJosef Bacik 922b9dbef2SJosef Bacik /* We can free old roots now. */ 932b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 942b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 952b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 962b9dbef2SJosef Bacik struct btrfs_root, root_list); 972b9dbef2SJosef Bacik list_del_init(&root->root_list); 982b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 992b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1002b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1012b9dbef2SJosef Bacik } 1022b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1039e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 104817d52f8SJosef Bacik } 105817d52f8SJosef Bacik 1060860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1070860adfdSMiao Xie unsigned int type) 1080860adfdSMiao Xie { 1090860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1100860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1110860adfdSMiao Xie } 1120860adfdSMiao Xie 1130860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1140860adfdSMiao Xie unsigned int type) 1150860adfdSMiao Xie { 1160860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1170860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1180860adfdSMiao Xie } 1190860adfdSMiao Xie 1200860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1210860adfdSMiao Xie unsigned int type) 1220860adfdSMiao Xie { 1230860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1240860adfdSMiao Xie } 1250860adfdSMiao Xie 1260860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1270860adfdSMiao Xie { 1280860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 129178260b2SMiao Xie } 130178260b2SMiao Xie 131d352ac68SChris Mason /* 132*fb6dea26SJosef Bacik * To be called after all the new block groups attached to the transaction 133*fb6dea26SJosef Bacik * handle have been created (btrfs_create_pending_block_groups()). 134*fb6dea26SJosef Bacik */ 135*fb6dea26SJosef Bacik void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) 136*fb6dea26SJosef Bacik { 137*fb6dea26SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 138*fb6dea26SJosef Bacik 139*fb6dea26SJosef Bacik if (!trans->chunk_bytes_reserved) 140*fb6dea26SJosef Bacik return; 141*fb6dea26SJosef Bacik 142*fb6dea26SJosef Bacik WARN_ON_ONCE(!list_empty(&trans->new_bgs)); 143*fb6dea26SJosef Bacik 144*fb6dea26SJosef Bacik btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv, 145*fb6dea26SJosef Bacik trans->chunk_bytes_reserved); 146*fb6dea26SJosef Bacik trans->chunk_bytes_reserved = 0; 147*fb6dea26SJosef Bacik } 148*fb6dea26SJosef Bacik 149*fb6dea26SJosef Bacik /* 150d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 151d352ac68SChris Mason */ 1522ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1532ff7e61eSJeff Mahoney unsigned int type) 15479154b1bSChris Mason { 15579154b1bSChris Mason struct btrfs_transaction *cur_trans; 156a4abeea4SJosef Bacik 15719ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 158d43317dcSChris Mason loop: 15949b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 16087533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 16119ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16249b25e05SJeff Mahoney return -EROFS; 16349b25e05SJeff Mahoney } 16449b25e05SJeff Mahoney 16519ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 166a4abeea4SJosef Bacik if (cur_trans) { 167871383beSDavid Sterba if (cur_trans->aborted) { 16819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16949b25e05SJeff Mahoney return cur_trans->aborted; 170871383beSDavid Sterba } 1714a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 172178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 173178260b2SMiao Xie return -EBUSY; 174178260b2SMiao Xie } 1759b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 176a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1770860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 17819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 179a4abeea4SJosef Bacik return 0; 180a4abeea4SJosef Bacik } 18119ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 182a4abeea4SJosef Bacik 183354aa0fbSMiao Xie /* 184354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 185354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 186354aa0fbSMiao Xie */ 187354aa0fbSMiao Xie if (type == TRANS_ATTACH) 188354aa0fbSMiao Xie return -ENOENT; 189354aa0fbSMiao Xie 1904a9d8bdeSMiao Xie /* 1914a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1924a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1934a9d8bdeSMiao Xie */ 1944a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1954a9d8bdeSMiao Xie 1964b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS); 197db5b493aSTsutomu Itoh if (!cur_trans) 198db5b493aSTsutomu Itoh return -ENOMEM; 199d43317dcSChris Mason 20019ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 20119ae4e81SJan Schmidt if (fs_info->running_transaction) { 202d43317dcSChris Mason /* 203d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2044a9d8bdeSMiao Xie * to redo the checks above 205d43317dcSChris Mason */ 2064b5faeacSDavid Sterba kfree(cur_trans); 207d43317dcSChris Mason goto loop; 20887533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 209e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2104b5faeacSDavid Sterba kfree(cur_trans); 2117b8b92afSJosef Bacik return -EROFS; 212a4abeea4SJosef Bacik } 213d43317dcSChris Mason 214ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 21513c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2160860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 21779154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 21879154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 2194a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 220a4abeea4SJosef Bacik /* 221a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 222a4abeea4SJosef Bacik * commit the transaction. 223a4abeea4SJosef Bacik */ 2249b64f57dSElena Reshetova refcount_set(&cur_trans->use_count, 2); 2253204d33cSJosef Bacik cur_trans->flags = 0; 226afd48513SArnd Bergmann cur_trans->start_time = ktime_get_seconds(); 22756bec294SChris Mason 228a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 229a099d0fdSAlexandru Moise 2305c9d028bSLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; 2313368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 232d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 23320b297d6SJan Schmidt 23420b297d6SJan Schmidt /* 23520b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 23620b297d6SJan Schmidt * the log must never go across transaction boundaries. 23720b297d6SJan Schmidt */ 23820b297d6SJan Schmidt smp_mb(); 23931b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2405d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 24131b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2425d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n"); 243fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 24420b297d6SJan Schmidt 24556bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 24656bec294SChris Mason 2473063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 248bbbf7243SNikolay Borisov INIT_LIST_HEAD(&cur_trans->dev_update_list); 2499e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 250ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2511bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2522b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2531bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 254ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 255e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2562b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 25719ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 258c258d6e3SQu Wenruo extent_io_tree_init(fs_info, &cur_trans->dirty_pages, 25943eb5f29SQu Wenruo IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); 26019ae4e81SJan Schmidt fs_info->generation++; 26119ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 26219ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 26349b25e05SJeff Mahoney cur_trans->aborted = 0; 26419ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 26515ee9bc7SJosef Bacik 26679154b1bSChris Mason return 0; 26779154b1bSChris Mason } 26879154b1bSChris Mason 269d352ac68SChris Mason /* 270d397712bSChris Mason * this does all the record keeping required to make sure that a reference 271d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 272d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 273d397712bSChris Mason * when the transaction commits 274d352ac68SChris Mason */ 2757585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2766426c7adSQu Wenruo struct btrfs_root *root, 2776426c7adSQu Wenruo int force) 2786702ed49SChris Mason { 2790b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2800b246afaSJeff Mahoney 2816426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2826426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 2830b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 2844d31778aSQu Wenruo WARN_ON(!force && root->commit_root != root->node); 2855d4f98a2SYan Zheng 2867585717fSChris Mason /* 28727cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2887585717fSChris Mason * we have the reloc mutex held now, so there 2897585717fSChris Mason * is only one writer in this function 2907585717fSChris Mason */ 29127cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2927585717fSChris Mason 29327cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2947585717fSChris Mason * they find our root->last_trans update 2957585717fSChris Mason */ 2967585717fSChris Mason smp_wmb(); 2977585717fSChris Mason 2980b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 2996426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 3000b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 301a4abeea4SJosef Bacik return 0; 302a4abeea4SJosef Bacik } 3030b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 3046702ed49SChris Mason (unsigned long)root->root_key.objectid, 3056702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 3060b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3077585717fSChris Mason root->last_trans = trans->transid; 3087585717fSChris Mason 3097585717fSChris Mason /* this is pretty tricky. We don't want to 3107585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3117585717fSChris Mason * unless we're really doing the first setup for this root in 3127585717fSChris Mason * this transaction. 3137585717fSChris Mason * 3147585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3157585717fSChris Mason * if we want to take the expensive mutex. 3167585717fSChris Mason * 3177585717fSChris Mason * But, we have to set root->last_trans before we 3187585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3197585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 32027cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3217585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3227585717fSChris Mason * 3237585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3247585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3257585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3267585717fSChris Mason * done before we pop in the zero below 3277585717fSChris Mason */ 3285d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 329c7548af6SChris Mason smp_mb__before_atomic(); 33027cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3316702ed49SChris Mason } 3325d4f98a2SYan Zheng return 0; 3336702ed49SChris Mason } 3345d4f98a2SYan Zheng 3357585717fSChris Mason 3362b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3372b9dbef2SJosef Bacik struct btrfs_root *root) 3382b9dbef2SJosef Bacik { 3390b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3402b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3412b9dbef2SJosef Bacik 3422b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3432b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3442b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3452b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3462b9dbef2SJosef Bacik 3472b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3480b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3490b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3502b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3512b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3520b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3532b9dbef2SJosef Bacik } 3542b9dbef2SJosef Bacik 3557585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3567585717fSChris Mason struct btrfs_root *root) 3577585717fSChris Mason { 3580b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3590b246afaSJeff Mahoney 36027cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3617585717fSChris Mason return 0; 3627585717fSChris Mason 3637585717fSChris Mason /* 36427cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3657585717fSChris Mason * and barriers 3667585717fSChris Mason */ 3677585717fSChris Mason smp_rmb(); 3687585717fSChris Mason if (root->last_trans == trans->transid && 36927cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3707585717fSChris Mason return 0; 3717585717fSChris Mason 3720b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 3736426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 3740b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 3757585717fSChris Mason 3767585717fSChris Mason return 0; 3777585717fSChris Mason } 3787585717fSChris Mason 3794a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3804a9d8bdeSMiao Xie { 3814a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 382501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 383501407aaSJosef Bacik !trans->aborted); 3844a9d8bdeSMiao Xie } 3854a9d8bdeSMiao Xie 386d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 387d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 388d352ac68SChris Mason * transaction might not be fully on disk. 389d352ac68SChris Mason */ 3902ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 39179154b1bSChris Mason { 392f9295749SChris Mason struct btrfs_transaction *cur_trans; 39379154b1bSChris Mason 3940b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 3950b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 3964a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 3979b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 3980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 39972d63ed6SLi Zefan 4000b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 401501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 402501407aaSJosef Bacik cur_trans->aborted); 403724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 404a4abeea4SJosef Bacik } else { 4050b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 406f9295749SChris Mason } 40737d1aeeeSChris Mason } 40837d1aeeeSChris Mason 4092ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 41037d1aeeeSChris Mason { 4110b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 412a4abeea4SJosef Bacik return 0; 413a4abeea4SJosef Bacik 41492e2f7e3SNikolay Borisov if (type == TRANS_START) 415a4abeea4SJosef Bacik return 1; 416a4abeea4SJosef Bacik 417a22285a6SYan, Zheng return 0; 418a22285a6SYan, Zheng } 419a22285a6SYan, Zheng 42020dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 42120dd2cbfSMiao Xie { 4220b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4230b246afaSJeff Mahoney 4240b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 42527cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 42620dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 42720dd2cbfSMiao Xie root->reloc_root) 42820dd2cbfSMiao Xie return false; 42920dd2cbfSMiao Xie 43020dd2cbfSMiao Xie return true; 43120dd2cbfSMiao Xie } 43220dd2cbfSMiao Xie 43308e007d2SMiao Xie static struct btrfs_trans_handle * 4345aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 435003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 436003d7c59SJeff Mahoney bool enforce_qgroups) 437a22285a6SYan, Zheng { 4380b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 439ba2c4d4eSJosef Bacik struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; 440a22285a6SYan, Zheng struct btrfs_trans_handle *h; 441a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 442b5009945SJosef Bacik u64 num_bytes = 0; 443c5567237SArne Jansen u64 qgroup_reserved = 0; 44420dd2cbfSMiao Xie bool reloc_reserved = false; 44520dd2cbfSMiao Xie int ret; 446acce952bSliubo 44746c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4482755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 44946c4e71eSFilipe Manana 4500b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 451acce952bSliubo return ERR_PTR(-EROFS); 4522a1eb461SJosef Bacik 45346c4e71eSFilipe Manana if (current->journal_info) { 4540860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4552a1eb461SJosef Bacik h = current->journal_info; 456b50fff81SDavid Sterba refcount_inc(&h->use_count); 457b50fff81SDavid Sterba WARN_ON(refcount_read(&h->use_count) > 2); 4582a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4592a1eb461SJosef Bacik h->block_rsv = NULL; 4602a1eb461SJosef Bacik goto got_it; 4612a1eb461SJosef Bacik } 462b5009945SJosef Bacik 463b5009945SJosef Bacik /* 464b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 465b5009945SJosef Bacik * the appropriate flushing if need be. 466b5009945SJosef Bacik */ 467003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 468ba2c4d4eSJosef Bacik struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; 469ba2c4d4eSJosef Bacik u64 delayed_refs_bytes = 0; 470ba2c4d4eSJosef Bacik 4710b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 472733e03a0SQu Wenruo ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, 473003d7c59SJeff Mahoney enforce_qgroups); 474c5567237SArne Jansen if (ret) 475c5567237SArne Jansen return ERR_PTR(ret); 476c5567237SArne Jansen 477ba2c4d4eSJosef Bacik /* 478ba2c4d4eSJosef Bacik * We want to reserve all the bytes we may need all at once, so 479ba2c4d4eSJosef Bacik * we only do 1 enospc flushing cycle per transaction start. We 480ba2c4d4eSJosef Bacik * accomplish this by simply assuming we'll do 2 x num_items 481ba2c4d4eSJosef Bacik * worth of delayed refs updates in this trans handle, and 482ba2c4d4eSJosef Bacik * refill that amount for whatever is missing in the reserve. 483ba2c4d4eSJosef Bacik */ 4840b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 485ba2c4d4eSJosef Bacik if (delayed_refs_rsv->full == 0) { 486ba2c4d4eSJosef Bacik delayed_refs_bytes = num_bytes; 487ba2c4d4eSJosef Bacik num_bytes <<= 1; 488ba2c4d4eSJosef Bacik } 489ba2c4d4eSJosef Bacik 49020dd2cbfSMiao Xie /* 49120dd2cbfSMiao Xie * Do the reservation for the relocation root creation 49220dd2cbfSMiao Xie */ 493ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 4940b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 49520dd2cbfSMiao Xie reloc_reserved = true; 49620dd2cbfSMiao Xie } 49720dd2cbfSMiao Xie 498ba2c4d4eSJosef Bacik ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); 499ba2c4d4eSJosef Bacik if (ret) 500ba2c4d4eSJosef Bacik goto reserve_fail; 501ba2c4d4eSJosef Bacik if (delayed_refs_bytes) { 502ba2c4d4eSJosef Bacik btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, 503ba2c4d4eSJosef Bacik delayed_refs_bytes); 504ba2c4d4eSJosef Bacik num_bytes -= delayed_refs_bytes; 505ba2c4d4eSJosef Bacik } 506ba2c4d4eSJosef Bacik } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && 507ba2c4d4eSJosef Bacik !delayed_refs_rsv->full) { 508ba2c4d4eSJosef Bacik /* 509ba2c4d4eSJosef Bacik * Some people call with btrfs_start_transaction(root, 0) 510ba2c4d4eSJosef Bacik * because they can be throttled, but have some other mechanism 511ba2c4d4eSJosef Bacik * for reserving space. We still want these guys to refill the 512ba2c4d4eSJosef Bacik * delayed block_rsv so just add 1 items worth of reservation 513ba2c4d4eSJosef Bacik * here. 514ba2c4d4eSJosef Bacik */ 515ba2c4d4eSJosef Bacik ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); 516b5009945SJosef Bacik if (ret) 517843fcf35SMiao Xie goto reserve_fail; 518b5009945SJosef Bacik } 519a22285a6SYan, Zheng again: 520f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 521843fcf35SMiao Xie if (!h) { 522843fcf35SMiao Xie ret = -ENOMEM; 523843fcf35SMiao Xie goto alloc_fail; 524843fcf35SMiao Xie } 525a22285a6SYan, Zheng 52698114659SJosef Bacik /* 52798114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 52898114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 52998114659SJosef Bacik * because we're already holding a ref. We need this because we could 53098114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53198114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 532354aa0fbSMiao Xie * 533354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 534354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 53598114659SJosef Bacik */ 5360860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5370b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 538b2b5ef5cSJan Kara 5392ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5402ff7e61eSJeff Mahoney wait_current_trans(fs_info); 541a22285a6SYan, Zheng 542a4abeea4SJosef Bacik do { 5432ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 544178260b2SMiao Xie if (ret == -EBUSY) { 5452ff7e61eSJeff Mahoney wait_current_trans(fs_info); 546178260b2SMiao Xie if (unlikely(type == TRANS_ATTACH)) 547178260b2SMiao Xie ret = -ENOENT; 548178260b2SMiao Xie } 549a4abeea4SJosef Bacik } while (ret == -EBUSY); 550a4abeea4SJosef Bacik 551a43f7f82SLiu Bo if (ret < 0) 552843fcf35SMiao Xie goto join_fail; 5530f7d52f4SChris Mason 5540b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 555a22285a6SYan, Zheng 556a22285a6SYan, Zheng h->transid = cur_trans->transid; 557a22285a6SYan, Zheng h->transaction = cur_trans; 558d13603efSArne Jansen h->root = root; 559b50fff81SDavid Sterba refcount_set(&h->use_count, 1); 56064b63580SJeff Mahoney h->fs_info = root->fs_info; 5617174109cSQu Wenruo 562a698d075SMiao Xie h->type = type; 563d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 564ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 565b7ec40d7SChris Mason 566a22285a6SYan, Zheng smp_mb(); 5674a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5682ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 569abdd2e80SFilipe Manana current->journal_info = h; 5703a45bb20SJeff Mahoney btrfs_commit_transaction(h); 571a22285a6SYan, Zheng goto again; 572a22285a6SYan, Zheng } 5739ed74f2dSJosef Bacik 574b5009945SJosef Bacik if (num_bytes) { 5750b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5762bcc0328SLiu Bo h->transid, num_bytes, 1); 5770b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 578b5009945SJosef Bacik h->bytes_reserved = num_bytes; 57920dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 580a22285a6SYan, Zheng } 581a22285a6SYan, Zheng 5822a1eb461SJosef Bacik got_it: 583a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 584a22285a6SYan, Zheng 585bcf3a3e7SNikolay Borisov if (!current->journal_info) 586a22285a6SYan, Zheng current->journal_info = h; 58779154b1bSChris Mason return h; 588843fcf35SMiao Xie 589843fcf35SMiao Xie join_fail: 5900860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5910b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 592843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 593843fcf35SMiao Xie alloc_fail: 594843fcf35SMiao Xie if (num_bytes) 5952ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 596843fcf35SMiao Xie num_bytes); 597843fcf35SMiao Xie reserve_fail: 598733e03a0SQu Wenruo btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); 599843fcf35SMiao Xie return ERR_PTR(ret); 60079154b1bSChris Mason } 60179154b1bSChris Mason 602f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6035aed1dd8SAlexandru Moise unsigned int num_items) 604f9295749SChris Mason { 60508e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 606003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 607f9295749SChris Mason } 608003d7c59SJeff Mahoney 6098eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6108eab77ffSFilipe Manana struct btrfs_root *root, 6118eab77ffSFilipe Manana unsigned int num_items, 6128eab77ffSFilipe Manana int min_factor) 6138eab77ffSFilipe Manana { 6140b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6158eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6168eab77ffSFilipe Manana u64 num_bytes; 6178eab77ffSFilipe Manana int ret; 6188eab77ffSFilipe Manana 619003d7c59SJeff Mahoney /* 620003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 621003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 622003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 623003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 624003d7c59SJeff Mahoney */ 625003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 626003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6278eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6288eab77ffSFilipe Manana return trans; 6298eab77ffSFilipe Manana 6308eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6318eab77ffSFilipe Manana if (IS_ERR(trans)) 6328eab77ffSFilipe Manana return trans; 6338eab77ffSFilipe Manana 6340b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6350b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6360b246afaSJeff Mahoney num_bytes, min_factor); 6378eab77ffSFilipe Manana if (ret) { 6383a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6398eab77ffSFilipe Manana return ERR_PTR(ret); 6408eab77ffSFilipe Manana } 6418eab77ffSFilipe Manana 6420b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6438eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6440b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 64588d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6468eab77ffSFilipe Manana 6478eab77ffSFilipe Manana return trans; 6488eab77ffSFilipe Manana } 6498407aa46SMiao Xie 6507a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 651f9295749SChris Mason { 652003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 653003d7c59SJeff Mahoney true); 654f9295749SChris Mason } 655f9295749SChris Mason 6567a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6570af3d00bSJosef Bacik { 658575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 659003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6600af3d00bSJosef Bacik } 6610af3d00bSJosef Bacik 662d4edf39bSMiao Xie /* 663d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 664d4edf39bSMiao Xie * 665d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 666d4edf39bSMiao Xie * don't want to start a new one. 667d4edf39bSMiao Xie * 668d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 669d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 670d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 671d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 672d4edf39bSMiao Xie * invoke 673d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 674d4edf39bSMiao Xie */ 675354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 67660376ce4SJosef Bacik { 677575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 678003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 67960376ce4SJosef Bacik } 68060376ce4SJosef Bacik 681d4edf39bSMiao Xie /* 68290b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 683d4edf39bSMiao Xie * 68452042d8eSAndrea Gelmini * It is similar to the above function, the difference is this one 685d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 686d4edf39bSMiao Xie * complete. 687d4edf39bSMiao Xie */ 688d4edf39bSMiao Xie struct btrfs_trans_handle * 689d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 690d4edf39bSMiao Xie { 691d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 692d4edf39bSMiao Xie 693575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 694003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6958d9e220cSAl Viro if (trans == ERR_PTR(-ENOENT)) 6962ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 697d4edf39bSMiao Xie 698d4edf39bSMiao Xie return trans; 699d4edf39bSMiao Xie } 700d4edf39bSMiao Xie 701d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 7022ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 70389ce8a63SChris Mason { 7044a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 70589ce8a63SChris Mason } 70689ce8a63SChris Mason 7072ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 70846204592SSage Weil { 70946204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7108cd2807fSMiao Xie int ret = 0; 71146204592SSage Weil 71246204592SSage Weil if (transid) { 7130b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 714a4abeea4SJosef Bacik goto out; 71546204592SSage Weil 71646204592SSage Weil /* find specified transaction */ 7170b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7180b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 71946204592SSage Weil if (t->transid == transid) { 72046204592SSage Weil cur_trans = t; 7219b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7228cd2807fSMiao Xie ret = 0; 72346204592SSage Weil break; 72446204592SSage Weil } 7258cd2807fSMiao Xie if (t->transid > transid) { 7268cd2807fSMiao Xie ret = 0; 72746204592SSage Weil break; 72846204592SSage Weil } 7298cd2807fSMiao Xie } 7300b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 73142383020SSage Weil 73242383020SSage Weil /* 73342383020SSage Weil * The specified transaction doesn't exist, or we 73442383020SSage Weil * raced with btrfs_commit_transaction 73542383020SSage Weil */ 73642383020SSage Weil if (!cur_trans) { 7370b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 73842383020SSage Weil ret = -EINVAL; 7398cd2807fSMiao Xie goto out; 74042383020SSage Weil } 74146204592SSage Weil } else { 74246204592SSage Weil /* find newest transaction that is committing | committed */ 7430b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7440b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 74546204592SSage Weil list) { 7464a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7474a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7483473f3c0SJosef Bacik break; 74946204592SSage Weil cur_trans = t; 7509b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 75146204592SSage Weil break; 75246204592SSage Weil } 75346204592SSage Weil } 7540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 75546204592SSage Weil if (!cur_trans) 756a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 75746204592SSage Weil } 75846204592SSage Weil 7592ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 760724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 761a4abeea4SJosef Bacik out: 76246204592SSage Weil return ret; 76346204592SSage Weil } 76446204592SSage Weil 7652ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 76637d1aeeeSChris Mason { 7672ff7e61eSJeff Mahoney wait_current_trans(fs_info); 76837d1aeeeSChris Mason } 76937d1aeeeSChris Mason 7702ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7718929ecfaSYan, Zheng { 7722ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7730b246afaSJeff Mahoney 77464403612SJosef Bacik if (btrfs_check_space_for_delayed_refs(fs_info)) 7751be41b78SJosef Bacik return 1; 77636ba022aSJosef Bacik 7772ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 7788929ecfaSYan, Zheng } 7798929ecfaSYan, Zheng 7803a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 7818929ecfaSYan, Zheng { 7828929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7838929ecfaSYan, Zheng 784a4abeea4SJosef Bacik smp_mb(); 7854a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 7864a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 7878929ecfaSYan, Zheng return 1; 7888929ecfaSYan, Zheng 7892ff7e61eSJeff Mahoney return should_end_transaction(trans); 7908929ecfaSYan, Zheng } 7918929ecfaSYan, Zheng 792dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) 793dc60c525SNikolay Borisov 7940e34693fSNikolay Borisov { 795dc60c525SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 796dc60c525SNikolay Borisov 7970e34693fSNikolay Borisov if (!trans->block_rsv) { 7980e34693fSNikolay Borisov ASSERT(!trans->bytes_reserved); 7990e34693fSNikolay Borisov return; 8000e34693fSNikolay Borisov } 8010e34693fSNikolay Borisov 8020e34693fSNikolay Borisov if (!trans->bytes_reserved) 8030e34693fSNikolay Borisov return; 8040e34693fSNikolay Borisov 8050e34693fSNikolay Borisov ASSERT(trans->block_rsv == &fs_info->trans_block_rsv); 8060e34693fSNikolay Borisov trace_btrfs_space_reservation(fs_info, "transaction", 8070e34693fSNikolay Borisov trans->transid, trans->bytes_reserved, 0); 8080e34693fSNikolay Borisov btrfs_block_rsv_release(fs_info, trans->block_rsv, 8090e34693fSNikolay Borisov trans->bytes_reserved); 8100e34693fSNikolay Borisov trans->bytes_reserved = 0; 8110e34693fSNikolay Borisov } 8120e34693fSNikolay Borisov 81389ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 8143a45bb20SJeff Mahoney int throttle) 81579154b1bSChris Mason { 8163a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8178929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 818a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8194edc2ca3SDave Jones int err = 0; 820d6e4a428SChris Mason 821b50fff81SDavid Sterba if (refcount_read(&trans->use_count) > 1) { 822b50fff81SDavid Sterba refcount_dec(&trans->use_count); 8232a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8242a1eb461SJosef Bacik return 0; 8252a1eb461SJosef Bacik } 8262a1eb461SJosef Bacik 827dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8284c13d758SJosef Bacik trans->block_rsv = NULL; 829c5567237SArne Jansen 8306c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 831ea658badSJosef Bacik 8324fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8334fbcdf66SFilipe Manana 83420c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8353bbb24b2SJosef Bacik if (throttle) 8363a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8373bbb24b2SJosef Bacik else 8388929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8398929ecfaSYan, Zheng } 8408929ecfaSYan, Zheng 8410860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8420b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8436df7881aSJosef Bacik 8448929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 84513c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 84613c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8470860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 84889ce8a63SChris Mason 849093258e6SDavid Sterba cond_wake_up(&cur_trans->writer_wait); 850724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8519ed74f2dSJosef Bacik 8529ed74f2dSJosef Bacik if (current->journal_info == trans) 8539ed74f2dSJosef Bacik current->journal_info = NULL; 854ab78c84dSChris Mason 85524bbcf04SYan, Zheng if (throttle) 8562ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 85724bbcf04SYan, Zheng 85849b25e05SJeff Mahoney if (trans->aborted || 8590b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 8604e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8614edc2ca3SDave Jones err = -EIO; 8624e121c06SJosef Bacik } 86349b25e05SJeff Mahoney 8644edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 8654edc2ca3SDave Jones return err; 86679154b1bSChris Mason } 86779154b1bSChris Mason 8683a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 86989ce8a63SChris Mason { 8703a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 87189ce8a63SChris Mason } 87289ce8a63SChris Mason 8733a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 87489ce8a63SChris Mason { 8753a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 87616cdcec7SMiao Xie } 87716cdcec7SMiao Xie 878d352ac68SChris Mason /* 879d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 880d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 881690587d1SChris Mason * those extents are sent to disk but does not wait on them 882d352ac68SChris Mason */ 8832ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 8848cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 88579154b1bSChris Mason { 886777e6bd7SChris Mason int err = 0; 8877c4452b9SChris Mason int werr = 0; 8880b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 889e6138876SJosef Bacik struct extent_state *cached_state = NULL; 890777e6bd7SChris Mason u64 start = 0; 8915f39d397SChris Mason u64 end; 8927c4452b9SChris Mason 8936300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 8941728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 895e6138876SJosef Bacik mark, &cached_state)) { 896663dfbb0SFilipe Manana bool wait_writeback = false; 897663dfbb0SFilipe Manana 898663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 899663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 900210aa277SDavid Sterba mark, &cached_state); 901663dfbb0SFilipe Manana /* 902663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 903663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 904663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 905663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 906bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 907bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 908bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 909bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 910bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 911663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 91241e7acd3SNikolay Borisov * the transaction (through extent_io_tree_release()). 913663dfbb0SFilipe Manana */ 914663dfbb0SFilipe Manana if (err == -ENOMEM) { 915663dfbb0SFilipe Manana err = 0; 916663dfbb0SFilipe Manana wait_writeback = true; 917663dfbb0SFilipe Manana } 918663dfbb0SFilipe Manana if (!err) 9191728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9207c4452b9SChris Mason if (err) 9217c4452b9SChris Mason werr = err; 922663dfbb0SFilipe Manana else if (wait_writeback) 923663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 924e38e2ed7SFilipe Manana free_extent_state(cached_state); 925663dfbb0SFilipe Manana cached_state = NULL; 9261728366eSJosef Bacik cond_resched(); 9271728366eSJosef Bacik start = end + 1; 9287c4452b9SChris Mason } 9296300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 930690587d1SChris Mason return werr; 931690587d1SChris Mason } 932690587d1SChris Mason 933690587d1SChris Mason /* 934690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 935690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 936690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 937690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 938690587d1SChris Mason */ 939bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 940bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 941690587d1SChris Mason { 942690587d1SChris Mason int err = 0; 943690587d1SChris Mason int werr = 0; 9440b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 945e6138876SJosef Bacik struct extent_state *cached_state = NULL; 946690587d1SChris Mason u64 start = 0; 947690587d1SChris Mason u64 end; 948690587d1SChris Mason 9491728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 950e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 951663dfbb0SFilipe Manana /* 952663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 953663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 954663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 955663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 956663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 95741e7acd3SNikolay Borisov * it's safe to do it (through extent_io_tree_release()). 958663dfbb0SFilipe Manana */ 959663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 960ae0f1625SDavid Sterba EXTENT_NEED_WAIT, 0, 0, &cached_state); 961663dfbb0SFilipe Manana if (err == -ENOMEM) 962663dfbb0SFilipe Manana err = 0; 963663dfbb0SFilipe Manana if (!err) 9641728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 965777e6bd7SChris Mason if (err) 966777e6bd7SChris Mason werr = err; 967e38e2ed7SFilipe Manana free_extent_state(cached_state); 968e38e2ed7SFilipe Manana cached_state = NULL; 969777e6bd7SChris Mason cond_resched(); 9701728366eSJosef Bacik start = end + 1; 971777e6bd7SChris Mason } 9727c4452b9SChris Mason if (err) 9737c4452b9SChris Mason werr = err; 974bf89d38fSJeff Mahoney return werr; 975bf89d38fSJeff Mahoney } 976656f30dbSFilipe Manana 977bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 978bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 979bf89d38fSJeff Mahoney { 980bf89d38fSJeff Mahoney bool errors = false; 981bf89d38fSJeff Mahoney int err; 982bf89d38fSJeff Mahoney 983bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 984bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 985bf89d38fSJeff Mahoney errors = true; 986bf89d38fSJeff Mahoney 987bf89d38fSJeff Mahoney if (errors && !err) 988bf89d38fSJeff Mahoney err = -EIO; 989bf89d38fSJeff Mahoney return err; 990bf89d38fSJeff Mahoney } 991bf89d38fSJeff Mahoney 992bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 993bf89d38fSJeff Mahoney { 994bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 995bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 996bf89d38fSJeff Mahoney bool errors = false; 997bf89d38fSJeff Mahoney int err; 998bf89d38fSJeff Mahoney 999bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 1000bf89d38fSJeff Mahoney 1001bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1002656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10030b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1004656f30dbSFilipe Manana errors = true; 1005656f30dbSFilipe Manana 1006656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10070b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1008656f30dbSFilipe Manana errors = true; 1009656f30dbSFilipe Manana 1010bf89d38fSJeff Mahoney if (errors && !err) 1011bf89d38fSJeff Mahoney err = -EIO; 1012bf89d38fSJeff Mahoney return err; 101379154b1bSChris Mason } 101479154b1bSChris Mason 1015690587d1SChris Mason /* 1016c9b577c0SNikolay Borisov * When btree blocks are allocated the corresponding extents are marked dirty. 1017c9b577c0SNikolay Borisov * This function ensures such extents are persisted on disk for transaction or 1018c9b577c0SNikolay Borisov * log commit. 1019c9b577c0SNikolay Borisov * 1020c9b577c0SNikolay Borisov * @trans: transaction whose dirty pages we'd like to write 1021690587d1SChris Mason */ 102270458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans) 1023d0c803c4SChris Mason { 1024663dfbb0SFilipe Manana int ret; 1025c9b577c0SNikolay Borisov int ret2; 1026c9b577c0SNikolay Borisov struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages; 102770458a58SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1028c9b577c0SNikolay Borisov struct blk_plug plug; 1029663dfbb0SFilipe Manana 1030c9b577c0SNikolay Borisov blk_start_plug(&plug); 1031c9b577c0SNikolay Borisov ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY); 1032c9b577c0SNikolay Borisov blk_finish_plug(&plug); 1033c9b577c0SNikolay Borisov ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1034c9b577c0SNikolay Borisov 103541e7acd3SNikolay Borisov extent_io_tree_release(&trans->transaction->dirty_pages); 1036663dfbb0SFilipe Manana 1037c9b577c0SNikolay Borisov if (ret) 1038663dfbb0SFilipe Manana return ret; 1039c9b577c0SNikolay Borisov else if (ret2) 1040c9b577c0SNikolay Borisov return ret2; 1041c9b577c0SNikolay Borisov else 1042c9b577c0SNikolay Borisov return 0; 1043d0c803c4SChris Mason } 1044d0c803c4SChris Mason 1045d352ac68SChris Mason /* 1046d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1047d352ac68SChris Mason * 1048d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1049d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1050d352ac68SChris Mason * allocation tree. 1051d352ac68SChris Mason * 1052d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1053d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1054d352ac68SChris Mason */ 10550b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 105679154b1bSChris Mason struct btrfs_root *root) 105779154b1bSChris Mason { 105879154b1bSChris Mason int ret; 10590b86a832SChris Mason u64 old_root_bytenr; 106086b9f2ecSYan, Zheng u64 old_root_used; 10610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10620b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 106379154b1bSChris Mason 106486b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 106556bec294SChris Mason 106679154b1bSChris Mason while (1) { 10670b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 106886b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1069ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 107079154b1bSChris Mason break; 107187ef2bb4SChris Mason 10725d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 107379154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10740b86a832SChris Mason &root->root_key, 10750b86a832SChris Mason &root->root_item); 107649b25e05SJeff Mahoney if (ret) 107749b25e05SJeff Mahoney return ret; 107856bec294SChris Mason 107986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1080e7070be1SJosef Bacik } 1081276e680dSYan Zheng 10820b86a832SChris Mason return 0; 10830b86a832SChris Mason } 10840b86a832SChris Mason 1085d352ac68SChris Mason /* 1086d352ac68SChris Mason * update all the cowonly tree roots on disk 108749b25e05SJeff Mahoney * 108849b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 108949b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 109049b25e05SJeff Mahoney * to clean up the delayed refs. 1091d352ac68SChris Mason */ 10929386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) 10930b86a832SChris Mason { 10949386d8bcSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1095ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 10961bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 10970b86a832SChris Mason struct list_head *next; 109884234f3aSYan Zheng struct extent_buffer *eb; 109956bec294SChris Mason int ret; 110084234f3aSYan Zheng 110184234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 110249b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 110349b25e05SJeff Mahoney 0, &eb); 110484234f3aSYan Zheng btrfs_tree_unlock(eb); 110584234f3aSYan Zheng free_extent_buffer(eb); 11060b86a832SChris Mason 110749b25e05SJeff Mahoney if (ret) 110849b25e05SJeff Mahoney return ret; 110949b25e05SJeff Mahoney 1110c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 111149b25e05SJeff Mahoney if (ret) 111249b25e05SJeff Mahoney return ret; 111387ef2bb4SChris Mason 1114196c9d8dSDavid Sterba ret = btrfs_run_dev_stats(trans); 1115c16ce190SJosef Bacik if (ret) 1116c16ce190SJosef Bacik return ret; 11172b584c68SDavid Sterba ret = btrfs_run_dev_replace(trans); 1118c16ce190SJosef Bacik if (ret) 1119c16ce190SJosef Bacik return ret; 1120280f8bd2SLu Fengqi ret = btrfs_run_qgroups(trans); 1121c16ce190SJosef Bacik if (ret) 1122c16ce190SJosef Bacik return ret; 1123546adb0dSJan Schmidt 1124bbebb3e0SDavid Sterba ret = btrfs_setup_space_cache(trans); 1125dcdf7f6dSJosef Bacik if (ret) 1126dcdf7f6dSJosef Bacik return ret; 1127dcdf7f6dSJosef Bacik 1128546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1129c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1130c16ce190SJosef Bacik if (ret) 1131c16ce190SJosef Bacik return ret; 1132ea526d18SJosef Bacik again: 11330b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11342ff7e61eSJeff Mahoney struct btrfs_root *root; 11350b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11360b86a832SChris Mason list_del_init(next); 11370b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1138e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 113987ef2bb4SChris Mason 11409e351cc8SJosef Bacik if (root != fs_info->extent_root) 11419e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11429e351cc8SJosef Bacik &trans->transaction->switch_commits); 114349b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 114449b25e05SJeff Mahoney if (ret) 114549b25e05SJeff Mahoney return ret; 1146c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1147ea526d18SJosef Bacik if (ret) 1148ea526d18SJosef Bacik return ret; 114979154b1bSChris Mason } 1150276e680dSYan Zheng 11511bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 11525742d15fSDavid Sterba ret = btrfs_write_dirty_block_groups(trans); 1153ea526d18SJosef Bacik if (ret) 1154ea526d18SJosef Bacik return ret; 1155c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1156ea526d18SJosef Bacik if (ret) 1157ea526d18SJosef Bacik return ret; 1158ea526d18SJosef Bacik } 1159ea526d18SJosef Bacik 1160ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1161ea526d18SJosef Bacik goto again; 1162ea526d18SJosef Bacik 11639e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11649e351cc8SJosef Bacik &trans->transaction->switch_commits); 11659f6cbcbbSDavid Sterba 11669f6cbcbbSDavid Sterba /* Update dev-replace pointer once everything is committed */ 11679f6cbcbbSDavid Sterba fs_info->dev_replace.committed_cursor_left = 11689f6cbcbbSDavid Sterba fs_info->dev_replace.cursor_left_last_write_of_item; 11698dabb742SStefan Behrens 117079154b1bSChris Mason return 0; 117179154b1bSChris Mason } 117279154b1bSChris Mason 1173d352ac68SChris Mason /* 1174d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1175d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1176d352ac68SChris Mason * be deleted 1177d352ac68SChris Mason */ 1178cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11795eda7b5eSChris Mason { 11800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11810b246afaSJeff Mahoney 11820b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1183cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11840b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 11850b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 11865eda7b5eSChris Mason } 11875eda7b5eSChris Mason 1188d352ac68SChris Mason /* 11895d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1190d352ac68SChris Mason */ 11917e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) 11920f7d52f4SChris Mason { 11937e4443d9SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 11940f7d52f4SChris Mason struct btrfs_root *gang[8]; 11950f7d52f4SChris Mason int i; 11960f7d52f4SChris Mason int ret; 119754aa1f4dSChris Mason int err = 0; 119854aa1f4dSChris Mason 1199a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12000f7d52f4SChris Mason while (1) { 12015d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12025d4f98a2SYan Zheng (void **)gang, 0, 12030f7d52f4SChris Mason ARRAY_SIZE(gang), 12040f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12050f7d52f4SChris Mason if (ret == 0) 12060f7d52f4SChris Mason break; 12070f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12085b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12095d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12102619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12110f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1212a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 121331153d81SYan Zheng 1214e02119d5SChris Mason btrfs_free_log(trans, root); 12155d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1216e02119d5SChris Mason 121782d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 121882d5902dSLi Zefan 1219f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 122027cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1221c7548af6SChris Mason smp_mb__after_atomic(); 1222f1ebcc74SLiu Bo 1223978d910dSYan Zheng if (root->commit_root != root->node) { 12249e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12259e351cc8SJosef Bacik &trans->transaction->switch_commits); 1226978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1227978d910dSYan Zheng root->node); 1228978d910dSYan Zheng } 122931153d81SYan Zheng 12305d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12310f7d52f4SChris Mason &root->root_key, 12320f7d52f4SChris Mason &root->root_item); 1233a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 123454aa1f4dSChris Mason if (err) 123554aa1f4dSChris Mason break; 1236733e03a0SQu Wenruo btrfs_qgroup_free_meta_all_pertrans(root); 12370f7d52f4SChris Mason } 12389f3a7427SChris Mason } 1239a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 124054aa1f4dSChris Mason return err; 12410f7d52f4SChris Mason } 12420f7d52f4SChris Mason 1243d352ac68SChris Mason /* 1244de78b51aSEric Sandeen * defrag a given btree. 1245de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1246d352ac68SChris Mason */ 1247de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1248e9d0b13bSChris Mason { 1249e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1250e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12518929ecfaSYan, Zheng int ret; 1252e9d0b13bSChris Mason 125327cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1254e9d0b13bSChris Mason return 0; 12558929ecfaSYan, Zheng 12566b80053dSChris Mason while (1) { 12578929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12588929ecfaSYan, Zheng if (IS_ERR(trans)) 12598929ecfaSYan, Zheng return PTR_ERR(trans); 12608929ecfaSYan, Zheng 1261de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12628929ecfaSYan, Zheng 12633a45bb20SJeff Mahoney btrfs_end_transaction(trans); 12642ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1265e9d0b13bSChris Mason cond_resched(); 1266e9d0b13bSChris Mason 1267ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1268e9d0b13bSChris Mason break; 1269210549ebSDavid Sterba 1270ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1271ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1272210549ebSDavid Sterba ret = -EAGAIN; 1273210549ebSDavid Sterba break; 1274210549ebSDavid Sterba } 1275e9d0b13bSChris Mason } 127627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12778929ecfaSYan, Zheng return ret; 1278e9d0b13bSChris Mason } 1279e9d0b13bSChris Mason 1280d352ac68SChris Mason /* 12816426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 12826426c7adSQu Wenruo * 12836426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 12846426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 12856426c7adSQu Wenruo * qgroup works. 12866426c7adSQu Wenruo */ 12876426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 12886426c7adSQu Wenruo struct btrfs_root *src, 12896426c7adSQu Wenruo struct btrfs_root *parent, 12906426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 12916426c7adSQu Wenruo u64 dst_objectid) 12926426c7adSQu Wenruo { 12936426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 12946426c7adSQu Wenruo int ret; 12956426c7adSQu Wenruo 12966426c7adSQu Wenruo /* 12976426c7adSQu Wenruo * Save some performance in the case that qgroups are not 12986426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 12996426c7adSQu Wenruo * kick in anyway. 13006426c7adSQu Wenruo */ 13019ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 13026426c7adSQu Wenruo return 0; 13036426c7adSQu Wenruo 13046426c7adSQu Wenruo /* 130552042d8eSAndrea Gelmini * Ensure dirty @src will be committed. Or, after coming 13064d31778aSQu Wenruo * commit_fs_roots() and switch_commit_roots(), any dirty but not 13074d31778aSQu Wenruo * recorded root will never be updated again, causing an outdated root 13084d31778aSQu Wenruo * item. 13094d31778aSQu Wenruo */ 13104d31778aSQu Wenruo record_root_in_trans(trans, src, 1); 13114d31778aSQu Wenruo 13124d31778aSQu Wenruo /* 13136426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13146426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13156426c7adSQu Wenruo */ 13166426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13176426c7adSQu Wenruo 13187e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 13196426c7adSQu Wenruo if (ret) 13206426c7adSQu Wenruo goto out; 1321460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 13226426c7adSQu Wenruo if (ret < 0) 13236426c7adSQu Wenruo goto out; 13246426c7adSQu Wenruo 13256426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 1326a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid, 13276426c7adSQu Wenruo inherit); 13286426c7adSQu Wenruo if (ret < 0) 13296426c7adSQu Wenruo goto out; 13306426c7adSQu Wenruo 13316426c7adSQu Wenruo /* 13326426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13336426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13346426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13356426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13366426c7adSQu Wenruo * 2) write all btree blocks onto disk 13376426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13386426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13396426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13406426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13416426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13426426c7adSQu Wenruo */ 13439386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 13446426c7adSQu Wenruo if (ret) 13456426c7adSQu Wenruo goto out; 134616916a88SNikolay Borisov switch_commit_roots(trans->transaction); 134770458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 13486426c7adSQu Wenruo if (ret) 1349f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13506426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13516426c7adSQu Wenruo 13526426c7adSQu Wenruo out: 13536426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 13546426c7adSQu Wenruo 13556426c7adSQu Wenruo /* 13566426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 13576426c7adSQu Wenruo * last_trans == cur_transid. 13586426c7adSQu Wenruo * Or it won't be committed again onto disk after later 13596426c7adSQu Wenruo * insert_dir_item() 13606426c7adSQu Wenruo */ 13616426c7adSQu Wenruo if (!ret) 13626426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 13636426c7adSQu Wenruo return ret; 13646426c7adSQu Wenruo } 13656426c7adSQu Wenruo 13666426c7adSQu Wenruo /* 1367d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1368aec8030aSMiao Xie * transaction commit. This does the actual creation. 1369aec8030aSMiao Xie * 1370aec8030aSMiao Xie * Note: 1371aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1372aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1373aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1374d352ac68SChris Mason */ 137580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13763063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13773063d29fSChris Mason { 137808d50ca3SNikolay Borisov 137908d50ca3SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 13803063d29fSChris Mason struct btrfs_key key; 138180b6794dSChris Mason struct btrfs_root_item *new_root_item; 13823063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13833063d29fSChris Mason struct btrfs_root *root = pending->root; 13846bdb72deSSage Weil struct btrfs_root *parent_root; 138598c9942aSLiu Bo struct btrfs_block_rsv *rsv; 13866bdb72deSSage Weil struct inode *parent_inode; 138742874b3dSMiao Xie struct btrfs_path *path; 138842874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1389a22285a6SYan, Zheng struct dentry *dentry; 13903063d29fSChris Mason struct extent_buffer *tmp; 1391925baeddSChris Mason struct extent_buffer *old; 139295582b00SDeepa Dinamani struct timespec64 cur_time; 1393aec8030aSMiao Xie int ret = 0; 1394d68fc57bSYan, Zheng u64 to_reserve = 0; 13956bdb72deSSage Weil u64 index = 0; 1396a22285a6SYan, Zheng u64 objectid; 1397b83cc969SLi Zefan u64 root_flags; 13988ea05e3aSAlexander Block uuid_le new_uuid; 13993063d29fSChris Mason 14008546b570SDavid Sterba ASSERT(pending->path); 14018546b570SDavid Sterba path = pending->path; 140242874b3dSMiao Xie 1403b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1404b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1405a22285a6SYan, Zheng 1406aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1407aec8030aSMiao Xie if (pending->error) 14086fa9700eSMiao Xie goto no_free_objectid; 14093063d29fSChris Mason 1410d6726335SQu Wenruo /* 1411d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1412d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1413d6726335SQu Wenruo */ 1414d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1415d6726335SQu Wenruo 1416147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1417d68fc57bSYan, Zheng 1418d68fc57bSYan, Zheng if (to_reserve > 0) { 1419aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1420aec8030aSMiao Xie &pending->block_rsv, 142108e007d2SMiao Xie to_reserve, 142208e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1423aec8030aSMiao Xie if (pending->error) 1424d6726335SQu Wenruo goto clear_skip_qgroup; 1425d68fc57bSYan, Zheng } 1426d68fc57bSYan, Zheng 14273063d29fSChris Mason key.objectid = objectid; 1428a22285a6SYan, Zheng key.offset = (u64)-1; 1429a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14303063d29fSChris Mason 14316fa9700eSMiao Xie rsv = trans->block_rsv; 1432a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14332382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14340b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 143588d3a5aaSJosef Bacik trans->transid, 143688d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1437a22285a6SYan, Zheng dentry = pending->dentry; 1438e9662f70SMiao Xie parent_inode = pending->dir; 1439a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14406426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1441a22285a6SYan, Zheng 1442c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 144304b285f3SDeepa Dinamani 14446bdb72deSSage Weil /* 14456bdb72deSSage Weil * insert the directory item 14466bdb72deSSage Weil */ 1447877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 144849b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 144942874b3dSMiao Xie 145042874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 145142874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 14524a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 145342874b3dSMiao Xie dentry->d_name.name, 145442874b3dSMiao Xie dentry->d_name.len, 0); 145542874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1456fe66a05aSChris Mason pending->error = -EEXIST; 1457aec8030aSMiao Xie goto dir_item_existed; 145842874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 145942874b3dSMiao Xie ret = PTR_ERR(dir_item); 146066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14618732d44fSMiao Xie goto fail; 146279787eaaSJeff Mahoney } 146342874b3dSMiao Xie btrfs_release_path(path); 14646bdb72deSSage Weil 1465e999376fSChris Mason /* 1466e999376fSChris Mason * pull in the delayed directory update 1467e999376fSChris Mason * and the delayed inode item 1468e999376fSChris Mason * otherwise we corrupt the FS during 1469e999376fSChris Mason * snapshot 1470e999376fSChris Mason */ 1471e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 14728732d44fSMiao Xie if (ret) { /* Transaction aborted */ 147366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14748732d44fSMiao Xie goto fail; 14758732d44fSMiao Xie } 1476e999376fSChris Mason 14776426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 14786bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14796bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 148008fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14816bdb72deSSage Weil 1482b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1483b83cc969SLi Zefan if (pending->readonly) 1484b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1485b83cc969SLi Zefan else 1486b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1487b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1488b83cc969SLi Zefan 14898ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 14908ea05e3aSAlexander Block trans->transid); 14918ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 14928ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 14938ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 14948ea05e3aSAlexander Block BTRFS_UUID_SIZE); 149570023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 149670023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 149770023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 14988ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 14998ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15008ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15018ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 150270023da2SStefan Behrens } 15033cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15043cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 150570023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15068ea05e3aSAlexander Block 1507925baeddSChris Mason old = btrfs_lock_root_node(root); 150849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 150979787eaaSJeff Mahoney if (ret) { 151079787eaaSJeff Mahoney btrfs_tree_unlock(old); 151179787eaaSJeff Mahoney free_extent_buffer(old); 151266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15138732d44fSMiao Xie goto fail; 151479787eaaSJeff Mahoney } 151549b25e05SJeff Mahoney 15168bead258SDavid Sterba btrfs_set_lock_blocking_write(old); 15173063d29fSChris Mason 151849b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 151979787eaaSJeff Mahoney /* clean up in any case */ 1520925baeddSChris Mason btrfs_tree_unlock(old); 1521925baeddSChris Mason free_extent_buffer(old); 15228732d44fSMiao Xie if (ret) { 152366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15248732d44fSMiao Xie goto fail; 15258732d44fSMiao Xie } 1526f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 152727cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1528f1ebcc74SLiu Bo smp_wmb(); 1529f1ebcc74SLiu Bo 15305d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1531a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1532a22285a6SYan, Zheng key.offset = trans->transid; 1533a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1534925baeddSChris Mason btrfs_tree_unlock(tmp); 15353063d29fSChris Mason free_extent_buffer(tmp); 15368732d44fSMiao Xie if (ret) { 153766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15388732d44fSMiao Xie goto fail; 15398732d44fSMiao Xie } 15400660b5afSChris Mason 1541a22285a6SYan, Zheng /* 1542a22285a6SYan, Zheng * insert root back/forward references 1543a22285a6SYan, Zheng */ 15446025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, 1545a22285a6SYan, Zheng parent_root->root_key.objectid, 15464a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1547a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15488732d44fSMiao Xie if (ret) { 154966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15508732d44fSMiao Xie goto fail; 15518732d44fSMiao Xie } 1552a22285a6SYan, Zheng 1553a22285a6SYan, Zheng key.offset = (u64)-1; 15540b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 155579787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 155679787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 155766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15588732d44fSMiao Xie goto fail; 155979787eaaSJeff Mahoney } 1560d68fc57bSYan, Zheng 156149b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15628732d44fSMiao Xie if (ret) { 156366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15648732d44fSMiao Xie goto fail; 15658732d44fSMiao Xie } 1566361048f5SMiao Xie 1567c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 15688732d44fSMiao Xie if (ret) { 156966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15708732d44fSMiao Xie goto fail; 15718732d44fSMiao Xie } 157242874b3dSMiao Xie 15736426c7adSQu Wenruo /* 15746426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 15756426c7adSQu Wenruo * snapshot hack to do fast snapshot. 15766426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 15776426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 15786426c7adSQu Wenruo */ 15796426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 15806426c7adSQu Wenruo pending->inherit, objectid); 15816426c7adSQu Wenruo if (ret < 0) 15826426c7adSQu Wenruo goto fail; 15836426c7adSQu Wenruo 1584684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, dentry->d_name.name, 1585684572dfSLu Fengqi dentry->d_name.len, BTRFS_I(parent_inode), 1586684572dfSLu Fengqi &key, BTRFS_FT_DIR, index); 158742874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 15889c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 15898732d44fSMiao Xie if (ret) { 159066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15918732d44fSMiao Xie goto fail; 15928732d44fSMiao Xie } 159342874b3dSMiao Xie 15946ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 159542874b3dSMiao Xie dentry->d_name.len * 2); 159604b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1597c2050a45SDeepa Dinamani current_time(parent_inode); 1598be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1599dd5f9615SStefan Behrens if (ret) { 160066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1601dd5f9615SStefan Behrens goto fail; 1602dd5f9615SStefan Behrens } 1603cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, 1604cdb345a8SLu Fengqi objectid); 1605dd5f9615SStefan Behrens if (ret) { 160666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1607dd5f9615SStefan Behrens goto fail; 1608dd5f9615SStefan Behrens } 1609dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1610cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid, 1611dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1612dd5f9615SStefan Behrens objectid); 1613dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 161466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1615dd5f9615SStefan Behrens goto fail; 1616dd5f9615SStefan Behrens } 1617dd5f9615SStefan Behrens } 1618d6726335SQu Wenruo 1619c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1620d6726335SQu Wenruo if (ret) { 162166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1622d6726335SQu Wenruo goto fail; 1623d6726335SQu Wenruo } 1624d6726335SQu Wenruo 16253063d29fSChris Mason fail: 1626aec8030aSMiao Xie pending->error = ret; 1627aec8030aSMiao Xie dir_item_existed: 162898c9942aSLiu Bo trans->block_rsv = rsv; 16292382c5ccSLiu Bo trans->bytes_reserved = 0; 1630d6726335SQu Wenruo clear_skip_qgroup: 1631d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16326fa9700eSMiao Xie no_free_objectid: 16336fa9700eSMiao Xie kfree(new_root_item); 1634b0c0ea63SDavid Sterba pending->root_item = NULL; 163542874b3dSMiao Xie btrfs_free_path(path); 16368546b570SDavid Sterba pending->path = NULL; 16378546b570SDavid Sterba 163849b25e05SJeff Mahoney return ret; 16393063d29fSChris Mason } 16403063d29fSChris Mason 1641d352ac68SChris Mason /* 1642d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1643d352ac68SChris Mason */ 164408d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans) 16453063d29fSChris Mason { 1646aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16473063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1648aec8030aSMiao Xie int ret = 0; 16493de4586cSChris Mason 1650aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1651aec8030aSMiao Xie list_del(&pending->list); 165208d50ca3SNikolay Borisov ret = create_pending_snapshot(trans, pending); 1653aec8030aSMiao Xie if (ret) 1654aec8030aSMiao Xie break; 1655aec8030aSMiao Xie } 1656aec8030aSMiao Xie return ret; 16573de4586cSChris Mason } 16583de4586cSChris Mason 16592ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 16605d4f98a2SYan Zheng { 16615d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16625d4f98a2SYan Zheng struct btrfs_super_block *super; 16635d4f98a2SYan Zheng 16640b246afaSJeff Mahoney super = fs_info->super_copy; 16655d4f98a2SYan Zheng 16660b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 1667093e037cSDavid Sterba super->chunk_root = root_item->bytenr; 1668093e037cSDavid Sterba super->chunk_root_generation = root_item->generation; 1669093e037cSDavid Sterba super->chunk_root_level = root_item->level; 16705d4f98a2SYan Zheng 16710b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 1672093e037cSDavid Sterba super->root = root_item->bytenr; 1673093e037cSDavid Sterba super->generation = root_item->generation; 1674093e037cSDavid Sterba super->root_level = root_item->level; 16750b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 1676093e037cSDavid Sterba super->cache_generation = root_item->generation; 16770b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 1678093e037cSDavid Sterba super->uuid_tree_generation = root_item->generation; 16795d4f98a2SYan Zheng } 16805d4f98a2SYan Zheng 1681f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1682f36f3042SChris Mason { 16834a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1684f36f3042SChris Mason int ret = 0; 16854a9d8bdeSMiao Xie 1686a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 16874a9d8bdeSMiao Xie trans = info->running_transaction; 16884a9d8bdeSMiao Xie if (trans) 16894a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1690a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1691f36f3042SChris Mason return ret; 1692f36f3042SChris Mason } 1693f36f3042SChris Mason 16948929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 16958929ecfaSYan, Zheng { 16964a9d8bdeSMiao Xie struct btrfs_transaction *trans; 16978929ecfaSYan, Zheng int ret = 0; 16984a9d8bdeSMiao Xie 1699a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17004a9d8bdeSMiao Xie trans = info->running_transaction; 17014a9d8bdeSMiao Xie if (trans) 17024a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1703a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17048929ecfaSYan, Zheng return ret; 17058929ecfaSYan, Zheng } 17068929ecfaSYan, Zheng 1707bb9c12c9SSage Weil /* 1708bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1709bb9c12c9SSage Weil * transaction joins 1710bb9c12c9SSage Weil */ 17112ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1712bb9c12c9SSage Weil struct btrfs_transaction *trans) 1713bb9c12c9SSage Weil { 17142ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 17152ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1716bb9c12c9SSage Weil } 1717bb9c12c9SSage Weil 1718bb9c12c9SSage Weil /* 1719bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1720bb9c12c9SSage Weil * caller holds ref. 1721bb9c12c9SSage Weil */ 17222ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17232ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1724bb9c12c9SSage Weil struct btrfs_transaction *trans) 1725bb9c12c9SSage Weil { 17262ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17272ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1728bb9c12c9SSage Weil } 1729bb9c12c9SSage Weil 1730bb9c12c9SSage Weil /* 1731bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1732bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1733bb9c12c9SSage Weil */ 1734bb9c12c9SSage Weil struct btrfs_async_commit { 1735bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17367892b5afSMiao Xie struct work_struct work; 1737bb9c12c9SSage Weil }; 1738bb9c12c9SSage Weil 1739bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1740bb9c12c9SSage Weil { 1741bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17427892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1743bb9c12c9SSage Weil 17446fc4e354SSage Weil /* 17456fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17466fc4e354SSage Weil * Tell lockdep about it. 17476fc4e354SSage Weil */ 1748b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17493a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 17506fc4e354SSage Weil 1751e209db7aSSage Weil current->journal_info = ac->newtrans; 1752e209db7aSSage Weil 17533a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1754bb9c12c9SSage Weil kfree(ac); 1755bb9c12c9SSage Weil } 1756bb9c12c9SSage Weil 1757bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1758bb9c12c9SSage Weil int wait_for_unblock) 1759bb9c12c9SSage Weil { 17603a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1761bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1762bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1763bb9c12c9SSage Weil 1764bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1765db5b493aSTsutomu Itoh if (!ac) 1766db5b493aSTsutomu Itoh return -ENOMEM; 1767bb9c12c9SSage Weil 17687892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 17693a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 17703612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17713612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17723612b495STsutomu Itoh kfree(ac); 17733612b495STsutomu Itoh return err; 17743612b495STsutomu Itoh } 1775bb9c12c9SSage Weil 1776bb9c12c9SSage Weil /* take transaction reference */ 1777bb9c12c9SSage Weil cur_trans = trans->transaction; 17789b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1779bb9c12c9SSage Weil 17803a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17816fc4e354SSage Weil 17826fc4e354SSage Weil /* 17836fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17846fc4e354SSage Weil * async commit thread will be the one to unlock it. 17856fc4e354SSage Weil */ 1786b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17870b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 17886fc4e354SSage Weil 17897892b5afSMiao Xie schedule_work(&ac->work); 1790bb9c12c9SSage Weil 1791bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1792bb9c12c9SSage Weil if (wait_for_unblock) 17932ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1794bb9c12c9SSage Weil else 17952ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1796bb9c12c9SSage Weil 179738e88054SSage Weil if (current->journal_info == trans) 179838e88054SSage Weil current->journal_info = NULL; 179938e88054SSage Weil 1800724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1801bb9c12c9SSage Weil return 0; 1802bb9c12c9SSage Weil } 1803bb9c12c9SSage Weil 180449b25e05SJeff Mahoney 180597cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) 180649b25e05SJeff Mahoney { 180797cb39bbSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 180849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 180949b25e05SJeff Mahoney 1810b50fff81SDavid Sterba WARN_ON(refcount_read(&trans->use_count) > 1); 181149b25e05SJeff Mahoney 181266642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18137b8b92afSJosef Bacik 18140b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 181566b6135bSLiu Bo 181625d8c284SMiao Xie /* 181725d8c284SMiao Xie * If the transaction is removed from the list, it means this 181825d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 181925d8c284SMiao Xie * to call the cleanup function. 182025d8c284SMiao Xie */ 182125d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 182266b6135bSLiu Bo 182349b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18240b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18254a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18260b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1827f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1828f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1829f094ac32SLiu Bo 18300b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1831d7096fc3SJosef Bacik } 18320b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 183349b25e05SJeff Mahoney 18342ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 183549b25e05SJeff Mahoney 18360b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18370b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18380b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18390b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18404a9d8bdeSMiao Xie 1841e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18420b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1843724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1844724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 184549b25e05SJeff Mahoney 184697cb39bbSNikolay Borisov trace_btrfs_transaction_commit(trans->root); 184749b25e05SJeff Mahoney 184849b25e05SJeff Mahoney if (current->journal_info == trans) 184949b25e05SJeff Mahoney current->journal_info = NULL; 18500b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 185149b25e05SJeff Mahoney 185249b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 185349b25e05SJeff Mahoney } 185449b25e05SJeff Mahoney 1855c7cc64a9SDavid Sterba /* 1856c7cc64a9SDavid Sterba * Release reserved delayed ref space of all pending block groups of the 1857c7cc64a9SDavid Sterba * transaction and remove them from the list 1858c7cc64a9SDavid Sterba */ 1859c7cc64a9SDavid Sterba static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) 1860c7cc64a9SDavid Sterba { 1861c7cc64a9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 1862c7cc64a9SDavid Sterba struct btrfs_block_group_cache *block_group, *tmp; 1863c7cc64a9SDavid Sterba 1864c7cc64a9SDavid Sterba list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { 1865c7cc64a9SDavid Sterba btrfs_delayed_refs_rsv_release(fs_info, 1); 1866c7cc64a9SDavid Sterba list_del_init(&block_group->bg_list); 1867c7cc64a9SDavid Sterba } 1868c7cc64a9SDavid Sterba } 1869c7cc64a9SDavid Sterba 1870609e804dSFilipe Manana static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) 187182436617SMiao Xie { 1872609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1873609e804dSFilipe Manana 1874ce8ea7ccSJosef Bacik /* 1875ce8ea7ccSJosef Bacik * We use writeback_inodes_sb here because if we used 1876ce8ea7ccSJosef Bacik * btrfs_start_delalloc_roots we would deadlock with fs freeze. 1877ce8ea7ccSJosef Bacik * Currently are holding the fs freeze lock, if we do an async flush 1878ce8ea7ccSJosef Bacik * we'll do btrfs_join_transaction() and deadlock because we need to 1879ce8ea7ccSJosef Bacik * wait for the fs freeze lock. Using the direct flushing we benefit 1880ce8ea7ccSJosef Bacik * from already being in a transaction and our join_transaction doesn't 1881ce8ea7ccSJosef Bacik * have to re-take the fs freeze lock. 1882ce8ea7ccSJosef Bacik */ 1883609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 1884ce8ea7ccSJosef Bacik writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); 1885609e804dSFilipe Manana } else { 1886609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1887609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1888609e804dSFilipe Manana 1889609e804dSFilipe Manana /* 1890609e804dSFilipe Manana * Flush dellaloc for any root that is going to be snapshotted. 1891609e804dSFilipe Manana * This is done to avoid a corrupted version of files, in the 1892609e804dSFilipe Manana * snapshots, that had both buffered and direct IO writes (even 1893609e804dSFilipe Manana * if they were done sequentially) due to an unordered update of 1894609e804dSFilipe Manana * the inode's size on disk. 1895609e804dSFilipe Manana */ 1896609e804dSFilipe Manana list_for_each_entry(pending, head, list) { 1897609e804dSFilipe Manana int ret; 1898609e804dSFilipe Manana 1899609e804dSFilipe Manana ret = btrfs_start_delalloc_snapshot(pending->root); 1900609e804dSFilipe Manana if (ret) 1901609e804dSFilipe Manana return ret; 1902609e804dSFilipe Manana } 1903609e804dSFilipe Manana } 190482436617SMiao Xie return 0; 190582436617SMiao Xie } 190682436617SMiao Xie 1907609e804dSFilipe Manana static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) 190882436617SMiao Xie { 1909609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1910609e804dSFilipe Manana 1911609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 19126374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1913609e804dSFilipe Manana } else { 1914609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1915609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1916609e804dSFilipe Manana 1917609e804dSFilipe Manana /* 1918609e804dSFilipe Manana * Wait for any dellaloc that we started previously for the roots 1919609e804dSFilipe Manana * that are going to be snapshotted. This is to avoid a corrupted 1920609e804dSFilipe Manana * version of files in the snapshots that had both buffered and 1921609e804dSFilipe Manana * direct IO writes (even if they were done sequentially). 1922609e804dSFilipe Manana */ 1923609e804dSFilipe Manana list_for_each_entry(pending, head, list) 1924609e804dSFilipe Manana btrfs_wait_ordered_extents(pending->root, 1925609e804dSFilipe Manana U64_MAX, 0, U64_MAX); 1926609e804dSFilipe Manana } 192782436617SMiao Xie } 192882436617SMiao Xie 19293a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 193079154b1bSChris Mason { 19313a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 193249b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19338fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 193425287e0aSMiao Xie int ret; 193579154b1bSChris Mason 19368d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 193720c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 193825287e0aSMiao Xie ret = cur_trans->aborted; 19393a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1940e4a2bcacSJosef Bacik return ret; 194125287e0aSMiao Xie } 194249b25e05SJeff Mahoney 1943f45c752bSJosef Bacik btrfs_trans_release_metadata(trans); 1944f45c752bSJosef Bacik trans->block_rsv = NULL; 1945f45c752bSJosef Bacik 194656bec294SChris Mason /* make a pass through all the delayed refs we have so far 194756bec294SChris Mason * any runnings procs may add more while we are here 194856bec294SChris Mason */ 1949c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1950e4a2bcacSJosef Bacik if (ret) { 19513a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1952e4a2bcacSJosef Bacik return ret; 1953e4a2bcacSJosef Bacik } 195456bec294SChris Mason 1955b7ec40d7SChris Mason cur_trans = trans->transaction; 195649b25e05SJeff Mahoney 195756bec294SChris Mason /* 195856bec294SChris Mason * set the flushing flag so procs in this transaction have to 195956bec294SChris Mason * start sending their work down. 196056bec294SChris Mason */ 1961b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19621be41b78SJosef Bacik smp_wmb(); 196356bec294SChris Mason 19646c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 1965ea658badSJosef Bacik 1966c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1967e4a2bcacSJosef Bacik if (ret) { 19683a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1969e4a2bcacSJosef Bacik return ret; 1970e4a2bcacSJosef Bacik } 197156bec294SChris Mason 19723204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19731bbc621eSChris Mason int run_it = 0; 19741bbc621eSChris Mason 19751bbc621eSChris Mason /* this mutex is also taken before trying to set 19761bbc621eSChris Mason * block groups readonly. We need to make sure 19771bbc621eSChris Mason * that nobody has set a block group readonly 19781bbc621eSChris Mason * after a extents from that block group have been 19791bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19801bbc621eSChris Mason * will wait for the transaction to commit if it 19813204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19821bbc621eSChris Mason * 19833204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19843204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 19851bbc621eSChris Mason * hurt to have more than one go through, but there's no 19861bbc621eSChris Mason * real advantage to it either. 19871bbc621eSChris Mason */ 19880b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 19893204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 19903204d33cSJosef Bacik &cur_trans->flags)) 19911bbc621eSChris Mason run_it = 1; 19920b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 19931bbc621eSChris Mason 1994f9cacae3SNikolay Borisov if (run_it) { 199521217054SNikolay Borisov ret = btrfs_start_dirty_block_groups(trans); 19961bbc621eSChris Mason if (ret) { 19973a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19981bbc621eSChris Mason return ret; 19991bbc621eSChris Mason } 2000f9cacae3SNikolay Borisov } 2001f9cacae3SNikolay Borisov } 20021bbc621eSChris Mason 20030b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20044a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20050b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 20069b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 20073a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 2008ccd467d6SChris Mason 20092ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 201015ee9bc7SJosef Bacik 2011b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2012b4924a0fSLiu Bo ret = cur_trans->aborted; 2013b4924a0fSLiu Bo 2014724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 201515ee9bc7SJosef Bacik 201649b25e05SJeff Mahoney return ret; 201779154b1bSChris Mason } 20184313b399SChris Mason 20194a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20200b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2021bb9c12c9SSage Weil 20220b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2023ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2024ccd467d6SChris Mason struct btrfs_transaction, list); 20254a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20269b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20270b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2028ccd467d6SChris Mason 20292ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20301f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2031ccd467d6SChris Mason 2032724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20331f9b8c8fSFilipe Manana if (ret) 20341f9b8c8fSFilipe Manana goto cleanup_transaction; 2035a4abeea4SJosef Bacik } else { 20360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2037ccd467d6SChris Mason } 2038a4abeea4SJosef Bacik } else { 20390b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2040ccd467d6SChris Mason } 204115ee9bc7SJosef Bacik 20420860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20430860adfdSMiao Xie 2044609e804dSFilipe Manana ret = btrfs_start_delalloc_flush(trans); 204582436617SMiao Xie if (ret) 204682436617SMiao Xie goto cleanup_transaction; 204782436617SMiao Xie 2048e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 204949b25e05SJeff Mahoney if (ret) 205049b25e05SJeff Mahoney goto cleanup_transaction; 205116cdcec7SMiao Xie 2052581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2053581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2054ed3b3d31SChris Mason 2055581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 2056e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 2057ca469637SMiao Xie if (ret) 2058ca469637SMiao Xie goto cleanup_transaction; 2059ca469637SMiao Xie 2060609e804dSFilipe Manana btrfs_wait_delalloc_flush(trans); 2061cb7ab021SWang Shilong 20622ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2063ed0ca140SJosef Bacik /* 2064ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2065ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20664a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2067ed0ca140SJosef Bacik */ 20680b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20694a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20700b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2071ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2072ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 207315ee9bc7SJosef Bacik 20742cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 207520c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 20762cba30f1SMiao Xie ret = cur_trans->aborted; 20776cf7f77eSWang Shilong goto scrub_continue; 20782cba30f1SMiao Xie } 20797585717fSChris Mason /* 20807585717fSChris Mason * the reloc mutex makes sure that we stop 20817585717fSChris Mason * the balancing code from coming in and moving 20827585717fSChris Mason * extents around in the middle of the commit 20837585717fSChris Mason */ 20840b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 20857585717fSChris Mason 208642874b3dSMiao Xie /* 208742874b3dSMiao Xie * We needn't worry about the delayed items because we will 208842874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 208942874b3dSMiao Xie * core function of the snapshot creation. 209042874b3dSMiao Xie */ 209108d50ca3SNikolay Borisov ret = create_pending_snapshots(trans); 209249b25e05SJeff Mahoney if (ret) { 20930b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 20946cf7f77eSWang Shilong goto scrub_continue; 209549b25e05SJeff Mahoney } 20963063d29fSChris Mason 209742874b3dSMiao Xie /* 209842874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 209942874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 210042874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 210142874b3dSMiao Xie * them. 210242874b3dSMiao Xie * 210342874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 210442874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 210542874b3dSMiao Xie * the nodes and leaves. 210642874b3dSMiao Xie */ 2107e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 210849b25e05SJeff Mahoney if (ret) { 21090b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21106cf7f77eSWang Shilong goto scrub_continue; 211149b25e05SJeff Mahoney } 211216cdcec7SMiao Xie 2113c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 211449b25e05SJeff Mahoney if (ret) { 21150b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21166cf7f77eSWang Shilong goto scrub_continue; 211749b25e05SJeff Mahoney } 211856bec294SChris Mason 2119e999376fSChris Mason /* 2120e999376fSChris Mason * make sure none of the code above managed to slip in a 2121e999376fSChris Mason * delayed item 2122e999376fSChris Mason */ 2123ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2124e999376fSChris Mason 21252c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2126dc17ff8fSChris Mason 2127e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2128e02119d5SChris Mason * various roots consistent with each other. Every pointer 2129e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2130e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2131e02119d5SChris Mason * the tree logging code from jumping in and changing any 2132e02119d5SChris Mason * of the trees. 2133e02119d5SChris Mason * 2134e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2135e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2136e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2137e02119d5SChris Mason * from now until after the super is written, we avoid races 2138e02119d5SChris Mason * with the tree-log code. 2139e02119d5SChris Mason */ 21400b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21411a40e23bSZheng Yan 21427e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 214349b25e05SJeff Mahoney if (ret) { 21440b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21450b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21466cf7f77eSWang Shilong goto scrub_continue; 214749b25e05SJeff Mahoney } 214854aa1f4dSChris Mason 21493818aea2SQu Wenruo /* 21507e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21517e1876acSDavid Sterba * before the next transaction. 21523818aea2SQu Wenruo */ 21530b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21543818aea2SQu Wenruo 21555d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2156e02119d5SChris Mason * safe to free the root of tree log roots 2157e02119d5SChris Mason */ 21580b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2159e02119d5SChris Mason 21600ed4792aSQu Wenruo /* 216182bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 216282bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 216382bafb38SQu Wenruo */ 2164c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 216582bafb38SQu Wenruo if (ret) { 216682bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 216782bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 216882bafb38SQu Wenruo goto scrub_continue; 216982bafb38SQu Wenruo } 217082bafb38SQu Wenruo 217182bafb38SQu Wenruo /* 21720ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21730ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21740ed4792aSQu Wenruo */ 2175460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 21760ed4792aSQu Wenruo if (ret < 0) { 21770b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21780b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21790ed4792aSQu Wenruo goto scrub_continue; 21800ed4792aSQu Wenruo } 21810ed4792aSQu Wenruo 21829386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 218349b25e05SJeff Mahoney if (ret) { 21840b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21850b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21866cf7f77eSWang Shilong goto scrub_continue; 218749b25e05SJeff Mahoney } 218854aa1f4dSChris Mason 21892cba30f1SMiao Xie /* 21902cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 21912cba30f1SMiao Xie * update ->aborted, check it. 21922cba30f1SMiao Xie */ 219320c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 21942cba30f1SMiao Xie ret = cur_trans->aborted; 21950b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21960b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21976cf7f77eSWang Shilong goto scrub_continue; 21982cba30f1SMiao Xie } 21992cba30f1SMiao Xie 22008b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 220111833d66SYan Zheng 22020b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22035f39d397SChris Mason 22040b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22050b246afaSJeff Mahoney fs_info->tree_root->node); 22060b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22079e351cc8SJosef Bacik &cur_trans->switch_commits); 22085d4f98a2SYan Zheng 22090b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22100b246afaSJeff Mahoney fs_info->chunk_root->node); 22110b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22129e351cc8SJosef Bacik &cur_trans->switch_commits); 22139e351cc8SJosef Bacik 221416916a88SNikolay Borisov switch_commit_roots(cur_trans); 22155d4f98a2SYan Zheng 2216ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22171bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22182ff7e61eSJeff Mahoney update_super_roots(fs_info); 2219e02119d5SChris Mason 22200b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22210b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22220b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22230b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2224ccd467d6SChris Mason 2225bbbf7243SNikolay Borisov btrfs_commit_device_sizes(cur_trans); 2226935e5cc9SMiao Xie 22270b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22280b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2229656f30dbSFilipe Manana 22304fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22314fbcdf66SFilipe Manana 22320b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22334a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22340b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22350b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22360b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2237b7ec40d7SChris Mason 22380b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2239e6dcd2dcSChris Mason 224070458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 224149b25e05SJeff Mahoney if (ret) { 22420b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 224308748810SDavid Sterba "Error while writing out transaction"); 22440b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22456cf7f77eSWang Shilong goto scrub_continue; 224649b25e05SJeff Mahoney } 224749b25e05SJeff Mahoney 2248eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 2249e02119d5SChris Mason /* 2250e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2251e02119d5SChris Mason * to go about their business 2252e02119d5SChris Mason */ 22530b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2254c1f32b7cSAnand Jain if (ret) 2255c1f32b7cSAnand Jain goto scrub_continue; 2256e02119d5SChris Mason 22575ead2dd0SNikolay Borisov btrfs_finish_extent_commit(trans); 22584313b399SChris Mason 22593204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22600b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 226113212b54SZhao Lei 22620b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22634a9d8bdeSMiao Xie /* 22644a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22654a9d8bdeSMiao Xie * which can change it. 22664a9d8bdeSMiao Xie */ 22674a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22682c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 2269a514d638SQu Wenruo clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); 22703de4586cSChris Mason 22710b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 227213c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22730b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2274a4abeea4SJosef Bacik 2275724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2276724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 227758176a96SJosef Bacik 22780860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 22790b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2280b2b5ef5cSJan Kara 22813a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 22821abe9b8aSliubo 22832ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2284a2de733cSArne Jansen 22859ed74f2dSJosef Bacik if (current->journal_info == trans) 22869ed74f2dSJosef Bacik current->journal_info = NULL; 22879ed74f2dSJosef Bacik 22882c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 228924bbcf04SYan, Zheng 229079154b1bSChris Mason return ret; 229149b25e05SJeff Mahoney 22926cf7f77eSWang Shilong scrub_continue: 22932ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 229449b25e05SJeff Mahoney cleanup_transaction: 2295dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 2296c7cc64a9SDavid Sterba btrfs_cleanup_pending_block_groups(trans); 22974fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22980e721106SJosef Bacik trans->block_rsv = NULL; 22990b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 230049b25e05SJeff Mahoney if (current->journal_info == trans) 230149b25e05SJeff Mahoney current->journal_info = NULL; 230297cb39bbSNikolay Borisov cleanup_transaction(trans, ret); 230349b25e05SJeff Mahoney 230449b25e05SJeff Mahoney return ret; 230579154b1bSChris Mason } 230679154b1bSChris Mason 2307d352ac68SChris Mason /* 23089d1a2a3aSDavid Sterba * return < 0 if error 23099d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23109d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23119d1a2a3aSDavid Sterba * 23129d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23139d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23149d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23159d1a2a3aSDavid Sterba * few seconds later. 2316d352ac68SChris Mason */ 23179d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2318e9d0b13bSChris Mason { 23199d1a2a3aSDavid Sterba int ret; 23205d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2321e9d0b13bSChris Mason 2322a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23239d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23249d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23259d1a2a3aSDavid Sterba return 0; 23269d1a2a3aSDavid Sterba } 23279d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23289d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2329cfad392bSJosef Bacik list_del_init(&root->root_list); 2330a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23315d4f98a2SYan Zheng 23324fd786e6SMisono Tomohiro btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); 233376dda93cSYan, Zheng 233416cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 233516cdcec7SMiao Xie 233676dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 233776dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23382c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 233976dda93cSYan, Zheng else 23402c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 234132471dc2SDavid Sterba 23426596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2343e9d0b13bSChris Mason } 2344572d9ab7SDavid Sterba 2345572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2346572d9ab7SDavid Sterba { 2347572d9ab7SDavid Sterba unsigned long prev; 2348572d9ab7SDavid Sterba unsigned long bit; 2349572d9ab7SDavid Sterba 23506c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2351572d9ab7SDavid Sterba if (!prev) 2352572d9ab7SDavid Sterba return; 2353572d9ab7SDavid Sterba 23547e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23557e1876acSDavid Sterba if (prev & bit) 23567e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23577e1876acSDavid Sterba prev &= ~bit; 23587e1876acSDavid Sterba 23597e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23607e1876acSDavid Sterba if (prev & bit) 23617e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23627e1876acSDavid Sterba prev &= ~bit; 23637e1876acSDavid Sterba 2364d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2365d51033d0SDavid Sterba if (prev & bit) 2366d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2367d51033d0SDavid Sterba prev &= ~bit; 2368d51033d0SDavid Sterba 2369572d9ab7SDavid Sterba if (prev) 2370572d9ab7SDavid Sterba btrfs_warn(fs_info, 2371572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2372572d9ab7SDavid Sterba } 2373