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> 2379154b1bSChris Mason #include "ctree.h" 2479154b1bSChris Mason #include "disk-io.h" 2579154b1bSChris Mason #include "transaction.h" 26925baeddSChris Mason #include "locking.h" 2779154b1bSChris Mason 2878fae27eSChris Mason static int total_trans = 0; 292c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 302c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 312c90e5d6SChris Mason 320f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 336702ed49SChris Mason #define BTRFS_ROOT_DEFRAG_TAG 1 340f7d52f4SChris Mason 3580b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3679154b1bSChris Mason { 372c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3879154b1bSChris Mason transaction->use_count--; 3978fae27eSChris Mason if (transaction->use_count == 0) { 4078fae27eSChris Mason WARN_ON(total_trans == 0); 4178fae27eSChris Mason total_trans--; 428fd17795SChris Mason list_del_init(&transaction->list); 432c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 442c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4579154b1bSChris Mason } 4678fae27eSChris Mason } 4779154b1bSChris Mason 4880b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 4979154b1bSChris Mason { 5079154b1bSChris Mason struct btrfs_transaction *cur_trans; 5179154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5279154b1bSChris Mason if (!cur_trans) { 532c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 542c90e5d6SChris Mason GFP_NOFS); 5578fae27eSChris Mason total_trans++; 5679154b1bSChris Mason BUG_ON(!cur_trans); 570f7d52f4SChris Mason root->fs_info->generation++; 58e18e4809SChris Mason root->fs_info->last_alloc = 0; 594529ba49SChris Mason root->fs_info->last_data_alloc = 0; 6015ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6115ee9bc7SJosef Bacik cur_trans->num_joined = 0; 620f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6379154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6479154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6579154b1bSChris Mason cur_trans->in_commit = 0; 66d5719762SChris Mason cur_trans->use_count = 1; 6779154b1bSChris Mason cur_trans->commit_done = 0; 6808607c1bSChris Mason cur_trans->start_time = get_seconds(); 693063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 708fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 71dc17ff8fSChris Mason btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree); 72d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 735f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 745f39d397SChris Mason GFP_NOFS); 7548ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 7648ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 7748ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 7815ee9bc7SJosef Bacik } else { 7979154b1bSChris Mason cur_trans->num_writers++; 8015ee9bc7SJosef Bacik cur_trans->num_joined++; 8115ee9bc7SJosef Bacik } 8215ee9bc7SJosef Bacik 8379154b1bSChris Mason return 0; 8479154b1bSChris Mason } 8579154b1bSChris Mason 8680b6794dSChris Mason static noinline int record_root_in_trans(struct btrfs_root *root) 876702ed49SChris Mason { 886702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 896702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 906702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 916702ed49SChris Mason if (root->root_item.refs != 0) { 926702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 936702ed49SChris Mason (unsigned long)root->root_key.objectid, 946702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 956702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 966702ed49SChris Mason (unsigned long)root->root_key.objectid, 976702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 98925baeddSChris Mason root->commit_root = btrfs_root_node(root); 996702ed49SChris Mason } else { 1006702ed49SChris Mason WARN_ON(1); 1016702ed49SChris Mason } 1026702ed49SChris Mason root->last_trans = running_trans_id; 1036702ed49SChris Mason } 1046702ed49SChris Mason return 0; 1056702ed49SChris Mason } 1066702ed49SChris Mason 10779154b1bSChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 10879154b1bSChris Mason int num_blocks) 10979154b1bSChris Mason { 1102c90e5d6SChris Mason struct btrfs_trans_handle *h = 1112c90e5d6SChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 11279154b1bSChris Mason int ret; 11379154b1bSChris Mason 11479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 11579154b1bSChris Mason ret = join_transaction(root); 11679154b1bSChris Mason BUG_ON(ret); 1170f7d52f4SChris Mason 1186702ed49SChris Mason record_root_in_trans(root); 1196702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 12079154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 12179154b1bSChris Mason h->blocks_reserved = num_blocks; 12279154b1bSChris Mason h->blocks_used = 0; 12331f3c99bSChris Mason h->block_group = NULL; 12426b8003fSChris Mason h->alloc_exclude_nr = 0; 12526b8003fSChris Mason h->alloc_exclude_start = 0; 12679154b1bSChris Mason root->fs_info->running_transaction->use_count++; 12779154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 12879154b1bSChris Mason return h; 12979154b1bSChris Mason } 13079154b1bSChris Mason 13189ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 13289ce8a63SChris Mason struct btrfs_transaction *commit) 13389ce8a63SChris Mason { 13489ce8a63SChris Mason DEFINE_WAIT(wait); 13589ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 13689ce8a63SChris Mason while(!commit->commit_done) { 13789ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 13889ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 13989ce8a63SChris Mason if (commit->commit_done) 14089ce8a63SChris Mason break; 14189ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 14289ce8a63SChris Mason schedule(); 14389ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 14489ce8a63SChris Mason } 14589ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 14689ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 14789ce8a63SChris Mason return 0; 14889ce8a63SChris Mason } 14989ce8a63SChris Mason 15089ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 15189ce8a63SChris Mason struct btrfs_root *root, int throttle) 15279154b1bSChris Mason { 15379154b1bSChris Mason struct btrfs_transaction *cur_trans; 154d6e4a428SChris Mason 15579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 15679154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 157ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 158d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 159ccd467d6SChris Mason cur_trans->num_writers--; 16089ce8a63SChris Mason 16179154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 16279154b1bSChris Mason wake_up(&cur_trans->writer_wait); 16389ce8a63SChris Mason 16489ce8a63SChris Mason if (cur_trans->in_commit && throttle) { 16589ce8a63SChris Mason int ret; 16689ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 16789ce8a63SChris Mason ret = wait_for_commit(root, cur_trans); 16889ce8a63SChris Mason BUG_ON(ret); 16989ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 17089ce8a63SChris Mason } 17189ce8a63SChris Mason 17279154b1bSChris Mason put_transaction(cur_trans); 17379154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 174d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 1752c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 17679154b1bSChris Mason return 0; 17779154b1bSChris Mason } 17879154b1bSChris Mason 17989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 18089ce8a63SChris Mason struct btrfs_root *root) 18189ce8a63SChris Mason { 18289ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 18389ce8a63SChris Mason } 18489ce8a63SChris Mason 18589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 18689ce8a63SChris Mason struct btrfs_root *root) 18789ce8a63SChris Mason { 18889ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 18989ce8a63SChris Mason } 19089ce8a63SChris Mason 19179154b1bSChris Mason 19279154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 19379154b1bSChris Mason struct btrfs_root *root) 19479154b1bSChris Mason { 1957c4452b9SChris Mason int ret; 1967c4452b9SChris Mason int err; 1977c4452b9SChris Mason int werr = 0; 198d1310b2eSChris Mason struct extent_io_tree *dirty_pages; 1997c4452b9SChris Mason struct page *page; 2007c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 2015f39d397SChris Mason u64 start; 2025f39d397SChris Mason u64 end; 2035f39d397SChris Mason unsigned long index; 2047c4452b9SChris Mason 2057c4452b9SChris Mason if (!trans || !trans->transaction) { 2067c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 2077c4452b9SChris Mason } 2087c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 2097c4452b9SChris Mason while(1) { 2105f39d397SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 2115f39d397SChris Mason EXTENT_DIRTY); 2125f39d397SChris Mason if (ret) 2137c4452b9SChris Mason break; 2145f39d397SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 2155f39d397SChris Mason while(start <= end) { 2165f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 21735ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 2185f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 2197c4452b9SChris Mason if (!page) 2207c4452b9SChris Mason continue; 2216702ed49SChris Mason if (PageWriteback(page)) { 2226702ed49SChris Mason if (PageDirty(page)) 2236702ed49SChris Mason wait_on_page_writeback(page); 2246702ed49SChris Mason else { 2256702ed49SChris Mason unlock_page(page); 2266702ed49SChris Mason page_cache_release(page); 2276702ed49SChris Mason continue; 2286702ed49SChris Mason } 2296702ed49SChris Mason } 2307c4452b9SChris Mason err = write_one_page(page, 0); 2317c4452b9SChris Mason if (err) 2327c4452b9SChris Mason werr = err; 2337c4452b9SChris Mason page_cache_release(page); 2347c4452b9SChris Mason } 2357c4452b9SChris Mason } 2367c4452b9SChris Mason err = filemap_fdatawait(btree_inode->i_mapping); 2377c4452b9SChris Mason if (err) 2387c4452b9SChris Mason werr = err; 2397c4452b9SChris Mason return werr; 24079154b1bSChris Mason } 24179154b1bSChris Mason 2420b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 24379154b1bSChris Mason struct btrfs_root *root) 24479154b1bSChris Mason { 24579154b1bSChris Mason int ret; 2460b86a832SChris Mason u64 old_root_bytenr; 2470b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 24879154b1bSChris Mason 2490b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 25079154b1bSChris Mason while(1) { 2510b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 2520b86a832SChris Mason if (old_root_bytenr == root->node->start) 25379154b1bSChris Mason break; 2540b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 2550b86a832SChris Mason root->node->start); 2560b86a832SChris Mason btrfs_set_root_level(&root->root_item, 2570b86a832SChris Mason btrfs_header_level(root->node)); 25879154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 2590b86a832SChris Mason &root->root_key, 2600b86a832SChris Mason &root->root_item); 26179154b1bSChris Mason BUG_ON(ret); 2620b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 2630b86a832SChris Mason } 2640b86a832SChris Mason return 0; 2650b86a832SChris Mason } 2660b86a832SChris Mason 2670b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 2680b86a832SChris Mason struct btrfs_root *root) 2690b86a832SChris Mason { 2700b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 2710b86a832SChris Mason struct list_head *next; 2720b86a832SChris Mason 2730b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 2740b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 2750b86a832SChris Mason list_del_init(next); 2760b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 2770b86a832SChris Mason update_cowonly_root(trans, root); 27879154b1bSChris Mason } 27979154b1bSChris Mason return 0; 28079154b1bSChris Mason } 28179154b1bSChris Mason 2820f7d52f4SChris Mason struct dirty_root { 2830f7d52f4SChris Mason struct list_head list; 2840f7d52f4SChris Mason struct btrfs_root *root; 28558176a96SJosef Bacik struct btrfs_root *latest_root; 2860f7d52f4SChris Mason }; 2870f7d52f4SChris Mason 2885ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 2895ce14bbcSChris Mason struct btrfs_root *latest, 2905ce14bbcSChris Mason struct list_head *dead_list) 2915eda7b5eSChris Mason { 2925eda7b5eSChris Mason struct dirty_root *dirty; 2935eda7b5eSChris Mason 2945eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 2955eda7b5eSChris Mason if (!dirty) 2965eda7b5eSChris Mason return -ENOMEM; 2975eda7b5eSChris Mason dirty->root = root; 2985ce14bbcSChris Mason dirty->latest_root = latest; 2995eda7b5eSChris Mason list_add(&dirty->list, dead_list); 3005eda7b5eSChris Mason return 0; 3015eda7b5eSChris Mason } 3025eda7b5eSChris Mason 30380b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 30435b7e476SChris Mason struct radix_tree_root *radix, 30535b7e476SChris Mason struct list_head *list) 3060f7d52f4SChris Mason { 3070f7d52f4SChris Mason struct dirty_root *dirty; 3080f7d52f4SChris Mason struct btrfs_root *gang[8]; 3090f7d52f4SChris Mason struct btrfs_root *root; 3100f7d52f4SChris Mason int i; 3110f7d52f4SChris Mason int ret; 31254aa1f4dSChris Mason int err = 0; 3135eda7b5eSChris Mason u32 refs; 31454aa1f4dSChris Mason 3150f7d52f4SChris Mason while(1) { 3160f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 3170f7d52f4SChris Mason ARRAY_SIZE(gang), 3180f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 3190f7d52f4SChris Mason if (ret == 0) 3200f7d52f4SChris Mason break; 3210f7d52f4SChris Mason for (i = 0; i < ret; i++) { 3220f7d52f4SChris Mason root = gang[i]; 3232619ba1fSChris Mason radix_tree_tag_clear(radix, 3242619ba1fSChris Mason (unsigned long)root->root_key.objectid, 3250f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 3260f7d52f4SChris Mason if (root->commit_root == root->node) { 327db94535dSChris Mason WARN_ON(root->node->start != 328db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 3295f39d397SChris Mason free_extent_buffer(root->commit_root); 3300f7d52f4SChris Mason root->commit_root = NULL; 33158176a96SJosef Bacik 33258176a96SJosef Bacik /* make sure to update the root on disk 33358176a96SJosef Bacik * so we get any updates to the block used 33458176a96SJosef Bacik * counts 33558176a96SJosef Bacik */ 33658176a96SJosef Bacik err = btrfs_update_root(trans, 33758176a96SJosef Bacik root->fs_info->tree_root, 33858176a96SJosef Bacik &root->root_key, 33958176a96SJosef Bacik &root->root_item); 3400f7d52f4SChris Mason continue; 3410f7d52f4SChris Mason } 3420f7d52f4SChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3430f7d52f4SChris Mason BUG_ON(!dirty); 3449f3a7427SChris Mason dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 3459f3a7427SChris Mason BUG_ON(!dirty->root); 3469f3a7427SChris Mason 3479f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 3489f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 3499f3a7427SChris Mason root->root_item.drop_level = 0; 3509f3a7427SChris Mason 3519f3a7427SChris Mason memcpy(dirty->root, root, sizeof(*root)); 3529f3a7427SChris Mason dirty->root->node = root->commit_root; 35358176a96SJosef Bacik dirty->latest_root = root; 3540f7d52f4SChris Mason root->commit_root = NULL; 3555eda7b5eSChris Mason 3560f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 357db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 358db94535dSChris Mason root->node->start); 359db94535dSChris Mason btrfs_set_root_level(&root->root_item, 360db94535dSChris Mason btrfs_header_level(root->node)); 3610f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 3620f7d52f4SChris Mason &root->root_key, 3630f7d52f4SChris Mason &root->root_item); 36454aa1f4dSChris Mason if (err) 36554aa1f4dSChris Mason break; 3669f3a7427SChris Mason 3679f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 3689f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 3695eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 3709f3a7427SChris Mason &dirty->root->root_key, 3719f3a7427SChris Mason &dirty->root->root_item); 3725eda7b5eSChris Mason 3735eda7b5eSChris Mason BUG_ON(err); 3749f3a7427SChris Mason if (refs == 1) { 3750f7d52f4SChris Mason list_add(&dirty->list, list); 3769f3a7427SChris Mason } else { 3779f3a7427SChris Mason WARN_ON(1); 3789f3a7427SChris Mason kfree(dirty->root); 3795eda7b5eSChris Mason kfree(dirty); 3800f7d52f4SChris Mason } 3810f7d52f4SChris Mason } 3829f3a7427SChris Mason } 38354aa1f4dSChris Mason return err; 3840f7d52f4SChris Mason } 3850f7d52f4SChris Mason 386e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 387e9d0b13bSChris Mason { 388e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 389e9d0b13bSChris Mason int ret; 390e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 391d3c2fdcfSChris Mason unsigned long nr; 392e9d0b13bSChris Mason 393a2135011SChris Mason smp_mb(); 394e9d0b13bSChris Mason if (root->defrag_running) 395e9d0b13bSChris Mason return 0; 396e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 3976b80053dSChris Mason while (1) { 398e9d0b13bSChris Mason root->defrag_running = 1; 399e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 400d3c2fdcfSChris Mason nr = trans->blocks_used; 401e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 402d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 403e9d0b13bSChris Mason cond_resched(); 404e9d0b13bSChris Mason 405e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 406e9d0b13bSChris Mason if (ret != -EAGAIN) 407e9d0b13bSChris Mason break; 408e9d0b13bSChris Mason } 409e9d0b13bSChris Mason root->defrag_running = 0; 410a2135011SChris Mason smp_mb(); 411e9d0b13bSChris Mason radix_tree_tag_clear(&info->fs_roots_radix, 412e9d0b13bSChris Mason (unsigned long)root->root_key.objectid, 413e9d0b13bSChris Mason BTRFS_ROOT_DEFRAG_TAG); 414e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 415e9d0b13bSChris Mason return 0; 416e9d0b13bSChris Mason } 417e9d0b13bSChris Mason 4186702ed49SChris Mason int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info) 4196702ed49SChris Mason { 4206702ed49SChris Mason struct btrfs_root *gang[1]; 4216702ed49SChris Mason struct btrfs_root *root; 4226702ed49SChris Mason int i; 4236702ed49SChris Mason int ret; 4246702ed49SChris Mason int err = 0; 4256702ed49SChris Mason u64 last = 0; 4266702ed49SChris Mason 4276702ed49SChris Mason while(1) { 4286702ed49SChris Mason ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix, 4296702ed49SChris Mason (void **)gang, last, 4306702ed49SChris Mason ARRAY_SIZE(gang), 4316702ed49SChris Mason BTRFS_ROOT_DEFRAG_TAG); 4326702ed49SChris Mason if (ret == 0) 4336702ed49SChris Mason break; 4346702ed49SChris Mason for (i = 0; i < ret; i++) { 4356702ed49SChris Mason root = gang[i]; 4366702ed49SChris Mason last = root->root_key.objectid + 1; 437f510cfecSChris Mason btrfs_defrag_root(root, 1); 4386702ed49SChris Mason } 4396702ed49SChris Mason } 4406b80053dSChris Mason btrfs_defrag_root(info->extent_root, 1); 4416702ed49SChris Mason return err; 4426702ed49SChris Mason } 4436702ed49SChris Mason 44480b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 44535b7e476SChris Mason struct list_head *list) 4460f7d52f4SChris Mason { 4470f7d52f4SChris Mason struct dirty_root *dirty; 4480f7d52f4SChris Mason struct btrfs_trans_handle *trans; 449d3c2fdcfSChris Mason unsigned long nr; 450db94535dSChris Mason u64 num_bytes; 451db94535dSChris Mason u64 bytes_used; 45254aa1f4dSChris Mason int ret = 0; 4539f3a7427SChris Mason int err; 4549f3a7427SChris Mason 4550f7d52f4SChris Mason while(!list_empty(list)) { 45658176a96SJosef Bacik struct btrfs_root *root; 45758176a96SJosef Bacik 4580f7d52f4SChris Mason dirty = list_entry(list->next, struct dirty_root, list); 4590f7d52f4SChris Mason list_del_init(&dirty->list); 4605eda7b5eSChris Mason 461db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 46258176a96SJosef Bacik root = dirty->latest_root; 463a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 46458176a96SJosef Bacik 465a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 4669f3a7427SChris Mason while(1) { 4670f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4689f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4699f3a7427SChris Mason if (ret != -EAGAIN) { 4709f3a7427SChris Mason break; 4719f3a7427SChris Mason } 47258176a96SJosef Bacik 4739f3a7427SChris Mason err = btrfs_update_root(trans, 4749f3a7427SChris Mason tree_root, 4759f3a7427SChris Mason &dirty->root->root_key, 4769f3a7427SChris Mason &dirty->root->root_item); 4779f3a7427SChris Mason if (err) 4789f3a7427SChris Mason ret = err; 479d3c2fdcfSChris Mason nr = trans->blocks_used; 4809f3a7427SChris Mason ret = btrfs_end_transaction(trans, tree_root); 4810f7d52f4SChris Mason BUG_ON(ret); 482a2135011SChris Mason 483a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 484d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 4854dc11904SChris Mason cond_resched(); 486a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 4879f3a7427SChris Mason } 4889f3a7427SChris Mason BUG_ON(ret); 489a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 49058176a96SJosef Bacik 491a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 492db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 493db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 494db94535dSChris Mason if (num_bytes) { 49558176a96SJosef Bacik record_root_in_trans(root); 4965f39d397SChris Mason btrfs_set_root_used(&root->root_item, 497db94535dSChris Mason bytes_used - num_bytes); 49858176a96SJosef Bacik } 499a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 500a2135011SChris Mason 5019f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 50258176a96SJosef Bacik if (ret) { 50358176a96SJosef Bacik BUG(); 50454aa1f4dSChris Mason break; 50558176a96SJosef Bacik } 506a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 507a2135011SChris Mason 508d3c2fdcfSChris Mason nr = trans->blocks_used; 5090f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 5100f7d52f4SChris Mason BUG_ON(ret); 5115eda7b5eSChris Mason 512f510cfecSChris Mason free_extent_buffer(dirty->root->node); 5135eda7b5eSChris Mason kfree(dirty->root); 5140f7d52f4SChris Mason kfree(dirty); 515d3c2fdcfSChris Mason 516d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 5174dc11904SChris Mason cond_resched(); 5180f7d52f4SChris Mason } 51954aa1f4dSChris Mason return ret; 5200f7d52f4SChris Mason } 5210f7d52f4SChris Mason 522dc17ff8fSChris Mason int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans, 523dc17ff8fSChris Mason struct btrfs_root *root) 524dc17ff8fSChris Mason { 525dc17ff8fSChris Mason struct btrfs_transaction *cur_trans = trans->transaction; 526dc17ff8fSChris Mason struct inode *inode; 527dc17ff8fSChris Mason u64 root_objectid = 0; 528dc17ff8fSChris Mason u64 objectid = 0; 529dc17ff8fSChris Mason int ret; 530dc17ff8fSChris Mason 531a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 532dc17ff8fSChris Mason while(1) { 533dc17ff8fSChris Mason ret = btrfs_find_first_ordered_inode( 534dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 5354d5e74bcSChris Mason &root_objectid, &objectid, &inode); 536dc17ff8fSChris Mason if (!ret) 537dc17ff8fSChris Mason break; 538dc17ff8fSChris Mason 539dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5404d5e74bcSChris Mason 54181d7ed29SChris Mason if (S_ISREG(inode->i_mode)) { 54281d7ed29SChris Mason atomic_inc(&BTRFS_I(inode)->ordered_writeback); 543dc17ff8fSChris Mason filemap_fdatawrite(inode->i_mapping); 54481d7ed29SChris Mason atomic_dec(&BTRFS_I(inode)->ordered_writeback); 54581d7ed29SChris Mason } 546dc17ff8fSChris Mason iput(inode); 5474d5e74bcSChris Mason 548dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 549dc17ff8fSChris Mason } 550dc17ff8fSChris Mason while(1) { 551dc17ff8fSChris Mason root_objectid = 0; 552dc17ff8fSChris Mason objectid = 0; 553dc17ff8fSChris Mason ret = btrfs_find_del_first_ordered_inode( 554dc17ff8fSChris Mason &cur_trans->ordered_inode_tree, 5554d5e74bcSChris Mason &root_objectid, &objectid, &inode); 556dc17ff8fSChris Mason if (!ret) 557dc17ff8fSChris Mason break; 558dc17ff8fSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 5594d5e74bcSChris Mason 56081d7ed29SChris Mason if (S_ISREG(inode->i_mode)) { 56181d7ed29SChris Mason atomic_inc(&BTRFS_I(inode)->ordered_writeback); 562dc17ff8fSChris Mason filemap_write_and_wait(inode->i_mapping); 56381d7ed29SChris Mason atomic_dec(&BTRFS_I(inode)->ordered_writeback); 56481d7ed29SChris Mason } 5652da98f00SChris Mason atomic_dec(&inode->i_count); 566dc17ff8fSChris Mason iput(inode); 5674d5e74bcSChris Mason 568dc17ff8fSChris Mason mutex_lock(&root->fs_info->trans_mutex); 569dc17ff8fSChris Mason } 570a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 5713063d29fSChris Mason return 0; 5723063d29fSChris Mason } 5733063d29fSChris Mason 57480b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 5753063d29fSChris Mason struct btrfs_fs_info *fs_info, 5763063d29fSChris Mason struct btrfs_pending_snapshot *pending) 5773063d29fSChris Mason { 5783063d29fSChris Mason struct btrfs_key key; 57980b6794dSChris Mason struct btrfs_root_item *new_root_item; 5803063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 5813063d29fSChris Mason struct btrfs_root *root = pending->root; 5823063d29fSChris Mason struct extent_buffer *tmp; 583925baeddSChris Mason struct extent_buffer *old; 5843063d29fSChris Mason int ret; 5853b96362cSSven Wegener int namelen; 5863063d29fSChris Mason u64 objectid; 5873063d29fSChris Mason 58880b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 58980b6794dSChris Mason if (!new_root_item) { 59080b6794dSChris Mason ret = -ENOMEM; 59180b6794dSChris Mason goto fail; 59280b6794dSChris Mason } 5933063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 5943063d29fSChris Mason if (ret) 5953063d29fSChris Mason goto fail; 5963063d29fSChris Mason 59780b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 5983063d29fSChris Mason 5993063d29fSChris Mason key.objectid = objectid; 6003063d29fSChris Mason key.offset = 1; 6013063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 6023063d29fSChris Mason 603925baeddSChris Mason old = btrfs_lock_root_node(root); 604925baeddSChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 6053063d29fSChris Mason 606925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 607925baeddSChris Mason btrfs_tree_unlock(old); 608925baeddSChris Mason free_extent_buffer(old); 6093063d29fSChris Mason 61080b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 61180b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 6123063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 61380b6794dSChris Mason new_root_item); 614925baeddSChris Mason btrfs_tree_unlock(tmp); 6153063d29fSChris Mason free_extent_buffer(tmp); 6163063d29fSChris Mason if (ret) 6173063d29fSChris Mason goto fail; 6183063d29fSChris Mason 6193063d29fSChris Mason /* 6203063d29fSChris Mason * insert the directory item 6213063d29fSChris Mason */ 6223063d29fSChris Mason key.offset = (u64)-1; 6233b96362cSSven Wegener namelen = strlen(pending->name); 6243063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 6253b96362cSSven Wegener pending->name, namelen, 6263063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 6273063d29fSChris Mason &key, BTRFS_FT_DIR); 6283063d29fSChris Mason 6293063d29fSChris Mason if (ret) 6303063d29fSChris Mason goto fail; 6313063d29fSChris Mason 6323063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 6333063d29fSChris Mason pending->name, strlen(pending->name), objectid, 6343063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino); 6353b96362cSSven Wegener 6363b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 6373b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 6383b96362cSSven Wegener 6393063d29fSChris Mason fail: 64080b6794dSChris Mason kfree(new_root_item); 6413063d29fSChris Mason return ret; 6423063d29fSChris Mason } 6433063d29fSChris Mason 64480b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 6453063d29fSChris Mason struct btrfs_fs_info *fs_info) 6463063d29fSChris Mason { 6473063d29fSChris Mason struct btrfs_pending_snapshot *pending; 6483063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 6493063d29fSChris Mason int ret; 6503063d29fSChris Mason 6513063d29fSChris Mason while(!list_empty(head)) { 6523063d29fSChris Mason pending = list_entry(head->next, 6533063d29fSChris Mason struct btrfs_pending_snapshot, list); 6543063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 6553063d29fSChris Mason BUG_ON(ret); 6563063d29fSChris Mason list_del(&pending->list); 6573063d29fSChris Mason kfree(pending->name); 6583063d29fSChris Mason kfree(pending); 6593063d29fSChris Mason } 660dc17ff8fSChris Mason return 0; 661dc17ff8fSChris Mason } 662dc17ff8fSChris Mason 66379154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 66479154b1bSChris Mason struct btrfs_root *root) 66579154b1bSChris Mason { 66615ee9bc7SJosef Bacik unsigned long joined = 0; 66715ee9bc7SJosef Bacik unsigned long timeout = 1; 66879154b1bSChris Mason struct btrfs_transaction *cur_trans; 6698fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 6700b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 6710f7d52f4SChris Mason struct list_head dirty_fs_roots; 672d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 67379154b1bSChris Mason DEFINE_WAIT(wait); 67415ee9bc7SJosef Bacik int ret; 67579154b1bSChris Mason 6760f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 677d6e4a428SChris Mason 67879154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 67979154b1bSChris Mason if (trans->transaction->in_commit) { 68079154b1bSChris Mason cur_trans = trans->transaction; 68179154b1bSChris Mason trans->transaction->use_count++; 682ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 68379154b1bSChris Mason btrfs_end_transaction(trans, root); 684ccd467d6SChris Mason 68579154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 68679154b1bSChris Mason BUG_ON(ret); 68715ee9bc7SJosef Bacik 68815ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 68979154b1bSChris Mason put_transaction(cur_trans); 69015ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 69115ee9bc7SJosef Bacik 69279154b1bSChris Mason return 0; 69379154b1bSChris Mason } 6944313b399SChris Mason 6954313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 6964313b399SChris Mason if (!pinned_copy) 6974313b399SChris Mason return -ENOMEM; 6984313b399SChris Mason 699d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 7004313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 7014313b399SChris Mason 7022c90e5d6SChris Mason trans->transaction->in_commit = 1; 70389ce8a63SChris Mason printk("trans %Lu in commit\n", trans->transid); 704ccd467d6SChris Mason cur_trans = trans->transaction; 705ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 706ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 707ccd467d6SChris Mason struct btrfs_transaction, list); 708ccd467d6SChris Mason if (!prev_trans->commit_done) { 709ccd467d6SChris Mason prev_trans->use_count++; 710ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 711ccd467d6SChris Mason 712ccd467d6SChris Mason wait_for_commit(root, prev_trans); 713ccd467d6SChris Mason 714ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 71515ee9bc7SJosef Bacik put_transaction(prev_trans); 716ccd467d6SChris Mason } 717ccd467d6SChris Mason } 71815ee9bc7SJosef Bacik 71915ee9bc7SJosef Bacik do { 72015ee9bc7SJosef Bacik joined = cur_trans->num_joined; 7212c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 72215ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 72379154b1bSChris Mason TASK_UNINTERRUPTIBLE); 72415ee9bc7SJosef Bacik 72515ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 72615ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 72715ee9bc7SJosef Bacik else 72815ee9bc7SJosef Bacik timeout = 1; 72915ee9bc7SJosef Bacik 73079154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 73115ee9bc7SJosef Bacik 73215ee9bc7SJosef Bacik schedule_timeout(timeout); 73315ee9bc7SJosef Bacik 73479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 73515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 736dc17ff8fSChris Mason ret = btrfs_write_ordered_inodes(trans, root); 737dc17ff8fSChris Mason 73815ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 73915ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 74015ee9bc7SJosef Bacik 7413063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 7423063d29fSChris Mason BUG_ON(ret); 7433063d29fSChris Mason 7442c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 745dc17ff8fSChris Mason 74654aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 74754aa1f4dSChris Mason &dirty_fs_roots); 74854aa1f4dSChris Mason BUG_ON(ret); 74954aa1f4dSChris Mason 75079154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 75179154b1bSChris Mason BUG_ON(ret); 75254aa1f4dSChris Mason 75378fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 754cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 75578fae27eSChris Mason root->fs_info->running_transaction = NULL; 756cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 7574b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 7584b52dff6SChris Mason cur_trans->transid); 7594b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 760db94535dSChris Mason root->fs_info->tree_root->node->start); 761db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 762db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 7635f39d397SChris Mason 7640b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 7650b86a832SChris Mason chunk_root->node->start); 7660b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 7670b86a832SChris Mason btrfs_header_level(chunk_root->node)); 768a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 7694b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 770ccd467d6SChris Mason 7714313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 772ccd467d6SChris Mason 77378fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 77479154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 77579154b1bSChris Mason BUG_ON(ret); 77679154b1bSChris Mason write_ctree_super(trans, root); 7774313b399SChris Mason 7784313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 77978fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 7804313b399SChris Mason 7814313b399SChris Mason kfree(pinned_copy); 7824313b399SChris Mason 7832c90e5d6SChris Mason cur_trans->commit_done = 1; 78489ce8a63SChris Mason printk("trans %Lu done in commit\n", cur_trans->transid); 78515ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 7862c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 78779154b1bSChris Mason put_transaction(cur_trans); 78878fae27eSChris Mason put_transaction(cur_trans); 78958176a96SJosef Bacik 790facda1e7SChris Mason if (root->fs_info->closing) 791facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 792facda1e7SChris Mason else 793facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 79458176a96SJosef Bacik 79578fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 7962c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 79779154b1bSChris Mason 798facda1e7SChris Mason if (root->fs_info->closing) { 7990f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 800facda1e7SChris Mason } 80179154b1bSChris Mason return ret; 80279154b1bSChris Mason } 80379154b1bSChris Mason 804e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 805e9d0b13bSChris Mason { 806e9d0b13bSChris Mason struct list_head dirty_roots; 807e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 808a74a4b97SChris Mason again: 809e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 810e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 811e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 812e9d0b13bSChris Mason 813e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 814e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 815a74a4b97SChris Mason goto again; 816e9d0b13bSChris Mason } 817e9d0b13bSChris Mason return 0; 818e9d0b13bSChris Mason } 81908607c1bSChris Mason 820