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 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; 66f9295749SChris Mason cur_trans->blocked = 0; 67d5719762SChris Mason cur_trans->use_count = 1; 6879154b1bSChris Mason cur_trans->commit_done = 0; 6908607c1bSChris Mason cur_trans->start_time = get_seconds(); 703063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 718fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 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 { 88f321e491SYan Zheng struct btrfs_dirty_root *dirty; 896702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 906702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 916702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 926702ed49SChris Mason if (root->root_item.refs != 0) { 936702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 946702ed49SChris Mason (unsigned long)root->root_key.objectid, 956702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 9631153d81SYan Zheng 9731153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 9831153d81SYan Zheng BUG_ON(!dirty); 9931153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 10031153d81SYan Zheng BUG_ON(!dirty->root); 10131153d81SYan Zheng dirty->latest_root = root; 10231153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 10331153d81SYan Zheng 104925baeddSChris Mason root->commit_root = btrfs_root_node(root); 10531153d81SYan Zheng 10631153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 10731153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 108bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 10931153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 110bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 11131153d81SYan Zheng dirty->root->node = root->commit_root; 11231153d81SYan Zheng dirty->root->commit_root = NULL; 113bcc63abbSYan 114bcc63abbSYan spin_lock(&root->list_lock); 115bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 116bcc63abbSYan spin_unlock(&root->list_lock); 117bcc63abbSYan 118bcc63abbSYan root->dirty_root = dirty; 1196702ed49SChris Mason } else { 1206702ed49SChris Mason WARN_ON(1); 1216702ed49SChris Mason } 1226702ed49SChris Mason root->last_trans = running_trans_id; 1236702ed49SChris Mason } 1246702ed49SChris Mason return 0; 1256702ed49SChris Mason } 1266702ed49SChris Mason 12737d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 12879154b1bSChris Mason { 129f9295749SChris Mason struct btrfs_transaction *cur_trans; 13079154b1bSChris Mason 131f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 13237d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 133f9295749SChris Mason DEFINE_WAIT(wait); 134f9295749SChris Mason cur_trans->use_count++; 135f9295749SChris Mason while(1) { 136f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 137f9295749SChris Mason TASK_UNINTERRUPTIBLE); 138f9295749SChris Mason if (cur_trans->blocked) { 139f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 140f9295749SChris Mason schedule(); 141f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 142f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 143f9295749SChris Mason &wait); 144f9295749SChris Mason } else { 145f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 146f9295749SChris Mason &wait); 147f9295749SChris Mason break; 148f9295749SChris Mason } 149f9295749SChris Mason } 150f9295749SChris Mason put_transaction(cur_trans); 151f9295749SChris Mason } 15237d1aeeeSChris Mason } 15337d1aeeeSChris Mason 15437d1aeeeSChris Mason struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1559ca9ee09SSage Weil int num_blocks, int wait) 15637d1aeeeSChris Mason { 15737d1aeeeSChris Mason struct btrfs_trans_handle *h = 15837d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 15937d1aeeeSChris Mason int ret; 16037d1aeeeSChris Mason 16137d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1629ca9ee09SSage Weil if ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2) 16337d1aeeeSChris Mason wait_current_trans(root); 16479154b1bSChris Mason ret = join_transaction(root); 16579154b1bSChris Mason BUG_ON(ret); 1660f7d52f4SChris Mason 1676702ed49SChris Mason record_root_in_trans(root); 1686702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 16979154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 17079154b1bSChris Mason h->blocks_reserved = num_blocks; 17179154b1bSChris Mason h->blocks_used = 0; 17231f3c99bSChris Mason h->block_group = NULL; 17326b8003fSChris Mason h->alloc_exclude_nr = 0; 17426b8003fSChris Mason h->alloc_exclude_start = 0; 17579154b1bSChris Mason root->fs_info->running_transaction->use_count++; 17679154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 17779154b1bSChris Mason return h; 17879154b1bSChris Mason } 17979154b1bSChris Mason 180f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 181f9295749SChris Mason int num_blocks) 182f9295749SChris Mason { 1839ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 184f9295749SChris Mason } 185f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 186f9295749SChris Mason int num_blocks) 187f9295749SChris Mason { 1889ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 189f9295749SChris Mason } 190f9295749SChris Mason 1919ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 1929ca9ee09SSage Weil int num_blocks) 1939ca9ee09SSage Weil { 1949ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 1959ca9ee09SSage Weil } 1969ca9ee09SSage Weil 1979ca9ee09SSage Weil 19889ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 19989ce8a63SChris Mason struct btrfs_transaction *commit) 20089ce8a63SChris Mason { 20189ce8a63SChris Mason DEFINE_WAIT(wait); 20289ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 20389ce8a63SChris Mason while(!commit->commit_done) { 20489ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 20589ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 20689ce8a63SChris Mason if (commit->commit_done) 20789ce8a63SChris Mason break; 20889ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 20989ce8a63SChris Mason schedule(); 21089ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21189ce8a63SChris Mason } 21289ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21389ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 21489ce8a63SChris Mason return 0; 21589ce8a63SChris Mason } 21689ce8a63SChris Mason 21737d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 218ab78c84dSChris Mason { 219ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2202dd3e67bSChris Mason int harder_count = 0; 221ab78c84dSChris Mason 2222dd3e67bSChris Mason harder: 223ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 224ab78c84dSChris Mason DEFINE_WAIT(wait); 225ab78c84dSChris Mason int thr; 226ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 227ab78c84dSChris Mason 228ab78c84dSChris Mason do { 229ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 230ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 231ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 232ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 233ab78c84dSChris Mason break; 234ab78c84dSChris Mason } 235ab78c84dSChris Mason schedule(); 236ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 237ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2382dd3e67bSChris Mason harder_count++; 2392dd3e67bSChris Mason 2402dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2412dd3e67bSChris Mason harder_count < 2) 2422dd3e67bSChris Mason goto harder; 2432dd3e67bSChris Mason 2442dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2452dd3e67bSChris Mason harder_count < 10) 2462dd3e67bSChris Mason goto harder; 2472dd3e67bSChris Mason 2482dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2492dd3e67bSChris Mason harder_count < 20) 2502dd3e67bSChris Mason goto harder; 251ab78c84dSChris Mason } 252ab78c84dSChris Mason } 253ab78c84dSChris Mason 25437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 25537d1aeeeSChris Mason { 25637d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2579ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 25837d1aeeeSChris Mason wait_current_trans(root); 25937d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 26037d1aeeeSChris Mason 26137d1aeeeSChris Mason throttle_on_drops(root); 26237d1aeeeSChris Mason } 26337d1aeeeSChris Mason 26489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 26589ce8a63SChris Mason struct btrfs_root *root, int throttle) 26679154b1bSChris Mason { 26779154b1bSChris Mason struct btrfs_transaction *cur_trans; 268ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 269d6e4a428SChris Mason 270ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 271ab78c84dSChris Mason cur_trans = info->running_transaction; 272ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 273d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 274ccd467d6SChris Mason cur_trans->num_writers--; 27589ce8a63SChris Mason 27679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 27779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 27879154b1bSChris Mason put_transaction(cur_trans); 279ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 280d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 2812c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 282ab78c84dSChris Mason 283ab78c84dSChris Mason if (throttle) 28437d1aeeeSChris Mason throttle_on_drops(root); 285ab78c84dSChris Mason 28679154b1bSChris Mason return 0; 28779154b1bSChris Mason } 28879154b1bSChris Mason 28989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 29089ce8a63SChris Mason struct btrfs_root *root) 29189ce8a63SChris Mason { 29289ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 29389ce8a63SChris Mason } 29489ce8a63SChris Mason 29589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 29689ce8a63SChris Mason struct btrfs_root *root) 29789ce8a63SChris Mason { 29889ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 29989ce8a63SChris Mason } 30089ce8a63SChris Mason 30179154b1bSChris Mason 30279154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 30379154b1bSChris Mason struct btrfs_root *root) 30479154b1bSChris Mason { 3057c4452b9SChris Mason int ret; 306*777e6bd7SChris Mason int err = 0; 3077c4452b9SChris Mason int werr = 0; 308d1310b2eSChris Mason struct extent_io_tree *dirty_pages; 3097c4452b9SChris Mason struct page *page; 3107c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 311*777e6bd7SChris Mason u64 start = 0; 3125f39d397SChris Mason u64 end; 3135f39d397SChris Mason unsigned long index; 3147c4452b9SChris Mason 3157c4452b9SChris Mason if (!trans || !trans->transaction) { 3167c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 3177c4452b9SChris Mason } 3187c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 3197c4452b9SChris Mason while(1) { 320*777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3215f39d397SChris Mason EXTENT_DIRTY); 3225f39d397SChris Mason if (ret) 3237c4452b9SChris Mason break; 3245f39d397SChris Mason while(start <= end) { 325*777e6bd7SChris Mason if (btrfs_congested_async(root->fs_info, 0)) 326*777e6bd7SChris Mason congestion_wait(WRITE, HZ/10); 327*777e6bd7SChris Mason cond_resched(); 328*777e6bd7SChris Mason 3295f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 33035ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3315f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 3327c4452b9SChris Mason if (!page) 3337c4452b9SChris Mason continue; 3346702ed49SChris Mason if (PageWriteback(page)) { 3356702ed49SChris Mason if (PageDirty(page)) 3366702ed49SChris Mason wait_on_page_writeback(page); 3376702ed49SChris Mason else { 3386702ed49SChris Mason unlock_page(page); 3396702ed49SChris Mason page_cache_release(page); 3406702ed49SChris Mason continue; 3416702ed49SChris Mason } 3426702ed49SChris Mason } 3437c4452b9SChris Mason err = write_one_page(page, 0); 3447c4452b9SChris Mason if (err) 3457c4452b9SChris Mason werr = err; 3467c4452b9SChris Mason page_cache_release(page); 3477c4452b9SChris Mason } 3487c4452b9SChris Mason } 349*777e6bd7SChris Mason while(1) { 350*777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 351*777e6bd7SChris Mason EXTENT_DIRTY); 352*777e6bd7SChris Mason if (ret) 353*777e6bd7SChris Mason break; 354*777e6bd7SChris Mason 355*777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 356*777e6bd7SChris Mason while(start <= end) { 357*777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 358*777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 359*777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 360*777e6bd7SChris Mason if (!page) 361*777e6bd7SChris Mason continue; 362*777e6bd7SChris Mason if (PageDirty(page)) { 363*777e6bd7SChris Mason lock_page(page); 364*777e6bd7SChris Mason err = write_one_page(page, 0); 365*777e6bd7SChris Mason if (err) 366*777e6bd7SChris Mason werr = err; 367*777e6bd7SChris Mason } 368*777e6bd7SChris Mason wait_on_page_writeback(page); 369*777e6bd7SChris Mason page_cache_release(page); 370*777e6bd7SChris Mason cond_resched(); 371*777e6bd7SChris Mason } 372*777e6bd7SChris Mason } 3737c4452b9SChris Mason if (err) 3747c4452b9SChris Mason werr = err; 3757c4452b9SChris Mason return werr; 37679154b1bSChris Mason } 37779154b1bSChris Mason 3780b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 37979154b1bSChris Mason struct btrfs_root *root) 38079154b1bSChris Mason { 38179154b1bSChris Mason int ret; 3820b86a832SChris Mason u64 old_root_bytenr; 3830b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 38479154b1bSChris Mason 3850b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 38679154b1bSChris Mason while(1) { 3870b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 3880b86a832SChris Mason if (old_root_bytenr == root->node->start) 38979154b1bSChris Mason break; 3900b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 3910b86a832SChris Mason root->node->start); 3920b86a832SChris Mason btrfs_set_root_level(&root->root_item, 3930b86a832SChris Mason btrfs_header_level(root->node)); 39479154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 3950b86a832SChris Mason &root->root_key, 3960b86a832SChris Mason &root->root_item); 39779154b1bSChris Mason BUG_ON(ret); 3980b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 3990b86a832SChris Mason } 4000b86a832SChris Mason return 0; 4010b86a832SChris Mason } 4020b86a832SChris Mason 4030b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4040b86a832SChris Mason struct btrfs_root *root) 4050b86a832SChris Mason { 4060b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4070b86a832SChris Mason struct list_head *next; 4080b86a832SChris Mason 4090b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 4100b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4110b86a832SChris Mason list_del_init(next); 4120b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 4130b86a832SChris Mason update_cowonly_root(trans, root); 41479154b1bSChris Mason } 41579154b1bSChris Mason return 0; 41679154b1bSChris Mason } 41779154b1bSChris Mason 418b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 4195eda7b5eSChris Mason { 420f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4215eda7b5eSChris Mason 4225eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 4235eda7b5eSChris Mason if (!dirty) 4245eda7b5eSChris Mason return -ENOMEM; 4255eda7b5eSChris Mason dirty->root = root; 4265ce14bbcSChris Mason dirty->latest_root = latest; 427b48652c1SYan Zheng 428b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 429b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 430b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 4315eda7b5eSChris Mason return 0; 4325eda7b5eSChris Mason } 4335eda7b5eSChris Mason 43480b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 43535b7e476SChris Mason struct radix_tree_root *radix, 43635b7e476SChris Mason struct list_head *list) 4370f7d52f4SChris Mason { 438f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4390f7d52f4SChris Mason struct btrfs_root *gang[8]; 4400f7d52f4SChris Mason struct btrfs_root *root; 4410f7d52f4SChris Mason int i; 4420f7d52f4SChris Mason int ret; 44354aa1f4dSChris Mason int err = 0; 4445eda7b5eSChris Mason u32 refs; 44554aa1f4dSChris Mason 4460f7d52f4SChris Mason while(1) { 4470f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 4480f7d52f4SChris Mason ARRAY_SIZE(gang), 4490f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 4500f7d52f4SChris Mason if (ret == 0) 4510f7d52f4SChris Mason break; 4520f7d52f4SChris Mason for (i = 0; i < ret; i++) { 4530f7d52f4SChris Mason root = gang[i]; 4542619ba1fSChris Mason radix_tree_tag_clear(radix, 4552619ba1fSChris Mason (unsigned long)root->root_key.objectid, 4560f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 45731153d81SYan Zheng 45831153d81SYan Zheng BUG_ON(!root->ref_tree); 459017e5369SChris Mason dirty = root->dirty_root; 46031153d81SYan Zheng 4610f7d52f4SChris Mason if (root->commit_root == root->node) { 462db94535dSChris Mason WARN_ON(root->node->start != 463db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 46431153d81SYan Zheng 4655f39d397SChris Mason free_extent_buffer(root->commit_root); 4660f7d52f4SChris Mason root->commit_root = NULL; 4677ea394f1SYan Zheng root->dirty_root = NULL; 46831153d81SYan Zheng 469bcc63abbSYan spin_lock(&root->list_lock); 470bcc63abbSYan list_del_init(&dirty->root->dead_list); 471bcc63abbSYan spin_unlock(&root->list_lock); 472bcc63abbSYan 47331153d81SYan Zheng kfree(dirty->root); 47431153d81SYan Zheng kfree(dirty); 47558176a96SJosef Bacik 47658176a96SJosef Bacik /* make sure to update the root on disk 47758176a96SJosef Bacik * so we get any updates to the block used 47858176a96SJosef Bacik * counts 47958176a96SJosef Bacik */ 48058176a96SJosef Bacik err = btrfs_update_root(trans, 48158176a96SJosef Bacik root->fs_info->tree_root, 48258176a96SJosef Bacik &root->root_key, 48358176a96SJosef Bacik &root->root_item); 4840f7d52f4SChris Mason continue; 4850f7d52f4SChris Mason } 4869f3a7427SChris Mason 4879f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 4889f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 4899f3a7427SChris Mason root->root_item.drop_level = 0; 4900f7d52f4SChris Mason root->commit_root = NULL; 4917ea394f1SYan Zheng root->dirty_root = NULL; 4920f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 493db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 494db94535dSChris Mason root->node->start); 495db94535dSChris Mason btrfs_set_root_level(&root->root_item, 496db94535dSChris Mason btrfs_header_level(root->node)); 4970f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 4980f7d52f4SChris Mason &root->root_key, 4990f7d52f4SChris Mason &root->root_item); 50054aa1f4dSChris Mason if (err) 50154aa1f4dSChris Mason break; 5029f3a7427SChris Mason 5039f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 5049f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 5055eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 5069f3a7427SChris Mason &dirty->root->root_key, 5079f3a7427SChris Mason &dirty->root->root_item); 5085eda7b5eSChris Mason 5095eda7b5eSChris Mason BUG_ON(err); 5109f3a7427SChris Mason if (refs == 1) { 5110f7d52f4SChris Mason list_add(&dirty->list, list); 5129f3a7427SChris Mason } else { 5139f3a7427SChris Mason WARN_ON(1); 51431153d81SYan Zheng free_extent_buffer(dirty->root->node); 5159f3a7427SChris Mason kfree(dirty->root); 5165eda7b5eSChris Mason kfree(dirty); 5170f7d52f4SChris Mason } 5180f7d52f4SChris Mason } 5199f3a7427SChris Mason } 52054aa1f4dSChris Mason return err; 5210f7d52f4SChris Mason } 5220f7d52f4SChris Mason 523e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 524e9d0b13bSChris Mason { 525e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 526e9d0b13bSChris Mason int ret; 527e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 528d3c2fdcfSChris Mason unsigned long nr; 529e9d0b13bSChris Mason 530a2135011SChris Mason smp_mb(); 531e9d0b13bSChris Mason if (root->defrag_running) 532e9d0b13bSChris Mason return 0; 533e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5346b80053dSChris Mason while (1) { 535e9d0b13bSChris Mason root->defrag_running = 1; 536e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 537d3c2fdcfSChris Mason nr = trans->blocks_used; 538e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 539d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 540e9d0b13bSChris Mason cond_resched(); 541e9d0b13bSChris Mason 542e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5433f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 544e9d0b13bSChris Mason break; 545e9d0b13bSChris Mason } 546e9d0b13bSChris Mason root->defrag_running = 0; 547a2135011SChris Mason smp_mb(); 548e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 549e9d0b13bSChris Mason return 0; 550e9d0b13bSChris Mason } 551e9d0b13bSChris Mason 55280b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 55335b7e476SChris Mason struct list_head *list) 5540f7d52f4SChris Mason { 555f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5560f7d52f4SChris Mason struct btrfs_trans_handle *trans; 557d3c2fdcfSChris Mason unsigned long nr; 558db94535dSChris Mason u64 num_bytes; 559db94535dSChris Mason u64 bytes_used; 560bcc63abbSYan u64 max_useless; 56154aa1f4dSChris Mason int ret = 0; 5629f3a7427SChris Mason int err; 5639f3a7427SChris Mason 5640f7d52f4SChris Mason while(!list_empty(list)) { 56558176a96SJosef Bacik struct btrfs_root *root; 56658176a96SJosef Bacik 567f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 5680f7d52f4SChris Mason list_del_init(&dirty->list); 5695eda7b5eSChris Mason 570db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 57158176a96SJosef Bacik root = dirty->latest_root; 572a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 57358176a96SJosef Bacik 574a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5759f3a7427SChris Mason while(1) { 5760f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 5779f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 5789f3a7427SChris Mason if (ret != -EAGAIN) { 5799f3a7427SChris Mason break; 5809f3a7427SChris Mason } 58158176a96SJosef Bacik 5829f3a7427SChris Mason err = btrfs_update_root(trans, 5839f3a7427SChris Mason tree_root, 5849f3a7427SChris Mason &dirty->root->root_key, 5859f3a7427SChris Mason &dirty->root->root_item); 5869f3a7427SChris Mason if (err) 5879f3a7427SChris Mason ret = err; 588d3c2fdcfSChris Mason nr = trans->blocks_used; 589017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 5900f7d52f4SChris Mason BUG_ON(ret); 591a2135011SChris Mason 592a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 593d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 5944dc11904SChris Mason cond_resched(); 595a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5969f3a7427SChris Mason } 5979f3a7427SChris Mason BUG_ON(ret); 598a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 599017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 60058176a96SJosef Bacik 601a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 602db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 603db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 604db94535dSChris Mason if (num_bytes) { 60558176a96SJosef Bacik record_root_in_trans(root); 6065f39d397SChris Mason btrfs_set_root_used(&root->root_item, 607db94535dSChris Mason bytes_used - num_bytes); 60858176a96SJosef Bacik } 609a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 610a2135011SChris Mason 6119f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 61258176a96SJosef Bacik if (ret) { 61358176a96SJosef Bacik BUG(); 61454aa1f4dSChris Mason break; 61558176a96SJosef Bacik } 616a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 617a2135011SChris Mason 618bcc63abbSYan spin_lock(&root->list_lock); 619bcc63abbSYan list_del_init(&dirty->root->dead_list); 620bcc63abbSYan if (!list_empty(&root->dead_list)) { 621bcc63abbSYan struct btrfs_root *oldest; 622bcc63abbSYan oldest = list_entry(root->dead_list.prev, 623bcc63abbSYan struct btrfs_root, dead_list); 624bcc63abbSYan max_useless = oldest->root_key.offset - 1; 625bcc63abbSYan } else { 626bcc63abbSYan max_useless = root->root_key.offset - 1; 627bcc63abbSYan } 628bcc63abbSYan spin_unlock(&root->list_lock); 629bcc63abbSYan 630d3c2fdcfSChris Mason nr = trans->blocks_used; 6310f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6320f7d52f4SChris Mason BUG_ON(ret); 6335eda7b5eSChris Mason 634bcc63abbSYan ret = btrfs_remove_leaf_refs(root, max_useless); 635bcc63abbSYan BUG_ON(ret); 636bcc63abbSYan 637f510cfecSChris Mason free_extent_buffer(dirty->root->node); 6385eda7b5eSChris Mason kfree(dirty->root); 6390f7d52f4SChris Mason kfree(dirty); 640d3c2fdcfSChris Mason 641d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6424dc11904SChris Mason cond_resched(); 6430f7d52f4SChris Mason } 64454aa1f4dSChris Mason return ret; 6450f7d52f4SChris Mason } 6460f7d52f4SChris Mason 64780b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 6483063d29fSChris Mason struct btrfs_fs_info *fs_info, 6493063d29fSChris Mason struct btrfs_pending_snapshot *pending) 6503063d29fSChris Mason { 6513063d29fSChris Mason struct btrfs_key key; 65280b6794dSChris Mason struct btrfs_root_item *new_root_item; 6533063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 6543063d29fSChris Mason struct btrfs_root *root = pending->root; 6553063d29fSChris Mason struct extent_buffer *tmp; 656925baeddSChris Mason struct extent_buffer *old; 6573063d29fSChris Mason int ret; 6583b96362cSSven Wegener int namelen; 6593063d29fSChris Mason u64 objectid; 6603063d29fSChris Mason 66180b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 66280b6794dSChris Mason if (!new_root_item) { 66380b6794dSChris Mason ret = -ENOMEM; 66480b6794dSChris Mason goto fail; 66580b6794dSChris Mason } 6663063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 6673063d29fSChris Mason if (ret) 6683063d29fSChris Mason goto fail; 6693063d29fSChris Mason 67080b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 6713063d29fSChris Mason 6723063d29fSChris Mason key.objectid = objectid; 6733063d29fSChris Mason key.offset = 1; 6743063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 6753063d29fSChris Mason 676925baeddSChris Mason old = btrfs_lock_root_node(root); 67765b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 6783063d29fSChris Mason 679925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 680925baeddSChris Mason btrfs_tree_unlock(old); 681925baeddSChris Mason free_extent_buffer(old); 6823063d29fSChris Mason 68380b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 68480b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 6853063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 68680b6794dSChris Mason new_root_item); 687925baeddSChris Mason btrfs_tree_unlock(tmp); 6883063d29fSChris Mason free_extent_buffer(tmp); 6893063d29fSChris Mason if (ret) 6903063d29fSChris Mason goto fail; 6913063d29fSChris Mason 6923063d29fSChris Mason /* 6933063d29fSChris Mason * insert the directory item 6943063d29fSChris Mason */ 6953063d29fSChris Mason key.offset = (u64)-1; 6963b96362cSSven Wegener namelen = strlen(pending->name); 6973063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 6983b96362cSSven Wegener pending->name, namelen, 6993063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 700aec7477bSJosef Bacik &key, BTRFS_FT_DIR, 0); 7013063d29fSChris Mason 7023063d29fSChris Mason if (ret) 7033063d29fSChris Mason goto fail; 7043063d29fSChris Mason 7053063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 7063063d29fSChris Mason pending->name, strlen(pending->name), objectid, 707aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 7083b96362cSSven Wegener 7093b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 7103b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 7113b96362cSSven Wegener 7123063d29fSChris Mason fail: 71380b6794dSChris Mason kfree(new_root_item); 7143063d29fSChris Mason return ret; 7153063d29fSChris Mason } 7163063d29fSChris Mason 71780b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 7183063d29fSChris Mason struct btrfs_fs_info *fs_info) 7193063d29fSChris Mason { 7203063d29fSChris Mason struct btrfs_pending_snapshot *pending; 7213063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 7223063d29fSChris Mason int ret; 7233063d29fSChris Mason 7243063d29fSChris Mason while(!list_empty(head)) { 7253063d29fSChris Mason pending = list_entry(head->next, 7263063d29fSChris Mason struct btrfs_pending_snapshot, list); 7273063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 7283063d29fSChris Mason BUG_ON(ret); 7293063d29fSChris Mason list_del(&pending->list); 7303063d29fSChris Mason kfree(pending->name); 7313063d29fSChris Mason kfree(pending); 7323063d29fSChris Mason } 733dc17ff8fSChris Mason return 0; 734dc17ff8fSChris Mason } 735dc17ff8fSChris Mason 73679154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 73779154b1bSChris Mason struct btrfs_root *root) 73879154b1bSChris Mason { 73915ee9bc7SJosef Bacik unsigned long joined = 0; 74015ee9bc7SJosef Bacik unsigned long timeout = 1; 74179154b1bSChris Mason struct btrfs_transaction *cur_trans; 7428fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 7430b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 7440f7d52f4SChris Mason struct list_head dirty_fs_roots; 745d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 74679154b1bSChris Mason DEFINE_WAIT(wait); 74715ee9bc7SJosef Bacik int ret; 74879154b1bSChris Mason 7490f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 750d6e4a428SChris Mason 75179154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 75279154b1bSChris Mason if (trans->transaction->in_commit) { 75379154b1bSChris Mason cur_trans = trans->transaction; 75479154b1bSChris Mason trans->transaction->use_count++; 755ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 75679154b1bSChris Mason btrfs_end_transaction(trans, root); 757ccd467d6SChris Mason 75879154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 75979154b1bSChris Mason BUG_ON(ret); 76015ee9bc7SJosef Bacik 76115ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 76279154b1bSChris Mason put_transaction(cur_trans); 76315ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 76415ee9bc7SJosef Bacik 76579154b1bSChris Mason return 0; 76679154b1bSChris Mason } 7674313b399SChris Mason 7684313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 7694313b399SChris Mason if (!pinned_copy) 7704313b399SChris Mason return -ENOMEM; 7714313b399SChris Mason 772d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 7734313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 7744313b399SChris Mason 7752c90e5d6SChris Mason trans->transaction->in_commit = 1; 776f9295749SChris Mason trans->transaction->blocked = 1; 777ccd467d6SChris Mason cur_trans = trans->transaction; 778ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 779ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 780ccd467d6SChris Mason struct btrfs_transaction, list); 781ccd467d6SChris Mason if (!prev_trans->commit_done) { 782ccd467d6SChris Mason prev_trans->use_count++; 783ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 784ccd467d6SChris Mason 785ccd467d6SChris Mason wait_for_commit(root, prev_trans); 786ccd467d6SChris Mason 787ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 78815ee9bc7SJosef Bacik put_transaction(prev_trans); 789ccd467d6SChris Mason } 790ccd467d6SChris Mason } 79115ee9bc7SJosef Bacik 79215ee9bc7SJosef Bacik do { 7937ea394f1SYan Zheng int snap_pending = 0; 79415ee9bc7SJosef Bacik joined = cur_trans->num_joined; 7957ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 7967ea394f1SYan Zheng snap_pending = 1; 7977ea394f1SYan Zheng 7982c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 79915ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 80079154b1bSChris Mason TASK_UNINTERRUPTIBLE); 80115ee9bc7SJosef Bacik 80215ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 80315ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 80415ee9bc7SJosef Bacik else 80515ee9bc7SJosef Bacik timeout = 1; 80615ee9bc7SJosef Bacik 80779154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 80815ee9bc7SJosef Bacik 8097ea394f1SYan Zheng if (snap_pending) { 8107ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 8117ea394f1SYan Zheng BUG_ON(ret); 8127ea394f1SYan Zheng } 8137ea394f1SYan Zheng 81415ee9bc7SJosef Bacik schedule_timeout(timeout); 81515ee9bc7SJosef Bacik 81679154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 81715ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 81815ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 81915ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 82015ee9bc7SJosef Bacik 8213063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 8223063d29fSChris Mason BUG_ON(ret); 8233063d29fSChris Mason 8242c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 825dc17ff8fSChris Mason 82654aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 82754aa1f4dSChris Mason &dirty_fs_roots); 82854aa1f4dSChris Mason BUG_ON(ret); 82954aa1f4dSChris Mason 83079154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 83179154b1bSChris Mason BUG_ON(ret); 83254aa1f4dSChris Mason 83378fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 834cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 83578fae27eSChris Mason root->fs_info->running_transaction = NULL; 836cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8374b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 8384b52dff6SChris Mason cur_trans->transid); 8394b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 840db94535dSChris Mason root->fs_info->tree_root->node->start); 841db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 842db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 8435f39d397SChris Mason 8440b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 8450b86a832SChris Mason chunk_root->node->start); 8460b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 8470b86a832SChris Mason btrfs_header_level(chunk_root->node)); 848a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 8494b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 850ccd467d6SChris Mason 8514313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 852ccd467d6SChris Mason 853f9295749SChris Mason trans->transaction->blocked = 0; 854e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 855f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 856e6dcd2dcSChris Mason 85778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 85879154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 85979154b1bSChris Mason BUG_ON(ret); 86079154b1bSChris Mason write_ctree_super(trans, root); 8614313b399SChris Mason 8624313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 86378fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 8644313b399SChris Mason 8654313b399SChris Mason kfree(pinned_copy); 8664313b399SChris Mason 8672c90e5d6SChris Mason cur_trans->commit_done = 1; 86815ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 8692c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 87079154b1bSChris Mason put_transaction(cur_trans); 87178fae27eSChris Mason put_transaction(cur_trans); 87258176a96SJosef Bacik 873bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 874facda1e7SChris Mason if (root->fs_info->closing) 875facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 87658176a96SJosef Bacik 87778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 8782c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 87979154b1bSChris Mason 880facda1e7SChris Mason if (root->fs_info->closing) { 8810f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 882facda1e7SChris Mason } 88379154b1bSChris Mason return ret; 88479154b1bSChris Mason } 88579154b1bSChris Mason 886e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 887e9d0b13bSChris Mason { 888e9d0b13bSChris Mason struct list_head dirty_roots; 889e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 890a74a4b97SChris Mason again: 891e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 892e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 893e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 894e9d0b13bSChris Mason 895e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 896e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 897a74a4b97SChris Mason goto again; 898e9d0b13bSChris Mason } 899e9d0b13bSChris Mason return 0; 900e9d0b13bSChris Mason } 901