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