1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
7 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/writeback.h>
10 #include <linux/pagemap.h>
11 #include <linux/blkdev.h>
12 #include <linux/uuid.h>
15 #include "transaction.h"
18 #include "inode-map.h"
20 #include "dev-replace.h"
23 #define BTRFS_ROOT_TRANS_TAG 0
25 static const unsigned int btrfs_blocked_trans_types
[TRANS_STATE_MAX
] = {
26 [TRANS_STATE_RUNNING
] = 0U,
27 [TRANS_STATE_BLOCKED
] = __TRANS_START
,
28 [TRANS_STATE_COMMIT_START
] = (__TRANS_START
| __TRANS_ATTACH
),
29 [TRANS_STATE_COMMIT_DOING
] = (__TRANS_START
|
32 [TRANS_STATE_UNBLOCKED
] = (__TRANS_START
|
36 [TRANS_STATE_COMPLETED
] = (__TRANS_START
|
42 void btrfs_put_transaction(struct btrfs_transaction
*transaction
)
44 WARN_ON(refcount_read(&transaction
->use_count
) == 0);
45 if (refcount_dec_and_test(&transaction
->use_count
)) {
46 BUG_ON(!list_empty(&transaction
->list
));
47 WARN_ON(!RB_EMPTY_ROOT(
48 &transaction
->delayed_refs
.href_root
.rb_root
));
49 if (transaction
->delayed_refs
.pending_csums
)
50 btrfs_err(transaction
->fs_info
,
51 "pending csums is %llu",
52 transaction
->delayed_refs
.pending_csums
);
53 while (!list_empty(&transaction
->pending_chunks
)) {
54 struct extent_map
*em
;
56 em
= list_first_entry(&transaction
->pending_chunks
,
57 struct extent_map
, list
);
58 list_del_init(&em
->list
);
62 * If any block groups are found in ->deleted_bgs then it's
63 * because the transaction was aborted and a commit did not
64 * happen (things failed before writing the new superblock
65 * and calling btrfs_finish_extent_commit()), so we can not
66 * discard the physical locations of the block groups.
68 while (!list_empty(&transaction
->deleted_bgs
)) {
69 struct btrfs_block_group_cache
*cache
;
71 cache
= list_first_entry(&transaction
->deleted_bgs
,
72 struct btrfs_block_group_cache
,
74 list_del_init(&cache
->bg_list
);
75 btrfs_put_block_group_trimming(cache
);
76 btrfs_put_block_group(cache
);
82 static void clear_btree_io_tree(struct extent_io_tree
*tree
)
84 spin_lock(&tree
->lock
);
86 * Do a single barrier for the waitqueue_active check here, the state
87 * of the waitqueue should not change once clear_btree_io_tree is
91 while (!RB_EMPTY_ROOT(&tree
->state
)) {
93 struct extent_state
*state
;
95 node
= rb_first(&tree
->state
);
96 state
= rb_entry(node
, struct extent_state
, rb_node
);
97 rb_erase(&state
->rb_node
, &tree
->state
);
98 RB_CLEAR_NODE(&state
->rb_node
);
100 * btree io trees aren't supposed to have tasks waiting for
101 * changes in the flags of extent states ever.
103 ASSERT(!waitqueue_active(&state
->wq
));
104 free_extent_state(state
);
106 cond_resched_lock(&tree
->lock
);
108 spin_unlock(&tree
->lock
);
111 static noinline
void switch_commit_roots(struct btrfs_transaction
*trans
)
113 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
114 struct btrfs_root
*root
, *tmp
;
116 down_write(&fs_info
->commit_root_sem
);
117 list_for_each_entry_safe(root
, tmp
, &trans
->switch_commits
,
119 list_del_init(&root
->dirty_list
);
120 free_extent_buffer(root
->commit_root
);
121 root
->commit_root
= btrfs_root_node(root
);
122 if (is_fstree(root
->root_key
.objectid
))
123 btrfs_unpin_free_ino(root
);
124 clear_btree_io_tree(&root
->dirty_log_pages
);
127 /* We can free old roots now. */
128 spin_lock(&trans
->dropped_roots_lock
);
129 while (!list_empty(&trans
->dropped_roots
)) {
130 root
= list_first_entry(&trans
->dropped_roots
,
131 struct btrfs_root
, root_list
);
132 list_del_init(&root
->root_list
);
133 spin_unlock(&trans
->dropped_roots_lock
);
134 btrfs_drop_and_free_fs_root(fs_info
, root
);
135 spin_lock(&trans
->dropped_roots_lock
);
137 spin_unlock(&trans
->dropped_roots_lock
);
138 up_write(&fs_info
->commit_root_sem
);
141 static inline void extwriter_counter_inc(struct btrfs_transaction
*trans
,
144 if (type
& TRANS_EXTWRITERS
)
145 atomic_inc(&trans
->num_extwriters
);
148 static inline void extwriter_counter_dec(struct btrfs_transaction
*trans
,
151 if (type
& TRANS_EXTWRITERS
)
152 atomic_dec(&trans
->num_extwriters
);
155 static inline void extwriter_counter_init(struct btrfs_transaction
*trans
,
158 atomic_set(&trans
->num_extwriters
, ((type
& TRANS_EXTWRITERS
) ? 1 : 0));
161 static inline int extwriter_counter_read(struct btrfs_transaction
*trans
)
163 return atomic_read(&trans
->num_extwriters
);
167 * either allocate a new transaction or hop into the existing one
169 static noinline
int join_transaction(struct btrfs_fs_info
*fs_info
,
172 struct btrfs_transaction
*cur_trans
;
174 spin_lock(&fs_info
->trans_lock
);
176 /* The file system has been taken offline. No new transactions. */
177 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
)) {
178 spin_unlock(&fs_info
->trans_lock
);
182 cur_trans
= fs_info
->running_transaction
;
184 if (cur_trans
->aborted
) {
185 spin_unlock(&fs_info
->trans_lock
);
186 return cur_trans
->aborted
;
188 if (btrfs_blocked_trans_types
[cur_trans
->state
] & type
) {
189 spin_unlock(&fs_info
->trans_lock
);
192 refcount_inc(&cur_trans
->use_count
);
193 atomic_inc(&cur_trans
->num_writers
);
194 extwriter_counter_inc(cur_trans
, type
);
195 spin_unlock(&fs_info
->trans_lock
);
198 spin_unlock(&fs_info
->trans_lock
);
201 * If we are ATTACH, we just want to catch the current transaction,
202 * and commit it. If there is no transaction, just return ENOENT.
204 if (type
== TRANS_ATTACH
)
208 * JOIN_NOLOCK only happens during the transaction commit, so
209 * it is impossible that ->running_transaction is NULL
211 BUG_ON(type
== TRANS_JOIN_NOLOCK
);
213 cur_trans
= kmalloc(sizeof(*cur_trans
), GFP_NOFS
);
217 spin_lock(&fs_info
->trans_lock
);
218 if (fs_info
->running_transaction
) {
220 * someone started a transaction after we unlocked. Make sure
221 * to redo the checks above
225 } else if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
)) {
226 spin_unlock(&fs_info
->trans_lock
);
231 cur_trans
->fs_info
= fs_info
;
232 atomic_set(&cur_trans
->num_writers
, 1);
233 extwriter_counter_init(cur_trans
, type
);
234 init_waitqueue_head(&cur_trans
->writer_wait
);
235 init_waitqueue_head(&cur_trans
->commit_wait
);
236 cur_trans
->state
= TRANS_STATE_RUNNING
;
238 * One for this trans handle, one so it will live on until we
239 * commit the transaction.
241 refcount_set(&cur_trans
->use_count
, 2);
242 cur_trans
->flags
= 0;
243 cur_trans
->start_time
= ktime_get_seconds();
245 memset(&cur_trans
->delayed_refs
, 0, sizeof(cur_trans
->delayed_refs
));
247 cur_trans
->delayed_refs
.href_root
= RB_ROOT_CACHED
;
248 cur_trans
->delayed_refs
.dirty_extent_root
= RB_ROOT
;
249 atomic_set(&cur_trans
->delayed_refs
.num_entries
, 0);
252 * although the tree mod log is per file system and not per transaction,
253 * the log must never go across transaction boundaries.
256 if (!list_empty(&fs_info
->tree_mod_seq_list
))
257 WARN(1, KERN_ERR
"BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
258 if (!RB_EMPTY_ROOT(&fs_info
->tree_mod_log
))
259 WARN(1, KERN_ERR
"BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
260 atomic64_set(&fs_info
->tree_mod_seq
, 0);
262 spin_lock_init(&cur_trans
->delayed_refs
.lock
);
264 INIT_LIST_HEAD(&cur_trans
->pending_snapshots
);
265 INIT_LIST_HEAD(&cur_trans
->pending_chunks
);
266 INIT_LIST_HEAD(&cur_trans
->switch_commits
);
267 INIT_LIST_HEAD(&cur_trans
->dirty_bgs
);
268 INIT_LIST_HEAD(&cur_trans
->io_bgs
);
269 INIT_LIST_HEAD(&cur_trans
->dropped_roots
);
270 mutex_init(&cur_trans
->cache_write_mutex
);
271 cur_trans
->num_dirty_bgs
= 0;
272 spin_lock_init(&cur_trans
->dirty_bgs_lock
);
273 INIT_LIST_HEAD(&cur_trans
->deleted_bgs
);
274 spin_lock_init(&cur_trans
->dropped_roots_lock
);
275 list_add_tail(&cur_trans
->list
, &fs_info
->trans_list
);
276 extent_io_tree_init(&cur_trans
->dirty_pages
,
277 fs_info
->btree_inode
);
278 fs_info
->generation
++;
279 cur_trans
->transid
= fs_info
->generation
;
280 fs_info
->running_transaction
= cur_trans
;
281 cur_trans
->aborted
= 0;
282 spin_unlock(&fs_info
->trans_lock
);
288 * this does all the record keeping required to make sure that a reference
289 * counted root is properly recorded in a given transaction. This is required
290 * to make sure the old root from before we joined the transaction is deleted
291 * when the transaction commits
293 static int record_root_in_trans(struct btrfs_trans_handle
*trans
,
294 struct btrfs_root
*root
,
297 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
299 if ((test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
300 root
->last_trans
< trans
->transid
) || force
) {
301 WARN_ON(root
== fs_info
->extent_root
);
302 WARN_ON(!force
&& root
->commit_root
!= root
->node
);
305 * see below for IN_TRANS_SETUP usage rules
306 * we have the reloc mutex held now, so there
307 * is only one writer in this function
309 set_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
);
311 /* make sure readers find IN_TRANS_SETUP before
312 * they find our root->last_trans update
316 spin_lock(&fs_info
->fs_roots_radix_lock
);
317 if (root
->last_trans
== trans
->transid
&& !force
) {
318 spin_unlock(&fs_info
->fs_roots_radix_lock
);
321 radix_tree_tag_set(&fs_info
->fs_roots_radix
,
322 (unsigned long)root
->root_key
.objectid
,
323 BTRFS_ROOT_TRANS_TAG
);
324 spin_unlock(&fs_info
->fs_roots_radix_lock
);
325 root
->last_trans
= trans
->transid
;
327 /* this is pretty tricky. We don't want to
328 * take the relocation lock in btrfs_record_root_in_trans
329 * unless we're really doing the first setup for this root in
332 * Normally we'd use root->last_trans as a flag to decide
333 * if we want to take the expensive mutex.
335 * But, we have to set root->last_trans before we
336 * init the relocation root, otherwise, we trip over warnings
337 * in ctree.c. The solution used here is to flag ourselves
338 * with root IN_TRANS_SETUP. When this is 1, we're still
339 * fixing up the reloc trees and everyone must wait.
341 * When this is zero, they can trust root->last_trans and fly
342 * through btrfs_record_root_in_trans without having to take the
343 * lock. smp_wmb() makes sure that all the writes above are
344 * done before we pop in the zero below
346 btrfs_init_reloc_root(trans
, root
);
347 smp_mb__before_atomic();
348 clear_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
);
354 void btrfs_add_dropped_root(struct btrfs_trans_handle
*trans
,
355 struct btrfs_root
*root
)
357 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
358 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
360 /* Add ourselves to the transaction dropped list */
361 spin_lock(&cur_trans
->dropped_roots_lock
);
362 list_add_tail(&root
->root_list
, &cur_trans
->dropped_roots
);
363 spin_unlock(&cur_trans
->dropped_roots_lock
);
365 /* Make sure we don't try to update the root at commit time */
366 spin_lock(&fs_info
->fs_roots_radix_lock
);
367 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
368 (unsigned long)root
->root_key
.objectid
,
369 BTRFS_ROOT_TRANS_TAG
);
370 spin_unlock(&fs_info
->fs_roots_radix_lock
);
373 int btrfs_record_root_in_trans(struct btrfs_trans_handle
*trans
,
374 struct btrfs_root
*root
)
376 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
378 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
382 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
386 if (root
->last_trans
== trans
->transid
&&
387 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
))
390 mutex_lock(&fs_info
->reloc_mutex
);
391 record_root_in_trans(trans
, root
, 0);
392 mutex_unlock(&fs_info
->reloc_mutex
);
397 static inline int is_transaction_blocked(struct btrfs_transaction
*trans
)
399 return (trans
->state
>= TRANS_STATE_BLOCKED
&&
400 trans
->state
< TRANS_STATE_UNBLOCKED
&&
404 /* wait for commit against the current transaction to become unblocked
405 * when this is done, it is safe to start a new transaction, but the current
406 * transaction might not be fully on disk.
408 static void wait_current_trans(struct btrfs_fs_info
*fs_info
)
410 struct btrfs_transaction
*cur_trans
;
412 spin_lock(&fs_info
->trans_lock
);
413 cur_trans
= fs_info
->running_transaction
;
414 if (cur_trans
&& is_transaction_blocked(cur_trans
)) {
415 refcount_inc(&cur_trans
->use_count
);
416 spin_unlock(&fs_info
->trans_lock
);
418 wait_event(fs_info
->transaction_wait
,
419 cur_trans
->state
>= TRANS_STATE_UNBLOCKED
||
421 btrfs_put_transaction(cur_trans
);
423 spin_unlock(&fs_info
->trans_lock
);
427 static int may_wait_transaction(struct btrfs_fs_info
*fs_info
, int type
)
429 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
432 if (type
== TRANS_START
)
438 static inline bool need_reserve_reloc_root(struct btrfs_root
*root
)
440 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
442 if (!fs_info
->reloc_ctl
||
443 !test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
444 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
451 static struct btrfs_trans_handle
*
452 start_transaction(struct btrfs_root
*root
, unsigned int num_items
,
453 unsigned int type
, enum btrfs_reserve_flush_enum flush
,
454 bool enforce_qgroups
)
456 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
457 struct btrfs_block_rsv
*delayed_refs_rsv
= &fs_info
->delayed_refs_rsv
;
458 struct btrfs_trans_handle
*h
;
459 struct btrfs_transaction
*cur_trans
;
461 u64 qgroup_reserved
= 0;
462 bool reloc_reserved
= false;
465 /* Send isn't supposed to start transactions. */
466 ASSERT(current
->journal_info
!= BTRFS_SEND_TRANS_STUB
);
468 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
469 return ERR_PTR(-EROFS
);
471 if (current
->journal_info
) {
472 WARN_ON(type
& TRANS_EXTWRITERS
);
473 h
= current
->journal_info
;
474 refcount_inc(&h
->use_count
);
475 WARN_ON(refcount_read(&h
->use_count
) > 2);
476 h
->orig_rsv
= h
->block_rsv
;
482 * Do the reservation before we join the transaction so we can do all
483 * the appropriate flushing if need be.
485 if (num_items
&& root
!= fs_info
->chunk_root
) {
486 struct btrfs_block_rsv
*rsv
= &fs_info
->trans_block_rsv
;
487 u64 delayed_refs_bytes
= 0;
489 qgroup_reserved
= num_items
* fs_info
->nodesize
;
490 ret
= btrfs_qgroup_reserve_meta_pertrans(root
, qgroup_reserved
,
496 * We want to reserve all the bytes we may need all at once, so
497 * we only do 1 enospc flushing cycle per transaction start. We
498 * accomplish this by simply assuming we'll do 2 x num_items
499 * worth of delayed refs updates in this trans handle, and
500 * refill that amount for whatever is missing in the reserve.
502 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, num_items
);
503 if (delayed_refs_rsv
->full
== 0) {
504 delayed_refs_bytes
= num_bytes
;
509 * Do the reservation for the relocation root creation
511 if (need_reserve_reloc_root(root
)) {
512 num_bytes
+= fs_info
->nodesize
;
513 reloc_reserved
= true;
516 ret
= btrfs_block_rsv_add(root
, rsv
, num_bytes
, flush
);
519 if (delayed_refs_bytes
) {
520 btrfs_migrate_to_delayed_refs_rsv(fs_info
, rsv
,
522 num_bytes
-= delayed_refs_bytes
;
524 } else if (num_items
== 0 && flush
== BTRFS_RESERVE_FLUSH_ALL
&&
525 !delayed_refs_rsv
->full
) {
527 * Some people call with btrfs_start_transaction(root, 0)
528 * because they can be throttled, but have some other mechanism
529 * for reserving space. We still want these guys to refill the
530 * delayed block_rsv so just add 1 items worth of reservation
533 ret
= btrfs_delayed_refs_rsv_refill(fs_info
, flush
);
538 h
= kmem_cache_zalloc(btrfs_trans_handle_cachep
, GFP_NOFS
);
545 * If we are JOIN_NOLOCK we're already committing a transaction and
546 * waiting on this guy, so we don't need to do the sb_start_intwrite
547 * because we're already holding a ref. We need this because we could
548 * have raced in and did an fsync() on a file which can kick a commit
549 * and then we deadlock with somebody doing a freeze.
551 * If we are ATTACH, it means we just want to catch the current
552 * transaction and commit it, so we needn't do sb_start_intwrite().
554 if (type
& __TRANS_FREEZABLE
)
555 sb_start_intwrite(fs_info
->sb
);
557 if (may_wait_transaction(fs_info
, type
))
558 wait_current_trans(fs_info
);
561 ret
= join_transaction(fs_info
, type
);
563 wait_current_trans(fs_info
);
564 if (unlikely(type
== TRANS_ATTACH
))
567 } while (ret
== -EBUSY
);
572 cur_trans
= fs_info
->running_transaction
;
574 h
->transid
= cur_trans
->transid
;
575 h
->transaction
= cur_trans
;
577 refcount_set(&h
->use_count
, 1);
578 h
->fs_info
= root
->fs_info
;
581 h
->can_flush_pending_bgs
= true;
582 INIT_LIST_HEAD(&h
->new_bgs
);
585 if (cur_trans
->state
>= TRANS_STATE_BLOCKED
&&
586 may_wait_transaction(fs_info
, type
)) {
587 current
->journal_info
= h
;
588 btrfs_commit_transaction(h
);
593 trace_btrfs_space_reservation(fs_info
, "transaction",
594 h
->transid
, num_bytes
, 1);
595 h
->block_rsv
= &fs_info
->trans_block_rsv
;
596 h
->bytes_reserved
= num_bytes
;
597 h
->reloc_reserved
= reloc_reserved
;
601 btrfs_record_root_in_trans(h
, root
);
603 if (!current
->journal_info
)
604 current
->journal_info
= h
;
608 if (type
& __TRANS_FREEZABLE
)
609 sb_end_intwrite(fs_info
->sb
);
610 kmem_cache_free(btrfs_trans_handle_cachep
, h
);
613 btrfs_block_rsv_release(fs_info
, &fs_info
->trans_block_rsv
,
616 btrfs_qgroup_free_meta_pertrans(root
, qgroup_reserved
);
620 struct btrfs_trans_handle
*btrfs_start_transaction(struct btrfs_root
*root
,
621 unsigned int num_items
)
623 return start_transaction(root
, num_items
, TRANS_START
,
624 BTRFS_RESERVE_FLUSH_ALL
, true);
627 struct btrfs_trans_handle
*btrfs_start_transaction_fallback_global_rsv(
628 struct btrfs_root
*root
,
629 unsigned int num_items
,
632 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
633 struct btrfs_trans_handle
*trans
;
638 * We have two callers: unlink and block group removal. The
639 * former should succeed even if we will temporarily exceed
640 * quota and the latter operates on the extent root so
641 * qgroup enforcement is ignored anyway.
643 trans
= start_transaction(root
, num_items
, TRANS_START
,
644 BTRFS_RESERVE_FLUSH_ALL
, false);
645 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
648 trans
= btrfs_start_transaction(root
, 0);
652 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, num_items
);
653 ret
= btrfs_cond_migrate_bytes(fs_info
, &fs_info
->trans_block_rsv
,
654 num_bytes
, min_factor
);
656 btrfs_end_transaction(trans
);
660 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
661 trans
->bytes_reserved
= num_bytes
;
662 trace_btrfs_space_reservation(fs_info
, "transaction",
663 trans
->transid
, num_bytes
, 1);
668 struct btrfs_trans_handle
*btrfs_join_transaction(struct btrfs_root
*root
)
670 return start_transaction(root
, 0, TRANS_JOIN
, BTRFS_RESERVE_NO_FLUSH
,
674 struct btrfs_trans_handle
*btrfs_join_transaction_nolock(struct btrfs_root
*root
)
676 return start_transaction(root
, 0, TRANS_JOIN_NOLOCK
,
677 BTRFS_RESERVE_NO_FLUSH
, true);
681 * btrfs_attach_transaction() - catch the running transaction
683 * It is used when we want to commit the current the transaction, but
684 * don't want to start a new one.
686 * Note: If this function return -ENOENT, it just means there is no
687 * running transaction. But it is possible that the inactive transaction
688 * is still in the memory, not fully on disk. If you hope there is no
689 * inactive transaction in the fs when -ENOENT is returned, you should
691 * btrfs_attach_transaction_barrier()
693 struct btrfs_trans_handle
*btrfs_attach_transaction(struct btrfs_root
*root
)
695 return start_transaction(root
, 0, TRANS_ATTACH
,
696 BTRFS_RESERVE_NO_FLUSH
, true);
700 * btrfs_attach_transaction_barrier() - catch the running transaction
702 * It is similar to the above function, the difference is this one
703 * will wait for all the inactive transactions until they fully
706 struct btrfs_trans_handle
*
707 btrfs_attach_transaction_barrier(struct btrfs_root
*root
)
709 struct btrfs_trans_handle
*trans
;
711 trans
= start_transaction(root
, 0, TRANS_ATTACH
,
712 BTRFS_RESERVE_NO_FLUSH
, true);
713 if (trans
== ERR_PTR(-ENOENT
))
714 btrfs_wait_for_commit(root
->fs_info
, 0);
719 /* wait for a transaction commit to be fully complete */
720 static noinline
void wait_for_commit(struct btrfs_transaction
*commit
)
722 wait_event(commit
->commit_wait
, commit
->state
== TRANS_STATE_COMPLETED
);
725 int btrfs_wait_for_commit(struct btrfs_fs_info
*fs_info
, u64 transid
)
727 struct btrfs_transaction
*cur_trans
= NULL
, *t
;
731 if (transid
<= fs_info
->last_trans_committed
)
734 /* find specified transaction */
735 spin_lock(&fs_info
->trans_lock
);
736 list_for_each_entry(t
, &fs_info
->trans_list
, list
) {
737 if (t
->transid
== transid
) {
739 refcount_inc(&cur_trans
->use_count
);
743 if (t
->transid
> transid
) {
748 spin_unlock(&fs_info
->trans_lock
);
751 * The specified transaction doesn't exist, or we
752 * raced with btrfs_commit_transaction
755 if (transid
> fs_info
->last_trans_committed
)
760 /* find newest transaction that is committing | committed */
761 spin_lock(&fs_info
->trans_lock
);
762 list_for_each_entry_reverse(t
, &fs_info
->trans_list
,
764 if (t
->state
>= TRANS_STATE_COMMIT_START
) {
765 if (t
->state
== TRANS_STATE_COMPLETED
)
768 refcount_inc(&cur_trans
->use_count
);
772 spin_unlock(&fs_info
->trans_lock
);
774 goto out
; /* nothing committing|committed */
777 wait_for_commit(cur_trans
);
778 btrfs_put_transaction(cur_trans
);
783 void btrfs_throttle(struct btrfs_fs_info
*fs_info
)
785 wait_current_trans(fs_info
);
788 static int should_end_transaction(struct btrfs_trans_handle
*trans
)
790 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
792 if (btrfs_check_space_for_delayed_refs(fs_info
))
795 return !!btrfs_block_rsv_check(&fs_info
->global_block_rsv
, 5);
798 int btrfs_should_end_transaction(struct btrfs_trans_handle
*trans
)
800 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
803 if (cur_trans
->state
>= TRANS_STATE_BLOCKED
||
804 cur_trans
->delayed_refs
.flushing
)
807 return should_end_transaction(trans
);
810 static void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
)
813 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
815 if (!trans
->block_rsv
) {
816 ASSERT(!trans
->bytes_reserved
);
820 if (!trans
->bytes_reserved
)
823 ASSERT(trans
->block_rsv
== &fs_info
->trans_block_rsv
);
824 trace_btrfs_space_reservation(fs_info
, "transaction",
825 trans
->transid
, trans
->bytes_reserved
, 0);
826 btrfs_block_rsv_release(fs_info
, trans
->block_rsv
,
827 trans
->bytes_reserved
);
828 trans
->bytes_reserved
= 0;
831 static int __btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
834 struct btrfs_fs_info
*info
= trans
->fs_info
;
835 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
836 int lock
= (trans
->type
!= TRANS_JOIN_NOLOCK
);
839 if (refcount_read(&trans
->use_count
) > 1) {
840 refcount_dec(&trans
->use_count
);
841 trans
->block_rsv
= trans
->orig_rsv
;
845 btrfs_trans_release_metadata(trans
);
846 trans
->block_rsv
= NULL
;
848 if (!list_empty(&trans
->new_bgs
))
849 btrfs_create_pending_block_groups(trans
);
851 btrfs_trans_release_chunk_metadata(trans
);
853 if (lock
&& should_end_transaction(trans
) &&
854 READ_ONCE(cur_trans
->state
) == TRANS_STATE_RUNNING
) {
855 spin_lock(&info
->trans_lock
);
856 if (cur_trans
->state
== TRANS_STATE_RUNNING
)
857 cur_trans
->state
= TRANS_STATE_BLOCKED
;
858 spin_unlock(&info
->trans_lock
);
861 if (lock
&& READ_ONCE(cur_trans
->state
) == TRANS_STATE_BLOCKED
) {
863 return btrfs_commit_transaction(trans
);
865 wake_up_process(info
->transaction_kthread
);
868 if (trans
->type
& __TRANS_FREEZABLE
)
869 sb_end_intwrite(info
->sb
);
871 WARN_ON(cur_trans
!= info
->running_transaction
);
872 WARN_ON(atomic_read(&cur_trans
->num_writers
) < 1);
873 atomic_dec(&cur_trans
->num_writers
);
874 extwriter_counter_dec(cur_trans
, trans
->type
);
876 cond_wake_up(&cur_trans
->writer_wait
);
877 btrfs_put_transaction(cur_trans
);
879 if (current
->journal_info
== trans
)
880 current
->journal_info
= NULL
;
883 btrfs_run_delayed_iputs(info
);
885 if (trans
->aborted
||
886 test_bit(BTRFS_FS_STATE_ERROR
, &info
->fs_state
)) {
887 wake_up_process(info
->transaction_kthread
);
891 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
895 int btrfs_end_transaction(struct btrfs_trans_handle
*trans
)
897 return __btrfs_end_transaction(trans
, 0);
900 int btrfs_end_transaction_throttle(struct btrfs_trans_handle
*trans
)
902 return __btrfs_end_transaction(trans
, 1);
906 * when btree blocks are allocated, they have some corresponding bits set for
907 * them in one of two extent_io trees. This is used to make sure all of
908 * those extents are sent to disk but does not wait on them
910 int btrfs_write_marked_extents(struct btrfs_fs_info
*fs_info
,
911 struct extent_io_tree
*dirty_pages
, int mark
)
915 struct address_space
*mapping
= fs_info
->btree_inode
->i_mapping
;
916 struct extent_state
*cached_state
= NULL
;
920 atomic_inc(&BTRFS_I(fs_info
->btree_inode
)->sync_writers
);
921 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
922 mark
, &cached_state
)) {
923 bool wait_writeback
= false;
925 err
= convert_extent_bit(dirty_pages
, start
, end
,
927 mark
, &cached_state
);
929 * convert_extent_bit can return -ENOMEM, which is most of the
930 * time a temporary error. So when it happens, ignore the error
931 * and wait for writeback of this range to finish - because we
932 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
933 * to __btrfs_wait_marked_extents() would not know that
934 * writeback for this range started and therefore wouldn't
935 * wait for it to finish - we don't want to commit a
936 * superblock that points to btree nodes/leafs for which
937 * writeback hasn't finished yet (and without errors).
938 * We cleanup any entries left in the io tree when committing
939 * the transaction (through clear_btree_io_tree()).
941 if (err
== -ENOMEM
) {
943 wait_writeback
= true;
946 err
= filemap_fdatawrite_range(mapping
, start
, end
);
949 else if (wait_writeback
)
950 werr
= filemap_fdatawait_range(mapping
, start
, end
);
951 free_extent_state(cached_state
);
956 atomic_dec(&BTRFS_I(fs_info
->btree_inode
)->sync_writers
);
961 * when btree blocks are allocated, they have some corresponding bits set for
962 * them in one of two extent_io trees. This is used to make sure all of
963 * those extents are on disk for transaction or log commit. We wait
964 * on all the pages and clear them from the dirty pages state tree
966 static int __btrfs_wait_marked_extents(struct btrfs_fs_info
*fs_info
,
967 struct extent_io_tree
*dirty_pages
)
971 struct address_space
*mapping
= fs_info
->btree_inode
->i_mapping
;
972 struct extent_state
*cached_state
= NULL
;
976 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
977 EXTENT_NEED_WAIT
, &cached_state
)) {
979 * Ignore -ENOMEM errors returned by clear_extent_bit().
980 * When committing the transaction, we'll remove any entries
981 * left in the io tree. For a log commit, we don't remove them
982 * after committing the log because the tree can be accessed
983 * concurrently - we do it only at transaction commit time when
984 * it's safe to do it (through clear_btree_io_tree()).
986 err
= clear_extent_bit(dirty_pages
, start
, end
,
987 EXTENT_NEED_WAIT
, 0, 0, &cached_state
);
991 err
= filemap_fdatawait_range(mapping
, start
, end
);
994 free_extent_state(cached_state
);
1004 int btrfs_wait_extents(struct btrfs_fs_info
*fs_info
,
1005 struct extent_io_tree
*dirty_pages
)
1007 bool errors
= false;
1010 err
= __btrfs_wait_marked_extents(fs_info
, dirty_pages
);
1011 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR
, &fs_info
->flags
))
1019 int btrfs_wait_tree_log_extents(struct btrfs_root
*log_root
, int mark
)
1021 struct btrfs_fs_info
*fs_info
= log_root
->fs_info
;
1022 struct extent_io_tree
*dirty_pages
= &log_root
->dirty_log_pages
;
1023 bool errors
= false;
1026 ASSERT(log_root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
);
1028 err
= __btrfs_wait_marked_extents(fs_info
, dirty_pages
);
1029 if ((mark
& EXTENT_DIRTY
) &&
1030 test_and_clear_bit(BTRFS_FS_LOG1_ERR
, &fs_info
->flags
))
1033 if ((mark
& EXTENT_NEW
) &&
1034 test_and_clear_bit(BTRFS_FS_LOG2_ERR
, &fs_info
->flags
))
1043 * When btree blocks are allocated the corresponding extents are marked dirty.
1044 * This function ensures such extents are persisted on disk for transaction or
1047 * @trans: transaction whose dirty pages we'd like to write
1049 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle
*trans
)
1053 struct extent_io_tree
*dirty_pages
= &trans
->transaction
->dirty_pages
;
1054 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1055 struct blk_plug plug
;
1057 blk_start_plug(&plug
);
1058 ret
= btrfs_write_marked_extents(fs_info
, dirty_pages
, EXTENT_DIRTY
);
1059 blk_finish_plug(&plug
);
1060 ret2
= btrfs_wait_extents(fs_info
, dirty_pages
);
1062 clear_btree_io_tree(&trans
->transaction
->dirty_pages
);
1073 * this is used to update the root pointer in the tree of tree roots.
1075 * But, in the case of the extent allocation tree, updating the root
1076 * pointer may allocate blocks which may change the root of the extent
1079 * So, this loops and repeats and makes sure the cowonly root didn't
1080 * change while the root pointer was being updated in the metadata.
1082 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
1083 struct btrfs_root
*root
)
1086 u64 old_root_bytenr
;
1088 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1089 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1091 old_root_used
= btrfs_root_used(&root
->root_item
);
1094 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
1095 if (old_root_bytenr
== root
->node
->start
&&
1096 old_root_used
== btrfs_root_used(&root
->root_item
))
1099 btrfs_set_root_node(&root
->root_item
, root
->node
);
1100 ret
= btrfs_update_root(trans
, tree_root
,
1106 old_root_used
= btrfs_root_used(&root
->root_item
);
1113 * update all the cowonly tree roots on disk
1115 * The error handling in this function may not be obvious. Any of the
1116 * failures will cause the file system to go offline. We still need
1117 * to clean up the delayed refs.
1119 static noinline
int commit_cowonly_roots(struct btrfs_trans_handle
*trans
)
1121 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1122 struct list_head
*dirty_bgs
= &trans
->transaction
->dirty_bgs
;
1123 struct list_head
*io_bgs
= &trans
->transaction
->io_bgs
;
1124 struct list_head
*next
;
1125 struct extent_buffer
*eb
;
1128 eb
= btrfs_lock_root_node(fs_info
->tree_root
);
1129 ret
= btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
,
1131 btrfs_tree_unlock(eb
);
1132 free_extent_buffer(eb
);
1137 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1141 ret
= btrfs_run_dev_stats(trans
, fs_info
);
1144 ret
= btrfs_run_dev_replace(trans
, fs_info
);
1147 ret
= btrfs_run_qgroups(trans
);
1151 ret
= btrfs_setup_space_cache(trans
, fs_info
);
1155 /* run_qgroups might have added some more refs */
1156 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1160 while (!list_empty(&fs_info
->dirty_cowonly_roots
)) {
1161 struct btrfs_root
*root
;
1162 next
= fs_info
->dirty_cowonly_roots
.next
;
1163 list_del_init(next
);
1164 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
1165 clear_bit(BTRFS_ROOT_DIRTY
, &root
->state
);
1167 if (root
!= fs_info
->extent_root
)
1168 list_add_tail(&root
->dirty_list
,
1169 &trans
->transaction
->switch_commits
);
1170 ret
= update_cowonly_root(trans
, root
);
1173 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1178 while (!list_empty(dirty_bgs
) || !list_empty(io_bgs
)) {
1179 ret
= btrfs_write_dirty_block_groups(trans
, fs_info
);
1182 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1187 if (!list_empty(&fs_info
->dirty_cowonly_roots
))
1190 list_add_tail(&fs_info
->extent_root
->dirty_list
,
1191 &trans
->transaction
->switch_commits
);
1193 /* Update dev-replace pointer once everything is committed */
1194 fs_info
->dev_replace
.committed_cursor_left
=
1195 fs_info
->dev_replace
.cursor_left_last_write_of_item
;
1201 * dead roots are old snapshots that need to be deleted. This allocates
1202 * a dirty root struct and adds it into the list of dead roots that need to
1205 void btrfs_add_dead_root(struct btrfs_root
*root
)
1207 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1209 spin_lock(&fs_info
->trans_lock
);
1210 if (list_empty(&root
->root_list
))
1211 list_add_tail(&root
->root_list
, &fs_info
->dead_roots
);
1212 spin_unlock(&fs_info
->trans_lock
);
1216 * update all the cowonly tree roots on disk
1218 static noinline
int commit_fs_roots(struct btrfs_trans_handle
*trans
)
1220 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1221 struct btrfs_root
*gang
[8];
1226 spin_lock(&fs_info
->fs_roots_radix_lock
);
1228 ret
= radix_tree_gang_lookup_tag(&fs_info
->fs_roots_radix
,
1231 BTRFS_ROOT_TRANS_TAG
);
1234 for (i
= 0; i
< ret
; i
++) {
1235 struct btrfs_root
*root
= gang
[i
];
1236 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
1237 (unsigned long)root
->root_key
.objectid
,
1238 BTRFS_ROOT_TRANS_TAG
);
1239 spin_unlock(&fs_info
->fs_roots_radix_lock
);
1241 btrfs_free_log(trans
, root
);
1242 btrfs_update_reloc_root(trans
, root
);
1244 btrfs_save_ino_cache(root
, trans
);
1246 /* see comments in should_cow_block() */
1247 clear_bit(BTRFS_ROOT_FORCE_COW
, &root
->state
);
1248 smp_mb__after_atomic();
1250 if (root
->commit_root
!= root
->node
) {
1251 list_add_tail(&root
->dirty_list
,
1252 &trans
->transaction
->switch_commits
);
1253 btrfs_set_root_node(&root
->root_item
,
1257 err
= btrfs_update_root(trans
, fs_info
->tree_root
,
1260 spin_lock(&fs_info
->fs_roots_radix_lock
);
1263 btrfs_qgroup_free_meta_all_pertrans(root
);
1266 spin_unlock(&fs_info
->fs_roots_radix_lock
);
1271 * defrag a given btree.
1272 * Every leaf in the btree is read and defragged.
1274 int btrfs_defrag_root(struct btrfs_root
*root
)
1276 struct btrfs_fs_info
*info
= root
->fs_info
;
1277 struct btrfs_trans_handle
*trans
;
1280 if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING
, &root
->state
))
1284 trans
= btrfs_start_transaction(root
, 0);
1286 return PTR_ERR(trans
);
1288 ret
= btrfs_defrag_leaves(trans
, root
);
1290 btrfs_end_transaction(trans
);
1291 btrfs_btree_balance_dirty(info
);
1294 if (btrfs_fs_closing(info
) || ret
!= -EAGAIN
)
1297 if (btrfs_defrag_cancelled(info
)) {
1298 btrfs_debug(info
, "defrag_root cancelled");
1303 clear_bit(BTRFS_ROOT_DEFRAG_RUNNING
, &root
->state
);
1308 * Do all special snapshot related qgroup dirty hack.
1310 * Will do all needed qgroup inherit and dirty hack like switch commit
1311 * roots inside one transaction and write all btree into disk, to make
1314 static int qgroup_account_snapshot(struct btrfs_trans_handle
*trans
,
1315 struct btrfs_root
*src
,
1316 struct btrfs_root
*parent
,
1317 struct btrfs_qgroup_inherit
*inherit
,
1320 struct btrfs_fs_info
*fs_info
= src
->fs_info
;
1324 * Save some performance in the case that qgroups are not
1325 * enabled. If this check races with the ioctl, rescan will
1328 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
1332 * Ensure dirty @src will be committed. Or, after coming
1333 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1334 * recorded root will never be updated again, causing an outdated root
1337 record_root_in_trans(trans
, src
, 1);
1340 * We are going to commit transaction, see btrfs_commit_transaction()
1341 * comment for reason locking tree_log_mutex
1343 mutex_lock(&fs_info
->tree_log_mutex
);
1345 ret
= commit_fs_roots(trans
);
1348 ret
= btrfs_qgroup_account_extents(trans
);
1352 /* Now qgroup are all updated, we can inherit it to new qgroups */
1353 ret
= btrfs_qgroup_inherit(trans
, src
->root_key
.objectid
, dst_objectid
,
1359 * Now we do a simplified commit transaction, which will:
1360 * 1) commit all subvolume and extent tree
1361 * To ensure all subvolume and extent tree have a valid
1362 * commit_root to accounting later insert_dir_item()
1363 * 2) write all btree blocks onto disk
1364 * This is to make sure later btree modification will be cowed
1365 * Or commit_root can be populated and cause wrong qgroup numbers
1366 * In this simplified commit, we don't really care about other trees
1367 * like chunk and root tree, as they won't affect qgroup.
1368 * And we don't write super to avoid half committed status.
1370 ret
= commit_cowonly_roots(trans
);
1373 switch_commit_roots(trans
->transaction
);
1374 ret
= btrfs_write_and_wait_transaction(trans
);
1376 btrfs_handle_fs_error(fs_info
, ret
,
1377 "Error while writing out transaction for qgroup");
1380 mutex_unlock(&fs_info
->tree_log_mutex
);
1383 * Force parent root to be updated, as we recorded it before so its
1384 * last_trans == cur_transid.
1385 * Or it won't be committed again onto disk after later
1389 record_root_in_trans(trans
, parent
, 1);
1394 * new snapshots need to be created at a very specific time in the
1395 * transaction commit. This does the actual creation.
1398 * If the error which may affect the commitment of the current transaction
1399 * happens, we should return the error number. If the error which just affect
1400 * the creation of the pending snapshots, just return 0.
1402 static noinline
int create_pending_snapshot(struct btrfs_trans_handle
*trans
,
1403 struct btrfs_pending_snapshot
*pending
)
1406 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1407 struct btrfs_key key
;
1408 struct btrfs_root_item
*new_root_item
;
1409 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1410 struct btrfs_root
*root
= pending
->root
;
1411 struct btrfs_root
*parent_root
;
1412 struct btrfs_block_rsv
*rsv
;
1413 struct inode
*parent_inode
;
1414 struct btrfs_path
*path
;
1415 struct btrfs_dir_item
*dir_item
;
1416 struct dentry
*dentry
;
1417 struct extent_buffer
*tmp
;
1418 struct extent_buffer
*old
;
1419 struct timespec64 cur_time
;
1427 ASSERT(pending
->path
);
1428 path
= pending
->path
;
1430 ASSERT(pending
->root_item
);
1431 new_root_item
= pending
->root_item
;
1433 pending
->error
= btrfs_find_free_objectid(tree_root
, &objectid
);
1435 goto no_free_objectid
;
1438 * Make qgroup to skip current new snapshot's qgroupid, as it is
1439 * accounted by later btrfs_qgroup_inherit().
1441 btrfs_set_skip_qgroup(trans
, objectid
);
1443 btrfs_reloc_pre_snapshot(pending
, &to_reserve
);
1445 if (to_reserve
> 0) {
1446 pending
->error
= btrfs_block_rsv_add(root
,
1447 &pending
->block_rsv
,
1449 BTRFS_RESERVE_NO_FLUSH
);
1451 goto clear_skip_qgroup
;
1454 key
.objectid
= objectid
;
1455 key
.offset
= (u64
)-1;
1456 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1458 rsv
= trans
->block_rsv
;
1459 trans
->block_rsv
= &pending
->block_rsv
;
1460 trans
->bytes_reserved
= trans
->block_rsv
->reserved
;
1461 trace_btrfs_space_reservation(fs_info
, "transaction",
1463 trans
->bytes_reserved
, 1);
1464 dentry
= pending
->dentry
;
1465 parent_inode
= pending
->dir
;
1466 parent_root
= BTRFS_I(parent_inode
)->root
;
1467 record_root_in_trans(trans
, parent_root
, 0);
1469 cur_time
= current_time(parent_inode
);
1472 * insert the directory item
1474 ret
= btrfs_set_inode_index(BTRFS_I(parent_inode
), &index
);
1475 BUG_ON(ret
); /* -ENOMEM */
1477 /* check if there is a file/dir which has the same name. */
1478 dir_item
= btrfs_lookup_dir_item(NULL
, parent_root
, path
,
1479 btrfs_ino(BTRFS_I(parent_inode
)),
1480 dentry
->d_name
.name
,
1481 dentry
->d_name
.len
, 0);
1482 if (dir_item
!= NULL
&& !IS_ERR(dir_item
)) {
1483 pending
->error
= -EEXIST
;
1484 goto dir_item_existed
;
1485 } else if (IS_ERR(dir_item
)) {
1486 ret
= PTR_ERR(dir_item
);
1487 btrfs_abort_transaction(trans
, ret
);
1490 btrfs_release_path(path
);
1493 * pull in the delayed directory update
1494 * and the delayed inode item
1495 * otherwise we corrupt the FS during
1498 ret
= btrfs_run_delayed_items(trans
);
1499 if (ret
) { /* Transaction aborted */
1500 btrfs_abort_transaction(trans
, ret
);
1504 record_root_in_trans(trans
, root
, 0);
1505 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
);
1506 memcpy(new_root_item
, &root
->root_item
, sizeof(*new_root_item
));
1507 btrfs_check_and_init_root_item(new_root_item
);
1509 root_flags
= btrfs_root_flags(new_root_item
);
1510 if (pending
->readonly
)
1511 root_flags
|= BTRFS_ROOT_SUBVOL_RDONLY
;
1513 root_flags
&= ~BTRFS_ROOT_SUBVOL_RDONLY
;
1514 btrfs_set_root_flags(new_root_item
, root_flags
);
1516 btrfs_set_root_generation_v2(new_root_item
,
1518 uuid_le_gen(&new_uuid
);
1519 memcpy(new_root_item
->uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
1520 memcpy(new_root_item
->parent_uuid
, root
->root_item
.uuid
,
1522 if (!(root_flags
& BTRFS_ROOT_SUBVOL_RDONLY
)) {
1523 memset(new_root_item
->received_uuid
, 0,
1524 sizeof(new_root_item
->received_uuid
));
1525 memset(&new_root_item
->stime
, 0, sizeof(new_root_item
->stime
));
1526 memset(&new_root_item
->rtime
, 0, sizeof(new_root_item
->rtime
));
1527 btrfs_set_root_stransid(new_root_item
, 0);
1528 btrfs_set_root_rtransid(new_root_item
, 0);
1530 btrfs_set_stack_timespec_sec(&new_root_item
->otime
, cur_time
.tv_sec
);
1531 btrfs_set_stack_timespec_nsec(&new_root_item
->otime
, cur_time
.tv_nsec
);
1532 btrfs_set_root_otransid(new_root_item
, trans
->transid
);
1534 old
= btrfs_lock_root_node(root
);
1535 ret
= btrfs_cow_block(trans
, root
, old
, NULL
, 0, &old
);
1537 btrfs_tree_unlock(old
);
1538 free_extent_buffer(old
);
1539 btrfs_abort_transaction(trans
, ret
);
1543 btrfs_set_lock_blocking(old
);
1545 ret
= btrfs_copy_root(trans
, root
, old
, &tmp
, objectid
);
1546 /* clean up in any case */
1547 btrfs_tree_unlock(old
);
1548 free_extent_buffer(old
);
1550 btrfs_abort_transaction(trans
, ret
);
1553 /* see comments in should_cow_block() */
1554 set_bit(BTRFS_ROOT_FORCE_COW
, &root
->state
);
1557 btrfs_set_root_node(new_root_item
, tmp
);
1558 /* record when the snapshot was created in key.offset */
1559 key
.offset
= trans
->transid
;
1560 ret
= btrfs_insert_root(trans
, tree_root
, &key
, new_root_item
);
1561 btrfs_tree_unlock(tmp
);
1562 free_extent_buffer(tmp
);
1564 btrfs_abort_transaction(trans
, ret
);
1569 * insert root back/forward references
1571 ret
= btrfs_add_root_ref(trans
, objectid
,
1572 parent_root
->root_key
.objectid
,
1573 btrfs_ino(BTRFS_I(parent_inode
)), index
,
1574 dentry
->d_name
.name
, dentry
->d_name
.len
);
1576 btrfs_abort_transaction(trans
, ret
);
1580 key
.offset
= (u64
)-1;
1581 pending
->snap
= btrfs_read_fs_root_no_name(fs_info
, &key
);
1582 if (IS_ERR(pending
->snap
)) {
1583 ret
= PTR_ERR(pending
->snap
);
1584 btrfs_abort_transaction(trans
, ret
);
1588 ret
= btrfs_reloc_post_snapshot(trans
, pending
);
1590 btrfs_abort_transaction(trans
, ret
);
1594 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1596 btrfs_abort_transaction(trans
, ret
);
1601 * Do special qgroup accounting for snapshot, as we do some qgroup
1602 * snapshot hack to do fast snapshot.
1603 * To co-operate with that hack, we do hack again.
1604 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1606 ret
= qgroup_account_snapshot(trans
, root
, parent_root
,
1607 pending
->inherit
, objectid
);
1611 ret
= btrfs_insert_dir_item(trans
, dentry
->d_name
.name
,
1612 dentry
->d_name
.len
, BTRFS_I(parent_inode
),
1613 &key
, BTRFS_FT_DIR
, index
);
1614 /* We have check then name at the beginning, so it is impossible. */
1615 BUG_ON(ret
== -EEXIST
|| ret
== -EOVERFLOW
);
1617 btrfs_abort_transaction(trans
, ret
);
1621 btrfs_i_size_write(BTRFS_I(parent_inode
), parent_inode
->i_size
+
1622 dentry
->d_name
.len
* 2);
1623 parent_inode
->i_mtime
= parent_inode
->i_ctime
=
1624 current_time(parent_inode
);
1625 ret
= btrfs_update_inode_fallback(trans
, parent_root
, parent_inode
);
1627 btrfs_abort_transaction(trans
, ret
);
1630 ret
= btrfs_uuid_tree_add(trans
, new_uuid
.b
, BTRFS_UUID_KEY_SUBVOL
,
1633 btrfs_abort_transaction(trans
, ret
);
1636 if (!btrfs_is_empty_uuid(new_root_item
->received_uuid
)) {
1637 ret
= btrfs_uuid_tree_add(trans
, new_root_item
->received_uuid
,
1638 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
1640 if (ret
&& ret
!= -EEXIST
) {
1641 btrfs_abort_transaction(trans
, ret
);
1646 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1648 btrfs_abort_transaction(trans
, ret
);
1653 pending
->error
= ret
;
1655 trans
->block_rsv
= rsv
;
1656 trans
->bytes_reserved
= 0;
1658 btrfs_clear_skip_qgroup(trans
);
1660 kfree(new_root_item
);
1661 pending
->root_item
= NULL
;
1662 btrfs_free_path(path
);
1663 pending
->path
= NULL
;
1669 * create all the snapshots we've scheduled for creation
1671 static noinline
int create_pending_snapshots(struct btrfs_trans_handle
*trans
)
1673 struct btrfs_pending_snapshot
*pending
, *next
;
1674 struct list_head
*head
= &trans
->transaction
->pending_snapshots
;
1677 list_for_each_entry_safe(pending
, next
, head
, list
) {
1678 list_del(&pending
->list
);
1679 ret
= create_pending_snapshot(trans
, pending
);
1686 static void update_super_roots(struct btrfs_fs_info
*fs_info
)
1688 struct btrfs_root_item
*root_item
;
1689 struct btrfs_super_block
*super
;
1691 super
= fs_info
->super_copy
;
1693 root_item
= &fs_info
->chunk_root
->root_item
;
1694 super
->chunk_root
= root_item
->bytenr
;
1695 super
->chunk_root_generation
= root_item
->generation
;
1696 super
->chunk_root_level
= root_item
->level
;
1698 root_item
= &fs_info
->tree_root
->root_item
;
1699 super
->root
= root_item
->bytenr
;
1700 super
->generation
= root_item
->generation
;
1701 super
->root_level
= root_item
->level
;
1702 if (btrfs_test_opt(fs_info
, SPACE_CACHE
))
1703 super
->cache_generation
= root_item
->generation
;
1704 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
))
1705 super
->uuid_tree_generation
= root_item
->generation
;
1708 int btrfs_transaction_in_commit(struct btrfs_fs_info
*info
)
1710 struct btrfs_transaction
*trans
;
1713 spin_lock(&info
->trans_lock
);
1714 trans
= info
->running_transaction
;
1716 ret
= (trans
->state
>= TRANS_STATE_COMMIT_START
);
1717 spin_unlock(&info
->trans_lock
);
1721 int btrfs_transaction_blocked(struct btrfs_fs_info
*info
)
1723 struct btrfs_transaction
*trans
;
1726 spin_lock(&info
->trans_lock
);
1727 trans
= info
->running_transaction
;
1729 ret
= is_transaction_blocked(trans
);
1730 spin_unlock(&info
->trans_lock
);
1735 * wait for the current transaction commit to start and block subsequent
1738 static void wait_current_trans_commit_start(struct btrfs_fs_info
*fs_info
,
1739 struct btrfs_transaction
*trans
)
1741 wait_event(fs_info
->transaction_blocked_wait
,
1742 trans
->state
>= TRANS_STATE_COMMIT_START
|| trans
->aborted
);
1746 * wait for the current transaction to start and then become unblocked.
1749 static void wait_current_trans_commit_start_and_unblock(
1750 struct btrfs_fs_info
*fs_info
,
1751 struct btrfs_transaction
*trans
)
1753 wait_event(fs_info
->transaction_wait
,
1754 trans
->state
>= TRANS_STATE_UNBLOCKED
|| trans
->aborted
);
1758 * commit transactions asynchronously. once btrfs_commit_transaction_async
1759 * returns, any subsequent transaction will not be allowed to join.
1761 struct btrfs_async_commit
{
1762 struct btrfs_trans_handle
*newtrans
;
1763 struct work_struct work
;
1766 static void do_async_commit(struct work_struct
*work
)
1768 struct btrfs_async_commit
*ac
=
1769 container_of(work
, struct btrfs_async_commit
, work
);
1772 * We've got freeze protection passed with the transaction.
1773 * Tell lockdep about it.
1775 if (ac
->newtrans
->type
& __TRANS_FREEZABLE
)
1776 __sb_writers_acquired(ac
->newtrans
->fs_info
->sb
, SB_FREEZE_FS
);
1778 current
->journal_info
= ac
->newtrans
;
1780 btrfs_commit_transaction(ac
->newtrans
);
1784 int btrfs_commit_transaction_async(struct btrfs_trans_handle
*trans
,
1785 int wait_for_unblock
)
1787 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1788 struct btrfs_async_commit
*ac
;
1789 struct btrfs_transaction
*cur_trans
;
1791 ac
= kmalloc(sizeof(*ac
), GFP_NOFS
);
1795 INIT_WORK(&ac
->work
, do_async_commit
);
1796 ac
->newtrans
= btrfs_join_transaction(trans
->root
);
1797 if (IS_ERR(ac
->newtrans
)) {
1798 int err
= PTR_ERR(ac
->newtrans
);
1803 /* take transaction reference */
1804 cur_trans
= trans
->transaction
;
1805 refcount_inc(&cur_trans
->use_count
);
1807 btrfs_end_transaction(trans
);
1810 * Tell lockdep we've released the freeze rwsem, since the
1811 * async commit thread will be the one to unlock it.
1813 if (ac
->newtrans
->type
& __TRANS_FREEZABLE
)
1814 __sb_writers_release(fs_info
->sb
, SB_FREEZE_FS
);
1816 schedule_work(&ac
->work
);
1818 /* wait for transaction to start and unblock */
1819 if (wait_for_unblock
)
1820 wait_current_trans_commit_start_and_unblock(fs_info
, cur_trans
);
1822 wait_current_trans_commit_start(fs_info
, cur_trans
);
1824 if (current
->journal_info
== trans
)
1825 current
->journal_info
= NULL
;
1827 btrfs_put_transaction(cur_trans
);
1832 static void cleanup_transaction(struct btrfs_trans_handle
*trans
, int err
)
1834 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1835 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1837 WARN_ON(refcount_read(&trans
->use_count
) > 1);
1839 btrfs_abort_transaction(trans
, err
);
1841 spin_lock(&fs_info
->trans_lock
);
1844 * If the transaction is removed from the list, it means this
1845 * transaction has been committed successfully, so it is impossible
1846 * to call the cleanup function.
1848 BUG_ON(list_empty(&cur_trans
->list
));
1850 list_del_init(&cur_trans
->list
);
1851 if (cur_trans
== fs_info
->running_transaction
) {
1852 cur_trans
->state
= TRANS_STATE_COMMIT_DOING
;
1853 spin_unlock(&fs_info
->trans_lock
);
1854 wait_event(cur_trans
->writer_wait
,
1855 atomic_read(&cur_trans
->num_writers
) == 1);
1857 spin_lock(&fs_info
->trans_lock
);
1859 spin_unlock(&fs_info
->trans_lock
);
1861 btrfs_cleanup_one_transaction(trans
->transaction
, fs_info
);
1863 spin_lock(&fs_info
->trans_lock
);
1864 if (cur_trans
== fs_info
->running_transaction
)
1865 fs_info
->running_transaction
= NULL
;
1866 spin_unlock(&fs_info
->trans_lock
);
1868 if (trans
->type
& __TRANS_FREEZABLE
)
1869 sb_end_intwrite(fs_info
->sb
);
1870 btrfs_put_transaction(cur_trans
);
1871 btrfs_put_transaction(cur_trans
);
1873 trace_btrfs_transaction_commit(trans
->root
);
1875 if (current
->journal_info
== trans
)
1876 current
->journal_info
= NULL
;
1877 btrfs_scrub_cancel(fs_info
);
1879 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
1882 static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info
*fs_info
)
1885 * We use writeback_inodes_sb here because if we used
1886 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1887 * Currently are holding the fs freeze lock, if we do an async flush
1888 * we'll do btrfs_join_transaction() and deadlock because we need to
1889 * wait for the fs freeze lock. Using the direct flushing we benefit
1890 * from already being in a transaction and our join_transaction doesn't
1891 * have to re-take the fs freeze lock.
1893 if (btrfs_test_opt(fs_info
, FLUSHONCOMMIT
))
1894 writeback_inodes_sb(fs_info
->sb
, WB_REASON_SYNC
);
1898 static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info
*fs_info
)
1900 if (btrfs_test_opt(fs_info
, FLUSHONCOMMIT
))
1901 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
1904 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
)
1906 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1907 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1908 struct btrfs_transaction
*prev_trans
= NULL
;
1911 /* Stop the commit early if ->aborted is set */
1912 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
1913 ret
= cur_trans
->aborted
;
1914 btrfs_end_transaction(trans
);
1918 btrfs_trans_release_metadata(trans
);
1919 trans
->block_rsv
= NULL
;
1921 /* make a pass through all the delayed refs we have so far
1922 * any runnings procs may add more while we are here
1924 ret
= btrfs_run_delayed_refs(trans
, 0);
1926 btrfs_end_transaction(trans
);
1930 cur_trans
= trans
->transaction
;
1933 * set the flushing flag so procs in this transaction have to
1934 * start sending their work down.
1936 cur_trans
->delayed_refs
.flushing
= 1;
1939 if (!list_empty(&trans
->new_bgs
))
1940 btrfs_create_pending_block_groups(trans
);
1942 ret
= btrfs_run_delayed_refs(trans
, 0);
1944 btrfs_end_transaction(trans
);
1948 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN
, &cur_trans
->flags
)) {
1951 /* this mutex is also taken before trying to set
1952 * block groups readonly. We need to make sure
1953 * that nobody has set a block group readonly
1954 * after a extents from that block group have been
1955 * allocated for cache files. btrfs_set_block_group_ro
1956 * will wait for the transaction to commit if it
1957 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
1959 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
1960 * only one process starts all the block group IO. It wouldn't
1961 * hurt to have more than one go through, but there's no
1962 * real advantage to it either.
1964 mutex_lock(&fs_info
->ro_block_group_mutex
);
1965 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN
,
1968 mutex_unlock(&fs_info
->ro_block_group_mutex
);
1971 ret
= btrfs_start_dirty_block_groups(trans
);
1973 btrfs_end_transaction(trans
);
1979 spin_lock(&fs_info
->trans_lock
);
1980 if (cur_trans
->state
>= TRANS_STATE_COMMIT_START
) {
1981 spin_unlock(&fs_info
->trans_lock
);
1982 refcount_inc(&cur_trans
->use_count
);
1983 ret
= btrfs_end_transaction(trans
);
1985 wait_for_commit(cur_trans
);
1987 if (unlikely(cur_trans
->aborted
))
1988 ret
= cur_trans
->aborted
;
1990 btrfs_put_transaction(cur_trans
);
1995 cur_trans
->state
= TRANS_STATE_COMMIT_START
;
1996 wake_up(&fs_info
->transaction_blocked_wait
);
1998 if (cur_trans
->list
.prev
!= &fs_info
->trans_list
) {
1999 prev_trans
= list_entry(cur_trans
->list
.prev
,
2000 struct btrfs_transaction
, list
);
2001 if (prev_trans
->state
!= TRANS_STATE_COMPLETED
) {
2002 refcount_inc(&prev_trans
->use_count
);
2003 spin_unlock(&fs_info
->trans_lock
);
2005 wait_for_commit(prev_trans
);
2006 ret
= prev_trans
->aborted
;
2008 btrfs_put_transaction(prev_trans
);
2010 goto cleanup_transaction
;
2012 spin_unlock(&fs_info
->trans_lock
);
2015 spin_unlock(&fs_info
->trans_lock
);
2018 extwriter_counter_dec(cur_trans
, trans
->type
);
2020 ret
= btrfs_start_delalloc_flush(fs_info
);
2022 goto cleanup_transaction
;
2024 ret
= btrfs_run_delayed_items(trans
);
2026 goto cleanup_transaction
;
2028 wait_event(cur_trans
->writer_wait
,
2029 extwriter_counter_read(cur_trans
) == 0);
2031 /* some pending stuffs might be added after the previous flush. */
2032 ret
= btrfs_run_delayed_items(trans
);
2034 goto cleanup_transaction
;
2036 btrfs_wait_delalloc_flush(fs_info
);
2038 btrfs_scrub_pause(fs_info
);
2040 * Ok now we need to make sure to block out any other joins while we
2041 * commit the transaction. We could have started a join before setting
2042 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2044 spin_lock(&fs_info
->trans_lock
);
2045 cur_trans
->state
= TRANS_STATE_COMMIT_DOING
;
2046 spin_unlock(&fs_info
->trans_lock
);
2047 wait_event(cur_trans
->writer_wait
,
2048 atomic_read(&cur_trans
->num_writers
) == 1);
2050 /* ->aborted might be set after the previous check, so check it */
2051 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
2052 ret
= cur_trans
->aborted
;
2053 goto scrub_continue
;
2056 * the reloc mutex makes sure that we stop
2057 * the balancing code from coming in and moving
2058 * extents around in the middle of the commit
2060 mutex_lock(&fs_info
->reloc_mutex
);
2063 * We needn't worry about the delayed items because we will
2064 * deal with them in create_pending_snapshot(), which is the
2065 * core function of the snapshot creation.
2067 ret
= create_pending_snapshots(trans
);
2069 mutex_unlock(&fs_info
->reloc_mutex
);
2070 goto scrub_continue
;
2074 * We insert the dir indexes of the snapshots and update the inode
2075 * of the snapshots' parents after the snapshot creation, so there
2076 * are some delayed items which are not dealt with. Now deal with
2079 * We needn't worry that this operation will corrupt the snapshots,
2080 * because all the tree which are snapshoted will be forced to COW
2081 * the nodes and leaves.
2083 ret
= btrfs_run_delayed_items(trans
);
2085 mutex_unlock(&fs_info
->reloc_mutex
);
2086 goto scrub_continue
;
2089 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
2091 mutex_unlock(&fs_info
->reloc_mutex
);
2092 goto scrub_continue
;
2096 * make sure none of the code above managed to slip in a
2099 btrfs_assert_delayed_root_empty(fs_info
);
2101 WARN_ON(cur_trans
!= trans
->transaction
);
2103 /* btrfs_commit_tree_roots is responsible for getting the
2104 * various roots consistent with each other. Every pointer
2105 * in the tree of tree roots has to point to the most up to date
2106 * root for every subvolume and other tree. So, we have to keep
2107 * the tree logging code from jumping in and changing any
2110 * At this point in the commit, there can't be any tree-log
2111 * writers, but a little lower down we drop the trans mutex
2112 * and let new people in. By holding the tree_log_mutex
2113 * from now until after the super is written, we avoid races
2114 * with the tree-log code.
2116 mutex_lock(&fs_info
->tree_log_mutex
);
2118 ret
= commit_fs_roots(trans
);
2120 mutex_unlock(&fs_info
->tree_log_mutex
);
2121 mutex_unlock(&fs_info
->reloc_mutex
);
2122 goto scrub_continue
;
2126 * Since the transaction is done, we can apply the pending changes
2127 * before the next transaction.
2129 btrfs_apply_pending_changes(fs_info
);
2131 /* commit_fs_roots gets rid of all the tree log roots, it is now
2132 * safe to free the root of tree log roots
2134 btrfs_free_log_root_tree(trans
, fs_info
);
2137 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2138 * new delayed refs. Must handle them or qgroup can be wrong.
2140 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
2142 mutex_unlock(&fs_info
->tree_log_mutex
);
2143 mutex_unlock(&fs_info
->reloc_mutex
);
2144 goto scrub_continue
;
2148 * Since fs roots are all committed, we can get a quite accurate
2149 * new_roots. So let's do quota accounting.
2151 ret
= btrfs_qgroup_account_extents(trans
);
2153 mutex_unlock(&fs_info
->tree_log_mutex
);
2154 mutex_unlock(&fs_info
->reloc_mutex
);
2155 goto scrub_continue
;
2158 ret
= commit_cowonly_roots(trans
);
2160 mutex_unlock(&fs_info
->tree_log_mutex
);
2161 mutex_unlock(&fs_info
->reloc_mutex
);
2162 goto scrub_continue
;
2166 * The tasks which save the space cache and inode cache may also
2167 * update ->aborted, check it.
2169 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
2170 ret
= cur_trans
->aborted
;
2171 mutex_unlock(&fs_info
->tree_log_mutex
);
2172 mutex_unlock(&fs_info
->reloc_mutex
);
2173 goto scrub_continue
;
2176 btrfs_prepare_extent_commit(fs_info
);
2178 cur_trans
= fs_info
->running_transaction
;
2180 btrfs_set_root_node(&fs_info
->tree_root
->root_item
,
2181 fs_info
->tree_root
->node
);
2182 list_add_tail(&fs_info
->tree_root
->dirty_list
,
2183 &cur_trans
->switch_commits
);
2185 btrfs_set_root_node(&fs_info
->chunk_root
->root_item
,
2186 fs_info
->chunk_root
->node
);
2187 list_add_tail(&fs_info
->chunk_root
->dirty_list
,
2188 &cur_trans
->switch_commits
);
2190 switch_commit_roots(cur_trans
);
2192 ASSERT(list_empty(&cur_trans
->dirty_bgs
));
2193 ASSERT(list_empty(&cur_trans
->io_bgs
));
2194 update_super_roots(fs_info
);
2196 btrfs_set_super_log_root(fs_info
->super_copy
, 0);
2197 btrfs_set_super_log_root_level(fs_info
->super_copy
, 0);
2198 memcpy(fs_info
->super_for_commit
, fs_info
->super_copy
,
2199 sizeof(*fs_info
->super_copy
));
2201 btrfs_update_commit_device_size(fs_info
);
2202 btrfs_update_commit_device_bytes_used(cur_trans
);
2204 clear_bit(BTRFS_FS_LOG1_ERR
, &fs_info
->flags
);
2205 clear_bit(BTRFS_FS_LOG2_ERR
, &fs_info
->flags
);
2207 btrfs_trans_release_chunk_metadata(trans
);
2209 spin_lock(&fs_info
->trans_lock
);
2210 cur_trans
->state
= TRANS_STATE_UNBLOCKED
;
2211 fs_info
->running_transaction
= NULL
;
2212 spin_unlock(&fs_info
->trans_lock
);
2213 mutex_unlock(&fs_info
->reloc_mutex
);
2215 wake_up(&fs_info
->transaction_wait
);
2217 ret
= btrfs_write_and_wait_transaction(trans
);
2219 btrfs_handle_fs_error(fs_info
, ret
,
2220 "Error while writing out transaction");
2221 mutex_unlock(&fs_info
->tree_log_mutex
);
2222 goto scrub_continue
;
2225 ret
= write_all_supers(fs_info
, 0);
2227 * the super is written, we can safely allow the tree-loggers
2228 * to go about their business
2230 mutex_unlock(&fs_info
->tree_log_mutex
);
2232 goto scrub_continue
;
2234 btrfs_finish_extent_commit(trans
);
2236 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &cur_trans
->flags
))
2237 btrfs_clear_space_info_full(fs_info
);
2239 fs_info
->last_trans_committed
= cur_trans
->transid
;
2241 * We needn't acquire the lock here because there is no other task
2242 * which can change it.
2244 cur_trans
->state
= TRANS_STATE_COMPLETED
;
2245 wake_up(&cur_trans
->commit_wait
);
2246 clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT
, &fs_info
->flags
);
2248 spin_lock(&fs_info
->trans_lock
);
2249 list_del_init(&cur_trans
->list
);
2250 spin_unlock(&fs_info
->trans_lock
);
2252 btrfs_put_transaction(cur_trans
);
2253 btrfs_put_transaction(cur_trans
);
2255 if (trans
->type
& __TRANS_FREEZABLE
)
2256 sb_end_intwrite(fs_info
->sb
);
2258 trace_btrfs_transaction_commit(trans
->root
);
2260 btrfs_scrub_continue(fs_info
);
2262 if (current
->journal_info
== trans
)
2263 current
->journal_info
= NULL
;
2265 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
2270 btrfs_scrub_continue(fs_info
);
2271 cleanup_transaction
:
2272 btrfs_trans_release_metadata(trans
);
2273 btrfs_trans_release_chunk_metadata(trans
);
2274 trans
->block_rsv
= NULL
;
2275 btrfs_warn(fs_info
, "Skipping commit of aborted transaction.");
2276 if (current
->journal_info
== trans
)
2277 current
->journal_info
= NULL
;
2278 cleanup_transaction(trans
, ret
);
2284 * return < 0 if error
2285 * 0 if there are no more dead_roots at the time of call
2286 * 1 there are more to be processed, call me again
2288 * The return value indicates there are certainly more snapshots to delete, but
2289 * if there comes a new one during processing, it may return 0. We don't mind,
2290 * because btrfs_commit_super will poke cleaner thread and it will process it a
2291 * few seconds later.
2293 int btrfs_clean_one_deleted_snapshot(struct btrfs_root
*root
)
2296 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2298 spin_lock(&fs_info
->trans_lock
);
2299 if (list_empty(&fs_info
->dead_roots
)) {
2300 spin_unlock(&fs_info
->trans_lock
);
2303 root
= list_first_entry(&fs_info
->dead_roots
,
2304 struct btrfs_root
, root_list
);
2305 list_del_init(&root
->root_list
);
2306 spin_unlock(&fs_info
->trans_lock
);
2308 btrfs_debug(fs_info
, "cleaner removing %llu", root
->root_key
.objectid
);
2310 btrfs_kill_all_delayed_nodes(root
);
2312 if (btrfs_header_backref_rev(root
->node
) <
2313 BTRFS_MIXED_BACKREF_REV
)
2314 ret
= btrfs_drop_snapshot(root
, NULL
, 0, 0);
2316 ret
= btrfs_drop_snapshot(root
, NULL
, 1, 0);
2318 return (ret
< 0) ? 0 : 1;
2321 void btrfs_apply_pending_changes(struct btrfs_fs_info
*fs_info
)
2326 prev
= xchg(&fs_info
->pending_changes
, 0);
2330 bit
= 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE
;
2332 btrfs_set_opt(fs_info
->mount_opt
, INODE_MAP_CACHE
);
2335 bit
= 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE
;
2337 btrfs_clear_opt(fs_info
->mount_opt
, INODE_MAP_CACHE
);
2340 bit
= 1 << BTRFS_PENDING_COMMIT
;
2342 btrfs_debug(fs_info
, "pending commit done");
2347 "unknown pending changes left 0x%lx, ignoring", prev
);