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>
16 #include "transaction.h"
19 #include "inode-map.h"
21 #include "dev-replace.h"
23 #include "block-group.h"
24 #include "space-info.h"
26 #define BTRFS_ROOT_TRANS_TAG 0
29 * Transaction states and transitions
31 * No running transaction (fs tree blocks are not modified)
34 * | Call start_transaction() variants. Except btrfs_join_transaction_nostart().
36 * Transaction N [[TRANS_STATE_RUNNING]]
38 * | New trans handles can be attached to transaction N by calling all
39 * | start_transaction() variants.
42 * | Call btrfs_commit_transaction() on any trans handle attached to
45 * Transaction N [[TRANS_STATE_COMMIT_START]]
47 * | Will wait for previous running transaction to completely finish if there
50 * | Then one of the following happes:
51 * | - Wait for all other trans handle holders to release.
52 * | The btrfs_commit_transaction() caller will do the commit work.
53 * | - Wait for current transaction to be committed by others.
54 * | Other btrfs_commit_transaction() caller will do the commit work.
56 * | At this stage, only btrfs_join_transaction*() variants can attach
57 * | to this running transaction.
58 * | All other variants will wait for current one to finish and attach to
62 * | Caller is chosen to commit transaction N, and all other trans handle
63 * | haven been released.
65 * Transaction N [[TRANS_STATE_COMMIT_DOING]]
67 * | The heavy lifting transaction work is started.
68 * | From running delayed refs (modifying extent tree) to creating pending
69 * | snapshots, running qgroups.
70 * | In short, modify supporting trees to reflect modifications of subvolume
73 * | At this stage, all start_transaction() calls will wait for this
74 * | transaction to finish and attach to transaction N+1.
77 * | Until all supporting trees are updated.
79 * Transaction N [[TRANS_STATE_UNBLOCKED]]
81 * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]]
82 * | need to write them back to disk and update |
85 * | At this stage, new transaction is allowed to |
87 * | All new start_transaction() calls will be |
88 * | attached to transid N+1. |
91 * | Until all tree blocks are super blocks are |
92 * | written to block devices |
94 * Transaction N [[TRANS_STATE_COMPLETED]] V
95 * All tree blocks and super blocks are written. Transaction N+1
96 * This transaction is finished and all its [[TRANS_STATE_COMMIT_START]]
97 * data structures will be cleaned up. | Life goes on
99 static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
100 [TRANS_STATE_RUNNING] = 0U,
101 [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
102 [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
105 __TRANS_JOIN_NOSTART),
106 [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
109 __TRANS_JOIN_NOLOCK |
110 __TRANS_JOIN_NOSTART),
111 [TRANS_STATE_COMPLETED] = (__TRANS_START |
114 __TRANS_JOIN_NOLOCK |
115 __TRANS_JOIN_NOSTART),
118 void btrfs_put_transaction(struct btrfs_transaction *transaction)
120 WARN_ON(refcount_read(&transaction->use_count) == 0);
121 if (refcount_dec_and_test(&transaction->use_count)) {
122 BUG_ON(!list_empty(&transaction->list));
123 WARN_ON(!RB_EMPTY_ROOT(
124 &transaction->delayed_refs.href_root.rb_root));
125 WARN_ON(!RB_EMPTY_ROOT(
126 &transaction->delayed_refs.dirty_extent_root));
127 if (transaction->delayed_refs.pending_csums)
128 btrfs_err(transaction->fs_info,
129 "pending csums is %llu",
130 transaction->delayed_refs.pending_csums);
132 * If any block groups are found in ->deleted_bgs then it's
133 * because the transaction was aborted and a commit did not
134 * happen (things failed before writing the new superblock
135 * and calling btrfs_finish_extent_commit()), so we can not
136 * discard the physical locations of the block groups.
138 while (!list_empty(&transaction->deleted_bgs)) {
139 struct btrfs_block_group *cache;
141 cache = list_first_entry(&transaction->deleted_bgs,
142 struct btrfs_block_group,
144 list_del_init(&cache->bg_list);
145 btrfs_unfreeze_block_group(cache);
146 btrfs_put_block_group(cache);
148 WARN_ON(!list_empty(&transaction->dev_update_list));
153 static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
155 struct btrfs_transaction *cur_trans = trans->transaction;
156 struct btrfs_fs_info *fs_info = trans->fs_info;
157 struct btrfs_root *root, *tmp;
159 down_write(&fs_info->commit_root_sem);
160 list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
162 list_del_init(&root->dirty_list);
163 free_extent_buffer(root->commit_root);
164 root->commit_root = btrfs_root_node(root);
165 if (is_fstree(root->root_key.objectid))
166 btrfs_unpin_free_ino(root);
167 extent_io_tree_release(&root->dirty_log_pages);
168 btrfs_qgroup_clean_swapped_blocks(root);
171 /* We can free old roots now. */
172 spin_lock(&cur_trans->dropped_roots_lock);
173 while (!list_empty(&cur_trans->dropped_roots)) {
174 root = list_first_entry(&cur_trans->dropped_roots,
175 struct btrfs_root, root_list);
176 list_del_init(&root->root_list);
177 spin_unlock(&cur_trans->dropped_roots_lock);
178 btrfs_free_log(trans, root);
179 btrfs_drop_and_free_fs_root(fs_info, root);
180 spin_lock(&cur_trans->dropped_roots_lock);
182 spin_unlock(&cur_trans->dropped_roots_lock);
183 up_write(&fs_info->commit_root_sem);
186 static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
189 if (type & TRANS_EXTWRITERS)
190 atomic_inc(&trans->num_extwriters);
193 static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
196 if (type & TRANS_EXTWRITERS)
197 atomic_dec(&trans->num_extwriters);
200 static inline void extwriter_counter_init(struct btrfs_transaction *trans,
203 atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
206 static inline int extwriter_counter_read(struct btrfs_transaction *trans)
208 return atomic_read(&trans->num_extwriters);
212 * To be called after all the new block groups attached to the transaction
213 * handle have been created (btrfs_create_pending_block_groups()).
215 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
217 struct btrfs_fs_info *fs_info = trans->fs_info;
219 if (!trans->chunk_bytes_reserved)
222 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
224 btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
225 trans->chunk_bytes_reserved, NULL);
226 trans->chunk_bytes_reserved = 0;
230 * either allocate a new transaction or hop into the existing one
232 static noinline int join_transaction(struct btrfs_fs_info *fs_info,
235 struct btrfs_transaction *cur_trans;
237 spin_lock(&fs_info->trans_lock);
239 /* The file system has been taken offline. No new transactions. */
240 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
241 spin_unlock(&fs_info->trans_lock);
245 cur_trans = fs_info->running_transaction;
247 if (TRANS_ABORTED(cur_trans)) {
248 spin_unlock(&fs_info->trans_lock);
249 return cur_trans->aborted;
251 if (btrfs_blocked_trans_types[cur_trans->state] & type) {
252 spin_unlock(&fs_info->trans_lock);
255 refcount_inc(&cur_trans->use_count);
256 atomic_inc(&cur_trans->num_writers);
257 extwriter_counter_inc(cur_trans, type);
258 spin_unlock(&fs_info->trans_lock);
261 spin_unlock(&fs_info->trans_lock);
264 * If we are ATTACH, we just want to catch the current transaction,
265 * and commit it. If there is no transaction, just return ENOENT.
267 if (type == TRANS_ATTACH)
271 * JOIN_NOLOCK only happens during the transaction commit, so
272 * it is impossible that ->running_transaction is NULL
274 BUG_ON(type == TRANS_JOIN_NOLOCK);
276 cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
280 spin_lock(&fs_info->trans_lock);
281 if (fs_info->running_transaction) {
283 * someone started a transaction after we unlocked. Make sure
284 * to redo the checks above
288 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
289 spin_unlock(&fs_info->trans_lock);
294 cur_trans->fs_info = fs_info;
295 atomic_set(&cur_trans->pending_ordered, 0);
296 init_waitqueue_head(&cur_trans->pending_wait);
297 atomic_set(&cur_trans->num_writers, 1);
298 extwriter_counter_init(cur_trans, type);
299 init_waitqueue_head(&cur_trans->writer_wait);
300 init_waitqueue_head(&cur_trans->commit_wait);
301 cur_trans->state = TRANS_STATE_RUNNING;
303 * One for this trans handle, one so it will live on until we
304 * commit the transaction.
306 refcount_set(&cur_trans->use_count, 2);
307 cur_trans->flags = 0;
308 cur_trans->start_time = ktime_get_seconds();
310 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
312 cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
313 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
314 atomic_set(&cur_trans->delayed_refs.num_entries, 0);
317 * although the tree mod log is per file system and not per transaction,
318 * the log must never go across transaction boundaries.
321 if (!list_empty(&fs_info->tree_mod_seq_list))
322 WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
323 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
324 WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
325 atomic64_set(&fs_info->tree_mod_seq, 0);
327 spin_lock_init(&cur_trans->delayed_refs.lock);
329 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
330 INIT_LIST_HEAD(&cur_trans->dev_update_list);
331 INIT_LIST_HEAD(&cur_trans->switch_commits);
332 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
333 INIT_LIST_HEAD(&cur_trans->io_bgs);
334 INIT_LIST_HEAD(&cur_trans->dropped_roots);
335 mutex_init(&cur_trans->cache_write_mutex);
336 spin_lock_init(&cur_trans->dirty_bgs_lock);
337 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
338 spin_lock_init(&cur_trans->dropped_roots_lock);
339 list_add_tail(&cur_trans->list, &fs_info->trans_list);
340 extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
341 IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode);
342 extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
343 IO_TREE_FS_PINNED_EXTENTS, NULL);
344 fs_info->generation++;
345 cur_trans->transid = fs_info->generation;
346 fs_info->running_transaction = cur_trans;
347 cur_trans->aborted = 0;
348 spin_unlock(&fs_info->trans_lock);
354 * This does all the record keeping required to make sure that a shareable root
355 * is properly recorded in a given transaction. This is required to make sure
356 * the old root from before we joined the transaction is deleted when the
357 * transaction commits.
359 static int record_root_in_trans(struct btrfs_trans_handle *trans,
360 struct btrfs_root *root,
363 struct btrfs_fs_info *fs_info = root->fs_info;
365 if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
366 root->last_trans < trans->transid) || force) {
367 WARN_ON(root == fs_info->extent_root);
368 WARN_ON(!force && root->commit_root != root->node);
371 * see below for IN_TRANS_SETUP usage rules
372 * we have the reloc mutex held now, so there
373 * is only one writer in this function
375 set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
377 /* make sure readers find IN_TRANS_SETUP before
378 * they find our root->last_trans update
382 spin_lock(&fs_info->fs_roots_radix_lock);
383 if (root->last_trans == trans->transid && !force) {
384 spin_unlock(&fs_info->fs_roots_radix_lock);
387 radix_tree_tag_set(&fs_info->fs_roots_radix,
388 (unsigned long)root->root_key.objectid,
389 BTRFS_ROOT_TRANS_TAG);
390 spin_unlock(&fs_info->fs_roots_radix_lock);
391 root->last_trans = trans->transid;
393 /* this is pretty tricky. We don't want to
394 * take the relocation lock in btrfs_record_root_in_trans
395 * unless we're really doing the first setup for this root in
398 * Normally we'd use root->last_trans as a flag to decide
399 * if we want to take the expensive mutex.
401 * But, we have to set root->last_trans before we
402 * init the relocation root, otherwise, we trip over warnings
403 * in ctree.c. The solution used here is to flag ourselves
404 * with root IN_TRANS_SETUP. When this is 1, we're still
405 * fixing up the reloc trees and everyone must wait.
407 * When this is zero, they can trust root->last_trans and fly
408 * through btrfs_record_root_in_trans without having to take the
409 * lock. smp_wmb() makes sure that all the writes above are
410 * done before we pop in the zero below
412 btrfs_init_reloc_root(trans, root);
413 smp_mb__before_atomic();
414 clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
420 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
423 struct btrfs_fs_info *fs_info = root->fs_info;
424 struct btrfs_transaction *cur_trans = trans->transaction;
426 /* Add ourselves to the transaction dropped list */
427 spin_lock(&cur_trans->dropped_roots_lock);
428 list_add_tail(&root->root_list, &cur_trans->dropped_roots);
429 spin_unlock(&cur_trans->dropped_roots_lock);
431 /* Make sure we don't try to update the root at commit time */
432 spin_lock(&fs_info->fs_roots_radix_lock);
433 radix_tree_tag_clear(&fs_info->fs_roots_radix,
434 (unsigned long)root->root_key.objectid,
435 BTRFS_ROOT_TRANS_TAG);
436 spin_unlock(&fs_info->fs_roots_radix_lock);
439 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
442 struct btrfs_fs_info *fs_info = root->fs_info;
444 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
448 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
452 if (root->last_trans == trans->transid &&
453 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
456 mutex_lock(&fs_info->reloc_mutex);
457 record_root_in_trans(trans, root, 0);
458 mutex_unlock(&fs_info->reloc_mutex);
463 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
465 return (trans->state >= TRANS_STATE_COMMIT_START &&
466 trans->state < TRANS_STATE_UNBLOCKED &&
467 !TRANS_ABORTED(trans));
470 /* wait for commit against the current transaction to become unblocked
471 * when this is done, it is safe to start a new transaction, but the current
472 * transaction might not be fully on disk.
474 static void wait_current_trans(struct btrfs_fs_info *fs_info)
476 struct btrfs_transaction *cur_trans;
478 spin_lock(&fs_info->trans_lock);
479 cur_trans = fs_info->running_transaction;
480 if (cur_trans && is_transaction_blocked(cur_trans)) {
481 refcount_inc(&cur_trans->use_count);
482 spin_unlock(&fs_info->trans_lock);
484 wait_event(fs_info->transaction_wait,
485 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
486 TRANS_ABORTED(cur_trans));
487 btrfs_put_transaction(cur_trans);
489 spin_unlock(&fs_info->trans_lock);
493 static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
495 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
498 if (type == TRANS_START)
504 static inline bool need_reserve_reloc_root(struct btrfs_root *root)
506 struct btrfs_fs_info *fs_info = root->fs_info;
508 if (!fs_info->reloc_ctl ||
509 !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
510 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
517 static struct btrfs_trans_handle *
518 start_transaction(struct btrfs_root *root, unsigned int num_items,
519 unsigned int type, enum btrfs_reserve_flush_enum flush,
520 bool enforce_qgroups)
522 struct btrfs_fs_info *fs_info = root->fs_info;
523 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
524 struct btrfs_trans_handle *h;
525 struct btrfs_transaction *cur_trans;
527 u64 qgroup_reserved = 0;
528 bool reloc_reserved = false;
529 bool do_chunk_alloc = false;
532 /* Send isn't supposed to start transactions. */
533 ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
535 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
536 return ERR_PTR(-EROFS);
538 if (current->journal_info) {
539 WARN_ON(type & TRANS_EXTWRITERS);
540 h = current->journal_info;
541 refcount_inc(&h->use_count);
542 WARN_ON(refcount_read(&h->use_count) > 2);
543 h->orig_rsv = h->block_rsv;
549 * Do the reservation before we join the transaction so we can do all
550 * the appropriate flushing if need be.
552 if (num_items && root != fs_info->chunk_root) {
553 struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
554 u64 delayed_refs_bytes = 0;
556 qgroup_reserved = num_items * fs_info->nodesize;
557 ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
563 * We want to reserve all the bytes we may need all at once, so
564 * we only do 1 enospc flushing cycle per transaction start. We
565 * accomplish this by simply assuming we'll do 2 x num_items
566 * worth of delayed refs updates in this trans handle, and
567 * refill that amount for whatever is missing in the reserve.
569 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
570 if (flush == BTRFS_RESERVE_FLUSH_ALL &&
571 delayed_refs_rsv->full == 0) {
572 delayed_refs_bytes = num_bytes;
577 * Do the reservation for the relocation root creation
579 if (need_reserve_reloc_root(root)) {
580 num_bytes += fs_info->nodesize;
581 reloc_reserved = true;
584 ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
587 if (delayed_refs_bytes) {
588 btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
590 num_bytes -= delayed_refs_bytes;
593 if (rsv->space_info->force_alloc)
594 do_chunk_alloc = true;
595 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
596 !delayed_refs_rsv->full) {
598 * Some people call with btrfs_start_transaction(root, 0)
599 * because they can be throttled, but have some other mechanism
600 * for reserving space. We still want these guys to refill the
601 * delayed block_rsv so just add 1 items worth of reservation
604 ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
609 h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
616 * If we are JOIN_NOLOCK we're already committing a transaction and
617 * waiting on this guy, so we don't need to do the sb_start_intwrite
618 * because we're already holding a ref. We need this because we could
619 * have raced in and did an fsync() on a file which can kick a commit
620 * and then we deadlock with somebody doing a freeze.
622 * If we are ATTACH, it means we just want to catch the current
623 * transaction and commit it, so we needn't do sb_start_intwrite().
625 if (type & __TRANS_FREEZABLE)
626 sb_start_intwrite(fs_info->sb);
628 if (may_wait_transaction(fs_info, type))
629 wait_current_trans(fs_info);
632 ret = join_transaction(fs_info, type);
634 wait_current_trans(fs_info);
635 if (unlikely(type == TRANS_ATTACH ||
636 type == TRANS_JOIN_NOSTART))
639 } while (ret == -EBUSY);
644 cur_trans = fs_info->running_transaction;
646 h->transid = cur_trans->transid;
647 h->transaction = cur_trans;
649 refcount_set(&h->use_count, 1);
650 h->fs_info = root->fs_info;
653 h->can_flush_pending_bgs = true;
654 INIT_LIST_HEAD(&h->new_bgs);
657 if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
658 may_wait_transaction(fs_info, type)) {
659 current->journal_info = h;
660 btrfs_commit_transaction(h);
665 trace_btrfs_space_reservation(fs_info, "transaction",
666 h->transid, num_bytes, 1);
667 h->block_rsv = &fs_info->trans_block_rsv;
668 h->bytes_reserved = num_bytes;
669 h->reloc_reserved = reloc_reserved;
673 if (!current->journal_info)
674 current->journal_info = h;
677 * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
678 * ALLOC_FORCE the first run through, and then we won't allocate for
679 * anybody else who races in later. We don't care about the return
682 if (do_chunk_alloc && num_bytes) {
683 u64 flags = h->block_rsv->space_info->flags;
685 btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
686 CHUNK_ALLOC_NO_FORCE);
690 * btrfs_record_root_in_trans() needs to alloc new extents, and may
691 * call btrfs_join_transaction() while we're also starting a
694 * Thus it need to be called after current->journal_info initialized,
695 * or we can deadlock.
697 btrfs_record_root_in_trans(h, root);
702 if (type & __TRANS_FREEZABLE)
703 sb_end_intwrite(fs_info->sb);
704 kmem_cache_free(btrfs_trans_handle_cachep, h);
707 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
710 btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
714 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
715 unsigned int num_items)
717 return start_transaction(root, num_items, TRANS_START,
718 BTRFS_RESERVE_FLUSH_ALL, true);
721 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
722 struct btrfs_root *root,
723 unsigned int num_items)
725 return start_transaction(root, num_items, TRANS_START,
726 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
729 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
731 return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
735 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
737 return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
738 BTRFS_RESERVE_NO_FLUSH, true);
742 * Similar to regular join but it never starts a transaction when none is
743 * running or after waiting for the current one to finish.
745 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
747 return start_transaction(root, 0, TRANS_JOIN_NOSTART,
748 BTRFS_RESERVE_NO_FLUSH, true);
752 * btrfs_attach_transaction() - catch the running transaction
754 * It is used when we want to commit the current the transaction, but
755 * don't want to start a new one.
757 * Note: If this function return -ENOENT, it just means there is no
758 * running transaction. But it is possible that the inactive transaction
759 * is still in the memory, not fully on disk. If you hope there is no
760 * inactive transaction in the fs when -ENOENT is returned, you should
762 * btrfs_attach_transaction_barrier()
764 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
766 return start_transaction(root, 0, TRANS_ATTACH,
767 BTRFS_RESERVE_NO_FLUSH, true);
771 * btrfs_attach_transaction_barrier() - catch the running transaction
773 * It is similar to the above function, the difference is this one
774 * will wait for all the inactive transactions until they fully
777 struct btrfs_trans_handle *
778 btrfs_attach_transaction_barrier(struct btrfs_root *root)
780 struct btrfs_trans_handle *trans;
782 trans = start_transaction(root, 0, TRANS_ATTACH,
783 BTRFS_RESERVE_NO_FLUSH, true);
784 if (trans == ERR_PTR(-ENOENT))
785 btrfs_wait_for_commit(root->fs_info, 0);
790 /* wait for a transaction commit to be fully complete */
791 static noinline void wait_for_commit(struct btrfs_transaction *commit)
793 wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
796 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
798 struct btrfs_transaction *cur_trans = NULL, *t;
802 if (transid <= fs_info->last_trans_committed)
805 /* find specified transaction */
806 spin_lock(&fs_info->trans_lock);
807 list_for_each_entry(t, &fs_info->trans_list, list) {
808 if (t->transid == transid) {
810 refcount_inc(&cur_trans->use_count);
814 if (t->transid > transid) {
819 spin_unlock(&fs_info->trans_lock);
822 * The specified transaction doesn't exist, or we
823 * raced with btrfs_commit_transaction
826 if (transid > fs_info->last_trans_committed)
831 /* find newest transaction that is committing | committed */
832 spin_lock(&fs_info->trans_lock);
833 list_for_each_entry_reverse(t, &fs_info->trans_list,
835 if (t->state >= TRANS_STATE_COMMIT_START) {
836 if (t->state == TRANS_STATE_COMPLETED)
839 refcount_inc(&cur_trans->use_count);
843 spin_unlock(&fs_info->trans_lock);
845 goto out; /* nothing committing|committed */
848 wait_for_commit(cur_trans);
849 btrfs_put_transaction(cur_trans);
854 void btrfs_throttle(struct btrfs_fs_info *fs_info)
856 wait_current_trans(fs_info);
859 static int should_end_transaction(struct btrfs_trans_handle *trans)
861 struct btrfs_fs_info *fs_info = trans->fs_info;
863 if (btrfs_check_space_for_delayed_refs(fs_info))
866 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
869 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
871 struct btrfs_transaction *cur_trans = trans->transaction;
874 if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
875 cur_trans->delayed_refs.flushing)
878 return should_end_transaction(trans);
881 static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
884 struct btrfs_fs_info *fs_info = trans->fs_info;
886 if (!trans->block_rsv) {
887 ASSERT(!trans->bytes_reserved);
891 if (!trans->bytes_reserved)
894 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
895 trace_btrfs_space_reservation(fs_info, "transaction",
896 trans->transid, trans->bytes_reserved, 0);
897 btrfs_block_rsv_release(fs_info, trans->block_rsv,
898 trans->bytes_reserved, NULL);
899 trans->bytes_reserved = 0;
902 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
905 struct btrfs_fs_info *info = trans->fs_info;
906 struct btrfs_transaction *cur_trans = trans->transaction;
909 if (refcount_read(&trans->use_count) > 1) {
910 refcount_dec(&trans->use_count);
911 trans->block_rsv = trans->orig_rsv;
915 btrfs_trans_release_metadata(trans);
916 trans->block_rsv = NULL;
918 btrfs_create_pending_block_groups(trans);
920 btrfs_trans_release_chunk_metadata(trans);
922 if (trans->type & __TRANS_FREEZABLE)
923 sb_end_intwrite(info->sb);
925 WARN_ON(cur_trans != info->running_transaction);
926 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
927 atomic_dec(&cur_trans->num_writers);
928 extwriter_counter_dec(cur_trans, trans->type);
930 cond_wake_up(&cur_trans->writer_wait);
931 btrfs_put_transaction(cur_trans);
933 if (current->journal_info == trans)
934 current->journal_info = NULL;
937 btrfs_run_delayed_iputs(info);
939 if (TRANS_ABORTED(trans) ||
940 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
941 wake_up_process(info->transaction_kthread);
942 if (TRANS_ABORTED(trans))
943 err = trans->aborted;
948 kmem_cache_free(btrfs_trans_handle_cachep, trans);
952 int btrfs_end_transaction(struct btrfs_trans_handle *trans)
954 return __btrfs_end_transaction(trans, 0);
957 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
959 return __btrfs_end_transaction(trans, 1);
963 * when btree blocks are allocated, they have some corresponding bits set for
964 * them in one of two extent_io trees. This is used to make sure all of
965 * those extents are sent to disk but does not wait on them
967 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
968 struct extent_io_tree *dirty_pages, int mark)
972 struct address_space *mapping = fs_info->btree_inode->i_mapping;
973 struct extent_state *cached_state = NULL;
977 atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
978 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
979 mark, &cached_state)) {
980 bool wait_writeback = false;
982 err = convert_extent_bit(dirty_pages, start, end,
984 mark, &cached_state);
986 * convert_extent_bit can return -ENOMEM, which is most of the
987 * time a temporary error. So when it happens, ignore the error
988 * and wait for writeback of this range to finish - because we
989 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
990 * to __btrfs_wait_marked_extents() would not know that
991 * writeback for this range started and therefore wouldn't
992 * wait for it to finish - we don't want to commit a
993 * superblock that points to btree nodes/leafs for which
994 * writeback hasn't finished yet (and without errors).
995 * We cleanup any entries left in the io tree when committing
996 * the transaction (through extent_io_tree_release()).
998 if (err == -ENOMEM) {
1000 wait_writeback = true;
1003 err = filemap_fdatawrite_range(mapping, start, end);
1006 else if (wait_writeback)
1007 werr = filemap_fdatawait_range(mapping, start, end);
1008 free_extent_state(cached_state);
1009 cached_state = NULL;
1013 atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
1018 * when btree blocks are allocated, they have some corresponding bits set for
1019 * them in one of two extent_io trees. This is used to make sure all of
1020 * those extents are on disk for transaction or log commit. We wait
1021 * on all the pages and clear them from the dirty pages state tree
1023 static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
1024 struct extent_io_tree *dirty_pages)
1028 struct address_space *mapping = fs_info->btree_inode->i_mapping;
1029 struct extent_state *cached_state = NULL;
1033 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1034 EXTENT_NEED_WAIT, &cached_state)) {
1036 * Ignore -ENOMEM errors returned by clear_extent_bit().
1037 * When committing the transaction, we'll remove any entries
1038 * left in the io tree. For a log commit, we don't remove them
1039 * after committing the log because the tree can be accessed
1040 * concurrently - we do it only at transaction commit time when
1041 * it's safe to do it (through extent_io_tree_release()).
1043 err = clear_extent_bit(dirty_pages, start, end,
1044 EXTENT_NEED_WAIT, 0, 0, &cached_state);
1048 err = filemap_fdatawait_range(mapping, start, end);
1051 free_extent_state(cached_state);
1052 cached_state = NULL;
1061 static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1062 struct extent_io_tree *dirty_pages)
1064 bool errors = false;
1067 err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1068 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1076 int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1078 struct btrfs_fs_info *fs_info = log_root->fs_info;
1079 struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
1080 bool errors = false;
1083 ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
1085 err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1086 if ((mark & EXTENT_DIRTY) &&
1087 test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1090 if ((mark & EXTENT_NEW) &&
1091 test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1100 * When btree blocks are allocated the corresponding extents are marked dirty.
1101 * This function ensures such extents are persisted on disk for transaction or
1104 * @trans: transaction whose dirty pages we'd like to write
1106 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1110 struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
1111 struct btrfs_fs_info *fs_info = trans->fs_info;
1112 struct blk_plug plug;
1114 blk_start_plug(&plug);
1115 ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
1116 blk_finish_plug(&plug);
1117 ret2 = btrfs_wait_extents(fs_info, dirty_pages);
1119 extent_io_tree_release(&trans->transaction->dirty_pages);
1130 * this is used to update the root pointer in the tree of tree roots.
1132 * But, in the case of the extent allocation tree, updating the root
1133 * pointer may allocate blocks which may change the root of the extent
1136 * So, this loops and repeats and makes sure the cowonly root didn't
1137 * change while the root pointer was being updated in the metadata.
1139 static int update_cowonly_root(struct btrfs_trans_handle *trans,
1140 struct btrfs_root *root)
1143 u64 old_root_bytenr;
1145 struct btrfs_fs_info *fs_info = root->fs_info;
1146 struct btrfs_root *tree_root = fs_info->tree_root;
1148 old_root_used = btrfs_root_used(&root->root_item);
1151 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
1152 if (old_root_bytenr == root->node->start &&
1153 old_root_used == btrfs_root_used(&root->root_item))
1156 btrfs_set_root_node(&root->root_item, root->node);
1157 ret = btrfs_update_root(trans, tree_root,
1163 old_root_used = btrfs_root_used(&root->root_item);
1170 * update all the cowonly tree roots on disk
1172 * The error handling in this function may not be obvious. Any of the
1173 * failures will cause the file system to go offline. We still need
1174 * to clean up the delayed refs.
1176 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
1178 struct btrfs_fs_info *fs_info = trans->fs_info;
1179 struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1180 struct list_head *io_bgs = &trans->transaction->io_bgs;
1181 struct list_head *next;
1182 struct extent_buffer *eb;
1185 eb = btrfs_lock_root_node(fs_info->tree_root);
1186 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1187 0, &eb, BTRFS_NESTING_COW);
1188 btrfs_tree_unlock(eb);
1189 free_extent_buffer(eb);
1194 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1198 ret = btrfs_run_dev_stats(trans);
1201 ret = btrfs_run_dev_replace(trans);
1204 ret = btrfs_run_qgroups(trans);
1208 ret = btrfs_setup_space_cache(trans);
1212 /* run_qgroups might have added some more refs */
1213 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1217 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1218 struct btrfs_root *root;
1219 next = fs_info->dirty_cowonly_roots.next;
1220 list_del_init(next);
1221 root = list_entry(next, struct btrfs_root, dirty_list);
1222 clear_bit(BTRFS_ROOT_DIRTY, &root->state);
1224 if (root != fs_info->extent_root)
1225 list_add_tail(&root->dirty_list,
1226 &trans->transaction->switch_commits);
1227 ret = update_cowonly_root(trans, root);
1230 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1235 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1236 ret = btrfs_write_dirty_block_groups(trans);
1239 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1244 if (!list_empty(&fs_info->dirty_cowonly_roots))
1247 list_add_tail(&fs_info->extent_root->dirty_list,
1248 &trans->transaction->switch_commits);
1250 /* Update dev-replace pointer once everything is committed */
1251 fs_info->dev_replace.committed_cursor_left =
1252 fs_info->dev_replace.cursor_left_last_write_of_item;
1258 * dead roots are old snapshots that need to be deleted. This allocates
1259 * a dirty root struct and adds it into the list of dead roots that need to
1262 void btrfs_add_dead_root(struct btrfs_root *root)
1264 struct btrfs_fs_info *fs_info = root->fs_info;
1266 spin_lock(&fs_info->trans_lock);
1267 if (list_empty(&root->root_list)) {
1268 btrfs_grab_root(root);
1269 list_add_tail(&root->root_list, &fs_info->dead_roots);
1271 spin_unlock(&fs_info->trans_lock);
1275 * update all the cowonly tree roots on disk
1277 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
1279 struct btrfs_fs_info *fs_info = trans->fs_info;
1280 struct btrfs_root *gang[8];
1285 spin_lock(&fs_info->fs_roots_radix_lock);
1287 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1290 BTRFS_ROOT_TRANS_TAG);
1293 for (i = 0; i < ret; i++) {
1294 struct btrfs_root *root = gang[i];
1295 radix_tree_tag_clear(&fs_info->fs_roots_radix,
1296 (unsigned long)root->root_key.objectid,
1297 BTRFS_ROOT_TRANS_TAG);
1298 spin_unlock(&fs_info->fs_roots_radix_lock);
1300 btrfs_free_log(trans, root);
1301 btrfs_update_reloc_root(trans, root);
1303 btrfs_save_ino_cache(root, trans);
1305 /* see comments in should_cow_block() */
1306 clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1307 smp_mb__after_atomic();
1309 if (root->commit_root != root->node) {
1310 list_add_tail(&root->dirty_list,
1311 &trans->transaction->switch_commits);
1312 btrfs_set_root_node(&root->root_item,
1316 err = btrfs_update_root(trans, fs_info->tree_root,
1319 spin_lock(&fs_info->fs_roots_radix_lock);
1322 btrfs_qgroup_free_meta_all_pertrans(root);
1325 spin_unlock(&fs_info->fs_roots_radix_lock);
1330 * defrag a given btree.
1331 * Every leaf in the btree is read and defragged.
1333 int btrfs_defrag_root(struct btrfs_root *root)
1335 struct btrfs_fs_info *info = root->fs_info;
1336 struct btrfs_trans_handle *trans;
1339 if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1343 trans = btrfs_start_transaction(root, 0);
1345 return PTR_ERR(trans);
1347 ret = btrfs_defrag_leaves(trans, root);
1349 btrfs_end_transaction(trans);
1350 btrfs_btree_balance_dirty(info);
1353 if (btrfs_fs_closing(info) || ret != -EAGAIN)
1356 if (btrfs_defrag_cancelled(info)) {
1357 btrfs_debug(info, "defrag_root cancelled");
1362 clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
1367 * Do all special snapshot related qgroup dirty hack.
1369 * Will do all needed qgroup inherit and dirty hack like switch commit
1370 * roots inside one transaction and write all btree into disk, to make
1373 static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *src,
1375 struct btrfs_root *parent,
1376 struct btrfs_qgroup_inherit *inherit,
1379 struct btrfs_fs_info *fs_info = src->fs_info;
1383 * Save some performance in the case that qgroups are not
1384 * enabled. If this check races with the ioctl, rescan will
1387 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1391 * Ensure dirty @src will be committed. Or, after coming
1392 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1393 * recorded root will never be updated again, causing an outdated root
1396 record_root_in_trans(trans, src, 1);
1399 * We are going to commit transaction, see btrfs_commit_transaction()
1400 * comment for reason locking tree_log_mutex
1402 mutex_lock(&fs_info->tree_log_mutex);
1404 ret = commit_fs_roots(trans);
1407 ret = btrfs_qgroup_account_extents(trans);
1411 /* Now qgroup are all updated, we can inherit it to new qgroups */
1412 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1418 * Now we do a simplified commit transaction, which will:
1419 * 1) commit all subvolume and extent tree
1420 * To ensure all subvolume and extent tree have a valid
1421 * commit_root to accounting later insert_dir_item()
1422 * 2) write all btree blocks onto disk
1423 * This is to make sure later btree modification will be cowed
1424 * Or commit_root can be populated and cause wrong qgroup numbers
1425 * In this simplified commit, we don't really care about other trees
1426 * like chunk and root tree, as they won't affect qgroup.
1427 * And we don't write super to avoid half committed status.
1429 ret = commit_cowonly_roots(trans);
1432 switch_commit_roots(trans);
1433 ret = btrfs_write_and_wait_transaction(trans);
1435 btrfs_handle_fs_error(fs_info, ret,
1436 "Error while writing out transaction for qgroup");
1439 mutex_unlock(&fs_info->tree_log_mutex);
1442 * Force parent root to be updated, as we recorded it before so its
1443 * last_trans == cur_transid.
1444 * Or it won't be committed again onto disk after later
1448 record_root_in_trans(trans, parent, 1);
1453 * new snapshots need to be created at a very specific time in the
1454 * transaction commit. This does the actual creation.
1457 * If the error which may affect the commitment of the current transaction
1458 * happens, we should return the error number. If the error which just affect
1459 * the creation of the pending snapshots, just return 0.
1461 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1462 struct btrfs_pending_snapshot *pending)
1465 struct btrfs_fs_info *fs_info = trans->fs_info;
1466 struct btrfs_key key;
1467 struct btrfs_root_item *new_root_item;
1468 struct btrfs_root *tree_root = fs_info->tree_root;
1469 struct btrfs_root *root = pending->root;
1470 struct btrfs_root *parent_root;
1471 struct btrfs_block_rsv *rsv;
1472 struct inode *parent_inode;
1473 struct btrfs_path *path;
1474 struct btrfs_dir_item *dir_item;
1475 struct dentry *dentry;
1476 struct extent_buffer *tmp;
1477 struct extent_buffer *old;
1478 struct timespec64 cur_time;
1485 ASSERT(pending->path);
1486 path = pending->path;
1488 ASSERT(pending->root_item);
1489 new_root_item = pending->root_item;
1491 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1493 goto no_free_objectid;
1496 * Make qgroup to skip current new snapshot's qgroupid, as it is
1497 * accounted by later btrfs_qgroup_inherit().
1499 btrfs_set_skip_qgroup(trans, objectid);
1501 btrfs_reloc_pre_snapshot(pending, &to_reserve);
1503 if (to_reserve > 0) {
1504 pending->error = btrfs_block_rsv_add(root,
1505 &pending->block_rsv,
1507 BTRFS_RESERVE_NO_FLUSH);
1509 goto clear_skip_qgroup;
1512 key.objectid = objectid;
1513 key.offset = (u64)-1;
1514 key.type = BTRFS_ROOT_ITEM_KEY;
1516 rsv = trans->block_rsv;
1517 trans->block_rsv = &pending->block_rsv;
1518 trans->bytes_reserved = trans->block_rsv->reserved;
1519 trace_btrfs_space_reservation(fs_info, "transaction",
1521 trans->bytes_reserved, 1);
1522 dentry = pending->dentry;
1523 parent_inode = pending->dir;
1524 parent_root = BTRFS_I(parent_inode)->root;
1525 record_root_in_trans(trans, parent_root, 0);
1527 cur_time = current_time(parent_inode);
1530 * insert the directory item
1532 ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
1533 BUG_ON(ret); /* -ENOMEM */
1535 /* check if there is a file/dir which has the same name. */
1536 dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1537 btrfs_ino(BTRFS_I(parent_inode)),
1538 dentry->d_name.name,
1539 dentry->d_name.len, 0);
1540 if (dir_item != NULL && !IS_ERR(dir_item)) {
1541 pending->error = -EEXIST;
1542 goto dir_item_existed;
1543 } else if (IS_ERR(dir_item)) {
1544 ret = PTR_ERR(dir_item);
1545 btrfs_abort_transaction(trans, ret);
1548 btrfs_release_path(path);
1551 * pull in the delayed directory update
1552 * and the delayed inode item
1553 * otherwise we corrupt the FS during
1556 ret = btrfs_run_delayed_items(trans);
1557 if (ret) { /* Transaction aborted */
1558 btrfs_abort_transaction(trans, ret);
1562 record_root_in_trans(trans, root, 0);
1563 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1564 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1565 btrfs_check_and_init_root_item(new_root_item);
1567 root_flags = btrfs_root_flags(new_root_item);
1568 if (pending->readonly)
1569 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1571 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1572 btrfs_set_root_flags(new_root_item, root_flags);
1574 btrfs_set_root_generation_v2(new_root_item,
1576 generate_random_guid(new_root_item->uuid);
1577 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1579 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1580 memset(new_root_item->received_uuid, 0,
1581 sizeof(new_root_item->received_uuid));
1582 memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1583 memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1584 btrfs_set_root_stransid(new_root_item, 0);
1585 btrfs_set_root_rtransid(new_root_item, 0);
1587 btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1588 btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
1589 btrfs_set_root_otransid(new_root_item, trans->transid);
1591 old = btrfs_lock_root_node(root);
1592 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1595 btrfs_tree_unlock(old);
1596 free_extent_buffer(old);
1597 btrfs_abort_transaction(trans, ret);
1601 btrfs_set_lock_blocking_write(old);
1603 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1604 /* clean up in any case */
1605 btrfs_tree_unlock(old);
1606 free_extent_buffer(old);
1608 btrfs_abort_transaction(trans, ret);
1611 /* see comments in should_cow_block() */
1612 set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1615 btrfs_set_root_node(new_root_item, tmp);
1616 /* record when the snapshot was created in key.offset */
1617 key.offset = trans->transid;
1618 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1619 btrfs_tree_unlock(tmp);
1620 free_extent_buffer(tmp);
1622 btrfs_abort_transaction(trans, ret);
1627 * insert root back/forward references
1629 ret = btrfs_add_root_ref(trans, objectid,
1630 parent_root->root_key.objectid,
1631 btrfs_ino(BTRFS_I(parent_inode)), index,
1632 dentry->d_name.name, dentry->d_name.len);
1634 btrfs_abort_transaction(trans, ret);
1638 key.offset = (u64)-1;
1639 pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev);
1640 if (IS_ERR(pending->snap)) {
1641 ret = PTR_ERR(pending->snap);
1642 pending->snap = NULL;
1643 btrfs_abort_transaction(trans, ret);
1647 ret = btrfs_reloc_post_snapshot(trans, pending);
1649 btrfs_abort_transaction(trans, ret);
1653 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1655 btrfs_abort_transaction(trans, ret);
1660 * Do special qgroup accounting for snapshot, as we do some qgroup
1661 * snapshot hack to do fast snapshot.
1662 * To co-operate with that hack, we do hack again.
1663 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1665 ret = qgroup_account_snapshot(trans, root, parent_root,
1666 pending->inherit, objectid);
1670 ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1671 dentry->d_name.len, BTRFS_I(parent_inode),
1672 &key, BTRFS_FT_DIR, index);
1673 /* We have check then name at the beginning, so it is impossible. */
1674 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
1676 btrfs_abort_transaction(trans, ret);
1680 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
1681 dentry->d_name.len * 2);
1682 parent_inode->i_mtime = parent_inode->i_ctime =
1683 current_time(parent_inode);
1684 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1686 btrfs_abort_transaction(trans, ret);
1689 ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1690 BTRFS_UUID_KEY_SUBVOL,
1693 btrfs_abort_transaction(trans, ret);
1696 if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1697 ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
1698 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1700 if (ret && ret != -EEXIST) {
1701 btrfs_abort_transaction(trans, ret);
1706 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1708 btrfs_abort_transaction(trans, ret);
1713 pending->error = ret;
1715 trans->block_rsv = rsv;
1716 trans->bytes_reserved = 0;
1718 btrfs_clear_skip_qgroup(trans);
1720 kfree(new_root_item);
1721 pending->root_item = NULL;
1722 btrfs_free_path(path);
1723 pending->path = NULL;
1729 * create all the snapshots we've scheduled for creation
1731 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
1733 struct btrfs_pending_snapshot *pending, *next;
1734 struct list_head *head = &trans->transaction->pending_snapshots;
1737 list_for_each_entry_safe(pending, next, head, list) {
1738 list_del(&pending->list);
1739 ret = create_pending_snapshot(trans, pending);
1746 static void update_super_roots(struct btrfs_fs_info *fs_info)
1748 struct btrfs_root_item *root_item;
1749 struct btrfs_super_block *super;
1751 super = fs_info->super_copy;
1753 root_item = &fs_info->chunk_root->root_item;
1754 super->chunk_root = root_item->bytenr;
1755 super->chunk_root_generation = root_item->generation;
1756 super->chunk_root_level = root_item->level;
1758 root_item = &fs_info->tree_root->root_item;
1759 super->root = root_item->bytenr;
1760 super->generation = root_item->generation;
1761 super->root_level = root_item->level;
1762 if (btrfs_test_opt(fs_info, SPACE_CACHE))
1763 super->cache_generation = root_item->generation;
1764 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1765 super->uuid_tree_generation = root_item->generation;
1768 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1770 struct btrfs_transaction *trans;
1773 spin_lock(&info->trans_lock);
1774 trans = info->running_transaction;
1776 ret = (trans->state >= TRANS_STATE_COMMIT_START);
1777 spin_unlock(&info->trans_lock);
1781 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1783 struct btrfs_transaction *trans;
1786 spin_lock(&info->trans_lock);
1787 trans = info->running_transaction;
1789 ret = is_transaction_blocked(trans);
1790 spin_unlock(&info->trans_lock);
1795 * wait for the current transaction commit to start and block subsequent
1798 static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1799 struct btrfs_transaction *trans)
1801 wait_event(fs_info->transaction_blocked_wait,
1802 trans->state >= TRANS_STATE_COMMIT_START ||
1803 TRANS_ABORTED(trans));
1807 * wait for the current transaction to start and then become unblocked.
1810 static void wait_current_trans_commit_start_and_unblock(
1811 struct btrfs_fs_info *fs_info,
1812 struct btrfs_transaction *trans)
1814 wait_event(fs_info->transaction_wait,
1815 trans->state >= TRANS_STATE_UNBLOCKED ||
1816 TRANS_ABORTED(trans));
1820 * commit transactions asynchronously. once btrfs_commit_transaction_async
1821 * returns, any subsequent transaction will not be allowed to join.
1823 struct btrfs_async_commit {
1824 struct btrfs_trans_handle *newtrans;
1825 struct work_struct work;
1828 static void do_async_commit(struct work_struct *work)
1830 struct btrfs_async_commit *ac =
1831 container_of(work, struct btrfs_async_commit, work);
1834 * We've got freeze protection passed with the transaction.
1835 * Tell lockdep about it.
1837 if (ac->newtrans->type & __TRANS_FREEZABLE)
1838 __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
1840 current->journal_info = ac->newtrans;
1842 btrfs_commit_transaction(ac->newtrans);
1846 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1847 int wait_for_unblock)
1849 struct btrfs_fs_info *fs_info = trans->fs_info;
1850 struct btrfs_async_commit *ac;
1851 struct btrfs_transaction *cur_trans;
1853 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1857 INIT_WORK(&ac->work, do_async_commit);
1858 ac->newtrans = btrfs_join_transaction(trans->root);
1859 if (IS_ERR(ac->newtrans)) {
1860 int err = PTR_ERR(ac->newtrans);
1865 /* take transaction reference */
1866 cur_trans = trans->transaction;
1867 refcount_inc(&cur_trans->use_count);
1869 btrfs_end_transaction(trans);
1872 * Tell lockdep we've released the freeze rwsem, since the
1873 * async commit thread will be the one to unlock it.
1875 if (ac->newtrans->type & __TRANS_FREEZABLE)
1876 __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
1878 schedule_work(&ac->work);
1880 /* wait for transaction to start and unblock */
1881 if (wait_for_unblock)
1882 wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1884 wait_current_trans_commit_start(fs_info, cur_trans);
1886 if (current->journal_info == trans)
1887 current->journal_info = NULL;
1889 btrfs_put_transaction(cur_trans);
1894 static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
1896 struct btrfs_fs_info *fs_info = trans->fs_info;
1897 struct btrfs_transaction *cur_trans = trans->transaction;
1899 WARN_ON(refcount_read(&trans->use_count) > 1);
1901 btrfs_abort_transaction(trans, err);
1903 spin_lock(&fs_info->trans_lock);
1906 * If the transaction is removed from the list, it means this
1907 * transaction has been committed successfully, so it is impossible
1908 * to call the cleanup function.
1910 BUG_ON(list_empty(&cur_trans->list));
1912 list_del_init(&cur_trans->list);
1913 if (cur_trans == fs_info->running_transaction) {
1914 cur_trans->state = TRANS_STATE_COMMIT_DOING;
1915 spin_unlock(&fs_info->trans_lock);
1916 wait_event(cur_trans->writer_wait,
1917 atomic_read(&cur_trans->num_writers) == 1);
1919 spin_lock(&fs_info->trans_lock);
1921 spin_unlock(&fs_info->trans_lock);
1923 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
1925 spin_lock(&fs_info->trans_lock);
1926 if (cur_trans == fs_info->running_transaction)
1927 fs_info->running_transaction = NULL;
1928 spin_unlock(&fs_info->trans_lock);
1930 if (trans->type & __TRANS_FREEZABLE)
1931 sb_end_intwrite(fs_info->sb);
1932 btrfs_put_transaction(cur_trans);
1933 btrfs_put_transaction(cur_trans);
1935 trace_btrfs_transaction_commit(trans->root);
1937 if (current->journal_info == trans)
1938 current->journal_info = NULL;
1939 btrfs_scrub_cancel(fs_info);
1941 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1945 * Release reserved delayed ref space of all pending block groups of the
1946 * transaction and remove them from the list
1948 static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
1950 struct btrfs_fs_info *fs_info = trans->fs_info;
1951 struct btrfs_block_group *block_group, *tmp;
1953 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
1954 btrfs_delayed_refs_rsv_release(fs_info, 1);
1955 list_del_init(&block_group->bg_list);
1959 static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
1961 struct btrfs_fs_info *fs_info = trans->fs_info;
1964 * We use writeback_inodes_sb here because if we used
1965 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
1966 * Currently are holding the fs freeze lock, if we do an async flush
1967 * we'll do btrfs_join_transaction() and deadlock because we need to
1968 * wait for the fs freeze lock. Using the direct flushing we benefit
1969 * from already being in a transaction and our join_transaction doesn't
1970 * have to re-take the fs freeze lock.
1972 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
1973 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
1975 struct btrfs_pending_snapshot *pending;
1976 struct list_head *head = &trans->transaction->pending_snapshots;
1979 * Flush dellaloc for any root that is going to be snapshotted.
1980 * This is done to avoid a corrupted version of files, in the
1981 * snapshots, that had both buffered and direct IO writes (even
1982 * if they were done sequentially) due to an unordered update of
1983 * the inode's size on disk.
1985 list_for_each_entry(pending, head, list) {
1988 ret = btrfs_start_delalloc_snapshot(pending->root);
1996 static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
1998 struct btrfs_fs_info *fs_info = trans->fs_info;
2000 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2001 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
2003 struct btrfs_pending_snapshot *pending;
2004 struct list_head *head = &trans->transaction->pending_snapshots;
2007 * Wait for any dellaloc that we started previously for the roots
2008 * that are going to be snapshotted. This is to avoid a corrupted
2009 * version of files in the snapshots that had both buffered and
2010 * direct IO writes (even if they were done sequentially).
2012 list_for_each_entry(pending, head, list)
2013 btrfs_wait_ordered_extents(pending->root,
2014 U64_MAX, 0, U64_MAX);
2018 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
2020 struct btrfs_fs_info *fs_info = trans->fs_info;
2021 struct btrfs_transaction *cur_trans = trans->transaction;
2022 struct btrfs_transaction *prev_trans = NULL;
2025 ASSERT(refcount_read(&trans->use_count) == 1);
2028 * Some places just start a transaction to commit it. We need to make
2029 * sure that if this commit fails that the abort code actually marks the
2030 * transaction as failed, so set trans->dirty to make the abort code do
2033 trans->dirty = true;
2035 /* Stop the commit early if ->aborted is set */
2036 if (TRANS_ABORTED(cur_trans)) {
2037 ret = cur_trans->aborted;
2038 btrfs_end_transaction(trans);
2042 btrfs_trans_release_metadata(trans);
2043 trans->block_rsv = NULL;
2045 /* make a pass through all the delayed refs we have so far
2046 * any runnings procs may add more while we are here
2048 ret = btrfs_run_delayed_refs(trans, 0);
2050 btrfs_end_transaction(trans);
2054 cur_trans = trans->transaction;
2057 * set the flushing flag so procs in this transaction have to
2058 * start sending their work down.
2060 cur_trans->delayed_refs.flushing = 1;
2063 btrfs_create_pending_block_groups(trans);
2065 ret = btrfs_run_delayed_refs(trans, 0);
2067 btrfs_end_transaction(trans);
2071 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
2074 /* this mutex is also taken before trying to set
2075 * block groups readonly. We need to make sure
2076 * that nobody has set a block group readonly
2077 * after a extents from that block group have been
2078 * allocated for cache files. btrfs_set_block_group_ro
2079 * will wait for the transaction to commit if it
2080 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
2082 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2083 * only one process starts all the block group IO. It wouldn't
2084 * hurt to have more than one go through, but there's no
2085 * real advantage to it either.
2087 mutex_lock(&fs_info->ro_block_group_mutex);
2088 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2091 mutex_unlock(&fs_info->ro_block_group_mutex);
2094 ret = btrfs_start_dirty_block_groups(trans);
2096 btrfs_end_transaction(trans);
2102 spin_lock(&fs_info->trans_lock);
2103 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
2104 spin_unlock(&fs_info->trans_lock);
2105 refcount_inc(&cur_trans->use_count);
2106 ret = btrfs_end_transaction(trans);
2108 wait_for_commit(cur_trans);
2110 if (TRANS_ABORTED(cur_trans))
2111 ret = cur_trans->aborted;
2113 btrfs_put_transaction(cur_trans);
2118 cur_trans->state = TRANS_STATE_COMMIT_START;
2119 wake_up(&fs_info->transaction_blocked_wait);
2121 if (cur_trans->list.prev != &fs_info->trans_list) {
2122 prev_trans = list_entry(cur_trans->list.prev,
2123 struct btrfs_transaction, list);
2124 if (prev_trans->state != TRANS_STATE_COMPLETED) {
2125 refcount_inc(&prev_trans->use_count);
2126 spin_unlock(&fs_info->trans_lock);
2128 wait_for_commit(prev_trans);
2129 ret = READ_ONCE(prev_trans->aborted);
2131 btrfs_put_transaction(prev_trans);
2133 goto cleanup_transaction;
2135 spin_unlock(&fs_info->trans_lock);
2138 spin_unlock(&fs_info->trans_lock);
2140 * The previous transaction was aborted and was already removed
2141 * from the list of transactions at fs_info->trans_list. So we
2142 * abort to prevent writing a new superblock that reflects a
2143 * corrupt state (pointing to trees with unwritten nodes/leafs).
2145 if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
2147 goto cleanup_transaction;
2151 extwriter_counter_dec(cur_trans, trans->type);
2153 ret = btrfs_start_delalloc_flush(trans);
2155 goto cleanup_transaction;
2157 ret = btrfs_run_delayed_items(trans);
2159 goto cleanup_transaction;
2161 wait_event(cur_trans->writer_wait,
2162 extwriter_counter_read(cur_trans) == 0);
2164 /* some pending stuffs might be added after the previous flush. */
2165 ret = btrfs_run_delayed_items(trans);
2167 goto cleanup_transaction;
2169 btrfs_wait_delalloc_flush(trans);
2172 * Wait for all ordered extents started by a fast fsync that joined this
2173 * transaction. Otherwise if this transaction commits before the ordered
2174 * extents complete we lose logged data after a power failure.
2176 wait_event(cur_trans->pending_wait,
2177 atomic_read(&cur_trans->pending_ordered) == 0);
2179 btrfs_scrub_pause(fs_info);
2181 * Ok now we need to make sure to block out any other joins while we
2182 * commit the transaction. We could have started a join before setting
2183 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2185 spin_lock(&fs_info->trans_lock);
2186 cur_trans->state = TRANS_STATE_COMMIT_DOING;
2187 spin_unlock(&fs_info->trans_lock);
2188 wait_event(cur_trans->writer_wait,
2189 atomic_read(&cur_trans->num_writers) == 1);
2191 if (TRANS_ABORTED(cur_trans)) {
2192 ret = cur_trans->aborted;
2193 goto scrub_continue;
2196 * the reloc mutex makes sure that we stop
2197 * the balancing code from coming in and moving
2198 * extents around in the middle of the commit
2200 mutex_lock(&fs_info->reloc_mutex);
2203 * We needn't worry about the delayed items because we will
2204 * deal with them in create_pending_snapshot(), which is the
2205 * core function of the snapshot creation.
2207 ret = create_pending_snapshots(trans);
2212 * We insert the dir indexes of the snapshots and update the inode
2213 * of the snapshots' parents after the snapshot creation, so there
2214 * are some delayed items which are not dealt with. Now deal with
2217 * We needn't worry that this operation will corrupt the snapshots,
2218 * because all the tree which are snapshoted will be forced to COW
2219 * the nodes and leaves.
2221 ret = btrfs_run_delayed_items(trans);
2225 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2230 * make sure none of the code above managed to slip in a
2233 btrfs_assert_delayed_root_empty(fs_info);
2235 WARN_ON(cur_trans != trans->transaction);
2237 /* btrfs_commit_tree_roots is responsible for getting the
2238 * various roots consistent with each other. Every pointer
2239 * in the tree of tree roots has to point to the most up to date
2240 * root for every subvolume and other tree. So, we have to keep
2241 * the tree logging code from jumping in and changing any
2244 * At this point in the commit, there can't be any tree-log
2245 * writers, but a little lower down we drop the trans mutex
2246 * and let new people in. By holding the tree_log_mutex
2247 * from now until after the super is written, we avoid races
2248 * with the tree-log code.
2250 mutex_lock(&fs_info->tree_log_mutex);
2252 ret = commit_fs_roots(trans);
2254 goto unlock_tree_log;
2257 * Since the transaction is done, we can apply the pending changes
2258 * before the next transaction.
2260 btrfs_apply_pending_changes(fs_info);
2262 /* commit_fs_roots gets rid of all the tree log roots, it is now
2263 * safe to free the root of tree log roots
2265 btrfs_free_log_root_tree(trans, fs_info);
2268 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2269 * new delayed refs. Must handle them or qgroup can be wrong.
2271 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2273 goto unlock_tree_log;
2276 * Since fs roots are all committed, we can get a quite accurate
2277 * new_roots. So let's do quota accounting.
2279 ret = btrfs_qgroup_account_extents(trans);
2281 goto unlock_tree_log;
2283 ret = commit_cowonly_roots(trans);
2285 goto unlock_tree_log;
2288 * The tasks which save the space cache and inode cache may also
2289 * update ->aborted, check it.
2291 if (TRANS_ABORTED(cur_trans)) {
2292 ret = cur_trans->aborted;
2293 goto unlock_tree_log;
2296 btrfs_prepare_extent_commit(fs_info);
2298 cur_trans = fs_info->running_transaction;
2300 btrfs_set_root_node(&fs_info->tree_root->root_item,
2301 fs_info->tree_root->node);
2302 list_add_tail(&fs_info->tree_root->dirty_list,
2303 &cur_trans->switch_commits);
2305 btrfs_set_root_node(&fs_info->chunk_root->root_item,
2306 fs_info->chunk_root->node);
2307 list_add_tail(&fs_info->chunk_root->dirty_list,
2308 &cur_trans->switch_commits);
2310 switch_commit_roots(trans);
2312 ASSERT(list_empty(&cur_trans->dirty_bgs));
2313 ASSERT(list_empty(&cur_trans->io_bgs));
2314 update_super_roots(fs_info);
2316 btrfs_set_super_log_root(fs_info->super_copy, 0);
2317 btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2318 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2319 sizeof(*fs_info->super_copy));
2321 btrfs_commit_device_sizes(cur_trans);
2323 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2324 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2326 btrfs_trans_release_chunk_metadata(trans);
2328 spin_lock(&fs_info->trans_lock);
2329 cur_trans->state = TRANS_STATE_UNBLOCKED;
2330 fs_info->running_transaction = NULL;
2331 spin_unlock(&fs_info->trans_lock);
2332 mutex_unlock(&fs_info->reloc_mutex);
2334 wake_up(&fs_info->transaction_wait);
2336 ret = btrfs_write_and_wait_transaction(trans);
2338 btrfs_handle_fs_error(fs_info, ret,
2339 "Error while writing out transaction");
2341 * reloc_mutex has been unlocked, tree_log_mutex is still held
2342 * but we can't jump to unlock_tree_log causing double unlock
2344 mutex_unlock(&fs_info->tree_log_mutex);
2345 goto scrub_continue;
2348 ret = write_all_supers(fs_info, 0);
2350 * the super is written, we can safely allow the tree-loggers
2351 * to go about their business
2353 mutex_unlock(&fs_info->tree_log_mutex);
2355 goto scrub_continue;
2357 btrfs_finish_extent_commit(trans);
2359 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2360 btrfs_clear_space_info_full(fs_info);
2362 fs_info->last_trans_committed = cur_trans->transid;
2364 * We needn't acquire the lock here because there is no other task
2365 * which can change it.
2367 cur_trans->state = TRANS_STATE_COMPLETED;
2368 wake_up(&cur_trans->commit_wait);
2370 spin_lock(&fs_info->trans_lock);
2371 list_del_init(&cur_trans->list);
2372 spin_unlock(&fs_info->trans_lock);
2374 btrfs_put_transaction(cur_trans);
2375 btrfs_put_transaction(cur_trans);
2377 if (trans->type & __TRANS_FREEZABLE)
2378 sb_end_intwrite(fs_info->sb);
2380 trace_btrfs_transaction_commit(trans->root);
2382 btrfs_scrub_continue(fs_info);
2384 if (current->journal_info == trans)
2385 current->journal_info = NULL;
2387 kmem_cache_free(btrfs_trans_handle_cachep, trans);
2392 mutex_unlock(&fs_info->tree_log_mutex);
2394 mutex_unlock(&fs_info->reloc_mutex);
2396 btrfs_scrub_continue(fs_info);
2397 cleanup_transaction:
2398 btrfs_trans_release_metadata(trans);
2399 btrfs_cleanup_pending_block_groups(trans);
2400 btrfs_trans_release_chunk_metadata(trans);
2401 trans->block_rsv = NULL;
2402 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
2403 if (current->journal_info == trans)
2404 current->journal_info = NULL;
2405 cleanup_transaction(trans, ret);
2411 * return < 0 if error
2412 * 0 if there are no more dead_roots at the time of call
2413 * 1 there are more to be processed, call me again
2415 * The return value indicates there are certainly more snapshots to delete, but
2416 * if there comes a new one during processing, it may return 0. We don't mind,
2417 * because btrfs_commit_super will poke cleaner thread and it will process it a
2418 * few seconds later.
2420 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2423 struct btrfs_fs_info *fs_info = root->fs_info;
2425 spin_lock(&fs_info->trans_lock);
2426 if (list_empty(&fs_info->dead_roots)) {
2427 spin_unlock(&fs_info->trans_lock);
2430 root = list_first_entry(&fs_info->dead_roots,
2431 struct btrfs_root, root_list);
2432 list_del_init(&root->root_list);
2433 spin_unlock(&fs_info->trans_lock);
2435 btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
2437 btrfs_kill_all_delayed_nodes(root);
2438 if (root->ino_cache_inode) {
2439 iput(root->ino_cache_inode);
2440 root->ino_cache_inode = NULL;
2443 if (btrfs_header_backref_rev(root->node) <
2444 BTRFS_MIXED_BACKREF_REV)
2445 ret = btrfs_drop_snapshot(root, 0, 0);
2447 ret = btrfs_drop_snapshot(root, 1, 0);
2449 btrfs_put_root(root);
2450 return (ret < 0) ? 0 : 1;
2453 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2458 prev = xchg(&fs_info->pending_changes, 0);
2462 bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
2464 btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2467 bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
2469 btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2472 bit = 1 << BTRFS_PENDING_COMMIT;
2474 btrfs_debug(fs_info, "pending commit done");
2479 "unknown pending changes left 0x%lx, ignoring", prev);