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> 205a0e3ad6STejun Heo #include <linux/slab.h> 2134088780SChris Mason #include <linux/sched.h> 22d3c2fdcfSChris Mason #include <linux/writeback.h> 235f39d397SChris Mason #include <linux/pagemap.h> 245f2cc086SChris Mason #include <linux/blkdev.h> 2579154b1bSChris Mason #include "ctree.h" 2679154b1bSChris Mason #include "disk-io.h" 2779154b1bSChris Mason #include "transaction.h" 28925baeddSChris Mason #include "locking.h" 29e02119d5SChris Mason #include "tree-log.h" 3079154b1bSChris Mason 310f7d52f4SChris Mason #define BTRFS_ROOT_TRANS_TAG 0 320f7d52f4SChris Mason 3380b6794dSChris Mason static noinline void put_transaction(struct btrfs_transaction *transaction) 3479154b1bSChris Mason { 352c90e5d6SChris Mason WARN_ON(transaction->use_count == 0); 3679154b1bSChris Mason transaction->use_count--; 3778fae27eSChris Mason if (transaction->use_count == 0) { 388fd17795SChris Mason list_del_init(&transaction->list); 392c90e5d6SChris Mason memset(transaction, 0, sizeof(*transaction)); 402c90e5d6SChris Mason kmem_cache_free(btrfs_transaction_cachep, transaction); 4179154b1bSChris Mason } 4278fae27eSChris Mason } 4379154b1bSChris Mason 44817d52f8SJosef Bacik static noinline void switch_commit_root(struct btrfs_root *root) 45817d52f8SJosef Bacik { 46817d52f8SJosef Bacik free_extent_buffer(root->commit_root); 47817d52f8SJosef Bacik root->commit_root = btrfs_root_node(root); 48817d52f8SJosef Bacik } 49817d52f8SJosef Bacik 50d352ac68SChris Mason /* 51d352ac68SChris Mason * either allocate a new transaction or hop into the existing one 52d352ac68SChris Mason */ 5380b6794dSChris Mason static noinline int join_transaction(struct btrfs_root *root) 5479154b1bSChris Mason { 5579154b1bSChris Mason struct btrfs_transaction *cur_trans; 5679154b1bSChris Mason cur_trans = root->fs_info->running_transaction; 5779154b1bSChris Mason if (!cur_trans) { 582c90e5d6SChris Mason cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 592c90e5d6SChris Mason GFP_NOFS); 6079154b1bSChris Mason BUG_ON(!cur_trans); 610f7d52f4SChris Mason root->fs_info->generation++; 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(); 7256bec294SChris Mason 736bef4d31SEric Paris cur_trans->delayed_refs.root = RB_ROOT; 7456bec294SChris Mason cur_trans->delayed_refs.num_entries = 0; 75c3e69d58SChris Mason cur_trans->delayed_refs.num_heads_ready = 0; 76c3e69d58SChris Mason cur_trans->delayed_refs.num_heads = 0; 7756bec294SChris Mason cur_trans->delayed_refs.flushing = 0; 78c3e69d58SChris Mason cur_trans->delayed_refs.run_delayed_start = 0; 7956bec294SChris Mason spin_lock_init(&cur_trans->delayed_refs.lock); 8056bec294SChris Mason 813063d29fSChris Mason INIT_LIST_HEAD(&cur_trans->pending_snapshots); 828fd17795SChris Mason list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 83d1310b2eSChris Mason extent_io_tree_init(&cur_trans->dirty_pages, 845f39d397SChris Mason root->fs_info->btree_inode->i_mapping, 855f39d397SChris Mason GFP_NOFS); 8648ec2cf8SChris Mason spin_lock(&root->fs_info->new_trans_lock); 8748ec2cf8SChris Mason root->fs_info->running_transaction = cur_trans; 8848ec2cf8SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 8915ee9bc7SJosef Bacik } else { 9079154b1bSChris Mason cur_trans->num_writers++; 9115ee9bc7SJosef Bacik cur_trans->num_joined++; 9215ee9bc7SJosef Bacik } 9315ee9bc7SJosef Bacik 9479154b1bSChris Mason return 0; 9579154b1bSChris Mason } 9679154b1bSChris Mason 97d352ac68SChris Mason /* 98d397712bSChris Mason * this does all the record keeping required to make sure that a reference 99d397712bSChris Mason * counted root is properly recorded in a given transaction. This is required 100d397712bSChris Mason * to make sure the old root from before we joined the transaction is deleted 101d397712bSChris Mason * when the transaction commits 102d352ac68SChris Mason */ 1035d4f98a2SYan Zheng static noinline int record_root_in_trans(struct btrfs_trans_handle *trans, 1045d4f98a2SYan Zheng struct btrfs_root *root) 1056702ed49SChris Mason { 1065d4f98a2SYan Zheng if (root->ref_cows && root->last_trans < trans->transid) { 1076702ed49SChris Mason WARN_ON(root == root->fs_info->extent_root); 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); 151471fa17dSZhao Lei if (!cur_trans->blocked) 152471fa17dSZhao Lei break; 153f9295749SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 154f9295749SChris Mason schedule(); 155f9295749SChris Mason mutex_lock(&root->fs_info->trans_mutex); 156f9295749SChris Mason } 157471fa17dSZhao Lei finish_wait(&root->fs_info->transaction_wait, &wait); 158f9295749SChris Mason put_transaction(cur_trans); 159f9295749SChris Mason } 16037d1aeeeSChris Mason } 16137d1aeeeSChris Mason 162249ac1e5SJosef Bacik enum btrfs_trans_type { 163249ac1e5SJosef Bacik TRANS_START, 164249ac1e5SJosef Bacik TRANS_JOIN, 165249ac1e5SJosef Bacik TRANS_USERSPACE, 1660af3d00bSJosef Bacik TRANS_JOIN_NOLOCK, 167249ac1e5SJosef Bacik }; 168249ac1e5SJosef Bacik 169a22285a6SYan, Zheng static int may_wait_transaction(struct btrfs_root *root, int type) 17037d1aeeeSChris Mason { 1714bef0848SChris Mason if (!root->fs_info->log_root_recovering && 172249ac1e5SJosef Bacik ((type == TRANS_START && !root->fs_info->open_ioctl_trans) || 173249ac1e5SJosef Bacik type == TRANS_USERSPACE)) 174a22285a6SYan, Zheng return 1; 175a22285a6SYan, Zheng return 0; 176a22285a6SYan, Zheng } 177a22285a6SYan, Zheng 178a22285a6SYan, Zheng static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 179a22285a6SYan, Zheng u64 num_items, int type) 180a22285a6SYan, Zheng { 181a22285a6SYan, Zheng struct btrfs_trans_handle *h; 182a22285a6SYan, Zheng struct btrfs_transaction *cur_trans; 183a22285a6SYan, Zheng int ret; 184a22285a6SYan, Zheng again: 185a22285a6SYan, Zheng h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 186a22285a6SYan, Zheng if (!h) 187a22285a6SYan, Zheng return ERR_PTR(-ENOMEM); 188a22285a6SYan, Zheng 1890af3d00bSJosef Bacik if (type != TRANS_JOIN_NOLOCK) 190a22285a6SYan, Zheng mutex_lock(&root->fs_info->trans_mutex); 191a22285a6SYan, Zheng if (may_wait_transaction(root, type)) 19237d1aeeeSChris Mason wait_current_trans(root); 193a22285a6SYan, Zheng 19479154b1bSChris Mason ret = join_transaction(root); 19579154b1bSChris Mason BUG_ON(ret); 1960f7d52f4SChris Mason 197a22285a6SYan, Zheng cur_trans = root->fs_info->running_transaction; 198a22285a6SYan, Zheng cur_trans->use_count++; 1990af3d00bSJosef Bacik if (type != TRANS_JOIN_NOLOCK) 200a22285a6SYan, Zheng mutex_unlock(&root->fs_info->trans_mutex); 201a22285a6SYan, Zheng 202a22285a6SYan, Zheng h->transid = cur_trans->transid; 203a22285a6SYan, Zheng h->transaction = cur_trans; 20479154b1bSChris Mason h->blocks_used = 0; 205d2fb3437SYan Zheng h->block_group = 0; 206a22285a6SYan, Zheng h->bytes_reserved = 0; 20756bec294SChris Mason h->delayed_ref_updates = 0; 208f0486c68SYan, Zheng h->block_rsv = NULL; 209b7ec40d7SChris Mason 210a22285a6SYan, Zheng smp_mb(); 211a22285a6SYan, Zheng if (cur_trans->blocked && may_wait_transaction(root, type)) { 212a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 213a22285a6SYan, Zheng goto again; 214a22285a6SYan, Zheng } 2159ed74f2dSJosef Bacik 216a22285a6SYan, Zheng if (num_items > 0) { 2178bb8ab2eSJosef Bacik ret = btrfs_trans_reserve_metadata(h, root, num_items); 218a22285a6SYan, Zheng if (ret == -EAGAIN) { 219a22285a6SYan, Zheng btrfs_commit_transaction(h, root); 220a22285a6SYan, Zheng goto again; 221a22285a6SYan, Zheng } 222a22285a6SYan, Zheng if (ret < 0) { 223a22285a6SYan, Zheng btrfs_end_transaction(h, root); 224a22285a6SYan, Zheng return ERR_PTR(ret); 225a22285a6SYan, Zheng } 226a22285a6SYan, Zheng } 227a22285a6SYan, Zheng 2280af3d00bSJosef Bacik if (type != TRANS_JOIN_NOLOCK) 229a22285a6SYan, Zheng mutex_lock(&root->fs_info->trans_mutex); 2305d4f98a2SYan Zheng record_root_in_trans(h, root); 2310af3d00bSJosef Bacik if (type != TRANS_JOIN_NOLOCK) 23279154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 233a22285a6SYan, Zheng 234a22285a6SYan, Zheng if (!current->journal_info && type != TRANS_USERSPACE) 235a22285a6SYan, Zheng current->journal_info = h; 23679154b1bSChris Mason return h; 23779154b1bSChris Mason } 23879154b1bSChris Mason 239f9295749SChris Mason struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 240a22285a6SYan, Zheng int num_items) 241f9295749SChris Mason { 242a22285a6SYan, Zheng return start_transaction(root, num_items, TRANS_START); 243f9295749SChris Mason } 244f9295749SChris Mason struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 245f9295749SChris Mason int num_blocks) 246f9295749SChris Mason { 247a22285a6SYan, Zheng return start_transaction(root, 0, TRANS_JOIN); 248f9295749SChris Mason } 249f9295749SChris Mason 2500af3d00bSJosef Bacik struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root, 2510af3d00bSJosef Bacik int num_blocks) 2520af3d00bSJosef Bacik { 2530af3d00bSJosef Bacik return start_transaction(root, 0, TRANS_JOIN_NOLOCK); 2540af3d00bSJosef Bacik } 2550af3d00bSJosef Bacik 2569ca9ee09SSage Weil struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 2579ca9ee09SSage Weil int num_blocks) 2589ca9ee09SSage Weil { 259a22285a6SYan, Zheng return start_transaction(r, 0, TRANS_USERSPACE); 2609ca9ee09SSage Weil } 2619ca9ee09SSage Weil 262d352ac68SChris Mason /* wait for a transaction commit to be fully complete */ 26389ce8a63SChris Mason static noinline int wait_for_commit(struct btrfs_root *root, 26489ce8a63SChris Mason struct btrfs_transaction *commit) 26589ce8a63SChris Mason { 26689ce8a63SChris Mason DEFINE_WAIT(wait); 26789ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 26889ce8a63SChris Mason while (!commit->commit_done) { 26989ce8a63SChris Mason prepare_to_wait(&commit->commit_wait, &wait, 27089ce8a63SChris Mason TASK_UNINTERRUPTIBLE); 27189ce8a63SChris Mason if (commit->commit_done) 27289ce8a63SChris Mason break; 27389ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 27489ce8a63SChris Mason schedule(); 27589ce8a63SChris Mason mutex_lock(&root->fs_info->trans_mutex); 27689ce8a63SChris Mason } 27789ce8a63SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 27889ce8a63SChris Mason finish_wait(&commit->commit_wait, &wait); 27989ce8a63SChris Mason return 0; 28089ce8a63SChris Mason } 28189ce8a63SChris Mason 28246204592SSage Weil int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 28346204592SSage Weil { 28446204592SSage Weil struct btrfs_transaction *cur_trans = NULL, *t; 28546204592SSage Weil int ret; 28646204592SSage Weil 28746204592SSage Weil mutex_lock(&root->fs_info->trans_mutex); 28846204592SSage Weil 28946204592SSage Weil ret = 0; 29046204592SSage Weil if (transid) { 29146204592SSage Weil if (transid <= root->fs_info->last_trans_committed) 29246204592SSage Weil goto out_unlock; 29346204592SSage Weil 29446204592SSage Weil /* find specified transaction */ 29546204592SSage Weil list_for_each_entry(t, &root->fs_info->trans_list, list) { 29646204592SSage Weil if (t->transid == transid) { 29746204592SSage Weil cur_trans = t; 29846204592SSage Weil break; 29946204592SSage Weil } 30046204592SSage Weil if (t->transid > transid) 30146204592SSage Weil break; 30246204592SSage Weil } 30346204592SSage Weil ret = -EINVAL; 30446204592SSage Weil if (!cur_trans) 30546204592SSage Weil goto out_unlock; /* bad transid */ 30646204592SSage Weil } else { 30746204592SSage Weil /* find newest transaction that is committing | committed */ 30846204592SSage Weil list_for_each_entry_reverse(t, &root->fs_info->trans_list, 30946204592SSage Weil list) { 31046204592SSage Weil if (t->in_commit) { 31146204592SSage Weil if (t->commit_done) 31246204592SSage Weil goto out_unlock; 31346204592SSage Weil cur_trans = t; 31446204592SSage Weil break; 31546204592SSage Weil } 31646204592SSage Weil } 31746204592SSage Weil if (!cur_trans) 31846204592SSage Weil goto out_unlock; /* nothing committing|committed */ 31946204592SSage Weil } 32046204592SSage Weil 32146204592SSage Weil cur_trans->use_count++; 32246204592SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 32346204592SSage Weil 32446204592SSage Weil wait_for_commit(root, cur_trans); 32546204592SSage Weil 32646204592SSage Weil mutex_lock(&root->fs_info->trans_mutex); 32746204592SSage Weil put_transaction(cur_trans); 32846204592SSage Weil ret = 0; 32946204592SSage Weil out_unlock: 33046204592SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 33146204592SSage Weil return ret; 33246204592SSage Weil } 33346204592SSage Weil 3345d4f98a2SYan Zheng #if 0 335d352ac68SChris Mason /* 336d397712bSChris Mason * rate limit against the drop_snapshot code. This helps to slow down new 337d397712bSChris Mason * operations if the drop_snapshot code isn't able to keep up. 338d352ac68SChris Mason */ 33937d1aeeeSChris Mason static void throttle_on_drops(struct btrfs_root *root) 340ab78c84dSChris Mason { 341ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 3422dd3e67bSChris Mason int harder_count = 0; 343ab78c84dSChris Mason 3442dd3e67bSChris Mason harder: 345ab78c84dSChris Mason if (atomic_read(&info->throttles)) { 346ab78c84dSChris Mason DEFINE_WAIT(wait); 347ab78c84dSChris Mason int thr; 348ab78c84dSChris Mason thr = atomic_read(&info->throttle_gen); 349ab78c84dSChris Mason 350ab78c84dSChris Mason do { 351ab78c84dSChris Mason prepare_to_wait(&info->transaction_throttle, 352ab78c84dSChris Mason &wait, TASK_UNINTERRUPTIBLE); 353ab78c84dSChris Mason if (!atomic_read(&info->throttles)) { 354ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 355ab78c84dSChris Mason break; 356ab78c84dSChris Mason } 357ab78c84dSChris Mason schedule(); 358ab78c84dSChris Mason finish_wait(&info->transaction_throttle, &wait); 359ab78c84dSChris Mason } while (thr == atomic_read(&info->throttle_gen)); 3602dd3e67bSChris Mason harder_count++; 3612dd3e67bSChris Mason 3622dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 && 3632dd3e67bSChris Mason harder_count < 2) 3642dd3e67bSChris Mason goto harder; 3652dd3e67bSChris Mason 3662dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 && 3672dd3e67bSChris Mason harder_count < 10) 3682dd3e67bSChris Mason goto harder; 3692dd3e67bSChris Mason 3702dd3e67bSChris Mason if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 && 3712dd3e67bSChris Mason harder_count < 20) 3722dd3e67bSChris Mason goto harder; 373ab78c84dSChris Mason } 374ab78c84dSChris Mason } 3755d4f98a2SYan Zheng #endif 376ab78c84dSChris Mason 37737d1aeeeSChris Mason void btrfs_throttle(struct btrfs_root *root) 37837d1aeeeSChris Mason { 37937d1aeeeSChris Mason mutex_lock(&root->fs_info->trans_mutex); 3809ca9ee09SSage Weil if (!root->fs_info->open_ioctl_trans) 38137d1aeeeSChris Mason wait_current_trans(root); 38237d1aeeeSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 38337d1aeeeSChris Mason } 38437d1aeeeSChris Mason 3858929ecfaSYan, Zheng static int should_end_transaction(struct btrfs_trans_handle *trans, 3868929ecfaSYan, Zheng struct btrfs_root *root) 3878929ecfaSYan, Zheng { 3888929ecfaSYan, Zheng int ret; 3898929ecfaSYan, Zheng ret = btrfs_block_rsv_check(trans, root, 3908929ecfaSYan, Zheng &root->fs_info->global_block_rsv, 0, 5); 3918929ecfaSYan, Zheng return ret ? 1 : 0; 3928929ecfaSYan, Zheng } 3938929ecfaSYan, Zheng 3948929ecfaSYan, Zheng int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 3958929ecfaSYan, Zheng struct btrfs_root *root) 3968929ecfaSYan, Zheng { 3978929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 3988929ecfaSYan, Zheng int updates; 3998929ecfaSYan, Zheng 4008929ecfaSYan, Zheng if (cur_trans->blocked || cur_trans->delayed_refs.flushing) 4018929ecfaSYan, Zheng return 1; 4028929ecfaSYan, Zheng 4038929ecfaSYan, Zheng updates = trans->delayed_ref_updates; 4048929ecfaSYan, Zheng trans->delayed_ref_updates = 0; 4058929ecfaSYan, Zheng if (updates) 4068929ecfaSYan, Zheng btrfs_run_delayed_refs(trans, root, updates); 4078929ecfaSYan, Zheng 4088929ecfaSYan, Zheng return should_end_transaction(trans, root); 4098929ecfaSYan, Zheng } 4108929ecfaSYan, Zheng 41189ce8a63SChris Mason static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 4120af3d00bSJosef Bacik struct btrfs_root *root, int throttle, int lock) 41379154b1bSChris Mason { 4148929ecfaSYan, Zheng struct btrfs_transaction *cur_trans = trans->transaction; 415ab78c84dSChris Mason struct btrfs_fs_info *info = root->fs_info; 416c3e69d58SChris Mason int count = 0; 417d6e4a428SChris Mason 418c3e69d58SChris Mason while (count < 4) { 419c3e69d58SChris Mason unsigned long cur = trans->delayed_ref_updates; 420c3e69d58SChris Mason trans->delayed_ref_updates = 0; 421c3e69d58SChris Mason if (cur && 422c3e69d58SChris Mason trans->transaction->delayed_refs.num_heads_ready > 64) { 423c3e69d58SChris Mason trans->delayed_ref_updates = 0; 424b7ec40d7SChris Mason 425b7ec40d7SChris Mason /* 426b7ec40d7SChris Mason * do a full flush if the transaction is trying 427b7ec40d7SChris Mason * to close 428b7ec40d7SChris Mason */ 429b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) 430b7ec40d7SChris Mason cur = 0; 431c3e69d58SChris Mason btrfs_run_delayed_refs(trans, root, cur); 432c3e69d58SChris Mason } else { 433c3e69d58SChris Mason break; 434c3e69d58SChris Mason } 435c3e69d58SChris Mason count++; 43656bec294SChris Mason } 43756bec294SChris Mason 438a22285a6SYan, Zheng btrfs_trans_release_metadata(trans, root); 439a22285a6SYan, Zheng 4400af3d00bSJosef Bacik if (lock && !root->fs_info->open_ioctl_trans && 4418929ecfaSYan, Zheng should_end_transaction(trans, root)) 4428929ecfaSYan, Zheng trans->transaction->blocked = 1; 4438929ecfaSYan, Zheng 4440af3d00bSJosef Bacik if (lock && cur_trans->blocked && !cur_trans->in_commit) { 4458929ecfaSYan, Zheng if (throttle) 4468929ecfaSYan, Zheng return btrfs_commit_transaction(trans, root); 4478929ecfaSYan, Zheng else 4488929ecfaSYan, Zheng wake_up_process(info->transaction_kthread); 4498929ecfaSYan, Zheng } 4508929ecfaSYan, Zheng 4510af3d00bSJosef Bacik if (lock) 452ab78c84dSChris Mason mutex_lock(&info->trans_mutex); 4538929ecfaSYan, Zheng WARN_ON(cur_trans != info->running_transaction); 454d5719762SChris Mason WARN_ON(cur_trans->num_writers < 1); 455ccd467d6SChris Mason cur_trans->num_writers--; 45689ce8a63SChris Mason 45799d16cbcSSage Weil smp_mb(); 45879154b1bSChris Mason if (waitqueue_active(&cur_trans->writer_wait)) 45979154b1bSChris Mason wake_up(&cur_trans->writer_wait); 46079154b1bSChris Mason put_transaction(cur_trans); 4610af3d00bSJosef Bacik if (lock) 462ab78c84dSChris Mason mutex_unlock(&info->trans_mutex); 4639ed74f2dSJosef Bacik 4649ed74f2dSJosef Bacik if (current->journal_info == trans) 4659ed74f2dSJosef Bacik current->journal_info = NULL; 466d6025579SChris Mason memset(trans, 0, sizeof(*trans)); 4672c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 468ab78c84dSChris Mason 46924bbcf04SYan, Zheng if (throttle) 47024bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 47124bbcf04SYan, Zheng 47279154b1bSChris Mason return 0; 47379154b1bSChris Mason } 47479154b1bSChris Mason 47589ce8a63SChris Mason int btrfs_end_transaction(struct btrfs_trans_handle *trans, 47689ce8a63SChris Mason struct btrfs_root *root) 47789ce8a63SChris Mason { 4780af3d00bSJosef Bacik return __btrfs_end_transaction(trans, root, 0, 1); 47989ce8a63SChris Mason } 48089ce8a63SChris Mason 48189ce8a63SChris Mason int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans, 48289ce8a63SChris Mason struct btrfs_root *root) 48389ce8a63SChris Mason { 4840af3d00bSJosef Bacik return __btrfs_end_transaction(trans, root, 1, 1); 4850af3d00bSJosef Bacik } 4860af3d00bSJosef Bacik 4870af3d00bSJosef Bacik int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans, 4880af3d00bSJosef Bacik struct btrfs_root *root) 4890af3d00bSJosef Bacik { 4900af3d00bSJosef Bacik return __btrfs_end_transaction(trans, root, 0, 0); 49189ce8a63SChris Mason } 49289ce8a63SChris Mason 493d352ac68SChris Mason /* 494d352ac68SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 495d352ac68SChris Mason * them in one of two extent_io trees. This is used to make sure all of 496690587d1SChris Mason * those extents are sent to disk but does not wait on them 497d352ac68SChris Mason */ 498690587d1SChris Mason int btrfs_write_marked_extents(struct btrfs_root *root, 4998cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 50079154b1bSChris Mason { 5017c4452b9SChris Mason int ret; 502777e6bd7SChris Mason int err = 0; 5037c4452b9SChris Mason int werr = 0; 5047c4452b9SChris Mason struct page *page; 5057c4452b9SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 506777e6bd7SChris Mason u64 start = 0; 5075f39d397SChris Mason u64 end; 5085f39d397SChris Mason unsigned long index; 5097c4452b9SChris Mason 5107c4452b9SChris Mason while (1) { 511777e6bd7SChris Mason ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5128cef4e16SYan, Zheng mark); 5135f39d397SChris Mason if (ret) 5147c4452b9SChris Mason break; 5155f39d397SChris Mason while (start <= end) { 516777e6bd7SChris Mason cond_resched(); 517777e6bd7SChris Mason 5185f39d397SChris Mason index = start >> PAGE_CACHE_SHIFT; 51935ebb934SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 5204bef0848SChris Mason page = find_get_page(btree_inode->i_mapping, index); 5217c4452b9SChris Mason if (!page) 5227c4452b9SChris Mason continue; 5234bef0848SChris Mason 5244bef0848SChris Mason btree_lock_page_hook(page); 5254bef0848SChris Mason if (!page->mapping) { 5264bef0848SChris Mason unlock_page(page); 5274bef0848SChris Mason page_cache_release(page); 5284bef0848SChris Mason continue; 5294bef0848SChris Mason } 5304bef0848SChris Mason 5316702ed49SChris Mason if (PageWriteback(page)) { 5326702ed49SChris Mason if (PageDirty(page)) 5336702ed49SChris Mason wait_on_page_writeback(page); 5346702ed49SChris Mason else { 5356702ed49SChris Mason unlock_page(page); 5366702ed49SChris Mason page_cache_release(page); 5376702ed49SChris Mason continue; 5386702ed49SChris Mason } 5396702ed49SChris Mason } 5407c4452b9SChris Mason err = write_one_page(page, 0); 5417c4452b9SChris Mason if (err) 5427c4452b9SChris Mason werr = err; 5437c4452b9SChris Mason page_cache_release(page); 5447c4452b9SChris Mason } 5457c4452b9SChris Mason } 546690587d1SChris Mason if (err) 547690587d1SChris Mason werr = err; 548690587d1SChris Mason return werr; 549690587d1SChris Mason } 550690587d1SChris Mason 551690587d1SChris Mason /* 552690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 553690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 554690587d1SChris Mason * those extents are on disk for transaction or log commit. We wait 555690587d1SChris Mason * on all the pages and clear them from the dirty pages state tree 556690587d1SChris Mason */ 557690587d1SChris Mason int btrfs_wait_marked_extents(struct btrfs_root *root, 5588cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 559690587d1SChris Mason { 560690587d1SChris Mason int ret; 561690587d1SChris Mason int err = 0; 562690587d1SChris Mason int werr = 0; 563690587d1SChris Mason struct page *page; 564690587d1SChris Mason struct inode *btree_inode = root->fs_info->btree_inode; 565690587d1SChris Mason u64 start = 0; 566690587d1SChris Mason u64 end; 567690587d1SChris Mason unsigned long index; 568690587d1SChris Mason 569777e6bd7SChris Mason while (1) { 5708cef4e16SYan, Zheng ret = find_first_extent_bit(dirty_pages, start, &start, &end, 5718cef4e16SYan, Zheng mark); 572777e6bd7SChris Mason if (ret) 573777e6bd7SChris Mason break; 574777e6bd7SChris Mason 5758cef4e16SYan, Zheng clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS); 576777e6bd7SChris Mason while (start <= end) { 577777e6bd7SChris Mason index = start >> PAGE_CACHE_SHIFT; 578777e6bd7SChris Mason start = (u64)(index + 1) << PAGE_CACHE_SHIFT; 579777e6bd7SChris Mason page = find_get_page(btree_inode->i_mapping, index); 580777e6bd7SChris Mason if (!page) 581777e6bd7SChris Mason continue; 582777e6bd7SChris Mason if (PageDirty(page)) { 5834bef0848SChris Mason btree_lock_page_hook(page); 5844bef0848SChris Mason wait_on_page_writeback(page); 585777e6bd7SChris Mason err = write_one_page(page, 0); 586777e6bd7SChris Mason if (err) 587777e6bd7SChris Mason werr = err; 588777e6bd7SChris Mason } 589777e6bd7SChris Mason wait_on_page_writeback(page); 590777e6bd7SChris Mason page_cache_release(page); 591777e6bd7SChris Mason cond_resched(); 592777e6bd7SChris Mason } 593777e6bd7SChris Mason } 5947c4452b9SChris Mason if (err) 5957c4452b9SChris Mason werr = err; 5967c4452b9SChris Mason return werr; 59779154b1bSChris Mason } 59879154b1bSChris Mason 599690587d1SChris Mason /* 600690587d1SChris Mason * when btree blocks are allocated, they have some corresponding bits set for 601690587d1SChris Mason * them in one of two extent_io trees. This is used to make sure all of 602690587d1SChris Mason * those extents are on disk for transaction or log commit 603690587d1SChris Mason */ 604690587d1SChris Mason int btrfs_write_and_wait_marked_extents(struct btrfs_root *root, 6058cef4e16SYan, Zheng struct extent_io_tree *dirty_pages, int mark) 606690587d1SChris Mason { 607690587d1SChris Mason int ret; 608690587d1SChris Mason int ret2; 609690587d1SChris Mason 6108cef4e16SYan, Zheng ret = btrfs_write_marked_extents(root, dirty_pages, mark); 6118cef4e16SYan, Zheng ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 612690587d1SChris Mason return ret || ret2; 613690587d1SChris Mason } 614690587d1SChris Mason 615d0c803c4SChris Mason int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 616d0c803c4SChris Mason struct btrfs_root *root) 617d0c803c4SChris Mason { 618d0c803c4SChris Mason if (!trans || !trans->transaction) { 619d0c803c4SChris Mason struct inode *btree_inode; 620d0c803c4SChris Mason btree_inode = root->fs_info->btree_inode; 621d0c803c4SChris Mason return filemap_write_and_wait(btree_inode->i_mapping); 622d0c803c4SChris Mason } 623d0c803c4SChris Mason return btrfs_write_and_wait_marked_extents(root, 6248cef4e16SYan, Zheng &trans->transaction->dirty_pages, 6258cef4e16SYan, Zheng EXTENT_DIRTY); 626d0c803c4SChris Mason } 627d0c803c4SChris Mason 628d352ac68SChris Mason /* 629d352ac68SChris Mason * this is used to update the root pointer in the tree of tree roots. 630d352ac68SChris Mason * 631d352ac68SChris Mason * But, in the case of the extent allocation tree, updating the root 632d352ac68SChris Mason * pointer may allocate blocks which may change the root of the extent 633d352ac68SChris Mason * allocation tree. 634d352ac68SChris Mason * 635d352ac68SChris Mason * So, this loops and repeats and makes sure the cowonly root didn't 636d352ac68SChris Mason * change while the root pointer was being updated in the metadata. 637d352ac68SChris Mason */ 6380b86a832SChris Mason static int update_cowonly_root(struct btrfs_trans_handle *trans, 63979154b1bSChris Mason struct btrfs_root *root) 64079154b1bSChris Mason { 64179154b1bSChris Mason int ret; 6420b86a832SChris Mason u64 old_root_bytenr; 64386b9f2ecSYan, Zheng u64 old_root_used; 6440b86a832SChris Mason struct btrfs_root *tree_root = root->fs_info->tree_root; 64579154b1bSChris Mason 64686b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 6470b86a832SChris Mason btrfs_write_dirty_block_groups(trans, root); 64856bec294SChris Mason 64979154b1bSChris Mason while (1) { 6500b86a832SChris Mason old_root_bytenr = btrfs_root_bytenr(&root->root_item); 65186b9f2ecSYan, Zheng if (old_root_bytenr == root->node->start && 65286b9f2ecSYan, Zheng old_root_used == btrfs_root_used(&root->root_item)) 65379154b1bSChris Mason break; 65487ef2bb4SChris Mason 6555d4f98a2SYan Zheng btrfs_set_root_node(&root->root_item, root->node); 65679154b1bSChris Mason ret = btrfs_update_root(trans, tree_root, 6570b86a832SChris Mason &root->root_key, 6580b86a832SChris Mason &root->root_item); 65979154b1bSChris Mason BUG_ON(ret); 66056bec294SChris Mason 66186b9f2ecSYan, Zheng old_root_used = btrfs_root_used(&root->root_item); 6624a8c9a62SYan Zheng ret = btrfs_write_dirty_block_groups(trans, root); 66356bec294SChris Mason BUG_ON(ret); 6640b86a832SChris Mason } 665276e680dSYan Zheng 666276e680dSYan Zheng if (root != root->fs_info->extent_root) 667817d52f8SJosef Bacik switch_commit_root(root); 668276e680dSYan Zheng 6690b86a832SChris Mason return 0; 6700b86a832SChris Mason } 6710b86a832SChris Mason 672d352ac68SChris Mason /* 673d352ac68SChris Mason * update all the cowonly tree roots on disk 674d352ac68SChris Mason */ 6755d4f98a2SYan Zheng static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 6760b86a832SChris Mason struct btrfs_root *root) 6770b86a832SChris Mason { 6780b86a832SChris Mason struct btrfs_fs_info *fs_info = root->fs_info; 6790b86a832SChris Mason struct list_head *next; 68084234f3aSYan Zheng struct extent_buffer *eb; 68156bec294SChris Mason int ret; 68284234f3aSYan Zheng 68356bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 68456bec294SChris Mason BUG_ON(ret); 68587ef2bb4SChris Mason 68684234f3aSYan Zheng eb = btrfs_lock_root_node(fs_info->tree_root); 6879fa8cfe7SChris Mason btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb); 68884234f3aSYan Zheng btrfs_tree_unlock(eb); 68984234f3aSYan Zheng free_extent_buffer(eb); 6900b86a832SChris Mason 69156bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 69256bec294SChris Mason BUG_ON(ret); 69387ef2bb4SChris Mason 6940b86a832SChris Mason while (!list_empty(&fs_info->dirty_cowonly_roots)) { 6950b86a832SChris Mason next = fs_info->dirty_cowonly_roots.next; 6960b86a832SChris Mason list_del_init(next); 6970b86a832SChris Mason root = list_entry(next, struct btrfs_root, dirty_list); 69887ef2bb4SChris Mason 6990b86a832SChris Mason update_cowonly_root(trans, root); 70079154b1bSChris Mason } 701276e680dSYan Zheng 702276e680dSYan Zheng down_write(&fs_info->extent_commit_sem); 703276e680dSYan Zheng switch_commit_root(fs_info->extent_root); 704276e680dSYan Zheng up_write(&fs_info->extent_commit_sem); 705276e680dSYan Zheng 70679154b1bSChris Mason return 0; 70779154b1bSChris Mason } 70879154b1bSChris Mason 709d352ac68SChris Mason /* 710d352ac68SChris Mason * dead roots are old snapshots that need to be deleted. This allocates 711d352ac68SChris Mason * a dirty root struct and adds it into the list of dead roots that need to 712d352ac68SChris Mason * be deleted 713d352ac68SChris Mason */ 7145d4f98a2SYan Zheng int btrfs_add_dead_root(struct btrfs_root *root) 7155eda7b5eSChris Mason { 716b48652c1SYan Zheng mutex_lock(&root->fs_info->trans_mutex); 7175d4f98a2SYan Zheng list_add(&root->root_list, &root->fs_info->dead_roots); 718b48652c1SYan Zheng mutex_unlock(&root->fs_info->trans_mutex); 7195eda7b5eSChris Mason return 0; 7205eda7b5eSChris Mason } 7215eda7b5eSChris Mason 722d352ac68SChris Mason /* 7235d4f98a2SYan Zheng * update all the cowonly tree roots on disk 724d352ac68SChris Mason */ 7255d4f98a2SYan Zheng static noinline int commit_fs_roots(struct btrfs_trans_handle *trans, 7265d4f98a2SYan Zheng struct btrfs_root *root) 7270f7d52f4SChris Mason { 7280f7d52f4SChris Mason struct btrfs_root *gang[8]; 7295d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 7300f7d52f4SChris Mason int i; 7310f7d52f4SChris Mason int ret; 73254aa1f4dSChris Mason int err = 0; 73354aa1f4dSChris Mason 7340f7d52f4SChris Mason while (1) { 7355d4f98a2SYan Zheng ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 7365d4f98a2SYan Zheng (void **)gang, 0, 7370f7d52f4SChris Mason ARRAY_SIZE(gang), 7380f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 7390f7d52f4SChris Mason if (ret == 0) 7400f7d52f4SChris Mason break; 7410f7d52f4SChris Mason for (i = 0; i < ret; i++) { 7420f7d52f4SChris Mason root = gang[i]; 7435d4f98a2SYan Zheng radix_tree_tag_clear(&fs_info->fs_roots_radix, 7442619ba1fSChris Mason (unsigned long)root->root_key.objectid, 7450f7d52f4SChris Mason BTRFS_ROOT_TRANS_TAG); 74631153d81SYan Zheng 747e02119d5SChris Mason btrfs_free_log(trans, root); 7485d4f98a2SYan Zheng btrfs_update_reloc_root(trans, root); 749d68fc57bSYan, Zheng btrfs_orphan_commit_root(trans, root); 750e02119d5SChris Mason 751978d910dSYan Zheng if (root->commit_root != root->node) { 752817d52f8SJosef Bacik switch_commit_root(root); 753978d910dSYan Zheng btrfs_set_root_node(&root->root_item, 754978d910dSYan Zheng root->node); 755978d910dSYan Zheng } 75631153d81SYan Zheng 7575d4f98a2SYan Zheng err = btrfs_update_root(trans, fs_info->tree_root, 7580f7d52f4SChris Mason &root->root_key, 7590f7d52f4SChris Mason &root->root_item); 76054aa1f4dSChris Mason if (err) 76154aa1f4dSChris Mason break; 7620f7d52f4SChris Mason } 7639f3a7427SChris Mason } 76454aa1f4dSChris Mason return err; 7650f7d52f4SChris Mason } 7660f7d52f4SChris Mason 767d352ac68SChris Mason /* 768d352ac68SChris Mason * defrag a given btree. If cacheonly == 1, this won't read from the disk, 769d352ac68SChris Mason * otherwise every leaf in the btree is read and defragged. 770d352ac68SChris Mason */ 771e9d0b13bSChris Mason int btrfs_defrag_root(struct btrfs_root *root, int cacheonly) 772e9d0b13bSChris Mason { 773e9d0b13bSChris Mason struct btrfs_fs_info *info = root->fs_info; 774e9d0b13bSChris Mason struct btrfs_trans_handle *trans; 7758929ecfaSYan, Zheng int ret; 776d3c2fdcfSChris Mason unsigned long nr; 777e9d0b13bSChris Mason 7788929ecfaSYan, Zheng if (xchg(&root->defrag_running, 1)) 779e9d0b13bSChris Mason return 0; 7808929ecfaSYan, Zheng 7816b80053dSChris Mason while (1) { 7828929ecfaSYan, Zheng trans = btrfs_start_transaction(root, 0); 7838929ecfaSYan, Zheng if (IS_ERR(trans)) 7848929ecfaSYan, Zheng return PTR_ERR(trans); 7858929ecfaSYan, Zheng 786e9d0b13bSChris Mason ret = btrfs_defrag_leaves(trans, root, cacheonly); 7878929ecfaSYan, Zheng 788d3c2fdcfSChris Mason nr = trans->blocks_used; 789e9d0b13bSChris Mason btrfs_end_transaction(trans, root); 790d3c2fdcfSChris Mason btrfs_btree_balance_dirty(info->tree_root, nr); 791e9d0b13bSChris Mason cond_resched(); 792e9d0b13bSChris Mason 7933f157a2fSChris Mason if (root->fs_info->closing || ret != -EAGAIN) 794e9d0b13bSChris Mason break; 795e9d0b13bSChris Mason } 796e9d0b13bSChris Mason root->defrag_running = 0; 7978929ecfaSYan, Zheng return ret; 798e9d0b13bSChris Mason } 799e9d0b13bSChris Mason 8002c47e605SYan Zheng #if 0 801d352ac68SChris Mason /* 802b7ec40d7SChris Mason * when dropping snapshots, we generate a ton of delayed refs, and it makes 803b7ec40d7SChris Mason * sense not to join the transaction while it is trying to flush the current 804b7ec40d7SChris Mason * queue of delayed refs out. 805b7ec40d7SChris Mason * 806b7ec40d7SChris Mason * This is used by the drop snapshot code only 807b7ec40d7SChris Mason */ 808b7ec40d7SChris Mason static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info) 809b7ec40d7SChris Mason { 810b7ec40d7SChris Mason DEFINE_WAIT(wait); 811b7ec40d7SChris Mason 812b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 813b7ec40d7SChris Mason while (info->running_transaction && 814b7ec40d7SChris Mason info->running_transaction->delayed_refs.flushing) { 815b7ec40d7SChris Mason prepare_to_wait(&info->transaction_wait, &wait, 816b7ec40d7SChris Mason TASK_UNINTERRUPTIBLE); 817b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 81859bc5c75SChris Mason 819b7ec40d7SChris Mason schedule(); 82059bc5c75SChris Mason 821b7ec40d7SChris Mason mutex_lock(&info->trans_mutex); 822b7ec40d7SChris Mason finish_wait(&info->transaction_wait, &wait); 823b7ec40d7SChris Mason } 824b7ec40d7SChris Mason mutex_unlock(&info->trans_mutex); 825b7ec40d7SChris Mason return 0; 826b7ec40d7SChris Mason } 827b7ec40d7SChris Mason 828b7ec40d7SChris Mason /* 829d352ac68SChris Mason * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on 830d352ac68SChris Mason * all of them 831d352ac68SChris Mason */ 8325d4f98a2SYan Zheng int btrfs_drop_dead_root(struct btrfs_root *root) 8330f7d52f4SChris Mason { 8340f7d52f4SChris Mason struct btrfs_trans_handle *trans; 8355d4f98a2SYan Zheng struct btrfs_root *tree_root = root->fs_info->tree_root; 836d3c2fdcfSChris Mason unsigned long nr; 8375d4f98a2SYan Zheng int ret; 83858176a96SJosef Bacik 8399f3a7427SChris Mason while (1) { 840b7ec40d7SChris Mason /* 841b7ec40d7SChris Mason * we don't want to jump in and create a bunch of 842b7ec40d7SChris Mason * delayed refs if the transaction is starting to close 843b7ec40d7SChris Mason */ 844b7ec40d7SChris Mason wait_transaction_pre_flush(tree_root->fs_info); 8450f7d52f4SChris Mason trans = btrfs_start_transaction(tree_root, 1); 846b7ec40d7SChris Mason 847b7ec40d7SChris Mason /* 848b7ec40d7SChris Mason * we've joined a transaction, make sure it isn't 849b7ec40d7SChris Mason * closing right now 850b7ec40d7SChris Mason */ 851b7ec40d7SChris Mason if (trans->transaction->delayed_refs.flushing) { 852b7ec40d7SChris Mason btrfs_end_transaction(trans, tree_root); 853b7ec40d7SChris Mason continue; 854b7ec40d7SChris Mason } 855b7ec40d7SChris Mason 8565d4f98a2SYan Zheng ret = btrfs_drop_snapshot(trans, root); 857d397712bSChris Mason if (ret != -EAGAIN) 8589f3a7427SChris Mason break; 85958176a96SJosef Bacik 8605d4f98a2SYan Zheng ret = btrfs_update_root(trans, tree_root, 8615d4f98a2SYan Zheng &root->root_key, 8625d4f98a2SYan Zheng &root->root_item); 8635d4f98a2SYan Zheng if (ret) 86454aa1f4dSChris Mason break; 865bcc63abbSYan 866d3c2fdcfSChris Mason nr = trans->blocks_used; 8670f7d52f4SChris Mason ret = btrfs_end_transaction(trans, tree_root); 8680f7d52f4SChris Mason BUG_ON(ret); 8695eda7b5eSChris Mason 870d3c2fdcfSChris Mason btrfs_btree_balance_dirty(tree_root, nr); 8714dc11904SChris Mason cond_resched(); 8720f7d52f4SChris Mason } 8735d4f98a2SYan Zheng BUG_ON(ret); 8745d4f98a2SYan Zheng 8755d4f98a2SYan Zheng ret = btrfs_del_root(trans, tree_root, &root->root_key); 8765d4f98a2SYan Zheng BUG_ON(ret); 8775d4f98a2SYan Zheng 8785d4f98a2SYan Zheng nr = trans->blocks_used; 8795d4f98a2SYan Zheng ret = btrfs_end_transaction(trans, tree_root); 8805d4f98a2SYan Zheng BUG_ON(ret); 8815d4f98a2SYan Zheng 8825d4f98a2SYan Zheng free_extent_buffer(root->node); 8835d4f98a2SYan Zheng free_extent_buffer(root->commit_root); 8845d4f98a2SYan Zheng kfree(root); 8855d4f98a2SYan Zheng 8865d4f98a2SYan Zheng btrfs_btree_balance_dirty(tree_root, nr); 88754aa1f4dSChris Mason return ret; 8880f7d52f4SChris Mason } 8892c47e605SYan Zheng #endif 8900f7d52f4SChris Mason 891d352ac68SChris Mason /* 892d352ac68SChris Mason * new snapshots need to be created at a very specific time in the 893d352ac68SChris Mason * transaction commit. This does the actual creation 894d352ac68SChris Mason */ 89580b6794dSChris Mason static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, 8963063d29fSChris Mason struct btrfs_fs_info *fs_info, 8973063d29fSChris Mason struct btrfs_pending_snapshot *pending) 8983063d29fSChris Mason { 8993063d29fSChris Mason struct btrfs_key key; 90080b6794dSChris Mason struct btrfs_root_item *new_root_item; 9013063d29fSChris Mason struct btrfs_root *tree_root = fs_info->tree_root; 9023063d29fSChris Mason struct btrfs_root *root = pending->root; 9036bdb72deSSage Weil struct btrfs_root *parent_root; 9046bdb72deSSage Weil struct inode *parent_inode; 905*6a912213SJosef Bacik struct dentry *parent; 906a22285a6SYan, Zheng struct dentry *dentry; 9073063d29fSChris Mason struct extent_buffer *tmp; 908925baeddSChris Mason struct extent_buffer *old; 9093063d29fSChris Mason int ret; 910d68fc57bSYan, Zheng u64 to_reserve = 0; 9116bdb72deSSage Weil u64 index = 0; 912a22285a6SYan, Zheng u64 objectid; 9133063d29fSChris Mason 91480b6794dSChris Mason new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 91580b6794dSChris Mason if (!new_root_item) { 916a22285a6SYan, Zheng pending->error = -ENOMEM; 91780b6794dSChris Mason goto fail; 91880b6794dSChris Mason } 919a22285a6SYan, Zheng 9203063d29fSChris Mason ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 921a22285a6SYan, Zheng if (ret) { 922a22285a6SYan, Zheng pending->error = ret; 9233063d29fSChris Mason goto fail; 924a22285a6SYan, Zheng } 9253063d29fSChris Mason 9263fd0a558SYan, Zheng btrfs_reloc_pre_snapshot(trans, pending, &to_reserve); 927d68fc57bSYan, Zheng btrfs_orphan_pre_snapshot(trans, pending, &to_reserve); 928d68fc57bSYan, Zheng 929d68fc57bSYan, Zheng if (to_reserve > 0) { 930d68fc57bSYan, Zheng ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv, 9318bb8ab2eSJosef Bacik to_reserve); 932d68fc57bSYan, Zheng if (ret) { 933d68fc57bSYan, Zheng pending->error = ret; 934d68fc57bSYan, Zheng goto fail; 935d68fc57bSYan, Zheng } 936d68fc57bSYan, Zheng } 937d68fc57bSYan, Zheng 9383063d29fSChris Mason key.objectid = objectid; 939a22285a6SYan, Zheng key.offset = (u64)-1; 940a22285a6SYan, Zheng key.type = BTRFS_ROOT_ITEM_KEY; 9413063d29fSChris Mason 942a22285a6SYan, Zheng trans->block_rsv = &pending->block_rsv; 9436bdb72deSSage Weil 944a22285a6SYan, Zheng dentry = pending->dentry; 945*6a912213SJosef Bacik parent = dget_parent(dentry); 946*6a912213SJosef Bacik parent_inode = parent->d_inode; 947a22285a6SYan, Zheng parent_root = BTRFS_I(parent_inode)->root; 9486bdb72deSSage Weil record_root_in_trans(trans, parent_root); 949a22285a6SYan, Zheng 9506bdb72deSSage Weil /* 9516bdb72deSSage Weil * insert the directory item 9526bdb72deSSage Weil */ 9536bdb72deSSage Weil ret = btrfs_set_inode_index(parent_inode, &index); 9546bdb72deSSage Weil BUG_ON(ret); 9556bdb72deSSage Weil ret = btrfs_insert_dir_item(trans, parent_root, 956a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len, 957a22285a6SYan, Zheng parent_inode->i_ino, &key, 958a22285a6SYan, Zheng BTRFS_FT_DIR, index); 9596bdb72deSSage Weil BUG_ON(ret); 9606bdb72deSSage Weil 961a22285a6SYan, Zheng btrfs_i_size_write(parent_inode, parent_inode->i_size + 962a22285a6SYan, Zheng dentry->d_name.len * 2); 9636bdb72deSSage Weil ret = btrfs_update_inode(trans, parent_root, parent_inode); 9646bdb72deSSage Weil BUG_ON(ret); 9656bdb72deSSage Weil 9666bdb72deSSage Weil record_root_in_trans(trans, root); 9676bdb72deSSage Weil btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 9686bdb72deSSage Weil memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 9696bdb72deSSage Weil 970925baeddSChris Mason old = btrfs_lock_root_node(root); 9719fa8cfe7SChris Mason btrfs_cow_block(trans, root, old, NULL, 0, &old); 9725d4f98a2SYan Zheng btrfs_set_lock_blocking(old); 9733063d29fSChris Mason 974925baeddSChris Mason btrfs_copy_root(trans, root, old, &tmp, objectid); 975925baeddSChris Mason btrfs_tree_unlock(old); 976925baeddSChris Mason free_extent_buffer(old); 9773063d29fSChris Mason 9785d4f98a2SYan Zheng btrfs_set_root_node(new_root_item, tmp); 979a22285a6SYan, Zheng /* record when the snapshot was created in key.offset */ 980a22285a6SYan, Zheng key.offset = trans->transid; 981a22285a6SYan, Zheng ret = btrfs_insert_root(trans, tree_root, &key, new_root_item); 982925baeddSChris Mason btrfs_tree_unlock(tmp); 9833063d29fSChris Mason free_extent_buffer(tmp); 9840660b5afSChris Mason BUG_ON(ret); 9850660b5afSChris Mason 986a22285a6SYan, Zheng /* 987a22285a6SYan, Zheng * insert root back/forward references 988a22285a6SYan, Zheng */ 989a22285a6SYan, Zheng ret = btrfs_add_root_ref(trans, tree_root, objectid, 990a22285a6SYan, Zheng parent_root->root_key.objectid, 991a22285a6SYan, Zheng parent_inode->i_ino, index, 992a22285a6SYan, Zheng dentry->d_name.name, dentry->d_name.len); 993a22285a6SYan, Zheng BUG_ON(ret); 994*6a912213SJosef Bacik dput(parent); 995a22285a6SYan, Zheng 996a22285a6SYan, Zheng key.offset = (u64)-1; 997a22285a6SYan, Zheng pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 998a22285a6SYan, Zheng BUG_ON(IS_ERR(pending->snap)); 999d68fc57bSYan, Zheng 10003fd0a558SYan, Zheng btrfs_reloc_post_snapshot(trans, pending); 1001d68fc57bSYan, Zheng btrfs_orphan_post_snapshot(trans, pending); 10023063d29fSChris Mason fail: 10036bdb72deSSage Weil kfree(new_root_item); 1004a22285a6SYan, Zheng btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); 1005a22285a6SYan, Zheng return 0; 10063063d29fSChris Mason } 10073063d29fSChris Mason 1008d352ac68SChris Mason /* 1009d352ac68SChris Mason * create all the snapshots we've scheduled for creation 1010d352ac68SChris Mason */ 101180b6794dSChris Mason static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans, 10123063d29fSChris Mason struct btrfs_fs_info *fs_info) 10133063d29fSChris Mason { 10143063d29fSChris Mason struct btrfs_pending_snapshot *pending; 10153063d29fSChris Mason struct list_head *head = &trans->transaction->pending_snapshots; 10163de4586cSChris Mason int ret; 10173de4586cSChris Mason 1018c6e30871SQinghuang Feng list_for_each_entry(pending, head, list) { 10193de4586cSChris Mason ret = create_pending_snapshot(trans, fs_info, pending); 10203de4586cSChris Mason BUG_ON(ret); 10213de4586cSChris Mason } 10223de4586cSChris Mason return 0; 10233de4586cSChris Mason } 10243de4586cSChris Mason 10255d4f98a2SYan Zheng static void update_super_roots(struct btrfs_root *root) 10265d4f98a2SYan Zheng { 10275d4f98a2SYan Zheng struct btrfs_root_item *root_item; 10285d4f98a2SYan Zheng struct btrfs_super_block *super; 10295d4f98a2SYan Zheng 10305d4f98a2SYan Zheng super = &root->fs_info->super_copy; 10315d4f98a2SYan Zheng 10325d4f98a2SYan Zheng root_item = &root->fs_info->chunk_root->root_item; 10335d4f98a2SYan Zheng super->chunk_root = root_item->bytenr; 10345d4f98a2SYan Zheng super->chunk_root_generation = root_item->generation; 10355d4f98a2SYan Zheng super->chunk_root_level = root_item->level; 10365d4f98a2SYan Zheng 10375d4f98a2SYan Zheng root_item = &root->fs_info->tree_root->root_item; 10385d4f98a2SYan Zheng super->root = root_item->bytenr; 10395d4f98a2SYan Zheng super->generation = root_item->generation; 10405d4f98a2SYan Zheng super->root_level = root_item->level; 10410af3d00bSJosef Bacik if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE)) 10420af3d00bSJosef Bacik super->cache_generation = root_item->generation; 10435d4f98a2SYan Zheng } 10445d4f98a2SYan Zheng 1045f36f3042SChris Mason int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 1046f36f3042SChris Mason { 1047f36f3042SChris Mason int ret = 0; 1048f36f3042SChris Mason spin_lock(&info->new_trans_lock); 1049f36f3042SChris Mason if (info->running_transaction) 1050f36f3042SChris Mason ret = info->running_transaction->in_commit; 1051f36f3042SChris Mason spin_unlock(&info->new_trans_lock); 1052f36f3042SChris Mason return ret; 1053f36f3042SChris Mason } 1054f36f3042SChris Mason 10558929ecfaSYan, Zheng int btrfs_transaction_blocked(struct btrfs_fs_info *info) 10568929ecfaSYan, Zheng { 10578929ecfaSYan, Zheng int ret = 0; 10588929ecfaSYan, Zheng spin_lock(&info->new_trans_lock); 10598929ecfaSYan, Zheng if (info->running_transaction) 10608929ecfaSYan, Zheng ret = info->running_transaction->blocked; 10618929ecfaSYan, Zheng spin_unlock(&info->new_trans_lock); 10628929ecfaSYan, Zheng return ret; 10638929ecfaSYan, Zheng } 10648929ecfaSYan, Zheng 1065bb9c12c9SSage Weil /* 1066bb9c12c9SSage Weil * wait for the current transaction commit to start and block subsequent 1067bb9c12c9SSage Weil * transaction joins 1068bb9c12c9SSage Weil */ 1069bb9c12c9SSage Weil static void wait_current_trans_commit_start(struct btrfs_root *root, 1070bb9c12c9SSage Weil struct btrfs_transaction *trans) 1071bb9c12c9SSage Weil { 1072bb9c12c9SSage Weil DEFINE_WAIT(wait); 1073bb9c12c9SSage Weil 1074bb9c12c9SSage Weil if (trans->in_commit) 1075bb9c12c9SSage Weil return; 1076bb9c12c9SSage Weil 1077bb9c12c9SSage Weil while (1) { 1078bb9c12c9SSage Weil prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait, 1079bb9c12c9SSage Weil TASK_UNINTERRUPTIBLE); 1080bb9c12c9SSage Weil if (trans->in_commit) { 1081bb9c12c9SSage Weil finish_wait(&root->fs_info->transaction_blocked_wait, 1082bb9c12c9SSage Weil &wait); 1083bb9c12c9SSage Weil break; 1084bb9c12c9SSage Weil } 1085bb9c12c9SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 1086bb9c12c9SSage Weil schedule(); 1087bb9c12c9SSage Weil mutex_lock(&root->fs_info->trans_mutex); 1088bb9c12c9SSage Weil finish_wait(&root->fs_info->transaction_blocked_wait, &wait); 1089bb9c12c9SSage Weil } 1090bb9c12c9SSage Weil } 1091bb9c12c9SSage Weil 1092bb9c12c9SSage Weil /* 1093bb9c12c9SSage Weil * wait for the current transaction to start and then become unblocked. 1094bb9c12c9SSage Weil * caller holds ref. 1095bb9c12c9SSage Weil */ 1096bb9c12c9SSage Weil static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1097bb9c12c9SSage Weil struct btrfs_transaction *trans) 1098bb9c12c9SSage Weil { 1099bb9c12c9SSage Weil DEFINE_WAIT(wait); 1100bb9c12c9SSage Weil 1101bb9c12c9SSage Weil if (trans->commit_done || (trans->in_commit && !trans->blocked)) 1102bb9c12c9SSage Weil return; 1103bb9c12c9SSage Weil 1104bb9c12c9SSage Weil while (1) { 1105bb9c12c9SSage Weil prepare_to_wait(&root->fs_info->transaction_wait, &wait, 1106bb9c12c9SSage Weil TASK_UNINTERRUPTIBLE); 1107bb9c12c9SSage Weil if (trans->commit_done || 1108bb9c12c9SSage Weil (trans->in_commit && !trans->blocked)) { 1109bb9c12c9SSage Weil finish_wait(&root->fs_info->transaction_wait, 1110bb9c12c9SSage Weil &wait); 1111bb9c12c9SSage Weil break; 1112bb9c12c9SSage Weil } 1113bb9c12c9SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 1114bb9c12c9SSage Weil schedule(); 1115bb9c12c9SSage Weil mutex_lock(&root->fs_info->trans_mutex); 1116bb9c12c9SSage Weil finish_wait(&root->fs_info->transaction_wait, 1117bb9c12c9SSage Weil &wait); 1118bb9c12c9SSage Weil } 1119bb9c12c9SSage Weil } 1120bb9c12c9SSage Weil 1121bb9c12c9SSage Weil /* 1122bb9c12c9SSage Weil * commit transactions asynchronously. once btrfs_commit_transaction_async 1123bb9c12c9SSage Weil * returns, any subsequent transaction will not be allowed to join. 1124bb9c12c9SSage Weil */ 1125bb9c12c9SSage Weil struct btrfs_async_commit { 1126bb9c12c9SSage Weil struct btrfs_trans_handle *newtrans; 1127bb9c12c9SSage Weil struct btrfs_root *root; 1128bb9c12c9SSage Weil struct delayed_work work; 1129bb9c12c9SSage Weil }; 1130bb9c12c9SSage Weil 1131bb9c12c9SSage Weil static void do_async_commit(struct work_struct *work) 1132bb9c12c9SSage Weil { 1133bb9c12c9SSage Weil struct btrfs_async_commit *ac = 1134bb9c12c9SSage Weil container_of(work, struct btrfs_async_commit, work.work); 1135bb9c12c9SSage Weil 1136bb9c12c9SSage Weil btrfs_commit_transaction(ac->newtrans, ac->root); 1137bb9c12c9SSage Weil kfree(ac); 1138bb9c12c9SSage Weil } 1139bb9c12c9SSage Weil 1140bb9c12c9SSage Weil int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans, 1141bb9c12c9SSage Weil struct btrfs_root *root, 1142bb9c12c9SSage Weil int wait_for_unblock) 1143bb9c12c9SSage Weil { 1144bb9c12c9SSage Weil struct btrfs_async_commit *ac; 1145bb9c12c9SSage Weil struct btrfs_transaction *cur_trans; 1146bb9c12c9SSage Weil 1147bb9c12c9SSage Weil ac = kmalloc(sizeof(*ac), GFP_NOFS); 1148bb9c12c9SSage Weil BUG_ON(!ac); 1149bb9c12c9SSage Weil 1150bb9c12c9SSage Weil INIT_DELAYED_WORK(&ac->work, do_async_commit); 1151bb9c12c9SSage Weil ac->root = root; 1152bb9c12c9SSage Weil ac->newtrans = btrfs_join_transaction(root, 0); 1153bb9c12c9SSage Weil 1154bb9c12c9SSage Weil /* take transaction reference */ 1155bb9c12c9SSage Weil mutex_lock(&root->fs_info->trans_mutex); 1156bb9c12c9SSage Weil cur_trans = trans->transaction; 1157bb9c12c9SSage Weil cur_trans->use_count++; 1158bb9c12c9SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 1159bb9c12c9SSage Weil 1160bb9c12c9SSage Weil btrfs_end_transaction(trans, root); 1161bb9c12c9SSage Weil schedule_delayed_work(&ac->work, 0); 1162bb9c12c9SSage Weil 1163bb9c12c9SSage Weil /* wait for transaction to start and unblock */ 1164bb9c12c9SSage Weil mutex_lock(&root->fs_info->trans_mutex); 1165bb9c12c9SSage Weil if (wait_for_unblock) 1166bb9c12c9SSage Weil wait_current_trans_commit_start_and_unblock(root, cur_trans); 1167bb9c12c9SSage Weil else 1168bb9c12c9SSage Weil wait_current_trans_commit_start(root, cur_trans); 1169bb9c12c9SSage Weil put_transaction(cur_trans); 1170bb9c12c9SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 1171bb9c12c9SSage Weil 1172bb9c12c9SSage Weil return 0; 1173bb9c12c9SSage Weil } 1174bb9c12c9SSage Weil 1175bb9c12c9SSage Weil /* 1176bb9c12c9SSage Weil * btrfs_transaction state sequence: 1177bb9c12c9SSage Weil * in_commit = 0, blocked = 0 (initial) 1178bb9c12c9SSage Weil * in_commit = 1, blocked = 1 1179bb9c12c9SSage Weil * blocked = 0 1180bb9c12c9SSage Weil * commit_done = 1 1181bb9c12c9SSage Weil */ 118279154b1bSChris Mason int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 118379154b1bSChris Mason struct btrfs_root *root) 118479154b1bSChris Mason { 118515ee9bc7SJosef Bacik unsigned long joined = 0; 118679154b1bSChris Mason struct btrfs_transaction *cur_trans; 11878fd17795SChris Mason struct btrfs_transaction *prev_trans = NULL; 118879154b1bSChris Mason DEFINE_WAIT(wait); 118915ee9bc7SJosef Bacik int ret; 119089573b9cSChris Mason int should_grow = 0; 119189573b9cSChris Mason unsigned long now = get_seconds(); 1192dccae999SSage Weil int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT); 119379154b1bSChris Mason 11945a3f23d5SChris Mason btrfs_run_ordered_operations(root, 0); 11955a3f23d5SChris Mason 119656bec294SChris Mason /* make a pass through all the delayed refs we have so far 119756bec294SChris Mason * any runnings procs may add more while we are here 119856bec294SChris Mason */ 119956bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 120056bec294SChris Mason BUG_ON(ret); 120156bec294SChris Mason 1202a22285a6SYan, Zheng btrfs_trans_release_metadata(trans, root); 1203a22285a6SYan, Zheng 1204b7ec40d7SChris Mason cur_trans = trans->transaction; 120556bec294SChris Mason /* 120656bec294SChris Mason * set the flushing flag so procs in this transaction have to 120756bec294SChris Mason * start sending their work down. 120856bec294SChris Mason */ 1209b7ec40d7SChris Mason cur_trans->delayed_refs.flushing = 1; 121056bec294SChris Mason 1211c3e69d58SChris Mason ret = btrfs_run_delayed_refs(trans, root, 0); 121256bec294SChris Mason BUG_ON(ret); 121356bec294SChris Mason 121479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 1215b7ec40d7SChris Mason if (cur_trans->in_commit) { 1216b7ec40d7SChris Mason cur_trans->use_count++; 1217ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 121879154b1bSChris Mason btrfs_end_transaction(trans, root); 1219ccd467d6SChris Mason 122079154b1bSChris Mason ret = wait_for_commit(root, cur_trans); 122179154b1bSChris Mason BUG_ON(ret); 122215ee9bc7SJosef Bacik 122315ee9bc7SJosef Bacik mutex_lock(&root->fs_info->trans_mutex); 122479154b1bSChris Mason put_transaction(cur_trans); 122515ee9bc7SJosef Bacik mutex_unlock(&root->fs_info->trans_mutex); 122615ee9bc7SJosef Bacik 122779154b1bSChris Mason return 0; 122879154b1bSChris Mason } 12294313b399SChris Mason 12302c90e5d6SChris Mason trans->transaction->in_commit = 1; 1231f9295749SChris Mason trans->transaction->blocked = 1; 1232bb9c12c9SSage Weil wake_up(&root->fs_info->transaction_blocked_wait); 1233bb9c12c9SSage Weil 1234ccd467d6SChris Mason if (cur_trans->list.prev != &root->fs_info->trans_list) { 1235ccd467d6SChris Mason prev_trans = list_entry(cur_trans->list.prev, 1236ccd467d6SChris Mason struct btrfs_transaction, list); 1237ccd467d6SChris Mason if (!prev_trans->commit_done) { 1238ccd467d6SChris Mason prev_trans->use_count++; 1239ccd467d6SChris Mason mutex_unlock(&root->fs_info->trans_mutex); 1240ccd467d6SChris Mason 1241ccd467d6SChris Mason wait_for_commit(root, prev_trans); 1242ccd467d6SChris Mason 1243ccd467d6SChris Mason mutex_lock(&root->fs_info->trans_mutex); 124415ee9bc7SJosef Bacik put_transaction(prev_trans); 1245ccd467d6SChris Mason } 1246ccd467d6SChris Mason } 124715ee9bc7SJosef Bacik 124889573b9cSChris Mason if (now < cur_trans->start_time || now - cur_trans->start_time < 1) 124989573b9cSChris Mason should_grow = 1; 125089573b9cSChris Mason 125115ee9bc7SJosef Bacik do { 12527ea394f1SYan Zheng int snap_pending = 0; 125315ee9bc7SJosef Bacik joined = cur_trans->num_joined; 12547ea394f1SYan Zheng if (!list_empty(&trans->transaction->pending_snapshots)) 12557ea394f1SYan Zheng snap_pending = 1; 12567ea394f1SYan Zheng 12572c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 125879154b1bSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 125915ee9bc7SJosef Bacik 12600bdb1db2SSage Weil if (flush_on_commit || snap_pending) { 126124bbcf04SYan, Zheng btrfs_start_delalloc_inodes(root, 1); 126224bbcf04SYan, Zheng ret = btrfs_wait_ordered_extents(root, 0, 1); 1263ebecd3d9SSage Weil BUG_ON(ret); 12647ea394f1SYan Zheng } 12657ea394f1SYan Zheng 12665a3f23d5SChris Mason /* 12675a3f23d5SChris Mason * rename don't use btrfs_join_transaction, so, once we 12685a3f23d5SChris Mason * set the transaction to blocked above, we aren't going 12695a3f23d5SChris Mason * to get any new ordered operations. We can safely run 12705a3f23d5SChris Mason * it here and no for sure that nothing new will be added 12715a3f23d5SChris Mason * to the list 12725a3f23d5SChris Mason */ 12735a3f23d5SChris Mason btrfs_run_ordered_operations(root, 1); 12745a3f23d5SChris Mason 1275ed3b3d31SChris Mason prepare_to_wait(&cur_trans->writer_wait, &wait, 1276ed3b3d31SChris Mason TASK_UNINTERRUPTIBLE); 1277ed3b3d31SChris Mason 127889573b9cSChris Mason smp_mb(); 127999d16cbcSSage Weil if (cur_trans->num_writers > 1) 128099d16cbcSSage Weil schedule_timeout(MAX_SCHEDULE_TIMEOUT); 128199d16cbcSSage Weil else if (should_grow) 128299d16cbcSSage Weil schedule_timeout(1); 128315ee9bc7SJosef Bacik 128479154b1bSChris Mason mutex_lock(&root->fs_info->trans_mutex); 128515ee9bc7SJosef Bacik finish_wait(&cur_trans->writer_wait, &wait); 128615ee9bc7SJosef Bacik } while (cur_trans->num_writers > 1 || 128789573b9cSChris Mason (should_grow && cur_trans->num_joined != joined)); 128815ee9bc7SJosef Bacik 12893063d29fSChris Mason ret = create_pending_snapshots(trans, root->fs_info); 12903063d29fSChris Mason BUG_ON(ret); 12913063d29fSChris Mason 129256bec294SChris Mason ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 129356bec294SChris Mason BUG_ON(ret); 129456bec294SChris Mason 12952c90e5d6SChris Mason WARN_ON(cur_trans != trans->transaction); 1296dc17ff8fSChris Mason 1297e02119d5SChris Mason /* btrfs_commit_tree_roots is responsible for getting the 1298e02119d5SChris Mason * various roots consistent with each other. Every pointer 1299e02119d5SChris Mason * in the tree of tree roots has to point to the most up to date 1300e02119d5SChris Mason * root for every subvolume and other tree. So, we have to keep 1301e02119d5SChris Mason * the tree logging code from jumping in and changing any 1302e02119d5SChris Mason * of the trees. 1303e02119d5SChris Mason * 1304e02119d5SChris Mason * At this point in the commit, there can't be any tree-log 1305e02119d5SChris Mason * writers, but a little lower down we drop the trans mutex 1306e02119d5SChris Mason * and let new people in. By holding the tree_log_mutex 1307e02119d5SChris Mason * from now until after the super is written, we avoid races 1308e02119d5SChris Mason * with the tree-log code. 1309e02119d5SChris Mason */ 1310e02119d5SChris Mason mutex_lock(&root->fs_info->tree_log_mutex); 13111a40e23bSZheng Yan 13125d4f98a2SYan Zheng ret = commit_fs_roots(trans, root); 131354aa1f4dSChris Mason BUG_ON(ret); 131454aa1f4dSChris Mason 13155d4f98a2SYan Zheng /* commit_fs_roots gets rid of all the tree log roots, it is now 1316e02119d5SChris Mason * safe to free the root of tree log roots 1317e02119d5SChris Mason */ 1318e02119d5SChris Mason btrfs_free_log_root_tree(trans, root->fs_info); 1319e02119d5SChris Mason 13205d4f98a2SYan Zheng ret = commit_cowonly_roots(trans, root); 132179154b1bSChris Mason BUG_ON(ret); 132254aa1f4dSChris Mason 132311833d66SYan Zheng btrfs_prepare_extent_commit(trans, root); 132411833d66SYan Zheng 132578fae27eSChris Mason cur_trans = root->fs_info->running_transaction; 1326cee36a03SChris Mason spin_lock(&root->fs_info->new_trans_lock); 132778fae27eSChris Mason root->fs_info->running_transaction = NULL; 1328cee36a03SChris Mason spin_unlock(&root->fs_info->new_trans_lock); 13295f39d397SChris Mason 13305d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->tree_root->root_item, 13315d4f98a2SYan Zheng root->fs_info->tree_root->node); 1332817d52f8SJosef Bacik switch_commit_root(root->fs_info->tree_root); 13335d4f98a2SYan Zheng 13345d4f98a2SYan Zheng btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 13355d4f98a2SYan Zheng root->fs_info->chunk_root->node); 1336817d52f8SJosef Bacik switch_commit_root(root->fs_info->chunk_root); 13375d4f98a2SYan Zheng 13385d4f98a2SYan Zheng update_super_roots(root); 1339e02119d5SChris Mason 1340e02119d5SChris Mason if (!root->fs_info->log_root_recovering) { 1341e02119d5SChris Mason btrfs_set_super_log_root(&root->fs_info->super_copy, 0); 1342e02119d5SChris Mason btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); 1343e02119d5SChris Mason } 1344e02119d5SChris Mason 1345a061fc8dSChris Mason memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy, 13464b52dff6SChris Mason sizeof(root->fs_info->super_copy)); 1347ccd467d6SChris Mason 1348f9295749SChris Mason trans->transaction->blocked = 0; 1349b7ec40d7SChris Mason 1350f9295749SChris Mason wake_up(&root->fs_info->transaction_wait); 1351e6dcd2dcSChris Mason 135278fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 135379154b1bSChris Mason ret = btrfs_write_and_wait_transaction(trans, root); 135479154b1bSChris Mason BUG_ON(ret); 1355a512bbf8SYan Zheng write_ctree_super(trans, root, 0); 13564313b399SChris Mason 1357e02119d5SChris Mason /* 1358e02119d5SChris Mason * the super is written, we can safely allow the tree-loggers 1359e02119d5SChris Mason * to go about their business 1360e02119d5SChris Mason */ 1361e02119d5SChris Mason mutex_unlock(&root->fs_info->tree_log_mutex); 1362e02119d5SChris Mason 136311833d66SYan Zheng btrfs_finish_extent_commit(trans, root); 13644313b399SChris Mason 13651a40e23bSZheng Yan mutex_lock(&root->fs_info->trans_mutex); 13661a40e23bSZheng Yan 13672c90e5d6SChris Mason cur_trans->commit_done = 1; 1368b7ec40d7SChris Mason 136915ee9bc7SJosef Bacik root->fs_info->last_trans_committed = cur_trans->transid; 1370817d52f8SJosef Bacik 13712c90e5d6SChris Mason wake_up(&cur_trans->commit_wait); 13723de4586cSChris Mason 137379154b1bSChris Mason put_transaction(cur_trans); 137478fae27eSChris Mason put_transaction(cur_trans); 137558176a96SJosef Bacik 137678fae27eSChris Mason mutex_unlock(&root->fs_info->trans_mutex); 13773de4586cSChris Mason 13789ed74f2dSJosef Bacik if (current->journal_info == trans) 13799ed74f2dSJosef Bacik current->journal_info = NULL; 13809ed74f2dSJosef Bacik 13812c90e5d6SChris Mason kmem_cache_free(btrfs_trans_handle_cachep, trans); 138224bbcf04SYan, Zheng 138324bbcf04SYan, Zheng if (current != root->fs_info->transaction_kthread) 138424bbcf04SYan, Zheng btrfs_run_delayed_iputs(root); 138524bbcf04SYan, Zheng 138679154b1bSChris Mason return ret; 138779154b1bSChris Mason } 138879154b1bSChris Mason 1389d352ac68SChris Mason /* 1390d352ac68SChris Mason * interface function to delete all the snapshots we have scheduled for deletion 1391d352ac68SChris Mason */ 1392e9d0b13bSChris Mason int btrfs_clean_old_snapshots(struct btrfs_root *root) 1393e9d0b13bSChris Mason { 13945d4f98a2SYan Zheng LIST_HEAD(list); 13955d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = root->fs_info; 1396e9d0b13bSChris Mason 13975d4f98a2SYan Zheng mutex_lock(&fs_info->trans_mutex); 13985d4f98a2SYan Zheng list_splice_init(&fs_info->dead_roots, &list); 13995d4f98a2SYan Zheng mutex_unlock(&fs_info->trans_mutex); 14005d4f98a2SYan Zheng 14015d4f98a2SYan Zheng while (!list_empty(&list)) { 14025d4f98a2SYan Zheng root = list_entry(list.next, struct btrfs_root, root_list); 140376dda93cSYan, Zheng list_del(&root->root_list); 140476dda93cSYan, Zheng 140576dda93cSYan, Zheng if (btrfs_header_backref_rev(root->node) < 140676dda93cSYan, Zheng BTRFS_MIXED_BACKREF_REV) 14073fd0a558SYan, Zheng btrfs_drop_snapshot(root, NULL, 0); 140876dda93cSYan, Zheng else 14093fd0a558SYan, Zheng btrfs_drop_snapshot(root, NULL, 1); 1410e9d0b13bSChris Mason } 1411e9d0b13bSChris Mason return 0; 1412e9d0b13bSChris Mason } 1413