blob: 667735fb45e6f851b0fe56e4af2ded66e77ae9b6 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason79154b12007-03-22 15:59:16 -040019#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason34088782007-06-12 11:36:58 -040021#include <linux/sched.h>
Chris Masond3c2fdcf2007-09-17 10:58:06 -040022#include <linux/writeback.h>
Chris Mason5f39d392007-10-15 16:14:19 -040023#include <linux/pagemap.h>
Chris Mason5f2cc082008-11-07 18:22:45 -050024#include <linux/blkdev.h>
Chris Mason79154b12007-03-22 15:59:16 -040025#include "ctree.h"
26#include "disk-io.h"
27#include "transaction.h"
Chris Mason925baed2008-06-25 16:01:30 -040028#include "locking.h"
Chris Masone02119d2008-09-05 16:13:11 -040029#include "tree-log.h"
Li Zefan581bb052011-04-20 10:06:11 +080030#include "inode-map.h"
Chris Mason79154b12007-03-22 15:59:16 -040031
Chris Mason0f7d52f2007-04-09 10:42:37 -040032#define BTRFS_ROOT_TRANS_TAG 0
33
Jeff Mahoney49b25e02012-03-01 17:24:58 +010034void put_transaction(struct btrfs_transaction *transaction)
Chris Mason79154b12007-03-22 15:59:16 -040035{
Josef Bacik13c5a932011-04-11 15:45:29 -040036 WARN_ON(atomic_read(&transaction->use_count) == 0);
37 if (atomic_dec_and_test(&transaction->use_count)) {
Josef Bacika4abeea2011-04-11 17:25:13 -040038 BUG_ON(!list_empty(&transaction->list));
Arne Jansen00f04b82011-09-14 12:37:00 +020039 WARN_ON(transaction->delayed_refs.root.rb_node);
40 WARN_ON(!list_empty(&transaction->delayed_refs.seq_head));
Chris Mason2c90e5d2007-04-02 10:50:19 -040041 memset(transaction, 0, sizeof(*transaction));
42 kmem_cache_free(btrfs_transaction_cachep, transaction);
Chris Mason78fae272007-03-25 11:35:08 -040043 }
Chris Mason79154b12007-03-22 15:59:16 -040044}
45
Josef Bacik817d52f2009-07-13 21:29:25 -040046static noinline void switch_commit_root(struct btrfs_root *root)
47{
Josef Bacik817d52f2009-07-13 21:29:25 -040048 free_extent_buffer(root->commit_root);
49 root->commit_root = btrfs_root_node(root);
Josef Bacik817d52f2009-07-13 21:29:25 -040050}
51
Chris Masond352ac62008-09-29 15:18:18 -040052/*
53 * either allocate a new transaction or hop into the existing one
54 */
Josef Bacika4abeea2011-04-11 17:25:13 -040055static noinline int join_transaction(struct btrfs_root *root, int nofail)
Chris Mason79154b12007-03-22 15:59:16 -040056{
57 struct btrfs_transaction *cur_trans;
Jan Schmidt19ae4e82012-05-20 15:42:19 +020058 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacika4abeea2011-04-11 17:25:13 -040059
Jan Schmidt19ae4e82012-05-20 15:42:19 +020060 spin_lock(&fs_info->trans_lock);
Chris Masond43317d2011-11-06 03:26:19 -050061loop:
Jeff Mahoney49b25e02012-03-01 17:24:58 +010062 /* The file system has been taken offline. No new transactions. */
Jan Schmidt19ae4e82012-05-20 15:42:19 +020063 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
64 spin_unlock(&fs_info->trans_lock);
Jeff Mahoney49b25e02012-03-01 17:24:58 +010065 return -EROFS;
66 }
67
Jan Schmidt19ae4e82012-05-20 15:42:19 +020068 if (fs_info->trans_no_join) {
Josef Bacika4abeea2011-04-11 17:25:13 -040069 if (!nofail) {
Jan Schmidt19ae4e82012-05-20 15:42:19 +020070 spin_unlock(&fs_info->trans_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -040071 return -EBUSY;
72 }
73 }
74
Jan Schmidt19ae4e82012-05-20 15:42:19 +020075 cur_trans = fs_info->running_transaction;
Josef Bacika4abeea2011-04-11 17:25:13 -040076 if (cur_trans) {
David Sterba871383b2012-04-02 18:31:37 +020077 if (cur_trans->aborted) {
Jan Schmidt19ae4e82012-05-20 15:42:19 +020078 spin_unlock(&fs_info->trans_lock);
Jeff Mahoney49b25e02012-03-01 17:24:58 +010079 return cur_trans->aborted;
David Sterba871383b2012-04-02 18:31:37 +020080 }
Josef Bacika4abeea2011-04-11 17:25:13 -040081 atomic_inc(&cur_trans->use_count);
Josef Bacik13c5a932011-04-11 15:45:29 -040082 atomic_inc(&cur_trans->num_writers);
Josef Bacik15ee9bc2007-08-10 16:22:09 -040083 cur_trans->num_joined++;
Jan Schmidt19ae4e82012-05-20 15:42:19 +020084 spin_unlock(&fs_info->trans_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -040085 return 0;
Chris Mason79154b12007-03-22 15:59:16 -040086 }
Jan Schmidt19ae4e82012-05-20 15:42:19 +020087 spin_unlock(&fs_info->trans_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -040088
89 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
90 if (!cur_trans)
91 return -ENOMEM;
Chris Masond43317d2011-11-06 03:26:19 -050092
Jan Schmidt19ae4e82012-05-20 15:42:19 +020093 spin_lock(&fs_info->trans_lock);
94 if (fs_info->running_transaction) {
Chris Masond43317d2011-11-06 03:26:19 -050095 /*
96 * someone started a transaction after we unlocked. Make sure
97 * to redo the trans_no_join checks above
98 */
Josef Bacika4abeea2011-04-11 17:25:13 -040099 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
Jan Schmidt19ae4e82012-05-20 15:42:19 +0200100 cur_trans = fs_info->running_transaction;
Chris Masond43317d2011-11-06 03:26:19 -0500101 goto loop;
Josef Bacika4abeea2011-04-11 17:25:13 -0400102 }
Chris Masond43317d2011-11-06 03:26:19 -0500103
Josef Bacika4abeea2011-04-11 17:25:13 -0400104 atomic_set(&cur_trans->num_writers, 1);
105 cur_trans->num_joined = 0;
106 init_waitqueue_head(&cur_trans->writer_wait);
107 init_waitqueue_head(&cur_trans->commit_wait);
108 cur_trans->in_commit = 0;
109 cur_trans->blocked = 0;
110 /*
111 * One for this trans handle, one so it will live on until we
112 * commit the transaction.
113 */
114 atomic_set(&cur_trans->use_count, 2);
115 cur_trans->commit_done = 0;
116 cur_trans->start_time = get_seconds();
117
118 cur_trans->delayed_refs.root = RB_ROOT;
119 cur_trans->delayed_refs.num_entries = 0;
120 cur_trans->delayed_refs.num_heads_ready = 0;
121 cur_trans->delayed_refs.num_heads = 0;
122 cur_trans->delayed_refs.flushing = 0;
123 cur_trans->delayed_refs.run_delayed_start = 0;
Arne Jansen00f04b82011-09-14 12:37:00 +0200124 cur_trans->delayed_refs.seq = 1;
Jan Schmidt20b297d2012-05-20 15:43:53 +0200125
126 /*
127 * although the tree mod log is per file system and not per transaction,
128 * the log must never go across transaction boundaries.
129 */
130 smp_mb();
131 if (!list_empty(&fs_info->tree_mod_seq_list)) {
132 printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
133 "creating a fresh transaction\n");
134 WARN_ON(1);
135 }
136 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
137 printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
138 "creating a fresh transaction\n");
139 WARN_ON(1);
140 }
141 atomic_set(&fs_info->tree_mod_seq, 0);
142
Jan Schmidta1686502011-12-12 16:10:07 +0100143 init_waitqueue_head(&cur_trans->delayed_refs.seq_wait);
Josef Bacika4abeea2011-04-11 17:25:13 -0400144 spin_lock_init(&cur_trans->commit_lock);
145 spin_lock_init(&cur_trans->delayed_refs.lock);
Arne Jansen00f04b82011-09-14 12:37:00 +0200146 INIT_LIST_HEAD(&cur_trans->delayed_refs.seq_head);
Josef Bacika4abeea2011-04-11 17:25:13 -0400147
148 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
Jan Schmidt19ae4e82012-05-20 15:42:19 +0200149 list_add_tail(&cur_trans->list, &fs_info->trans_list);
Josef Bacika4abeea2011-04-11 17:25:13 -0400150 extent_io_tree_init(&cur_trans->dirty_pages,
Jan Schmidt19ae4e82012-05-20 15:42:19 +0200151 fs_info->btree_inode->i_mapping);
152 fs_info->generation++;
153 cur_trans->transid = fs_info->generation;
154 fs_info->running_transaction = cur_trans;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100155 cur_trans->aborted = 0;
Jan Schmidt19ae4e82012-05-20 15:42:19 +0200156 spin_unlock(&fs_info->trans_lock);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400157
Chris Mason79154b12007-03-22 15:59:16 -0400158 return 0;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
Chris Masond3977122009-01-05 21:25:51 -0500162 * this does all the record keeping required to make sure that a reference
163 * counted root is properly recorded in a given transaction. This is required
164 * to make sure the old root from before we joined the transaction is deleted
165 * when the transaction commits
Chris Masond352ac62008-09-29 15:18:18 -0400166 */
Chris Mason75857172011-06-13 20:00:16 -0400167static int record_root_in_trans(struct btrfs_trans_handle *trans,
Josef Bacika4abeea2011-04-11 17:25:13 -0400168 struct btrfs_root *root)
Chris Mason6702ed42007-08-07 16:15:09 -0400169{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400170 if (root->ref_cows && root->last_trans < trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400171 WARN_ON(root == root->fs_info->extent_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400172 WARN_ON(root->commit_root != root->node);
Yan Zheng31153d82008-07-28 15:32:19 -0400173
Chris Mason75857172011-06-13 20:00:16 -0400174 /*
175 * see below for in_trans_setup usage rules
176 * we have the reloc mutex held now, so there
177 * is only one writer in this function
178 */
179 root->in_trans_setup = 1;
180
181 /* make sure readers find in_trans_setup before
182 * they find our root->last_trans update
183 */
184 smp_wmb();
185
Josef Bacika4abeea2011-04-11 17:25:13 -0400186 spin_lock(&root->fs_info->fs_roots_radix_lock);
187 if (root->last_trans == trans->transid) {
188 spin_unlock(&root->fs_info->fs_roots_radix_lock);
189 return 0;
190 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400191 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
192 (unsigned long)root->root_key.objectid,
193 BTRFS_ROOT_TRANS_TAG);
Josef Bacika4abeea2011-04-11 17:25:13 -0400194 spin_unlock(&root->fs_info->fs_roots_radix_lock);
Chris Mason75857172011-06-13 20:00:16 -0400195 root->last_trans = trans->transid;
196
197 /* this is pretty tricky. We don't want to
198 * take the relocation lock in btrfs_record_root_in_trans
199 * unless we're really doing the first setup for this root in
200 * this transaction.
201 *
202 * Normally we'd use root->last_trans as a flag to decide
203 * if we want to take the expensive mutex.
204 *
205 * But, we have to set root->last_trans before we
206 * init the relocation root, otherwise, we trip over warnings
207 * in ctree.c. The solution used here is to flag ourselves
208 * with root->in_trans_setup. When this is 1, we're still
209 * fixing up the reloc trees and everyone must wait.
210 *
211 * When this is zero, they can trust root->last_trans and fly
212 * through btrfs_record_root_in_trans without having to take the
213 * lock. smp_wmb() makes sure that all the writes above are
214 * done before we pop in the zero below
215 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400216 btrfs_init_reloc_root(trans, root);
Chris Mason75857172011-06-13 20:00:16 -0400217 smp_wmb();
218 root->in_trans_setup = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400219 }
220 return 0;
221}
222
Chris Mason75857172011-06-13 20:00:16 -0400223
224int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
225 struct btrfs_root *root)
226{
227 if (!root->ref_cows)
228 return 0;
229
230 /*
231 * see record_root_in_trans for comments about in_trans_setup usage
232 * and barriers
233 */
234 smp_rmb();
235 if (root->last_trans == trans->transid &&
236 !root->in_trans_setup)
237 return 0;
238
239 mutex_lock(&root->fs_info->reloc_mutex);
240 record_root_in_trans(trans, root);
241 mutex_unlock(&root->fs_info->reloc_mutex);
242
243 return 0;
244}
245
Chris Masond352ac62008-09-29 15:18:18 -0400246/* wait for commit against the current transaction to become unblocked
247 * when this is done, it is safe to start a new transaction, but the current
248 * transaction might not be fully on disk.
249 */
Chris Mason37d1aee2008-07-31 10:48:37 -0400250static void wait_current_trans(struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400251{
Chris Masonf9295742008-07-17 12:54:14 -0400252 struct btrfs_transaction *cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400253
Josef Bacika4abeea2011-04-11 17:25:13 -0400254 spin_lock(&root->fs_info->trans_lock);
Chris Masonf9295742008-07-17 12:54:14 -0400255 cur_trans = root->fs_info->running_transaction;
Chris Mason37d1aee2008-07-31 10:48:37 -0400256 if (cur_trans && cur_trans->blocked) {
Josef Bacik13c5a932011-04-11 15:45:29 -0400257 atomic_inc(&cur_trans->use_count);
Josef Bacika4abeea2011-04-11 17:25:13 -0400258 spin_unlock(&root->fs_info->trans_lock);
Li Zefan72d63ed2011-07-14 03:17:00 +0000259
260 wait_event(root->fs_info->transaction_wait,
261 !cur_trans->blocked);
Chris Masonf9295742008-07-17 12:54:14 -0400262 put_transaction(cur_trans);
Josef Bacika4abeea2011-04-11 17:25:13 -0400263 } else {
264 spin_unlock(&root->fs_info->trans_lock);
Chris Masonf9295742008-07-17 12:54:14 -0400265 }
Chris Mason37d1aee2008-07-31 10:48:37 -0400266}
267
Josef Bacik249ac1e2009-11-10 21:23:48 -0500268enum btrfs_trans_type {
269 TRANS_START,
270 TRANS_JOIN,
271 TRANS_USERSPACE,
Josef Bacik0af3d002010-06-21 14:48:16 -0400272 TRANS_JOIN_NOLOCK,
Josef Bacik249ac1e2009-11-10 21:23:48 -0500273};
274
Yan, Zhenga22285a2010-05-16 10:48:46 -0400275static int may_wait_transaction(struct btrfs_root *root, int type)
Chris Mason37d1aee2008-07-31 10:48:37 -0400276{
Josef Bacika4abeea2011-04-11 17:25:13 -0400277 if (root->fs_info->log_root_recovering)
278 return 0;
279
280 if (type == TRANS_USERSPACE)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400281 return 1;
Josef Bacika4abeea2011-04-11 17:25:13 -0400282
283 if (type == TRANS_START &&
284 !atomic_read(&root->fs_info->open_ioctl_trans))
285 return 1;
286
Yan, Zhenga22285a2010-05-16 10:48:46 -0400287 return 0;
288}
289
290static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
291 u64 num_items, int type)
292{
293 struct btrfs_trans_handle *h;
294 struct btrfs_transaction *cur_trans;
Josef Bacikb5009942011-06-07 15:07:51 -0400295 u64 num_bytes = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400296 int ret;
liuboacce9522011-01-06 19:30:25 +0800297
298 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
299 return ERR_PTR(-EROFS);
Josef Bacik2a1eb462011-04-13 15:15:59 -0400300
301 if (current->journal_info) {
302 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
303 h = current->journal_info;
304 h->use_count++;
305 h->orig_rsv = h->block_rsv;
306 h->block_rsv = NULL;
307 goto got_it;
308 }
Josef Bacikb5009942011-06-07 15:07:51 -0400309
310 /*
311 * Do the reservation before we join the transaction so we can do all
312 * the appropriate flushing if need be.
313 */
314 if (num_items > 0 && root != root->fs_info->chunk_root) {
315 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
Josef Bacik4a92b1b2011-08-30 12:34:28 -0400316 ret = btrfs_block_rsv_add(root,
Josef Bacikb5009942011-06-07 15:07:51 -0400317 &root->fs_info->trans_block_rsv,
318 num_bytes);
319 if (ret)
320 return ERR_PTR(ret);
321 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400322again:
323 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
324 if (!h)
325 return ERR_PTR(-ENOMEM);
326
Yan, Zhenga22285a2010-05-16 10:48:46 -0400327 if (may_wait_transaction(root, type))
Chris Mason37d1aee2008-07-31 10:48:37 -0400328 wait_current_trans(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400329
Josef Bacika4abeea2011-04-11 17:25:13 -0400330 do {
331 ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
332 if (ret == -EBUSY)
333 wait_current_trans(root);
334 } while (ret == -EBUSY);
335
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000336 if (ret < 0) {
Yoshinori Sano6e8df2a2011-04-03 12:31:28 +0000337 kmem_cache_free(btrfs_trans_handle_cachep, h);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +0000338 return ERR_PTR(ret);
339 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400340
Yan, Zhenga22285a2010-05-16 10:48:46 -0400341 cur_trans = root->fs_info->running_transaction;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400342
343 h->transid = cur_trans->transid;
344 h->transaction = cur_trans;
Chris Mason79154b12007-03-22 15:59:16 -0400345 h->blocks_used = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400346 h->bytes_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400347 h->delayed_ref_updates = 0;
Josef Bacik2a1eb462011-04-13 15:15:59 -0400348 h->use_count = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400349 h->block_rsv = NULL;
Josef Bacik2a1eb462011-04-13 15:15:59 -0400350 h->orig_rsv = NULL;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100351 h->aborted = 0;
Chris Masonb7ec40d2009-03-12 20:12:45 -0400352
Yan, Zhenga22285a2010-05-16 10:48:46 -0400353 smp_mb();
354 if (cur_trans->blocked && may_wait_transaction(root, type)) {
355 btrfs_commit_transaction(h, root);
356 goto again;
357 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400358
Josef Bacikb5009942011-06-07 15:07:51 -0400359 if (num_bytes) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500360 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -0400361 h->transid, num_bytes, 1);
Josef Bacikb5009942011-06-07 15:07:51 -0400362 h->block_rsv = &root->fs_info->trans_block_rsv;
363 h->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400364 }
365
Josef Bacik2a1eb462011-04-13 15:15:59 -0400366got_it:
Josef Bacika4abeea2011-04-11 17:25:13 -0400367 btrfs_record_root_in_trans(h, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400368
369 if (!current->journal_info && type != TRANS_USERSPACE)
370 current->journal_info = h;
Chris Mason79154b12007-03-22 15:59:16 -0400371 return h;
372}
373
Chris Masonf9295742008-07-17 12:54:14 -0400374struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400375 int num_items)
Chris Masonf9295742008-07-17 12:54:14 -0400376{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400377 return start_transaction(root, num_items, TRANS_START);
Chris Masonf9295742008-07-17 12:54:14 -0400378}
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400379struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
Chris Masonf9295742008-07-17 12:54:14 -0400380{
Yan, Zhenga22285a2010-05-16 10:48:46 -0400381 return start_transaction(root, 0, TRANS_JOIN);
Chris Masonf9295742008-07-17 12:54:14 -0400382}
383
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400384struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
Josef Bacik0af3d002010-06-21 14:48:16 -0400385{
386 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
387}
388
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400389struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
Sage Weil9ca9ee02008-08-04 10:41:27 -0400390{
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400391 return start_transaction(root, 0, TRANS_USERSPACE);
Sage Weil9ca9ee02008-08-04 10:41:27 -0400392}
393
Chris Masond352ac62008-09-29 15:18:18 -0400394/* wait for a transaction commit to be fully complete */
Li Zefanb9c83002011-07-14 03:17:14 +0000395static noinline void wait_for_commit(struct btrfs_root *root,
Chris Mason89ce8a62008-06-25 16:01:31 -0400396 struct btrfs_transaction *commit)
397{
Li Zefan72d63ed2011-07-14 03:17:00 +0000398 wait_event(commit->commit_wait, commit->commit_done);
Chris Mason89ce8a62008-06-25 16:01:31 -0400399}
400
Sage Weil46204592010-10-29 15:41:32 -0400401int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
402{
403 struct btrfs_transaction *cur_trans = NULL, *t;
404 int ret;
405
Sage Weil46204592010-10-29 15:41:32 -0400406 ret = 0;
407 if (transid) {
408 if (transid <= root->fs_info->last_trans_committed)
Josef Bacika4abeea2011-04-11 17:25:13 -0400409 goto out;
Sage Weil46204592010-10-29 15:41:32 -0400410
411 /* find specified transaction */
Josef Bacika4abeea2011-04-11 17:25:13 -0400412 spin_lock(&root->fs_info->trans_lock);
Sage Weil46204592010-10-29 15:41:32 -0400413 list_for_each_entry(t, &root->fs_info->trans_list, list) {
414 if (t->transid == transid) {
415 cur_trans = t;
Josef Bacika4abeea2011-04-11 17:25:13 -0400416 atomic_inc(&cur_trans->use_count);
Sage Weil46204592010-10-29 15:41:32 -0400417 break;
418 }
419 if (t->transid > transid)
420 break;
421 }
Josef Bacika4abeea2011-04-11 17:25:13 -0400422 spin_unlock(&root->fs_info->trans_lock);
Sage Weil46204592010-10-29 15:41:32 -0400423 ret = -EINVAL;
424 if (!cur_trans)
Josef Bacika4abeea2011-04-11 17:25:13 -0400425 goto out; /* bad transid */
Sage Weil46204592010-10-29 15:41:32 -0400426 } else {
427 /* find newest transaction that is committing | committed */
Josef Bacika4abeea2011-04-11 17:25:13 -0400428 spin_lock(&root->fs_info->trans_lock);
Sage Weil46204592010-10-29 15:41:32 -0400429 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
430 list) {
431 if (t->in_commit) {
432 if (t->commit_done)
Josef Bacik3473f3c2011-06-09 10:15:17 -0400433 break;
Sage Weil46204592010-10-29 15:41:32 -0400434 cur_trans = t;
Josef Bacika4abeea2011-04-11 17:25:13 -0400435 atomic_inc(&cur_trans->use_count);
Sage Weil46204592010-10-29 15:41:32 -0400436 break;
437 }
438 }
Josef Bacika4abeea2011-04-11 17:25:13 -0400439 spin_unlock(&root->fs_info->trans_lock);
Sage Weil46204592010-10-29 15:41:32 -0400440 if (!cur_trans)
Josef Bacika4abeea2011-04-11 17:25:13 -0400441 goto out; /* nothing committing|committed */
Sage Weil46204592010-10-29 15:41:32 -0400442 }
443
Sage Weil46204592010-10-29 15:41:32 -0400444 wait_for_commit(root, cur_trans);
445
Sage Weil46204592010-10-29 15:41:32 -0400446 put_transaction(cur_trans);
447 ret = 0;
Josef Bacika4abeea2011-04-11 17:25:13 -0400448out:
Sage Weil46204592010-10-29 15:41:32 -0400449 return ret;
450}
451
Chris Mason37d1aee2008-07-31 10:48:37 -0400452void btrfs_throttle(struct btrfs_root *root)
453{
Josef Bacika4abeea2011-04-11 17:25:13 -0400454 if (!atomic_read(&root->fs_info->open_ioctl_trans))
Sage Weil9ca9ee02008-08-04 10:41:27 -0400455 wait_current_trans(root);
Chris Mason37d1aee2008-07-31 10:48:37 -0400456}
457
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400458static int should_end_transaction(struct btrfs_trans_handle *trans,
459 struct btrfs_root *root)
460{
461 int ret;
Josef Bacik36ba0222011-10-18 12:15:48 -0400462
463 ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400464 return ret ? 1 : 0;
465}
466
467int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
468 struct btrfs_root *root)
469{
470 struct btrfs_transaction *cur_trans = trans->transaction;
Josef Bacik9c8d86d2011-09-19 11:58:54 -0400471 struct btrfs_block_rsv *rsv = trans->block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400472 int updates;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100473 int err;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400474
Josef Bacika4abeea2011-04-11 17:25:13 -0400475 smp_mb();
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400476 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
477 return 1;
478
Josef Bacik9c8d86d2011-09-19 11:58:54 -0400479 /*
480 * We need to do this in case we're deleting csums so the global block
481 * rsv get's used instead of the csum block rsv.
482 */
483 trans->block_rsv = NULL;
484
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400485 updates = trans->delayed_ref_updates;
486 trans->delayed_ref_updates = 0;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100487 if (updates) {
488 err = btrfs_run_delayed_refs(trans, root, updates);
489 if (err) /* Error code will also eval true */
490 return err;
491 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400492
Josef Bacik9c8d86d2011-09-19 11:58:54 -0400493 trans->block_rsv = rsv;
494
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400495 return should_end_transaction(trans, root);
496}
497
Chris Mason89ce8a62008-06-25 16:01:31 -0400498static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
Josef Bacik0af3d002010-06-21 14:48:16 -0400499 struct btrfs_root *root, int throttle, int lock)
Chris Mason79154b12007-03-22 15:59:16 -0400500{
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400501 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Masonab78c842008-07-29 16:15:18 -0400502 struct btrfs_fs_info *info = root->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -0400503 int count = 0;
Dave Jones4edc2ca2012-04-12 16:03:56 -0400504 int err = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400505
Josef Bacik2a1eb462011-04-13 15:15:59 -0400506 if (--trans->use_count) {
507 trans->block_rsv = trans->orig_rsv;
508 return 0;
509 }
510
Josef Bacikb24e03d2011-10-14 14:40:17 -0400511 btrfs_trans_release_metadata(trans, root);
Josef Bacik4c13d752011-08-30 11:31:29 -0400512 trans->block_rsv = NULL;
Chris Mason203bf282012-01-06 15:23:57 -0500513 while (count < 2) {
Chris Masonc3e69d52009-03-13 10:17:05 -0400514 unsigned long cur = trans->delayed_ref_updates;
515 trans->delayed_ref_updates = 0;
516 if (cur &&
517 trans->transaction->delayed_refs.num_heads_ready > 64) {
518 trans->delayed_ref_updates = 0;
519 btrfs_run_delayed_refs(trans, root, cur);
520 } else {
521 break;
522 }
523 count++;
Chris Mason56bec292009-03-13 10:10:06 -0400524 }
525
Josef Bacika4abeea2011-04-11 17:25:13 -0400526 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
527 should_end_transaction(trans, root)) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400528 trans->transaction->blocked = 1;
Josef Bacika4abeea2011-04-11 17:25:13 -0400529 smp_wmb();
530 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400531
Josef Bacik0af3d002010-06-21 14:48:16 -0400532 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
Josef Bacik81317fd2011-07-24 15:45:34 -0400533 if (throttle) {
534 /*
535 * We may race with somebody else here so end up having
536 * to call end_transaction on ourselves again, so inc
537 * our use_count.
538 */
539 trans->use_count++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400540 return btrfs_commit_transaction(trans, root);
Josef Bacik81317fd2011-07-24 15:45:34 -0400541 } else {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400542 wake_up_process(info->transaction_kthread);
Josef Bacik81317fd2011-07-24 15:45:34 -0400543 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400544 }
545
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400546 WARN_ON(cur_trans != info->running_transaction);
Josef Bacik13c5a932011-04-11 15:45:29 -0400547 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
548 atomic_dec(&cur_trans->num_writers);
Chris Mason89ce8a62008-06-25 16:01:31 -0400549
Sage Weil99d16cb2010-10-29 15:37:34 -0400550 smp_mb();
Chris Mason79154b12007-03-22 15:59:16 -0400551 if (waitqueue_active(&cur_trans->writer_wait))
552 wake_up(&cur_trans->writer_wait);
Chris Mason79154b12007-03-22 15:59:16 -0400553 put_transaction(cur_trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400554
555 if (current->journal_info == trans)
556 current->journal_info = NULL;
Chris Masonab78c842008-07-29 16:15:18 -0400557
Yan, Zheng24bbcf042009-11-12 09:36:34 +0000558 if (throttle)
559 btrfs_run_delayed_iputs(root);
560
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100561 if (trans->aborted ||
562 root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
Dave Jones4edc2ca2012-04-12 16:03:56 -0400563 err = -EIO;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100564 }
565
Dave Jones4edc2ca2012-04-12 16:03:56 -0400566 memset(trans, 0, sizeof(*trans));
567 kmem_cache_free(btrfs_trans_handle_cachep, trans);
568 return err;
Chris Mason79154b12007-03-22 15:59:16 -0400569}
570
Chris Mason89ce8a62008-06-25 16:01:31 -0400571int btrfs_end_transaction(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root)
573{
Miao Xie16cdcec2011-04-22 18:12:22 +0800574 int ret;
575
576 ret = __btrfs_end_transaction(trans, root, 0, 1);
577 if (ret)
578 return ret;
579 return 0;
Chris Mason89ce8a62008-06-25 16:01:31 -0400580}
581
582int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
583 struct btrfs_root *root)
584{
Miao Xie16cdcec2011-04-22 18:12:22 +0800585 int ret;
586
587 ret = __btrfs_end_transaction(trans, root, 1, 1);
588 if (ret)
589 return ret;
590 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -0400591}
592
593int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
594 struct btrfs_root *root)
595{
Miao Xie16cdcec2011-04-22 18:12:22 +0800596 int ret;
597
598 ret = __btrfs_end_transaction(trans, root, 0, 0);
599 if (ret)
600 return ret;
601 return 0;
602}
603
604int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
605 struct btrfs_root *root)
606{
607 return __btrfs_end_transaction(trans, root, 1, 1);
Chris Mason89ce8a62008-06-25 16:01:31 -0400608}
609
Chris Masond352ac62008-09-29 15:18:18 -0400610/*
611 * when btree blocks are allocated, they have some corresponding bits set for
612 * them in one of two extent_io trees. This is used to make sure all of
Chris Mason690587d2009-10-13 13:29:19 -0400613 * those extents are sent to disk but does not wait on them
Chris Masond352ac62008-09-29 15:18:18 -0400614 */
Chris Mason690587d2009-10-13 13:29:19 -0400615int btrfs_write_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000616 struct extent_io_tree *dirty_pages, int mark)
Chris Mason79154b12007-03-22 15:59:16 -0400617{
Chris Mason777e6bd2008-08-15 15:34:15 -0400618 int err = 0;
Chris Mason7c4452b2007-04-28 09:29:35 -0400619 int werr = 0;
Josef Bacik17283662011-09-26 13:58:47 -0400620 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
Chris Mason777e6bd2008-08-15 15:34:15 -0400621 u64 start = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400622 u64 end;
Chris Mason7c4452b2007-04-28 09:29:35 -0400623
Josef Bacik17283662011-09-26 13:58:47 -0400624 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
625 mark)) {
626 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark,
627 GFP_NOFS);
628 err = filemap_fdatawrite_range(mapping, start, end);
629 if (err)
630 werr = err;
631 cond_resched();
632 start = end + 1;
Chris Mason7c4452b2007-04-28 09:29:35 -0400633 }
Chris Mason690587d2009-10-13 13:29:19 -0400634 if (err)
635 werr = err;
636 return werr;
637}
638
639/*
640 * when btree blocks are allocated, they have some corresponding bits set for
641 * them in one of two extent_io trees. This is used to make sure all of
642 * those extents are on disk for transaction or log commit. We wait
643 * on all the pages and clear them from the dirty pages state tree
644 */
645int btrfs_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000646 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400647{
Chris Mason690587d2009-10-13 13:29:19 -0400648 int err = 0;
649 int werr = 0;
Josef Bacik17283662011-09-26 13:58:47 -0400650 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
Chris Mason690587d2009-10-13 13:29:19 -0400651 u64 start = 0;
652 u64 end;
Chris Mason690587d2009-10-13 13:29:19 -0400653
Josef Bacik17283662011-09-26 13:58:47 -0400654 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
655 EXTENT_NEED_WAIT)) {
656 clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS);
657 err = filemap_fdatawait_range(mapping, start, end);
658 if (err)
659 werr = err;
660 cond_resched();
661 start = end + 1;
Chris Mason777e6bd2008-08-15 15:34:15 -0400662 }
Chris Mason7c4452b2007-04-28 09:29:35 -0400663 if (err)
664 werr = err;
665 return werr;
Chris Mason79154b12007-03-22 15:59:16 -0400666}
667
Chris Mason690587d2009-10-13 13:29:19 -0400668/*
669 * when btree blocks are allocated, they have some corresponding bits set for
670 * them in one of two extent_io trees. This is used to make sure all of
671 * those extents are on disk for transaction or log commit
672 */
673int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000674 struct extent_io_tree *dirty_pages, int mark)
Chris Mason690587d2009-10-13 13:29:19 -0400675{
676 int ret;
677 int ret2;
678
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000679 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
680 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
Chris Masonbf0da8c2011-11-04 12:29:37 -0400681
682 if (ret)
683 return ret;
684 if (ret2)
685 return ret2;
686 return 0;
Chris Mason690587d2009-10-13 13:29:19 -0400687}
688
Chris Masond0c803c2008-09-11 16:17:57 -0400689int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root)
691{
692 if (!trans || !trans->transaction) {
693 struct inode *btree_inode;
694 btree_inode = root->fs_info->btree_inode;
695 return filemap_write_and_wait(btree_inode->i_mapping);
696 }
697 return btrfs_write_and_wait_marked_extents(root,
Yan, Zheng8cef4e12009-11-12 09:33:26 +0000698 &trans->transaction->dirty_pages,
699 EXTENT_DIRTY);
Chris Masond0c803c2008-09-11 16:17:57 -0400700}
701
Chris Masond352ac62008-09-29 15:18:18 -0400702/*
703 * this is used to update the root pointer in the tree of tree roots.
704 *
705 * But, in the case of the extent allocation tree, updating the root
706 * pointer may allocate blocks which may change the root of the extent
707 * allocation tree.
708 *
709 * So, this loops and repeats and makes sure the cowonly root didn't
710 * change while the root pointer was being updated in the metadata.
711 */
Chris Mason0b86a832008-03-24 15:01:56 -0400712static int update_cowonly_root(struct btrfs_trans_handle *trans,
713 struct btrfs_root *root)
714{
715 int ret;
716 u64 old_root_bytenr;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000717 u64 old_root_used;
Chris Mason0b86a832008-03-24 15:01:56 -0400718 struct btrfs_root *tree_root = root->fs_info->tree_root;
719
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000720 old_root_used = btrfs_root_used(&root->root_item);
Chris Mason0b86a832008-03-24 15:01:56 -0400721 btrfs_write_dirty_block_groups(trans, root);
Chris Mason56bec292009-03-13 10:10:06 -0400722
Chris Masond3977122009-01-05 21:25:51 -0500723 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -0400724 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000725 if (old_root_bytenr == root->node->start &&
726 old_root_used == btrfs_root_used(&root->root_item))
Chris Mason0b86a832008-03-24 15:01:56 -0400727 break;
Chris Mason87ef2bb2008-10-30 11:23:27 -0400728
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400729 btrfs_set_root_node(&root->root_item, root->node);
Chris Mason0b86a832008-03-24 15:01:56 -0400730 ret = btrfs_update_root(trans, tree_root,
731 &root->root_key,
732 &root->root_item);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100733 if (ret)
734 return ret;
Chris Mason56bec292009-03-13 10:10:06 -0400735
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000736 old_root_used = btrfs_root_used(&root->root_item);
Yan Zheng4a8c9a62009-07-22 10:07:05 -0400737 ret = btrfs_write_dirty_block_groups(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100738 if (ret)
739 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -0400740 }
Yan Zheng276e6802009-07-30 09:40:40 -0400741
742 if (root != root->fs_info->extent_root)
743 switch_commit_root(root);
744
Chris Mason0b86a832008-03-24 15:01:56 -0400745 return 0;
746}
747
Chris Masond352ac62008-09-29 15:18:18 -0400748/*
749 * update all the cowonly tree roots on disk
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100750 *
751 * The error handling in this function may not be obvious. Any of the
752 * failures will cause the file system to go offline. We still need
753 * to clean up the delayed refs.
Chris Masond352ac62008-09-29 15:18:18 -0400754 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root)
Chris Mason79154b12007-03-22 15:59:16 -0400757{
Chris Mason79154b12007-03-22 15:59:16 -0400758 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -0400759 struct list_head *next;
Yan Zheng84234f32008-10-29 14:49:05 -0400760 struct extent_buffer *eb;
Chris Mason56bec292009-03-13 10:10:06 -0400761 int ret;
Yan Zheng84234f32008-10-29 14:49:05 -0400762
Chris Mason56bec292009-03-13 10:10:06 -0400763 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100764 if (ret)
765 return ret;
Chris Mason87ef2bb2008-10-30 11:23:27 -0400766
Yan Zheng84234f32008-10-29 14:49:05 -0400767 eb = btrfs_lock_root_node(fs_info->tree_root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100768 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
769 0, &eb);
Yan Zheng84234f32008-10-29 14:49:05 -0400770 btrfs_tree_unlock(eb);
771 free_extent_buffer(eb);
Chris Mason79154b12007-03-22 15:59:16 -0400772
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100773 if (ret)
774 return ret;
775
Chris Mason56bec292009-03-13 10:10:06 -0400776 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100777 if (ret)
778 return ret;
Chris Mason87ef2bb2008-10-30 11:23:27 -0400779
Chris Masond3977122009-01-05 21:25:51 -0500780 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400781 next = fs_info->dirty_cowonly_roots.next;
782 list_del_init(next);
783 root = list_entry(next, struct btrfs_root, dirty_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400784
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100785 ret = update_cowonly_root(trans, root);
786 if (ret)
787 return ret;
Chris Mason79154b12007-03-22 15:59:16 -0400788 }
Yan Zheng276e6802009-07-30 09:40:40 -0400789
790 down_write(&fs_info->extent_commit_sem);
791 switch_commit_root(fs_info->extent_root);
792 up_write(&fs_info->extent_commit_sem);
793
Chris Mason79154b12007-03-22 15:59:16 -0400794 return 0;
795}
796
Chris Masond352ac62008-09-29 15:18:18 -0400797/*
798 * dead roots are old snapshots that need to be deleted. This allocates
799 * a dirty root struct and adds it into the list of dead roots that need to
800 * be deleted
801 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400802int btrfs_add_dead_root(struct btrfs_root *root)
Chris Mason5eda7b52007-06-22 14:16:25 -0400803{
Josef Bacika4abeea2011-04-11 17:25:13 -0400804 spin_lock(&root->fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400805 list_add(&root->root_list, &root->fs_info->dead_roots);
Josef Bacika4abeea2011-04-11 17:25:13 -0400806 spin_unlock(&root->fs_info->trans_lock);
Chris Mason5eda7b52007-06-22 14:16:25 -0400807 return 0;
808}
809
Chris Masond352ac62008-09-29 15:18:18 -0400810/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400811 * update all the cowonly tree roots on disk
Chris Masond352ac62008-09-29 15:18:18 -0400812 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
814 struct btrfs_root *root)
Chris Mason0f7d52f2007-04-09 10:42:37 -0400815{
Chris Mason0f7d52f2007-04-09 10:42:37 -0400816 struct btrfs_root *gang[8];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400817 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400818 int i;
819 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400820 int err = 0;
821
Josef Bacika4abeea2011-04-11 17:25:13 -0400822 spin_lock(&fs_info->fs_roots_radix_lock);
Chris Masond3977122009-01-05 21:25:51 -0500823 while (1) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400824 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
825 (void **)gang, 0,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400826 ARRAY_SIZE(gang),
827 BTRFS_ROOT_TRANS_TAG);
828 if (ret == 0)
829 break;
830 for (i = 0; i < ret; i++) {
831 root = gang[i];
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400832 radix_tree_tag_clear(&fs_info->fs_roots_radix,
833 (unsigned long)root->root_key.objectid,
834 BTRFS_ROOT_TRANS_TAG);
Josef Bacika4abeea2011-04-11 17:25:13 -0400835 spin_unlock(&fs_info->fs_roots_radix_lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400836
Chris Masone02119d2008-09-05 16:13:11 -0400837 btrfs_free_log(trans, root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 btrfs_update_reloc_root(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400839 btrfs_orphan_commit_root(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -0400840
Li Zefan82d59022011-04-20 10:33:24 +0800841 btrfs_save_ino_cache(root, trans);
842
Liu Bof1ebcc72011-11-14 20:48:06 -0500843 /* see comments in should_cow_block() */
844 root->force_cow = 0;
845 smp_wmb();
846
Yan Zheng978d9102009-06-15 20:01:02 -0400847 if (root->commit_root != root->node) {
Li Zefan581bb052011-04-20 10:06:11 +0800848 mutex_lock(&root->fs_commit_mutex);
Josef Bacik817d52f2009-07-13 21:29:25 -0400849 switch_commit_root(root);
Li Zefan581bb052011-04-20 10:06:11 +0800850 btrfs_unpin_free_ino(root);
851 mutex_unlock(&root->fs_commit_mutex);
852
Yan Zheng978d9102009-06-15 20:01:02 -0400853 btrfs_set_root_node(&root->root_item,
854 root->node);
855 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400856
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 err = btrfs_update_root(trans, fs_info->tree_root,
Chris Mason0f7d52f2007-04-09 10:42:37 -0400858 &root->root_key,
859 &root->root_item);
Josef Bacika4abeea2011-04-11 17:25:13 -0400860 spin_lock(&fs_info->fs_roots_radix_lock);
Chris Mason54aa1f42007-06-22 14:16:25 -0400861 if (err)
862 break;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400863 }
864 }
Josef Bacika4abeea2011-04-11 17:25:13 -0400865 spin_unlock(&fs_info->fs_roots_radix_lock);
Chris Mason54aa1f42007-06-22 14:16:25 -0400866 return err;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400867}
868
Chris Masond352ac62008-09-29 15:18:18 -0400869/*
870 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
871 * otherwise every leaf in the btree is read and defragged.
872 */
Chris Masone9d0b132007-08-10 14:06:19 -0400873int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
874{
875 struct btrfs_fs_info *info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -0400876 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400877 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400878 unsigned long nr;
Chris Masone9d0b132007-08-10 14:06:19 -0400879
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400880 if (xchg(&root->defrag_running, 1))
Chris Masone9d0b132007-08-10 14:06:19 -0400881 return 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400882
Chris Mason6b800532007-10-15 16:17:34 -0400883 while (1) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400884 trans = btrfs_start_transaction(root, 0);
885 if (IS_ERR(trans))
886 return PTR_ERR(trans);
887
Chris Masone9d0b132007-08-10 14:06:19 -0400888 ret = btrfs_defrag_leaves(trans, root, cacheonly);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400889
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400890 nr = trans->blocks_used;
Chris Masone9d0b132007-08-10 14:06:19 -0400891 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400892 btrfs_btree_balance_dirty(info->tree_root, nr);
Chris Masone9d0b132007-08-10 14:06:19 -0400893 cond_resched();
894
David Sterba7841cb22011-05-31 18:07:27 +0200895 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
Chris Masone9d0b132007-08-10 14:06:19 -0400896 break;
897 }
898 root->defrag_running = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -0400899 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -0400900}
901
Chris Masond352ac62008-09-29 15:18:18 -0400902/*
903 * new snapshots need to be created at a very specific time in the
904 * transaction commit. This does the actual creation
905 */
Chris Mason80b67942008-02-01 16:35:04 -0500906static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
Chris Mason3063d292008-01-08 15:46:30 -0500907 struct btrfs_fs_info *fs_info,
908 struct btrfs_pending_snapshot *pending)
909{
910 struct btrfs_key key;
Chris Mason80b67942008-02-01 16:35:04 -0500911 struct btrfs_root_item *new_root_item;
Chris Mason3063d292008-01-08 15:46:30 -0500912 struct btrfs_root *tree_root = fs_info->tree_root;
913 struct btrfs_root *root = pending->root;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000914 struct btrfs_root *parent_root;
Liu Bo98c99422011-09-11 10:52:24 -0400915 struct btrfs_block_rsv *rsv;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000916 struct inode *parent_inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000917 struct dentry *parent;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400918 struct dentry *dentry;
Chris Mason3063d292008-01-08 15:46:30 -0500919 struct extent_buffer *tmp;
Chris Mason925baed2008-06-25 16:01:30 -0400920 struct extent_buffer *old;
Chris Mason3063d292008-01-08 15:46:30 -0500921 int ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -0400922 u64 to_reserve = 0;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000923 u64 index = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400924 u64 objectid;
Li Zefanb83cc962010-12-20 16:04:08 +0800925 u64 root_flags;
Chris Mason3063d292008-01-08 15:46:30 -0500926
Liu Bo98c99422011-09-11 10:52:24 -0400927 rsv = trans->block_rsv;
928
Chris Mason80b67942008-02-01 16:35:04 -0500929 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
930 if (!new_root_item) {
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100931 ret = pending->error = -ENOMEM;
Chris Mason80b67942008-02-01 16:35:04 -0500932 goto fail;
933 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400934
Li Zefan581bb052011-04-20 10:06:11 +0800935 ret = btrfs_find_free_objectid(tree_root, &objectid);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400936 if (ret) {
937 pending->error = ret;
Chris Mason3063d292008-01-08 15:46:30 -0500938 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400939 }
Chris Mason3063d292008-01-08 15:46:30 -0500940
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400941 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400942
943 if (to_reserve > 0) {
Miao Xie62f30c52011-11-10 20:45:05 -0500944 ret = btrfs_block_rsv_add_noflush(root, &pending->block_rsv,
945 to_reserve);
Yan, Zhengd68fc572010-05-16 10:49:58 -0400946 if (ret) {
947 pending->error = ret;
948 goto fail;
949 }
950 }
951
Chris Mason3063d292008-01-08 15:46:30 -0500952 key.objectid = objectid;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400953 key.offset = (u64)-1;
954 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Mason3063d292008-01-08 15:46:30 -0500955
Yan, Zhenga22285a2010-05-16 10:48:46 -0400956 trans->block_rsv = &pending->block_rsv;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000957
Yan, Zhenga22285a2010-05-16 10:48:46 -0400958 dentry = pending->dentry;
Josef Bacik6a912212010-11-20 09:48:00 +0000959 parent = dget_parent(dentry);
960 parent_inode = parent->d_inode;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400961 parent_root = BTRFS_I(parent_inode)->root;
Chris Mason75857172011-06-13 20:00:16 -0400962 record_root_in_trans(trans, parent_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400963
Sage Weil6bdb72d2010-03-15 17:27:13 +0000964 /*
965 * insert the directory item
966 */
Sage Weil6bdb72d2010-03-15 17:27:13 +0000967 ret = btrfs_set_inode_index(parent_inode, &index);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100968 BUG_ON(ret); /* -ENOMEM */
Sage Weil6bdb72d2010-03-15 17:27:13 +0000969 ret = btrfs_insert_dir_item(trans, parent_root,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400970 dentry->d_name.name, dentry->d_name.len,
Miao Xie16cdcec2011-04-22 18:12:22 +0800971 parent_inode, &key,
Yan, Zhenga22285a2010-05-16 10:48:46 -0400972 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100973 if (ret == -EEXIST) {
Chris Masonfe66a052012-02-20 08:40:56 -0500974 pending->error = -EEXIST;
975 dput(parent);
976 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 } else if (ret) {
978 goto abort_trans_dput;
979 }
Sage Weil6bdb72d2010-03-15 17:27:13 +0000980
Yan, Zhenga22285a2010-05-16 10:48:46 -0400981 btrfs_i_size_write(parent_inode, parent_inode->i_size +
982 dentry->d_name.len * 2);
Sage Weil6bdb72d2010-03-15 17:27:13 +0000983 ret = btrfs_update_inode(trans, parent_root, parent_inode);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100984 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100985 goto abort_trans_dput;
Sage Weil6bdb72d2010-03-15 17:27:13 +0000986
Chris Masone9993762011-06-17 16:14:09 -0400987 /*
988 * pull in the delayed directory update
989 * and the delayed inode item
990 * otherwise we corrupt the FS during
991 * snapshot
992 */
993 ret = btrfs_run_delayed_items(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 if (ret) { /* Transaction aborted */
995 dput(parent);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100996 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100997 }
Chris Masone9993762011-06-17 16:14:09 -0400998
Chris Mason75857172011-06-13 20:00:16 -0400999 record_root_in_trans(trans, root);
Sage Weil6bdb72d2010-03-15 17:27:13 +00001000 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1001 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
Li Zefan08fe4db2011-03-28 02:01:25 +00001002 btrfs_check_and_init_root_item(new_root_item);
Sage Weil6bdb72d2010-03-15 17:27:13 +00001003
Li Zefanb83cc962010-12-20 16:04:08 +08001004 root_flags = btrfs_root_flags(new_root_item);
1005 if (pending->readonly)
1006 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1007 else
1008 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1009 btrfs_set_root_flags(new_root_item, root_flags);
1010
Chris Mason925baed2008-06-25 16:01:30 -04001011 old = btrfs_lock_root_node(root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001012 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001013 if (ret) {
1014 btrfs_tree_unlock(old);
1015 free_extent_buffer(old);
1016 goto abort_trans_dput;
1017 }
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 btrfs_set_lock_blocking(old);
Chris Mason3063d292008-01-08 15:46:30 -05001020
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001021 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001022 /* clean up in any case */
Chris Mason925baed2008-06-25 16:01:30 -04001023 btrfs_tree_unlock(old);
1024 free_extent_buffer(old);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025 if (ret)
1026 goto abort_trans_dput;
Chris Mason3063d292008-01-08 15:46:30 -05001027
Liu Bof1ebcc72011-11-14 20:48:06 -05001028 /* see comments in should_cow_block() */
1029 root->force_cow = 1;
1030 smp_wmb();
1031
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 btrfs_set_root_node(new_root_item, tmp);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001033 /* record when the snapshot was created in key.offset */
1034 key.offset = trans->transid;
1035 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
Chris Mason925baed2008-06-25 16:01:30 -04001036 btrfs_tree_unlock(tmp);
Chris Mason3063d292008-01-08 15:46:30 -05001037 free_extent_buffer(tmp);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001038 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 goto abort_trans_dput;
Chris Mason0660b5a2008-11-17 20:37:39 -05001040
Yan, Zhenga22285a2010-05-16 10:48:46 -04001041 /*
1042 * insert root back/forward references
1043 */
1044 ret = btrfs_add_root_ref(trans, tree_root, objectid,
1045 parent_root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001046 btrfs_ino(parent_inode), index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04001047 dentry->d_name.name, dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001048 dput(parent);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001049 if (ret)
1050 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001051
1052 key.offset = (u64)-1;
1053 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001054 if (IS_ERR(pending->snap)) {
1055 ret = PTR_ERR(pending->snap);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001056 goto abort_trans;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001057 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04001058
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001059 ret = btrfs_reloc_post_snapshot(trans, pending);
1060 if (ret)
1061 goto abort_trans;
1062 ret = 0;
Chris Mason3063d292008-01-08 15:46:30 -05001063fail:
Sage Weil6bdb72d2010-03-15 17:27:13 +00001064 kfree(new_root_item);
Liu Bo98c99422011-09-11 10:52:24 -04001065 trans->block_rsv = rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001066 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001067 return ret;
1068
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001069abort_trans_dput:
1070 dput(parent);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001071abort_trans:
1072 btrfs_abort_transaction(trans, root, ret);
1073 goto fail;
Chris Mason3063d292008-01-08 15:46:30 -05001074}
1075
Chris Masond352ac62008-09-29 15:18:18 -04001076/*
1077 * create all the snapshots we've scheduled for creation
1078 */
Chris Mason80b67942008-02-01 16:35:04 -05001079static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1080 struct btrfs_fs_info *fs_info)
Chris Mason3063d292008-01-08 15:46:30 -05001081{
1082 struct btrfs_pending_snapshot *pending;
1083 struct list_head *head = &trans->transaction->pending_snapshots;
Chris Mason3de45862008-11-17 21:02:50 -05001084
Chris Masonfe66a052012-02-20 08:40:56 -05001085 list_for_each_entry(pending, head, list)
1086 create_pending_snapshot(trans, fs_info, pending);
Chris Mason3de45862008-11-17 21:02:50 -05001087 return 0;
1088}
1089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090static void update_super_roots(struct btrfs_root *root)
1091{
1092 struct btrfs_root_item *root_item;
1093 struct btrfs_super_block *super;
1094
David Sterba6c417612011-04-13 15:41:04 +02001095 super = root->fs_info->super_copy;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096
1097 root_item = &root->fs_info->chunk_root->root_item;
1098 super->chunk_root = root_item->bytenr;
1099 super->chunk_root_generation = root_item->generation;
1100 super->chunk_root_level = root_item->level;
1101
1102 root_item = &root->fs_info->tree_root->root_item;
1103 super->root = root_item->bytenr;
1104 super->generation = root_item->generation;
1105 super->root_level = root_item->level;
Josef Bacik73bc1872011-10-03 14:07:49 -04001106 if (btrfs_test_opt(root, SPACE_CACHE))
Josef Bacik0af3d002010-06-21 14:48:16 -04001107 super->cache_generation = root_item->generation;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108}
1109
Chris Masonf36f3042009-07-30 10:04:48 -04001110int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1111{
1112 int ret = 0;
Josef Bacika4abeea2011-04-11 17:25:13 -04001113 spin_lock(&info->trans_lock);
Chris Masonf36f3042009-07-30 10:04:48 -04001114 if (info->running_transaction)
1115 ret = info->running_transaction->in_commit;
Josef Bacika4abeea2011-04-11 17:25:13 -04001116 spin_unlock(&info->trans_lock);
Chris Masonf36f3042009-07-30 10:04:48 -04001117 return ret;
1118}
1119
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001120int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1121{
1122 int ret = 0;
Josef Bacika4abeea2011-04-11 17:25:13 -04001123 spin_lock(&info->trans_lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001124 if (info->running_transaction)
1125 ret = info->running_transaction->blocked;
Josef Bacika4abeea2011-04-11 17:25:13 -04001126 spin_unlock(&info->trans_lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001127 return ret;
1128}
1129
Sage Weilbb9c12c2010-10-29 15:37:34 -04001130/*
1131 * wait for the current transaction commit to start and block subsequent
1132 * transaction joins
1133 */
1134static void wait_current_trans_commit_start(struct btrfs_root *root,
1135 struct btrfs_transaction *trans)
1136{
Li Zefan72d63ed2011-07-14 03:17:00 +00001137 wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001138}
1139
1140/*
1141 * wait for the current transaction to start and then become unblocked.
1142 * caller holds ref.
1143 */
1144static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1145 struct btrfs_transaction *trans)
1146{
Li Zefan72d63ed2011-07-14 03:17:00 +00001147 wait_event(root->fs_info->transaction_wait,
1148 trans->commit_done || (trans->in_commit && !trans->blocked));
Sage Weilbb9c12c2010-10-29 15:37:34 -04001149}
1150
1151/*
1152 * commit transactions asynchronously. once btrfs_commit_transaction_async
1153 * returns, any subsequent transaction will not be allowed to join.
1154 */
1155struct btrfs_async_commit {
1156 struct btrfs_trans_handle *newtrans;
1157 struct btrfs_root *root;
1158 struct delayed_work work;
1159};
1160
1161static void do_async_commit(struct work_struct *work)
1162{
1163 struct btrfs_async_commit *ac =
1164 container_of(work, struct btrfs_async_commit, work.work);
1165
1166 btrfs_commit_transaction(ac->newtrans, ac->root);
1167 kfree(ac);
1168}
1169
1170int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1171 struct btrfs_root *root,
1172 int wait_for_unblock)
1173{
1174 struct btrfs_async_commit *ac;
1175 struct btrfs_transaction *cur_trans;
1176
1177 ac = kmalloc(sizeof(*ac), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00001178 if (!ac)
1179 return -ENOMEM;
Sage Weilbb9c12c2010-10-29 15:37:34 -04001180
1181 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1182 ac->root = root;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001183 ac->newtrans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001184 if (IS_ERR(ac->newtrans)) {
1185 int err = PTR_ERR(ac->newtrans);
1186 kfree(ac);
1187 return err;
1188 }
Sage Weilbb9c12c2010-10-29 15:37:34 -04001189
1190 /* take transaction reference */
Sage Weilbb9c12c2010-10-29 15:37:34 -04001191 cur_trans = trans->transaction;
Josef Bacik13c5a932011-04-11 15:45:29 -04001192 atomic_inc(&cur_trans->use_count);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001193
1194 btrfs_end_transaction(trans, root);
1195 schedule_delayed_work(&ac->work, 0);
1196
1197 /* wait for transaction to start and unblock */
Sage Weilbb9c12c2010-10-29 15:37:34 -04001198 if (wait_for_unblock)
1199 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1200 else
1201 wait_current_trans_commit_start(root, cur_trans);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001202
Sage Weil38e88052011-06-10 18:43:13 +00001203 if (current->journal_info == trans)
1204 current->journal_info = NULL;
1205
1206 put_transaction(cur_trans);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001207 return 0;
1208}
1209
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001210
1211static void cleanup_transaction(struct btrfs_trans_handle *trans,
1212 struct btrfs_root *root)
1213{
1214 struct btrfs_transaction *cur_trans = trans->transaction;
1215
1216 WARN_ON(trans->use_count > 1);
1217
1218 spin_lock(&root->fs_info->trans_lock);
1219 list_del_init(&cur_trans->list);
1220 spin_unlock(&root->fs_info->trans_lock);
1221
1222 btrfs_cleanup_one_transaction(trans->transaction, root);
1223
1224 put_transaction(cur_trans);
1225 put_transaction(cur_trans);
1226
1227 trace_btrfs_transaction_commit(root);
1228
1229 btrfs_scrub_continue(root);
1230
1231 if (current->journal_info == trans)
1232 current->journal_info = NULL;
1233
1234 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1235}
1236
Sage Weilbb9c12c2010-10-29 15:37:34 -04001237/*
1238 * btrfs_transaction state sequence:
1239 * in_commit = 0, blocked = 0 (initial)
1240 * in_commit = 1, blocked = 1
1241 * blocked = 0
1242 * commit_done = 1
1243 */
Chris Mason79154b12007-03-22 15:59:16 -04001244int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1245 struct btrfs_root *root)
1246{
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001247 unsigned long joined = 0;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001248 struct btrfs_transaction *cur_trans = trans->transaction;
Chris Mason8fd17792007-04-19 21:01:03 -04001249 struct btrfs_transaction *prev_trans = NULL;
Chris Mason79154b12007-03-22 15:59:16 -04001250 DEFINE_WAIT(wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001251 int ret = -EIO;
Chris Mason89573b92009-03-12 20:12:45 -04001252 int should_grow = 0;
1253 unsigned long now = get_seconds();
Sage Weildccae992009-04-02 16:59:01 -04001254 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
Chris Mason79154b12007-03-22 15:59:16 -04001255
Chris Mason5a3f23d2009-03-31 13:27:11 -04001256 btrfs_run_ordered_operations(root, 0);
1257
Josef Bacikb24e03d2011-10-14 14:40:17 -04001258 btrfs_trans_release_metadata(trans, root);
Josef Bacik9c8d86d2011-09-19 11:58:54 -04001259 trans->block_rsv = NULL;
1260
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001261 if (cur_trans->aborted)
1262 goto cleanup_transaction;
1263
Chris Mason56bec292009-03-13 10:10:06 -04001264 /* make a pass through all the delayed refs we have so far
1265 * any runnings procs may add more while we are here
1266 */
1267 ret = btrfs_run_delayed_refs(trans, root, 0);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001268 if (ret)
1269 goto cleanup_transaction;
Chris Mason56bec292009-03-13 10:10:06 -04001270
Chris Masonb7ec40d2009-03-12 20:12:45 -04001271 cur_trans = trans->transaction;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001272
Chris Mason56bec292009-03-13 10:10:06 -04001273 /*
1274 * set the flushing flag so procs in this transaction have to
1275 * start sending their work down.
1276 */
Chris Masonb7ec40d2009-03-12 20:12:45 -04001277 cur_trans->delayed_refs.flushing = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001278
Chris Masonc3e69d52009-03-13 10:17:05 -04001279 ret = btrfs_run_delayed_refs(trans, root, 0);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001280 if (ret)
1281 goto cleanup_transaction;
Chris Mason56bec292009-03-13 10:10:06 -04001282
Josef Bacika4abeea2011-04-11 17:25:13 -04001283 spin_lock(&cur_trans->commit_lock);
Chris Masonb7ec40d2009-03-12 20:12:45 -04001284 if (cur_trans->in_commit) {
Josef Bacika4abeea2011-04-11 17:25:13 -04001285 spin_unlock(&cur_trans->commit_lock);
Josef Bacik13c5a932011-04-11 15:45:29 -04001286 atomic_inc(&cur_trans->use_count);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001287 ret = btrfs_end_transaction(trans, root);
Chris Masonccd467d2007-06-28 15:57:36 -04001288
Li Zefanb9c83002011-07-14 03:17:14 +00001289 wait_for_commit(root, cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001290
Chris Mason79154b12007-03-22 15:59:16 -04001291 put_transaction(cur_trans);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001292
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001293 return ret;
Chris Mason79154b12007-03-22 15:59:16 -04001294 }
Chris Mason4313b392008-01-03 09:08:48 -05001295
Chris Mason2c90e5d2007-04-02 10:50:19 -04001296 trans->transaction->in_commit = 1;
Chris Masonf9295742008-07-17 12:54:14 -04001297 trans->transaction->blocked = 1;
Josef Bacika4abeea2011-04-11 17:25:13 -04001298 spin_unlock(&cur_trans->commit_lock);
Sage Weilbb9c12c2010-10-29 15:37:34 -04001299 wake_up(&root->fs_info->transaction_blocked_wait);
1300
Josef Bacika4abeea2011-04-11 17:25:13 -04001301 spin_lock(&root->fs_info->trans_lock);
Chris Masonccd467d2007-06-28 15:57:36 -04001302 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1303 prev_trans = list_entry(cur_trans->list.prev,
1304 struct btrfs_transaction, list);
1305 if (!prev_trans->commit_done) {
Josef Bacik13c5a932011-04-11 15:45:29 -04001306 atomic_inc(&prev_trans->use_count);
Josef Bacika4abeea2011-04-11 17:25:13 -04001307 spin_unlock(&root->fs_info->trans_lock);
Chris Masonccd467d2007-06-28 15:57:36 -04001308
1309 wait_for_commit(root, prev_trans);
Chris Masonccd467d2007-06-28 15:57:36 -04001310
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001311 put_transaction(prev_trans);
Josef Bacika4abeea2011-04-11 17:25:13 -04001312 } else {
1313 spin_unlock(&root->fs_info->trans_lock);
Chris Masonccd467d2007-06-28 15:57:36 -04001314 }
Josef Bacika4abeea2011-04-11 17:25:13 -04001315 } else {
1316 spin_unlock(&root->fs_info->trans_lock);
Chris Masonccd467d2007-06-28 15:57:36 -04001317 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001318
Chris Mason89573b92009-03-12 20:12:45 -04001319 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1320 should_grow = 1;
1321
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001322 do {
Yan Zheng7ea394f2008-08-05 13:05:02 -04001323 int snap_pending = 0;
Josef Bacika4abeea2011-04-11 17:25:13 -04001324
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001325 joined = cur_trans->num_joined;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001326 if (!list_empty(&trans->transaction->pending_snapshots))
1327 snap_pending = 1;
1328
Chris Mason2c90e5d2007-04-02 10:50:19 -04001329 WARN_ON(cur_trans != trans->transaction);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001330
Sage Weil0bdb1db2010-02-19 14:13:50 -08001331 if (flush_on_commit || snap_pending) {
Yan, Zheng24bbcf042009-11-12 09:36:34 +00001332 btrfs_start_delalloc_inodes(root, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001333 btrfs_wait_ordered_extents(root, 0, 1);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001334 }
1335
Miao Xie16cdcec2011-04-22 18:12:22 +08001336 ret = btrfs_run_delayed_items(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001337 if (ret)
1338 goto cleanup_transaction;
Miao Xie16cdcec2011-04-22 18:12:22 +08001339
Chris Mason5a3f23d2009-03-31 13:27:11 -04001340 /*
1341 * rename don't use btrfs_join_transaction, so, once we
1342 * set the transaction to blocked above, we aren't going
1343 * to get any new ordered operations. We can safely run
1344 * it here and no for sure that nothing new will be added
1345 * to the list
1346 */
1347 btrfs_run_ordered_operations(root, 1);
1348
Chris Masoned3b3d3142010-05-25 10:12:41 -04001349 prepare_to_wait(&cur_trans->writer_wait, &wait,
1350 TASK_UNINTERRUPTIBLE);
1351
Josef Bacik13c5a932011-04-11 15:45:29 -04001352 if (atomic_read(&cur_trans->num_writers) > 1)
Sage Weil99d16cb2010-10-29 15:37:34 -04001353 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1354 else if (should_grow)
1355 schedule_timeout(1);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001356
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001357 finish_wait(&cur_trans->writer_wait, &wait);
Josef Bacik13c5a932011-04-11 15:45:29 -04001358 } while (atomic_read(&cur_trans->num_writers) > 1 ||
Chris Mason89573b92009-03-12 20:12:45 -04001359 (should_grow && cur_trans->num_joined != joined));
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001360
Chris Mason75857172011-06-13 20:00:16 -04001361 /*
Josef Baciked0ca142011-06-14 16:22:15 -04001362 * Ok now we need to make sure to block out any other joins while we
1363 * commit the transaction. We could have started a join before setting
1364 * no_join so make sure to wait for num_writers to == 1 again.
1365 */
1366 spin_lock(&root->fs_info->trans_lock);
1367 root->fs_info->trans_no_join = 1;
1368 spin_unlock(&root->fs_info->trans_lock);
1369 wait_event(cur_trans->writer_wait,
1370 atomic_read(&cur_trans->num_writers) == 1);
1371
Chris Masone038dca2011-06-17 14:16:13 -04001372 /*
Chris Mason75857172011-06-13 20:00:16 -04001373 * the reloc mutex makes sure that we stop
1374 * the balancing code from coming in and moving
1375 * extents around in the middle of the commit
1376 */
1377 mutex_lock(&root->fs_info->reloc_mutex);
1378
Chris Masone9993762011-06-17 16:14:09 -04001379 ret = btrfs_run_delayed_items(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001380 if (ret) {
1381 mutex_unlock(&root->fs_info->reloc_mutex);
1382 goto cleanup_transaction;
1383 }
Chris Mason3063d292008-01-08 15:46:30 -05001384
Chris Masone9993762011-06-17 16:14:09 -04001385 ret = create_pending_snapshots(trans, root->fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001386 if (ret) {
1387 mutex_unlock(&root->fs_info->reloc_mutex);
1388 goto cleanup_transaction;
1389 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001390
Chris Mason56bec292009-03-13 10:10:06 -04001391 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001392 if (ret) {
1393 mutex_unlock(&root->fs_info->reloc_mutex);
1394 goto cleanup_transaction;
1395 }
Chris Mason56bec292009-03-13 10:10:06 -04001396
Chris Masone9993762011-06-17 16:14:09 -04001397 /*
1398 * make sure none of the code above managed to slip in a
1399 * delayed item
1400 */
1401 btrfs_assert_delayed_root_empty(root);
1402
Chris Mason2c90e5d2007-04-02 10:50:19 -04001403 WARN_ON(cur_trans != trans->transaction);
Chris Masondc17ff82008-01-08 15:46:30 -05001404
Arne Jansena2de7332011-03-08 14:14:00 +01001405 btrfs_scrub_pause(root);
Chris Masone02119d2008-09-05 16:13:11 -04001406 /* btrfs_commit_tree_roots is responsible for getting the
1407 * various roots consistent with each other. Every pointer
1408 * in the tree of tree roots has to point to the most up to date
1409 * root for every subvolume and other tree. So, we have to keep
1410 * the tree logging code from jumping in and changing any
1411 * of the trees.
1412 *
1413 * At this point in the commit, there can't be any tree-log
1414 * writers, but a little lower down we drop the trans mutex
1415 * and let new people in. By holding the tree_log_mutex
1416 * from now until after the super is written, we avoid races
1417 * with the tree-log code.
1418 */
1419 mutex_lock(&root->fs_info->tree_log_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04001420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001421 ret = commit_fs_roots(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001422 if (ret) {
1423 mutex_unlock(&root->fs_info->tree_log_mutex);
David Sterba871383b2012-04-02 18:31:37 +02001424 mutex_unlock(&root->fs_info->reloc_mutex);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001425 goto cleanup_transaction;
1426 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001427
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001428 /* commit_fs_roots gets rid of all the tree log roots, it is now
Chris Masone02119d2008-09-05 16:13:11 -04001429 * safe to free the root of tree log roots
1430 */
1431 btrfs_free_log_root_tree(trans, root->fs_info);
1432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001433 ret = commit_cowonly_roots(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001434 if (ret) {
1435 mutex_unlock(&root->fs_info->tree_log_mutex);
David Sterba871383b2012-04-02 18:31:37 +02001436 mutex_unlock(&root->fs_info->reloc_mutex);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001437 goto cleanup_transaction;
1438 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001439
Yan Zheng11833d62009-09-11 16:11:19 -04001440 btrfs_prepare_extent_commit(trans, root);
1441
Chris Mason78fae272007-03-25 11:35:08 -04001442 cur_trans = root->fs_info->running_transaction;
Chris Mason5f39d392007-10-15 16:14:19 -04001443
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001444 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1445 root->fs_info->tree_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001446 switch_commit_root(root->fs_info->tree_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447
1448 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1449 root->fs_info->chunk_root->node);
Josef Bacik817d52f2009-07-13 21:29:25 -04001450 switch_commit_root(root->fs_info->chunk_root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451
1452 update_super_roots(root);
Chris Masone02119d2008-09-05 16:13:11 -04001453
1454 if (!root->fs_info->log_root_recovering) {
David Sterba6c417612011-04-13 15:41:04 +02001455 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
1456 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
Chris Masone02119d2008-09-05 16:13:11 -04001457 }
1458
David Sterba6c417612011-04-13 15:41:04 +02001459 memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
1460 sizeof(*root->fs_info->super_copy));
Chris Masonccd467d2007-06-28 15:57:36 -04001461
Chris Masonf9295742008-07-17 12:54:14 -04001462 trans->transaction->blocked = 0;
Josef Bacika4abeea2011-04-11 17:25:13 -04001463 spin_lock(&root->fs_info->trans_lock);
1464 root->fs_info->running_transaction = NULL;
1465 root->fs_info->trans_no_join = 0;
1466 spin_unlock(&root->fs_info->trans_lock);
Chris Mason75857172011-06-13 20:00:16 -04001467 mutex_unlock(&root->fs_info->reloc_mutex);
Chris Masonb7ec40d2009-03-12 20:12:45 -04001468
Chris Masonf9295742008-07-17 12:54:14 -04001469 wake_up(&root->fs_info->transaction_wait);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001470
Chris Mason79154b12007-03-22 15:59:16 -04001471 ret = btrfs_write_and_wait_transaction(trans, root);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001472 if (ret) {
1473 btrfs_error(root->fs_info, ret,
1474 "Error while writing out transaction.");
1475 mutex_unlock(&root->fs_info->tree_log_mutex);
1476 goto cleanup_transaction;
1477 }
1478
1479 ret = write_ctree_super(trans, root, 0);
1480 if (ret) {
1481 mutex_unlock(&root->fs_info->tree_log_mutex);
1482 goto cleanup_transaction;
1483 }
Chris Mason4313b392008-01-03 09:08:48 -05001484
Chris Masone02119d2008-09-05 16:13:11 -04001485 /*
1486 * the super is written, we can safely allow the tree-loggers
1487 * to go about their business
1488 */
1489 mutex_unlock(&root->fs_info->tree_log_mutex);
1490
Yan Zheng11833d62009-09-11 16:11:19 -04001491 btrfs_finish_extent_commit(trans, root);
Chris Mason4313b392008-01-03 09:08:48 -05001492
Chris Mason2c90e5d2007-04-02 10:50:19 -04001493 cur_trans->commit_done = 1;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001494
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001495 root->fs_info->last_trans_committed = cur_trans->transid;
Josef Bacik817d52f2009-07-13 21:29:25 -04001496
Chris Mason2c90e5d2007-04-02 10:50:19 -04001497 wake_up(&cur_trans->commit_wait);
Chris Mason3de45862008-11-17 21:02:50 -05001498
Josef Bacika4abeea2011-04-11 17:25:13 -04001499 spin_lock(&root->fs_info->trans_lock);
Josef Bacik13c5a932011-04-11 15:45:29 -04001500 list_del_init(&cur_trans->list);
Josef Bacika4abeea2011-04-11 17:25:13 -04001501 spin_unlock(&root->fs_info->trans_lock);
1502
Chris Mason79154b12007-03-22 15:59:16 -04001503 put_transaction(cur_trans);
Chris Mason78fae272007-03-25 11:35:08 -04001504 put_transaction(cur_trans);
Josef Bacik58176a92007-08-29 15:47:34 -04001505
liubo1abe9b82011-03-24 11:18:59 +00001506 trace_btrfs_transaction_commit(root);
1507
Arne Jansena2de7332011-03-08 14:14:00 +01001508 btrfs_scrub_continue(root);
1509
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510 if (current->journal_info == trans)
1511 current->journal_info = NULL;
1512
Chris Mason2c90e5d2007-04-02 10:50:19 -04001513 kmem_cache_free(btrfs_trans_handle_cachep, trans);
Yan, Zheng24bbcf042009-11-12 09:36:34 +00001514
1515 if (current != root->fs_info->transaction_kthread)
1516 btrfs_run_delayed_iputs(root);
1517
Chris Mason79154b12007-03-22 15:59:16 -04001518 return ret;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001519
1520cleanup_transaction:
1521 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1522// WARN_ON(1);
1523 if (current->journal_info == trans)
1524 current->journal_info = NULL;
1525 cleanup_transaction(trans, root);
1526
1527 return ret;
Chris Mason79154b12007-03-22 15:59:16 -04001528}
1529
Chris Masond352ac62008-09-29 15:18:18 -04001530/*
1531 * interface function to delete all the snapshots we have scheduled for deletion
1532 */
Chris Masone9d0b132007-08-10 14:06:19 -04001533int btrfs_clean_old_snapshots(struct btrfs_root *root)
1534{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001535 LIST_HEAD(list);
1536 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone9d0b132007-08-10 14:06:19 -04001537
Josef Bacika4abeea2011-04-11 17:25:13 -04001538 spin_lock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 list_splice_init(&fs_info->dead_roots, &list);
Josef Bacika4abeea2011-04-11 17:25:13 -04001540 spin_unlock(&fs_info->trans_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541
1542 while (!list_empty(&list)) {
Jeff Mahoney2c536792011-10-03 23:22:41 -04001543 int ret;
1544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001545 root = list_entry(list.next, struct btrfs_root, root_list);
Yan, Zheng76dda932009-09-21 16:00:26 -04001546 list_del(&root->root_list);
1547
Miao Xie16cdcec2011-04-22 18:12:22 +08001548 btrfs_kill_all_delayed_nodes(root);
1549
Yan, Zheng76dda932009-09-21 16:00:26 -04001550 if (btrfs_header_backref_rev(root->node) <
1551 BTRFS_MIXED_BACKREF_REV)
Jeff Mahoney2c536792011-10-03 23:22:41 -04001552 ret = btrfs_drop_snapshot(root, NULL, 0, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04001553 else
Jeff Mahoney2c536792011-10-03 23:22:41 -04001554 ret =btrfs_drop_snapshot(root, NULL, 1, 0);
1555 BUG_ON(ret < 0);
Chris Masone9d0b132007-08-10 14:06:19 -04001556 }
1557 return 0;
1558}