]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/transaction.c
Btrfs: remove unnecessary varient ->num_joined in btrfs_transaction structure
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / transaction.c
CommitLineData
6cbd5570
CM
1/*
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
79154b1b 19#include <linux/fs.h>
5a0e3ad6 20#include <linux/slab.h>
34088780 21#include <linux/sched.h>
d3c2fdcf 22#include <linux/writeback.h>
5f39d397 23#include <linux/pagemap.h>
5f2cc086 24#include <linux/blkdev.h>
8ea05e3a 25#include <linux/uuid.h>
79154b1b
CM
26#include "ctree.h"
27#include "disk-io.h"
28#include "transaction.h"
925baedd 29#include "locking.h"
e02119d5 30#include "tree-log.h"
581bb050 31#include "inode-map.h"
733f4fbb 32#include "volumes.h"
8dabb742 33#include "dev-replace.h"
79154b1b 34
0f7d52f4
CM
35#define BTRFS_ROOT_TRANS_TAG 0
36
48a3b636 37static void put_transaction(struct btrfs_transaction *transaction)
79154b1b 38{
13c5a93e
JB
39 WARN_ON(atomic_read(&transaction->use_count) == 0);
40 if (atomic_dec_and_test(&transaction->use_count)) {
a4abeea4 41 BUG_ON(!list_empty(&transaction->list));
00f04b88 42 WARN_ON(transaction->delayed_refs.root.rb_node);
2c90e5d6 43 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 44 }
79154b1b
CM
45}
46
817d52f8
JB
47static noinline void switch_commit_root(struct btrfs_root *root)
48{
817d52f8
JB
49 free_extent_buffer(root->commit_root);
50 root->commit_root = btrfs_root_node(root);
817d52f8
JB
51}
52
178260b2 53static inline int can_join_transaction(struct btrfs_transaction *trans,
0860adfd 54 unsigned int type)
178260b2
MX
55{
56 return !(trans->in_commit &&
0860adfd
MX
57 (type & TRANS_EXTWRITERS));
58}
59
60static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
61 unsigned int type)
62{
63 if (type & TRANS_EXTWRITERS)
64 atomic_inc(&trans->num_extwriters);
65}
66
67static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
68 unsigned int type)
69{
70 if (type & TRANS_EXTWRITERS)
71 atomic_dec(&trans->num_extwriters);
72}
73
74static inline void extwriter_counter_init(struct btrfs_transaction *trans,
75 unsigned int type)
76{
77 atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
78}
79
80static inline int extwriter_counter_read(struct btrfs_transaction *trans)
81{
82 return atomic_read(&trans->num_extwriters);
178260b2
MX
83}
84
d352ac68
CM
85/*
86 * either allocate a new transaction or hop into the existing one
87 */
0860adfd 88static noinline int join_transaction(struct btrfs_root *root, unsigned int type)
79154b1b
CM
89{
90 struct btrfs_transaction *cur_trans;
19ae4e81 91 struct btrfs_fs_info *fs_info = root->fs_info;
a4abeea4 92
19ae4e81 93 spin_lock(&fs_info->trans_lock);
d43317dc 94loop:
49b25e05 95 /* The file system has been taken offline. No new transactions. */
87533c47 96 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
19ae4e81 97 spin_unlock(&fs_info->trans_lock);
49b25e05
JM
98 return -EROFS;
99 }
100
19ae4e81 101 if (fs_info->trans_no_join) {
354aa0fb
MX
102 /*
103 * If we are JOIN_NOLOCK we're already committing a current
104 * transaction, we just need a handle to deal with something
105 * when committing the transaction, such as inode cache and
106 * space cache. It is a special case.
107 */
108 if (type != TRANS_JOIN_NOLOCK) {
19ae4e81 109 spin_unlock(&fs_info->trans_lock);
a4abeea4
JB
110 return -EBUSY;
111 }
112 }
113
19ae4e81 114 cur_trans = fs_info->running_transaction;
a4abeea4 115 if (cur_trans) {
871383be 116 if (cur_trans->aborted) {
19ae4e81 117 spin_unlock(&fs_info->trans_lock);
49b25e05 118 return cur_trans->aborted;
871383be 119 }
178260b2
MX
120 if (!can_join_transaction(cur_trans, type)) {
121 spin_unlock(&fs_info->trans_lock);
122 return -EBUSY;
123 }
a4abeea4 124 atomic_inc(&cur_trans->use_count);
13c5a93e 125 atomic_inc(&cur_trans->num_writers);
0860adfd 126 extwriter_counter_inc(cur_trans, type);
19ae4e81 127 spin_unlock(&fs_info->trans_lock);
a4abeea4 128 return 0;
79154b1b 129 }
19ae4e81 130 spin_unlock(&fs_info->trans_lock);
a4abeea4 131
354aa0fb
MX
132 /*
133 * If we are ATTACH, we just want to catch the current transaction,
134 * and commit it. If there is no transaction, just return ENOENT.
135 */
136 if (type == TRANS_ATTACH)
137 return -ENOENT;
138
a4abeea4
JB
139 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
140 if (!cur_trans)
141 return -ENOMEM;
d43317dc 142
19ae4e81
JS
143 spin_lock(&fs_info->trans_lock);
144 if (fs_info->running_transaction) {
d43317dc
CM
145 /*
146 * someone started a transaction after we unlocked. Make sure
147 * to redo the trans_no_join checks above
148 */
a4abeea4 149 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
d43317dc 150 goto loop;
87533c47 151 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
e4b50e14 152 spin_unlock(&fs_info->trans_lock);
7b8b92af
JB
153 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
154 return -EROFS;
79154b1b 155 }
d43317dc 156
a4abeea4 157 atomic_set(&cur_trans->num_writers, 1);
0860adfd 158 extwriter_counter_init(cur_trans, type);
a4abeea4
JB
159 init_waitqueue_head(&cur_trans->writer_wait);
160 init_waitqueue_head(&cur_trans->commit_wait);
161 cur_trans->in_commit = 0;
162 cur_trans->blocked = 0;
163 /*
164 * One for this trans handle, one so it will live on until we
165 * commit the transaction.
166 */
167 atomic_set(&cur_trans->use_count, 2);
168 cur_trans->commit_done = 0;
169 cur_trans->start_time = get_seconds();
170
171 cur_trans->delayed_refs.root = RB_ROOT;
172 cur_trans->delayed_refs.num_entries = 0;
173 cur_trans->delayed_refs.num_heads_ready = 0;
174 cur_trans->delayed_refs.num_heads = 0;
175 cur_trans->delayed_refs.flushing = 0;
176 cur_trans->delayed_refs.run_delayed_start = 0;
20b297d6
JS
177
178 /*
179 * although the tree mod log is per file system and not per transaction,
180 * the log must never go across transaction boundaries.
181 */
182 smp_mb();
31b1a2bd
JL
183 if (!list_empty(&fs_info->tree_mod_seq_list))
184 WARN(1, KERN_ERR "btrfs: tree_mod_seq_list not empty when "
20b297d6 185 "creating a fresh transaction\n");
31b1a2bd
JL
186 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
187 WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
20b297d6 188 "creating a fresh transaction\n");
fc36ed7e 189 atomic64_set(&fs_info->tree_mod_seq, 0);
20b297d6 190
a4abeea4
JB
191 spin_lock_init(&cur_trans->commit_lock);
192 spin_lock_init(&cur_trans->delayed_refs.lock);
bb721703
CM
193 atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
194 atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
195 init_waitqueue_head(&cur_trans->delayed_refs.wait);
a4abeea4
JB
196
197 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
569e0f35 198 INIT_LIST_HEAD(&cur_trans->ordered_operations);
19ae4e81 199 list_add_tail(&cur_trans->list, &fs_info->trans_list);
a4abeea4 200 extent_io_tree_init(&cur_trans->dirty_pages,
19ae4e81
JS
201 fs_info->btree_inode->i_mapping);
202 fs_info->generation++;
203 cur_trans->transid = fs_info->generation;
204 fs_info->running_transaction = cur_trans;
49b25e05 205 cur_trans->aborted = 0;
19ae4e81 206 spin_unlock(&fs_info->trans_lock);
15ee9bc7 207
79154b1b
CM
208 return 0;
209}
210
d352ac68 211/*
d397712b
CM
212 * this does all the record keeping required to make sure that a reference
213 * counted root is properly recorded in a given transaction. This is required
214 * to make sure the old root from before we joined the transaction is deleted
215 * when the transaction commits
d352ac68 216 */
7585717f 217static int record_root_in_trans(struct btrfs_trans_handle *trans,
a4abeea4 218 struct btrfs_root *root)
6702ed49 219{
5d4f98a2 220 if (root->ref_cows && root->last_trans < trans->transid) {
6702ed49 221 WARN_ON(root == root->fs_info->extent_root);
5d4f98a2
YZ
222 WARN_ON(root->commit_root != root->node);
223
7585717f
CM
224 /*
225 * see below for in_trans_setup usage rules
226 * we have the reloc mutex held now, so there
227 * is only one writer in this function
228 */
229 root->in_trans_setup = 1;
230
231 /* make sure readers find in_trans_setup before
232 * they find our root->last_trans update
233 */
234 smp_wmb();
235
a4abeea4
JB
236 spin_lock(&root->fs_info->fs_roots_radix_lock);
237 if (root->last_trans == trans->transid) {
238 spin_unlock(&root->fs_info->fs_roots_radix_lock);
239 return 0;
240 }
5d4f98a2
YZ
241 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
242 (unsigned long)root->root_key.objectid,
243 BTRFS_ROOT_TRANS_TAG);
a4abeea4 244 spin_unlock(&root->fs_info->fs_roots_radix_lock);
7585717f
CM
245 root->last_trans = trans->transid;
246
247 /* this is pretty tricky. We don't want to
248 * take the relocation lock in btrfs_record_root_in_trans
249 * unless we're really doing the first setup for this root in
250 * this transaction.
251 *
252 * Normally we'd use root->last_trans as a flag to decide
253 * if we want to take the expensive mutex.
254 *
255 * But, we have to set root->last_trans before we
256 * init the relocation root, otherwise, we trip over warnings
257 * in ctree.c. The solution used here is to flag ourselves
258 * with root->in_trans_setup. When this is 1, we're still
259 * fixing up the reloc trees and everyone must wait.
260 *
261 * When this is zero, they can trust root->last_trans and fly
262 * through btrfs_record_root_in_trans without having to take the
263 * lock. smp_wmb() makes sure that all the writes above are
264 * done before we pop in the zero below
265 */
5d4f98a2 266 btrfs_init_reloc_root(trans, root);
7585717f
CM
267 smp_wmb();
268 root->in_trans_setup = 0;
5d4f98a2
YZ
269 }
270 return 0;
271}
bcc63abb 272
7585717f
CM
273
274int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
275 struct btrfs_root *root)
276{
277 if (!root->ref_cows)
278 return 0;
279
280 /*
281 * see record_root_in_trans for comments about in_trans_setup usage
282 * and barriers
283 */
284 smp_rmb();
285 if (root->last_trans == trans->transid &&
286 !root->in_trans_setup)
287 return 0;
288
289 mutex_lock(&root->fs_info->reloc_mutex);
290 record_root_in_trans(trans, root);
291 mutex_unlock(&root->fs_info->reloc_mutex);
292
293 return 0;
294}
295
d352ac68
CM
296/* wait for commit against the current transaction to become unblocked
297 * when this is done, it is safe to start a new transaction, but the current
298 * transaction might not be fully on disk.
299 */
37d1aeee 300static void wait_current_trans(struct btrfs_root *root)
79154b1b 301{
f9295749 302 struct btrfs_transaction *cur_trans;
79154b1b 303
a4abeea4 304 spin_lock(&root->fs_info->trans_lock);
f9295749 305 cur_trans = root->fs_info->running_transaction;
37d1aeee 306 if (cur_trans && cur_trans->blocked) {
13c5a93e 307 atomic_inc(&cur_trans->use_count);
a4abeea4 308 spin_unlock(&root->fs_info->trans_lock);
72d63ed6
LZ
309
310 wait_event(root->fs_info->transaction_wait,
311 !cur_trans->blocked);
f9295749 312 put_transaction(cur_trans);
a4abeea4
JB
313 } else {
314 spin_unlock(&root->fs_info->trans_lock);
f9295749 315 }
37d1aeee
CM
316}
317
a22285a6
YZ
318static int may_wait_transaction(struct btrfs_root *root, int type)
319{
a4abeea4
JB
320 if (root->fs_info->log_root_recovering)
321 return 0;
322
323 if (type == TRANS_USERSPACE)
324 return 1;
325
326 if (type == TRANS_START &&
327 !atomic_read(&root->fs_info->open_ioctl_trans))
a22285a6 328 return 1;
a4abeea4 329
a22285a6
YZ
330 return 0;
331}
332
08e007d2 333static struct btrfs_trans_handle *
0860adfd 334start_transaction(struct btrfs_root *root, u64 num_items, unsigned int type,
08e007d2 335 enum btrfs_reserve_flush_enum flush)
37d1aeee 336{
a22285a6
YZ
337 struct btrfs_trans_handle *h;
338 struct btrfs_transaction *cur_trans;
b5009945 339 u64 num_bytes = 0;
37d1aeee 340 int ret;
c5567237 341 u64 qgroup_reserved = 0;
acce952b 342
87533c47 343 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
acce952b 344 return ERR_PTR(-EROFS);
2a1eb461
JB
345
346 if (current->journal_info) {
0860adfd 347 WARN_ON(type & TRANS_EXTWRITERS);
2a1eb461
JB
348 h = current->journal_info;
349 h->use_count++;
b7d5b0a8 350 WARN_ON(h->use_count > 2);
2a1eb461
JB
351 h->orig_rsv = h->block_rsv;
352 h->block_rsv = NULL;
353 goto got_it;
354 }
b5009945
JB
355
356 /*
357 * Do the reservation before we join the transaction so we can do all
358 * the appropriate flushing if need be.
359 */
360 if (num_items > 0 && root != root->fs_info->chunk_root) {
c5567237
AJ
361 if (root->fs_info->quota_enabled &&
362 is_fstree(root->root_key.objectid)) {
363 qgroup_reserved = num_items * root->leafsize;
364 ret = btrfs_qgroup_reserve(root, qgroup_reserved);
365 if (ret)
366 return ERR_PTR(ret);
367 }
368
b5009945 369 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
08e007d2
MX
370 ret = btrfs_block_rsv_add(root,
371 &root->fs_info->trans_block_rsv,
372 num_bytes, flush);
b5009945 373 if (ret)
843fcf35 374 goto reserve_fail;
b5009945 375 }
a22285a6
YZ
376again:
377 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
843fcf35
MX
378 if (!h) {
379 ret = -ENOMEM;
380 goto alloc_fail;
381 }
37d1aeee 382
98114659
JB
383 /*
384 * If we are JOIN_NOLOCK we're already committing a transaction and
385 * waiting on this guy, so we don't need to do the sb_start_intwrite
386 * because we're already holding a ref. We need this because we could
387 * have raced in and did an fsync() on a file which can kick a commit
388 * and then we deadlock with somebody doing a freeze.
354aa0fb
MX
389 *
390 * If we are ATTACH, it means we just want to catch the current
391 * transaction and commit it, so we needn't do sb_start_intwrite().
98114659 392 */
0860adfd 393 if (type & __TRANS_FREEZABLE)
60376ce4 394 sb_start_intwrite(root->fs_info->sb);
b2b5ef5c 395
a22285a6 396 if (may_wait_transaction(root, type))
37d1aeee 397 wait_current_trans(root);
a22285a6 398
a4abeea4 399 do {
354aa0fb 400 ret = join_transaction(root, type);
178260b2 401 if (ret == -EBUSY) {
a4abeea4 402 wait_current_trans(root);
178260b2
MX
403 if (unlikely(type == TRANS_ATTACH))
404 ret = -ENOENT;
405 }
a4abeea4
JB
406 } while (ret == -EBUSY);
407
db5b493a 408 if (ret < 0) {
354aa0fb
MX
409 /* We must get the transaction if we are JOIN_NOLOCK. */
410 BUG_ON(type == TRANS_JOIN_NOLOCK);
843fcf35 411 goto join_fail;
db5b493a 412 }
0f7d52f4 413
a22285a6 414 cur_trans = root->fs_info->running_transaction;
a22285a6
YZ
415
416 h->transid = cur_trans->transid;
417 h->transaction = cur_trans;
79154b1b 418 h->blocks_used = 0;
a22285a6 419 h->bytes_reserved = 0;
d13603ef 420 h->root = root;
56bec294 421 h->delayed_ref_updates = 0;
2a1eb461 422 h->use_count = 1;
0e721106 423 h->adding_csums = 0;
f0486c68 424 h->block_rsv = NULL;
2a1eb461 425 h->orig_rsv = NULL;
49b25e05 426 h->aborted = 0;
4b824906 427 h->qgroup_reserved = 0;
bed92eae 428 h->delayed_ref_elem.seq = 0;
a698d075 429 h->type = type;
c6b305a8 430 h->allocating_chunk = false;
bed92eae 431 INIT_LIST_HEAD(&h->qgroup_ref_list);
ea658bad 432 INIT_LIST_HEAD(&h->new_bgs);
b7ec40d7 433
a22285a6
YZ
434 smp_mb();
435 if (cur_trans->blocked && may_wait_transaction(root, type)) {
436 btrfs_commit_transaction(h, root);
437 goto again;
438 }
439
b5009945 440 if (num_bytes) {
8c2a3ca2 441 trace_btrfs_space_reservation(root->fs_info, "transaction",
2bcc0328 442 h->transid, num_bytes, 1);
b5009945
JB
443 h->block_rsv = &root->fs_info->trans_block_rsv;
444 h->bytes_reserved = num_bytes;
a22285a6 445 }
4b824906 446 h->qgroup_reserved = qgroup_reserved;
9ed74f2d 447
2a1eb461 448got_it:
a4abeea4 449 btrfs_record_root_in_trans(h, root);
a22285a6
YZ
450
451 if (!current->journal_info && type != TRANS_USERSPACE)
452 current->journal_info = h;
79154b1b 453 return h;
843fcf35
MX
454
455join_fail:
0860adfd 456 if (type & __TRANS_FREEZABLE)
843fcf35
MX
457 sb_end_intwrite(root->fs_info->sb);
458 kmem_cache_free(btrfs_trans_handle_cachep, h);
459alloc_fail:
460 if (num_bytes)
461 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
462 num_bytes);
463reserve_fail:
464 if (qgroup_reserved)
465 btrfs_qgroup_free(root, qgroup_reserved);
466 return ERR_PTR(ret);
79154b1b
CM
467}
468
f9295749 469struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
a22285a6 470 int num_items)
f9295749 471{
08e007d2
MX
472 return start_transaction(root, num_items, TRANS_START,
473 BTRFS_RESERVE_FLUSH_ALL);
f9295749 474}
8407aa46 475
08e007d2 476struct btrfs_trans_handle *btrfs_start_transaction_lflush(
8407aa46
MX
477 struct btrfs_root *root, int num_items)
478{
08e007d2
MX
479 return start_transaction(root, num_items, TRANS_START,
480 BTRFS_RESERVE_FLUSH_LIMIT);
8407aa46
MX
481}
482
7a7eaa40 483struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
f9295749 484{
8407aa46 485 return start_transaction(root, 0, TRANS_JOIN, 0);
f9295749
CM
486}
487
7a7eaa40 488struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
0af3d00b 489{
8407aa46 490 return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
0af3d00b
JB
491}
492
7a7eaa40 493struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
9ca9ee09 494{
8407aa46 495 return start_transaction(root, 0, TRANS_USERSPACE, 0);
9ca9ee09
SW
496}
497
d4edf39b
MX
498/*
499 * btrfs_attach_transaction() - catch the running transaction
500 *
501 * It is used when we want to commit the current the transaction, but
502 * don't want to start a new one.
503 *
504 * Note: If this function return -ENOENT, it just means there is no
505 * running transaction. But it is possible that the inactive transaction
506 * is still in the memory, not fully on disk. If you hope there is no
507 * inactive transaction in the fs when -ENOENT is returned, you should
508 * invoke
509 * btrfs_attach_transaction_barrier()
510 */
354aa0fb 511struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
60376ce4 512{
354aa0fb 513 return start_transaction(root, 0, TRANS_ATTACH, 0);
60376ce4
JB
514}
515
d4edf39b
MX
516/*
517 * btrfs_attach_transaction() - catch the running transaction
518 *
519 * It is similar to the above function, the differentia is this one
520 * will wait for all the inactive transactions until they fully
521 * complete.
522 */
523struct btrfs_trans_handle *
524btrfs_attach_transaction_barrier(struct btrfs_root *root)
525{
526 struct btrfs_trans_handle *trans;
527
528 trans = start_transaction(root, 0, TRANS_ATTACH, 0);
529 if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
530 btrfs_wait_for_commit(root, 0);
531
532 return trans;
533}
534
d352ac68 535/* wait for a transaction commit to be fully complete */
b9c8300c 536static noinline void wait_for_commit(struct btrfs_root *root,
89ce8a63
CM
537 struct btrfs_transaction *commit)
538{
72d63ed6 539 wait_event(commit->commit_wait, commit->commit_done);
89ce8a63
CM
540}
541
46204592
SW
542int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
543{
544 struct btrfs_transaction *cur_trans = NULL, *t;
8cd2807f 545 int ret = 0;
46204592 546
46204592
SW
547 if (transid) {
548 if (transid <= root->fs_info->last_trans_committed)
a4abeea4 549 goto out;
46204592 550
8cd2807f 551 ret = -EINVAL;
46204592 552 /* find specified transaction */
a4abeea4 553 spin_lock(&root->fs_info->trans_lock);
46204592
SW
554 list_for_each_entry(t, &root->fs_info->trans_list, list) {
555 if (t->transid == transid) {
556 cur_trans = t;
a4abeea4 557 atomic_inc(&cur_trans->use_count);
8cd2807f 558 ret = 0;
46204592
SW
559 break;
560 }
8cd2807f
MX
561 if (t->transid > transid) {
562 ret = 0;
46204592 563 break;
8cd2807f 564 }
46204592 565 }
a4abeea4 566 spin_unlock(&root->fs_info->trans_lock);
8cd2807f 567 /* The specified transaction doesn't exist */
46204592 568 if (!cur_trans)
8cd2807f 569 goto out;
46204592
SW
570 } else {
571 /* find newest transaction that is committing | committed */
a4abeea4 572 spin_lock(&root->fs_info->trans_lock);
46204592
SW
573 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
574 list) {
575 if (t->in_commit) {
576 if (t->commit_done)
3473f3c0 577 break;
46204592 578 cur_trans = t;
a4abeea4 579 atomic_inc(&cur_trans->use_count);
46204592
SW
580 break;
581 }
582 }
a4abeea4 583 spin_unlock(&root->fs_info->trans_lock);
46204592 584 if (!cur_trans)
a4abeea4 585 goto out; /* nothing committing|committed */
46204592
SW
586 }
587
46204592 588 wait_for_commit(root, cur_trans);
46204592 589 put_transaction(cur_trans);
a4abeea4 590out:
46204592
SW
591 return ret;
592}
593
37d1aeee
CM
594void btrfs_throttle(struct btrfs_root *root)
595{
a4abeea4 596 if (!atomic_read(&root->fs_info->open_ioctl_trans))
9ca9ee09 597 wait_current_trans(root);
37d1aeee
CM
598}
599
8929ecfa
YZ
600static int should_end_transaction(struct btrfs_trans_handle *trans,
601 struct btrfs_root *root)
602{
603 int ret;
36ba022a
JB
604
605 ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
8929ecfa
YZ
606 return ret ? 1 : 0;
607}
608
609int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root)
611{
612 struct btrfs_transaction *cur_trans = trans->transaction;
613 int updates;
49b25e05 614 int err;
8929ecfa 615
a4abeea4 616 smp_mb();
8929ecfa
YZ
617 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
618 return 1;
619
620 updates = trans->delayed_ref_updates;
621 trans->delayed_ref_updates = 0;
49b25e05
JM
622 if (updates) {
623 err = btrfs_run_delayed_refs(trans, root, updates);
624 if (err) /* Error code will also eval true */
625 return err;
626 }
8929ecfa
YZ
627
628 return should_end_transaction(trans, root);
629}
630
89ce8a63 631static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
a698d075 632 struct btrfs_root *root, int throttle)
79154b1b 633{
8929ecfa 634 struct btrfs_transaction *cur_trans = trans->transaction;
ab78c84d 635 struct btrfs_fs_info *info = root->fs_info;
c3e69d58 636 int count = 0;
a698d075 637 int lock = (trans->type != TRANS_JOIN_NOLOCK);
4edc2ca3 638 int err = 0;
c3e69d58 639
2a1eb461
JB
640 if (--trans->use_count) {
641 trans->block_rsv = trans->orig_rsv;
642 return 0;
643 }
644
edf39272
JS
645 /*
646 * do the qgroup accounting as early as possible
647 */
648 err = btrfs_delayed_refs_qgroup_accounting(trans, info);
649
b24e03db 650 btrfs_trans_release_metadata(trans, root);
4c13d758 651 trans->block_rsv = NULL;
c5567237
AJ
652
653 if (trans->qgroup_reserved) {
7c2ec3f0
LB
654 /*
655 * the same root has to be passed here between start_transaction
656 * and end_transaction. Subvolume quota depends on this.
657 */
658 btrfs_qgroup_free(trans->root, trans->qgroup_reserved);
c5567237
AJ
659 trans->qgroup_reserved = 0;
660 }
661
ea658bad
JB
662 if (!list_empty(&trans->new_bgs))
663 btrfs_create_pending_block_groups(trans, root);
664
bb721703 665 while (count < 1) {
c3e69d58
CM
666 unsigned long cur = trans->delayed_ref_updates;
667 trans->delayed_ref_updates = 0;
668 if (cur &&
669 trans->transaction->delayed_refs.num_heads_ready > 64) {
670 trans->delayed_ref_updates = 0;
671 btrfs_run_delayed_refs(trans, root, cur);
672 } else {
673 break;
674 }
675 count++;
56bec294 676 }
bb721703 677
0e721106
JB
678 btrfs_trans_release_metadata(trans, root);
679 trans->block_rsv = NULL;
56bec294 680
ea658bad
JB
681 if (!list_empty(&trans->new_bgs))
682 btrfs_create_pending_block_groups(trans, root);
683
a4abeea4
JB
684 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
685 should_end_transaction(trans, root)) {
8929ecfa 686 trans->transaction->blocked = 1;
a4abeea4
JB
687 smp_wmb();
688 }
8929ecfa 689
0af3d00b 690 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
81317fde
JB
691 if (throttle) {
692 /*
693 * We may race with somebody else here so end up having
694 * to call end_transaction on ourselves again, so inc
695 * our use_count.
696 */
697 trans->use_count++;
8929ecfa 698 return btrfs_commit_transaction(trans, root);
81317fde 699 } else {
8929ecfa 700 wake_up_process(info->transaction_kthread);
81317fde 701 }
8929ecfa
YZ
702 }
703
0860adfd 704 if (trans->type & __TRANS_FREEZABLE)
98114659 705 sb_end_intwrite(root->fs_info->sb);
6df7881a 706
8929ecfa 707 WARN_ON(cur_trans != info->running_transaction);
13c5a93e
JB
708 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
709 atomic_dec(&cur_trans->num_writers);
0860adfd 710 extwriter_counter_dec(cur_trans, trans->type);
89ce8a63 711
99d16cbc 712 smp_mb();
79154b1b
CM
713 if (waitqueue_active(&cur_trans->writer_wait))
714 wake_up(&cur_trans->writer_wait);
79154b1b 715 put_transaction(cur_trans);
9ed74f2d
JB
716
717 if (current->journal_info == trans)
718 current->journal_info = NULL;
ab78c84d 719
24bbcf04
YZ
720 if (throttle)
721 btrfs_run_delayed_iputs(root);
722
49b25e05 723 if (trans->aborted ||
87533c47 724 test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
4edc2ca3 725 err = -EIO;
edf39272 726 assert_qgroups_uptodate(trans);
49b25e05 727
4edc2ca3
DJ
728 kmem_cache_free(btrfs_trans_handle_cachep, trans);
729 return err;
79154b1b
CM
730}
731
89ce8a63
CM
732int btrfs_end_transaction(struct btrfs_trans_handle *trans,
733 struct btrfs_root *root)
734{
98ad43be 735 return __btrfs_end_transaction(trans, root, 0);
89ce8a63
CM
736}
737
738int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root)
740{
98ad43be 741 return __btrfs_end_transaction(trans, root, 1);
16cdcec7
MX
742}
743
744int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root)
746{
a698d075 747 return __btrfs_end_transaction(trans, root, 1);
89ce8a63
CM
748}
749
d352ac68
CM
750/*
751 * when btree blocks are allocated, they have some corresponding bits set for
752 * them in one of two extent_io trees. This is used to make sure all of
690587d1 753 * those extents are sent to disk but does not wait on them
d352ac68 754 */
690587d1 755int btrfs_write_marked_extents(struct btrfs_root *root,
8cef4e16 756 struct extent_io_tree *dirty_pages, int mark)
79154b1b 757{
777e6bd7 758 int err = 0;
7c4452b9 759 int werr = 0;
1728366e 760 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
e6138876 761 struct extent_state *cached_state = NULL;
777e6bd7 762 u64 start = 0;
5f39d397 763 u64 end;
53b381b3 764 struct blk_plug plug;
7c4452b9 765
53b381b3 766 blk_start_plug(&plug);
1728366e 767 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
e6138876
JB
768 mark, &cached_state)) {
769 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
770 mark, &cached_state, GFP_NOFS);
771 cached_state = NULL;
1728366e
JB
772 err = filemap_fdatawrite_range(mapping, start, end);
773 if (err)
774 werr = err;
775 cond_resched();
776 start = end + 1;
7c4452b9 777 }
690587d1
CM
778 if (err)
779 werr = err;
53b381b3 780 blk_finish_plug(&plug);
690587d1
CM
781 return werr;
782}
783
784/*
785 * when btree blocks are allocated, they have some corresponding bits set for
786 * them in one of two extent_io trees. This is used to make sure all of
787 * those extents are on disk for transaction or log commit. We wait
788 * on all the pages and clear them from the dirty pages state tree
789 */
790int btrfs_wait_marked_extents(struct btrfs_root *root,
8cef4e16 791 struct extent_io_tree *dirty_pages, int mark)
690587d1 792{
690587d1
CM
793 int err = 0;
794 int werr = 0;
1728366e 795 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
e6138876 796 struct extent_state *cached_state = NULL;
690587d1
CM
797 u64 start = 0;
798 u64 end;
777e6bd7 799
1728366e 800 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
e6138876
JB
801 EXTENT_NEED_WAIT, &cached_state)) {
802 clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
803 0, 0, &cached_state, GFP_NOFS);
1728366e
JB
804 err = filemap_fdatawait_range(mapping, start, end);
805 if (err)
806 werr = err;
807 cond_resched();
808 start = end + 1;
777e6bd7 809 }
7c4452b9
CM
810 if (err)
811 werr = err;
812 return werr;
79154b1b
CM
813}
814
690587d1
CM
815/*
816 * when btree blocks are allocated, they have some corresponding bits set for
817 * them in one of two extent_io trees. This is used to make sure all of
818 * those extents are on disk for transaction or log commit
819 */
820int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8cef4e16 821 struct extent_io_tree *dirty_pages, int mark)
690587d1
CM
822{
823 int ret;
824 int ret2;
825
8cef4e16
YZ
826 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
827 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
bf0da8c1
CM
828
829 if (ret)
830 return ret;
831 if (ret2)
832 return ret2;
833 return 0;
690587d1
CM
834}
835
d0c803c4
CM
836int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
837 struct btrfs_root *root)
838{
839 if (!trans || !trans->transaction) {
840 struct inode *btree_inode;
841 btree_inode = root->fs_info->btree_inode;
842 return filemap_write_and_wait(btree_inode->i_mapping);
843 }
844 return btrfs_write_and_wait_marked_extents(root,
8cef4e16
YZ
845 &trans->transaction->dirty_pages,
846 EXTENT_DIRTY);
d0c803c4
CM
847}
848
d352ac68
CM
849/*
850 * this is used to update the root pointer in the tree of tree roots.
851 *
852 * But, in the case of the extent allocation tree, updating the root
853 * pointer may allocate blocks which may change the root of the extent
854 * allocation tree.
855 *
856 * So, this loops and repeats and makes sure the cowonly root didn't
857 * change while the root pointer was being updated in the metadata.
858 */
0b86a832
CM
859static int update_cowonly_root(struct btrfs_trans_handle *trans,
860 struct btrfs_root *root)
79154b1b
CM
861{
862 int ret;
0b86a832 863 u64 old_root_bytenr;
86b9f2ec 864 u64 old_root_used;
0b86a832 865 struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b 866
86b9f2ec 867 old_root_used = btrfs_root_used(&root->root_item);
0b86a832 868 btrfs_write_dirty_block_groups(trans, root);
56bec294 869
d397712b 870 while (1) {
0b86a832 871 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
86b9f2ec
YZ
872 if (old_root_bytenr == root->node->start &&
873 old_root_used == btrfs_root_used(&root->root_item))
79154b1b 874 break;
87ef2bb4 875
5d4f98a2 876 btrfs_set_root_node(&root->root_item, root->node);
79154b1b 877 ret = btrfs_update_root(trans, tree_root,
0b86a832
CM
878 &root->root_key,
879 &root->root_item);
49b25e05
JM
880 if (ret)
881 return ret;
56bec294 882
86b9f2ec 883 old_root_used = btrfs_root_used(&root->root_item);
4a8c9a62 884 ret = btrfs_write_dirty_block_groups(trans, root);
49b25e05
JM
885 if (ret)
886 return ret;
0b86a832 887 }
276e680d
YZ
888
889 if (root != root->fs_info->extent_root)
890 switch_commit_root(root);
891
0b86a832
CM
892 return 0;
893}
894
d352ac68
CM
895/*
896 * update all the cowonly tree roots on disk
49b25e05
JM
897 *
898 * The error handling in this function may not be obvious. Any of the
899 * failures will cause the file system to go offline. We still need
900 * to clean up the delayed refs.
d352ac68 901 */
5d4f98a2
YZ
902static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
903 struct btrfs_root *root)
0b86a832
CM
904{
905 struct btrfs_fs_info *fs_info = root->fs_info;
906 struct list_head *next;
84234f3a 907 struct extent_buffer *eb;
56bec294 908 int ret;
84234f3a 909
56bec294 910 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49b25e05
JM
911 if (ret)
912 return ret;
87ef2bb4 913
84234f3a 914 eb = btrfs_lock_root_node(fs_info->tree_root);
49b25e05
JM
915 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
916 0, &eb);
84234f3a
YZ
917 btrfs_tree_unlock(eb);
918 free_extent_buffer(eb);
0b86a832 919
49b25e05
JM
920 if (ret)
921 return ret;
922
56bec294 923 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49b25e05
JM
924 if (ret)
925 return ret;
87ef2bb4 926
733f4fbb 927 ret = btrfs_run_dev_stats(trans, root->fs_info);
8dabb742
SB
928 WARN_ON(ret);
929 ret = btrfs_run_dev_replace(trans, root->fs_info);
930 WARN_ON(ret);
733f4fbb 931
546adb0d
JS
932 ret = btrfs_run_qgroups(trans, root->fs_info);
933 BUG_ON(ret);
934
935 /* run_qgroups might have added some more refs */
936 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
937 BUG_ON(ret);
938
d397712b 939 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
0b86a832
CM
940 next = fs_info->dirty_cowonly_roots.next;
941 list_del_init(next);
942 root = list_entry(next, struct btrfs_root, dirty_list);
87ef2bb4 943
49b25e05
JM
944 ret = update_cowonly_root(trans, root);
945 if (ret)
946 return ret;
79154b1b 947 }
276e680d
YZ
948
949 down_write(&fs_info->extent_commit_sem);
950 switch_commit_root(fs_info->extent_root);
951 up_write(&fs_info->extent_commit_sem);
952
8dabb742
SB
953 btrfs_after_dev_replace_commit(fs_info);
954
79154b1b
CM
955 return 0;
956}
957
d352ac68
CM
958/*
959 * dead roots are old snapshots that need to be deleted. This allocates
960 * a dirty root struct and adds it into the list of dead roots that need to
961 * be deleted
962 */
5d4f98a2 963int btrfs_add_dead_root(struct btrfs_root *root)
5eda7b5e 964{
a4abeea4 965 spin_lock(&root->fs_info->trans_lock);
9d1a2a3a 966 list_add_tail(&root->root_list, &root->fs_info->dead_roots);
a4abeea4 967 spin_unlock(&root->fs_info->trans_lock);
5eda7b5e
CM
968 return 0;
969}
970
d352ac68 971/*
5d4f98a2 972 * update all the cowonly tree roots on disk
d352ac68 973 */
5d4f98a2
YZ
974static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
975 struct btrfs_root *root)
0f7d52f4 976{
0f7d52f4 977 struct btrfs_root *gang[8];
5d4f98a2 978 struct btrfs_fs_info *fs_info = root->fs_info;
0f7d52f4
CM
979 int i;
980 int ret;
54aa1f4d
CM
981 int err = 0;
982
a4abeea4 983 spin_lock(&fs_info->fs_roots_radix_lock);
d397712b 984 while (1) {
5d4f98a2
YZ
985 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
986 (void **)gang, 0,
0f7d52f4
CM
987 ARRAY_SIZE(gang),
988 BTRFS_ROOT_TRANS_TAG);
989 if (ret == 0)
990 break;
991 for (i = 0; i < ret; i++) {
992 root = gang[i];
5d4f98a2
YZ
993 radix_tree_tag_clear(&fs_info->fs_roots_radix,
994 (unsigned long)root->root_key.objectid,
995 BTRFS_ROOT_TRANS_TAG);
a4abeea4 996 spin_unlock(&fs_info->fs_roots_radix_lock);
31153d81 997
e02119d5 998 btrfs_free_log(trans, root);
5d4f98a2 999 btrfs_update_reloc_root(trans, root);
d68fc57b 1000 btrfs_orphan_commit_root(trans, root);
bcc63abb 1001
82d5902d
LZ
1002 btrfs_save_ino_cache(root, trans);
1003
f1ebcc74
LB
1004 /* see comments in should_cow_block() */
1005 root->force_cow = 0;
1006 smp_wmb();
1007
978d910d 1008 if (root->commit_root != root->node) {
581bb050 1009 mutex_lock(&root->fs_commit_mutex);
817d52f8 1010 switch_commit_root(root);
581bb050
LZ
1011 btrfs_unpin_free_ino(root);
1012 mutex_unlock(&root->fs_commit_mutex);
1013
978d910d
YZ
1014 btrfs_set_root_node(&root->root_item,
1015 root->node);
1016 }
5d4f98a2 1017
5d4f98a2 1018 err = btrfs_update_root(trans, fs_info->tree_root,
0f7d52f4
CM
1019 &root->root_key,
1020 &root->root_item);
a4abeea4 1021 spin_lock(&fs_info->fs_roots_radix_lock);
54aa1f4d
CM
1022 if (err)
1023 break;
0f7d52f4
CM
1024 }
1025 }
a4abeea4 1026 spin_unlock(&fs_info->fs_roots_radix_lock);
54aa1f4d 1027 return err;
0f7d52f4
CM
1028}
1029
d352ac68 1030/*
de78b51a
ES
1031 * defrag a given btree.
1032 * Every leaf in the btree is read and defragged.
d352ac68 1033 */
de78b51a 1034int btrfs_defrag_root(struct btrfs_root *root)
e9d0b13b
CM
1035{
1036 struct btrfs_fs_info *info = root->fs_info;
e9d0b13b 1037 struct btrfs_trans_handle *trans;
8929ecfa 1038 int ret;
e9d0b13b 1039
8929ecfa 1040 if (xchg(&root->defrag_running, 1))
e9d0b13b 1041 return 0;
8929ecfa 1042
6b80053d 1043 while (1) {
8929ecfa
YZ
1044 trans = btrfs_start_transaction(root, 0);
1045 if (IS_ERR(trans))
1046 return PTR_ERR(trans);
1047
de78b51a 1048 ret = btrfs_defrag_leaves(trans, root);
8929ecfa 1049
e9d0b13b 1050 btrfs_end_transaction(trans, root);
b53d3f5d 1051 btrfs_btree_balance_dirty(info->tree_root);
e9d0b13b
CM
1052 cond_resched();
1053
7841cb28 1054 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
e9d0b13b 1055 break;
210549eb
DS
1056
1057 if (btrfs_defrag_cancelled(root->fs_info)) {
1058 printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1059 ret = -EAGAIN;
1060 break;
1061 }
e9d0b13b
CM
1062 }
1063 root->defrag_running = 0;
8929ecfa 1064 return ret;
e9d0b13b
CM
1065}
1066
d352ac68
CM
1067/*
1068 * new snapshots need to be created at a very specific time in the
aec8030a
MX
1069 * transaction commit. This does the actual creation.
1070 *
1071 * Note:
1072 * If the error which may affect the commitment of the current transaction
1073 * happens, we should return the error number. If the error which just affect
1074 * the creation of the pending snapshots, just return 0.
d352ac68 1075 */
80b6794d 1076static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f
CM
1077 struct btrfs_fs_info *fs_info,
1078 struct btrfs_pending_snapshot *pending)
1079{
1080 struct btrfs_key key;
80b6794d 1081 struct btrfs_root_item *new_root_item;
3063d29f
CM
1082 struct btrfs_root *tree_root = fs_info->tree_root;
1083 struct btrfs_root *root = pending->root;
6bdb72de 1084 struct btrfs_root *parent_root;
98c9942a 1085 struct btrfs_block_rsv *rsv;
6bdb72de 1086 struct inode *parent_inode;
42874b3d
MX
1087 struct btrfs_path *path;
1088 struct btrfs_dir_item *dir_item;
a22285a6 1089 struct dentry *dentry;
3063d29f 1090 struct extent_buffer *tmp;
925baedd 1091 struct extent_buffer *old;
8ea05e3a 1092 struct timespec cur_time = CURRENT_TIME;
aec8030a 1093 int ret = 0;
d68fc57b 1094 u64 to_reserve = 0;
6bdb72de 1095 u64 index = 0;
a22285a6 1096 u64 objectid;
b83cc969 1097 u64 root_flags;
8ea05e3a 1098 uuid_le new_uuid;
3063d29f 1099
42874b3d
MX
1100 path = btrfs_alloc_path();
1101 if (!path) {
aec8030a
MX
1102 pending->error = -ENOMEM;
1103 return 0;
42874b3d
MX
1104 }
1105
80b6794d
CM
1106 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
1107 if (!new_root_item) {
aec8030a 1108 pending->error = -ENOMEM;
6fa9700e 1109 goto root_item_alloc_fail;
80b6794d 1110 }
a22285a6 1111
aec8030a
MX
1112 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1113 if (pending->error)
6fa9700e 1114 goto no_free_objectid;
3063d29f 1115
3fd0a558 1116 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
d68fc57b
YZ
1117
1118 if (to_reserve > 0) {
aec8030a
MX
1119 pending->error = btrfs_block_rsv_add(root,
1120 &pending->block_rsv,
1121 to_reserve,
1122 BTRFS_RESERVE_NO_FLUSH);
1123 if (pending->error)
6fa9700e 1124 goto no_free_objectid;
d68fc57b
YZ
1125 }
1126
aec8030a
MX
1127 pending->error = btrfs_qgroup_inherit(trans, fs_info,
1128 root->root_key.objectid,
1129 objectid, pending->inherit);
1130 if (pending->error)
6fa9700e 1131 goto no_free_objectid;
6f72c7e2 1132
3063d29f 1133 key.objectid = objectid;
a22285a6
YZ
1134 key.offset = (u64)-1;
1135 key.type = BTRFS_ROOT_ITEM_KEY;
3063d29f 1136
6fa9700e 1137 rsv = trans->block_rsv;
a22285a6 1138 trans->block_rsv = &pending->block_rsv;
2382c5cc 1139 trans->bytes_reserved = trans->block_rsv->reserved;
3de4586c 1140
a22285a6 1141 dentry = pending->dentry;
e9662f70 1142 parent_inode = pending->dir;
a22285a6 1143 parent_root = BTRFS_I(parent_inode)->root;
7585717f 1144 record_root_in_trans(trans, parent_root);
a22285a6 1145
3063d29f
CM
1146 /*
1147 * insert the directory item
1148 */
3de4586c 1149 ret = btrfs_set_inode_index(parent_inode, &index);
49b25e05 1150 BUG_ON(ret); /* -ENOMEM */
42874b3d
MX
1151
1152 /* check if there is a file/dir which has the same name. */
1153 dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1154 btrfs_ino(parent_inode),
1155 dentry->d_name.name,
1156 dentry->d_name.len, 0);
1157 if (dir_item != NULL && !IS_ERR(dir_item)) {
fe66a05a 1158 pending->error = -EEXIST;
aec8030a 1159 goto dir_item_existed;
42874b3d
MX
1160 } else if (IS_ERR(dir_item)) {
1161 ret = PTR_ERR(dir_item);
8732d44f
MX
1162 btrfs_abort_transaction(trans, root, ret);
1163 goto fail;
79787eaa 1164 }
42874b3d 1165 btrfs_release_path(path);
52c26179 1166
e999376f
CM
1167 /*
1168 * pull in the delayed directory update
1169 * and the delayed inode item
1170 * otherwise we corrupt the FS during
1171 * snapshot
1172 */
1173 ret = btrfs_run_delayed_items(trans, root);
8732d44f
MX
1174 if (ret) { /* Transaction aborted */
1175 btrfs_abort_transaction(trans, root, ret);
1176 goto fail;
1177 }
e999376f 1178
7585717f 1179 record_root_in_trans(trans, root);
6bdb72de
SW
1180 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1181 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
08fe4db1 1182 btrfs_check_and_init_root_item(new_root_item);
6bdb72de 1183
b83cc969
LZ
1184 root_flags = btrfs_root_flags(new_root_item);
1185 if (pending->readonly)
1186 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1187 else
1188 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1189 btrfs_set_root_flags(new_root_item, root_flags);
1190
8ea05e3a
AB
1191 btrfs_set_root_generation_v2(new_root_item,
1192 trans->transid);
1193 uuid_le_gen(&new_uuid);
1194 memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
1195 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1196 BTRFS_UUID_SIZE);
70023da2
SB
1197 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1198 memset(new_root_item->received_uuid, 0,
1199 sizeof(new_root_item->received_uuid));
1200 memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1201 memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1202 btrfs_set_root_stransid(new_root_item, 0);
1203 btrfs_set_root_rtransid(new_root_item, 0);
1204 }
8ea05e3a 1205 new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
dadd1105 1206 new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
8ea05e3a 1207 btrfs_set_root_otransid(new_root_item, trans->transid);
8ea05e3a 1208
6bdb72de 1209 old = btrfs_lock_root_node(root);
49b25e05 1210 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
79787eaa
JM
1211 if (ret) {
1212 btrfs_tree_unlock(old);
1213 free_extent_buffer(old);
8732d44f
MX
1214 btrfs_abort_transaction(trans, root, ret);
1215 goto fail;
79787eaa 1216 }
49b25e05 1217
6bdb72de
SW
1218 btrfs_set_lock_blocking(old);
1219
49b25e05 1220 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
79787eaa 1221 /* clean up in any case */
6bdb72de
SW
1222 btrfs_tree_unlock(old);
1223 free_extent_buffer(old);
8732d44f
MX
1224 if (ret) {
1225 btrfs_abort_transaction(trans, root, ret);
1226 goto fail;
1227 }
6bdb72de 1228
f1ebcc74
LB
1229 /* see comments in should_cow_block() */
1230 root->force_cow = 1;
1231 smp_wmb();
1232
6bdb72de 1233 btrfs_set_root_node(new_root_item, tmp);
a22285a6
YZ
1234 /* record when the snapshot was created in key.offset */
1235 key.offset = trans->transid;
1236 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
6bdb72de
SW
1237 btrfs_tree_unlock(tmp);
1238 free_extent_buffer(tmp);
8732d44f
MX
1239 if (ret) {
1240 btrfs_abort_transaction(trans, root, ret);
1241 goto fail;
1242 }
6bdb72de 1243
a22285a6
YZ
1244 /*
1245 * insert root back/forward references
1246 */
1247 ret = btrfs_add_root_ref(trans, tree_root, objectid,
0660b5af 1248 parent_root->root_key.objectid,
33345d01 1249 btrfs_ino(parent_inode), index,
a22285a6 1250 dentry->d_name.name, dentry->d_name.len);
8732d44f
MX
1251 if (ret) {
1252 btrfs_abort_transaction(trans, root, ret);
1253 goto fail;
1254 }
0660b5af 1255
a22285a6
YZ
1256 key.offset = (u64)-1;
1257 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
79787eaa
JM
1258 if (IS_ERR(pending->snap)) {
1259 ret = PTR_ERR(pending->snap);
8732d44f
MX
1260 btrfs_abort_transaction(trans, root, ret);
1261 goto fail;
79787eaa 1262 }
d68fc57b 1263
49b25e05 1264 ret = btrfs_reloc_post_snapshot(trans, pending);
8732d44f
MX
1265 if (ret) {
1266 btrfs_abort_transaction(trans, root, ret);
1267 goto fail;
1268 }
361048f5
MX
1269
1270 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
8732d44f
MX
1271 if (ret) {
1272 btrfs_abort_transaction(trans, root, ret);
1273 goto fail;
1274 }
42874b3d
MX
1275
1276 ret = btrfs_insert_dir_item(trans, parent_root,
1277 dentry->d_name.name, dentry->d_name.len,
1278 parent_inode, &key,
1279 BTRFS_FT_DIR, index);
1280 /* We have check then name at the beginning, so it is impossible. */
9c52057c 1281 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
8732d44f
MX
1282 if (ret) {
1283 btrfs_abort_transaction(trans, root, ret);
1284 goto fail;
1285 }
42874b3d
MX
1286
1287 btrfs_i_size_write(parent_inode, parent_inode->i_size +
1288 dentry->d_name.len * 2);
1289 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
be6aef60 1290 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
42874b3d 1291 if (ret)
8732d44f 1292 btrfs_abort_transaction(trans, root, ret);
3063d29f 1293fail:
aec8030a
MX
1294 pending->error = ret;
1295dir_item_existed:
98c9942a 1296 trans->block_rsv = rsv;
2382c5cc 1297 trans->bytes_reserved = 0;
6fa9700e
MX
1298no_free_objectid:
1299 kfree(new_root_item);
1300root_item_alloc_fail:
42874b3d 1301 btrfs_free_path(path);
49b25e05 1302 return ret;
3063d29f
CM
1303}
1304
d352ac68
CM
1305/*
1306 * create all the snapshots we've scheduled for creation
1307 */
80b6794d
CM
1308static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1309 struct btrfs_fs_info *fs_info)
3de4586c 1310{
aec8030a 1311 struct btrfs_pending_snapshot *pending, *next;
3de4586c 1312 struct list_head *head = &trans->transaction->pending_snapshots;
aec8030a 1313 int ret = 0;
3de4586c 1314
aec8030a
MX
1315 list_for_each_entry_safe(pending, next, head, list) {
1316 list_del(&pending->list);
1317 ret = create_pending_snapshot(trans, fs_info, pending);
1318 if (ret)
1319 break;
1320 }
1321 return ret;
3de4586c
CM
1322}
1323
5d4f98a2
YZ
1324static void update_super_roots(struct btrfs_root *root)
1325{
1326 struct btrfs_root_item *root_item;
1327 struct btrfs_super_block *super;
1328
6c41761f 1329 super = root->fs_info->super_copy;
5d4f98a2
YZ
1330
1331 root_item = &root->fs_info->chunk_root->root_item;
1332 super->chunk_root = root_item->bytenr;
1333 super->chunk_root_generation = root_item->generation;
1334 super->chunk_root_level = root_item->level;
1335
1336 root_item = &root->fs_info->tree_root->root_item;
1337 super->root = root_item->bytenr;
1338 super->generation = root_item->generation;
1339 super->root_level = root_item->level;
73bc1876 1340 if (btrfs_test_opt(root, SPACE_CACHE))
0af3d00b 1341 super->cache_generation = root_item->generation;
5d4f98a2
YZ
1342}
1343
f36f3042
CM
1344int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1345{
1346 int ret = 0;
a4abeea4 1347 spin_lock(&info->trans_lock);
f36f3042
CM
1348 if (info->running_transaction)
1349 ret = info->running_transaction->in_commit;
a4abeea4 1350 spin_unlock(&info->trans_lock);
f36f3042
CM
1351 return ret;
1352}
1353
8929ecfa
YZ
1354int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1355{
1356 int ret = 0;
a4abeea4 1357 spin_lock(&info->trans_lock);
8929ecfa
YZ
1358 if (info->running_transaction)
1359 ret = info->running_transaction->blocked;
a4abeea4 1360 spin_unlock(&info->trans_lock);
8929ecfa
YZ
1361 return ret;
1362}
1363
bb9c12c9
SW
1364/*
1365 * wait for the current transaction commit to start and block subsequent
1366 * transaction joins
1367 */
1368static void wait_current_trans_commit_start(struct btrfs_root *root,
1369 struct btrfs_transaction *trans)
1370{
72d63ed6 1371 wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
bb9c12c9
SW
1372}
1373
1374/*
1375 * wait for the current transaction to start and then become unblocked.
1376 * caller holds ref.
1377 */
1378static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1379 struct btrfs_transaction *trans)
1380{
72d63ed6
LZ
1381 wait_event(root->fs_info->transaction_wait,
1382 trans->commit_done || (trans->in_commit && !trans->blocked));
bb9c12c9
SW
1383}
1384
1385/*
1386 * commit transactions asynchronously. once btrfs_commit_transaction_async
1387 * returns, any subsequent transaction will not be allowed to join.
1388 */
1389struct btrfs_async_commit {
1390 struct btrfs_trans_handle *newtrans;
1391 struct btrfs_root *root;
7892b5af 1392 struct work_struct work;
bb9c12c9
SW
1393};
1394
1395static void do_async_commit(struct work_struct *work)
1396{
1397 struct btrfs_async_commit *ac =
7892b5af 1398 container_of(work, struct btrfs_async_commit, work);
bb9c12c9 1399
6fc4e354
SW
1400 /*
1401 * We've got freeze protection passed with the transaction.
1402 * Tell lockdep about it.
1403 */
ff7c1d33
MX
1404 if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
1405 rwsem_acquire_read(
1406 &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1407 0, 1, _THIS_IP_);
6fc4e354 1408
e209db7a
SW
1409 current->journal_info = ac->newtrans;
1410
bb9c12c9
SW
1411 btrfs_commit_transaction(ac->newtrans, ac->root);
1412 kfree(ac);
1413}
1414
1415int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1416 struct btrfs_root *root,
1417 int wait_for_unblock)
1418{
1419 struct btrfs_async_commit *ac;
1420 struct btrfs_transaction *cur_trans;
1421
1422 ac = kmalloc(sizeof(*ac), GFP_NOFS);
db5b493a
TI
1423 if (!ac)
1424 return -ENOMEM;
bb9c12c9 1425
7892b5af 1426 INIT_WORK(&ac->work, do_async_commit);
bb9c12c9 1427 ac->root = root;
7a7eaa40 1428 ac->newtrans = btrfs_join_transaction(root);
3612b495
TI
1429 if (IS_ERR(ac->newtrans)) {
1430 int err = PTR_ERR(ac->newtrans);
1431 kfree(ac);
1432 return err;
1433 }
bb9c12c9
SW
1434
1435 /* take transaction reference */
bb9c12c9 1436 cur_trans = trans->transaction;
13c5a93e 1437 atomic_inc(&cur_trans->use_count);
bb9c12c9
SW
1438
1439 btrfs_end_transaction(trans, root);
6fc4e354
SW
1440
1441 /*
1442 * Tell lockdep we've released the freeze rwsem, since the
1443 * async commit thread will be the one to unlock it.
1444 */
ff7c1d33
MX
1445 if (trans->type < TRANS_JOIN_NOLOCK)
1446 rwsem_release(
1447 &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1448 1, _THIS_IP_);
6fc4e354 1449
7892b5af 1450 schedule_work(&ac->work);
bb9c12c9
SW
1451
1452 /* wait for transaction to start and unblock */
bb9c12c9
SW
1453 if (wait_for_unblock)
1454 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1455 else
1456 wait_current_trans_commit_start(root, cur_trans);
bb9c12c9 1457
38e88054
SW
1458 if (current->journal_info == trans)
1459 current->journal_info = NULL;
1460
1461 put_transaction(cur_trans);
bb9c12c9
SW
1462 return 0;
1463}
1464
49b25e05
JM
1465
1466static void cleanup_transaction(struct btrfs_trans_handle *trans,
7b8b92af 1467 struct btrfs_root *root, int err)
49b25e05
JM
1468{
1469 struct btrfs_transaction *cur_trans = trans->transaction;
f094ac32 1470 DEFINE_WAIT(wait);
49b25e05
JM
1471
1472 WARN_ON(trans->use_count > 1);
1473
7b8b92af
JB
1474 btrfs_abort_transaction(trans, root, err);
1475
49b25e05 1476 spin_lock(&root->fs_info->trans_lock);
66b6135b 1477
25d8c284
MX
1478 /*
1479 * If the transaction is removed from the list, it means this
1480 * transaction has been committed successfully, so it is impossible
1481 * to call the cleanup function.
1482 */
1483 BUG_ON(list_empty(&cur_trans->list));
66b6135b 1484
49b25e05 1485 list_del_init(&cur_trans->list);
d7096fc3 1486 if (cur_trans == root->fs_info->running_transaction) {
f094ac32
LB
1487 root->fs_info->trans_no_join = 1;
1488 spin_unlock(&root->fs_info->trans_lock);
1489 wait_event(cur_trans->writer_wait,
1490 atomic_read(&cur_trans->num_writers) == 1);
1491
1492 spin_lock(&root->fs_info->trans_lock);
d7096fc3 1493 root->fs_info->running_transaction = NULL;
d7096fc3 1494 }
49b25e05
JM
1495 spin_unlock(&root->fs_info->trans_lock);
1496
1497 btrfs_cleanup_one_transaction(trans->transaction, root);
1498
1499 put_transaction(cur_trans);
1500 put_transaction(cur_trans);
1501
1502 trace_btrfs_transaction_commit(root);
1503
1504 btrfs_scrub_continue(root);
1505
1506 if (current->journal_info == trans)
1507 current->journal_info = NULL;
1508
1509 kmem_cache_free(btrfs_trans_handle_cachep, trans);
cf79ffb5
JB
1510
1511 spin_lock(&root->fs_info->trans_lock);
1512 root->fs_info->trans_no_join = 0;
1513 spin_unlock(&root->fs_info->trans_lock);
49b25e05
JM
1514}
1515
ca469637
MX
1516static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
1517 struct btrfs_root *root)
1518{
ca469637
MX
1519 int ret;
1520
ca469637
MX
1521 ret = btrfs_run_delayed_items(trans, root);
1522 if (ret)
1523 return ret;
1524
1525 /*
1526 * running the delayed items may have added new refs. account
1527 * them now so that they hinder processing of more delayed refs
1528 * as little as possible.
1529 */
1530 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
1531
1532 /*
1533 * rename don't use btrfs_join_transaction, so, once we
1534 * set the transaction to blocked above, we aren't going
1535 * to get any new ordered operations. We can safely run
1536 * it here and no for sure that nothing new will be added
1537 * to the list
1538 */
569e0f35 1539 ret = btrfs_run_ordered_operations(trans, root, 1);
ca469637 1540
eebc6084 1541 return ret;
ca469637
MX
1542}
1543
82436617
MX
1544static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
1545{
1546 if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1547 return btrfs_start_all_delalloc_inodes(fs_info, 1);
1548 return 0;
1549}
1550
1551static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
1552{
1553 if (btrfs_test_opt(fs_info->tree_root, FLUSHONCOMMIT))
1554 btrfs_wait_all_ordered_extents(fs_info, 1);
1555}
1556
bb9c12c9
SW
1557/*
1558 * btrfs_transaction state sequence:
1559 * in_commit = 0, blocked = 0 (initial)
1560 * in_commit = 1, blocked = 1
1561 * blocked = 0
1562 * commit_done = 1
1563 */
79154b1b
CM
1564int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1565 struct btrfs_root *root)
1566{
49b25e05 1567 struct btrfs_transaction *cur_trans = trans->transaction;
8fd17795 1568 struct btrfs_transaction *prev_trans = NULL;
79154b1b 1569 DEFINE_WAIT(wait);
25287e0a 1570 int ret;
89573b9c
CM
1571 int should_grow = 0;
1572 unsigned long now = get_seconds();
79154b1b 1573
569e0f35 1574 ret = btrfs_run_ordered_operations(trans, root, 0);
25287e0a
MX
1575 if (ret) {
1576 btrfs_abort_transaction(trans, root, ret);
e4a2bcac
JB
1577 btrfs_end_transaction(trans, root);
1578 return ret;
25287e0a 1579 }
5a3f23d5 1580
8d25a086
MX
1581 /* Stop the commit early if ->aborted is set */
1582 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
25287e0a 1583 ret = cur_trans->aborted;
e4a2bcac
JB
1584 btrfs_end_transaction(trans, root);
1585 return ret;
25287e0a 1586 }
49b25e05 1587
56bec294
CM
1588 /* make a pass through all the delayed refs we have so far
1589 * any runnings procs may add more while we are here
1590 */
1591 ret = btrfs_run_delayed_refs(trans, root, 0);
e4a2bcac
JB
1592 if (ret) {
1593 btrfs_end_transaction(trans, root);
1594 return ret;
1595 }
56bec294 1596
0e721106
JB
1597 btrfs_trans_release_metadata(trans, root);
1598 trans->block_rsv = NULL;
272d26d0
MX
1599 if (trans->qgroup_reserved) {
1600 btrfs_qgroup_free(root, trans->qgroup_reserved);
1601 trans->qgroup_reserved = 0;
1602 }
0e721106 1603
b7ec40d7 1604 cur_trans = trans->transaction;
49b25e05 1605
56bec294
CM
1606 /*
1607 * set the flushing flag so procs in this transaction have to
1608 * start sending their work down.
1609 */
b7ec40d7 1610 cur_trans->delayed_refs.flushing = 1;
56bec294 1611
ea658bad
JB
1612 if (!list_empty(&trans->new_bgs))
1613 btrfs_create_pending_block_groups(trans, root);
1614
c3e69d58 1615 ret = btrfs_run_delayed_refs(trans, root, 0);
e4a2bcac
JB
1616 if (ret) {
1617 btrfs_end_transaction(trans, root);
1618 return ret;
1619 }
56bec294 1620
a4abeea4 1621 spin_lock(&cur_trans->commit_lock);
b7ec40d7 1622 if (cur_trans->in_commit) {
a4abeea4 1623 spin_unlock(&cur_trans->commit_lock);
13c5a93e 1624 atomic_inc(&cur_trans->use_count);
49b25e05 1625 ret = btrfs_end_transaction(trans, root);
ccd467d6 1626
b9c8300c 1627 wait_for_commit(root, cur_trans);
15ee9bc7 1628
79154b1b 1629 put_transaction(cur_trans);
15ee9bc7 1630
49b25e05 1631 return ret;
79154b1b 1632 }
4313b399 1633
2c90e5d6 1634 trans->transaction->in_commit = 1;
f9295749 1635 trans->transaction->blocked = 1;
a4abeea4 1636 spin_unlock(&cur_trans->commit_lock);
bb9c12c9
SW
1637 wake_up(&root->fs_info->transaction_blocked_wait);
1638
a4abeea4 1639 spin_lock(&root->fs_info->trans_lock);
ccd467d6
CM
1640 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1641 prev_trans = list_entry(cur_trans->list.prev,
1642 struct btrfs_transaction, list);
1643 if (!prev_trans->commit_done) {
13c5a93e 1644 atomic_inc(&prev_trans->use_count);
a4abeea4 1645 spin_unlock(&root->fs_info->trans_lock);
ccd467d6
CM
1646
1647 wait_for_commit(root, prev_trans);
ccd467d6 1648
15ee9bc7 1649 put_transaction(prev_trans);
a4abeea4
JB
1650 } else {
1651 spin_unlock(&root->fs_info->trans_lock);
ccd467d6 1652 }
a4abeea4
JB
1653 } else {
1654 spin_unlock(&root->fs_info->trans_lock);
ccd467d6 1655 }
15ee9bc7 1656
0860adfd
MX
1657 extwriter_counter_dec(cur_trans, trans->type);
1658
82436617
MX
1659 ret = btrfs_start_delalloc_flush(root->fs_info);
1660 if (ret)
1661 goto cleanup_transaction;
1662
e39e64ac
CM
1663 if (!btrfs_test_opt(root, SSD) &&
1664 (now < cur_trans->start_time || now - cur_trans->start_time < 1))
89573b9c
CM
1665 should_grow = 1;
1666
15ee9bc7 1667 do {
2c90e5d6 1668 WARN_ON(cur_trans != trans->transaction);
15ee9bc7 1669
ca469637 1670 ret = btrfs_flush_all_pending_stuffs(trans, root);
49b25e05
JM
1671 if (ret)
1672 goto cleanup_transaction;
16cdcec7 1673
ed3b3d31
CM
1674 prepare_to_wait(&cur_trans->writer_wait, &wait,
1675 TASK_UNINTERRUPTIBLE);
1676
0860adfd
MX
1677 if (extwriter_counter_read(cur_trans) > 0)
1678 schedule();
99d16cbc
SW
1679 else if (should_grow)
1680 schedule_timeout(1);
15ee9bc7 1681
15ee9bc7 1682 finish_wait(&cur_trans->writer_wait, &wait);
3f1e3fa6 1683 } while (extwriter_counter_read(cur_trans) > 0);
15ee9bc7 1684
ca469637
MX
1685 ret = btrfs_flush_all_pending_stuffs(trans, root);
1686 if (ret)
1687 goto cleanup_transaction;
1688
82436617 1689 btrfs_wait_delalloc_flush(root->fs_info);
ed0ca140
JB
1690 /*
1691 * Ok now we need to make sure to block out any other joins while we
1692 * commit the transaction. We could have started a join before setting
1693 * no_join so make sure to wait for num_writers to == 1 again.
1694 */
1695 spin_lock(&root->fs_info->trans_lock);
1696 root->fs_info->trans_no_join = 1;
1697 spin_unlock(&root->fs_info->trans_lock);
1698 wait_event(cur_trans->writer_wait,
1699 atomic_read(&cur_trans->num_writers) == 1);
1700
2cba30f1
MX
1701 /* ->aborted might be set after the previous check, so check it */
1702 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1703 ret = cur_trans->aborted;
1704 goto cleanup_transaction;
1705 }
7585717f
CM
1706 /*
1707 * the reloc mutex makes sure that we stop
1708 * the balancing code from coming in and moving
1709 * extents around in the middle of the commit
1710 */
1711 mutex_lock(&root->fs_info->reloc_mutex);
1712
42874b3d
MX
1713 /*
1714 * We needn't worry about the delayed items because we will
1715 * deal with them in create_pending_snapshot(), which is the
1716 * core function of the snapshot creation.
1717 */
1718 ret = create_pending_snapshots(trans, root->fs_info);
49b25e05
JM
1719 if (ret) {
1720 mutex_unlock(&root->fs_info->reloc_mutex);
1721 goto cleanup_transaction;
1722 }
3063d29f 1723
42874b3d
MX
1724 /*
1725 * We insert the dir indexes of the snapshots and update the inode
1726 * of the snapshots' parents after the snapshot creation, so there
1727 * are some delayed items which are not dealt with. Now deal with
1728 * them.
1729 *
1730 * We needn't worry that this operation will corrupt the snapshots,
1731 * because all the tree which are snapshoted will be forced to COW
1732 * the nodes and leaves.
1733 */
1734 ret = btrfs_run_delayed_items(trans, root);
49b25e05
JM
1735 if (ret) {
1736 mutex_unlock(&root->fs_info->reloc_mutex);
1737 goto cleanup_transaction;
1738 }
16cdcec7 1739
56bec294 1740 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
49b25e05
JM
1741 if (ret) {
1742 mutex_unlock(&root->fs_info->reloc_mutex);
1743 goto cleanup_transaction;
1744 }
56bec294 1745
e999376f
CM
1746 /*
1747 * make sure none of the code above managed to slip in a
1748 * delayed item
1749 */
1750 btrfs_assert_delayed_root_empty(root);
1751
2c90e5d6 1752 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 1753
a2de733c 1754 btrfs_scrub_pause(root);
e02119d5
CM
1755 /* btrfs_commit_tree_roots is responsible for getting the
1756 * various roots consistent with each other. Every pointer
1757 * in the tree of tree roots has to point to the most up to date
1758 * root for every subvolume and other tree. So, we have to keep
1759 * the tree logging code from jumping in and changing any
1760 * of the trees.
1761 *
1762 * At this point in the commit, there can't be any tree-log
1763 * writers, but a little lower down we drop the trans mutex
1764 * and let new people in. By holding the tree_log_mutex
1765 * from now until after the super is written, we avoid races
1766 * with the tree-log code.
1767 */
1768 mutex_lock(&root->fs_info->tree_log_mutex);
1769
5d4f98a2 1770 ret = commit_fs_roots(trans, root);
49b25e05
JM
1771 if (ret) {
1772 mutex_unlock(&root->fs_info->tree_log_mutex);
871383be 1773 mutex_unlock(&root->fs_info->reloc_mutex);
49b25e05
JM
1774 goto cleanup_transaction;
1775 }
54aa1f4d 1776
5d4f98a2 1777 /* commit_fs_roots gets rid of all the tree log roots, it is now
e02119d5
CM
1778 * safe to free the root of tree log roots
1779 */
1780 btrfs_free_log_root_tree(trans, root->fs_info);
1781
5d4f98a2 1782 ret = commit_cowonly_roots(trans, root);
49b25e05
JM
1783 if (ret) {
1784 mutex_unlock(&root->fs_info->tree_log_mutex);
871383be 1785 mutex_unlock(&root->fs_info->reloc_mutex);
49b25e05
JM
1786 goto cleanup_transaction;
1787 }
54aa1f4d 1788
2cba30f1
MX
1789 /*
1790 * The tasks which save the space cache and inode cache may also
1791 * update ->aborted, check it.
1792 */
1793 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1794 ret = cur_trans->aborted;
1795 mutex_unlock(&root->fs_info->tree_log_mutex);
1796 mutex_unlock(&root->fs_info->reloc_mutex);
1797 goto cleanup_transaction;
1798 }
1799
11833d66
YZ
1800 btrfs_prepare_extent_commit(trans, root);
1801
78fae27e 1802 cur_trans = root->fs_info->running_transaction;
5d4f98a2
YZ
1803
1804 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1805 root->fs_info->tree_root->node);
817d52f8 1806 switch_commit_root(root->fs_info->tree_root);
5d4f98a2
YZ
1807
1808 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1809 root->fs_info->chunk_root->node);
817d52f8 1810 switch_commit_root(root->fs_info->chunk_root);
5d4f98a2 1811
edf39272 1812 assert_qgroups_uptodate(trans);
5d4f98a2 1813 update_super_roots(root);
e02119d5
CM
1814
1815 if (!root->fs_info->log_root_recovering) {
6c41761f
DS
1816 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
1817 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
e02119d5
CM
1818 }
1819
6c41761f
DS
1820 memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
1821 sizeof(*root->fs_info->super_copy));
ccd467d6 1822
f9295749 1823 trans->transaction->blocked = 0;
a4abeea4
JB
1824 spin_lock(&root->fs_info->trans_lock);
1825 root->fs_info->running_transaction = NULL;
1826 root->fs_info->trans_no_join = 0;
1827 spin_unlock(&root->fs_info->trans_lock);
7585717f 1828 mutex_unlock(&root->fs_info->reloc_mutex);
b7ec40d7 1829
f9295749 1830 wake_up(&root->fs_info->transaction_wait);
e6dcd2dc 1831
79154b1b 1832 ret = btrfs_write_and_wait_transaction(trans, root);
49b25e05
JM
1833 if (ret) {
1834 btrfs_error(root->fs_info, ret,
08748810 1835 "Error while writing out transaction");
49b25e05
JM
1836 mutex_unlock(&root->fs_info->tree_log_mutex);
1837 goto cleanup_transaction;
1838 }
1839
1840 ret = write_ctree_super(trans, root, 0);
1841 if (ret) {
1842 mutex_unlock(&root->fs_info->tree_log_mutex);
1843 goto cleanup_transaction;
1844 }
4313b399 1845
e02119d5
CM
1846 /*
1847 * the super is written, we can safely allow the tree-loggers
1848 * to go about their business
1849 */
1850 mutex_unlock(&root->fs_info->tree_log_mutex);
1851
11833d66 1852 btrfs_finish_extent_commit(trans, root);
4313b399 1853
2c90e5d6 1854 cur_trans->commit_done = 1;
b7ec40d7 1855
15ee9bc7 1856 root->fs_info->last_trans_committed = cur_trans->transid;
817d52f8 1857
2c90e5d6 1858 wake_up(&cur_trans->commit_wait);
3de4586c 1859
a4abeea4 1860 spin_lock(&root->fs_info->trans_lock);
13c5a93e 1861 list_del_init(&cur_trans->list);
a4abeea4
JB
1862 spin_unlock(&root->fs_info->trans_lock);
1863
78fae27e 1864 put_transaction(cur_trans);
79154b1b 1865 put_transaction(cur_trans);
58176a96 1866
0860adfd 1867 if (trans->type & __TRANS_FREEZABLE)
354aa0fb 1868 sb_end_intwrite(root->fs_info->sb);
b2b5ef5c 1869
1abe9b8a 1870 trace_btrfs_transaction_commit(root);
1871
a2de733c
AJ
1872 btrfs_scrub_continue(root);
1873
9ed74f2d
JB
1874 if (current->journal_info == trans)
1875 current->journal_info = NULL;
1876
2c90e5d6 1877 kmem_cache_free(btrfs_trans_handle_cachep, trans);
24bbcf04
YZ
1878
1879 if (current != root->fs_info->transaction_kthread)
1880 btrfs_run_delayed_iputs(root);
1881
79154b1b 1882 return ret;
49b25e05
JM
1883
1884cleanup_transaction:
0e721106
JB
1885 btrfs_trans_release_metadata(trans, root);
1886 trans->block_rsv = NULL;
272d26d0
MX
1887 if (trans->qgroup_reserved) {
1888 btrfs_qgroup_free(root, trans->qgroup_reserved);
1889 trans->qgroup_reserved = 0;
1890 }
c2cf52eb 1891 btrfs_warn(root->fs_info, "Skipping commit of aborted transaction.");
49b25e05
JM
1892 if (current->journal_info == trans)
1893 current->journal_info = NULL;
7b8b92af 1894 cleanup_transaction(trans, root, ret);
49b25e05
JM
1895
1896 return ret;
79154b1b
CM
1897}
1898
d352ac68 1899/*
9d1a2a3a
DS
1900 * return < 0 if error
1901 * 0 if there are no more dead_roots at the time of call
1902 * 1 there are more to be processed, call me again
1903 *
1904 * The return value indicates there are certainly more snapshots to delete, but
1905 * if there comes a new one during processing, it may return 0. We don't mind,
1906 * because btrfs_commit_super will poke cleaner thread and it will process it a
1907 * few seconds later.
d352ac68 1908 */
9d1a2a3a 1909int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
e9d0b13b 1910{
9d1a2a3a 1911 int ret;
5d4f98a2
YZ
1912 struct btrfs_fs_info *fs_info = root->fs_info;
1913
a4abeea4 1914 spin_lock(&fs_info->trans_lock);
9d1a2a3a
DS
1915 if (list_empty(&fs_info->dead_roots)) {
1916 spin_unlock(&fs_info->trans_lock);
1917 return 0;
1918 }
1919 root = list_first_entry(&fs_info->dead_roots,
1920 struct btrfs_root, root_list);
1921 list_del(&root->root_list);
a4abeea4 1922 spin_unlock(&fs_info->trans_lock);
e9d0b13b 1923
9d1a2a3a
DS
1924 pr_debug("btrfs: cleaner removing %llu\n",
1925 (unsigned long long)root->objectid);
76dda93c 1926
9d1a2a3a 1927 btrfs_kill_all_delayed_nodes(root);
16cdcec7 1928
9d1a2a3a
DS
1929 if (btrfs_header_backref_rev(root->node) <
1930 BTRFS_MIXED_BACKREF_REV)
1931 ret = btrfs_drop_snapshot(root, NULL, 0, 0);
1932 else
1933 ret = btrfs_drop_snapshot(root, NULL, 1, 0);
1934 /*
1935 * If we encounter a transaction abort during snapshot cleaning, we
1936 * don't want to crash here
1937 */
1938 BUG_ON(ret < 0 && ret != -EAGAIN && ret != -EROFS);
1939 return 1;
e9d0b13b 1940}