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"
30 #include "inode-map.h"
32 #define BTRFS_ROOT_TRANS_TAG 0
34 void put_transaction(struct btrfs_transaction
*transaction
)
36 WARN_ON(atomic_read(&transaction
->use_count
) == 0);
37 if (atomic_dec_and_test(&transaction
->use_count
)) {
38 BUG_ON(!list_empty(&transaction
->list
));
39 WARN_ON(transaction
->delayed_refs
.root
.rb_node
);
40 WARN_ON(!list_empty(&transaction
->delayed_refs
.seq_head
));
41 memset(transaction
, 0, sizeof(*transaction
));
42 kmem_cache_free(btrfs_transaction_cachep
, transaction
);
46 static noinline
void switch_commit_root(struct btrfs_root
*root
)
48 free_extent_buffer(root
->commit_root
);
49 root
->commit_root
= btrfs_root_node(root
);
53 * either allocate a new transaction or hop into the existing one
55 static noinline
int join_transaction(struct btrfs_root
*root
, int nofail
)
57 struct btrfs_transaction
*cur_trans
;
59 spin_lock(&root
->fs_info
->trans_lock
);
61 /* The file system has been taken offline. No new transactions. */
62 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
) {
63 spin_unlock(&root
->fs_info
->trans_lock
);
67 if (root
->fs_info
->trans_no_join
) {
69 spin_unlock(&root
->fs_info
->trans_lock
);
74 cur_trans
= root
->fs_info
->running_transaction
;
76 if (cur_trans
->aborted
) {
77 spin_unlock(&root
->fs_info
->trans_lock
);
78 return cur_trans
->aborted
;
80 atomic_inc(&cur_trans
->use_count
);
81 atomic_inc(&cur_trans
->num_writers
);
82 cur_trans
->num_joined
++;
83 spin_unlock(&root
->fs_info
->trans_lock
);
86 spin_unlock(&root
->fs_info
->trans_lock
);
88 cur_trans
= kmem_cache_alloc(btrfs_transaction_cachep
, GFP_NOFS
);
92 spin_lock(&root
->fs_info
->trans_lock
);
93 if (root
->fs_info
->running_transaction
) {
95 * someone started a transaction after we unlocked. Make sure
96 * to redo the trans_no_join checks above
98 kmem_cache_free(btrfs_transaction_cachep
, cur_trans
);
99 cur_trans
= root
->fs_info
->running_transaction
;
103 atomic_set(&cur_trans
->num_writers
, 1);
104 cur_trans
->num_joined
= 0;
105 init_waitqueue_head(&cur_trans
->writer_wait
);
106 init_waitqueue_head(&cur_trans
->commit_wait
);
107 cur_trans
->in_commit
= 0;
108 cur_trans
->blocked
= 0;
110 * One for this trans handle, one so it will live on until we
111 * commit the transaction.
113 atomic_set(&cur_trans
->use_count
, 2);
114 cur_trans
->commit_done
= 0;
115 cur_trans
->start_time
= get_seconds();
117 cur_trans
->delayed_refs
.root
= RB_ROOT
;
118 cur_trans
->delayed_refs
.num_entries
= 0;
119 cur_trans
->delayed_refs
.num_heads_ready
= 0;
120 cur_trans
->delayed_refs
.num_heads
= 0;
121 cur_trans
->delayed_refs
.flushing
= 0;
122 cur_trans
->delayed_refs
.run_delayed_start
= 0;
123 cur_trans
->delayed_refs
.seq
= 1;
124 init_waitqueue_head(&cur_trans
->delayed_refs
.seq_wait
);
125 spin_lock_init(&cur_trans
->commit_lock
);
126 spin_lock_init(&cur_trans
->delayed_refs
.lock
);
127 INIT_LIST_HEAD(&cur_trans
->delayed_refs
.seq_head
);
129 INIT_LIST_HEAD(&cur_trans
->pending_snapshots
);
130 list_add_tail(&cur_trans
->list
, &root
->fs_info
->trans_list
);
131 extent_io_tree_init(&cur_trans
->dirty_pages
,
132 root
->fs_info
->btree_inode
->i_mapping
);
133 root
->fs_info
->generation
++;
134 cur_trans
->transid
= root
->fs_info
->generation
;
135 root
->fs_info
->running_transaction
= cur_trans
;
136 cur_trans
->aborted
= 0;
137 spin_unlock(&root
->fs_info
->trans_lock
);
143 * this does all the record keeping required to make sure that a reference
144 * counted root is properly recorded in a given transaction. This is required
145 * to make sure the old root from before we joined the transaction is deleted
146 * when the transaction commits
148 static int record_root_in_trans(struct btrfs_trans_handle
*trans
,
149 struct btrfs_root
*root
)
151 if (root
->ref_cows
&& root
->last_trans
< trans
->transid
) {
152 WARN_ON(root
== root
->fs_info
->extent_root
);
153 WARN_ON(root
->commit_root
!= root
->node
);
156 * see below for in_trans_setup usage rules
157 * we have the reloc mutex held now, so there
158 * is only one writer in this function
160 root
->in_trans_setup
= 1;
162 /* make sure readers find in_trans_setup before
163 * they find our root->last_trans update
167 spin_lock(&root
->fs_info
->fs_roots_radix_lock
);
168 if (root
->last_trans
== trans
->transid
) {
169 spin_unlock(&root
->fs_info
->fs_roots_radix_lock
);
172 radix_tree_tag_set(&root
->fs_info
->fs_roots_radix
,
173 (unsigned long)root
->root_key
.objectid
,
174 BTRFS_ROOT_TRANS_TAG
);
175 spin_unlock(&root
->fs_info
->fs_roots_radix_lock
);
176 root
->last_trans
= trans
->transid
;
178 /* this is pretty tricky. We don't want to
179 * take the relocation lock in btrfs_record_root_in_trans
180 * unless we're really doing the first setup for this root in
183 * Normally we'd use root->last_trans as a flag to decide
184 * if we want to take the expensive mutex.
186 * But, we have to set root->last_trans before we
187 * init the relocation root, otherwise, we trip over warnings
188 * in ctree.c. The solution used here is to flag ourselves
189 * with root->in_trans_setup. When this is 1, we're still
190 * fixing up the reloc trees and everyone must wait.
192 * When this is zero, they can trust root->last_trans and fly
193 * through btrfs_record_root_in_trans without having to take the
194 * lock. smp_wmb() makes sure that all the writes above are
195 * done before we pop in the zero below
197 btrfs_init_reloc_root(trans
, root
);
199 root
->in_trans_setup
= 0;
205 int btrfs_record_root_in_trans(struct btrfs_trans_handle
*trans
,
206 struct btrfs_root
*root
)
212 * see record_root_in_trans for comments about in_trans_setup usage
216 if (root
->last_trans
== trans
->transid
&&
217 !root
->in_trans_setup
)
220 mutex_lock(&root
->fs_info
->reloc_mutex
);
221 record_root_in_trans(trans
, root
);
222 mutex_unlock(&root
->fs_info
->reloc_mutex
);
227 /* wait for commit against the current transaction to become unblocked
228 * when this is done, it is safe to start a new transaction, but the current
229 * transaction might not be fully on disk.
231 static void wait_current_trans(struct btrfs_root
*root
)
233 struct btrfs_transaction
*cur_trans
;
235 spin_lock(&root
->fs_info
->trans_lock
);
236 cur_trans
= root
->fs_info
->running_transaction
;
237 if (cur_trans
&& cur_trans
->blocked
) {
238 atomic_inc(&cur_trans
->use_count
);
239 spin_unlock(&root
->fs_info
->trans_lock
);
241 wait_event(root
->fs_info
->transaction_wait
,
242 !cur_trans
->blocked
);
243 put_transaction(cur_trans
);
245 spin_unlock(&root
->fs_info
->trans_lock
);
249 enum btrfs_trans_type
{
256 static int may_wait_transaction(struct btrfs_root
*root
, int type
)
258 if (root
->fs_info
->log_root_recovering
)
261 if (type
== TRANS_USERSPACE
)
264 if (type
== TRANS_START
&&
265 !atomic_read(&root
->fs_info
->open_ioctl_trans
))
271 static struct btrfs_trans_handle
*start_transaction(struct btrfs_root
*root
,
272 u64 num_items
, int type
)
274 struct btrfs_trans_handle
*h
;
275 struct btrfs_transaction
*cur_trans
;
279 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
)
280 return ERR_PTR(-EROFS
);
282 if (current
->journal_info
) {
283 WARN_ON(type
!= TRANS_JOIN
&& type
!= TRANS_JOIN_NOLOCK
);
284 h
= current
->journal_info
;
286 h
->orig_rsv
= h
->block_rsv
;
292 * Do the reservation before we join the transaction so we can do all
293 * the appropriate flushing if need be.
295 if (num_items
> 0 && root
!= root
->fs_info
->chunk_root
) {
296 num_bytes
= btrfs_calc_trans_metadata_size(root
, num_items
);
297 ret
= btrfs_block_rsv_add(root
,
298 &root
->fs_info
->trans_block_rsv
,
304 h
= kmem_cache_alloc(btrfs_trans_handle_cachep
, GFP_NOFS
);
306 return ERR_PTR(-ENOMEM
);
308 if (may_wait_transaction(root
, type
))
309 wait_current_trans(root
);
312 ret
= join_transaction(root
, type
== TRANS_JOIN_NOLOCK
);
314 wait_current_trans(root
);
315 } while (ret
== -EBUSY
);
318 kmem_cache_free(btrfs_trans_handle_cachep
, h
);
322 cur_trans
= root
->fs_info
->running_transaction
;
324 h
->transid
= cur_trans
->transid
;
325 h
->transaction
= cur_trans
;
327 h
->bytes_reserved
= 0;
328 h
->delayed_ref_updates
= 0;
335 if (cur_trans
->blocked
&& may_wait_transaction(root
, type
)) {
336 btrfs_commit_transaction(h
, root
);
341 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
342 h
->transid
, num_bytes
, 1);
343 h
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
344 h
->bytes_reserved
= num_bytes
;
348 btrfs_record_root_in_trans(h
, root
);
350 if (!current
->journal_info
&& type
!= TRANS_USERSPACE
)
351 current
->journal_info
= h
;
355 struct btrfs_trans_handle
*btrfs_start_transaction(struct btrfs_root
*root
,
358 return start_transaction(root
, num_items
, TRANS_START
);
360 struct btrfs_trans_handle
*btrfs_join_transaction(struct btrfs_root
*root
)
362 return start_transaction(root
, 0, TRANS_JOIN
);
365 struct btrfs_trans_handle
*btrfs_join_transaction_nolock(struct btrfs_root
*root
)
367 return start_transaction(root
, 0, TRANS_JOIN_NOLOCK
);
370 struct btrfs_trans_handle
*btrfs_start_ioctl_transaction(struct btrfs_root
*root
)
372 return start_transaction(root
, 0, TRANS_USERSPACE
);
375 /* wait for a transaction commit to be fully complete */
376 static noinline
void wait_for_commit(struct btrfs_root
*root
,
377 struct btrfs_transaction
*commit
)
379 wait_event(commit
->commit_wait
, commit
->commit_done
);
382 int btrfs_wait_for_commit(struct btrfs_root
*root
, u64 transid
)
384 struct btrfs_transaction
*cur_trans
= NULL
, *t
;
389 if (transid
<= root
->fs_info
->last_trans_committed
)
392 /* find specified transaction */
393 spin_lock(&root
->fs_info
->trans_lock
);
394 list_for_each_entry(t
, &root
->fs_info
->trans_list
, list
) {
395 if (t
->transid
== transid
) {
397 atomic_inc(&cur_trans
->use_count
);
400 if (t
->transid
> transid
)
403 spin_unlock(&root
->fs_info
->trans_lock
);
406 goto out
; /* bad transid */
408 /* find newest transaction that is committing | committed */
409 spin_lock(&root
->fs_info
->trans_lock
);
410 list_for_each_entry_reverse(t
, &root
->fs_info
->trans_list
,
416 atomic_inc(&cur_trans
->use_count
);
420 spin_unlock(&root
->fs_info
->trans_lock
);
422 goto out
; /* nothing committing|committed */
425 wait_for_commit(root
, cur_trans
);
427 put_transaction(cur_trans
);
433 void btrfs_throttle(struct btrfs_root
*root
)
435 if (!atomic_read(&root
->fs_info
->open_ioctl_trans
))
436 wait_current_trans(root
);
439 static int should_end_transaction(struct btrfs_trans_handle
*trans
,
440 struct btrfs_root
*root
)
444 ret
= btrfs_block_rsv_check(root
, &root
->fs_info
->global_block_rsv
, 5);
448 int btrfs_should_end_transaction(struct btrfs_trans_handle
*trans
,
449 struct btrfs_root
*root
)
451 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
452 struct btrfs_block_rsv
*rsv
= trans
->block_rsv
;
457 if (cur_trans
->blocked
|| cur_trans
->delayed_refs
.flushing
)
461 * We need to do this in case we're deleting csums so the global block
462 * rsv get's used instead of the csum block rsv.
464 trans
->block_rsv
= NULL
;
466 updates
= trans
->delayed_ref_updates
;
467 trans
->delayed_ref_updates
= 0;
469 err
= btrfs_run_delayed_refs(trans
, root
, updates
);
470 if (err
) /* Error code will also eval true */
474 trans
->block_rsv
= rsv
;
476 return should_end_transaction(trans
, root
);
479 static int __btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
480 struct btrfs_root
*root
, int throttle
, int lock
)
482 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
483 struct btrfs_fs_info
*info
= root
->fs_info
;
487 if (--trans
->use_count
) {
488 trans
->block_rsv
= trans
->orig_rsv
;
492 btrfs_trans_release_metadata(trans
, root
);
493 trans
->block_rsv
= NULL
;
495 unsigned long cur
= trans
->delayed_ref_updates
;
496 trans
->delayed_ref_updates
= 0;
498 trans
->transaction
->delayed_refs
.num_heads_ready
> 64) {
499 trans
->delayed_ref_updates
= 0;
500 btrfs_run_delayed_refs(trans
, root
, cur
);
507 if (lock
&& !atomic_read(&root
->fs_info
->open_ioctl_trans
) &&
508 should_end_transaction(trans
, root
)) {
509 trans
->transaction
->blocked
= 1;
513 if (lock
&& cur_trans
->blocked
&& !cur_trans
->in_commit
) {
516 * We may race with somebody else here so end up having
517 * to call end_transaction on ourselves again, so inc
521 return btrfs_commit_transaction(trans
, root
);
523 wake_up_process(info
->transaction_kthread
);
527 WARN_ON(cur_trans
!= info
->running_transaction
);
528 WARN_ON(atomic_read(&cur_trans
->num_writers
) < 1);
529 atomic_dec(&cur_trans
->num_writers
);
532 if (waitqueue_active(&cur_trans
->writer_wait
))
533 wake_up(&cur_trans
->writer_wait
);
534 put_transaction(cur_trans
);
536 if (current
->journal_info
== trans
)
537 current
->journal_info
= NULL
;
540 btrfs_run_delayed_iputs(root
);
542 if (trans
->aborted
||
543 root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
) {
547 memset(trans
, 0, sizeof(*trans
));
548 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
552 int btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
553 struct btrfs_root
*root
)
557 ret
= __btrfs_end_transaction(trans
, root
, 0, 1);
563 int btrfs_end_transaction_throttle(struct btrfs_trans_handle
*trans
,
564 struct btrfs_root
*root
)
568 ret
= __btrfs_end_transaction(trans
, root
, 1, 1);
574 int btrfs_end_transaction_nolock(struct btrfs_trans_handle
*trans
,
575 struct btrfs_root
*root
)
579 ret
= __btrfs_end_transaction(trans
, root
, 0, 0);
585 int btrfs_end_transaction_dmeta(struct btrfs_trans_handle
*trans
,
586 struct btrfs_root
*root
)
588 return __btrfs_end_transaction(trans
, root
, 1, 1);
592 * when btree blocks are allocated, they have some corresponding bits set for
593 * them in one of two extent_io trees. This is used to make sure all of
594 * those extents are sent to disk but does not wait on them
596 int btrfs_write_marked_extents(struct btrfs_root
*root
,
597 struct extent_io_tree
*dirty_pages
, int mark
)
601 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
605 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
607 convert_extent_bit(dirty_pages
, start
, end
, EXTENT_NEED_WAIT
, mark
,
609 err
= filemap_fdatawrite_range(mapping
, start
, end
);
621 * when btree blocks are allocated, they have some corresponding bits set for
622 * them in one of two extent_io trees. This is used to make sure all of
623 * those extents are on disk for transaction or log commit. We wait
624 * on all the pages and clear them from the dirty pages state tree
626 int btrfs_wait_marked_extents(struct btrfs_root
*root
,
627 struct extent_io_tree
*dirty_pages
, int mark
)
631 struct address_space
*mapping
= root
->fs_info
->btree_inode
->i_mapping
;
635 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
637 clear_extent_bits(dirty_pages
, start
, end
, EXTENT_NEED_WAIT
, GFP_NOFS
);
638 err
= filemap_fdatawait_range(mapping
, start
, end
);
650 * when btree blocks are allocated, they have some corresponding bits set for
651 * them in one of two extent_io trees. This is used to make sure all of
652 * those extents are on disk for transaction or log commit
654 int btrfs_write_and_wait_marked_extents(struct btrfs_root
*root
,
655 struct extent_io_tree
*dirty_pages
, int mark
)
660 ret
= btrfs_write_marked_extents(root
, dirty_pages
, mark
);
661 ret2
= btrfs_wait_marked_extents(root
, dirty_pages
, mark
);
670 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle
*trans
,
671 struct btrfs_root
*root
)
673 if (!trans
|| !trans
->transaction
) {
674 struct inode
*btree_inode
;
675 btree_inode
= root
->fs_info
->btree_inode
;
676 return filemap_write_and_wait(btree_inode
->i_mapping
);
678 return btrfs_write_and_wait_marked_extents(root
,
679 &trans
->transaction
->dirty_pages
,
684 * this is used to update the root pointer in the tree of tree roots.
686 * But, in the case of the extent allocation tree, updating the root
687 * pointer may allocate blocks which may change the root of the extent
690 * So, this loops and repeats and makes sure the cowonly root didn't
691 * change while the root pointer was being updated in the metadata.
693 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
694 struct btrfs_root
*root
)
699 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
701 old_root_used
= btrfs_root_used(&root
->root_item
);
702 btrfs_write_dirty_block_groups(trans
, root
);
705 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
706 if (old_root_bytenr
== root
->node
->start
&&
707 old_root_used
== btrfs_root_used(&root
->root_item
))
710 btrfs_set_root_node(&root
->root_item
, root
->node
);
711 ret
= btrfs_update_root(trans
, tree_root
,
717 old_root_used
= btrfs_root_used(&root
->root_item
);
718 ret
= btrfs_write_dirty_block_groups(trans
, root
);
723 if (root
!= root
->fs_info
->extent_root
)
724 switch_commit_root(root
);
730 * update all the cowonly tree roots on disk
732 * The error handling in this function may not be obvious. Any of the
733 * failures will cause the file system to go offline. We still need
734 * to clean up the delayed refs.
736 static noinline
int commit_cowonly_roots(struct btrfs_trans_handle
*trans
,
737 struct btrfs_root
*root
)
739 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
740 struct list_head
*next
;
741 struct extent_buffer
*eb
;
744 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
748 eb
= btrfs_lock_root_node(fs_info
->tree_root
);
749 ret
= btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
,
751 btrfs_tree_unlock(eb
);
752 free_extent_buffer(eb
);
757 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
761 while (!list_empty(&fs_info
->dirty_cowonly_roots
)) {
762 next
= fs_info
->dirty_cowonly_roots
.next
;
764 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
766 ret
= update_cowonly_root(trans
, root
);
771 down_write(&fs_info
->extent_commit_sem
);
772 switch_commit_root(fs_info
->extent_root
);
773 up_write(&fs_info
->extent_commit_sem
);
779 * dead roots are old snapshots that need to be deleted. This allocates
780 * a dirty root struct and adds it into the list of dead roots that need to
783 int btrfs_add_dead_root(struct btrfs_root
*root
)
785 spin_lock(&root
->fs_info
->trans_lock
);
786 list_add(&root
->root_list
, &root
->fs_info
->dead_roots
);
787 spin_unlock(&root
->fs_info
->trans_lock
);
792 * update all the cowonly tree roots on disk
794 static noinline
int commit_fs_roots(struct btrfs_trans_handle
*trans
,
795 struct btrfs_root
*root
)
797 struct btrfs_root
*gang
[8];
798 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
803 spin_lock(&fs_info
->fs_roots_radix_lock
);
805 ret
= radix_tree_gang_lookup_tag(&fs_info
->fs_roots_radix
,
808 BTRFS_ROOT_TRANS_TAG
);
811 for (i
= 0; i
< ret
; i
++) {
813 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
814 (unsigned long)root
->root_key
.objectid
,
815 BTRFS_ROOT_TRANS_TAG
);
816 spin_unlock(&fs_info
->fs_roots_radix_lock
);
818 btrfs_free_log(trans
, root
);
819 btrfs_update_reloc_root(trans
, root
);
820 btrfs_orphan_commit_root(trans
, root
);
822 btrfs_save_ino_cache(root
, trans
);
824 /* see comments in should_cow_block() */
828 if (root
->commit_root
!= root
->node
) {
829 mutex_lock(&root
->fs_commit_mutex
);
830 switch_commit_root(root
);
831 btrfs_unpin_free_ino(root
);
832 mutex_unlock(&root
->fs_commit_mutex
);
834 btrfs_set_root_node(&root
->root_item
,
838 err
= btrfs_update_root(trans
, fs_info
->tree_root
,
841 spin_lock(&fs_info
->fs_roots_radix_lock
);
846 spin_unlock(&fs_info
->fs_roots_radix_lock
);
851 * defrag a given btree. If cacheonly == 1, this won't read from the disk,
852 * otherwise every leaf in the btree is read and defragged.
854 int btrfs_defrag_root(struct btrfs_root
*root
, int cacheonly
)
856 struct btrfs_fs_info
*info
= root
->fs_info
;
857 struct btrfs_trans_handle
*trans
;
861 if (xchg(&root
->defrag_running
, 1))
865 trans
= btrfs_start_transaction(root
, 0);
867 return PTR_ERR(trans
);
869 ret
= btrfs_defrag_leaves(trans
, root
, cacheonly
);
871 nr
= trans
->blocks_used
;
872 btrfs_end_transaction(trans
, root
);
873 btrfs_btree_balance_dirty(info
->tree_root
, nr
);
876 if (btrfs_fs_closing(root
->fs_info
) || ret
!= -EAGAIN
)
879 root
->defrag_running
= 0;
884 * new snapshots need to be created at a very specific time in the
885 * transaction commit. This does the actual creation
887 static noinline
int create_pending_snapshot(struct btrfs_trans_handle
*trans
,
888 struct btrfs_fs_info
*fs_info
,
889 struct btrfs_pending_snapshot
*pending
)
891 struct btrfs_key key
;
892 struct btrfs_root_item
*new_root_item
;
893 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
894 struct btrfs_root
*root
= pending
->root
;
895 struct btrfs_root
*parent_root
;
896 struct btrfs_block_rsv
*rsv
;
897 struct inode
*parent_inode
;
898 struct dentry
*parent
;
899 struct dentry
*dentry
;
900 struct extent_buffer
*tmp
;
901 struct extent_buffer
*old
;
908 rsv
= trans
->block_rsv
;
910 new_root_item
= kmalloc(sizeof(*new_root_item
), GFP_NOFS
);
911 if (!new_root_item
) {
912 ret
= pending
->error
= -ENOMEM
;
916 ret
= btrfs_find_free_objectid(tree_root
, &objectid
);
918 pending
->error
= ret
;
922 btrfs_reloc_pre_snapshot(trans
, pending
, &to_reserve
);
924 if (to_reserve
> 0) {
925 ret
= btrfs_block_rsv_add_noflush(root
, &pending
->block_rsv
,
928 pending
->error
= ret
;
933 key
.objectid
= objectid
;
934 key
.offset
= (u64
)-1;
935 key
.type
= BTRFS_ROOT_ITEM_KEY
;
937 trans
->block_rsv
= &pending
->block_rsv
;
939 dentry
= pending
->dentry
;
940 parent
= dget_parent(dentry
);
941 parent_inode
= parent
->d_inode
;
942 parent_root
= BTRFS_I(parent_inode
)->root
;
943 record_root_in_trans(trans
, parent_root
);
946 * insert the directory item
948 ret
= btrfs_set_inode_index(parent_inode
, &index
);
949 BUG_ON(ret
); /* -ENOMEM */
950 ret
= btrfs_insert_dir_item(trans
, parent_root
,
951 dentry
->d_name
.name
, dentry
->d_name
.len
,
953 BTRFS_FT_DIR
, index
);
954 if (ret
== -EEXIST
) {
955 pending
->error
= -EEXIST
;
959 goto abort_trans_dput
;
962 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
963 dentry
->d_name
.len
* 2);
964 ret
= btrfs_update_inode(trans
, parent_root
, parent_inode
);
966 goto abort_trans_dput
;
969 * pull in the delayed directory update
970 * and the delayed inode item
971 * otherwise we corrupt the FS during
974 ret
= btrfs_run_delayed_items(trans
, root
);
975 if (ret
) { /* Transaction aborted */
980 record_root_in_trans(trans
, root
);
981 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
);
982 memcpy(new_root_item
, &root
->root_item
, sizeof(*new_root_item
));
983 btrfs_check_and_init_root_item(new_root_item
);
985 root_flags
= btrfs_root_flags(new_root_item
);
986 if (pending
->readonly
)
987 root_flags
|= BTRFS_ROOT_SUBVOL_RDONLY
;
989 root_flags
&= ~BTRFS_ROOT_SUBVOL_RDONLY
;
990 btrfs_set_root_flags(new_root_item
, root_flags
);
992 old
= btrfs_lock_root_node(root
);
993 ret
= btrfs_cow_block(trans
, root
, old
, NULL
, 0, &old
);
995 btrfs_tree_unlock(old
);
996 free_extent_buffer(old
);
997 goto abort_trans_dput
;
1000 btrfs_set_lock_blocking(old
);
1002 ret
= btrfs_copy_root(trans
, root
, old
, &tmp
, objectid
);
1003 /* clean up in any case */
1004 btrfs_tree_unlock(old
);
1005 free_extent_buffer(old
);
1007 goto abort_trans_dput
;
1009 /* see comments in should_cow_block() */
1010 root
->force_cow
= 1;
1013 btrfs_set_root_node(new_root_item
, tmp
);
1014 /* record when the snapshot was created in key.offset */
1015 key
.offset
= trans
->transid
;
1016 ret
= btrfs_insert_root(trans
, tree_root
, &key
, new_root_item
);
1017 btrfs_tree_unlock(tmp
);
1018 free_extent_buffer(tmp
);
1020 goto abort_trans_dput
;
1023 * insert root back/forward references
1025 ret
= btrfs_add_root_ref(trans
, tree_root
, objectid
,
1026 parent_root
->root_key
.objectid
,
1027 btrfs_ino(parent_inode
), index
,
1028 dentry
->d_name
.name
, dentry
->d_name
.len
);
1033 key
.offset
= (u64
)-1;
1034 pending
->snap
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
1035 if (IS_ERR(pending
->snap
)) {
1036 ret
= PTR_ERR(pending
->snap
);
1040 ret
= btrfs_reloc_post_snapshot(trans
, pending
);
1045 kfree(new_root_item
);
1046 trans
->block_rsv
= rsv
;
1047 btrfs_block_rsv_release(root
, &pending
->block_rsv
, (u64
)-1);
1053 btrfs_abort_transaction(trans
, root
, ret
);
1058 * create all the snapshots we've scheduled for creation
1060 static noinline
int create_pending_snapshots(struct btrfs_trans_handle
*trans
,
1061 struct btrfs_fs_info
*fs_info
)
1063 struct btrfs_pending_snapshot
*pending
;
1064 struct list_head
*head
= &trans
->transaction
->pending_snapshots
;
1066 list_for_each_entry(pending
, head
, list
)
1067 create_pending_snapshot(trans
, fs_info
, pending
);
1071 static void update_super_roots(struct btrfs_root
*root
)
1073 struct btrfs_root_item
*root_item
;
1074 struct btrfs_super_block
*super
;
1076 super
= root
->fs_info
->super_copy
;
1078 root_item
= &root
->fs_info
->chunk_root
->root_item
;
1079 super
->chunk_root
= root_item
->bytenr
;
1080 super
->chunk_root_generation
= root_item
->generation
;
1081 super
->chunk_root_level
= root_item
->level
;
1083 root_item
= &root
->fs_info
->tree_root
->root_item
;
1084 super
->root
= root_item
->bytenr
;
1085 super
->generation
= root_item
->generation
;
1086 super
->root_level
= root_item
->level
;
1087 if (btrfs_test_opt(root
, SPACE_CACHE
))
1088 super
->cache_generation
= root_item
->generation
;
1091 int btrfs_transaction_in_commit(struct btrfs_fs_info
*info
)
1094 spin_lock(&info
->trans_lock
);
1095 if (info
->running_transaction
)
1096 ret
= info
->running_transaction
->in_commit
;
1097 spin_unlock(&info
->trans_lock
);
1101 int btrfs_transaction_blocked(struct btrfs_fs_info
*info
)
1104 spin_lock(&info
->trans_lock
);
1105 if (info
->running_transaction
)
1106 ret
= info
->running_transaction
->blocked
;
1107 spin_unlock(&info
->trans_lock
);
1112 * wait for the current transaction commit to start and block subsequent
1115 static void wait_current_trans_commit_start(struct btrfs_root
*root
,
1116 struct btrfs_transaction
*trans
)
1118 wait_event(root
->fs_info
->transaction_blocked_wait
, trans
->in_commit
);
1122 * wait for the current transaction to start and then become unblocked.
1125 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root
*root
,
1126 struct btrfs_transaction
*trans
)
1128 wait_event(root
->fs_info
->transaction_wait
,
1129 trans
->commit_done
|| (trans
->in_commit
&& !trans
->blocked
));
1133 * commit transactions asynchronously. once btrfs_commit_transaction_async
1134 * returns, any subsequent transaction will not be allowed to join.
1136 struct btrfs_async_commit
{
1137 struct btrfs_trans_handle
*newtrans
;
1138 struct btrfs_root
*root
;
1139 struct delayed_work work
;
1142 static void do_async_commit(struct work_struct
*work
)
1144 struct btrfs_async_commit
*ac
=
1145 container_of(work
, struct btrfs_async_commit
, work
.work
);
1147 btrfs_commit_transaction(ac
->newtrans
, ac
->root
);
1151 int btrfs_commit_transaction_async(struct btrfs_trans_handle
*trans
,
1152 struct btrfs_root
*root
,
1153 int wait_for_unblock
)
1155 struct btrfs_async_commit
*ac
;
1156 struct btrfs_transaction
*cur_trans
;
1158 ac
= kmalloc(sizeof(*ac
), GFP_NOFS
);
1162 INIT_DELAYED_WORK(&ac
->work
, do_async_commit
);
1164 ac
->newtrans
= btrfs_join_transaction(root
);
1165 if (IS_ERR(ac
->newtrans
)) {
1166 int err
= PTR_ERR(ac
->newtrans
);
1171 /* take transaction reference */
1172 cur_trans
= trans
->transaction
;
1173 atomic_inc(&cur_trans
->use_count
);
1175 btrfs_end_transaction(trans
, root
);
1176 schedule_delayed_work(&ac
->work
, 0);
1178 /* wait for transaction to start and unblock */
1179 if (wait_for_unblock
)
1180 wait_current_trans_commit_start_and_unblock(root
, cur_trans
);
1182 wait_current_trans_commit_start(root
, cur_trans
);
1184 if (current
->journal_info
== trans
)
1185 current
->journal_info
= NULL
;
1187 put_transaction(cur_trans
);
1192 static void cleanup_transaction(struct btrfs_trans_handle
*trans
,
1193 struct btrfs_root
*root
)
1195 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1197 WARN_ON(trans
->use_count
> 1);
1199 spin_lock(&root
->fs_info
->trans_lock
);
1200 list_del_init(&cur_trans
->list
);
1201 spin_unlock(&root
->fs_info
->trans_lock
);
1203 btrfs_cleanup_one_transaction(trans
->transaction
, root
);
1205 put_transaction(cur_trans
);
1206 put_transaction(cur_trans
);
1208 trace_btrfs_transaction_commit(root
);
1210 btrfs_scrub_continue(root
);
1212 if (current
->journal_info
== trans
)
1213 current
->journal_info
= NULL
;
1215 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
1219 * btrfs_transaction state sequence:
1220 * in_commit = 0, blocked = 0 (initial)
1221 * in_commit = 1, blocked = 1
1225 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
1226 struct btrfs_root
*root
)
1228 unsigned long joined
= 0;
1229 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1230 struct btrfs_transaction
*prev_trans
= NULL
;
1233 int should_grow
= 0;
1234 unsigned long now
= get_seconds();
1235 int flush_on_commit
= btrfs_test_opt(root
, FLUSHONCOMMIT
);
1237 btrfs_run_ordered_operations(root
, 0);
1239 btrfs_trans_release_metadata(trans
, root
);
1240 trans
->block_rsv
= NULL
;
1242 if (cur_trans
->aborted
)
1243 goto cleanup_transaction
;
1245 /* make a pass through all the delayed refs we have so far
1246 * any runnings procs may add more while we are here
1248 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
1250 goto cleanup_transaction
;
1252 cur_trans
= trans
->transaction
;
1255 * set the flushing flag so procs in this transaction have to
1256 * start sending their work down.
1258 cur_trans
->delayed_refs
.flushing
= 1;
1260 ret
= btrfs_run_delayed_refs(trans
, root
, 0);
1262 goto cleanup_transaction
;
1264 spin_lock(&cur_trans
->commit_lock
);
1265 if (cur_trans
->in_commit
) {
1266 spin_unlock(&cur_trans
->commit_lock
);
1267 atomic_inc(&cur_trans
->use_count
);
1268 ret
= btrfs_end_transaction(trans
, root
);
1270 wait_for_commit(root
, cur_trans
);
1272 put_transaction(cur_trans
);
1277 trans
->transaction
->in_commit
= 1;
1278 trans
->transaction
->blocked
= 1;
1279 spin_unlock(&cur_trans
->commit_lock
);
1280 wake_up(&root
->fs_info
->transaction_blocked_wait
);
1282 spin_lock(&root
->fs_info
->trans_lock
);
1283 if (cur_trans
->list
.prev
!= &root
->fs_info
->trans_list
) {
1284 prev_trans
= list_entry(cur_trans
->list
.prev
,
1285 struct btrfs_transaction
, list
);
1286 if (!prev_trans
->commit_done
) {
1287 atomic_inc(&prev_trans
->use_count
);
1288 spin_unlock(&root
->fs_info
->trans_lock
);
1290 wait_for_commit(root
, prev_trans
);
1292 put_transaction(prev_trans
);
1294 spin_unlock(&root
->fs_info
->trans_lock
);
1297 spin_unlock(&root
->fs_info
->trans_lock
);
1300 if (now
< cur_trans
->start_time
|| now
- cur_trans
->start_time
< 1)
1304 int snap_pending
= 0;
1306 joined
= cur_trans
->num_joined
;
1307 if (!list_empty(&trans
->transaction
->pending_snapshots
))
1310 WARN_ON(cur_trans
!= trans
->transaction
);
1312 if (flush_on_commit
|| snap_pending
) {
1313 btrfs_start_delalloc_inodes(root
, 1);
1314 btrfs_wait_ordered_extents(root
, 0, 1);
1317 ret
= btrfs_run_delayed_items(trans
, root
);
1319 goto cleanup_transaction
;
1322 * rename don't use btrfs_join_transaction, so, once we
1323 * set the transaction to blocked above, we aren't going
1324 * to get any new ordered operations. We can safely run
1325 * it here and no for sure that nothing new will be added
1328 btrfs_run_ordered_operations(root
, 1);
1330 prepare_to_wait(&cur_trans
->writer_wait
, &wait
,
1331 TASK_UNINTERRUPTIBLE
);
1333 if (atomic_read(&cur_trans
->num_writers
) > 1)
1334 schedule_timeout(MAX_SCHEDULE_TIMEOUT
);
1335 else if (should_grow
)
1336 schedule_timeout(1);
1338 finish_wait(&cur_trans
->writer_wait
, &wait
);
1339 } while (atomic_read(&cur_trans
->num_writers
) > 1 ||
1340 (should_grow
&& cur_trans
->num_joined
!= joined
));
1343 * Ok now we need to make sure to block out any other joins while we
1344 * commit the transaction. We could have started a join before setting
1345 * no_join so make sure to wait for num_writers to == 1 again.
1347 spin_lock(&root
->fs_info
->trans_lock
);
1348 root
->fs_info
->trans_no_join
= 1;
1349 spin_unlock(&root
->fs_info
->trans_lock
);
1350 wait_event(cur_trans
->writer_wait
,
1351 atomic_read(&cur_trans
->num_writers
) == 1);
1354 * the reloc mutex makes sure that we stop
1355 * the balancing code from coming in and moving
1356 * extents around in the middle of the commit
1358 mutex_lock(&root
->fs_info
->reloc_mutex
);
1360 ret
= btrfs_run_delayed_items(trans
, root
);
1362 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1363 goto cleanup_transaction
;
1366 ret
= create_pending_snapshots(trans
, root
->fs_info
);
1368 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1369 goto cleanup_transaction
;
1372 ret
= btrfs_run_delayed_refs(trans
, root
, (unsigned long)-1);
1374 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1375 goto cleanup_transaction
;
1379 * make sure none of the code above managed to slip in a
1382 btrfs_assert_delayed_root_empty(root
);
1384 WARN_ON(cur_trans
!= trans
->transaction
);
1386 btrfs_scrub_pause(root
);
1387 /* btrfs_commit_tree_roots is responsible for getting the
1388 * various roots consistent with each other. Every pointer
1389 * in the tree of tree roots has to point to the most up to date
1390 * root for every subvolume and other tree. So, we have to keep
1391 * the tree logging code from jumping in and changing any
1394 * At this point in the commit, there can't be any tree-log
1395 * writers, but a little lower down we drop the trans mutex
1396 * and let new people in. By holding the tree_log_mutex
1397 * from now until after the super is written, we avoid races
1398 * with the tree-log code.
1400 mutex_lock(&root
->fs_info
->tree_log_mutex
);
1402 ret
= commit_fs_roots(trans
, root
);
1404 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1405 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1406 goto cleanup_transaction
;
1409 /* commit_fs_roots gets rid of all the tree log roots, it is now
1410 * safe to free the root of tree log roots
1412 btrfs_free_log_root_tree(trans
, root
->fs_info
);
1414 ret
= commit_cowonly_roots(trans
, root
);
1416 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1417 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1418 goto cleanup_transaction
;
1421 btrfs_prepare_extent_commit(trans
, root
);
1423 cur_trans
= root
->fs_info
->running_transaction
;
1425 btrfs_set_root_node(&root
->fs_info
->tree_root
->root_item
,
1426 root
->fs_info
->tree_root
->node
);
1427 switch_commit_root(root
->fs_info
->tree_root
);
1429 btrfs_set_root_node(&root
->fs_info
->chunk_root
->root_item
,
1430 root
->fs_info
->chunk_root
->node
);
1431 switch_commit_root(root
->fs_info
->chunk_root
);
1433 update_super_roots(root
);
1435 if (!root
->fs_info
->log_root_recovering
) {
1436 btrfs_set_super_log_root(root
->fs_info
->super_copy
, 0);
1437 btrfs_set_super_log_root_level(root
->fs_info
->super_copy
, 0);
1440 memcpy(root
->fs_info
->super_for_commit
, root
->fs_info
->super_copy
,
1441 sizeof(*root
->fs_info
->super_copy
));
1443 trans
->transaction
->blocked
= 0;
1444 spin_lock(&root
->fs_info
->trans_lock
);
1445 root
->fs_info
->running_transaction
= NULL
;
1446 root
->fs_info
->trans_no_join
= 0;
1447 spin_unlock(&root
->fs_info
->trans_lock
);
1448 mutex_unlock(&root
->fs_info
->reloc_mutex
);
1450 wake_up(&root
->fs_info
->transaction_wait
);
1452 ret
= btrfs_write_and_wait_transaction(trans
, root
);
1454 btrfs_error(root
->fs_info
, ret
,
1455 "Error while writing out transaction.");
1456 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1457 goto cleanup_transaction
;
1460 ret
= write_ctree_super(trans
, root
, 0);
1462 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1463 goto cleanup_transaction
;
1467 * the super is written, we can safely allow the tree-loggers
1468 * to go about their business
1470 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
1472 btrfs_finish_extent_commit(trans
, root
);
1474 cur_trans
->commit_done
= 1;
1476 root
->fs_info
->last_trans_committed
= cur_trans
->transid
;
1478 wake_up(&cur_trans
->commit_wait
);
1480 spin_lock(&root
->fs_info
->trans_lock
);
1481 list_del_init(&cur_trans
->list
);
1482 spin_unlock(&root
->fs_info
->trans_lock
);
1484 put_transaction(cur_trans
);
1485 put_transaction(cur_trans
);
1487 trace_btrfs_transaction_commit(root
);
1489 btrfs_scrub_continue(root
);
1491 if (current
->journal_info
== trans
)
1492 current
->journal_info
= NULL
;
1494 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
1496 if (current
!= root
->fs_info
->transaction_kthread
)
1497 btrfs_run_delayed_iputs(root
);
1501 cleanup_transaction
:
1502 btrfs_printk(root
->fs_info
, "Skipping commit of aborted transaction.\n");
1504 if (current
->journal_info
== trans
)
1505 current
->journal_info
= NULL
;
1506 cleanup_transaction(trans
, root
);
1512 * interface function to delete all the snapshots we have scheduled for deletion
1514 int btrfs_clean_old_snapshots(struct btrfs_root
*root
)
1517 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1519 spin_lock(&fs_info
->trans_lock
);
1520 list_splice_init(&fs_info
->dead_roots
, &list
);
1521 spin_unlock(&fs_info
->trans_lock
);
1523 while (!list_empty(&list
)) {
1526 root
= list_entry(list
.next
, struct btrfs_root
, root_list
);
1527 list_del(&root
->root_list
);
1529 btrfs_kill_all_delayed_nodes(root
);
1531 if (btrfs_header_backref_rev(root
->node
) <
1532 BTRFS_MIXED_BACKREF_REV
)
1533 ret
= btrfs_drop_snapshot(root
, NULL
, 0, 0);
1535 ret
=btrfs_drop_snapshot(root
, NULL
, 1, 0);