]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/transaction.c
Btrfs: if we've already started a trans handle, use that one
[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>
79154b1b
CM
25#include "ctree.h"
26#include "disk-io.h"
27#include "transaction.h"
925baedd 28#include "locking.h"
e02119d5 29#include "tree-log.h"
79154b1b 30
0f7d52f4
CM
31#define BTRFS_ROOT_TRANS_TAG 0
32
80b6794d 33static noinline void put_transaction(struct btrfs_transaction *transaction)
79154b1b 34{
13c5a93e
JB
35 WARN_ON(atomic_read(&transaction->use_count) == 0);
36 if (atomic_dec_and_test(&transaction->use_count)) {
2c90e5d6
CM
37 memset(transaction, 0, sizeof(*transaction));
38 kmem_cache_free(btrfs_transaction_cachep, transaction);
78fae27e 39 }
79154b1b
CM
40}
41
817d52f8
JB
42static noinline void switch_commit_root(struct btrfs_root *root)
43{
817d52f8
JB
44 free_extent_buffer(root->commit_root);
45 root->commit_root = btrfs_root_node(root);
817d52f8
JB
46}
47
d352ac68
CM
48/*
49 * either allocate a new transaction or hop into the existing one
50 */
80b6794d 51static noinline int join_transaction(struct btrfs_root *root)
79154b1b
CM
52{
53 struct btrfs_transaction *cur_trans;
54 cur_trans = root->fs_info->running_transaction;
55 if (!cur_trans) {
2c90e5d6
CM
56 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
57 GFP_NOFS);
db5b493a
TI
58 if (!cur_trans)
59 return -ENOMEM;
0f7d52f4 60 root->fs_info->generation++;
13c5a93e 61 atomic_set(&cur_trans->num_writers, 1);
15ee9bc7 62 cur_trans->num_joined = 0;
0f7d52f4 63 cur_trans->transid = root->fs_info->generation;
79154b1b
CM
64 init_waitqueue_head(&cur_trans->writer_wait);
65 init_waitqueue_head(&cur_trans->commit_wait);
66 cur_trans->in_commit = 0;
f9295749 67 cur_trans->blocked = 0;
13c5a93e 68 atomic_set(&cur_trans->use_count, 1);
79154b1b 69 cur_trans->commit_done = 0;
08607c1b 70 cur_trans->start_time = get_seconds();
56bec294 71
6bef4d31 72 cur_trans->delayed_refs.root = RB_ROOT;
56bec294 73 cur_trans->delayed_refs.num_entries = 0;
c3e69d58
CM
74 cur_trans->delayed_refs.num_heads_ready = 0;
75 cur_trans->delayed_refs.num_heads = 0;
56bec294 76 cur_trans->delayed_refs.flushing = 0;
c3e69d58 77 cur_trans->delayed_refs.run_delayed_start = 0;
56bec294
CM
78 spin_lock_init(&cur_trans->delayed_refs.lock);
79
3063d29f 80 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
8fd17795 81 list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
d1310b2e 82 extent_io_tree_init(&cur_trans->dirty_pages,
5f39d397
CM
83 root->fs_info->btree_inode->i_mapping,
84 GFP_NOFS);
48ec2cf8
CM
85 spin_lock(&root->fs_info->new_trans_lock);
86 root->fs_info->running_transaction = cur_trans;
87 spin_unlock(&root->fs_info->new_trans_lock);
15ee9bc7 88 } else {
13c5a93e 89 atomic_inc(&cur_trans->num_writers);
15ee9bc7 90 cur_trans->num_joined++;
79154b1b 91 }
15ee9bc7 92
79154b1b
CM
93 return 0;
94}
95
d352ac68 96/*
d397712b
CM
97 * this does all the record keeping required to make sure that a reference
98 * counted root is properly recorded in a given transaction. This is required
99 * to make sure the old root from before we joined the transaction is deleted
100 * when the transaction commits
d352ac68 101 */
5d4f98a2
YZ
102static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root)
6702ed49 104{
5d4f98a2 105 if (root->ref_cows && root->last_trans < trans->transid) {
6702ed49 106 WARN_ON(root == root->fs_info->extent_root);
5d4f98a2
YZ
107 WARN_ON(root->commit_root != root->node);
108
109 radix_tree_tag_set(&root->fs_info->fs_roots_radix,
110 (unsigned long)root->root_key.objectid,
111 BTRFS_ROOT_TRANS_TAG);
112 root->last_trans = trans->transid;
113 btrfs_init_reloc_root(trans, root);
114 }
115 return 0;
116}
bcc63abb 117
5d4f98a2
YZ
118int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
119 struct btrfs_root *root)
120{
121 if (!root->ref_cows)
122 return 0;
bcc63abb 123
5d4f98a2
YZ
124 mutex_lock(&root->fs_info->trans_mutex);
125 if (root->last_trans == trans->transid) {
126 mutex_unlock(&root->fs_info->trans_mutex);
127 return 0;
6702ed49 128 }
5d4f98a2
YZ
129
130 record_root_in_trans(trans, root);
131 mutex_unlock(&root->fs_info->trans_mutex);
6702ed49
CM
132 return 0;
133}
134
d352ac68
CM
135/* wait for commit against the current transaction to become unblocked
136 * when this is done, it is safe to start a new transaction, but the current
137 * transaction might not be fully on disk.
138 */
37d1aeee 139static void wait_current_trans(struct btrfs_root *root)
79154b1b 140{
f9295749 141 struct btrfs_transaction *cur_trans;
79154b1b 142
f9295749 143 cur_trans = root->fs_info->running_transaction;
37d1aeee 144 if (cur_trans && cur_trans->blocked) {
f9295749 145 DEFINE_WAIT(wait);
13c5a93e 146 atomic_inc(&cur_trans->use_count);
d397712b 147 while (1) {
f9295749
CM
148 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149 TASK_UNINTERRUPTIBLE);
471fa17d 150 if (!cur_trans->blocked)
f9295749 151 break;
471fa17d
ZL
152 mutex_unlock(&root->fs_info->trans_mutex);
153 schedule();
154 mutex_lock(&root->fs_info->trans_mutex);
f9295749 155 }
471fa17d 156 finish_wait(&root->fs_info->transaction_wait, &wait);
f9295749
CM
157 put_transaction(cur_trans);
158 }
37d1aeee
CM
159}
160
249ac1e5
JB
161enum btrfs_trans_type {
162 TRANS_START,
163 TRANS_JOIN,
164 TRANS_USERSPACE,
0af3d00b 165 TRANS_JOIN_NOLOCK,
249ac1e5
JB
166};
167
a22285a6
YZ
168static int may_wait_transaction(struct btrfs_root *root, int type)
169{
170 if (!root->fs_info->log_root_recovering &&
171 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
172 type == TRANS_USERSPACE))
173 return 1;
174 return 0;
175}
176
e02119d5 177static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
a22285a6 178 u64 num_items, int type)
37d1aeee 179{
a22285a6
YZ
180 struct btrfs_trans_handle *h;
181 struct btrfs_transaction *cur_trans;
06d5a589 182 int retries = 0;
37d1aeee 183 int ret;
acce952b 184
185 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
186 return ERR_PTR(-EROFS);
2a1eb461
JB
187
188 if (current->journal_info) {
189 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
190 h = current->journal_info;
191 h->use_count++;
192 h->orig_rsv = h->block_rsv;
193 h->block_rsv = NULL;
194 goto got_it;
195 }
a22285a6
YZ
196again:
197 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
198 if (!h)
199 return ERR_PTR(-ENOMEM);
37d1aeee 200
0af3d00b
JB
201 if (type != TRANS_JOIN_NOLOCK)
202 mutex_lock(&root->fs_info->trans_mutex);
a22285a6 203 if (may_wait_transaction(root, type))
37d1aeee 204 wait_current_trans(root);
a22285a6 205
79154b1b 206 ret = join_transaction(root);
db5b493a 207 if (ret < 0) {
6e8df2ae 208 kmem_cache_free(btrfs_trans_handle_cachep, h);
db5b493a
TI
209 if (type != TRANS_JOIN_NOLOCK)
210 mutex_unlock(&root->fs_info->trans_mutex);
211 return ERR_PTR(ret);
212 }
0f7d52f4 213
a22285a6 214 cur_trans = root->fs_info->running_transaction;
13c5a93e 215 atomic_inc(&cur_trans->use_count);
0af3d00b
JB
216 if (type != TRANS_JOIN_NOLOCK)
217 mutex_unlock(&root->fs_info->trans_mutex);
a22285a6
YZ
218
219 h->transid = cur_trans->transid;
220 h->transaction = cur_trans;
79154b1b 221 h->blocks_used = 0;
d2fb3437 222 h->block_group = 0;
a22285a6 223 h->bytes_reserved = 0;
56bec294 224 h->delayed_ref_updates = 0;
2a1eb461 225 h->use_count = 1;
f0486c68 226 h->block_rsv = NULL;
2a1eb461 227 h->orig_rsv = NULL;
b7ec40d7 228
a22285a6
YZ
229 smp_mb();
230 if (cur_trans->blocked && may_wait_transaction(root, type)) {
231 btrfs_commit_transaction(h, root);
232 goto again;
233 }
234
235 if (num_items > 0) {
8bb8ab2e 236 ret = btrfs_trans_reserve_metadata(h, root, num_items);
06d5a589
JB
237 if (ret == -EAGAIN && !retries) {
238 retries++;
a22285a6
YZ
239 btrfs_commit_transaction(h, root);
240 goto again;
06d5a589
JB
241 } else if (ret == -EAGAIN) {
242 /*
243 * We have already retried and got EAGAIN, so really we
244 * don't have space, so set ret to -ENOSPC.
245 */
246 ret = -ENOSPC;
a22285a6 247 }
06d5a589 248
a22285a6
YZ
249 if (ret < 0) {
250 btrfs_end_transaction(h, root);
251 return ERR_PTR(ret);
252 }
253 }
9ed74f2d 254
2a1eb461 255got_it:
0af3d00b
JB
256 if (type != TRANS_JOIN_NOLOCK)
257 mutex_lock(&root->fs_info->trans_mutex);
5d4f98a2 258 record_root_in_trans(h, root);
0af3d00b
JB
259 if (type != TRANS_JOIN_NOLOCK)
260 mutex_unlock(&root->fs_info->trans_mutex);
a22285a6
YZ
261
262 if (!current->journal_info && type != TRANS_USERSPACE)
263 current->journal_info = h;
79154b1b
CM
264 return h;
265}
266
f9295749 267struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
a22285a6 268 int num_items)
f9295749 269{
a22285a6 270 return start_transaction(root, num_items, TRANS_START);
f9295749 271}
7a7eaa40 272struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
f9295749 273{
a22285a6 274 return start_transaction(root, 0, TRANS_JOIN);
f9295749
CM
275}
276
7a7eaa40 277struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
0af3d00b
JB
278{
279 return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
280}
281
7a7eaa40 282struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
9ca9ee09 283{
7a7eaa40 284 return start_transaction(root, 0, TRANS_USERSPACE);
9ca9ee09
SW
285}
286
d352ac68 287/* wait for a transaction commit to be fully complete */
89ce8a63
CM
288static noinline int wait_for_commit(struct btrfs_root *root,
289 struct btrfs_transaction *commit)
290{
291 DEFINE_WAIT(wait);
292 mutex_lock(&root->fs_info->trans_mutex);
d397712b 293 while (!commit->commit_done) {
89ce8a63
CM
294 prepare_to_wait(&commit->commit_wait, &wait,
295 TASK_UNINTERRUPTIBLE);
296 if (commit->commit_done)
297 break;
298 mutex_unlock(&root->fs_info->trans_mutex);
299 schedule();
300 mutex_lock(&root->fs_info->trans_mutex);
301 }
302 mutex_unlock(&root->fs_info->trans_mutex);
303 finish_wait(&commit->commit_wait, &wait);
304 return 0;
305}
306
46204592
SW
307int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
308{
309 struct btrfs_transaction *cur_trans = NULL, *t;
310 int ret;
311
312 mutex_lock(&root->fs_info->trans_mutex);
313
314 ret = 0;
315 if (transid) {
316 if (transid <= root->fs_info->last_trans_committed)
317 goto out_unlock;
318
319 /* find specified transaction */
320 list_for_each_entry(t, &root->fs_info->trans_list, list) {
321 if (t->transid == transid) {
322 cur_trans = t;
323 break;
324 }
325 if (t->transid > transid)
326 break;
327 }
328 ret = -EINVAL;
329 if (!cur_trans)
330 goto out_unlock; /* bad transid */
331 } else {
332 /* find newest transaction that is committing | committed */
333 list_for_each_entry_reverse(t, &root->fs_info->trans_list,
334 list) {
335 if (t->in_commit) {
336 if (t->commit_done)
337 goto out_unlock;
338 cur_trans = t;
339 break;
340 }
341 }
342 if (!cur_trans)
343 goto out_unlock; /* nothing committing|committed */
344 }
345
13c5a93e 346 atomic_inc(&cur_trans->use_count);
46204592
SW
347 mutex_unlock(&root->fs_info->trans_mutex);
348
349 wait_for_commit(root, cur_trans);
350
351 mutex_lock(&root->fs_info->trans_mutex);
352 put_transaction(cur_trans);
353 ret = 0;
354out_unlock:
355 mutex_unlock(&root->fs_info->trans_mutex);
356 return ret;
357}
358
5d4f98a2 359#if 0
d352ac68 360/*
d397712b
CM
361 * rate limit against the drop_snapshot code. This helps to slow down new
362 * operations if the drop_snapshot code isn't able to keep up.
d352ac68 363 */
37d1aeee 364static void throttle_on_drops(struct btrfs_root *root)
ab78c84d
CM
365{
366 struct btrfs_fs_info *info = root->fs_info;
2dd3e67b 367 int harder_count = 0;
ab78c84d 368
2dd3e67b 369harder:
ab78c84d
CM
370 if (atomic_read(&info->throttles)) {
371 DEFINE_WAIT(wait);
372 int thr;
ab78c84d
CM
373 thr = atomic_read(&info->throttle_gen);
374
375 do {
376 prepare_to_wait(&info->transaction_throttle,
377 &wait, TASK_UNINTERRUPTIBLE);
378 if (!atomic_read(&info->throttles)) {
379 finish_wait(&info->transaction_throttle, &wait);
380 break;
381 }
382 schedule();
383 finish_wait(&info->transaction_throttle, &wait);
384 } while (thr == atomic_read(&info->throttle_gen));
2dd3e67b
CM
385 harder_count++;
386
387 if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
388 harder_count < 2)
389 goto harder;
390
391 if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
392 harder_count < 10)
393 goto harder;
394
395 if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
396 harder_count < 20)
397 goto harder;
ab78c84d
CM
398 }
399}
5d4f98a2 400#endif
ab78c84d 401
37d1aeee
CM
402void btrfs_throttle(struct btrfs_root *root)
403{
404 mutex_lock(&root->fs_info->trans_mutex);
9ca9ee09
SW
405 if (!root->fs_info->open_ioctl_trans)
406 wait_current_trans(root);
37d1aeee 407 mutex_unlock(&root->fs_info->trans_mutex);
37d1aeee
CM
408}
409
8929ecfa
YZ
410static int should_end_transaction(struct btrfs_trans_handle *trans,
411 struct btrfs_root *root)
412{
413 int ret;
414 ret = btrfs_block_rsv_check(trans, root,
415 &root->fs_info->global_block_rsv, 0, 5);
416 return ret ? 1 : 0;
417}
418
419int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
420 struct btrfs_root *root)
421{
422 struct btrfs_transaction *cur_trans = trans->transaction;
423 int updates;
424
425 if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
426 return 1;
427
428 updates = trans->delayed_ref_updates;
429 trans->delayed_ref_updates = 0;
430 if (updates)
431 btrfs_run_delayed_refs(trans, root, updates);
432
433 return should_end_transaction(trans, root);
434}
435
89ce8a63 436static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
0af3d00b 437 struct btrfs_root *root, int throttle, int lock)
79154b1b 438{
8929ecfa 439 struct btrfs_transaction *cur_trans = trans->transaction;
ab78c84d 440 struct btrfs_fs_info *info = root->fs_info;
c3e69d58
CM
441 int count = 0;
442
2a1eb461
JB
443 if (--trans->use_count) {
444 trans->block_rsv = trans->orig_rsv;
445 return 0;
446 }
447
c3e69d58
CM
448 while (count < 4) {
449 unsigned long cur = trans->delayed_ref_updates;
450 trans->delayed_ref_updates = 0;
451 if (cur &&
452 trans->transaction->delayed_refs.num_heads_ready > 64) {
453 trans->delayed_ref_updates = 0;
b7ec40d7
CM
454
455 /*
456 * do a full flush if the transaction is trying
457 * to close
458 */
459 if (trans->transaction->delayed_refs.flushing)
460 cur = 0;
c3e69d58
CM
461 btrfs_run_delayed_refs(trans, root, cur);
462 } else {
463 break;
464 }
465 count++;
56bec294
CM
466 }
467
a22285a6
YZ
468 btrfs_trans_release_metadata(trans, root);
469
0af3d00b 470 if (lock && !root->fs_info->open_ioctl_trans &&
8929ecfa
YZ
471 should_end_transaction(trans, root))
472 trans->transaction->blocked = 1;
473
0af3d00b 474 if (lock && cur_trans->blocked && !cur_trans->in_commit) {
8929ecfa
YZ
475 if (throttle)
476 return btrfs_commit_transaction(trans, root);
477 else
478 wake_up_process(info->transaction_kthread);
479 }
480
8929ecfa 481 WARN_ON(cur_trans != info->running_transaction);
13c5a93e
JB
482 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
483 atomic_dec(&cur_trans->num_writers);
89ce8a63 484
99d16cbc 485 smp_mb();
79154b1b
CM
486 if (waitqueue_active(&cur_trans->writer_wait))
487 wake_up(&cur_trans->writer_wait);
79154b1b 488 put_transaction(cur_trans);
9ed74f2d
JB
489
490 if (current->journal_info == trans)
491 current->journal_info = NULL;
d6025579 492 memset(trans, 0, sizeof(*trans));
2c90e5d6 493 kmem_cache_free(btrfs_trans_handle_cachep, trans);
ab78c84d 494
24bbcf04
YZ
495 if (throttle)
496 btrfs_run_delayed_iputs(root);
497
79154b1b
CM
498 return 0;
499}
500
89ce8a63
CM
501int btrfs_end_transaction(struct btrfs_trans_handle *trans,
502 struct btrfs_root *root)
503{
0af3d00b 504 return __btrfs_end_transaction(trans, root, 0, 1);
89ce8a63
CM
505}
506
507int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root)
509{
0af3d00b
JB
510 return __btrfs_end_transaction(trans, root, 1, 1);
511}
512
513int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root)
515{
516 return __btrfs_end_transaction(trans, root, 0, 0);
89ce8a63
CM
517}
518
d352ac68
CM
519/*
520 * when btree blocks are allocated, they have some corresponding bits set for
521 * them in one of two extent_io trees. This is used to make sure all of
690587d1 522 * those extents are sent to disk but does not wait on them
d352ac68 523 */
690587d1 524int btrfs_write_marked_extents(struct btrfs_root *root,
8cef4e16 525 struct extent_io_tree *dirty_pages, int mark)
79154b1b 526{
7c4452b9 527 int ret;
777e6bd7 528 int err = 0;
7c4452b9
CM
529 int werr = 0;
530 struct page *page;
7c4452b9 531 struct inode *btree_inode = root->fs_info->btree_inode;
777e6bd7 532 u64 start = 0;
5f39d397
CM
533 u64 end;
534 unsigned long index;
7c4452b9 535
d397712b 536 while (1) {
777e6bd7 537 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
8cef4e16 538 mark);
5f39d397 539 if (ret)
7c4452b9 540 break;
d397712b 541 while (start <= end) {
777e6bd7
CM
542 cond_resched();
543
5f39d397 544 index = start >> PAGE_CACHE_SHIFT;
35ebb934 545 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
4bef0848 546 page = find_get_page(btree_inode->i_mapping, index);
7c4452b9
CM
547 if (!page)
548 continue;
4bef0848
CM
549
550 btree_lock_page_hook(page);
551 if (!page->mapping) {
552 unlock_page(page);
553 page_cache_release(page);
554 continue;
555 }
556
6702ed49
CM
557 if (PageWriteback(page)) {
558 if (PageDirty(page))
559 wait_on_page_writeback(page);
560 else {
561 unlock_page(page);
562 page_cache_release(page);
563 continue;
564 }
565 }
7c4452b9
CM
566 err = write_one_page(page, 0);
567 if (err)
568 werr = err;
569 page_cache_release(page);
570 }
571 }
690587d1
CM
572 if (err)
573 werr = err;
574 return werr;
575}
576
577/*
578 * when btree blocks are allocated, they have some corresponding bits set for
579 * them in one of two extent_io trees. This is used to make sure all of
580 * those extents are on disk for transaction or log commit. We wait
581 * on all the pages and clear them from the dirty pages state tree
582 */
583int btrfs_wait_marked_extents(struct btrfs_root *root,
8cef4e16 584 struct extent_io_tree *dirty_pages, int mark)
690587d1
CM
585{
586 int ret;
587 int err = 0;
588 int werr = 0;
589 struct page *page;
590 struct inode *btree_inode = root->fs_info->btree_inode;
591 u64 start = 0;
592 u64 end;
593 unsigned long index;
594
d397712b 595 while (1) {
8cef4e16
YZ
596 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
597 mark);
777e6bd7
CM
598 if (ret)
599 break;
600
8cef4e16 601 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
d397712b 602 while (start <= end) {
777e6bd7
CM
603 index = start >> PAGE_CACHE_SHIFT;
604 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
605 page = find_get_page(btree_inode->i_mapping, index);
606 if (!page)
607 continue;
608 if (PageDirty(page)) {
4bef0848
CM
609 btree_lock_page_hook(page);
610 wait_on_page_writeback(page);
777e6bd7
CM
611 err = write_one_page(page, 0);
612 if (err)
613 werr = err;
614 }
105d931d 615 wait_on_page_writeback(page);
777e6bd7
CM
616 page_cache_release(page);
617 cond_resched();
618 }
619 }
7c4452b9
CM
620 if (err)
621 werr = err;
622 return werr;
79154b1b
CM
623}
624
690587d1
CM
625/*
626 * when btree blocks are allocated, they have some corresponding bits set for
627 * them in one of two extent_io trees. This is used to make sure all of
628 * those extents are on disk for transaction or log commit
629 */
630int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
8cef4e16 631 struct extent_io_tree *dirty_pages, int mark)
690587d1
CM
632{
633 int ret;
634 int ret2;
635
8cef4e16
YZ
636 ret = btrfs_write_marked_extents(root, dirty_pages, mark);
637 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
690587d1
CM
638 return ret || ret2;
639}
640
d0c803c4
CM
641int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
642 struct btrfs_root *root)
643{
644 if (!trans || !trans->transaction) {
645 struct inode *btree_inode;
646 btree_inode = root->fs_info->btree_inode;
647 return filemap_write_and_wait(btree_inode->i_mapping);
648 }
649 return btrfs_write_and_wait_marked_extents(root,
8cef4e16
YZ
650 &trans->transaction->dirty_pages,
651 EXTENT_DIRTY);
d0c803c4
CM
652}
653
d352ac68
CM
654/*
655 * this is used to update the root pointer in the tree of tree roots.
656 *
657 * But, in the case of the extent allocation tree, updating the root
658 * pointer may allocate blocks which may change the root of the extent
659 * allocation tree.
660 *
661 * So, this loops and repeats and makes sure the cowonly root didn't
662 * change while the root pointer was being updated in the metadata.
663 */
0b86a832
CM
664static int update_cowonly_root(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root)
79154b1b
CM
666{
667 int ret;
0b86a832 668 u64 old_root_bytenr;
86b9f2ec 669 u64 old_root_used;
0b86a832 670 struct btrfs_root *tree_root = root->fs_info->tree_root;
79154b1b 671
86b9f2ec 672 old_root_used = btrfs_root_used(&root->root_item);
0b86a832 673 btrfs_write_dirty_block_groups(trans, root);
56bec294 674
d397712b 675 while (1) {
0b86a832 676 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
86b9f2ec
YZ
677 if (old_root_bytenr == root->node->start &&
678 old_root_used == btrfs_root_used(&root->root_item))
79154b1b 679 break;
87ef2bb4 680
5d4f98a2 681 btrfs_set_root_node(&root->root_item, root->node);
79154b1b 682 ret = btrfs_update_root(trans, tree_root,
0b86a832
CM
683 &root->root_key,
684 &root->root_item);
79154b1b 685 BUG_ON(ret);
56bec294 686
86b9f2ec 687 old_root_used = btrfs_root_used(&root->root_item);
4a8c9a62 688 ret = btrfs_write_dirty_block_groups(trans, root);
56bec294 689 BUG_ON(ret);
0b86a832 690 }
276e680d
YZ
691
692 if (root != root->fs_info->extent_root)
693 switch_commit_root(root);
694
0b86a832
CM
695 return 0;
696}
697
d352ac68
CM
698/*
699 * update all the cowonly tree roots on disk
700 */
5d4f98a2
YZ
701static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
702 struct btrfs_root *root)
0b86a832
CM
703{
704 struct btrfs_fs_info *fs_info = root->fs_info;
705 struct list_head *next;
84234f3a 706 struct extent_buffer *eb;
56bec294 707 int ret;
84234f3a 708
56bec294
CM
709 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
710 BUG_ON(ret);
87ef2bb4 711
84234f3a 712 eb = btrfs_lock_root_node(fs_info->tree_root);
9fa8cfe7 713 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
84234f3a
YZ
714 btrfs_tree_unlock(eb);
715 free_extent_buffer(eb);
0b86a832 716
56bec294
CM
717 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
718 BUG_ON(ret);
87ef2bb4 719
d397712b 720 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
0b86a832
CM
721 next = fs_info->dirty_cowonly_roots.next;
722 list_del_init(next);
723 root = list_entry(next, struct btrfs_root, dirty_list);
87ef2bb4 724
0b86a832 725 update_cowonly_root(trans, root);
79154b1b 726 }
276e680d
YZ
727
728 down_write(&fs_info->extent_commit_sem);
729 switch_commit_root(fs_info->extent_root);
730 up_write(&fs_info->extent_commit_sem);
731
79154b1b
CM
732 return 0;
733}
734
d352ac68
CM
735/*
736 * dead roots are old snapshots that need to be deleted. This allocates
737 * a dirty root struct and adds it into the list of dead roots that need to
738 * be deleted
739 */
5d4f98a2 740int btrfs_add_dead_root(struct btrfs_root *root)
5eda7b5e 741{
b48652c1 742 mutex_lock(&root->fs_info->trans_mutex);
5d4f98a2 743 list_add(&root->root_list, &root->fs_info->dead_roots);
b48652c1 744 mutex_unlock(&root->fs_info->trans_mutex);
5eda7b5e
CM
745 return 0;
746}
747
d352ac68 748/*
5d4f98a2 749 * update all the cowonly tree roots on disk
d352ac68 750 */
5d4f98a2
YZ
751static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root)
0f7d52f4 753{
0f7d52f4 754 struct btrfs_root *gang[8];
5d4f98a2 755 struct btrfs_fs_info *fs_info = root->fs_info;
0f7d52f4
CM
756 int i;
757 int ret;
54aa1f4d
CM
758 int err = 0;
759
d397712b 760 while (1) {
5d4f98a2
YZ
761 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
762 (void **)gang, 0,
0f7d52f4
CM
763 ARRAY_SIZE(gang),
764 BTRFS_ROOT_TRANS_TAG);
765 if (ret == 0)
766 break;
767 for (i = 0; i < ret; i++) {
768 root = gang[i];
5d4f98a2
YZ
769 radix_tree_tag_clear(&fs_info->fs_roots_radix,
770 (unsigned long)root->root_key.objectid,
771 BTRFS_ROOT_TRANS_TAG);
31153d81 772
e02119d5 773 btrfs_free_log(trans, root);
5d4f98a2 774 btrfs_update_reloc_root(trans, root);
d68fc57b 775 btrfs_orphan_commit_root(trans, root);
bcc63abb 776
978d910d 777 if (root->commit_root != root->node) {
817d52f8 778 switch_commit_root(root);
978d910d
YZ
779 btrfs_set_root_node(&root->root_item,
780 root->node);
781 }
5d4f98a2 782
5d4f98a2 783 err = btrfs_update_root(trans, fs_info->tree_root,
0f7d52f4
CM
784 &root->root_key,
785 &root->root_item);
54aa1f4d
CM
786 if (err)
787 break;
0f7d52f4
CM
788 }
789 }
54aa1f4d 790 return err;
0f7d52f4
CM
791}
792
d352ac68
CM
793/*
794 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
795 * otherwise every leaf in the btree is read and defragged.
796 */
e9d0b13b
CM
797int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
798{
799 struct btrfs_fs_info *info = root->fs_info;
e9d0b13b 800 struct btrfs_trans_handle *trans;
8929ecfa 801 int ret;
d3c2fdcf 802 unsigned long nr;
e9d0b13b 803
8929ecfa 804 if (xchg(&root->defrag_running, 1))
e9d0b13b 805 return 0;
8929ecfa 806
6b80053d 807 while (1) {
8929ecfa
YZ
808 trans = btrfs_start_transaction(root, 0);
809 if (IS_ERR(trans))
810 return PTR_ERR(trans);
811
e9d0b13b 812 ret = btrfs_defrag_leaves(trans, root, cacheonly);
8929ecfa 813
d3c2fdcf 814 nr = trans->blocks_used;
e9d0b13b 815 btrfs_end_transaction(trans, root);
d3c2fdcf 816 btrfs_btree_balance_dirty(info->tree_root, nr);
e9d0b13b
CM
817 cond_resched();
818
3f157a2f 819 if (root->fs_info->closing || ret != -EAGAIN)
e9d0b13b
CM
820 break;
821 }
822 root->defrag_running = 0;
8929ecfa 823 return ret;
e9d0b13b
CM
824}
825
2c47e605 826#if 0
b7ec40d7
CM
827/*
828 * when dropping snapshots, we generate a ton of delayed refs, and it makes
829 * sense not to join the transaction while it is trying to flush the current
830 * queue of delayed refs out.
831 *
832 * This is used by the drop snapshot code only
833 */
834static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
835{
836 DEFINE_WAIT(wait);
837
838 mutex_lock(&info->trans_mutex);
839 while (info->running_transaction &&
840 info->running_transaction->delayed_refs.flushing) {
841 prepare_to_wait(&info->transaction_wait, &wait,
842 TASK_UNINTERRUPTIBLE);
843 mutex_unlock(&info->trans_mutex);
59bc5c75 844
b7ec40d7 845 schedule();
59bc5c75 846
b7ec40d7
CM
847 mutex_lock(&info->trans_mutex);
848 finish_wait(&info->transaction_wait, &wait);
849 }
850 mutex_unlock(&info->trans_mutex);
851 return 0;
852}
853
d352ac68
CM
854/*
855 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
856 * all of them
857 */
5d4f98a2 858int btrfs_drop_dead_root(struct btrfs_root *root)
0f7d52f4 859{
0f7d52f4 860 struct btrfs_trans_handle *trans;
5d4f98a2 861 struct btrfs_root *tree_root = root->fs_info->tree_root;
d3c2fdcf 862 unsigned long nr;
5d4f98a2 863 int ret;
58176a96 864
5d4f98a2
YZ
865 while (1) {
866 /*
867 * we don't want to jump in and create a bunch of
868 * delayed refs if the transaction is starting to close
869 */
870 wait_transaction_pre_flush(tree_root->fs_info);
871 trans = btrfs_start_transaction(tree_root, 1);
a2135011 872
5d4f98a2
YZ
873 /*
874 * we've joined a transaction, make sure it isn't
875 * closing right now
876 */
877 if (trans->transaction->delayed_refs.flushing) {
878 btrfs_end_transaction(trans, tree_root);
879 continue;
9f3a7427 880 }
58176a96 881
5d4f98a2
YZ
882 ret = btrfs_drop_snapshot(trans, root);
883 if (ret != -EAGAIN)
884 break;
a2135011 885
5d4f98a2
YZ
886 ret = btrfs_update_root(trans, tree_root,
887 &root->root_key,
888 &root->root_item);
889 if (ret)
54aa1f4d 890 break;
bcc63abb 891
d3c2fdcf 892 nr = trans->blocks_used;
0f7d52f4
CM
893 ret = btrfs_end_transaction(trans, tree_root);
894 BUG_ON(ret);
5eda7b5e 895
d3c2fdcf 896 btrfs_btree_balance_dirty(tree_root, nr);
4dc11904 897 cond_resched();
0f7d52f4 898 }
5d4f98a2
YZ
899 BUG_ON(ret);
900
901 ret = btrfs_del_root(trans, tree_root, &root->root_key);
902 BUG_ON(ret);
903
904 nr = trans->blocks_used;
905 ret = btrfs_end_transaction(trans, tree_root);
906 BUG_ON(ret);
907
908 free_extent_buffer(root->node);
909 free_extent_buffer(root->commit_root);
910 kfree(root);
911
912 btrfs_btree_balance_dirty(tree_root, nr);
54aa1f4d 913 return ret;
0f7d52f4 914}
2c47e605 915#endif
0f7d52f4 916
d352ac68
CM
917/*
918 * new snapshots need to be created at a very specific time in the
919 * transaction commit. This does the actual creation
920 */
80b6794d 921static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
3063d29f
CM
922 struct btrfs_fs_info *fs_info,
923 struct btrfs_pending_snapshot *pending)
924{
925 struct btrfs_key key;
80b6794d 926 struct btrfs_root_item *new_root_item;
3063d29f
CM
927 struct btrfs_root *tree_root = fs_info->tree_root;
928 struct btrfs_root *root = pending->root;
6bdb72de
SW
929 struct btrfs_root *parent_root;
930 struct inode *parent_inode;
6a912213 931 struct dentry *parent;
a22285a6 932 struct dentry *dentry;
3063d29f 933 struct extent_buffer *tmp;
925baedd 934 struct extent_buffer *old;
3063d29f 935 int ret;
d68fc57b 936 u64 to_reserve = 0;
6bdb72de 937 u64 index = 0;
a22285a6 938 u64 objectid;
b83cc969 939 u64 root_flags;
3063d29f 940
80b6794d
CM
941 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
942 if (!new_root_item) {
a22285a6 943 pending->error = -ENOMEM;
80b6794d
CM
944 goto fail;
945 }
a22285a6 946
3063d29f 947 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
a22285a6
YZ
948 if (ret) {
949 pending->error = ret;
3063d29f 950 goto fail;
a22285a6 951 }
3063d29f 952
3fd0a558 953 btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
d68fc57b
YZ
954 btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
955
956 if (to_reserve > 0) {
957 ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
8bb8ab2e 958 to_reserve);
d68fc57b
YZ
959 if (ret) {
960 pending->error = ret;
961 goto fail;
962 }
963 }
964
3063d29f 965 key.objectid = objectid;
a22285a6
YZ
966 key.offset = (u64)-1;
967 key.type = BTRFS_ROOT_ITEM_KEY;
3063d29f 968
a22285a6 969 trans->block_rsv = &pending->block_rsv;
3de4586c 970
a22285a6 971 dentry = pending->dentry;
6a912213
JB
972 parent = dget_parent(dentry);
973 parent_inode = parent->d_inode;
a22285a6 974 parent_root = BTRFS_I(parent_inode)->root;
6bdb72de 975 record_root_in_trans(trans, parent_root);
a22285a6 976
3063d29f
CM
977 /*
978 * insert the directory item
979 */
3de4586c 980 ret = btrfs_set_inode_index(parent_inode, &index);
6bdb72de 981 BUG_ON(ret);
0660b5af 982 ret = btrfs_insert_dir_item(trans, parent_root,
a22285a6
YZ
983 dentry->d_name.name, dentry->d_name.len,
984 parent_inode->i_ino, &key,
985 BTRFS_FT_DIR, index);
6bdb72de 986 BUG_ON(ret);
0660b5af 987
a22285a6
YZ
988 btrfs_i_size_write(parent_inode, parent_inode->i_size +
989 dentry->d_name.len * 2);
52c26179
YZ
990 ret = btrfs_update_inode(trans, parent_root, parent_inode);
991 BUG_ON(ret);
992
6bdb72de
SW
993 record_root_in_trans(trans, root);
994 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
995 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
08fe4db1 996 btrfs_check_and_init_root_item(new_root_item);
6bdb72de 997
b83cc969
LZ
998 root_flags = btrfs_root_flags(new_root_item);
999 if (pending->readonly)
1000 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1001 else
1002 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1003 btrfs_set_root_flags(new_root_item, root_flags);
1004
6bdb72de
SW
1005 old = btrfs_lock_root_node(root);
1006 btrfs_cow_block(trans, root, old, NULL, 0, &old);
1007 btrfs_set_lock_blocking(old);
1008
1009 btrfs_copy_root(trans, root, old, &tmp, objectid);
1010 btrfs_tree_unlock(old);
1011 free_extent_buffer(old);
1012
1013 btrfs_set_root_node(new_root_item, tmp);
a22285a6
YZ
1014 /* record when the snapshot was created in key.offset */
1015 key.offset = trans->transid;
1016 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
6bdb72de
SW
1017 btrfs_tree_unlock(tmp);
1018 free_extent_buffer(tmp);
a22285a6 1019 BUG_ON(ret);
6bdb72de 1020
a22285a6
YZ
1021 /*
1022 * insert root back/forward references
1023 */
1024 ret = btrfs_add_root_ref(trans, tree_root, objectid,
0660b5af 1025 parent_root->root_key.objectid,
a22285a6
YZ
1026 parent_inode->i_ino, index,
1027 dentry->d_name.name, dentry->d_name.len);
0660b5af 1028 BUG_ON(ret);
6a912213 1029 dput(parent);
0660b5af 1030
a22285a6
YZ
1031 key.offset = (u64)-1;
1032 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1033 BUG_ON(IS_ERR(pending->snap));
d68fc57b 1034
3fd0a558 1035 btrfs_reloc_post_snapshot(trans, pending);
d68fc57b 1036 btrfs_orphan_post_snapshot(trans, pending);
3063d29f 1037fail:
6bdb72de 1038 kfree(new_root_item);
a22285a6
YZ
1039 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1040 return 0;
3063d29f
CM
1041}
1042
d352ac68
CM
1043/*
1044 * create all the snapshots we've scheduled for creation
1045 */
80b6794d
CM
1046static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1047 struct btrfs_fs_info *fs_info)
3de4586c
CM
1048{
1049 struct btrfs_pending_snapshot *pending;
1050 struct list_head *head = &trans->transaction->pending_snapshots;
3de4586c
CM
1051 int ret;
1052
c6e30871 1053 list_for_each_entry(pending, head, list) {
3de4586c
CM
1054 ret = create_pending_snapshot(trans, fs_info, pending);
1055 BUG_ON(ret);
1056 }
1057 return 0;
1058}
1059
5d4f98a2
YZ
1060static void update_super_roots(struct btrfs_root *root)
1061{
1062 struct btrfs_root_item *root_item;
1063 struct btrfs_super_block *super;
1064
1065 super = &root->fs_info->super_copy;
1066
1067 root_item = &root->fs_info->chunk_root->root_item;
1068 super->chunk_root = root_item->bytenr;
1069 super->chunk_root_generation = root_item->generation;
1070 super->chunk_root_level = root_item->level;
1071
1072 root_item = &root->fs_info->tree_root->root_item;
1073 super->root = root_item->bytenr;
1074 super->generation = root_item->generation;
1075 super->root_level = root_item->level;
0af3d00b
JB
1076 if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
1077 super->cache_generation = root_item->generation;
5d4f98a2
YZ
1078}
1079
f36f3042
CM
1080int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1081{
1082 int ret = 0;
1083 spin_lock(&info->new_trans_lock);
1084 if (info->running_transaction)
1085 ret = info->running_transaction->in_commit;
1086 spin_unlock(&info->new_trans_lock);
1087 return ret;
1088}
1089
8929ecfa
YZ
1090int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1091{
1092 int ret = 0;
1093 spin_lock(&info->new_trans_lock);
1094 if (info->running_transaction)
1095 ret = info->running_transaction->blocked;
1096 spin_unlock(&info->new_trans_lock);
1097 return ret;
1098}
1099
bb9c12c9
SW
1100/*
1101 * wait for the current transaction commit to start and block subsequent
1102 * transaction joins
1103 */
1104static void wait_current_trans_commit_start(struct btrfs_root *root,
1105 struct btrfs_transaction *trans)
1106{
1107 DEFINE_WAIT(wait);
1108
1109 if (trans->in_commit)
1110 return;
1111
1112 while (1) {
1113 prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
1114 TASK_UNINTERRUPTIBLE);
1115 if (trans->in_commit) {
1116 finish_wait(&root->fs_info->transaction_blocked_wait,
1117 &wait);
1118 break;
1119 }
1120 mutex_unlock(&root->fs_info->trans_mutex);
1121 schedule();
1122 mutex_lock(&root->fs_info->trans_mutex);
1123 finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
1124 }
1125}
1126
1127/*
1128 * wait for the current transaction to start and then become unblocked.
1129 * caller holds ref.
1130 */
1131static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
1132 struct btrfs_transaction *trans)
1133{
1134 DEFINE_WAIT(wait);
1135
1136 if (trans->commit_done || (trans->in_commit && !trans->blocked))
1137 return;
1138
1139 while (1) {
1140 prepare_to_wait(&root->fs_info->transaction_wait, &wait,
1141 TASK_UNINTERRUPTIBLE);
1142 if (trans->commit_done ||
1143 (trans->in_commit && !trans->blocked)) {
1144 finish_wait(&root->fs_info->transaction_wait,
1145 &wait);
1146 break;
1147 }
1148 mutex_unlock(&root->fs_info->trans_mutex);
1149 schedule();
1150 mutex_lock(&root->fs_info->trans_mutex);
1151 finish_wait(&root->fs_info->transaction_wait,
1152 &wait);
1153 }
1154}
1155
1156/*
1157 * commit transactions asynchronously. once btrfs_commit_transaction_async
1158 * returns, any subsequent transaction will not be allowed to join.
1159 */
1160struct btrfs_async_commit {
1161 struct btrfs_trans_handle *newtrans;
1162 struct btrfs_root *root;
1163 struct delayed_work work;
1164};
1165
1166static void do_async_commit(struct work_struct *work)
1167{
1168 struct btrfs_async_commit *ac =
1169 container_of(work, struct btrfs_async_commit, work.work);
1170
1171 btrfs_commit_transaction(ac->newtrans, ac->root);
1172 kfree(ac);
1173}
1174
1175int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root,
1177 int wait_for_unblock)
1178{
1179 struct btrfs_async_commit *ac;
1180 struct btrfs_transaction *cur_trans;
1181
1182 ac = kmalloc(sizeof(*ac), GFP_NOFS);
db5b493a
TI
1183 if (!ac)
1184 return -ENOMEM;
bb9c12c9
SW
1185
1186 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1187 ac->root = root;
7a7eaa40 1188 ac->newtrans = btrfs_join_transaction(root);
3612b495
TI
1189 if (IS_ERR(ac->newtrans)) {
1190 int err = PTR_ERR(ac->newtrans);
1191 kfree(ac);
1192 return err;
1193 }
bb9c12c9
SW
1194
1195 /* take transaction reference */
1196 mutex_lock(&root->fs_info->trans_mutex);
1197 cur_trans = trans->transaction;
13c5a93e 1198 atomic_inc(&cur_trans->use_count);
bb9c12c9
SW
1199 mutex_unlock(&root->fs_info->trans_mutex);
1200
1201 btrfs_end_transaction(trans, root);
1202 schedule_delayed_work(&ac->work, 0);
1203
1204 /* wait for transaction to start and unblock */
1205 mutex_lock(&root->fs_info->trans_mutex);
1206 if (wait_for_unblock)
1207 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1208 else
1209 wait_current_trans_commit_start(root, cur_trans);
1210 put_transaction(cur_trans);
1211 mutex_unlock(&root->fs_info->trans_mutex);
1212
1213 return 0;
1214}
1215
1216/*
1217 * btrfs_transaction state sequence:
1218 * in_commit = 0, blocked = 0 (initial)
1219 * in_commit = 1, blocked = 1
1220 * blocked = 0
1221 * commit_done = 1
1222 */
79154b1b
CM
1223int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1224 struct btrfs_root *root)
1225{
15ee9bc7 1226 unsigned long joined = 0;
79154b1b 1227 struct btrfs_transaction *cur_trans;
8fd17795 1228 struct btrfs_transaction *prev_trans = NULL;
79154b1b 1229 DEFINE_WAIT(wait);
15ee9bc7 1230 int ret;
89573b9c
CM
1231 int should_grow = 0;
1232 unsigned long now = get_seconds();
dccae999 1233 int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
79154b1b 1234
5a3f23d5
CM
1235 btrfs_run_ordered_operations(root, 0);
1236
56bec294
CM
1237 /* make a pass through all the delayed refs we have so far
1238 * any runnings procs may add more while we are here
1239 */
1240 ret = btrfs_run_delayed_refs(trans, root, 0);
1241 BUG_ON(ret);
1242
a22285a6
YZ
1243 btrfs_trans_release_metadata(trans, root);
1244
b7ec40d7 1245 cur_trans = trans->transaction;
56bec294
CM
1246 /*
1247 * set the flushing flag so procs in this transaction have to
1248 * start sending their work down.
1249 */
b7ec40d7 1250 cur_trans->delayed_refs.flushing = 1;
56bec294 1251
c3e69d58 1252 ret = btrfs_run_delayed_refs(trans, root, 0);
56bec294
CM
1253 BUG_ON(ret);
1254
79154b1b 1255 mutex_lock(&root->fs_info->trans_mutex);
b7ec40d7 1256 if (cur_trans->in_commit) {
13c5a93e 1257 atomic_inc(&cur_trans->use_count);
ccd467d6 1258 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b 1259 btrfs_end_transaction(trans, root);
ccd467d6 1260
79154b1b
CM
1261 ret = wait_for_commit(root, cur_trans);
1262 BUG_ON(ret);
15ee9bc7
JB
1263
1264 mutex_lock(&root->fs_info->trans_mutex);
79154b1b 1265 put_transaction(cur_trans);
15ee9bc7
JB
1266 mutex_unlock(&root->fs_info->trans_mutex);
1267
79154b1b
CM
1268 return 0;
1269 }
4313b399 1270
2c90e5d6 1271 trans->transaction->in_commit = 1;
f9295749 1272 trans->transaction->blocked = 1;
bb9c12c9
SW
1273 wake_up(&root->fs_info->transaction_blocked_wait);
1274
ccd467d6
CM
1275 if (cur_trans->list.prev != &root->fs_info->trans_list) {
1276 prev_trans = list_entry(cur_trans->list.prev,
1277 struct btrfs_transaction, list);
1278 if (!prev_trans->commit_done) {
13c5a93e 1279 atomic_inc(&prev_trans->use_count);
ccd467d6
CM
1280 mutex_unlock(&root->fs_info->trans_mutex);
1281
1282 wait_for_commit(root, prev_trans);
ccd467d6 1283
ccd467d6 1284 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 1285 put_transaction(prev_trans);
ccd467d6
CM
1286 }
1287 }
15ee9bc7 1288
89573b9c
CM
1289 if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1290 should_grow = 1;
1291
15ee9bc7 1292 do {
7ea394f1 1293 int snap_pending = 0;
15ee9bc7 1294 joined = cur_trans->num_joined;
7ea394f1
YZ
1295 if (!list_empty(&trans->transaction->pending_snapshots))
1296 snap_pending = 1;
1297
2c90e5d6 1298 WARN_ON(cur_trans != trans->transaction);
79154b1b 1299 mutex_unlock(&root->fs_info->trans_mutex);
15ee9bc7 1300
0bdb1db2 1301 if (flush_on_commit || snap_pending) {
24bbcf04
YZ
1302 btrfs_start_delalloc_inodes(root, 1);
1303 ret = btrfs_wait_ordered_extents(root, 0, 1);
ebecd3d9 1304 BUG_ON(ret);
7ea394f1
YZ
1305 }
1306
5a3f23d5
CM
1307 /*
1308 * rename don't use btrfs_join_transaction, so, once we
1309 * set the transaction to blocked above, we aren't going
1310 * to get any new ordered operations. We can safely run
1311 * it here and no for sure that nothing new will be added
1312 * to the list
1313 */
1314 btrfs_run_ordered_operations(root, 1);
1315
ed3b3d31
CM
1316 prepare_to_wait(&cur_trans->writer_wait, &wait,
1317 TASK_UNINTERRUPTIBLE);
1318
89573b9c 1319 smp_mb();
13c5a93e 1320 if (atomic_read(&cur_trans->num_writers) > 1)
99d16cbc
SW
1321 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1322 else if (should_grow)
1323 schedule_timeout(1);
15ee9bc7 1324
79154b1b 1325 mutex_lock(&root->fs_info->trans_mutex);
15ee9bc7 1326 finish_wait(&cur_trans->writer_wait, &wait);
13c5a93e 1327 } while (atomic_read(&cur_trans->num_writers) > 1 ||
89573b9c 1328 (should_grow && cur_trans->num_joined != joined));
15ee9bc7 1329
3063d29f
CM
1330 ret = create_pending_snapshots(trans, root->fs_info);
1331 BUG_ON(ret);
1332
56bec294
CM
1333 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1334 BUG_ON(ret);
1335
2c90e5d6 1336 WARN_ON(cur_trans != trans->transaction);
dc17ff8f 1337
e02119d5
CM
1338 /* btrfs_commit_tree_roots is responsible for getting the
1339 * various roots consistent with each other. Every pointer
1340 * in the tree of tree roots has to point to the most up to date
1341 * root for every subvolume and other tree. So, we have to keep
1342 * the tree logging code from jumping in and changing any
1343 * of the trees.
1344 *
1345 * At this point in the commit, there can't be any tree-log
1346 * writers, but a little lower down we drop the trans mutex
1347 * and let new people in. By holding the tree_log_mutex
1348 * from now until after the super is written, we avoid races
1349 * with the tree-log code.
1350 */
1351 mutex_lock(&root->fs_info->tree_log_mutex);
1352
5d4f98a2 1353 ret = commit_fs_roots(trans, root);
54aa1f4d
CM
1354 BUG_ON(ret);
1355
5d4f98a2 1356 /* commit_fs_roots gets rid of all the tree log roots, it is now
e02119d5
CM
1357 * safe to free the root of tree log roots
1358 */
1359 btrfs_free_log_root_tree(trans, root->fs_info);
1360
5d4f98a2 1361 ret = commit_cowonly_roots(trans, root);
79154b1b 1362 BUG_ON(ret);
54aa1f4d 1363
11833d66
YZ
1364 btrfs_prepare_extent_commit(trans, root);
1365
78fae27e 1366 cur_trans = root->fs_info->running_transaction;
cee36a03 1367 spin_lock(&root->fs_info->new_trans_lock);
78fae27e 1368 root->fs_info->running_transaction = NULL;
cee36a03 1369 spin_unlock(&root->fs_info->new_trans_lock);
5d4f98a2
YZ
1370
1371 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1372 root->fs_info->tree_root->node);
817d52f8 1373 switch_commit_root(root->fs_info->tree_root);
5d4f98a2
YZ
1374
1375 btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
1376 root->fs_info->chunk_root->node);
817d52f8 1377 switch_commit_root(root->fs_info->chunk_root);
5d4f98a2
YZ
1378
1379 update_super_roots(root);
e02119d5
CM
1380
1381 if (!root->fs_info->log_root_recovering) {
1382 btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
1383 btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
1384 }
1385
a061fc8d
CM
1386 memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
1387 sizeof(root->fs_info->super_copy));
ccd467d6 1388
f9295749 1389 trans->transaction->blocked = 0;
b7ec40d7 1390
f9295749 1391 wake_up(&root->fs_info->transaction_wait);
e6dcd2dc 1392
78fae27e 1393 mutex_unlock(&root->fs_info->trans_mutex);
79154b1b
CM
1394 ret = btrfs_write_and_wait_transaction(trans, root);
1395 BUG_ON(ret);
a512bbf8 1396 write_ctree_super(trans, root, 0);
4313b399 1397
e02119d5
CM
1398 /*
1399 * the super is written, we can safely allow the tree-loggers
1400 * to go about their business
1401 */
1402 mutex_unlock(&root->fs_info->tree_log_mutex);
1403
11833d66 1404 btrfs_finish_extent_commit(trans, root);
4313b399 1405
1a40e23b
ZY
1406 mutex_lock(&root->fs_info->trans_mutex);
1407
2c90e5d6 1408 cur_trans->commit_done = 1;
b7ec40d7 1409
15ee9bc7 1410 root->fs_info->last_trans_committed = cur_trans->transid;
817d52f8 1411
2c90e5d6 1412 wake_up(&cur_trans->commit_wait);
3de4586c 1413
13c5a93e 1414 list_del_init(&cur_trans->list);
78fae27e 1415 put_transaction(cur_trans);
79154b1b 1416 put_transaction(cur_trans);
58176a96 1417
1abe9b8a 1418 trace_btrfs_transaction_commit(root);
1419
78fae27e 1420 mutex_unlock(&root->fs_info->trans_mutex);
3de4586c 1421
9ed74f2d
JB
1422 if (current->journal_info == trans)
1423 current->journal_info = NULL;
1424
2c90e5d6 1425 kmem_cache_free(btrfs_trans_handle_cachep, trans);
24bbcf04
YZ
1426
1427 if (current != root->fs_info->transaction_kthread)
1428 btrfs_run_delayed_iputs(root);
1429
79154b1b
CM
1430 return ret;
1431}
1432
d352ac68
CM
1433/*
1434 * interface function to delete all the snapshots we have scheduled for deletion
1435 */
e9d0b13b
CM
1436int btrfs_clean_old_snapshots(struct btrfs_root *root)
1437{
5d4f98a2
YZ
1438 LIST_HEAD(list);
1439 struct btrfs_fs_info *fs_info = root->fs_info;
1440
1441 mutex_lock(&fs_info->trans_mutex);
1442 list_splice_init(&fs_info->dead_roots, &list);
1443 mutex_unlock(&fs_info->trans_mutex);
e9d0b13b 1444
5d4f98a2
YZ
1445 while (!list_empty(&list)) {
1446 root = list_entry(list.next, struct btrfs_root, root_list);
76dda93c
YZ
1447 list_del(&root->root_list);
1448
1449 if (btrfs_header_backref_rev(root->node) <
1450 BTRFS_MIXED_BACKREF_REV)
3fd0a558 1451 btrfs_drop_snapshot(root, NULL, 0);
76dda93c 1452 else
3fd0a558 1453 btrfs_drop_snapshot(root, NULL, 1);
e9d0b13b
CM
1454 }
1455 return 0;
1456}