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 49d352ac68SChris Mason /* 50d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 51d352ac68SChris Mason */ 5280b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5379154b1bSChris Mason { 5479154b1bSChris Mason struct btrfs_transaction *cur_trans; 5579154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5679154b1bSChris Mason if (!cur_trans) { 572c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 582c90e5d6SChris Mason GFP_NOFS); 5978fae27eSChris Mason total_trans++; 6079154b1bSChris Mason BUG_ON(!cur_trans); 610f7d52f4SChris Mason root->fs_info->generation++; 62e18e4809SChris Mason root->fs_info->last_alloc = 0; 634529ba49SChris Mason root->fs_info->last_data_alloc = 0; 6415ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6515ee9bc7SJosef Bacik cur_trans->num_joined = 0; 660f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6779154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6879154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6979154b1bSChris Mason cur_trans->in_commit = 0; 70f9295749SChris Mason cur_trans->blocked = 0; 71d5719762SChris Mason cur_trans->use_count = 1; 7279154b1bSChris Mason cur_trans->commit_done = 0; 7308607c1bSChris Mason cur_trans->start_time = get_seconds(); 743063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 758fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 76d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 775f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 785f39d397SChris Mason GFP_NOFS); 7948ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8048ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8148ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8215ee9bc7SJosef Bacik } else { 8379154b1bSChris Mason cur_trans->num_writers++; 8415ee9bc7SJosef Bacik cur_trans->num_joined++; 8515ee9bc7SJosef Bacik } 8615ee9bc7SJosef Bacik 8779154b1bSChris Mason return 0; 8879154b1bSChris Mason } 8979154b1bSChris Mason 90d352ac68SChris Mason /* 91d352ac68SChris Mason * this does all the record keeping required to make sure that a 92d352ac68SChris Mason * reference counted root is properly recorded in a given transaction. 93d352ac68SChris Mason * This is required to make sure the old root from before we joined the transaction 94d352ac68SChris Mason * is deleted when the transaction commits 95d352ac68SChris Mason */ 96e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 976702ed49SChris Mason { 98f321e491SYan Zheng struct btrfs_dirty_root *dirty; 996702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 1006702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 1016702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 1026702ed49SChris Mason if (root->root_item.refs != 0) { 1036702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1046702ed49SChris Mason (unsigned long)root->root_key.objectid, 1056702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 10631153d81SYan Zheng 10731153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10831153d81SYan Zheng BUG_ON(!dirty); 10931153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 11031153d81SYan Zheng BUG_ON(!dirty->root); 11131153d81SYan Zheng dirty->latest_root = root; 11231153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 11331153d81SYan Zheng 114925baeddSChris Mason root->commit_root = btrfs_root_node(root); 11531153d81SYan Zheng 11631153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 11731153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 118bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 11931153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 1205b21f2edSZheng Yan mutex_init(&dirty->root->log_mutex); 121bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 12231153d81SYan Zheng dirty->root->node = root->commit_root; 12331153d81SYan Zheng dirty->root->commit_root = NULL; 124bcc63abbSYan 125bcc63abbSYan spin_lock(&root->list_lock); 126bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 127bcc63abbSYan spin_unlock(&root->list_lock); 128bcc63abbSYan 129bcc63abbSYan root->dirty_root = dirty; 1306702ed49SChris Mason } else { 1316702ed49SChris Mason WARN_ON(1); 1326702ed49SChris Mason } 1336702ed49SChris Mason root->last_trans = running_trans_id; 1346702ed49SChris Mason } 1356702ed49SChris Mason return 0; 1366702ed49SChris Mason } 1376702ed49SChris Mason 138d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 139d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 140d352ac68SChris Mason * transaction might not be fully on disk. 141d352ac68SChris Mason */ 14237d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 14379154b1bSChris Mason { 144f9295749SChris Mason struct btrfs_transaction *cur_trans; 14579154b1bSChris Mason 146f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 14737d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 148f9295749SChris Mason DEFINE_WAIT(wait); 149f9295749SChris Mason cur_trans->use_count++; 150f9295749SChris Mason while(1) { 151f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 152f9295749SChris Mason TASK_UNINTERRUPTIBLE); 153f9295749SChris Mason if (cur_trans->blocked) { 154f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 155f9295749SChris Mason schedule(); 156f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 157f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 158f9295749SChris Mason &wait); 159f9295749SChris Mason } else { 160f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 161f9295749SChris Mason &wait); 162f9295749SChris Mason break; 163f9295749SChris Mason } 164f9295749SChris Mason } 165f9295749SChris Mason put_transaction(cur_trans); 166f9295749SChris Mason } 16737d1aeeeSChris Mason } 16837d1aeeeSChris Mason 169e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1709ca9ee09SSage Weil int num_blocks, int wait) 17137d1aeeeSChris Mason { 17237d1aeeeSChris Mason struct btrfs_trans_handle *h = 17337d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 17437d1aeeeSChris Mason int ret; 17537d1aeeeSChris Mason 17637d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1774bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1784bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 17937d1aeeeSChris Mason wait_current_trans(root); 18079154b1bSChris Mason ret = join_transaction(root); 18179154b1bSChris Mason BUG_ON(ret); 1820f7d52f4SChris Mason 183e02119d5SChris Mason btrfs_record_root_in_trans(root); 1846702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 18579154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 18679154b1bSChris Mason h->blocks_reserved = num_blocks; 18779154b1bSChris Mason h->blocks_used = 0; 18831f3c99bSChris Mason h->block_group = NULL; 18926b8003fSChris Mason h->alloc_exclude_nr = 0; 19026b8003fSChris Mason h->alloc_exclude_start = 0; 19179154b1bSChris Mason root->fs_info->running_transaction->use_count++; 19279154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 19379154b1bSChris Mason return h; 19479154b1bSChris Mason } 19579154b1bSChris Mason 196f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 197f9295749SChris Mason int num_blocks) 198f9295749SChris Mason { 1999ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 200f9295749SChris Mason } 201f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 202f9295749SChris Mason int num_blocks) 203f9295749SChris Mason { 2049ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 205f9295749SChris Mason } 206f9295749SChris Mason 2079ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2089ca9ee09SSage Weil int num_blocks) 2099ca9ee09SSage Weil { 2109ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 2119ca9ee09SSage Weil } 2129ca9ee09SSage Weil 213d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 21489ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 21589ce8a63SChris Mason struct btrfs_transaction *commit) 21689ce8a63SChris Mason { 21789ce8a63SChris Mason DEFINE_WAIT(wait); 21889ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21989ce8a63SChris Mason while(!commit->commit_done) { 22089ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 22189ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 22289ce8a63SChris Mason if (commit->commit_done) 22389ce8a63SChris Mason break; 22489ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22589ce8a63SChris Mason schedule(); 22689ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22789ce8a63SChris Mason } 22889ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22989ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 23089ce8a63SChris Mason return 0; 23189ce8a63SChris Mason } 23289ce8a63SChris Mason 233d352ac68SChris Mason /* 234d352ac68SChris Mason * rate limit against the drop_snapshot code. This helps to slow down new operations 235d352ac68SChris Mason * if the drop_snapshot code isn't able to keep up. 236d352ac68SChris Mason */ 23737d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 238ab78c84dSChris Mason { 239ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2402dd3e67bSChris Mason int harder_count = 0; 241ab78c84dSChris Mason 2422dd3e67bSChris Mason harder: 243ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 244ab78c84dSChris Mason DEFINE_WAIT(wait); 245ab78c84dSChris Mason int thr; 246ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 247ab78c84dSChris Mason 248ab78c84dSChris Mason do { 249ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 250ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 251ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 252ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 253ab78c84dSChris Mason break; 254ab78c84dSChris Mason } 255ab78c84dSChris Mason schedule(); 256ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 257ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2582dd3e67bSChris Mason harder_count++; 2592dd3e67bSChris Mason 2602dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2612dd3e67bSChris Mason harder_count < 2) 2622dd3e67bSChris Mason goto harder; 2632dd3e67bSChris Mason 2642dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2652dd3e67bSChris Mason harder_count < 10) 2662dd3e67bSChris Mason goto harder; 2672dd3e67bSChris Mason 2682dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2692dd3e67bSChris Mason harder_count < 20) 2702dd3e67bSChris Mason goto harder; 271ab78c84dSChris Mason } 272ab78c84dSChris Mason } 273ab78c84dSChris Mason 27437d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 27537d1aeeeSChris Mason { 27637d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2779ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 27837d1aeeeSChris Mason wait_current_trans(root); 27937d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 28037d1aeeeSChris Mason 28137d1aeeeSChris Mason throttle_on_drops(root); 28237d1aeeeSChris Mason } 28337d1aeeeSChris Mason 28489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 28589ce8a63SChris Mason struct btrfs_root *root, int throttle) 28679154b1bSChris Mason { 28779154b1bSChris Mason struct btrfs_transaction *cur_trans; 288ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 289d6e4a428SChris Mason 290ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 291ab78c84dSChris Mason cur_trans = info->running_transaction; 292ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 293d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 294ccd467d6SChris Mason cur_trans->num_writers--; 29589ce8a63SChris Mason 29679154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 29779154b1bSChris Mason wake_up(&cur_trans->writer_wait); 29879154b1bSChris Mason put_transaction(cur_trans); 299ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 300d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 3012c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 302ab78c84dSChris Mason 303ab78c84dSChris Mason if (throttle) 30437d1aeeeSChris Mason throttle_on_drops(root); 305ab78c84dSChris Mason 30679154b1bSChris Mason return 0; 30779154b1bSChris Mason } 30879154b1bSChris Mason 30989ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 31089ce8a63SChris Mason struct btrfs_root *root) 31189ce8a63SChris Mason { 31289ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 31389ce8a63SChris Mason } 31489ce8a63SChris Mason 31589ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 31689ce8a63SChris Mason struct btrfs_root *root) 31789ce8a63SChris Mason { 31889ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 31989ce8a63SChris Mason } 32089ce8a63SChris Mason 321d352ac68SChris Mason /* 322d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 323d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 324d352ac68SChris Mason * those extents are on disk for transaction or log commit 325d352ac68SChris Mason */ 326d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 327d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 32879154b1bSChris Mason { 3297c4452b9SChris Mason int ret; 330777e6bd7SChris Mason int err = 0; 3317c4452b9SChris Mason int werr = 0; 3327c4452b9SChris Mason struct page *page; 3337c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 334777e6bd7SChris Mason u64 start = 0; 3355f39d397SChris Mason u64 end; 3365f39d397SChris Mason unsigned long index; 3377c4452b9SChris Mason 3387c4452b9SChris Mason while(1) { 339777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3405f39d397SChris Mason EXTENT_DIRTY); 3415f39d397SChris Mason if (ret) 3427c4452b9SChris Mason break; 3435f39d397SChris Mason while(start <= end) { 344777e6bd7SChris Mason cond_resched(); 345777e6bd7SChris Mason 3465f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 34735ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3484bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3497c4452b9SChris Mason if (!page) 3507c4452b9SChris Mason continue; 3514bef0848SChris Mason 3524bef0848SChris Mason btree_lock_page_hook(page); 3534bef0848SChris Mason if (!page->mapping) { 3544bef0848SChris Mason unlock_page(page); 3554bef0848SChris Mason page_cache_release(page); 3564bef0848SChris Mason continue; 3574bef0848SChris Mason } 3584bef0848SChris Mason 3596702ed49SChris Mason if (PageWriteback(page)) { 3606702ed49SChris Mason if (PageDirty(page)) 3616702ed49SChris Mason wait_on_page_writeback(page); 3626702ed49SChris Mason else { 3636702ed49SChris Mason unlock_page(page); 3646702ed49SChris Mason page_cache_release(page); 3656702ed49SChris Mason continue; 3666702ed49SChris Mason } 3676702ed49SChris Mason } 3687c4452b9SChris Mason err = write_one_page(page, 0); 3697c4452b9SChris Mason if (err) 3707c4452b9SChris Mason werr = err; 3717c4452b9SChris Mason page_cache_release(page); 3727c4452b9SChris Mason } 3737c4452b9SChris Mason } 374777e6bd7SChris Mason while(1) { 375777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 376777e6bd7SChris Mason EXTENT_DIRTY); 377777e6bd7SChris Mason if (ret) 378777e6bd7SChris Mason break; 379777e6bd7SChris Mason 380777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 381777e6bd7SChris Mason while(start <= end) { 382777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 383777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 384777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 385777e6bd7SChris Mason if (!page) 386777e6bd7SChris Mason continue; 387777e6bd7SChris Mason if (PageDirty(page)) { 3884bef0848SChris Mason btree_lock_page_hook(page); 3894bef0848SChris Mason wait_on_page_writeback(page); 390777e6bd7SChris Mason err = write_one_page(page, 0); 391777e6bd7SChris Mason if (err) 392777e6bd7SChris Mason werr = err; 393777e6bd7SChris Mason } 394777e6bd7SChris Mason wait_on_page_writeback(page); 395777e6bd7SChris Mason page_cache_release(page); 396777e6bd7SChris Mason cond_resched(); 397777e6bd7SChris Mason } 398777e6bd7SChris Mason } 3997c4452b9SChris Mason if (err) 4007c4452b9SChris Mason werr = err; 4017c4452b9SChris Mason return werr; 40279154b1bSChris Mason } 40379154b1bSChris Mason 404d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 405d0c803c4SChris Mason struct btrfs_root *root) 406d0c803c4SChris Mason { 407d0c803c4SChris Mason if (!trans || !trans->transaction) { 408d0c803c4SChris Mason struct inode *btree_inode; 409d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 410d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 411d0c803c4SChris Mason } 412d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 413d0c803c4SChris Mason &trans->transaction->dirty_pages); 414d0c803c4SChris Mason } 415d0c803c4SChris Mason 416d352ac68SChris Mason /* 417d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 418d352ac68SChris Mason * 419d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 420d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 421d352ac68SChris Mason * allocation tree. 422d352ac68SChris Mason * 423d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 424d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 425d352ac68SChris Mason */ 4260b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 42779154b1bSChris Mason struct btrfs_root *root) 42879154b1bSChris Mason { 42979154b1bSChris Mason int ret; 4300b86a832SChris Mason u64 old_root_bytenr; 4310b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 43279154b1bSChris Mason 4330b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 43479154b1bSChris Mason while(1) { 4350b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4360b86a832SChris Mason if (old_root_bytenr == root->node->start) 43779154b1bSChris Mason break; 4380b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 4390b86a832SChris Mason root->node->start); 4400b86a832SChris Mason btrfs_set_root_level(&root->root_item, 4410b86a832SChris Mason btrfs_header_level(root->node)); 44279154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4430b86a832SChris Mason &root->root_key, 4440b86a832SChris Mason &root->root_item); 44579154b1bSChris Mason BUG_ON(ret); 4460b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 4470b86a832SChris Mason } 4480b86a832SChris Mason return 0; 4490b86a832SChris Mason } 4500b86a832SChris Mason 451d352ac68SChris Mason /* 452d352ac68SChris Mason * update all the cowonly tree roots on disk 453d352ac68SChris Mason */ 4540b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4550b86a832SChris Mason struct btrfs_root *root) 4560b86a832SChris Mason { 4570b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4580b86a832SChris Mason struct list_head *next; 4590b86a832SChris Mason 4600b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 4610b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4620b86a832SChris Mason list_del_init(next); 4630b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 4640b86a832SChris Mason update_cowonly_root(trans, root); 46579154b1bSChris Mason } 46679154b1bSChris Mason return 0; 46779154b1bSChris Mason } 46879154b1bSChris Mason 469d352ac68SChris Mason /* 470d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 471d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 472d352ac68SChris Mason * be deleted 473d352ac68SChris Mason */ 474b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 4755eda7b5eSChris Mason { 476f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4775eda7b5eSChris Mason 4785eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 4795eda7b5eSChris Mason if (!dirty) 4805eda7b5eSChris Mason return -ENOMEM; 4815eda7b5eSChris Mason dirty->root = root; 4825ce14bbcSChris Mason dirty->latest_root = latest; 483b48652c1SYan Zheng 484b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 485b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 486b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 4875eda7b5eSChris Mason return 0; 4885eda7b5eSChris Mason } 4895eda7b5eSChris Mason 490d352ac68SChris Mason /* 491d352ac68SChris Mason * at transaction commit time we need to schedule the old roots for 492d352ac68SChris Mason * deletion via btrfs_drop_snapshot. This runs through all the 493d352ac68SChris Mason * reference counted roots that were modified in the current 494d352ac68SChris Mason * transaction and puts them into the drop list 495d352ac68SChris Mason */ 49680b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 49735b7e476SChris Mason struct radix_tree_root *radix, 49835b7e476SChris Mason struct list_head *list) 4990f7d52f4SChris Mason { 500f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5010f7d52f4SChris Mason struct btrfs_root *gang[8]; 5020f7d52f4SChris Mason struct btrfs_root *root; 5030f7d52f4SChris Mason int i; 5040f7d52f4SChris Mason int ret; 50554aa1f4dSChris Mason int err = 0; 5065eda7b5eSChris Mason u32 refs; 50754aa1f4dSChris Mason 5080f7d52f4SChris Mason while(1) { 5090f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 5100f7d52f4SChris Mason ARRAY_SIZE(gang), 5110f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 5120f7d52f4SChris Mason if (ret == 0) 5130f7d52f4SChris Mason break; 5140f7d52f4SChris Mason for (i = 0; i < ret; i++) { 5150f7d52f4SChris Mason root = gang[i]; 5162619ba1fSChris Mason radix_tree_tag_clear(radix, 5172619ba1fSChris Mason (unsigned long)root->root_key.objectid, 5180f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 51931153d81SYan Zheng 52031153d81SYan Zheng BUG_ON(!root->ref_tree); 521017e5369SChris Mason dirty = root->dirty_root; 52231153d81SYan Zheng 523e02119d5SChris Mason btrfs_free_log(trans, root); 524*f82d02d9SYan Zheng btrfs_free_reloc_root(trans, root); 525e02119d5SChris Mason 5260f7d52f4SChris Mason if (root->commit_root == root->node) { 527db94535dSChris Mason WARN_ON(root->node->start != 528db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 52931153d81SYan Zheng 5305f39d397SChris Mason free_extent_buffer(root->commit_root); 5310f7d52f4SChris Mason root->commit_root = NULL; 5327ea394f1SYan Zheng root->dirty_root = NULL; 53331153d81SYan Zheng 534bcc63abbSYan spin_lock(&root->list_lock); 535bcc63abbSYan list_del_init(&dirty->root->dead_list); 536bcc63abbSYan spin_unlock(&root->list_lock); 537bcc63abbSYan 53831153d81SYan Zheng kfree(dirty->root); 53931153d81SYan Zheng kfree(dirty); 54058176a96SJosef Bacik 54158176a96SJosef Bacik /* make sure to update the root on disk 54258176a96SJosef Bacik * so we get any updates to the block used 54358176a96SJosef Bacik * counts 54458176a96SJosef Bacik */ 54558176a96SJosef Bacik err = btrfs_update_root(trans, 54658176a96SJosef Bacik root->fs_info->tree_root, 54758176a96SJosef Bacik &root->root_key, 54858176a96SJosef Bacik &root->root_item); 5490f7d52f4SChris Mason continue; 5500f7d52f4SChris Mason } 5519f3a7427SChris Mason 5529f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 5539f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 5549f3a7427SChris Mason root->root_item.drop_level = 0; 5550f7d52f4SChris Mason root->commit_root = NULL; 5567ea394f1SYan Zheng root->dirty_root = NULL; 5570f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 558db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 559db94535dSChris Mason root->node->start); 560db94535dSChris Mason btrfs_set_root_level(&root->root_item, 561db94535dSChris Mason btrfs_header_level(root->node)); 5620f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 5630f7d52f4SChris Mason &root->root_key, 5640f7d52f4SChris Mason &root->root_item); 56554aa1f4dSChris Mason if (err) 56654aa1f4dSChris Mason break; 5679f3a7427SChris Mason 5689f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 5699f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 5705eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 5719f3a7427SChris Mason &dirty->root->root_key, 5729f3a7427SChris Mason &dirty->root->root_item); 5735eda7b5eSChris Mason 5745eda7b5eSChris Mason BUG_ON(err); 5759f3a7427SChris Mason if (refs == 1) { 5760f7d52f4SChris Mason list_add(&dirty->list, list); 5779f3a7427SChris Mason } else { 5789f3a7427SChris Mason WARN_ON(1); 57931153d81SYan Zheng free_extent_buffer(dirty->root->node); 5809f3a7427SChris Mason kfree(dirty->root); 5815eda7b5eSChris Mason kfree(dirty); 5820f7d52f4SChris Mason } 5830f7d52f4SChris Mason } 5849f3a7427SChris Mason } 58554aa1f4dSChris Mason return err; 5860f7d52f4SChris Mason } 5870f7d52f4SChris Mason 588d352ac68SChris Mason /* 589d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 590d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 591d352ac68SChris Mason */ 592e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 593e9d0b13bSChris Mason { 594e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 595e9d0b13bSChris Mason int ret; 596e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 597d3c2fdcfSChris Mason unsigned long nr; 598e9d0b13bSChris Mason 599a2135011SChris Mason smp_mb(); 600e9d0b13bSChris Mason if (root->defrag_running) 601e9d0b13bSChris Mason return 0; 602e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6036b80053dSChris Mason while (1) { 604e9d0b13bSChris Mason root->defrag_running = 1; 605e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 606d3c2fdcfSChris Mason nr = trans->blocks_used; 607e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 608d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 609e9d0b13bSChris Mason cond_resched(); 610e9d0b13bSChris Mason 611e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6123f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 613e9d0b13bSChris Mason break; 614e9d0b13bSChris Mason } 615e9d0b13bSChris Mason root->defrag_running = 0; 616a2135011SChris Mason smp_mb(); 617e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 618e9d0b13bSChris Mason return 0; 619e9d0b13bSChris Mason } 620e9d0b13bSChris Mason 621d352ac68SChris Mason /* 622d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 623d352ac68SChris Mason * all of them 624d352ac68SChris Mason */ 62580b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 62635b7e476SChris Mason struct list_head *list) 6270f7d52f4SChris Mason { 628f321e491SYan Zheng struct btrfs_dirty_root *dirty; 6290f7d52f4SChris Mason struct btrfs_trans_handle *trans; 630d3c2fdcfSChris Mason unsigned long nr; 631db94535dSChris Mason u64 num_bytes; 632db94535dSChris Mason u64 bytes_used; 633bcc63abbSYan u64 max_useless; 63454aa1f4dSChris Mason int ret = 0; 6359f3a7427SChris Mason int err; 6369f3a7427SChris Mason 6370f7d52f4SChris Mason while(!list_empty(list)) { 63858176a96SJosef Bacik struct btrfs_root *root; 63958176a96SJosef Bacik 640f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 6410f7d52f4SChris Mason list_del_init(&dirty->list); 6425eda7b5eSChris Mason 643db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 64458176a96SJosef Bacik root = dirty->latest_root; 645a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 64658176a96SJosef Bacik 6479f3a7427SChris Mason while(1) { 6480f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 6495b21f2edSZheng Yan mutex_lock(&root->fs_info->drop_mutex); 6509f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 6519f3a7427SChris Mason if (ret != -EAGAIN) { 6529f3a7427SChris Mason break; 6539f3a7427SChris Mason } 6545b21f2edSZheng Yan mutex_unlock(&root->fs_info->drop_mutex); 65558176a96SJosef Bacik 6569f3a7427SChris Mason err = btrfs_update_root(trans, 6579f3a7427SChris Mason tree_root, 6589f3a7427SChris Mason &dirty->root->root_key, 6599f3a7427SChris Mason &dirty->root->root_item); 6609f3a7427SChris Mason if (err) 6619f3a7427SChris Mason ret = err; 662d3c2fdcfSChris Mason nr = trans->blocks_used; 663017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6640f7d52f4SChris Mason BUG_ON(ret); 665a2135011SChris Mason 666d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6674dc11904SChris Mason cond_resched(); 6689f3a7427SChris Mason } 6699f3a7427SChris Mason BUG_ON(ret); 670a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 671017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 67258176a96SJosef Bacik 673a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 674db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 675db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 676db94535dSChris Mason if (num_bytes) { 677e02119d5SChris Mason btrfs_record_root_in_trans(root); 6785f39d397SChris Mason btrfs_set_root_used(&root->root_item, 679db94535dSChris Mason bytes_used - num_bytes); 68058176a96SJosef Bacik } 681a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 682a2135011SChris Mason 6839f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 68458176a96SJosef Bacik if (ret) { 68558176a96SJosef Bacik BUG(); 68654aa1f4dSChris Mason break; 68758176a96SJosef Bacik } 688a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 689a2135011SChris Mason 690bcc63abbSYan spin_lock(&root->list_lock); 691bcc63abbSYan list_del_init(&dirty->root->dead_list); 692bcc63abbSYan if (!list_empty(&root->dead_list)) { 693bcc63abbSYan struct btrfs_root *oldest; 694bcc63abbSYan oldest = list_entry(root->dead_list.prev, 695bcc63abbSYan struct btrfs_root, dead_list); 696bcc63abbSYan max_useless = oldest->root_key.offset - 1; 697bcc63abbSYan } else { 698bcc63abbSYan max_useless = root->root_key.offset - 1; 699bcc63abbSYan } 700bcc63abbSYan spin_unlock(&root->list_lock); 701bcc63abbSYan 702d3c2fdcfSChris Mason nr = trans->blocks_used; 7030f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7040f7d52f4SChris Mason BUG_ON(ret); 7055eda7b5eSChris Mason 706e4657689SZheng Yan ret = btrfs_remove_leaf_refs(root, max_useless, 0); 707bcc63abbSYan BUG_ON(ret); 708bcc63abbSYan 709f510cfecSChris Mason free_extent_buffer(dirty->root->node); 7105eda7b5eSChris Mason kfree(dirty->root); 7110f7d52f4SChris Mason kfree(dirty); 712d3c2fdcfSChris Mason 713d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 7144dc11904SChris Mason cond_resched(); 7150f7d52f4SChris Mason } 71654aa1f4dSChris Mason return ret; 7170f7d52f4SChris Mason } 7180f7d52f4SChris Mason 719d352ac68SChris Mason /* 720d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 721d352ac68SChris Mason * transaction commit. This does the actual creation 722d352ac68SChris Mason */ 72380b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 7243063d29fSChris Mason struct btrfs_fs_info *fs_info, 7253063d29fSChris Mason struct btrfs_pending_snapshot *pending) 7263063d29fSChris Mason { 7273063d29fSChris Mason struct btrfs_key key; 72880b6794dSChris Mason struct btrfs_root_item *new_root_item; 7293063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 7303063d29fSChris Mason struct btrfs_root *root = pending->root; 7313063d29fSChris Mason struct extent_buffer *tmp; 732925baeddSChris Mason struct extent_buffer *old; 7333063d29fSChris Mason int ret; 7343b96362cSSven Wegener int namelen; 7353063d29fSChris Mason u64 objectid; 7363063d29fSChris Mason 73780b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 73880b6794dSChris Mason if (!new_root_item) { 73980b6794dSChris Mason ret = -ENOMEM; 74080b6794dSChris Mason goto fail; 74180b6794dSChris Mason } 7423063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 7433063d29fSChris Mason if (ret) 7443063d29fSChris Mason goto fail; 7453063d29fSChris Mason 74680b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 7473063d29fSChris Mason 7483063d29fSChris Mason key.objectid = objectid; 7495b21f2edSZheng Yan key.offset = trans->transid; 7503063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 7513063d29fSChris Mason 752925baeddSChris Mason old = btrfs_lock_root_node(root); 75365b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 7543063d29fSChris Mason 755925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 756925baeddSChris Mason btrfs_tree_unlock(old); 757925baeddSChris Mason free_extent_buffer(old); 7583063d29fSChris Mason 75980b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 76080b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 7613063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 76280b6794dSChris Mason new_root_item); 763925baeddSChris Mason btrfs_tree_unlock(tmp); 7643063d29fSChris Mason free_extent_buffer(tmp); 7653063d29fSChris Mason if (ret) 7663063d29fSChris Mason goto fail; 7673063d29fSChris Mason 7683063d29fSChris Mason /* 7693063d29fSChris Mason * insert the directory item 7703063d29fSChris Mason */ 7713063d29fSChris Mason key.offset = (u64)-1; 7723b96362cSSven Wegener namelen = strlen(pending->name); 7733063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 7743b96362cSSven Wegener pending->name, namelen, 7753063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 776aec7477bSJosef Bacik &key, BTRFS_FT_DIR, 0); 7773063d29fSChris Mason 7783063d29fSChris Mason if (ret) 7793063d29fSChris Mason goto fail; 7803063d29fSChris Mason 7813063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 7823063d29fSChris Mason pending->name, strlen(pending->name), objectid, 783aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 7843b96362cSSven Wegener 7853b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 7863b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 7873b96362cSSven Wegener 7883063d29fSChris Mason fail: 78980b6794dSChris Mason kfree(new_root_item); 7903063d29fSChris Mason return ret; 7913063d29fSChris Mason } 7923063d29fSChris Mason 793d352ac68SChris Mason /* 794d352ac68SChris Mason * create all the snapshots we've scheduled for creation 795d352ac68SChris Mason */ 79680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 7973063d29fSChris Mason struct btrfs_fs_info *fs_info) 7983063d29fSChris Mason { 7993063d29fSChris Mason struct btrfs_pending_snapshot *pending; 8003063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8013063d29fSChris Mason int ret; 8023063d29fSChris Mason 8033063d29fSChris Mason while(!list_empty(head)) { 8043063d29fSChris Mason pending = list_entry(head->next, 8053063d29fSChris Mason struct btrfs_pending_snapshot, list); 8063063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 8073063d29fSChris Mason BUG_ON(ret); 8083063d29fSChris Mason list_del(&pending->list); 8093063d29fSChris Mason kfree(pending->name); 8103063d29fSChris Mason kfree(pending); 8113063d29fSChris Mason } 812dc17ff8fSChris Mason return 0; 813dc17ff8fSChris Mason } 814dc17ff8fSChris Mason 81579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 81679154b1bSChris Mason struct btrfs_root *root) 81779154b1bSChris Mason { 81815ee9bc7SJosef Bacik unsigned long joined = 0; 81915ee9bc7SJosef Bacik unsigned long timeout = 1; 82079154b1bSChris Mason struct btrfs_transaction *cur_trans; 8218fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 8220b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 8230f7d52f4SChris Mason struct list_head dirty_fs_roots; 824d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 82579154b1bSChris Mason DEFINE_WAIT(wait); 82615ee9bc7SJosef Bacik int ret; 82779154b1bSChris Mason 8280f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 82979154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 83079154b1bSChris Mason if (trans->transaction->in_commit) { 83179154b1bSChris Mason cur_trans = trans->transaction; 83279154b1bSChris Mason trans->transaction->use_count++; 833ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 83479154b1bSChris Mason btrfs_end_transaction(trans, root); 835ccd467d6SChris Mason 83679154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 83779154b1bSChris Mason BUG_ON(ret); 83815ee9bc7SJosef Bacik 83915ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 84079154b1bSChris Mason put_transaction(cur_trans); 84115ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 84215ee9bc7SJosef Bacik 84379154b1bSChris Mason return 0; 84479154b1bSChris Mason } 8454313b399SChris Mason 8464313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 8474313b399SChris Mason if (!pinned_copy) 8484313b399SChris Mason return -ENOMEM; 8494313b399SChris Mason 850d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 8514313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 8524313b399SChris Mason 8532c90e5d6SChris Mason trans->transaction->in_commit = 1; 854f9295749SChris Mason trans->transaction->blocked = 1; 855ccd467d6SChris Mason cur_trans = trans->transaction; 856ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 857ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 858ccd467d6SChris Mason struct btrfs_transaction, list); 859ccd467d6SChris Mason if (!prev_trans->commit_done) { 860ccd467d6SChris Mason prev_trans->use_count++; 861ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 862ccd467d6SChris Mason 863ccd467d6SChris Mason wait_for_commit(root, prev_trans); 864ccd467d6SChris Mason 865ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 86615ee9bc7SJosef Bacik put_transaction(prev_trans); 867ccd467d6SChris Mason } 868ccd467d6SChris Mason } 86915ee9bc7SJosef Bacik 87015ee9bc7SJosef Bacik do { 8717ea394f1SYan Zheng int snap_pending = 0; 87215ee9bc7SJosef Bacik joined = cur_trans->num_joined; 8737ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 8747ea394f1SYan Zheng snap_pending = 1; 8757ea394f1SYan Zheng 8762c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 87715ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 87879154b1bSChris Mason TASK_UNINTERRUPTIBLE); 87915ee9bc7SJosef Bacik 88015ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 88115ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 88215ee9bc7SJosef Bacik else 88315ee9bc7SJosef Bacik timeout = 1; 88415ee9bc7SJosef Bacik 88579154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 88615ee9bc7SJosef Bacik 8877ea394f1SYan Zheng if (snap_pending) { 8887ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 8897ea394f1SYan Zheng BUG_ON(ret); 8907ea394f1SYan Zheng } 8917ea394f1SYan Zheng 89215ee9bc7SJosef Bacik schedule_timeout(timeout); 89315ee9bc7SJosef Bacik 89479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 89515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 89615ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 89715ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 89815ee9bc7SJosef Bacik 8993063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 9003063d29fSChris Mason BUG_ON(ret); 9013063d29fSChris Mason 9022c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 903dc17ff8fSChris Mason 904e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 905e02119d5SChris Mason * various roots consistent with each other. Every pointer 906e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 907e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 908e02119d5SChris Mason * the tree logging code from jumping in and changing any 909e02119d5SChris Mason * of the trees. 910e02119d5SChris Mason * 911e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 912e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 913e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 914e02119d5SChris Mason * from now until after the super is written, we avoid races 915e02119d5SChris Mason * with the tree-log code. 916e02119d5SChris Mason */ 917e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 9181a40e23bSZheng Yan /* 9191a40e23bSZheng Yan * keep tree reloc code from adding new reloc trees 9201a40e23bSZheng Yan */ 9211a40e23bSZheng Yan mutex_lock(&root->fs_info->tree_reloc_mutex); 9221a40e23bSZheng Yan 923e02119d5SChris Mason 92454aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 92554aa1f4dSChris Mason &dirty_fs_roots); 92654aa1f4dSChris Mason BUG_ON(ret); 92754aa1f4dSChris Mason 928e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 929e02119d5SChris Mason * safe to free the root of tree log roots 930e02119d5SChris Mason */ 931e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 932e02119d5SChris Mason 93379154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 93479154b1bSChris Mason BUG_ON(ret); 93554aa1f4dSChris Mason 93678fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 937cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 93878fae27eSChris Mason root->fs_info->running_transaction = NULL; 939cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 9404b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 9414b52dff6SChris Mason cur_trans->transid); 9424b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 943db94535dSChris Mason root->fs_info->tree_root->node->start); 944db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 945db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 9465f39d397SChris Mason 9470b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 9480b86a832SChris Mason chunk_root->node->start); 9490b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 9500b86a832SChris Mason btrfs_header_level(chunk_root->node)); 951e02119d5SChris Mason 952e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 953e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 954e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 955e02119d5SChris Mason } 956e02119d5SChris Mason 957a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 9584b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 959ccd467d6SChris Mason 9604313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 961ccd467d6SChris Mason 962f9295749SChris Mason trans->transaction->blocked = 0; 963e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 964f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 965e6dcd2dcSChris Mason 96678fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 96779154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 96879154b1bSChris Mason BUG_ON(ret); 96979154b1bSChris Mason write_ctree_super(trans, root); 9704313b399SChris Mason 971e02119d5SChris Mason /* 972e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 973e02119d5SChris Mason * to go about their business 974e02119d5SChris Mason */ 975e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 976e02119d5SChris Mason 9774313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 9784313b399SChris Mason kfree(pinned_copy); 9794313b399SChris Mason 9801a40e23bSZheng Yan btrfs_drop_dead_reloc_roots(root); 9811a40e23bSZheng Yan mutex_unlock(&root->fs_info->tree_reloc_mutex); 9821a40e23bSZheng Yan 9831a40e23bSZheng Yan mutex_lock(&root->fs_info->trans_mutex); 9841a40e23bSZheng Yan 9852c90e5d6SChris Mason cur_trans->commit_done = 1; 98615ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 9872c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 98879154b1bSChris Mason put_transaction(cur_trans); 98978fae27eSChris Mason put_transaction(cur_trans); 99058176a96SJosef Bacik 991bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 992facda1e7SChris Mason if (root->fs_info->closing) 993facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 99458176a96SJosef Bacik 99578fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 9962c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 99779154b1bSChris Mason 998facda1e7SChris Mason if (root->fs_info->closing) { 9990f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 1000facda1e7SChris Mason } 100179154b1bSChris Mason return ret; 100279154b1bSChris Mason } 100379154b1bSChris Mason 1004d352ac68SChris Mason /* 1005d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1006d352ac68SChris Mason */ 1007e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1008e9d0b13bSChris Mason { 1009e9d0b13bSChris Mason struct list_head dirty_roots; 1010e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 1011a74a4b97SChris Mason again: 1012e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1013e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 1014e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1015e9d0b13bSChris Mason 1016e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 1017e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 1018a74a4b97SChris Mason goto again; 1019e9d0b13bSChris Mason } 1020e9d0b13bSChris Mason return 0; 1021e9d0b13bSChris Mason } 1022