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" 28e02119d5SChris Mason #include "tree-log.h" 2979154b1bSChris Mason 300f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 310f7d52f4SChris Mason 3280b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3379154b1bSChris Mason { 342c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3579154b1bSChris Mason transaction->use_count--; 3678fae27eSChris Mason if (transaction->use_count == 0) { 378fd17795SChris Mason list_del_init(&transaction->list); 382c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 392c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4079154b1bSChris Mason } 4178fae27eSChris Mason } 4279154b1bSChris Mason 43817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root) 44817d52f8SJosef Bacik { 45817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 46817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 47817d52f8SJosef Bacik } 48817d52f8SJosef Bacik 49d352ac68SChris Mason /* 50d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 51d352ac68SChris Mason */ 5280b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5379154b1bSChris Mason { 5479154b1bSChris Mason struct btrfs_transaction *cur_trans; 5579154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5679154b1bSChris Mason if (!cur_trans) { 572c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 582c90e5d6SChris Mason GFP_NOFS); 5979154b1bSChris Mason BUG_ON(!cur_trans); 600f7d52f4SChris Mason root->fs_info->generation++; 6115ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6215ee9bc7SJosef Bacik cur_trans->num_joined = 0; 630f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6479154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6579154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6679154b1bSChris Mason cur_trans->in_commit = 0; 67f9295749SChris Mason cur_trans->blocked = 0; 68d5719762SChris Mason cur_trans->use_count = 1; 6979154b1bSChris Mason cur_trans->commit_done = 0; 7008607c1bSChris Mason cur_trans->start_time = get_seconds(); 7156bec294SChris Mason 7256bec294SChris Mason cur_trans->delayed_refs.root.rb_node = NULL; 7356bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 74c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 75c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 7656bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 77c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 7856bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 7956bec294SChris Mason 803063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 818fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 82d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 835f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 845f39d397SChris Mason GFP_NOFS); 8548ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8648ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8748ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8815ee9bc7SJosef Bacik } else { 8979154b1bSChris Mason cur_trans->num_writers++; 9015ee9bc7SJosef Bacik cur_trans->num_joined++; 9115ee9bc7SJosef Bacik } 9215ee9bc7SJosef Bacik 9379154b1bSChris Mason return 0; 9479154b1bSChris Mason } 9579154b1bSChris Mason 96d352ac68SChris Mason /* 97d397712bSChris Mason * this does all the record keeping required to make sure that a reference 98d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 99d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 100d397712bSChris Mason * when the transaction commits 101d352ac68SChris Mason */ 1025d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans, 1035d4f98a2SYan Zheng struct btrfs_root *root) 1046702ed49SChris Mason { 1055d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 1066702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 1075d4f98a2SYan Zheng WARN_ON(root->root_item.refs == 0); 1085d4f98a2SYan Zheng WARN_ON(root->commit_root != root->node); 1095d4f98a2SYan Zheng 1106702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 1116702ed49SChris Mason (unsigned long)root->root_key.objectid, 1126702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 1135d4f98a2SYan Zheng root->last_trans = trans->transid; 1145d4f98a2SYan Zheng btrfs_init_reloc_root(trans, root); 1156702ed49SChris Mason } 1165d4f98a2SYan Zheng return 0; 1176702ed49SChris Mason } 1185d4f98a2SYan Zheng 1195d4f98a2SYan Zheng int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 1205d4f98a2SYan Zheng struct btrfs_root *root) 1215d4f98a2SYan Zheng { 1225d4f98a2SYan Zheng if (!root->ref_cows) 1235d4f98a2SYan Zheng return 0; 1245d4f98a2SYan Zheng 1255d4f98a2SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 1265d4f98a2SYan Zheng if (root->last_trans == trans->transid) { 1275d4f98a2SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 1285d4f98a2SYan Zheng return 0; 1295d4f98a2SYan Zheng } 1305d4f98a2SYan Zheng 1315d4f98a2SYan Zheng record_root_in_trans(trans, root); 1325d4f98a2SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 1336702ed49SChris Mason return 0; 1346702ed49SChris Mason } 1356702ed49SChris Mason 136d352ac68SChris Mason /* wait for commit against the current transaction to become unblocked 137d352ac68SChris Mason * when this is done, it is safe to start a new transaction, but the current 138d352ac68SChris Mason * transaction might not be fully on disk. 139d352ac68SChris Mason */ 14037d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 14179154b1bSChris Mason { 142f9295749SChris Mason struct btrfs_transaction *cur_trans; 14379154b1bSChris Mason 144f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 14537d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 146f9295749SChris Mason DEFINE_WAIT(wait); 147f9295749SChris Mason cur_trans->use_count++; 148f9295749SChris Mason while (1) { 149f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 150f9295749SChris Mason TASK_UNINTERRUPTIBLE); 151f9295749SChris Mason if (cur_trans->blocked) { 152f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 153f9295749SChris Mason schedule(); 154f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 155f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 156f9295749SChris Mason &wait); 157f9295749SChris Mason } else { 158f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 159f9295749SChris Mason &wait); 160f9295749SChris Mason break; 161f9295749SChris Mason } 162f9295749SChris Mason } 163f9295749SChris Mason put_transaction(cur_trans); 164f9295749SChris Mason } 16537d1aeeeSChris Mason } 16637d1aeeeSChris Mason 167e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1689ca9ee09SSage Weil int num_blocks, int wait) 16937d1aeeeSChris Mason { 17037d1aeeeSChris Mason struct btrfs_trans_handle *h = 17137d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 17237d1aeeeSChris Mason int ret; 17337d1aeeeSChris Mason 17437d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1754bef0848SChris Mason if (!root->fs_info->log_root_recovering && 1764bef0848SChris Mason ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)) 17737d1aeeeSChris Mason wait_current_trans(root); 17879154b1bSChris Mason ret = join_transaction(root); 17979154b1bSChris Mason BUG_ON(ret); 1800f7d52f4SChris Mason 1816702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 18279154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 18379154b1bSChris Mason h->blocks_reserved = num_blocks; 18479154b1bSChris Mason h->blocks_used = 0; 185d2fb3437SYan Zheng h->block_group = 0; 18626b8003fSChris Mason h->alloc_exclude_nr = 0; 18726b8003fSChris Mason h->alloc_exclude_start = 0; 18856bec294SChris Mason h->delayed_ref_updates = 0; 189b7ec40d7SChris Mason 19079154b1bSChris Mason root->fs_info->running_transaction->use_count++; 1915d4f98a2SYan Zheng record_root_in_trans(h, root); 19279154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 19379154b1bSChris Mason return h; 19479154b1bSChris Mason } 19579154b1bSChris Mason 196f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 197f9295749SChris Mason int num_blocks) 198f9295749SChris Mason { 1999ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 200f9295749SChris Mason } 201f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 202f9295749SChris Mason int num_blocks) 203f9295749SChris Mason { 2049ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 205f9295749SChris Mason } 206f9295749SChris Mason 2079ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2089ca9ee09SSage Weil int num_blocks) 2099ca9ee09SSage Weil { 2109ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 2119ca9ee09SSage Weil } 2129ca9ee09SSage Weil 213d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 21489ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 21589ce8a63SChris Mason struct btrfs_transaction *commit) 21689ce8a63SChris Mason { 21789ce8a63SChris Mason DEFINE_WAIT(wait); 21889ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21989ce8a63SChris Mason while (!commit->commit_done) { 22089ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 22189ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 22289ce8a63SChris Mason if (commit->commit_done) 22389ce8a63SChris Mason break; 22489ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22589ce8a63SChris Mason schedule(); 22689ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 22789ce8a63SChris Mason } 22889ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 22989ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 23089ce8a63SChris Mason return 0; 23189ce8a63SChris Mason } 23289ce8a63SChris Mason 2335d4f98a2SYan Zheng #if 0 234d352ac68SChris Mason /* 235d397712bSChris Mason * rate limit against the drop_snapshot code. This helps to slow down new 236d397712bSChris Mason * operations 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 } 2745d4f98a2SYan Zheng #endif 275ab78c84dSChris Mason 27637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 27737d1aeeeSChris Mason { 27837d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2799ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 28037d1aeeeSChris Mason wait_current_trans(root); 28137d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 28237d1aeeeSChris Mason } 28337d1aeeeSChris Mason 28489ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 28589ce8a63SChris Mason struct btrfs_root *root, int throttle) 28679154b1bSChris Mason { 28779154b1bSChris Mason struct btrfs_transaction *cur_trans; 288ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 289c3e69d58SChris Mason int count = 0; 290d6e4a428SChris Mason 291c3e69d58SChris Mason while (count < 4) { 292c3e69d58SChris Mason unsigned long cur = trans->delayed_ref_updates; 293c3e69d58SChris Mason trans->delayed_ref_updates = 0; 294c3e69d58SChris Mason if (cur && 295c3e69d58SChris Mason trans->transaction->delayed_refs.num_heads_ready > 64) { 296c3e69d58SChris Mason trans->delayed_ref_updates = 0; 297b7ec40d7SChris Mason 298b7ec40d7SChris Mason /* 299b7ec40d7SChris Mason * do a full flush if the transaction is trying 300b7ec40d7SChris Mason * to close 301b7ec40d7SChris Mason */ 302b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) 303b7ec40d7SChris Mason cur = 0; 304c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 305c3e69d58SChris Mason } else { 306c3e69d58SChris Mason break; 307c3e69d58SChris Mason } 308c3e69d58SChris Mason count++; 30956bec294SChris Mason } 31056bec294SChris Mason 311ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 312ab78c84dSChris Mason cur_trans = info->running_transaction; 313ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 314d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 315ccd467d6SChris Mason cur_trans->num_writers--; 31689ce8a63SChris Mason 31779154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 31879154b1bSChris Mason wake_up(&cur_trans->writer_wait); 31979154b1bSChris Mason put_transaction(cur_trans); 320ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 321d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 3222c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 323ab78c84dSChris Mason 32479154b1bSChris Mason return 0; 32579154b1bSChris Mason } 32679154b1bSChris Mason 32789ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 32889ce8a63SChris Mason struct btrfs_root *root) 32989ce8a63SChris Mason { 33089ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 33189ce8a63SChris Mason } 33289ce8a63SChris Mason 33389ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 33489ce8a63SChris Mason struct btrfs_root *root) 33589ce8a63SChris Mason { 33689ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 33789ce8a63SChris Mason } 33889ce8a63SChris Mason 339d352ac68SChris Mason /* 340d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 341d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 342d352ac68SChris Mason * those extents are on disk for transaction or log commit 343d352ac68SChris Mason */ 344d0c803c4SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 345d0c803c4SChris Mason struct extent_io_tree *dirty_pages) 34679154b1bSChris Mason { 3477c4452b9SChris Mason int ret; 348777e6bd7SChris Mason int err = 0; 3497c4452b9SChris Mason int werr = 0; 3507c4452b9SChris Mason struct page *page; 3517c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 352777e6bd7SChris Mason u64 start = 0; 3535f39d397SChris Mason u64 end; 3545f39d397SChris Mason unsigned long index; 3557c4452b9SChris Mason 3567c4452b9SChris Mason while (1) { 357777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3585f39d397SChris Mason EXTENT_DIRTY); 3595f39d397SChris Mason if (ret) 3607c4452b9SChris Mason break; 3615f39d397SChris Mason while (start <= end) { 362777e6bd7SChris Mason cond_resched(); 363777e6bd7SChris Mason 3645f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 36535ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3664bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 3677c4452b9SChris Mason if (!page) 3687c4452b9SChris Mason continue; 3694bef0848SChris Mason 3704bef0848SChris Mason btree_lock_page_hook(page); 3714bef0848SChris Mason if (!page->mapping) { 3724bef0848SChris Mason unlock_page(page); 3734bef0848SChris Mason page_cache_release(page); 3744bef0848SChris Mason continue; 3754bef0848SChris Mason } 3764bef0848SChris Mason 3776702ed49SChris Mason if (PageWriteback(page)) { 3786702ed49SChris Mason if (PageDirty(page)) 3796702ed49SChris Mason wait_on_page_writeback(page); 3806702ed49SChris Mason else { 3816702ed49SChris Mason unlock_page(page); 3826702ed49SChris Mason page_cache_release(page); 3836702ed49SChris Mason continue; 3846702ed49SChris Mason } 3856702ed49SChris Mason } 3867c4452b9SChris Mason err = write_one_page(page, 0); 3877c4452b9SChris Mason if (err) 3887c4452b9SChris Mason werr = err; 3897c4452b9SChris Mason page_cache_release(page); 3907c4452b9SChris Mason } 3917c4452b9SChris Mason } 392777e6bd7SChris Mason while (1) { 393777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 394777e6bd7SChris Mason EXTENT_DIRTY); 395777e6bd7SChris Mason if (ret) 396777e6bd7SChris Mason break; 397777e6bd7SChris Mason 398777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 399777e6bd7SChris Mason while (start <= end) { 400777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 401777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 402777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 403777e6bd7SChris Mason if (!page) 404777e6bd7SChris Mason continue; 405777e6bd7SChris Mason if (PageDirty(page)) { 4064bef0848SChris Mason btree_lock_page_hook(page); 4074bef0848SChris Mason wait_on_page_writeback(page); 408777e6bd7SChris Mason err = write_one_page(page, 0); 409777e6bd7SChris Mason if (err) 410777e6bd7SChris Mason werr = err; 411777e6bd7SChris Mason } 412777e6bd7SChris Mason wait_on_page_writeback(page); 413777e6bd7SChris Mason page_cache_release(page); 414777e6bd7SChris Mason cond_resched(); 415777e6bd7SChris Mason } 416777e6bd7SChris Mason } 4177c4452b9SChris Mason if (err) 4187c4452b9SChris Mason werr = err; 4197c4452b9SChris Mason return werr; 42079154b1bSChris Mason } 42179154b1bSChris Mason 422d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 423d0c803c4SChris Mason struct btrfs_root *root) 424d0c803c4SChris Mason { 425d0c803c4SChris Mason if (!trans || !trans->transaction) { 426d0c803c4SChris Mason struct inode *btree_inode; 427d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 428d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 429d0c803c4SChris Mason } 430d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 431d0c803c4SChris Mason &trans->transaction->dirty_pages); 432d0c803c4SChris Mason } 433d0c803c4SChris Mason 434d352ac68SChris Mason /* 435d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 436d352ac68SChris Mason * 437d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 438d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 439d352ac68SChris Mason * allocation tree. 440d352ac68SChris Mason * 441d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 442d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 443d352ac68SChris Mason */ 4440b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 44579154b1bSChris Mason struct btrfs_root *root) 44679154b1bSChris Mason { 44779154b1bSChris Mason int ret; 4480b86a832SChris Mason u64 old_root_bytenr; 4490b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 45079154b1bSChris Mason 4510b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 45256bec294SChris Mason 45379154b1bSChris Mason while (1) { 4540b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 4550b86a832SChris Mason if (old_root_bytenr == root->node->start) 45679154b1bSChris Mason break; 45787ef2bb4SChris Mason 4585d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 45979154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 4600b86a832SChris Mason &root->root_key, 4610b86a832SChris Mason &root->root_item); 46279154b1bSChris Mason BUG_ON(ret); 46356bec294SChris Mason 4644a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 46556bec294SChris Mason BUG_ON(ret); 4660b86a832SChris Mason } 467*276e680dSYan Zheng 468*276e680dSYan Zheng if (root != root->fs_info->extent_root) 469817d52f8SJosef Bacik switch_commit_root(root); 470*276e680dSYan Zheng 4710b86a832SChris Mason return 0; 4720b86a832SChris Mason } 4730b86a832SChris Mason 474d352ac68SChris Mason /* 475d352ac68SChris Mason * update all the cowonly tree roots on disk 476d352ac68SChris Mason */ 4775d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 4780b86a832SChris Mason struct btrfs_root *root) 4790b86a832SChris Mason { 4800b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4810b86a832SChris Mason struct list_head *next; 48284234f3aSYan Zheng struct extent_buffer *eb; 48356bec294SChris Mason int ret; 48484234f3aSYan Zheng 48556bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 48656bec294SChris Mason BUG_ON(ret); 48787ef2bb4SChris Mason 48884234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 4899fa8cfe7SChris Mason btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); 49084234f3aSYan Zheng btrfs_tree_unlock(eb); 49184234f3aSYan Zheng free_extent_buffer(eb); 4920b86a832SChris Mason 49356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 49456bec294SChris Mason BUG_ON(ret); 49587ef2bb4SChris Mason 4960b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 4970b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4980b86a832SChris Mason list_del_init(next); 4990b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 50087ef2bb4SChris Mason 5010b86a832SChris Mason update_cowonly_root(trans, root); 50279154b1bSChris Mason } 503*276e680dSYan Zheng 504*276e680dSYan Zheng down_write(&fs_info->extent_commit_sem); 505*276e680dSYan Zheng switch_commit_root(fs_info->extent_root); 506*276e680dSYan Zheng up_write(&fs_info->extent_commit_sem); 507*276e680dSYan Zheng 50879154b1bSChris Mason return 0; 50979154b1bSChris Mason } 51079154b1bSChris Mason 511d352ac68SChris Mason /* 512d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 513d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 514d352ac68SChris Mason * be deleted 515d352ac68SChris Mason */ 5165d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root) 5175eda7b5eSChris Mason { 518b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 5195d4f98a2SYan Zheng list_add(&root->root_list, &root->fs_info->dead_roots); 520b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 5215eda7b5eSChris Mason return 0; 5225eda7b5eSChris Mason } 5235eda7b5eSChris Mason 524d352ac68SChris Mason /* 5255d4f98a2SYan Zheng * update all the cowonly tree roots on disk 526d352ac68SChris Mason */ 5275d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 5285d4f98a2SYan Zheng struct btrfs_root *root) 5290f7d52f4SChris Mason { 5300f7d52f4SChris Mason struct btrfs_root *gang[8]; 5315d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 5320f7d52f4SChris Mason int i; 5330f7d52f4SChris Mason int ret; 53454aa1f4dSChris Mason int err = 0; 53554aa1f4dSChris Mason 5360f7d52f4SChris Mason while (1) { 5375d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 5385d4f98a2SYan Zheng (void **)gang, 0, 5390f7d52f4SChris Mason ARRAY_SIZE(gang), 5400f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 5410f7d52f4SChris Mason if (ret == 0) 5420f7d52f4SChris Mason break; 5430f7d52f4SChris Mason for (i = 0; i < ret; i++) { 5440f7d52f4SChris Mason root = gang[i]; 5455d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 5462619ba1fSChris Mason (unsigned long)root->root_key.objectid, 5470f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 54831153d81SYan Zheng 549e02119d5SChris Mason btrfs_free_log(trans, root); 5505d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 551e02119d5SChris Mason 552978d910dSYan Zheng if (root->commit_root != root->node) { 553817d52f8SJosef Bacik switch_commit_root(root); 554978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 555978d910dSYan Zheng root->node); 556978d910dSYan Zheng } 55731153d81SYan Zheng 5585d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 5590f7d52f4SChris Mason &root->root_key, 5600f7d52f4SChris Mason &root->root_item); 56154aa1f4dSChris Mason if (err) 56254aa1f4dSChris Mason break; 5630f7d52f4SChris Mason } 5649f3a7427SChris Mason } 56554aa1f4dSChris Mason return err; 5660f7d52f4SChris Mason } 5670f7d52f4SChris Mason 568d352ac68SChris Mason /* 569d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 570d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 571d352ac68SChris Mason */ 572e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 573e9d0b13bSChris Mason { 574e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 575e9d0b13bSChris Mason int ret; 576e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 577d3c2fdcfSChris Mason unsigned long nr; 578e9d0b13bSChris Mason 579a2135011SChris Mason smp_mb(); 580e9d0b13bSChris Mason if (root->defrag_running) 581e9d0b13bSChris Mason return 0; 582e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5836b80053dSChris Mason while (1) { 584e9d0b13bSChris Mason root->defrag_running = 1; 585e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 586d3c2fdcfSChris Mason nr = trans->blocks_used; 587e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 588d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 589e9d0b13bSChris Mason cond_resched(); 590e9d0b13bSChris Mason 591e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5923f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 593e9d0b13bSChris Mason break; 594e9d0b13bSChris Mason } 595e9d0b13bSChris Mason root->defrag_running = 0; 596a2135011SChris Mason smp_mb(); 597e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 598e9d0b13bSChris Mason return 0; 599e9d0b13bSChris Mason } 600e9d0b13bSChris Mason 6012c47e605SYan Zheng #if 0 602d352ac68SChris Mason /* 603b7ec40d7SChris Mason * when dropping snapshots, we generate a ton of delayed refs, and it makes 604b7ec40d7SChris Mason * sense not to join the transaction while it is trying to flush the current 605b7ec40d7SChris Mason * queue of delayed refs out. 606b7ec40d7SChris Mason * 607b7ec40d7SChris Mason * This is used by the drop snapshot code only 608b7ec40d7SChris Mason */ 609b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) 610b7ec40d7SChris Mason { 611b7ec40d7SChris Mason DEFINE_WAIT(wait); 612b7ec40d7SChris Mason 613b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 614b7ec40d7SChris Mason while (info->running_transaction && 615b7ec40d7SChris Mason info->running_transaction->delayed_refs.flushing) { 616b7ec40d7SChris Mason prepare_to_wait(&info->transaction_wait, &wait, 617b7ec40d7SChris Mason TASK_UNINTERRUPTIBLE); 618b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 61959bc5c75SChris Mason 620b7ec40d7SChris Mason schedule(); 62159bc5c75SChris Mason 622b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 623b7ec40d7SChris Mason finish_wait(&info->transaction_wait, &wait); 624b7ec40d7SChris Mason } 625b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 626b7ec40d7SChris Mason return 0; 627b7ec40d7SChris Mason } 628b7ec40d7SChris Mason 629b7ec40d7SChris Mason /* 630d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 631d352ac68SChris Mason * all of them 632d352ac68SChris Mason */ 6335d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root) 6340f7d52f4SChris Mason { 6350f7d52f4SChris Mason struct btrfs_trans_handle *trans; 6365d4f98a2SYan Zheng struct btrfs_root *tree_root = root->fs_info->tree_root; 637d3c2fdcfSChris Mason unsigned long nr; 6385d4f98a2SYan Zheng int ret; 63958176a96SJosef Bacik 6409f3a7427SChris Mason while (1) { 641b7ec40d7SChris Mason /* 642b7ec40d7SChris Mason * we don't want to jump in and create a bunch of 643b7ec40d7SChris Mason * delayed refs if the transaction is starting to close 644b7ec40d7SChris Mason */ 645b7ec40d7SChris Mason wait_transaction_pre_flush(tree_root->fs_info); 6460f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 647b7ec40d7SChris Mason 648b7ec40d7SChris Mason /* 649b7ec40d7SChris Mason * we've joined a transaction, make sure it isn't 650b7ec40d7SChris Mason * closing right now 651b7ec40d7SChris Mason */ 652b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) { 653b7ec40d7SChris Mason btrfs_end_transaction(trans, tree_root); 654b7ec40d7SChris Mason continue; 655b7ec40d7SChris Mason } 656b7ec40d7SChris Mason 6575d4f98a2SYan Zheng ret = btrfs_drop_snapshot(trans, root); 658d397712bSChris Mason if (ret != -EAGAIN) 6599f3a7427SChris Mason break; 66058176a96SJosef Bacik 6615d4f98a2SYan Zheng ret = btrfs_update_root(trans, tree_root, 6625d4f98a2SYan Zheng &root->root_key, 6635d4f98a2SYan Zheng &root->root_item); 6645d4f98a2SYan Zheng if (ret) 66554aa1f4dSChris Mason break; 666bcc63abbSYan 667d3c2fdcfSChris Mason nr = trans->blocks_used; 6680f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6690f7d52f4SChris Mason BUG_ON(ret); 6705eda7b5eSChris Mason 671d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6724dc11904SChris Mason cond_resched(); 6730f7d52f4SChris Mason } 6745d4f98a2SYan Zheng BUG_ON(ret); 6755d4f98a2SYan Zheng 6765d4f98a2SYan Zheng ret = btrfs_del_root(trans, tree_root, &root->root_key); 6775d4f98a2SYan Zheng BUG_ON(ret); 6785d4f98a2SYan Zheng 6795d4f98a2SYan Zheng nr = trans->blocks_used; 6805d4f98a2SYan Zheng ret = btrfs_end_transaction(trans, tree_root); 6815d4f98a2SYan Zheng BUG_ON(ret); 6825d4f98a2SYan Zheng 6835d4f98a2SYan Zheng free_extent_buffer(root->node); 6845d4f98a2SYan Zheng free_extent_buffer(root->commit_root); 6855d4f98a2SYan Zheng kfree(root); 6865d4f98a2SYan Zheng 6875d4f98a2SYan Zheng btrfs_btree_balance_dirty(tree_root, nr); 68854aa1f4dSChris Mason return ret; 6890f7d52f4SChris Mason } 6902c47e605SYan Zheng #endif 6910f7d52f4SChris Mason 692d352ac68SChris Mason /* 693d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 694d352ac68SChris Mason * transaction commit. This does the actual creation 695d352ac68SChris Mason */ 69680b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 6973063d29fSChris Mason struct btrfs_fs_info *fs_info, 6983063d29fSChris Mason struct btrfs_pending_snapshot *pending) 6993063d29fSChris Mason { 7003063d29fSChris Mason struct btrfs_key key; 70180b6794dSChris Mason struct btrfs_root_item *new_root_item; 7023063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 7033063d29fSChris Mason struct btrfs_root *root = pending->root; 7043063d29fSChris Mason struct extent_buffer *tmp; 705925baeddSChris Mason struct extent_buffer *old; 7063063d29fSChris Mason int ret; 7073063d29fSChris Mason u64 objectid; 7083063d29fSChris Mason 70980b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 71080b6794dSChris Mason if (!new_root_item) { 71180b6794dSChris Mason ret = -ENOMEM; 71280b6794dSChris Mason goto fail; 71380b6794dSChris Mason } 7143063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 7153063d29fSChris Mason if (ret) 7163063d29fSChris Mason goto fail; 7173063d29fSChris Mason 7185d4f98a2SYan Zheng record_root_in_trans(trans, root); 71980ff3856SYan Zheng btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 72080b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 7213063d29fSChris Mason 7223063d29fSChris Mason key.objectid = objectid; 7235d4f98a2SYan Zheng key.offset = 0; 7243063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 7253063d29fSChris Mason 726925baeddSChris Mason old = btrfs_lock_root_node(root); 7279fa8cfe7SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 7285d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 7293063d29fSChris Mason 730925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 731925baeddSChris Mason btrfs_tree_unlock(old); 732925baeddSChris Mason free_extent_buffer(old); 7333063d29fSChris Mason 7345d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 7353063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 73680b6794dSChris Mason new_root_item); 737925baeddSChris Mason btrfs_tree_unlock(tmp); 7383063d29fSChris Mason free_extent_buffer(tmp); 7393063d29fSChris Mason if (ret) 7403063d29fSChris Mason goto fail; 7413063d29fSChris Mason 7423de4586cSChris Mason key.offset = (u64)-1; 7433de4586cSChris Mason memcpy(&pending->root_key, &key, sizeof(key)); 7443de4586cSChris Mason fail: 7453de4586cSChris Mason kfree(new_root_item); 7463de4586cSChris Mason return ret; 7473de4586cSChris Mason } 7483de4586cSChris Mason 7493de4586cSChris Mason static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info, 7503de4586cSChris Mason struct btrfs_pending_snapshot *pending) 7513de4586cSChris Mason { 7523de4586cSChris Mason int ret; 7533de4586cSChris Mason int namelen; 7543de4586cSChris Mason u64 index = 0; 7553de4586cSChris Mason struct btrfs_trans_handle *trans; 7563de4586cSChris Mason struct inode *parent_inode; 7573de4586cSChris Mason struct inode *inode; 7580660b5afSChris Mason struct btrfs_root *parent_root; 7593de4586cSChris Mason 7603394e160SChris Mason parent_inode = pending->dentry->d_parent->d_inode; 7610660b5afSChris Mason parent_root = BTRFS_I(parent_inode)->root; 762180591bcSYan Zheng trans = btrfs_join_transaction(parent_root, 1); 7633de4586cSChris Mason 7643063d29fSChris Mason /* 7653063d29fSChris Mason * insert the directory item 7663063d29fSChris Mason */ 7673b96362cSSven Wegener namelen = strlen(pending->name); 7683de4586cSChris Mason ret = btrfs_set_inode_index(parent_inode, &index); 7690660b5afSChris Mason ret = btrfs_insert_dir_item(trans, parent_root, 7703b96362cSSven Wegener pending->name, namelen, 7713de4586cSChris Mason parent_inode->i_ino, 7723de4586cSChris Mason &pending->root_key, BTRFS_FT_DIR, index); 7733063d29fSChris Mason 7743063d29fSChris Mason if (ret) 7753063d29fSChris Mason goto fail; 7760660b5afSChris Mason 77752c26179SYan Zheng btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); 77852c26179SYan Zheng ret = btrfs_update_inode(trans, parent_root, parent_inode); 77952c26179SYan Zheng BUG_ON(ret); 78052c26179SYan Zheng 7810660b5afSChris Mason /* add the backref first */ 7820660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 7830660b5afSChris Mason pending->root_key.objectid, 7840660b5afSChris Mason BTRFS_ROOT_BACKREF_KEY, 7850660b5afSChris Mason parent_root->root_key.objectid, 7860660b5afSChris Mason parent_inode->i_ino, index, pending->name, 7870660b5afSChris Mason namelen); 7880660b5afSChris Mason 7890660b5afSChris Mason BUG_ON(ret); 7900660b5afSChris Mason 7910660b5afSChris Mason /* now add the forward ref */ 7920660b5afSChris Mason ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 7930660b5afSChris Mason parent_root->root_key.objectid, 7940660b5afSChris Mason BTRFS_ROOT_REF_KEY, 7950660b5afSChris Mason pending->root_key.objectid, 7960660b5afSChris Mason parent_inode->i_ino, index, pending->name, 7970660b5afSChris Mason namelen); 7980660b5afSChris Mason 7993de4586cSChris Mason inode = btrfs_lookup_dentry(parent_inode, pending->dentry); 8003de4586cSChris Mason d_instantiate(pending->dentry, inode); 8013063d29fSChris Mason fail: 8023de4586cSChris Mason btrfs_end_transaction(trans, fs_info->fs_root); 8033063d29fSChris Mason return ret; 8043063d29fSChris Mason } 8053063d29fSChris Mason 806d352ac68SChris Mason /* 807d352ac68SChris Mason * create all the snapshots we've scheduled for creation 808d352ac68SChris Mason */ 80980b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 8103063d29fSChris Mason struct btrfs_fs_info *fs_info) 8113063d29fSChris Mason { 8123063d29fSChris Mason struct btrfs_pending_snapshot *pending; 8133063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8143de4586cSChris Mason int ret; 8153de4586cSChris Mason 816c6e30871SQinghuang Feng list_for_each_entry(pending, head, list) { 8173de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 8183de4586cSChris Mason BUG_ON(ret); 8193de4586cSChris Mason } 8203de4586cSChris Mason return 0; 8213de4586cSChris Mason } 8223de4586cSChris Mason 8233de4586cSChris Mason static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans, 8243de4586cSChris Mason struct btrfs_fs_info *fs_info) 8253de4586cSChris Mason { 8263de4586cSChris Mason struct btrfs_pending_snapshot *pending; 8273de4586cSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 8283063d29fSChris Mason int ret; 8293063d29fSChris Mason 8303063d29fSChris Mason while (!list_empty(head)) { 8313063d29fSChris Mason pending = list_entry(head->next, 8323063d29fSChris Mason struct btrfs_pending_snapshot, list); 8333de4586cSChris Mason ret = finish_pending_snapshot(fs_info, pending); 8343063d29fSChris Mason BUG_ON(ret); 8353063d29fSChris Mason list_del(&pending->list); 8363063d29fSChris Mason kfree(pending->name); 8373063d29fSChris Mason kfree(pending); 8383063d29fSChris Mason } 839dc17ff8fSChris Mason return 0; 840dc17ff8fSChris Mason } 841dc17ff8fSChris Mason 8425d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 8435d4f98a2SYan Zheng { 8445d4f98a2SYan Zheng struct btrfs_root_item *root_item; 8455d4f98a2SYan Zheng struct btrfs_super_block *super; 8465d4f98a2SYan Zheng 8475d4f98a2SYan Zheng super = &root->fs_info->super_copy; 8485d4f98a2SYan Zheng 8495d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 8505d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 8515d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 8525d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 8535d4f98a2SYan Zheng 8545d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 8555d4f98a2SYan Zheng super->root = root_item->bytenr; 8565d4f98a2SYan Zheng super->generation = root_item->generation; 8575d4f98a2SYan Zheng super->root_level = root_item->level; 8585d4f98a2SYan Zheng } 8595d4f98a2SYan Zheng 86079154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 86179154b1bSChris Mason struct btrfs_root *root) 86279154b1bSChris Mason { 86315ee9bc7SJosef Bacik unsigned long joined = 0; 86415ee9bc7SJosef Bacik unsigned long timeout = 1; 86579154b1bSChris Mason struct btrfs_transaction *cur_trans; 8668fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 867d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 86879154b1bSChris Mason DEFINE_WAIT(wait); 86915ee9bc7SJosef Bacik int ret; 87089573b9cSChris Mason int should_grow = 0; 87189573b9cSChris Mason unsigned long now = get_seconds(); 872dccae999SSage Weil int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); 87379154b1bSChris Mason 8745a3f23d5SChris Mason btrfs_run_ordered_operations(root, 0); 8755a3f23d5SChris Mason 87656bec294SChris Mason /* make a pass through all the delayed refs we have so far 87756bec294SChris Mason * any runnings procs may add more while we are here 87856bec294SChris Mason */ 87956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 88056bec294SChris Mason BUG_ON(ret); 88156bec294SChris Mason 882b7ec40d7SChris Mason cur_trans = trans->transaction; 88356bec294SChris Mason /* 88456bec294SChris Mason * set the flushing flag so procs in this transaction have to 88556bec294SChris Mason * start sending their work down. 88656bec294SChris Mason */ 887b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 88856bec294SChris Mason 889c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 89056bec294SChris Mason BUG_ON(ret); 89156bec294SChris Mason 89279154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 893b7ec40d7SChris Mason if (cur_trans->in_commit) { 894b7ec40d7SChris Mason cur_trans->use_count++; 895ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 89679154b1bSChris Mason btrfs_end_transaction(trans, root); 897ccd467d6SChris Mason 89879154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 89979154b1bSChris Mason BUG_ON(ret); 90015ee9bc7SJosef Bacik 90115ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 90279154b1bSChris Mason put_transaction(cur_trans); 90315ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 90415ee9bc7SJosef Bacik 90579154b1bSChris Mason return 0; 90679154b1bSChris Mason } 9074313b399SChris Mason 9084313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 9094313b399SChris Mason if (!pinned_copy) 9104313b399SChris Mason return -ENOMEM; 9114313b399SChris Mason 912d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 9134313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 9144313b399SChris Mason 9152c90e5d6SChris Mason trans->transaction->in_commit = 1; 916f9295749SChris Mason trans->transaction->blocked = 1; 917ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 918ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 919ccd467d6SChris Mason struct btrfs_transaction, list); 920ccd467d6SChris Mason if (!prev_trans->commit_done) { 921ccd467d6SChris Mason prev_trans->use_count++; 922ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 923ccd467d6SChris Mason 924ccd467d6SChris Mason wait_for_commit(root, prev_trans); 925ccd467d6SChris Mason 926ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 92715ee9bc7SJosef Bacik put_transaction(prev_trans); 928ccd467d6SChris Mason } 929ccd467d6SChris Mason } 93015ee9bc7SJosef Bacik 93189573b9cSChris Mason if (now < cur_trans->start_time || now - cur_trans->start_time < 1) 93289573b9cSChris Mason should_grow = 1; 93389573b9cSChris Mason 93415ee9bc7SJosef Bacik do { 9357ea394f1SYan Zheng int snap_pending = 0; 93615ee9bc7SJosef Bacik joined = cur_trans->num_joined; 9377ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 9387ea394f1SYan Zheng snap_pending = 1; 9397ea394f1SYan Zheng 9402c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 94115ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 94279154b1bSChris Mason TASK_UNINTERRUPTIBLE); 94315ee9bc7SJosef Bacik 94415ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 94515ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 94689573b9cSChris Mason else if (should_grow) 94715ee9bc7SJosef Bacik timeout = 1; 94815ee9bc7SJosef Bacik 94979154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 95015ee9bc7SJosef Bacik 951ebecd3d9SSage Weil if (flush_on_commit) { 952dccae999SSage Weil btrfs_start_delalloc_inodes(root); 953ebecd3d9SSage Weil ret = btrfs_wait_ordered_extents(root, 0); 954ebecd3d9SSage Weil BUG_ON(ret); 955ebecd3d9SSage Weil } else if (snap_pending) { 9567ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 9577ea394f1SYan Zheng BUG_ON(ret); 9587ea394f1SYan Zheng } 9597ea394f1SYan Zheng 9605a3f23d5SChris Mason /* 9615a3f23d5SChris Mason * rename don't use btrfs_join_transaction, so, once we 9625a3f23d5SChris Mason * set the transaction to blocked above, we aren't going 9635a3f23d5SChris Mason * to get any new ordered operations. We can safely run 9645a3f23d5SChris Mason * it here and no for sure that nothing new will be added 9655a3f23d5SChris Mason * to the list 9665a3f23d5SChris Mason */ 9675a3f23d5SChris Mason btrfs_run_ordered_operations(root, 1); 9685a3f23d5SChris Mason 96989573b9cSChris Mason smp_mb(); 97089573b9cSChris Mason if (cur_trans->num_writers > 1 || should_grow) 97115ee9bc7SJosef Bacik schedule_timeout(timeout); 97215ee9bc7SJosef Bacik 97379154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 97415ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 97515ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 97689573b9cSChris Mason (should_grow && cur_trans->num_joined != joined)); 97715ee9bc7SJosef Bacik 9783063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 9793063d29fSChris Mason BUG_ON(ret); 9803063d29fSChris Mason 98156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 98256bec294SChris Mason BUG_ON(ret); 98356bec294SChris Mason 9842c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 985dc17ff8fSChris Mason 986e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 987e02119d5SChris Mason * various roots consistent with each other. Every pointer 988e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 989e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 990e02119d5SChris Mason * the tree logging code from jumping in and changing any 991e02119d5SChris Mason * of the trees. 992e02119d5SChris Mason * 993e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 994e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 995e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 996e02119d5SChris Mason * from now until after the super is written, we avoid races 997e02119d5SChris Mason * with the tree-log code. 998e02119d5SChris Mason */ 999e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 10001a40e23bSZheng Yan 10015d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 100254aa1f4dSChris Mason BUG_ON(ret); 100354aa1f4dSChris Mason 10045d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1005e02119d5SChris Mason * safe to free the root of tree log roots 1006e02119d5SChris Mason */ 1007e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1008e02119d5SChris Mason 10095d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 101079154b1bSChris Mason BUG_ON(ret); 101154aa1f4dSChris Mason 101278fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 1013cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 101478fae27eSChris Mason root->fs_info->running_transaction = NULL; 1015cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 10165f39d397SChris Mason 10175d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 10185d4f98a2SYan Zheng root->fs_info->tree_root->node); 1019817d52f8SJosef Bacik switch_commit_root(root->fs_info->tree_root); 10205d4f98a2SYan Zheng 10215d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 10225d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1023817d52f8SJosef Bacik switch_commit_root(root->fs_info->chunk_root); 10245d4f98a2SYan Zheng 10255d4f98a2SYan Zheng update_super_roots(root); 1026e02119d5SChris Mason 1027e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 1028e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 1029e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 1030e02119d5SChris Mason } 1031e02119d5SChris Mason 1032a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 10334b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 1034ccd467d6SChris Mason 10354313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 1036ccd467d6SChris Mason 1037f9295749SChris Mason trans->transaction->blocked = 0; 1038b7ec40d7SChris Mason 1039f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1040e6dcd2dcSChris Mason 104178fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 104279154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 104379154b1bSChris Mason BUG_ON(ret); 1044a512bbf8SYan Zheng write_ctree_super(trans, root, 0); 10454313b399SChris Mason 1046e02119d5SChris Mason /* 1047e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1048e02119d5SChris Mason * to go about their business 1049e02119d5SChris Mason */ 1050e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1051e02119d5SChris Mason 10524313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 10534313b399SChris Mason kfree(pinned_copy); 10544313b399SChris Mason 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; 1061b7ec40d7SChris Mason 106215ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 1063817d52f8SJosef Bacik 10642c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 10653de4586cSChris Mason 106679154b1bSChris Mason put_transaction(cur_trans); 106778fae27eSChris Mason put_transaction(cur_trans); 106858176a96SJosef Bacik 106978fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 10703de4586cSChris Mason 10712c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 107279154b1bSChris Mason return ret; 107379154b1bSChris Mason } 107479154b1bSChris Mason 1075d352ac68SChris Mason /* 1076d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1077d352ac68SChris Mason */ 1078e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1079e9d0b13bSChris Mason { 10805d4f98a2SYan Zheng LIST_HEAD(list); 10815d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1082e9d0b13bSChris Mason 10835d4f98a2SYan Zheng mutex_lock(&fs_info->trans_mutex); 10845d4f98a2SYan Zheng list_splice_init(&fs_info->dead_roots, &list); 10855d4f98a2SYan Zheng mutex_unlock(&fs_info->trans_mutex); 10865d4f98a2SYan Zheng 10875d4f98a2SYan Zheng while (!list_empty(&list)) { 10885d4f98a2SYan Zheng root = list_entry(list.next, struct btrfs_root, root_list); 10895d4f98a2SYan Zheng list_del_init(&root->root_list); 10902c47e605SYan Zheng btrfs_drop_snapshot(root, 0); 1091e9d0b13bSChris Mason } 1092e9d0b13bSChris Mason return 0; 1093e9d0b13bSChris Mason } 1094