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> 13602cbe91SDavid Sterba #include "misc.h" 1479154b1bSChris Mason #include "ctree.h" 1579154b1bSChris Mason #include "disk-io.h" 1679154b1bSChris Mason #include "transaction.h" 17925baeddSChris Mason #include "locking.h" 18e02119d5SChris Mason #include "tree-log.h" 19581bb050SLi Zefan #include "inode-map.h" 20733f4fbbSStefan Behrens #include "volumes.h" 218dabb742SStefan Behrens #include "dev-replace.h" 22fcebe456SJosef Bacik #include "qgroup.h" 23aac0023cSJosef Bacik #include "block-group.h" 2479154b1bSChris Mason 250f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 260f7d52f4SChris Mason 27e8c9f186SDavid Sterba static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = { 284a9d8bdeSMiao Xie [TRANS_STATE_RUNNING] = 0U, 29bcf3a3e7SNikolay Borisov [TRANS_STATE_BLOCKED] = __TRANS_START, 30bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH), 31bcf3a3e7SNikolay Borisov [TRANS_STATE_COMMIT_DOING] = (__TRANS_START | 324a9d8bdeSMiao Xie __TRANS_ATTACH | 33a6d155d2SFilipe Manana __TRANS_JOIN | 34a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 35bcf3a3e7SNikolay Borisov [TRANS_STATE_UNBLOCKED] = (__TRANS_START | 364a9d8bdeSMiao Xie __TRANS_ATTACH | 374a9d8bdeSMiao Xie __TRANS_JOIN | 38a6d155d2SFilipe Manana __TRANS_JOIN_NOLOCK | 39a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 40bcf3a3e7SNikolay Borisov [TRANS_STATE_COMPLETED] = (__TRANS_START | 414a9d8bdeSMiao Xie __TRANS_ATTACH | 424a9d8bdeSMiao Xie __TRANS_JOIN | 43a6d155d2SFilipe Manana __TRANS_JOIN_NOLOCK | 44a6d155d2SFilipe Manana __TRANS_JOIN_NOSTART), 454a9d8bdeSMiao Xie }; 464a9d8bdeSMiao Xie 47724e2315SJosef Bacik void btrfs_put_transaction(struct btrfs_transaction *transaction) 4879154b1bSChris Mason { 499b64f57dSElena Reshetova WARN_ON(refcount_read(&transaction->use_count) == 0); 509b64f57dSElena Reshetova if (refcount_dec_and_test(&transaction->use_count)) { 51a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 525c9d028bSLiu Bo WARN_ON(!RB_EMPTY_ROOT( 535c9d028bSLiu Bo &transaction->delayed_refs.href_root.rb_root)); 541262133bSJosef Bacik if (transaction->delayed_refs.pending_csums) 55ab8d0fc4SJeff Mahoney btrfs_err(transaction->fs_info, 56ab8d0fc4SJeff Mahoney "pending csums is %llu", 571262133bSJosef Bacik transaction->delayed_refs.pending_csums); 587785a663SFilipe Manana /* 597785a663SFilipe Manana * If any block groups are found in ->deleted_bgs then it's 607785a663SFilipe Manana * because the transaction was aborted and a commit did not 617785a663SFilipe Manana * happen (things failed before writing the new superblock 627785a663SFilipe Manana * and calling btrfs_finish_extent_commit()), so we can not 637785a663SFilipe Manana * discard the physical locations of the block groups. 647785a663SFilipe Manana */ 657785a663SFilipe Manana while (!list_empty(&transaction->deleted_bgs)) { 667785a663SFilipe Manana struct btrfs_block_group_cache *cache; 677785a663SFilipe Manana 687785a663SFilipe Manana cache = list_first_entry(&transaction->deleted_bgs, 697785a663SFilipe Manana struct btrfs_block_group_cache, 707785a663SFilipe Manana bg_list); 717785a663SFilipe Manana list_del_init(&cache->bg_list); 727785a663SFilipe Manana btrfs_put_block_group_trimming(cache); 737785a663SFilipe Manana btrfs_put_block_group(cache); 747785a663SFilipe Manana } 75bbbf7243SNikolay Borisov WARN_ON(!list_empty(&transaction->dev_update_list)); 764b5faeacSDavid Sterba kfree(transaction); 7779154b1bSChris Mason } 7878fae27eSChris Mason } 7979154b1bSChris Mason 8016916a88SNikolay Borisov static noinline void switch_commit_roots(struct btrfs_transaction *trans) 81817d52f8SJosef Bacik { 8216916a88SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 839e351cc8SJosef Bacik struct btrfs_root *root, *tmp; 849e351cc8SJosef Bacik 859e351cc8SJosef Bacik down_write(&fs_info->commit_root_sem); 869e351cc8SJosef Bacik list_for_each_entry_safe(root, tmp, &trans->switch_commits, 879e351cc8SJosef Bacik dirty_list) { 889e351cc8SJosef Bacik list_del_init(&root->dirty_list); 89817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 90817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 914fd786e6SMisono Tomohiro if (is_fstree(root->root_key.objectid)) 929e351cc8SJosef Bacik btrfs_unpin_free_ino(root); 9341e7acd3SNikolay Borisov extent_io_tree_release(&root->dirty_log_pages); 94370a11b8SQu Wenruo btrfs_qgroup_clean_swapped_blocks(root); 959e351cc8SJosef Bacik } 962b9dbef2SJosef Bacik 972b9dbef2SJosef Bacik /* We can free old roots now. */ 982b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 992b9dbef2SJosef Bacik while (!list_empty(&trans->dropped_roots)) { 1002b9dbef2SJosef Bacik root = list_first_entry(&trans->dropped_roots, 1012b9dbef2SJosef Bacik struct btrfs_root, root_list); 1022b9dbef2SJosef Bacik list_del_init(&root->root_list); 1032b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1042b9dbef2SJosef Bacik btrfs_drop_and_free_fs_root(fs_info, root); 1052b9dbef2SJosef Bacik spin_lock(&trans->dropped_roots_lock); 1062b9dbef2SJosef Bacik } 1072b9dbef2SJosef Bacik spin_unlock(&trans->dropped_roots_lock); 1089e351cc8SJosef Bacik up_write(&fs_info->commit_root_sem); 109817d52f8SJosef Bacik } 110817d52f8SJosef Bacik 1110860adfdSMiao Xie static inline void extwriter_counter_inc(struct btrfs_transaction *trans, 1120860adfdSMiao Xie unsigned int type) 1130860adfdSMiao Xie { 1140860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1150860adfdSMiao Xie atomic_inc(&trans->num_extwriters); 1160860adfdSMiao Xie } 1170860adfdSMiao Xie 1180860adfdSMiao Xie static inline void extwriter_counter_dec(struct btrfs_transaction *trans, 1190860adfdSMiao Xie unsigned int type) 1200860adfdSMiao Xie { 1210860adfdSMiao Xie if (type & TRANS_EXTWRITERS) 1220860adfdSMiao Xie atomic_dec(&trans->num_extwriters); 1230860adfdSMiao Xie } 1240860adfdSMiao Xie 1250860adfdSMiao Xie static inline void extwriter_counter_init(struct btrfs_transaction *trans, 1260860adfdSMiao Xie unsigned int type) 1270860adfdSMiao Xie { 1280860adfdSMiao Xie atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0)); 1290860adfdSMiao Xie } 1300860adfdSMiao Xie 1310860adfdSMiao Xie static inline int extwriter_counter_read(struct btrfs_transaction *trans) 1320860adfdSMiao Xie { 1330860adfdSMiao Xie return atomic_read(&trans->num_extwriters); 134178260b2SMiao Xie } 135178260b2SMiao Xie 136d352ac68SChris Mason /* 137fb6dea26SJosef Bacik * To be called after all the new block groups attached to the transaction 138fb6dea26SJosef Bacik * handle have been created (btrfs_create_pending_block_groups()). 139fb6dea26SJosef Bacik */ 140fb6dea26SJosef Bacik void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) 141fb6dea26SJosef Bacik { 142fb6dea26SJosef Bacik struct btrfs_fs_info *fs_info = trans->fs_info; 143fb6dea26SJosef Bacik 144fb6dea26SJosef Bacik if (!trans->chunk_bytes_reserved) 145fb6dea26SJosef Bacik return; 146fb6dea26SJosef Bacik 147fb6dea26SJosef Bacik WARN_ON_ONCE(!list_empty(&trans->new_bgs)); 148fb6dea26SJosef Bacik 149fb6dea26SJosef Bacik btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv, 150fb6dea26SJosef Bacik trans->chunk_bytes_reserved); 151fb6dea26SJosef Bacik trans->chunk_bytes_reserved = 0; 152fb6dea26SJosef Bacik } 153fb6dea26SJosef Bacik 154fb6dea26SJosef Bacik /* 155d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 156d352ac68SChris Mason */ 1572ff7e61eSJeff Mahoney static noinline int join_transaction(struct btrfs_fs_info *fs_info, 1582ff7e61eSJeff Mahoney unsigned int type) 15979154b1bSChris Mason { 16079154b1bSChris Mason struct btrfs_transaction *cur_trans; 161a4abeea4SJosef Bacik 16219ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 163d43317dcSChris Mason loop: 16449b25e05SJeff Mahoney /* The file system has been taken offline. No new transactions. */ 16587533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 16619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 16749b25e05SJeff Mahoney return -EROFS; 16849b25e05SJeff Mahoney } 16949b25e05SJeff Mahoney 17019ae4e81SJan Schmidt cur_trans = fs_info->running_transaction; 171a4abeea4SJosef Bacik if (cur_trans) { 172871383beSDavid Sterba if (cur_trans->aborted) { 17319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 17449b25e05SJeff Mahoney return cur_trans->aborted; 175871383beSDavid Sterba } 1764a9d8bdeSMiao Xie if (btrfs_blocked_trans_types[cur_trans->state] & type) { 177178260b2SMiao Xie spin_unlock(&fs_info->trans_lock); 178178260b2SMiao Xie return -EBUSY; 179178260b2SMiao Xie } 1809b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 181a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 1820860adfdSMiao Xie extwriter_counter_inc(cur_trans, type); 18319ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 184a4abeea4SJosef Bacik return 0; 185a4abeea4SJosef Bacik } 18619ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 187a4abeea4SJosef Bacik 188354aa0fbSMiao Xie /* 189354aa0fbSMiao Xie * If we are ATTACH, we just want to catch the current transaction, 190354aa0fbSMiao Xie * and commit it. If there is no transaction, just return ENOENT. 191354aa0fbSMiao Xie */ 192354aa0fbSMiao Xie if (type == TRANS_ATTACH) 193354aa0fbSMiao Xie return -ENOENT; 194354aa0fbSMiao Xie 1954a9d8bdeSMiao Xie /* 1964a9d8bdeSMiao Xie * JOIN_NOLOCK only happens during the transaction commit, so 1974a9d8bdeSMiao Xie * it is impossible that ->running_transaction is NULL 1984a9d8bdeSMiao Xie */ 1994a9d8bdeSMiao Xie BUG_ON(type == TRANS_JOIN_NOLOCK); 2004a9d8bdeSMiao Xie 2014b5faeacSDavid Sterba cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS); 202db5b493aSTsutomu Itoh if (!cur_trans) 203db5b493aSTsutomu Itoh return -ENOMEM; 204d43317dcSChris Mason 20519ae4e81SJan Schmidt spin_lock(&fs_info->trans_lock); 20619ae4e81SJan Schmidt if (fs_info->running_transaction) { 207d43317dcSChris Mason /* 208d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 2094a9d8bdeSMiao Xie * to redo the checks above 210d43317dcSChris Mason */ 2114b5faeacSDavid Sterba kfree(cur_trans); 212d43317dcSChris Mason goto loop; 21387533c47SMiao Xie } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 214e4b50e14SDan Carpenter spin_unlock(&fs_info->trans_lock); 2154b5faeacSDavid Sterba kfree(cur_trans); 2167b8b92afSJosef Bacik return -EROFS; 217a4abeea4SJosef Bacik } 218d43317dcSChris Mason 219ab8d0fc4SJeff Mahoney cur_trans->fs_info = fs_info; 22013c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 2210860adfdSMiao Xie extwriter_counter_init(cur_trans, type); 22279154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 22379154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 2244a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_RUNNING; 225a4abeea4SJosef Bacik /* 226a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 227a4abeea4SJosef Bacik * commit the transaction. 228a4abeea4SJosef Bacik */ 2299b64f57dSElena Reshetova refcount_set(&cur_trans->use_count, 2); 2303204d33cSJosef Bacik cur_trans->flags = 0; 231afd48513SArnd Bergmann cur_trans->start_time = ktime_get_seconds(); 23256bec294SChris Mason 233a099d0fdSAlexandru Moise memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs)); 234a099d0fdSAlexandru Moise 2355c9d028bSLiu Bo cur_trans->delayed_refs.href_root = RB_ROOT_CACHED; 2363368d001SQu Wenruo cur_trans->delayed_refs.dirty_extent_root = RB_ROOT; 237d7df2c79SJosef Bacik atomic_set(&cur_trans->delayed_refs.num_entries, 0); 23820b297d6SJan Schmidt 23920b297d6SJan Schmidt /* 24020b297d6SJan Schmidt * although the tree mod log is per file system and not per transaction, 24120b297d6SJan Schmidt * the log must never go across transaction boundaries. 24220b297d6SJan Schmidt */ 24320b297d6SJan Schmidt smp_mb(); 24431b1a2bdSJulia Lawall if (!list_empty(&fs_info->tree_mod_seq_list)) 2455d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n"); 24631b1a2bdSJulia Lawall if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) 2475d163e0eSJeff Mahoney WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n"); 248fc36ed7eSJan Schmidt atomic64_set(&fs_info->tree_mod_seq, 0); 24920b297d6SJan Schmidt 25056bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 25156bec294SChris Mason 2523063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 253bbbf7243SNikolay Borisov INIT_LIST_HEAD(&cur_trans->dev_update_list); 2549e351cc8SJosef Bacik INIT_LIST_HEAD(&cur_trans->switch_commits); 255ce93ec54SJosef Bacik INIT_LIST_HEAD(&cur_trans->dirty_bgs); 2561bbc621eSChris Mason INIT_LIST_HEAD(&cur_trans->io_bgs); 2572b9dbef2SJosef Bacik INIT_LIST_HEAD(&cur_trans->dropped_roots); 2581bbc621eSChris Mason mutex_init(&cur_trans->cache_write_mutex); 259ce93ec54SJosef Bacik spin_lock_init(&cur_trans->dirty_bgs_lock); 260e33e17eeSJeff Mahoney INIT_LIST_HEAD(&cur_trans->deleted_bgs); 2612b9dbef2SJosef Bacik spin_lock_init(&cur_trans->dropped_roots_lock); 26219ae4e81SJan Schmidt list_add_tail(&cur_trans->list, &fs_info->trans_list); 263c258d6e3SQu Wenruo extent_io_tree_init(fs_info, &cur_trans->dirty_pages, 26443eb5f29SQu Wenruo IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode); 26519ae4e81SJan Schmidt fs_info->generation++; 26619ae4e81SJan Schmidt cur_trans->transid = fs_info->generation; 26719ae4e81SJan Schmidt fs_info->running_transaction = cur_trans; 26849b25e05SJeff Mahoney cur_trans->aborted = 0; 26919ae4e81SJan Schmidt spin_unlock(&fs_info->trans_lock); 27015ee9bc7SJosef Bacik 27179154b1bSChris Mason return 0; 27279154b1bSChris Mason } 27379154b1bSChris Mason 274d352ac68SChris Mason /* 275d397712bSChris Mason * this does all the record keeping required to make sure that a reference 276d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 277d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 278d397712bSChris Mason * when the transaction commits 279d352ac68SChris Mason */ 2807585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 2816426c7adSQu Wenruo struct btrfs_root *root, 2826426c7adSQu Wenruo int force) 2836702ed49SChris Mason { 2840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 2850b246afaSJeff Mahoney 2866426c7adSQu Wenruo if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 2876426c7adSQu Wenruo root->last_trans < trans->transid) || force) { 2880b246afaSJeff Mahoney WARN_ON(root == fs_info->extent_root); 2894d31778aSQu Wenruo WARN_ON(!force && root->commit_root != root->node); 2905d4f98a2SYan Zheng 2917585717fSChris Mason /* 29227cdeb70SMiao Xie * see below for IN_TRANS_SETUP usage rules 2937585717fSChris Mason * we have the reloc mutex held now, so there 2947585717fSChris Mason * is only one writer in this function 2957585717fSChris Mason */ 29627cdeb70SMiao Xie set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 2977585717fSChris Mason 29827cdeb70SMiao Xie /* make sure readers find IN_TRANS_SETUP before 2997585717fSChris Mason * they find our root->last_trans update 3007585717fSChris Mason */ 3017585717fSChris Mason smp_wmb(); 3027585717fSChris Mason 3030b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3046426c7adSQu Wenruo if (root->last_trans == trans->transid && !force) { 3050b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 306a4abeea4SJosef Bacik return 0; 307a4abeea4SJosef Bacik } 3080b246afaSJeff Mahoney radix_tree_tag_set(&fs_info->fs_roots_radix, 3096702ed49SChris Mason (unsigned long)root->root_key.objectid, 3106702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 3110b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3127585717fSChris Mason root->last_trans = trans->transid; 3137585717fSChris Mason 3147585717fSChris Mason /* this is pretty tricky. We don't want to 3157585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 3167585717fSChris Mason * unless we're really doing the first setup for this root in 3177585717fSChris Mason * this transaction. 3187585717fSChris Mason * 3197585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 3207585717fSChris Mason * if we want to take the expensive mutex. 3217585717fSChris Mason * 3227585717fSChris Mason * But, we have to set root->last_trans before we 3237585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 3247585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 32527cdeb70SMiao Xie * with root IN_TRANS_SETUP. When this is 1, we're still 3267585717fSChris Mason * fixing up the reloc trees and everyone must wait. 3277585717fSChris Mason * 3287585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 3297585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 3307585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 3317585717fSChris Mason * done before we pop in the zero below 3327585717fSChris Mason */ 3335d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 334c7548af6SChris Mason smp_mb__before_atomic(); 33527cdeb70SMiao Xie clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); 3366702ed49SChris Mason } 3375d4f98a2SYan Zheng return 0; 3386702ed49SChris Mason } 3395d4f98a2SYan Zheng 3407585717fSChris Mason 3412b9dbef2SJosef Bacik void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 3422b9dbef2SJosef Bacik struct btrfs_root *root) 3432b9dbef2SJosef Bacik { 3440b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3452b9dbef2SJosef Bacik struct btrfs_transaction *cur_trans = trans->transaction; 3462b9dbef2SJosef Bacik 3472b9dbef2SJosef Bacik /* Add ourselves to the transaction dropped list */ 3482b9dbef2SJosef Bacik spin_lock(&cur_trans->dropped_roots_lock); 3492b9dbef2SJosef Bacik list_add_tail(&root->root_list, &cur_trans->dropped_roots); 3502b9dbef2SJosef Bacik spin_unlock(&cur_trans->dropped_roots_lock); 3512b9dbef2SJosef Bacik 3522b9dbef2SJosef Bacik /* Make sure we don't try to update the root at commit time */ 3530b246afaSJeff Mahoney spin_lock(&fs_info->fs_roots_radix_lock); 3540b246afaSJeff Mahoney radix_tree_tag_clear(&fs_info->fs_roots_radix, 3552b9dbef2SJosef Bacik (unsigned long)root->root_key.objectid, 3562b9dbef2SJosef Bacik BTRFS_ROOT_TRANS_TAG); 3570b246afaSJeff Mahoney spin_unlock(&fs_info->fs_roots_radix_lock); 3582b9dbef2SJosef Bacik } 3592b9dbef2SJosef Bacik 3607585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 3617585717fSChris Mason struct btrfs_root *root) 3627585717fSChris Mason { 3630b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 3640b246afaSJeff Mahoney 36527cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 3667585717fSChris Mason return 0; 3677585717fSChris Mason 3687585717fSChris Mason /* 36927cdeb70SMiao Xie * see record_root_in_trans for comments about IN_TRANS_SETUP usage 3707585717fSChris Mason * and barriers 3717585717fSChris Mason */ 3727585717fSChris Mason smp_rmb(); 3737585717fSChris Mason if (root->last_trans == trans->transid && 37427cdeb70SMiao Xie !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 3757585717fSChris Mason return 0; 3767585717fSChris Mason 3770b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 3786426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 3790b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 3807585717fSChris Mason 3817585717fSChris Mason return 0; 3827585717fSChris Mason } 3837585717fSChris Mason 3844a9d8bdeSMiao Xie static inline int is_transaction_blocked(struct btrfs_transaction *trans) 3854a9d8bdeSMiao Xie { 3864a9d8bdeSMiao Xie return (trans->state >= TRANS_STATE_BLOCKED && 387501407aaSJosef Bacik trans->state < TRANS_STATE_UNBLOCKED && 388501407aaSJosef Bacik !trans->aborted); 3894a9d8bdeSMiao Xie } 3904a9d8bdeSMiao Xie 391d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 392d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 393d352ac68SChris Mason * transaction might not be fully on disk. 394d352ac68SChris Mason */ 3952ff7e61eSJeff Mahoney static void wait_current_trans(struct btrfs_fs_info *fs_info) 39679154b1bSChris Mason { 397f9295749SChris Mason struct btrfs_transaction *cur_trans; 39879154b1bSChris Mason 3990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 4000b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 4014a9d8bdeSMiao Xie if (cur_trans && is_transaction_blocked(cur_trans)) { 4029b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 4030b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 40472d63ed6SLi Zefan 4050b246afaSJeff Mahoney wait_event(fs_info->transaction_wait, 406501407aaSJosef Bacik cur_trans->state >= TRANS_STATE_UNBLOCKED || 407501407aaSJosef Bacik cur_trans->aborted); 408724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 409a4abeea4SJosef Bacik } else { 4100b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 411f9295749SChris Mason } 41237d1aeeeSChris Mason } 41337d1aeeeSChris Mason 4142ff7e61eSJeff Mahoney static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type) 41537d1aeeeSChris Mason { 4160b246afaSJeff Mahoney if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 417a4abeea4SJosef Bacik return 0; 418a4abeea4SJosef Bacik 41992e2f7e3SNikolay Borisov if (type == TRANS_START) 420a4abeea4SJosef Bacik return 1; 421a4abeea4SJosef Bacik 422a22285a6SYan, Zheng return 0; 423a22285a6SYan, Zheng } 424a22285a6SYan, Zheng 42520dd2cbfSMiao Xie static inline bool need_reserve_reloc_root(struct btrfs_root *root) 42620dd2cbfSMiao Xie { 4270b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 4280b246afaSJeff Mahoney 4290b246afaSJeff Mahoney if (!fs_info->reloc_ctl || 43027cdeb70SMiao Xie !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 43120dd2cbfSMiao Xie root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 43220dd2cbfSMiao Xie root->reloc_root) 43320dd2cbfSMiao Xie return false; 43420dd2cbfSMiao Xie 43520dd2cbfSMiao Xie return true; 43620dd2cbfSMiao Xie } 43720dd2cbfSMiao Xie 43808e007d2SMiao Xie static struct btrfs_trans_handle * 4395aed1dd8SAlexandru Moise start_transaction(struct btrfs_root *root, unsigned int num_items, 440003d7c59SJeff Mahoney unsigned int type, enum btrfs_reserve_flush_enum flush, 441003d7c59SJeff Mahoney bool enforce_qgroups) 442a22285a6SYan, Zheng { 4430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 444ba2c4d4eSJosef Bacik struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; 445a22285a6SYan, Zheng struct btrfs_trans_handle *h; 446a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 447b5009945SJosef Bacik u64 num_bytes = 0; 448c5567237SArne Jansen u64 qgroup_reserved = 0; 44920dd2cbfSMiao Xie bool reloc_reserved = false; 45020dd2cbfSMiao Xie int ret; 451acce952bSliubo 45246c4e71eSFilipe Manana /* Send isn't supposed to start transactions. */ 4532755a0deSDavid Sterba ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 45446c4e71eSFilipe Manana 4550b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 456acce952bSliubo return ERR_PTR(-EROFS); 4572a1eb461SJosef Bacik 45846c4e71eSFilipe Manana if (current->journal_info) { 4590860adfdSMiao Xie WARN_ON(type & TRANS_EXTWRITERS); 4602a1eb461SJosef Bacik h = current->journal_info; 461b50fff81SDavid Sterba refcount_inc(&h->use_count); 462b50fff81SDavid Sterba WARN_ON(refcount_read(&h->use_count) > 2); 4632a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 4642a1eb461SJosef Bacik h->block_rsv = NULL; 4652a1eb461SJosef Bacik goto got_it; 4662a1eb461SJosef Bacik } 467b5009945SJosef Bacik 468b5009945SJosef Bacik /* 469b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 470b5009945SJosef Bacik * the appropriate flushing if need be. 471b5009945SJosef Bacik */ 472003d7c59SJeff Mahoney if (num_items && root != fs_info->chunk_root) { 473ba2c4d4eSJosef Bacik struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv; 474ba2c4d4eSJosef Bacik u64 delayed_refs_bytes = 0; 475ba2c4d4eSJosef Bacik 4760b246afaSJeff Mahoney qgroup_reserved = num_items * fs_info->nodesize; 477733e03a0SQu Wenruo ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved, 478003d7c59SJeff Mahoney enforce_qgroups); 479c5567237SArne Jansen if (ret) 480c5567237SArne Jansen return ERR_PTR(ret); 481c5567237SArne Jansen 482ba2c4d4eSJosef Bacik /* 483ba2c4d4eSJosef Bacik * We want to reserve all the bytes we may need all at once, so 484ba2c4d4eSJosef Bacik * we only do 1 enospc flushing cycle per transaction start. We 485ba2c4d4eSJosef Bacik * accomplish this by simply assuming we'll do 2 x num_items 486ba2c4d4eSJosef Bacik * worth of delayed refs updates in this trans handle, and 487ba2c4d4eSJosef Bacik * refill that amount for whatever is missing in the reserve. 488ba2c4d4eSJosef Bacik */ 4892bd36e7bSJosef Bacik num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items); 490ba2c4d4eSJosef Bacik if (delayed_refs_rsv->full == 0) { 491ba2c4d4eSJosef Bacik delayed_refs_bytes = num_bytes; 492ba2c4d4eSJosef Bacik num_bytes <<= 1; 493ba2c4d4eSJosef Bacik } 494ba2c4d4eSJosef Bacik 49520dd2cbfSMiao Xie /* 49620dd2cbfSMiao Xie * Do the reservation for the relocation root creation 49720dd2cbfSMiao Xie */ 498ee39b432SDavid Sterba if (need_reserve_reloc_root(root)) { 4990b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 50020dd2cbfSMiao Xie reloc_reserved = true; 50120dd2cbfSMiao Xie } 50220dd2cbfSMiao Xie 503ba2c4d4eSJosef Bacik ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush); 504ba2c4d4eSJosef Bacik if (ret) 505ba2c4d4eSJosef Bacik goto reserve_fail; 506ba2c4d4eSJosef Bacik if (delayed_refs_bytes) { 507ba2c4d4eSJosef Bacik btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv, 508ba2c4d4eSJosef Bacik delayed_refs_bytes); 509ba2c4d4eSJosef Bacik num_bytes -= delayed_refs_bytes; 510ba2c4d4eSJosef Bacik } 511ba2c4d4eSJosef Bacik } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL && 512ba2c4d4eSJosef Bacik !delayed_refs_rsv->full) { 513ba2c4d4eSJosef Bacik /* 514ba2c4d4eSJosef Bacik * Some people call with btrfs_start_transaction(root, 0) 515ba2c4d4eSJosef Bacik * because they can be throttled, but have some other mechanism 516ba2c4d4eSJosef Bacik * for reserving space. We still want these guys to refill the 517ba2c4d4eSJosef Bacik * delayed block_rsv so just add 1 items worth of reservation 518ba2c4d4eSJosef Bacik * here. 519ba2c4d4eSJosef Bacik */ 520ba2c4d4eSJosef Bacik ret = btrfs_delayed_refs_rsv_refill(fs_info, flush); 521b5009945SJosef Bacik if (ret) 522843fcf35SMiao Xie goto reserve_fail; 523b5009945SJosef Bacik } 524a22285a6SYan, Zheng again: 525f2f767e7SAlexandru Moise h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS); 526843fcf35SMiao Xie if (!h) { 527843fcf35SMiao Xie ret = -ENOMEM; 528843fcf35SMiao Xie goto alloc_fail; 529843fcf35SMiao Xie } 530a22285a6SYan, Zheng 53198114659SJosef Bacik /* 53298114659SJosef Bacik * If we are JOIN_NOLOCK we're already committing a transaction and 53398114659SJosef Bacik * waiting on this guy, so we don't need to do the sb_start_intwrite 53498114659SJosef Bacik * because we're already holding a ref. We need this because we could 53598114659SJosef Bacik * have raced in and did an fsync() on a file which can kick a commit 53698114659SJosef Bacik * and then we deadlock with somebody doing a freeze. 537354aa0fbSMiao Xie * 538354aa0fbSMiao Xie * If we are ATTACH, it means we just want to catch the current 539354aa0fbSMiao Xie * transaction and commit it, so we needn't do sb_start_intwrite(). 54098114659SJosef Bacik */ 5410860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5420b246afaSJeff Mahoney sb_start_intwrite(fs_info->sb); 543b2b5ef5cSJan Kara 5442ff7e61eSJeff Mahoney if (may_wait_transaction(fs_info, type)) 5452ff7e61eSJeff Mahoney wait_current_trans(fs_info); 546a22285a6SYan, Zheng 547a4abeea4SJosef Bacik do { 5482ff7e61eSJeff Mahoney ret = join_transaction(fs_info, type); 549178260b2SMiao Xie if (ret == -EBUSY) { 5502ff7e61eSJeff Mahoney wait_current_trans(fs_info); 551a6d155d2SFilipe Manana if (unlikely(type == TRANS_ATTACH || 552a6d155d2SFilipe Manana type == TRANS_JOIN_NOSTART)) 553178260b2SMiao Xie ret = -ENOENT; 554178260b2SMiao Xie } 555a4abeea4SJosef Bacik } while (ret == -EBUSY); 556a4abeea4SJosef Bacik 557a43f7f82SLiu Bo if (ret < 0) 558843fcf35SMiao Xie goto join_fail; 5590f7d52f4SChris Mason 5600b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 561a22285a6SYan, Zheng 562a22285a6SYan, Zheng h->transid = cur_trans->transid; 563a22285a6SYan, Zheng h->transaction = cur_trans; 564d13603efSArne Jansen h->root = root; 565b50fff81SDavid Sterba refcount_set(&h->use_count, 1); 56664b63580SJeff Mahoney h->fs_info = root->fs_info; 5677174109cSQu Wenruo 568a698d075SMiao Xie h->type = type; 569d9a0540aSFilipe Manana h->can_flush_pending_bgs = true; 570ea658badSJosef Bacik INIT_LIST_HEAD(&h->new_bgs); 571b7ec40d7SChris Mason 572a22285a6SYan, Zheng smp_mb(); 5734a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED && 5742ff7e61eSJeff Mahoney may_wait_transaction(fs_info, type)) { 575abdd2e80SFilipe Manana current->journal_info = h; 5763a45bb20SJeff Mahoney btrfs_commit_transaction(h); 577a22285a6SYan, Zheng goto again; 578a22285a6SYan, Zheng } 5799ed74f2dSJosef Bacik 580b5009945SJosef Bacik if (num_bytes) { 5810b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 5822bcc0328SLiu Bo h->transid, num_bytes, 1); 5830b246afaSJeff Mahoney h->block_rsv = &fs_info->trans_block_rsv; 584b5009945SJosef Bacik h->bytes_reserved = num_bytes; 58520dd2cbfSMiao Xie h->reloc_reserved = reloc_reserved; 586a22285a6SYan, Zheng } 587a22285a6SYan, Zheng 5882a1eb461SJosef Bacik got_it: 589a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 590a22285a6SYan, Zheng 591bcf3a3e7SNikolay Borisov if (!current->journal_info) 592a22285a6SYan, Zheng current->journal_info = h; 59379154b1bSChris Mason return h; 594843fcf35SMiao Xie 595843fcf35SMiao Xie join_fail: 5960860adfdSMiao Xie if (type & __TRANS_FREEZABLE) 5970b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 598843fcf35SMiao Xie kmem_cache_free(btrfs_trans_handle_cachep, h); 599843fcf35SMiao Xie alloc_fail: 600843fcf35SMiao Xie if (num_bytes) 6012ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv, 602843fcf35SMiao Xie num_bytes); 603843fcf35SMiao Xie reserve_fail: 604733e03a0SQu Wenruo btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved); 605843fcf35SMiao Xie return ERR_PTR(ret); 60679154b1bSChris Mason } 60779154b1bSChris Mason 608f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 6095aed1dd8SAlexandru Moise unsigned int num_items) 610f9295749SChris Mason { 61108e007d2SMiao Xie return start_transaction(root, num_items, TRANS_START, 612003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, true); 613f9295749SChris Mason } 614003d7c59SJeff Mahoney 6158eab77ffSFilipe Manana struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv( 6168eab77ffSFilipe Manana struct btrfs_root *root, 6178eab77ffSFilipe Manana unsigned int num_items, 6188eab77ffSFilipe Manana int min_factor) 6198eab77ffSFilipe Manana { 6200b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6218eab77ffSFilipe Manana struct btrfs_trans_handle *trans; 6228eab77ffSFilipe Manana u64 num_bytes; 6238eab77ffSFilipe Manana int ret; 6248eab77ffSFilipe Manana 625003d7c59SJeff Mahoney /* 626003d7c59SJeff Mahoney * We have two callers: unlink and block group removal. The 627003d7c59SJeff Mahoney * former should succeed even if we will temporarily exceed 628003d7c59SJeff Mahoney * quota and the latter operates on the extent root so 629003d7c59SJeff Mahoney * qgroup enforcement is ignored anyway. 630003d7c59SJeff Mahoney */ 631003d7c59SJeff Mahoney trans = start_transaction(root, num_items, TRANS_START, 632003d7c59SJeff Mahoney BTRFS_RESERVE_FLUSH_ALL, false); 6338eab77ffSFilipe Manana if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 6348eab77ffSFilipe Manana return trans; 6358eab77ffSFilipe Manana 6368eab77ffSFilipe Manana trans = btrfs_start_transaction(root, 0); 6378eab77ffSFilipe Manana if (IS_ERR(trans)) 6388eab77ffSFilipe Manana return trans; 6398eab77ffSFilipe Manana 6402bd36e7bSJosef Bacik num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items); 6410b246afaSJeff Mahoney ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv, 6420b246afaSJeff Mahoney num_bytes, min_factor); 6438eab77ffSFilipe Manana if (ret) { 6443a45bb20SJeff Mahoney btrfs_end_transaction(trans); 6458eab77ffSFilipe Manana return ERR_PTR(ret); 6468eab77ffSFilipe Manana } 6478eab77ffSFilipe Manana 6480b246afaSJeff Mahoney trans->block_rsv = &fs_info->trans_block_rsv; 6498eab77ffSFilipe Manana trans->bytes_reserved = num_bytes; 6500b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 65188d3a5aaSJosef Bacik trans->transid, num_bytes, 1); 6528eab77ffSFilipe Manana 6538eab77ffSFilipe Manana return trans; 6548eab77ffSFilipe Manana } 6558407aa46SMiao Xie 6567a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 657f9295749SChris Mason { 658003d7c59SJeff Mahoney return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH, 659003d7c59SJeff Mahoney true); 660f9295749SChris Mason } 661f9295749SChris Mason 6627a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 6630af3d00bSJosef Bacik { 664575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 665003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 6660af3d00bSJosef Bacik } 6670af3d00bSJosef Bacik 668d4edf39bSMiao Xie /* 669a6d155d2SFilipe Manana * Similar to regular join but it never starts a transaction when none is 670a6d155d2SFilipe Manana * running or after waiting for the current one to finish. 671a6d155d2SFilipe Manana */ 672a6d155d2SFilipe Manana struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root) 673a6d155d2SFilipe Manana { 674a6d155d2SFilipe Manana return start_transaction(root, 0, TRANS_JOIN_NOSTART, 675a6d155d2SFilipe Manana BTRFS_RESERVE_NO_FLUSH, true); 676a6d155d2SFilipe Manana } 677a6d155d2SFilipe Manana 678a6d155d2SFilipe Manana /* 679d4edf39bSMiao Xie * btrfs_attach_transaction() - catch the running transaction 680d4edf39bSMiao Xie * 681d4edf39bSMiao Xie * It is used when we want to commit the current the transaction, but 682d4edf39bSMiao Xie * don't want to start a new one. 683d4edf39bSMiao Xie * 684d4edf39bSMiao Xie * Note: If this function return -ENOENT, it just means there is no 685d4edf39bSMiao Xie * running transaction. But it is possible that the inactive transaction 686d4edf39bSMiao Xie * is still in the memory, not fully on disk. If you hope there is no 687d4edf39bSMiao Xie * inactive transaction in the fs when -ENOENT is returned, you should 688d4edf39bSMiao Xie * invoke 689d4edf39bSMiao Xie * btrfs_attach_transaction_barrier() 690d4edf39bSMiao Xie */ 691354aa0fbSMiao Xie struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root) 69260376ce4SJosef Bacik { 693575a75d6SAlexandru Moise return start_transaction(root, 0, TRANS_ATTACH, 694003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 69560376ce4SJosef Bacik } 69660376ce4SJosef Bacik 697d4edf39bSMiao Xie /* 69890b6d283SWang Sheng-Hui * btrfs_attach_transaction_barrier() - catch the running transaction 699d4edf39bSMiao Xie * 70052042d8eSAndrea Gelmini * It is similar to the above function, the difference is this one 701d4edf39bSMiao Xie * will wait for all the inactive transactions until they fully 702d4edf39bSMiao Xie * complete. 703d4edf39bSMiao Xie */ 704d4edf39bSMiao Xie struct btrfs_trans_handle * 705d4edf39bSMiao Xie btrfs_attach_transaction_barrier(struct btrfs_root *root) 706d4edf39bSMiao Xie { 707d4edf39bSMiao Xie struct btrfs_trans_handle *trans; 708d4edf39bSMiao Xie 709575a75d6SAlexandru Moise trans = start_transaction(root, 0, TRANS_ATTACH, 710003d7c59SJeff Mahoney BTRFS_RESERVE_NO_FLUSH, true); 7118d9e220cSAl Viro if (trans == ERR_PTR(-ENOENT)) 7122ff7e61eSJeff Mahoney btrfs_wait_for_commit(root->fs_info, 0); 713d4edf39bSMiao Xie 714d4edf39bSMiao Xie return trans; 715d4edf39bSMiao Xie } 716d4edf39bSMiao Xie 717d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 7182ff7e61eSJeff Mahoney static noinline void wait_for_commit(struct btrfs_transaction *commit) 71989ce8a63SChris Mason { 7204a9d8bdeSMiao Xie wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 72189ce8a63SChris Mason } 72289ce8a63SChris Mason 7232ff7e61eSJeff Mahoney int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) 72446204592SSage Weil { 72546204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 7268cd2807fSMiao Xie int ret = 0; 72746204592SSage Weil 72846204592SSage Weil if (transid) { 7290b246afaSJeff Mahoney if (transid <= fs_info->last_trans_committed) 730a4abeea4SJosef Bacik goto out; 73146204592SSage Weil 73246204592SSage Weil /* find specified transaction */ 7330b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7340b246afaSJeff Mahoney list_for_each_entry(t, &fs_info->trans_list, list) { 73546204592SSage Weil if (t->transid == transid) { 73646204592SSage Weil cur_trans = t; 7379b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 7388cd2807fSMiao Xie ret = 0; 73946204592SSage Weil break; 74046204592SSage Weil } 7418cd2807fSMiao Xie if (t->transid > transid) { 7428cd2807fSMiao Xie ret = 0; 74346204592SSage Weil break; 74446204592SSage Weil } 7458cd2807fSMiao Xie } 7460b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 74742383020SSage Weil 74842383020SSage Weil /* 74942383020SSage Weil * The specified transaction doesn't exist, or we 75042383020SSage Weil * raced with btrfs_commit_transaction 75142383020SSage Weil */ 75242383020SSage Weil if (!cur_trans) { 7530b246afaSJeff Mahoney if (transid > fs_info->last_trans_committed) 75442383020SSage Weil ret = -EINVAL; 7558cd2807fSMiao Xie goto out; 75642383020SSage Weil } 75746204592SSage Weil } else { 75846204592SSage Weil /* find newest transaction that is committing | committed */ 7590b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 7600b246afaSJeff Mahoney list_for_each_entry_reverse(t, &fs_info->trans_list, 76146204592SSage Weil list) { 7624a9d8bdeSMiao Xie if (t->state >= TRANS_STATE_COMMIT_START) { 7634a9d8bdeSMiao Xie if (t->state == TRANS_STATE_COMPLETED) 7643473f3c0SJosef Bacik break; 76546204592SSage Weil cur_trans = t; 7669b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 76746204592SSage Weil break; 76846204592SSage Weil } 76946204592SSage Weil } 7700b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 77146204592SSage Weil if (!cur_trans) 772a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 77346204592SSage Weil } 77446204592SSage Weil 7752ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 776724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 777a4abeea4SJosef Bacik out: 77846204592SSage Weil return ret; 77946204592SSage Weil } 78046204592SSage Weil 7812ff7e61eSJeff Mahoney void btrfs_throttle(struct btrfs_fs_info *fs_info) 78237d1aeeeSChris Mason { 7832ff7e61eSJeff Mahoney wait_current_trans(fs_info); 78437d1aeeeSChris Mason } 78537d1aeeeSChris Mason 7862ff7e61eSJeff Mahoney static int should_end_transaction(struct btrfs_trans_handle *trans) 7878929ecfaSYan, Zheng { 7882ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 7890b246afaSJeff Mahoney 79064403612SJosef Bacik if (btrfs_check_space_for_delayed_refs(fs_info)) 7911be41b78SJosef Bacik return 1; 79236ba022aSJosef Bacik 7932ff7e61eSJeff Mahoney return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); 7948929ecfaSYan, Zheng } 7958929ecfaSYan, Zheng 7963a45bb20SJeff Mahoney int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) 7978929ecfaSYan, Zheng { 7988929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 7998929ecfaSYan, Zheng 800a4abeea4SJosef Bacik smp_mb(); 8014a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_BLOCKED || 8024a9d8bdeSMiao Xie cur_trans->delayed_refs.flushing) 8038929ecfaSYan, Zheng return 1; 8048929ecfaSYan, Zheng 8052ff7e61eSJeff Mahoney return should_end_transaction(trans); 8068929ecfaSYan, Zheng } 8078929ecfaSYan, Zheng 808dc60c525SNikolay Borisov static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) 809dc60c525SNikolay Borisov 8100e34693fSNikolay Borisov { 811dc60c525SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 812dc60c525SNikolay Borisov 8130e34693fSNikolay Borisov if (!trans->block_rsv) { 8140e34693fSNikolay Borisov ASSERT(!trans->bytes_reserved); 8150e34693fSNikolay Borisov return; 8160e34693fSNikolay Borisov } 8170e34693fSNikolay Borisov 8180e34693fSNikolay Borisov if (!trans->bytes_reserved) 8190e34693fSNikolay Borisov return; 8200e34693fSNikolay Borisov 8210e34693fSNikolay Borisov ASSERT(trans->block_rsv == &fs_info->trans_block_rsv); 8220e34693fSNikolay Borisov trace_btrfs_space_reservation(fs_info, "transaction", 8230e34693fSNikolay Borisov trans->transid, trans->bytes_reserved, 0); 8240e34693fSNikolay Borisov btrfs_block_rsv_release(fs_info, trans->block_rsv, 8250e34693fSNikolay Borisov trans->bytes_reserved); 8260e34693fSNikolay Borisov trans->bytes_reserved = 0; 8270e34693fSNikolay Borisov } 8280e34693fSNikolay Borisov 82989ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 8303a45bb20SJeff Mahoney int throttle) 83179154b1bSChris Mason { 8323a45bb20SJeff Mahoney struct btrfs_fs_info *info = trans->fs_info; 8338929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 834a698d075SMiao Xie int lock = (trans->type != TRANS_JOIN_NOLOCK); 8354edc2ca3SDave Jones int err = 0; 836d6e4a428SChris Mason 837b50fff81SDavid Sterba if (refcount_read(&trans->use_count) > 1) { 838b50fff81SDavid Sterba refcount_dec(&trans->use_count); 8392a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 8402a1eb461SJosef Bacik return 0; 8412a1eb461SJosef Bacik } 8422a1eb461SJosef Bacik 843dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 8444c13d758SJosef Bacik trans->block_rsv = NULL; 845c5567237SArne Jansen 8466c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 847ea658badSJosef Bacik 8484fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 8494fbcdf66SFilipe Manana 85020c7bcecSSeraphime Kirkovski if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) { 8513bbb24b2SJosef Bacik if (throttle) 8523a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 8533bbb24b2SJosef Bacik else 8548929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 8558929ecfaSYan, Zheng } 8568929ecfaSYan, Zheng 8570860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 8580b246afaSJeff Mahoney sb_end_intwrite(info->sb); 8596df7881aSJosef Bacik 8608929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 86113c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 86213c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 8630860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 86489ce8a63SChris Mason 865093258e6SDavid Sterba cond_wake_up(&cur_trans->writer_wait); 866724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 8679ed74f2dSJosef Bacik 8689ed74f2dSJosef Bacik if (current->journal_info == trans) 8699ed74f2dSJosef Bacik current->journal_info = NULL; 870ab78c84dSChris Mason 87124bbcf04SYan, Zheng if (throttle) 8722ff7e61eSJeff Mahoney btrfs_run_delayed_iputs(info); 87324bbcf04SYan, Zheng 87449b25e05SJeff Mahoney if (trans->aborted || 8750b246afaSJeff Mahoney test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 8764e121c06SJosef Bacik wake_up_process(info->transaction_kthread); 8774edc2ca3SDave Jones err = -EIO; 8784e121c06SJosef Bacik } 87949b25e05SJeff Mahoney 8804edc2ca3SDave Jones kmem_cache_free(btrfs_trans_handle_cachep, trans); 8814edc2ca3SDave Jones return err; 88279154b1bSChris Mason } 88379154b1bSChris Mason 8843a45bb20SJeff Mahoney int btrfs_end_transaction(struct btrfs_trans_handle *trans) 88589ce8a63SChris Mason { 8863a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 0); 88789ce8a63SChris Mason } 88889ce8a63SChris Mason 8893a45bb20SJeff Mahoney int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans) 89089ce8a63SChris Mason { 8913a45bb20SJeff Mahoney return __btrfs_end_transaction(trans, 1); 89216cdcec7SMiao Xie } 89316cdcec7SMiao Xie 894d352ac68SChris Mason /* 895d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 896d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 897690587d1SChris Mason * those extents are sent to disk but does not wait on them 898d352ac68SChris Mason */ 8992ff7e61eSJeff Mahoney int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info, 9008cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 90179154b1bSChris Mason { 902777e6bd7SChris Mason int err = 0; 9037c4452b9SChris Mason int werr = 0; 9040b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 905e6138876SJosef Bacik struct extent_state *cached_state = NULL; 906777e6bd7SChris Mason u64 start = 0; 9075f39d397SChris Mason u64 end; 9087c4452b9SChris Mason 9096300463bSLiu Bo atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers); 9101728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 911e6138876SJosef Bacik mark, &cached_state)) { 912663dfbb0SFilipe Manana bool wait_writeback = false; 913663dfbb0SFilipe Manana 914663dfbb0SFilipe Manana err = convert_extent_bit(dirty_pages, start, end, 915663dfbb0SFilipe Manana EXTENT_NEED_WAIT, 916210aa277SDavid Sterba mark, &cached_state); 917663dfbb0SFilipe Manana /* 918663dfbb0SFilipe Manana * convert_extent_bit can return -ENOMEM, which is most of the 919663dfbb0SFilipe Manana * time a temporary error. So when it happens, ignore the error 920663dfbb0SFilipe Manana * and wait for writeback of this range to finish - because we 921663dfbb0SFilipe Manana * failed to set the bit EXTENT_NEED_WAIT for the range, a call 922bf89d38fSJeff Mahoney * to __btrfs_wait_marked_extents() would not know that 923bf89d38fSJeff Mahoney * writeback for this range started and therefore wouldn't 924bf89d38fSJeff Mahoney * wait for it to finish - we don't want to commit a 925bf89d38fSJeff Mahoney * superblock that points to btree nodes/leafs for which 926bf89d38fSJeff Mahoney * writeback hasn't finished yet (and without errors). 927663dfbb0SFilipe Manana * We cleanup any entries left in the io tree when committing 92841e7acd3SNikolay Borisov * the transaction (through extent_io_tree_release()). 929663dfbb0SFilipe Manana */ 930663dfbb0SFilipe Manana if (err == -ENOMEM) { 931663dfbb0SFilipe Manana err = 0; 932663dfbb0SFilipe Manana wait_writeback = true; 933663dfbb0SFilipe Manana } 934663dfbb0SFilipe Manana if (!err) 9351728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 9367c4452b9SChris Mason if (err) 9377c4452b9SChris Mason werr = err; 938663dfbb0SFilipe Manana else if (wait_writeback) 939663dfbb0SFilipe Manana werr = filemap_fdatawait_range(mapping, start, end); 940e38e2ed7SFilipe Manana free_extent_state(cached_state); 941663dfbb0SFilipe Manana cached_state = NULL; 9421728366eSJosef Bacik cond_resched(); 9431728366eSJosef Bacik start = end + 1; 9447c4452b9SChris Mason } 9456300463bSLiu Bo atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers); 946690587d1SChris Mason return werr; 947690587d1SChris Mason } 948690587d1SChris Mason 949690587d1SChris Mason /* 950690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 951690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 952690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 953690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 954690587d1SChris Mason */ 955bf89d38fSJeff Mahoney static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info, 956bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 957690587d1SChris Mason { 958690587d1SChris Mason int err = 0; 959690587d1SChris Mason int werr = 0; 9600b246afaSJeff Mahoney struct address_space *mapping = fs_info->btree_inode->i_mapping; 961e6138876SJosef Bacik struct extent_state *cached_state = NULL; 962690587d1SChris Mason u64 start = 0; 963690587d1SChris Mason u64 end; 964690587d1SChris Mason 9651728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 966e6138876SJosef Bacik EXTENT_NEED_WAIT, &cached_state)) { 967663dfbb0SFilipe Manana /* 968663dfbb0SFilipe Manana * Ignore -ENOMEM errors returned by clear_extent_bit(). 969663dfbb0SFilipe Manana * When committing the transaction, we'll remove any entries 970663dfbb0SFilipe Manana * left in the io tree. For a log commit, we don't remove them 971663dfbb0SFilipe Manana * after committing the log because the tree can be accessed 972663dfbb0SFilipe Manana * concurrently - we do it only at transaction commit time when 97341e7acd3SNikolay Borisov * it's safe to do it (through extent_io_tree_release()). 974663dfbb0SFilipe Manana */ 975663dfbb0SFilipe Manana err = clear_extent_bit(dirty_pages, start, end, 976ae0f1625SDavid Sterba EXTENT_NEED_WAIT, 0, 0, &cached_state); 977663dfbb0SFilipe Manana if (err == -ENOMEM) 978663dfbb0SFilipe Manana err = 0; 979663dfbb0SFilipe Manana if (!err) 9801728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 981777e6bd7SChris Mason if (err) 982777e6bd7SChris Mason werr = err; 983e38e2ed7SFilipe Manana free_extent_state(cached_state); 984e38e2ed7SFilipe Manana cached_state = NULL; 985777e6bd7SChris Mason cond_resched(); 9861728366eSJosef Bacik start = end + 1; 987777e6bd7SChris Mason } 9887c4452b9SChris Mason if (err) 9897c4452b9SChris Mason werr = err; 990bf89d38fSJeff Mahoney return werr; 991bf89d38fSJeff Mahoney } 992656f30dbSFilipe Manana 993bf89d38fSJeff Mahoney int btrfs_wait_extents(struct btrfs_fs_info *fs_info, 994bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages) 995bf89d38fSJeff Mahoney { 996bf89d38fSJeff Mahoney bool errors = false; 997bf89d38fSJeff Mahoney int err; 998bf89d38fSJeff Mahoney 999bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1000bf89d38fSJeff Mahoney if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags)) 1001bf89d38fSJeff Mahoney errors = true; 1002bf89d38fSJeff Mahoney 1003bf89d38fSJeff Mahoney if (errors && !err) 1004bf89d38fSJeff Mahoney err = -EIO; 1005bf89d38fSJeff Mahoney return err; 1006bf89d38fSJeff Mahoney } 1007bf89d38fSJeff Mahoney 1008bf89d38fSJeff Mahoney int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark) 1009bf89d38fSJeff Mahoney { 1010bf89d38fSJeff Mahoney struct btrfs_fs_info *fs_info = log_root->fs_info; 1011bf89d38fSJeff Mahoney struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages; 1012bf89d38fSJeff Mahoney bool errors = false; 1013bf89d38fSJeff Mahoney int err; 1014bf89d38fSJeff Mahoney 1015bf89d38fSJeff Mahoney ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); 1016bf89d38fSJeff Mahoney 1017bf89d38fSJeff Mahoney err = __btrfs_wait_marked_extents(fs_info, dirty_pages); 1018656f30dbSFilipe Manana if ((mark & EXTENT_DIRTY) && 10190b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags)) 1020656f30dbSFilipe Manana errors = true; 1021656f30dbSFilipe Manana 1022656f30dbSFilipe Manana if ((mark & EXTENT_NEW) && 10230b246afaSJeff Mahoney test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags)) 1024656f30dbSFilipe Manana errors = true; 1025656f30dbSFilipe Manana 1026bf89d38fSJeff Mahoney if (errors && !err) 1027bf89d38fSJeff Mahoney err = -EIO; 1028bf89d38fSJeff Mahoney return err; 102979154b1bSChris Mason } 103079154b1bSChris Mason 1031690587d1SChris Mason /* 1032c9b577c0SNikolay Borisov * When btree blocks are allocated the corresponding extents are marked dirty. 1033c9b577c0SNikolay Borisov * This function ensures such extents are persisted on disk for transaction or 1034c9b577c0SNikolay Borisov * log commit. 1035c9b577c0SNikolay Borisov * 1036c9b577c0SNikolay Borisov * @trans: transaction whose dirty pages we'd like to write 1037690587d1SChris Mason */ 103870458a58SNikolay Borisov static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans) 1039d0c803c4SChris Mason { 1040663dfbb0SFilipe Manana int ret; 1041c9b577c0SNikolay Borisov int ret2; 1042c9b577c0SNikolay Borisov struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages; 104370458a58SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1044c9b577c0SNikolay Borisov struct blk_plug plug; 1045663dfbb0SFilipe Manana 1046c9b577c0SNikolay Borisov blk_start_plug(&plug); 1047c9b577c0SNikolay Borisov ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY); 1048c9b577c0SNikolay Borisov blk_finish_plug(&plug); 1049c9b577c0SNikolay Borisov ret2 = btrfs_wait_extents(fs_info, dirty_pages); 1050c9b577c0SNikolay Borisov 105141e7acd3SNikolay Borisov extent_io_tree_release(&trans->transaction->dirty_pages); 1052663dfbb0SFilipe Manana 1053c9b577c0SNikolay Borisov if (ret) 1054663dfbb0SFilipe Manana return ret; 1055c9b577c0SNikolay Borisov else if (ret2) 1056c9b577c0SNikolay Borisov return ret2; 1057c9b577c0SNikolay Borisov else 1058c9b577c0SNikolay Borisov return 0; 1059d0c803c4SChris Mason } 1060d0c803c4SChris Mason 1061d352ac68SChris Mason /* 1062d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 1063d352ac68SChris Mason * 1064d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 1065d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 1066d352ac68SChris Mason * allocation tree. 1067d352ac68SChris Mason * 1068d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 1069d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 1070d352ac68SChris Mason */ 10710b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 107279154b1bSChris Mason struct btrfs_root *root) 107379154b1bSChris Mason { 107479154b1bSChris Mason int ret; 10750b86a832SChris Mason u64 old_root_bytenr; 107686b9f2ecSYan, Zheng u64 old_root_used; 10770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10780b246afaSJeff Mahoney struct btrfs_root *tree_root = fs_info->tree_root; 107979154b1bSChris Mason 108086b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 108156bec294SChris Mason 108279154b1bSChris Mason while (1) { 10830b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 108486b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 1085ea526d18SJosef Bacik old_root_used == btrfs_root_used(&root->root_item)) 108679154b1bSChris Mason break; 108787ef2bb4SChris Mason 10885d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 108979154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 10900b86a832SChris Mason &root->root_key, 10910b86a832SChris Mason &root->root_item); 109249b25e05SJeff Mahoney if (ret) 109349b25e05SJeff Mahoney return ret; 109456bec294SChris Mason 109586b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 1096e7070be1SJosef Bacik } 1097276e680dSYan Zheng 10980b86a832SChris Mason return 0; 10990b86a832SChris Mason } 11000b86a832SChris Mason 1101d352ac68SChris Mason /* 1102d352ac68SChris Mason * update all the cowonly tree roots on disk 110349b25e05SJeff Mahoney * 110449b25e05SJeff Mahoney * The error handling in this function may not be obvious. Any of the 110549b25e05SJeff Mahoney * failures will cause the file system to go offline. We still need 110649b25e05SJeff Mahoney * to clean up the delayed refs. 1107d352ac68SChris Mason */ 11089386d8bcSNikolay Borisov static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) 11090b86a832SChris Mason { 11109386d8bcSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 1111ea526d18SJosef Bacik struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 11121bbc621eSChris Mason struct list_head *io_bgs = &trans->transaction->io_bgs; 11130b86a832SChris Mason struct list_head *next; 111484234f3aSYan Zheng struct extent_buffer *eb; 111556bec294SChris Mason int ret; 111684234f3aSYan Zheng 111784234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 111849b25e05SJeff Mahoney ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 111949b25e05SJeff Mahoney 0, &eb); 112084234f3aSYan Zheng btrfs_tree_unlock(eb); 112184234f3aSYan Zheng free_extent_buffer(eb); 11220b86a832SChris Mason 112349b25e05SJeff Mahoney if (ret) 112449b25e05SJeff Mahoney return ret; 112549b25e05SJeff Mahoney 1126c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 112749b25e05SJeff Mahoney if (ret) 112849b25e05SJeff Mahoney return ret; 112987ef2bb4SChris Mason 1130196c9d8dSDavid Sterba ret = btrfs_run_dev_stats(trans); 1131c16ce190SJosef Bacik if (ret) 1132c16ce190SJosef Bacik return ret; 11332b584c68SDavid Sterba ret = btrfs_run_dev_replace(trans); 1134c16ce190SJosef Bacik if (ret) 1135c16ce190SJosef Bacik return ret; 1136280f8bd2SLu Fengqi ret = btrfs_run_qgroups(trans); 1137c16ce190SJosef Bacik if (ret) 1138c16ce190SJosef Bacik return ret; 1139546adb0dSJan Schmidt 1140bbebb3e0SDavid Sterba ret = btrfs_setup_space_cache(trans); 1141dcdf7f6dSJosef Bacik if (ret) 1142dcdf7f6dSJosef Bacik return ret; 1143dcdf7f6dSJosef Bacik 1144546adb0dSJan Schmidt /* run_qgroups might have added some more refs */ 1145c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1146c16ce190SJosef Bacik if (ret) 1147c16ce190SJosef Bacik return ret; 1148ea526d18SJosef Bacik again: 11490b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 11502ff7e61eSJeff Mahoney struct btrfs_root *root; 11510b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 11520b86a832SChris Mason list_del_init(next); 11530b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 1154e7070be1SJosef Bacik clear_bit(BTRFS_ROOT_DIRTY, &root->state); 115587ef2bb4SChris Mason 11569e351cc8SJosef Bacik if (root != fs_info->extent_root) 11579e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 11589e351cc8SJosef Bacik &trans->transaction->switch_commits); 115949b25e05SJeff Mahoney ret = update_cowonly_root(trans, root); 116049b25e05SJeff Mahoney if (ret) 116149b25e05SJeff Mahoney return ret; 1162c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1163ea526d18SJosef Bacik if (ret) 1164ea526d18SJosef Bacik return ret; 116579154b1bSChris Mason } 1166276e680dSYan Zheng 11671bbc621eSChris Mason while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 11685742d15fSDavid Sterba ret = btrfs_write_dirty_block_groups(trans); 1169ea526d18SJosef Bacik if (ret) 1170ea526d18SJosef Bacik return ret; 1171c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1172ea526d18SJosef Bacik if (ret) 1173ea526d18SJosef Bacik return ret; 1174ea526d18SJosef Bacik } 1175ea526d18SJosef Bacik 1176ea526d18SJosef Bacik if (!list_empty(&fs_info->dirty_cowonly_roots)) 1177ea526d18SJosef Bacik goto again; 1178ea526d18SJosef Bacik 11799e351cc8SJosef Bacik list_add_tail(&fs_info->extent_root->dirty_list, 11809e351cc8SJosef Bacik &trans->transaction->switch_commits); 11819f6cbcbbSDavid Sterba 11829f6cbcbbSDavid Sterba /* Update dev-replace pointer once everything is committed */ 11839f6cbcbbSDavid Sterba fs_info->dev_replace.committed_cursor_left = 11849f6cbcbbSDavid Sterba fs_info->dev_replace.cursor_left_last_write_of_item; 11858dabb742SStefan Behrens 118679154b1bSChris Mason return 0; 118779154b1bSChris Mason } 118879154b1bSChris Mason 1189d352ac68SChris Mason /* 1190d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 1191d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 1192d352ac68SChris Mason * be deleted 1193d352ac68SChris Mason */ 1194cfad392bSJosef Bacik void btrfs_add_dead_root(struct btrfs_root *root) 11955eda7b5eSChris Mason { 11960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 11970b246afaSJeff Mahoney 11980b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1199cfad392bSJosef Bacik if (list_empty(&root->root_list)) 12000b246afaSJeff Mahoney list_add_tail(&root->root_list, &fs_info->dead_roots); 12010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 12025eda7b5eSChris Mason } 12035eda7b5eSChris Mason 1204d352ac68SChris Mason /* 12055d4f98a2SYan Zheng * update all the cowonly tree roots on disk 1206d352ac68SChris Mason */ 12077e4443d9SNikolay Borisov static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) 12080f7d52f4SChris Mason { 12097e4443d9SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 12100f7d52f4SChris Mason struct btrfs_root *gang[8]; 12110f7d52f4SChris Mason int i; 12120f7d52f4SChris Mason int ret; 121354aa1f4dSChris Mason int err = 0; 121454aa1f4dSChris Mason 1215a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 12160f7d52f4SChris Mason while (1) { 12175d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 12185d4f98a2SYan Zheng (void **)gang, 0, 12190f7d52f4SChris Mason ARRAY_SIZE(gang), 12200f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 12210f7d52f4SChris Mason if (ret == 0) 12220f7d52f4SChris Mason break; 12230f7d52f4SChris Mason for (i = 0; i < ret; i++) { 12245b4aacefSJeff Mahoney struct btrfs_root *root = gang[i]; 12255d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 12262619ba1fSChris Mason (unsigned long)root->root_key.objectid, 12270f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 1228a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 122931153d81SYan Zheng 1230e02119d5SChris Mason btrfs_free_log(trans, root); 12315d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 1232e02119d5SChris Mason 123382d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 123482d5902dSLi Zefan 1235f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 123627cdeb70SMiao Xie clear_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1237c7548af6SChris Mason smp_mb__after_atomic(); 1238f1ebcc74SLiu Bo 1239978d910dSYan Zheng if (root->commit_root != root->node) { 12409e351cc8SJosef Bacik list_add_tail(&root->dirty_list, 12419e351cc8SJosef Bacik &trans->transaction->switch_commits); 1242978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 1243978d910dSYan Zheng root->node); 1244978d910dSYan Zheng } 124531153d81SYan Zheng 12465d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 12470f7d52f4SChris Mason &root->root_key, 12480f7d52f4SChris Mason &root->root_item); 1249a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 125054aa1f4dSChris Mason if (err) 125154aa1f4dSChris Mason break; 1252733e03a0SQu Wenruo btrfs_qgroup_free_meta_all_pertrans(root); 12530f7d52f4SChris Mason } 12549f3a7427SChris Mason } 1255a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 125654aa1f4dSChris Mason return err; 12570f7d52f4SChris Mason } 12580f7d52f4SChris Mason 1259d352ac68SChris Mason /* 1260de78b51aSEric Sandeen * defrag a given btree. 1261de78b51aSEric Sandeen * Every leaf in the btree is read and defragged. 1262d352ac68SChris Mason */ 1263de78b51aSEric Sandeen int btrfs_defrag_root(struct btrfs_root *root) 1264e9d0b13bSChris Mason { 1265e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 1266e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 12678929ecfaSYan, Zheng int ret; 1268e9d0b13bSChris Mason 126927cdeb70SMiao Xie if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state)) 1270e9d0b13bSChris Mason return 0; 12718929ecfaSYan, Zheng 12726b80053dSChris Mason while (1) { 12738929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 12748929ecfaSYan, Zheng if (IS_ERR(trans)) 12758929ecfaSYan, Zheng return PTR_ERR(trans); 12768929ecfaSYan, Zheng 1277de78b51aSEric Sandeen ret = btrfs_defrag_leaves(trans, root); 12788929ecfaSYan, Zheng 12793a45bb20SJeff Mahoney btrfs_end_transaction(trans); 12802ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(info); 1281e9d0b13bSChris Mason cond_resched(); 1282e9d0b13bSChris Mason 1283ab8d0fc4SJeff Mahoney if (btrfs_fs_closing(info) || ret != -EAGAIN) 1284e9d0b13bSChris Mason break; 1285210549ebSDavid Sterba 1286ab8d0fc4SJeff Mahoney if (btrfs_defrag_cancelled(info)) { 1287ab8d0fc4SJeff Mahoney btrfs_debug(info, "defrag_root cancelled"); 1288210549ebSDavid Sterba ret = -EAGAIN; 1289210549ebSDavid Sterba break; 1290210549ebSDavid Sterba } 1291e9d0b13bSChris Mason } 129227cdeb70SMiao Xie clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state); 12938929ecfaSYan, Zheng return ret; 1294e9d0b13bSChris Mason } 1295e9d0b13bSChris Mason 1296d352ac68SChris Mason /* 12976426c7adSQu Wenruo * Do all special snapshot related qgroup dirty hack. 12986426c7adSQu Wenruo * 12996426c7adSQu Wenruo * Will do all needed qgroup inherit and dirty hack like switch commit 13006426c7adSQu Wenruo * roots inside one transaction and write all btree into disk, to make 13016426c7adSQu Wenruo * qgroup works. 13026426c7adSQu Wenruo */ 13036426c7adSQu Wenruo static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, 13046426c7adSQu Wenruo struct btrfs_root *src, 13056426c7adSQu Wenruo struct btrfs_root *parent, 13066426c7adSQu Wenruo struct btrfs_qgroup_inherit *inherit, 13076426c7adSQu Wenruo u64 dst_objectid) 13086426c7adSQu Wenruo { 13096426c7adSQu Wenruo struct btrfs_fs_info *fs_info = src->fs_info; 13106426c7adSQu Wenruo int ret; 13116426c7adSQu Wenruo 13126426c7adSQu Wenruo /* 13136426c7adSQu Wenruo * Save some performance in the case that qgroups are not 13146426c7adSQu Wenruo * enabled. If this check races with the ioctl, rescan will 13156426c7adSQu Wenruo * kick in anyway. 13166426c7adSQu Wenruo */ 13179ea6e2b5SDavid Sterba if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) 13186426c7adSQu Wenruo return 0; 13196426c7adSQu Wenruo 13206426c7adSQu Wenruo /* 132152042d8eSAndrea Gelmini * Ensure dirty @src will be committed. Or, after coming 13224d31778aSQu Wenruo * commit_fs_roots() and switch_commit_roots(), any dirty but not 13234d31778aSQu Wenruo * recorded root will never be updated again, causing an outdated root 13244d31778aSQu Wenruo * item. 13254d31778aSQu Wenruo */ 13264d31778aSQu Wenruo record_root_in_trans(trans, src, 1); 13274d31778aSQu Wenruo 13284d31778aSQu Wenruo /* 13296426c7adSQu Wenruo * We are going to commit transaction, see btrfs_commit_transaction() 13306426c7adSQu Wenruo * comment for reason locking tree_log_mutex 13316426c7adSQu Wenruo */ 13326426c7adSQu Wenruo mutex_lock(&fs_info->tree_log_mutex); 13336426c7adSQu Wenruo 13347e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 13356426c7adSQu Wenruo if (ret) 13366426c7adSQu Wenruo goto out; 1337460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 13386426c7adSQu Wenruo if (ret < 0) 13396426c7adSQu Wenruo goto out; 13406426c7adSQu Wenruo 13416426c7adSQu Wenruo /* Now qgroup are all updated, we can inherit it to new qgroups */ 1342a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid, 13436426c7adSQu Wenruo inherit); 13446426c7adSQu Wenruo if (ret < 0) 13456426c7adSQu Wenruo goto out; 13466426c7adSQu Wenruo 13476426c7adSQu Wenruo /* 13486426c7adSQu Wenruo * Now we do a simplified commit transaction, which will: 13496426c7adSQu Wenruo * 1) commit all subvolume and extent tree 13506426c7adSQu Wenruo * To ensure all subvolume and extent tree have a valid 13516426c7adSQu Wenruo * commit_root to accounting later insert_dir_item() 13526426c7adSQu Wenruo * 2) write all btree blocks onto disk 13536426c7adSQu Wenruo * This is to make sure later btree modification will be cowed 13546426c7adSQu Wenruo * Or commit_root can be populated and cause wrong qgroup numbers 13556426c7adSQu Wenruo * In this simplified commit, we don't really care about other trees 13566426c7adSQu Wenruo * like chunk and root tree, as they won't affect qgroup. 13576426c7adSQu Wenruo * And we don't write super to avoid half committed status. 13586426c7adSQu Wenruo */ 13599386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 13606426c7adSQu Wenruo if (ret) 13616426c7adSQu Wenruo goto out; 136216916a88SNikolay Borisov switch_commit_roots(trans->transaction); 136370458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 13646426c7adSQu Wenruo if (ret) 1365f7af3934SDavid Sterba btrfs_handle_fs_error(fs_info, ret, 13666426c7adSQu Wenruo "Error while writing out transaction for qgroup"); 13676426c7adSQu Wenruo 13686426c7adSQu Wenruo out: 13696426c7adSQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 13706426c7adSQu Wenruo 13716426c7adSQu Wenruo /* 13726426c7adSQu Wenruo * Force parent root to be updated, as we recorded it before so its 13736426c7adSQu Wenruo * last_trans == cur_transid. 13746426c7adSQu Wenruo * Or it won't be committed again onto disk after later 13756426c7adSQu Wenruo * insert_dir_item() 13766426c7adSQu Wenruo */ 13776426c7adSQu Wenruo if (!ret) 13786426c7adSQu Wenruo record_root_in_trans(trans, parent, 1); 13796426c7adSQu Wenruo return ret; 13806426c7adSQu Wenruo } 13816426c7adSQu Wenruo 13826426c7adSQu Wenruo /* 1383d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 1384aec8030aSMiao Xie * transaction commit. This does the actual creation. 1385aec8030aSMiao Xie * 1386aec8030aSMiao Xie * Note: 1387aec8030aSMiao Xie * If the error which may affect the commitment of the current transaction 1388aec8030aSMiao Xie * happens, we should return the error number. If the error which just affect 1389aec8030aSMiao Xie * the creation of the pending snapshots, just return 0. 1390d352ac68SChris Mason */ 139180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 13923063d29fSChris Mason struct btrfs_pending_snapshot *pending) 13933063d29fSChris Mason { 139408d50ca3SNikolay Borisov 139508d50ca3SNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 13963063d29fSChris Mason struct btrfs_key key; 139780b6794dSChris Mason struct btrfs_root_item *new_root_item; 13983063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 13993063d29fSChris Mason struct btrfs_root *root = pending->root; 14006bdb72deSSage Weil struct btrfs_root *parent_root; 140198c9942aSLiu Bo struct btrfs_block_rsv *rsv; 14026bdb72deSSage Weil struct inode *parent_inode; 140342874b3dSMiao Xie struct btrfs_path *path; 140442874b3dSMiao Xie struct btrfs_dir_item *dir_item; 1405a22285a6SYan, Zheng struct dentry *dentry; 14063063d29fSChris Mason struct extent_buffer *tmp; 1407925baeddSChris Mason struct extent_buffer *old; 140895582b00SDeepa Dinamani struct timespec64 cur_time; 1409aec8030aSMiao Xie int ret = 0; 1410d68fc57bSYan, Zheng u64 to_reserve = 0; 14116bdb72deSSage Weil u64 index = 0; 1412a22285a6SYan, Zheng u64 objectid; 1413b83cc969SLi Zefan u64 root_flags; 14148ea05e3aSAlexander Block uuid_le new_uuid; 14153063d29fSChris Mason 14168546b570SDavid Sterba ASSERT(pending->path); 14178546b570SDavid Sterba path = pending->path; 141842874b3dSMiao Xie 1419b0c0ea63SDavid Sterba ASSERT(pending->root_item); 1420b0c0ea63SDavid Sterba new_root_item = pending->root_item; 1421a22285a6SYan, Zheng 1422aec8030aSMiao Xie pending->error = btrfs_find_free_objectid(tree_root, &objectid); 1423aec8030aSMiao Xie if (pending->error) 14246fa9700eSMiao Xie goto no_free_objectid; 14253063d29fSChris Mason 1426d6726335SQu Wenruo /* 1427d6726335SQu Wenruo * Make qgroup to skip current new snapshot's qgroupid, as it is 1428d6726335SQu Wenruo * accounted by later btrfs_qgroup_inherit(). 1429d6726335SQu Wenruo */ 1430d6726335SQu Wenruo btrfs_set_skip_qgroup(trans, objectid); 1431d6726335SQu Wenruo 1432147d256eSZhaolei btrfs_reloc_pre_snapshot(pending, &to_reserve); 1433d68fc57bSYan, Zheng 1434d68fc57bSYan, Zheng if (to_reserve > 0) { 1435aec8030aSMiao Xie pending->error = btrfs_block_rsv_add(root, 1436aec8030aSMiao Xie &pending->block_rsv, 143708e007d2SMiao Xie to_reserve, 143808e007d2SMiao Xie BTRFS_RESERVE_NO_FLUSH); 1439aec8030aSMiao Xie if (pending->error) 1440d6726335SQu Wenruo goto clear_skip_qgroup; 1441d68fc57bSYan, Zheng } 1442d68fc57bSYan, Zheng 14433063d29fSChris Mason key.objectid = objectid; 1444a22285a6SYan, Zheng key.offset = (u64)-1; 1445a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 14463063d29fSChris Mason 14476fa9700eSMiao Xie rsv = trans->block_rsv; 1448a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 14492382c5ccSLiu Bo trans->bytes_reserved = trans->block_rsv->reserved; 14500b246afaSJeff Mahoney trace_btrfs_space_reservation(fs_info, "transaction", 145188d3a5aaSJosef Bacik trans->transid, 145288d3a5aaSJosef Bacik trans->bytes_reserved, 1); 1453a22285a6SYan, Zheng dentry = pending->dentry; 1454e9662f70SMiao Xie parent_inode = pending->dir; 1455a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 14566426c7adSQu Wenruo record_root_in_trans(trans, parent_root, 0); 1457a22285a6SYan, Zheng 1458c2050a45SDeepa Dinamani cur_time = current_time(parent_inode); 145904b285f3SDeepa Dinamani 14606bdb72deSSage Weil /* 14616bdb72deSSage Weil * insert the directory item 14626bdb72deSSage Weil */ 1463877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index); 146449b25e05SJeff Mahoney BUG_ON(ret); /* -ENOMEM */ 146542874b3dSMiao Xie 146642874b3dSMiao Xie /* check if there is a file/dir which has the same name. */ 146742874b3dSMiao Xie dir_item = btrfs_lookup_dir_item(NULL, parent_root, path, 14684a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), 146942874b3dSMiao Xie dentry->d_name.name, 147042874b3dSMiao Xie dentry->d_name.len, 0); 147142874b3dSMiao Xie if (dir_item != NULL && !IS_ERR(dir_item)) { 1472fe66a05aSChris Mason pending->error = -EEXIST; 1473aec8030aSMiao Xie goto dir_item_existed; 147442874b3dSMiao Xie } else if (IS_ERR(dir_item)) { 147542874b3dSMiao Xie ret = PTR_ERR(dir_item); 147666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14778732d44fSMiao Xie goto fail; 147879787eaaSJeff Mahoney } 147942874b3dSMiao Xie btrfs_release_path(path); 14806bdb72deSSage Weil 1481e999376fSChris Mason /* 1482e999376fSChris Mason * pull in the delayed directory update 1483e999376fSChris Mason * and the delayed inode item 1484e999376fSChris Mason * otherwise we corrupt the FS during 1485e999376fSChris Mason * snapshot 1486e999376fSChris Mason */ 1487e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 14888732d44fSMiao Xie if (ret) { /* Transaction aborted */ 148966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 14908732d44fSMiao Xie goto fail; 14918732d44fSMiao Xie } 1492e999376fSChris Mason 14936426c7adSQu Wenruo record_root_in_trans(trans, root, 0); 14946bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 14956bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 149608fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 14976bdb72deSSage Weil 1498b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 1499b83cc969SLi Zefan if (pending->readonly) 1500b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 1501b83cc969SLi Zefan else 1502b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 1503b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 1504b83cc969SLi Zefan 15058ea05e3aSAlexander Block btrfs_set_root_generation_v2(new_root_item, 15068ea05e3aSAlexander Block trans->transid); 15078ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 15088ea05e3aSAlexander Block memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 15098ea05e3aSAlexander Block memcpy(new_root_item->parent_uuid, root->root_item.uuid, 15108ea05e3aSAlexander Block BTRFS_UUID_SIZE); 151170023da2SStefan Behrens if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) { 151270023da2SStefan Behrens memset(new_root_item->received_uuid, 0, 151370023da2SStefan Behrens sizeof(new_root_item->received_uuid)); 15148ea05e3aSAlexander Block memset(&new_root_item->stime, 0, sizeof(new_root_item->stime)); 15158ea05e3aSAlexander Block memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime)); 15168ea05e3aSAlexander Block btrfs_set_root_stransid(new_root_item, 0); 15178ea05e3aSAlexander Block btrfs_set_root_rtransid(new_root_item, 0); 151870023da2SStefan Behrens } 15193cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec); 15203cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec); 152170023da2SStefan Behrens btrfs_set_root_otransid(new_root_item, trans->transid); 15228ea05e3aSAlexander Block 1523925baeddSChris Mason old = btrfs_lock_root_node(root); 152449b25e05SJeff Mahoney ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 152579787eaaSJeff Mahoney if (ret) { 152679787eaaSJeff Mahoney btrfs_tree_unlock(old); 152779787eaaSJeff Mahoney free_extent_buffer(old); 152866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15298732d44fSMiao Xie goto fail; 153079787eaaSJeff Mahoney } 153149b25e05SJeff Mahoney 15328bead258SDavid Sterba btrfs_set_lock_blocking_write(old); 15333063d29fSChris Mason 153449b25e05SJeff Mahoney ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 153579787eaaSJeff Mahoney /* clean up in any case */ 1536925baeddSChris Mason btrfs_tree_unlock(old); 1537925baeddSChris Mason free_extent_buffer(old); 15388732d44fSMiao Xie if (ret) { 153966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15408732d44fSMiao Xie goto fail; 15418732d44fSMiao Xie } 1542f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 154327cdeb70SMiao Xie set_bit(BTRFS_ROOT_FORCE_COW, &root->state); 1544f1ebcc74SLiu Bo smp_wmb(); 1545f1ebcc74SLiu Bo 15465d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 1547a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 1548a22285a6SYan, Zheng key.offset = trans->transid; 1549a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 1550925baeddSChris Mason btrfs_tree_unlock(tmp); 15513063d29fSChris Mason free_extent_buffer(tmp); 15528732d44fSMiao Xie if (ret) { 155366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15548732d44fSMiao Xie goto fail; 15558732d44fSMiao Xie } 15560660b5afSChris Mason 1557a22285a6SYan, Zheng /* 1558a22285a6SYan, Zheng * insert root back/forward references 1559a22285a6SYan, Zheng */ 15606025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, 1561a22285a6SYan, Zheng parent_root->root_key.objectid, 15624a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(parent_inode)), index, 1563a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 15648732d44fSMiao Xie if (ret) { 156566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15668732d44fSMiao Xie goto fail; 15678732d44fSMiao Xie } 1568a22285a6SYan, Zheng 1569a22285a6SYan, Zheng key.offset = (u64)-1; 15700b246afaSJeff Mahoney pending->snap = btrfs_read_fs_root_no_name(fs_info, &key); 157179787eaaSJeff Mahoney if (IS_ERR(pending->snap)) { 157279787eaaSJeff Mahoney ret = PTR_ERR(pending->snap); 157366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15748732d44fSMiao Xie goto fail; 157579787eaaSJeff Mahoney } 1576d68fc57bSYan, Zheng 157749b25e05SJeff Mahoney ret = btrfs_reloc_post_snapshot(trans, pending); 15788732d44fSMiao Xie if (ret) { 157966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15808732d44fSMiao Xie goto fail; 15818732d44fSMiao Xie } 1582361048f5SMiao Xie 1583c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 15848732d44fSMiao Xie if (ret) { 158566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 15868732d44fSMiao Xie goto fail; 15878732d44fSMiao Xie } 158842874b3dSMiao Xie 15896426c7adSQu Wenruo /* 15906426c7adSQu Wenruo * Do special qgroup accounting for snapshot, as we do some qgroup 15916426c7adSQu Wenruo * snapshot hack to do fast snapshot. 15926426c7adSQu Wenruo * To co-operate with that hack, we do hack again. 15936426c7adSQu Wenruo * Or snapshot will be greatly slowed down by a subtree qgroup rescan 15946426c7adSQu Wenruo */ 15956426c7adSQu Wenruo ret = qgroup_account_snapshot(trans, root, parent_root, 15966426c7adSQu Wenruo pending->inherit, objectid); 15976426c7adSQu Wenruo if (ret < 0) 15986426c7adSQu Wenruo goto fail; 15996426c7adSQu Wenruo 1600684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, dentry->d_name.name, 1601684572dfSLu Fengqi dentry->d_name.len, BTRFS_I(parent_inode), 1602684572dfSLu Fengqi &key, BTRFS_FT_DIR, index); 160342874b3dSMiao Xie /* We have check then name at the beginning, so it is impossible. */ 16049c52057cSChris Mason BUG_ON(ret == -EEXIST || ret == -EOVERFLOW); 16058732d44fSMiao Xie if (ret) { 160666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 16078732d44fSMiao Xie goto fail; 16088732d44fSMiao Xie } 160942874b3dSMiao Xie 16106ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size + 161142874b3dSMiao Xie dentry->d_name.len * 2); 161204b285f3SDeepa Dinamani parent_inode->i_mtime = parent_inode->i_ctime = 1613c2050a45SDeepa Dinamani current_time(parent_inode); 1614be6aef60SJosef Bacik ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode); 1615dd5f9615SStefan Behrens if (ret) { 161666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1617dd5f9615SStefan Behrens goto fail; 1618dd5f9615SStefan Behrens } 1619cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL, 1620cdb345a8SLu Fengqi objectid); 1621dd5f9615SStefan Behrens if (ret) { 162266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1623dd5f9615SStefan Behrens goto fail; 1624dd5f9615SStefan Behrens } 1625dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) { 1626cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid, 1627dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 1628dd5f9615SStefan Behrens objectid); 1629dd5f9615SStefan Behrens if (ret && ret != -EEXIST) { 163066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1631dd5f9615SStefan Behrens goto fail; 1632dd5f9615SStefan Behrens } 1633dd5f9615SStefan Behrens } 1634d6726335SQu Wenruo 1635c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 1636d6726335SQu Wenruo if (ret) { 163766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 1638d6726335SQu Wenruo goto fail; 1639d6726335SQu Wenruo } 1640d6726335SQu Wenruo 16413063d29fSChris Mason fail: 1642aec8030aSMiao Xie pending->error = ret; 1643aec8030aSMiao Xie dir_item_existed: 164498c9942aSLiu Bo trans->block_rsv = rsv; 16452382c5ccSLiu Bo trans->bytes_reserved = 0; 1646d6726335SQu Wenruo clear_skip_qgroup: 1647d6726335SQu Wenruo btrfs_clear_skip_qgroup(trans); 16486fa9700eSMiao Xie no_free_objectid: 16496fa9700eSMiao Xie kfree(new_root_item); 1650b0c0ea63SDavid Sterba pending->root_item = NULL; 165142874b3dSMiao Xie btrfs_free_path(path); 16528546b570SDavid Sterba pending->path = NULL; 16538546b570SDavid Sterba 165449b25e05SJeff Mahoney return ret; 16553063d29fSChris Mason } 16563063d29fSChris Mason 1657d352ac68SChris Mason /* 1658d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1659d352ac68SChris Mason */ 166008d50ca3SNikolay Borisov static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans) 16613063d29fSChris Mason { 1662aec8030aSMiao Xie struct btrfs_pending_snapshot *pending, *next; 16633063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 1664aec8030aSMiao Xie int ret = 0; 16653de4586cSChris Mason 1666aec8030aSMiao Xie list_for_each_entry_safe(pending, next, head, list) { 1667aec8030aSMiao Xie list_del(&pending->list); 166808d50ca3SNikolay Borisov ret = create_pending_snapshot(trans, pending); 1669aec8030aSMiao Xie if (ret) 1670aec8030aSMiao Xie break; 1671aec8030aSMiao Xie } 1672aec8030aSMiao Xie return ret; 16733de4586cSChris Mason } 16743de4586cSChris Mason 16752ff7e61eSJeff Mahoney static void update_super_roots(struct btrfs_fs_info *fs_info) 16765d4f98a2SYan Zheng { 16775d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16785d4f98a2SYan Zheng struct btrfs_super_block *super; 16795d4f98a2SYan Zheng 16800b246afaSJeff Mahoney super = fs_info->super_copy; 16815d4f98a2SYan Zheng 16820b246afaSJeff Mahoney root_item = &fs_info->chunk_root->root_item; 1683093e037cSDavid Sterba super->chunk_root = root_item->bytenr; 1684093e037cSDavid Sterba super->chunk_root_generation = root_item->generation; 1685093e037cSDavid Sterba super->chunk_root_level = root_item->level; 16865d4f98a2SYan Zheng 16870b246afaSJeff Mahoney root_item = &fs_info->tree_root->root_item; 1688093e037cSDavid Sterba super->root = root_item->bytenr; 1689093e037cSDavid Sterba super->generation = root_item->generation; 1690093e037cSDavid Sterba super->root_level = root_item->level; 16910b246afaSJeff Mahoney if (btrfs_test_opt(fs_info, SPACE_CACHE)) 1692093e037cSDavid Sterba super->cache_generation = root_item->generation; 16930b246afaSJeff Mahoney if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags)) 1694093e037cSDavid Sterba super->uuid_tree_generation = root_item->generation; 16955d4f98a2SYan Zheng } 16965d4f98a2SYan Zheng 1697f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1698f36f3042SChris Mason { 16994a9d8bdeSMiao Xie struct btrfs_transaction *trans; 1700f36f3042SChris Mason int ret = 0; 17014a9d8bdeSMiao Xie 1702a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17034a9d8bdeSMiao Xie trans = info->running_transaction; 17044a9d8bdeSMiao Xie if (trans) 17054a9d8bdeSMiao Xie ret = (trans->state >= TRANS_STATE_COMMIT_START); 1706a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1707f36f3042SChris Mason return ret; 1708f36f3042SChris Mason } 1709f36f3042SChris Mason 17108929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 17118929ecfaSYan, Zheng { 17124a9d8bdeSMiao Xie struct btrfs_transaction *trans; 17138929ecfaSYan, Zheng int ret = 0; 17144a9d8bdeSMiao Xie 1715a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 17164a9d8bdeSMiao Xie trans = info->running_transaction; 17174a9d8bdeSMiao Xie if (trans) 17184a9d8bdeSMiao Xie ret = is_transaction_blocked(trans); 1719a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 17208929ecfaSYan, Zheng return ret; 17218929ecfaSYan, Zheng } 17228929ecfaSYan, Zheng 1723bb9c12c9SSage Weil /* 1724bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1725bb9c12c9SSage Weil * transaction joins 1726bb9c12c9SSage Weil */ 17272ff7e61eSJeff Mahoney static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info, 1728bb9c12c9SSage Weil struct btrfs_transaction *trans) 1729bb9c12c9SSage Weil { 17302ff7e61eSJeff Mahoney wait_event(fs_info->transaction_blocked_wait, 17312ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_COMMIT_START || trans->aborted); 1732bb9c12c9SSage Weil } 1733bb9c12c9SSage Weil 1734bb9c12c9SSage Weil /* 1735bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1736bb9c12c9SSage Weil * caller holds ref. 1737bb9c12c9SSage Weil */ 17382ff7e61eSJeff Mahoney static void wait_current_trans_commit_start_and_unblock( 17392ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info, 1740bb9c12c9SSage Weil struct btrfs_transaction *trans) 1741bb9c12c9SSage Weil { 17422ff7e61eSJeff Mahoney wait_event(fs_info->transaction_wait, 17432ff7e61eSJeff Mahoney trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted); 1744bb9c12c9SSage Weil } 1745bb9c12c9SSage Weil 1746bb9c12c9SSage Weil /* 1747bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1748bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1749bb9c12c9SSage Weil */ 1750bb9c12c9SSage Weil struct btrfs_async_commit { 1751bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 17527892b5afSMiao Xie struct work_struct work; 1753bb9c12c9SSage Weil }; 1754bb9c12c9SSage Weil 1755bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1756bb9c12c9SSage Weil { 1757bb9c12c9SSage Weil struct btrfs_async_commit *ac = 17587892b5afSMiao Xie container_of(work, struct btrfs_async_commit, work); 1759bb9c12c9SSage Weil 17606fc4e354SSage Weil /* 17616fc4e354SSage Weil * We've got freeze protection passed with the transaction. 17626fc4e354SSage Weil * Tell lockdep about it. 17636fc4e354SSage Weil */ 1764b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 17653a45bb20SJeff Mahoney __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS); 17666fc4e354SSage Weil 1767e209db7aSSage Weil current->journal_info = ac->newtrans; 1768e209db7aSSage Weil 17693a45bb20SJeff Mahoney btrfs_commit_transaction(ac->newtrans); 1770bb9c12c9SSage Weil kfree(ac); 1771bb9c12c9SSage Weil } 1772bb9c12c9SSage Weil 1773bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1774bb9c12c9SSage Weil int wait_for_unblock) 1775bb9c12c9SSage Weil { 17763a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 1777bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1778bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1779bb9c12c9SSage Weil 1780bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1781db5b493aSTsutomu Itoh if (!ac) 1782db5b493aSTsutomu Itoh return -ENOMEM; 1783bb9c12c9SSage Weil 17847892b5afSMiao Xie INIT_WORK(&ac->work, do_async_commit); 17853a45bb20SJeff Mahoney ac->newtrans = btrfs_join_transaction(trans->root); 17863612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 17873612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 17883612b495STsutomu Itoh kfree(ac); 17893612b495STsutomu Itoh return err; 17903612b495STsutomu Itoh } 1791bb9c12c9SSage Weil 1792bb9c12c9SSage Weil /* take transaction reference */ 1793bb9c12c9SSage Weil cur_trans = trans->transaction; 17949b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 1795bb9c12c9SSage Weil 17963a45bb20SJeff Mahoney btrfs_end_transaction(trans); 17976fc4e354SSage Weil 17986fc4e354SSage Weil /* 17996fc4e354SSage Weil * Tell lockdep we've released the freeze rwsem, since the 18006fc4e354SSage Weil * async commit thread will be the one to unlock it. 18016fc4e354SSage Weil */ 1802b1a06a4bSLiu Bo if (ac->newtrans->type & __TRANS_FREEZABLE) 18030b246afaSJeff Mahoney __sb_writers_release(fs_info->sb, SB_FREEZE_FS); 18046fc4e354SSage Weil 18057892b5afSMiao Xie schedule_work(&ac->work); 1806bb9c12c9SSage Weil 1807bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1808bb9c12c9SSage Weil if (wait_for_unblock) 18092ff7e61eSJeff Mahoney wait_current_trans_commit_start_and_unblock(fs_info, cur_trans); 1810bb9c12c9SSage Weil else 18112ff7e61eSJeff Mahoney wait_current_trans_commit_start(fs_info, cur_trans); 1812bb9c12c9SSage Weil 181338e88054SSage Weil if (current->journal_info == trans) 181438e88054SSage Weil current->journal_info = NULL; 181538e88054SSage Weil 1816724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1817bb9c12c9SSage Weil return 0; 1818bb9c12c9SSage Weil } 1819bb9c12c9SSage Weil 182049b25e05SJeff Mahoney 182197cb39bbSNikolay Borisov static void cleanup_transaction(struct btrfs_trans_handle *trans, int err) 182249b25e05SJeff Mahoney { 182397cb39bbSNikolay Borisov struct btrfs_fs_info *fs_info = trans->fs_info; 182449b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 182549b25e05SJeff Mahoney 1826b50fff81SDavid Sterba WARN_ON(refcount_read(&trans->use_count) > 1); 182749b25e05SJeff Mahoney 182866642832SJeff Mahoney btrfs_abort_transaction(trans, err); 18297b8b92afSJosef Bacik 18300b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 183166b6135bSLiu Bo 183225d8c284SMiao Xie /* 183325d8c284SMiao Xie * If the transaction is removed from the list, it means this 183425d8c284SMiao Xie * transaction has been committed successfully, so it is impossible 183525d8c284SMiao Xie * to call the cleanup function. 183625d8c284SMiao Xie */ 183725d8c284SMiao Xie BUG_ON(list_empty(&cur_trans->list)); 183866b6135bSLiu Bo 183949b25e05SJeff Mahoney list_del_init(&cur_trans->list); 18400b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) { 18414a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 18420b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 1843f094ac32SLiu Bo wait_event(cur_trans->writer_wait, 1844f094ac32SLiu Bo atomic_read(&cur_trans->num_writers) == 1); 1845f094ac32SLiu Bo 18460b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 1847d7096fc3SJosef Bacik } 18480b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 184949b25e05SJeff Mahoney 18502ff7e61eSJeff Mahoney btrfs_cleanup_one_transaction(trans->transaction, fs_info); 185149b25e05SJeff Mahoney 18520b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 18530b246afaSJeff Mahoney if (cur_trans == fs_info->running_transaction) 18540b246afaSJeff Mahoney fs_info->running_transaction = NULL; 18550b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 18564a9d8bdeSMiao Xie 1857e0228285SJosef Bacik if (trans->type & __TRANS_FREEZABLE) 18580b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 1859724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 1860724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 186149b25e05SJeff Mahoney 186297cb39bbSNikolay Borisov trace_btrfs_transaction_commit(trans->root); 186349b25e05SJeff Mahoney 186449b25e05SJeff Mahoney if (current->journal_info == trans) 186549b25e05SJeff Mahoney current->journal_info = NULL; 18660b246afaSJeff Mahoney btrfs_scrub_cancel(fs_info); 186749b25e05SJeff Mahoney 186849b25e05SJeff Mahoney kmem_cache_free(btrfs_trans_handle_cachep, trans); 186949b25e05SJeff Mahoney } 187049b25e05SJeff Mahoney 1871c7cc64a9SDavid Sterba /* 1872c7cc64a9SDavid Sterba * Release reserved delayed ref space of all pending block groups of the 1873c7cc64a9SDavid Sterba * transaction and remove them from the list 1874c7cc64a9SDavid Sterba */ 1875c7cc64a9SDavid Sterba static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) 1876c7cc64a9SDavid Sterba { 1877c7cc64a9SDavid Sterba struct btrfs_fs_info *fs_info = trans->fs_info; 1878c7cc64a9SDavid Sterba struct btrfs_block_group_cache *block_group, *tmp; 1879c7cc64a9SDavid Sterba 1880c7cc64a9SDavid Sterba list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { 1881c7cc64a9SDavid Sterba btrfs_delayed_refs_rsv_release(fs_info, 1); 1882c7cc64a9SDavid Sterba list_del_init(&block_group->bg_list); 1883c7cc64a9SDavid Sterba } 1884c7cc64a9SDavid Sterba } 1885c7cc64a9SDavid Sterba 1886609e804dSFilipe Manana static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans) 188782436617SMiao Xie { 1888609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1889609e804dSFilipe Manana 1890ce8ea7ccSJosef Bacik /* 1891ce8ea7ccSJosef Bacik * We use writeback_inodes_sb here because if we used 1892ce8ea7ccSJosef Bacik * btrfs_start_delalloc_roots we would deadlock with fs freeze. 1893ce8ea7ccSJosef Bacik * Currently are holding the fs freeze lock, if we do an async flush 1894ce8ea7ccSJosef Bacik * we'll do btrfs_join_transaction() and deadlock because we need to 1895ce8ea7ccSJosef Bacik * wait for the fs freeze lock. Using the direct flushing we benefit 1896ce8ea7ccSJosef Bacik * from already being in a transaction and our join_transaction doesn't 1897ce8ea7ccSJosef Bacik * have to re-take the fs freeze lock. 1898ce8ea7ccSJosef Bacik */ 1899609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 1900ce8ea7ccSJosef Bacik writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC); 1901609e804dSFilipe Manana } else { 1902609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1903609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1904609e804dSFilipe Manana 1905609e804dSFilipe Manana /* 1906609e804dSFilipe Manana * Flush dellaloc for any root that is going to be snapshotted. 1907609e804dSFilipe Manana * This is done to avoid a corrupted version of files, in the 1908609e804dSFilipe Manana * snapshots, that had both buffered and direct IO writes (even 1909609e804dSFilipe Manana * if they were done sequentially) due to an unordered update of 1910609e804dSFilipe Manana * the inode's size on disk. 1911609e804dSFilipe Manana */ 1912609e804dSFilipe Manana list_for_each_entry(pending, head, list) { 1913609e804dSFilipe Manana int ret; 1914609e804dSFilipe Manana 1915609e804dSFilipe Manana ret = btrfs_start_delalloc_snapshot(pending->root); 1916609e804dSFilipe Manana if (ret) 1917609e804dSFilipe Manana return ret; 1918609e804dSFilipe Manana } 1919609e804dSFilipe Manana } 192082436617SMiao Xie return 0; 192182436617SMiao Xie } 192282436617SMiao Xie 1923609e804dSFilipe Manana static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans) 192482436617SMiao Xie { 1925609e804dSFilipe Manana struct btrfs_fs_info *fs_info = trans->fs_info; 1926609e804dSFilipe Manana 1927609e804dSFilipe Manana if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) { 19286374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1929609e804dSFilipe Manana } else { 1930609e804dSFilipe Manana struct btrfs_pending_snapshot *pending; 1931609e804dSFilipe Manana struct list_head *head = &trans->transaction->pending_snapshots; 1932609e804dSFilipe Manana 1933609e804dSFilipe Manana /* 1934609e804dSFilipe Manana * Wait for any dellaloc that we started previously for the roots 1935609e804dSFilipe Manana * that are going to be snapshotted. This is to avoid a corrupted 1936609e804dSFilipe Manana * version of files in the snapshots that had both buffered and 1937609e804dSFilipe Manana * direct IO writes (even if they were done sequentially). 1938609e804dSFilipe Manana */ 1939609e804dSFilipe Manana list_for_each_entry(pending, head, list) 1940609e804dSFilipe Manana btrfs_wait_ordered_extents(pending->root, 1941609e804dSFilipe Manana U64_MAX, 0, U64_MAX); 1942609e804dSFilipe Manana } 194382436617SMiao Xie } 194482436617SMiao Xie 19453a45bb20SJeff Mahoney int btrfs_commit_transaction(struct btrfs_trans_handle *trans) 194679154b1bSChris Mason { 19473a45bb20SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 194849b25e05SJeff Mahoney struct btrfs_transaction *cur_trans = trans->transaction; 19498fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 195025287e0aSMiao Xie int ret; 195179154b1bSChris Mason 1952*35b814f3SNikolay Borisov ASSERT(refcount_read(&trans->use_count) == 1); 1953*35b814f3SNikolay Borisov 19548d25a086SMiao Xie /* Stop the commit early if ->aborted is set */ 195520c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 195625287e0aSMiao Xie ret = cur_trans->aborted; 19573a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1958e4a2bcacSJosef Bacik return ret; 195925287e0aSMiao Xie } 196049b25e05SJeff Mahoney 1961f45c752bSJosef Bacik btrfs_trans_release_metadata(trans); 1962f45c752bSJosef Bacik trans->block_rsv = NULL; 1963f45c752bSJosef Bacik 196456bec294SChris Mason /* make a pass through all the delayed refs we have so far 196556bec294SChris Mason * any runnings procs may add more while we are here 196656bec294SChris Mason */ 1967c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1968e4a2bcacSJosef Bacik if (ret) { 19693a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1970e4a2bcacSJosef Bacik return ret; 1971e4a2bcacSJosef Bacik } 197256bec294SChris Mason 1973b7ec40d7SChris Mason cur_trans = trans->transaction; 197449b25e05SJeff Mahoney 197556bec294SChris Mason /* 197656bec294SChris Mason * set the flushing flag so procs in this transaction have to 197756bec294SChris Mason * start sending their work down. 197856bec294SChris Mason */ 1979b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 19801be41b78SJosef Bacik smp_wmb(); 198156bec294SChris Mason 19826c686b35SNikolay Borisov btrfs_create_pending_block_groups(trans); 1983ea658badSJosef Bacik 1984c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, 0); 1985e4a2bcacSJosef Bacik if (ret) { 19863a45bb20SJeff Mahoney btrfs_end_transaction(trans); 1987e4a2bcacSJosef Bacik return ret; 1988e4a2bcacSJosef Bacik } 198956bec294SChris Mason 19903204d33cSJosef Bacik if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) { 19911bbc621eSChris Mason int run_it = 0; 19921bbc621eSChris Mason 19931bbc621eSChris Mason /* this mutex is also taken before trying to set 19941bbc621eSChris Mason * block groups readonly. We need to make sure 19951bbc621eSChris Mason * that nobody has set a block group readonly 19961bbc621eSChris Mason * after a extents from that block group have been 19971bbc621eSChris Mason * allocated for cache files. btrfs_set_block_group_ro 19981bbc621eSChris Mason * will wait for the transaction to commit if it 19993204d33cSJosef Bacik * finds BTRFS_TRANS_DIRTY_BG_RUN set. 20001bbc621eSChris Mason * 20013204d33cSJosef Bacik * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure 20023204d33cSJosef Bacik * only one process starts all the block group IO. It wouldn't 20031bbc621eSChris Mason * hurt to have more than one go through, but there's no 20041bbc621eSChris Mason * real advantage to it either. 20051bbc621eSChris Mason */ 20060b246afaSJeff Mahoney mutex_lock(&fs_info->ro_block_group_mutex); 20073204d33cSJosef Bacik if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 20083204d33cSJosef Bacik &cur_trans->flags)) 20091bbc621eSChris Mason run_it = 1; 20100b246afaSJeff Mahoney mutex_unlock(&fs_info->ro_block_group_mutex); 20111bbc621eSChris Mason 2012f9cacae3SNikolay Borisov if (run_it) { 201321217054SNikolay Borisov ret = btrfs_start_dirty_block_groups(trans); 20141bbc621eSChris Mason if (ret) { 20153a45bb20SJeff Mahoney btrfs_end_transaction(trans); 20161bbc621eSChris Mason return ret; 20171bbc621eSChris Mason } 2018f9cacae3SNikolay Borisov } 2019f9cacae3SNikolay Borisov } 20201bbc621eSChris Mason 20210b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20224a9d8bdeSMiao Xie if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 20230b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 20249b64f57dSElena Reshetova refcount_inc(&cur_trans->use_count); 20253a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 2026ccd467d6SChris Mason 20272ff7e61eSJeff Mahoney wait_for_commit(cur_trans); 202815ee9bc7SJosef Bacik 2029b4924a0fSLiu Bo if (unlikely(cur_trans->aborted)) 2030b4924a0fSLiu Bo ret = cur_trans->aborted; 2031b4924a0fSLiu Bo 2032724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 203315ee9bc7SJosef Bacik 203449b25e05SJeff Mahoney return ret; 203579154b1bSChris Mason } 20364313b399SChris Mason 20374a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_START; 20380b246afaSJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 2039bb9c12c9SSage Weil 20400b246afaSJeff Mahoney if (cur_trans->list.prev != &fs_info->trans_list) { 2041ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 2042ccd467d6SChris Mason struct btrfs_transaction, list); 20434a9d8bdeSMiao Xie if (prev_trans->state != TRANS_STATE_COMPLETED) { 20449b64f57dSElena Reshetova refcount_inc(&prev_trans->use_count); 20450b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2046ccd467d6SChris Mason 20472ff7e61eSJeff Mahoney wait_for_commit(prev_trans); 20481f9b8c8fSFilipe Manana ret = prev_trans->aborted; 2049ccd467d6SChris Mason 2050724e2315SJosef Bacik btrfs_put_transaction(prev_trans); 20511f9b8c8fSFilipe Manana if (ret) 20521f9b8c8fSFilipe Manana goto cleanup_transaction; 2053a4abeea4SJosef Bacik } else { 20540b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2055ccd467d6SChris Mason } 2056a4abeea4SJosef Bacik } else { 20570b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2058cb2d3dadSFilipe Manana /* 2059cb2d3dadSFilipe Manana * The previous transaction was aborted and was already removed 2060cb2d3dadSFilipe Manana * from the list of transactions at fs_info->trans_list. So we 2061cb2d3dadSFilipe Manana * abort to prevent writing a new superblock that reflects a 2062cb2d3dadSFilipe Manana * corrupt state (pointing to trees with unwritten nodes/leafs). 2063cb2d3dadSFilipe Manana */ 2064cb2d3dadSFilipe Manana if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) { 2065cb2d3dadSFilipe Manana ret = -EROFS; 2066cb2d3dadSFilipe Manana goto cleanup_transaction; 2067cb2d3dadSFilipe Manana } 2068ccd467d6SChris Mason } 206915ee9bc7SJosef Bacik 20700860adfdSMiao Xie extwriter_counter_dec(cur_trans, trans->type); 20710860adfdSMiao Xie 2072609e804dSFilipe Manana ret = btrfs_start_delalloc_flush(trans); 207382436617SMiao Xie if (ret) 207482436617SMiao Xie goto cleanup_transaction; 207582436617SMiao Xie 2076e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 207749b25e05SJeff Mahoney if (ret) 207849b25e05SJeff Mahoney goto cleanup_transaction; 207916cdcec7SMiao Xie 2080581227d0SMiao Xie wait_event(cur_trans->writer_wait, 2081581227d0SMiao Xie extwriter_counter_read(cur_trans) == 0); 2082ed3b3d31SChris Mason 2083581227d0SMiao Xie /* some pending stuffs might be added after the previous flush. */ 2084e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 2085ca469637SMiao Xie if (ret) 2086ca469637SMiao Xie goto cleanup_transaction; 2087ca469637SMiao Xie 2088609e804dSFilipe Manana btrfs_wait_delalloc_flush(trans); 2089cb7ab021SWang Shilong 20902ff7e61eSJeff Mahoney btrfs_scrub_pause(fs_info); 2091ed0ca140SJosef Bacik /* 2092ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 2093ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 20944a9d8bdeSMiao Xie * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2095ed0ca140SJosef Bacik */ 20960b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 20974a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMMIT_DOING; 20980b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2099ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 2100ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 210115ee9bc7SJosef Bacik 21022cba30f1SMiao Xie /* ->aborted might be set after the previous check, so check it */ 210320c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 21042cba30f1SMiao Xie ret = cur_trans->aborted; 21056cf7f77eSWang Shilong goto scrub_continue; 21062cba30f1SMiao Xie } 21077585717fSChris Mason /* 21087585717fSChris Mason * the reloc mutex makes sure that we stop 21097585717fSChris Mason * the balancing code from coming in and moving 21107585717fSChris Mason * extents around in the middle of the commit 21117585717fSChris Mason */ 21120b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 21137585717fSChris Mason 211442874b3dSMiao Xie /* 211542874b3dSMiao Xie * We needn't worry about the delayed items because we will 211642874b3dSMiao Xie * deal with them in create_pending_snapshot(), which is the 211742874b3dSMiao Xie * core function of the snapshot creation. 211842874b3dSMiao Xie */ 211908d50ca3SNikolay Borisov ret = create_pending_snapshots(trans); 212049b25e05SJeff Mahoney if (ret) { 21210b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21226cf7f77eSWang Shilong goto scrub_continue; 212349b25e05SJeff Mahoney } 21243063d29fSChris Mason 212542874b3dSMiao Xie /* 212642874b3dSMiao Xie * We insert the dir indexes of the snapshots and update the inode 212742874b3dSMiao Xie * of the snapshots' parents after the snapshot creation, so there 212842874b3dSMiao Xie * are some delayed items which are not dealt with. Now deal with 212942874b3dSMiao Xie * them. 213042874b3dSMiao Xie * 213142874b3dSMiao Xie * We needn't worry that this operation will corrupt the snapshots, 213242874b3dSMiao Xie * because all the tree which are snapshoted will be forced to COW 213342874b3dSMiao Xie * the nodes and leaves. 213442874b3dSMiao Xie */ 2135e5c304e6SNikolay Borisov ret = btrfs_run_delayed_items(trans); 213649b25e05SJeff Mahoney if (ret) { 21370b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21386cf7f77eSWang Shilong goto scrub_continue; 213949b25e05SJeff Mahoney } 214016cdcec7SMiao Xie 2141c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 214249b25e05SJeff Mahoney if (ret) { 21430b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21446cf7f77eSWang Shilong goto scrub_continue; 214549b25e05SJeff Mahoney } 214656bec294SChris Mason 2147e999376fSChris Mason /* 2148e999376fSChris Mason * make sure none of the code above managed to slip in a 2149e999376fSChris Mason * delayed item 2150e999376fSChris Mason */ 2151ccdf9b30SJeff Mahoney btrfs_assert_delayed_root_empty(fs_info); 2152e999376fSChris Mason 21532c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 2154dc17ff8fSChris Mason 2155e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 2156e02119d5SChris Mason * various roots consistent with each other. Every pointer 2157e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 2158e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 2159e02119d5SChris Mason * the tree logging code from jumping in and changing any 2160e02119d5SChris Mason * of the trees. 2161e02119d5SChris Mason * 2162e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 2163e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 2164e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 2165e02119d5SChris Mason * from now until after the super is written, we avoid races 2166e02119d5SChris Mason * with the tree-log code. 2167e02119d5SChris Mason */ 21680b246afaSJeff Mahoney mutex_lock(&fs_info->tree_log_mutex); 21691a40e23bSZheng Yan 21707e4443d9SNikolay Borisov ret = commit_fs_roots(trans); 217149b25e05SJeff Mahoney if (ret) { 21720b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 21730b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 21746cf7f77eSWang Shilong goto scrub_continue; 217549b25e05SJeff Mahoney } 217654aa1f4dSChris Mason 21773818aea2SQu Wenruo /* 21787e1876acSDavid Sterba * Since the transaction is done, we can apply the pending changes 21797e1876acSDavid Sterba * before the next transaction. 21803818aea2SQu Wenruo */ 21810b246afaSJeff Mahoney btrfs_apply_pending_changes(fs_info); 21823818aea2SQu Wenruo 21835d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 2184e02119d5SChris Mason * safe to free the root of tree log roots 2185e02119d5SChris Mason */ 21860b246afaSJeff Mahoney btrfs_free_log_root_tree(trans, fs_info); 2187e02119d5SChris Mason 21880ed4792aSQu Wenruo /* 218982bafb38SQu Wenruo * commit_fs_roots() can call btrfs_save_ino_cache(), which generates 219082bafb38SQu Wenruo * new delayed refs. Must handle them or qgroup can be wrong. 219182bafb38SQu Wenruo */ 2192c79a70b1SNikolay Borisov ret = btrfs_run_delayed_refs(trans, (unsigned long)-1); 219382bafb38SQu Wenruo if (ret) { 219482bafb38SQu Wenruo mutex_unlock(&fs_info->tree_log_mutex); 219582bafb38SQu Wenruo mutex_unlock(&fs_info->reloc_mutex); 219682bafb38SQu Wenruo goto scrub_continue; 219782bafb38SQu Wenruo } 219882bafb38SQu Wenruo 219982bafb38SQu Wenruo /* 22000ed4792aSQu Wenruo * Since fs roots are all committed, we can get a quite accurate 22010ed4792aSQu Wenruo * new_roots. So let's do quota accounting. 22020ed4792aSQu Wenruo */ 2203460fb20aSNikolay Borisov ret = btrfs_qgroup_account_extents(trans); 22040ed4792aSQu Wenruo if (ret < 0) { 22050b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22060b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22070ed4792aSQu Wenruo goto scrub_continue; 22080ed4792aSQu Wenruo } 22090ed4792aSQu Wenruo 22109386d8bcSNikolay Borisov ret = commit_cowonly_roots(trans); 221149b25e05SJeff Mahoney if (ret) { 22120b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22130b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22146cf7f77eSWang Shilong goto scrub_continue; 221549b25e05SJeff Mahoney } 221654aa1f4dSChris Mason 22172cba30f1SMiao Xie /* 22182cba30f1SMiao Xie * The tasks which save the space cache and inode cache may also 22192cba30f1SMiao Xie * update ->aborted, check it. 22202cba30f1SMiao Xie */ 222120c7bcecSSeraphime Kirkovski if (unlikely(READ_ONCE(cur_trans->aborted))) { 22222cba30f1SMiao Xie ret = cur_trans->aborted; 22230b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22240b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 22256cf7f77eSWang Shilong goto scrub_continue; 22262cba30f1SMiao Xie } 22272cba30f1SMiao Xie 22288b74c03eSDavid Sterba btrfs_prepare_extent_commit(fs_info); 222911833d66SYan Zheng 22300b246afaSJeff Mahoney cur_trans = fs_info->running_transaction; 22315f39d397SChris Mason 22320b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->tree_root->root_item, 22330b246afaSJeff Mahoney fs_info->tree_root->node); 22340b246afaSJeff Mahoney list_add_tail(&fs_info->tree_root->dirty_list, 22359e351cc8SJosef Bacik &cur_trans->switch_commits); 22365d4f98a2SYan Zheng 22370b246afaSJeff Mahoney btrfs_set_root_node(&fs_info->chunk_root->root_item, 22380b246afaSJeff Mahoney fs_info->chunk_root->node); 22390b246afaSJeff Mahoney list_add_tail(&fs_info->chunk_root->dirty_list, 22409e351cc8SJosef Bacik &cur_trans->switch_commits); 22419e351cc8SJosef Bacik 224216916a88SNikolay Borisov switch_commit_roots(cur_trans); 22435d4f98a2SYan Zheng 2244ce93ec54SJosef Bacik ASSERT(list_empty(&cur_trans->dirty_bgs)); 22451bbc621eSChris Mason ASSERT(list_empty(&cur_trans->io_bgs)); 22462ff7e61eSJeff Mahoney update_super_roots(fs_info); 2247e02119d5SChris Mason 22480b246afaSJeff Mahoney btrfs_set_super_log_root(fs_info->super_copy, 0); 22490b246afaSJeff Mahoney btrfs_set_super_log_root_level(fs_info->super_copy, 0); 22500b246afaSJeff Mahoney memcpy(fs_info->super_for_commit, fs_info->super_copy, 22510b246afaSJeff Mahoney sizeof(*fs_info->super_copy)); 2252ccd467d6SChris Mason 2253bbbf7243SNikolay Borisov btrfs_commit_device_sizes(cur_trans); 2254935e5cc9SMiao Xie 22550b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 22560b246afaSJeff Mahoney clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2257656f30dbSFilipe Manana 22584fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 22594fbcdf66SFilipe Manana 22600b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 22614a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_UNBLOCKED; 22620b246afaSJeff Mahoney fs_info->running_transaction = NULL; 22630b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 22640b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 2265b7ec40d7SChris Mason 22660b246afaSJeff Mahoney wake_up(&fs_info->transaction_wait); 2267e6dcd2dcSChris Mason 226870458a58SNikolay Borisov ret = btrfs_write_and_wait_transaction(trans); 226949b25e05SJeff Mahoney if (ret) { 22700b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, 227108748810SDavid Sterba "Error while writing out transaction"); 22720b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 22736cf7f77eSWang Shilong goto scrub_continue; 227449b25e05SJeff Mahoney } 227549b25e05SJeff Mahoney 2276eece6a9cSDavid Sterba ret = write_all_supers(fs_info, 0); 2277e02119d5SChris Mason /* 2278e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 2279e02119d5SChris Mason * to go about their business 2280e02119d5SChris Mason */ 22810b246afaSJeff Mahoney mutex_unlock(&fs_info->tree_log_mutex); 2282c1f32b7cSAnand Jain if (ret) 2283c1f32b7cSAnand Jain goto scrub_continue; 2284e02119d5SChris Mason 22855ead2dd0SNikolay Borisov btrfs_finish_extent_commit(trans); 22864313b399SChris Mason 22873204d33cSJosef Bacik if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 22880b246afaSJeff Mahoney btrfs_clear_space_info_full(fs_info); 228913212b54SZhao Lei 22900b246afaSJeff Mahoney fs_info->last_trans_committed = cur_trans->transid; 22914a9d8bdeSMiao Xie /* 22924a9d8bdeSMiao Xie * We needn't acquire the lock here because there is no other task 22934a9d8bdeSMiao Xie * which can change it. 22944a9d8bdeSMiao Xie */ 22954a9d8bdeSMiao Xie cur_trans->state = TRANS_STATE_COMPLETED; 22962c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 2297a514d638SQu Wenruo clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags); 22983de4586cSChris Mason 22990b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 230013c5a93eSJosef Bacik list_del_init(&cur_trans->list); 23010b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 2302a4abeea4SJosef Bacik 2303724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 2304724e2315SJosef Bacik btrfs_put_transaction(cur_trans); 230558176a96SJosef Bacik 23060860adfdSMiao Xie if (trans->type & __TRANS_FREEZABLE) 23070b246afaSJeff Mahoney sb_end_intwrite(fs_info->sb); 2308b2b5ef5cSJan Kara 23093a45bb20SJeff Mahoney trace_btrfs_transaction_commit(trans->root); 23101abe9b8aSliubo 23112ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 2312a2de733cSArne Jansen 23139ed74f2dSJosef Bacik if (current->journal_info == trans) 23149ed74f2dSJosef Bacik current->journal_info = NULL; 23159ed74f2dSJosef Bacik 23162c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 231724bbcf04SYan, Zheng 231879154b1bSChris Mason return ret; 231949b25e05SJeff Mahoney 23206cf7f77eSWang Shilong scrub_continue: 23212ff7e61eSJeff Mahoney btrfs_scrub_continue(fs_info); 232249b25e05SJeff Mahoney cleanup_transaction: 2323dc60c525SNikolay Borisov btrfs_trans_release_metadata(trans); 2324c7cc64a9SDavid Sterba btrfs_cleanup_pending_block_groups(trans); 23254fbcdf66SFilipe Manana btrfs_trans_release_chunk_metadata(trans); 23260e721106SJosef Bacik trans->block_rsv = NULL; 23270b246afaSJeff Mahoney btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 232849b25e05SJeff Mahoney if (current->journal_info == trans) 232949b25e05SJeff Mahoney current->journal_info = NULL; 233097cb39bbSNikolay Borisov cleanup_transaction(trans, ret); 233149b25e05SJeff Mahoney 233249b25e05SJeff Mahoney return ret; 233379154b1bSChris Mason } 233479154b1bSChris Mason 2335d352ac68SChris Mason /* 23369d1a2a3aSDavid Sterba * return < 0 if error 23379d1a2a3aSDavid Sterba * 0 if there are no more dead_roots at the time of call 23389d1a2a3aSDavid Sterba * 1 there are more to be processed, call me again 23399d1a2a3aSDavid Sterba * 23409d1a2a3aSDavid Sterba * The return value indicates there are certainly more snapshots to delete, but 23419d1a2a3aSDavid Sterba * if there comes a new one during processing, it may return 0. We don't mind, 23429d1a2a3aSDavid Sterba * because btrfs_commit_super will poke cleaner thread and it will process it a 23439d1a2a3aSDavid Sterba * few seconds later. 2344d352ac68SChris Mason */ 23459d1a2a3aSDavid Sterba int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root) 2346e9d0b13bSChris Mason { 23479d1a2a3aSDavid Sterba int ret; 23485d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 2349e9d0b13bSChris Mason 2350a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 23519d1a2a3aSDavid Sterba if (list_empty(&fs_info->dead_roots)) { 23529d1a2a3aSDavid Sterba spin_unlock(&fs_info->trans_lock); 23539d1a2a3aSDavid Sterba return 0; 23549d1a2a3aSDavid Sterba } 23559d1a2a3aSDavid Sterba root = list_first_entry(&fs_info->dead_roots, 23569d1a2a3aSDavid Sterba struct btrfs_root, root_list); 2357cfad392bSJosef Bacik list_del_init(&root->root_list); 2358a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 23595d4f98a2SYan Zheng 23604fd786e6SMisono Tomohiro btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid); 236176dda93cSYan, Zheng 236216cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 236316cdcec7SMiao Xie 236476dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 236576dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 23662c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 0, 0); 236776dda93cSYan, Zheng else 23682c536799SJeff Mahoney ret = btrfs_drop_snapshot(root, NULL, 1, 0); 236932471dc2SDavid Sterba 23706596a928SJosef Bacik return (ret < 0) ? 0 : 1; 2371e9d0b13bSChris Mason } 2372572d9ab7SDavid Sterba 2373572d9ab7SDavid Sterba void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info) 2374572d9ab7SDavid Sterba { 2375572d9ab7SDavid Sterba unsigned long prev; 2376572d9ab7SDavid Sterba unsigned long bit; 2377572d9ab7SDavid Sterba 23786c9fe14fSQu Wenruo prev = xchg(&fs_info->pending_changes, 0); 2379572d9ab7SDavid Sterba if (!prev) 2380572d9ab7SDavid Sterba return; 2381572d9ab7SDavid Sterba 23827e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE; 23837e1876acSDavid Sterba if (prev & bit) 23847e1876acSDavid Sterba btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23857e1876acSDavid Sterba prev &= ~bit; 23867e1876acSDavid Sterba 23877e1876acSDavid Sterba bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE; 23887e1876acSDavid Sterba if (prev & bit) 23897e1876acSDavid Sterba btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE); 23907e1876acSDavid Sterba prev &= ~bit; 23917e1876acSDavid Sterba 2392d51033d0SDavid Sterba bit = 1 << BTRFS_PENDING_COMMIT; 2393d51033d0SDavid Sterba if (prev & bit) 2394d51033d0SDavid Sterba btrfs_debug(fs_info, "pending commit done"); 2395d51033d0SDavid Sterba prev &= ~bit; 2396d51033d0SDavid Sterba 2397572d9ab7SDavid Sterba if (prev) 2398572d9ab7SDavid Sterba btrfs_warn(fs_info, 2399572d9ab7SDavid Sterba "unknown pending changes left 0x%lx, ignoring", prev); 2400572d9ab7SDavid Sterba } 2401