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> 2034088780SChris Mason #include <linux/sched.h> 21d3c2fdcfSChris Mason #include <linux/writeback.h> 225f39d397SChris Mason #include <linux/pagemap.h> 235f2cc086SChris Mason #include <linux/blkdev.h> 2479154b1bSChris Mason #include "ctree.h" 2579154b1bSChris Mason #include "disk-io.h" 2679154b1bSChris Mason #include "transaction.h" 27925baeddSChris Mason #include "locking.h" 2831153d81SYan Zheng #include "ref-cache.h" 29e02119d5SChris Mason #include "tree-log.h" 3079154b1bSChris Mason 310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 320f7d52f4SChris Mason 3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3479154b1bSChris Mason { 352c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3679154b1bSChris Mason transaction->use_count--; 3778fae27eSChris Mason if (transaction->use_count == 0) { 388fd17795SChris Mason list_del_init(&transaction->list); 392c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 402c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4179154b1bSChris Mason } 4278fae27eSChris Mason } 4379154b1bSChris Mason 44d352ac68SChris Mason /* 45d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 46d352ac68SChris Mason */ 4780b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 4879154b1bSChris Mason { 4979154b1bSChris Mason struct btrfs_transaction *cur_trans; 5079154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5179154b1bSChris Mason if (!cur_trans) { 522c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 532c90e5d6SChris Mason GFP_NOFS); 5479154b1bSChris Mason BUG_ON(!cur_trans); 550f7d52f4SChris Mason root->fs_info->generation++; 5615ee9bc7SJosef Bacik cur_trans->num_writers = 1; 5715ee9bc7SJosef Bacik cur_trans->num_joined = 0; 580f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 5979154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6079154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6179154b1bSChris Mason cur_trans->in_commit = 0; 62f9295749SChris Mason cur_trans->blocked = 0; 63d5719762SChris Mason cur_trans->use_count = 1; 6479154b1bSChris Mason cur_trans->commit_done = 0; 6508607c1bSChris Mason cur_trans->start_time = get_seconds(); 6656bec294SChris Mason 6756bec294SChris Mason cur_trans->delayed_refs.root.rb_node = NULL; 6856bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 69c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 70c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 7156bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 72c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 7356bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 7456bec294SChris Mason 753063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 768fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 77d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 785f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 795f39d397SChris Mason GFP_NOFS); 8048ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8148ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8248ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8315ee9bc7SJosef Bacik } else { 8479154b1bSChris Mason cur_trans->num_writers++; 8515ee9bc7SJosef Bacik cur_trans->num_joined++; 8615ee9bc7SJosef Bacik } 8715ee9bc7SJosef Bacik 8879154b1bSChris Mason return 0; 8979154b1bSChris Mason } 9079154b1bSChris Mason 91d352ac68SChris Mason /* 92d397712bSChris Mason * this does all the record keeping required to make sure that a reference 93d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 94d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 95d397712bSChris Mason * when the transaction commits 96d352ac68SChris Mason */ 97e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 986702ed49SChris Mason { 99f321e491SYan Zheng struct btrfs_dirty_root *dirty; 1006702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 1016702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 1026702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 1036702ed49SChris Mason if (root->root_item.refs != 0) { 1046702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1056702ed49SChris Mason (unsigned long)root->root_key.objectid, 1066702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 10731153d81SYan Zheng 10831153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10931153d81SYan Zheng BUG_ON(!dirty); 11031153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 11131153d81SYan Zheng BUG_ON(!dirty->root); 11231153d81SYan Zheng dirty->latest_root = root; 11331153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 11431153d81SYan Zheng 115925baeddSChris Mason root->commit_root = btrfs_root_node(root); 11631153d81SYan Zheng 11731153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 11831153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 119bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 12031153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 1215b21f2edSZheng Yan mutex_init(&dirty->root->log_mutex); 122bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 12331153d81SYan Zheng dirty->root->node = root->commit_root; 12431153d81SYan Zheng dirty->root->commit_root = NULL; 125bcc63abbSYan 126bcc63abbSYan spin_lock(&root->list_lock); 127bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 128bcc63abbSYan spin_unlock(&root->list_lock); 129bcc63abbSYan 130bcc63abbSYan root->dirty_root = dirty; 1316702ed49SChris Mason } else { 1326702ed49SChris Mason WARN_ON(1); 1336702ed49SChris Mason } 1346702ed49SChris Mason root->last_trans = running_trans_id; 1356702ed49SChris Mason } 1366702ed49SChris Mason return 0; 1376702ed49SChris Mason } 1386702ed49SChris Mason 139d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 140d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 141d352ac68SChris Mason * transaction might not be fully on disk. 142d352ac68SChris Mason */ 14337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 14479154b1bSChris Mason { 145f9295749SChris Mason struct btrfs_transaction *cur_trans; 14679154b1bSChris Mason 147f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 14837d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 149f9295749SChris Mason DEFINE_WAIT(wait); 150f9295749SChris Mason cur_trans->use_count++; 151f9295749SChris Mason while (1) { 152f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 153f9295749SChris Mason TASK_UNINTERRUPTIBLE); 154f9295749SChris Mason if (cur_trans->blocked) { 155f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 156f9295749SChris Mason schedule(); 157f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 158f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 159f9295749SChris Mason &wait); 160f9295749SChris Mason } else { 161f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 162f9295749SChris Mason &wait); 163f9295749SChris Mason break; 164f9295749SChris Mason } 165f9295749SChris Mason } 166f9295749SChris Mason put_transaction(cur_trans); 167f9295749SChris Mason } 16837d1aeeeSChris Mason } 16937d1aeeeSChris Mason 170e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1719ca9ee09SSage Weil int num_blocks, int wait) 17237d1aeeeSChris Mason { 17337d1aeeeSChris Mason struct btrfs_trans_handle *h = 17437d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 17537d1aeeeSChris Mason int ret; 17637d1aeeeSChris Mason 17737d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1784bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1794bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 18037d1aeeeSChris Mason wait_current_trans(root); 18179154b1bSChris Mason ret = join_transaction(root); 18279154b1bSChris Mason BUG_ON(ret); 1830f7d52f4SChris Mason 184e02119d5SChris Mason btrfs_record_root_in_trans(root); 1856702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 18679154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 18779154b1bSChris Mason h->blocks_reserved = num_blocks; 18879154b1bSChris Mason h->blocks_used = 0; 189d2fb3437SYan Zheng h->block_group = 0; 19026b8003fSChris Mason h->alloc_exclude_nr = 0; 19126b8003fSChris Mason h->alloc_exclude_start = 0; 19256bec294SChris Mason h->delayed_ref_updates = 0; 193b7ec40d7SChris Mason 19479154b1bSChris Mason root->fs_info->running_transaction->use_count++; 19579154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 19679154b1bSChris Mason return h; 19779154b1bSChris Mason } 19879154b1bSChris Mason 199f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 200f9295749SChris Mason int num_blocks) 201f9295749SChris Mason { 2029ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 203f9295749SChris Mason } 204f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 205f9295749SChris Mason int num_blocks) 206f9295749SChris Mason { 2079ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 208f9295749SChris Mason } 209f9295749SChris Mason 2109ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2119ca9ee09SSage Weil int num_blocks) 2129ca9ee09SSage Weil { 2139ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 2149ca9ee09SSage Weil } 2159ca9ee09SSage Weil 216d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 21789ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 21889ce8a63SChris Mason struct btrfs_transaction *commit) 21989ce8a63SChris Mason { 22089ce8a63SChris Mason DEFINE_WAIT(wait); 22189ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22289ce8a63SChris Mason while (!commit->commit_done) { 22389ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 22489ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 22589ce8a63SChris Mason if (commit->commit_done) 22689ce8a63SChris Mason break; 22789ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22889ce8a63SChris Mason schedule(); 22989ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 23089ce8a63SChris Mason } 23189ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23289ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 23389ce8a63SChris Mason return 0; 23489ce8a63SChris Mason } 23589ce8a63SChris Mason 236d352ac68SChris Mason /* 237d397712bSChris Mason * rate limit against the drop_snapshot code. This helps to slow down new 238d397712bSChris Mason * operations if the drop_snapshot code isn't able to keep up. 239d352ac68SChris Mason */ 24037d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 241ab78c84dSChris Mason { 242ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2432dd3e67bSChris Mason int harder_count = 0; 244ab78c84dSChris Mason 2452dd3e67bSChris Mason harder: 246ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 247ab78c84dSChris Mason DEFINE_WAIT(wait); 248ab78c84dSChris Mason int thr; 249ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 250ab78c84dSChris Mason 251ab78c84dSChris Mason do { 252ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 253ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 254ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 255ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 256ab78c84dSChris Mason break; 257ab78c84dSChris Mason } 258ab78c84dSChris Mason schedule(); 259ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 260ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2612dd3e67bSChris Mason harder_count++; 2622dd3e67bSChris Mason 2632dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2642dd3e67bSChris Mason harder_count < 2) 2652dd3e67bSChris Mason goto harder; 2662dd3e67bSChris Mason 2672dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2682dd3e67bSChris Mason harder_count < 10) 2692dd3e67bSChris Mason goto harder; 2702dd3e67bSChris Mason 2712dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2722dd3e67bSChris Mason harder_count < 20) 2732dd3e67bSChris Mason goto harder; 274ab78c84dSChris Mason } 275ab78c84dSChris Mason } 276ab78c84dSChris Mason 27737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 27837d1aeeeSChris Mason { 27937d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2809ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 28137d1aeeeSChris Mason wait_current_trans(root); 28237d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 28337d1aeeeSChris Mason throttle_on_drops(root); 28437d1aeeeSChris Mason } 28537d1aeeeSChris Mason 28689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 28789ce8a63SChris Mason struct btrfs_root *root, int throttle) 28879154b1bSChris Mason { 28979154b1bSChris Mason struct btrfs_transaction *cur_trans; 290ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 291c3e69d58SChris Mason int count = 0; 292d6e4a428SChris Mason 293c3e69d58SChris Mason while (count < 4) { 294c3e69d58SChris Mason unsigned long cur = trans->delayed_ref_updates; 295c3e69d58SChris Mason trans->delayed_ref_updates = 0; 296c3e69d58SChris Mason if (cur && 297c3e69d58SChris Mason trans->transaction->delayed_refs.num_heads_ready > 64) { 298c3e69d58SChris Mason trans->delayed_ref_updates = 0; 299b7ec40d7SChris Mason 300b7ec40d7SChris Mason /* 301b7ec40d7SChris Mason * do a full flush if the transaction is trying 302b7ec40d7SChris Mason * to close 303b7ec40d7SChris Mason */ 304b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) 305b7ec40d7SChris Mason cur = 0; 306c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 307c3e69d58SChris Mason } else { 308c3e69d58SChris Mason break; 309c3e69d58SChris Mason } 310c3e69d58SChris Mason count++; 31156bec294SChris Mason } 31256bec294SChris Mason 313ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 314ab78c84dSChris Mason cur_trans = info->running_transaction; 315ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 316d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 317ccd467d6SChris Mason cur_trans->num_writers--; 31889ce8a63SChris Mason 31979154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 32079154b1bSChris Mason wake_up(&cur_trans->writer_wait); 32179154b1bSChris Mason put_transaction(cur_trans); 322ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 323d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 3242c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 325ab78c84dSChris Mason 326ab78c84dSChris Mason if (throttle) 32737d1aeeeSChris Mason throttle_on_drops(root); 328ab78c84dSChris Mason 32979154b1bSChris Mason return 0; 33079154b1bSChris Mason } 33179154b1bSChris Mason 33289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 33389ce8a63SChris Mason struct btrfs_root *root) 33489ce8a63SChris Mason { 33589ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 33689ce8a63SChris Mason } 33789ce8a63SChris Mason 33889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 33989ce8a63SChris Mason struct btrfs_root *root) 34089ce8a63SChris Mason { 34189ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 34289ce8a63SChris Mason } 34389ce8a63SChris Mason 344d352ac68SChris Mason /* 345d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 346d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 347d352ac68SChris Mason * those extents are on disk for transaction or log commit 348d352ac68SChris Mason */ 349d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 350d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 35179154b1bSChris Mason { 3527c4452b9SChris Mason int ret; 353777e6bd7SChris Mason int err = 0; 3547c4452b9SChris Mason int werr = 0; 3557c4452b9SChris Mason struct page *page; 3567c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 357777e6bd7SChris Mason u64 start = 0; 3585f39d397SChris Mason u64 end; 3595f39d397SChris Mason unsigned long index; 3607c4452b9SChris Mason 3617c4452b9SChris Mason while (1) { 362777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3635f39d397SChris Mason EXTENT_DIRTY); 3645f39d397SChris Mason if (ret) 3657c4452b9SChris Mason break; 3665f39d397SChris Mason while (start <= end) { 367777e6bd7SChris Mason cond_resched(); 368777e6bd7SChris Mason 3695f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 37035ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3714bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3727c4452b9SChris Mason if (!page) 3737c4452b9SChris Mason continue; 3744bef0848SChris Mason 3754bef0848SChris Mason btree_lock_page_hook(page); 3764bef0848SChris Mason if (!page->mapping) { 3774bef0848SChris Mason unlock_page(page); 3784bef0848SChris Mason page_cache_release(page); 3794bef0848SChris Mason continue; 3804bef0848SChris Mason } 3814bef0848SChris Mason 3826702ed49SChris Mason if (PageWriteback(page)) { 3836702ed49SChris Mason if (PageDirty(page)) 3846702ed49SChris Mason wait_on_page_writeback(page); 3856702ed49SChris Mason else { 3866702ed49SChris Mason unlock_page(page); 3876702ed49SChris Mason page_cache_release(page); 3886702ed49SChris Mason continue; 3896702ed49SChris Mason } 3906702ed49SChris Mason } 3917c4452b9SChris Mason err = write_one_page(page, 0); 3927c4452b9SChris Mason if (err) 3937c4452b9SChris Mason werr = err; 3947c4452b9SChris Mason page_cache_release(page); 3957c4452b9SChris Mason } 3967c4452b9SChris Mason } 397777e6bd7SChris Mason while (1) { 398777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 399777e6bd7SChris Mason EXTENT_DIRTY); 400777e6bd7SChris Mason if (ret) 401777e6bd7SChris Mason break; 402777e6bd7SChris Mason 403777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 404777e6bd7SChris Mason while (start <= end) { 405777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 406777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 407777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 408777e6bd7SChris Mason if (!page) 409777e6bd7SChris Mason continue; 410777e6bd7SChris Mason if (PageDirty(page)) { 4114bef0848SChris Mason btree_lock_page_hook(page); 4124bef0848SChris Mason wait_on_page_writeback(page); 413777e6bd7SChris Mason err = write_one_page(page, 0); 414777e6bd7SChris Mason if (err) 415777e6bd7SChris Mason werr = err; 416777e6bd7SChris Mason } 417777e6bd7SChris Mason wait_on_page_writeback(page); 418777e6bd7SChris Mason page_cache_release(page); 419777e6bd7SChris Mason cond_resched(); 420777e6bd7SChris Mason } 421777e6bd7SChris Mason } 4227c4452b9SChris Mason if (err) 4237c4452b9SChris Mason werr = err; 4247c4452b9SChris Mason return werr; 42579154b1bSChris Mason } 42679154b1bSChris Mason 427d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 428d0c803c4SChris Mason struct btrfs_root *root) 429d0c803c4SChris Mason { 430d0c803c4SChris Mason if (!trans || !trans->transaction) { 431d0c803c4SChris Mason struct inode *btree_inode; 432d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 433d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 434d0c803c4SChris Mason } 435d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 436d0c803c4SChris Mason &trans->transaction->dirty_pages); 437d0c803c4SChris Mason } 438d0c803c4SChris Mason 439d352ac68SChris Mason /* 440d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 441d352ac68SChris Mason * 442d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 443d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 444d352ac68SChris Mason * allocation tree. 445d352ac68SChris Mason * 446d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 447d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 448d352ac68SChris Mason */ 4490b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 45079154b1bSChris Mason struct btrfs_root *root) 45179154b1bSChris Mason { 45279154b1bSChris Mason int ret; 4530b86a832SChris Mason u64 old_root_bytenr; 4540b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 45579154b1bSChris Mason 4560b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 45756bec294SChris Mason 45856bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 45956bec294SChris Mason BUG_ON(ret); 46087ef2bb4SChris Mason 46179154b1bSChris Mason while (1) { 4620b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4630b86a832SChris Mason if (old_root_bytenr == root->node->start) 46479154b1bSChris Mason break; 4650b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 4660b86a832SChris Mason root->node->start); 4670b86a832SChris Mason btrfs_set_root_level(&root->root_item, 4680b86a832SChris Mason btrfs_header_level(root->node)); 46984234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, trans->transid); 47087ef2bb4SChris Mason 47179154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4720b86a832SChris Mason &root->root_key, 4730b86a832SChris Mason &root->root_item); 47479154b1bSChris Mason BUG_ON(ret); 4750b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 47656bec294SChris Mason 47756bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 47856bec294SChris Mason BUG_ON(ret); 4790b86a832SChris Mason } 4800b86a832SChris Mason return 0; 4810b86a832SChris Mason } 4820b86a832SChris Mason 483d352ac68SChris Mason /* 484d352ac68SChris Mason * update all the cowonly tree roots on disk 485d352ac68SChris Mason */ 4860b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4870b86a832SChris Mason struct btrfs_root *root) 4880b86a832SChris Mason { 4890b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4900b86a832SChris Mason struct list_head *next; 49184234f3aSYan Zheng struct extent_buffer *eb; 49256bec294SChris Mason int ret; 49384234f3aSYan Zheng 49456bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 49556bec294SChris Mason BUG_ON(ret); 49687ef2bb4SChris Mason 49784234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 4989fa8cfe7SChris Mason btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); 49984234f3aSYan Zheng btrfs_tree_unlock(eb); 50084234f3aSYan Zheng free_extent_buffer(eb); 5010b86a832SChris Mason 50256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 50356bec294SChris Mason BUG_ON(ret); 50487ef2bb4SChris Mason 5050b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 5060b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 5070b86a832SChris Mason list_del_init(next); 5080b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 50987ef2bb4SChris Mason 5100b86a832SChris Mason update_cowonly_root(trans, root); 51156bec294SChris Mason 51256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 51356bec294SChris Mason BUG_ON(ret); 51479154b1bSChris Mason } 51579154b1bSChris Mason return 0; 51679154b1bSChris Mason } 51779154b1bSChris Mason 518d352ac68SChris Mason /* 519d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 520d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 521d352ac68SChris Mason * be deleted 522d352ac68SChris Mason */ 523b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 5245eda7b5eSChris Mason { 525f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5265eda7b5eSChris Mason 5275eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 5285eda7b5eSChris Mason if (!dirty) 5295eda7b5eSChris Mason return -ENOMEM; 5305eda7b5eSChris Mason dirty->root = root; 5315ce14bbcSChris Mason dirty->latest_root = latest; 532b48652c1SYan Zheng 533b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 534b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 535b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 5365eda7b5eSChris Mason return 0; 5375eda7b5eSChris Mason } 5385eda7b5eSChris Mason 539d352ac68SChris Mason /* 540d352ac68SChris Mason * at transaction commit time we need to schedule the old roots for 541d352ac68SChris Mason * deletion via btrfs_drop_snapshot. This runs through all the 542d352ac68SChris Mason * reference counted roots that were modified in the current 543d352ac68SChris Mason * transaction and puts them into the drop list 544d352ac68SChris Mason */ 54580b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 54635b7e476SChris Mason struct radix_tree_root *radix, 54735b7e476SChris Mason struct list_head *list) 5480f7d52f4SChris Mason { 549f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5500f7d52f4SChris Mason struct btrfs_root *gang[8]; 5510f7d52f4SChris Mason struct btrfs_root *root; 5520f7d52f4SChris Mason int i; 5530f7d52f4SChris Mason int ret; 55454aa1f4dSChris Mason int err = 0; 5555eda7b5eSChris Mason u32 refs; 55654aa1f4dSChris Mason 5570f7d52f4SChris Mason while (1) { 5580f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 5590f7d52f4SChris Mason ARRAY_SIZE(gang), 5600f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 5610f7d52f4SChris Mason if (ret == 0) 5620f7d52f4SChris Mason break; 5630f7d52f4SChris Mason for (i = 0; i < ret; i++) { 5640f7d52f4SChris Mason root = gang[i]; 5652619ba1fSChris Mason radix_tree_tag_clear(radix, 5662619ba1fSChris Mason (unsigned long)root->root_key.objectid, 5670f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 56831153d81SYan Zheng 56931153d81SYan Zheng BUG_ON(!root->ref_tree); 570017e5369SChris Mason dirty = root->dirty_root; 57131153d81SYan Zheng 572e02119d5SChris Mason btrfs_free_log(trans, root); 573f82d02d9SYan Zheng btrfs_free_reloc_root(trans, root); 574e02119d5SChris Mason 5750f7d52f4SChris Mason if (root->commit_root == root->node) { 576db94535dSChris Mason WARN_ON(root->node->start != 577db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 57831153d81SYan Zheng 5795f39d397SChris Mason free_extent_buffer(root->commit_root); 5800f7d52f4SChris Mason root->commit_root = NULL; 5817ea394f1SYan Zheng root->dirty_root = NULL; 58231153d81SYan Zheng 583bcc63abbSYan spin_lock(&root->list_lock); 584bcc63abbSYan list_del_init(&dirty->root->dead_list); 585bcc63abbSYan spin_unlock(&root->list_lock); 586bcc63abbSYan 58731153d81SYan Zheng kfree(dirty->root); 58831153d81SYan Zheng kfree(dirty); 58958176a96SJosef Bacik 59058176a96SJosef Bacik /* make sure to update the root on disk 59158176a96SJosef Bacik * so we get any updates to the block used 59258176a96SJosef Bacik * counts 59358176a96SJosef Bacik */ 59458176a96SJosef Bacik err = btrfs_update_root(trans, 59558176a96SJosef Bacik root->fs_info->tree_root, 59658176a96SJosef Bacik &root->root_key, 59758176a96SJosef Bacik &root->root_item); 5980f7d52f4SChris Mason continue; 5990f7d52f4SChris Mason } 6009f3a7427SChris Mason 6019f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 6029f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 6039f3a7427SChris Mason root->root_item.drop_level = 0; 6040f7d52f4SChris Mason root->commit_root = NULL; 6057ea394f1SYan Zheng root->dirty_root = NULL; 6060f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 607db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 608db94535dSChris Mason root->node->start); 609db94535dSChris Mason btrfs_set_root_level(&root->root_item, 610db94535dSChris Mason btrfs_header_level(root->node)); 61184234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, 61284234f3aSYan Zheng root->root_key.offset); 61384234f3aSYan Zheng 6140f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 6150f7d52f4SChris Mason &root->root_key, 6160f7d52f4SChris Mason &root->root_item); 61754aa1f4dSChris Mason if (err) 61854aa1f4dSChris Mason break; 6199f3a7427SChris Mason 6209f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 6219f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 6225eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 6239f3a7427SChris Mason &dirty->root->root_key, 6249f3a7427SChris Mason &dirty->root->root_item); 6255eda7b5eSChris Mason 6265eda7b5eSChris Mason BUG_ON(err); 6279f3a7427SChris Mason if (refs == 1) { 6280f7d52f4SChris Mason list_add(&dirty->list, list); 6299f3a7427SChris Mason } else { 6309f3a7427SChris Mason WARN_ON(1); 63131153d81SYan Zheng free_extent_buffer(dirty->root->node); 6329f3a7427SChris Mason kfree(dirty->root); 6335eda7b5eSChris Mason kfree(dirty); 6340f7d52f4SChris Mason } 6350f7d52f4SChris Mason } 6369f3a7427SChris Mason } 63754aa1f4dSChris Mason return err; 6380f7d52f4SChris Mason } 6390f7d52f4SChris Mason 640d352ac68SChris Mason /* 641d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 642d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 643d352ac68SChris Mason */ 644e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 645e9d0b13bSChris Mason { 646e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 647e9d0b13bSChris Mason int ret; 648e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 649d3c2fdcfSChris Mason unsigned long nr; 650e9d0b13bSChris Mason 651a2135011SChris Mason smp_mb(); 652e9d0b13bSChris Mason if (root->defrag_running) 653e9d0b13bSChris Mason return 0; 654e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6556b80053dSChris Mason while (1) { 656e9d0b13bSChris Mason root->defrag_running = 1; 657e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 658d3c2fdcfSChris Mason nr = trans->blocks_used; 659e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 660d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 661e9d0b13bSChris Mason cond_resched(); 662e9d0b13bSChris Mason 663e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6643f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 665e9d0b13bSChris Mason break; 666e9d0b13bSChris Mason } 667e9d0b13bSChris Mason root->defrag_running = 0; 668a2135011SChris Mason smp_mb(); 669e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 670e9d0b13bSChris Mason return 0; 671e9d0b13bSChris Mason } 672e9d0b13bSChris Mason 673d352ac68SChris Mason /* 674b7ec40d7SChris Mason * when dropping snapshots, we generate a ton of delayed refs, and it makes 675b7ec40d7SChris Mason * sense not to join the transaction while it is trying to flush the current 676b7ec40d7SChris Mason * queue of delayed refs out. 677b7ec40d7SChris Mason * 678b7ec40d7SChris Mason * This is used by the drop snapshot code only 679b7ec40d7SChris Mason */ 680b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) 681b7ec40d7SChris Mason { 682b7ec40d7SChris Mason DEFINE_WAIT(wait); 683b7ec40d7SChris Mason 684b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 685b7ec40d7SChris Mason while (info->running_transaction && 686b7ec40d7SChris Mason info->running_transaction->delayed_refs.flushing) { 687b7ec40d7SChris Mason prepare_to_wait(&info->transaction_wait, &wait, 688b7ec40d7SChris Mason TASK_UNINTERRUPTIBLE); 689b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 690b7ec40d7SChris Mason schedule(); 691b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 692b7ec40d7SChris Mason finish_wait(&info->transaction_wait, &wait); 693b7ec40d7SChris Mason } 694b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 695b7ec40d7SChris Mason return 0; 696b7ec40d7SChris Mason } 697b7ec40d7SChris Mason 698b7ec40d7SChris Mason /* 699d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 700d352ac68SChris Mason * all of them 701d352ac68SChris Mason */ 70280b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 70335b7e476SChris Mason struct list_head *list) 7040f7d52f4SChris Mason { 705f321e491SYan Zheng struct btrfs_dirty_root *dirty; 7060f7d52f4SChris Mason struct btrfs_trans_handle *trans; 707d3c2fdcfSChris Mason unsigned long nr; 708db94535dSChris Mason u64 num_bytes; 709db94535dSChris Mason u64 bytes_used; 710bcc63abbSYan u64 max_useless; 71154aa1f4dSChris Mason int ret = 0; 7129f3a7427SChris Mason int err; 7139f3a7427SChris Mason 7140f7d52f4SChris Mason while (!list_empty(list)) { 71558176a96SJosef Bacik struct btrfs_root *root; 71658176a96SJosef Bacik 717f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 7180f7d52f4SChris Mason list_del_init(&dirty->list); 7195eda7b5eSChris Mason 720db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 72158176a96SJosef Bacik root = dirty->latest_root; 722a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 72358176a96SJosef Bacik 7249f3a7427SChris Mason while (1) { 725b7ec40d7SChris Mason /* 726b7ec40d7SChris Mason * we don't want to jump in and create a bunch of 727b7ec40d7SChris Mason * delayed refs if the transaction is starting to close 728b7ec40d7SChris Mason */ 729b7ec40d7SChris Mason wait_transaction_pre_flush(tree_root->fs_info); 7300f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 731b7ec40d7SChris Mason 732b7ec40d7SChris Mason /* 733b7ec40d7SChris Mason * we've joined a transaction, make sure it isn't 734b7ec40d7SChris Mason * closing right now 735b7ec40d7SChris Mason */ 736b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) { 737b7ec40d7SChris Mason btrfs_end_transaction(trans, tree_root); 738b7ec40d7SChris Mason continue; 739b7ec40d7SChris Mason } 740b7ec40d7SChris Mason 7415b21f2edSZheng Yan mutex_lock(&root->fs_info->drop_mutex); 7429f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 743d397712bSChris Mason if (ret != -EAGAIN) 7449f3a7427SChris Mason break; 7455b21f2edSZheng Yan mutex_unlock(&root->fs_info->drop_mutex); 74658176a96SJosef Bacik 7479f3a7427SChris Mason err = btrfs_update_root(trans, 7489f3a7427SChris Mason tree_root, 7499f3a7427SChris Mason &dirty->root->root_key, 7509f3a7427SChris Mason &dirty->root->root_item); 7519f3a7427SChris Mason if (err) 7529f3a7427SChris Mason ret = err; 753d3c2fdcfSChris Mason nr = trans->blocks_used; 754017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7550f7d52f4SChris Mason BUG_ON(ret); 756a2135011SChris Mason 757d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 7584dc11904SChris Mason cond_resched(); 7599f3a7427SChris Mason } 7609f3a7427SChris Mason BUG_ON(ret); 761a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 762017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 76358176a96SJosef Bacik 764db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 765db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 766db94535dSChris Mason if (num_bytes) { 76724562425SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 768e02119d5SChris Mason btrfs_record_root_in_trans(root); 76924562425SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 7705f39d397SChris Mason btrfs_set_root_used(&root->root_item, 771db94535dSChris Mason bytes_used - num_bytes); 77258176a96SJosef Bacik } 773a2135011SChris Mason 7749f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 77558176a96SJosef Bacik if (ret) { 77658176a96SJosef Bacik BUG(); 77754aa1f4dSChris Mason break; 77858176a96SJosef Bacik } 779a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 780a2135011SChris Mason 781bcc63abbSYan spin_lock(&root->list_lock); 782bcc63abbSYan list_del_init(&dirty->root->dead_list); 783bcc63abbSYan if (!list_empty(&root->dead_list)) { 784bcc63abbSYan struct btrfs_root *oldest; 785bcc63abbSYan oldest = list_entry(root->dead_list.prev, 786bcc63abbSYan struct btrfs_root, dead_list); 787bcc63abbSYan max_useless = oldest->root_key.offset - 1; 788bcc63abbSYan } else { 789bcc63abbSYan max_useless = root->root_key.offset - 1; 790bcc63abbSYan } 791bcc63abbSYan spin_unlock(&root->list_lock); 792bcc63abbSYan 793d3c2fdcfSChris Mason nr = trans->blocks_used; 7940f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7950f7d52f4SChris Mason BUG_ON(ret); 7965eda7b5eSChris Mason 797e4657689SZheng Yan ret = btrfs_remove_leaf_refs(root, max_useless, 0); 798bcc63abbSYan BUG_ON(ret); 799bcc63abbSYan 800f510cfecSChris Mason free_extent_buffer(dirty->root->node); 8015eda7b5eSChris Mason kfree(dirty->root); 8020f7d52f4SChris Mason kfree(dirty); 803d3c2fdcfSChris Mason 804d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 8054dc11904SChris Mason cond_resched(); 8060f7d52f4SChris Mason } 80754aa1f4dSChris Mason return ret; 8080f7d52f4SChris Mason } 8090f7d52f4SChris Mason 810d352ac68SChris Mason /* 811d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 812d352ac68SChris Mason * transaction commit. This does the actual creation 813d352ac68SChris Mason */ 81480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 8153063d29fSChris Mason struct btrfs_fs_info *fs_info, 8163063d29fSChris Mason struct btrfs_pending_snapshot *pending) 8173063d29fSChris Mason { 8183063d29fSChris Mason struct btrfs_key key; 81980b6794dSChris Mason struct btrfs_root_item *new_root_item; 8203063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 8213063d29fSChris Mason struct btrfs_root *root = pending->root; 8223063d29fSChris Mason struct extent_buffer *tmp; 823925baeddSChris Mason struct extent_buffer *old; 8243063d29fSChris Mason int ret; 8253063d29fSChris Mason u64 objectid; 8263063d29fSChris Mason 82780b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 82880b6794dSChris Mason if (!new_root_item) { 82980b6794dSChris Mason ret = -ENOMEM; 83080b6794dSChris Mason goto fail; 83180b6794dSChris Mason } 8323063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 8333063d29fSChris Mason if (ret) 8343063d29fSChris Mason goto fail; 8353063d29fSChris Mason 83680ff3856SYan Zheng btrfs_record_root_in_trans(root); 83780ff3856SYan Zheng btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 83880b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 8393063d29fSChris Mason 8403063d29fSChris Mason key.objectid = objectid; 8415b21f2edSZheng Yan key.offset = trans->transid; 8423063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 8433063d29fSChris Mason 844925baeddSChris Mason old = btrfs_lock_root_node(root); 8459fa8cfe7SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 8463063d29fSChris Mason 847925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 848925baeddSChris Mason btrfs_tree_unlock(old); 849925baeddSChris Mason free_extent_buffer(old); 8503063d29fSChris Mason 85180b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 85280b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 85384234f3aSYan Zheng btrfs_set_root_generation(new_root_item, trans->transid); 8543063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 85580b6794dSChris Mason new_root_item); 856925baeddSChris Mason btrfs_tree_unlock(tmp); 8573063d29fSChris Mason free_extent_buffer(tmp); 8583063d29fSChris Mason if (ret) 8593063d29fSChris Mason goto fail; 8603063d29fSChris Mason 8613de4586cSChris Mason key.offset = (u64)-1; 8623de4586cSChris Mason memcpy(&pending->root_key, &key, sizeof(key)); 8633de4586cSChris Mason fail: 8643de4586cSChris Mason kfree(new_root_item); 8653de4586cSChris Mason return ret; 8663de4586cSChris Mason } 8673de4586cSChris Mason 8683de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info, 8693de4586cSChris Mason struct btrfs_pending_snapshot *pending) 8703de4586cSChris Mason { 8713de4586cSChris Mason int ret; 8723de4586cSChris Mason int namelen; 8733de4586cSChris Mason u64 index = 0; 8743de4586cSChris Mason struct btrfs_trans_handle *trans; 8753de4586cSChris Mason struct inode *parent_inode; 8763de4586cSChris Mason struct inode *inode; 8770660b5afSChris Mason struct btrfs_root *parent_root; 8783de4586cSChris Mason 8793394e160SChris Mason parent_inode = pending->dentry->d_parent->d_inode; 8800660b5afSChris Mason parent_root = BTRFS_I(parent_inode)->root; 881180591bcSYan Zheng trans = btrfs_join_transaction(parent_root, 1); 8823de4586cSChris Mason 8833063d29fSChris Mason /* 8843063d29fSChris Mason * insert the directory item 8853063d29fSChris Mason */ 8863b96362cSSven Wegener namelen = strlen(pending->name); 8873de4586cSChris Mason ret = btrfs_set_inode_index(parent_inode, &index); 8880660b5afSChris Mason ret = btrfs_insert_dir_item(trans, parent_root, 8893b96362cSSven Wegener pending->name, namelen, 8903de4586cSChris Mason parent_inode->i_ino, 8913de4586cSChris Mason &pending->root_key, BTRFS_FT_DIR, index); 8923063d29fSChris Mason 8933063d29fSChris Mason if (ret) 8943063d29fSChris Mason goto fail; 8950660b5afSChris Mason 89652c26179SYan Zheng btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); 89752c26179SYan Zheng ret = btrfs_update_inode(trans, parent_root, parent_inode); 89852c26179SYan Zheng BUG_ON(ret); 89952c26179SYan Zheng 9000660b5afSChris Mason /* add the backref first */ 9010660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 9020660b5afSChris Mason pending->root_key.objectid, 9030660b5afSChris Mason BTRFS_ROOT_BACKREF_KEY, 9040660b5afSChris Mason parent_root->root_key.objectid, 9050660b5afSChris Mason parent_inode->i_ino, index, pending->name, 9060660b5afSChris Mason namelen); 9070660b5afSChris Mason 9080660b5afSChris Mason BUG_ON(ret); 9090660b5afSChris Mason 9100660b5afSChris Mason /* now add the forward ref */ 9110660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 9120660b5afSChris Mason parent_root->root_key.objectid, 9130660b5afSChris Mason BTRFS_ROOT_REF_KEY, 9140660b5afSChris Mason pending->root_key.objectid, 9150660b5afSChris Mason parent_inode->i_ino, index, pending->name, 9160660b5afSChris Mason namelen); 9170660b5afSChris Mason 9183de4586cSChris Mason inode = btrfs_lookup_dentry(parent_inode, pending->dentry); 9193de4586cSChris Mason d_instantiate(pending->dentry, inode); 9203063d29fSChris Mason fail: 9213de4586cSChris Mason btrfs_end_transaction(trans, fs_info->fs_root); 9223063d29fSChris Mason return ret; 9233063d29fSChris Mason } 9243063d29fSChris Mason 925d352ac68SChris Mason /* 926d352ac68SChris Mason * create all the snapshots we've scheduled for creation 927d352ac68SChris Mason */ 92880b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 9293063d29fSChris Mason struct btrfs_fs_info *fs_info) 9303063d29fSChris Mason { 9313063d29fSChris Mason struct btrfs_pending_snapshot *pending; 9323063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 9333de4586cSChris Mason int ret; 9343de4586cSChris Mason 935c6e30871SQinghuang Feng list_for_each_entry(pending, head, list) { 9363de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 9373de4586cSChris Mason BUG_ON(ret); 9383de4586cSChris Mason } 9393de4586cSChris Mason return 0; 9403de4586cSChris Mason } 9413de4586cSChris Mason 9423de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans, 9433de4586cSChris Mason struct btrfs_fs_info *fs_info) 9443de4586cSChris Mason { 9453de4586cSChris Mason struct btrfs_pending_snapshot *pending; 9463de4586cSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 9473063d29fSChris Mason int ret; 9483063d29fSChris Mason 9493063d29fSChris Mason while (!list_empty(head)) { 9503063d29fSChris Mason pending = list_entry(head->next, 9513063d29fSChris Mason struct btrfs_pending_snapshot, list); 9523de4586cSChris Mason ret = finish_pending_snapshot(fs_info, pending); 9533063d29fSChris Mason BUG_ON(ret); 9543063d29fSChris Mason list_del(&pending->list); 9553063d29fSChris Mason kfree(pending->name); 9563063d29fSChris Mason kfree(pending); 9573063d29fSChris Mason } 958dc17ff8fSChris Mason return 0; 959dc17ff8fSChris Mason } 960dc17ff8fSChris Mason 96179154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 96279154b1bSChris Mason struct btrfs_root *root) 96379154b1bSChris Mason { 96415ee9bc7SJosef Bacik unsigned long joined = 0; 96515ee9bc7SJosef Bacik unsigned long timeout = 1; 96679154b1bSChris Mason struct btrfs_transaction *cur_trans; 9678fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 9680b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 9690f7d52f4SChris Mason struct list_head dirty_fs_roots; 970d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 97179154b1bSChris Mason DEFINE_WAIT(wait); 97215ee9bc7SJosef Bacik int ret; 97389573b9cSChris Mason int should_grow = 0; 97489573b9cSChris Mason unsigned long now = get_seconds(); 975*dccae999SSage Weil int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); 97679154b1bSChris Mason 9775a3f23d5SChris Mason btrfs_run_ordered_operations(root, 0); 9785a3f23d5SChris Mason 97956bec294SChris Mason /* make a pass through all the delayed refs we have so far 98056bec294SChris Mason * any runnings procs may add more while we are here 98156bec294SChris Mason */ 98256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 98356bec294SChris Mason BUG_ON(ret); 98456bec294SChris Mason 985b7ec40d7SChris Mason cur_trans = trans->transaction; 98656bec294SChris Mason /* 98756bec294SChris Mason * set the flushing flag so procs in this transaction have to 98856bec294SChris Mason * start sending their work down. 98956bec294SChris Mason */ 990b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 99156bec294SChris Mason 992c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 99356bec294SChris Mason BUG_ON(ret); 99456bec294SChris Mason 99579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 996b7ec40d7SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 997b7ec40d7SChris Mason if (cur_trans->in_commit) { 998b7ec40d7SChris Mason cur_trans->use_count++; 999ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 100079154b1bSChris Mason btrfs_end_transaction(trans, root); 1001ccd467d6SChris Mason 100279154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 100379154b1bSChris Mason BUG_ON(ret); 100415ee9bc7SJosef Bacik 100515ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 100679154b1bSChris Mason put_transaction(cur_trans); 100715ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 100815ee9bc7SJosef Bacik 100979154b1bSChris Mason return 0; 101079154b1bSChris Mason } 10114313b399SChris Mason 10124313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 10134313b399SChris Mason if (!pinned_copy) 10144313b399SChris Mason return -ENOMEM; 10154313b399SChris Mason 1016d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 10174313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 10184313b399SChris Mason 10192c90e5d6SChris Mason trans->transaction->in_commit = 1; 1020f9295749SChris Mason trans->transaction->blocked = 1; 1021ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1022ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1023ccd467d6SChris Mason struct btrfs_transaction, list); 1024ccd467d6SChris Mason if (!prev_trans->commit_done) { 1025ccd467d6SChris Mason prev_trans->use_count++; 1026ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1027ccd467d6SChris Mason 1028ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1029ccd467d6SChris Mason 1030ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 103115ee9bc7SJosef Bacik put_transaction(prev_trans); 1032ccd467d6SChris Mason } 1033ccd467d6SChris Mason } 103415ee9bc7SJosef Bacik 103589573b9cSChris Mason if (now < cur_trans->start_time || now - cur_trans->start_time < 1) 103689573b9cSChris Mason should_grow = 1; 103789573b9cSChris Mason 103815ee9bc7SJosef Bacik do { 10397ea394f1SYan Zheng int snap_pending = 0; 104015ee9bc7SJosef Bacik joined = cur_trans->num_joined; 10417ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 10427ea394f1SYan Zheng snap_pending = 1; 10437ea394f1SYan Zheng 10442c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 104515ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 104679154b1bSChris Mason TASK_UNINTERRUPTIBLE); 104715ee9bc7SJosef Bacik 104815ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 104915ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 105089573b9cSChris Mason else if (should_grow) 105115ee9bc7SJosef Bacik timeout = 1; 105215ee9bc7SJosef Bacik 105379154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 105415ee9bc7SJosef Bacik 1055*dccae999SSage Weil if (flush_on_commit || snap_pending) { 1056*dccae999SSage Weil if (flush_on_commit) 1057*dccae999SSage Weil btrfs_start_delalloc_inodes(root); 10587ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 10597ea394f1SYan Zheng BUG_ON(ret); 10607ea394f1SYan Zheng } 10617ea394f1SYan Zheng 10625a3f23d5SChris Mason /* 10635a3f23d5SChris Mason * rename don't use btrfs_join_transaction, so, once we 10645a3f23d5SChris Mason * set the transaction to blocked above, we aren't going 10655a3f23d5SChris Mason * to get any new ordered operations. We can safely run 10665a3f23d5SChris Mason * it here and no for sure that nothing new will be added 10675a3f23d5SChris Mason * to the list 10685a3f23d5SChris Mason */ 10695a3f23d5SChris Mason btrfs_run_ordered_operations(root, 1); 10705a3f23d5SChris Mason 107189573b9cSChris Mason smp_mb(); 107289573b9cSChris Mason if (cur_trans->num_writers > 1 || should_grow) 107315ee9bc7SJosef Bacik schedule_timeout(timeout); 107415ee9bc7SJosef Bacik 107579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 107615ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 107715ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 107889573b9cSChris Mason (should_grow && cur_trans->num_joined != joined)); 107915ee9bc7SJosef Bacik 10803063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 10813063d29fSChris Mason BUG_ON(ret); 10823063d29fSChris Mason 108356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 108456bec294SChris Mason BUG_ON(ret); 108556bec294SChris Mason 10862c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1087dc17ff8fSChris Mason 1088e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1089e02119d5SChris Mason * various roots consistent with each other. Every pointer 1090e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1091e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1092e02119d5SChris Mason * the tree logging code from jumping in and changing any 1093e02119d5SChris Mason * of the trees. 1094e02119d5SChris Mason * 1095e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1096e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1097e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1098e02119d5SChris Mason * from now until after the super is written, we avoid races 1099e02119d5SChris Mason * with the tree-log code. 1100e02119d5SChris Mason */ 1101e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 11021a40e23bSZheng Yan /* 11031a40e23bSZheng Yan * keep tree reloc code from adding new reloc trees 11041a40e23bSZheng Yan */ 11051a40e23bSZheng Yan mutex_lock(&root->fs_info->tree_reloc_mutex); 11061a40e23bSZheng Yan 1107e02119d5SChris Mason 110854aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 110954aa1f4dSChris Mason &dirty_fs_roots); 111054aa1f4dSChris Mason BUG_ON(ret); 111154aa1f4dSChris Mason 1112e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 1113e02119d5SChris Mason * safe to free the root of tree log roots 1114e02119d5SChris Mason */ 1115e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1116e02119d5SChris Mason 111779154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 111879154b1bSChris Mason BUG_ON(ret); 111954aa1f4dSChris Mason 112078fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 1121cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 112278fae27eSChris Mason root->fs_info->running_transaction = NULL; 1123cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 11244b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 11254b52dff6SChris Mason cur_trans->transid); 11264b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 1127db94535dSChris Mason root->fs_info->tree_root->node->start); 1128db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 1129db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 11305f39d397SChris Mason 11310b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 11320b86a832SChris Mason chunk_root->node->start); 11330b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 11340b86a832SChris Mason btrfs_header_level(chunk_root->node)); 113584234f3aSYan Zheng btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy, 113684234f3aSYan Zheng btrfs_header_generation(chunk_root->node)); 1137e02119d5SChris Mason 1138e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 1139e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 1140e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 1141e02119d5SChris Mason } 1142e02119d5SChris Mason 1143a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 11444b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 1145ccd467d6SChris Mason 11464313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 1147ccd467d6SChris Mason 1148f9295749SChris Mason trans->transaction->blocked = 0; 1149b7ec40d7SChris Mason 1150e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 1151f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1152e6dcd2dcSChris Mason 115378fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 115479154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 115579154b1bSChris Mason BUG_ON(ret); 1156a512bbf8SYan Zheng write_ctree_super(trans, root, 0); 11574313b399SChris Mason 1158e02119d5SChris Mason /* 1159e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1160e02119d5SChris Mason * to go about their business 1161e02119d5SChris Mason */ 1162e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1163e02119d5SChris Mason 11644313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 11654313b399SChris Mason kfree(pinned_copy); 11664313b399SChris Mason 11671a40e23bSZheng Yan btrfs_drop_dead_reloc_roots(root); 11681a40e23bSZheng Yan mutex_unlock(&root->fs_info->tree_reloc_mutex); 11691a40e23bSZheng Yan 11703de4586cSChris Mason /* do the directory inserts of any pending snapshot creations */ 11713de4586cSChris Mason finish_pending_snapshots(trans, root->fs_info); 11723de4586cSChris Mason 11731a40e23bSZheng Yan mutex_lock(&root->fs_info->trans_mutex); 11741a40e23bSZheng Yan 11752c90e5d6SChris Mason cur_trans->commit_done = 1; 1176b7ec40d7SChris Mason 117715ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 11782c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 11793de4586cSChris Mason 118079154b1bSChris Mason put_transaction(cur_trans); 118178fae27eSChris Mason put_transaction(cur_trans); 118258176a96SJosef Bacik 1183bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 1184facda1e7SChris Mason if (root->fs_info->closing) 1185facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 118658176a96SJosef Bacik 118778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 11883de4586cSChris Mason 11892c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 119079154b1bSChris Mason 1191d397712bSChris Mason if (root->fs_info->closing) 11920f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 119379154b1bSChris Mason return ret; 119479154b1bSChris Mason } 119579154b1bSChris Mason 1196d352ac68SChris Mason /* 1197d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1198d352ac68SChris Mason */ 1199e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1200e9d0b13bSChris Mason { 1201e9d0b13bSChris Mason struct list_head dirty_roots; 1202e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 1203a74a4b97SChris Mason again: 1204e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1205e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 1206e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1207e9d0b13bSChris Mason 1208e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 1209e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 1210a74a4b97SChris Mason goto again; 1211e9d0b13bSChris Mason } 1212e9d0b13bSChris Mason return 0; 1213e9d0b13bSChris Mason } 1214