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> 235f2cc086SChris Mason #include <linux/blkdev.h> 2479154b1bSChris Mason #include "ctree.h" 2579154b1bSChris Mason #include "disk-io.h" 2679154b1bSChris Mason #include "transaction.h" 27925baeddSChris Mason #include "locking.h" 2831153d81SYan Zheng #include "ref-cache.h" 29e02119d5SChris Mason #include "tree-log.h" 3079154b1bSChris Mason 310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 320f7d52f4SChris Mason 3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3479154b1bSChris Mason { 352c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3679154b1bSChris Mason transaction->use_count--; 3778fae27eSChris Mason if (transaction->use_count == 0) { 388fd17795SChris Mason list_del_init(&transaction->list); 392c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 402c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4179154b1bSChris Mason } 4278fae27eSChris Mason } 4379154b1bSChris Mason 44d352ac68SChris Mason /* 45d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 46d352ac68SChris Mason */ 4780b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 4879154b1bSChris Mason { 4979154b1bSChris Mason struct btrfs_transaction *cur_trans; 5079154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5179154b1bSChris Mason if (!cur_trans) { 522c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 532c90e5d6SChris Mason GFP_NOFS); 5479154b1bSChris Mason BUG_ON(!cur_trans); 550f7d52f4SChris Mason root->fs_info->generation++; 56e18e4809SChris Mason root->fs_info->last_alloc = 0; 574529ba49SChris Mason root->fs_info->last_data_alloc = 0; 5815ee9bc7SJosef Bacik cur_trans->num_writers = 1; 5915ee9bc7SJosef Bacik cur_trans->num_joined = 0; 600f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6179154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6279154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6379154b1bSChris Mason cur_trans->in_commit = 0; 64f9295749SChris Mason cur_trans->blocked = 0; 65d5719762SChris Mason cur_trans->use_count = 1; 6679154b1bSChris Mason cur_trans->commit_done = 0; 6708607c1bSChris Mason cur_trans->start_time = get_seconds(); 683063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 698fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 70d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 715f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 725f39d397SChris Mason GFP_NOFS); 7348ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 7448ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 7548ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 7615ee9bc7SJosef Bacik } else { 7779154b1bSChris Mason cur_trans->num_writers++; 7815ee9bc7SJosef Bacik cur_trans->num_joined++; 7915ee9bc7SJosef Bacik } 8015ee9bc7SJosef Bacik 8179154b1bSChris Mason return 0; 8279154b1bSChris Mason } 8379154b1bSChris Mason 84d352ac68SChris Mason /* 85d397712bSChris Mason * this does all the record keeping required to make sure that a reference 86d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 87d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 88d397712bSChris Mason * when the transaction commits 89d352ac68SChris Mason */ 90e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 916702ed49SChris Mason { 92f321e491SYan Zheng struct btrfs_dirty_root *dirty; 936702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 946702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 956702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 966702ed49SChris Mason if (root->root_item.refs != 0) { 976702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 986702ed49SChris Mason (unsigned long)root->root_key.objectid, 996702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 10031153d81SYan Zheng 10131153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10231153d81SYan Zheng BUG_ON(!dirty); 10331153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 10431153d81SYan Zheng BUG_ON(!dirty->root); 10531153d81SYan Zheng dirty->latest_root = root; 10631153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 10731153d81SYan Zheng 108925baeddSChris Mason root->commit_root = btrfs_root_node(root); 10931153d81SYan Zheng 11031153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 11131153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 112bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 11331153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 1145b21f2edSZheng Yan mutex_init(&dirty->root->log_mutex); 115bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 11631153d81SYan Zheng dirty->root->node = root->commit_root; 11731153d81SYan Zheng dirty->root->commit_root = NULL; 118bcc63abbSYan 119bcc63abbSYan spin_lock(&root->list_lock); 120bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 121bcc63abbSYan spin_unlock(&root->list_lock); 122bcc63abbSYan 123bcc63abbSYan root->dirty_root = dirty; 1246702ed49SChris Mason } else { 1256702ed49SChris Mason WARN_ON(1); 1266702ed49SChris Mason } 1276702ed49SChris Mason root->last_trans = running_trans_id; 1286702ed49SChris Mason } 1296702ed49SChris Mason return 0; 1306702ed49SChris Mason } 1316702ed49SChris Mason 132d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 133d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 134d352ac68SChris Mason * transaction might not be fully on disk. 135d352ac68SChris Mason */ 13637d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 13779154b1bSChris Mason { 138f9295749SChris Mason struct btrfs_transaction *cur_trans; 13979154b1bSChris Mason 140f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 14137d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 142f9295749SChris Mason DEFINE_WAIT(wait); 143f9295749SChris Mason cur_trans->use_count++; 144f9295749SChris Mason while (1) { 145f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 146f9295749SChris Mason TASK_UNINTERRUPTIBLE); 147f9295749SChris Mason if (cur_trans->blocked) { 148f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 149f9295749SChris Mason schedule(); 150f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 151f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 152f9295749SChris Mason &wait); 153f9295749SChris Mason } else { 154f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 155f9295749SChris Mason &wait); 156f9295749SChris Mason break; 157f9295749SChris Mason } 158f9295749SChris Mason } 159f9295749SChris Mason put_transaction(cur_trans); 160f9295749SChris Mason } 16137d1aeeeSChris Mason } 16237d1aeeeSChris Mason 163e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1649ca9ee09SSage Weil int num_blocks, int wait) 16537d1aeeeSChris Mason { 16637d1aeeeSChris Mason struct btrfs_trans_handle *h = 16737d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 16837d1aeeeSChris Mason int ret; 16937d1aeeeSChris Mason 17037d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1714bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1724bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 17337d1aeeeSChris Mason wait_current_trans(root); 17479154b1bSChris Mason ret = join_transaction(root); 17579154b1bSChris Mason BUG_ON(ret); 1760f7d52f4SChris Mason 177e02119d5SChris Mason btrfs_record_root_in_trans(root); 1786702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 17979154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 18079154b1bSChris Mason h->blocks_reserved = num_blocks; 18179154b1bSChris Mason h->blocks_used = 0; 182d2fb3437SYan Zheng h->block_group = 0; 18326b8003fSChris Mason h->alloc_exclude_nr = 0; 18426b8003fSChris Mason h->alloc_exclude_start = 0; 18579154b1bSChris Mason root->fs_info->running_transaction->use_count++; 18679154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 18779154b1bSChris Mason return h; 18879154b1bSChris Mason } 18979154b1bSChris Mason 190f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 191f9295749SChris Mason int num_blocks) 192f9295749SChris Mason { 1939ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 194f9295749SChris Mason } 195f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 196f9295749SChris Mason int num_blocks) 197f9295749SChris Mason { 1989ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 199f9295749SChris Mason } 200f9295749SChris Mason 2019ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2029ca9ee09SSage Weil int num_blocks) 2039ca9ee09SSage Weil { 2049ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 2059ca9ee09SSage Weil } 2069ca9ee09SSage Weil 207d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 20889ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 20989ce8a63SChris Mason struct btrfs_transaction *commit) 21089ce8a63SChris Mason { 21189ce8a63SChris Mason DEFINE_WAIT(wait); 21289ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21389ce8a63SChris Mason while (!commit->commit_done) { 21489ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 21589ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 21689ce8a63SChris Mason if (commit->commit_done) 21789ce8a63SChris Mason break; 21889ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21989ce8a63SChris Mason schedule(); 22089ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22189ce8a63SChris Mason } 22289ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22389ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 22489ce8a63SChris Mason return 0; 22589ce8a63SChris Mason } 22689ce8a63SChris Mason 227d352ac68SChris Mason /* 228d397712bSChris Mason * rate limit against the drop_snapshot code. This helps to slow down new 229d397712bSChris Mason * operations if the drop_snapshot code isn't able to keep up. 230d352ac68SChris Mason */ 23137d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 232ab78c84dSChris Mason { 233ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2342dd3e67bSChris Mason int harder_count = 0; 235ab78c84dSChris Mason 2362dd3e67bSChris Mason harder: 237ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 238ab78c84dSChris Mason DEFINE_WAIT(wait); 239ab78c84dSChris Mason int thr; 240ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 241ab78c84dSChris Mason 242ab78c84dSChris Mason do { 243ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 244ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 245ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 246ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 247ab78c84dSChris Mason break; 248ab78c84dSChris Mason } 249ab78c84dSChris Mason schedule(); 250ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 251ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2522dd3e67bSChris Mason harder_count++; 2532dd3e67bSChris Mason 2542dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2552dd3e67bSChris Mason harder_count < 2) 2562dd3e67bSChris Mason goto harder; 2572dd3e67bSChris Mason 2582dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2592dd3e67bSChris Mason harder_count < 10) 2602dd3e67bSChris Mason goto harder; 2612dd3e67bSChris Mason 2622dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2632dd3e67bSChris Mason harder_count < 20) 2642dd3e67bSChris Mason goto harder; 265ab78c84dSChris Mason } 266ab78c84dSChris Mason } 267ab78c84dSChris Mason 26837d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 26937d1aeeeSChris Mason { 27037d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2719ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 27237d1aeeeSChris Mason wait_current_trans(root); 27337d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 27437d1aeeeSChris Mason 27537d1aeeeSChris Mason throttle_on_drops(root); 27637d1aeeeSChris Mason } 27737d1aeeeSChris Mason 27889ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 27989ce8a63SChris Mason struct btrfs_root *root, int throttle) 28079154b1bSChris Mason { 28179154b1bSChris Mason struct btrfs_transaction *cur_trans; 282ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 283d6e4a428SChris Mason 284ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 285ab78c84dSChris Mason cur_trans = info->running_transaction; 286ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 287d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 288ccd467d6SChris Mason cur_trans->num_writers--; 28989ce8a63SChris Mason 29079154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 29179154b1bSChris Mason wake_up(&cur_trans->writer_wait); 29279154b1bSChris Mason put_transaction(cur_trans); 293ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 294d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 2952c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 296ab78c84dSChris Mason 297ab78c84dSChris Mason if (throttle) 29837d1aeeeSChris Mason throttle_on_drops(root); 299ab78c84dSChris Mason 30079154b1bSChris Mason return 0; 30179154b1bSChris Mason } 30279154b1bSChris Mason 30389ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 30489ce8a63SChris Mason struct btrfs_root *root) 30589ce8a63SChris Mason { 30689ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 30789ce8a63SChris Mason } 30889ce8a63SChris Mason 30989ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 31089ce8a63SChris Mason struct btrfs_root *root) 31189ce8a63SChris Mason { 31289ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 31389ce8a63SChris Mason } 31489ce8a63SChris Mason 315d352ac68SChris Mason /* 316d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 317d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 318d352ac68SChris Mason * those extents are on disk for transaction or log commit 319d352ac68SChris Mason */ 320d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 321d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 32279154b1bSChris Mason { 3237c4452b9SChris Mason int ret; 324777e6bd7SChris Mason int err = 0; 3257c4452b9SChris Mason int werr = 0; 3267c4452b9SChris Mason struct page *page; 3277c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 328777e6bd7SChris Mason u64 start = 0; 3295f39d397SChris Mason u64 end; 3305f39d397SChris Mason unsigned long index; 3317c4452b9SChris Mason 3327c4452b9SChris Mason while (1) { 333777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3345f39d397SChris Mason EXTENT_DIRTY); 3355f39d397SChris Mason if (ret) 3367c4452b9SChris Mason break; 3375f39d397SChris Mason while (start <= end) { 338777e6bd7SChris Mason cond_resched(); 339777e6bd7SChris Mason 3405f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 34135ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3424bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3437c4452b9SChris Mason if (!page) 3447c4452b9SChris Mason continue; 3454bef0848SChris Mason 3464bef0848SChris Mason btree_lock_page_hook(page); 3474bef0848SChris Mason if (!page->mapping) { 3484bef0848SChris Mason unlock_page(page); 3494bef0848SChris Mason page_cache_release(page); 3504bef0848SChris Mason continue; 3514bef0848SChris Mason } 3524bef0848SChris Mason 3536702ed49SChris Mason if (PageWriteback(page)) { 3546702ed49SChris Mason if (PageDirty(page)) 3556702ed49SChris Mason wait_on_page_writeback(page); 3566702ed49SChris Mason else { 3576702ed49SChris Mason unlock_page(page); 3586702ed49SChris Mason page_cache_release(page); 3596702ed49SChris Mason continue; 3606702ed49SChris Mason } 3616702ed49SChris Mason } 3627c4452b9SChris Mason err = write_one_page(page, 0); 3637c4452b9SChris Mason if (err) 3647c4452b9SChris Mason werr = err; 3657c4452b9SChris Mason page_cache_release(page); 3667c4452b9SChris Mason } 3677c4452b9SChris Mason } 368777e6bd7SChris Mason while (1) { 369777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 370777e6bd7SChris Mason EXTENT_DIRTY); 371777e6bd7SChris Mason if (ret) 372777e6bd7SChris Mason break; 373777e6bd7SChris Mason 374777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 375777e6bd7SChris Mason while (start <= end) { 376777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 377777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 378777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 379777e6bd7SChris Mason if (!page) 380777e6bd7SChris Mason continue; 381777e6bd7SChris Mason if (PageDirty(page)) { 3824bef0848SChris Mason btree_lock_page_hook(page); 3834bef0848SChris Mason wait_on_page_writeback(page); 384777e6bd7SChris Mason err = write_one_page(page, 0); 385777e6bd7SChris Mason if (err) 386777e6bd7SChris Mason werr = err; 387777e6bd7SChris Mason } 388777e6bd7SChris Mason wait_on_page_writeback(page); 389777e6bd7SChris Mason page_cache_release(page); 390777e6bd7SChris Mason cond_resched(); 391777e6bd7SChris Mason } 392777e6bd7SChris Mason } 3937c4452b9SChris Mason if (err) 3947c4452b9SChris Mason werr = err; 3957c4452b9SChris Mason return werr; 39679154b1bSChris Mason } 39779154b1bSChris Mason 398d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 399d0c803c4SChris Mason struct btrfs_root *root) 400d0c803c4SChris Mason { 401d0c803c4SChris Mason if (!trans || !trans->transaction) { 402d0c803c4SChris Mason struct inode *btree_inode; 403d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 404d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 405d0c803c4SChris Mason } 406d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 407d0c803c4SChris Mason &trans->transaction->dirty_pages); 408d0c803c4SChris Mason } 409d0c803c4SChris Mason 410d352ac68SChris Mason /* 411d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 412d352ac68SChris Mason * 413d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 414d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 415d352ac68SChris Mason * allocation tree. 416d352ac68SChris Mason * 417d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 418d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 419d352ac68SChris Mason */ 4200b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 42179154b1bSChris Mason struct btrfs_root *root) 42279154b1bSChris Mason { 42379154b1bSChris Mason int ret; 4240b86a832SChris Mason u64 old_root_bytenr; 4250b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 42679154b1bSChris Mason 42787ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 4280b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 42987ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 43087ef2bb4SChris Mason 43179154b1bSChris Mason while (1) { 4320b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4330b86a832SChris Mason if (old_root_bytenr == root->node->start) 43479154b1bSChris Mason break; 4350b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 4360b86a832SChris Mason root->node->start); 4370b86a832SChris Mason btrfs_set_root_level(&root->root_item, 4380b86a832SChris Mason btrfs_header_level(root->node)); 43984234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, trans->transid); 44087ef2bb4SChris Mason 44187ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 44287ef2bb4SChris Mason 44379154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4440b86a832SChris Mason &root->root_key, 4450b86a832SChris Mason &root->root_item); 44679154b1bSChris Mason BUG_ON(ret); 4470b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 44887ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 4490b86a832SChris Mason } 4500b86a832SChris Mason return 0; 4510b86a832SChris Mason } 4520b86a832SChris Mason 453d352ac68SChris Mason /* 454d352ac68SChris Mason * update all the cowonly tree roots on disk 455d352ac68SChris Mason */ 4560b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4570b86a832SChris Mason struct btrfs_root *root) 4580b86a832SChris Mason { 4590b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4600b86a832SChris Mason struct list_head *next; 46184234f3aSYan Zheng struct extent_buffer *eb; 46284234f3aSYan Zheng 46387ef2bb4SChris Mason btrfs_extent_post_op(trans, fs_info->tree_root); 46487ef2bb4SChris Mason 46584234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 46684234f3aSYan Zheng btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0); 46784234f3aSYan Zheng btrfs_tree_unlock(eb); 46884234f3aSYan Zheng free_extent_buffer(eb); 4690b86a832SChris Mason 47087ef2bb4SChris Mason btrfs_extent_post_op(trans, fs_info->tree_root); 47187ef2bb4SChris Mason 4720b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 4730b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4740b86a832SChris Mason list_del_init(next); 4750b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 47687ef2bb4SChris Mason 4770b86a832SChris Mason update_cowonly_root(trans, root); 47879154b1bSChris Mason } 47979154b1bSChris Mason return 0; 48079154b1bSChris Mason } 48179154b1bSChris Mason 482d352ac68SChris Mason /* 483d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 484d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 485d352ac68SChris Mason * be deleted 486d352ac68SChris Mason */ 487b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 4885eda7b5eSChris Mason { 489f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4905eda7b5eSChris Mason 4915eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 4925eda7b5eSChris Mason if (!dirty) 4935eda7b5eSChris Mason return -ENOMEM; 4945eda7b5eSChris Mason dirty->root = root; 4955ce14bbcSChris Mason dirty->latest_root = latest; 496b48652c1SYan Zheng 497b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 498b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 499b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 5005eda7b5eSChris Mason return 0; 5015eda7b5eSChris Mason } 5025eda7b5eSChris Mason 503d352ac68SChris Mason /* 504d352ac68SChris Mason * at transaction commit time we need to schedule the old roots for 505d352ac68SChris Mason * deletion via btrfs_drop_snapshot. This runs through all the 506d352ac68SChris Mason * reference counted roots that were modified in the current 507d352ac68SChris Mason * transaction and puts them into the drop list 508d352ac68SChris Mason */ 50980b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 51035b7e476SChris Mason struct radix_tree_root *radix, 51135b7e476SChris Mason struct list_head *list) 5120f7d52f4SChris Mason { 513f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5140f7d52f4SChris Mason struct btrfs_root *gang[8]; 5150f7d52f4SChris Mason struct btrfs_root *root; 5160f7d52f4SChris Mason int i; 5170f7d52f4SChris Mason int ret; 51854aa1f4dSChris Mason int err = 0; 5195eda7b5eSChris Mason u32 refs; 52054aa1f4dSChris Mason 5210f7d52f4SChris Mason while (1) { 5220f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 5230f7d52f4SChris Mason ARRAY_SIZE(gang), 5240f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 5250f7d52f4SChris Mason if (ret == 0) 5260f7d52f4SChris Mason break; 5270f7d52f4SChris Mason for (i = 0; i < ret; i++) { 5280f7d52f4SChris Mason root = gang[i]; 5292619ba1fSChris Mason radix_tree_tag_clear(radix, 5302619ba1fSChris Mason (unsigned long)root->root_key.objectid, 5310f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 53231153d81SYan Zheng 53331153d81SYan Zheng BUG_ON(!root->ref_tree); 534017e5369SChris Mason dirty = root->dirty_root; 53531153d81SYan Zheng 536e02119d5SChris Mason btrfs_free_log(trans, root); 537f82d02d9SYan Zheng btrfs_free_reloc_root(trans, root); 538e02119d5SChris Mason 5390f7d52f4SChris Mason if (root->commit_root == root->node) { 540db94535dSChris Mason WARN_ON(root->node->start != 541db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 54231153d81SYan Zheng 5435f39d397SChris Mason free_extent_buffer(root->commit_root); 5440f7d52f4SChris Mason root->commit_root = NULL; 5457ea394f1SYan Zheng root->dirty_root = NULL; 54631153d81SYan Zheng 547bcc63abbSYan spin_lock(&root->list_lock); 548bcc63abbSYan list_del_init(&dirty->root->dead_list); 549bcc63abbSYan spin_unlock(&root->list_lock); 550bcc63abbSYan 55131153d81SYan Zheng kfree(dirty->root); 55231153d81SYan Zheng kfree(dirty); 55358176a96SJosef Bacik 55458176a96SJosef Bacik /* make sure to update the root on disk 55558176a96SJosef Bacik * so we get any updates to the block used 55658176a96SJosef Bacik * counts 55758176a96SJosef Bacik */ 55858176a96SJosef Bacik err = btrfs_update_root(trans, 55958176a96SJosef Bacik root->fs_info->tree_root, 56058176a96SJosef Bacik &root->root_key, 56158176a96SJosef Bacik &root->root_item); 5620f7d52f4SChris Mason continue; 5630f7d52f4SChris Mason } 5649f3a7427SChris Mason 5659f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 5669f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 5679f3a7427SChris Mason root->root_item.drop_level = 0; 5680f7d52f4SChris Mason root->commit_root = NULL; 5697ea394f1SYan Zheng root->dirty_root = NULL; 5700f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 571db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 572db94535dSChris Mason root->node->start); 573db94535dSChris Mason btrfs_set_root_level(&root->root_item, 574db94535dSChris Mason btrfs_header_level(root->node)); 57584234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, 57684234f3aSYan Zheng root->root_key.offset); 57784234f3aSYan Zheng 5780f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 5790f7d52f4SChris Mason &root->root_key, 5800f7d52f4SChris Mason &root->root_item); 58154aa1f4dSChris Mason if (err) 58254aa1f4dSChris Mason break; 5839f3a7427SChris Mason 5849f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 5859f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 5865eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 5879f3a7427SChris Mason &dirty->root->root_key, 5889f3a7427SChris Mason &dirty->root->root_item); 5895eda7b5eSChris Mason 5905eda7b5eSChris Mason BUG_ON(err); 5919f3a7427SChris Mason if (refs == 1) { 5920f7d52f4SChris Mason list_add(&dirty->list, list); 5939f3a7427SChris Mason } else { 5949f3a7427SChris Mason WARN_ON(1); 59531153d81SYan Zheng free_extent_buffer(dirty->root->node); 5969f3a7427SChris Mason kfree(dirty->root); 5975eda7b5eSChris Mason kfree(dirty); 5980f7d52f4SChris Mason } 5990f7d52f4SChris Mason } 6009f3a7427SChris Mason } 60154aa1f4dSChris Mason return err; 6020f7d52f4SChris Mason } 6030f7d52f4SChris Mason 604d352ac68SChris Mason /* 605d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 606d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 607d352ac68SChris Mason */ 608e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 609e9d0b13bSChris Mason { 610e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 611e9d0b13bSChris Mason int ret; 612e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 613d3c2fdcfSChris Mason unsigned long nr; 614e9d0b13bSChris Mason 615a2135011SChris Mason smp_mb(); 616e9d0b13bSChris Mason if (root->defrag_running) 617e9d0b13bSChris Mason return 0; 618e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6196b80053dSChris Mason while (1) { 620e9d0b13bSChris Mason root->defrag_running = 1; 621e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 622d3c2fdcfSChris Mason nr = trans->blocks_used; 623e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 624d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 625e9d0b13bSChris Mason cond_resched(); 626e9d0b13bSChris Mason 627e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6283f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 629e9d0b13bSChris Mason break; 630e9d0b13bSChris Mason } 631e9d0b13bSChris Mason root->defrag_running = 0; 632a2135011SChris Mason smp_mb(); 633e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 634e9d0b13bSChris Mason return 0; 635e9d0b13bSChris Mason } 636e9d0b13bSChris Mason 637d352ac68SChris Mason /* 638d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 639d352ac68SChris Mason * all of them 640d352ac68SChris Mason */ 64180b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 64235b7e476SChris Mason struct list_head *list) 6430f7d52f4SChris Mason { 644f321e491SYan Zheng struct btrfs_dirty_root *dirty; 6450f7d52f4SChris Mason struct btrfs_trans_handle *trans; 646d3c2fdcfSChris Mason unsigned long nr; 647db94535dSChris Mason u64 num_bytes; 648db94535dSChris Mason u64 bytes_used; 649bcc63abbSYan u64 max_useless; 65054aa1f4dSChris Mason int ret = 0; 6519f3a7427SChris Mason int err; 6529f3a7427SChris Mason 6530f7d52f4SChris Mason while (!list_empty(list)) { 65458176a96SJosef Bacik struct btrfs_root *root; 65558176a96SJosef Bacik 656f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 6570f7d52f4SChris Mason list_del_init(&dirty->list); 6585eda7b5eSChris Mason 659db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 66058176a96SJosef Bacik root = dirty->latest_root; 661a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 66258176a96SJosef Bacik 6639f3a7427SChris Mason while (1) { 6640f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 6655b21f2edSZheng Yan mutex_lock(&root->fs_info->drop_mutex); 6669f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 667d397712bSChris Mason if (ret != -EAGAIN) 6689f3a7427SChris Mason break; 6695b21f2edSZheng Yan mutex_unlock(&root->fs_info->drop_mutex); 67058176a96SJosef Bacik 6719f3a7427SChris Mason err = btrfs_update_root(trans, 6729f3a7427SChris Mason tree_root, 6739f3a7427SChris Mason &dirty->root->root_key, 6749f3a7427SChris Mason &dirty->root->root_item); 6759f3a7427SChris Mason if (err) 6769f3a7427SChris Mason ret = err; 677d3c2fdcfSChris Mason nr = trans->blocks_used; 678017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6790f7d52f4SChris Mason BUG_ON(ret); 680a2135011SChris Mason 681d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6824dc11904SChris Mason cond_resched(); 6839f3a7427SChris Mason } 6849f3a7427SChris Mason BUG_ON(ret); 685a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 686017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 68758176a96SJosef Bacik 688db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 689db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 690db94535dSChris Mason if (num_bytes) { 691*24562425SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 692e02119d5SChris Mason btrfs_record_root_in_trans(root); 693*24562425SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 6945f39d397SChris Mason btrfs_set_root_used(&root->root_item, 695db94535dSChris Mason bytes_used - num_bytes); 69658176a96SJosef Bacik } 697a2135011SChris Mason 6989f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 69958176a96SJosef Bacik if (ret) { 70058176a96SJosef Bacik BUG(); 70154aa1f4dSChris Mason break; 70258176a96SJosef Bacik } 703a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 704a2135011SChris Mason 705bcc63abbSYan spin_lock(&root->list_lock); 706bcc63abbSYan list_del_init(&dirty->root->dead_list); 707bcc63abbSYan if (!list_empty(&root->dead_list)) { 708bcc63abbSYan struct btrfs_root *oldest; 709bcc63abbSYan oldest = list_entry(root->dead_list.prev, 710bcc63abbSYan struct btrfs_root, dead_list); 711bcc63abbSYan max_useless = oldest->root_key.offset - 1; 712bcc63abbSYan } else { 713bcc63abbSYan max_useless = root->root_key.offset - 1; 714bcc63abbSYan } 715bcc63abbSYan spin_unlock(&root->list_lock); 716bcc63abbSYan 717d3c2fdcfSChris Mason nr = trans->blocks_used; 7180f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7190f7d52f4SChris Mason BUG_ON(ret); 7205eda7b5eSChris Mason 721e4657689SZheng Yan ret = btrfs_remove_leaf_refs(root, max_useless, 0); 722bcc63abbSYan BUG_ON(ret); 723bcc63abbSYan 724f510cfecSChris Mason free_extent_buffer(dirty->root->node); 7255eda7b5eSChris Mason kfree(dirty->root); 7260f7d52f4SChris Mason kfree(dirty); 727d3c2fdcfSChris Mason 728d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 7294dc11904SChris Mason cond_resched(); 7300f7d52f4SChris Mason } 73154aa1f4dSChris Mason return ret; 7320f7d52f4SChris Mason } 7330f7d52f4SChris Mason 734d352ac68SChris Mason /* 735d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 736d352ac68SChris Mason * transaction commit. This does the actual creation 737d352ac68SChris Mason */ 73880b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 7393063d29fSChris Mason struct btrfs_fs_info *fs_info, 7403063d29fSChris Mason struct btrfs_pending_snapshot *pending) 7413063d29fSChris Mason { 7423063d29fSChris Mason struct btrfs_key key; 74380b6794dSChris Mason struct btrfs_root_item *new_root_item; 7443063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 7453063d29fSChris Mason struct btrfs_root *root = pending->root; 7463063d29fSChris Mason struct extent_buffer *tmp; 747925baeddSChris Mason struct extent_buffer *old; 7483063d29fSChris Mason int ret; 7493063d29fSChris Mason u64 objectid; 7503063d29fSChris Mason 75180b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 75280b6794dSChris Mason if (!new_root_item) { 75380b6794dSChris Mason ret = -ENOMEM; 75480b6794dSChris Mason goto fail; 75580b6794dSChris Mason } 7563063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 7573063d29fSChris Mason if (ret) 7583063d29fSChris Mason goto fail; 7593063d29fSChris Mason 76080ff3856SYan Zheng btrfs_record_root_in_trans(root); 76180ff3856SYan Zheng btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 76280b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 7633063d29fSChris Mason 7643063d29fSChris Mason key.objectid = objectid; 7655b21f2edSZheng Yan key.offset = trans->transid; 7663063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 7673063d29fSChris Mason 768925baeddSChris Mason old = btrfs_lock_root_node(root); 76965b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 7703063d29fSChris Mason 771925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 772925baeddSChris Mason btrfs_tree_unlock(old); 773925baeddSChris Mason free_extent_buffer(old); 7743063d29fSChris Mason 77580b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 77680b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 77784234f3aSYan Zheng btrfs_set_root_generation(new_root_item, trans->transid); 7783063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 77980b6794dSChris Mason new_root_item); 780925baeddSChris Mason btrfs_tree_unlock(tmp); 7813063d29fSChris Mason free_extent_buffer(tmp); 7823063d29fSChris Mason if (ret) 7833063d29fSChris Mason goto fail; 7843063d29fSChris Mason 7853de4586cSChris Mason key.offset = (u64)-1; 7863de4586cSChris Mason memcpy(&pending->root_key, &key, sizeof(key)); 7873de4586cSChris Mason fail: 7883de4586cSChris Mason kfree(new_root_item); 7893de4586cSChris Mason return ret; 7903de4586cSChris Mason } 7913de4586cSChris Mason 7923de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info, 7933de4586cSChris Mason struct btrfs_pending_snapshot *pending) 7943de4586cSChris Mason { 7953de4586cSChris Mason int ret; 7963de4586cSChris Mason int namelen; 7973de4586cSChris Mason u64 index = 0; 7983de4586cSChris Mason struct btrfs_trans_handle *trans; 7993de4586cSChris Mason struct inode *parent_inode; 8003de4586cSChris Mason struct inode *inode; 8010660b5afSChris Mason struct btrfs_root *parent_root; 8023de4586cSChris Mason 8033394e160SChris Mason parent_inode = pending->dentry->d_parent->d_inode; 8040660b5afSChris Mason parent_root = BTRFS_I(parent_inode)->root; 805180591bcSYan Zheng trans = btrfs_join_transaction(parent_root, 1); 8063de4586cSChris Mason 8073063d29fSChris Mason /* 8083063d29fSChris Mason * insert the directory item 8093063d29fSChris Mason */ 8103b96362cSSven Wegener namelen = strlen(pending->name); 8113de4586cSChris Mason ret = btrfs_set_inode_index(parent_inode, &index); 8120660b5afSChris Mason ret = btrfs_insert_dir_item(trans, parent_root, 8133b96362cSSven Wegener pending->name, namelen, 8143de4586cSChris Mason parent_inode->i_ino, 8153de4586cSChris Mason &pending->root_key, BTRFS_FT_DIR, index); 8163063d29fSChris Mason 8173063d29fSChris Mason if (ret) 8183063d29fSChris Mason goto fail; 8190660b5afSChris Mason 82052c26179SYan Zheng btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); 82152c26179SYan Zheng ret = btrfs_update_inode(trans, parent_root, parent_inode); 82252c26179SYan Zheng BUG_ON(ret); 82352c26179SYan Zheng 8240660b5afSChris Mason /* add the backref first */ 8250660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 8260660b5afSChris Mason pending->root_key.objectid, 8270660b5afSChris Mason BTRFS_ROOT_BACKREF_KEY, 8280660b5afSChris Mason parent_root->root_key.objectid, 8290660b5afSChris Mason parent_inode->i_ino, index, pending->name, 8300660b5afSChris Mason namelen); 8310660b5afSChris Mason 8320660b5afSChris Mason BUG_ON(ret); 8330660b5afSChris Mason 8340660b5afSChris Mason /* now add the forward ref */ 8350660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 8360660b5afSChris Mason parent_root->root_key.objectid, 8370660b5afSChris Mason BTRFS_ROOT_REF_KEY, 8380660b5afSChris Mason pending->root_key.objectid, 8390660b5afSChris Mason parent_inode->i_ino, index, pending->name, 8400660b5afSChris Mason namelen); 8410660b5afSChris Mason 8423de4586cSChris Mason inode = btrfs_lookup_dentry(parent_inode, pending->dentry); 8433de4586cSChris Mason d_instantiate(pending->dentry, inode); 8443063d29fSChris Mason fail: 8453de4586cSChris Mason btrfs_end_transaction(trans, fs_info->fs_root); 8463063d29fSChris Mason return ret; 8473063d29fSChris Mason } 8483063d29fSChris Mason 849d352ac68SChris Mason /* 850d352ac68SChris Mason * create all the snapshots we've scheduled for creation 851d352ac68SChris Mason */ 85280b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 8533063d29fSChris Mason struct btrfs_fs_info *fs_info) 8543063d29fSChris Mason { 8553063d29fSChris Mason struct btrfs_pending_snapshot *pending; 8563063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8573de4586cSChris Mason int ret; 8583de4586cSChris Mason 859c6e30871SQinghuang Feng list_for_each_entry(pending, head, list) { 8603de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 8613de4586cSChris Mason BUG_ON(ret); 8623de4586cSChris Mason } 8633de4586cSChris Mason return 0; 8643de4586cSChris Mason } 8653de4586cSChris Mason 8663de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans, 8673de4586cSChris Mason struct btrfs_fs_info *fs_info) 8683de4586cSChris Mason { 8693de4586cSChris Mason struct btrfs_pending_snapshot *pending; 8703de4586cSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8713063d29fSChris Mason int ret; 8723063d29fSChris Mason 8733063d29fSChris Mason while (!list_empty(head)) { 8743063d29fSChris Mason pending = list_entry(head->next, 8753063d29fSChris Mason struct btrfs_pending_snapshot, list); 8763de4586cSChris Mason ret = finish_pending_snapshot(fs_info, pending); 8773063d29fSChris Mason BUG_ON(ret); 8783063d29fSChris Mason list_del(&pending->list); 8793063d29fSChris Mason kfree(pending->name); 8803063d29fSChris Mason kfree(pending); 8813063d29fSChris Mason } 882dc17ff8fSChris Mason return 0; 883dc17ff8fSChris Mason } 884dc17ff8fSChris Mason 88579154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 88679154b1bSChris Mason struct btrfs_root *root) 88779154b1bSChris Mason { 88815ee9bc7SJosef Bacik unsigned long joined = 0; 88915ee9bc7SJosef Bacik unsigned long timeout = 1; 89079154b1bSChris Mason struct btrfs_transaction *cur_trans; 8918fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 8920b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 8930f7d52f4SChris Mason struct list_head dirty_fs_roots; 894d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 89579154b1bSChris Mason DEFINE_WAIT(wait); 89615ee9bc7SJosef Bacik int ret; 89779154b1bSChris Mason 8980f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 89979154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 90079154b1bSChris Mason if (trans->transaction->in_commit) { 90179154b1bSChris Mason cur_trans = trans->transaction; 90279154b1bSChris Mason trans->transaction->use_count++; 903ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 90479154b1bSChris Mason btrfs_end_transaction(trans, root); 905ccd467d6SChris Mason 90679154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 90779154b1bSChris Mason BUG_ON(ret); 90815ee9bc7SJosef Bacik 90915ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 91079154b1bSChris Mason put_transaction(cur_trans); 91115ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 91215ee9bc7SJosef Bacik 91379154b1bSChris Mason return 0; 91479154b1bSChris Mason } 9154313b399SChris Mason 9164313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 9174313b399SChris Mason if (!pinned_copy) 9184313b399SChris Mason return -ENOMEM; 9194313b399SChris Mason 920d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 9214313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 9224313b399SChris Mason 9232c90e5d6SChris Mason trans->transaction->in_commit = 1; 924f9295749SChris Mason trans->transaction->blocked = 1; 925ccd467d6SChris Mason cur_trans = trans->transaction; 926ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 927ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 928ccd467d6SChris Mason struct btrfs_transaction, list); 929ccd467d6SChris Mason if (!prev_trans->commit_done) { 930ccd467d6SChris Mason prev_trans->use_count++; 931ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 932ccd467d6SChris Mason 933ccd467d6SChris Mason wait_for_commit(root, prev_trans); 934ccd467d6SChris Mason 935ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 93615ee9bc7SJosef Bacik put_transaction(prev_trans); 937ccd467d6SChris Mason } 938ccd467d6SChris Mason } 93915ee9bc7SJosef Bacik 94015ee9bc7SJosef Bacik do { 9417ea394f1SYan Zheng int snap_pending = 0; 94215ee9bc7SJosef Bacik joined = cur_trans->num_joined; 9437ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 9447ea394f1SYan Zheng snap_pending = 1; 9457ea394f1SYan Zheng 9462c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 94715ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 94879154b1bSChris Mason TASK_UNINTERRUPTIBLE); 94915ee9bc7SJosef Bacik 95015ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 95115ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 95215ee9bc7SJosef Bacik else 95315ee9bc7SJosef Bacik timeout = 1; 95415ee9bc7SJosef Bacik 95579154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 95615ee9bc7SJosef Bacik 9577ea394f1SYan Zheng if (snap_pending) { 9587ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 9597ea394f1SYan Zheng BUG_ON(ret); 9607ea394f1SYan Zheng } 9617ea394f1SYan Zheng 96215ee9bc7SJosef Bacik schedule_timeout(timeout); 96315ee9bc7SJosef Bacik 96479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 96515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 96615ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 96715ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 96815ee9bc7SJosef Bacik 9693063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 9703063d29fSChris Mason BUG_ON(ret); 9713063d29fSChris Mason 9722c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 973dc17ff8fSChris Mason 974e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 975e02119d5SChris Mason * various roots consistent with each other. Every pointer 976e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 977e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 978e02119d5SChris Mason * the tree logging code from jumping in and changing any 979e02119d5SChris Mason * of the trees. 980e02119d5SChris Mason * 981e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 982e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 983e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 984e02119d5SChris Mason * from now until after the super is written, we avoid races 985e02119d5SChris Mason * with the tree-log code. 986e02119d5SChris Mason */ 987e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 9881a40e23bSZheng Yan /* 9891a40e23bSZheng Yan * keep tree reloc code from adding new reloc trees 9901a40e23bSZheng Yan */ 9911a40e23bSZheng Yan mutex_lock(&root->fs_info->tree_reloc_mutex); 9921a40e23bSZheng Yan 993e02119d5SChris Mason 99454aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 99554aa1f4dSChris Mason &dirty_fs_roots); 99654aa1f4dSChris Mason BUG_ON(ret); 99754aa1f4dSChris Mason 998e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 999e02119d5SChris Mason * safe to free the root of tree log roots 1000e02119d5SChris Mason */ 1001e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1002e02119d5SChris Mason 100379154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 100479154b1bSChris Mason BUG_ON(ret); 100554aa1f4dSChris Mason 100678fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 1007cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 100878fae27eSChris Mason root->fs_info->running_transaction = NULL; 1009cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 10104b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 10114b52dff6SChris Mason cur_trans->transid); 10124b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 1013db94535dSChris Mason root->fs_info->tree_root->node->start); 1014db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 1015db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 10165f39d397SChris Mason 10170b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 10180b86a832SChris Mason chunk_root->node->start); 10190b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 10200b86a832SChris Mason btrfs_header_level(chunk_root->node)); 102184234f3aSYan Zheng btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy, 102284234f3aSYan Zheng btrfs_header_generation(chunk_root->node)); 1023e02119d5SChris Mason 1024e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 1025e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 1026e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 1027e02119d5SChris Mason } 1028e02119d5SChris Mason 1029a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 10304b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 1031ccd467d6SChris Mason 10324313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 1033ccd467d6SChris Mason 1034f9295749SChris Mason trans->transaction->blocked = 0; 1035e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 1036f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1037e6dcd2dcSChris Mason 103878fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 103979154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 104079154b1bSChris Mason BUG_ON(ret); 1041a512bbf8SYan Zheng write_ctree_super(trans, root, 0); 10424313b399SChris Mason 1043e02119d5SChris Mason /* 1044e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1045e02119d5SChris Mason * to go about their business 1046e02119d5SChris Mason */ 1047e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1048e02119d5SChris Mason 10494313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 10504313b399SChris Mason kfree(pinned_copy); 10514313b399SChris Mason 10521a40e23bSZheng Yan btrfs_drop_dead_reloc_roots(root); 10531a40e23bSZheng Yan mutex_unlock(&root->fs_info->tree_reloc_mutex); 10541a40e23bSZheng Yan 10553de4586cSChris Mason /* do the directory inserts of any pending snapshot creations */ 10563de4586cSChris Mason finish_pending_snapshots(trans, root->fs_info); 10573de4586cSChris Mason 10581a40e23bSZheng Yan mutex_lock(&root->fs_info->trans_mutex); 10591a40e23bSZheng Yan 10602c90e5d6SChris Mason cur_trans->commit_done = 1; 106115ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 10622c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 10633de4586cSChris Mason 106479154b1bSChris Mason put_transaction(cur_trans); 106578fae27eSChris Mason put_transaction(cur_trans); 106658176a96SJosef Bacik 1067bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 1068facda1e7SChris Mason if (root->fs_info->closing) 1069facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 107058176a96SJosef Bacik 107178fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 10723de4586cSChris Mason 10732c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 107479154b1bSChris Mason 1075d397712bSChris Mason if (root->fs_info->closing) 10760f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 107779154b1bSChris Mason return ret; 107879154b1bSChris Mason } 107979154b1bSChris Mason 1080d352ac68SChris Mason /* 1081d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1082d352ac68SChris Mason */ 1083e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1084e9d0b13bSChris Mason { 1085e9d0b13bSChris Mason struct list_head dirty_roots; 1086e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 1087a74a4b97SChris Mason again: 1088e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1089e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 1090e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1091e9d0b13bSChris Mason 1092e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 1093e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 1094a74a4b97SChris Mason goto again; 1095e9d0b13bSChris Mason } 1096e9d0b13bSChris Mason return 0; 1097e9d0b13bSChris Mason } 1098