transaction.c (e3900e74f26fc924c8e9e2a922bd40369b0bb517) transaction.c (2382c5cc7ed0396b61a359765bf5ee125b0a2f46)
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,

--- 26 unchanged lines hidden (view full) ---

35#define BTRFS_ROOT_TRANS_TAG 0
36
37void put_transaction(struct btrfs_transaction *transaction)
38{
39 WARN_ON(atomic_read(&transaction->use_count) == 0);
40 if (atomic_dec_and_test(&transaction->use_count)) {
41 BUG_ON(!list_empty(&transaction->list));
42 WARN_ON(transaction->delayed_refs.root.rb_node);
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,

--- 26 unchanged lines hidden (view full) ---

35#define BTRFS_ROOT_TRANS_TAG 0
36
37void put_transaction(struct btrfs_transaction *transaction)
38{
39 WARN_ON(atomic_read(&transaction->use_count) == 0);
40 if (atomic_dec_and_test(&transaction->use_count)) {
41 BUG_ON(!list_empty(&transaction->list));
42 WARN_ON(transaction->delayed_refs.root.rb_node);
43 memset(transaction, 0, sizeof(*transaction));
44 kmem_cache_free(btrfs_transaction_cachep, transaction);
45 }
46}
47
48static noinline void switch_commit_root(struct btrfs_root *root)
49{
50 free_extent_buffer(root->commit_root);
51 root->commit_root = btrfs_root_node(root);
52}
53
43 kmem_cache_free(btrfs_transaction_cachep, transaction);
44 }
45}
46
47static noinline void switch_commit_root(struct btrfs_root *root)
48{
49 free_extent_buffer(root->commit_root);
50 root->commit_root = btrfs_root_node(root);
51}
52
53static inline int can_join_transaction(struct btrfs_transaction *trans,
54 int type)
55{
56 return !(trans->in_commit &&
57 type != TRANS_JOIN &&
58 type != TRANS_JOIN_NOLOCK);
59}
60
54/*
55 * either allocate a new transaction or hop into the existing one
56 */
57static noinline int join_transaction(struct btrfs_root *root, int type)
58{
59 struct btrfs_transaction *cur_trans;
60 struct btrfs_fs_info *fs_info = root->fs_info;
61
62 spin_lock(&fs_info->trans_lock);
63loop:
64 /* The file system has been taken offline. No new transactions. */
61/*
62 * either allocate a new transaction or hop into the existing one
63 */
64static noinline int join_transaction(struct btrfs_root *root, int type)
65{
66 struct btrfs_transaction *cur_trans;
67 struct btrfs_fs_info *fs_info = root->fs_info;
68
69 spin_lock(&fs_info->trans_lock);
70loop:
71 /* The file system has been taken offline. No new transactions. */
65 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
72 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
66 spin_unlock(&fs_info->trans_lock);
67 return -EROFS;
68 }
69
70 if (fs_info->trans_no_join) {
71 /*
72 * If we are JOIN_NOLOCK we're already committing a current
73 * transaction, we just need a handle to deal with something

--- 7 unchanged lines hidden (view full) ---

81 }
82
83 cur_trans = fs_info->running_transaction;
84 if (cur_trans) {
85 if (cur_trans->aborted) {
86 spin_unlock(&fs_info->trans_lock);
87 return cur_trans->aborted;
88 }
73 spin_unlock(&fs_info->trans_lock);
74 return -EROFS;
75 }
76
77 if (fs_info->trans_no_join) {
78 /*
79 * If we are JOIN_NOLOCK we're already committing a current
80 * transaction, we just need a handle to deal with something

--- 7 unchanged lines hidden (view full) ---

88 }
89
90 cur_trans = fs_info->running_transaction;
91 if (cur_trans) {
92 if (cur_trans->aborted) {
93 spin_unlock(&fs_info->trans_lock);
94 return cur_trans->aborted;
95 }
96 if (!can_join_transaction(cur_trans, type)) {
97 spin_unlock(&fs_info->trans_lock);
98 return -EBUSY;
99 }
89 atomic_inc(&cur_trans->use_count);
90 atomic_inc(&cur_trans->num_writers);
91 cur_trans->num_joined++;
92 spin_unlock(&fs_info->trans_lock);
93 return 0;
94 }
95 spin_unlock(&fs_info->trans_lock);
96

--- 12 unchanged lines hidden (view full) ---

109 if (fs_info->running_transaction) {
110 /*
111 * someone started a transaction after we unlocked. Make sure
112 * to redo the trans_no_join checks above
113 */
114 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
115 cur_trans = fs_info->running_transaction;
116 goto loop;
100 atomic_inc(&cur_trans->use_count);
101 atomic_inc(&cur_trans->num_writers);
102 cur_trans->num_joined++;
103 spin_unlock(&fs_info->trans_lock);
104 return 0;
105 }
106 spin_unlock(&fs_info->trans_lock);
107

--- 12 unchanged lines hidden (view full) ---

120 if (fs_info->running_transaction) {
121 /*
122 * someone started a transaction after we unlocked. Make sure
123 * to redo the trans_no_join checks above
124 */
125 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
126 cur_trans = fs_info->running_transaction;
127 goto loop;
117 } else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
128 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
118 spin_unlock(&fs_info->trans_lock);
119 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
120 return -EROFS;
121 }
122
123 atomic_set(&cur_trans->num_writers, 1);
124 cur_trans->num_joined = 0;
125 init_waitqueue_head(&cur_trans->writer_wait);

--- 25 unchanged lines hidden (view full) ---

151 "creating a fresh transaction\n");
152 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
153 WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
154 "creating a fresh transaction\n");
155 atomic_set(&fs_info->tree_mod_seq, 0);
156
157 spin_lock_init(&cur_trans->commit_lock);
158 spin_lock_init(&cur_trans->delayed_refs.lock);
129 spin_unlock(&fs_info->trans_lock);
130 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
131 return -EROFS;
132 }
133
134 atomic_set(&cur_trans->num_writers, 1);
135 cur_trans->num_joined = 0;
136 init_waitqueue_head(&cur_trans->writer_wait);

--- 25 unchanged lines hidden (view full) ---

162 "creating a fresh transaction\n");
163 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
164 WARN(1, KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
165 "creating a fresh transaction\n");
166 atomic_set(&fs_info->tree_mod_seq, 0);
167
168 spin_lock_init(&cur_trans->commit_lock);
169 spin_lock_init(&cur_trans->delayed_refs.lock);
170 atomic_set(&cur_trans->delayed_refs.procs_running_refs, 0);
171 atomic_set(&cur_trans->delayed_refs.ref_seq, 0);
172 init_waitqueue_head(&cur_trans->delayed_refs.wait);
159
160 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
173
174 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
175 INIT_LIST_HEAD(&cur_trans->ordered_operations);
161 list_add_tail(&cur_trans->list, &fs_info->trans_list);
162 extent_io_tree_init(&cur_trans->dirty_pages,
163 fs_info->btree_inode->i_mapping);
164 fs_info->generation++;
165 cur_trans->transid = fs_info->generation;
166 fs_info->running_transaction = cur_trans;
167 cur_trans->aborted = 0;
168 spin_unlock(&fs_info->trans_lock);

--- 128 unchanged lines hidden (view full) ---

297 enum btrfs_reserve_flush_enum flush)
298{
299 struct btrfs_trans_handle *h;
300 struct btrfs_transaction *cur_trans;
301 u64 num_bytes = 0;
302 int ret;
303 u64 qgroup_reserved = 0;
304
176 list_add_tail(&cur_trans->list, &fs_info->trans_list);
177 extent_io_tree_init(&cur_trans->dirty_pages,
178 fs_info->btree_inode->i_mapping);
179 fs_info->generation++;
180 cur_trans->transid = fs_info->generation;
181 fs_info->running_transaction = cur_trans;
182 cur_trans->aborted = 0;
183 spin_unlock(&fs_info->trans_lock);

--- 128 unchanged lines hidden (view full) ---

312 enum btrfs_reserve_flush_enum flush)
313{
314 struct btrfs_trans_handle *h;
315 struct btrfs_transaction *cur_trans;
316 u64 num_bytes = 0;
317 int ret;
318 u64 qgroup_reserved = 0;
319
305 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
320 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
306 return ERR_PTR(-EROFS);
307
308 if (current->journal_info) {
309 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
310 h = current->journal_info;
311 h->use_count++;
312 WARN_ON(h->use_count > 2);
313 h->orig_rsv = h->block_rsv;

--- 14 unchanged lines hidden (view full) ---

328 return ERR_PTR(ret);
329 }
330
331 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
332 ret = btrfs_block_rsv_add(root,
333 &root->fs_info->trans_block_rsv,
334 num_bytes, flush);
335 if (ret)
321 return ERR_PTR(-EROFS);
322
323 if (current->journal_info) {
324 WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
325 h = current->journal_info;
326 h->use_count++;
327 WARN_ON(h->use_count > 2);
328 h->orig_rsv = h->block_rsv;

--- 14 unchanged lines hidden (view full) ---

343 return ERR_PTR(ret);
344 }
345
346 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
347 ret = btrfs_block_rsv_add(root,
348 &root->fs_info->trans_block_rsv,
349 num_bytes, flush);
350 if (ret)
336 return ERR_PTR(ret);
351 goto reserve_fail;
337 }
338again:
339 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
352 }
353again:
354 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
340 if (!h)
341 return ERR_PTR(-ENOMEM);
355 if (!h) {
356 ret = -ENOMEM;
357 goto alloc_fail;
358 }
342
343 /*
344 * If we are JOIN_NOLOCK we're already committing a transaction and
345 * waiting on this guy, so we don't need to do the sb_start_intwrite
346 * because we're already holding a ref. We need this because we could
347 * have raced in and did an fsync() on a file which can kick a commit
348 * and then we deadlock with somebody doing a freeze.
349 *
350 * If we are ATTACH, it means we just want to catch the current
351 * transaction and commit it, so we needn't do sb_start_intwrite().
352 */
353 if (type < TRANS_JOIN_NOLOCK)
354 sb_start_intwrite(root->fs_info->sb);
355
356 if (may_wait_transaction(root, type))
357 wait_current_trans(root);
358
359 do {
360 ret = join_transaction(root, type);
359
360 /*
361 * If we are JOIN_NOLOCK we're already committing a transaction and
362 * waiting on this guy, so we don't need to do the sb_start_intwrite
363 * because we're already holding a ref. We need this because we could
364 * have raced in and did an fsync() on a file which can kick a commit
365 * and then we deadlock with somebody doing a freeze.
366 *
367 * If we are ATTACH, it means we just want to catch the current
368 * transaction and commit it, so we needn't do sb_start_intwrite().
369 */
370 if (type < TRANS_JOIN_NOLOCK)
371 sb_start_intwrite(root->fs_info->sb);
372
373 if (may_wait_transaction(root, type))
374 wait_current_trans(root);
375
376 do {
377 ret = join_transaction(root, type);
361 if (ret == -EBUSY)
378 if (ret == -EBUSY) {
362 wait_current_trans(root);
379 wait_current_trans(root);
380 if (unlikely(type == TRANS_ATTACH))
381 ret = -ENOENT;
382 }
363 } while (ret == -EBUSY);
364
365 if (ret < 0) {
366 /* We must get the transaction if we are JOIN_NOLOCK. */
367 BUG_ON(type == TRANS_JOIN_NOLOCK);
383 } while (ret == -EBUSY);
384
385 if (ret < 0) {
386 /* We must get the transaction if we are JOIN_NOLOCK. */
387 BUG_ON(type == TRANS_JOIN_NOLOCK);
368
369 if (type < TRANS_JOIN_NOLOCK)
370 sb_end_intwrite(root->fs_info->sb);
371 kmem_cache_free(btrfs_trans_handle_cachep, h);
372 return ERR_PTR(ret);
388 goto join_fail;
373 }
374
375 cur_trans = root->fs_info->running_transaction;
376
377 h->transid = cur_trans->transid;
378 h->transaction = cur_trans;
379 h->blocks_used = 0;
380 h->bytes_reserved = 0;
381 h->root = root;
382 h->delayed_ref_updates = 0;
383 h->use_count = 1;
384 h->adding_csums = 0;
385 h->block_rsv = NULL;
386 h->orig_rsv = NULL;
387 h->aborted = 0;
389 }
390
391 cur_trans = root->fs_info->running_transaction;
392
393 h->transid = cur_trans->transid;
394 h->transaction = cur_trans;
395 h->blocks_used = 0;
396 h->bytes_reserved = 0;
397 h->root = root;
398 h->delayed_ref_updates = 0;
399 h->use_count = 1;
400 h->adding_csums = 0;
401 h->block_rsv = NULL;
402 h->orig_rsv = NULL;
403 h->aborted = 0;
388 h->qgroup_reserved = qgroup_reserved;
404 h->qgroup_reserved = 0;
389 h->delayed_ref_elem.seq = 0;
390 h->type = type;
405 h->delayed_ref_elem.seq = 0;
406 h->type = type;
407 h->allocating_chunk = false;
391 INIT_LIST_HEAD(&h->qgroup_ref_list);
392 INIT_LIST_HEAD(&h->new_bgs);
393
394 smp_mb();
395 if (cur_trans->blocked && may_wait_transaction(root, type)) {
396 btrfs_commit_transaction(h, root);
397 goto again;
398 }
399
400 if (num_bytes) {
401 trace_btrfs_space_reservation(root->fs_info, "transaction",
402 h->transid, num_bytes, 1);
403 h->block_rsv = &root->fs_info->trans_block_rsv;
404 h->bytes_reserved = num_bytes;
405 }
408 INIT_LIST_HEAD(&h->qgroup_ref_list);
409 INIT_LIST_HEAD(&h->new_bgs);
410
411 smp_mb();
412 if (cur_trans->blocked && may_wait_transaction(root, type)) {
413 btrfs_commit_transaction(h, root);
414 goto again;
415 }
416
417 if (num_bytes) {
418 trace_btrfs_space_reservation(root->fs_info, "transaction",
419 h->transid, num_bytes, 1);
420 h->block_rsv = &root->fs_info->trans_block_rsv;
421 h->bytes_reserved = num_bytes;
422 }
423 h->qgroup_reserved = qgroup_reserved;
406
407got_it:
408 btrfs_record_root_in_trans(h, root);
409
410 if (!current->journal_info && type != TRANS_USERSPACE)
411 current->journal_info = h;
412 return h;
424
425got_it:
426 btrfs_record_root_in_trans(h, root);
427
428 if (!current->journal_info && type != TRANS_USERSPACE)
429 current->journal_info = h;
430 return h;
431
432join_fail:
433 if (type < TRANS_JOIN_NOLOCK)
434 sb_end_intwrite(root->fs_info->sb);
435 kmem_cache_free(btrfs_trans_handle_cachep, h);
436alloc_fail:
437 if (num_bytes)
438 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
439 num_bytes);
440reserve_fail:
441 if (qgroup_reserved)
442 btrfs_qgroup_free(root, qgroup_reserved);
443 return ERR_PTR(ret);
413}
414
415struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
416 int num_items)
417{
418 return start_transaction(root, num_items, TRANS_START,
419 BTRFS_RESERVE_FLUSH_ALL);
420}

--- 15 unchanged lines hidden (view full) ---

436 return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
437}
438
439struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
440{
441 return start_transaction(root, 0, TRANS_USERSPACE, 0);
442}
443
444}
445
446struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
447 int num_items)
448{
449 return start_transaction(root, num_items, TRANS_START,
450 BTRFS_RESERVE_FLUSH_ALL);
451}

--- 15 unchanged lines hidden (view full) ---

467 return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
468}
469
470struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
471{
472 return start_transaction(root, 0, TRANS_USERSPACE, 0);
473}
474
475/*
476 * btrfs_attach_transaction() - catch the running transaction
477 *
478 * It is used when we want to commit the current the transaction, but
479 * don't want to start a new one.
480 *
481 * Note: If this function return -ENOENT, it just means there is no
482 * running transaction. But it is possible that the inactive transaction
483 * is still in the memory, not fully on disk. If you hope there is no
484 * inactive transaction in the fs when -ENOENT is returned, you should
485 * invoke
486 * btrfs_attach_transaction_barrier()
487 */
444struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
445{
446 return start_transaction(root, 0, TRANS_ATTACH, 0);
447}
448
488struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
489{
490 return start_transaction(root, 0, TRANS_ATTACH, 0);
491}
492
493/*
494 * btrfs_attach_transaction() - catch the running transaction
495 *
496 * It is similar to the above function, the differentia is this one
497 * will wait for all the inactive transactions until they fully
498 * complete.
499 */
500struct btrfs_trans_handle *
501btrfs_attach_transaction_barrier(struct btrfs_root *root)
502{
503 struct btrfs_trans_handle *trans;
504
505 trans = start_transaction(root, 0, TRANS_ATTACH, 0);
506 if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
507 btrfs_wait_for_commit(root, 0);
508
509 return trans;
510}
511
449/* wait for a transaction commit to be fully complete */
450static noinline void wait_for_commit(struct btrfs_root *root,
451 struct btrfs_transaction *commit)
452{
453 wait_event(commit->commit_wait, commit->commit_done);
454}
455
456int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)

--- 115 unchanged lines hidden (view full) ---

572 if (trans->qgroup_reserved) {
573 btrfs_qgroup_free(root, trans->qgroup_reserved);
574 trans->qgroup_reserved = 0;
575 }
576
577 if (!list_empty(&trans->new_bgs))
578 btrfs_create_pending_block_groups(trans, root);
579
512/* wait for a transaction commit to be fully complete */
513static noinline void wait_for_commit(struct btrfs_root *root,
514 struct btrfs_transaction *commit)
515{
516 wait_event(commit->commit_wait, commit->commit_done);
517}
518
519int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)

--- 115 unchanged lines hidden (view full) ---

635 if (trans->qgroup_reserved) {
636 btrfs_qgroup_free(root, trans->qgroup_reserved);
637 trans->qgroup_reserved = 0;
638 }
639
640 if (!list_empty(&trans->new_bgs))
641 btrfs_create_pending_block_groups(trans, root);
642
580 while (count < 2) {
643 while (count < 1) {
581 unsigned long cur = trans->delayed_ref_updates;
582 trans->delayed_ref_updates = 0;
583 if (cur &&
584 trans->transaction->delayed_refs.num_heads_ready > 64) {
585 trans->delayed_ref_updates = 0;
586 btrfs_run_delayed_refs(trans, root, cur);
587 } else {
588 break;
589 }
590 count++;
591 }
644 unsigned long cur = trans->delayed_ref_updates;
645 trans->delayed_ref_updates = 0;
646 if (cur &&
647 trans->transaction->delayed_refs.num_heads_ready > 64) {
648 trans->delayed_ref_updates = 0;
649 btrfs_run_delayed_refs(trans, root, cur);
650 } else {
651 break;
652 }
653 count++;
654 }
655
592 btrfs_trans_release_metadata(trans, root);
593 trans->block_rsv = NULL;
594
595 if (!list_empty(&trans->new_bgs))
596 btrfs_create_pending_block_groups(trans, root);
597
598 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
599 should_end_transaction(trans, root)) {

--- 29 unchanged lines hidden (view full) ---

629
630 if (current->journal_info == trans)
631 current->journal_info = NULL;
632
633 if (throttle)
634 btrfs_run_delayed_iputs(root);
635
636 if (trans->aborted ||
656 btrfs_trans_release_metadata(trans, root);
657 trans->block_rsv = NULL;
658
659 if (!list_empty(&trans->new_bgs))
660 btrfs_create_pending_block_groups(trans, root);
661
662 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
663 should_end_transaction(trans, root)) {

--- 29 unchanged lines hidden (view full) ---

693
694 if (current->journal_info == trans)
695 current->journal_info = NULL;
696
697 if (throttle)
698 btrfs_run_delayed_iputs(root);
699
700 if (trans->aborted ||
637 root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
701 test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
638 err = -EIO;
702 err = -EIO;
639 }
640 assert_qgroups_uptodate(trans);
641
703 assert_qgroups_uptodate(trans);
704
642 memset(trans, 0, sizeof(*trans));
643 kmem_cache_free(btrfs_trans_handle_cachep, trans);
644 return err;
645}
646
647int btrfs_end_transaction(struct btrfs_trans_handle *trans,
648 struct btrfs_root *root)
649{
650 int ret;

--- 30 unchanged lines hidden (view full) ---

681 struct extent_io_tree *dirty_pages, int mark)
682{
683 int err = 0;
684 int werr = 0;
685 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
686 struct extent_state *cached_state = NULL;
687 u64 start = 0;
688 u64 end;
705 kmem_cache_free(btrfs_trans_handle_cachep, trans);
706 return err;
707}
708
709int btrfs_end_transaction(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root)
711{
712 int ret;

--- 30 unchanged lines hidden (view full) ---

743 struct extent_io_tree *dirty_pages, int mark)
744{
745 int err = 0;
746 int werr = 0;
747 struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
748 struct extent_state *cached_state = NULL;
749 u64 start = 0;
750 u64 end;
751 struct blk_plug plug;
689
752
753 blk_start_plug(&plug);
690 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
691 mark, &cached_state)) {
692 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
693 mark, &cached_state, GFP_NOFS);
694 cached_state = NULL;
695 err = filemap_fdatawrite_range(mapping, start, end);
696 if (err)
697 werr = err;
698 cond_resched();
699 start = end + 1;
700 }
701 if (err)
702 werr = err;
754 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
755 mark, &cached_state)) {
756 convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
757 mark, &cached_state, GFP_NOFS);
758 cached_state = NULL;
759 err = filemap_fdatawrite_range(mapping, start, end);
760 if (err)
761 werr = err;
762 cond_resched();
763 start = end + 1;
764 }
765 if (err)
766 werr = err;
767 blk_finish_plug(&plug);
703 return werr;
704}
705
706/*
707 * when btree blocks are allocated, they have some corresponding bits set for
708 * them in one of two extent_io trees. This is used to make sure all of
709 * those extents are on disk for transaction or log commit. We wait
710 * on all the pages and clear them from the dirty pages state tree

--- 234 unchanged lines hidden (view full) ---

945 break;
946 }
947 }
948 spin_unlock(&fs_info->fs_roots_radix_lock);
949 return err;
950}
951
952/*
768 return werr;
769}
770
771/*
772 * when btree blocks are allocated, they have some corresponding bits set for
773 * them in one of two extent_io trees. This is used to make sure all of
774 * those extents are on disk for transaction or log commit. We wait
775 * on all the pages and clear them from the dirty pages state tree

--- 234 unchanged lines hidden (view full) ---

1010 break;
1011 }
1012 }
1013 spin_unlock(&fs_info->fs_roots_radix_lock);
1014 return err;
1015}
1016
1017/*
953 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
954 * otherwise every leaf in the btree is read and defragged.
1018 * defrag a given btree.
1019 * Every leaf in the btree is read and defragged.
955 */
1020 */
956int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
1021int btrfs_defrag_root(struct btrfs_root *root)
957{
958 struct btrfs_fs_info *info = root->fs_info;
959 struct btrfs_trans_handle *trans;
960 int ret;
961
962 if (xchg(&root->defrag_running, 1))
963 return 0;
964
965 while (1) {
966 trans = btrfs_start_transaction(root, 0);
967 if (IS_ERR(trans))
968 return PTR_ERR(trans);
969
1022{
1023 struct btrfs_fs_info *info = root->fs_info;
1024 struct btrfs_trans_handle *trans;
1025 int ret;
1026
1027 if (xchg(&root->defrag_running, 1))
1028 return 0;
1029
1030 while (1) {
1031 trans = btrfs_start_transaction(root, 0);
1032 if (IS_ERR(trans))
1033 return PTR_ERR(trans);
1034
970 ret = btrfs_defrag_leaves(trans, root, cacheonly);
1035 ret = btrfs_defrag_leaves(trans, root);
971
972 btrfs_end_transaction(trans, root);
973 btrfs_btree_balance_dirty(info->tree_root);
974 cond_resched();
975
976 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
977 break;
1036
1037 btrfs_end_transaction(trans, root);
1038 btrfs_btree_balance_dirty(info->tree_root);
1039 cond_resched();
1040
1041 if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
1042 break;
1043
1044 if (btrfs_defrag_cancelled(root->fs_info)) {
1045 printk(KERN_DEBUG "btrfs: defrag_root cancelled\n");
1046 ret = -EAGAIN;
1047 break;
1048 }
978 }
979 root->defrag_running = 0;
980 return ret;
981}
982
983/*
984 * new snapshots need to be created at a very specific time in the
985 * transaction commit. This does the actual creation

--- 61 unchanged lines hidden (view full) ---

1047 }
1048
1049 key.objectid = objectid;
1050 key.offset = (u64)-1;
1051 key.type = BTRFS_ROOT_ITEM_KEY;
1052
1053 rsv = trans->block_rsv;
1054 trans->block_rsv = &pending->block_rsv;
1049 }
1050 root->defrag_running = 0;
1051 return ret;
1052}
1053
1054/*
1055 * new snapshots need to be created at a very specific time in the
1056 * transaction commit. This does the actual creation

--- 61 unchanged lines hidden (view full) ---

1118 }
1119
1120 key.objectid = objectid;
1121 key.offset = (u64)-1;
1122 key.type = BTRFS_ROOT_ITEM_KEY;
1123
1124 rsv = trans->block_rsv;
1125 trans->block_rsv = &pending->block_rsv;
1126 trans->bytes_reserved = trans->block_rsv->reserved;
1055
1056 dentry = pending->dentry;
1057 parent = dget_parent(dentry);
1058 parent_inode = parent->d_inode;
1059 parent_root = BTRFS_I(parent_inode)->root;
1060 record_root_in_trans(trans, parent_root);
1061
1062 /*

--- 137 unchanged lines hidden (view full) ---

1200 dentry->d_name.len * 2);
1201 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1202 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1203 if (ret)
1204 btrfs_abort_transaction(trans, root, ret);
1205fail:
1206 dput(parent);
1207 trans->block_rsv = rsv;
1127
1128 dentry = pending->dentry;
1129 parent = dget_parent(dentry);
1130 parent_inode = parent->d_inode;
1131 parent_root = BTRFS_I(parent_inode)->root;
1132 record_root_in_trans(trans, parent_root);
1133
1134 /*

--- 137 unchanged lines hidden (view full) ---

1272 dentry->d_name.len * 2);
1273 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1274 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1275 if (ret)
1276 btrfs_abort_transaction(trans, root, ret);
1277fail:
1278 dput(parent);
1279 trans->block_rsv = rsv;
1280 trans->bytes_reserved = 0;
1208no_free_objectid:
1209 kfree(new_root_item);
1210root_item_alloc_fail:
1211 btrfs_free_path(path);
1212path_alloc_fail:
1213 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1214 return ret;
1215}

--- 75 unchanged lines hidden (view full) ---

1291
1292/*
1293 * commit transactions asynchronously. once btrfs_commit_transaction_async
1294 * returns, any subsequent transaction will not be allowed to join.
1295 */
1296struct btrfs_async_commit {
1297 struct btrfs_trans_handle *newtrans;
1298 struct btrfs_root *root;
1281no_free_objectid:
1282 kfree(new_root_item);
1283root_item_alloc_fail:
1284 btrfs_free_path(path);
1285path_alloc_fail:
1286 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1287 return ret;
1288}

--- 75 unchanged lines hidden (view full) ---

1364
1365/*
1366 * commit transactions asynchronously. once btrfs_commit_transaction_async
1367 * returns, any subsequent transaction will not be allowed to join.
1368 */
1369struct btrfs_async_commit {
1370 struct btrfs_trans_handle *newtrans;
1371 struct btrfs_root *root;
1299 struct delayed_work work;
1372 struct work_struct work;
1300};
1301
1302static void do_async_commit(struct work_struct *work)
1303{
1304 struct btrfs_async_commit *ac =
1373};
1374
1375static void do_async_commit(struct work_struct *work)
1376{
1377 struct btrfs_async_commit *ac =
1305 container_of(work, struct btrfs_async_commit, work.work);
1378 container_of(work, struct btrfs_async_commit, work);
1306
1307 /*
1308 * We've got freeze protection passed with the transaction.
1309 * Tell lockdep about it.
1310 */
1311 if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
1312 rwsem_acquire_read(
1313 &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],

--- 11 unchanged lines hidden (view full) ---

1325{
1326 struct btrfs_async_commit *ac;
1327 struct btrfs_transaction *cur_trans;
1328
1329 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1330 if (!ac)
1331 return -ENOMEM;
1332
1379
1380 /*
1381 * We've got freeze protection passed with the transaction.
1382 * Tell lockdep about it.
1383 */
1384 if (ac->newtrans->type < TRANS_JOIN_NOLOCK)
1385 rwsem_acquire_read(
1386 &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],

--- 11 unchanged lines hidden (view full) ---

1398{
1399 struct btrfs_async_commit *ac;
1400 struct btrfs_transaction *cur_trans;
1401
1402 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1403 if (!ac)
1404 return -ENOMEM;
1405
1333 INIT_DELAYED_WORK(&ac->work, do_async_commit);
1406 INIT_WORK(&ac->work, do_async_commit);
1334 ac->root = root;
1335 ac->newtrans = btrfs_join_transaction(root);
1336 if (IS_ERR(ac->newtrans)) {
1337 int err = PTR_ERR(ac->newtrans);
1338 kfree(ac);
1339 return err;
1340 }
1341

--- 7 unchanged lines hidden (view full) ---

1349 * Tell lockdep we've released the freeze rwsem, since the
1350 * async commit thread will be the one to unlock it.
1351 */
1352 if (trans->type < TRANS_JOIN_NOLOCK)
1353 rwsem_release(
1354 &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1355 1, _THIS_IP_);
1356
1407 ac->root = root;
1408 ac->newtrans = btrfs_join_transaction(root);
1409 if (IS_ERR(ac->newtrans)) {
1410 int err = PTR_ERR(ac->newtrans);
1411 kfree(ac);
1412 return err;
1413 }
1414

--- 7 unchanged lines hidden (view full) ---

1422 * Tell lockdep we've released the freeze rwsem, since the
1423 * async commit thread will be the one to unlock it.
1424 */
1425 if (trans->type < TRANS_JOIN_NOLOCK)
1426 rwsem_release(
1427 &root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
1428 1, _THIS_IP_);
1429
1357 schedule_delayed_work(&ac->work, 0);
1430 schedule_work(&ac->work);
1358
1359 /* wait for transaction to start and unblock */
1360 if (wait_for_unblock)
1361 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1362 else
1363 wait_current_trans_commit_start(root, cur_trans);
1364
1365 if (current->journal_info == trans)

--- 46 unchanged lines hidden (view full) ---

1412 if (!flush_on_commit) {
1413 spin_lock(&root->fs_info->trans_lock);
1414 if (!list_empty(&trans->transaction->pending_snapshots))
1415 snap_pending = 1;
1416 spin_unlock(&root->fs_info->trans_lock);
1417 }
1418
1419 if (flush_on_commit || snap_pending) {
1431
1432 /* wait for transaction to start and unblock */
1433 if (wait_for_unblock)
1434 wait_current_trans_commit_start_and_unblock(root, cur_trans);
1435 else
1436 wait_current_trans_commit_start(root, cur_trans);
1437
1438 if (current->journal_info == trans)

--- 46 unchanged lines hidden (view full) ---

1485 if (!flush_on_commit) {
1486 spin_lock(&root->fs_info->trans_lock);
1487 if (!list_empty(&trans->transaction->pending_snapshots))
1488 snap_pending = 1;
1489 spin_unlock(&root->fs_info->trans_lock);
1490 }
1491
1492 if (flush_on_commit || snap_pending) {
1420 btrfs_start_delalloc_inodes(root, 1);
1493 ret = btrfs_start_delalloc_inodes(root, 1);
1494 if (ret)
1495 return ret;
1421 btrfs_wait_ordered_extents(root, 1);
1422 }
1423
1424 ret = btrfs_run_delayed_items(trans, root);
1425 if (ret)
1426 return ret;
1427
1428 /*

--- 5 unchanged lines hidden (view full) ---

1434
1435 /*
1436 * rename don't use btrfs_join_transaction, so, once we
1437 * set the transaction to blocked above, we aren't going
1438 * to get any new ordered operations. We can safely run
1439 * it here and no for sure that nothing new will be added
1440 * to the list
1441 */
1496 btrfs_wait_ordered_extents(root, 1);
1497 }
1498
1499 ret = btrfs_run_delayed_items(trans, root);
1500 if (ret)
1501 return ret;
1502
1503 /*

--- 5 unchanged lines hidden (view full) ---

1509
1510 /*
1511 * rename don't use btrfs_join_transaction, so, once we
1512 * set the transaction to blocked above, we aren't going
1513 * to get any new ordered operations. We can safely run
1514 * it here and no for sure that nothing new will be added
1515 * to the list
1516 */
1442 btrfs_run_ordered_operations(root, 1);
1517 ret = btrfs_run_ordered_operations(trans, root, 1);
1443
1518
1444 return 0;
1519 return ret;
1445}
1446
1447/*
1448 * btrfs_transaction state sequence:
1449 * in_commit = 0, blocked = 0 (initial)
1450 * in_commit = 1, blocked = 1
1451 * blocked = 0
1452 * commit_done = 1

--- 4 unchanged lines hidden (view full) ---

1457 unsigned long joined = 0;
1458 struct btrfs_transaction *cur_trans = trans->transaction;
1459 struct btrfs_transaction *prev_trans = NULL;
1460 DEFINE_WAIT(wait);
1461 int ret;
1462 int should_grow = 0;
1463 unsigned long now = get_seconds();
1464
1520}
1521
1522/*
1523 * btrfs_transaction state sequence:
1524 * in_commit = 0, blocked = 0 (initial)
1525 * in_commit = 1, blocked = 1
1526 * blocked = 0
1527 * commit_done = 1

--- 4 unchanged lines hidden (view full) ---

1532 unsigned long joined = 0;
1533 struct btrfs_transaction *cur_trans = trans->transaction;
1534 struct btrfs_transaction *prev_trans = NULL;
1535 DEFINE_WAIT(wait);
1536 int ret;
1537 int should_grow = 0;
1538 unsigned long now = get_seconds();
1539
1465 ret = btrfs_run_ordered_operations(root, 0);
1540 ret = btrfs_run_ordered_operations(trans, root, 0);
1466 if (ret) {
1467 btrfs_abort_transaction(trans, root, ret);
1541 if (ret) {
1542 btrfs_abort_transaction(trans, root, ret);
1468 goto cleanup_transaction;
1543 btrfs_end_transaction(trans, root);
1544 return ret;
1469 }
1470
1545 }
1546
1471 if (cur_trans->aborted) {
1547 /* Stop the commit early if ->aborted is set */
1548 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1472 ret = cur_trans->aborted;
1549 ret = cur_trans->aborted;
1473 goto cleanup_transaction;
1550 btrfs_end_transaction(trans, root);
1551 return ret;
1474 }
1475
1476 /* make a pass through all the delayed refs we have so far
1477 * any runnings procs may add more while we are here
1478 */
1479 ret = btrfs_run_delayed_refs(trans, root, 0);
1552 }
1553
1554 /* make a pass through all the delayed refs we have so far
1555 * any runnings procs may add more while we are here
1556 */
1557 ret = btrfs_run_delayed_refs(trans, root, 0);
1480 if (ret)
1481 goto cleanup_transaction;
1558 if (ret) {
1559 btrfs_end_transaction(trans, root);
1560 return ret;
1561 }
1482
1483 btrfs_trans_release_metadata(trans, root);
1484 trans->block_rsv = NULL;
1562
1563 btrfs_trans_release_metadata(trans, root);
1564 trans->block_rsv = NULL;
1565 if (trans->qgroup_reserved) {
1566 btrfs_qgroup_free(root, trans->qgroup_reserved);
1567 trans->qgroup_reserved = 0;
1568 }
1485
1486 cur_trans = trans->transaction;
1487
1488 /*
1489 * set the flushing flag so procs in this transaction have to
1490 * start sending their work down.
1491 */
1492 cur_trans->delayed_refs.flushing = 1;
1493
1494 if (!list_empty(&trans->new_bgs))
1495 btrfs_create_pending_block_groups(trans, root);
1496
1497 ret = btrfs_run_delayed_refs(trans, root, 0);
1569
1570 cur_trans = trans->transaction;
1571
1572 /*
1573 * set the flushing flag so procs in this transaction have to
1574 * start sending their work down.
1575 */
1576 cur_trans->delayed_refs.flushing = 1;
1577
1578 if (!list_empty(&trans->new_bgs))
1579 btrfs_create_pending_block_groups(trans, root);
1580
1581 ret = btrfs_run_delayed_refs(trans, root, 0);
1498 if (ret)
1499 goto cleanup_transaction;
1582 if (ret) {
1583 btrfs_end_transaction(trans, root);
1584 return ret;
1585 }
1500
1501 spin_lock(&cur_trans->commit_lock);
1502 if (cur_trans->in_commit) {
1503 spin_unlock(&cur_trans->commit_lock);
1504 atomic_inc(&cur_trans->use_count);
1505 ret = btrfs_end_transaction(trans, root);
1506
1507 wait_for_commit(root, cur_trans);

--- 61 unchanged lines hidden (view full) ---

1569 * no_join so make sure to wait for num_writers to == 1 again.
1570 */
1571 spin_lock(&root->fs_info->trans_lock);
1572 root->fs_info->trans_no_join = 1;
1573 spin_unlock(&root->fs_info->trans_lock);
1574 wait_event(cur_trans->writer_wait,
1575 atomic_read(&cur_trans->num_writers) == 1);
1576
1586
1587 spin_lock(&cur_trans->commit_lock);
1588 if (cur_trans->in_commit) {
1589 spin_unlock(&cur_trans->commit_lock);
1590 atomic_inc(&cur_trans->use_count);
1591 ret = btrfs_end_transaction(trans, root);
1592
1593 wait_for_commit(root, cur_trans);

--- 61 unchanged lines hidden (view full) ---

1655 * no_join so make sure to wait for num_writers to == 1 again.
1656 */
1657 spin_lock(&root->fs_info->trans_lock);
1658 root->fs_info->trans_no_join = 1;
1659 spin_unlock(&root->fs_info->trans_lock);
1660 wait_event(cur_trans->writer_wait,
1661 atomic_read(&cur_trans->num_writers) == 1);
1662
1663 /* ->aborted might be set after the previous check, so check it */
1664 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1665 ret = cur_trans->aborted;
1666 goto cleanup_transaction;
1667 }
1577 /*
1578 * the reloc mutex makes sure that we stop
1579 * the balancing code from coming in and moving
1580 * extents around in the middle of the commit
1581 */
1582 mutex_lock(&root->fs_info->reloc_mutex);
1583
1584 /*

--- 67 unchanged lines hidden (view full) ---

1652
1653 ret = commit_cowonly_roots(trans, root);
1654 if (ret) {
1655 mutex_unlock(&root->fs_info->tree_log_mutex);
1656 mutex_unlock(&root->fs_info->reloc_mutex);
1657 goto cleanup_transaction;
1658 }
1659
1668 /*
1669 * the reloc mutex makes sure that we stop
1670 * the balancing code from coming in and moving
1671 * extents around in the middle of the commit
1672 */
1673 mutex_lock(&root->fs_info->reloc_mutex);
1674
1675 /*

--- 67 unchanged lines hidden (view full) ---

1743
1744 ret = commit_cowonly_roots(trans, root);
1745 if (ret) {
1746 mutex_unlock(&root->fs_info->tree_log_mutex);
1747 mutex_unlock(&root->fs_info->reloc_mutex);
1748 goto cleanup_transaction;
1749 }
1750
1751 /*
1752 * The tasks which save the space cache and inode cache may also
1753 * update ->aborted, check it.
1754 */
1755 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1756 ret = cur_trans->aborted;
1757 mutex_unlock(&root->fs_info->tree_log_mutex);
1758 mutex_unlock(&root->fs_info->reloc_mutex);
1759 goto cleanup_transaction;
1760 }
1761
1660 btrfs_prepare_extent_commit(trans, root);
1661
1662 cur_trans = root->fs_info->running_transaction;
1663
1664 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1665 root->fs_info->tree_root->node);
1666 switch_commit_root(root->fs_info->tree_root);
1667

--- 71 unchanged lines hidden (view full) ---

1739 if (current != root->fs_info->transaction_kthread)
1740 btrfs_run_delayed_iputs(root);
1741
1742 return ret;
1743
1744cleanup_transaction:
1745 btrfs_trans_release_metadata(trans, root);
1746 trans->block_rsv = NULL;
1762 btrfs_prepare_extent_commit(trans, root);
1763
1764 cur_trans = root->fs_info->running_transaction;
1765
1766 btrfs_set_root_node(&root->fs_info->tree_root->root_item,
1767 root->fs_info->tree_root->node);
1768 switch_commit_root(root->fs_info->tree_root);
1769

--- 71 unchanged lines hidden (view full) ---

1841 if (current != root->fs_info->transaction_kthread)
1842 btrfs_run_delayed_iputs(root);
1843
1844 return ret;
1845
1846cleanup_transaction:
1847 btrfs_trans_release_metadata(trans, root);
1848 trans->block_rsv = NULL;
1849 if (trans->qgroup_reserved) {
1850 btrfs_qgroup_free(root, trans->qgroup_reserved);
1851 trans->qgroup_reserved = 0;
1852 }
1747 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1748// WARN_ON(1);
1749 if (current->journal_info == trans)
1750 current->journal_info = NULL;
1751 cleanup_transaction(trans, root, ret);
1752
1753 return ret;
1754}

--- 30 unchanged lines hidden ---
1853 btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
1854// WARN_ON(1);
1855 if (current->journal_info == trans)
1856 current->journal_info = NULL;
1857 cleanup_transaction(trans, root, ret);
1858
1859 return ret;
1860}

--- 30 unchanged lines hidden ---