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" 22*aac0023cSJosef Bacik #include "block-group.h" 2379154b1bSChris Mason 240f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 250f7d52f4SChris Mason 26e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 274a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 28bcf3a3e7SNikolay Borisov [TRANS_STATE_BLOCKED] = __TRANS_START, 29bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), 30bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | 314a9d8bdeSMiao Xie __TRANS_ATTACH | 32a6d155d2SFilipe Manana __TRANS_JOIN | 33a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 34bcf3a3e7SNikolay Borisov [TRANS_STATE_UNBLOCKED] = (__TRANS_START | 354a9d8bdeSMiao Xie __TRANS_ATTACH | 364a9d8bdeSMiao Xie __TRANS_JOIN | 37a6d155d2SFilipe Manana __TRANS_JOIN_NOLOCK | 38a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 39bcf3a3e7SNikolay Borisov [TRANS_STATE_COMPLETED] = (__TRANS_START | 404a9d8bdeSMiao Xie __TRANS_ATTACH | 414a9d8bdeSMiao Xie __TRANS_JOIN | 42a6d155d2SFilipe Manana __TRANS_JOIN_NOLOCK | 43a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 444a9d8bdeSMiao Xie }; 454a9d8bdeSMiao Xie 46724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 4779154b1bSChris Mason { 489b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 499b64f57dSElena Reshetova if (refcount_dec_and_test(&transaction->use_count)) { 50a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 515c9d028bSLiu Bo WARN_ON(!RB_EMPTY_ROOT( 525c9d028bSLiu Bo &transaction->delayed_refs.href_root.rb_root)); 531262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 54ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 55ab8d0fc4SJeff Mahoney "pending csums is %llu", 561262133bSJosef Bacik transaction->delayed_refs.pending_csums); 577785a663SFilipe Manana /* 587785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 597785a663SFilipe Manana * because the transaction was aborted and a commit did not 607785a663SFilipe Manana * happen (things failed before writing the new superblock 617785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 627785a663SFilipe Manana * discard the physical locations of the block groups. 637785a663SFilipe Manana */ 647785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 657785a663SFilipe Manana struct btrfs_block_group_cache *cache; 667785a663SFilipe Manana 677785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 687785a663SFilipe Manana struct btrfs_block_group_cache, 697785a663SFilipe Manana bg_list); 707785a663SFilipe Manana list_del_init(&cache->bg_list); 717785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 727785a663SFilipe Manana btrfs_put_block_group(cache); 737785a663SFilipe Manana } 74bbbf7243SNikolay Borisov WARN_ON(!list_empty(&transaction->dev_update_list)); 754b5faeacSDavid Sterba kfree(transaction); 7679154b1bSChris Mason } 7778fae27eSChris Mason } 7879154b1bSChris Mason 7916916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans) 80817d52f8SJosef Bacik { 8116916a88SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 829e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 839e351cc8SJosef Bacik 849e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 859e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 869e351cc8SJosef Bacik dirty_list) { 879e351cc8SJosef Bacik list_del_init(&root->dirty_list); 88817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 89817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 904fd786e6SMisono Tomohiro if (is_fstree(root->root_key.objectid)) 919e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 9241e7acd3SNikolay Borisov extent_io_tree_release(&root->dirty_log_pages); 93370a11b8SQu Wenruo btrfs_qgroup_clean_swapped_blocks(root); 949e351cc8SJosef Bacik } 952b9dbef2SJosef Bacik 962b9dbef2SJosef Bacik /* We can free old roots now. */ 972b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 982b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 992b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1002b9dbef2SJosef Bacik struct btrfs_root, root_list); 1012b9dbef2SJosef Bacik list_del_init(&root->root_list); 1022b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1032b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1042b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1052b9dbef2SJosef Bacik } 1062b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1079e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 108817d52f8SJosef Bacik } 109817d52f8SJosef Bacik 1100860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1110860adfdSMiao Xie unsigned int type) 1120860adfdSMiao Xie { 1130860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1140860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1150860adfdSMiao Xie } 1160860adfdSMiao Xie 1170860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1180860adfdSMiao Xie unsigned int type) 1190860adfdSMiao Xie { 1200860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1210860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1220860adfdSMiao Xie } 1230860adfdSMiao Xie 1240860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1250860adfdSMiao Xie unsigned int type) 1260860adfdSMiao Xie { 1270860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1280860adfdSMiao Xie } 1290860adfdSMiao Xie 1300860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1310860adfdSMiao Xie { 1320860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 133178260b2SMiao Xie } 134178260b2SMiao Xie 135d352ac68SChris Mason /* 136fb6dea26SJosef Bacik * To be called after all the new block groups attached to the transaction 137fb6dea26SJosef Bacik * handle have been created (btrfs_create_pending_block_groups()). 138fb6dea26SJosef Bacik */ 139fb6dea26SJosef Bacik void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) 140fb6dea26SJosef Bacik { 141fb6dea26SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 142fb6dea26SJosef Bacik 143fb6dea26SJosef Bacik if (!trans->chunk_bytes_reserved) 144fb6dea26SJosef Bacik return; 145fb6dea26SJosef Bacik 146fb6dea26SJosef Bacik WARN_ON_ONCE(!list_empty(&trans->new_bgs)); 147fb6dea26SJosef Bacik 148fb6dea26SJosef Bacik btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv, 149fb6dea26SJosef Bacik trans->chunk_bytes_reserved); 150fb6dea26SJosef Bacik trans->chunk_bytes_reserved = 0; 151fb6dea26SJosef Bacik } 152fb6dea26SJosef Bacik 153fb6dea26SJosef Bacik /* 154d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 155d352ac68SChris Mason */ 1562ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1572ff7e61eSJeff Mahoney unsigned int type) 15879154b1bSChris Mason { 15979154b1bSChris Mason struct btrfs_transaction *cur_trans; 160a4abeea4SJosef Bacik 16119ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 162d43317dcSChris Mason loop: 16349b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 16487533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 16519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16649b25e05SJeff Mahoney return -EROFS; 16749b25e05SJeff Mahoney } 16849b25e05SJeff Mahoney 16919ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 170a4abeea4SJosef Bacik if (cur_trans) { 171871383beSDavid Sterba if (cur_trans->aborted) { 17219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 17349b25e05SJeff Mahoney return cur_trans->aborted; 174871383beSDavid Sterba } 1754a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 176178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 177178260b2SMiao Xie return -EBUSY; 178178260b2SMiao Xie } 1799b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 180a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1810860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 18219ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 183a4abeea4SJosef Bacik return 0; 184a4abeea4SJosef Bacik } 18519ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 186a4abeea4SJosef Bacik 187354aa0fbSMiao Xie /* 188354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 189354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 190354aa0fbSMiao Xie */ 191354aa0fbSMiao Xie if (type == TRANS_ATTACH) 192354aa0fbSMiao Xie return -ENOENT; 193354aa0fbSMiao Xie 1944a9d8bdeSMiao Xie /* 1954a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1964a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1974a9d8bdeSMiao Xie */ 1984a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 1994a9d8bdeSMiao Xie 2004b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS); 201db5b493aSTsutomu Itoh if (!cur_trans) 202db5b493aSTsutomu Itoh return -ENOMEM; 203d43317dcSChris Mason 20419ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 20519ae4e81SJan Schmidt if (fs_info->running_transaction) { 206d43317dcSChris Mason /* 207d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2084a9d8bdeSMiao Xie * to redo the checks above 209d43317dcSChris Mason */ 2104b5faeacSDavid Sterba kfree(cur_trans); 211d43317dcSChris Mason goto loop; 21287533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 213e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2144b5faeacSDavid Sterba kfree(cur_trans); 2157b8b92afSJosef Bacik return -EROFS; 216a4abeea4SJosef Bacik } 217d43317dcSChris Mason 218ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 21913c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2200860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 22179154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 22279154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 2234a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 224a4abeea4SJosef Bacik /* 225a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 226a4abeea4SJosef Bacik * commit the transaction. 227a4abeea4SJosef Bacik */ 2289b64f57dSElena Reshetova refcount_set(&cur_trans->use_count, 2); 2293204d33cSJosef Bacik cur_trans->flags = 0; 230afd48513SArnd Bergmann cur_trans->start_time = ktime_get_seconds(); 23156bec294SChris Mason 232a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 233a099d0fdSAlexandru Moise 2345c9d028bSLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; 2353368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 236d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 23720b297d6SJan Schmidt 23820b297d6SJan Schmidt /* 23920b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 24020b297d6SJan Schmidt * the log must never go across transaction boundaries. 24120b297d6SJan Schmidt */ 24220b297d6SJan Schmidt smp_mb(); 24331b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2445d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 24531b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2465d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n"); 247fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 24820b297d6SJan Schmidt 24956bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 25056bec294SChris Mason 2513063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 252bbbf7243SNikolay Borisov INIT_LIST_HEAD(&cur_trans->dev_update_list); 2539e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 254ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2551bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2562b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2571bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 258ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 259e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2602b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 26119ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 262c258d6e3SQu Wenruo extent_io_tree_init(fs_info, &cur_trans->dirty_pages, 26343eb5f29SQu Wenruo IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); 26419ae4e81SJan Schmidt fs_info->generation++; 26519ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 26619ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 26749b25e05SJeff Mahoney cur_trans->aborted = 0; 26819ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 26915ee9bc7SJosef Bacik 27079154b1bSChris Mason return 0; 27179154b1bSChris Mason } 27279154b1bSChris Mason 273d352ac68SChris Mason /* 274d397712bSChris Mason * this does all the record keeping required to make sure that a reference 275d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 276d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 277d397712bSChris Mason * when the transaction commits 278d352ac68SChris Mason */ 2797585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2806426c7adSQu Wenruo struct btrfs_root *root, 2816426c7adSQu Wenruo int force) 2826702ed49SChris Mason { 2830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2840b246afaSJeff Mahoney 2856426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2866426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 2870b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 2884d31778aSQu Wenruo WARN_ON(!force && root->commit_root != root->node); 2895d4f98a2SYan Zheng 2907585717fSChris Mason /* 29127cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2927585717fSChris Mason * we have the reloc mutex held now, so there 2937585717fSChris Mason * is only one writer in this function 2947585717fSChris Mason */ 29527cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2967585717fSChris Mason 29727cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2987585717fSChris Mason * they find our root->last_trans update 2997585717fSChris Mason */ 3007585717fSChris Mason smp_wmb(); 3017585717fSChris Mason 3020b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3036426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 3040b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 305a4abeea4SJosef Bacik return 0; 306a4abeea4SJosef Bacik } 3070b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 3086702ed49SChris Mason (unsigned long)root->root_key.objectid, 3096702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 3100b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3117585717fSChris Mason root->last_trans = trans->transid; 3127585717fSChris Mason 3137585717fSChris Mason /* this is pretty tricky. We don't want to 3147585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3157585717fSChris Mason * unless we're really doing the first setup for this root in 3167585717fSChris Mason * this transaction. 3177585717fSChris Mason * 3187585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3197585717fSChris Mason * if we want to take the expensive mutex. 3207585717fSChris Mason * 3217585717fSChris Mason * But, we have to set root->last_trans before we 3227585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3237585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 32427cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3257585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3267585717fSChris Mason * 3277585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3287585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3297585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3307585717fSChris Mason * done before we pop in the zero below 3317585717fSChris Mason */ 3325d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 333c7548af6SChris Mason smp_mb__before_atomic(); 33427cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3356702ed49SChris Mason } 3365d4f98a2SYan Zheng return 0; 3376702ed49SChris Mason } 3385d4f98a2SYan Zheng 3397585717fSChris Mason 3402b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3412b9dbef2SJosef Bacik struct btrfs_root *root) 3422b9dbef2SJosef Bacik { 3430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3442b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3452b9dbef2SJosef Bacik 3462b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3472b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3482b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3492b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3502b9dbef2SJosef Bacik 3512b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3520b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3530b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3542b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3552b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3560b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3572b9dbef2SJosef Bacik } 3582b9dbef2SJosef Bacik 3597585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3607585717fSChris Mason struct btrfs_root *root) 3617585717fSChris Mason { 3620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3630b246afaSJeff Mahoney 36427cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3657585717fSChris Mason return 0; 3667585717fSChris Mason 3677585717fSChris Mason /* 36827cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3697585717fSChris Mason * and barriers 3707585717fSChris Mason */ 3717585717fSChris Mason smp_rmb(); 3727585717fSChris Mason if (root->last_trans == trans->transid && 37327cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3747585717fSChris Mason return 0; 3757585717fSChris Mason 3760b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 3776426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 3780b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 3797585717fSChris Mason 3807585717fSChris Mason return 0; 3817585717fSChris Mason } 3827585717fSChris Mason 3834a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3844a9d8bdeSMiao Xie { 3854a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 386501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 387501407aaSJosef Bacik !trans->aborted); 3884a9d8bdeSMiao Xie } 3894a9d8bdeSMiao Xie 390d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 391d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 392d352ac68SChris Mason * transaction might not be fully on disk. 393d352ac68SChris Mason */ 3942ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 39579154b1bSChris Mason { 396f9295749SChris Mason struct btrfs_transaction *cur_trans; 39779154b1bSChris Mason 3980b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 3990b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 4004a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 4019b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 4020b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 40372d63ed6SLi Zefan 4040b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 405501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 406501407aaSJosef Bacik cur_trans->aborted); 407724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 408a4abeea4SJosef Bacik } else { 4090b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 410f9295749SChris Mason } 41137d1aeeeSChris Mason } 41237d1aeeeSChris Mason 4132ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 41437d1aeeeSChris Mason { 4150b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 416a4abeea4SJosef Bacik return 0; 417a4abeea4SJosef Bacik 41892e2f7e3SNikolay Borisov if (type == TRANS_START) 419a4abeea4SJosef Bacik return 1; 420a4abeea4SJosef Bacik 421a22285a6SYan, Zheng return 0; 422a22285a6SYan, Zheng } 423a22285a6SYan, Zheng 42420dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 42520dd2cbfSMiao Xie { 4260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4270b246afaSJeff Mahoney 4280b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 42927cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 43020dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 43120dd2cbfSMiao Xie root->reloc_root) 43220dd2cbfSMiao Xie return false; 43320dd2cbfSMiao Xie 43420dd2cbfSMiao Xie return true; 43520dd2cbfSMiao Xie } 43620dd2cbfSMiao Xie 43708e007d2SMiao Xie static struct btrfs_trans_handle * 4385aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 439003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 440003d7c59SJeff Mahoney bool enforce_qgroups) 441a22285a6SYan, Zheng { 4420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 443ba2c4d4eSJosef Bacik struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; 444a22285a6SYan, Zheng struct btrfs_trans_handle *h; 445a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 446b5009945SJosef Bacik u64 num_bytes = 0; 447c5567237SArne Jansen u64 qgroup_reserved = 0; 44820dd2cbfSMiao Xie bool reloc_reserved = false; 44920dd2cbfSMiao Xie int ret; 450acce952bSliubo 45146c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4522755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 45346c4e71eSFilipe Manana 4540b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 455acce952bSliubo return ERR_PTR(-EROFS); 4562a1eb461SJosef Bacik 45746c4e71eSFilipe Manana if (current->journal_info) { 4580860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4592a1eb461SJosef Bacik h = current->journal_info; 460b50fff81SDavid Sterba refcount_inc(&h->use_count); 461b50fff81SDavid Sterba WARN_ON(refcount_read(&h->use_count) > 2); 4622a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4632a1eb461SJosef Bacik h->block_rsv = NULL; 4642a1eb461SJosef Bacik goto got_it; 4652a1eb461SJosef Bacik } 466b5009945SJosef Bacik 467b5009945SJosef Bacik /* 468b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 469b5009945SJosef Bacik * the appropriate flushing if need be. 470b5009945SJosef Bacik */ 471003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 472ba2c4d4eSJosef Bacik struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; 473ba2c4d4eSJosef Bacik u64 delayed_refs_bytes = 0; 474ba2c4d4eSJosef Bacik 4750b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 476733e03a0SQu Wenruo ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, 477003d7c59SJeff Mahoney enforce_qgroups); 478c5567237SArne Jansen if (ret) 479c5567237SArne Jansen return ERR_PTR(ret); 480c5567237SArne Jansen 481ba2c4d4eSJosef Bacik /* 482ba2c4d4eSJosef Bacik * We want to reserve all the bytes we may need all at once, so 483ba2c4d4eSJosef Bacik * we only do 1 enospc flushing cycle per transaction start. We 484ba2c4d4eSJosef Bacik * accomplish this by simply assuming we'll do 2 x num_items 485ba2c4d4eSJosef Bacik * worth of delayed refs updates in this trans handle, and 486ba2c4d4eSJosef Bacik * refill that amount for whatever is missing in the reserve. 487ba2c4d4eSJosef Bacik */ 4880b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 489ba2c4d4eSJosef Bacik if (delayed_refs_rsv->full == 0) { 490ba2c4d4eSJosef Bacik delayed_refs_bytes = num_bytes; 491ba2c4d4eSJosef Bacik num_bytes <<= 1; 492ba2c4d4eSJosef Bacik } 493ba2c4d4eSJosef Bacik 49420dd2cbfSMiao Xie /* 49520dd2cbfSMiao Xie * Do the reservation for the relocation root creation 49620dd2cbfSMiao Xie */ 497ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 4980b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 49920dd2cbfSMiao Xie reloc_reserved = true; 50020dd2cbfSMiao Xie } 50120dd2cbfSMiao Xie 502ba2c4d4eSJosef Bacik ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); 503ba2c4d4eSJosef Bacik if (ret) 504ba2c4d4eSJosef Bacik goto reserve_fail; 505ba2c4d4eSJosef Bacik if (delayed_refs_bytes) { 506ba2c4d4eSJosef Bacik btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, 507ba2c4d4eSJosef Bacik delayed_refs_bytes); 508ba2c4d4eSJosef Bacik num_bytes -= delayed_refs_bytes; 509ba2c4d4eSJosef Bacik } 510ba2c4d4eSJosef Bacik } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && 511ba2c4d4eSJosef Bacik !delayed_refs_rsv->full) { 512ba2c4d4eSJosef Bacik /* 513ba2c4d4eSJosef Bacik * Some people call with btrfs_start_transaction(root, 0) 514ba2c4d4eSJosef Bacik * because they can be throttled, but have some other mechanism 515ba2c4d4eSJosef Bacik * for reserving space. We still want these guys to refill the 516ba2c4d4eSJosef Bacik * delayed block_rsv so just add 1 items worth of reservation 517ba2c4d4eSJosef Bacik * here. 518ba2c4d4eSJosef Bacik */ 519ba2c4d4eSJosef Bacik ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); 520b5009945SJosef Bacik if (ret) 521843fcf35SMiao Xie goto reserve_fail; 522b5009945SJosef Bacik } 523a22285a6SYan, Zheng again: 524f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 525843fcf35SMiao Xie if (!h) { 526843fcf35SMiao Xie ret = -ENOMEM; 527843fcf35SMiao Xie goto alloc_fail; 528843fcf35SMiao Xie } 529a22285a6SYan, Zheng 53098114659SJosef Bacik /* 53198114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 53298114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 53398114659SJosef Bacik * because we're already holding a ref. We need this because we could 53498114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53598114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 536354aa0fbSMiao Xie * 537354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 538354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 53998114659SJosef Bacik */ 5400860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5410b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 542b2b5ef5cSJan Kara 5432ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5442ff7e61eSJeff Mahoney wait_current_trans(fs_info); 545a22285a6SYan, Zheng 546a4abeea4SJosef Bacik do { 5472ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 548178260b2SMiao Xie if (ret == -EBUSY) { 5492ff7e61eSJeff Mahoney wait_current_trans(fs_info); 550a6d155d2SFilipe Manana if (unlikely(type == TRANS_ATTACH || 551a6d155d2SFilipe Manana type == TRANS_JOIN_NOSTART)) 552178260b2SMiao Xie ret = -ENOENT; 553178260b2SMiao Xie } 554a4abeea4SJosef Bacik } while (ret == -EBUSY); 555a4abeea4SJosef Bacik 556a43f7f82SLiu Bo if (ret < 0) 557843fcf35SMiao Xie goto join_fail; 5580f7d52f4SChris Mason 5590b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 560a22285a6SYan, Zheng 561a22285a6SYan, Zheng h->transid = cur_trans->transid; 562a22285a6SYan, Zheng h->transaction = cur_trans; 563d13603efSArne Jansen h->root = root; 564b50fff81SDavid Sterba refcount_set(&h->use_count, 1); 56564b63580SJeff Mahoney h->fs_info = root->fs_info; 5667174109cSQu Wenruo 567a698d075SMiao Xie h->type = type; 568d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 569ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 570b7ec40d7SChris Mason 571a22285a6SYan, Zheng smp_mb(); 5724a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5732ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 574abdd2e80SFilipe Manana current->journal_info = h; 5753a45bb20SJeff Mahoney btrfs_commit_transaction(h); 576a22285a6SYan, Zheng goto again; 577a22285a6SYan, Zheng } 5789ed74f2dSJosef Bacik 579b5009945SJosef Bacik if (num_bytes) { 5800b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5812bcc0328SLiu Bo h->transid, num_bytes, 1); 5820b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 583b5009945SJosef Bacik h->bytes_reserved = num_bytes; 58420dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 585a22285a6SYan, Zheng } 586a22285a6SYan, Zheng 5872a1eb461SJosef Bacik got_it: 588a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 589a22285a6SYan, Zheng 590bcf3a3e7SNikolay Borisov if (!current->journal_info) 591a22285a6SYan, Zheng current->journal_info = h; 59279154b1bSChris Mason return h; 593843fcf35SMiao Xie 594843fcf35SMiao Xie join_fail: 5950860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5960b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 597843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 598843fcf35SMiao Xie alloc_fail: 599843fcf35SMiao Xie if (num_bytes) 6002ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 601843fcf35SMiao Xie num_bytes); 602843fcf35SMiao Xie reserve_fail: 603733e03a0SQu Wenruo btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); 604843fcf35SMiao Xie return ERR_PTR(ret); 60579154b1bSChris Mason } 60679154b1bSChris Mason 607f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6085aed1dd8SAlexandru Moise unsigned int num_items) 609f9295749SChris Mason { 61008e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 611003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 612f9295749SChris Mason } 613003d7c59SJeff Mahoney 6148eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6158eab77ffSFilipe Manana struct btrfs_root *root, 6168eab77ffSFilipe Manana unsigned int num_items, 6178eab77ffSFilipe Manana int min_factor) 6188eab77ffSFilipe Manana { 6190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6208eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6218eab77ffSFilipe Manana u64 num_bytes; 6228eab77ffSFilipe Manana int ret; 6238eab77ffSFilipe Manana 624003d7c59SJeff Mahoney /* 625003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 626003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 627003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 628003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 629003d7c59SJeff Mahoney */ 630003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 631003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6328eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6338eab77ffSFilipe Manana return trans; 6348eab77ffSFilipe Manana 6358eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6368eab77ffSFilipe Manana if (IS_ERR(trans)) 6378eab77ffSFilipe Manana return trans; 6388eab77ffSFilipe Manana 6390b246afaSJeff Mahoney num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items); 6400b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6410b246afaSJeff Mahoney num_bytes, min_factor); 6428eab77ffSFilipe Manana if (ret) { 6433a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6448eab77ffSFilipe Manana return ERR_PTR(ret); 6458eab77ffSFilipe Manana } 6468eab77ffSFilipe Manana 6470b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6488eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6490b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 65088d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6518eab77ffSFilipe Manana 6528eab77ffSFilipe Manana return trans; 6538eab77ffSFilipe Manana } 6548407aa46SMiao Xie 6557a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 656f9295749SChris Mason { 657003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 658003d7c59SJeff Mahoney true); 659f9295749SChris Mason } 660f9295749SChris Mason 6617a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6620af3d00bSJosef Bacik { 663575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 664003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6650af3d00bSJosef Bacik } 6660af3d00bSJosef Bacik 667d4edf39bSMiao Xie /* 668a6d155d2SFilipe Manana * Similar to regular join but it never starts a transaction when none is 669a6d155d2SFilipe Manana * running or after waiting for the current one to finish. 670a6d155d2SFilipe Manana */ 671a6d155d2SFilipe Manana struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root) 672a6d155d2SFilipe Manana { 673a6d155d2SFilipe Manana return start_transaction(root, 0, TRANS_JOIN_NOSTART, 674a6d155d2SFilipe Manana BTRFS_RESERVE_NO_FLUSH, true); 675a6d155d2SFilipe Manana } 676a6d155d2SFilipe Manana 677a6d155d2SFilipe Manana /* 678d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 679d4edf39bSMiao Xie * 680d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 681d4edf39bSMiao Xie * don't want to start a new one. 682d4edf39bSMiao Xie * 683d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 684d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 685d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 686d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 687d4edf39bSMiao Xie * invoke 688d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 689d4edf39bSMiao Xie */ 690354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 69160376ce4SJosef Bacik { 692575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 693003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 69460376ce4SJosef Bacik } 69560376ce4SJosef Bacik 696d4edf39bSMiao Xie /* 69790b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 698d4edf39bSMiao Xie * 69952042d8eSAndrea Gelmini * It is similar to the above function, the difference is this one 700d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 701d4edf39bSMiao Xie * complete. 702d4edf39bSMiao Xie */ 703d4edf39bSMiao Xie struct btrfs_trans_handle * 704d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 705d4edf39bSMiao Xie { 706d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 707d4edf39bSMiao Xie 708575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 709003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 7108d9e220cSAl Viro if (trans == ERR_PTR(-ENOENT)) 7112ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 712d4edf39bSMiao Xie 713d4edf39bSMiao Xie return trans; 714d4edf39bSMiao Xie } 715d4edf39bSMiao Xie 716d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 7172ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 71889ce8a63SChris Mason { 7194a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 72089ce8a63SChris Mason } 72189ce8a63SChris Mason 7222ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 72346204592SSage Weil { 72446204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7258cd2807fSMiao Xie int ret = 0; 72646204592SSage Weil 72746204592SSage Weil if (transid) { 7280b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 729a4abeea4SJosef Bacik goto out; 73046204592SSage Weil 73146204592SSage Weil /* find specified transaction */ 7320b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7330b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 73446204592SSage Weil if (t->transid == transid) { 73546204592SSage Weil cur_trans = t; 7369b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7378cd2807fSMiao Xie ret = 0; 73846204592SSage Weil break; 73946204592SSage Weil } 7408cd2807fSMiao Xie if (t->transid > transid) { 7418cd2807fSMiao Xie ret = 0; 74246204592SSage Weil break; 74346204592SSage Weil } 7448cd2807fSMiao Xie } 7450b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 74642383020SSage Weil 74742383020SSage Weil /* 74842383020SSage Weil * The specified transaction doesn't exist, or we 74942383020SSage Weil * raced with btrfs_commit_transaction 75042383020SSage Weil */ 75142383020SSage Weil if (!cur_trans) { 7520b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 75342383020SSage Weil ret = -EINVAL; 7548cd2807fSMiao Xie goto out; 75542383020SSage Weil } 75646204592SSage Weil } else { 75746204592SSage Weil /* find newest transaction that is committing | committed */ 7580b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7590b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 76046204592SSage Weil list) { 7614a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7624a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7633473f3c0SJosef Bacik break; 76446204592SSage Weil cur_trans = t; 7659b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 76646204592SSage Weil break; 76746204592SSage Weil } 76846204592SSage Weil } 7690b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 77046204592SSage Weil if (!cur_trans) 771a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 77246204592SSage Weil } 77346204592SSage Weil 7742ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 775724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 776a4abeea4SJosef Bacik out: 77746204592SSage Weil return ret; 77846204592SSage Weil } 77946204592SSage Weil 7802ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 78137d1aeeeSChris Mason { 7822ff7e61eSJeff Mahoney wait_current_trans(fs_info); 78337d1aeeeSChris Mason } 78437d1aeeeSChris Mason 7852ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7868929ecfaSYan, Zheng { 7872ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7880b246afaSJeff Mahoney 78964403612SJosef Bacik if (btrfs_check_space_for_delayed_refs(fs_info)) 7901be41b78SJosef Bacik return 1; 79136ba022aSJosef Bacik 7922ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 7938929ecfaSYan, Zheng } 7948929ecfaSYan, Zheng 7953a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 7968929ecfaSYan, Zheng { 7978929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7988929ecfaSYan, Zheng 799a4abeea4SJosef Bacik smp_mb(); 8004a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8014a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8028929ecfaSYan, Zheng return 1; 8038929ecfaSYan, Zheng 8042ff7e61eSJeff Mahoney return should_end_transaction(trans); 8058929ecfaSYan, Zheng } 8068929ecfaSYan, Zheng 807dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) 808dc60c525SNikolay Borisov 8090e34693fSNikolay Borisov { 810dc60c525SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 811dc60c525SNikolay Borisov 8120e34693fSNikolay Borisov if (!trans->block_rsv) { 8130e34693fSNikolay Borisov ASSERT(!trans->bytes_reserved); 8140e34693fSNikolay Borisov return; 8150e34693fSNikolay Borisov } 8160e34693fSNikolay Borisov 8170e34693fSNikolay Borisov if (!trans->bytes_reserved) 8180e34693fSNikolay Borisov return; 8190e34693fSNikolay Borisov 8200e34693fSNikolay Borisov ASSERT(trans->block_rsv == &fs_info->trans_block_rsv); 8210e34693fSNikolay Borisov trace_btrfs_space_reservation(fs_info, "transaction", 8220e34693fSNikolay Borisov trans->transid, trans->bytes_reserved, 0); 8230e34693fSNikolay Borisov btrfs_block_rsv_release(fs_info, trans->block_rsv, 8240e34693fSNikolay Borisov trans->bytes_reserved); 8250e34693fSNikolay Borisov trans->bytes_reserved = 0; 8260e34693fSNikolay Borisov } 8270e34693fSNikolay Borisov 82889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 8293a45bb20SJeff Mahoney int throttle) 83079154b1bSChris Mason { 8313a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8328929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 833a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8344edc2ca3SDave Jones int err = 0; 835d6e4a428SChris Mason 836b50fff81SDavid Sterba if (refcount_read(&trans->use_count) > 1) { 837b50fff81SDavid Sterba refcount_dec(&trans->use_count); 8382a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8392a1eb461SJosef Bacik return 0; 8402a1eb461SJosef Bacik } 8412a1eb461SJosef Bacik 842dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8434c13d758SJosef Bacik trans->block_rsv = NULL; 844c5567237SArne Jansen 8456c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 846ea658badSJosef Bacik 8474fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8484fbcdf66SFilipe Manana 84920c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8503bbb24b2SJosef Bacik if (throttle) 8513a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8523bbb24b2SJosef Bacik else 8538929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8548929ecfaSYan, Zheng } 8558929ecfaSYan, Zheng 8560860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8570b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8586df7881aSJosef Bacik 8598929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 86013c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 86113c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8620860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 86389ce8a63SChris Mason 864093258e6SDavid Sterba cond_wake_up(&cur_trans->writer_wait); 865724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8669ed74f2dSJosef Bacik 8679ed74f2dSJosef Bacik if (current->journal_info == trans) 8689ed74f2dSJosef Bacik current->journal_info = NULL; 869ab78c84dSChris Mason 87024bbcf04SYan, Zheng if (throttle) 8712ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 87224bbcf04SYan, Zheng 87349b25e05SJeff Mahoney if (trans->aborted || 8740b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 8754e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8764edc2ca3SDave Jones err = -EIO; 8774e121c06SJosef Bacik } 87849b25e05SJeff Mahoney 8794edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 8804edc2ca3SDave Jones return err; 88179154b1bSChris Mason } 88279154b1bSChris Mason 8833a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 88489ce8a63SChris Mason { 8853a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 88689ce8a63SChris Mason } 88789ce8a63SChris Mason 8883a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 88989ce8a63SChris Mason { 8903a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 89116cdcec7SMiao Xie } 89216cdcec7SMiao Xie 893d352ac68SChris Mason /* 894d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 895d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 896690587d1SChris Mason * those extents are sent to disk but does not wait on them 897d352ac68SChris Mason */ 8982ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 8998cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 90079154b1bSChris Mason { 901777e6bd7SChris Mason int err = 0; 9027c4452b9SChris Mason int werr = 0; 9030b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 904e6138876SJosef Bacik struct extent_state *cached_state = NULL; 905777e6bd7SChris Mason u64 start = 0; 9065f39d397SChris Mason u64 end; 9077c4452b9SChris Mason 9086300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 9091728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 910e6138876SJosef Bacik mark, &cached_state)) { 911663dfbb0SFilipe Manana bool wait_writeback = false; 912663dfbb0SFilipe Manana 913663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 914663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 915210aa277SDavid Sterba mark, &cached_state); 916663dfbb0SFilipe Manana /* 917663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 918663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 919663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 920663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 921bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 922bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 923bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 924bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 925bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 926663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 92741e7acd3SNikolay Borisov * the transaction (through extent_io_tree_release()). 928663dfbb0SFilipe Manana */ 929663dfbb0SFilipe Manana if (err == -ENOMEM) { 930663dfbb0SFilipe Manana err = 0; 931663dfbb0SFilipe Manana wait_writeback = true; 932663dfbb0SFilipe Manana } 933663dfbb0SFilipe Manana if (!err) 9341728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9357c4452b9SChris Mason if (err) 9367c4452b9SChris Mason werr = err; 937663dfbb0SFilipe Manana else if (wait_writeback) 938663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 939e38e2ed7SFilipe Manana free_extent_state(cached_state); 940663dfbb0SFilipe Manana cached_state = NULL; 9411728366eSJosef Bacik cond_resched(); 9421728366eSJosef Bacik start = end + 1; 9437c4452b9SChris Mason } 9446300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 945690587d1SChris Mason return werr; 946690587d1SChris Mason } 947690587d1SChris Mason 948690587d1SChris Mason /* 949690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 950690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 951690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 952690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 953690587d1SChris Mason */ 954bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 955bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 956690587d1SChris Mason { 957690587d1SChris Mason int err = 0; 958690587d1SChris Mason int werr = 0; 9590b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 960e6138876SJosef Bacik struct extent_state *cached_state = NULL; 961690587d1SChris Mason u64 start = 0; 962690587d1SChris Mason u64 end; 963690587d1SChris Mason 9641728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 965e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 966663dfbb0SFilipe Manana /* 967663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 968663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 969663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 970663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 971663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 97241e7acd3SNikolay Borisov * it's safe to do it (through extent_io_tree_release()). 973663dfbb0SFilipe Manana */ 974663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 975ae0f1625SDavid Sterba EXTENT_NEED_WAIT, 0, 0, &cached_state); 976663dfbb0SFilipe Manana if (err == -ENOMEM) 977663dfbb0SFilipe Manana err = 0; 978663dfbb0SFilipe Manana if (!err) 9791728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 980777e6bd7SChris Mason if (err) 981777e6bd7SChris Mason werr = err; 982e38e2ed7SFilipe Manana free_extent_state(cached_state); 983e38e2ed7SFilipe Manana cached_state = NULL; 984777e6bd7SChris Mason cond_resched(); 9851728366eSJosef Bacik start = end + 1; 986777e6bd7SChris Mason } 9877c4452b9SChris Mason if (err) 9887c4452b9SChris Mason werr = err; 989bf89d38fSJeff Mahoney return werr; 990bf89d38fSJeff Mahoney } 991656f30dbSFilipe Manana 992bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 993bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 994bf89d38fSJeff Mahoney { 995bf89d38fSJeff Mahoney bool errors = false; 996bf89d38fSJeff Mahoney int err; 997bf89d38fSJeff Mahoney 998bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 999bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 1000bf89d38fSJeff Mahoney errors = true; 1001bf89d38fSJeff Mahoney 1002bf89d38fSJeff Mahoney if (errors && !err) 1003bf89d38fSJeff Mahoney err = -EIO; 1004bf89d38fSJeff Mahoney return err; 1005bf89d38fSJeff Mahoney } 1006bf89d38fSJeff Mahoney 1007bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 1008bf89d38fSJeff Mahoney { 1009bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 1010bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 1011bf89d38fSJeff Mahoney bool errors = false; 1012bf89d38fSJeff Mahoney int err; 1013bf89d38fSJeff Mahoney 1014bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 1015bf89d38fSJeff Mahoney 1016bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1017656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10180b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1019656f30dbSFilipe Manana errors = true; 1020656f30dbSFilipe Manana 1021656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10220b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1023656f30dbSFilipe Manana errors = true; 1024656f30dbSFilipe Manana 1025bf89d38fSJeff Mahoney if (errors && !err) 1026bf89d38fSJeff Mahoney err = -EIO; 1027bf89d38fSJeff Mahoney return err; 102879154b1bSChris Mason } 102979154b1bSChris Mason 1030690587d1SChris Mason /* 1031c9b577c0SNikolay Borisov * When btree blocks are allocated the corresponding extents are marked dirty. 1032c9b577c0SNikolay Borisov * This function ensures such extents are persisted on disk for transaction or 1033c9b577c0SNikolay Borisov * log commit. 1034c9b577c0SNikolay Borisov * 1035c9b577c0SNikolay Borisov * @trans: transaction whose dirty pages we'd like to write 1036690587d1SChris Mason */ 103770458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans) 1038d0c803c4SChris Mason { 1039663dfbb0SFilipe Manana int ret; 1040c9b577c0SNikolay Borisov int ret2; 1041c9b577c0SNikolay Borisov struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages; 104270458a58SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1043c9b577c0SNikolay Borisov struct blk_plug plug; 1044663dfbb0SFilipe Manana 1045c9b577c0SNikolay Borisov blk_start_plug(&plug); 1046c9b577c0SNikolay Borisov ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY); 1047c9b577c0SNikolay Borisov blk_finish_plug(&plug); 1048c9b577c0SNikolay Borisov ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1049c9b577c0SNikolay Borisov 105041e7acd3SNikolay Borisov extent_io_tree_release(&trans->transaction->dirty_pages); 1051663dfbb0SFilipe Manana 1052c9b577c0SNikolay Borisov if (ret) 1053663dfbb0SFilipe Manana return ret; 1054c9b577c0SNikolay Borisov else if (ret2) 1055c9b577c0SNikolay Borisov return ret2; 1056c9b577c0SNikolay Borisov else 1057c9b577c0SNikolay Borisov return 0; 1058d0c803c4SChris Mason } 1059d0c803c4SChris Mason 1060d352ac68SChris Mason /* 1061d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1062d352ac68SChris Mason * 1063d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1064d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1065d352ac68SChris Mason * allocation tree. 1066d352ac68SChris Mason * 1067d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1068d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1069d352ac68SChris Mason */ 10700b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 107179154b1bSChris Mason struct btrfs_root *root) 107279154b1bSChris Mason { 107379154b1bSChris Mason int ret; 10740b86a832SChris Mason u64 old_root_bytenr; 107586b9f2ecSYan, Zheng u64 old_root_used; 10760b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10770b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 107879154b1bSChris Mason 107986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 108056bec294SChris Mason 108179154b1bSChris Mason while (1) { 10820b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 108386b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1084ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 108579154b1bSChris Mason break; 108687ef2bb4SChris Mason 10875d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 108879154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10890b86a832SChris Mason &root->root_key, 10900b86a832SChris Mason &root->root_item); 109149b25e05SJeff Mahoney if (ret) 109249b25e05SJeff Mahoney return ret; 109356bec294SChris Mason 109486b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1095e7070be1SJosef Bacik } 1096276e680dSYan Zheng 10970b86a832SChris Mason return 0; 10980b86a832SChris Mason } 10990b86a832SChris Mason 1100d352ac68SChris Mason /* 1101d352ac68SChris Mason * update all the cowonly tree roots on disk 110249b25e05SJeff Mahoney * 110349b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 110449b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 110549b25e05SJeff Mahoney * to clean up the delayed refs. 1106d352ac68SChris Mason */ 11079386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) 11080b86a832SChris Mason { 11099386d8bcSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1110ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11111bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11120b86a832SChris Mason struct list_head *next; 111384234f3aSYan Zheng struct extent_buffer *eb; 111456bec294SChris Mason int ret; 111584234f3aSYan Zheng 111684234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 111749b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 111849b25e05SJeff Mahoney 0, &eb); 111984234f3aSYan Zheng btrfs_tree_unlock(eb); 112084234f3aSYan Zheng free_extent_buffer(eb); 11210b86a832SChris Mason 112249b25e05SJeff Mahoney if (ret) 112349b25e05SJeff Mahoney return ret; 112449b25e05SJeff Mahoney 1125c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 112649b25e05SJeff Mahoney if (ret) 112749b25e05SJeff Mahoney return ret; 112887ef2bb4SChris Mason 1129196c9d8dSDavid Sterba ret = btrfs_run_dev_stats(trans); 1130c16ce190SJosef Bacik if (ret) 1131c16ce190SJosef Bacik return ret; 11322b584c68SDavid Sterba ret = btrfs_run_dev_replace(trans); 1133c16ce190SJosef Bacik if (ret) 1134c16ce190SJosef Bacik return ret; 1135280f8bd2SLu Fengqi ret = btrfs_run_qgroups(trans); 1136c16ce190SJosef Bacik if (ret) 1137c16ce190SJosef Bacik return ret; 1138546adb0dSJan Schmidt 1139bbebb3e0SDavid Sterba ret = btrfs_setup_space_cache(trans); 1140dcdf7f6dSJosef Bacik if (ret) 1141dcdf7f6dSJosef Bacik return ret; 1142dcdf7f6dSJosef Bacik 1143546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1144c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1145c16ce190SJosef Bacik if (ret) 1146c16ce190SJosef Bacik return ret; 1147ea526d18SJosef Bacik again: 11480b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11492ff7e61eSJeff Mahoney struct btrfs_root *root; 11500b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11510b86a832SChris Mason list_del_init(next); 11520b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1153e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 115487ef2bb4SChris Mason 11559e351cc8SJosef Bacik if (root != fs_info->extent_root) 11569e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11579e351cc8SJosef Bacik &trans->transaction->switch_commits); 115849b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 115949b25e05SJeff Mahoney if (ret) 116049b25e05SJeff Mahoney return ret; 1161c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1162ea526d18SJosef Bacik if (ret) 1163ea526d18SJosef Bacik return ret; 116479154b1bSChris Mason } 1165276e680dSYan Zheng 11661bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 11675742d15fSDavid Sterba ret = btrfs_write_dirty_block_groups(trans); 1168ea526d18SJosef Bacik if (ret) 1169ea526d18SJosef Bacik return ret; 1170c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1171ea526d18SJosef Bacik if (ret) 1172ea526d18SJosef Bacik return ret; 1173ea526d18SJosef Bacik } 1174ea526d18SJosef Bacik 1175ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1176ea526d18SJosef Bacik goto again; 1177ea526d18SJosef Bacik 11789e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11799e351cc8SJosef Bacik &trans->transaction->switch_commits); 11809f6cbcbbSDavid Sterba 11819f6cbcbbSDavid Sterba /* Update dev-replace pointer once everything is committed */ 11829f6cbcbbSDavid Sterba fs_info->dev_replace.committed_cursor_left = 11839f6cbcbbSDavid Sterba fs_info->dev_replace.cursor_left_last_write_of_item; 11848dabb742SStefan Behrens 118579154b1bSChris Mason return 0; 118679154b1bSChris Mason } 118779154b1bSChris Mason 1188d352ac68SChris Mason /* 1189d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1190d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1191d352ac68SChris Mason * be deleted 1192d352ac68SChris Mason */ 1193cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11945eda7b5eSChris Mason { 11950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11960b246afaSJeff Mahoney 11970b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1198cfad392bSJosef Bacik if (list_empty(&root->root_list)) 11990b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 12000b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 12015eda7b5eSChris Mason } 12025eda7b5eSChris Mason 1203d352ac68SChris Mason /* 12045d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1205d352ac68SChris Mason */ 12067e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) 12070f7d52f4SChris Mason { 12087e4443d9SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 12090f7d52f4SChris Mason struct btrfs_root *gang[8]; 12100f7d52f4SChris Mason int i; 12110f7d52f4SChris Mason int ret; 121254aa1f4dSChris Mason int err = 0; 121354aa1f4dSChris Mason 1214a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12150f7d52f4SChris Mason while (1) { 12165d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12175d4f98a2SYan Zheng (void **)gang, 0, 12180f7d52f4SChris Mason ARRAY_SIZE(gang), 12190f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12200f7d52f4SChris Mason if (ret == 0) 12210f7d52f4SChris Mason break; 12220f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12235b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12245d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12252619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12260f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1227a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 122831153d81SYan Zheng 1229e02119d5SChris Mason btrfs_free_log(trans, root); 12305d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1231e02119d5SChris Mason 123282d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 123382d5902dSLi Zefan 1234f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 123527cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1236c7548af6SChris Mason smp_mb__after_atomic(); 1237f1ebcc74SLiu Bo 1238978d910dSYan Zheng if (root->commit_root != root->node) { 12399e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12409e351cc8SJosef Bacik &trans->transaction->switch_commits); 1241978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1242978d910dSYan Zheng root->node); 1243978d910dSYan Zheng } 124431153d81SYan Zheng 12455d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12460f7d52f4SChris Mason &root->root_key, 12470f7d52f4SChris Mason &root->root_item); 1248a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 124954aa1f4dSChris Mason if (err) 125054aa1f4dSChris Mason break; 1251733e03a0SQu Wenruo btrfs_qgroup_free_meta_all_pertrans(root); 12520f7d52f4SChris Mason } 12539f3a7427SChris Mason } 1254a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 125554aa1f4dSChris Mason return err; 12560f7d52f4SChris Mason } 12570f7d52f4SChris Mason 1258d352ac68SChris Mason /* 1259de78b51aSEric Sandeen * defrag a given btree. 1260de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1261d352ac68SChris Mason */ 1262de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1263e9d0b13bSChris Mason { 1264e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1265e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12668929ecfaSYan, Zheng int ret; 1267e9d0b13bSChris Mason 126827cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1269e9d0b13bSChris Mason return 0; 12708929ecfaSYan, Zheng 12716b80053dSChris Mason while (1) { 12728929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12738929ecfaSYan, Zheng if (IS_ERR(trans)) 12748929ecfaSYan, Zheng return PTR_ERR(trans); 12758929ecfaSYan, Zheng 1276de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12778929ecfaSYan, Zheng 12783a45bb20SJeff Mahoney btrfs_end_transaction(trans); 12792ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1280e9d0b13bSChris Mason cond_resched(); 1281e9d0b13bSChris Mason 1282ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1283e9d0b13bSChris Mason break; 1284210549ebSDavid Sterba 1285ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1286ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1287210549ebSDavid Sterba ret = -EAGAIN; 1288210549ebSDavid Sterba break; 1289210549ebSDavid Sterba } 1290e9d0b13bSChris Mason } 129127cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12928929ecfaSYan, Zheng return ret; 1293e9d0b13bSChris Mason } 1294e9d0b13bSChris Mason 1295d352ac68SChris Mason /* 12966426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 12976426c7adSQu Wenruo * 12986426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 12996426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13006426c7adSQu Wenruo * qgroup works. 13016426c7adSQu Wenruo */ 13026426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13036426c7adSQu Wenruo struct btrfs_root *src, 13046426c7adSQu Wenruo struct btrfs_root *parent, 13056426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13066426c7adSQu Wenruo u64 dst_objectid) 13076426c7adSQu Wenruo { 13086426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13096426c7adSQu Wenruo int ret; 13106426c7adSQu Wenruo 13116426c7adSQu Wenruo /* 13126426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13136426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13146426c7adSQu Wenruo * kick in anyway. 13156426c7adSQu Wenruo */ 13169ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 13176426c7adSQu Wenruo return 0; 13186426c7adSQu Wenruo 13196426c7adSQu Wenruo /* 132052042d8eSAndrea Gelmini * Ensure dirty @src will be committed. Or, after coming 13214d31778aSQu Wenruo * commit_fs_roots() and switch_commit_roots(), any dirty but not 13224d31778aSQu Wenruo * recorded root will never be updated again, causing an outdated root 13234d31778aSQu Wenruo * item. 13244d31778aSQu Wenruo */ 13254d31778aSQu Wenruo record_root_in_trans(trans, src, 1); 13264d31778aSQu Wenruo 13274d31778aSQu Wenruo /* 13286426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13296426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13306426c7adSQu Wenruo */ 13316426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13326426c7adSQu Wenruo 13337e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 13346426c7adSQu Wenruo if (ret) 13356426c7adSQu Wenruo goto out; 1336460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 13376426c7adSQu Wenruo if (ret < 0) 13386426c7adSQu Wenruo goto out; 13396426c7adSQu Wenruo 13406426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 1341a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid, 13426426c7adSQu Wenruo inherit); 13436426c7adSQu Wenruo if (ret < 0) 13446426c7adSQu Wenruo goto out; 13456426c7adSQu Wenruo 13466426c7adSQu Wenruo /* 13476426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13486426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13496426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13506426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13516426c7adSQu Wenruo * 2) write all btree blocks onto disk 13526426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13536426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13546426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13556426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13566426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13576426c7adSQu Wenruo */ 13589386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 13596426c7adSQu Wenruo if (ret) 13606426c7adSQu Wenruo goto out; 136116916a88SNikolay Borisov switch_commit_roots(trans->transaction); 136270458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 13636426c7adSQu Wenruo if (ret) 1364f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13656426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13666426c7adSQu Wenruo 13676426c7adSQu Wenruo out: 13686426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 13696426c7adSQu Wenruo 13706426c7adSQu Wenruo /* 13716426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 13726426c7adSQu Wenruo * last_trans == cur_transid. 13736426c7adSQu Wenruo * Or it won't be committed again onto disk after later 13746426c7adSQu Wenruo * insert_dir_item() 13756426c7adSQu Wenruo */ 13766426c7adSQu Wenruo if (!ret) 13776426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 13786426c7adSQu Wenruo return ret; 13796426c7adSQu Wenruo } 13806426c7adSQu Wenruo 13816426c7adSQu Wenruo /* 1382d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1383aec8030aSMiao Xie * transaction commit. This does the actual creation. 1384aec8030aSMiao Xie * 1385aec8030aSMiao Xie * Note: 1386aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1387aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1388aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1389d352ac68SChris Mason */ 139080b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13913063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13923063d29fSChris Mason { 139308d50ca3SNikolay Borisov 139408d50ca3SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 13953063d29fSChris Mason struct btrfs_key key; 139680b6794dSChris Mason struct btrfs_root_item *new_root_item; 13973063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13983063d29fSChris Mason struct btrfs_root *root = pending->root; 13996bdb72deSSage Weil struct btrfs_root *parent_root; 140098c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14016bdb72deSSage Weil struct inode *parent_inode; 140242874b3dSMiao Xie struct btrfs_path *path; 140342874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1404a22285a6SYan, Zheng struct dentry *dentry; 14053063d29fSChris Mason struct extent_buffer *tmp; 1406925baeddSChris Mason struct extent_buffer *old; 140795582b00SDeepa Dinamani struct timespec64 cur_time; 1408aec8030aSMiao Xie int ret = 0; 1409d68fc57bSYan, Zheng u64 to_reserve = 0; 14106bdb72deSSage Weil u64 index = 0; 1411a22285a6SYan, Zheng u64 objectid; 1412b83cc969SLi Zefan u64 root_flags; 14138ea05e3aSAlexander Block uuid_le new_uuid; 14143063d29fSChris Mason 14158546b570SDavid Sterba ASSERT(pending->path); 14168546b570SDavid Sterba path = pending->path; 141742874b3dSMiao Xie 1418b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1419b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1420a22285a6SYan, Zheng 1421aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1422aec8030aSMiao Xie if (pending->error) 14236fa9700eSMiao Xie goto no_free_objectid; 14243063d29fSChris Mason 1425d6726335SQu Wenruo /* 1426d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1427d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1428d6726335SQu Wenruo */ 1429d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1430d6726335SQu Wenruo 1431147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1432d68fc57bSYan, Zheng 1433d68fc57bSYan, Zheng if (to_reserve > 0) { 1434aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1435aec8030aSMiao Xie &pending->block_rsv, 143608e007d2SMiao Xie to_reserve, 143708e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1438aec8030aSMiao Xie if (pending->error) 1439d6726335SQu Wenruo goto clear_skip_qgroup; 1440d68fc57bSYan, Zheng } 1441d68fc57bSYan, Zheng 14423063d29fSChris Mason key.objectid = objectid; 1443a22285a6SYan, Zheng key.offset = (u64)-1; 1444a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14453063d29fSChris Mason 14466fa9700eSMiao Xie rsv = trans->block_rsv; 1447a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14482382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14490b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 145088d3a5aaSJosef Bacik trans->transid, 145188d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1452a22285a6SYan, Zheng dentry = pending->dentry; 1453e9662f70SMiao Xie parent_inode = pending->dir; 1454a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14556426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1456a22285a6SYan, Zheng 1457c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 145804b285f3SDeepa Dinamani 14596bdb72deSSage Weil /* 14606bdb72deSSage Weil * insert the directory item 14616bdb72deSSage Weil */ 1462877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 146349b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 146442874b3dSMiao Xie 146542874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 146642874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 14674a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 146842874b3dSMiao Xie dentry->d_name.name, 146942874b3dSMiao Xie dentry->d_name.len, 0); 147042874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1471fe66a05aSChris Mason pending->error = -EEXIST; 1472aec8030aSMiao Xie goto dir_item_existed; 147342874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 147442874b3dSMiao Xie ret = PTR_ERR(dir_item); 147566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14768732d44fSMiao Xie goto fail; 147779787eaaSJeff Mahoney } 147842874b3dSMiao Xie btrfs_release_path(path); 14796bdb72deSSage Weil 1480e999376fSChris Mason /* 1481e999376fSChris Mason * pull in the delayed directory update 1482e999376fSChris Mason * and the delayed inode item 1483e999376fSChris Mason * otherwise we corrupt the FS during 1484e999376fSChris Mason * snapshot 1485e999376fSChris Mason */ 1486e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 14878732d44fSMiao Xie if (ret) { /* Transaction aborted */ 148866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14898732d44fSMiao Xie goto fail; 14908732d44fSMiao Xie } 1491e999376fSChris Mason 14926426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 14936bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14946bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 149508fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14966bdb72deSSage Weil 1497b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1498b83cc969SLi Zefan if (pending->readonly) 1499b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1500b83cc969SLi Zefan else 1501b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1502b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1503b83cc969SLi Zefan 15048ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15058ea05e3aSAlexander Block trans->transid); 15068ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15078ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15088ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15098ea05e3aSAlexander Block BTRFS_UUID_SIZE); 151070023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 151170023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 151270023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15138ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15148ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15158ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15168ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 151770023da2SStefan Behrens } 15183cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15193cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 152070023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15218ea05e3aSAlexander Block 1522925baeddSChris Mason old = btrfs_lock_root_node(root); 152349b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 152479787eaaSJeff Mahoney if (ret) { 152579787eaaSJeff Mahoney btrfs_tree_unlock(old); 152679787eaaSJeff Mahoney free_extent_buffer(old); 152766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15288732d44fSMiao Xie goto fail; 152979787eaaSJeff Mahoney } 153049b25e05SJeff Mahoney 15318bead258SDavid Sterba btrfs_set_lock_blocking_write(old); 15323063d29fSChris Mason 153349b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 153479787eaaSJeff Mahoney /* clean up in any case */ 1535925baeddSChris Mason btrfs_tree_unlock(old); 1536925baeddSChris Mason free_extent_buffer(old); 15378732d44fSMiao Xie if (ret) { 153866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15398732d44fSMiao Xie goto fail; 15408732d44fSMiao Xie } 1541f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 154227cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1543f1ebcc74SLiu Bo smp_wmb(); 1544f1ebcc74SLiu Bo 15455d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1546a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1547a22285a6SYan, Zheng key.offset = trans->transid; 1548a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1549925baeddSChris Mason btrfs_tree_unlock(tmp); 15503063d29fSChris Mason free_extent_buffer(tmp); 15518732d44fSMiao Xie if (ret) { 155266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15538732d44fSMiao Xie goto fail; 15548732d44fSMiao Xie } 15550660b5afSChris Mason 1556a22285a6SYan, Zheng /* 1557a22285a6SYan, Zheng * insert root back/forward references 1558a22285a6SYan, Zheng */ 15596025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, 1560a22285a6SYan, Zheng parent_root->root_key.objectid, 15614a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1562a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15638732d44fSMiao Xie if (ret) { 156466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15658732d44fSMiao Xie goto fail; 15668732d44fSMiao Xie } 1567a22285a6SYan, Zheng 1568a22285a6SYan, Zheng key.offset = (u64)-1; 15690b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 157079787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 157179787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 157266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15738732d44fSMiao Xie goto fail; 157479787eaaSJeff Mahoney } 1575d68fc57bSYan, Zheng 157649b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15778732d44fSMiao Xie if (ret) { 157866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15798732d44fSMiao Xie goto fail; 15808732d44fSMiao Xie } 1581361048f5SMiao Xie 1582c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 15838732d44fSMiao Xie if (ret) { 158466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15858732d44fSMiao Xie goto fail; 15868732d44fSMiao Xie } 158742874b3dSMiao Xie 15886426c7adSQu Wenruo /* 15896426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 15906426c7adSQu Wenruo * snapshot hack to do fast snapshot. 15916426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 15926426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 15936426c7adSQu Wenruo */ 15946426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 15956426c7adSQu Wenruo pending->inherit, objectid); 15966426c7adSQu Wenruo if (ret < 0) 15976426c7adSQu Wenruo goto fail; 15986426c7adSQu Wenruo 1599684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, dentry->d_name.name, 1600684572dfSLu Fengqi dentry->d_name.len, BTRFS_I(parent_inode), 1601684572dfSLu Fengqi &key, BTRFS_FT_DIR, index); 160242874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16039c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16048732d44fSMiao Xie if (ret) { 160566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16068732d44fSMiao Xie goto fail; 16078732d44fSMiao Xie } 160842874b3dSMiao Xie 16096ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 161042874b3dSMiao Xie dentry->d_name.len * 2); 161104b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1612c2050a45SDeepa Dinamani current_time(parent_inode); 1613be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1614dd5f9615SStefan Behrens if (ret) { 161566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1616dd5f9615SStefan Behrens goto fail; 1617dd5f9615SStefan Behrens } 1618cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, 1619cdb345a8SLu Fengqi objectid); 1620dd5f9615SStefan Behrens if (ret) { 162166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1622dd5f9615SStefan Behrens goto fail; 1623dd5f9615SStefan Behrens } 1624dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1625cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid, 1626dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1627dd5f9615SStefan Behrens objectid); 1628dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 162966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1630dd5f9615SStefan Behrens goto fail; 1631dd5f9615SStefan Behrens } 1632dd5f9615SStefan Behrens } 1633d6726335SQu Wenruo 1634c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1635d6726335SQu Wenruo if (ret) { 163666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1637d6726335SQu Wenruo goto fail; 1638d6726335SQu Wenruo } 1639d6726335SQu Wenruo 16403063d29fSChris Mason fail: 1641aec8030aSMiao Xie pending->error = ret; 1642aec8030aSMiao Xie dir_item_existed: 164398c9942aSLiu Bo trans->block_rsv = rsv; 16442382c5ccSLiu Bo trans->bytes_reserved = 0; 1645d6726335SQu Wenruo clear_skip_qgroup: 1646d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16476fa9700eSMiao Xie no_free_objectid: 16486fa9700eSMiao Xie kfree(new_root_item); 1649b0c0ea63SDavid Sterba pending->root_item = NULL; 165042874b3dSMiao Xie btrfs_free_path(path); 16518546b570SDavid Sterba pending->path = NULL; 16528546b570SDavid Sterba 165349b25e05SJeff Mahoney return ret; 16543063d29fSChris Mason } 16553063d29fSChris Mason 1656d352ac68SChris Mason /* 1657d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1658d352ac68SChris Mason */ 165908d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans) 16603063d29fSChris Mason { 1661aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16623063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1663aec8030aSMiao Xie int ret = 0; 16643de4586cSChris Mason 1665aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1666aec8030aSMiao Xie list_del(&pending->list); 166708d50ca3SNikolay Borisov ret = create_pending_snapshot(trans, pending); 1668aec8030aSMiao Xie if (ret) 1669aec8030aSMiao Xie break; 1670aec8030aSMiao Xie } 1671aec8030aSMiao Xie return ret; 16723de4586cSChris Mason } 16733de4586cSChris Mason 16742ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 16755d4f98a2SYan Zheng { 16765d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16775d4f98a2SYan Zheng struct btrfs_super_block *super; 16785d4f98a2SYan Zheng 16790b246afaSJeff Mahoney super = fs_info->super_copy; 16805d4f98a2SYan Zheng 16810b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 1682093e037cSDavid Sterba super->chunk_root = root_item->bytenr; 1683093e037cSDavid Sterba super->chunk_root_generation = root_item->generation; 1684093e037cSDavid Sterba super->chunk_root_level = root_item->level; 16855d4f98a2SYan Zheng 16860b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 1687093e037cSDavid Sterba super->root = root_item->bytenr; 1688093e037cSDavid Sterba super->generation = root_item->generation; 1689093e037cSDavid Sterba super->root_level = root_item->level; 16900b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 1691093e037cSDavid Sterba super->cache_generation = root_item->generation; 16920b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 1693093e037cSDavid Sterba super->uuid_tree_generation = root_item->generation; 16945d4f98a2SYan Zheng } 16955d4f98a2SYan Zheng 1696f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1697f36f3042SChris Mason { 16984a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1699f36f3042SChris Mason int ret = 0; 17004a9d8bdeSMiao Xie 1701a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17024a9d8bdeSMiao Xie trans = info->running_transaction; 17034a9d8bdeSMiao Xie if (trans) 17044a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1705a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1706f36f3042SChris Mason return ret; 1707f36f3042SChris Mason } 1708f36f3042SChris Mason 17098929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17108929ecfaSYan, Zheng { 17114a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17128929ecfaSYan, Zheng int ret = 0; 17134a9d8bdeSMiao Xie 1714a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17154a9d8bdeSMiao Xie trans = info->running_transaction; 17164a9d8bdeSMiao Xie if (trans) 17174a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1718a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17198929ecfaSYan, Zheng return ret; 17208929ecfaSYan, Zheng } 17218929ecfaSYan, Zheng 1722bb9c12c9SSage Weil /* 1723bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1724bb9c12c9SSage Weil * transaction joins 1725bb9c12c9SSage Weil */ 17262ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1727bb9c12c9SSage Weil struct btrfs_transaction *trans) 1728bb9c12c9SSage Weil { 17292ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 17302ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1731bb9c12c9SSage Weil } 1732bb9c12c9SSage Weil 1733bb9c12c9SSage Weil /* 1734bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1735bb9c12c9SSage Weil * caller holds ref. 1736bb9c12c9SSage Weil */ 17372ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17382ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1739bb9c12c9SSage Weil struct btrfs_transaction *trans) 1740bb9c12c9SSage Weil { 17412ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17422ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1743bb9c12c9SSage Weil } 1744bb9c12c9SSage Weil 1745bb9c12c9SSage Weil /* 1746bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1747bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1748bb9c12c9SSage Weil */ 1749bb9c12c9SSage Weil struct btrfs_async_commit { 1750bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17517892b5afSMiao Xie struct work_struct work; 1752bb9c12c9SSage Weil }; 1753bb9c12c9SSage Weil 1754bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1755bb9c12c9SSage Weil { 1756bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17577892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1758bb9c12c9SSage Weil 17596fc4e354SSage Weil /* 17606fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17616fc4e354SSage Weil * Tell lockdep about it. 17626fc4e354SSage Weil */ 1763b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17643a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 17656fc4e354SSage Weil 1766e209db7aSSage Weil current->journal_info = ac->newtrans; 1767e209db7aSSage Weil 17683a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1769bb9c12c9SSage Weil kfree(ac); 1770bb9c12c9SSage Weil } 1771bb9c12c9SSage Weil 1772bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1773bb9c12c9SSage Weil int wait_for_unblock) 1774bb9c12c9SSage Weil { 17753a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1776bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1777bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1778bb9c12c9SSage Weil 1779bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1780db5b493aSTsutomu Itoh if (!ac) 1781db5b493aSTsutomu Itoh return -ENOMEM; 1782bb9c12c9SSage Weil 17837892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 17843a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 17853612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17863612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17873612b495STsutomu Itoh kfree(ac); 17883612b495STsutomu Itoh return err; 17893612b495STsutomu Itoh } 1790bb9c12c9SSage Weil 1791bb9c12c9SSage Weil /* take transaction reference */ 1792bb9c12c9SSage Weil cur_trans = trans->transaction; 17939b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1794bb9c12c9SSage Weil 17953a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17966fc4e354SSage Weil 17976fc4e354SSage Weil /* 17986fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 17996fc4e354SSage Weil * async commit thread will be the one to unlock it. 18006fc4e354SSage Weil */ 1801b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18020b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 18036fc4e354SSage Weil 18047892b5afSMiao Xie schedule_work(&ac->work); 1805bb9c12c9SSage Weil 1806bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1807bb9c12c9SSage Weil if (wait_for_unblock) 18082ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1809bb9c12c9SSage Weil else 18102ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1811bb9c12c9SSage Weil 181238e88054SSage Weil if (current->journal_info == trans) 181338e88054SSage Weil current->journal_info = NULL; 181438e88054SSage Weil 1815724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1816bb9c12c9SSage Weil return 0; 1817bb9c12c9SSage Weil } 1818bb9c12c9SSage Weil 181949b25e05SJeff Mahoney 182097cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) 182149b25e05SJeff Mahoney { 182297cb39bbSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 182349b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 182449b25e05SJeff Mahoney 1825b50fff81SDavid Sterba WARN_ON(refcount_read(&trans->use_count) > 1); 182649b25e05SJeff Mahoney 182766642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18287b8b92afSJosef Bacik 18290b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 183066b6135bSLiu Bo 183125d8c284SMiao Xie /* 183225d8c284SMiao Xie * If the transaction is removed from the list, it means this 183325d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 183425d8c284SMiao Xie * to call the cleanup function. 183525d8c284SMiao Xie */ 183625d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 183766b6135bSLiu Bo 183849b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18390b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18404a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18410b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1842f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1843f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1844f094ac32SLiu Bo 18450b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1846d7096fc3SJosef Bacik } 18470b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 184849b25e05SJeff Mahoney 18492ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 185049b25e05SJeff Mahoney 18510b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18520b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18530b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18554a9d8bdeSMiao Xie 1856e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18570b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1858724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1859724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 186049b25e05SJeff Mahoney 186197cb39bbSNikolay Borisov trace_btrfs_transaction_commit(trans->root); 186249b25e05SJeff Mahoney 186349b25e05SJeff Mahoney if (current->journal_info == trans) 186449b25e05SJeff Mahoney current->journal_info = NULL; 18650b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 186649b25e05SJeff Mahoney 186749b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 186849b25e05SJeff Mahoney } 186949b25e05SJeff Mahoney 1870c7cc64a9SDavid Sterba /* 1871c7cc64a9SDavid Sterba * Release reserved delayed ref space of all pending block groups of the 1872c7cc64a9SDavid Sterba * transaction and remove them from the list 1873c7cc64a9SDavid Sterba */ 1874c7cc64a9SDavid Sterba static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) 1875c7cc64a9SDavid Sterba { 1876c7cc64a9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 1877c7cc64a9SDavid Sterba struct btrfs_block_group_cache *block_group, *tmp; 1878c7cc64a9SDavid Sterba 1879c7cc64a9SDavid Sterba list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { 1880c7cc64a9SDavid Sterba btrfs_delayed_refs_rsv_release(fs_info, 1); 1881c7cc64a9SDavid Sterba list_del_init(&block_group->bg_list); 1882c7cc64a9SDavid Sterba } 1883c7cc64a9SDavid Sterba } 1884c7cc64a9SDavid Sterba 1885609e804dSFilipe Manana static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) 188682436617SMiao Xie { 1887609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1888609e804dSFilipe Manana 1889ce8ea7ccSJosef Bacik /* 1890ce8ea7ccSJosef Bacik * We use writeback_inodes_sb here because if we used 1891ce8ea7ccSJosef Bacik * btrfs_start_delalloc_roots we would deadlock with fs freeze. 1892ce8ea7ccSJosef Bacik * Currently are holding the fs freeze lock, if we do an async flush 1893ce8ea7ccSJosef Bacik * we'll do btrfs_join_transaction() and deadlock because we need to 1894ce8ea7ccSJosef Bacik * wait for the fs freeze lock. Using the direct flushing we benefit 1895ce8ea7ccSJosef Bacik * from already being in a transaction and our join_transaction doesn't 1896ce8ea7ccSJosef Bacik * have to re-take the fs freeze lock. 1897ce8ea7ccSJosef Bacik */ 1898609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 1899ce8ea7ccSJosef Bacik writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); 1900609e804dSFilipe Manana } else { 1901609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1902609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1903609e804dSFilipe Manana 1904609e804dSFilipe Manana /* 1905609e804dSFilipe Manana * Flush dellaloc for any root that is going to be snapshotted. 1906609e804dSFilipe Manana * This is done to avoid a corrupted version of files, in the 1907609e804dSFilipe Manana * snapshots, that had both buffered and direct IO writes (even 1908609e804dSFilipe Manana * if they were done sequentially) due to an unordered update of 1909609e804dSFilipe Manana * the inode's size on disk. 1910609e804dSFilipe Manana */ 1911609e804dSFilipe Manana list_for_each_entry(pending, head, list) { 1912609e804dSFilipe Manana int ret; 1913609e804dSFilipe Manana 1914609e804dSFilipe Manana ret = btrfs_start_delalloc_snapshot(pending->root); 1915609e804dSFilipe Manana if (ret) 1916609e804dSFilipe Manana return ret; 1917609e804dSFilipe Manana } 1918609e804dSFilipe Manana } 191982436617SMiao Xie return 0; 192082436617SMiao Xie } 192182436617SMiao Xie 1922609e804dSFilipe Manana static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) 192382436617SMiao Xie { 1924609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1925609e804dSFilipe Manana 1926609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 19276374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1928609e804dSFilipe Manana } else { 1929609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1930609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1931609e804dSFilipe Manana 1932609e804dSFilipe Manana /* 1933609e804dSFilipe Manana * Wait for any dellaloc that we started previously for the roots 1934609e804dSFilipe Manana * that are going to be snapshotted. This is to avoid a corrupted 1935609e804dSFilipe Manana * version of files in the snapshots that had both buffered and 1936609e804dSFilipe Manana * direct IO writes (even if they were done sequentially). 1937609e804dSFilipe Manana */ 1938609e804dSFilipe Manana list_for_each_entry(pending, head, list) 1939609e804dSFilipe Manana btrfs_wait_ordered_extents(pending->root, 1940609e804dSFilipe Manana U64_MAX, 0, U64_MAX); 1941609e804dSFilipe Manana } 194282436617SMiao Xie } 194382436617SMiao Xie 19443a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 194579154b1bSChris Mason { 19463a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 194749b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19488fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 194925287e0aSMiao Xie int ret; 195079154b1bSChris Mason 19518d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 195220c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 195325287e0aSMiao Xie ret = cur_trans->aborted; 19543a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1955e4a2bcacSJosef Bacik return ret; 195625287e0aSMiao Xie } 195749b25e05SJeff Mahoney 1958f45c752bSJosef Bacik btrfs_trans_release_metadata(trans); 1959f45c752bSJosef Bacik trans->block_rsv = NULL; 1960f45c752bSJosef Bacik 196156bec294SChris Mason /* make a pass through all the delayed refs we have so far 196256bec294SChris Mason * any runnings procs may add more while we are here 196356bec294SChris Mason */ 1964c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1965e4a2bcacSJosef Bacik if (ret) { 19663a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1967e4a2bcacSJosef Bacik return ret; 1968e4a2bcacSJosef Bacik } 196956bec294SChris Mason 1970b7ec40d7SChris Mason cur_trans = trans->transaction; 197149b25e05SJeff Mahoney 197256bec294SChris Mason /* 197356bec294SChris Mason * set the flushing flag so procs in this transaction have to 197456bec294SChris Mason * start sending their work down. 197556bec294SChris Mason */ 1976b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19771be41b78SJosef Bacik smp_wmb(); 197856bec294SChris Mason 19796c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 1980ea658badSJosef Bacik 1981c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1982e4a2bcacSJosef Bacik if (ret) { 19833a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1984e4a2bcacSJosef Bacik return ret; 1985e4a2bcacSJosef Bacik } 198656bec294SChris Mason 19873204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19881bbc621eSChris Mason int run_it = 0; 19891bbc621eSChris Mason 19901bbc621eSChris Mason /* this mutex is also taken before trying to set 19911bbc621eSChris Mason * block groups readonly. We need to make sure 19921bbc621eSChris Mason * that nobody has set a block group readonly 19931bbc621eSChris Mason * after a extents from that block group have been 19941bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19951bbc621eSChris Mason * will wait for the transaction to commit if it 19963204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 19971bbc621eSChris Mason * 19983204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 19993204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 20001bbc621eSChris Mason * hurt to have more than one go through, but there's no 20011bbc621eSChris Mason * real advantage to it either. 20021bbc621eSChris Mason */ 20030b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 20043204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 20053204d33cSJosef Bacik &cur_trans->flags)) 20061bbc621eSChris Mason run_it = 1; 20070b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 20081bbc621eSChris Mason 2009f9cacae3SNikolay Borisov if (run_it) { 201021217054SNikolay Borisov ret = btrfs_start_dirty_block_groups(trans); 20111bbc621eSChris Mason if (ret) { 20123a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20131bbc621eSChris Mason return ret; 20141bbc621eSChris Mason } 2015f9cacae3SNikolay Borisov } 2016f9cacae3SNikolay Borisov } 20171bbc621eSChris Mason 20180b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20194a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20200b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 20219b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 20223a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 2023ccd467d6SChris Mason 20242ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 202515ee9bc7SJosef Bacik 2026b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2027b4924a0fSLiu Bo ret = cur_trans->aborted; 2028b4924a0fSLiu Bo 2029724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 203015ee9bc7SJosef Bacik 203149b25e05SJeff Mahoney return ret; 203279154b1bSChris Mason } 20334313b399SChris Mason 20344a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20350b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2036bb9c12c9SSage Weil 20370b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2038ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2039ccd467d6SChris Mason struct btrfs_transaction, list); 20404a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20419b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20420b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2043ccd467d6SChris Mason 20442ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20451f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2046ccd467d6SChris Mason 2047724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20481f9b8c8fSFilipe Manana if (ret) 20491f9b8c8fSFilipe Manana goto cleanup_transaction; 2050a4abeea4SJosef Bacik } else { 20510b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2052ccd467d6SChris Mason } 2053a4abeea4SJosef Bacik } else { 20540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2055cb2d3dadSFilipe Manana /* 2056cb2d3dadSFilipe Manana * The previous transaction was aborted and was already removed 2057cb2d3dadSFilipe Manana * from the list of transactions at fs_info->trans_list. So we 2058cb2d3dadSFilipe Manana * abort to prevent writing a new superblock that reflects a 2059cb2d3dadSFilipe Manana * corrupt state (pointing to trees with unwritten nodes/leafs). 2060cb2d3dadSFilipe Manana */ 2061cb2d3dadSFilipe Manana if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) { 2062cb2d3dadSFilipe Manana ret = -EROFS; 2063cb2d3dadSFilipe Manana goto cleanup_transaction; 2064cb2d3dadSFilipe Manana } 2065ccd467d6SChris Mason } 206615ee9bc7SJosef Bacik 20670860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20680860adfdSMiao Xie 2069609e804dSFilipe Manana ret = btrfs_start_delalloc_flush(trans); 207082436617SMiao Xie if (ret) 207182436617SMiao Xie goto cleanup_transaction; 207282436617SMiao Xie 2073e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 207449b25e05SJeff Mahoney if (ret) 207549b25e05SJeff Mahoney goto cleanup_transaction; 207616cdcec7SMiao Xie 2077581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2078581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2079ed3b3d31SChris Mason 2080581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 2081e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 2082ca469637SMiao Xie if (ret) 2083ca469637SMiao Xie goto cleanup_transaction; 2084ca469637SMiao Xie 2085609e804dSFilipe Manana btrfs_wait_delalloc_flush(trans); 2086cb7ab021SWang Shilong 20872ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2088ed0ca140SJosef Bacik /* 2089ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2090ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20914a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2092ed0ca140SJosef Bacik */ 20930b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20944a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2096ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2097ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 209815ee9bc7SJosef Bacik 20992cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 210020c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 21012cba30f1SMiao Xie ret = cur_trans->aborted; 21026cf7f77eSWang Shilong goto scrub_continue; 21032cba30f1SMiao Xie } 21047585717fSChris Mason /* 21057585717fSChris Mason * the reloc mutex makes sure that we stop 21067585717fSChris Mason * the balancing code from coming in and moving 21077585717fSChris Mason * extents around in the middle of the commit 21087585717fSChris Mason */ 21090b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 21107585717fSChris Mason 211142874b3dSMiao Xie /* 211242874b3dSMiao Xie * We needn't worry about the delayed items because we will 211342874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 211442874b3dSMiao Xie * core function of the snapshot creation. 211542874b3dSMiao Xie */ 211608d50ca3SNikolay Borisov ret = create_pending_snapshots(trans); 211749b25e05SJeff Mahoney if (ret) { 21180b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21196cf7f77eSWang Shilong goto scrub_continue; 212049b25e05SJeff Mahoney } 21213063d29fSChris Mason 212242874b3dSMiao Xie /* 212342874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 212442874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 212542874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 212642874b3dSMiao Xie * them. 212742874b3dSMiao Xie * 212842874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 212942874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 213042874b3dSMiao Xie * the nodes and leaves. 213142874b3dSMiao Xie */ 2132e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 213349b25e05SJeff Mahoney if (ret) { 21340b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21356cf7f77eSWang Shilong goto scrub_continue; 213649b25e05SJeff Mahoney } 213716cdcec7SMiao Xie 2138c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 213949b25e05SJeff Mahoney if (ret) { 21400b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21416cf7f77eSWang Shilong goto scrub_continue; 214249b25e05SJeff Mahoney } 214356bec294SChris Mason 2144e999376fSChris Mason /* 2145e999376fSChris Mason * make sure none of the code above managed to slip in a 2146e999376fSChris Mason * delayed item 2147e999376fSChris Mason */ 2148ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2149e999376fSChris Mason 21502c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2151dc17ff8fSChris Mason 2152e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2153e02119d5SChris Mason * various roots consistent with each other. Every pointer 2154e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2155e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2156e02119d5SChris Mason * the tree logging code from jumping in and changing any 2157e02119d5SChris Mason * of the trees. 2158e02119d5SChris Mason * 2159e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2160e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2161e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2162e02119d5SChris Mason * from now until after the super is written, we avoid races 2163e02119d5SChris Mason * with the tree-log code. 2164e02119d5SChris Mason */ 21650b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21661a40e23bSZheng Yan 21677e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 216849b25e05SJeff Mahoney if (ret) { 21690b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21700b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21716cf7f77eSWang Shilong goto scrub_continue; 217249b25e05SJeff Mahoney } 217354aa1f4dSChris Mason 21743818aea2SQu Wenruo /* 21757e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21767e1876acSDavid Sterba * before the next transaction. 21773818aea2SQu Wenruo */ 21780b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21793818aea2SQu Wenruo 21805d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2181e02119d5SChris Mason * safe to free the root of tree log roots 2182e02119d5SChris Mason */ 21830b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2184e02119d5SChris Mason 21850ed4792aSQu Wenruo /* 218682bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 218782bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 218882bafb38SQu Wenruo */ 2189c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 219082bafb38SQu Wenruo if (ret) { 219182bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 219282bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 219382bafb38SQu Wenruo goto scrub_continue; 219482bafb38SQu Wenruo } 219582bafb38SQu Wenruo 219682bafb38SQu Wenruo /* 21970ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 21980ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 21990ed4792aSQu Wenruo */ 2200460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 22010ed4792aSQu Wenruo if (ret < 0) { 22020b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22030b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22040ed4792aSQu Wenruo goto scrub_continue; 22050ed4792aSQu Wenruo } 22060ed4792aSQu Wenruo 22079386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 220849b25e05SJeff Mahoney if (ret) { 22090b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22100b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22116cf7f77eSWang Shilong goto scrub_continue; 221249b25e05SJeff Mahoney } 221354aa1f4dSChris Mason 22142cba30f1SMiao Xie /* 22152cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 22162cba30f1SMiao Xie * update ->aborted, check it. 22172cba30f1SMiao Xie */ 221820c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 22192cba30f1SMiao Xie ret = cur_trans->aborted; 22200b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22210b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22226cf7f77eSWang Shilong goto scrub_continue; 22232cba30f1SMiao Xie } 22242cba30f1SMiao Xie 22258b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 222611833d66SYan Zheng 22270b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22285f39d397SChris Mason 22290b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22300b246afaSJeff Mahoney fs_info->tree_root->node); 22310b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22329e351cc8SJosef Bacik &cur_trans->switch_commits); 22335d4f98a2SYan Zheng 22340b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22350b246afaSJeff Mahoney fs_info->chunk_root->node); 22360b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22379e351cc8SJosef Bacik &cur_trans->switch_commits); 22389e351cc8SJosef Bacik 223916916a88SNikolay Borisov switch_commit_roots(cur_trans); 22405d4f98a2SYan Zheng 2241ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22421bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22432ff7e61eSJeff Mahoney update_super_roots(fs_info); 2244e02119d5SChris Mason 22450b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22460b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22470b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22480b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2249ccd467d6SChris Mason 2250bbbf7243SNikolay Borisov btrfs_commit_device_sizes(cur_trans); 2251935e5cc9SMiao Xie 22520b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22530b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2254656f30dbSFilipe Manana 22554fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22564fbcdf66SFilipe Manana 22570b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22584a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22590b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22600b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22610b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2262b7ec40d7SChris Mason 22630b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2264e6dcd2dcSChris Mason 226570458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 226649b25e05SJeff Mahoney if (ret) { 22670b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 226808748810SDavid Sterba "Error while writing out transaction"); 22690b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22706cf7f77eSWang Shilong goto scrub_continue; 227149b25e05SJeff Mahoney } 227249b25e05SJeff Mahoney 2273eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 2274e02119d5SChris Mason /* 2275e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2276e02119d5SChris Mason * to go about their business 2277e02119d5SChris Mason */ 22780b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2279c1f32b7cSAnand Jain if (ret) 2280c1f32b7cSAnand Jain goto scrub_continue; 2281e02119d5SChris Mason 22825ead2dd0SNikolay Borisov btrfs_finish_extent_commit(trans); 22834313b399SChris Mason 22843204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22850b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 228613212b54SZhao Lei 22870b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22884a9d8bdeSMiao Xie /* 22894a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22904a9d8bdeSMiao Xie * which can change it. 22914a9d8bdeSMiao Xie */ 22924a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22932c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 2294a514d638SQu Wenruo clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); 22953de4586cSChris Mason 22960b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 229713c5a93eSJosef Bacik list_del_init(&cur_trans->list); 22980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2299a4abeea4SJosef Bacik 2300724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2301724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 230258176a96SJosef Bacik 23030860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 23040b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2305b2b5ef5cSJan Kara 23063a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 23071abe9b8aSliubo 23082ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2309a2de733cSArne Jansen 23109ed74f2dSJosef Bacik if (current->journal_info == trans) 23119ed74f2dSJosef Bacik current->journal_info = NULL; 23129ed74f2dSJosef Bacik 23132c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 231424bbcf04SYan, Zheng 231579154b1bSChris Mason return ret; 231649b25e05SJeff Mahoney 23176cf7f77eSWang Shilong scrub_continue: 23182ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 231949b25e05SJeff Mahoney cleanup_transaction: 2320dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 2321c7cc64a9SDavid Sterba btrfs_cleanup_pending_block_groups(trans); 23224fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 23230e721106SJosef Bacik trans->block_rsv = NULL; 23240b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 232549b25e05SJeff Mahoney if (current->journal_info == trans) 232649b25e05SJeff Mahoney current->journal_info = NULL; 232797cb39bbSNikolay Borisov cleanup_transaction(trans, ret); 232849b25e05SJeff Mahoney 232949b25e05SJeff Mahoney return ret; 233079154b1bSChris Mason } 233179154b1bSChris Mason 2332d352ac68SChris Mason /* 23339d1a2a3aSDavid Sterba * return < 0 if error 23349d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23359d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23369d1a2a3aSDavid Sterba * 23379d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23389d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23399d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23409d1a2a3aSDavid Sterba * few seconds later. 2341d352ac68SChris Mason */ 23429d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2343e9d0b13bSChris Mason { 23449d1a2a3aSDavid Sterba int ret; 23455d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2346e9d0b13bSChris Mason 2347a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23489d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23499d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23509d1a2a3aSDavid Sterba return 0; 23519d1a2a3aSDavid Sterba } 23529d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23539d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2354cfad392bSJosef Bacik list_del_init(&root->root_list); 2355a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23565d4f98a2SYan Zheng 23574fd786e6SMisono Tomohiro btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); 235876dda93cSYan, Zheng 235916cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 236016cdcec7SMiao Xie 236176dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 236276dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23632c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 236476dda93cSYan, Zheng else 23652c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 236632471dc2SDavid Sterba 23676596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2368e9d0b13bSChris Mason } 2369572d9ab7SDavid Sterba 2370572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2371572d9ab7SDavid Sterba { 2372572d9ab7SDavid Sterba unsigned long prev; 2373572d9ab7SDavid Sterba unsigned long bit; 2374572d9ab7SDavid Sterba 23756c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2376572d9ab7SDavid Sterba if (!prev) 2377572d9ab7SDavid Sterba return; 2378572d9ab7SDavid Sterba 23797e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23807e1876acSDavid Sterba if (prev & bit) 23817e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23827e1876acSDavid Sterba prev &= ~bit; 23837e1876acSDavid Sterba 23847e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23857e1876acSDavid Sterba if (prev & bit) 23867e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23877e1876acSDavid Sterba prev &= ~bit; 23887e1876acSDavid Sterba 2389d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2390d51033d0SDavid Sterba if (prev & bit) 2391d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2392d51033d0SDavid Sterba prev &= ~bit; 2393d51033d0SDavid Sterba 2394572d9ab7SDavid Sterba if (prev) 2395572d9ab7SDavid Sterba btrfs_warn(fs_info, 2396572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2397572d9ab7SDavid Sterba } 2398