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" 28e02119d5SChris Mason #include "tree-log.h" 2979154b1bSChris Mason 3078fae27eSChris Mason static int total_trans = 0; 312c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 322c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 332c90e5d6SChris Mason 340f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 350f7d52f4SChris Mason 3680b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3779154b1bSChris Mason { 382c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3979154b1bSChris Mason transaction->use_count--; 4078fae27eSChris Mason if (transaction->use_count == 0) { 4178fae27eSChris Mason WARN_ON(total_trans == 0); 4278fae27eSChris Mason total_trans--; 438fd17795SChris Mason list_del_init(&transaction->list); 442c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 452c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4679154b1bSChris Mason } 4778fae27eSChris Mason } 4879154b1bSChris Mason 4980b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5079154b1bSChris Mason { 5179154b1bSChris Mason struct btrfs_transaction *cur_trans; 5279154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5379154b1bSChris Mason if (!cur_trans) { 542c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 552c90e5d6SChris Mason GFP_NOFS); 5678fae27eSChris Mason total_trans++; 5779154b1bSChris Mason BUG_ON(!cur_trans); 580f7d52f4SChris Mason root->fs_info->generation++; 59e18e4809SChris Mason root->fs_info->last_alloc = 0; 604529ba49SChris Mason root->fs_info->last_data_alloc = 0; 61e02119d5SChris Mason root->fs_info->last_log_alloc = 0; 6215ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6315ee9bc7SJosef Bacik cur_trans->num_joined = 0; 640f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6579154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6679154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6779154b1bSChris Mason cur_trans->in_commit = 0; 68f9295749SChris Mason cur_trans->blocked = 0; 69d5719762SChris Mason cur_trans->use_count = 1; 7079154b1bSChris Mason cur_trans->commit_done = 0; 7108607c1bSChris Mason cur_trans->start_time = get_seconds(); 723063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 738fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 74d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 755f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 765f39d397SChris Mason GFP_NOFS); 7748ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 7848ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 7948ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8015ee9bc7SJosef Bacik } else { 8179154b1bSChris Mason cur_trans->num_writers++; 8215ee9bc7SJosef Bacik cur_trans->num_joined++; 8315ee9bc7SJosef Bacik } 8415ee9bc7SJosef Bacik 8579154b1bSChris Mason return 0; 8679154b1bSChris Mason } 8779154b1bSChris Mason 88e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 896702ed49SChris Mason { 90f321e491SYan Zheng struct btrfs_dirty_root *dirty; 916702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 926702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 936702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 946702ed49SChris Mason if (root->root_item.refs != 0) { 956702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 966702ed49SChris Mason (unsigned long)root->root_key.objectid, 976702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 9831153d81SYan Zheng 9931153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10031153d81SYan Zheng BUG_ON(!dirty); 10131153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 10231153d81SYan Zheng BUG_ON(!dirty->root); 10331153d81SYan Zheng dirty->latest_root = root; 10431153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 10531153d81SYan Zheng 106925baeddSChris Mason root->commit_root = btrfs_root_node(root); 10731153d81SYan Zheng 10831153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 10931153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 110bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 11131153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 112bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 11331153d81SYan Zheng dirty->root->node = root->commit_root; 11431153d81SYan Zheng dirty->root->commit_root = NULL; 115bcc63abbSYan 116bcc63abbSYan spin_lock(&root->list_lock); 117bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 118bcc63abbSYan spin_unlock(&root->list_lock); 119bcc63abbSYan 120bcc63abbSYan root->dirty_root = dirty; 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 12937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 13079154b1bSChris Mason { 131f9295749SChris Mason struct btrfs_transaction *cur_trans; 13279154b1bSChris Mason 133f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 13437d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 135f9295749SChris Mason DEFINE_WAIT(wait); 136f9295749SChris Mason cur_trans->use_count++; 137f9295749SChris Mason while(1) { 138f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 139f9295749SChris Mason TASK_UNINTERRUPTIBLE); 140f9295749SChris Mason if (cur_trans->blocked) { 141f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 142f9295749SChris Mason schedule(); 143f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 144f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 145f9295749SChris Mason &wait); 146f9295749SChris Mason } else { 147f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 148f9295749SChris Mason &wait); 149f9295749SChris Mason break; 150f9295749SChris Mason } 151f9295749SChris Mason } 152f9295749SChris Mason put_transaction(cur_trans); 153f9295749SChris Mason } 15437d1aeeeSChris Mason } 15537d1aeeeSChris Mason 156e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1579ca9ee09SSage Weil int num_blocks, int wait) 15837d1aeeeSChris Mason { 15937d1aeeeSChris Mason struct btrfs_trans_handle *h = 16037d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 16137d1aeeeSChris Mason int ret; 16237d1aeeeSChris Mason 16337d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1644bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1654bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 16637d1aeeeSChris Mason wait_current_trans(root); 16779154b1bSChris Mason ret = join_transaction(root); 16879154b1bSChris Mason BUG_ON(ret); 1690f7d52f4SChris Mason 170e02119d5SChris Mason btrfs_record_root_in_trans(root); 1716702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 17279154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 17379154b1bSChris Mason h->blocks_reserved = num_blocks; 17479154b1bSChris Mason h->blocks_used = 0; 17531f3c99bSChris Mason h->block_group = NULL; 17626b8003fSChris Mason h->alloc_exclude_nr = 0; 17726b8003fSChris Mason h->alloc_exclude_start = 0; 17879154b1bSChris Mason root->fs_info->running_transaction->use_count++; 17979154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 18079154b1bSChris Mason return h; 18179154b1bSChris Mason } 18279154b1bSChris Mason 183f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 184f9295749SChris Mason int num_blocks) 185f9295749SChris Mason { 1869ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 187f9295749SChris Mason } 188f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 189f9295749SChris Mason int num_blocks) 190f9295749SChris Mason { 1919ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 192f9295749SChris Mason } 193f9295749SChris Mason 1949ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 1959ca9ee09SSage Weil int num_blocks) 1969ca9ee09SSage Weil { 1979ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 1989ca9ee09SSage Weil } 1999ca9ee09SSage Weil 2009ca9ee09SSage Weil 20189ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 20289ce8a63SChris Mason struct btrfs_transaction *commit) 20389ce8a63SChris Mason { 20489ce8a63SChris Mason DEFINE_WAIT(wait); 20589ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 20689ce8a63SChris Mason while(!commit->commit_done) { 20789ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 20889ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 20989ce8a63SChris Mason if (commit->commit_done) 21089ce8a63SChris Mason break; 21189ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21289ce8a63SChris Mason schedule(); 21389ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21489ce8a63SChris Mason } 21589ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21689ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 21789ce8a63SChris Mason return 0; 21889ce8a63SChris Mason } 21989ce8a63SChris Mason 22037d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 221ab78c84dSChris Mason { 222ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2232dd3e67bSChris Mason int harder_count = 0; 224ab78c84dSChris Mason 2252dd3e67bSChris Mason harder: 226ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 227ab78c84dSChris Mason DEFINE_WAIT(wait); 228ab78c84dSChris Mason int thr; 229ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 230ab78c84dSChris Mason 231ab78c84dSChris Mason do { 232ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 233ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 234ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 235ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 236ab78c84dSChris Mason break; 237ab78c84dSChris Mason } 238ab78c84dSChris Mason schedule(); 239ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 240ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2412dd3e67bSChris Mason harder_count++; 2422dd3e67bSChris Mason 2432dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2442dd3e67bSChris Mason harder_count < 2) 2452dd3e67bSChris Mason goto harder; 2462dd3e67bSChris Mason 2472dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2482dd3e67bSChris Mason harder_count < 10) 2492dd3e67bSChris Mason goto harder; 2502dd3e67bSChris Mason 2512dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2522dd3e67bSChris Mason harder_count < 20) 2532dd3e67bSChris Mason goto harder; 254ab78c84dSChris Mason } 255ab78c84dSChris Mason } 256ab78c84dSChris Mason 25737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 25837d1aeeeSChris Mason { 25937d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2609ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 26137d1aeeeSChris Mason wait_current_trans(root); 26237d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 26337d1aeeeSChris Mason 26437d1aeeeSChris Mason throttle_on_drops(root); 26537d1aeeeSChris Mason } 26637d1aeeeSChris Mason 26789ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 26889ce8a63SChris Mason struct btrfs_root *root, int throttle) 26979154b1bSChris Mason { 27079154b1bSChris Mason struct btrfs_transaction *cur_trans; 271ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 272d6e4a428SChris Mason 273ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 274ab78c84dSChris Mason cur_trans = info->running_transaction; 275ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 276d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 277ccd467d6SChris Mason cur_trans->num_writers--; 27889ce8a63SChris Mason 27979154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 28079154b1bSChris Mason wake_up(&cur_trans->writer_wait); 28179154b1bSChris Mason put_transaction(cur_trans); 282ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 283d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 2842c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 285ab78c84dSChris Mason 286ab78c84dSChris Mason if (throttle) 28737d1aeeeSChris Mason throttle_on_drops(root); 288ab78c84dSChris Mason 28979154b1bSChris Mason return 0; 29079154b1bSChris Mason } 29179154b1bSChris Mason 29289ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 29389ce8a63SChris Mason struct btrfs_root *root) 29489ce8a63SChris Mason { 29589ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 29689ce8a63SChris Mason } 29789ce8a63SChris Mason 29889ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 29989ce8a63SChris Mason struct btrfs_root *root) 30089ce8a63SChris Mason { 30189ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 30289ce8a63SChris Mason } 30389ce8a63SChris Mason 30479154b1bSChris Mason 305d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 306d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 30779154b1bSChris Mason { 3087c4452b9SChris Mason int ret; 309777e6bd7SChris Mason int err = 0; 3107c4452b9SChris Mason int werr = 0; 3117c4452b9SChris Mason struct page *page; 3127c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 313777e6bd7SChris Mason u64 start = 0; 3145f39d397SChris Mason u64 end; 3155f39d397SChris Mason unsigned long index; 3167c4452b9SChris Mason 3177c4452b9SChris Mason while(1) { 318777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3195f39d397SChris Mason EXTENT_DIRTY); 3205f39d397SChris Mason if (ret) 3217c4452b9SChris Mason break; 3225f39d397SChris Mason while(start <= end) { 323777e6bd7SChris Mason cond_resched(); 324777e6bd7SChris Mason 3255f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 32635ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3274bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3287c4452b9SChris Mason if (!page) 3297c4452b9SChris Mason continue; 3304bef0848SChris Mason 3314bef0848SChris Mason btree_lock_page_hook(page); 3324bef0848SChris Mason if (!page->mapping) { 3334bef0848SChris Mason unlock_page(page); 3344bef0848SChris Mason page_cache_release(page); 3354bef0848SChris Mason continue; 3364bef0848SChris Mason } 3374bef0848SChris Mason 3386702ed49SChris Mason if (PageWriteback(page)) { 3396702ed49SChris Mason if (PageDirty(page)) 3406702ed49SChris Mason wait_on_page_writeback(page); 3416702ed49SChris Mason else { 3426702ed49SChris Mason unlock_page(page); 3436702ed49SChris Mason page_cache_release(page); 3446702ed49SChris Mason continue; 3456702ed49SChris Mason } 3466702ed49SChris Mason } 3477c4452b9SChris Mason err = write_one_page(page, 0); 3487c4452b9SChris Mason if (err) 3497c4452b9SChris Mason werr = err; 3507c4452b9SChris Mason page_cache_release(page); 3517c4452b9SChris Mason } 3527c4452b9SChris Mason } 353777e6bd7SChris Mason while(1) { 354777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 355777e6bd7SChris Mason EXTENT_DIRTY); 356777e6bd7SChris Mason if (ret) 357777e6bd7SChris Mason break; 358777e6bd7SChris Mason 359777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 360777e6bd7SChris Mason while(start <= end) { 361777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 362777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 363777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 364777e6bd7SChris Mason if (!page) 365777e6bd7SChris Mason continue; 366777e6bd7SChris Mason if (PageDirty(page)) { 3674bef0848SChris Mason btree_lock_page_hook(page); 3684bef0848SChris Mason wait_on_page_writeback(page); 369777e6bd7SChris Mason err = write_one_page(page, 0); 370777e6bd7SChris Mason if (err) 371777e6bd7SChris Mason werr = err; 372777e6bd7SChris Mason } 373777e6bd7SChris Mason wait_on_page_writeback(page); 374777e6bd7SChris Mason page_cache_release(page); 375777e6bd7SChris Mason cond_resched(); 376777e6bd7SChris Mason } 377777e6bd7SChris Mason } 3787c4452b9SChris Mason if (err) 3797c4452b9SChris Mason werr = err; 3807c4452b9SChris Mason return werr; 38179154b1bSChris Mason } 38279154b1bSChris Mason 383d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 384d0c803c4SChris Mason struct btrfs_root *root) 385d0c803c4SChris Mason { 386d0c803c4SChris Mason if (!trans || !trans->transaction) { 387d0c803c4SChris Mason struct inode *btree_inode; 388d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 389d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 390d0c803c4SChris Mason } 391d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 392d0c803c4SChris Mason &trans->transaction->dirty_pages); 393d0c803c4SChris Mason } 394d0c803c4SChris Mason 3950b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 39679154b1bSChris Mason struct btrfs_root *root) 39779154b1bSChris Mason { 39879154b1bSChris Mason int ret; 3990b86a832SChris Mason u64 old_root_bytenr; 4000b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 40179154b1bSChris Mason 4020b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 40379154b1bSChris Mason while(1) { 4040b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4050b86a832SChris Mason if (old_root_bytenr == root->node->start) 40679154b1bSChris Mason break; 4070b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 4080b86a832SChris Mason root->node->start); 4090b86a832SChris Mason btrfs_set_root_level(&root->root_item, 4100b86a832SChris Mason btrfs_header_level(root->node)); 41179154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4120b86a832SChris Mason &root->root_key, 4130b86a832SChris Mason &root->root_item); 41479154b1bSChris Mason BUG_ON(ret); 4150b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 4160b86a832SChris Mason } 4170b86a832SChris Mason return 0; 4180b86a832SChris Mason } 4190b86a832SChris Mason 4200b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4210b86a832SChris Mason struct btrfs_root *root) 4220b86a832SChris Mason { 4230b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4240b86a832SChris Mason struct list_head *next; 4250b86a832SChris Mason 4260b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 4270b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4280b86a832SChris Mason list_del_init(next); 4290b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 4300b86a832SChris Mason update_cowonly_root(trans, root); 43179154b1bSChris Mason } 43279154b1bSChris Mason return 0; 43379154b1bSChris Mason } 43479154b1bSChris Mason 435b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 4365eda7b5eSChris Mason { 437f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4385eda7b5eSChris Mason 4395eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 4405eda7b5eSChris Mason if (!dirty) 4415eda7b5eSChris Mason return -ENOMEM; 4425eda7b5eSChris Mason dirty->root = root; 4435ce14bbcSChris Mason dirty->latest_root = latest; 444b48652c1SYan Zheng 445b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 446b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 447b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 4485eda7b5eSChris Mason return 0; 4495eda7b5eSChris Mason } 4505eda7b5eSChris Mason 45180b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 45235b7e476SChris Mason struct radix_tree_root *radix, 45335b7e476SChris Mason struct list_head *list) 4540f7d52f4SChris Mason { 455f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4560f7d52f4SChris Mason struct btrfs_root *gang[8]; 4570f7d52f4SChris Mason struct btrfs_root *root; 4580f7d52f4SChris Mason int i; 4590f7d52f4SChris Mason int ret; 46054aa1f4dSChris Mason int err = 0; 4615eda7b5eSChris Mason u32 refs; 46254aa1f4dSChris Mason 4630f7d52f4SChris Mason while(1) { 4640f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 4650f7d52f4SChris Mason ARRAY_SIZE(gang), 4660f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 4670f7d52f4SChris Mason if (ret == 0) 4680f7d52f4SChris Mason break; 4690f7d52f4SChris Mason for (i = 0; i < ret; i++) { 4700f7d52f4SChris Mason root = gang[i]; 4712619ba1fSChris Mason radix_tree_tag_clear(radix, 4722619ba1fSChris Mason (unsigned long)root->root_key.objectid, 4730f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 47431153d81SYan Zheng 47531153d81SYan Zheng BUG_ON(!root->ref_tree); 476017e5369SChris Mason dirty = root->dirty_root; 47731153d81SYan Zheng 478e02119d5SChris Mason btrfs_free_log(trans, root); 479e02119d5SChris Mason 4800f7d52f4SChris Mason if (root->commit_root == root->node) { 481db94535dSChris Mason WARN_ON(root->node->start != 482db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 48331153d81SYan Zheng 4845f39d397SChris Mason free_extent_buffer(root->commit_root); 4850f7d52f4SChris Mason root->commit_root = NULL; 4867ea394f1SYan Zheng root->dirty_root = NULL; 48731153d81SYan Zheng 488bcc63abbSYan spin_lock(&root->list_lock); 489bcc63abbSYan list_del_init(&dirty->root->dead_list); 490bcc63abbSYan spin_unlock(&root->list_lock); 491bcc63abbSYan 49231153d81SYan Zheng kfree(dirty->root); 49331153d81SYan Zheng kfree(dirty); 49458176a96SJosef Bacik 49558176a96SJosef Bacik /* make sure to update the root on disk 49658176a96SJosef Bacik * so we get any updates to the block used 49758176a96SJosef Bacik * counts 49858176a96SJosef Bacik */ 49958176a96SJosef Bacik err = btrfs_update_root(trans, 50058176a96SJosef Bacik root->fs_info->tree_root, 50158176a96SJosef Bacik &root->root_key, 50258176a96SJosef Bacik &root->root_item); 5030f7d52f4SChris Mason continue; 5040f7d52f4SChris Mason } 5059f3a7427SChris Mason 5069f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 5079f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 5089f3a7427SChris Mason root->root_item.drop_level = 0; 5090f7d52f4SChris Mason root->commit_root = NULL; 5107ea394f1SYan Zheng root->dirty_root = NULL; 5110f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 512db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 513db94535dSChris Mason root->node->start); 514db94535dSChris Mason btrfs_set_root_level(&root->root_item, 515db94535dSChris Mason btrfs_header_level(root->node)); 5160f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 5170f7d52f4SChris Mason &root->root_key, 5180f7d52f4SChris Mason &root->root_item); 51954aa1f4dSChris Mason if (err) 52054aa1f4dSChris Mason break; 5219f3a7427SChris Mason 5229f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 5239f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 5245eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 5259f3a7427SChris Mason &dirty->root->root_key, 5269f3a7427SChris Mason &dirty->root->root_item); 5275eda7b5eSChris Mason 5285eda7b5eSChris Mason BUG_ON(err); 5299f3a7427SChris Mason if (refs == 1) { 5300f7d52f4SChris Mason list_add(&dirty->list, list); 5319f3a7427SChris Mason } else { 5329f3a7427SChris Mason WARN_ON(1); 53331153d81SYan Zheng free_extent_buffer(dirty->root->node); 5349f3a7427SChris Mason kfree(dirty->root); 5355eda7b5eSChris Mason kfree(dirty); 5360f7d52f4SChris Mason } 5370f7d52f4SChris Mason } 5389f3a7427SChris Mason } 53954aa1f4dSChris Mason return err; 5400f7d52f4SChris Mason } 5410f7d52f4SChris Mason 542e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 543e9d0b13bSChris Mason { 544e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 545e9d0b13bSChris Mason int ret; 546e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 547d3c2fdcfSChris Mason unsigned long nr; 548e9d0b13bSChris Mason 549a2135011SChris Mason smp_mb(); 550e9d0b13bSChris Mason if (root->defrag_running) 551e9d0b13bSChris Mason return 0; 552e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5536b80053dSChris Mason while (1) { 554e9d0b13bSChris Mason root->defrag_running = 1; 555e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 556d3c2fdcfSChris Mason nr = trans->blocks_used; 557e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 558d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 559e9d0b13bSChris Mason cond_resched(); 560e9d0b13bSChris Mason 561e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5623f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 563e9d0b13bSChris Mason break; 564e9d0b13bSChris Mason } 565e9d0b13bSChris Mason root->defrag_running = 0; 566a2135011SChris Mason smp_mb(); 567e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 568e9d0b13bSChris Mason return 0; 569e9d0b13bSChris Mason } 570e9d0b13bSChris Mason 57180b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 57235b7e476SChris Mason struct list_head *list) 5730f7d52f4SChris Mason { 574f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5750f7d52f4SChris Mason struct btrfs_trans_handle *trans; 576d3c2fdcfSChris Mason unsigned long nr; 577db94535dSChris Mason u64 num_bytes; 578db94535dSChris Mason u64 bytes_used; 579bcc63abbSYan u64 max_useless; 58054aa1f4dSChris Mason int ret = 0; 5819f3a7427SChris Mason int err; 5829f3a7427SChris Mason 5830f7d52f4SChris Mason while(!list_empty(list)) { 58458176a96SJosef Bacik struct btrfs_root *root; 58558176a96SJosef Bacik 586f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 5870f7d52f4SChris Mason list_del_init(&dirty->list); 5885eda7b5eSChris Mason 589db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 59058176a96SJosef Bacik root = dirty->latest_root; 591a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 59258176a96SJosef Bacik 593a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5949f3a7427SChris Mason while(1) { 5950f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 5969f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 5979f3a7427SChris Mason if (ret != -EAGAIN) { 5989f3a7427SChris Mason break; 5999f3a7427SChris Mason } 60058176a96SJosef Bacik 6019f3a7427SChris Mason err = btrfs_update_root(trans, 6029f3a7427SChris Mason tree_root, 6039f3a7427SChris Mason &dirty->root->root_key, 6049f3a7427SChris Mason &dirty->root->root_item); 6059f3a7427SChris Mason if (err) 6069f3a7427SChris Mason ret = err; 607d3c2fdcfSChris Mason nr = trans->blocks_used; 608017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6090f7d52f4SChris Mason BUG_ON(ret); 610a2135011SChris Mason 611a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 612d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6134dc11904SChris Mason cond_resched(); 614a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 6159f3a7427SChris Mason } 6169f3a7427SChris Mason BUG_ON(ret); 617a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 618017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 61958176a96SJosef Bacik 620a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 621db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 622db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 623db94535dSChris Mason if (num_bytes) { 624e02119d5SChris Mason btrfs_record_root_in_trans(root); 6255f39d397SChris Mason btrfs_set_root_used(&root->root_item, 626db94535dSChris Mason bytes_used - num_bytes); 62758176a96SJosef Bacik } 628a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 629a2135011SChris Mason 6309f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 63158176a96SJosef Bacik if (ret) { 63258176a96SJosef Bacik BUG(); 63354aa1f4dSChris Mason break; 63458176a96SJosef Bacik } 635a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 636a2135011SChris Mason 637bcc63abbSYan spin_lock(&root->list_lock); 638bcc63abbSYan list_del_init(&dirty->root->dead_list); 639bcc63abbSYan if (!list_empty(&root->dead_list)) { 640bcc63abbSYan struct btrfs_root *oldest; 641bcc63abbSYan oldest = list_entry(root->dead_list.prev, 642bcc63abbSYan struct btrfs_root, dead_list); 643bcc63abbSYan max_useless = oldest->root_key.offset - 1; 644bcc63abbSYan } else { 645bcc63abbSYan max_useless = root->root_key.offset - 1; 646bcc63abbSYan } 647bcc63abbSYan spin_unlock(&root->list_lock); 648bcc63abbSYan 649d3c2fdcfSChris Mason nr = trans->blocks_used; 6500f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6510f7d52f4SChris Mason BUG_ON(ret); 6525eda7b5eSChris Mason 653*e4657689SZheng Yan ret = btrfs_remove_leaf_refs(root, max_useless, 0); 654bcc63abbSYan BUG_ON(ret); 655bcc63abbSYan 656f510cfecSChris Mason free_extent_buffer(dirty->root->node); 6575eda7b5eSChris Mason kfree(dirty->root); 6580f7d52f4SChris Mason kfree(dirty); 659d3c2fdcfSChris Mason 660d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6614dc11904SChris Mason cond_resched(); 6620f7d52f4SChris Mason } 66354aa1f4dSChris Mason return ret; 6640f7d52f4SChris Mason } 6650f7d52f4SChris Mason 66680b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 6673063d29fSChris Mason struct btrfs_fs_info *fs_info, 6683063d29fSChris Mason struct btrfs_pending_snapshot *pending) 6693063d29fSChris Mason { 6703063d29fSChris Mason struct btrfs_key key; 67180b6794dSChris Mason struct btrfs_root_item *new_root_item; 6723063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 6733063d29fSChris Mason struct btrfs_root *root = pending->root; 6743063d29fSChris Mason struct extent_buffer *tmp; 675925baeddSChris Mason struct extent_buffer *old; 6763063d29fSChris Mason int ret; 6773b96362cSSven Wegener int namelen; 6783063d29fSChris Mason u64 objectid; 6793063d29fSChris Mason 68080b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 68180b6794dSChris Mason if (!new_root_item) { 68280b6794dSChris Mason ret = -ENOMEM; 68380b6794dSChris Mason goto fail; 68480b6794dSChris Mason } 6853063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 6863063d29fSChris Mason if (ret) 6873063d29fSChris Mason goto fail; 6883063d29fSChris Mason 68980b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 6903063d29fSChris Mason 6913063d29fSChris Mason key.objectid = objectid; 6923063d29fSChris Mason key.offset = 1; 6933063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 6943063d29fSChris Mason 695925baeddSChris Mason old = btrfs_lock_root_node(root); 69665b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 6973063d29fSChris Mason 698925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 699925baeddSChris Mason btrfs_tree_unlock(old); 700925baeddSChris Mason free_extent_buffer(old); 7013063d29fSChris Mason 70280b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 70380b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 7043063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 70580b6794dSChris Mason new_root_item); 706925baeddSChris Mason btrfs_tree_unlock(tmp); 7073063d29fSChris Mason free_extent_buffer(tmp); 7083063d29fSChris Mason if (ret) 7093063d29fSChris Mason goto fail; 7103063d29fSChris Mason 7113063d29fSChris Mason /* 7123063d29fSChris Mason * insert the directory item 7133063d29fSChris Mason */ 7143063d29fSChris Mason key.offset = (u64)-1; 7153b96362cSSven Wegener namelen = strlen(pending->name); 7163063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 7173b96362cSSven Wegener pending->name, namelen, 7183063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 719aec7477bSJosef Bacik &key, BTRFS_FT_DIR, 0); 7203063d29fSChris Mason 7213063d29fSChris Mason if (ret) 7223063d29fSChris Mason goto fail; 7233063d29fSChris Mason 7243063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 7253063d29fSChris Mason pending->name, strlen(pending->name), objectid, 726aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 7273b96362cSSven Wegener 7283b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 7293b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 7303b96362cSSven Wegener 7313063d29fSChris Mason fail: 73280b6794dSChris Mason kfree(new_root_item); 7333063d29fSChris Mason return ret; 7343063d29fSChris Mason } 7353063d29fSChris Mason 73680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 7373063d29fSChris Mason struct btrfs_fs_info *fs_info) 7383063d29fSChris Mason { 7393063d29fSChris Mason struct btrfs_pending_snapshot *pending; 7403063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 7413063d29fSChris Mason int ret; 7423063d29fSChris Mason 7433063d29fSChris Mason while(!list_empty(head)) { 7443063d29fSChris Mason pending = list_entry(head->next, 7453063d29fSChris Mason struct btrfs_pending_snapshot, list); 7463063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 7473063d29fSChris Mason BUG_ON(ret); 7483063d29fSChris Mason list_del(&pending->list); 7493063d29fSChris Mason kfree(pending->name); 7503063d29fSChris Mason kfree(pending); 7513063d29fSChris Mason } 752dc17ff8fSChris Mason return 0; 753dc17ff8fSChris Mason } 754dc17ff8fSChris Mason 75579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 75679154b1bSChris Mason struct btrfs_root *root) 75779154b1bSChris Mason { 75815ee9bc7SJosef Bacik unsigned long joined = 0; 75915ee9bc7SJosef Bacik unsigned long timeout = 1; 76079154b1bSChris Mason struct btrfs_transaction *cur_trans; 7618fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 7620b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 7630f7d52f4SChris Mason struct list_head dirty_fs_roots; 764d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 76579154b1bSChris Mason DEFINE_WAIT(wait); 76615ee9bc7SJosef Bacik int ret; 76779154b1bSChris Mason 7680f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 76979154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 77079154b1bSChris Mason if (trans->transaction->in_commit) { 77179154b1bSChris Mason cur_trans = trans->transaction; 77279154b1bSChris Mason trans->transaction->use_count++; 773ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 77479154b1bSChris Mason btrfs_end_transaction(trans, root); 775ccd467d6SChris Mason 77679154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 77779154b1bSChris Mason BUG_ON(ret); 77815ee9bc7SJosef Bacik 77915ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 78079154b1bSChris Mason put_transaction(cur_trans); 78115ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 78215ee9bc7SJosef Bacik 78379154b1bSChris Mason return 0; 78479154b1bSChris Mason } 7854313b399SChris Mason 7864313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 7874313b399SChris Mason if (!pinned_copy) 7884313b399SChris Mason return -ENOMEM; 7894313b399SChris Mason 790d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 7914313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 7924313b399SChris Mason 7932c90e5d6SChris Mason trans->transaction->in_commit = 1; 794f9295749SChris Mason trans->transaction->blocked = 1; 795ccd467d6SChris Mason cur_trans = trans->transaction; 796ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 797ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 798ccd467d6SChris Mason struct btrfs_transaction, list); 799ccd467d6SChris Mason if (!prev_trans->commit_done) { 800ccd467d6SChris Mason prev_trans->use_count++; 801ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 802ccd467d6SChris Mason 803ccd467d6SChris Mason wait_for_commit(root, prev_trans); 804ccd467d6SChris Mason 805ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 80615ee9bc7SJosef Bacik put_transaction(prev_trans); 807ccd467d6SChris Mason } 808ccd467d6SChris Mason } 80915ee9bc7SJosef Bacik 81015ee9bc7SJosef Bacik do { 8117ea394f1SYan Zheng int snap_pending = 0; 81215ee9bc7SJosef Bacik joined = cur_trans->num_joined; 8137ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 8147ea394f1SYan Zheng snap_pending = 1; 8157ea394f1SYan Zheng 8162c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 81715ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 81879154b1bSChris Mason TASK_UNINTERRUPTIBLE); 81915ee9bc7SJosef Bacik 82015ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 82115ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 82215ee9bc7SJosef Bacik else 82315ee9bc7SJosef Bacik timeout = 1; 82415ee9bc7SJosef Bacik 82579154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 82615ee9bc7SJosef Bacik 8277ea394f1SYan Zheng if (snap_pending) { 8287ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 8297ea394f1SYan Zheng BUG_ON(ret); 8307ea394f1SYan Zheng } 8317ea394f1SYan Zheng 83215ee9bc7SJosef Bacik schedule_timeout(timeout); 83315ee9bc7SJosef Bacik 83479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 83515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 83615ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 83715ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 83815ee9bc7SJosef Bacik 8393063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 8403063d29fSChris Mason BUG_ON(ret); 8413063d29fSChris Mason 8422c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 843dc17ff8fSChris Mason 844e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 845e02119d5SChris Mason * various roots consistent with each other. Every pointer 846e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 847e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 848e02119d5SChris Mason * the tree logging code from jumping in and changing any 849e02119d5SChris Mason * of the trees. 850e02119d5SChris Mason * 851e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 852e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 853e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 854e02119d5SChris Mason * from now until after the super is written, we avoid races 855e02119d5SChris Mason * with the tree-log code. 856e02119d5SChris Mason */ 857e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 858e02119d5SChris Mason 85954aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 86054aa1f4dSChris Mason &dirty_fs_roots); 86154aa1f4dSChris Mason BUG_ON(ret); 86254aa1f4dSChris Mason 863e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 864e02119d5SChris Mason * safe to free the root of tree log roots 865e02119d5SChris Mason */ 866e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 867e02119d5SChris Mason 86879154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 86979154b1bSChris Mason BUG_ON(ret); 87054aa1f4dSChris Mason 87178fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 872cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 87378fae27eSChris Mason root->fs_info->running_transaction = NULL; 874cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8754b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 8764b52dff6SChris Mason cur_trans->transid); 8774b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 878db94535dSChris Mason root->fs_info->tree_root->node->start); 879db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 880db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 8815f39d397SChris Mason 8820b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 8830b86a832SChris Mason chunk_root->node->start); 8840b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 8850b86a832SChris Mason btrfs_header_level(chunk_root->node)); 886e02119d5SChris Mason 887e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 888e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 889e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 890e02119d5SChris Mason } 891e02119d5SChris Mason 892a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 8934b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 894ccd467d6SChris Mason 8954313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 896ccd467d6SChris Mason 897f9295749SChris Mason trans->transaction->blocked = 0; 898e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 899f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 900e6dcd2dcSChris Mason 90178fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 90279154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 90379154b1bSChris Mason BUG_ON(ret); 90479154b1bSChris Mason write_ctree_super(trans, root); 9054313b399SChris Mason 906e02119d5SChris Mason /* 907e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 908e02119d5SChris Mason * to go about their business 909e02119d5SChris Mason */ 910e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 911e02119d5SChris Mason 9124313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 91378fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 9144313b399SChris Mason 9154313b399SChris Mason kfree(pinned_copy); 9164313b399SChris Mason 9172c90e5d6SChris Mason cur_trans->commit_done = 1; 91815ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 9192c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 92079154b1bSChris Mason put_transaction(cur_trans); 92178fae27eSChris Mason put_transaction(cur_trans); 92258176a96SJosef Bacik 923bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 924facda1e7SChris Mason if (root->fs_info->closing) 925facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 92658176a96SJosef Bacik 92778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 9282c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 92979154b1bSChris Mason 930facda1e7SChris Mason if (root->fs_info->closing) { 9310f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 932facda1e7SChris Mason } 93379154b1bSChris Mason return ret; 93479154b1bSChris Mason } 93579154b1bSChris Mason 936e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 937e9d0b13bSChris Mason { 938e9d0b13bSChris Mason struct list_head dirty_roots; 939e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 940a74a4b97SChris Mason again: 941e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 942e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 943e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 944e9d0b13bSChris Mason 945e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 946e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 947a74a4b97SChris Mason goto again; 948e9d0b13bSChris Mason } 949e9d0b13bSChris Mason return 0; 950e9d0b13bSChris Mason } 951