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 3178fae27eSChris Mason static int total_trans = 0; 322c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 332c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 342c90e5d6SChris Mason 350f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 360f7d52f4SChris Mason 3780b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3879154b1bSChris Mason { 392c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 4079154b1bSChris Mason transaction->use_count--; 4178fae27eSChris Mason if (transaction->use_count == 0) { 4278fae27eSChris Mason WARN_ON(total_trans == 0); 4378fae27eSChris Mason total_trans--; 448fd17795SChris Mason list_del_init(&transaction->list); 452c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 462c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4779154b1bSChris Mason } 4878fae27eSChris Mason } 4979154b1bSChris Mason 50d352ac68SChris Mason /* 51d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 52d352ac68SChris Mason */ 5380b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5479154b1bSChris Mason { 5579154b1bSChris Mason struct btrfs_transaction *cur_trans; 5679154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5779154b1bSChris Mason if (!cur_trans) { 582c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 592c90e5d6SChris Mason GFP_NOFS); 6078fae27eSChris Mason total_trans++; 6179154b1bSChris Mason BUG_ON(!cur_trans); 620f7d52f4SChris Mason root->fs_info->generation++; 63e18e4809SChris Mason root->fs_info->last_alloc = 0; 644529ba49SChris Mason root->fs_info->last_data_alloc = 0; 6515ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6615ee9bc7SJosef Bacik cur_trans->num_joined = 0; 670f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6879154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6979154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 7079154b1bSChris Mason cur_trans->in_commit = 0; 71f9295749SChris Mason cur_trans->blocked = 0; 72d5719762SChris Mason cur_trans->use_count = 1; 7379154b1bSChris Mason cur_trans->commit_done = 0; 7408607c1bSChris Mason cur_trans->start_time = get_seconds(); 753063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 768fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 77d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 785f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 795f39d397SChris Mason GFP_NOFS); 8048ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8148ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8248ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8315ee9bc7SJosef Bacik } else { 8479154b1bSChris Mason cur_trans->num_writers++; 8515ee9bc7SJosef Bacik cur_trans->num_joined++; 8615ee9bc7SJosef Bacik } 8715ee9bc7SJosef Bacik 8879154b1bSChris Mason return 0; 8979154b1bSChris Mason } 9079154b1bSChris Mason 91d352ac68SChris Mason /* 92d352ac68SChris Mason * this does all the record keeping required to make sure that a 93d352ac68SChris Mason * reference counted root is properly recorded in a given transaction. 94d352ac68SChris Mason * This is required to make sure the old root from before we joined the transaction 95d352ac68SChris Mason * is deleted when the transaction commits 96d352ac68SChris Mason */ 97e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 986702ed49SChris Mason { 99f321e491SYan Zheng struct btrfs_dirty_root *dirty; 1006702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 1016702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 1026702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 1036702ed49SChris Mason if (root->root_item.refs != 0) { 1046702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1056702ed49SChris Mason (unsigned long)root->root_key.objectid, 1066702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 10731153d81SYan Zheng 10831153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10931153d81SYan Zheng BUG_ON(!dirty); 11031153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 11131153d81SYan Zheng BUG_ON(!dirty->root); 11231153d81SYan Zheng dirty->latest_root = root; 11331153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 11431153d81SYan Zheng 115925baeddSChris Mason root->commit_root = btrfs_root_node(root); 11631153d81SYan Zheng 11731153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 11831153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 119bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 12031153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 1215b21f2edSZheng Yan mutex_init(&dirty->root->log_mutex); 122bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 12331153d81SYan Zheng dirty->root->node = root->commit_root; 12431153d81SYan Zheng dirty->root->commit_root = NULL; 125bcc63abbSYan 126bcc63abbSYan spin_lock(&root->list_lock); 127bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 128bcc63abbSYan spin_unlock(&root->list_lock); 129bcc63abbSYan 130bcc63abbSYan root->dirty_root = dirty; 1316702ed49SChris Mason } else { 1326702ed49SChris Mason WARN_ON(1); 1336702ed49SChris Mason } 1346702ed49SChris Mason root->last_trans = running_trans_id; 1356702ed49SChris Mason } 1366702ed49SChris Mason return 0; 1376702ed49SChris Mason } 1386702ed49SChris Mason 139d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 140d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 141d352ac68SChris Mason * transaction might not be fully on disk. 142d352ac68SChris Mason */ 14337d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 14479154b1bSChris Mason { 145f9295749SChris Mason struct btrfs_transaction *cur_trans; 14679154b1bSChris Mason 147f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 14837d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 149f9295749SChris Mason DEFINE_WAIT(wait); 150f9295749SChris Mason cur_trans->use_count++; 151f9295749SChris Mason while(1) { 152f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 153f9295749SChris Mason TASK_UNINTERRUPTIBLE); 154f9295749SChris Mason if (cur_trans->blocked) { 155f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 156f9295749SChris Mason schedule(); 157f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 158f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 159f9295749SChris Mason &wait); 160f9295749SChris Mason } else { 161f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 162f9295749SChris Mason &wait); 163f9295749SChris Mason break; 164f9295749SChris Mason } 165f9295749SChris Mason } 166f9295749SChris Mason put_transaction(cur_trans); 167f9295749SChris Mason } 16837d1aeeeSChris Mason } 16937d1aeeeSChris Mason 170e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1719ca9ee09SSage Weil int num_blocks, int wait) 17237d1aeeeSChris Mason { 17337d1aeeeSChris Mason struct btrfs_trans_handle *h = 17437d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 17537d1aeeeSChris Mason int ret; 17637d1aeeeSChris Mason 17737d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1784bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1794bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 18037d1aeeeSChris Mason wait_current_trans(root); 18179154b1bSChris Mason ret = join_transaction(root); 18279154b1bSChris Mason BUG_ON(ret); 1830f7d52f4SChris Mason 184e02119d5SChris Mason btrfs_record_root_in_trans(root); 1856702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 18679154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 18779154b1bSChris Mason h->blocks_reserved = num_blocks; 18879154b1bSChris Mason h->blocks_used = 0; 18931f3c99bSChris Mason h->block_group = NULL; 19026b8003fSChris Mason h->alloc_exclude_nr = 0; 19126b8003fSChris Mason h->alloc_exclude_start = 0; 19279154b1bSChris Mason root->fs_info->running_transaction->use_count++; 19379154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 19479154b1bSChris Mason return h; 19579154b1bSChris Mason } 19679154b1bSChris Mason 197f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 198f9295749SChris Mason int num_blocks) 199f9295749SChris Mason { 2009ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 201f9295749SChris Mason } 202f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 203f9295749SChris Mason int num_blocks) 204f9295749SChris Mason { 2059ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 206f9295749SChris Mason } 207f9295749SChris Mason 2089ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2099ca9ee09SSage Weil int num_blocks) 2109ca9ee09SSage Weil { 2119ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 2129ca9ee09SSage Weil } 2139ca9ee09SSage Weil 214d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 21589ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 21689ce8a63SChris Mason struct btrfs_transaction *commit) 21789ce8a63SChris Mason { 21889ce8a63SChris Mason DEFINE_WAIT(wait); 21989ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22089ce8a63SChris Mason while(!commit->commit_done) { 22189ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 22289ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 22389ce8a63SChris Mason if (commit->commit_done) 22489ce8a63SChris Mason break; 22589ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22689ce8a63SChris Mason schedule(); 22789ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22889ce8a63SChris Mason } 22989ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 23089ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 23189ce8a63SChris Mason return 0; 23289ce8a63SChris Mason } 23389ce8a63SChris Mason 234d352ac68SChris Mason /* 235d352ac68SChris Mason * rate limit against the drop_snapshot code. This helps to slow down new operations 236d352ac68SChris Mason * if the drop_snapshot code isn't able to keep up. 237d352ac68SChris Mason */ 23837d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 239ab78c84dSChris Mason { 240ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2412dd3e67bSChris Mason int harder_count = 0; 242ab78c84dSChris Mason 2432dd3e67bSChris Mason harder: 244ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 245ab78c84dSChris Mason DEFINE_WAIT(wait); 246ab78c84dSChris Mason int thr; 247ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 248ab78c84dSChris Mason 249ab78c84dSChris Mason do { 250ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 251ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 252ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 253ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 254ab78c84dSChris Mason break; 255ab78c84dSChris Mason } 256ab78c84dSChris Mason schedule(); 257ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 258ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2592dd3e67bSChris Mason harder_count++; 2602dd3e67bSChris Mason 2612dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2622dd3e67bSChris Mason harder_count < 2) 2632dd3e67bSChris Mason goto harder; 2642dd3e67bSChris Mason 2652dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2662dd3e67bSChris Mason harder_count < 10) 2672dd3e67bSChris Mason goto harder; 2682dd3e67bSChris Mason 2692dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2702dd3e67bSChris Mason harder_count < 20) 2712dd3e67bSChris Mason goto harder; 272ab78c84dSChris Mason } 273ab78c84dSChris Mason } 274ab78c84dSChris Mason 27537d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 27637d1aeeeSChris Mason { 27737d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2789ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 27937d1aeeeSChris Mason wait_current_trans(root); 28037d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 28137d1aeeeSChris Mason 28237d1aeeeSChris Mason throttle_on_drops(root); 28337d1aeeeSChris Mason } 28437d1aeeeSChris Mason 28589ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 28689ce8a63SChris Mason struct btrfs_root *root, int throttle) 28779154b1bSChris Mason { 28879154b1bSChris Mason struct btrfs_transaction *cur_trans; 289ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 290d6e4a428SChris Mason 291ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 292ab78c84dSChris Mason cur_trans = info->running_transaction; 293ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 294d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 295ccd467d6SChris Mason cur_trans->num_writers--; 29689ce8a63SChris Mason 29779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 29879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 29979154b1bSChris Mason put_transaction(cur_trans); 300ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 301d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 3022c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 303ab78c84dSChris Mason 304ab78c84dSChris Mason if (throttle) 30537d1aeeeSChris Mason throttle_on_drops(root); 306ab78c84dSChris Mason 30779154b1bSChris Mason return 0; 30879154b1bSChris Mason } 30979154b1bSChris Mason 31089ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 31189ce8a63SChris Mason struct btrfs_root *root) 31289ce8a63SChris Mason { 31389ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 31489ce8a63SChris Mason } 31589ce8a63SChris Mason 31689ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 31789ce8a63SChris Mason struct btrfs_root *root) 31889ce8a63SChris Mason { 31989ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 32089ce8a63SChris Mason } 32189ce8a63SChris Mason 322d352ac68SChris Mason /* 323d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 324d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 325d352ac68SChris Mason * those extents are on disk for transaction or log commit 326d352ac68SChris Mason */ 327d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 328d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 32979154b1bSChris Mason { 3307c4452b9SChris Mason int ret; 331777e6bd7SChris Mason int err = 0; 3327c4452b9SChris Mason int werr = 0; 3337c4452b9SChris Mason struct page *page; 3347c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 3355f2cc086SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 336777e6bd7SChris Mason u64 start = 0; 3375f39d397SChris Mason u64 end; 3385f39d397SChris Mason unsigned long index; 3397c4452b9SChris Mason 3407c4452b9SChris Mason while(1) { 341777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3425f39d397SChris Mason EXTENT_DIRTY); 3435f39d397SChris Mason if (ret) 3447c4452b9SChris Mason break; 3455f39d397SChris Mason while(start <= end) { 346777e6bd7SChris Mason cond_resched(); 347777e6bd7SChris Mason 3485f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 34935ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3504bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3517c4452b9SChris Mason if (!page) 3527c4452b9SChris Mason continue; 3534bef0848SChris Mason 3544bef0848SChris Mason btree_lock_page_hook(page); 3554bef0848SChris Mason if (!page->mapping) { 3564bef0848SChris Mason unlock_page(page); 3574bef0848SChris Mason page_cache_release(page); 3584bef0848SChris Mason continue; 3594bef0848SChris Mason } 3604bef0848SChris Mason 3616702ed49SChris Mason if (PageWriteback(page)) { 3626702ed49SChris Mason if (PageDirty(page)) 3636702ed49SChris Mason wait_on_page_writeback(page); 3646702ed49SChris Mason else { 3656702ed49SChris Mason unlock_page(page); 3666702ed49SChris Mason page_cache_release(page); 3676702ed49SChris Mason continue; 3686702ed49SChris Mason } 3696702ed49SChris Mason } 3707c4452b9SChris Mason err = write_one_page(page, 0); 3717c4452b9SChris Mason if (err) 3727c4452b9SChris Mason werr = err; 3737c4452b9SChris Mason page_cache_release(page); 3747c4452b9SChris Mason } 3757c4452b9SChris Mason } 3765f2cc086SChris Mason /* 3775f2cc086SChris Mason * we unplug once and then use the wait_on_extent_bit for 3785f2cc086SChris Mason * everything else 3795f2cc086SChris Mason */ 3805f2cc086SChris Mason blk_run_address_space(btree_inode->i_mapping); 381777e6bd7SChris Mason while(1) { 382777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 383777e6bd7SChris Mason EXTENT_DIRTY); 384777e6bd7SChris Mason if (ret) 385777e6bd7SChris Mason break; 386777e6bd7SChris Mason 387777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 388777e6bd7SChris Mason while(start <= end) { 389777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 390777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 391777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 392777e6bd7SChris Mason if (!page) 393777e6bd7SChris Mason continue; 394777e6bd7SChris Mason if (PageDirty(page)) { 3954bef0848SChris Mason btree_lock_page_hook(page); 3964bef0848SChris Mason wait_on_page_writeback(page); 397777e6bd7SChris Mason err = write_one_page(page, 0); 398777e6bd7SChris Mason if (err) 399777e6bd7SChris Mason werr = err; 400777e6bd7SChris Mason } 4015f2cc086SChris Mason if (PageWriteback(page)) { 4025f2cc086SChris Mason /* 4035f2cc086SChris Mason * we don't wait on the page writeback bit 4045f2cc086SChris Mason * because that triggers a lot of unplugs. 4055f2cc086SChris Mason * The extent bits are much nicer to 4065f2cc086SChris Mason * the disks, but come with a slightly 4075f2cc086SChris Mason * higher latency because we aren't forcing 4085f2cc086SChris Mason * unplugs. 4095f2cc086SChris Mason */ 4105f2cc086SChris Mason wait_on_extent_writeback(io_tree, 4115f2cc086SChris Mason page_offset(page), 4125f2cc086SChris Mason page_offset(page) + 4135f2cc086SChris Mason PAGE_CACHE_SIZE - 1); 4145f2cc086SChris Mason } 4155f2cc086SChris Mason if (PageWriteback(page)) { 4165f2cc086SChris Mason /* 4175f2cc086SChris Mason * the state bits get cleared before the 4185f2cc086SChris Mason * page bits, lets add some extra 4195f2cc086SChris Mason * paranoia here 4205f2cc086SChris Mason */ 421777e6bd7SChris Mason wait_on_page_writeback(page); 4225f2cc086SChris Mason } 423777e6bd7SChris Mason page_cache_release(page); 424777e6bd7SChris Mason cond_resched(); 425777e6bd7SChris Mason } 426777e6bd7SChris Mason } 4277c4452b9SChris Mason if (err) 4287c4452b9SChris Mason werr = err; 4297c4452b9SChris Mason return werr; 43079154b1bSChris Mason } 43179154b1bSChris Mason 432d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 433d0c803c4SChris Mason struct btrfs_root *root) 434d0c803c4SChris Mason { 435d0c803c4SChris Mason if (!trans || !trans->transaction) { 436d0c803c4SChris Mason struct inode *btree_inode; 437d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 438d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 439d0c803c4SChris Mason } 440d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 441d0c803c4SChris Mason &trans->transaction->dirty_pages); 442d0c803c4SChris Mason } 443d0c803c4SChris Mason 444d352ac68SChris Mason /* 445d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 446d352ac68SChris Mason * 447d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 448d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 449d352ac68SChris Mason * allocation tree. 450d352ac68SChris Mason * 451d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 452d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 453d352ac68SChris Mason */ 4540b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 45579154b1bSChris Mason struct btrfs_root *root) 45679154b1bSChris Mason { 45779154b1bSChris Mason int ret; 4580b86a832SChris Mason u64 old_root_bytenr; 4590b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 46079154b1bSChris Mason 46187ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 4620b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 46387ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 46487ef2bb4SChris Mason 46579154b1bSChris Mason while(1) { 4660b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4670b86a832SChris Mason if (old_root_bytenr == root->node->start) 46879154b1bSChris Mason break; 4690b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 4700b86a832SChris Mason root->node->start); 4710b86a832SChris Mason btrfs_set_root_level(&root->root_item, 4720b86a832SChris Mason btrfs_header_level(root->node)); 47384234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, trans->transid); 47487ef2bb4SChris Mason 47587ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 47687ef2bb4SChris Mason 47779154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4780b86a832SChris Mason &root->root_key, 4790b86a832SChris Mason &root->root_item); 48079154b1bSChris Mason BUG_ON(ret); 4810b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 48287ef2bb4SChris Mason btrfs_extent_post_op(trans, root); 4830b86a832SChris Mason } 4840b86a832SChris Mason return 0; 4850b86a832SChris Mason } 4860b86a832SChris Mason 487d352ac68SChris Mason /* 488d352ac68SChris Mason * update all the cowonly tree roots on disk 489d352ac68SChris Mason */ 4900b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4910b86a832SChris Mason struct btrfs_root *root) 4920b86a832SChris Mason { 4930b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4940b86a832SChris Mason struct list_head *next; 49584234f3aSYan Zheng struct extent_buffer *eb; 49684234f3aSYan Zheng 49787ef2bb4SChris Mason btrfs_extent_post_op(trans, fs_info->tree_root); 49887ef2bb4SChris Mason 49984234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 50084234f3aSYan Zheng btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0); 50184234f3aSYan Zheng btrfs_tree_unlock(eb); 50284234f3aSYan Zheng free_extent_buffer(eb); 5030b86a832SChris Mason 50487ef2bb4SChris Mason btrfs_extent_post_op(trans, fs_info->tree_root); 50587ef2bb4SChris Mason 5060b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 5070b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 5080b86a832SChris Mason list_del_init(next); 5090b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 51087ef2bb4SChris Mason 5110b86a832SChris Mason update_cowonly_root(trans, root); 51279154b1bSChris Mason } 51379154b1bSChris Mason return 0; 51479154b1bSChris Mason } 51579154b1bSChris Mason 516d352ac68SChris Mason /* 517d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 518d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 519d352ac68SChris Mason * be deleted 520d352ac68SChris Mason */ 521b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 5225eda7b5eSChris Mason { 523f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5245eda7b5eSChris Mason 5255eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 5265eda7b5eSChris Mason if (!dirty) 5275eda7b5eSChris Mason return -ENOMEM; 5285eda7b5eSChris Mason dirty->root = root; 5295ce14bbcSChris Mason dirty->latest_root = latest; 530b48652c1SYan Zheng 531b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 532b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 533b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 5345eda7b5eSChris Mason return 0; 5355eda7b5eSChris Mason } 5365eda7b5eSChris Mason 537d352ac68SChris Mason /* 538d352ac68SChris Mason * at transaction commit time we need to schedule the old roots for 539d352ac68SChris Mason * deletion via btrfs_drop_snapshot. This runs through all the 540d352ac68SChris Mason * reference counted roots that were modified in the current 541d352ac68SChris Mason * transaction and puts them into the drop list 542d352ac68SChris Mason */ 54380b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 54435b7e476SChris Mason struct radix_tree_root *radix, 54535b7e476SChris Mason struct list_head *list) 5460f7d52f4SChris Mason { 547f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5480f7d52f4SChris Mason struct btrfs_root *gang[8]; 5490f7d52f4SChris Mason struct btrfs_root *root; 5500f7d52f4SChris Mason int i; 5510f7d52f4SChris Mason int ret; 55254aa1f4dSChris Mason int err = 0; 5535eda7b5eSChris Mason u32 refs; 55454aa1f4dSChris Mason 5550f7d52f4SChris Mason while(1) { 5560f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 5570f7d52f4SChris Mason ARRAY_SIZE(gang), 5580f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 5590f7d52f4SChris Mason if (ret == 0) 5600f7d52f4SChris Mason break; 5610f7d52f4SChris Mason for (i = 0; i < ret; i++) { 5620f7d52f4SChris Mason root = gang[i]; 5632619ba1fSChris Mason radix_tree_tag_clear(radix, 5642619ba1fSChris Mason (unsigned long)root->root_key.objectid, 5650f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 56631153d81SYan Zheng 56731153d81SYan Zheng BUG_ON(!root->ref_tree); 568017e5369SChris Mason dirty = root->dirty_root; 56931153d81SYan Zheng 570e02119d5SChris Mason btrfs_free_log(trans, root); 571f82d02d9SYan Zheng btrfs_free_reloc_root(trans, root); 572e02119d5SChris Mason 5730f7d52f4SChris Mason if (root->commit_root == root->node) { 574db94535dSChris Mason WARN_ON(root->node->start != 575db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 57631153d81SYan Zheng 5775f39d397SChris Mason free_extent_buffer(root->commit_root); 5780f7d52f4SChris Mason root->commit_root = NULL; 5797ea394f1SYan Zheng root->dirty_root = NULL; 58031153d81SYan Zheng 581bcc63abbSYan spin_lock(&root->list_lock); 582bcc63abbSYan list_del_init(&dirty->root->dead_list); 583bcc63abbSYan spin_unlock(&root->list_lock); 584bcc63abbSYan 58531153d81SYan Zheng kfree(dirty->root); 58631153d81SYan Zheng kfree(dirty); 58758176a96SJosef Bacik 58858176a96SJosef Bacik /* make sure to update the root on disk 58958176a96SJosef Bacik * so we get any updates to the block used 59058176a96SJosef Bacik * counts 59158176a96SJosef Bacik */ 59258176a96SJosef Bacik err = btrfs_update_root(trans, 59358176a96SJosef Bacik root->fs_info->tree_root, 59458176a96SJosef Bacik &root->root_key, 59558176a96SJosef Bacik &root->root_item); 5960f7d52f4SChris Mason continue; 5970f7d52f4SChris Mason } 5989f3a7427SChris Mason 5999f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 6009f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 6019f3a7427SChris Mason root->root_item.drop_level = 0; 6020f7d52f4SChris Mason root->commit_root = NULL; 6037ea394f1SYan Zheng root->dirty_root = NULL; 6040f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 605db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 606db94535dSChris Mason root->node->start); 607db94535dSChris Mason btrfs_set_root_level(&root->root_item, 608db94535dSChris Mason btrfs_header_level(root->node)); 60984234f3aSYan Zheng btrfs_set_root_generation(&root->root_item, 61084234f3aSYan Zheng root->root_key.offset); 61184234f3aSYan Zheng 6120f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 6130f7d52f4SChris Mason &root->root_key, 6140f7d52f4SChris Mason &root->root_item); 61554aa1f4dSChris Mason if (err) 61654aa1f4dSChris Mason break; 6179f3a7427SChris Mason 6189f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 6199f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 6205eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 6219f3a7427SChris Mason &dirty->root->root_key, 6229f3a7427SChris Mason &dirty->root->root_item); 6235eda7b5eSChris Mason 6245eda7b5eSChris Mason BUG_ON(err); 6259f3a7427SChris Mason if (refs == 1) { 6260f7d52f4SChris Mason list_add(&dirty->list, list); 6279f3a7427SChris Mason } else { 6289f3a7427SChris Mason WARN_ON(1); 62931153d81SYan Zheng free_extent_buffer(dirty->root->node); 6309f3a7427SChris Mason kfree(dirty->root); 6315eda7b5eSChris Mason kfree(dirty); 6320f7d52f4SChris Mason } 6330f7d52f4SChris Mason } 6349f3a7427SChris Mason } 63554aa1f4dSChris Mason return err; 6360f7d52f4SChris Mason } 6370f7d52f4SChris Mason 638d352ac68SChris Mason /* 639d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 640d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 641d352ac68SChris Mason */ 642e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 643e9d0b13bSChris Mason { 644e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 645e9d0b13bSChris Mason int ret; 646e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 647d3c2fdcfSChris Mason unsigned long nr; 648e9d0b13bSChris Mason 649a2135011SChris Mason smp_mb(); 650e9d0b13bSChris Mason if (root->defrag_running) 651e9d0b13bSChris Mason return 0; 652e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6536b80053dSChris Mason while (1) { 654e9d0b13bSChris Mason root->defrag_running = 1; 655e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 656d3c2fdcfSChris Mason nr = trans->blocks_used; 657e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 658d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 659e9d0b13bSChris Mason cond_resched(); 660e9d0b13bSChris Mason 661e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 6623f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 663e9d0b13bSChris Mason break; 664e9d0b13bSChris Mason } 665e9d0b13bSChris Mason root->defrag_running = 0; 666a2135011SChris Mason smp_mb(); 667e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 668e9d0b13bSChris Mason return 0; 669e9d0b13bSChris Mason } 670e9d0b13bSChris Mason 671d352ac68SChris Mason /* 672d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 673d352ac68SChris Mason * all of them 674d352ac68SChris Mason */ 67580b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 67635b7e476SChris Mason struct list_head *list) 6770f7d52f4SChris Mason { 678f321e491SYan Zheng struct btrfs_dirty_root *dirty; 6790f7d52f4SChris Mason struct btrfs_trans_handle *trans; 680d3c2fdcfSChris Mason unsigned long nr; 681db94535dSChris Mason u64 num_bytes; 682db94535dSChris Mason u64 bytes_used; 683bcc63abbSYan u64 max_useless; 68454aa1f4dSChris Mason int ret = 0; 6859f3a7427SChris Mason int err; 6869f3a7427SChris Mason 6870f7d52f4SChris Mason while(!list_empty(list)) { 68858176a96SJosef Bacik struct btrfs_root *root; 68958176a96SJosef Bacik 690f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 6910f7d52f4SChris Mason list_del_init(&dirty->list); 6925eda7b5eSChris Mason 693db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 69458176a96SJosef Bacik root = dirty->latest_root; 695a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 69658176a96SJosef Bacik 6979f3a7427SChris Mason while(1) { 6980f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 6995b21f2edSZheng Yan mutex_lock(&root->fs_info->drop_mutex); 7009f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 7019f3a7427SChris Mason if (ret != -EAGAIN) { 7029f3a7427SChris Mason break; 7039f3a7427SChris Mason } 7045b21f2edSZheng Yan mutex_unlock(&root->fs_info->drop_mutex); 70558176a96SJosef Bacik 7069f3a7427SChris Mason err = btrfs_update_root(trans, 7079f3a7427SChris Mason tree_root, 7089f3a7427SChris Mason &dirty->root->root_key, 7099f3a7427SChris Mason &dirty->root->root_item); 7109f3a7427SChris Mason if (err) 7119f3a7427SChris Mason ret = err; 712d3c2fdcfSChris Mason nr = trans->blocks_used; 713017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7140f7d52f4SChris Mason BUG_ON(ret); 715a2135011SChris Mason 716d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 7174dc11904SChris Mason cond_resched(); 7189f3a7427SChris Mason } 7199f3a7427SChris Mason BUG_ON(ret); 720a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 721017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 72258176a96SJosef Bacik 723db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 724db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 725db94535dSChris Mason if (num_bytes) { 726e02119d5SChris Mason btrfs_record_root_in_trans(root); 7275f39d397SChris Mason btrfs_set_root_used(&root->root_item, 728db94535dSChris Mason bytes_used - num_bytes); 72958176a96SJosef Bacik } 730a2135011SChris Mason 7319f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 73258176a96SJosef Bacik if (ret) { 73358176a96SJosef Bacik BUG(); 73454aa1f4dSChris Mason break; 73558176a96SJosef Bacik } 736a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 737a2135011SChris Mason 738bcc63abbSYan spin_lock(&root->list_lock); 739bcc63abbSYan list_del_init(&dirty->root->dead_list); 740bcc63abbSYan if (!list_empty(&root->dead_list)) { 741bcc63abbSYan struct btrfs_root *oldest; 742bcc63abbSYan oldest = list_entry(root->dead_list.prev, 743bcc63abbSYan struct btrfs_root, dead_list); 744bcc63abbSYan max_useless = oldest->root_key.offset - 1; 745bcc63abbSYan } else { 746bcc63abbSYan max_useless = root->root_key.offset - 1; 747bcc63abbSYan } 748bcc63abbSYan spin_unlock(&root->list_lock); 749bcc63abbSYan 750d3c2fdcfSChris Mason nr = trans->blocks_used; 7510f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 7520f7d52f4SChris Mason BUG_ON(ret); 7535eda7b5eSChris Mason 754e4657689SZheng Yan ret = btrfs_remove_leaf_refs(root, max_useless, 0); 755bcc63abbSYan BUG_ON(ret); 756bcc63abbSYan 757f510cfecSChris Mason free_extent_buffer(dirty->root->node); 7585eda7b5eSChris Mason kfree(dirty->root); 7590f7d52f4SChris Mason kfree(dirty); 760d3c2fdcfSChris Mason 761d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 7624dc11904SChris Mason cond_resched(); 7630f7d52f4SChris Mason } 76454aa1f4dSChris Mason return ret; 7650f7d52f4SChris Mason } 7660f7d52f4SChris Mason 767d352ac68SChris Mason /* 768d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 769d352ac68SChris Mason * transaction commit. This does the actual creation 770d352ac68SChris Mason */ 77180b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 7723063d29fSChris Mason struct btrfs_fs_info *fs_info, 7733063d29fSChris Mason struct btrfs_pending_snapshot *pending) 7743063d29fSChris Mason { 7753063d29fSChris Mason struct btrfs_key key; 77680b6794dSChris Mason struct btrfs_root_item *new_root_item; 7773063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 7783063d29fSChris Mason struct btrfs_root *root = pending->root; 7793063d29fSChris Mason struct extent_buffer *tmp; 780925baeddSChris Mason struct extent_buffer *old; 7813063d29fSChris Mason int ret; 7823063d29fSChris Mason u64 objectid; 7833063d29fSChris Mason 78480b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 78580b6794dSChris Mason if (!new_root_item) { 78680b6794dSChris Mason ret = -ENOMEM; 78780b6794dSChris Mason goto fail; 78880b6794dSChris Mason } 7893063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 7903063d29fSChris Mason if (ret) 7913063d29fSChris Mason goto fail; 7923063d29fSChris Mason 79380ff3856SYan Zheng btrfs_record_root_in_trans(root); 79480ff3856SYan Zheng btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 79580b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 7963063d29fSChris Mason 7973063d29fSChris Mason key.objectid = objectid; 7985b21f2edSZheng Yan key.offset = trans->transid; 7993063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 8003063d29fSChris Mason 801925baeddSChris Mason old = btrfs_lock_root_node(root); 80265b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 8033063d29fSChris Mason 804925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 805925baeddSChris Mason btrfs_tree_unlock(old); 806925baeddSChris Mason free_extent_buffer(old); 8073063d29fSChris Mason 80880b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 80980b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 81084234f3aSYan Zheng btrfs_set_root_generation(new_root_item, trans->transid); 8113063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 81280b6794dSChris Mason new_root_item); 813925baeddSChris Mason btrfs_tree_unlock(tmp); 8143063d29fSChris Mason free_extent_buffer(tmp); 8153063d29fSChris Mason if (ret) 8163063d29fSChris Mason goto fail; 8173063d29fSChris Mason 8183de4586cSChris Mason key.offset = (u64)-1; 8193de4586cSChris Mason memcpy(&pending->root_key, &key, sizeof(key)); 8203de4586cSChris Mason fail: 8213de4586cSChris Mason kfree(new_root_item); 8223de4586cSChris Mason return ret; 8233de4586cSChris Mason } 8243de4586cSChris Mason 8253de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info, 8263de4586cSChris Mason struct btrfs_pending_snapshot *pending) 8273de4586cSChris Mason { 8283de4586cSChris Mason int ret; 8293de4586cSChris Mason int namelen; 8303de4586cSChris Mason u64 index = 0; 8313de4586cSChris Mason struct btrfs_trans_handle *trans; 8323de4586cSChris Mason struct inode *parent_inode; 8333de4586cSChris Mason struct inode *inode; 8343de4586cSChris Mason 835*3394e160SChris Mason parent_inode = pending->dentry->d_parent->d_inode; 836*3394e160SChris Mason trans = btrfs_start_transaction(BTRFS_I(parent_inode)->root, 1); 8373de4586cSChris Mason 8383063d29fSChris Mason /* 8393063d29fSChris Mason * insert the directory item 8403063d29fSChris Mason */ 8413b96362cSSven Wegener namelen = strlen(pending->name); 8423de4586cSChris Mason ret = btrfs_set_inode_index(parent_inode, &index); 8433de4586cSChris Mason ret = btrfs_insert_dir_item(trans, 8443de4586cSChris Mason BTRFS_I(parent_inode)->root, 8453b96362cSSven Wegener pending->name, namelen, 8463de4586cSChris Mason parent_inode->i_ino, 8473de4586cSChris Mason &pending->root_key, BTRFS_FT_DIR, index); 8483063d29fSChris Mason 8493063d29fSChris Mason if (ret) 8503063d29fSChris Mason goto fail; 8513de4586cSChris Mason #if 0 8523063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 8533063d29fSChris Mason pending->name, strlen(pending->name), objectid, 854aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 8553de4586cSChris Mason #endif 8563de4586cSChris Mason inode = btrfs_lookup_dentry(parent_inode, pending->dentry); 8573de4586cSChris Mason d_instantiate(pending->dentry, inode); 8583063d29fSChris Mason fail: 8593de4586cSChris Mason btrfs_end_transaction(trans, fs_info->fs_root); 8603063d29fSChris Mason return ret; 8613063d29fSChris Mason } 8623063d29fSChris Mason 863d352ac68SChris Mason /* 864d352ac68SChris Mason * create all the snapshots we've scheduled for creation 865d352ac68SChris Mason */ 86680b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 8673063d29fSChris Mason struct btrfs_fs_info *fs_info) 8683063d29fSChris Mason { 8693063d29fSChris Mason struct btrfs_pending_snapshot *pending; 8703063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8713de4586cSChris Mason struct list_head *cur; 8723de4586cSChris Mason int ret; 8733de4586cSChris Mason 8743de4586cSChris Mason list_for_each(cur, head) { 8753de4586cSChris Mason pending = list_entry(cur, struct btrfs_pending_snapshot, list); 8763de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 8773de4586cSChris Mason BUG_ON(ret); 8783de4586cSChris Mason } 8793de4586cSChris Mason return 0; 8803de4586cSChris Mason } 8813de4586cSChris Mason 8823de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans, 8833de4586cSChris Mason struct btrfs_fs_info *fs_info) 8843de4586cSChris Mason { 8853de4586cSChris Mason struct btrfs_pending_snapshot *pending; 8863de4586cSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8873063d29fSChris Mason int ret; 8883063d29fSChris Mason 8893063d29fSChris Mason while(!list_empty(head)) { 8903063d29fSChris Mason pending = list_entry(head->next, 8913063d29fSChris Mason struct btrfs_pending_snapshot, list); 8923de4586cSChris Mason ret = finish_pending_snapshot(fs_info, pending); 8933063d29fSChris Mason BUG_ON(ret); 8943063d29fSChris Mason list_del(&pending->list); 8953063d29fSChris Mason kfree(pending->name); 8963063d29fSChris Mason kfree(pending); 8973063d29fSChris Mason } 898dc17ff8fSChris Mason return 0; 899dc17ff8fSChris Mason } 900dc17ff8fSChris Mason 90179154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 90279154b1bSChris Mason struct btrfs_root *root) 90379154b1bSChris Mason { 90415ee9bc7SJosef Bacik unsigned long joined = 0; 90515ee9bc7SJosef Bacik unsigned long timeout = 1; 90679154b1bSChris Mason struct btrfs_transaction *cur_trans; 9078fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 9080b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 9090f7d52f4SChris Mason struct list_head dirty_fs_roots; 910d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 91179154b1bSChris Mason DEFINE_WAIT(wait); 91215ee9bc7SJosef Bacik int ret; 91379154b1bSChris Mason 9140f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 91579154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 91679154b1bSChris Mason if (trans->transaction->in_commit) { 91779154b1bSChris Mason cur_trans = trans->transaction; 91879154b1bSChris Mason trans->transaction->use_count++; 919ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 92079154b1bSChris Mason btrfs_end_transaction(trans, root); 921ccd467d6SChris Mason 92279154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 92379154b1bSChris Mason BUG_ON(ret); 92415ee9bc7SJosef Bacik 92515ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 92679154b1bSChris Mason put_transaction(cur_trans); 92715ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 92815ee9bc7SJosef Bacik 92979154b1bSChris Mason return 0; 93079154b1bSChris Mason } 9314313b399SChris Mason 9324313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 9334313b399SChris Mason if (!pinned_copy) 9344313b399SChris Mason return -ENOMEM; 9354313b399SChris Mason 936d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 9374313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 9384313b399SChris Mason 9392c90e5d6SChris Mason trans->transaction->in_commit = 1; 940f9295749SChris Mason trans->transaction->blocked = 1; 941ccd467d6SChris Mason cur_trans = trans->transaction; 942ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 943ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 944ccd467d6SChris Mason struct btrfs_transaction, list); 945ccd467d6SChris Mason if (!prev_trans->commit_done) { 946ccd467d6SChris Mason prev_trans->use_count++; 947ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 948ccd467d6SChris Mason 949ccd467d6SChris Mason wait_for_commit(root, prev_trans); 950ccd467d6SChris Mason 951ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 95215ee9bc7SJosef Bacik put_transaction(prev_trans); 953ccd467d6SChris Mason } 954ccd467d6SChris Mason } 95515ee9bc7SJosef Bacik 95615ee9bc7SJosef Bacik do { 9577ea394f1SYan Zheng int snap_pending = 0; 95815ee9bc7SJosef Bacik joined = cur_trans->num_joined; 9597ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 9607ea394f1SYan Zheng snap_pending = 1; 9617ea394f1SYan Zheng 9622c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 96315ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 96479154b1bSChris Mason TASK_UNINTERRUPTIBLE); 96515ee9bc7SJosef Bacik 96615ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 96715ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 96815ee9bc7SJosef Bacik else 96915ee9bc7SJosef Bacik timeout = 1; 97015ee9bc7SJosef Bacik 97179154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 97215ee9bc7SJosef Bacik 9737ea394f1SYan Zheng if (snap_pending) { 9747ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 9757ea394f1SYan Zheng BUG_ON(ret); 9767ea394f1SYan Zheng } 9777ea394f1SYan Zheng 97815ee9bc7SJosef Bacik schedule_timeout(timeout); 97915ee9bc7SJosef Bacik 98079154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 98115ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 98215ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 98315ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 98415ee9bc7SJosef Bacik 9853063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 9863063d29fSChris Mason BUG_ON(ret); 9873063d29fSChris Mason 9882c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 989dc17ff8fSChris Mason 990e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 991e02119d5SChris Mason * various roots consistent with each other. Every pointer 992e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 993e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 994e02119d5SChris Mason * the tree logging code from jumping in and changing any 995e02119d5SChris Mason * of the trees. 996e02119d5SChris Mason * 997e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 998e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 999e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1000e02119d5SChris Mason * from now until after the super is written, we avoid races 1001e02119d5SChris Mason * with the tree-log code. 1002e02119d5SChris Mason */ 1003e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 10041a40e23bSZheng Yan /* 10051a40e23bSZheng Yan * keep tree reloc code from adding new reloc trees 10061a40e23bSZheng Yan */ 10071a40e23bSZheng Yan mutex_lock(&root->fs_info->tree_reloc_mutex); 10081a40e23bSZheng Yan 1009e02119d5SChris Mason 101054aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 101154aa1f4dSChris Mason &dirty_fs_roots); 101254aa1f4dSChris Mason BUG_ON(ret); 101354aa1f4dSChris Mason 1014e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 1015e02119d5SChris Mason * safe to free the root of tree log roots 1016e02119d5SChris Mason */ 1017e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1018e02119d5SChris Mason 101979154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 102079154b1bSChris Mason BUG_ON(ret); 102154aa1f4dSChris Mason 102278fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 1023cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 102478fae27eSChris Mason root->fs_info->running_transaction = NULL; 1025cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 10264b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 10274b52dff6SChris Mason cur_trans->transid); 10284b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 1029db94535dSChris Mason root->fs_info->tree_root->node->start); 1030db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 1031db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 10325f39d397SChris Mason 10330b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 10340b86a832SChris Mason chunk_root->node->start); 10350b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 10360b86a832SChris Mason btrfs_header_level(chunk_root->node)); 103784234f3aSYan Zheng btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy, 103884234f3aSYan Zheng btrfs_header_generation(chunk_root->node)); 1039e02119d5SChris Mason 1040e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 1041e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 1042e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 1043e02119d5SChris Mason } 1044e02119d5SChris Mason 1045a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 10464b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 1047ccd467d6SChris Mason 10484313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 1049ccd467d6SChris Mason 1050f9295749SChris Mason trans->transaction->blocked = 0; 1051e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 1052f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1053e6dcd2dcSChris Mason 105478fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 105579154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 105679154b1bSChris Mason BUG_ON(ret); 105779154b1bSChris Mason write_ctree_super(trans, root); 10584313b399SChris Mason 1059e02119d5SChris Mason /* 1060e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1061e02119d5SChris Mason * to go about their business 1062e02119d5SChris Mason */ 1063e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1064e02119d5SChris Mason 10654313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 10664313b399SChris Mason kfree(pinned_copy); 10674313b399SChris Mason 10681a40e23bSZheng Yan btrfs_drop_dead_reloc_roots(root); 10691a40e23bSZheng Yan mutex_unlock(&root->fs_info->tree_reloc_mutex); 10701a40e23bSZheng Yan 10713de4586cSChris Mason /* do the directory inserts of any pending snapshot creations */ 10723de4586cSChris Mason finish_pending_snapshots(trans, root->fs_info); 10733de4586cSChris Mason 10741a40e23bSZheng Yan mutex_lock(&root->fs_info->trans_mutex); 10751a40e23bSZheng Yan 10762c90e5d6SChris Mason cur_trans->commit_done = 1; 107715ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 10782c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 10793de4586cSChris Mason 108079154b1bSChris Mason put_transaction(cur_trans); 108178fae27eSChris Mason put_transaction(cur_trans); 108258176a96SJosef Bacik 1083bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 1084facda1e7SChris Mason if (root->fs_info->closing) 1085facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 108658176a96SJosef Bacik 108778fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 10883de4586cSChris Mason 10892c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 109079154b1bSChris Mason 1091facda1e7SChris Mason if (root->fs_info->closing) { 10920f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 1093facda1e7SChris Mason } 109479154b1bSChris Mason return ret; 109579154b1bSChris Mason } 109679154b1bSChris Mason 1097d352ac68SChris Mason /* 1098d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1099d352ac68SChris Mason */ 1100e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1101e9d0b13bSChris Mason { 1102e9d0b13bSChris Mason struct list_head dirty_roots; 1103e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 1104a74a4b97SChris Mason again: 1105e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1106e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 1107e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1108e9d0b13bSChris Mason 1109e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 1110e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 1111a74a4b97SChris Mason goto again; 1112e9d0b13bSChris Mason } 1113e9d0b13bSChris Mason return 0; 1114e9d0b13bSChris Mason } 1115