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_JOIN_NOSTART
),
33 [TRANS_STATE_UNBLOCKED
] = (__TRANS_START
|
37 __TRANS_JOIN_NOSTART
),
38 [TRANS_STATE_COMPLETED
] = (__TRANS_START
|
42 __TRANS_JOIN_NOSTART
),
45 void btrfs_put_transaction(struct btrfs_transaction
*transaction
)
47 WARN_ON(refcount_read(&transaction
->use_count
) == 0);
48 if (refcount_dec_and_test(&transaction
->use_count
)) {
49 BUG_ON(!list_empty(&transaction
->list
));
50 WARN_ON(!RB_EMPTY_ROOT(&transaction
->delayed_refs
.href_root
));
51 if (transaction
->delayed_refs
.pending_csums
)
52 btrfs_err(transaction
->fs_info
,
53 "pending csums is %llu",
54 transaction
->delayed_refs
.pending_csums
);
55 while (!list_empty(&transaction
->pending_chunks
)) {
56 struct extent_map
*em
;
58 em
= list_first_entry(&transaction
->pending_chunks
,
59 struct extent_map
, list
);
60 list_del_init(&em
->list
);
64 * If any block groups are found in ->deleted_bgs then it's
65 * because the transaction was aborted and a commit did not
66 * happen (things failed before writing the new superblock
67 * and calling btrfs_finish_extent_commit()), so we can not
68 * discard the physical locations of the block groups.
70 while (!list_empty(&transaction
->deleted_bgs
)) {
71 struct btrfs_block_group_cache
*cache
;
73 cache
= list_first_entry(&transaction
->deleted_bgs
,
74 struct btrfs_block_group_cache
,
76 list_del_init(&cache
->bg_list
);
77 btrfs_put_block_group_trimming(cache
);
78 btrfs_put_block_group(cache
);
84 static void clear_btree_io_tree(struct extent_io_tree
*tree
)
86 spin_lock(&tree
->lock
);
88 * Do a single barrier for the waitqueue_active check here, the state
89 * of the waitqueue should not change once clear_btree_io_tree is
93 while (!RB_EMPTY_ROOT(&tree
->state
)) {
95 struct extent_state
*state
;
97 node
= rb_first(&tree
->state
);
98 state
= rb_entry(node
, struct extent_state
, rb_node
);
99 rb_erase(&state
->rb_node
, &tree
->state
);
100 RB_CLEAR_NODE(&state
->rb_node
);
102 * btree io trees aren't supposed to have tasks waiting for
103 * changes in the flags of extent states ever.
105 ASSERT(!waitqueue_active(&state
->wq
));
106 free_extent_state(state
);
108 cond_resched_lock(&tree
->lock
);
110 spin_unlock(&tree
->lock
);
113 static noinline
void switch_commit_roots(struct btrfs_transaction
*trans
)
115 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
116 struct btrfs_root
*root
, *tmp
;
118 down_write(&fs_info
->commit_root_sem
);
119 list_for_each_entry_safe(root
, tmp
, &trans
->switch_commits
,
121 list_del_init(&root
->dirty_list
);
122 free_extent_buffer(root
->commit_root
);
123 root
->commit_root
= btrfs_root_node(root
);
124 if (is_fstree(root
->objectid
))
125 btrfs_unpin_free_ino(root
);
126 clear_btree_io_tree(&root
->dirty_log_pages
);
129 /* We can free old roots now. */
130 spin_lock(&trans
->dropped_roots_lock
);
131 while (!list_empty(&trans
->dropped_roots
)) {
132 root
= list_first_entry(&trans
->dropped_roots
,
133 struct btrfs_root
, root_list
);
134 list_del_init(&root
->root_list
);
135 spin_unlock(&trans
->dropped_roots_lock
);
136 btrfs_drop_and_free_fs_root(fs_info
, root
);
137 spin_lock(&trans
->dropped_roots_lock
);
139 spin_unlock(&trans
->dropped_roots_lock
);
140 up_write(&fs_info
->commit_root_sem
);
143 static inline void extwriter_counter_inc(struct btrfs_transaction
*trans
,
146 if (type
& TRANS_EXTWRITERS
)
147 atomic_inc(&trans
->num_extwriters
);
150 static inline void extwriter_counter_dec(struct btrfs_transaction
*trans
,
153 if (type
& TRANS_EXTWRITERS
)
154 atomic_dec(&trans
->num_extwriters
);
157 static inline void extwriter_counter_init(struct btrfs_transaction
*trans
,
160 atomic_set(&trans
->num_extwriters
, ((type
& TRANS_EXTWRITERS
) ? 1 : 0));
163 static inline int extwriter_counter_read(struct btrfs_transaction
*trans
)
165 return atomic_read(&trans
->num_extwriters
);
169 * either allocate a new transaction or hop into the existing one
171 static noinline
int join_transaction(struct btrfs_fs_info
*fs_info
,
174 struct btrfs_transaction
*cur_trans
;
176 spin_lock(&fs_info
->trans_lock
);
178 /* The file system has been taken offline. No new transactions. */
179 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
)) {
180 spin_unlock(&fs_info
->trans_lock
);
184 cur_trans
= fs_info
->running_transaction
;
186 if (cur_trans
->aborted
) {
187 spin_unlock(&fs_info
->trans_lock
);
188 return cur_trans
->aborted
;
190 if (btrfs_blocked_trans_types
[cur_trans
->state
] & type
) {
191 spin_unlock(&fs_info
->trans_lock
);
194 refcount_inc(&cur_trans
->use_count
);
195 atomic_inc(&cur_trans
->num_writers
);
196 extwriter_counter_inc(cur_trans
, type
);
197 spin_unlock(&fs_info
->trans_lock
);
200 spin_unlock(&fs_info
->trans_lock
);
203 * If we are ATTACH, we just want to catch the current transaction,
204 * and commit it. If there is no transaction, just return ENOENT.
206 if (type
== TRANS_ATTACH
)
210 * JOIN_NOLOCK only happens during the transaction commit, so
211 * it is impossible that ->running_transaction is NULL
213 BUG_ON(type
== TRANS_JOIN_NOLOCK
);
215 cur_trans
= kmalloc(sizeof(*cur_trans
), GFP_NOFS
);
219 spin_lock(&fs_info
->trans_lock
);
220 if (fs_info
->running_transaction
) {
222 * someone started a transaction after we unlocked. Make sure
223 * to redo the checks above
227 } else if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
)) {
228 spin_unlock(&fs_info
->trans_lock
);
233 cur_trans
->fs_info
= fs_info
;
234 atomic_set(&cur_trans
->num_writers
, 1);
235 extwriter_counter_init(cur_trans
, type
);
236 init_waitqueue_head(&cur_trans
->writer_wait
);
237 init_waitqueue_head(&cur_trans
->commit_wait
);
238 init_waitqueue_head(&cur_trans
->pending_wait
);
239 cur_trans
->state
= TRANS_STATE_RUNNING
;
241 * One for this trans handle, one so it will live on until we
242 * commit the transaction.
244 refcount_set(&cur_trans
->use_count
, 2);
245 atomic_set(&cur_trans
->pending_ordered
, 0);
246 cur_trans
->flags
= 0;
247 cur_trans
->start_time
= ktime_get_seconds();
249 memset(&cur_trans
->delayed_refs
, 0, sizeof(cur_trans
->delayed_refs
));
251 cur_trans
->delayed_refs
.href_root
= RB_ROOT
;
252 cur_trans
->delayed_refs
.dirty_extent_root
= RB_ROOT
;
253 atomic_set(&cur_trans
->delayed_refs
.num_entries
, 0);
256 * although the tree mod log is per file system and not per transaction,
257 * the log must never go across transaction boundaries.
260 if (!list_empty(&fs_info
->tree_mod_seq_list
))
261 WARN(1, KERN_ERR
"BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
262 if (!RB_EMPTY_ROOT(&fs_info
->tree_mod_log
))
263 WARN(1, KERN_ERR
"BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
264 atomic64_set(&fs_info
->tree_mod_seq
, 0);
266 spin_lock_init(&cur_trans
->delayed_refs
.lock
);
268 INIT_LIST_HEAD(&cur_trans
->pending_snapshots
);
269 INIT_LIST_HEAD(&cur_trans
->pending_chunks
);
270 INIT_LIST_HEAD(&cur_trans
->switch_commits
);
271 INIT_LIST_HEAD(&cur_trans
->dirty_bgs
);
272 INIT_LIST_HEAD(&cur_trans
->io_bgs
);
273 INIT_LIST_HEAD(&cur_trans
->dropped_roots
);
274 mutex_init(&cur_trans
->cache_write_mutex
);
275 cur_trans
->num_dirty_bgs
= 0;
276 spin_lock_init(&cur_trans
->dirty_bgs_lock
);
277 INIT_LIST_HEAD(&cur_trans
->deleted_bgs
);
278 spin_lock_init(&cur_trans
->dropped_roots_lock
);
279 list_add_tail(&cur_trans
->list
, &fs_info
->trans_list
);
280 extent_io_tree_init(&cur_trans
->dirty_pages
,
281 fs_info
->btree_inode
);
282 fs_info
->generation
++;
283 cur_trans
->transid
= fs_info
->generation
;
284 fs_info
->running_transaction
= cur_trans
;
285 cur_trans
->aborted
= 0;
286 spin_unlock(&fs_info
->trans_lock
);
292 * this does all the record keeping required to make sure that a reference
293 * counted root is properly recorded in a given transaction. This is required
294 * to make sure the old root from before we joined the transaction is deleted
295 * when the transaction commits
297 static int record_root_in_trans(struct btrfs_trans_handle
*trans
,
298 struct btrfs_root
*root
,
301 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
303 if ((test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
304 root
->last_trans
< trans
->transid
) || force
) {
305 WARN_ON(root
== fs_info
->extent_root
);
306 WARN_ON(!force
&& root
->commit_root
!= root
->node
);
309 * see below for IN_TRANS_SETUP usage rules
310 * we have the reloc mutex held now, so there
311 * is only one writer in this function
313 set_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
);
315 /* make sure readers find IN_TRANS_SETUP before
316 * they find our root->last_trans update
320 spin_lock(&fs_info
->fs_roots_radix_lock
);
321 if (root
->last_trans
== trans
->transid
&& !force
) {
322 spin_unlock(&fs_info
->fs_roots_radix_lock
);
325 radix_tree_tag_set(&fs_info
->fs_roots_radix
,
326 (unsigned long)root
->root_key
.objectid
,
327 BTRFS_ROOT_TRANS_TAG
);
328 spin_unlock(&fs_info
->fs_roots_radix_lock
);
329 root
->last_trans
= trans
->transid
;
331 /* this is pretty tricky. We don't want to
332 * take the relocation lock in btrfs_record_root_in_trans
333 * unless we're really doing the first setup for this root in
336 * Normally we'd use root->last_trans as a flag to decide
337 * if we want to take the expensive mutex.
339 * But, we have to set root->last_trans before we
340 * init the relocation root, otherwise, we trip over warnings
341 * in ctree.c. The solution used here is to flag ourselves
342 * with root IN_TRANS_SETUP. When this is 1, we're still
343 * fixing up the reloc trees and everyone must wait.
345 * When this is zero, they can trust root->last_trans and fly
346 * through btrfs_record_root_in_trans without having to take the
347 * lock. smp_wmb() makes sure that all the writes above are
348 * done before we pop in the zero below
350 btrfs_init_reloc_root(trans
, root
);
351 smp_mb__before_atomic();
352 clear_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
);
358 void btrfs_add_dropped_root(struct btrfs_trans_handle
*trans
,
359 struct btrfs_root
*root
)
361 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
362 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
364 /* Add ourselves to the transaction dropped list */
365 spin_lock(&cur_trans
->dropped_roots_lock
);
366 list_add_tail(&root
->root_list
, &cur_trans
->dropped_roots
);
367 spin_unlock(&cur_trans
->dropped_roots_lock
);
369 /* Make sure we don't try to update the root at commit time */
370 spin_lock(&fs_info
->fs_roots_radix_lock
);
371 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
372 (unsigned long)root
->root_key
.objectid
,
373 BTRFS_ROOT_TRANS_TAG
);
374 spin_unlock(&fs_info
->fs_roots_radix_lock
);
377 int btrfs_record_root_in_trans(struct btrfs_trans_handle
*trans
,
378 struct btrfs_root
*root
)
380 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
382 if (!test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
386 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
390 if (root
->last_trans
== trans
->transid
&&
391 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP
, &root
->state
))
394 mutex_lock(&fs_info
->reloc_mutex
);
395 record_root_in_trans(trans
, root
, 0);
396 mutex_unlock(&fs_info
->reloc_mutex
);
401 static inline int is_transaction_blocked(struct btrfs_transaction
*trans
)
403 return (trans
->state
>= TRANS_STATE_BLOCKED
&&
404 trans
->state
< TRANS_STATE_UNBLOCKED
&&
408 /* wait for commit against the current transaction to become unblocked
409 * when this is done, it is safe to start a new transaction, but the current
410 * transaction might not be fully on disk.
412 static void wait_current_trans(struct btrfs_fs_info
*fs_info
)
414 struct btrfs_transaction
*cur_trans
;
416 spin_lock(&fs_info
->trans_lock
);
417 cur_trans
= fs_info
->running_transaction
;
418 if (cur_trans
&& is_transaction_blocked(cur_trans
)) {
419 refcount_inc(&cur_trans
->use_count
);
420 spin_unlock(&fs_info
->trans_lock
);
422 wait_event(fs_info
->transaction_wait
,
423 cur_trans
->state
>= TRANS_STATE_UNBLOCKED
||
425 btrfs_put_transaction(cur_trans
);
427 spin_unlock(&fs_info
->trans_lock
);
431 static int may_wait_transaction(struct btrfs_fs_info
*fs_info
, int type
)
433 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
436 if (type
== TRANS_START
)
442 static inline bool need_reserve_reloc_root(struct btrfs_root
*root
)
444 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
446 if (!fs_info
->reloc_ctl
||
447 !test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
448 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
455 static struct btrfs_trans_handle
*
456 start_transaction(struct btrfs_root
*root
, unsigned int num_items
,
457 unsigned int type
, enum btrfs_reserve_flush_enum flush
,
458 bool enforce_qgroups
)
460 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
462 struct btrfs_trans_handle
*h
;
463 struct btrfs_transaction
*cur_trans
;
465 u64 qgroup_reserved
= 0;
466 bool reloc_reserved
= false;
469 /* Send isn't supposed to start transactions. */
470 ASSERT(current
->journal_info
!= BTRFS_SEND_TRANS_STUB
);
472 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
473 return ERR_PTR(-EROFS
);
475 if (current
->journal_info
) {
476 WARN_ON(type
& TRANS_EXTWRITERS
);
477 h
= current
->journal_info
;
478 refcount_inc(&h
->use_count
);
479 WARN_ON(refcount_read(&h
->use_count
) > 2);
480 h
->orig_rsv
= h
->block_rsv
;
486 * Do the reservation before we join the transaction so we can do all
487 * the appropriate flushing if need be.
489 if (num_items
&& root
!= fs_info
->chunk_root
) {
490 qgroup_reserved
= num_items
* fs_info
->nodesize
;
491 ret
= btrfs_qgroup_reserve_meta_pertrans(root
, qgroup_reserved
,
496 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, num_items
);
498 * Do the reservation for the relocation root creation
500 if (need_reserve_reloc_root(root
)) {
501 num_bytes
+= fs_info
->nodesize
;
502 reloc_reserved
= true;
505 ret
= btrfs_block_rsv_add(root
, &fs_info
->trans_block_rsv
,
511 h
= kmem_cache_zalloc(btrfs_trans_handle_cachep
, GFP_NOFS
);
518 * If we are JOIN_NOLOCK we're already committing a transaction and
519 * waiting on this guy, so we don't need to do the sb_start_intwrite
520 * because we're already holding a ref. We need this because we could
521 * have raced in and did an fsync() on a file which can kick a commit
522 * and then we deadlock with somebody doing a freeze.
524 * If we are ATTACH, it means we just want to catch the current
525 * transaction and commit it, so we needn't do sb_start_intwrite().
527 if (type
& __TRANS_FREEZABLE
)
528 sb_start_intwrite(fs_info
->sb
);
530 if (may_wait_transaction(fs_info
, type
))
531 wait_current_trans(fs_info
);
534 ret
= join_transaction(fs_info
, type
);
536 wait_current_trans(fs_info
);
537 if (unlikely(type
== TRANS_ATTACH
||
538 type
== TRANS_JOIN_NOSTART
))
541 } while (ret
== -EBUSY
);
546 cur_trans
= fs_info
->running_transaction
;
548 h
->transid
= cur_trans
->transid
;
549 h
->transaction
= cur_trans
;
551 refcount_set(&h
->use_count
, 1);
552 h
->fs_info
= root
->fs_info
;
555 h
->can_flush_pending_bgs
= true;
556 INIT_LIST_HEAD(&h
->new_bgs
);
559 if (cur_trans
->state
>= TRANS_STATE_BLOCKED
&&
560 may_wait_transaction(fs_info
, type
)) {
561 current
->journal_info
= h
;
562 btrfs_commit_transaction(h
);
567 trace_btrfs_space_reservation(fs_info
, "transaction",
568 h
->transid
, num_bytes
, 1);
569 h
->block_rsv
= &fs_info
->trans_block_rsv
;
570 h
->bytes_reserved
= num_bytes
;
571 h
->reloc_reserved
= reloc_reserved
;
575 if (!current
->journal_info
)
576 current
->journal_info
= h
;
579 * btrfs_record_root_in_trans() needs to alloc new extents, and may
580 * call btrfs_join_transaction() while we're also starting a
583 * Thus it need to be called after current->journal_info initialized,
584 * or we can deadlock.
586 btrfs_record_root_in_trans(h
, root
);
591 if (type
& __TRANS_FREEZABLE
)
592 sb_end_intwrite(fs_info
->sb
);
593 kmem_cache_free(btrfs_trans_handle_cachep
, h
);
596 btrfs_block_rsv_release(fs_info
, &fs_info
->trans_block_rsv
,
599 btrfs_qgroup_free_meta_pertrans(root
, qgroup_reserved
);
603 struct btrfs_trans_handle
*btrfs_start_transaction(struct btrfs_root
*root
,
604 unsigned int num_items
)
606 return start_transaction(root
, num_items
, TRANS_START
,
607 BTRFS_RESERVE_FLUSH_ALL
, true);
610 struct btrfs_trans_handle
*btrfs_start_transaction_fallback_global_rsv(
611 struct btrfs_root
*root
,
612 unsigned int num_items
,
615 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
616 struct btrfs_trans_handle
*trans
;
621 * We have two callers: unlink and block group removal. The
622 * former should succeed even if we will temporarily exceed
623 * quota and the latter operates on the extent root so
624 * qgroup enforcement is ignored anyway.
626 trans
= start_transaction(root
, num_items
, TRANS_START
,
627 BTRFS_RESERVE_FLUSH_ALL
, false);
628 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
631 trans
= btrfs_start_transaction(root
, 0);
635 num_bytes
= btrfs_calc_trans_metadata_size(fs_info
, num_items
);
636 ret
= btrfs_cond_migrate_bytes(fs_info
, &fs_info
->trans_block_rsv
,
637 num_bytes
, min_factor
);
639 btrfs_end_transaction(trans
);
643 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
644 trans
->bytes_reserved
= num_bytes
;
645 trace_btrfs_space_reservation(fs_info
, "transaction",
646 trans
->transid
, num_bytes
, 1);
651 struct btrfs_trans_handle
*btrfs_join_transaction(struct btrfs_root
*root
)
653 return start_transaction(root
, 0, TRANS_JOIN
, BTRFS_RESERVE_NO_FLUSH
,
657 struct btrfs_trans_handle
*btrfs_join_transaction_nolock(struct btrfs_root
*root
)
659 return start_transaction(root
, 0, TRANS_JOIN_NOLOCK
,
660 BTRFS_RESERVE_NO_FLUSH
, true);
664 * Similar to regular join but it never starts a transaction when none is
665 * running or after waiting for the current one to finish.
667 struct btrfs_trans_handle
*btrfs_join_transaction_nostart(struct btrfs_root
*root
)
669 return start_transaction(root
, 0, TRANS_JOIN_NOSTART
,
670 BTRFS_RESERVE_NO_FLUSH
, true);
674 * btrfs_attach_transaction() - catch the running transaction
676 * It is used when we want to commit the current the transaction, but
677 * don't want to start a new one.
679 * Note: If this function return -ENOENT, it just means there is no
680 * running transaction. But it is possible that the inactive transaction
681 * is still in the memory, not fully on disk. If you hope there is no
682 * inactive transaction in the fs when -ENOENT is returned, you should
684 * btrfs_attach_transaction_barrier()
686 struct btrfs_trans_handle
*btrfs_attach_transaction(struct btrfs_root
*root
)
688 return start_transaction(root
, 0, TRANS_ATTACH
,
689 BTRFS_RESERVE_NO_FLUSH
, true);
693 * btrfs_attach_transaction_barrier() - catch the running transaction
695 * It is similar to the above function, the differentia is this one
696 * will wait for all the inactive transactions until they fully
699 struct btrfs_trans_handle
*
700 btrfs_attach_transaction_barrier(struct btrfs_root
*root
)
702 struct btrfs_trans_handle
*trans
;
704 trans
= start_transaction(root
, 0, TRANS_ATTACH
,
705 BTRFS_RESERVE_NO_FLUSH
, true);
706 if (trans
== ERR_PTR(-ENOENT
))
707 btrfs_wait_for_commit(root
->fs_info
, 0);
712 /* wait for a transaction commit to be fully complete */
713 static noinline
void wait_for_commit(struct btrfs_transaction
*commit
)
715 wait_event(commit
->commit_wait
, commit
->state
== TRANS_STATE_COMPLETED
);
718 int btrfs_wait_for_commit(struct btrfs_fs_info
*fs_info
, u64 transid
)
720 struct btrfs_transaction
*cur_trans
= NULL
, *t
;
724 if (transid
<= fs_info
->last_trans_committed
)
727 /* find specified transaction */
728 spin_lock(&fs_info
->trans_lock
);
729 list_for_each_entry(t
, &fs_info
->trans_list
, list
) {
730 if (t
->transid
== transid
) {
732 refcount_inc(&cur_trans
->use_count
);
736 if (t
->transid
> transid
) {
741 spin_unlock(&fs_info
->trans_lock
);
744 * The specified transaction doesn't exist, or we
745 * raced with btrfs_commit_transaction
748 if (transid
> fs_info
->last_trans_committed
)
753 /* find newest transaction that is committing | committed */
754 spin_lock(&fs_info
->trans_lock
);
755 list_for_each_entry_reverse(t
, &fs_info
->trans_list
,
757 if (t
->state
>= TRANS_STATE_COMMIT_START
) {
758 if (t
->state
== TRANS_STATE_COMPLETED
)
761 refcount_inc(&cur_trans
->use_count
);
765 spin_unlock(&fs_info
->trans_lock
);
767 goto out
; /* nothing committing|committed */
770 wait_for_commit(cur_trans
);
771 btrfs_put_transaction(cur_trans
);
776 void btrfs_throttle(struct btrfs_fs_info
*fs_info
)
778 wait_current_trans(fs_info
);
781 static int should_end_transaction(struct btrfs_trans_handle
*trans
)
783 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
785 if (btrfs_check_space_for_delayed_refs(trans
, fs_info
))
788 return !!btrfs_block_rsv_check(&fs_info
->global_block_rsv
, 5);
791 int btrfs_should_end_transaction(struct btrfs_trans_handle
*trans
)
793 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
798 if (cur_trans
->state
>= TRANS_STATE_BLOCKED
||
799 cur_trans
->delayed_refs
.flushing
)
802 updates
= trans
->delayed_ref_updates
;
803 trans
->delayed_ref_updates
= 0;
805 err
= btrfs_run_delayed_refs(trans
, updates
* 2);
806 if (err
) /* Error code will also eval true */
810 return should_end_transaction(trans
);
813 static void btrfs_trans_release_metadata(struct btrfs_trans_handle
*trans
)
816 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
818 if (!trans
->block_rsv
) {
819 ASSERT(!trans
->bytes_reserved
);
823 if (!trans
->bytes_reserved
)
826 ASSERT(trans
->block_rsv
== &fs_info
->trans_block_rsv
);
827 trace_btrfs_space_reservation(fs_info
, "transaction",
828 trans
->transid
, trans
->bytes_reserved
, 0);
829 btrfs_block_rsv_release(fs_info
, trans
->block_rsv
,
830 trans
->bytes_reserved
);
831 trans
->bytes_reserved
= 0;
834 static int __btrfs_end_transaction(struct btrfs_trans_handle
*trans
,
837 struct btrfs_fs_info
*info
= trans
->fs_info
;
838 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
839 u64 transid
= trans
->transid
;
840 unsigned long cur
= trans
->delayed_ref_updates
;
841 int lock
= (trans
->type
!= TRANS_JOIN_NOLOCK
);
843 int must_run_delayed_refs
= 0;
845 if (refcount_read(&trans
->use_count
) > 1) {
846 refcount_dec(&trans
->use_count
);
847 trans
->block_rsv
= trans
->orig_rsv
;
851 btrfs_trans_release_metadata(trans
);
852 trans
->block_rsv
= NULL
;
854 if (!list_empty(&trans
->new_bgs
))
855 btrfs_create_pending_block_groups(trans
);
857 trans
->delayed_ref_updates
= 0;
859 must_run_delayed_refs
=
860 btrfs_should_throttle_delayed_refs(trans
, info
);
861 cur
= max_t(unsigned long, cur
, 32);
864 * don't make the caller wait if they are from a NOLOCK
865 * or ATTACH transaction, it will deadlock with commit
867 if (must_run_delayed_refs
== 1 &&
868 (trans
->type
& (__TRANS_JOIN_NOLOCK
| __TRANS_ATTACH
)))
869 must_run_delayed_refs
= 2;
872 btrfs_trans_release_metadata(trans
);
873 trans
->block_rsv
= NULL
;
875 if (!list_empty(&trans
->new_bgs
))
876 btrfs_create_pending_block_groups(trans
);
878 btrfs_trans_release_chunk_metadata(trans
);
880 if (lock
&& should_end_transaction(trans
) &&
881 READ_ONCE(cur_trans
->state
) == TRANS_STATE_RUNNING
) {
882 spin_lock(&info
->trans_lock
);
883 if (cur_trans
->state
== TRANS_STATE_RUNNING
)
884 cur_trans
->state
= TRANS_STATE_BLOCKED
;
885 spin_unlock(&info
->trans_lock
);
888 if (lock
&& READ_ONCE(cur_trans
->state
) == TRANS_STATE_BLOCKED
) {
890 return btrfs_commit_transaction(trans
);
892 wake_up_process(info
->transaction_kthread
);
895 if (trans
->type
& __TRANS_FREEZABLE
)
896 sb_end_intwrite(info
->sb
);
898 WARN_ON(cur_trans
!= info
->running_transaction
);
899 WARN_ON(atomic_read(&cur_trans
->num_writers
) < 1);
900 atomic_dec(&cur_trans
->num_writers
);
901 extwriter_counter_dec(cur_trans
, trans
->type
);
903 cond_wake_up(&cur_trans
->writer_wait
);
904 btrfs_put_transaction(cur_trans
);
906 if (current
->journal_info
== trans
)
907 current
->journal_info
= NULL
;
910 btrfs_run_delayed_iputs(info
);
912 if (trans
->aborted
||
913 test_bit(BTRFS_FS_STATE_ERROR
, &info
->fs_state
)) {
914 wake_up_process(info
->transaction_kthread
);
918 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
919 if (must_run_delayed_refs
) {
920 btrfs_async_run_delayed_refs(info
, cur
, transid
,
921 must_run_delayed_refs
== 1);
926 int btrfs_end_transaction(struct btrfs_trans_handle
*trans
)
928 return __btrfs_end_transaction(trans
, 0);
931 int btrfs_end_transaction_throttle(struct btrfs_trans_handle
*trans
)
933 return __btrfs_end_transaction(trans
, 1);
937 * when btree blocks are allocated, they have some corresponding bits set for
938 * them in one of two extent_io trees. This is used to make sure all of
939 * those extents are sent to disk but does not wait on them
941 int btrfs_write_marked_extents(struct btrfs_fs_info
*fs_info
,
942 struct extent_io_tree
*dirty_pages
, int mark
)
946 struct address_space
*mapping
= fs_info
->btree_inode
->i_mapping
;
947 struct extent_state
*cached_state
= NULL
;
951 atomic_inc(&BTRFS_I(fs_info
->btree_inode
)->sync_writers
);
952 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
953 mark
, &cached_state
)) {
954 bool wait_writeback
= false;
956 err
= convert_extent_bit(dirty_pages
, start
, end
,
958 mark
, &cached_state
);
960 * convert_extent_bit can return -ENOMEM, which is most of the
961 * time a temporary error. So when it happens, ignore the error
962 * and wait for writeback of this range to finish - because we
963 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
964 * to __btrfs_wait_marked_extents() would not know that
965 * writeback for this range started and therefore wouldn't
966 * wait for it to finish - we don't want to commit a
967 * superblock that points to btree nodes/leafs for which
968 * writeback hasn't finished yet (and without errors).
969 * We cleanup any entries left in the io tree when committing
970 * the transaction (through clear_btree_io_tree()).
972 if (err
== -ENOMEM
) {
974 wait_writeback
= true;
977 err
= filemap_fdatawrite_range(mapping
, start
, end
);
980 else if (wait_writeback
)
981 werr
= filemap_fdatawait_range(mapping
, start
, end
);
982 free_extent_state(cached_state
);
987 atomic_dec(&BTRFS_I(fs_info
->btree_inode
)->sync_writers
);
992 * when btree blocks are allocated, they have some corresponding bits set for
993 * them in one of two extent_io trees. This is used to make sure all of
994 * those extents are on disk for transaction or log commit. We wait
995 * on all the pages and clear them from the dirty pages state tree
997 static int __btrfs_wait_marked_extents(struct btrfs_fs_info
*fs_info
,
998 struct extent_io_tree
*dirty_pages
)
1002 struct address_space
*mapping
= fs_info
->btree_inode
->i_mapping
;
1003 struct extent_state
*cached_state
= NULL
;
1007 while (!find_first_extent_bit(dirty_pages
, start
, &start
, &end
,
1008 EXTENT_NEED_WAIT
, &cached_state
)) {
1010 * Ignore -ENOMEM errors returned by clear_extent_bit().
1011 * When committing the transaction, we'll remove any entries
1012 * left in the io tree. For a log commit, we don't remove them
1013 * after committing the log because the tree can be accessed
1014 * concurrently - we do it only at transaction commit time when
1015 * it's safe to do it (through clear_btree_io_tree()).
1017 err
= clear_extent_bit(dirty_pages
, start
, end
,
1018 EXTENT_NEED_WAIT
, 0, 0, &cached_state
);
1022 err
= filemap_fdatawait_range(mapping
, start
, end
);
1025 free_extent_state(cached_state
);
1026 cached_state
= NULL
;
1035 int btrfs_wait_extents(struct btrfs_fs_info
*fs_info
,
1036 struct extent_io_tree
*dirty_pages
)
1038 bool errors
= false;
1041 err
= __btrfs_wait_marked_extents(fs_info
, dirty_pages
);
1042 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR
, &fs_info
->flags
))
1050 int btrfs_wait_tree_log_extents(struct btrfs_root
*log_root
, int mark
)
1052 struct btrfs_fs_info
*fs_info
= log_root
->fs_info
;
1053 struct extent_io_tree
*dirty_pages
= &log_root
->dirty_log_pages
;
1054 bool errors
= false;
1057 ASSERT(log_root
->root_key
.objectid
== BTRFS_TREE_LOG_OBJECTID
);
1059 err
= __btrfs_wait_marked_extents(fs_info
, dirty_pages
);
1060 if ((mark
& EXTENT_DIRTY
) &&
1061 test_and_clear_bit(BTRFS_FS_LOG1_ERR
, &fs_info
->flags
))
1064 if ((mark
& EXTENT_NEW
) &&
1065 test_and_clear_bit(BTRFS_FS_LOG2_ERR
, &fs_info
->flags
))
1074 * When btree blocks are allocated the corresponding extents are marked dirty.
1075 * This function ensures such extents are persisted on disk for transaction or
1078 * @trans: transaction whose dirty pages we'd like to write
1080 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle
*trans
)
1084 struct extent_io_tree
*dirty_pages
= &trans
->transaction
->dirty_pages
;
1085 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1086 struct blk_plug plug
;
1088 blk_start_plug(&plug
);
1089 ret
= btrfs_write_marked_extents(fs_info
, dirty_pages
, EXTENT_DIRTY
);
1090 blk_finish_plug(&plug
);
1091 ret2
= btrfs_wait_extents(fs_info
, dirty_pages
);
1093 clear_btree_io_tree(&trans
->transaction
->dirty_pages
);
1104 * this is used to update the root pointer in the tree of tree roots.
1106 * But, in the case of the extent allocation tree, updating the root
1107 * pointer may allocate blocks which may change the root of the extent
1110 * So, this loops and repeats and makes sure the cowonly root didn't
1111 * change while the root pointer was being updated in the metadata.
1113 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
1114 struct btrfs_root
*root
)
1117 u64 old_root_bytenr
;
1119 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1120 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1122 old_root_used
= btrfs_root_used(&root
->root_item
);
1125 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
1126 if (old_root_bytenr
== root
->node
->start
&&
1127 old_root_used
== btrfs_root_used(&root
->root_item
))
1130 btrfs_set_root_node(&root
->root_item
, root
->node
);
1131 ret
= btrfs_update_root(trans
, tree_root
,
1137 old_root_used
= btrfs_root_used(&root
->root_item
);
1144 * update all the cowonly tree roots on disk
1146 * The error handling in this function may not be obvious. Any of the
1147 * failures will cause the file system to go offline. We still need
1148 * to clean up the delayed refs.
1150 static noinline
int commit_cowonly_roots(struct btrfs_trans_handle
*trans
)
1152 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1153 struct list_head
*dirty_bgs
= &trans
->transaction
->dirty_bgs
;
1154 struct list_head
*io_bgs
= &trans
->transaction
->io_bgs
;
1155 struct list_head
*next
;
1156 struct extent_buffer
*eb
;
1159 eb
= btrfs_lock_root_node(fs_info
->tree_root
);
1160 ret
= btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
,
1162 btrfs_tree_unlock(eb
);
1163 free_extent_buffer(eb
);
1168 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1172 ret
= btrfs_run_dev_stats(trans
, fs_info
);
1175 ret
= btrfs_run_dev_replace(trans
, fs_info
);
1178 ret
= btrfs_run_qgroups(trans
);
1182 ret
= btrfs_setup_space_cache(trans
, fs_info
);
1186 /* run_qgroups might have added some more refs */
1187 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1191 while (!list_empty(&fs_info
->dirty_cowonly_roots
)) {
1192 struct btrfs_root
*root
;
1193 next
= fs_info
->dirty_cowonly_roots
.next
;
1194 list_del_init(next
);
1195 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
1196 clear_bit(BTRFS_ROOT_DIRTY
, &root
->state
);
1198 if (root
!= fs_info
->extent_root
)
1199 list_add_tail(&root
->dirty_list
,
1200 &trans
->transaction
->switch_commits
);
1201 ret
= update_cowonly_root(trans
, root
);
1204 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1209 while (!list_empty(dirty_bgs
) || !list_empty(io_bgs
)) {
1210 ret
= btrfs_write_dirty_block_groups(trans
, fs_info
);
1213 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1218 if (!list_empty(&fs_info
->dirty_cowonly_roots
))
1221 list_add_tail(&fs_info
->extent_root
->dirty_list
,
1222 &trans
->transaction
->switch_commits
);
1223 btrfs_after_dev_replace_commit(fs_info
);
1229 * dead roots are old snapshots that need to be deleted. This allocates
1230 * a dirty root struct and adds it into the list of dead roots that need to
1233 void btrfs_add_dead_root(struct btrfs_root
*root
)
1235 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1237 spin_lock(&fs_info
->trans_lock
);
1238 if (list_empty(&root
->root_list
))
1239 list_add_tail(&root
->root_list
, &fs_info
->dead_roots
);
1240 spin_unlock(&fs_info
->trans_lock
);
1244 * update all the cowonly tree roots on disk
1246 static noinline
int commit_fs_roots(struct btrfs_trans_handle
*trans
)
1248 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1249 struct btrfs_root
*gang
[8];
1254 spin_lock(&fs_info
->fs_roots_radix_lock
);
1256 ret
= radix_tree_gang_lookup_tag(&fs_info
->fs_roots_radix
,
1259 BTRFS_ROOT_TRANS_TAG
);
1262 for (i
= 0; i
< ret
; i
++) {
1263 struct btrfs_root
*root
= gang
[i
];
1264 radix_tree_tag_clear(&fs_info
->fs_roots_radix
,
1265 (unsigned long)root
->root_key
.objectid
,
1266 BTRFS_ROOT_TRANS_TAG
);
1267 spin_unlock(&fs_info
->fs_roots_radix_lock
);
1269 btrfs_free_log(trans
, root
);
1270 btrfs_update_reloc_root(trans
, root
);
1272 btrfs_save_ino_cache(root
, trans
);
1274 /* see comments in should_cow_block() */
1275 clear_bit(BTRFS_ROOT_FORCE_COW
, &root
->state
);
1276 smp_mb__after_atomic();
1278 if (root
->commit_root
!= root
->node
) {
1279 list_add_tail(&root
->dirty_list
,
1280 &trans
->transaction
->switch_commits
);
1281 btrfs_set_root_node(&root
->root_item
,
1285 err
= btrfs_update_root(trans
, fs_info
->tree_root
,
1288 spin_lock(&fs_info
->fs_roots_radix_lock
);
1291 btrfs_qgroup_free_meta_all_pertrans(root
);
1294 spin_unlock(&fs_info
->fs_roots_radix_lock
);
1299 * defrag a given btree.
1300 * Every leaf in the btree is read and defragged.
1302 int btrfs_defrag_root(struct btrfs_root
*root
)
1304 struct btrfs_fs_info
*info
= root
->fs_info
;
1305 struct btrfs_trans_handle
*trans
;
1308 if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING
, &root
->state
))
1312 trans
= btrfs_start_transaction(root
, 0);
1314 return PTR_ERR(trans
);
1316 ret
= btrfs_defrag_leaves(trans
, root
);
1318 btrfs_end_transaction(trans
);
1319 btrfs_btree_balance_dirty(info
);
1322 if (btrfs_fs_closing(info
) || ret
!= -EAGAIN
)
1325 if (btrfs_defrag_cancelled(info
)) {
1326 btrfs_debug(info
, "defrag_root cancelled");
1331 clear_bit(BTRFS_ROOT_DEFRAG_RUNNING
, &root
->state
);
1336 * Do all special snapshot related qgroup dirty hack.
1338 * Will do all needed qgroup inherit and dirty hack like switch commit
1339 * roots inside one transaction and write all btree into disk, to make
1342 static int qgroup_account_snapshot(struct btrfs_trans_handle
*trans
,
1343 struct btrfs_root
*src
,
1344 struct btrfs_root
*parent
,
1345 struct btrfs_qgroup_inherit
*inherit
,
1348 struct btrfs_fs_info
*fs_info
= src
->fs_info
;
1352 * Save some performance in the case that qgroups are not
1353 * enabled. If this check races with the ioctl, rescan will
1356 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
1360 * Ensure dirty @src will be commited. Or, after comming
1361 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1362 * recorded root will never be updated again, causing an outdated root
1365 record_root_in_trans(trans
, src
, 1);
1368 * We are going to commit transaction, see btrfs_commit_transaction()
1369 * comment for reason locking tree_log_mutex
1371 mutex_lock(&fs_info
->tree_log_mutex
);
1373 ret
= commit_fs_roots(trans
);
1376 ret
= btrfs_qgroup_account_extents(trans
);
1380 /* Now qgroup are all updated, we can inherit it to new qgroups */
1381 ret
= btrfs_qgroup_inherit(trans
, src
->root_key
.objectid
, dst_objectid
,
1387 * Now we do a simplified commit transaction, which will:
1388 * 1) commit all subvolume and extent tree
1389 * To ensure all subvolume and extent tree have a valid
1390 * commit_root to accounting later insert_dir_item()
1391 * 2) write all btree blocks onto disk
1392 * This is to make sure later btree modification will be cowed
1393 * Or commit_root can be populated and cause wrong qgroup numbers
1394 * In this simplified commit, we don't really care about other trees
1395 * like chunk and root tree, as they won't affect qgroup.
1396 * And we don't write super to avoid half committed status.
1398 ret
= commit_cowonly_roots(trans
);
1401 switch_commit_roots(trans
->transaction
);
1402 ret
= btrfs_write_and_wait_transaction(trans
);
1404 btrfs_handle_fs_error(fs_info
, ret
,
1405 "Error while writing out transaction for qgroup");
1408 mutex_unlock(&fs_info
->tree_log_mutex
);
1411 * Force parent root to be updated, as we recorded it before so its
1412 * last_trans == cur_transid.
1413 * Or it won't be committed again onto disk after later
1417 record_root_in_trans(trans
, parent
, 1);
1422 * new snapshots need to be created at a very specific time in the
1423 * transaction commit. This does the actual creation.
1426 * If the error which may affect the commitment of the current transaction
1427 * happens, we should return the error number. If the error which just affect
1428 * the creation of the pending snapshots, just return 0.
1430 static noinline
int create_pending_snapshot(struct btrfs_trans_handle
*trans
,
1431 struct btrfs_pending_snapshot
*pending
)
1434 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1435 struct btrfs_key key
;
1436 struct btrfs_root_item
*new_root_item
;
1437 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1438 struct btrfs_root
*root
= pending
->root
;
1439 struct btrfs_root
*parent_root
;
1440 struct btrfs_block_rsv
*rsv
;
1441 struct inode
*parent_inode
;
1442 struct btrfs_path
*path
;
1443 struct btrfs_dir_item
*dir_item
;
1444 struct dentry
*dentry
;
1445 struct extent_buffer
*tmp
;
1446 struct extent_buffer
*old
;
1447 struct timespec64 cur_time
;
1455 ASSERT(pending
->path
);
1456 path
= pending
->path
;
1458 ASSERT(pending
->root_item
);
1459 new_root_item
= pending
->root_item
;
1461 pending
->error
= btrfs_find_free_objectid(tree_root
, &objectid
);
1463 goto no_free_objectid
;
1466 * Make qgroup to skip current new snapshot's qgroupid, as it is
1467 * accounted by later btrfs_qgroup_inherit().
1469 btrfs_set_skip_qgroup(trans
, objectid
);
1471 btrfs_reloc_pre_snapshot(pending
, &to_reserve
);
1473 if (to_reserve
> 0) {
1474 pending
->error
= btrfs_block_rsv_add(root
,
1475 &pending
->block_rsv
,
1477 BTRFS_RESERVE_NO_FLUSH
);
1479 goto clear_skip_qgroup
;
1482 key
.objectid
= objectid
;
1483 key
.offset
= (u64
)-1;
1484 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1486 rsv
= trans
->block_rsv
;
1487 trans
->block_rsv
= &pending
->block_rsv
;
1488 trans
->bytes_reserved
= trans
->block_rsv
->reserved
;
1489 trace_btrfs_space_reservation(fs_info
, "transaction",
1491 trans
->bytes_reserved
, 1);
1492 dentry
= pending
->dentry
;
1493 parent_inode
= pending
->dir
;
1494 parent_root
= BTRFS_I(parent_inode
)->root
;
1495 record_root_in_trans(trans
, parent_root
, 0);
1497 cur_time
= current_time(parent_inode
);
1500 * insert the directory item
1502 ret
= btrfs_set_inode_index(BTRFS_I(parent_inode
), &index
);
1503 BUG_ON(ret
); /* -ENOMEM */
1505 /* check if there is a file/dir which has the same name. */
1506 dir_item
= btrfs_lookup_dir_item(NULL
, parent_root
, path
,
1507 btrfs_ino(BTRFS_I(parent_inode
)),
1508 dentry
->d_name
.name
,
1509 dentry
->d_name
.len
, 0);
1510 if (dir_item
!= NULL
&& !IS_ERR(dir_item
)) {
1511 pending
->error
= -EEXIST
;
1512 goto dir_item_existed
;
1513 } else if (IS_ERR(dir_item
)) {
1514 ret
= PTR_ERR(dir_item
);
1515 btrfs_abort_transaction(trans
, ret
);
1518 btrfs_release_path(path
);
1521 * pull in the delayed directory update
1522 * and the delayed inode item
1523 * otherwise we corrupt the FS during
1526 ret
= btrfs_run_delayed_items(trans
);
1527 if (ret
) { /* Transaction aborted */
1528 btrfs_abort_transaction(trans
, ret
);
1532 record_root_in_trans(trans
, root
, 0);
1533 btrfs_set_root_last_snapshot(&root
->root_item
, trans
->transid
);
1534 memcpy(new_root_item
, &root
->root_item
, sizeof(*new_root_item
));
1535 btrfs_check_and_init_root_item(new_root_item
);
1537 root_flags
= btrfs_root_flags(new_root_item
);
1538 if (pending
->readonly
)
1539 root_flags
|= BTRFS_ROOT_SUBVOL_RDONLY
;
1541 root_flags
&= ~BTRFS_ROOT_SUBVOL_RDONLY
;
1542 btrfs_set_root_flags(new_root_item
, root_flags
);
1544 btrfs_set_root_generation_v2(new_root_item
,
1546 uuid_le_gen(&new_uuid
);
1547 memcpy(new_root_item
->uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
1548 memcpy(new_root_item
->parent_uuid
, root
->root_item
.uuid
,
1550 if (!(root_flags
& BTRFS_ROOT_SUBVOL_RDONLY
)) {
1551 memset(new_root_item
->received_uuid
, 0,
1552 sizeof(new_root_item
->received_uuid
));
1553 memset(&new_root_item
->stime
, 0, sizeof(new_root_item
->stime
));
1554 memset(&new_root_item
->rtime
, 0, sizeof(new_root_item
->rtime
));
1555 btrfs_set_root_stransid(new_root_item
, 0);
1556 btrfs_set_root_rtransid(new_root_item
, 0);
1558 btrfs_set_stack_timespec_sec(&new_root_item
->otime
, cur_time
.tv_sec
);
1559 btrfs_set_stack_timespec_nsec(&new_root_item
->otime
, cur_time
.tv_nsec
);
1560 btrfs_set_root_otransid(new_root_item
, trans
->transid
);
1562 old
= btrfs_lock_root_node(root
);
1563 ret
= btrfs_cow_block(trans
, root
, old
, NULL
, 0, &old
);
1565 btrfs_tree_unlock(old
);
1566 free_extent_buffer(old
);
1567 btrfs_abort_transaction(trans
, ret
);
1571 btrfs_set_lock_blocking(old
);
1573 ret
= btrfs_copy_root(trans
, root
, old
, &tmp
, objectid
);
1574 /* clean up in any case */
1575 btrfs_tree_unlock(old
);
1576 free_extent_buffer(old
);
1578 btrfs_abort_transaction(trans
, ret
);
1581 /* see comments in should_cow_block() */
1582 set_bit(BTRFS_ROOT_FORCE_COW
, &root
->state
);
1585 btrfs_set_root_node(new_root_item
, tmp
);
1586 /* record when the snapshot was created in key.offset */
1587 key
.offset
= trans
->transid
;
1588 ret
= btrfs_insert_root(trans
, tree_root
, &key
, new_root_item
);
1589 btrfs_tree_unlock(tmp
);
1590 free_extent_buffer(tmp
);
1592 btrfs_abort_transaction(trans
, ret
);
1597 * insert root back/forward references
1599 ret
= btrfs_add_root_ref(trans
, objectid
,
1600 parent_root
->root_key
.objectid
,
1601 btrfs_ino(BTRFS_I(parent_inode
)), index
,
1602 dentry
->d_name
.name
, dentry
->d_name
.len
);
1604 btrfs_abort_transaction(trans
, ret
);
1608 key
.offset
= (u64
)-1;
1609 pending
->snap
= btrfs_read_fs_root_no_name(fs_info
, &key
);
1610 if (IS_ERR(pending
->snap
)) {
1611 ret
= PTR_ERR(pending
->snap
);
1612 btrfs_abort_transaction(trans
, ret
);
1616 ret
= btrfs_reloc_post_snapshot(trans
, pending
);
1618 btrfs_abort_transaction(trans
, ret
);
1622 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1624 btrfs_abort_transaction(trans
, ret
);
1629 * Do special qgroup accounting for snapshot, as we do some qgroup
1630 * snapshot hack to do fast snapshot.
1631 * To co-operate with that hack, we do hack again.
1632 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1634 ret
= qgroup_account_snapshot(trans
, root
, parent_root
,
1635 pending
->inherit
, objectid
);
1639 ret
= btrfs_insert_dir_item(trans
, parent_root
,
1640 dentry
->d_name
.name
, dentry
->d_name
.len
,
1641 BTRFS_I(parent_inode
), &key
,
1642 BTRFS_FT_DIR
, index
);
1643 /* We have check then name at the beginning, so it is impossible. */
1644 BUG_ON(ret
== -EEXIST
|| ret
== -EOVERFLOW
);
1646 btrfs_abort_transaction(trans
, ret
);
1650 btrfs_i_size_write(BTRFS_I(parent_inode
), parent_inode
->i_size
+
1651 dentry
->d_name
.len
* 2);
1652 parent_inode
->i_mtime
= parent_inode
->i_ctime
=
1653 current_time(parent_inode
);
1654 ret
= btrfs_update_inode_fallback(trans
, parent_root
, parent_inode
);
1656 btrfs_abort_transaction(trans
, ret
);
1659 ret
= btrfs_uuid_tree_add(trans
, new_uuid
.b
, BTRFS_UUID_KEY_SUBVOL
,
1662 btrfs_abort_transaction(trans
, ret
);
1665 if (!btrfs_is_empty_uuid(new_root_item
->received_uuid
)) {
1666 ret
= btrfs_uuid_tree_add(trans
, new_root_item
->received_uuid
,
1667 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
1669 if (ret
&& ret
!= -EEXIST
) {
1670 btrfs_abort_transaction(trans
, ret
);
1675 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
1677 btrfs_abort_transaction(trans
, ret
);
1682 pending
->error
= ret
;
1684 trans
->block_rsv
= rsv
;
1685 trans
->bytes_reserved
= 0;
1687 btrfs_clear_skip_qgroup(trans
);
1689 kfree(new_root_item
);
1690 pending
->root_item
= NULL
;
1691 btrfs_free_path(path
);
1692 pending
->path
= NULL
;
1698 * create all the snapshots we've scheduled for creation
1700 static noinline
int create_pending_snapshots(struct btrfs_trans_handle
*trans
)
1702 struct btrfs_pending_snapshot
*pending
, *next
;
1703 struct list_head
*head
= &trans
->transaction
->pending_snapshots
;
1706 list_for_each_entry_safe(pending
, next
, head
, list
) {
1707 list_del(&pending
->list
);
1708 ret
= create_pending_snapshot(trans
, pending
);
1715 static void update_super_roots(struct btrfs_fs_info
*fs_info
)
1717 struct btrfs_root_item
*root_item
;
1718 struct btrfs_super_block
*super
;
1720 super
= fs_info
->super_copy
;
1722 root_item
= &fs_info
->chunk_root
->root_item
;
1723 super
->chunk_root
= root_item
->bytenr
;
1724 super
->chunk_root_generation
= root_item
->generation
;
1725 super
->chunk_root_level
= root_item
->level
;
1727 root_item
= &fs_info
->tree_root
->root_item
;
1728 super
->root
= root_item
->bytenr
;
1729 super
->generation
= root_item
->generation
;
1730 super
->root_level
= root_item
->level
;
1731 if (btrfs_test_opt(fs_info
, SPACE_CACHE
))
1732 super
->cache_generation
= root_item
->generation
;
1733 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
))
1734 super
->uuid_tree_generation
= root_item
->generation
;
1737 int btrfs_transaction_in_commit(struct btrfs_fs_info
*info
)
1739 struct btrfs_transaction
*trans
;
1742 spin_lock(&info
->trans_lock
);
1743 trans
= info
->running_transaction
;
1745 ret
= (trans
->state
>= TRANS_STATE_COMMIT_START
);
1746 spin_unlock(&info
->trans_lock
);
1750 int btrfs_transaction_blocked(struct btrfs_fs_info
*info
)
1752 struct btrfs_transaction
*trans
;
1755 spin_lock(&info
->trans_lock
);
1756 trans
= info
->running_transaction
;
1758 ret
= is_transaction_blocked(trans
);
1759 spin_unlock(&info
->trans_lock
);
1764 * wait for the current transaction commit to start and block subsequent
1767 static void wait_current_trans_commit_start(struct btrfs_fs_info
*fs_info
,
1768 struct btrfs_transaction
*trans
)
1770 wait_event(fs_info
->transaction_blocked_wait
,
1771 trans
->state
>= TRANS_STATE_COMMIT_START
|| trans
->aborted
);
1775 * wait for the current transaction to start and then become unblocked.
1778 static void wait_current_trans_commit_start_and_unblock(
1779 struct btrfs_fs_info
*fs_info
,
1780 struct btrfs_transaction
*trans
)
1782 wait_event(fs_info
->transaction_wait
,
1783 trans
->state
>= TRANS_STATE_UNBLOCKED
|| trans
->aborted
);
1787 * commit transactions asynchronously. once btrfs_commit_transaction_async
1788 * returns, any subsequent transaction will not be allowed to join.
1790 struct btrfs_async_commit
{
1791 struct btrfs_trans_handle
*newtrans
;
1792 struct work_struct work
;
1795 static void do_async_commit(struct work_struct
*work
)
1797 struct btrfs_async_commit
*ac
=
1798 container_of(work
, struct btrfs_async_commit
, work
);
1801 * We've got freeze protection passed with the transaction.
1802 * Tell lockdep about it.
1804 if (ac
->newtrans
->type
& __TRANS_FREEZABLE
)
1805 __sb_writers_acquired(ac
->newtrans
->fs_info
->sb
, SB_FREEZE_FS
);
1807 current
->journal_info
= ac
->newtrans
;
1809 btrfs_commit_transaction(ac
->newtrans
);
1813 int btrfs_commit_transaction_async(struct btrfs_trans_handle
*trans
,
1814 int wait_for_unblock
)
1816 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1817 struct btrfs_async_commit
*ac
;
1818 struct btrfs_transaction
*cur_trans
;
1820 ac
= kmalloc(sizeof(*ac
), GFP_NOFS
);
1824 INIT_WORK(&ac
->work
, do_async_commit
);
1825 ac
->newtrans
= btrfs_join_transaction(trans
->root
);
1826 if (IS_ERR(ac
->newtrans
)) {
1827 int err
= PTR_ERR(ac
->newtrans
);
1832 /* take transaction reference */
1833 cur_trans
= trans
->transaction
;
1834 refcount_inc(&cur_trans
->use_count
);
1836 btrfs_end_transaction(trans
);
1839 * Tell lockdep we've released the freeze rwsem, since the
1840 * async commit thread will be the one to unlock it.
1842 if (ac
->newtrans
->type
& __TRANS_FREEZABLE
)
1843 __sb_writers_release(fs_info
->sb
, SB_FREEZE_FS
);
1845 schedule_work(&ac
->work
);
1847 /* wait for transaction to start and unblock */
1848 if (wait_for_unblock
)
1849 wait_current_trans_commit_start_and_unblock(fs_info
, cur_trans
);
1851 wait_current_trans_commit_start(fs_info
, cur_trans
);
1853 if (current
->journal_info
== trans
)
1854 current
->journal_info
= NULL
;
1856 btrfs_put_transaction(cur_trans
);
1861 static void cleanup_transaction(struct btrfs_trans_handle
*trans
, int err
)
1863 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1864 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1867 WARN_ON(refcount_read(&trans
->use_count
) > 1);
1869 btrfs_abort_transaction(trans
, err
);
1871 spin_lock(&fs_info
->trans_lock
);
1874 * If the transaction is removed from the list, it means this
1875 * transaction has been committed successfully, so it is impossible
1876 * to call the cleanup function.
1878 BUG_ON(list_empty(&cur_trans
->list
));
1880 list_del_init(&cur_trans
->list
);
1881 if (cur_trans
== fs_info
->running_transaction
) {
1882 cur_trans
->state
= TRANS_STATE_COMMIT_DOING
;
1883 spin_unlock(&fs_info
->trans_lock
);
1884 wait_event(cur_trans
->writer_wait
,
1885 atomic_read(&cur_trans
->num_writers
) == 1);
1887 spin_lock(&fs_info
->trans_lock
);
1889 spin_unlock(&fs_info
->trans_lock
);
1891 btrfs_cleanup_one_transaction(trans
->transaction
, fs_info
);
1893 spin_lock(&fs_info
->trans_lock
);
1894 if (cur_trans
== fs_info
->running_transaction
)
1895 fs_info
->running_transaction
= NULL
;
1896 spin_unlock(&fs_info
->trans_lock
);
1898 if (trans
->type
& __TRANS_FREEZABLE
)
1899 sb_end_intwrite(fs_info
->sb
);
1900 btrfs_put_transaction(cur_trans
);
1901 btrfs_put_transaction(cur_trans
);
1903 trace_btrfs_transaction_commit(trans
->root
);
1905 if (current
->journal_info
== trans
)
1906 current
->journal_info
= NULL
;
1907 btrfs_scrub_cancel(fs_info
);
1909 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
1912 static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info
*fs_info
)
1915 * We use writeback_inodes_sb here because if we used
1916 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1917 * Currently are holding the fs freeze lock, if we do an async flush
1918 * we'll do btrfs_join_transaction() and deadlock because we need to
1919 * wait for the fs freeze lock. Using the direct flushing we benefit
1920 * from already being in a transaction and our join_transaction doesn't
1921 * have to re-take the fs freeze lock.
1923 if (btrfs_test_opt(fs_info
, FLUSHONCOMMIT
))
1924 writeback_inodes_sb(fs_info
->sb
, WB_REASON_SYNC
);
1928 static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info
*fs_info
)
1930 if (btrfs_test_opt(fs_info
, FLUSHONCOMMIT
))
1931 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
1935 btrfs_wait_pending_ordered(struct btrfs_transaction
*cur_trans
)
1937 wait_event(cur_trans
->pending_wait
,
1938 atomic_read(&cur_trans
->pending_ordered
) == 0);
1941 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
)
1943 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
1944 struct btrfs_transaction
*cur_trans
= trans
->transaction
;
1945 struct btrfs_transaction
*prev_trans
= NULL
;
1949 * Some places just start a transaction to commit it. We need to make
1950 * sure that if this commit fails that the abort code actually marks the
1951 * transaction as failed, so set trans->dirty to make the abort code do
1954 trans
->dirty
= true;
1956 /* Stop the commit early if ->aborted is set */
1957 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
1958 ret
= cur_trans
->aborted
;
1959 btrfs_end_transaction(trans
);
1963 btrfs_trans_release_metadata(trans
);
1964 trans
->block_rsv
= NULL
;
1966 /* make a pass through all the delayed refs we have so far
1967 * any runnings procs may add more while we are here
1969 ret
= btrfs_run_delayed_refs(trans
, 0);
1971 btrfs_end_transaction(trans
);
1975 cur_trans
= trans
->transaction
;
1978 * set the flushing flag so procs in this transaction have to
1979 * start sending their work down.
1981 cur_trans
->delayed_refs
.flushing
= 1;
1984 if (!list_empty(&trans
->new_bgs
))
1985 btrfs_create_pending_block_groups(trans
);
1987 ret
= btrfs_run_delayed_refs(trans
, 0);
1989 btrfs_end_transaction(trans
);
1993 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN
, &cur_trans
->flags
)) {
1996 /* this mutex is also taken before trying to set
1997 * block groups readonly. We need to make sure
1998 * that nobody has set a block group readonly
1999 * after a extents from that block group have been
2000 * allocated for cache files. btrfs_set_block_group_ro
2001 * will wait for the transaction to commit if it
2002 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
2004 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2005 * only one process starts all the block group IO. It wouldn't
2006 * hurt to have more than one go through, but there's no
2007 * real advantage to it either.
2009 mutex_lock(&fs_info
->ro_block_group_mutex
);
2010 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN
,
2013 mutex_unlock(&fs_info
->ro_block_group_mutex
);
2016 ret
= btrfs_start_dirty_block_groups(trans
);
2018 btrfs_end_transaction(trans
);
2024 spin_lock(&fs_info
->trans_lock
);
2025 if (cur_trans
->state
>= TRANS_STATE_COMMIT_START
) {
2026 spin_unlock(&fs_info
->trans_lock
);
2027 refcount_inc(&cur_trans
->use_count
);
2028 ret
= btrfs_end_transaction(trans
);
2030 wait_for_commit(cur_trans
);
2032 if (unlikely(cur_trans
->aborted
))
2033 ret
= cur_trans
->aborted
;
2035 btrfs_put_transaction(cur_trans
);
2040 cur_trans
->state
= TRANS_STATE_COMMIT_START
;
2041 wake_up(&fs_info
->transaction_blocked_wait
);
2043 if (cur_trans
->list
.prev
!= &fs_info
->trans_list
) {
2044 prev_trans
= list_entry(cur_trans
->list
.prev
,
2045 struct btrfs_transaction
, list
);
2046 if (prev_trans
->state
!= TRANS_STATE_COMPLETED
) {
2047 refcount_inc(&prev_trans
->use_count
);
2048 spin_unlock(&fs_info
->trans_lock
);
2050 wait_for_commit(prev_trans
);
2051 ret
= prev_trans
->aborted
;
2053 btrfs_put_transaction(prev_trans
);
2055 goto cleanup_transaction
;
2057 spin_unlock(&fs_info
->trans_lock
);
2060 spin_unlock(&fs_info
->trans_lock
);
2062 * The previous transaction was aborted and was already removed
2063 * from the list of transactions at fs_info->trans_list. So we
2064 * abort to prevent writing a new superblock that reflects a
2065 * corrupt state (pointing to trees with unwritten nodes/leafs).
2067 if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED
, &fs_info
->fs_state
)) {
2069 goto cleanup_transaction
;
2073 extwriter_counter_dec(cur_trans
, trans
->type
);
2075 ret
= btrfs_start_delalloc_flush(fs_info
);
2077 goto cleanup_transaction
;
2079 ret
= btrfs_run_delayed_items(trans
);
2081 goto cleanup_transaction
;
2083 wait_event(cur_trans
->writer_wait
,
2084 extwriter_counter_read(cur_trans
) == 0);
2086 /* some pending stuffs might be added after the previous flush. */
2087 ret
= btrfs_run_delayed_items(trans
);
2089 goto cleanup_transaction
;
2091 btrfs_wait_delalloc_flush(fs_info
);
2093 btrfs_wait_pending_ordered(cur_trans
);
2095 btrfs_scrub_pause(fs_info
);
2097 * Ok now we need to make sure to block out any other joins while we
2098 * commit the transaction. We could have started a join before setting
2099 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2101 spin_lock(&fs_info
->trans_lock
);
2102 cur_trans
->state
= TRANS_STATE_COMMIT_DOING
;
2103 spin_unlock(&fs_info
->trans_lock
);
2104 wait_event(cur_trans
->writer_wait
,
2105 atomic_read(&cur_trans
->num_writers
) == 1);
2107 /* ->aborted might be set after the previous check, so check it */
2108 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
2109 ret
= cur_trans
->aborted
;
2110 goto scrub_continue
;
2113 * the reloc mutex makes sure that we stop
2114 * the balancing code from coming in and moving
2115 * extents around in the middle of the commit
2117 mutex_lock(&fs_info
->reloc_mutex
);
2120 * We needn't worry about the delayed items because we will
2121 * deal with them in create_pending_snapshot(), which is the
2122 * core function of the snapshot creation.
2124 ret
= create_pending_snapshots(trans
);
2126 mutex_unlock(&fs_info
->reloc_mutex
);
2127 goto scrub_continue
;
2131 * We insert the dir indexes of the snapshots and update the inode
2132 * of the snapshots' parents after the snapshot creation, so there
2133 * are some delayed items which are not dealt with. Now deal with
2136 * We needn't worry that this operation will corrupt the snapshots,
2137 * because all the tree which are snapshoted will be forced to COW
2138 * the nodes and leaves.
2140 ret
= btrfs_run_delayed_items(trans
);
2142 mutex_unlock(&fs_info
->reloc_mutex
);
2143 goto scrub_continue
;
2146 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
2148 mutex_unlock(&fs_info
->reloc_mutex
);
2149 goto scrub_continue
;
2153 * make sure none of the code above managed to slip in a
2156 btrfs_assert_delayed_root_empty(fs_info
);
2158 WARN_ON(cur_trans
!= trans
->transaction
);
2160 /* btrfs_commit_tree_roots is responsible for getting the
2161 * various roots consistent with each other. Every pointer
2162 * in the tree of tree roots has to point to the most up to date
2163 * root for every subvolume and other tree. So, we have to keep
2164 * the tree logging code from jumping in and changing any
2167 * At this point in the commit, there can't be any tree-log
2168 * writers, but a little lower down we drop the trans mutex
2169 * and let new people in. By holding the tree_log_mutex
2170 * from now until after the super is written, we avoid races
2171 * with the tree-log code.
2173 mutex_lock(&fs_info
->tree_log_mutex
);
2175 ret
= commit_fs_roots(trans
);
2177 mutex_unlock(&fs_info
->tree_log_mutex
);
2178 mutex_unlock(&fs_info
->reloc_mutex
);
2179 goto scrub_continue
;
2183 * Since the transaction is done, we can apply the pending changes
2184 * before the next transaction.
2186 btrfs_apply_pending_changes(fs_info
);
2188 /* commit_fs_roots gets rid of all the tree log roots, it is now
2189 * safe to free the root of tree log roots
2191 btrfs_free_log_root_tree(trans
, fs_info
);
2194 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2195 * new delayed refs. Must handle them or qgroup can be wrong.
2197 ret
= btrfs_run_delayed_refs(trans
, (unsigned long)-1);
2199 mutex_unlock(&fs_info
->tree_log_mutex
);
2200 mutex_unlock(&fs_info
->reloc_mutex
);
2201 goto scrub_continue
;
2205 * Since fs roots are all committed, we can get a quite accurate
2206 * new_roots. So let's do quota accounting.
2208 ret
= btrfs_qgroup_account_extents(trans
);
2210 mutex_unlock(&fs_info
->tree_log_mutex
);
2211 mutex_unlock(&fs_info
->reloc_mutex
);
2212 goto scrub_continue
;
2215 ret
= commit_cowonly_roots(trans
);
2217 mutex_unlock(&fs_info
->tree_log_mutex
);
2218 mutex_unlock(&fs_info
->reloc_mutex
);
2219 goto scrub_continue
;
2223 * The tasks which save the space cache and inode cache may also
2224 * update ->aborted, check it.
2226 if (unlikely(READ_ONCE(cur_trans
->aborted
))) {
2227 ret
= cur_trans
->aborted
;
2228 mutex_unlock(&fs_info
->tree_log_mutex
);
2229 mutex_unlock(&fs_info
->reloc_mutex
);
2230 goto scrub_continue
;
2233 btrfs_prepare_extent_commit(fs_info
);
2235 cur_trans
= fs_info
->running_transaction
;
2237 btrfs_set_root_node(&fs_info
->tree_root
->root_item
,
2238 fs_info
->tree_root
->node
);
2239 list_add_tail(&fs_info
->tree_root
->dirty_list
,
2240 &cur_trans
->switch_commits
);
2242 btrfs_set_root_node(&fs_info
->chunk_root
->root_item
,
2243 fs_info
->chunk_root
->node
);
2244 list_add_tail(&fs_info
->chunk_root
->dirty_list
,
2245 &cur_trans
->switch_commits
);
2247 switch_commit_roots(cur_trans
);
2249 ASSERT(list_empty(&cur_trans
->dirty_bgs
));
2250 ASSERT(list_empty(&cur_trans
->io_bgs
));
2251 update_super_roots(fs_info
);
2253 btrfs_set_super_log_root(fs_info
->super_copy
, 0);
2254 btrfs_set_super_log_root_level(fs_info
->super_copy
, 0);
2255 memcpy(fs_info
->super_for_commit
, fs_info
->super_copy
,
2256 sizeof(*fs_info
->super_copy
));
2258 btrfs_update_commit_device_size(fs_info
);
2259 btrfs_update_commit_device_bytes_used(cur_trans
);
2261 clear_bit(BTRFS_FS_LOG1_ERR
, &fs_info
->flags
);
2262 clear_bit(BTRFS_FS_LOG2_ERR
, &fs_info
->flags
);
2264 btrfs_trans_release_chunk_metadata(trans
);
2266 spin_lock(&fs_info
->trans_lock
);
2267 cur_trans
->state
= TRANS_STATE_UNBLOCKED
;
2268 fs_info
->running_transaction
= NULL
;
2269 spin_unlock(&fs_info
->trans_lock
);
2270 mutex_unlock(&fs_info
->reloc_mutex
);
2272 wake_up(&fs_info
->transaction_wait
);
2274 ret
= btrfs_write_and_wait_transaction(trans
);
2276 btrfs_handle_fs_error(fs_info
, ret
,
2277 "Error while writing out transaction");
2278 mutex_unlock(&fs_info
->tree_log_mutex
);
2279 goto scrub_continue
;
2282 ret
= write_all_supers(fs_info
, 0);
2284 * the super is written, we can safely allow the tree-loggers
2285 * to go about their business
2287 mutex_unlock(&fs_info
->tree_log_mutex
);
2289 goto scrub_continue
;
2291 btrfs_finish_extent_commit(trans
);
2293 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &cur_trans
->flags
))
2294 btrfs_clear_space_info_full(fs_info
);
2296 fs_info
->last_trans_committed
= cur_trans
->transid
;
2298 * We needn't acquire the lock here because there is no other task
2299 * which can change it.
2301 cur_trans
->state
= TRANS_STATE_COMPLETED
;
2302 wake_up(&cur_trans
->commit_wait
);
2303 clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT
, &fs_info
->flags
);
2305 spin_lock(&fs_info
->trans_lock
);
2306 list_del_init(&cur_trans
->list
);
2307 spin_unlock(&fs_info
->trans_lock
);
2309 btrfs_put_transaction(cur_trans
);
2310 btrfs_put_transaction(cur_trans
);
2312 if (trans
->type
& __TRANS_FREEZABLE
)
2313 sb_end_intwrite(fs_info
->sb
);
2315 trace_btrfs_transaction_commit(trans
->root
);
2317 btrfs_scrub_continue(fs_info
);
2319 if (current
->journal_info
== trans
)
2320 current
->journal_info
= NULL
;
2322 kmem_cache_free(btrfs_trans_handle_cachep
, trans
);
2327 btrfs_scrub_continue(fs_info
);
2328 cleanup_transaction
:
2329 btrfs_trans_release_metadata(trans
);
2330 btrfs_trans_release_chunk_metadata(trans
);
2331 trans
->block_rsv
= NULL
;
2332 btrfs_warn(fs_info
, "Skipping commit of aborted transaction.");
2333 if (current
->journal_info
== trans
)
2334 current
->journal_info
= NULL
;
2335 cleanup_transaction(trans
, ret
);
2341 * return < 0 if error
2342 * 0 if there are no more dead_roots at the time of call
2343 * 1 there are more to be processed, call me again
2345 * The return value indicates there are certainly more snapshots to delete, but
2346 * if there comes a new one during processing, it may return 0. We don't mind,
2347 * because btrfs_commit_super will poke cleaner thread and it will process it a
2348 * few seconds later.
2350 int btrfs_clean_one_deleted_snapshot(struct btrfs_root
*root
)
2353 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2355 spin_lock(&fs_info
->trans_lock
);
2356 if (list_empty(&fs_info
->dead_roots
)) {
2357 spin_unlock(&fs_info
->trans_lock
);
2360 root
= list_first_entry(&fs_info
->dead_roots
,
2361 struct btrfs_root
, root_list
);
2362 list_del_init(&root
->root_list
);
2363 spin_unlock(&fs_info
->trans_lock
);
2365 btrfs_debug(fs_info
, "cleaner removing %llu", root
->objectid
);
2367 btrfs_kill_all_delayed_nodes(root
);
2369 if (btrfs_header_backref_rev(root
->node
) <
2370 BTRFS_MIXED_BACKREF_REV
)
2371 ret
= btrfs_drop_snapshot(root
, NULL
, 0, 0);
2373 ret
= btrfs_drop_snapshot(root
, NULL
, 1, 0);
2375 return (ret
< 0) ? 0 : 1;
2378 void btrfs_apply_pending_changes(struct btrfs_fs_info
*fs_info
)
2383 prev
= xchg(&fs_info
->pending_changes
, 0);
2387 bit
= 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE
;
2389 btrfs_set_opt(fs_info
->mount_opt
, INODE_MAP_CACHE
);
2392 bit
= 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE
;
2394 btrfs_clear_opt(fs_info
->mount_opt
, INODE_MAP_CACHE
);
2397 bit
= 1 << BTRFS_PENDING_COMMIT
;
2399 btrfs_debug(fs_info
, "pending commit done");
2404 "unknown pending changes left 0x%lx, ignoring", prev
);