16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 2579154b1bSChris Mason #include "ctree.h" 2679154b1bSChris Mason #include "disk-io.h" 2779154b1bSChris Mason #include "transaction.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 30581bb050SLi Zefan #include "inode-map.h" 3179154b1bSChris Mason 320f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 330f7d52f4SChris Mason 3480b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3579154b1bSChris Mason { 3613c5a93eSJosef Bacik WARN_ON(atomic_read(&transaction->use_count) == 0); 3713c5a93eSJosef Bacik if (atomic_dec_and_test(&transaction->use_count)) { 38a4abeea4SJosef Bacik BUG_ON(!list_empty(&transaction->list)); 392c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 402c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4179154b1bSChris Mason } 4278fae27eSChris Mason } 4379154b1bSChris Mason 44817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root) 45817d52f8SJosef Bacik { 46817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 47817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 48817d52f8SJosef Bacik } 49817d52f8SJosef Bacik 50d352ac68SChris Mason /* 51d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 52d352ac68SChris Mason */ 53a4abeea4SJosef Bacik static noinline int join_transaction(struct btrfs_root *root, int nofail) 5479154b1bSChris Mason { 5579154b1bSChris Mason struct btrfs_transaction *cur_trans; 56a4abeea4SJosef Bacik 57a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 58d43317dcSChris Mason loop: 59a4abeea4SJosef Bacik if (root->fs_info->trans_no_join) { 60a4abeea4SJosef Bacik if (!nofail) { 61a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 62a4abeea4SJosef Bacik return -EBUSY; 63a4abeea4SJosef Bacik } 64a4abeea4SJosef Bacik } 65a4abeea4SJosef Bacik 6679154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 67a4abeea4SJosef Bacik if (cur_trans) { 68a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 69a4abeea4SJosef Bacik atomic_inc(&cur_trans->num_writers); 70a4abeea4SJosef Bacik cur_trans->num_joined++; 71a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 72a4abeea4SJosef Bacik return 0; 73a4abeea4SJosef Bacik } 74a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 75a4abeea4SJosef Bacik 76a4abeea4SJosef Bacik cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 77db5b493aSTsutomu Itoh if (!cur_trans) 78db5b493aSTsutomu Itoh return -ENOMEM; 79d43317dcSChris Mason 80a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 81a4abeea4SJosef Bacik if (root->fs_info->running_transaction) { 82d43317dcSChris Mason /* 83d43317dcSChris Mason * someone started a transaction after we unlocked. Make sure 84d43317dcSChris Mason * to redo the trans_no_join checks above 85d43317dcSChris Mason */ 86a4abeea4SJosef Bacik kmem_cache_free(btrfs_transaction_cachep, cur_trans); 87a4abeea4SJosef Bacik cur_trans = root->fs_info->running_transaction; 88d43317dcSChris Mason goto loop; 89a4abeea4SJosef Bacik } 90d43317dcSChris Mason 9113c5a93eSJosef Bacik atomic_set(&cur_trans->num_writers, 1); 9215ee9bc7SJosef Bacik cur_trans->num_joined = 0; 9379154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 9479154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 9579154b1bSChris Mason cur_trans->in_commit = 0; 96f9295749SChris Mason cur_trans->blocked = 0; 97a4abeea4SJosef Bacik /* 98a4abeea4SJosef Bacik * One for this trans handle, one so it will live on until we 99a4abeea4SJosef Bacik * commit the transaction. 100a4abeea4SJosef Bacik */ 101a4abeea4SJosef Bacik atomic_set(&cur_trans->use_count, 2); 10279154b1bSChris Mason cur_trans->commit_done = 0; 10308607c1bSChris Mason cur_trans->start_time = get_seconds(); 10456bec294SChris Mason 1056bef4d31SEric Paris cur_trans->delayed_refs.root = RB_ROOT; 10656bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 107c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 108c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 10956bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 110c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 111a4abeea4SJosef Bacik spin_lock_init(&cur_trans->commit_lock); 11256bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 11356bec294SChris Mason 1143063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 1158fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 116d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 117f993c883SDavid Sterba root->fs_info->btree_inode->i_mapping); 118a4abeea4SJosef Bacik root->fs_info->generation++; 119a4abeea4SJosef Bacik cur_trans->transid = root->fs_info->generation; 12048ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 121a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 12215ee9bc7SJosef Bacik 12379154b1bSChris Mason return 0; 12479154b1bSChris Mason } 12579154b1bSChris Mason 126d352ac68SChris Mason /* 127d397712bSChris Mason * this does all the record keeping required to make sure that a reference 128d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 129d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 130d397712bSChris Mason * when the transaction commits 131d352ac68SChris Mason */ 1327585717fSChris Mason static int record_root_in_trans(struct btrfs_trans_handle *trans, 1335d4f98a2SYan Zheng struct btrfs_root *root) 1346702ed49SChris Mason { 1355d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 1366702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 1375d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 1385d4f98a2SYan Zheng 1397585717fSChris Mason /* 1407585717fSChris Mason * see below for in_trans_setup usage rules 1417585717fSChris Mason * we have the reloc mutex held now, so there 1427585717fSChris Mason * is only one writer in this function 1437585717fSChris Mason */ 1447585717fSChris Mason root->in_trans_setup = 1; 1457585717fSChris Mason 1467585717fSChris Mason /* make sure readers find in_trans_setup before 1477585717fSChris Mason * they find our root->last_trans update 1487585717fSChris Mason */ 1497585717fSChris Mason smp_wmb(); 1507585717fSChris Mason 151a4abeea4SJosef Bacik spin_lock(&root->fs_info->fs_roots_radix_lock); 152a4abeea4SJosef Bacik if (root->last_trans == trans->transid) { 153a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 154a4abeea4SJosef Bacik return 0; 155a4abeea4SJosef Bacik } 1566702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1576702ed49SChris Mason (unsigned long)root->root_key.objectid, 1586702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 159a4abeea4SJosef Bacik spin_unlock(&root->fs_info->fs_roots_radix_lock); 1607585717fSChris Mason root->last_trans = trans->transid; 1617585717fSChris Mason 1627585717fSChris Mason /* this is pretty tricky. We don't want to 1637585717fSChris Mason * take the relocation lock in btrfs_record_root_in_trans 1647585717fSChris Mason * unless we're really doing the first setup for this root in 1657585717fSChris Mason * this transaction. 1667585717fSChris Mason * 1677585717fSChris Mason * Normally we'd use root->last_trans as a flag to decide 1687585717fSChris Mason * if we want to take the expensive mutex. 1697585717fSChris Mason * 1707585717fSChris Mason * But, we have to set root->last_trans before we 1717585717fSChris Mason * init the relocation root, otherwise, we trip over warnings 1727585717fSChris Mason * in ctree.c. The solution used here is to flag ourselves 1737585717fSChris Mason * with root->in_trans_setup. When this is 1, we're still 1747585717fSChris Mason * fixing up the reloc trees and everyone must wait. 1757585717fSChris Mason * 1767585717fSChris Mason * When this is zero, they can trust root->last_trans and fly 1777585717fSChris Mason * through btrfs_record_root_in_trans without having to take the 1787585717fSChris Mason * lock. smp_wmb() makes sure that all the writes above are 1797585717fSChris Mason * done before we pop in the zero below 1807585717fSChris Mason */ 1815d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 1827585717fSChris Mason smp_wmb(); 1837585717fSChris Mason root->in_trans_setup = 0; 1846702ed49SChris Mason } 1855d4f98a2SYan Zheng return 0; 1866702ed49SChris Mason } 1875d4f98a2SYan Zheng 1887585717fSChris Mason 1897585717fSChris Mason int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 1907585717fSChris Mason struct btrfs_root *root) 1917585717fSChris Mason { 1927585717fSChris Mason if (!root->ref_cows) 1937585717fSChris Mason return 0; 1947585717fSChris Mason 1957585717fSChris Mason /* 1967585717fSChris Mason * see record_root_in_trans for comments about in_trans_setup usage 1977585717fSChris Mason * and barriers 1987585717fSChris Mason */ 1997585717fSChris Mason smp_rmb(); 2007585717fSChris Mason if (root->last_trans == trans->transid && 2017585717fSChris Mason !root->in_trans_setup) 2027585717fSChris Mason return 0; 2037585717fSChris Mason 2047585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 2057585717fSChris Mason record_root_in_trans(trans, root); 2067585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 2077585717fSChris Mason 2087585717fSChris Mason return 0; 2097585717fSChris Mason } 2107585717fSChris Mason 211d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 212d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 213d352ac68SChris Mason * transaction might not be fully on disk. 214d352ac68SChris Mason */ 21537d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 21679154b1bSChris Mason { 217f9295749SChris Mason struct btrfs_transaction *cur_trans; 21879154b1bSChris Mason 219a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 220f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 22137d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 22213c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 223a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 22472d63ed6SLi Zefan 22572d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 22672d63ed6SLi Zefan !cur_trans->blocked); 227f9295749SChris Mason put_transaction(cur_trans); 228a4abeea4SJosef Bacik } else { 229a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 230f9295749SChris Mason } 23137d1aeeeSChris Mason } 23237d1aeeeSChris Mason 233249ac1e5SJosef Bacik enum btrfs_trans_type { 234249ac1e5SJosef Bacik TRANS_START, 235249ac1e5SJosef Bacik TRANS_JOIN, 236249ac1e5SJosef Bacik TRANS_USERSPACE, 2370af3d00bSJosef Bacik TRANS_JOIN_NOLOCK, 238249ac1e5SJosef Bacik }; 239249ac1e5SJosef Bacik 240a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 24137d1aeeeSChris Mason { 242a4abeea4SJosef Bacik if (root->fs_info->log_root_recovering) 243a4abeea4SJosef Bacik return 0; 244a4abeea4SJosef Bacik 245a4abeea4SJosef Bacik if (type == TRANS_USERSPACE) 246a22285a6SYan, Zheng return 1; 247a4abeea4SJosef Bacik 248a4abeea4SJosef Bacik if (type == TRANS_START && 249a4abeea4SJosef Bacik !atomic_read(&root->fs_info->open_ioctl_trans)) 250a4abeea4SJosef Bacik return 1; 251a4abeea4SJosef Bacik 252a22285a6SYan, Zheng return 0; 253a22285a6SYan, Zheng } 254a22285a6SYan, Zheng 255a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 256a22285a6SYan, Zheng u64 num_items, int type) 257a22285a6SYan, Zheng { 258a22285a6SYan, Zheng struct btrfs_trans_handle *h; 259a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 260b5009945SJosef Bacik u64 num_bytes = 0; 261a22285a6SYan, Zheng int ret; 262acce952bSliubo 263acce952bSliubo if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 264acce952bSliubo return ERR_PTR(-EROFS); 2652a1eb461SJosef Bacik 2662a1eb461SJosef Bacik if (current->journal_info) { 2672a1eb461SJosef Bacik WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK); 2682a1eb461SJosef Bacik h = current->journal_info; 2692a1eb461SJosef Bacik h->use_count++; 2702a1eb461SJosef Bacik h->orig_rsv = h->block_rsv; 2712a1eb461SJosef Bacik h->block_rsv = NULL; 2722a1eb461SJosef Bacik goto got_it; 2732a1eb461SJosef Bacik } 274b5009945SJosef Bacik 275b5009945SJosef Bacik /* 276b5009945SJosef Bacik * Do the reservation before we join the transaction so we can do all 277b5009945SJosef Bacik * the appropriate flushing if need be. 278b5009945SJosef Bacik */ 279b5009945SJosef Bacik if (num_items > 0 && root != root->fs_info->chunk_root) { 280b5009945SJosef Bacik num_bytes = btrfs_calc_trans_metadata_size(root, num_items); 2814a92b1b8SJosef Bacik ret = btrfs_block_rsv_add(root, 282b5009945SJosef Bacik &root->fs_info->trans_block_rsv, 283b5009945SJosef Bacik num_bytes); 284b5009945SJosef Bacik if (ret) 285b5009945SJosef Bacik return ERR_PTR(ret); 286b5009945SJosef Bacik } 287a22285a6SYan, Zheng again: 288a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 289a22285a6SYan, Zheng if (!h) 290a22285a6SYan, Zheng return ERR_PTR(-ENOMEM); 291a22285a6SYan, Zheng 292a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 29337d1aeeeSChris Mason wait_current_trans(root); 294a22285a6SYan, Zheng 295a4abeea4SJosef Bacik do { 296a4abeea4SJosef Bacik ret = join_transaction(root, type == TRANS_JOIN_NOLOCK); 297a4abeea4SJosef Bacik if (ret == -EBUSY) 298a4abeea4SJosef Bacik wait_current_trans(root); 299a4abeea4SJosef Bacik } while (ret == -EBUSY); 300a4abeea4SJosef Bacik 301db5b493aSTsutomu Itoh if (ret < 0) { 3026e8df2aeSYoshinori Sano kmem_cache_free(btrfs_trans_handle_cachep, h); 303db5b493aSTsutomu Itoh return ERR_PTR(ret); 304db5b493aSTsutomu Itoh } 3050f7d52f4SChris Mason 306a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 307a22285a6SYan, Zheng 308a22285a6SYan, Zheng h->transid = cur_trans->transid; 309a22285a6SYan, Zheng h->transaction = cur_trans; 31079154b1bSChris Mason h->blocks_used = 0; 311a22285a6SYan, Zheng h->bytes_reserved = 0; 31256bec294SChris Mason h->delayed_ref_updates = 0; 3132a1eb461SJosef Bacik h->use_count = 1; 314f0486c68SYan, Zheng h->block_rsv = NULL; 3152a1eb461SJosef Bacik h->orig_rsv = NULL; 316b7ec40d7SChris Mason 317a22285a6SYan, Zheng smp_mb(); 318a22285a6SYan, Zheng if (cur_trans->blocked && may_wait_transaction(root, type)) { 319a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 320a22285a6SYan, Zheng goto again; 321a22285a6SYan, Zheng } 3229ed74f2dSJosef Bacik 323b5009945SJosef Bacik if (num_bytes) { 324b5009945SJosef Bacik h->block_rsv = &root->fs_info->trans_block_rsv; 325b5009945SJosef Bacik h->bytes_reserved = num_bytes; 326a22285a6SYan, Zheng } 327a22285a6SYan, Zheng 3282a1eb461SJosef Bacik got_it: 329a4abeea4SJosef Bacik btrfs_record_root_in_trans(h, root); 330a22285a6SYan, Zheng 331a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 332a22285a6SYan, Zheng current->journal_info = h; 33379154b1bSChris Mason return h; 33479154b1bSChris Mason } 33579154b1bSChris Mason 336f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 337a22285a6SYan, Zheng int num_items) 338f9295749SChris Mason { 339a22285a6SYan, Zheng return start_transaction(root, num_items, TRANS_START); 340f9295749SChris Mason } 3417a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 342f9295749SChris Mason { 343a22285a6SYan, Zheng return start_transaction(root, 0, TRANS_JOIN); 344f9295749SChris Mason } 345f9295749SChris Mason 3467a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 3470af3d00bSJosef Bacik { 3480af3d00bSJosef Bacik return start_transaction(root, 0, TRANS_JOIN_NOLOCK); 3490af3d00bSJosef Bacik } 3500af3d00bSJosef Bacik 3517a7eaa40SJosef Bacik struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 3529ca9ee09SSage Weil { 3537a7eaa40SJosef Bacik return start_transaction(root, 0, TRANS_USERSPACE); 3549ca9ee09SSage Weil } 3559ca9ee09SSage Weil 356d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 357b9c8300cSLi Zefan static noinline void wait_for_commit(struct btrfs_root *root, 35889ce8a63SChris Mason struct btrfs_transaction *commit) 35989ce8a63SChris Mason { 36072d63ed6SLi Zefan wait_event(commit->commit_wait, commit->commit_done); 36189ce8a63SChris Mason } 36289ce8a63SChris Mason 36346204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 36446204592SSage Weil { 36546204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 36646204592SSage Weil int ret; 36746204592SSage Weil 36846204592SSage Weil ret = 0; 36946204592SSage Weil if (transid) { 37046204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 371a4abeea4SJosef Bacik goto out; 37246204592SSage Weil 37346204592SSage Weil /* find specified transaction */ 374a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 37546204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 37646204592SSage Weil if (t->transid == transid) { 37746204592SSage Weil cur_trans = t; 378a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 37946204592SSage Weil break; 38046204592SSage Weil } 38146204592SSage Weil if (t->transid > transid) 38246204592SSage Weil break; 38346204592SSage Weil } 384a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 38546204592SSage Weil ret = -EINVAL; 38646204592SSage Weil if (!cur_trans) 387a4abeea4SJosef Bacik goto out; /* bad transid */ 38846204592SSage Weil } else { 38946204592SSage Weil /* find newest transaction that is committing | committed */ 390a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 39146204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 39246204592SSage Weil list) { 39346204592SSage Weil if (t->in_commit) { 39446204592SSage Weil if (t->commit_done) 3953473f3c0SJosef Bacik break; 39646204592SSage Weil cur_trans = t; 397a4abeea4SJosef Bacik atomic_inc(&cur_trans->use_count); 39846204592SSage Weil break; 39946204592SSage Weil } 40046204592SSage Weil } 401a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 40246204592SSage Weil if (!cur_trans) 403a4abeea4SJosef Bacik goto out; /* nothing committing|committed */ 40446204592SSage Weil } 40546204592SSage Weil 40646204592SSage Weil wait_for_commit(root, cur_trans); 40746204592SSage Weil 40846204592SSage Weil put_transaction(cur_trans); 40946204592SSage Weil ret = 0; 410a4abeea4SJosef Bacik out: 41146204592SSage Weil return ret; 41246204592SSage Weil } 41346204592SSage Weil 41437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 41537d1aeeeSChris Mason { 416a4abeea4SJosef Bacik if (!atomic_read(&root->fs_info->open_ioctl_trans)) 41737d1aeeeSChris Mason wait_current_trans(root); 41837d1aeeeSChris Mason } 41937d1aeeeSChris Mason 4208929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 4218929ecfaSYan, Zheng struct btrfs_root *root) 4228929ecfaSYan, Zheng { 4238929ecfaSYan, Zheng int ret; 42436ba022aSJosef Bacik 42536ba022aSJosef Bacik ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 4268929ecfaSYan, Zheng return ret ? 1 : 0; 4278929ecfaSYan, Zheng } 4288929ecfaSYan, Zheng 4298929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 4308929ecfaSYan, Zheng struct btrfs_root *root) 4318929ecfaSYan, Zheng { 4328929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 4339c8d86dbSJosef Bacik struct btrfs_block_rsv *rsv = trans->block_rsv; 4348929ecfaSYan, Zheng int updates; 4358929ecfaSYan, Zheng 436a4abeea4SJosef Bacik smp_mb(); 4378929ecfaSYan, Zheng if (cur_trans->blocked || cur_trans->delayed_refs.flushing) 4388929ecfaSYan, Zheng return 1; 4398929ecfaSYan, Zheng 4409c8d86dbSJosef Bacik /* 4419c8d86dbSJosef Bacik * We need to do this in case we're deleting csums so the global block 4429c8d86dbSJosef Bacik * rsv get's used instead of the csum block rsv. 4439c8d86dbSJosef Bacik */ 4449c8d86dbSJosef Bacik trans->block_rsv = NULL; 4459c8d86dbSJosef Bacik 4468929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 4478929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 4488929ecfaSYan, Zheng if (updates) 4498929ecfaSYan, Zheng btrfs_run_delayed_refs(trans, root, updates); 4508929ecfaSYan, Zheng 4519c8d86dbSJosef Bacik trans->block_rsv = rsv; 4529c8d86dbSJosef Bacik 4538929ecfaSYan, Zheng return should_end_transaction(trans, root); 4548929ecfaSYan, Zheng } 4558929ecfaSYan, Zheng 45689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 4570af3d00bSJosef Bacik struct btrfs_root *root, int throttle, int lock) 45879154b1bSChris Mason { 4598929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 460ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 461c3e69d58SChris Mason int count = 0; 462d6e4a428SChris Mason 4632a1eb461SJosef Bacik if (--trans->use_count) { 4642a1eb461SJosef Bacik trans->block_rsv = trans->orig_rsv; 4652a1eb461SJosef Bacik return 0; 4662a1eb461SJosef Bacik } 4672a1eb461SJosef Bacik 468b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 4694c13d758SJosef Bacik trans->block_rsv = NULL; 470c3e69d58SChris Mason while (count < 4) { 471c3e69d58SChris Mason unsigned long cur = trans->delayed_ref_updates; 472c3e69d58SChris Mason trans->delayed_ref_updates = 0; 473c3e69d58SChris Mason if (cur && 474c3e69d58SChris Mason trans->transaction->delayed_refs.num_heads_ready > 64) { 475c3e69d58SChris Mason trans->delayed_ref_updates = 0; 476b7ec40d7SChris Mason 477b7ec40d7SChris Mason /* 478b7ec40d7SChris Mason * do a full flush if the transaction is trying 479b7ec40d7SChris Mason * to close 480b7ec40d7SChris Mason */ 481b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) 482b7ec40d7SChris Mason cur = 0; 483c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 484c3e69d58SChris Mason } else { 485c3e69d58SChris Mason break; 486c3e69d58SChris Mason } 487c3e69d58SChris Mason count++; 48856bec294SChris Mason } 48956bec294SChris Mason 490a4abeea4SJosef Bacik if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 491a4abeea4SJosef Bacik should_end_transaction(trans, root)) { 4928929ecfaSYan, Zheng trans->transaction->blocked = 1; 493a4abeea4SJosef Bacik smp_wmb(); 494a4abeea4SJosef Bacik } 4958929ecfaSYan, Zheng 4960af3d00bSJosef Bacik if (lock && cur_trans->blocked && !cur_trans->in_commit) { 49781317fdeSJosef Bacik if (throttle) { 49881317fdeSJosef Bacik /* 49981317fdeSJosef Bacik * We may race with somebody else here so end up having 50081317fdeSJosef Bacik * to call end_transaction on ourselves again, so inc 50181317fdeSJosef Bacik * our use_count. 50281317fdeSJosef Bacik */ 50381317fdeSJosef Bacik trans->use_count++; 5048929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 50581317fdeSJosef Bacik } else { 5068929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 5078929ecfaSYan, Zheng } 50881317fdeSJosef Bacik } 5098929ecfaSYan, Zheng 5108929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 51113c5a93eSJosef Bacik WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 51213c5a93eSJosef Bacik atomic_dec(&cur_trans->num_writers); 51389ce8a63SChris Mason 51499d16cbcSSage Weil smp_mb(); 51579154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 51679154b1bSChris Mason wake_up(&cur_trans->writer_wait); 51779154b1bSChris Mason put_transaction(cur_trans); 5189ed74f2dSJosef Bacik 5199ed74f2dSJosef Bacik if (current->journal_info == trans) 5209ed74f2dSJosef Bacik current->journal_info = NULL; 521d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 5222c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 523ab78c84dSChris Mason 52424bbcf04SYan, Zheng if (throttle) 52524bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 52624bbcf04SYan, Zheng 52779154b1bSChris Mason return 0; 52879154b1bSChris Mason } 52979154b1bSChris Mason 53089ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 53189ce8a63SChris Mason struct btrfs_root *root) 53289ce8a63SChris Mason { 53316cdcec7SMiao Xie int ret; 53416cdcec7SMiao Xie 53516cdcec7SMiao Xie ret = __btrfs_end_transaction(trans, root, 0, 1); 53616cdcec7SMiao Xie if (ret) 53716cdcec7SMiao Xie return ret; 53816cdcec7SMiao Xie return 0; 53989ce8a63SChris Mason } 54089ce8a63SChris Mason 54189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 54289ce8a63SChris Mason struct btrfs_root *root) 54389ce8a63SChris Mason { 54416cdcec7SMiao Xie int ret; 54516cdcec7SMiao Xie 54616cdcec7SMiao Xie ret = __btrfs_end_transaction(trans, root, 1, 1); 54716cdcec7SMiao Xie if (ret) 54816cdcec7SMiao Xie return ret; 54916cdcec7SMiao Xie return 0; 5500af3d00bSJosef Bacik } 5510af3d00bSJosef Bacik 5520af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, 5530af3d00bSJosef Bacik struct btrfs_root *root) 5540af3d00bSJosef Bacik { 55516cdcec7SMiao Xie int ret; 55616cdcec7SMiao Xie 55716cdcec7SMiao Xie ret = __btrfs_end_transaction(trans, root, 0, 0); 55816cdcec7SMiao Xie if (ret) 55916cdcec7SMiao Xie return ret; 56016cdcec7SMiao Xie return 0; 56116cdcec7SMiao Xie } 56216cdcec7SMiao Xie 56316cdcec7SMiao Xie int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans, 56416cdcec7SMiao Xie struct btrfs_root *root) 56516cdcec7SMiao Xie { 56616cdcec7SMiao Xie return __btrfs_end_transaction(trans, root, 1, 1); 56789ce8a63SChris Mason } 56889ce8a63SChris Mason 569d352ac68SChris Mason /* 570d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 571d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 572690587d1SChris Mason * those extents are sent to disk but does not wait on them 573d352ac68SChris Mason */ 574690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 5758cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 57679154b1bSChris Mason { 577777e6bd7SChris Mason int err = 0; 5787c4452b9SChris Mason int werr = 0; 5791728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 580777e6bd7SChris Mason u64 start = 0; 5815f39d397SChris Mason u64 end; 5827c4452b9SChris Mason 5831728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 5841728366eSJosef Bacik mark)) { 5851728366eSJosef Bacik convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark, 5861728366eSJosef Bacik GFP_NOFS); 5871728366eSJosef Bacik err = filemap_fdatawrite_range(mapping, start, end); 5887c4452b9SChris Mason if (err) 5897c4452b9SChris Mason werr = err; 5901728366eSJosef Bacik cond_resched(); 5911728366eSJosef Bacik start = end + 1; 5927c4452b9SChris Mason } 593690587d1SChris Mason if (err) 594690587d1SChris Mason werr = err; 595690587d1SChris Mason return werr; 596690587d1SChris Mason } 597690587d1SChris Mason 598690587d1SChris Mason /* 599690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 600690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 601690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 602690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 603690587d1SChris Mason */ 604690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 6058cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 606690587d1SChris Mason { 607690587d1SChris Mason int err = 0; 608690587d1SChris Mason int werr = 0; 6091728366eSJosef Bacik struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 610690587d1SChris Mason u64 start = 0; 611690587d1SChris Mason u64 end; 612690587d1SChris Mason 6131728366eSJosef Bacik while (!find_first_extent_bit(dirty_pages, start, &start, &end, 6141728366eSJosef Bacik EXTENT_NEED_WAIT)) { 6151728366eSJosef Bacik clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS); 6161728366eSJosef Bacik err = filemap_fdatawait_range(mapping, start, end); 617777e6bd7SChris Mason if (err) 618777e6bd7SChris Mason werr = err; 619777e6bd7SChris Mason cond_resched(); 6201728366eSJosef Bacik start = end + 1; 621777e6bd7SChris Mason } 6227c4452b9SChris Mason if (err) 6237c4452b9SChris Mason werr = err; 6247c4452b9SChris Mason return werr; 62579154b1bSChris Mason } 62679154b1bSChris Mason 627690587d1SChris Mason /* 628690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 629690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 630690587d1SChris Mason * those extents are on disk for transaction or log commit 631690587d1SChris Mason */ 632690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 6338cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 634690587d1SChris Mason { 635690587d1SChris Mason int ret; 636690587d1SChris Mason int ret2; 637690587d1SChris Mason 6388cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 6398cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 640bf0da8c1SChris Mason 641bf0da8c1SChris Mason if (ret) 642bf0da8c1SChris Mason return ret; 643bf0da8c1SChris Mason if (ret2) 644bf0da8c1SChris Mason return ret2; 645bf0da8c1SChris Mason return 0; 646690587d1SChris Mason } 647690587d1SChris Mason 648d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 649d0c803c4SChris Mason struct btrfs_root *root) 650d0c803c4SChris Mason { 651d0c803c4SChris Mason if (!trans || !trans->transaction) { 652d0c803c4SChris Mason struct inode *btree_inode; 653d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 654d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 655d0c803c4SChris Mason } 656d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 6578cef4e16SYan, Zheng &trans->transaction->dirty_pages, 6588cef4e16SYan, Zheng EXTENT_DIRTY); 659d0c803c4SChris Mason } 660d0c803c4SChris Mason 661d352ac68SChris Mason /* 662d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 663d352ac68SChris Mason * 664d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 665d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 666d352ac68SChris Mason * allocation tree. 667d352ac68SChris Mason * 668d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 669d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 670d352ac68SChris Mason */ 6710b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 67279154b1bSChris Mason struct btrfs_root *root) 67379154b1bSChris Mason { 67479154b1bSChris Mason int ret; 6750b86a832SChris Mason u64 old_root_bytenr; 67686b9f2ecSYan, Zheng u64 old_root_used; 6770b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 67879154b1bSChris Mason 67986b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 6800b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 68156bec294SChris Mason 68279154b1bSChris Mason while (1) { 6830b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 68486b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 68586b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 68679154b1bSChris Mason break; 68787ef2bb4SChris Mason 6885d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 68979154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 6900b86a832SChris Mason &root->root_key, 6910b86a832SChris Mason &root->root_item); 69279154b1bSChris Mason BUG_ON(ret); 69356bec294SChris Mason 69486b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 6954a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 69656bec294SChris Mason BUG_ON(ret); 6970b86a832SChris Mason } 698276e680dSYan Zheng 699276e680dSYan Zheng if (root != root->fs_info->extent_root) 700817d52f8SJosef Bacik switch_commit_root(root); 701276e680dSYan Zheng 7020b86a832SChris Mason return 0; 7030b86a832SChris Mason } 7040b86a832SChris Mason 705d352ac68SChris Mason /* 706d352ac68SChris Mason * update all the cowonly tree roots on disk 707d352ac68SChris Mason */ 7085d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 7090b86a832SChris Mason struct btrfs_root *root) 7100b86a832SChris Mason { 7110b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 7120b86a832SChris Mason struct list_head *next; 71384234f3aSYan Zheng struct extent_buffer *eb; 71456bec294SChris Mason int ret; 71584234f3aSYan Zheng 71656bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 71756bec294SChris Mason BUG_ON(ret); 71887ef2bb4SChris Mason 71984234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 7209fa8cfe7SChris Mason btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); 72184234f3aSYan Zheng btrfs_tree_unlock(eb); 72284234f3aSYan Zheng free_extent_buffer(eb); 7230b86a832SChris Mason 72456bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 72556bec294SChris Mason BUG_ON(ret); 72687ef2bb4SChris Mason 7270b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 7280b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 7290b86a832SChris Mason list_del_init(next); 7300b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 73187ef2bb4SChris Mason 7320b86a832SChris Mason update_cowonly_root(trans, root); 73379154b1bSChris Mason } 734276e680dSYan Zheng 735276e680dSYan Zheng down_write(&fs_info->extent_commit_sem); 736276e680dSYan Zheng switch_commit_root(fs_info->extent_root); 737276e680dSYan Zheng up_write(&fs_info->extent_commit_sem); 738276e680dSYan Zheng 73979154b1bSChris Mason return 0; 74079154b1bSChris Mason } 74179154b1bSChris Mason 742d352ac68SChris Mason /* 743d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 744d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 745d352ac68SChris Mason * be deleted 746d352ac68SChris Mason */ 7475d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root) 7485eda7b5eSChris Mason { 749a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 7505d4f98a2SYan Zheng list_add(&root->root_list, &root->fs_info->dead_roots); 751a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 7525eda7b5eSChris Mason return 0; 7535eda7b5eSChris Mason } 7545eda7b5eSChris Mason 755d352ac68SChris Mason /* 7565d4f98a2SYan Zheng * update all the cowonly tree roots on disk 757d352ac68SChris Mason */ 7585d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 7595d4f98a2SYan Zheng struct btrfs_root *root) 7600f7d52f4SChris Mason { 7610f7d52f4SChris Mason struct btrfs_root *gang[8]; 7625d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 7630f7d52f4SChris Mason int i; 7640f7d52f4SChris Mason int ret; 76554aa1f4dSChris Mason int err = 0; 76654aa1f4dSChris Mason 767a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 7680f7d52f4SChris Mason while (1) { 7695d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 7705d4f98a2SYan Zheng (void **)gang, 0, 7710f7d52f4SChris Mason ARRAY_SIZE(gang), 7720f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 7730f7d52f4SChris Mason if (ret == 0) 7740f7d52f4SChris Mason break; 7750f7d52f4SChris Mason for (i = 0; i < ret; i++) { 7760f7d52f4SChris Mason root = gang[i]; 7775d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 7782619ba1fSChris Mason (unsigned long)root->root_key.objectid, 7790f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 780a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 78131153d81SYan Zheng 782e02119d5SChris Mason btrfs_free_log(trans, root); 7835d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 784d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 785e02119d5SChris Mason 78682d5902dSLi Zefan btrfs_save_ino_cache(root, trans); 78782d5902dSLi Zefan 788*f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 789*f1ebcc74SLiu Bo root->force_cow = 0; 790*f1ebcc74SLiu Bo smp_wmb(); 791*f1ebcc74SLiu Bo 792978d910dSYan Zheng if (root->commit_root != root->node) { 793581bb050SLi Zefan mutex_lock(&root->fs_commit_mutex); 794817d52f8SJosef Bacik switch_commit_root(root); 795581bb050SLi Zefan btrfs_unpin_free_ino(root); 796581bb050SLi Zefan mutex_unlock(&root->fs_commit_mutex); 797581bb050SLi Zefan 798978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 799978d910dSYan Zheng root->node); 800978d910dSYan Zheng } 80131153d81SYan Zheng 8025d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 8030f7d52f4SChris Mason &root->root_key, 8040f7d52f4SChris Mason &root->root_item); 805a4abeea4SJosef Bacik spin_lock(&fs_info->fs_roots_radix_lock); 80654aa1f4dSChris Mason if (err) 80754aa1f4dSChris Mason break; 8080f7d52f4SChris Mason } 8099f3a7427SChris Mason } 810a4abeea4SJosef Bacik spin_unlock(&fs_info->fs_roots_radix_lock); 81154aa1f4dSChris Mason return err; 8120f7d52f4SChris Mason } 8130f7d52f4SChris Mason 814d352ac68SChris Mason /* 815d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 816d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 817d352ac68SChris Mason */ 818e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 819e9d0b13bSChris Mason { 820e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 821e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 8228929ecfaSYan, Zheng int ret; 823d3c2fdcfSChris Mason unsigned long nr; 824e9d0b13bSChris Mason 8258929ecfaSYan, Zheng if (xchg(&root->defrag_running, 1)) 826e9d0b13bSChris Mason return 0; 8278929ecfaSYan, Zheng 8286b80053dSChris Mason while (1) { 8298929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 8308929ecfaSYan, Zheng if (IS_ERR(trans)) 8318929ecfaSYan, Zheng return PTR_ERR(trans); 8328929ecfaSYan, Zheng 833e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 8348929ecfaSYan, Zheng 835d3c2fdcfSChris Mason nr = trans->blocks_used; 836e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 837d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 838e9d0b13bSChris Mason cond_resched(); 839e9d0b13bSChris Mason 8407841cb28SDavid Sterba if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 841e9d0b13bSChris Mason break; 842e9d0b13bSChris Mason } 843e9d0b13bSChris Mason root->defrag_running = 0; 8448929ecfaSYan, Zheng return ret; 845e9d0b13bSChris Mason } 846e9d0b13bSChris Mason 847d352ac68SChris Mason /* 848d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 849d352ac68SChris Mason * transaction commit. This does the actual creation 850d352ac68SChris Mason */ 85180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 8523063d29fSChris Mason struct btrfs_fs_info *fs_info, 8533063d29fSChris Mason struct btrfs_pending_snapshot *pending) 8543063d29fSChris Mason { 8553063d29fSChris Mason struct btrfs_key key; 85680b6794dSChris Mason struct btrfs_root_item *new_root_item; 8573063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 8583063d29fSChris Mason struct btrfs_root *root = pending->root; 8596bdb72deSSage Weil struct btrfs_root *parent_root; 86098c9942aSLiu Bo struct btrfs_block_rsv *rsv; 8616bdb72deSSage Weil struct inode *parent_inode; 8626a912213SJosef Bacik struct dentry *parent; 863a22285a6SYan, Zheng struct dentry *dentry; 8643063d29fSChris Mason struct extent_buffer *tmp; 865925baeddSChris Mason struct extent_buffer *old; 8663063d29fSChris Mason int ret; 867d68fc57bSYan, Zheng u64 to_reserve = 0; 8686bdb72deSSage Weil u64 index = 0; 869a22285a6SYan, Zheng u64 objectid; 870b83cc969SLi Zefan u64 root_flags; 8713063d29fSChris Mason 87298c9942aSLiu Bo rsv = trans->block_rsv; 87398c9942aSLiu Bo 87480b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 87580b6794dSChris Mason if (!new_root_item) { 876a22285a6SYan, Zheng pending->error = -ENOMEM; 87780b6794dSChris Mason goto fail; 87880b6794dSChris Mason } 879a22285a6SYan, Zheng 880581bb050SLi Zefan ret = btrfs_find_free_objectid(tree_root, &objectid); 881a22285a6SYan, Zheng if (ret) { 882a22285a6SYan, Zheng pending->error = ret; 8833063d29fSChris Mason goto fail; 884a22285a6SYan, Zheng } 8853063d29fSChris Mason 8863fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 887d68fc57bSYan, Zheng 888d68fc57bSYan, Zheng if (to_reserve > 0) { 88962f30c54SMiao Xie ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv, 8908bb8ab2eSJosef Bacik to_reserve); 891d68fc57bSYan, Zheng if (ret) { 892d68fc57bSYan, Zheng pending->error = ret; 893d68fc57bSYan, Zheng goto fail; 894d68fc57bSYan, Zheng } 895d68fc57bSYan, Zheng } 896d68fc57bSYan, Zheng 8973063d29fSChris Mason key.objectid = objectid; 898a22285a6SYan, Zheng key.offset = (u64)-1; 899a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 9003063d29fSChris Mason 901a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 9026bdb72deSSage Weil 903a22285a6SYan, Zheng dentry = pending->dentry; 9046a912213SJosef Bacik parent = dget_parent(dentry); 9056a912213SJosef Bacik parent_inode = parent->d_inode; 906a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 9077585717fSChris Mason record_root_in_trans(trans, parent_root); 908a22285a6SYan, Zheng 9096bdb72deSSage Weil /* 9106bdb72deSSage Weil * insert the directory item 9116bdb72deSSage Weil */ 9126bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 9136bdb72deSSage Weil BUG_ON(ret); 9146bdb72deSSage Weil ret = btrfs_insert_dir_item(trans, parent_root, 915a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len, 91616cdcec7SMiao Xie parent_inode, &key, 917a22285a6SYan, Zheng BTRFS_FT_DIR, index); 9186bdb72deSSage Weil BUG_ON(ret); 9196bdb72deSSage Weil 920a22285a6SYan, Zheng btrfs_i_size_write(parent_inode, parent_inode->i_size + 921a22285a6SYan, Zheng dentry->d_name.len * 2); 9226bdb72deSSage Weil ret = btrfs_update_inode(trans, parent_root, parent_inode); 9236bdb72deSSage Weil BUG_ON(ret); 9246bdb72deSSage Weil 925e999376fSChris Mason /* 926e999376fSChris Mason * pull in the delayed directory update 927e999376fSChris Mason * and the delayed inode item 928e999376fSChris Mason * otherwise we corrupt the FS during 929e999376fSChris Mason * snapshot 930e999376fSChris Mason */ 931e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 932e999376fSChris Mason BUG_ON(ret); 933e999376fSChris Mason 9347585717fSChris Mason record_root_in_trans(trans, root); 9356bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 9366bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 93708fe4db1SLi Zefan btrfs_check_and_init_root_item(new_root_item); 9386bdb72deSSage Weil 939b83cc969SLi Zefan root_flags = btrfs_root_flags(new_root_item); 940b83cc969SLi Zefan if (pending->readonly) 941b83cc969SLi Zefan root_flags |= BTRFS_ROOT_SUBVOL_RDONLY; 942b83cc969SLi Zefan else 943b83cc969SLi Zefan root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY; 944b83cc969SLi Zefan btrfs_set_root_flags(new_root_item, root_flags); 945b83cc969SLi Zefan 946925baeddSChris Mason old = btrfs_lock_root_node(root); 9479fa8cfe7SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 9485d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 9493063d29fSChris Mason 950925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 951925baeddSChris Mason btrfs_tree_unlock(old); 952925baeddSChris Mason free_extent_buffer(old); 9533063d29fSChris Mason 954*f1ebcc74SLiu Bo /* see comments in should_cow_block() */ 955*f1ebcc74SLiu Bo root->force_cow = 1; 956*f1ebcc74SLiu Bo smp_wmb(); 957*f1ebcc74SLiu Bo 9585d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 959a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 960a22285a6SYan, Zheng key.offset = trans->transid; 961a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 962925baeddSChris Mason btrfs_tree_unlock(tmp); 9633063d29fSChris Mason free_extent_buffer(tmp); 9640660b5afSChris Mason BUG_ON(ret); 9650660b5afSChris Mason 966a22285a6SYan, Zheng /* 967a22285a6SYan, Zheng * insert root back/forward references 968a22285a6SYan, Zheng */ 969a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 970a22285a6SYan, Zheng parent_root->root_key.objectid, 97133345d01SLi Zefan btrfs_ino(parent_inode), index, 972a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 973a22285a6SYan, Zheng BUG_ON(ret); 9746a912213SJosef Bacik dput(parent); 975a22285a6SYan, Zheng 976a22285a6SYan, Zheng key.offset = (u64)-1; 977a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 978a22285a6SYan, Zheng BUG_ON(IS_ERR(pending->snap)); 979d68fc57bSYan, Zheng 9803fd0a558SYan, Zheng btrfs_reloc_post_snapshot(trans, pending); 9813063d29fSChris Mason fail: 9826bdb72deSSage Weil kfree(new_root_item); 98398c9942aSLiu Bo trans->block_rsv = rsv; 984a22285a6SYan, Zheng btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); 985a22285a6SYan, Zheng return 0; 9863063d29fSChris Mason } 9873063d29fSChris Mason 988d352ac68SChris Mason /* 989d352ac68SChris Mason * create all the snapshots we've scheduled for creation 990d352ac68SChris Mason */ 99180b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 9923063d29fSChris Mason struct btrfs_fs_info *fs_info) 9933063d29fSChris Mason { 9943063d29fSChris Mason struct btrfs_pending_snapshot *pending; 9953063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 9963de4586cSChris Mason int ret; 9973de4586cSChris Mason 998c6e30871SQinghuang Feng list_for_each_entry(pending, head, list) { 9993de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 10003de4586cSChris Mason BUG_ON(ret); 10013de4586cSChris Mason } 10023de4586cSChris Mason return 0; 10033de4586cSChris Mason } 10043de4586cSChris Mason 10055d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 10065d4f98a2SYan Zheng { 10075d4f98a2SYan Zheng struct btrfs_root_item *root_item; 10085d4f98a2SYan Zheng struct btrfs_super_block *super; 10095d4f98a2SYan Zheng 10106c41761fSDavid Sterba super = root->fs_info->super_copy; 10115d4f98a2SYan Zheng 10125d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 10135d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 10145d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 10155d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 10165d4f98a2SYan Zheng 10175d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 10185d4f98a2SYan Zheng super->root = root_item->bytenr; 10195d4f98a2SYan Zheng super->generation = root_item->generation; 10205d4f98a2SYan Zheng super->root_level = root_item->level; 102173bc1876SJosef Bacik if (btrfs_test_opt(root, SPACE_CACHE)) 10220af3d00bSJosef Bacik super->cache_generation = root_item->generation; 10235d4f98a2SYan Zheng } 10245d4f98a2SYan Zheng 1025f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1026f36f3042SChris Mason { 1027f36f3042SChris Mason int ret = 0; 1028a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 1029f36f3042SChris Mason if (info->running_transaction) 1030f36f3042SChris Mason ret = info->running_transaction->in_commit; 1031a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 1032f36f3042SChris Mason return ret; 1033f36f3042SChris Mason } 1034f36f3042SChris Mason 10358929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 10368929ecfaSYan, Zheng { 10378929ecfaSYan, Zheng int ret = 0; 1038a4abeea4SJosef Bacik spin_lock(&info->trans_lock); 10398929ecfaSYan, Zheng if (info->running_transaction) 10408929ecfaSYan, Zheng ret = info->running_transaction->blocked; 1041a4abeea4SJosef Bacik spin_unlock(&info->trans_lock); 10428929ecfaSYan, Zheng return ret; 10438929ecfaSYan, Zheng } 10448929ecfaSYan, Zheng 1045bb9c12c9SSage Weil /* 1046bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1047bb9c12c9SSage Weil * transaction joins 1048bb9c12c9SSage Weil */ 1049bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1050bb9c12c9SSage Weil struct btrfs_transaction *trans) 1051bb9c12c9SSage Weil { 105272d63ed6SLi Zefan wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit); 1053bb9c12c9SSage Weil } 1054bb9c12c9SSage Weil 1055bb9c12c9SSage Weil /* 1056bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1057bb9c12c9SSage Weil * caller holds ref. 1058bb9c12c9SSage Weil */ 1059bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1060bb9c12c9SSage Weil struct btrfs_transaction *trans) 1061bb9c12c9SSage Weil { 106272d63ed6SLi Zefan wait_event(root->fs_info->transaction_wait, 106372d63ed6SLi Zefan trans->commit_done || (trans->in_commit && !trans->blocked)); 1064bb9c12c9SSage Weil } 1065bb9c12c9SSage Weil 1066bb9c12c9SSage Weil /* 1067bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1068bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1069bb9c12c9SSage Weil */ 1070bb9c12c9SSage Weil struct btrfs_async_commit { 1071bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1072bb9c12c9SSage Weil struct btrfs_root *root; 1073bb9c12c9SSage Weil struct delayed_work work; 1074bb9c12c9SSage Weil }; 1075bb9c12c9SSage Weil 1076bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1077bb9c12c9SSage Weil { 1078bb9c12c9SSage Weil struct btrfs_async_commit *ac = 1079bb9c12c9SSage Weil container_of(work, struct btrfs_async_commit, work.work); 1080bb9c12c9SSage Weil 1081bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1082bb9c12c9SSage Weil kfree(ac); 1083bb9c12c9SSage Weil } 1084bb9c12c9SSage Weil 1085bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1086bb9c12c9SSage Weil struct btrfs_root *root, 1087bb9c12c9SSage Weil int wait_for_unblock) 1088bb9c12c9SSage Weil { 1089bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1090bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1091bb9c12c9SSage Weil 1092bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1093db5b493aSTsutomu Itoh if (!ac) 1094db5b493aSTsutomu Itoh return -ENOMEM; 1095bb9c12c9SSage Weil 1096bb9c12c9SSage Weil INIT_DELAYED_WORK(&ac->work, do_async_commit); 1097bb9c12c9SSage Weil ac->root = root; 10987a7eaa40SJosef Bacik ac->newtrans = btrfs_join_transaction(root); 10993612b495STsutomu Itoh if (IS_ERR(ac->newtrans)) { 11003612b495STsutomu Itoh int err = PTR_ERR(ac->newtrans); 11013612b495STsutomu Itoh kfree(ac); 11023612b495STsutomu Itoh return err; 11033612b495STsutomu Itoh } 1104bb9c12c9SSage Weil 1105bb9c12c9SSage Weil /* take transaction reference */ 1106bb9c12c9SSage Weil cur_trans = trans->transaction; 110713c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 1108bb9c12c9SSage Weil 1109bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 1110bb9c12c9SSage Weil schedule_delayed_work(&ac->work, 0); 1111bb9c12c9SSage Weil 1112bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1113bb9c12c9SSage Weil if (wait_for_unblock) 1114bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1115bb9c12c9SSage Weil else 1116bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1117bb9c12c9SSage Weil 111838e88054SSage Weil if (current->journal_info == trans) 111938e88054SSage Weil current->journal_info = NULL; 112038e88054SSage Weil 112138e88054SSage Weil put_transaction(cur_trans); 1122bb9c12c9SSage Weil return 0; 1123bb9c12c9SSage Weil } 1124bb9c12c9SSage Weil 1125bb9c12c9SSage Weil /* 1126bb9c12c9SSage Weil * btrfs_transaction state sequence: 1127bb9c12c9SSage Weil * in_commit = 0, blocked = 0 (initial) 1128bb9c12c9SSage Weil * in_commit = 1, blocked = 1 1129bb9c12c9SSage Weil * blocked = 0 1130bb9c12c9SSage Weil * commit_done = 1 1131bb9c12c9SSage Weil */ 113279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 113379154b1bSChris Mason struct btrfs_root *root) 113479154b1bSChris Mason { 113515ee9bc7SJosef Bacik unsigned long joined = 0; 113679154b1bSChris Mason struct btrfs_transaction *cur_trans; 11378fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 113879154b1bSChris Mason DEFINE_WAIT(wait); 113915ee9bc7SJosef Bacik int ret; 114089573b9cSChris Mason int should_grow = 0; 114189573b9cSChris Mason unsigned long now = get_seconds(); 1142dccae999SSage Weil int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); 114379154b1bSChris Mason 11445a3f23d5SChris Mason btrfs_run_ordered_operations(root, 0); 11455a3f23d5SChris Mason 1146b24e03dbSJosef Bacik btrfs_trans_release_metadata(trans, root); 11479c8d86dbSJosef Bacik trans->block_rsv = NULL; 11489c8d86dbSJosef Bacik 114956bec294SChris Mason /* make a pass through all the delayed refs we have so far 115056bec294SChris Mason * any runnings procs may add more while we are here 115156bec294SChris Mason */ 115256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 115356bec294SChris Mason BUG_ON(ret); 115456bec294SChris Mason 1155b7ec40d7SChris Mason cur_trans = trans->transaction; 115656bec294SChris Mason /* 115756bec294SChris Mason * set the flushing flag so procs in this transaction have to 115856bec294SChris Mason * start sending their work down. 115956bec294SChris Mason */ 1160b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 116156bec294SChris Mason 1162c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 116356bec294SChris Mason BUG_ON(ret); 116456bec294SChris Mason 1165a4abeea4SJosef Bacik spin_lock(&cur_trans->commit_lock); 1166b7ec40d7SChris Mason if (cur_trans->in_commit) { 1167a4abeea4SJosef Bacik spin_unlock(&cur_trans->commit_lock); 116813c5a93eSJosef Bacik atomic_inc(&cur_trans->use_count); 116979154b1bSChris Mason btrfs_end_transaction(trans, root); 1170ccd467d6SChris Mason 1171b9c8300cSLi Zefan wait_for_commit(root, cur_trans); 117215ee9bc7SJosef Bacik 117379154b1bSChris Mason put_transaction(cur_trans); 117415ee9bc7SJosef Bacik 117579154b1bSChris Mason return 0; 117679154b1bSChris Mason } 11774313b399SChris Mason 11782c90e5d6SChris Mason trans->transaction->in_commit = 1; 1179f9295749SChris Mason trans->transaction->blocked = 1; 1180a4abeea4SJosef Bacik spin_unlock(&cur_trans->commit_lock); 1181bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1182bb9c12c9SSage Weil 1183a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1184ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1185ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1186ccd467d6SChris Mason struct btrfs_transaction, list); 1187ccd467d6SChris Mason if (!prev_trans->commit_done) { 118813c5a93eSJosef Bacik atomic_inc(&prev_trans->use_count); 1189a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1190ccd467d6SChris Mason 1191ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1192ccd467d6SChris Mason 119315ee9bc7SJosef Bacik put_transaction(prev_trans); 1194a4abeea4SJosef Bacik } else { 1195a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1196ccd467d6SChris Mason } 1197a4abeea4SJosef Bacik } else { 1198a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1199ccd467d6SChris Mason } 120015ee9bc7SJosef Bacik 120189573b9cSChris Mason if (now < cur_trans->start_time || now - cur_trans->start_time < 1) 120289573b9cSChris Mason should_grow = 1; 120389573b9cSChris Mason 120415ee9bc7SJosef Bacik do { 12057ea394f1SYan Zheng int snap_pending = 0; 1206a4abeea4SJosef Bacik 120715ee9bc7SJosef Bacik joined = cur_trans->num_joined; 12087ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 12097ea394f1SYan Zheng snap_pending = 1; 12107ea394f1SYan Zheng 12112c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 121215ee9bc7SJosef Bacik 12130bdb1db2SSage Weil if (flush_on_commit || snap_pending) { 121424bbcf04SYan, Zheng btrfs_start_delalloc_inodes(root, 1); 121524bbcf04SYan, Zheng ret = btrfs_wait_ordered_extents(root, 0, 1); 1216ebecd3d9SSage Weil BUG_ON(ret); 12177ea394f1SYan Zheng } 12187ea394f1SYan Zheng 121916cdcec7SMiao Xie ret = btrfs_run_delayed_items(trans, root); 122016cdcec7SMiao Xie BUG_ON(ret); 122116cdcec7SMiao Xie 12225a3f23d5SChris Mason /* 12235a3f23d5SChris Mason * rename don't use btrfs_join_transaction, so, once we 12245a3f23d5SChris Mason * set the transaction to blocked above, we aren't going 12255a3f23d5SChris Mason * to get any new ordered operations. We can safely run 12265a3f23d5SChris Mason * it here and no for sure that nothing new will be added 12275a3f23d5SChris Mason * to the list 12285a3f23d5SChris Mason */ 12295a3f23d5SChris Mason btrfs_run_ordered_operations(root, 1); 12305a3f23d5SChris Mason 1231ed3b3d31SChris Mason prepare_to_wait(&cur_trans->writer_wait, &wait, 1232ed3b3d31SChris Mason TASK_UNINTERRUPTIBLE); 1233ed3b3d31SChris Mason 123413c5a93eSJosef Bacik if (atomic_read(&cur_trans->num_writers) > 1) 123599d16cbcSSage Weil schedule_timeout(MAX_SCHEDULE_TIMEOUT); 123699d16cbcSSage Weil else if (should_grow) 123799d16cbcSSage Weil schedule_timeout(1); 123815ee9bc7SJosef Bacik 123915ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 1240ed0ca140SJosef Bacik } while (atomic_read(&cur_trans->num_writers) > 1 || 1241ed0ca140SJosef Bacik (should_grow && cur_trans->num_joined != joined)); 1242ed0ca140SJosef Bacik 1243ed0ca140SJosef Bacik /* 1244ed0ca140SJosef Bacik * Ok now we need to make sure to block out any other joins while we 1245ed0ca140SJosef Bacik * commit the transaction. We could have started a join before setting 1246ed0ca140SJosef Bacik * no_join so make sure to wait for num_writers to == 1 again. 1247ed0ca140SJosef Bacik */ 1248a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1249a4abeea4SJosef Bacik root->fs_info->trans_no_join = 1; 1250a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1251ed0ca140SJosef Bacik wait_event(cur_trans->writer_wait, 1252ed0ca140SJosef Bacik atomic_read(&cur_trans->num_writers) == 1); 125315ee9bc7SJosef Bacik 12547585717fSChris Mason /* 12557585717fSChris Mason * the reloc mutex makes sure that we stop 12567585717fSChris Mason * the balancing code from coming in and moving 12577585717fSChris Mason * extents around in the middle of the commit 12587585717fSChris Mason */ 12597585717fSChris Mason mutex_lock(&root->fs_info->reloc_mutex); 12607585717fSChris Mason 1261e999376fSChris Mason ret = btrfs_run_delayed_items(trans, root); 12623063d29fSChris Mason BUG_ON(ret); 12633063d29fSChris Mason 1264e999376fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 126516cdcec7SMiao Xie BUG_ON(ret); 126616cdcec7SMiao Xie 126756bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 126856bec294SChris Mason BUG_ON(ret); 126956bec294SChris Mason 1270e999376fSChris Mason /* 1271e999376fSChris Mason * make sure none of the code above managed to slip in a 1272e999376fSChris Mason * delayed item 1273e999376fSChris Mason */ 1274e999376fSChris Mason btrfs_assert_delayed_root_empty(root); 1275e999376fSChris Mason 12762c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1277dc17ff8fSChris Mason 1278a2de733cSArne Jansen btrfs_scrub_pause(root); 1279e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1280e02119d5SChris Mason * various roots consistent with each other. Every pointer 1281e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1282e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1283e02119d5SChris Mason * the tree logging code from jumping in and changing any 1284e02119d5SChris Mason * of the trees. 1285e02119d5SChris Mason * 1286e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1287e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1288e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1289e02119d5SChris Mason * from now until after the super is written, we avoid races 1290e02119d5SChris Mason * with the tree-log code. 1291e02119d5SChris Mason */ 1292e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 12931a40e23bSZheng Yan 12945d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 129554aa1f4dSChris Mason BUG_ON(ret); 129654aa1f4dSChris Mason 12975d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1298e02119d5SChris Mason * safe to free the root of tree log roots 1299e02119d5SChris Mason */ 1300e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1301e02119d5SChris Mason 13025d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 130379154b1bSChris Mason BUG_ON(ret); 130454aa1f4dSChris Mason 130511833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 130611833d66SYan Zheng 130778fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 13085f39d397SChris Mason 13095d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 13105d4f98a2SYan Zheng root->fs_info->tree_root->node); 1311817d52f8SJosef Bacik switch_commit_root(root->fs_info->tree_root); 13125d4f98a2SYan Zheng 13135d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 13145d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1315817d52f8SJosef Bacik switch_commit_root(root->fs_info->chunk_root); 13165d4f98a2SYan Zheng 13175d4f98a2SYan Zheng update_super_roots(root); 1318e02119d5SChris Mason 1319e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 13206c41761fSDavid Sterba btrfs_set_super_log_root(root->fs_info->super_copy, 0); 13216c41761fSDavid Sterba btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 1322e02119d5SChris Mason } 1323e02119d5SChris Mason 13246c41761fSDavid Sterba memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 13256c41761fSDavid Sterba sizeof(*root->fs_info->super_copy)); 1326ccd467d6SChris Mason 1327f9295749SChris Mason trans->transaction->blocked = 0; 1328a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 1329a4abeea4SJosef Bacik root->fs_info->running_transaction = NULL; 1330a4abeea4SJosef Bacik root->fs_info->trans_no_join = 0; 1331a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 13327585717fSChris Mason mutex_unlock(&root->fs_info->reloc_mutex); 1333b7ec40d7SChris Mason 1334f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1335e6dcd2dcSChris Mason 133679154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 133779154b1bSChris Mason BUG_ON(ret); 1338a512bbf8SYan Zheng write_ctree_super(trans, root, 0); 13394313b399SChris Mason 1340e02119d5SChris Mason /* 1341e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1342e02119d5SChris Mason * to go about their business 1343e02119d5SChris Mason */ 1344e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1345e02119d5SChris Mason 134611833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 13474313b399SChris Mason 13482c90e5d6SChris Mason cur_trans->commit_done = 1; 1349b7ec40d7SChris Mason 135015ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 1351817d52f8SJosef Bacik 13522c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 13533de4586cSChris Mason 1354a4abeea4SJosef Bacik spin_lock(&root->fs_info->trans_lock); 135513c5a93eSJosef Bacik list_del_init(&cur_trans->list); 1356a4abeea4SJosef Bacik spin_unlock(&root->fs_info->trans_lock); 1357a4abeea4SJosef Bacik 135879154b1bSChris Mason put_transaction(cur_trans); 135978fae27eSChris Mason put_transaction(cur_trans); 136058176a96SJosef Bacik 13611abe9b8aSliubo trace_btrfs_transaction_commit(root); 13621abe9b8aSliubo 1363a2de733cSArne Jansen btrfs_scrub_continue(root); 1364a2de733cSArne Jansen 13659ed74f2dSJosef Bacik if (current->journal_info == trans) 13669ed74f2dSJosef Bacik current->journal_info = NULL; 13679ed74f2dSJosef Bacik 13682c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 136924bbcf04SYan, Zheng 137024bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 137124bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 137224bbcf04SYan, Zheng 137379154b1bSChris Mason return ret; 137479154b1bSChris Mason } 137579154b1bSChris Mason 1376d352ac68SChris Mason /* 1377d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1378d352ac68SChris Mason */ 1379e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1380e9d0b13bSChris Mason { 13815d4f98a2SYan Zheng LIST_HEAD(list); 13825d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1383e9d0b13bSChris Mason 1384a4abeea4SJosef Bacik spin_lock(&fs_info->trans_lock); 13855d4f98a2SYan Zheng list_splice_init(&fs_info->dead_roots, &list); 1386a4abeea4SJosef Bacik spin_unlock(&fs_info->trans_lock); 13875d4f98a2SYan Zheng 13885d4f98a2SYan Zheng while (!list_empty(&list)) { 13895d4f98a2SYan Zheng root = list_entry(list.next, struct btrfs_root, root_list); 139076dda93cSYan, Zheng list_del(&root->root_list); 139176dda93cSYan, Zheng 139216cdcec7SMiao Xie btrfs_kill_all_delayed_nodes(root); 139316cdcec7SMiao Xie 139476dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 139576dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 13963fd0a558SYan, Zheng btrfs_drop_snapshot(root, NULL, 0); 139776dda93cSYan, Zheng else 13983fd0a558SYan, Zheng btrfs_drop_snapshot(root, NULL, 1); 1399e9d0b13bSChris Mason } 1400e9d0b13bSChris Mason return 0; 1401e9d0b13bSChris Mason } 1402