1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include <linux/fs.h>
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>
13 #include "misc.h"
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "locking.h"
18 #include "tree-log.h"
19 #include "inode-map.h"
20 #include "volumes.h"
21 #include "dev-replace.h"
22 #include "qgroup.h"
23 #include "block-group.h"
24 #include "space-info.h"
25
26 #define BTRFS_ROOT_TRANS_TAG 0
27
28 /*
29 * Transaction states and transitions
30 *
31 * No running transaction (fs tree blocks are not modified)
32 * |
33 * | To next stage:
34 * | Call start_transaction() variants. Except btrfs_join_transaction_nostart().
35 * V
36 * Transaction N [[TRANS_STATE_RUNNING]]
37 * |
38 * | New trans handles can be attached to transaction N by calling all
39 * | start_transaction() variants.
40 * |
41 * | To next stage:
42 * | Call btrfs_commit_transaction() on any trans handle attached to
43 * | transaction N
44 * V
45 * Transaction N [[TRANS_STATE_COMMIT_START]]
46 * |
47 * | Will wait for previous running transaction to completely finish if there
48 * | is one
49 * |
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.
55 * |
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
59 * | transaction N+1.
60 * |
61 * | To next stage:
62 * | Caller is chosen to commit transaction N, and all other trans handle
63 * | haven been released.
64 * V
65 * Transaction N [[TRANS_STATE_COMMIT_DOING]]
66 * |
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
71 * | trees.
72 * |
73 * | At this stage, all start_transaction() calls will wait for this
74 * | transaction to finish and attach to transaction N+1.
75 * |
76 * | To next stage:
77 * | Until all supporting trees are updated.
78 * V
79 * Transaction N [[TRANS_STATE_UNBLOCKED]]
80 * | Transaction N+1
81 * | All needed trees are modified, thus we only [[TRANS_STATE_RUNNING]]
82 * | need to write them back to disk and update |
83 * | super blocks. |
84 * | |
85 * | At this stage, new transaction is allowed to |
86 * | start. |
87 * | All new start_transaction() calls will be |
88 * | attached to transid N+1. |
89 * | |
90 * | To next stage: |
91 * | Until all tree blocks are super blocks are |
92 * | written to block devices |
93 * V |
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
98 */
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 |
103 __TRANS_ATTACH |
104 __TRANS_JOIN |
105 __TRANS_JOIN_NOSTART),
106 [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
107 __TRANS_ATTACH |
108 __TRANS_JOIN |
109 __TRANS_JOIN_NOLOCK |
110 __TRANS_JOIN_NOSTART),
111 [TRANS_STATE_COMPLETED] = (__TRANS_START |
112 __TRANS_ATTACH |
113 __TRANS_JOIN |
114 __TRANS_JOIN_NOLOCK |
115 __TRANS_JOIN_NOSTART),
116 };
117
btrfs_put_transaction(struct btrfs_transaction * transaction)118 void btrfs_put_transaction(struct btrfs_transaction *transaction)
119 {
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);
131 /*
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.
137 */
138 while (!list_empty(&transaction->deleted_bgs)) {
139 struct btrfs_block_group *cache;
140
141 cache = list_first_entry(&transaction->deleted_bgs,
142 struct btrfs_block_group,
143 bg_list);
144 list_del_init(&cache->bg_list);
145 btrfs_unfreeze_block_group(cache);
146 btrfs_put_block_group(cache);
147 }
148 WARN_ON(!list_empty(&transaction->dev_update_list));
149 kfree(transaction);
150 }
151 }
152
switch_commit_roots(struct btrfs_trans_handle * trans)153 static noinline void switch_commit_roots(struct btrfs_trans_handle *trans)
154 {
155 struct btrfs_transaction *cur_trans = trans->transaction;
156 struct btrfs_fs_info *fs_info = trans->fs_info;
157 struct btrfs_root *root, *tmp;
158 struct btrfs_caching_control *caching_ctl, *next;
159
160 down_write(&fs_info->commit_root_sem);
161 list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
162 dirty_list) {
163 list_del_init(&root->dirty_list);
164 free_extent_buffer(root->commit_root);
165 root->commit_root = btrfs_root_node(root);
166 if (is_fstree(root->root_key.objectid))
167 btrfs_unpin_free_ino(root);
168 extent_io_tree_release(&root->dirty_log_pages);
169 btrfs_qgroup_clean_swapped_blocks(root);
170 }
171
172 /* We can free old roots now. */
173 spin_lock(&cur_trans->dropped_roots_lock);
174 while (!list_empty(&cur_trans->dropped_roots)) {
175 root = list_first_entry(&cur_trans->dropped_roots,
176 struct btrfs_root, root_list);
177 list_del_init(&root->root_list);
178 spin_unlock(&cur_trans->dropped_roots_lock);
179 btrfs_free_log(trans, root);
180 btrfs_drop_and_free_fs_root(fs_info, root);
181 spin_lock(&cur_trans->dropped_roots_lock);
182 }
183 spin_unlock(&cur_trans->dropped_roots_lock);
184
185 /*
186 * We have to update the last_byte_to_unpin under the commit_root_sem,
187 * at the same time we swap out the commit roots.
188 *
189 * This is because we must have a real view of the last spot the caching
190 * kthreads were while caching. Consider the following views of the
191 * extent tree for a block group
192 *
193 * commit root
194 * +----+----+----+----+----+----+----+
195 * |\\\\| |\\\\|\\\\| |\\\\|\\\\|
196 * +----+----+----+----+----+----+----+
197 * 0 1 2 3 4 5 6 7
198 *
199 * new commit root
200 * +----+----+----+----+----+----+----+
201 * | | | |\\\\| | |\\\\|
202 * +----+----+----+----+----+----+----+
203 * 0 1 2 3 4 5 6 7
204 *
205 * If the cache_ctl->progress was at 3, then we are only allowed to
206 * unpin [0,1) and [2,3], because the caching thread has already
207 * processed those extents. We are not allowed to unpin [5,6), because
208 * the caching thread will re-start it's search from 3, and thus find
209 * the hole from [4,6) to add to the free space cache.
210 */
211 list_for_each_entry_safe(caching_ctl, next,
212 &fs_info->caching_block_groups, list) {
213 struct btrfs_block_group *cache = caching_ctl->block_group;
214
215 if (btrfs_block_group_done(cache)) {
216 cache->last_byte_to_unpin = (u64)-1;
217 list_del_init(&caching_ctl->list);
218 btrfs_put_caching_control(caching_ctl);
219 } else {
220 cache->last_byte_to_unpin = caching_ctl->progress;
221 }
222 }
223 up_write(&fs_info->commit_root_sem);
224 }
225
extwriter_counter_inc(struct btrfs_transaction * trans,unsigned int type)226 static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
227 unsigned int type)
228 {
229 if (type & TRANS_EXTWRITERS)
230 atomic_inc(&trans->num_extwriters);
231 }
232
extwriter_counter_dec(struct btrfs_transaction * trans,unsigned int type)233 static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
234 unsigned int type)
235 {
236 if (type & TRANS_EXTWRITERS)
237 atomic_dec(&trans->num_extwriters);
238 }
239
extwriter_counter_init(struct btrfs_transaction * trans,unsigned int type)240 static inline void extwriter_counter_init(struct btrfs_transaction *trans,
241 unsigned int type)
242 {
243 atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
244 }
245
extwriter_counter_read(struct btrfs_transaction * trans)246 static inline int extwriter_counter_read(struct btrfs_transaction *trans)
247 {
248 return atomic_read(&trans->num_extwriters);
249 }
250
251 /*
252 * To be called after all the new block groups attached to the transaction
253 * handle have been created (btrfs_create_pending_block_groups()).
254 */
btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle * trans)255 void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
256 {
257 struct btrfs_fs_info *fs_info = trans->fs_info;
258
259 if (!trans->chunk_bytes_reserved)
260 return;
261
262 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
263
264 btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
265 trans->chunk_bytes_reserved, NULL);
266 trans->chunk_bytes_reserved = 0;
267 }
268
269 /*
270 * either allocate a new transaction or hop into the existing one
271 */
join_transaction(struct btrfs_fs_info * fs_info,unsigned int type)272 static noinline int join_transaction(struct btrfs_fs_info *fs_info,
273 unsigned int type)
274 {
275 struct btrfs_transaction *cur_trans;
276
277 spin_lock(&fs_info->trans_lock);
278 loop:
279 /* The file system has been taken offline. No new transactions. */
280 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
281 spin_unlock(&fs_info->trans_lock);
282 return -EROFS;
283 }
284
285 cur_trans = fs_info->running_transaction;
286 if (cur_trans) {
287 if (TRANS_ABORTED(cur_trans)) {
288 spin_unlock(&fs_info->trans_lock);
289 return cur_trans->aborted;
290 }
291 if (btrfs_blocked_trans_types[cur_trans->state] & type) {
292 spin_unlock(&fs_info->trans_lock);
293 return -EBUSY;
294 }
295 refcount_inc(&cur_trans->use_count);
296 atomic_inc(&cur_trans->num_writers);
297 extwriter_counter_inc(cur_trans, type);
298 spin_unlock(&fs_info->trans_lock);
299 return 0;
300 }
301 spin_unlock(&fs_info->trans_lock);
302
303 /*
304 * If we are ATTACH, we just want to catch the current transaction,
305 * and commit it. If there is no transaction, just return ENOENT.
306 */
307 if (type == TRANS_ATTACH)
308 return -ENOENT;
309
310 /*
311 * JOIN_NOLOCK only happens during the transaction commit, so
312 * it is impossible that ->running_transaction is NULL
313 */
314 BUG_ON(type == TRANS_JOIN_NOLOCK);
315
316 cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
317 if (!cur_trans)
318 return -ENOMEM;
319
320 spin_lock(&fs_info->trans_lock);
321 if (fs_info->running_transaction) {
322 /*
323 * someone started a transaction after we unlocked. Make sure
324 * to redo the checks above
325 */
326 kfree(cur_trans);
327 goto loop;
328 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
329 spin_unlock(&fs_info->trans_lock);
330 kfree(cur_trans);
331 return -EROFS;
332 }
333
334 cur_trans->fs_info = fs_info;
335 atomic_set(&cur_trans->pending_ordered, 0);
336 init_waitqueue_head(&cur_trans->pending_wait);
337 atomic_set(&cur_trans->num_writers, 1);
338 extwriter_counter_init(cur_trans, type);
339 init_waitqueue_head(&cur_trans->writer_wait);
340 init_waitqueue_head(&cur_trans->commit_wait);
341 cur_trans->state = TRANS_STATE_RUNNING;
342 /*
343 * One for this trans handle, one so it will live on until we
344 * commit the transaction.
345 */
346 refcount_set(&cur_trans->use_count, 2);
347 cur_trans->flags = 0;
348 cur_trans->start_time = ktime_get_seconds();
349
350 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
351
352 cur_trans->delayed_refs.href_root = RB_ROOT_CACHED;
353 cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
354 atomic_set(&cur_trans->delayed_refs.num_entries, 0);
355
356 /*
357 * although the tree mod log is per file system and not per transaction,
358 * the log must never go across transaction boundaries.
359 */
360 smp_mb();
361 if (!list_empty(&fs_info->tree_mod_seq_list))
362 WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
363 if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
364 WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
365 atomic64_set(&fs_info->tree_mod_seq, 0);
366
367 spin_lock_init(&cur_trans->delayed_refs.lock);
368
369 INIT_LIST_HEAD(&cur_trans->pending_snapshots);
370 INIT_LIST_HEAD(&cur_trans->dev_update_list);
371 INIT_LIST_HEAD(&cur_trans->switch_commits);
372 INIT_LIST_HEAD(&cur_trans->dirty_bgs);
373 INIT_LIST_HEAD(&cur_trans->io_bgs);
374 INIT_LIST_HEAD(&cur_trans->dropped_roots);
375 mutex_init(&cur_trans->cache_write_mutex);
376 spin_lock_init(&cur_trans->dirty_bgs_lock);
377 INIT_LIST_HEAD(&cur_trans->deleted_bgs);
378 spin_lock_init(&cur_trans->dropped_roots_lock);
379 list_add_tail(&cur_trans->list, &fs_info->trans_list);
380 extent_io_tree_init(fs_info, &cur_trans->dirty_pages,
381 IO_TREE_TRANS_DIRTY_PAGES, fs_info->btree_inode);
382 extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
383 IO_TREE_FS_PINNED_EXTENTS, NULL);
384 fs_info->generation++;
385 cur_trans->transid = fs_info->generation;
386 fs_info->running_transaction = cur_trans;
387 cur_trans->aborted = 0;
388 spin_unlock(&fs_info->trans_lock);
389
390 return 0;
391 }
392
393 /*
394 * This does all the record keeping required to make sure that a shareable root
395 * is properly recorded in a given transaction. This is required to make sure
396 * the old root from before we joined the transaction is deleted when the
397 * transaction commits.
398 */
record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root,int force)399 static int record_root_in_trans(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root,
401 int force)
402 {
403 struct btrfs_fs_info *fs_info = root->fs_info;
404
405 if ((test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
406 root->last_trans < trans->transid) || force) {
407 WARN_ON(root == fs_info->extent_root);
408 WARN_ON(!force && root->commit_root != root->node);
409
410 /*
411 * see below for IN_TRANS_SETUP usage rules
412 * we have the reloc mutex held now, so there
413 * is only one writer in this function
414 */
415 set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
416
417 /* make sure readers find IN_TRANS_SETUP before
418 * they find our root->last_trans update
419 */
420 smp_wmb();
421
422 spin_lock(&fs_info->fs_roots_radix_lock);
423 if (root->last_trans == trans->transid && !force) {
424 spin_unlock(&fs_info->fs_roots_radix_lock);
425 return 0;
426 }
427 radix_tree_tag_set(&fs_info->fs_roots_radix,
428 (unsigned long)root->root_key.objectid,
429 BTRFS_ROOT_TRANS_TAG);
430 spin_unlock(&fs_info->fs_roots_radix_lock);
431 root->last_trans = trans->transid;
432
433 /* this is pretty tricky. We don't want to
434 * take the relocation lock in btrfs_record_root_in_trans
435 * unless we're really doing the first setup for this root in
436 * this transaction.
437 *
438 * Normally we'd use root->last_trans as a flag to decide
439 * if we want to take the expensive mutex.
440 *
441 * But, we have to set root->last_trans before we
442 * init the relocation root, otherwise, we trip over warnings
443 * in ctree.c. The solution used here is to flag ourselves
444 * with root IN_TRANS_SETUP. When this is 1, we're still
445 * fixing up the reloc trees and everyone must wait.
446 *
447 * When this is zero, they can trust root->last_trans and fly
448 * through btrfs_record_root_in_trans without having to take the
449 * lock. smp_wmb() makes sure that all the writes above are
450 * done before we pop in the zero below
451 */
452 btrfs_init_reloc_root(trans, root);
453 smp_mb__before_atomic();
454 clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
455 }
456 return 0;
457 }
458
459
btrfs_add_dropped_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)460 void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root)
462 {
463 struct btrfs_fs_info *fs_info = root->fs_info;
464 struct btrfs_transaction *cur_trans = trans->transaction;
465
466 /* Add ourselves to the transaction dropped list */
467 spin_lock(&cur_trans->dropped_roots_lock);
468 list_add_tail(&root->root_list, &cur_trans->dropped_roots);
469 spin_unlock(&cur_trans->dropped_roots_lock);
470
471 /* Make sure we don't try to update the root at commit time */
472 spin_lock(&fs_info->fs_roots_radix_lock);
473 radix_tree_tag_clear(&fs_info->fs_roots_radix,
474 (unsigned long)root->root_key.objectid,
475 BTRFS_ROOT_TRANS_TAG);
476 spin_unlock(&fs_info->fs_roots_radix_lock);
477 }
478
btrfs_record_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * root)479 int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root)
481 {
482 struct btrfs_fs_info *fs_info = root->fs_info;
483
484 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
485 return 0;
486
487 /*
488 * see record_root_in_trans for comments about IN_TRANS_SETUP usage
489 * and barriers
490 */
491 smp_rmb();
492 if (root->last_trans == trans->transid &&
493 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
494 return 0;
495
496 mutex_lock(&fs_info->reloc_mutex);
497 record_root_in_trans(trans, root, 0);
498 mutex_unlock(&fs_info->reloc_mutex);
499
500 return 0;
501 }
502
is_transaction_blocked(struct btrfs_transaction * trans)503 static inline int is_transaction_blocked(struct btrfs_transaction *trans)
504 {
505 return (trans->state >= TRANS_STATE_COMMIT_START &&
506 trans->state < TRANS_STATE_UNBLOCKED &&
507 !TRANS_ABORTED(trans));
508 }
509
510 /* wait for commit against the current transaction to become unblocked
511 * when this is done, it is safe to start a new transaction, but the current
512 * transaction might not be fully on disk.
513 */
wait_current_trans(struct btrfs_fs_info * fs_info)514 static void wait_current_trans(struct btrfs_fs_info *fs_info)
515 {
516 struct btrfs_transaction *cur_trans;
517
518 spin_lock(&fs_info->trans_lock);
519 cur_trans = fs_info->running_transaction;
520 if (cur_trans && is_transaction_blocked(cur_trans)) {
521 refcount_inc(&cur_trans->use_count);
522 spin_unlock(&fs_info->trans_lock);
523
524 wait_event(fs_info->transaction_wait,
525 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
526 TRANS_ABORTED(cur_trans));
527 btrfs_put_transaction(cur_trans);
528 } else {
529 spin_unlock(&fs_info->trans_lock);
530 }
531 }
532
may_wait_transaction(struct btrfs_fs_info * fs_info,int type)533 static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
534 {
535 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
536 return 0;
537
538 if (type == TRANS_START)
539 return 1;
540
541 return 0;
542 }
543
need_reserve_reloc_root(struct btrfs_root * root)544 static inline bool need_reserve_reloc_root(struct btrfs_root *root)
545 {
546 struct btrfs_fs_info *fs_info = root->fs_info;
547
548 if (!fs_info->reloc_ctl ||
549 !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
550 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
551 root->reloc_root)
552 return false;
553
554 return true;
555 }
556
557 static struct btrfs_trans_handle *
start_transaction(struct btrfs_root * root,unsigned int num_items,unsigned int type,enum btrfs_reserve_flush_enum flush,bool enforce_qgroups)558 start_transaction(struct btrfs_root *root, unsigned int num_items,
559 unsigned int type, enum btrfs_reserve_flush_enum flush,
560 bool enforce_qgroups)
561 {
562 struct btrfs_fs_info *fs_info = root->fs_info;
563 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
564 struct btrfs_trans_handle *h;
565 struct btrfs_transaction *cur_trans;
566 u64 num_bytes = 0;
567 u64 qgroup_reserved = 0;
568 bool reloc_reserved = false;
569 bool do_chunk_alloc = false;
570 int ret;
571
572 /* Send isn't supposed to start transactions. */
573 ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
574
575 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
576 return ERR_PTR(-EROFS);
577
578 if (current->journal_info) {
579 WARN_ON(type & TRANS_EXTWRITERS);
580 h = current->journal_info;
581 refcount_inc(&h->use_count);
582 WARN_ON(refcount_read(&h->use_count) > 2);
583 h->orig_rsv = h->block_rsv;
584 h->block_rsv = NULL;
585 goto got_it;
586 }
587
588 /*
589 * Do the reservation before we join the transaction so we can do all
590 * the appropriate flushing if need be.
591 */
592 if (num_items && root != fs_info->chunk_root) {
593 struct btrfs_block_rsv *rsv = &fs_info->trans_block_rsv;
594 u64 delayed_refs_bytes = 0;
595
596 qgroup_reserved = num_items * fs_info->nodesize;
597 ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
598 enforce_qgroups);
599 if (ret)
600 return ERR_PTR(ret);
601
602 /*
603 * We want to reserve all the bytes we may need all at once, so
604 * we only do 1 enospc flushing cycle per transaction start. We
605 * accomplish this by simply assuming we'll do 2 x num_items
606 * worth of delayed refs updates in this trans handle, and
607 * refill that amount for whatever is missing in the reserve.
608 */
609 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
610 if (flush == BTRFS_RESERVE_FLUSH_ALL &&
611 delayed_refs_rsv->full == 0) {
612 delayed_refs_bytes = num_bytes;
613 num_bytes <<= 1;
614 }
615
616 /*
617 * Do the reservation for the relocation root creation
618 */
619 if (need_reserve_reloc_root(root)) {
620 num_bytes += fs_info->nodesize;
621 reloc_reserved = true;
622 }
623
624 ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
625 if (ret)
626 goto reserve_fail;
627 if (delayed_refs_bytes) {
628 btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
629 delayed_refs_bytes);
630 num_bytes -= delayed_refs_bytes;
631 }
632
633 if (rsv->space_info->force_alloc)
634 do_chunk_alloc = true;
635 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
636 !delayed_refs_rsv->full) {
637 /*
638 * Some people call with btrfs_start_transaction(root, 0)
639 * because they can be throttled, but have some other mechanism
640 * for reserving space. We still want these guys to refill the
641 * delayed block_rsv so just add 1 items worth of reservation
642 * here.
643 */
644 ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
645 if (ret)
646 goto reserve_fail;
647 }
648 again:
649 h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
650 if (!h) {
651 ret = -ENOMEM;
652 goto alloc_fail;
653 }
654
655 /*
656 * If we are JOIN_NOLOCK we're already committing a transaction and
657 * waiting on this guy, so we don't need to do the sb_start_intwrite
658 * because we're already holding a ref. We need this because we could
659 * have raced in and did an fsync() on a file which can kick a commit
660 * and then we deadlock with somebody doing a freeze.
661 *
662 * If we are ATTACH, it means we just want to catch the current
663 * transaction and commit it, so we needn't do sb_start_intwrite().
664 */
665 if (type & __TRANS_FREEZABLE)
666 sb_start_intwrite(fs_info->sb);
667
668 if (may_wait_transaction(fs_info, type))
669 wait_current_trans(fs_info);
670
671 do {
672 ret = join_transaction(fs_info, type);
673 if (ret == -EBUSY) {
674 wait_current_trans(fs_info);
675 if (unlikely(type == TRANS_ATTACH ||
676 type == TRANS_JOIN_NOSTART))
677 ret = -ENOENT;
678 }
679 } while (ret == -EBUSY);
680
681 if (ret < 0)
682 goto join_fail;
683
684 cur_trans = fs_info->running_transaction;
685
686 h->transid = cur_trans->transid;
687 h->transaction = cur_trans;
688 h->root = root;
689 refcount_set(&h->use_count, 1);
690 h->fs_info = root->fs_info;
691
692 h->type = type;
693 h->can_flush_pending_bgs = true;
694 INIT_LIST_HEAD(&h->new_bgs);
695
696 smp_mb();
697 if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
698 may_wait_transaction(fs_info, type)) {
699 current->journal_info = h;
700 btrfs_commit_transaction(h);
701 goto again;
702 }
703
704 if (num_bytes) {
705 trace_btrfs_space_reservation(fs_info, "transaction",
706 h->transid, num_bytes, 1);
707 h->block_rsv = &fs_info->trans_block_rsv;
708 h->bytes_reserved = num_bytes;
709 h->reloc_reserved = reloc_reserved;
710 }
711
712 got_it:
713 if (!current->journal_info)
714 current->journal_info = h;
715
716 /*
717 * If the space_info is marked ALLOC_FORCE then we'll get upgraded to
718 * ALLOC_FORCE the first run through, and then we won't allocate for
719 * anybody else who races in later. We don't care about the return
720 * value here.
721 */
722 if (do_chunk_alloc && num_bytes) {
723 u64 flags = h->block_rsv->space_info->flags;
724
725 btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
726 CHUNK_ALLOC_NO_FORCE);
727 }
728
729 /*
730 * btrfs_record_root_in_trans() needs to alloc new extents, and may
731 * call btrfs_join_transaction() while we're also starting a
732 * transaction.
733 *
734 * Thus it need to be called after current->journal_info initialized,
735 * or we can deadlock.
736 */
737 btrfs_record_root_in_trans(h, root);
738
739 return h;
740
741 join_fail:
742 if (type & __TRANS_FREEZABLE)
743 sb_end_intwrite(fs_info->sb);
744 kmem_cache_free(btrfs_trans_handle_cachep, h);
745 alloc_fail:
746 if (num_bytes)
747 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
748 num_bytes, NULL);
749 reserve_fail:
750 btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
751 return ERR_PTR(ret);
752 }
753
btrfs_start_transaction(struct btrfs_root * root,unsigned int num_items)754 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
755 unsigned int num_items)
756 {
757 return start_transaction(root, num_items, TRANS_START,
758 BTRFS_RESERVE_FLUSH_ALL, true);
759 }
760
btrfs_start_transaction_fallback_global_rsv(struct btrfs_root * root,unsigned int num_items)761 struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
762 struct btrfs_root *root,
763 unsigned int num_items)
764 {
765 return start_transaction(root, num_items, TRANS_START,
766 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
767 }
768
btrfs_join_transaction(struct btrfs_root * root)769 struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
770 {
771 return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
772 true);
773 }
774
btrfs_join_transaction_spacecache(struct btrfs_root * root)775 struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
776 {
777 return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
778 BTRFS_RESERVE_NO_FLUSH, true);
779 }
780
781 /*
782 * Similar to regular join but it never starts a transaction when none is
783 * running or after waiting for the current one to finish.
784 */
btrfs_join_transaction_nostart(struct btrfs_root * root)785 struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
786 {
787 return start_transaction(root, 0, TRANS_JOIN_NOSTART,
788 BTRFS_RESERVE_NO_FLUSH, true);
789 }
790
791 /*
792 * btrfs_attach_transaction() - catch the running transaction
793 *
794 * It is used when we want to commit the current the transaction, but
795 * don't want to start a new one.
796 *
797 * Note: If this function return -ENOENT, it just means there is no
798 * running transaction. But it is possible that the inactive transaction
799 * is still in the memory, not fully on disk. If you hope there is no
800 * inactive transaction in the fs when -ENOENT is returned, you should
801 * invoke
802 * btrfs_attach_transaction_barrier()
803 */
btrfs_attach_transaction(struct btrfs_root * root)804 struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
805 {
806 return start_transaction(root, 0, TRANS_ATTACH,
807 BTRFS_RESERVE_NO_FLUSH, true);
808 }
809
810 /*
811 * btrfs_attach_transaction_barrier() - catch the running transaction
812 *
813 * It is similar to the above function, the difference is this one
814 * will wait for all the inactive transactions until they fully
815 * complete.
816 */
817 struct btrfs_trans_handle *
btrfs_attach_transaction_barrier(struct btrfs_root * root)818 btrfs_attach_transaction_barrier(struct btrfs_root *root)
819 {
820 struct btrfs_trans_handle *trans;
821
822 trans = start_transaction(root, 0, TRANS_ATTACH,
823 BTRFS_RESERVE_NO_FLUSH, true);
824 if (trans == ERR_PTR(-ENOENT))
825 btrfs_wait_for_commit(root->fs_info, 0);
826
827 return trans;
828 }
829
830 /* wait for a transaction commit to be fully complete */
wait_for_commit(struct btrfs_transaction * commit)831 static noinline void wait_for_commit(struct btrfs_transaction *commit)
832 {
833 wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
834 }
835
btrfs_wait_for_commit(struct btrfs_fs_info * fs_info,u64 transid)836 int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
837 {
838 struct btrfs_transaction *cur_trans = NULL, *t;
839 int ret = 0;
840
841 if (transid) {
842 if (transid <= fs_info->last_trans_committed)
843 goto out;
844
845 /* find specified transaction */
846 spin_lock(&fs_info->trans_lock);
847 list_for_each_entry(t, &fs_info->trans_list, list) {
848 if (t->transid == transid) {
849 cur_trans = t;
850 refcount_inc(&cur_trans->use_count);
851 ret = 0;
852 break;
853 }
854 if (t->transid > transid) {
855 ret = 0;
856 break;
857 }
858 }
859 spin_unlock(&fs_info->trans_lock);
860
861 /*
862 * The specified transaction doesn't exist, or we
863 * raced with btrfs_commit_transaction
864 */
865 if (!cur_trans) {
866 if (transid > fs_info->last_trans_committed)
867 ret = -EINVAL;
868 goto out;
869 }
870 } else {
871 /* find newest transaction that is committing | committed */
872 spin_lock(&fs_info->trans_lock);
873 list_for_each_entry_reverse(t, &fs_info->trans_list,
874 list) {
875 if (t->state >= TRANS_STATE_COMMIT_START) {
876 if (t->state == TRANS_STATE_COMPLETED)
877 break;
878 cur_trans = t;
879 refcount_inc(&cur_trans->use_count);
880 break;
881 }
882 }
883 spin_unlock(&fs_info->trans_lock);
884 if (!cur_trans)
885 goto out; /* nothing committing|committed */
886 }
887
888 wait_for_commit(cur_trans);
889 btrfs_put_transaction(cur_trans);
890 out:
891 return ret;
892 }
893
btrfs_throttle(struct btrfs_fs_info * fs_info)894 void btrfs_throttle(struct btrfs_fs_info *fs_info)
895 {
896 wait_current_trans(fs_info);
897 }
898
should_end_transaction(struct btrfs_trans_handle * trans)899 static int should_end_transaction(struct btrfs_trans_handle *trans)
900 {
901 struct btrfs_fs_info *fs_info = trans->fs_info;
902
903 if (btrfs_check_space_for_delayed_refs(fs_info))
904 return 1;
905
906 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
907 }
908
btrfs_should_end_transaction(struct btrfs_trans_handle * trans)909 int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
910 {
911 struct btrfs_transaction *cur_trans = trans->transaction;
912
913 smp_mb();
914 if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
915 cur_trans->delayed_refs.flushing)
916 return 1;
917
918 return should_end_transaction(trans);
919 }
920
btrfs_trans_release_metadata(struct btrfs_trans_handle * trans)921 static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
922
923 {
924 struct btrfs_fs_info *fs_info = trans->fs_info;
925
926 if (!trans->block_rsv) {
927 ASSERT(!trans->bytes_reserved);
928 return;
929 }
930
931 if (!trans->bytes_reserved)
932 return;
933
934 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
935 trace_btrfs_space_reservation(fs_info, "transaction",
936 trans->transid, trans->bytes_reserved, 0);
937 btrfs_block_rsv_release(fs_info, trans->block_rsv,
938 trans->bytes_reserved, NULL);
939 trans->bytes_reserved = 0;
940 }
941
__btrfs_end_transaction(struct btrfs_trans_handle * trans,int throttle)942 static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
943 int throttle)
944 {
945 struct btrfs_fs_info *info = trans->fs_info;
946 struct btrfs_transaction *cur_trans = trans->transaction;
947 int err = 0;
948
949 if (refcount_read(&trans->use_count) > 1) {
950 refcount_dec(&trans->use_count);
951 trans->block_rsv = trans->orig_rsv;
952 return 0;
953 }
954
955 btrfs_trans_release_metadata(trans);
956 trans->block_rsv = NULL;
957
958 btrfs_create_pending_block_groups(trans);
959
960 btrfs_trans_release_chunk_metadata(trans);
961
962 if (trans->type & __TRANS_FREEZABLE)
963 sb_end_intwrite(info->sb);
964
965 WARN_ON(cur_trans != info->running_transaction);
966 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
967 atomic_dec(&cur_trans->num_writers);
968 extwriter_counter_dec(cur_trans, trans->type);
969
970 cond_wake_up(&cur_trans->writer_wait);
971 btrfs_put_transaction(cur_trans);
972
973 if (current->journal_info == trans)
974 current->journal_info = NULL;
975
976 if (throttle)
977 btrfs_run_delayed_iputs(info);
978
979 if (TRANS_ABORTED(trans) ||
980 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
981 wake_up_process(info->transaction_kthread);
982 if (TRANS_ABORTED(trans))
983 err = trans->aborted;
984 else
985 err = -EROFS;
986 }
987
988 kmem_cache_free(btrfs_trans_handle_cachep, trans);
989 return err;
990 }
991
btrfs_end_transaction(struct btrfs_trans_handle * trans)992 int btrfs_end_transaction(struct btrfs_trans_handle *trans)
993 {
994 return __btrfs_end_transaction(trans, 0);
995 }
996
btrfs_end_transaction_throttle(struct btrfs_trans_handle * trans)997 int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
998 {
999 return __btrfs_end_transaction(trans, 1);
1000 }
1001
1002 /*
1003 * when btree blocks are allocated, they have some corresponding bits set for
1004 * them in one of two extent_io trees. This is used to make sure all of
1005 * those extents are sent to disk but does not wait on them
1006 */
btrfs_write_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages,int mark)1007 int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
1008 struct extent_io_tree *dirty_pages, int mark)
1009 {
1010 int err = 0;
1011 int werr = 0;
1012 struct address_space *mapping = fs_info->btree_inode->i_mapping;
1013 struct extent_state *cached_state = NULL;
1014 u64 start = 0;
1015 u64 end;
1016
1017 atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
1018 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1019 mark, &cached_state)) {
1020 bool wait_writeback = false;
1021
1022 err = convert_extent_bit(dirty_pages, start, end,
1023 EXTENT_NEED_WAIT,
1024 mark, &cached_state);
1025 /*
1026 * convert_extent_bit can return -ENOMEM, which is most of the
1027 * time a temporary error. So when it happens, ignore the error
1028 * and wait for writeback of this range to finish - because we
1029 * failed to set the bit EXTENT_NEED_WAIT for the range, a call
1030 * to __btrfs_wait_marked_extents() would not know that
1031 * writeback for this range started and therefore wouldn't
1032 * wait for it to finish - we don't want to commit a
1033 * superblock that points to btree nodes/leafs for which
1034 * writeback hasn't finished yet (and without errors).
1035 * We cleanup any entries left in the io tree when committing
1036 * the transaction (through extent_io_tree_release()).
1037 */
1038 if (err == -ENOMEM) {
1039 err = 0;
1040 wait_writeback = true;
1041 }
1042 if (!err)
1043 err = filemap_fdatawrite_range(mapping, start, end);
1044 if (err)
1045 werr = err;
1046 else if (wait_writeback)
1047 werr = filemap_fdatawait_range(mapping, start, end);
1048 free_extent_state(cached_state);
1049 cached_state = NULL;
1050 cond_resched();
1051 start = end + 1;
1052 }
1053 atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
1054 return werr;
1055 }
1056
1057 /*
1058 * when btree blocks are allocated, they have some corresponding bits set for
1059 * them in one of two extent_io trees. This is used to make sure all of
1060 * those extents are on disk for transaction or log commit. We wait
1061 * on all the pages and clear them from the dirty pages state tree
1062 */
__btrfs_wait_marked_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)1063 static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
1064 struct extent_io_tree *dirty_pages)
1065 {
1066 int err = 0;
1067 int werr = 0;
1068 struct address_space *mapping = fs_info->btree_inode->i_mapping;
1069 struct extent_state *cached_state = NULL;
1070 u64 start = 0;
1071 u64 end;
1072
1073 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1074 EXTENT_NEED_WAIT, &cached_state)) {
1075 /*
1076 * Ignore -ENOMEM errors returned by clear_extent_bit().
1077 * When committing the transaction, we'll remove any entries
1078 * left in the io tree. For a log commit, we don't remove them
1079 * after committing the log because the tree can be accessed
1080 * concurrently - we do it only at transaction commit time when
1081 * it's safe to do it (through extent_io_tree_release()).
1082 */
1083 err = clear_extent_bit(dirty_pages, start, end,
1084 EXTENT_NEED_WAIT, 0, 0, &cached_state);
1085 if (err == -ENOMEM)
1086 err = 0;
1087 if (!err)
1088 err = filemap_fdatawait_range(mapping, start, end);
1089 if (err)
1090 werr = err;
1091 free_extent_state(cached_state);
1092 cached_state = NULL;
1093 cond_resched();
1094 start = end + 1;
1095 }
1096 if (err)
1097 werr = err;
1098 return werr;
1099 }
1100
btrfs_wait_extents(struct btrfs_fs_info * fs_info,struct extent_io_tree * dirty_pages)1101 static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1102 struct extent_io_tree *dirty_pages)
1103 {
1104 bool errors = false;
1105 int err;
1106
1107 err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1108 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1109 errors = true;
1110
1111 if (errors && !err)
1112 err = -EIO;
1113 return err;
1114 }
1115
btrfs_wait_tree_log_extents(struct btrfs_root * log_root,int mark)1116 int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1117 {
1118 struct btrfs_fs_info *fs_info = log_root->fs_info;
1119 struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
1120 bool errors = false;
1121 int err;
1122
1123 ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
1124
1125 err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1126 if ((mark & EXTENT_DIRTY) &&
1127 test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1128 errors = true;
1129
1130 if ((mark & EXTENT_NEW) &&
1131 test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1132 errors = true;
1133
1134 if (errors && !err)
1135 err = -EIO;
1136 return err;
1137 }
1138
1139 /*
1140 * When btree blocks are allocated the corresponding extents are marked dirty.
1141 * This function ensures such extents are persisted on disk for transaction or
1142 * log commit.
1143 *
1144 * @trans: transaction whose dirty pages we'd like to write
1145 */
btrfs_write_and_wait_transaction(struct btrfs_trans_handle * trans)1146 static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1147 {
1148 int ret;
1149 int ret2;
1150 struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
1151 struct btrfs_fs_info *fs_info = trans->fs_info;
1152 struct blk_plug plug;
1153
1154 blk_start_plug(&plug);
1155 ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
1156 blk_finish_plug(&plug);
1157 ret2 = btrfs_wait_extents(fs_info, dirty_pages);
1158
1159 extent_io_tree_release(&trans->transaction->dirty_pages);
1160
1161 if (ret)
1162 return ret;
1163 else if (ret2)
1164 return ret2;
1165 else
1166 return 0;
1167 }
1168
1169 /*
1170 * this is used to update the root pointer in the tree of tree roots.
1171 *
1172 * But, in the case of the extent allocation tree, updating the root
1173 * pointer may allocate blocks which may change the root of the extent
1174 * allocation tree.
1175 *
1176 * So, this loops and repeats and makes sure the cowonly root didn't
1177 * change while the root pointer was being updated in the metadata.
1178 */
update_cowonly_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)1179 static int update_cowonly_root(struct btrfs_trans_handle *trans,
1180 struct btrfs_root *root)
1181 {
1182 int ret;
1183 u64 old_root_bytenr;
1184 u64 old_root_used;
1185 struct btrfs_fs_info *fs_info = root->fs_info;
1186 struct btrfs_root *tree_root = fs_info->tree_root;
1187
1188 old_root_used = btrfs_root_used(&root->root_item);
1189
1190 while (1) {
1191 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
1192 if (old_root_bytenr == root->node->start &&
1193 old_root_used == btrfs_root_used(&root->root_item))
1194 break;
1195
1196 btrfs_set_root_node(&root->root_item, root->node);
1197 ret = btrfs_update_root(trans, tree_root,
1198 &root->root_key,
1199 &root->root_item);
1200 if (ret)
1201 return ret;
1202
1203 old_root_used = btrfs_root_used(&root->root_item);
1204 }
1205
1206 return 0;
1207 }
1208
1209 /*
1210 * update all the cowonly tree roots on disk
1211 *
1212 * The error handling in this function may not be obvious. Any of the
1213 * failures will cause the file system to go offline. We still need
1214 * to clean up the delayed refs.
1215 */
commit_cowonly_roots(struct btrfs_trans_handle * trans)1216 static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
1217 {
1218 struct btrfs_fs_info *fs_info = trans->fs_info;
1219 struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1220 struct list_head *io_bgs = &trans->transaction->io_bgs;
1221 struct list_head *next;
1222 struct extent_buffer *eb;
1223 int ret;
1224
1225 eb = btrfs_lock_root_node(fs_info->tree_root);
1226 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
1227 0, &eb, BTRFS_NESTING_COW);
1228 btrfs_tree_unlock(eb);
1229 free_extent_buffer(eb);
1230
1231 if (ret)
1232 return ret;
1233
1234 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1235 if (ret)
1236 return ret;
1237
1238 ret = btrfs_run_dev_stats(trans);
1239 if (ret)
1240 return ret;
1241 ret = btrfs_run_dev_replace(trans);
1242 if (ret)
1243 return ret;
1244 ret = btrfs_run_qgroups(trans);
1245 if (ret)
1246 return ret;
1247
1248 ret = btrfs_setup_space_cache(trans);
1249 if (ret)
1250 return ret;
1251
1252 /* run_qgroups might have added some more refs */
1253 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1254 if (ret)
1255 return ret;
1256 again:
1257 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1258 struct btrfs_root *root;
1259 next = fs_info->dirty_cowonly_roots.next;
1260 list_del_init(next);
1261 root = list_entry(next, struct btrfs_root, dirty_list);
1262 clear_bit(BTRFS_ROOT_DIRTY, &root->state);
1263
1264 if (root != fs_info->extent_root)
1265 list_add_tail(&root->dirty_list,
1266 &trans->transaction->switch_commits);
1267 ret = update_cowonly_root(trans, root);
1268 if (ret)
1269 return ret;
1270 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1271 if (ret)
1272 return ret;
1273 }
1274
1275 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1276 ret = btrfs_write_dirty_block_groups(trans);
1277 if (ret)
1278 return ret;
1279 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1280 if (ret)
1281 return ret;
1282 }
1283
1284 if (!list_empty(&fs_info->dirty_cowonly_roots))
1285 goto again;
1286
1287 list_add_tail(&fs_info->extent_root->dirty_list,
1288 &trans->transaction->switch_commits);
1289
1290 /* Update dev-replace pointer once everything is committed */
1291 fs_info->dev_replace.committed_cursor_left =
1292 fs_info->dev_replace.cursor_left_last_write_of_item;
1293
1294 return 0;
1295 }
1296
1297 /*
1298 * dead roots are old snapshots that need to be deleted. This allocates
1299 * a dirty root struct and adds it into the list of dead roots that need to
1300 * be deleted
1301 */
btrfs_add_dead_root(struct btrfs_root * root)1302 void btrfs_add_dead_root(struct btrfs_root *root)
1303 {
1304 struct btrfs_fs_info *fs_info = root->fs_info;
1305
1306 spin_lock(&fs_info->trans_lock);
1307 if (list_empty(&root->root_list)) {
1308 btrfs_grab_root(root);
1309 list_add_tail(&root->root_list, &fs_info->dead_roots);
1310 }
1311 spin_unlock(&fs_info->trans_lock);
1312 }
1313
1314 /*
1315 * update all the cowonly tree roots on disk
1316 */
commit_fs_roots(struct btrfs_trans_handle * trans)1317 static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
1318 {
1319 struct btrfs_fs_info *fs_info = trans->fs_info;
1320 struct btrfs_root *gang[8];
1321 int i;
1322 int ret;
1323
1324 spin_lock(&fs_info->fs_roots_radix_lock);
1325 while (1) {
1326 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1327 (void **)gang, 0,
1328 ARRAY_SIZE(gang),
1329 BTRFS_ROOT_TRANS_TAG);
1330 if (ret == 0)
1331 break;
1332 for (i = 0; i < ret; i++) {
1333 struct btrfs_root *root = gang[i];
1334 int ret2;
1335
1336 radix_tree_tag_clear(&fs_info->fs_roots_radix,
1337 (unsigned long)root->root_key.objectid,
1338 BTRFS_ROOT_TRANS_TAG);
1339 spin_unlock(&fs_info->fs_roots_radix_lock);
1340
1341 btrfs_free_log(trans, root);
1342 btrfs_update_reloc_root(trans, root);
1343
1344 btrfs_save_ino_cache(root, trans);
1345
1346 /* see comments in should_cow_block() */
1347 clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1348 smp_mb__after_atomic();
1349
1350 if (root->commit_root != root->node) {
1351 list_add_tail(&root->dirty_list,
1352 &trans->transaction->switch_commits);
1353 btrfs_set_root_node(&root->root_item,
1354 root->node);
1355 }
1356
1357 ret2 = btrfs_update_root(trans, fs_info->tree_root,
1358 &root->root_key,
1359 &root->root_item);
1360 if (ret2)
1361 return ret2;
1362 spin_lock(&fs_info->fs_roots_radix_lock);
1363 btrfs_qgroup_free_meta_all_pertrans(root);
1364 }
1365 }
1366 spin_unlock(&fs_info->fs_roots_radix_lock);
1367 return 0;
1368 }
1369
1370 /*
1371 * defrag a given btree.
1372 * Every leaf in the btree is read and defragged.
1373 */
btrfs_defrag_root(struct btrfs_root * root)1374 int btrfs_defrag_root(struct btrfs_root *root)
1375 {
1376 struct btrfs_fs_info *info = root->fs_info;
1377 struct btrfs_trans_handle *trans;
1378 int ret;
1379
1380 if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1381 return 0;
1382
1383 while (1) {
1384 trans = btrfs_start_transaction(root, 0);
1385 if (IS_ERR(trans)) {
1386 ret = PTR_ERR(trans);
1387 break;
1388 }
1389
1390 ret = btrfs_defrag_leaves(trans, root);
1391
1392 btrfs_end_transaction(trans);
1393 btrfs_btree_balance_dirty(info);
1394 cond_resched();
1395
1396 if (btrfs_fs_closing(info) || ret != -EAGAIN)
1397 break;
1398
1399 if (btrfs_defrag_cancelled(info)) {
1400 btrfs_debug(info, "defrag_root cancelled");
1401 ret = -EAGAIN;
1402 break;
1403 }
1404 }
1405 clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
1406 return ret;
1407 }
1408
1409 /*
1410 * Do all special snapshot related qgroup dirty hack.
1411 *
1412 * Will do all needed qgroup inherit and dirty hack like switch commit
1413 * roots inside one transaction and write all btree into disk, to make
1414 * qgroup works.
1415 */
qgroup_account_snapshot(struct btrfs_trans_handle * trans,struct btrfs_root * src,struct btrfs_root * parent,struct btrfs_qgroup_inherit * inherit,u64 dst_objectid)1416 static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *src,
1418 struct btrfs_root *parent,
1419 struct btrfs_qgroup_inherit *inherit,
1420 u64 dst_objectid)
1421 {
1422 struct btrfs_fs_info *fs_info = src->fs_info;
1423 int ret;
1424
1425 /*
1426 * Save some performance in the case that qgroups are not
1427 * enabled. If this check races with the ioctl, rescan will
1428 * kick in anyway.
1429 */
1430 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1431 return 0;
1432
1433 /*
1434 * Ensure dirty @src will be committed. Or, after coming
1435 * commit_fs_roots() and switch_commit_roots(), any dirty but not
1436 * recorded root will never be updated again, causing an outdated root
1437 * item.
1438 */
1439 record_root_in_trans(trans, src, 1);
1440
1441 /*
1442 * We are going to commit transaction, see btrfs_commit_transaction()
1443 * comment for reason locking tree_log_mutex
1444 */
1445 mutex_lock(&fs_info->tree_log_mutex);
1446
1447 ret = commit_fs_roots(trans);
1448 if (ret)
1449 goto out;
1450 ret = btrfs_qgroup_account_extents(trans);
1451 if (ret < 0)
1452 goto out;
1453
1454 /* Now qgroup are all updated, we can inherit it to new qgroups */
1455 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1456 inherit);
1457 if (ret < 0)
1458 goto out;
1459
1460 /*
1461 * Now we do a simplified commit transaction, which will:
1462 * 1) commit all subvolume and extent tree
1463 * To ensure all subvolume and extent tree have a valid
1464 * commit_root to accounting later insert_dir_item()
1465 * 2) write all btree blocks onto disk
1466 * This is to make sure later btree modification will be cowed
1467 * Or commit_root can be populated and cause wrong qgroup numbers
1468 * In this simplified commit, we don't really care about other trees
1469 * like chunk and root tree, as they won't affect qgroup.
1470 * And we don't write super to avoid half committed status.
1471 */
1472 ret = commit_cowonly_roots(trans);
1473 if (ret)
1474 goto out;
1475 switch_commit_roots(trans);
1476 ret = btrfs_write_and_wait_transaction(trans);
1477 if (ret)
1478 btrfs_handle_fs_error(fs_info, ret,
1479 "Error while writing out transaction for qgroup");
1480
1481 out:
1482 mutex_unlock(&fs_info->tree_log_mutex);
1483
1484 /*
1485 * Force parent root to be updated, as we recorded it before so its
1486 * last_trans == cur_transid.
1487 * Or it won't be committed again onto disk after later
1488 * insert_dir_item()
1489 */
1490 if (!ret)
1491 record_root_in_trans(trans, parent, 1);
1492 return ret;
1493 }
1494
1495 /*
1496 * new snapshots need to be created at a very specific time in the
1497 * transaction commit. This does the actual creation.
1498 *
1499 * Note:
1500 * If the error which may affect the commitment of the current transaction
1501 * happens, we should return the error number. If the error which just affect
1502 * the creation of the pending snapshots, just return 0.
1503 */
create_pending_snapshot(struct btrfs_trans_handle * trans,struct btrfs_pending_snapshot * pending)1504 static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1505 struct btrfs_pending_snapshot *pending)
1506 {
1507
1508 struct btrfs_fs_info *fs_info = trans->fs_info;
1509 struct btrfs_key key;
1510 struct btrfs_root_item *new_root_item;
1511 struct btrfs_root *tree_root = fs_info->tree_root;
1512 struct btrfs_root *root = pending->root;
1513 struct btrfs_root *parent_root;
1514 struct btrfs_block_rsv *rsv;
1515 struct inode *parent_inode;
1516 struct btrfs_path *path;
1517 struct btrfs_dir_item *dir_item;
1518 struct dentry *dentry;
1519 struct extent_buffer *tmp;
1520 struct extent_buffer *old;
1521 struct timespec64 cur_time;
1522 int ret = 0;
1523 u64 to_reserve = 0;
1524 u64 index = 0;
1525 u64 objectid;
1526 u64 root_flags;
1527
1528 ASSERT(pending->path);
1529 path = pending->path;
1530
1531 ASSERT(pending->root_item);
1532 new_root_item = pending->root_item;
1533
1534 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1535 if (pending->error)
1536 goto no_free_objectid;
1537
1538 /*
1539 * Make qgroup to skip current new snapshot's qgroupid, as it is
1540 * accounted by later btrfs_qgroup_inherit().
1541 */
1542 btrfs_set_skip_qgroup(trans, objectid);
1543
1544 btrfs_reloc_pre_snapshot(pending, &to_reserve);
1545
1546 if (to_reserve > 0) {
1547 pending->error = btrfs_block_rsv_add(root,
1548 &pending->block_rsv,
1549 to_reserve,
1550 BTRFS_RESERVE_NO_FLUSH);
1551 if (pending->error)
1552 goto clear_skip_qgroup;
1553 }
1554
1555 key.objectid = objectid;
1556 key.offset = (u64)-1;
1557 key.type = BTRFS_ROOT_ITEM_KEY;
1558
1559 rsv = trans->block_rsv;
1560 trans->block_rsv = &pending->block_rsv;
1561 trans->bytes_reserved = trans->block_rsv->reserved;
1562 trace_btrfs_space_reservation(fs_info, "transaction",
1563 trans->transid,
1564 trans->bytes_reserved, 1);
1565 dentry = pending->dentry;
1566 parent_inode = pending->dir;
1567 parent_root = BTRFS_I(parent_inode)->root;
1568 record_root_in_trans(trans, parent_root, 0);
1569
1570 cur_time = current_time(parent_inode);
1571
1572 /*
1573 * insert the directory item
1574 */
1575 ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
1576 BUG_ON(ret); /* -ENOMEM */
1577
1578 /* check if there is a file/dir which has the same name. */
1579 dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
1580 btrfs_ino(BTRFS_I(parent_inode)),
1581 dentry->d_name.name,
1582 dentry->d_name.len, 0);
1583 if (dir_item != NULL && !IS_ERR(dir_item)) {
1584 pending->error = -EEXIST;
1585 goto dir_item_existed;
1586 } else if (IS_ERR(dir_item)) {
1587 ret = PTR_ERR(dir_item);
1588 btrfs_abort_transaction(trans, ret);
1589 goto fail;
1590 }
1591 btrfs_release_path(path);
1592
1593 /*
1594 * pull in the delayed directory update
1595 * and the delayed inode item
1596 * otherwise we corrupt the FS during
1597 * snapshot
1598 */
1599 ret = btrfs_run_delayed_items(trans);
1600 if (ret) { /* Transaction aborted */
1601 btrfs_abort_transaction(trans, ret);
1602 goto fail;
1603 }
1604
1605 record_root_in_trans(trans, root, 0);
1606 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
1607 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
1608 btrfs_check_and_init_root_item(new_root_item);
1609
1610 root_flags = btrfs_root_flags(new_root_item);
1611 if (pending->readonly)
1612 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1613 else
1614 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1615 btrfs_set_root_flags(new_root_item, root_flags);
1616
1617 btrfs_set_root_generation_v2(new_root_item,
1618 trans->transid);
1619 generate_random_guid(new_root_item->uuid);
1620 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1621 BTRFS_UUID_SIZE);
1622 if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
1623 memset(new_root_item->received_uuid, 0,
1624 sizeof(new_root_item->received_uuid));
1625 memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
1626 memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
1627 btrfs_set_root_stransid(new_root_item, 0);
1628 btrfs_set_root_rtransid(new_root_item, 0);
1629 }
1630 btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
1631 btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
1632 btrfs_set_root_otransid(new_root_item, trans->transid);
1633
1634 old = btrfs_lock_root_node(root);
1635 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1636 BTRFS_NESTING_COW);
1637 if (ret) {
1638 btrfs_tree_unlock(old);
1639 free_extent_buffer(old);
1640 btrfs_abort_transaction(trans, ret);
1641 goto fail;
1642 }
1643
1644 btrfs_set_lock_blocking_write(old);
1645
1646 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1647 /* clean up in any case */
1648 btrfs_tree_unlock(old);
1649 free_extent_buffer(old);
1650 if (ret) {
1651 btrfs_abort_transaction(trans, ret);
1652 goto fail;
1653 }
1654 /* see comments in should_cow_block() */
1655 set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1656 smp_wmb();
1657
1658 btrfs_set_root_node(new_root_item, tmp);
1659 /* record when the snapshot was created in key.offset */
1660 key.offset = trans->transid;
1661 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
1662 btrfs_tree_unlock(tmp);
1663 free_extent_buffer(tmp);
1664 if (ret) {
1665 btrfs_abort_transaction(trans, ret);
1666 goto fail;
1667 }
1668
1669 /*
1670 * insert root back/forward references
1671 */
1672 ret = btrfs_add_root_ref(trans, objectid,
1673 parent_root->root_key.objectid,
1674 btrfs_ino(BTRFS_I(parent_inode)), index,
1675 dentry->d_name.name, dentry->d_name.len);
1676 if (ret) {
1677 btrfs_abort_transaction(trans, ret);
1678 goto fail;
1679 }
1680
1681 key.offset = (u64)-1;
1682 pending->snap = btrfs_get_new_fs_root(fs_info, objectid, pending->anon_dev);
1683 if (IS_ERR(pending->snap)) {
1684 ret = PTR_ERR(pending->snap);
1685 pending->snap = NULL;
1686 btrfs_abort_transaction(trans, ret);
1687 goto fail;
1688 }
1689
1690 ret = btrfs_reloc_post_snapshot(trans, pending);
1691 if (ret) {
1692 btrfs_abort_transaction(trans, ret);
1693 goto fail;
1694 }
1695
1696 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1697 if (ret) {
1698 btrfs_abort_transaction(trans, ret);
1699 goto fail;
1700 }
1701
1702 /*
1703 * Do special qgroup accounting for snapshot, as we do some qgroup
1704 * snapshot hack to do fast snapshot.
1705 * To co-operate with that hack, we do hack again.
1706 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1707 */
1708 ret = qgroup_account_snapshot(trans, root, parent_root,
1709 pending->inherit, objectid);
1710 if (ret < 0)
1711 goto fail;
1712
1713 ret = btrfs_insert_dir_item(trans, dentry->d_name.name,
1714 dentry->d_name.len, BTRFS_I(parent_inode),
1715 &key, BTRFS_FT_DIR, index);
1716 /* We have check then name at the beginning, so it is impossible. */
1717 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
1718 if (ret) {
1719 btrfs_abort_transaction(trans, ret);
1720 goto fail;
1721 }
1722
1723 btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
1724 dentry->d_name.len * 2);
1725 parent_inode->i_mtime = parent_inode->i_ctime =
1726 current_time(parent_inode);
1727 ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
1728 if (ret) {
1729 btrfs_abort_transaction(trans, ret);
1730 goto fail;
1731 }
1732 ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1733 BTRFS_UUID_KEY_SUBVOL,
1734 objectid);
1735 if (ret) {
1736 btrfs_abort_transaction(trans, ret);
1737 goto fail;
1738 }
1739 if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
1740 ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
1741 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
1742 objectid);
1743 if (ret && ret != -EEXIST) {
1744 btrfs_abort_transaction(trans, ret);
1745 goto fail;
1746 }
1747 }
1748
1749 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1750 if (ret) {
1751 btrfs_abort_transaction(trans, ret);
1752 goto fail;
1753 }
1754
1755 fail:
1756 pending->error = ret;
1757 dir_item_existed:
1758 trans->block_rsv = rsv;
1759 trans->bytes_reserved = 0;
1760 clear_skip_qgroup:
1761 btrfs_clear_skip_qgroup(trans);
1762 no_free_objectid:
1763 kfree(new_root_item);
1764 pending->root_item = NULL;
1765 btrfs_free_path(path);
1766 pending->path = NULL;
1767
1768 return ret;
1769 }
1770
1771 /*
1772 * create all the snapshots we've scheduled for creation
1773 */
create_pending_snapshots(struct btrfs_trans_handle * trans)1774 static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
1775 {
1776 struct btrfs_pending_snapshot *pending, *next;
1777 struct list_head *head = &trans->transaction->pending_snapshots;
1778 int ret = 0;
1779
1780 list_for_each_entry_safe(pending, next, head, list) {
1781 list_del(&pending->list);
1782 ret = create_pending_snapshot(trans, pending);
1783 if (ret)
1784 break;
1785 }
1786 return ret;
1787 }
1788
update_super_roots(struct btrfs_fs_info * fs_info)1789 static void update_super_roots(struct btrfs_fs_info *fs_info)
1790 {
1791 struct btrfs_root_item *root_item;
1792 struct btrfs_super_block *super;
1793
1794 super = fs_info->super_copy;
1795
1796 root_item = &fs_info->chunk_root->root_item;
1797 super->chunk_root = root_item->bytenr;
1798 super->chunk_root_generation = root_item->generation;
1799 super->chunk_root_level = root_item->level;
1800
1801 root_item = &fs_info->tree_root->root_item;
1802 super->root = root_item->bytenr;
1803 super->generation = root_item->generation;
1804 super->root_level = root_item->level;
1805 if (btrfs_test_opt(fs_info, SPACE_CACHE))
1806 super->cache_generation = root_item->generation;
1807 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1808 super->uuid_tree_generation = root_item->generation;
1809 }
1810
btrfs_transaction_in_commit(struct btrfs_fs_info * info)1811 int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1812 {
1813 struct btrfs_transaction *trans;
1814 int ret = 0;
1815
1816 spin_lock(&info->trans_lock);
1817 trans = info->running_transaction;
1818 if (trans)
1819 ret = (trans->state >= TRANS_STATE_COMMIT_START);
1820 spin_unlock(&info->trans_lock);
1821 return ret;
1822 }
1823
btrfs_transaction_blocked(struct btrfs_fs_info * info)1824 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1825 {
1826 struct btrfs_transaction *trans;
1827 int ret = 0;
1828
1829 spin_lock(&info->trans_lock);
1830 trans = info->running_transaction;
1831 if (trans)
1832 ret = is_transaction_blocked(trans);
1833 spin_unlock(&info->trans_lock);
1834 return ret;
1835 }
1836
1837 /*
1838 * wait for the current transaction commit to start and block subsequent
1839 * transaction joins
1840 */
wait_current_trans_commit_start(struct btrfs_fs_info * fs_info,struct btrfs_transaction * trans)1841 static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1842 struct btrfs_transaction *trans)
1843 {
1844 wait_event(fs_info->transaction_blocked_wait,
1845 trans->state >= TRANS_STATE_COMMIT_START ||
1846 TRANS_ABORTED(trans));
1847 }
1848
1849 /*
1850 * wait for the current transaction to start and then become unblocked.
1851 * caller holds ref.
1852 */
wait_current_trans_commit_start_and_unblock(struct btrfs_fs_info * fs_info,struct btrfs_transaction * trans)1853 static void wait_current_trans_commit_start_and_unblock(
1854 struct btrfs_fs_info *fs_info,
1855 struct btrfs_transaction *trans)
1856 {
1857 wait_event(fs_info->transaction_wait,
1858 trans->state >= TRANS_STATE_UNBLOCKED ||
1859 TRANS_ABORTED(trans));
1860 }
1861
1862 /*
1863 * commit transactions asynchronously. once btrfs_commit_transaction_async
1864 * returns, any subsequent transaction will not be allowed to join.
1865 */
1866 struct btrfs_async_commit {
1867 struct btrfs_trans_handle *newtrans;
1868 struct work_struct work;
1869 };
1870
do_async_commit(struct work_struct * work)1871 static void do_async_commit(struct work_struct *work)
1872 {
1873 struct btrfs_async_commit *ac =
1874 container_of(work, struct btrfs_async_commit, work);
1875
1876 /*
1877 * We've got freeze protection passed with the transaction.
1878 * Tell lockdep about it.
1879 */
1880 if (ac->newtrans->type & __TRANS_FREEZABLE)
1881 __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
1882
1883 current->journal_info = ac->newtrans;
1884
1885 btrfs_commit_transaction(ac->newtrans);
1886 kfree(ac);
1887 }
1888
btrfs_commit_transaction_async(struct btrfs_trans_handle * trans,int wait_for_unblock)1889 int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1890 int wait_for_unblock)
1891 {
1892 struct btrfs_fs_info *fs_info = trans->fs_info;
1893 struct btrfs_async_commit *ac;
1894 struct btrfs_transaction *cur_trans;
1895
1896 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1897 if (!ac)
1898 return -ENOMEM;
1899
1900 INIT_WORK(&ac->work, do_async_commit);
1901 ac->newtrans = btrfs_join_transaction(trans->root);
1902 if (IS_ERR(ac->newtrans)) {
1903 int err = PTR_ERR(ac->newtrans);
1904 kfree(ac);
1905 return err;
1906 }
1907
1908 /* take transaction reference */
1909 cur_trans = trans->transaction;
1910 refcount_inc(&cur_trans->use_count);
1911
1912 btrfs_end_transaction(trans);
1913
1914 /*
1915 * Tell lockdep we've released the freeze rwsem, since the
1916 * async commit thread will be the one to unlock it.
1917 */
1918 if (ac->newtrans->type & __TRANS_FREEZABLE)
1919 __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
1920
1921 schedule_work(&ac->work);
1922
1923 /* wait for transaction to start and unblock */
1924 if (wait_for_unblock)
1925 wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1926 else
1927 wait_current_trans_commit_start(fs_info, cur_trans);
1928
1929 if (current->journal_info == trans)
1930 current->journal_info = NULL;
1931
1932 btrfs_put_transaction(cur_trans);
1933 return 0;
1934 }
1935
1936
cleanup_transaction(struct btrfs_trans_handle * trans,int err)1937 static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
1938 {
1939 struct btrfs_fs_info *fs_info = trans->fs_info;
1940 struct btrfs_transaction *cur_trans = trans->transaction;
1941
1942 WARN_ON(refcount_read(&trans->use_count) > 1);
1943
1944 btrfs_abort_transaction(trans, err);
1945
1946 spin_lock(&fs_info->trans_lock);
1947
1948 /*
1949 * If the transaction is removed from the list, it means this
1950 * transaction has been committed successfully, so it is impossible
1951 * to call the cleanup function.
1952 */
1953 BUG_ON(list_empty(&cur_trans->list));
1954
1955 if (cur_trans == fs_info->running_transaction) {
1956 cur_trans->state = TRANS_STATE_COMMIT_DOING;
1957 spin_unlock(&fs_info->trans_lock);
1958 wait_event(cur_trans->writer_wait,
1959 atomic_read(&cur_trans->num_writers) == 1);
1960
1961 spin_lock(&fs_info->trans_lock);
1962 }
1963
1964 /*
1965 * Now that we know no one else is still using the transaction we can
1966 * remove the transaction from the list of transactions. This avoids
1967 * the transaction kthread from cleaning up the transaction while some
1968 * other task is still using it, which could result in a use-after-free
1969 * on things like log trees, as it forces the transaction kthread to
1970 * wait for this transaction to be cleaned up by us.
1971 */
1972 list_del_init(&cur_trans->list);
1973
1974 spin_unlock(&fs_info->trans_lock);
1975
1976 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
1977
1978 spin_lock(&fs_info->trans_lock);
1979 if (cur_trans == fs_info->running_transaction)
1980 fs_info->running_transaction = NULL;
1981 spin_unlock(&fs_info->trans_lock);
1982
1983 if (trans->type & __TRANS_FREEZABLE)
1984 sb_end_intwrite(fs_info->sb);
1985 btrfs_put_transaction(cur_trans);
1986 btrfs_put_transaction(cur_trans);
1987
1988 trace_btrfs_transaction_commit(trans->root);
1989
1990 if (current->journal_info == trans)
1991 current->journal_info = NULL;
1992 btrfs_scrub_cancel(fs_info);
1993
1994 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1995 }
1996
1997 /*
1998 * Release reserved delayed ref space of all pending block groups of the
1999 * transaction and remove them from the list
2000 */
btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle * trans)2001 static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
2002 {
2003 struct btrfs_fs_info *fs_info = trans->fs_info;
2004 struct btrfs_block_group *block_group, *tmp;
2005
2006 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
2007 btrfs_delayed_refs_rsv_release(fs_info, 1);
2008 list_del_init(&block_group->bg_list);
2009 }
2010 }
2011
btrfs_start_delalloc_flush(struct btrfs_trans_handle * trans)2012 static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
2013 {
2014 struct btrfs_fs_info *fs_info = trans->fs_info;
2015
2016 /*
2017 * We use writeback_inodes_sb here because if we used
2018 * btrfs_start_delalloc_roots we would deadlock with fs freeze.
2019 * Currently are holding the fs freeze lock, if we do an async flush
2020 * we'll do btrfs_join_transaction() and deadlock because we need to
2021 * wait for the fs freeze lock. Using the direct flushing we benefit
2022 * from already being in a transaction and our join_transaction doesn't
2023 * have to re-take the fs freeze lock.
2024 */
2025 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2026 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
2027 } else {
2028 struct btrfs_pending_snapshot *pending;
2029 struct list_head *head = &trans->transaction->pending_snapshots;
2030
2031 /*
2032 * Flush dellaloc for any root that is going to be snapshotted.
2033 * This is done to avoid a corrupted version of files, in the
2034 * snapshots, that had both buffered and direct IO writes (even
2035 * if they were done sequentially) due to an unordered update of
2036 * the inode's size on disk.
2037 */
2038 list_for_each_entry(pending, head, list) {
2039 int ret;
2040
2041 ret = btrfs_start_delalloc_snapshot(pending->root);
2042 if (ret)
2043 return ret;
2044 }
2045 }
2046 return 0;
2047 }
2048
btrfs_wait_delalloc_flush(struct btrfs_trans_handle * trans)2049 static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
2050 {
2051 struct btrfs_fs_info *fs_info = trans->fs_info;
2052
2053 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2054 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
2055 } else {
2056 struct btrfs_pending_snapshot *pending;
2057 struct list_head *head = &trans->transaction->pending_snapshots;
2058
2059 /*
2060 * Wait for any dellaloc that we started previously for the roots
2061 * that are going to be snapshotted. This is to avoid a corrupted
2062 * version of files in the snapshots that had both buffered and
2063 * direct IO writes (even if they were done sequentially).
2064 */
2065 list_for_each_entry(pending, head, list)
2066 btrfs_wait_ordered_extents(pending->root,
2067 U64_MAX, 0, U64_MAX);
2068 }
2069 }
2070
btrfs_commit_transaction(struct btrfs_trans_handle * trans)2071 int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
2072 {
2073 struct btrfs_fs_info *fs_info = trans->fs_info;
2074 struct btrfs_transaction *cur_trans = trans->transaction;
2075 struct btrfs_transaction *prev_trans = NULL;
2076 int ret;
2077
2078 ASSERT(refcount_read(&trans->use_count) == 1);
2079
2080 /*
2081 * Some places just start a transaction to commit it. We need to make
2082 * sure that if this commit fails that the abort code actually marks the
2083 * transaction as failed, so set trans->dirty to make the abort code do
2084 * the right thing.
2085 */
2086 trans->dirty = true;
2087
2088 /* Stop the commit early if ->aborted is set */
2089 if (TRANS_ABORTED(cur_trans)) {
2090 ret = cur_trans->aborted;
2091 btrfs_end_transaction(trans);
2092 return ret;
2093 }
2094
2095 btrfs_trans_release_metadata(trans);
2096 trans->block_rsv = NULL;
2097
2098 /* make a pass through all the delayed refs we have so far
2099 * any runnings procs may add more while we are here
2100 */
2101 ret = btrfs_run_delayed_refs(trans, 0);
2102 if (ret) {
2103 btrfs_end_transaction(trans);
2104 return ret;
2105 }
2106
2107 cur_trans = trans->transaction;
2108
2109 /*
2110 * set the flushing flag so procs in this transaction have to
2111 * start sending their work down.
2112 */
2113 cur_trans->delayed_refs.flushing = 1;
2114 smp_wmb();
2115
2116 btrfs_create_pending_block_groups(trans);
2117
2118 ret = btrfs_run_delayed_refs(trans, 0);
2119 if (ret) {
2120 btrfs_end_transaction(trans);
2121 return ret;
2122 }
2123
2124 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
2125 int run_it = 0;
2126
2127 /* this mutex is also taken before trying to set
2128 * block groups readonly. We need to make sure
2129 * that nobody has set a block group readonly
2130 * after a extents from that block group have been
2131 * allocated for cache files. btrfs_set_block_group_ro
2132 * will wait for the transaction to commit if it
2133 * finds BTRFS_TRANS_DIRTY_BG_RUN set.
2134 *
2135 * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
2136 * only one process starts all the block group IO. It wouldn't
2137 * hurt to have more than one go through, but there's no
2138 * real advantage to it either.
2139 */
2140 mutex_lock(&fs_info->ro_block_group_mutex);
2141 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2142 &cur_trans->flags))
2143 run_it = 1;
2144 mutex_unlock(&fs_info->ro_block_group_mutex);
2145
2146 if (run_it) {
2147 ret = btrfs_start_dirty_block_groups(trans);
2148 if (ret) {
2149 btrfs_end_transaction(trans);
2150 return ret;
2151 }
2152 }
2153 }
2154
2155 spin_lock(&fs_info->trans_lock);
2156 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
2157 spin_unlock(&fs_info->trans_lock);
2158 refcount_inc(&cur_trans->use_count);
2159 ret = btrfs_end_transaction(trans);
2160
2161 wait_for_commit(cur_trans);
2162
2163 if (TRANS_ABORTED(cur_trans))
2164 ret = cur_trans->aborted;
2165
2166 btrfs_put_transaction(cur_trans);
2167
2168 return ret;
2169 }
2170
2171 cur_trans->state = TRANS_STATE_COMMIT_START;
2172 wake_up(&fs_info->transaction_blocked_wait);
2173
2174 if (cur_trans->list.prev != &fs_info->trans_list) {
2175 prev_trans = list_entry(cur_trans->list.prev,
2176 struct btrfs_transaction, list);
2177 if (prev_trans->state != TRANS_STATE_COMPLETED) {
2178 refcount_inc(&prev_trans->use_count);
2179 spin_unlock(&fs_info->trans_lock);
2180
2181 wait_for_commit(prev_trans);
2182 ret = READ_ONCE(prev_trans->aborted);
2183
2184 btrfs_put_transaction(prev_trans);
2185 if (ret)
2186 goto cleanup_transaction;
2187 } else {
2188 spin_unlock(&fs_info->trans_lock);
2189 }
2190 } else {
2191 spin_unlock(&fs_info->trans_lock);
2192 /*
2193 * The previous transaction was aborted and was already removed
2194 * from the list of transactions at fs_info->trans_list. So we
2195 * abort to prevent writing a new superblock that reflects a
2196 * corrupt state (pointing to trees with unwritten nodes/leafs).
2197 */
2198 if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
2199 ret = -EROFS;
2200 goto cleanup_transaction;
2201 }
2202 }
2203
2204 extwriter_counter_dec(cur_trans, trans->type);
2205
2206 ret = btrfs_start_delalloc_flush(trans);
2207 if (ret)
2208 goto cleanup_transaction;
2209
2210 ret = btrfs_run_delayed_items(trans);
2211 if (ret)
2212 goto cleanup_transaction;
2213
2214 wait_event(cur_trans->writer_wait,
2215 extwriter_counter_read(cur_trans) == 0);
2216
2217 /* some pending stuffs might be added after the previous flush. */
2218 ret = btrfs_run_delayed_items(trans);
2219 if (ret)
2220 goto cleanup_transaction;
2221
2222 btrfs_wait_delalloc_flush(trans);
2223
2224 /*
2225 * Wait for all ordered extents started by a fast fsync that joined this
2226 * transaction. Otherwise if this transaction commits before the ordered
2227 * extents complete we lose logged data after a power failure.
2228 */
2229 wait_event(cur_trans->pending_wait,
2230 atomic_read(&cur_trans->pending_ordered) == 0);
2231
2232 btrfs_scrub_pause(fs_info);
2233 /*
2234 * Ok now we need to make sure to block out any other joins while we
2235 * commit the transaction. We could have started a join before setting
2236 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2237 */
2238 spin_lock(&fs_info->trans_lock);
2239 cur_trans->state = TRANS_STATE_COMMIT_DOING;
2240 spin_unlock(&fs_info->trans_lock);
2241 wait_event(cur_trans->writer_wait,
2242 atomic_read(&cur_trans->num_writers) == 1);
2243
2244 if (TRANS_ABORTED(cur_trans)) {
2245 ret = cur_trans->aborted;
2246 goto scrub_continue;
2247 }
2248 /*
2249 * the reloc mutex makes sure that we stop
2250 * the balancing code from coming in and moving
2251 * extents around in the middle of the commit
2252 */
2253 mutex_lock(&fs_info->reloc_mutex);
2254
2255 /*
2256 * We needn't worry about the delayed items because we will
2257 * deal with them in create_pending_snapshot(), which is the
2258 * core function of the snapshot creation.
2259 */
2260 ret = create_pending_snapshots(trans);
2261 if (ret)
2262 goto unlock_reloc;
2263
2264 /*
2265 * We insert the dir indexes of the snapshots and update the inode
2266 * of the snapshots' parents after the snapshot creation, so there
2267 * are some delayed items which are not dealt with. Now deal with
2268 * them.
2269 *
2270 * We needn't worry that this operation will corrupt the snapshots,
2271 * because all the tree which are snapshoted will be forced to COW
2272 * the nodes and leaves.
2273 */
2274 ret = btrfs_run_delayed_items(trans);
2275 if (ret)
2276 goto unlock_reloc;
2277
2278 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2279 if (ret)
2280 goto unlock_reloc;
2281
2282 /*
2283 * make sure none of the code above managed to slip in a
2284 * delayed item
2285 */
2286 btrfs_assert_delayed_root_empty(fs_info);
2287
2288 WARN_ON(cur_trans != trans->transaction);
2289
2290 /* btrfs_commit_tree_roots is responsible for getting the
2291 * various roots consistent with each other. Every pointer
2292 * in the tree of tree roots has to point to the most up to date
2293 * root for every subvolume and other tree. So, we have to keep
2294 * the tree logging code from jumping in and changing any
2295 * of the trees.
2296 *
2297 * At this point in the commit, there can't be any tree-log
2298 * writers, but a little lower down we drop the trans mutex
2299 * and let new people in. By holding the tree_log_mutex
2300 * from now until after the super is written, we avoid races
2301 * with the tree-log code.
2302 */
2303 mutex_lock(&fs_info->tree_log_mutex);
2304
2305 ret = commit_fs_roots(trans);
2306 if (ret)
2307 goto unlock_tree_log;
2308
2309 /*
2310 * Since the transaction is done, we can apply the pending changes
2311 * before the next transaction.
2312 */
2313 btrfs_apply_pending_changes(fs_info);
2314
2315 /* commit_fs_roots gets rid of all the tree log roots, it is now
2316 * safe to free the root of tree log roots
2317 */
2318 btrfs_free_log_root_tree(trans, fs_info);
2319
2320 /*
2321 * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
2322 * new delayed refs. Must handle them or qgroup can be wrong.
2323 */
2324 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2325 if (ret)
2326 goto unlock_tree_log;
2327
2328 /*
2329 * Since fs roots are all committed, we can get a quite accurate
2330 * new_roots. So let's do quota accounting.
2331 */
2332 ret = btrfs_qgroup_account_extents(trans);
2333 if (ret < 0)
2334 goto unlock_tree_log;
2335
2336 ret = commit_cowonly_roots(trans);
2337 if (ret)
2338 goto unlock_tree_log;
2339
2340 /*
2341 * The tasks which save the space cache and inode cache may also
2342 * update ->aborted, check it.
2343 */
2344 if (TRANS_ABORTED(cur_trans)) {
2345 ret = cur_trans->aborted;
2346 goto unlock_tree_log;
2347 }
2348
2349 cur_trans = fs_info->running_transaction;
2350
2351 btrfs_set_root_node(&fs_info->tree_root->root_item,
2352 fs_info->tree_root->node);
2353 list_add_tail(&fs_info->tree_root->dirty_list,
2354 &cur_trans->switch_commits);
2355
2356 btrfs_set_root_node(&fs_info->chunk_root->root_item,
2357 fs_info->chunk_root->node);
2358 list_add_tail(&fs_info->chunk_root->dirty_list,
2359 &cur_trans->switch_commits);
2360
2361 switch_commit_roots(trans);
2362
2363 ASSERT(list_empty(&cur_trans->dirty_bgs));
2364 ASSERT(list_empty(&cur_trans->io_bgs));
2365 update_super_roots(fs_info);
2366
2367 btrfs_set_super_log_root(fs_info->super_copy, 0);
2368 btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2369 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2370 sizeof(*fs_info->super_copy));
2371
2372 btrfs_commit_device_sizes(cur_trans);
2373
2374 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2375 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2376
2377 btrfs_trans_release_chunk_metadata(trans);
2378
2379 spin_lock(&fs_info->trans_lock);
2380 cur_trans->state = TRANS_STATE_UNBLOCKED;
2381 fs_info->running_transaction = NULL;
2382 spin_unlock(&fs_info->trans_lock);
2383 mutex_unlock(&fs_info->reloc_mutex);
2384
2385 wake_up(&fs_info->transaction_wait);
2386
2387 ret = btrfs_write_and_wait_transaction(trans);
2388 if (ret) {
2389 btrfs_handle_fs_error(fs_info, ret,
2390 "Error while writing out transaction");
2391 /*
2392 * reloc_mutex has been unlocked, tree_log_mutex is still held
2393 * but we can't jump to unlock_tree_log causing double unlock
2394 */
2395 mutex_unlock(&fs_info->tree_log_mutex);
2396 goto scrub_continue;
2397 }
2398
2399 ret = write_all_supers(fs_info, 0);
2400 /*
2401 * the super is written, we can safely allow the tree-loggers
2402 * to go about their business
2403 */
2404 mutex_unlock(&fs_info->tree_log_mutex);
2405 if (ret)
2406 goto scrub_continue;
2407
2408 btrfs_finish_extent_commit(trans);
2409
2410 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2411 btrfs_clear_space_info_full(fs_info);
2412
2413 fs_info->last_trans_committed = cur_trans->transid;
2414 /*
2415 * We needn't acquire the lock here because there is no other task
2416 * which can change it.
2417 */
2418 cur_trans->state = TRANS_STATE_COMPLETED;
2419 wake_up(&cur_trans->commit_wait);
2420
2421 spin_lock(&fs_info->trans_lock);
2422 list_del_init(&cur_trans->list);
2423 spin_unlock(&fs_info->trans_lock);
2424
2425 btrfs_put_transaction(cur_trans);
2426 btrfs_put_transaction(cur_trans);
2427
2428 if (trans->type & __TRANS_FREEZABLE)
2429 sb_end_intwrite(fs_info->sb);
2430
2431 trace_btrfs_transaction_commit(trans->root);
2432
2433 btrfs_scrub_continue(fs_info);
2434
2435 if (current->journal_info == trans)
2436 current->journal_info = NULL;
2437
2438 kmem_cache_free(btrfs_trans_handle_cachep, trans);
2439
2440 return ret;
2441
2442 unlock_tree_log:
2443 mutex_unlock(&fs_info->tree_log_mutex);
2444 unlock_reloc:
2445 mutex_unlock(&fs_info->reloc_mutex);
2446 scrub_continue:
2447 btrfs_scrub_continue(fs_info);
2448 cleanup_transaction:
2449 btrfs_trans_release_metadata(trans);
2450 btrfs_cleanup_pending_block_groups(trans);
2451 btrfs_trans_release_chunk_metadata(trans);
2452 trans->block_rsv = NULL;
2453 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
2454 if (current->journal_info == trans)
2455 current->journal_info = NULL;
2456 cleanup_transaction(trans, ret);
2457
2458 return ret;
2459 }
2460
2461 /*
2462 * return < 0 if error
2463 * 0 if there are no more dead_roots at the time of call
2464 * 1 there are more to be processed, call me again
2465 *
2466 * The return value indicates there are certainly more snapshots to delete, but
2467 * if there comes a new one during processing, it may return 0. We don't mind,
2468 * because btrfs_commit_super will poke cleaner thread and it will process it a
2469 * few seconds later.
2470 */
btrfs_clean_one_deleted_snapshot(struct btrfs_root * root)2471 int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2472 {
2473 int ret;
2474 struct btrfs_fs_info *fs_info = root->fs_info;
2475
2476 spin_lock(&fs_info->trans_lock);
2477 if (list_empty(&fs_info->dead_roots)) {
2478 spin_unlock(&fs_info->trans_lock);
2479 return 0;
2480 }
2481 root = list_first_entry(&fs_info->dead_roots,
2482 struct btrfs_root, root_list);
2483 list_del_init(&root->root_list);
2484 spin_unlock(&fs_info->trans_lock);
2485
2486 btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
2487
2488 btrfs_kill_all_delayed_nodes(root);
2489 if (root->ino_cache_inode) {
2490 iput(root->ino_cache_inode);
2491 root->ino_cache_inode = NULL;
2492 }
2493
2494 if (btrfs_header_backref_rev(root->node) <
2495 BTRFS_MIXED_BACKREF_REV)
2496 ret = btrfs_drop_snapshot(root, 0, 0);
2497 else
2498 ret = btrfs_drop_snapshot(root, 1, 0);
2499
2500 btrfs_put_root(root);
2501 return (ret < 0) ? 0 : 1;
2502 }
2503
btrfs_apply_pending_changes(struct btrfs_fs_info * fs_info)2504 void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2505 {
2506 unsigned long prev;
2507 unsigned long bit;
2508
2509 prev = xchg(&fs_info->pending_changes, 0);
2510 if (!prev)
2511 return;
2512
2513 bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
2514 if (prev & bit)
2515 btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2516 prev &= ~bit;
2517
2518 bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
2519 if (prev & bit)
2520 btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2521 prev &= ~bit;
2522
2523 bit = 1 << BTRFS_PENDING_COMMIT;
2524 if (prev & bit)
2525 btrfs_debug(fs_info, "pending commit done");
2526 prev &= ~bit;
2527
2528 if (prev)
2529 btrfs_warn(fs_info,
2530 "unknown pending changes left 0x%lx, ignoring", prev);
2531 }
2532