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" 2731153d81SYan Zheng #include "ref-cache.h" 2879154b1bSChris Mason 2978fae27eSChris Mason static int total_trans = 0; 302c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 312c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 322c90e5d6SChris Mason 330f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 340f7d52f4SChris Mason 3531153d81SYan Zheng struct dirty_root { 3631153d81SYan Zheng struct list_head list; 3731153d81SYan Zheng struct btrfs_root *root; 3831153d81SYan Zheng struct btrfs_root *latest_root; 3931153d81SYan Zheng }; 4031153d81SYan Zheng 4180b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 4279154b1bSChris Mason { 432c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 4479154b1bSChris Mason transaction->use_count--; 4578fae27eSChris Mason if (transaction->use_count == 0) { 4678fae27eSChris Mason WARN_ON(total_trans == 0); 4778fae27eSChris Mason total_trans--; 488fd17795SChris Mason list_del_init(&transaction->list); 492c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 502c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 5179154b1bSChris Mason } 5278fae27eSChris Mason } 5379154b1bSChris Mason 5480b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5579154b1bSChris Mason { 5679154b1bSChris Mason struct btrfs_transaction *cur_trans; 5779154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5879154b1bSChris Mason if (!cur_trans) { 592c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 602c90e5d6SChris Mason GFP_NOFS); 6178fae27eSChris Mason total_trans++; 6279154b1bSChris Mason BUG_ON(!cur_trans); 630f7d52f4SChris Mason root->fs_info->generation++; 64e18e4809SChris Mason root->fs_info->last_alloc = 0; 654529ba49SChris Mason root->fs_info->last_data_alloc = 0; 6615ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6715ee9bc7SJosef Bacik cur_trans->num_joined = 0; 680f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6979154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 7079154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 7179154b1bSChris Mason cur_trans->in_commit = 0; 72f9295749SChris Mason cur_trans->blocked = 0; 73d5719762SChris Mason cur_trans->use_count = 1; 7479154b1bSChris Mason cur_trans->commit_done = 0; 7508607c1bSChris Mason cur_trans->start_time = get_seconds(); 763063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 778fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 78d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 795f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 805f39d397SChris Mason GFP_NOFS); 8148ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8248ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8348ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8415ee9bc7SJosef Bacik } else { 8579154b1bSChris Mason cur_trans->num_writers++; 8615ee9bc7SJosef Bacik cur_trans->num_joined++; 8715ee9bc7SJosef Bacik } 8815ee9bc7SJosef Bacik 8979154b1bSChris Mason return 0; 9079154b1bSChris Mason } 9179154b1bSChris Mason 9280b6794dSChris Mason static noinline int record_root_in_trans(struct btrfs_root *root) 936702ed49SChris Mason { 9431153d81SYan Zheng struct dirty_root *dirty; 956702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 966702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 976702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 986702ed49SChris Mason if (root->root_item.refs != 0) { 996702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1006702ed49SChris Mason (unsigned long)root->root_key.objectid, 1016702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 10231153d81SYan Zheng 10331153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10431153d81SYan Zheng BUG_ON(!dirty); 10531153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 10631153d81SYan Zheng BUG_ON(!dirty->root); 10731153d81SYan Zheng 10831153d81SYan Zheng dirty->latest_root = root; 10931153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 11031153d81SYan Zheng 111925baeddSChris Mason root->commit_root = btrfs_root_node(root); 112017e5369SChris Mason root->dirty_root = dirty; 11331153d81SYan Zheng 11431153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 115017e5369SChris Mason dirty->root->ref_tree = &root->ref_tree_struct; 116017e5369SChris Mason 11731153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 11831153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 11931153d81SYan Zheng dirty->root->node = root->commit_root; 12031153d81SYan Zheng dirty->root->commit_root = NULL; 1216702ed49SChris Mason } else { 1226702ed49SChris Mason WARN_ON(1); 1236702ed49SChris Mason } 1246702ed49SChris Mason root->last_trans = running_trans_id; 1256702ed49SChris Mason } 1266702ed49SChris Mason return 0; 1276702ed49SChris Mason } 1286702ed49SChris Mason 129f9295749SChris Mason struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 130f9295749SChris Mason int num_blocks, int join) 13179154b1bSChris Mason { 1322c90e5d6SChris Mason struct btrfs_trans_handle *h = 1332c90e5d6SChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 134f9295749SChris Mason struct btrfs_transaction *cur_trans; 13579154b1bSChris Mason int ret; 13679154b1bSChris Mason 13779154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 138f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 139f9295749SChris Mason if (cur_trans && cur_trans->blocked && !join) { 140f9295749SChris Mason DEFINE_WAIT(wait); 141f9295749SChris Mason cur_trans->use_count++; 142f9295749SChris Mason while(1) { 143f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 144f9295749SChris Mason TASK_UNINTERRUPTIBLE); 145f9295749SChris Mason if (cur_trans->blocked) { 146f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 147f9295749SChris Mason schedule(); 148f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 149f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 150f9295749SChris Mason &wait); 151f9295749SChris Mason } else { 152f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 153f9295749SChris Mason &wait); 154f9295749SChris Mason break; 155f9295749SChris Mason } 156f9295749SChris Mason } 157f9295749SChris Mason put_transaction(cur_trans); 158f9295749SChris Mason } 15979154b1bSChris Mason ret = join_transaction(root); 16079154b1bSChris Mason BUG_ON(ret); 1610f7d52f4SChris Mason 1626702ed49SChris Mason record_root_in_trans(root); 1636702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 16479154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 16579154b1bSChris Mason h->blocks_reserved = num_blocks; 16679154b1bSChris Mason h->blocks_used = 0; 16731f3c99bSChris Mason h->block_group = NULL; 16826b8003fSChris Mason h->alloc_exclude_nr = 0; 16926b8003fSChris Mason h->alloc_exclude_start = 0; 17079154b1bSChris Mason root->fs_info->running_transaction->use_count++; 17179154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 17279154b1bSChris Mason return h; 17379154b1bSChris Mason } 17479154b1bSChris Mason 175f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 176f9295749SChris Mason int num_blocks) 177f9295749SChris Mason { 178f9295749SChris Mason return start_transaction(root, num_blocks, 0); 179f9295749SChris Mason } 180f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 181f9295749SChris Mason int num_blocks) 182f9295749SChris Mason { 183f9295749SChris Mason return start_transaction(root, num_blocks, 1); 184f9295749SChris Mason } 185f9295749SChris Mason 18689ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 18789ce8a63SChris Mason struct btrfs_transaction *commit) 18889ce8a63SChris Mason { 18989ce8a63SChris Mason DEFINE_WAIT(wait); 19089ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 19189ce8a63SChris Mason while(!commit->commit_done) { 19289ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 19389ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 19489ce8a63SChris Mason if (commit->commit_done) 19589ce8a63SChris Mason break; 19689ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 19789ce8a63SChris Mason schedule(); 19889ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 19989ce8a63SChris Mason } 20089ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 20189ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 20289ce8a63SChris Mason return 0; 20389ce8a63SChris Mason } 20489ce8a63SChris Mason 20589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 20689ce8a63SChris Mason struct btrfs_root *root, int throttle) 20779154b1bSChris Mason { 20879154b1bSChris Mason struct btrfs_transaction *cur_trans; 209d6e4a428SChris Mason 21079154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 21179154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 212ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 213d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 214ccd467d6SChris Mason cur_trans->num_writers--; 21589ce8a63SChris Mason 21679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 21779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 21889ce8a63SChris Mason 219017e5369SChris Mason if (throttle && atomic_read(&root->fs_info->throttles)) { 220e6dcd2dcSChris Mason DEFINE_WAIT(wait); 22189ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 222e6dcd2dcSChris Mason prepare_to_wait(&root->fs_info->transaction_throttle, &wait, 223e6dcd2dcSChris Mason TASK_UNINTERRUPTIBLE); 224017e5369SChris Mason if (atomic_read(&root->fs_info->throttles)) 225e6dcd2dcSChris Mason schedule(); 226e6dcd2dcSChris Mason finish_wait(&root->fs_info->transaction_throttle, &wait); 22789ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22889ce8a63SChris Mason } 22989ce8a63SChris Mason 23079154b1bSChris Mason put_transaction(cur_trans); 23179154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 232d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 2332c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 23479154b1bSChris Mason return 0; 23579154b1bSChris Mason } 23679154b1bSChris Mason 23789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 23889ce8a63SChris Mason struct btrfs_root *root) 23989ce8a63SChris Mason { 24089ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 24189ce8a63SChris Mason } 24289ce8a63SChris Mason 24389ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 24489ce8a63SChris Mason struct btrfs_root *root) 24589ce8a63SChris Mason { 24689ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 24789ce8a63SChris Mason } 24889ce8a63SChris Mason 24979154b1bSChris Mason 25079154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 25179154b1bSChris Mason struct btrfs_root *root) 25279154b1bSChris Mason { 2537c4452b9SChris Mason int ret; 2547c4452b9SChris Mason int err; 2557c4452b9SChris Mason int werr = 0; 256d1310b2eSChris Mason struct extent_io_tree *dirty_pages; 2577c4452b9SChris Mason struct page *page; 2587c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 2595f39d397SChris Mason u64 start; 2605f39d397SChris Mason u64 end; 2615f39d397SChris Mason unsigned long index; 2627c4452b9SChris Mason 2637c4452b9SChris Mason if (!trans || !trans->transaction) { 2647c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 2657c4452b9SChris Mason } 2667c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 2677c4452b9SChris Mason while(1) { 2685f39d397SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 2695f39d397SChris Mason EXTENT_DIRTY); 2705f39d397SChris Mason if (ret) 2717c4452b9SChris Mason break; 2725f39d397SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 2735f39d397SChris Mason while(start <= end) { 2745f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 27535ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 2765f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 2777c4452b9SChris Mason if (!page) 2787c4452b9SChris Mason continue; 2796702ed49SChris Mason if (PageWriteback(page)) { 2806702ed49SChris Mason if (PageDirty(page)) 2816702ed49SChris Mason wait_on_page_writeback(page); 2826702ed49SChris Mason else { 2836702ed49SChris Mason unlock_page(page); 2846702ed49SChris Mason page_cache_release(page); 2856702ed49SChris Mason continue; 2866702ed49SChris Mason } 2876702ed49SChris Mason } 2887c4452b9SChris Mason err = write_one_page(page, 0); 2897c4452b9SChris Mason if (err) 2907c4452b9SChris Mason werr = err; 2917c4452b9SChris Mason page_cache_release(page); 2927c4452b9SChris Mason } 2937c4452b9SChris Mason } 2947c4452b9SChris Mason err = filemap_fdatawait(btree_inode->i_mapping); 2957c4452b9SChris Mason if (err) 2967c4452b9SChris Mason werr = err; 2977c4452b9SChris Mason return werr; 29879154b1bSChris Mason } 29979154b1bSChris Mason 3000b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 30179154b1bSChris Mason struct btrfs_root *root) 30279154b1bSChris Mason { 30379154b1bSChris Mason int ret; 3040b86a832SChris Mason u64 old_root_bytenr; 3050b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 30679154b1bSChris Mason 3070b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 30879154b1bSChris Mason while(1) { 3090b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 3100b86a832SChris Mason if (old_root_bytenr == root->node->start) 31179154b1bSChris Mason break; 3120b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 3130b86a832SChris Mason root->node->start); 3140b86a832SChris Mason btrfs_set_root_level(&root->root_item, 3150b86a832SChris Mason btrfs_header_level(root->node)); 31679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 3170b86a832SChris Mason &root->root_key, 3180b86a832SChris Mason &root->root_item); 31979154b1bSChris Mason BUG_ON(ret); 3200b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 3210b86a832SChris Mason } 3220b86a832SChris Mason return 0; 3230b86a832SChris Mason } 3240b86a832SChris Mason 3250b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 3260b86a832SChris Mason struct btrfs_root *root) 3270b86a832SChris Mason { 3280b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 3290b86a832SChris Mason struct list_head *next; 3300b86a832SChris Mason 3310b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 3320b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 3330b86a832SChris Mason list_del_init(next); 3340b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 3350b86a832SChris Mason update_cowonly_root(trans, root); 336017e5369SChris Mason if (root->fs_info->closing) 337017e5369SChris Mason btrfs_remove_leaf_refs(root); 33879154b1bSChris Mason } 33979154b1bSChris Mason return 0; 34079154b1bSChris Mason } 34179154b1bSChris Mason 3425ce14bbcSChris Mason int btrfs_add_dead_root(struct btrfs_root *root, 3435ce14bbcSChris Mason struct btrfs_root *latest, 3445ce14bbcSChris Mason struct list_head *dead_list) 3455eda7b5eSChris Mason { 3465eda7b5eSChris Mason struct dirty_root *dirty; 3475eda7b5eSChris Mason 3485eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 3495eda7b5eSChris Mason if (!dirty) 3505eda7b5eSChris Mason return -ENOMEM; 3515eda7b5eSChris Mason dirty->root = root; 3525ce14bbcSChris Mason dirty->latest_root = latest; 3535eda7b5eSChris Mason list_add(&dirty->list, dead_list); 3545eda7b5eSChris Mason return 0; 3555eda7b5eSChris Mason } 3565eda7b5eSChris Mason 35780b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 35835b7e476SChris Mason struct radix_tree_root *radix, 35935b7e476SChris Mason struct list_head *list) 3600f7d52f4SChris Mason { 3610f7d52f4SChris Mason struct dirty_root *dirty; 3620f7d52f4SChris Mason struct btrfs_root *gang[8]; 3630f7d52f4SChris Mason struct btrfs_root *root; 3640f7d52f4SChris Mason int i; 3650f7d52f4SChris Mason int ret; 36654aa1f4dSChris Mason int err = 0; 3675eda7b5eSChris Mason u32 refs; 36854aa1f4dSChris Mason 3690f7d52f4SChris Mason while(1) { 3700f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 3710f7d52f4SChris Mason ARRAY_SIZE(gang), 3720f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 3730f7d52f4SChris Mason if (ret == 0) 3740f7d52f4SChris Mason break; 3750f7d52f4SChris Mason for (i = 0; i < ret; i++) { 3760f7d52f4SChris Mason root = gang[i]; 3772619ba1fSChris Mason radix_tree_tag_clear(radix, 3782619ba1fSChris Mason (unsigned long)root->root_key.objectid, 3790f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 38031153d81SYan Zheng 38131153d81SYan Zheng BUG_ON(!root->ref_tree); 382017e5369SChris Mason dirty = root->dirty_root; 38331153d81SYan Zheng 3840f7d52f4SChris Mason if (root->commit_root == root->node) { 385db94535dSChris Mason WARN_ON(root->node->start != 386db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 38731153d81SYan Zheng 3885f39d397SChris Mason free_extent_buffer(root->commit_root); 3890f7d52f4SChris Mason root->commit_root = NULL; 39031153d81SYan Zheng 39131153d81SYan Zheng kfree(dirty->root); 39231153d81SYan Zheng kfree(dirty); 39358176a96SJosef Bacik 39458176a96SJosef Bacik /* make sure to update the root on disk 39558176a96SJosef Bacik * so we get any updates to the block used 39658176a96SJosef Bacik * counts 39758176a96SJosef Bacik */ 39858176a96SJosef Bacik err = btrfs_update_root(trans, 39958176a96SJosef Bacik root->fs_info->tree_root, 40058176a96SJosef Bacik &root->root_key, 40158176a96SJosef Bacik &root->root_item); 4020f7d52f4SChris Mason continue; 4030f7d52f4SChris Mason } 4049f3a7427SChris Mason 4059f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 4069f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 4079f3a7427SChris Mason root->root_item.drop_level = 0; 4080f7d52f4SChris Mason root->commit_root = NULL; 4090f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 410db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 411db94535dSChris Mason root->node->start); 412db94535dSChris Mason btrfs_set_root_level(&root->root_item, 413db94535dSChris Mason btrfs_header_level(root->node)); 4140f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 4150f7d52f4SChris Mason &root->root_key, 4160f7d52f4SChris Mason &root->root_item); 41754aa1f4dSChris Mason if (err) 41854aa1f4dSChris Mason break; 4199f3a7427SChris Mason 4209f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 4219f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 4225eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 4239f3a7427SChris Mason &dirty->root->root_key, 4249f3a7427SChris Mason &dirty->root->root_item); 4255eda7b5eSChris Mason 4265eda7b5eSChris Mason BUG_ON(err); 4279f3a7427SChris Mason if (refs == 1) { 4280f7d52f4SChris Mason list_add(&dirty->list, list); 4299f3a7427SChris Mason } else { 4309f3a7427SChris Mason WARN_ON(1); 43131153d81SYan Zheng free_extent_buffer(dirty->root->node); 4329f3a7427SChris Mason kfree(dirty->root); 4335eda7b5eSChris Mason kfree(dirty); 4340f7d52f4SChris Mason } 4350f7d52f4SChris Mason } 4369f3a7427SChris Mason } 43754aa1f4dSChris Mason return err; 4380f7d52f4SChris Mason } 4390f7d52f4SChris Mason 440e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 441e9d0b13bSChris Mason { 442e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 443e9d0b13bSChris Mason int ret; 444e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 445d3c2fdcfSChris Mason unsigned long nr; 446e9d0b13bSChris Mason 447a2135011SChris Mason smp_mb(); 448e9d0b13bSChris Mason if (root->defrag_running) 449e9d0b13bSChris Mason return 0; 450e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 4516b80053dSChris Mason while (1) { 452e9d0b13bSChris Mason root->defrag_running = 1; 453e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 454d3c2fdcfSChris Mason nr = trans->blocks_used; 455e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 456d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 457e9d0b13bSChris Mason cond_resched(); 458e9d0b13bSChris Mason 459e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 4603f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 461e9d0b13bSChris Mason break; 462e9d0b13bSChris Mason } 463e9d0b13bSChris Mason root->defrag_running = 0; 464a2135011SChris Mason smp_mb(); 465e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 466e9d0b13bSChris Mason return 0; 467e9d0b13bSChris Mason } 468e9d0b13bSChris Mason 46980b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 47035b7e476SChris Mason struct list_head *list) 4710f7d52f4SChris Mason { 4720f7d52f4SChris Mason struct dirty_root *dirty; 4730f7d52f4SChris Mason struct btrfs_trans_handle *trans; 474d3c2fdcfSChris Mason unsigned long nr; 475db94535dSChris Mason u64 num_bytes; 476db94535dSChris Mason u64 bytes_used; 47754aa1f4dSChris Mason int ret = 0; 4789f3a7427SChris Mason int err; 4799f3a7427SChris Mason 4800f7d52f4SChris Mason while(!list_empty(list)) { 48158176a96SJosef Bacik struct btrfs_root *root; 48258176a96SJosef Bacik 483017e5369SChris Mason dirty = list_entry(list->prev, struct dirty_root, list); 4840f7d52f4SChris Mason list_del_init(&dirty->list); 4855eda7b5eSChris Mason 486db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 48758176a96SJosef Bacik root = dirty->latest_root; 488a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 48958176a96SJosef Bacik 490a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 4919f3a7427SChris Mason while(1) { 4920f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 4939f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 4949f3a7427SChris Mason if (ret != -EAGAIN) { 4959f3a7427SChris Mason break; 4969f3a7427SChris Mason } 49758176a96SJosef Bacik 4989f3a7427SChris Mason err = btrfs_update_root(trans, 4999f3a7427SChris Mason tree_root, 5009f3a7427SChris Mason &dirty->root->root_key, 5019f3a7427SChris Mason &dirty->root->root_item); 5029f3a7427SChris Mason if (err) 5039f3a7427SChris Mason ret = err; 504d3c2fdcfSChris Mason nr = trans->blocks_used; 505017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 5060f7d52f4SChris Mason BUG_ON(ret); 507a2135011SChris Mason 508a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 509d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 5104dc11904SChris Mason cond_resched(); 511a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5129f3a7427SChris Mason } 5139f3a7427SChris Mason BUG_ON(ret); 514a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 515017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 51658176a96SJosef Bacik 517a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 518db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 519db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 520db94535dSChris Mason if (num_bytes) { 52158176a96SJosef Bacik record_root_in_trans(root); 5225f39d397SChris Mason btrfs_set_root_used(&root->root_item, 523db94535dSChris Mason bytes_used - num_bytes); 52458176a96SJosef Bacik } 525a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 526a2135011SChris Mason 5279f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 52858176a96SJosef Bacik if (ret) { 52958176a96SJosef Bacik BUG(); 53054aa1f4dSChris Mason break; 53158176a96SJosef Bacik } 532a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 533a2135011SChris Mason 534d3c2fdcfSChris Mason nr = trans->blocks_used; 5350f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 5360f7d52f4SChris Mason BUG_ON(ret); 5375eda7b5eSChris Mason 538f510cfecSChris Mason free_extent_buffer(dirty->root->node); 5395eda7b5eSChris Mason kfree(dirty->root); 5400f7d52f4SChris Mason kfree(dirty); 541d3c2fdcfSChris Mason 542d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 5434dc11904SChris Mason cond_resched(); 5440f7d52f4SChris Mason } 54554aa1f4dSChris Mason return ret; 5460f7d52f4SChris Mason } 5470f7d52f4SChris Mason 54880b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 5493063d29fSChris Mason struct btrfs_fs_info *fs_info, 5503063d29fSChris Mason struct btrfs_pending_snapshot *pending) 5513063d29fSChris Mason { 5523063d29fSChris Mason struct btrfs_key key; 55380b6794dSChris Mason struct btrfs_root_item *new_root_item; 5543063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 5553063d29fSChris Mason struct btrfs_root *root = pending->root; 5563063d29fSChris Mason struct extent_buffer *tmp; 557925baeddSChris Mason struct extent_buffer *old; 5583063d29fSChris Mason int ret; 5593b96362cSSven Wegener int namelen; 5603063d29fSChris Mason u64 objectid; 5613063d29fSChris Mason 56280b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 56380b6794dSChris Mason if (!new_root_item) { 56480b6794dSChris Mason ret = -ENOMEM; 56580b6794dSChris Mason goto fail; 56680b6794dSChris Mason } 5673063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 5683063d29fSChris Mason if (ret) 5693063d29fSChris Mason goto fail; 5703063d29fSChris Mason 57180b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 5723063d29fSChris Mason 5733063d29fSChris Mason key.objectid = objectid; 5743063d29fSChris Mason key.offset = 1; 5753063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 5763063d29fSChris Mason 577925baeddSChris Mason old = btrfs_lock_root_node(root); 578925baeddSChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 5793063d29fSChris Mason 580925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 581925baeddSChris Mason btrfs_tree_unlock(old); 582925baeddSChris Mason free_extent_buffer(old); 5833063d29fSChris Mason 58480b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 58580b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 5863063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 58780b6794dSChris Mason new_root_item); 588925baeddSChris Mason btrfs_tree_unlock(tmp); 5893063d29fSChris Mason free_extent_buffer(tmp); 5903063d29fSChris Mason if (ret) 5913063d29fSChris Mason goto fail; 5923063d29fSChris Mason 5933063d29fSChris Mason /* 5943063d29fSChris Mason * insert the directory item 5953063d29fSChris Mason */ 5963063d29fSChris Mason key.offset = (u64)-1; 5973b96362cSSven Wegener namelen = strlen(pending->name); 5983063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 5993b96362cSSven Wegener pending->name, namelen, 6003063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 601aec7477bSJosef Bacik &key, BTRFS_FT_DIR, 0); 6023063d29fSChris Mason 6033063d29fSChris Mason if (ret) 6043063d29fSChris Mason goto fail; 6053063d29fSChris Mason 6063063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 6073063d29fSChris Mason pending->name, strlen(pending->name), objectid, 608aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 6093b96362cSSven Wegener 6103b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 6113b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 6123b96362cSSven Wegener 6133063d29fSChris Mason fail: 61480b6794dSChris Mason kfree(new_root_item); 6153063d29fSChris Mason return ret; 6163063d29fSChris Mason } 6173063d29fSChris Mason 61880b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 6193063d29fSChris Mason struct btrfs_fs_info *fs_info) 6203063d29fSChris Mason { 6213063d29fSChris Mason struct btrfs_pending_snapshot *pending; 6223063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 6233063d29fSChris Mason int ret; 6243063d29fSChris Mason 6253063d29fSChris Mason while(!list_empty(head)) { 6263063d29fSChris Mason pending = list_entry(head->next, 6273063d29fSChris Mason struct btrfs_pending_snapshot, list); 6283063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 6293063d29fSChris Mason BUG_ON(ret); 6303063d29fSChris Mason list_del(&pending->list); 6313063d29fSChris Mason kfree(pending->name); 6323063d29fSChris Mason kfree(pending); 6333063d29fSChris Mason } 634dc17ff8fSChris Mason return 0; 635dc17ff8fSChris Mason } 636dc17ff8fSChris Mason 63779154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 63879154b1bSChris Mason struct btrfs_root *root) 63979154b1bSChris Mason { 64015ee9bc7SJosef Bacik unsigned long joined = 0; 64115ee9bc7SJosef Bacik unsigned long timeout = 1; 64279154b1bSChris Mason struct btrfs_transaction *cur_trans; 6438fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 6440b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 6450f7d52f4SChris Mason struct list_head dirty_fs_roots; 646d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 64779154b1bSChris Mason DEFINE_WAIT(wait); 64815ee9bc7SJosef Bacik int ret; 64979154b1bSChris Mason 6500f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 651d6e4a428SChris Mason 65279154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 65379154b1bSChris Mason if (trans->transaction->in_commit) { 65479154b1bSChris Mason cur_trans = trans->transaction; 65579154b1bSChris Mason trans->transaction->use_count++; 656ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 65779154b1bSChris Mason btrfs_end_transaction(trans, root); 658ccd467d6SChris Mason 65979154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 66079154b1bSChris Mason BUG_ON(ret); 66115ee9bc7SJosef Bacik 66215ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 66379154b1bSChris Mason put_transaction(cur_trans); 66415ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 66515ee9bc7SJosef Bacik 66679154b1bSChris Mason return 0; 66779154b1bSChris Mason } 6684313b399SChris Mason 6694313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 6704313b399SChris Mason if (!pinned_copy) 6714313b399SChris Mason return -ENOMEM; 6724313b399SChris Mason 673d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 6744313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 6754313b399SChris Mason 6762c90e5d6SChris Mason trans->transaction->in_commit = 1; 677f9295749SChris Mason trans->transaction->blocked = 1; 678ccd467d6SChris Mason cur_trans = trans->transaction; 679ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 680ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 681ccd467d6SChris Mason struct btrfs_transaction, list); 682ccd467d6SChris Mason if (!prev_trans->commit_done) { 683ccd467d6SChris Mason prev_trans->use_count++; 684ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 685ccd467d6SChris Mason 686ccd467d6SChris Mason wait_for_commit(root, prev_trans); 687ccd467d6SChris Mason 688ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 68915ee9bc7SJosef Bacik put_transaction(prev_trans); 690ccd467d6SChris Mason } 691ccd467d6SChris Mason } 69215ee9bc7SJosef Bacik 69315ee9bc7SJosef Bacik do { 69415ee9bc7SJosef Bacik joined = cur_trans->num_joined; 6952c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 69615ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 69779154b1bSChris Mason TASK_UNINTERRUPTIBLE); 69815ee9bc7SJosef Bacik 69915ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 70015ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 70115ee9bc7SJosef Bacik else 70215ee9bc7SJosef Bacik timeout = 1; 70315ee9bc7SJosef Bacik 70479154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 70515ee9bc7SJosef Bacik 70615ee9bc7SJosef Bacik schedule_timeout(timeout); 70715ee9bc7SJosef Bacik 70879154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 70915ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 71015ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 71115ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 71215ee9bc7SJosef Bacik 7133063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 7143063d29fSChris Mason BUG_ON(ret); 7153063d29fSChris Mason 7162c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 717dc17ff8fSChris Mason 71854aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 71954aa1f4dSChris Mason &dirty_fs_roots); 72054aa1f4dSChris Mason BUG_ON(ret); 72154aa1f4dSChris Mason 72279154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 72379154b1bSChris Mason BUG_ON(ret); 72454aa1f4dSChris Mason 72578fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 726cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 72778fae27eSChris Mason root->fs_info->running_transaction = NULL; 728cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 7294b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 7304b52dff6SChris Mason cur_trans->transid); 7314b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 732db94535dSChris Mason root->fs_info->tree_root->node->start); 733db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 734db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 7355f39d397SChris Mason 7360b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 7370b86a832SChris Mason chunk_root->node->start); 7380b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 7390b86a832SChris Mason btrfs_header_level(chunk_root->node)); 740a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 7414b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 742ccd467d6SChris Mason 7434313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 744ccd467d6SChris Mason 745f9295749SChris Mason trans->transaction->blocked = 0; 746e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 747f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 748e6dcd2dcSChris Mason 74978fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 75079154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 75179154b1bSChris Mason BUG_ON(ret); 75279154b1bSChris Mason write_ctree_super(trans, root); 7534313b399SChris Mason 7544313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 75578fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 7564313b399SChris Mason 7574313b399SChris Mason kfree(pinned_copy); 7584313b399SChris Mason 7592c90e5d6SChris Mason cur_trans->commit_done = 1; 76015ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 7612c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 76279154b1bSChris Mason put_transaction(cur_trans); 76378fae27eSChris Mason put_transaction(cur_trans); 76458176a96SJosef Bacik 765facda1e7SChris Mason if (root->fs_info->closing) 766facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 767facda1e7SChris Mason else 768facda1e7SChris Mason list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 76958176a96SJosef Bacik 77078fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 7712c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 77279154b1bSChris Mason 773facda1e7SChris Mason if (root->fs_info->closing) { 7740f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 775facda1e7SChris Mason } 77679154b1bSChris Mason return ret; 77779154b1bSChris Mason } 77879154b1bSChris Mason 779e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 780e9d0b13bSChris Mason { 781e9d0b13bSChris Mason struct list_head dirty_roots; 782e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 783a74a4b97SChris Mason again: 784e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 785e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 786e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 787e9d0b13bSChris Mason 788e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 789e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 790a74a4b97SChris Mason goto again; 791e9d0b13bSChris Mason } 792e9d0b13bSChris Mason return 0; 793e9d0b13bSChris Mason } 79408607c1bSChris Mason 795