16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 1979154b1bSChris Mason #include <linux/fs.h> 2034088780SChris Mason #include <linux/sched.h> 21d3c2fdcfSChris Mason #include <linux/writeback.h> 225f39d397SChris Mason #include <linux/pagemap.h> 2379154b1bSChris Mason #include "ctree.h" 2479154b1bSChris Mason #include "disk-io.h" 2579154b1bSChris Mason #include "transaction.h" 26925baeddSChris Mason #include "locking.h" 2731153d81SYan Zheng #include "ref-cache.h" 28*e02119d5SChris Mason #include "tree-log.h" 2979154b1bSChris Mason 3078fae27eSChris Mason static int total_trans = 0; 312c90e5d6SChris Mason extern struct kmem_cache *btrfs_trans_handle_cachep; 322c90e5d6SChris Mason extern struct kmem_cache *btrfs_transaction_cachep; 332c90e5d6SChris Mason 340f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 350f7d52f4SChris Mason 3680b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3779154b1bSChris Mason { 382c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3979154b1bSChris Mason transaction->use_count--; 4078fae27eSChris Mason if (transaction->use_count == 0) { 4178fae27eSChris Mason WARN_ON(total_trans == 0); 4278fae27eSChris Mason total_trans--; 438fd17795SChris Mason list_del_init(&transaction->list); 442c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 452c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4679154b1bSChris Mason } 4778fae27eSChris Mason } 4879154b1bSChris Mason 4980b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5079154b1bSChris Mason { 5179154b1bSChris Mason struct btrfs_transaction *cur_trans; 5279154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5379154b1bSChris Mason if (!cur_trans) { 542c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 552c90e5d6SChris Mason GFP_NOFS); 5678fae27eSChris Mason total_trans++; 5779154b1bSChris Mason BUG_ON(!cur_trans); 580f7d52f4SChris Mason root->fs_info->generation++; 59e18e4809SChris Mason root->fs_info->last_alloc = 0; 604529ba49SChris Mason root->fs_info->last_data_alloc = 0; 61*e02119d5SChris Mason root->fs_info->last_log_alloc = 0; 6215ee9bc7SJosef Bacik cur_trans->num_writers = 1; 6315ee9bc7SJosef Bacik cur_trans->num_joined = 0; 640f7d52f4SChris Mason cur_trans->transid = root->fs_info->generation; 6579154b1bSChris Mason init_waitqueue_head(&cur_trans->writer_wait); 6679154b1bSChris Mason init_waitqueue_head(&cur_trans->commit_wait); 6779154b1bSChris Mason cur_trans->in_commit = 0; 68f9295749SChris Mason cur_trans->blocked = 0; 69d5719762SChris Mason cur_trans->use_count = 1; 7079154b1bSChris Mason cur_trans->commit_done = 0; 7108607c1bSChris Mason cur_trans->start_time = get_seconds(); 723063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 738fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 74d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 755f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 765f39d397SChris Mason GFP_NOFS); 7748ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 7848ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 7948ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8015ee9bc7SJosef Bacik } else { 8179154b1bSChris Mason cur_trans->num_writers++; 8215ee9bc7SJosef Bacik cur_trans->num_joined++; 8315ee9bc7SJosef Bacik } 8415ee9bc7SJosef Bacik 8579154b1bSChris Mason return 0; 8679154b1bSChris Mason } 8779154b1bSChris Mason 88*e02119d5SChris Mason noinline int btrfs_record_root_in_trans(struct btrfs_root *root) 896702ed49SChris Mason { 90f321e491SYan Zheng struct btrfs_dirty_root *dirty; 916702ed49SChris Mason u64 running_trans_id = root->fs_info->running_transaction->transid; 926702ed49SChris Mason if (root->ref_cows && root->last_trans < running_trans_id) { 936702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 946702ed49SChris Mason if (root->root_item.refs != 0) { 956702ed49SChris Mason radix_tree_tag_set(&root->fs_info->fs_roots_radix, 966702ed49SChris Mason (unsigned long)root->root_key.objectid, 976702ed49SChris Mason BTRFS_ROOT_TRANS_TAG); 9831153d81SYan Zheng 9931153d81SYan Zheng dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 10031153d81SYan Zheng BUG_ON(!dirty); 10131153d81SYan Zheng dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS); 10231153d81SYan Zheng BUG_ON(!dirty->root); 10331153d81SYan Zheng dirty->latest_root = root; 10431153d81SYan Zheng INIT_LIST_HEAD(&dirty->list); 10531153d81SYan Zheng 106925baeddSChris Mason root->commit_root = btrfs_root_node(root); 10731153d81SYan Zheng 10831153d81SYan Zheng memcpy(dirty->root, root, sizeof(*root)); 10931153d81SYan Zheng spin_lock_init(&dirty->root->node_lock); 110bcc63abbSYan spin_lock_init(&dirty->root->list_lock); 11131153d81SYan Zheng mutex_init(&dirty->root->objectid_mutex); 112bcc63abbSYan INIT_LIST_HEAD(&dirty->root->dead_list); 11331153d81SYan Zheng dirty->root->node = root->commit_root; 11431153d81SYan Zheng dirty->root->commit_root = NULL; 115bcc63abbSYan 116bcc63abbSYan spin_lock(&root->list_lock); 117bcc63abbSYan list_add(&dirty->root->dead_list, &root->dead_list); 118bcc63abbSYan spin_unlock(&root->list_lock); 119bcc63abbSYan 120bcc63abbSYan root->dirty_root = dirty; 1216702ed49SChris Mason } else { 1226702ed49SChris Mason WARN_ON(1); 1236702ed49SChris Mason } 1246702ed49SChris Mason root->last_trans = running_trans_id; 1256702ed49SChris Mason } 1266702ed49SChris Mason return 0; 1276702ed49SChris Mason } 1286702ed49SChris Mason 12937d1aeeeSChris Mason static void wait_current_trans(struct btrfs_root *root) 13079154b1bSChris Mason { 131f9295749SChris Mason struct btrfs_transaction *cur_trans; 13279154b1bSChris Mason 133f9295749SChris Mason cur_trans = root->fs_info->running_transaction; 13437d1aeeeSChris Mason if (cur_trans && cur_trans->blocked) { 135f9295749SChris Mason DEFINE_WAIT(wait); 136f9295749SChris Mason cur_trans->use_count++; 137f9295749SChris Mason while(1) { 138f9295749SChris Mason prepare_to_wait(&root->fs_info->transaction_wait, &wait, 139f9295749SChris Mason TASK_UNINTERRUPTIBLE); 140f9295749SChris Mason if (cur_trans->blocked) { 141f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 142f9295749SChris Mason schedule(); 143f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 144f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 145f9295749SChris Mason &wait); 146f9295749SChris Mason } else { 147f9295749SChris Mason finish_wait(&root->fs_info->transaction_wait, 148f9295749SChris Mason &wait); 149f9295749SChris Mason break; 150f9295749SChris Mason } 151f9295749SChris Mason } 152f9295749SChris Mason put_transaction(cur_trans); 153f9295749SChris Mason } 15437d1aeeeSChris Mason } 15537d1aeeeSChris Mason 156*e02119d5SChris Mason static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 1579ca9ee09SSage Weil int num_blocks, int wait) 15837d1aeeeSChris Mason { 15937d1aeeeSChris Mason struct btrfs_trans_handle *h = 16037d1aeeeSChris Mason kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 16137d1aeeeSChris Mason int ret; 16237d1aeeeSChris Mason 16337d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1649ca9ee09SSage Weil if ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2) 16537d1aeeeSChris Mason wait_current_trans(root); 16679154b1bSChris Mason ret = join_transaction(root); 16779154b1bSChris Mason BUG_ON(ret); 1680f7d52f4SChris Mason 169*e02119d5SChris Mason btrfs_record_root_in_trans(root); 1706702ed49SChris Mason h->transid = root->fs_info->running_transaction->transid; 17179154b1bSChris Mason h->transaction = root->fs_info->running_transaction; 17279154b1bSChris Mason h->blocks_reserved = num_blocks; 17379154b1bSChris Mason h->blocks_used = 0; 17431f3c99bSChris Mason h->block_group = NULL; 17526b8003fSChris Mason h->alloc_exclude_nr = 0; 17626b8003fSChris Mason h->alloc_exclude_start = 0; 17779154b1bSChris Mason root->fs_info->running_transaction->use_count++; 17879154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 17979154b1bSChris Mason return h; 18079154b1bSChris Mason } 18179154b1bSChris Mason 182f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 183f9295749SChris Mason int num_blocks) 184f9295749SChris Mason { 1859ca9ee09SSage Weil return start_transaction(root, num_blocks, 1); 186f9295749SChris Mason } 187f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 188f9295749SChris Mason int num_blocks) 189f9295749SChris Mason { 1909ca9ee09SSage Weil return start_transaction(root, num_blocks, 0); 191f9295749SChris Mason } 192f9295749SChris Mason 1939ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 1949ca9ee09SSage Weil int num_blocks) 1959ca9ee09SSage Weil { 1969ca9ee09SSage Weil return start_transaction(r, num_blocks, 2); 1979ca9ee09SSage Weil } 1989ca9ee09SSage Weil 1999ca9ee09SSage Weil 20089ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 20189ce8a63SChris Mason struct btrfs_transaction *commit) 20289ce8a63SChris Mason { 20389ce8a63SChris Mason DEFINE_WAIT(wait); 20489ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 20589ce8a63SChris Mason while(!commit->commit_done) { 20689ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 20789ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 20889ce8a63SChris Mason if (commit->commit_done) 20989ce8a63SChris Mason break; 21089ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21189ce8a63SChris Mason schedule(); 21289ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 21389ce8a63SChris Mason } 21489ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 21589ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 21689ce8a63SChris Mason return 0; 21789ce8a63SChris Mason } 21889ce8a63SChris Mason 21937d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 220ab78c84dSChris Mason { 221ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 2222dd3e67bSChris Mason int harder_count = 0; 223ab78c84dSChris Mason 2242dd3e67bSChris Mason harder: 225ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 226ab78c84dSChris Mason DEFINE_WAIT(wait); 227ab78c84dSChris Mason int thr; 228ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 229ab78c84dSChris Mason 230ab78c84dSChris Mason do { 231ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 232ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 233ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 234ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 235ab78c84dSChris Mason break; 236ab78c84dSChris Mason } 237ab78c84dSChris Mason schedule(); 238ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 239ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 2402dd3e67bSChris Mason harder_count++; 2412dd3e67bSChris Mason 2422dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 2432dd3e67bSChris Mason harder_count < 2) 2442dd3e67bSChris Mason goto harder; 2452dd3e67bSChris Mason 2462dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 2472dd3e67bSChris Mason harder_count < 10) 2482dd3e67bSChris Mason goto harder; 2492dd3e67bSChris Mason 2502dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 2512dd3e67bSChris Mason harder_count < 20) 2522dd3e67bSChris Mason goto harder; 253ab78c84dSChris Mason } 254ab78c84dSChris Mason } 255ab78c84dSChris Mason 25637d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 25737d1aeeeSChris Mason { 25837d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 2599ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 26037d1aeeeSChris Mason wait_current_trans(root); 26137d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 26237d1aeeeSChris Mason 26337d1aeeeSChris Mason throttle_on_drops(root); 26437d1aeeeSChris Mason } 26537d1aeeeSChris Mason 26689ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 26789ce8a63SChris Mason struct btrfs_root *root, int throttle) 26879154b1bSChris Mason { 26979154b1bSChris Mason struct btrfs_transaction *cur_trans; 270ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 271d6e4a428SChris Mason 272ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 273ab78c84dSChris Mason cur_trans = info->running_transaction; 274ccd467d6SChris Mason WARN_ON(cur_trans != trans->transaction); 275d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 276ccd467d6SChris Mason cur_trans->num_writers--; 27789ce8a63SChris Mason 27879154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 27979154b1bSChris Mason wake_up(&cur_trans->writer_wait); 28079154b1bSChris Mason put_transaction(cur_trans); 281ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 282d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 2832c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 284ab78c84dSChris Mason 285ab78c84dSChris Mason if (throttle) 28637d1aeeeSChris Mason throttle_on_drops(root); 287ab78c84dSChris Mason 28879154b1bSChris Mason return 0; 28979154b1bSChris Mason } 29079154b1bSChris Mason 29189ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 29289ce8a63SChris Mason struct btrfs_root *root) 29389ce8a63SChris Mason { 29489ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 0); 29589ce8a63SChris Mason } 29689ce8a63SChris Mason 29789ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 29889ce8a63SChris Mason struct btrfs_root *root) 29989ce8a63SChris Mason { 30089ce8a63SChris Mason return __btrfs_end_transaction(trans, root, 1); 30189ce8a63SChris Mason } 30289ce8a63SChris Mason 30379154b1bSChris Mason 30479154b1bSChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 30579154b1bSChris Mason struct btrfs_root *root) 30679154b1bSChris Mason { 3077c4452b9SChris Mason int ret; 308777e6bd7SChris Mason int err = 0; 3097c4452b9SChris Mason int werr = 0; 310d1310b2eSChris Mason struct extent_io_tree *dirty_pages; 3117c4452b9SChris Mason struct page *page; 3127c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 313777e6bd7SChris Mason u64 start = 0; 3145f39d397SChris Mason u64 end; 3155f39d397SChris Mason unsigned long index; 3167c4452b9SChris Mason 3177c4452b9SChris Mason if (!trans || !trans->transaction) { 3187c4452b9SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 3197c4452b9SChris Mason } 3207c4452b9SChris Mason dirty_pages = &trans->transaction->dirty_pages; 3217c4452b9SChris Mason while(1) { 322777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 3235f39d397SChris Mason EXTENT_DIRTY); 3245f39d397SChris Mason if (ret) 3257c4452b9SChris Mason break; 3265f39d397SChris Mason while(start <= end) { 327777e6bd7SChris Mason cond_resched(); 328777e6bd7SChris Mason 3295f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 33035ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 3315f39d397SChris Mason page = find_lock_page(btree_inode->i_mapping, index); 3327c4452b9SChris Mason if (!page) 3337c4452b9SChris Mason continue; 3346702ed49SChris Mason if (PageWriteback(page)) { 3356702ed49SChris Mason if (PageDirty(page)) 3366702ed49SChris Mason wait_on_page_writeback(page); 3376702ed49SChris Mason else { 3386702ed49SChris Mason unlock_page(page); 3396702ed49SChris Mason page_cache_release(page); 3406702ed49SChris Mason continue; 3416702ed49SChris Mason } 3426702ed49SChris Mason } 3437c4452b9SChris Mason err = write_one_page(page, 0); 3447c4452b9SChris Mason if (err) 3457c4452b9SChris Mason werr = err; 3467c4452b9SChris Mason page_cache_release(page); 3477c4452b9SChris Mason } 3487c4452b9SChris Mason } 349777e6bd7SChris Mason while(1) { 350777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, 0, &start, &end, 351777e6bd7SChris Mason EXTENT_DIRTY); 352777e6bd7SChris Mason if (ret) 353777e6bd7SChris Mason break; 354777e6bd7SChris Mason 355777e6bd7SChris Mason clear_extent_dirty(dirty_pages, start, end, GFP_NOFS); 356777e6bd7SChris Mason while(start <= end) { 357777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 358777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 359777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 360777e6bd7SChris Mason if (!page) 361777e6bd7SChris Mason continue; 362777e6bd7SChris Mason if (PageDirty(page)) { 363777e6bd7SChris Mason lock_page(page); 364777e6bd7SChris Mason err = write_one_page(page, 0); 365777e6bd7SChris Mason if (err) 366777e6bd7SChris Mason werr = err; 367777e6bd7SChris Mason } 368777e6bd7SChris Mason wait_on_page_writeback(page); 369777e6bd7SChris Mason page_cache_release(page); 370777e6bd7SChris Mason cond_resched(); 371777e6bd7SChris Mason } 372777e6bd7SChris Mason } 3737c4452b9SChris Mason if (err) 3747c4452b9SChris Mason werr = err; 3757c4452b9SChris Mason return werr; 37679154b1bSChris Mason } 37779154b1bSChris Mason 3780b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 37979154b1bSChris Mason struct btrfs_root *root) 38079154b1bSChris Mason { 38179154b1bSChris Mason int ret; 3820b86a832SChris Mason u64 old_root_bytenr; 3830b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 38479154b1bSChris Mason 3850b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 38679154b1bSChris Mason while(1) { 3870b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 3880b86a832SChris Mason if (old_root_bytenr == root->node->start) 38979154b1bSChris Mason break; 3900b86a832SChris Mason btrfs_set_root_bytenr(&root->root_item, 3910b86a832SChris Mason root->node->start); 3920b86a832SChris Mason btrfs_set_root_level(&root->root_item, 3930b86a832SChris Mason btrfs_header_level(root->node)); 39479154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 3950b86a832SChris Mason &root->root_key, 3960b86a832SChris Mason &root->root_item); 39779154b1bSChris Mason BUG_ON(ret); 3980b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 3990b86a832SChris Mason } 4000b86a832SChris Mason return 0; 4010b86a832SChris Mason } 4020b86a832SChris Mason 4030b86a832SChris Mason int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 4040b86a832SChris Mason struct btrfs_root *root) 4050b86a832SChris Mason { 4060b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 4070b86a832SChris Mason struct list_head *next; 4080b86a832SChris Mason 4090b86a832SChris Mason while(!list_empty(&fs_info->dirty_cowonly_roots)) { 4100b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 4110b86a832SChris Mason list_del_init(next); 4120b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 4130b86a832SChris Mason update_cowonly_root(trans, root); 41479154b1bSChris Mason } 41579154b1bSChris Mason return 0; 41679154b1bSChris Mason } 41779154b1bSChris Mason 418b48652c1SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest) 4195eda7b5eSChris Mason { 420f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4215eda7b5eSChris Mason 4225eda7b5eSChris Mason dirty = kmalloc(sizeof(*dirty), GFP_NOFS); 4235eda7b5eSChris Mason if (!dirty) 4245eda7b5eSChris Mason return -ENOMEM; 4255eda7b5eSChris Mason dirty->root = root; 4265ce14bbcSChris Mason dirty->latest_root = latest; 427b48652c1SYan Zheng 428b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 429b48652c1SYan Zheng list_add(&dirty->list, &latest->fs_info->dead_roots); 430b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 4315eda7b5eSChris Mason return 0; 4325eda7b5eSChris Mason } 4335eda7b5eSChris Mason 43480b6794dSChris Mason static noinline int add_dirty_roots(struct btrfs_trans_handle *trans, 43535b7e476SChris Mason struct radix_tree_root *radix, 43635b7e476SChris Mason struct list_head *list) 4370f7d52f4SChris Mason { 438f321e491SYan Zheng struct btrfs_dirty_root *dirty; 4390f7d52f4SChris Mason struct btrfs_root *gang[8]; 4400f7d52f4SChris Mason struct btrfs_root *root; 4410f7d52f4SChris Mason int i; 4420f7d52f4SChris Mason int ret; 44354aa1f4dSChris Mason int err = 0; 4445eda7b5eSChris Mason u32 refs; 44554aa1f4dSChris Mason 4460f7d52f4SChris Mason while(1) { 4470f7d52f4SChris Mason ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0, 4480f7d52f4SChris Mason ARRAY_SIZE(gang), 4490f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 4500f7d52f4SChris Mason if (ret == 0) 4510f7d52f4SChris Mason break; 4520f7d52f4SChris Mason for (i = 0; i < ret; i++) { 4530f7d52f4SChris Mason root = gang[i]; 4542619ba1fSChris Mason radix_tree_tag_clear(radix, 4552619ba1fSChris Mason (unsigned long)root->root_key.objectid, 4560f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 45731153d81SYan Zheng 45831153d81SYan Zheng BUG_ON(!root->ref_tree); 459017e5369SChris Mason dirty = root->dirty_root; 46031153d81SYan Zheng 461*e02119d5SChris Mason btrfs_free_log(trans, root); 462*e02119d5SChris Mason 4630f7d52f4SChris Mason if (root->commit_root == root->node) { 464db94535dSChris Mason WARN_ON(root->node->start != 465db94535dSChris Mason btrfs_root_bytenr(&root->root_item)); 46631153d81SYan Zheng 4675f39d397SChris Mason free_extent_buffer(root->commit_root); 4680f7d52f4SChris Mason root->commit_root = NULL; 4697ea394f1SYan Zheng root->dirty_root = NULL; 47031153d81SYan Zheng 471bcc63abbSYan spin_lock(&root->list_lock); 472bcc63abbSYan list_del_init(&dirty->root->dead_list); 473bcc63abbSYan spin_unlock(&root->list_lock); 474bcc63abbSYan 47531153d81SYan Zheng kfree(dirty->root); 47631153d81SYan Zheng kfree(dirty); 47758176a96SJosef Bacik 47858176a96SJosef Bacik /* make sure to update the root on disk 47958176a96SJosef Bacik * so we get any updates to the block used 48058176a96SJosef Bacik * counts 48158176a96SJosef Bacik */ 48258176a96SJosef Bacik err = btrfs_update_root(trans, 48358176a96SJosef Bacik root->fs_info->tree_root, 48458176a96SJosef Bacik &root->root_key, 48558176a96SJosef Bacik &root->root_item); 4860f7d52f4SChris Mason continue; 4870f7d52f4SChris Mason } 4889f3a7427SChris Mason 4899f3a7427SChris Mason memset(&root->root_item.drop_progress, 0, 4909f3a7427SChris Mason sizeof(struct btrfs_disk_key)); 4919f3a7427SChris Mason root->root_item.drop_level = 0; 4920f7d52f4SChris Mason root->commit_root = NULL; 4937ea394f1SYan Zheng root->dirty_root = NULL; 4940f7d52f4SChris Mason root->root_key.offset = root->fs_info->generation; 495db94535dSChris Mason btrfs_set_root_bytenr(&root->root_item, 496db94535dSChris Mason root->node->start); 497db94535dSChris Mason btrfs_set_root_level(&root->root_item, 498db94535dSChris Mason btrfs_header_level(root->node)); 4990f7d52f4SChris Mason err = btrfs_insert_root(trans, root->fs_info->tree_root, 5000f7d52f4SChris Mason &root->root_key, 5010f7d52f4SChris Mason &root->root_item); 50254aa1f4dSChris Mason if (err) 50354aa1f4dSChris Mason break; 5049f3a7427SChris Mason 5059f3a7427SChris Mason refs = btrfs_root_refs(&dirty->root->root_item); 5069f3a7427SChris Mason btrfs_set_root_refs(&dirty->root->root_item, refs - 1); 5075eda7b5eSChris Mason err = btrfs_update_root(trans, root->fs_info->tree_root, 5089f3a7427SChris Mason &dirty->root->root_key, 5099f3a7427SChris Mason &dirty->root->root_item); 5105eda7b5eSChris Mason 5115eda7b5eSChris Mason BUG_ON(err); 5129f3a7427SChris Mason if (refs == 1) { 5130f7d52f4SChris Mason list_add(&dirty->list, list); 5149f3a7427SChris Mason } else { 5159f3a7427SChris Mason WARN_ON(1); 51631153d81SYan Zheng free_extent_buffer(dirty->root->node); 5179f3a7427SChris Mason kfree(dirty->root); 5185eda7b5eSChris Mason kfree(dirty); 5190f7d52f4SChris Mason } 5200f7d52f4SChris Mason } 5219f3a7427SChris Mason } 52254aa1f4dSChris Mason return err; 5230f7d52f4SChris Mason } 5240f7d52f4SChris Mason 525e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 526e9d0b13bSChris Mason { 527e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 528e9d0b13bSChris Mason int ret; 529e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 530d3c2fdcfSChris Mason unsigned long nr; 531e9d0b13bSChris Mason 532a2135011SChris Mason smp_mb(); 533e9d0b13bSChris Mason if (root->defrag_running) 534e9d0b13bSChris Mason return 0; 535e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5366b80053dSChris Mason while (1) { 537e9d0b13bSChris Mason root->defrag_running = 1; 538e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 539d3c2fdcfSChris Mason nr = trans->blocks_used; 540e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 541d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 542e9d0b13bSChris Mason cond_resched(); 543e9d0b13bSChris Mason 544e9d0b13bSChris Mason trans = btrfs_start_transaction(root, 1); 5453f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 546e9d0b13bSChris Mason break; 547e9d0b13bSChris Mason } 548e9d0b13bSChris Mason root->defrag_running = 0; 549a2135011SChris Mason smp_mb(); 550e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 551e9d0b13bSChris Mason return 0; 552e9d0b13bSChris Mason } 553e9d0b13bSChris Mason 55480b6794dSChris Mason static noinline int drop_dirty_roots(struct btrfs_root *tree_root, 55535b7e476SChris Mason struct list_head *list) 5560f7d52f4SChris Mason { 557f321e491SYan Zheng struct btrfs_dirty_root *dirty; 5580f7d52f4SChris Mason struct btrfs_trans_handle *trans; 559d3c2fdcfSChris Mason unsigned long nr; 560db94535dSChris Mason u64 num_bytes; 561db94535dSChris Mason u64 bytes_used; 562bcc63abbSYan u64 max_useless; 56354aa1f4dSChris Mason int ret = 0; 5649f3a7427SChris Mason int err; 5659f3a7427SChris Mason 5660f7d52f4SChris Mason while(!list_empty(list)) { 56758176a96SJosef Bacik struct btrfs_root *root; 56858176a96SJosef Bacik 569f321e491SYan Zheng dirty = list_entry(list->prev, struct btrfs_dirty_root, list); 5700f7d52f4SChris Mason list_del_init(&dirty->list); 5715eda7b5eSChris Mason 572db94535dSChris Mason num_bytes = btrfs_root_used(&dirty->root->root_item); 57358176a96SJosef Bacik root = dirty->latest_root; 574a2135011SChris Mason atomic_inc(&root->fs_info->throttles); 57558176a96SJosef Bacik 576a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5779f3a7427SChris Mason while(1) { 5780f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 5799f3a7427SChris Mason ret = btrfs_drop_snapshot(trans, dirty->root); 5809f3a7427SChris Mason if (ret != -EAGAIN) { 5819f3a7427SChris Mason break; 5829f3a7427SChris Mason } 58358176a96SJosef Bacik 5849f3a7427SChris Mason err = btrfs_update_root(trans, 5859f3a7427SChris Mason tree_root, 5869f3a7427SChris Mason &dirty->root->root_key, 5879f3a7427SChris Mason &dirty->root->root_item); 5889f3a7427SChris Mason if (err) 5899f3a7427SChris Mason ret = err; 590d3c2fdcfSChris Mason nr = trans->blocks_used; 591017e5369SChris Mason ret = btrfs_end_transaction(trans, tree_root); 5920f7d52f4SChris Mason BUG_ON(ret); 593a2135011SChris Mason 594a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 595d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 5964dc11904SChris Mason cond_resched(); 597a2135011SChris Mason mutex_lock(&root->fs_info->drop_mutex); 5989f3a7427SChris Mason } 5999f3a7427SChris Mason BUG_ON(ret); 600a2135011SChris Mason atomic_dec(&root->fs_info->throttles); 601017e5369SChris Mason wake_up(&root->fs_info->transaction_throttle); 60258176a96SJosef Bacik 603a2135011SChris Mason mutex_lock(&root->fs_info->alloc_mutex); 604db94535dSChris Mason num_bytes -= btrfs_root_used(&dirty->root->root_item); 605db94535dSChris Mason bytes_used = btrfs_root_used(&root->root_item); 606db94535dSChris Mason if (num_bytes) { 607*e02119d5SChris Mason btrfs_record_root_in_trans(root); 6085f39d397SChris Mason btrfs_set_root_used(&root->root_item, 609db94535dSChris Mason bytes_used - num_bytes); 61058176a96SJosef Bacik } 611a2135011SChris Mason mutex_unlock(&root->fs_info->alloc_mutex); 612a2135011SChris Mason 6139f3a7427SChris Mason ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 61458176a96SJosef Bacik if (ret) { 61558176a96SJosef Bacik BUG(); 61654aa1f4dSChris Mason break; 61758176a96SJosef Bacik } 618a2135011SChris Mason mutex_unlock(&root->fs_info->drop_mutex); 619a2135011SChris Mason 620bcc63abbSYan spin_lock(&root->list_lock); 621bcc63abbSYan list_del_init(&dirty->root->dead_list); 622bcc63abbSYan if (!list_empty(&root->dead_list)) { 623bcc63abbSYan struct btrfs_root *oldest; 624bcc63abbSYan oldest = list_entry(root->dead_list.prev, 625bcc63abbSYan struct btrfs_root, dead_list); 626bcc63abbSYan max_useless = oldest->root_key.offset - 1; 627bcc63abbSYan } else { 628bcc63abbSYan max_useless = root->root_key.offset - 1; 629bcc63abbSYan } 630bcc63abbSYan spin_unlock(&root->list_lock); 631bcc63abbSYan 632d3c2fdcfSChris Mason nr = trans->blocks_used; 6330f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 6340f7d52f4SChris Mason BUG_ON(ret); 6355eda7b5eSChris Mason 636bcc63abbSYan ret = btrfs_remove_leaf_refs(root, max_useless); 637bcc63abbSYan BUG_ON(ret); 638bcc63abbSYan 639f510cfecSChris Mason free_extent_buffer(dirty->root->node); 6405eda7b5eSChris Mason kfree(dirty->root); 6410f7d52f4SChris Mason kfree(dirty); 642d3c2fdcfSChris Mason 643d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 6444dc11904SChris Mason cond_resched(); 6450f7d52f4SChris Mason } 64654aa1f4dSChris Mason return ret; 6470f7d52f4SChris Mason } 6480f7d52f4SChris Mason 64980b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 6503063d29fSChris Mason struct btrfs_fs_info *fs_info, 6513063d29fSChris Mason struct btrfs_pending_snapshot *pending) 6523063d29fSChris Mason { 6533063d29fSChris Mason struct btrfs_key key; 65480b6794dSChris Mason struct btrfs_root_item *new_root_item; 6553063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 6563063d29fSChris Mason struct btrfs_root *root = pending->root; 6573063d29fSChris Mason struct extent_buffer *tmp; 658925baeddSChris Mason struct extent_buffer *old; 6593063d29fSChris Mason int ret; 6603b96362cSSven Wegener int namelen; 6613063d29fSChris Mason u64 objectid; 6623063d29fSChris Mason 66380b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 66480b6794dSChris Mason if (!new_root_item) { 66580b6794dSChris Mason ret = -ENOMEM; 66680b6794dSChris Mason goto fail; 66780b6794dSChris Mason } 6683063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 6693063d29fSChris Mason if (ret) 6703063d29fSChris Mason goto fail; 6713063d29fSChris Mason 67280b6794dSChris Mason memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 6733063d29fSChris Mason 6743063d29fSChris Mason key.objectid = objectid; 6753063d29fSChris Mason key.offset = 1; 6763063d29fSChris Mason btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 6773063d29fSChris Mason 678925baeddSChris Mason old = btrfs_lock_root_node(root); 67965b51a00SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old, 0); 6803063d29fSChris Mason 681925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 682925baeddSChris Mason btrfs_tree_unlock(old); 683925baeddSChris Mason free_extent_buffer(old); 6843063d29fSChris Mason 68580b6794dSChris Mason btrfs_set_root_bytenr(new_root_item, tmp->start); 68680b6794dSChris Mason btrfs_set_root_level(new_root_item, btrfs_header_level(tmp)); 6873063d29fSChris Mason ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 68880b6794dSChris Mason new_root_item); 689925baeddSChris Mason btrfs_tree_unlock(tmp); 6903063d29fSChris Mason free_extent_buffer(tmp); 6913063d29fSChris Mason if (ret) 6923063d29fSChris Mason goto fail; 6933063d29fSChris Mason 6943063d29fSChris Mason /* 6953063d29fSChris Mason * insert the directory item 6963063d29fSChris Mason */ 6973063d29fSChris Mason key.offset = (u64)-1; 6983b96362cSSven Wegener namelen = strlen(pending->name); 6993063d29fSChris Mason ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, 7003b96362cSSven Wegener pending->name, namelen, 7013063d29fSChris Mason root->fs_info->sb->s_root->d_inode->i_ino, 702aec7477bSJosef Bacik &key, BTRFS_FT_DIR, 0); 7033063d29fSChris Mason 7043063d29fSChris Mason if (ret) 7053063d29fSChris Mason goto fail; 7063063d29fSChris Mason 7073063d29fSChris Mason ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, 7083063d29fSChris Mason pending->name, strlen(pending->name), objectid, 709aec7477bSJosef Bacik root->fs_info->sb->s_root->d_inode->i_ino, 0); 7103b96362cSSven Wegener 7113b96362cSSven Wegener /* Invalidate existing dcache entry for new snapshot. */ 7123b96362cSSven Wegener btrfs_invalidate_dcache_root(root, pending->name, namelen); 7133b96362cSSven Wegener 7143063d29fSChris Mason fail: 71580b6794dSChris Mason kfree(new_root_item); 7163063d29fSChris Mason return ret; 7173063d29fSChris Mason } 7183063d29fSChris Mason 71980b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 7203063d29fSChris Mason struct btrfs_fs_info *fs_info) 7213063d29fSChris Mason { 7223063d29fSChris Mason struct btrfs_pending_snapshot *pending; 7233063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 7243063d29fSChris Mason int ret; 7253063d29fSChris Mason 7263063d29fSChris Mason while(!list_empty(head)) { 7273063d29fSChris Mason pending = list_entry(head->next, 7283063d29fSChris Mason struct btrfs_pending_snapshot, list); 7293063d29fSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 7303063d29fSChris Mason BUG_ON(ret); 7313063d29fSChris Mason list_del(&pending->list); 7323063d29fSChris Mason kfree(pending->name); 7333063d29fSChris Mason kfree(pending); 7343063d29fSChris Mason } 735dc17ff8fSChris Mason return 0; 736dc17ff8fSChris Mason } 737dc17ff8fSChris Mason 73879154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 73979154b1bSChris Mason struct btrfs_root *root) 74079154b1bSChris Mason { 74115ee9bc7SJosef Bacik unsigned long joined = 0; 74215ee9bc7SJosef Bacik unsigned long timeout = 1; 74379154b1bSChris Mason struct btrfs_transaction *cur_trans; 7448fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 7450b86a832SChris Mason struct btrfs_root *chunk_root = root->fs_info->chunk_root; 7460f7d52f4SChris Mason struct list_head dirty_fs_roots; 747d1310b2eSChris Mason struct extent_io_tree *pinned_copy; 74879154b1bSChris Mason DEFINE_WAIT(wait); 74915ee9bc7SJosef Bacik int ret; 75079154b1bSChris Mason 7510f7d52f4SChris Mason INIT_LIST_HEAD(&dirty_fs_roots); 75279154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 75379154b1bSChris Mason if (trans->transaction->in_commit) { 75479154b1bSChris Mason cur_trans = trans->transaction; 75579154b1bSChris Mason trans->transaction->use_count++; 756ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 75779154b1bSChris Mason btrfs_end_transaction(trans, root); 758ccd467d6SChris Mason 75979154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 76079154b1bSChris Mason BUG_ON(ret); 76115ee9bc7SJosef Bacik 76215ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 76379154b1bSChris Mason put_transaction(cur_trans); 76415ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 76515ee9bc7SJosef Bacik 76679154b1bSChris Mason return 0; 76779154b1bSChris Mason } 7684313b399SChris Mason 7694313b399SChris Mason pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS); 7704313b399SChris Mason if (!pinned_copy) 7714313b399SChris Mason return -ENOMEM; 7724313b399SChris Mason 773d1310b2eSChris Mason extent_io_tree_init(pinned_copy, 7744313b399SChris Mason root->fs_info->btree_inode->i_mapping, GFP_NOFS); 7754313b399SChris Mason 7762c90e5d6SChris Mason trans->transaction->in_commit = 1; 777f9295749SChris Mason trans->transaction->blocked = 1; 778ccd467d6SChris Mason cur_trans = trans->transaction; 779ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 780ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 781ccd467d6SChris Mason struct btrfs_transaction, list); 782ccd467d6SChris Mason if (!prev_trans->commit_done) { 783ccd467d6SChris Mason prev_trans->use_count++; 784ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 785ccd467d6SChris Mason 786ccd467d6SChris Mason wait_for_commit(root, prev_trans); 787ccd467d6SChris Mason 788ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 78915ee9bc7SJosef Bacik put_transaction(prev_trans); 790ccd467d6SChris Mason } 791ccd467d6SChris Mason } 79215ee9bc7SJosef Bacik 79315ee9bc7SJosef Bacik do { 7947ea394f1SYan Zheng int snap_pending = 0; 79515ee9bc7SJosef Bacik joined = cur_trans->num_joined; 7967ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 7977ea394f1SYan Zheng snap_pending = 1; 7987ea394f1SYan Zheng 7992c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 80015ee9bc7SJosef Bacik prepare_to_wait(&cur_trans->writer_wait, &wait, 80179154b1bSChris Mason TASK_UNINTERRUPTIBLE); 80215ee9bc7SJosef Bacik 80315ee9bc7SJosef Bacik if (cur_trans->num_writers > 1) 80415ee9bc7SJosef Bacik timeout = MAX_SCHEDULE_TIMEOUT; 80515ee9bc7SJosef Bacik else 80615ee9bc7SJosef Bacik timeout = 1; 80715ee9bc7SJosef Bacik 80879154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 80915ee9bc7SJosef Bacik 8107ea394f1SYan Zheng if (snap_pending) { 8117ea394f1SYan Zheng ret = btrfs_wait_ordered_extents(root, 1); 8127ea394f1SYan Zheng BUG_ON(ret); 8137ea394f1SYan Zheng } 8147ea394f1SYan Zheng 81515ee9bc7SJosef Bacik schedule_timeout(timeout); 81615ee9bc7SJosef Bacik 81779154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 81815ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 81915ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 82015ee9bc7SJosef Bacik (cur_trans->num_joined != joined)); 82115ee9bc7SJosef Bacik 8223063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 8233063d29fSChris Mason BUG_ON(ret); 8243063d29fSChris Mason 8252c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 826dc17ff8fSChris Mason 827*e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 828*e02119d5SChris Mason * various roots consistent with each other. Every pointer 829*e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 830*e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 831*e02119d5SChris Mason * the tree logging code from jumping in and changing any 832*e02119d5SChris Mason * of the trees. 833*e02119d5SChris Mason * 834*e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 835*e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 836*e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 837*e02119d5SChris Mason * from now until after the super is written, we avoid races 838*e02119d5SChris Mason * with the tree-log code. 839*e02119d5SChris Mason */ 840*e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 841*e02119d5SChris Mason 84254aa1f4dSChris Mason ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix, 84354aa1f4dSChris Mason &dirty_fs_roots); 84454aa1f4dSChris Mason BUG_ON(ret); 84554aa1f4dSChris Mason 846*e02119d5SChris Mason /* add_dirty_roots gets rid of all the tree log roots, it is now 847*e02119d5SChris Mason * safe to free the root of tree log roots 848*e02119d5SChris Mason */ 849*e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 850*e02119d5SChris Mason 85179154b1bSChris Mason ret = btrfs_commit_tree_roots(trans, root); 85279154b1bSChris Mason BUG_ON(ret); 85354aa1f4dSChris Mason 85478fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 855cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 85678fae27eSChris Mason root->fs_info->running_transaction = NULL; 857cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8584b52dff6SChris Mason btrfs_set_super_generation(&root->fs_info->super_copy, 8594b52dff6SChris Mason cur_trans->transid); 8604b52dff6SChris Mason btrfs_set_super_root(&root->fs_info->super_copy, 861db94535dSChris Mason root->fs_info->tree_root->node->start); 862db94535dSChris Mason btrfs_set_super_root_level(&root->fs_info->super_copy, 863db94535dSChris Mason btrfs_header_level(root->fs_info->tree_root->node)); 8645f39d397SChris Mason 8650b86a832SChris Mason btrfs_set_super_chunk_root(&root->fs_info->super_copy, 8660b86a832SChris Mason chunk_root->node->start); 8670b86a832SChris Mason btrfs_set_super_chunk_root_level(&root->fs_info->super_copy, 8680b86a832SChris Mason btrfs_header_level(chunk_root->node)); 869*e02119d5SChris Mason 870*e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 871*e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 872*e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 873*e02119d5SChris Mason } 874*e02119d5SChris Mason 875a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 8764b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 877ccd467d6SChris Mason 8784313b399SChris Mason btrfs_copy_pinned(root, pinned_copy); 879ccd467d6SChris Mason 880f9295749SChris Mason trans->transaction->blocked = 0; 881e6dcd2dcSChris Mason wake_up(&root->fs_info->transaction_throttle); 882f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 883e6dcd2dcSChris Mason 88478fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 88579154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 88679154b1bSChris Mason BUG_ON(ret); 88779154b1bSChris Mason write_ctree_super(trans, root); 8884313b399SChris Mason 889*e02119d5SChris Mason /* 890*e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 891*e02119d5SChris Mason * to go about their business 892*e02119d5SChris Mason */ 893*e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 894*e02119d5SChris Mason 8954313b399SChris Mason btrfs_finish_extent_commit(trans, root, pinned_copy); 89678fae27eSChris Mason mutex_lock(&root->fs_info->trans_mutex); 8974313b399SChris Mason 8984313b399SChris Mason kfree(pinned_copy); 8994313b399SChris Mason 9002c90e5d6SChris Mason cur_trans->commit_done = 1; 90115ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 9022c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 90379154b1bSChris Mason put_transaction(cur_trans); 90478fae27eSChris Mason put_transaction(cur_trans); 90558176a96SJosef Bacik 906bcc63abbSYan list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots); 907facda1e7SChris Mason if (root->fs_info->closing) 908facda1e7SChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots); 90958176a96SJosef Bacik 91078fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 9112c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 91279154b1bSChris Mason 913facda1e7SChris Mason if (root->fs_info->closing) { 9140f7d52f4SChris Mason drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 915facda1e7SChris Mason } 91679154b1bSChris Mason return ret; 91779154b1bSChris Mason } 91879154b1bSChris Mason 919e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 920e9d0b13bSChris Mason { 921e9d0b13bSChris Mason struct list_head dirty_roots; 922e9d0b13bSChris Mason INIT_LIST_HEAD(&dirty_roots); 923a74a4b97SChris Mason again: 924e9d0b13bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 925e9d0b13bSChris Mason list_splice_init(&root->fs_info->dead_roots, &dirty_roots); 926e9d0b13bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 927e9d0b13bSChris Mason 928e9d0b13bSChris Mason if (!list_empty(&dirty_roots)) { 929e9d0b13bSChris Mason drop_dirty_roots(root, &dirty_roots); 930a74a4b97SChris Mason goto again; 931e9d0b13bSChris Mason } 932e9d0b13bSChris Mason return 0; 933e9d0b13bSChris Mason } 934