2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/writeback.h>
23 #include <linux/pagemap.h>
24 #include <linux/blkdev.h>
27 #include "transaction.h"
31 #define BTRFS_ROOT_TRANS_TAG 0
33 static noinline
void put_transaction(struct btrfs_transaction
*transaction
)
35 WARN_ON(atomic_read(&transaction
->use_count
) == 0);
36 if (atomic_dec_and_test(&transaction
->use_count
)) {
37 memset(transaction
, 0, sizeof(*transaction
));
38 kmem_cache_free(btrfs_transaction_cachep
, transaction
);
42 static noinline
void switch_commit_root(struct btrfs_root
*root
)
44 free_extent_buffer(root
->commit_root
);
45 root
->commit_root
= btrfs_root_node(root
);
49 * either allocate a new transaction or hop into the existing one
51 static noinline
int join_transaction(struct btrfs_root
*root
)
53 struct btrfs_transaction
*cur_trans
;
54 cur_trans
= root
->fs_info
->running_transaction
;
56 cur_trans
= kmem_cache_alloc(btrfs_transaction_cachep
,
60 root
->fs_info
->generation
++;
61 atomic_set(&cur_trans
->num_writers
, 1);
62 cur_trans
->num_joined
= 0;
63 cur_trans
->transid
= root
->fs_info
->generation
;
64 init_waitqueue_head(&cur_trans
->writer_wait
);
65 init_waitqueue_head(&cur_trans
->commit_wait
);
66 cur_trans
->in_commit
= 0;
67 cur_trans
->blocked
= 0;
68 atomic_set(&cur_trans
->use_count
, 1);
69 cur_trans
->commit_done
= 0;
70 cur_trans
->start_time
= get_seconds();
72 cur_trans
->delayed_refs
.root
= RB_ROOT
;
73 cur_trans
->delayed_refs
.num_entries
= 0;
74 cur_trans
->delayed_refs
.num_heads_ready
= 0;
75 cur_trans
->delayed_refs
.num_heads
= 0;
76 cur_trans
->delayed_refs
.flushing
= 0;
77 cur_trans
->delayed_refs
.run_delayed_start
= 0;
78 spin_lock_init(&cur_trans
->delayed_refs
.lock
);
80 INIT_LIST_HEAD(&cur_trans
->pending_snapshots
);
81 list_add_tail(&cur_trans
->list
, &root
->fs_info
->trans_list
);
82 extent_io_tree_init(&cur_trans
->dirty_pages
,
83 root
->fs_info
->btree_inode
->i_mapping
,
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
);
89 atomic_inc(&cur_trans
->num_writers
);
90 cur_trans
->num_joined
++;
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
102 static noinline
int record_root_in_trans(struct btrfs_trans_handle
*trans
,
103 struct btrfs_root
*root
)
105 if (root
->ref_cows
&& root
->last_trans
< trans
->transid
) {
106 WARN_ON(root
== root
->fs_info
->extent_root
);
107 WARN_ON(root
->commit_root
!= root
->node
);
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
);
118 int btrfs_record_root_in_trans(struct btrfs_trans_handle
*trans
,
119 struct btrfs_root
*root
)
124 mutex_lock(&root
->fs_info
->trans_mutex
);
125 if (root
->last_trans
== trans
->transid
) {
126 mutex_unlock(&root
->fs_info
->trans_mutex
);
130 record_root_in_trans(trans
, root
);
131 mutex_unlock(&root
->fs_info
->trans_mutex
);
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.
139 static void wait_current_trans(struct btrfs_root
*root
)
141 struct btrfs_transaction
*cur_trans
;
143 cur_trans
= root
->fs_info
->running_transaction
;
144 if (cur_trans
&& cur_trans
->blocked
) {
146 atomic_inc(&cur_trans
->use_count
);
148 prepare_to_wait(&root
->fs_info
->transaction_wait
, &wait
,
149 TASK_UNINTERRUPTIBLE
);
150 if (!cur_trans
->blocked
)
152 mutex_unlock(&root
->fs_info
->trans_mutex
);
154 mutex_lock(&root
->fs_info
->trans_mutex
);
156 finish_wait(&root
->fs_info
->transaction_wait
, &wait
);
157 put_transaction(cur_trans
);
161 enum btrfs_trans_type
{
168 static int may_wait_transaction(struct btrfs_root
*root
, int type
)
170 if (!root
->fs_info
->log_root_recovering
&&
171 ((type
== TRANS_START
&& !root
->fs_info
->open_ioctl_trans
) ||
172 type
== TRANS_USERSPACE
))
177 static struct btrfs_trans_handle
*start_transaction(struct btrfs_root
*root
,
178 u64 num_items
, int type
)
180 struct btrfs_trans_handle
*h
;
181 struct btrfs_transaction
*cur_trans
;
185 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
)
186 return ERR_PTR(-EROFS
);
188 h
= kmem_cache_alloc(btrfs_trans_handle_cachep
, GFP_NOFS
);
190 return ERR_PTR(-ENOMEM
);
192 if (type
!= TRANS_JOIN_NOLOCK
)
193 mutex_lock(&root
->fs_info
->trans_mutex
);
194 if (may_wait_transaction(root
, type
))
195 wait_current_trans(root
);
197 ret
= join_transaction(root
);
199 kmem_cache_free(btrfs_trans_handle_cachep
, h
);
200 if (type
!= TRANS_JOIN_NOLOCK
)
201 mutex_unlock(&root
->fs_info
->trans_mutex
);
205 cur_trans
= root
->fs_info
->running_transaction
;
206 atomic_inc(&cur_trans
->use_count
);
207 if (type
!= TRANS_JOIN_NOLOCK
)
208 mutex_unlock(&root
->fs_info
->trans_mutex
);
210 h
->transid
= cur_trans
->transid
;
211 h
->transaction
= cur_trans
;
214 h
->bytes_reserved
= 0;
215 h
->delayed_ref_updates
= 0;
219 if (cur_trans
->blocked
&& may_wait_transaction(root
, type
)) {
220 btrfs_commit_transaction(h
, root
);
225 ret
= btrfs_trans_reserve_metadata(h
, root
, num_items
);
226 if (ret
== -EAGAIN
&& !retries
) {
228 btrfs_commit_transaction(h
, root
);
230 } else if (ret
== -EAGAIN
) {
232 * We have already retried and got EAGAIN, so really we
233 * don't have space, so set ret to -ENOSPC.
239 btrfs_end_transaction(h
, root
);
244 if (type
!= TRANS_JOIN_NOLOCK
)
245 mutex_lock(&root
->fs_info
->trans_mutex
);
246 record_root_in_trans(h
, root
);
247 if (type
!= TRANS_JOIN_NOLOCK
)
248 mutex_unlock(&root
->fs_info
->trans_mutex
);
250 if (!current
->journal_info
&& type
!= TRANS_USERSPACE
)
251 current
->journal_info
= h
;
255 struct btrfs_trans_handle
*btrfs_start_transaction(struct btrfs_root
*root
,
258 return start_transaction(root
, num_items
, TRANS_START
);
260 struct btrfs_trans_handle
*btrfs_join_transaction(struct btrfs_root
*root
,
263 return start_transaction(root
, 0, TRANS_JOIN
);
266 struct btrfs_trans_handle
*btrfs_join_transaction_nolock(struct btrfs_root
*root
,
269 return start_transaction(root
, 0, TRANS_JOIN_NOLOCK
);
272 struct btrfs_trans_handle
*btrfs_start_ioctl_transaction(struct btrfs_root
*r
,
275 return start_transaction(r
, 0, TRANS_USERSPACE
);
278 /* wait for a transaction commit to be fully complete */
279 static noinline
int wait_for_commit(struct btrfs_root
*root
,
280 struct btrfs_transaction
*commit
)
283 mutex_lock(&root
->fs_info
->trans_mutex
);
284 while (!commit
->commit_done
) {
285 prepare_to_wait(&commit
->commit_wait
, &wait
,
286 TASK_UNINTERRUPTIBLE
);
287 if (commit
->commit_done
)
289 mutex_unlock(&root
->fs_info
->trans_mutex
);
291 mutex_lock(&root
->fs_info
->trans_mutex
);
293 mutex_unlock(&root
->fs_info
->trans_mutex
);
294 finish_wait(&commit
->commit_wait
, &wait
);
298 int btrfs_wait_for_commit(struct btrfs_root
*root
, u64 transid
)
300 struct btrfs_transaction
*cur_trans
= NULL
, *t
;
303 mutex_lock(&root
->fs_info
->trans_mutex
);
307 if (transid
<= root
->fs_info
->last_trans_committed
)
310 /* find specified transaction */
311 list_for_each_entry(t
, &root
->fs_info
->trans_list
, list
) {
312 if (t
->transid
== transid
) {
316 if (t
->transid
> transid
)
321 goto out_unlock
; /* bad transid */
323 /* find newest transaction that is committing | committed */
324 list_for_each_entry_reverse(t
, &root
->fs_info
->trans_list
,
334 goto out_unlock
; /* nothing committing|committed */
337 atomic_inc(&cur_trans
->use_count
);
338 mutex_unlock(&root
->fs_info
->trans_mutex
);
340 wait_for_commit(root
, cur_trans
);
342 mutex_lock(&root
->fs_info
->trans_mutex
);
343 put_transaction(cur_trans
);
346 mutex_unlock(&root
->fs_info
->trans_mutex
);
352 * rate limit against the drop_snapshot code. This helps to slow down new
353 * operations if the drop_snapshot code isn't able to keep up.
355 static void throttle_on_drops(struct btrfs_root
*root
)
357 struct btrfs_fs_info
*info
= root
->fs_info
;
358 int harder_count
= 0;
361 if (atomic_read(&info
->throttles
)) {
364 thr
= atomic_read(&info
->throttle_gen
);
367 prepare_to_wait(&info
->transaction_throttle
,
368 &wait
, TASK_UNINTERRUPTIBLE
);
369 if (!atomic_read(&info
->throttles
)) {
370 finish_wait(&info
->transaction_throttle
, &wait
);
374 finish_wait(&info
->transaction_throttle
, &wait
);
375 } while (thr
== atomic_read(&info
->throttle_gen
));
378 if (root
->fs_info
->total_ref_cache_size
> 1 * 1024 * 1024 &&
382 if (root
->fs_info
->total_ref_cache_size
> 5 * 1024 * 1024 &&
386 if (root
->fs_info
->total_ref_cache_size
> 10 * 1024 * 1024 &&
393 void btrfs_throttle(struct btrfs_root
*root
)
395 mutex_lock(&root
->fs_info
->trans_mutex
);
396 if (!root
->fs_info
->open_ioctl_trans
)
397 wait_current_trans(root
);
398 mutex_unlock(&root
->fs_info
->trans_mutex
);
401 static int should_end_transaction(struct btrfs_trans_handle
*trans
,
402 struct btrfs_root
*root
)
405 ret
= btrfs_block_rsv_check(trans
, root
,
406 &root
->fs_info
->global_block_rsv
, 0, 5);
410 int btrfs_should_end_transaction(struct btrfs_trans_handle
*trans
,
411 struct btrfs_root
*root
)
413 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
416 if (cur_trans
->blocked
|| cur_trans
->delayed_refs
.flushing
)
419 updates
= trans
->delayed_ref_updates
;
420 trans
->delayed_ref_updates
= 0;
422 btrfs_run_delayed_refs(trans
, root
, updates
);
424 return should_end_transaction(trans
, root
);
427 static int __btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
428 struct btrfs_root
*root
, int throttle
, int lock
)
430 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
431 struct btrfs_fs_info
*info
= root
->fs_info
;
435 unsigned long cur
= trans
->delayed_ref_updates
;
436 trans
->delayed_ref_updates
= 0;
438 trans
->transaction
->delayed_refs
.num_heads_ready
> 64) {
439 trans
->delayed_ref_updates
= 0;
442 * do a full flush if the transaction is trying
445 if (trans
->transaction
->delayed_refs
.flushing
)
447 btrfs_run_delayed_refs(trans
, root
, cur
);
454 btrfs_trans_release_metadata(trans
, root
);
456 if (lock
&& !root
->fs_info
->open_ioctl_trans
&&
457 should_end_transaction(trans
, root
))
458 trans
->transaction
->blocked
= 1;
460 if (lock
&& cur_trans
->blocked
&& !cur_trans
->in_commit
) {
462 return btrfs_commit_transaction(trans
, root
);
464 wake_up_process(info
->transaction_kthread
);
467 WARN_ON(cur_trans
!= info
->running_transaction
);
468 WARN_ON(atomic_read(&cur_trans
->num_writers
) < 1);
469 atomic_dec(&cur_trans
->num_writers
);
472 if (waitqueue_active(&cur_trans
->writer_wait
))
473 wake_up(&cur_trans
->writer_wait
);
474 put_transaction(cur_trans
);
476 if (current
->journal_info
== trans
)
477 current
->journal_info
= NULL
;
478 memset(trans
, 0, sizeof(*trans
));
479 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
482 btrfs_run_delayed_iputs(root
);
487 int btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
488 struct btrfs_root
*root
)
490 return __btrfs_end_transaction(trans
, root
, 0, 1);
493 int btrfs_end_transaction_throttle(struct btrfs_trans_handle
*trans
,
494 struct btrfs_root
*root
)
496 return __btrfs_end_transaction(trans
, root
, 1, 1);
499 int btrfs_end_transaction_nolock(struct btrfs_trans_handle
*trans
,
500 struct btrfs_root
*root
)
502 return __btrfs_end_transaction(trans
, root
, 0, 0);
506 * when btree blocks are allocated, they have some corresponding bits set for
507 * them in one of two extent_io trees. This is used to make sure all of
508 * those extents are sent to disk but does not wait on them
510 int btrfs_write_marked_extents(struct btrfs_root
*root
,
511 struct extent_io_tree
*dirty_pages
, int mark
)
517 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
523 ret
= find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
527 while (start
<= end
) {
530 index
= start
>> PAGE_CACHE_SHIFT
;
531 start
= (u64
)(index
+ 1) << PAGE_CACHE_SHIFT
;
532 page
= find_get_page(btree_inode
->i_mapping
, index
);
536 btree_lock_page_hook(page
);
537 if (!page
->mapping
) {
539 page_cache_release(page
);
543 if (PageWriteback(page
)) {
545 wait_on_page_writeback(page
);
548 page_cache_release(page
);
552 err
= write_one_page(page
, 0);
555 page_cache_release(page
);
564 * when btree blocks are allocated, they have some corresponding bits set for
565 * them in one of two extent_io trees. This is used to make sure all of
566 * those extents are on disk for transaction or log commit. We wait
567 * on all the pages and clear them from the dirty pages state tree
569 int btrfs_wait_marked_extents(struct btrfs_root
*root
,
570 struct extent_io_tree
*dirty_pages
, int mark
)
576 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
582 ret
= find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
587 clear_extent_bits(dirty_pages
, start
, end
, mark
, GFP_NOFS
);
588 while (start
<= end
) {
589 index
= start
>> PAGE_CACHE_SHIFT
;
590 start
= (u64
)(index
+ 1) << PAGE_CACHE_SHIFT
;
591 page
= find_get_page(btree_inode
->i_mapping
, index
);
594 if (PageDirty(page
)) {
595 btree_lock_page_hook(page
);
596 wait_on_page_writeback(page
);
597 err
= write_one_page(page
, 0);
601 wait_on_page_writeback(page
);
602 page_cache_release(page
);
612 * when btree blocks are allocated, they have some corresponding bits set for
613 * them in one of two extent_io trees. This is used to make sure all of
614 * those extents are on disk for transaction or log commit
616 int btrfs_write_and_wait_marked_extents(struct btrfs_root
*root
,
617 struct extent_io_tree
*dirty_pages
, int mark
)
622 ret
= btrfs_write_marked_extents(root
, dirty_pages
, mark
);
623 ret2
= btrfs_wait_marked_extents(root
, dirty_pages
, mark
);
627 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle
*trans
,
628 struct btrfs_root
*root
)
630 if (!trans
|| !trans
->transaction
) {
631 struct inode
*btree_inode
;
632 btree_inode
= root
->fs_info
->btree_inode
;
633 return filemap_write_and_wait(btree_inode
->i_mapping
);
635 return btrfs_write_and_wait_marked_extents(root
,
636 &trans
->transaction
->dirty_pages
,
641 * this is used to update the root pointer in the tree of tree roots.
643 * But, in the case of the extent allocation tree, updating the root
644 * pointer may allocate blocks which may change the root of the extent
647 * So, this loops and repeats and makes sure the cowonly root didn't
648 * change while the root pointer was being updated in the metadata.
650 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
651 struct btrfs_root
*root
)
656 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
658 old_root_used
= btrfs_root_used(&root
->root_item
);
659 btrfs_write_dirty_block_groups(trans
, root
);
662 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
663 if (old_root_bytenr
== root
->node
->start
&&
664 old_root_used
== btrfs_root_used(&root
->root_item
))
667 btrfs_set_root_node(&root
->root_item
, root
->node
);
668 ret
= btrfs_update_root(trans
, tree_root
,
673 old_root_used
= btrfs_root_used(&root
->root_item
);
674 ret
= btrfs_write_dirty_block_groups(trans
, root
);
678 if (root
!= root
->fs_info
->extent_root
)
679 switch_commit_root(root
);
685 * update all the cowonly tree roots on disk
687 static noinline
int commit_cowonly_roots(struct btrfs_trans_handle
*trans
,
688 struct btrfs_root
*root
)
690 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
691 struct list_head
*next
;
692 struct extent_buffer
*eb
;
695 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
698 eb
= btrfs_lock_root_node(fs_info
->tree_root
);
699 btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
700 btrfs_tree_unlock(eb
);
701 free_extent_buffer(eb
);
703 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
706 while (!list_empty(&fs_info
->dirty_cowonly_roots
)) {
707 next
= fs_info
->dirty_cowonly_roots
.next
;
709 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
711 update_cowonly_root(trans
, root
);
714 down_write(&fs_info
->extent_commit_sem
);
715 switch_commit_root(fs_info
->extent_root
);
716 up_write(&fs_info
->extent_commit_sem
);
722 * dead roots are old snapshots that need to be deleted. This allocates
723 * a dirty root struct and adds it into the list of dead roots that need to
726 int btrfs_add_dead_root(struct btrfs_root
*root
)
728 mutex_lock(&root
->fs_info
->trans_mutex
);
729 list_add(&root
->root_list
, &root
->fs_info
->dead_roots
);
730 mutex_unlock(&root
->fs_info
->trans_mutex
);
735 * update all the cowonly tree roots on disk
737 static noinline
int commit_fs_roots(struct btrfs_trans_handle
*trans
,
738 struct btrfs_root
*root
)
740 struct btrfs_root
*gang
[8];
741 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
747 ret
= radix_tree_gang_lookup_tag(&fs_info
->fs_roots_radix
,
750 BTRFS_ROOT_TRANS_TAG
);
753 for (i
= 0; i
< ret
; i
++) {
755 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
756 (unsigned long)root
->root_key
.objectid
,
757 BTRFS_ROOT_TRANS_TAG
);
759 btrfs_free_log(trans
, root
);
760 btrfs_update_reloc_root(trans
, root
);
761 btrfs_orphan_commit_root(trans
, root
);
763 if (root
->commit_root
!= root
->node
) {
764 switch_commit_root(root
);
765 btrfs_set_root_node(&root
->root_item
,
769 err
= btrfs_update_root(trans
, fs_info
->tree_root
,
780 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
781 * otherwise every leaf in the btree is read and defragged.
783 int btrfs_defrag_root(struct btrfs_root
*root
, int cacheonly
)
785 struct btrfs_fs_info
*info
= root
->fs_info
;
786 struct btrfs_trans_handle
*trans
;
790 if (xchg(&root
->defrag_running
, 1))
794 trans
= btrfs_start_transaction(root
, 0);
796 return PTR_ERR(trans
);
798 ret
= btrfs_defrag_leaves(trans
, root
, cacheonly
);
800 nr
= trans
->blocks_used
;
801 btrfs_end_transaction(trans
, root
);
802 btrfs_btree_balance_dirty(info
->tree_root
, nr
);
805 if (root
->fs_info
->closing
|| ret
!= -EAGAIN
)
808 root
->defrag_running
= 0;
814 * when dropping snapshots, we generate a ton of delayed refs, and it makes
815 * sense not to join the transaction while it is trying to flush the current
816 * queue of delayed refs out.
818 * This is used by the drop snapshot code only
820 static noinline
int wait_transaction_pre_flush(struct btrfs_fs_info
*info
)
824 mutex_lock(&info
->trans_mutex
);
825 while (info
->running_transaction
&&
826 info
->running_transaction
->delayed_refs
.flushing
) {
827 prepare_to_wait(&info
->transaction_wait
, &wait
,
828 TASK_UNINTERRUPTIBLE
);
829 mutex_unlock(&info
->trans_mutex
);
833 mutex_lock(&info
->trans_mutex
);
834 finish_wait(&info
->transaction_wait
, &wait
);
836 mutex_unlock(&info
->trans_mutex
);
841 * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
844 int btrfs_drop_dead_root(struct btrfs_root
*root
)
846 struct btrfs_trans_handle
*trans
;
847 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
853 * we don't want to jump in and create a bunch of
854 * delayed refs if the transaction is starting to close
856 wait_transaction_pre_flush(tree_root
->fs_info
);
857 trans
= btrfs_start_transaction(tree_root
, 1);
860 * we've joined a transaction, make sure it isn't
863 if (trans
->transaction
->delayed_refs
.flushing
) {
864 btrfs_end_transaction(trans
, tree_root
);
868 ret
= btrfs_drop_snapshot(trans
, root
);
872 ret
= btrfs_update_root(trans
, tree_root
,
878 nr
= trans
->blocks_used
;
879 ret
= btrfs_end_transaction(trans
, tree_root
);
882 btrfs_btree_balance_dirty(tree_root
, nr
);
887 ret
= btrfs_del_root(trans
, tree_root
, &root
->root_key
);
890 nr
= trans
->blocks_used
;
891 ret
= btrfs_end_transaction(trans
, tree_root
);
894 free_extent_buffer(root
->node
);
895 free_extent_buffer(root
->commit_root
);
898 btrfs_btree_balance_dirty(tree_root
, nr
);
904 * new snapshots need to be created at a very specific time in the
905 * transaction commit. This does the actual creation
907 static noinline
int create_pending_snapshot(struct btrfs_trans_handle
*trans
,
908 struct btrfs_fs_info
*fs_info
,
909 struct btrfs_pending_snapshot
*pending
)
911 struct btrfs_key key
;
912 struct btrfs_root_item
*new_root_item
;
913 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
914 struct btrfs_root
*root
= pending
->root
;
915 struct btrfs_root
*parent_root
;
916 struct inode
*parent_inode
;
917 struct dentry
*parent
;
918 struct dentry
*dentry
;
919 struct extent_buffer
*tmp
;
920 struct extent_buffer
*old
;
927 new_root_item
= kmalloc(sizeof(*new_root_item
), GFP_NOFS
);
928 if (!new_root_item
) {
929 pending
->error
= -ENOMEM
;
933 ret
= btrfs_find_free_objectid(trans
, tree_root
, 0, &objectid
);
935 pending
->error
= ret
;
939 btrfs_reloc_pre_snapshot(trans
, pending
, &to_reserve
);
940 btrfs_orphan_pre_snapshot(trans
, pending
, &to_reserve
);
942 if (to_reserve
> 0) {
943 ret
= btrfs_block_rsv_add(trans
, root
, &pending
->block_rsv
,
946 pending
->error
= ret
;
951 key
.objectid
= objectid
;
952 key
.offset
= (u64
)-1;
953 key
.type
= BTRFS_ROOT_ITEM_KEY
;
955 trans
->block_rsv
= &pending
->block_rsv
;
957 dentry
= pending
->dentry
;
958 parent
= dget_parent(dentry
);
959 parent_inode
= parent
->d_inode
;
960 parent_root
= BTRFS_I(parent_inode
)->root
;
961 record_root_in_trans(trans
, parent_root
);
964 * insert the directory item
966 ret
= btrfs_set_inode_index(parent_inode
, &index
);
968 ret
= btrfs_insert_dir_item(trans
, parent_root
,
969 dentry
->d_name
.name
, dentry
->d_name
.len
,
970 parent_inode
->i_ino
, &key
,
971 BTRFS_FT_DIR
, index
);
974 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
975 dentry
->d_name
.len
* 2);
976 ret
= btrfs_update_inode(trans
, parent_root
, parent_inode
);
979 record_root_in_trans(trans
, root
);
980 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
);
981 memcpy(new_root_item
, &root
->root_item
, sizeof(*new_root_item
));
982 btrfs_check_and_init_root_item(new_root_item
);
984 root_flags
= btrfs_root_flags(new_root_item
);
985 if (pending
->readonly
)
986 root_flags
|= BTRFS_ROOT_SUBVOL_RDONLY
;
988 root_flags
&= ~BTRFS_ROOT_SUBVOL_RDONLY
;
989 btrfs_set_root_flags(new_root_item
, root_flags
);
991 old
= btrfs_lock_root_node(root
);
992 btrfs_cow_block(trans
, root
, old
, NULL
, 0, &old
);
993 btrfs_set_lock_blocking(old
);
995 btrfs_copy_root(trans
, root
, old
, &tmp
, objectid
);
996 btrfs_tree_unlock(old
);
997 free_extent_buffer(old
);
999 btrfs_set_root_node(new_root_item
, tmp
);
1000 /* record when the snapshot was created in key.offset */
1001 key
.offset
= trans
->transid
;
1002 ret
= btrfs_insert_root(trans
, tree_root
, &key
, new_root_item
);
1003 btrfs_tree_unlock(tmp
);
1004 free_extent_buffer(tmp
);
1008 * insert root back/forward references
1010 ret
= btrfs_add_root_ref(trans
, tree_root
, objectid
,
1011 parent_root
->root_key
.objectid
,
1012 parent_inode
->i_ino
, index
,
1013 dentry
->d_name
.name
, dentry
->d_name
.len
);
1017 key
.offset
= (u64
)-1;
1018 pending
->snap
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
1019 BUG_ON(IS_ERR(pending
->snap
));
1021 btrfs_reloc_post_snapshot(trans
, pending
);
1022 btrfs_orphan_post_snapshot(trans
, pending
);
1024 kfree(new_root_item
);
1025 btrfs_block_rsv_release(root
, &pending
->block_rsv
, (u64
)-1);
1030 * create all the snapshots we've scheduled for creation
1032 static noinline
int create_pending_snapshots(struct btrfs_trans_handle
*trans
,
1033 struct btrfs_fs_info
*fs_info
)
1035 struct btrfs_pending_snapshot
*pending
;
1036 struct list_head
*head
= &trans
->transaction
->pending_snapshots
;
1039 list_for_each_entry(pending
, head
, list
) {
1040 ret
= create_pending_snapshot(trans
, fs_info
, pending
);
1046 static void update_super_roots(struct btrfs_root
*root
)
1048 struct btrfs_root_item
*root_item
;
1049 struct btrfs_super_block
*super
;
1051 super
= &root
->fs_info
->super_copy
;
1053 root_item
= &root
->fs_info
->chunk_root
->root_item
;
1054 super
->chunk_root
= root_item
->bytenr
;
1055 super
->chunk_root_generation
= root_item
->generation
;
1056 super
->chunk_root_level
= root_item
->level
;
1058 root_item
= &root
->fs_info
->tree_root
->root_item
;
1059 super
->root
= root_item
->bytenr
;
1060 super
->generation
= root_item
->generation
;
1061 super
->root_level
= root_item
->level
;
1062 if (super
->cache_generation
!= 0 || btrfs_test_opt(root
, SPACE_CACHE
))
1063 super
->cache_generation
= root_item
->generation
;
1066 int btrfs_transaction_in_commit(struct btrfs_fs_info
*info
)
1069 spin_lock(&info
->new_trans_lock
);
1070 if (info
->running_transaction
)
1071 ret
= info
->running_transaction
->in_commit
;
1072 spin_unlock(&info
->new_trans_lock
);
1076 int btrfs_transaction_blocked(struct btrfs_fs_info
*info
)
1079 spin_lock(&info
->new_trans_lock
);
1080 if (info
->running_transaction
)
1081 ret
= info
->running_transaction
->blocked
;
1082 spin_unlock(&info
->new_trans_lock
);
1087 * wait for the current transaction commit to start and block subsequent
1090 static void wait_current_trans_commit_start(struct btrfs_root
*root
,
1091 struct btrfs_transaction
*trans
)
1095 if (trans
->in_commit
)
1099 prepare_to_wait(&root
->fs_info
->transaction_blocked_wait
, &wait
,
1100 TASK_UNINTERRUPTIBLE
);
1101 if (trans
->in_commit
) {
1102 finish_wait(&root
->fs_info
->transaction_blocked_wait
,
1106 mutex_unlock(&root
->fs_info
->trans_mutex
);
1108 mutex_lock(&root
->fs_info
->trans_mutex
);
1109 finish_wait(&root
->fs_info
->transaction_blocked_wait
, &wait
);
1114 * wait for the current transaction to start and then become unblocked.
1117 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root
*root
,
1118 struct btrfs_transaction
*trans
)
1122 if (trans
->commit_done
|| (trans
->in_commit
&& !trans
->blocked
))
1126 prepare_to_wait(&root
->fs_info
->transaction_wait
, &wait
,
1127 TASK_UNINTERRUPTIBLE
);
1128 if (trans
->commit_done
||
1129 (trans
->in_commit
&& !trans
->blocked
)) {
1130 finish_wait(&root
->fs_info
->transaction_wait
,
1134 mutex_unlock(&root
->fs_info
->trans_mutex
);
1136 mutex_lock(&root
->fs_info
->trans_mutex
);
1137 finish_wait(&root
->fs_info
->transaction_wait
,
1143 * commit transactions asynchronously. once btrfs_commit_transaction_async
1144 * returns, any subsequent transaction will not be allowed to join.
1146 struct btrfs_async_commit
{
1147 struct btrfs_trans_handle
*newtrans
;
1148 struct btrfs_root
*root
;
1149 struct delayed_work work
;
1152 static void do_async_commit(struct work_struct
*work
)
1154 struct btrfs_async_commit
*ac
=
1155 container_of(work
, struct btrfs_async_commit
, work
.work
);
1157 btrfs_commit_transaction(ac
->newtrans
, ac
->root
);
1161 int btrfs_commit_transaction_async(struct btrfs_trans_handle
*trans
,
1162 struct btrfs_root
*root
,
1163 int wait_for_unblock
)
1165 struct btrfs_async_commit
*ac
;
1166 struct btrfs_transaction
*cur_trans
;
1168 ac
= kmalloc(sizeof(*ac
), GFP_NOFS
);
1172 INIT_DELAYED_WORK(&ac
->work
, do_async_commit
);
1174 ac
->newtrans
= btrfs_join_transaction(root
, 0);
1175 if (IS_ERR(ac
->newtrans
)) {
1176 int err
= PTR_ERR(ac
->newtrans
);
1181 /* take transaction reference */
1182 mutex_lock(&root
->fs_info
->trans_mutex
);
1183 cur_trans
= trans
->transaction
;
1184 atomic_inc(&cur_trans
->use_count
);
1185 mutex_unlock(&root
->fs_info
->trans_mutex
);
1187 btrfs_end_transaction(trans
, root
);
1188 schedule_delayed_work(&ac
->work
, 0);
1190 /* wait for transaction to start and unblock */
1191 mutex_lock(&root
->fs_info
->trans_mutex
);
1192 if (wait_for_unblock
)
1193 wait_current_trans_commit_start_and_unblock(root
, cur_trans
);
1195 wait_current_trans_commit_start(root
, cur_trans
);
1196 put_transaction(cur_trans
);
1197 mutex_unlock(&root
->fs_info
->trans_mutex
);
1203 * btrfs_transaction state sequence:
1204 * in_commit = 0, blocked = 0 (initial)
1205 * in_commit = 1, blocked = 1
1209 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
1210 struct btrfs_root
*root
)
1212 unsigned long joined
= 0;
1213 struct btrfs_transaction
*cur_trans
;
1214 struct btrfs_transaction
*prev_trans
= NULL
;
1217 int should_grow
= 0;
1218 unsigned long now
= get_seconds();
1219 int flush_on_commit
= btrfs_test_opt(root
, FLUSHONCOMMIT
);
1221 btrfs_run_ordered_operations(root
, 0);
1223 /* make a pass through all the delayed refs we have so far
1224 * any runnings procs may add more while we are here
1226 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
1229 btrfs_trans_release_metadata(trans
, root
);
1231 cur_trans
= trans
->transaction
;
1233 * set the flushing flag so procs in this transaction have to
1234 * start sending their work down.
1236 cur_trans
->delayed_refs
.flushing
= 1;
1238 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
1241 mutex_lock(&root
->fs_info
->trans_mutex
);
1242 if (cur_trans
->in_commit
) {
1243 atomic_inc(&cur_trans
->use_count
);
1244 mutex_unlock(&root
->fs_info
->trans_mutex
);
1245 btrfs_end_transaction(trans
, root
);
1247 ret
= wait_for_commit(root
, cur_trans
);
1250 mutex_lock(&root
->fs_info
->trans_mutex
);
1251 put_transaction(cur_trans
);
1252 mutex_unlock(&root
->fs_info
->trans_mutex
);
1257 trans
->transaction
->in_commit
= 1;
1258 trans
->transaction
->blocked
= 1;
1259 wake_up(&root
->fs_info
->transaction_blocked_wait
);
1261 if (cur_trans
->list
.prev
!= &root
->fs_info
->trans_list
) {
1262 prev_trans
= list_entry(cur_trans
->list
.prev
,
1263 struct btrfs_transaction
, list
);
1264 if (!prev_trans
->commit_done
) {
1265 atomic_inc(&prev_trans
->use_count
);
1266 mutex_unlock(&root
->fs_info
->trans_mutex
);
1268 wait_for_commit(root
, prev_trans
);
1270 mutex_lock(&root
->fs_info
->trans_mutex
);
1271 put_transaction(prev_trans
);
1275 if (now
< cur_trans
->start_time
|| now
- cur_trans
->start_time
< 1)
1279 int snap_pending
= 0;
1280 joined
= cur_trans
->num_joined
;
1281 if (!list_empty(&trans
->transaction
->pending_snapshots
))
1284 WARN_ON(cur_trans
!= trans
->transaction
);
1285 mutex_unlock(&root
->fs_info
->trans_mutex
);
1287 if (flush_on_commit
|| snap_pending
) {
1288 btrfs_start_delalloc_inodes(root
, 1);
1289 ret
= btrfs_wait_ordered_extents(root
, 0, 1);
1294 * rename don't use btrfs_join_transaction, so, once we
1295 * set the transaction to blocked above, we aren't going
1296 * to get any new ordered operations. We can safely run
1297 * it here and no for sure that nothing new will be added
1300 btrfs_run_ordered_operations(root
, 1);
1302 prepare_to_wait(&cur_trans
->writer_wait
, &wait
,
1303 TASK_UNINTERRUPTIBLE
);
1306 if (atomic_read(&cur_trans
->num_writers
) > 1)
1307 schedule_timeout(MAX_SCHEDULE_TIMEOUT
);
1308 else if (should_grow
)
1309 schedule_timeout(1);
1311 mutex_lock(&root
->fs_info
->trans_mutex
);
1312 finish_wait(&cur_trans
->writer_wait
, &wait
);
1313 } while (atomic_read(&cur_trans
->num_writers
) > 1 ||
1314 (should_grow
&& cur_trans
->num_joined
!= joined
));
1316 ret
= create_pending_snapshots(trans
, root
->fs_info
);
1319 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
1322 WARN_ON(cur_trans
!= trans
->transaction
);
1324 /* btrfs_commit_tree_roots is responsible for getting the
1325 * various roots consistent with each other. Every pointer
1326 * in the tree of tree roots has to point to the most up to date
1327 * root for every subvolume and other tree. So, we have to keep
1328 * the tree logging code from jumping in and changing any
1331 * At this point in the commit, there can't be any tree-log
1332 * writers, but a little lower down we drop the trans mutex
1333 * and let new people in. By holding the tree_log_mutex
1334 * from now until after the super is written, we avoid races
1335 * with the tree-log code.
1337 mutex_lock(&root
->fs_info
->tree_log_mutex
);
1339 ret
= commit_fs_roots(trans
, root
);
1342 /* commit_fs_roots gets rid of all the tree log roots, it is now
1343 * safe to free the root of tree log roots
1345 btrfs_free_log_root_tree(trans
, root
->fs_info
);
1347 ret
= commit_cowonly_roots(trans
, root
);
1350 btrfs_prepare_extent_commit(trans
, root
);
1352 cur_trans
= root
->fs_info
->running_transaction
;
1353 spin_lock(&root
->fs_info
->new_trans_lock
);
1354 root
->fs_info
->running_transaction
= NULL
;
1355 spin_unlock(&root
->fs_info
->new_trans_lock
);
1357 btrfs_set_root_node(&root
->fs_info
->tree_root
->root_item
,
1358 root
->fs_info
->tree_root
->node
);
1359 switch_commit_root(root
->fs_info
->tree_root
);
1361 btrfs_set_root_node(&root
->fs_info
->chunk_root
->root_item
,
1362 root
->fs_info
->chunk_root
->node
);
1363 switch_commit_root(root
->fs_info
->chunk_root
);
1365 update_super_roots(root
);
1367 if (!root
->fs_info
->log_root_recovering
) {
1368 btrfs_set_super_log_root(&root
->fs_info
->super_copy
, 0);
1369 btrfs_set_super_log_root_level(&root
->fs_info
->super_copy
, 0);
1372 memcpy(&root
->fs_info
->super_for_commit
, &root
->fs_info
->super_copy
,
1373 sizeof(root
->fs_info
->super_copy
));
1375 trans
->transaction
->blocked
= 0;
1377 wake_up(&root
->fs_info
->transaction_wait
);
1379 mutex_unlock(&root
->fs_info
->trans_mutex
);
1380 ret
= btrfs_write_and_wait_transaction(trans
, root
);
1382 write_ctree_super(trans
, root
, 0);
1385 * the super is written, we can safely allow the tree-loggers
1386 * to go about their business
1388 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1390 btrfs_finish_extent_commit(trans
, root
);
1392 mutex_lock(&root
->fs_info
->trans_mutex
);
1394 cur_trans
->commit_done
= 1;
1396 root
->fs_info
->last_trans_committed
= cur_trans
->transid
;
1398 wake_up(&cur_trans
->commit_wait
);
1400 list_del_init(&cur_trans
->list
);
1401 put_transaction(cur_trans
);
1402 put_transaction(cur_trans
);
1404 trace_btrfs_transaction_commit(root
);
1406 mutex_unlock(&root
->fs_info
->trans_mutex
);
1408 if (current
->journal_info
== trans
)
1409 current
->journal_info
= NULL
;
1411 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
1413 if (current
!= root
->fs_info
->transaction_kthread
)
1414 btrfs_run_delayed_iputs(root
);
1420 * interface function to delete all the snapshots we have scheduled for deletion
1422 int btrfs_clean_old_snapshots(struct btrfs_root
*root
)
1425 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1427 mutex_lock(&fs_info
->trans_mutex
);
1428 list_splice_init(&fs_info
->dead_roots
, &list
);
1429 mutex_unlock(&fs_info
->trans_mutex
);
1431 while (!list_empty(&list
)) {
1432 root
= list_entry(list
.next
, struct btrfs_root
, root_list
);
1433 list_del(&root
->root_list
);
1435 if (btrfs_header_backref_rev(root
->node
) <
1436 BTRFS_MIXED_BACKREF_REV
)
1437 btrfs_drop_snapshot(root
, NULL
, 0);
1439 btrfs_drop_snapshot(root
, NULL
, 1);