1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99static 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
118void 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
133
134
135
136
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
153static 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
159 down_write(&fs_info->commit_root_sem);
160 list_for_each_entry_safe(root, tmp, &cur_trans->switch_commits,
161 dirty_list) {
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);
169 }
170
171
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);
181 }
182 spin_unlock(&cur_trans->dropped_roots_lock);
183 up_write(&fs_info->commit_root_sem);
184}
185
186static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
187 unsigned int type)
188{
189 if (type & TRANS_EXTWRITERS)
190 atomic_inc(&trans->num_extwriters);
191}
192
193static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
194 unsigned int type)
195{
196 if (type & TRANS_EXTWRITERS)
197 atomic_dec(&trans->num_extwriters);
198}
199
200static inline void extwriter_counter_init(struct btrfs_transaction *trans,
201 unsigned int type)
202{
203 atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
204}
205
206static inline int extwriter_counter_read(struct btrfs_transaction *trans)
207{
208 return atomic_read(&trans->num_extwriters);
209}
210
211
212
213
214
215void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
216{
217 struct btrfs_fs_info *fs_info = trans->fs_info;
218
219 if (!trans->chunk_bytes_reserved)
220 return;
221
222 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
223
224 btrfs_block_rsv_release(fs_info, &fs_info->chunk_block_rsv,
225 trans->chunk_bytes_reserved, NULL);
226 trans->chunk_bytes_reserved = 0;
227}
228
229
230
231
232static noinline int join_transaction(struct btrfs_fs_info *fs_info,
233 unsigned int type)
234{
235 struct btrfs_transaction *cur_trans;
236
237 spin_lock(&fs_info->trans_lock);
238loop:
239
240 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
241 spin_unlock(&fs_info->trans_lock);
242 return -EROFS;
243 }
244
245 cur_trans = fs_info->running_transaction;
246 if (cur_trans) {
247 if (TRANS_ABORTED(cur_trans)) {
248 spin_unlock(&fs_info->trans_lock);
249 return cur_trans->aborted;
250 }
251 if (btrfs_blocked_trans_types[cur_trans->state] & type) {
252 spin_unlock(&fs_info->trans_lock);
253 return -EBUSY;
254 }
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);
259 return 0;
260 }
261 spin_unlock(&fs_info->trans_lock);
262
263
264
265
266
267 if (type == TRANS_ATTACH)
268 return -ENOENT;
269
270
271
272
273
274 BUG_ON(type == TRANS_JOIN_NOLOCK);
275
276 cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
277 if (!cur_trans)
278 return -ENOMEM;
279
280 spin_lock(&fs_info->trans_lock);
281 if (fs_info->running_transaction) {
282
283
284
285
286 kfree(cur_trans);
287 goto loop;
288 } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
289 spin_unlock(&fs_info->trans_lock);
290 kfree(cur_trans);
291 return -EROFS;
292 }
293
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;
302
303
304
305
306 refcount_set(&cur_trans->use_count, 2);
307 cur_trans->flags = 0;
308 cur_trans->start_time = ktime_get_seconds();
309
310 memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
311
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);
315
316
317
318
319
320 smp_mb();
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);
326
327 spin_lock_init(&cur_trans->delayed_refs.lock);
328
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);
349
350 return 0;
351}
352
353
354
355
356
357
358
359static int record_root_in_trans(struct btrfs_trans_handle *trans,
360 struct btrfs_root *root,
361 int force)
362{
363 struct btrfs_fs_info *fs_info = root->fs_info;
364
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);
369
370
371
372
373
374
375 set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
376
377
378
379
380 smp_wmb();
381
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);
385 return 0;
386 }
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;
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 btrfs_init_reloc_root(trans, root);
413 smp_mb__before_atomic();
414 clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
415 }
416 return 0;
417}
418
419
420void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
422{
423 struct btrfs_fs_info *fs_info = root->fs_info;
424 struct btrfs_transaction *cur_trans = trans->transaction;
425
426
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);
430
431
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);
437}
438
439int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
441{
442 struct btrfs_fs_info *fs_info = root->fs_info;
443
444 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
445 return 0;
446
447
448
449
450
451 smp_rmb();
452 if (root->last_trans == trans->transid &&
453 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
454 return 0;
455
456 mutex_lock(&fs_info->reloc_mutex);
457 record_root_in_trans(trans, root, 0);
458 mutex_unlock(&fs_info->reloc_mutex);
459
460 return 0;
461}
462
463static inline int is_transaction_blocked(struct btrfs_transaction *trans)
464{
465 return (trans->state >= TRANS_STATE_COMMIT_START &&
466 trans->state < TRANS_STATE_UNBLOCKED &&
467 !TRANS_ABORTED(trans));
468}
469
470
471
472
473
474static void wait_current_trans(struct btrfs_fs_info *fs_info)
475{
476 struct btrfs_transaction *cur_trans;
477
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);
483
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);
488 } else {
489 spin_unlock(&fs_info->trans_lock);
490 }
491}
492
493static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
494{
495 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
496 return 0;
497
498 if (type == TRANS_START)
499 return 1;
500
501 return 0;
502}
503
504static inline bool need_reserve_reloc_root(struct btrfs_root *root)
505{
506 struct btrfs_fs_info *fs_info = root->fs_info;
507
508 if (!fs_info->reloc_ctl ||
509 !test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
510 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
511 root->reloc_root)
512 return false;
513
514 return true;
515}
516
517static struct btrfs_trans_handle *
518start_transaction(struct btrfs_root *root, unsigned int num_items,
519 unsigned int type, enum btrfs_reserve_flush_enum flush,
520 bool enforce_qgroups)
521{
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;
526 u64 num_bytes = 0;
527 u64 qgroup_reserved = 0;
528 bool reloc_reserved = false;
529 bool do_chunk_alloc = false;
530 int ret;
531
532
533 ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
534
535 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
536 return ERR_PTR(-EROFS);
537
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;
544 h->block_rsv = NULL;
545 goto got_it;
546 }
547
548
549
550
551
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;
555
556 qgroup_reserved = num_items * fs_info->nodesize;
557 ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
558 enforce_qgroups);
559 if (ret)
560 return ERR_PTR(ret);
561
562
563
564
565
566
567
568
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;
573 num_bytes <<= 1;
574 }
575
576
577
578
579 if (need_reserve_reloc_root(root)) {
580 num_bytes += fs_info->nodesize;
581 reloc_reserved = true;
582 }
583
584 ret = btrfs_block_rsv_add(root, rsv, num_bytes, flush);
585 if (ret)
586 goto reserve_fail;
587 if (delayed_refs_bytes) {
588 btrfs_migrate_to_delayed_refs_rsv(fs_info, rsv,
589 delayed_refs_bytes);
590 num_bytes -= delayed_refs_bytes;
591 }
592
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) {
597
598
599
600
601
602
603
604 ret = btrfs_delayed_refs_rsv_refill(fs_info, flush);
605 if (ret)
606 goto reserve_fail;
607 }
608again:
609 h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
610 if (!h) {
611 ret = -ENOMEM;
612 goto alloc_fail;
613 }
614
615
616
617
618
619
620
621
622
623
624
625 if (type & __TRANS_FREEZABLE)
626 sb_start_intwrite(fs_info->sb);
627
628 if (may_wait_transaction(fs_info, type))
629 wait_current_trans(fs_info);
630
631 do {
632 ret = join_transaction(fs_info, type);
633 if (ret == -EBUSY) {
634 wait_current_trans(fs_info);
635 if (unlikely(type == TRANS_ATTACH ||
636 type == TRANS_JOIN_NOSTART))
637 ret = -ENOENT;
638 }
639 } while (ret == -EBUSY);
640
641 if (ret < 0)
642 goto join_fail;
643
644 cur_trans = fs_info->running_transaction;
645
646 h->transid = cur_trans->transid;
647 h->transaction = cur_trans;
648 h->root = root;
649 refcount_set(&h->use_count, 1);
650 h->fs_info = root->fs_info;
651
652 h->type = type;
653 h->can_flush_pending_bgs = true;
654 INIT_LIST_HEAD(&h->new_bgs);
655
656 smp_mb();
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);
661 goto again;
662 }
663
664 if (num_bytes) {
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;
670 }
671
672got_it:
673 if (!current->journal_info)
674 current->journal_info = h;
675
676
677
678
679
680
681
682 if (do_chunk_alloc && num_bytes) {
683 u64 flags = h->block_rsv->space_info->flags;
684
685 btrfs_chunk_alloc(h, btrfs_get_alloc_profile(fs_info, flags),
686 CHUNK_ALLOC_NO_FORCE);
687 }
688
689
690
691
692
693
694
695
696
697 btrfs_record_root_in_trans(h, root);
698
699 return h;
700
701join_fail:
702 if (type & __TRANS_FREEZABLE)
703 sb_end_intwrite(fs_info->sb);
704 kmem_cache_free(btrfs_trans_handle_cachep, h);
705alloc_fail:
706 if (num_bytes)
707 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
708 num_bytes, NULL);
709reserve_fail:
710 btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
711 return ERR_PTR(ret);
712}
713
714struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
715 unsigned int num_items)
716{
717 return start_transaction(root, num_items, TRANS_START,
718 BTRFS_RESERVE_FLUSH_ALL, true);
719}
720
721struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
722 struct btrfs_root *root,
723 unsigned int num_items)
724{
725 return start_transaction(root, num_items, TRANS_START,
726 BTRFS_RESERVE_FLUSH_ALL_STEAL, false);
727}
728
729struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
730{
731 return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
732 true);
733}
734
735struct btrfs_trans_handle *btrfs_join_transaction_spacecache(struct btrfs_root *root)
736{
737 return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
738 BTRFS_RESERVE_NO_FLUSH, true);
739}
740
741
742
743
744
745struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
746{
747 return start_transaction(root, 0, TRANS_JOIN_NOSTART,
748 BTRFS_RESERVE_NO_FLUSH, true);
749}
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
765{
766 return start_transaction(root, 0, TRANS_ATTACH,
767 BTRFS_RESERVE_NO_FLUSH, true);
768}
769
770
771
772
773
774
775
776
777struct btrfs_trans_handle *
778btrfs_attach_transaction_barrier(struct btrfs_root *root)
779{
780 struct btrfs_trans_handle *trans;
781
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);
786
787 return trans;
788}
789
790
791static noinline void wait_for_commit(struct btrfs_transaction *commit)
792{
793 wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
794}
795
796int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
797{
798 struct btrfs_transaction *cur_trans = NULL, *t;
799 int ret = 0;
800
801 if (transid) {
802 if (transid <= fs_info->last_trans_committed)
803 goto out;
804
805
806 spin_lock(&fs_info->trans_lock);
807 list_for_each_entry(t, &fs_info->trans_list, list) {
808 if (t->transid == transid) {
809 cur_trans = t;
810 refcount_inc(&cur_trans->use_count);
811 ret = 0;
812 break;
813 }
814 if (t->transid > transid) {
815 ret = 0;
816 break;
817 }
818 }
819 spin_unlock(&fs_info->trans_lock);
820
821
822
823
824
825 if (!cur_trans) {
826 if (transid > fs_info->last_trans_committed)
827 ret = -EINVAL;
828 goto out;
829 }
830 } else {
831
832 spin_lock(&fs_info->trans_lock);
833 list_for_each_entry_reverse(t, &fs_info->trans_list,
834 list) {
835 if (t->state >= TRANS_STATE_COMMIT_START) {
836 if (t->state == TRANS_STATE_COMPLETED)
837 break;
838 cur_trans = t;
839 refcount_inc(&cur_trans->use_count);
840 break;
841 }
842 }
843 spin_unlock(&fs_info->trans_lock);
844 if (!cur_trans)
845 goto out;
846 }
847
848 wait_for_commit(cur_trans);
849 btrfs_put_transaction(cur_trans);
850out:
851 return ret;
852}
853
854void btrfs_throttle(struct btrfs_fs_info *fs_info)
855{
856 wait_current_trans(fs_info);
857}
858
859static int should_end_transaction(struct btrfs_trans_handle *trans)
860{
861 struct btrfs_fs_info *fs_info = trans->fs_info;
862
863 if (btrfs_check_space_for_delayed_refs(fs_info))
864 return 1;
865
866 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
867}
868
869int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
870{
871 struct btrfs_transaction *cur_trans = trans->transaction;
872
873 smp_mb();
874 if (cur_trans->state >= TRANS_STATE_COMMIT_START ||
875 cur_trans->delayed_refs.flushing)
876 return 1;
877
878 return should_end_transaction(trans);
879}
880
881static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
882
883{
884 struct btrfs_fs_info *fs_info = trans->fs_info;
885
886 if (!trans->block_rsv) {
887 ASSERT(!trans->bytes_reserved);
888 return;
889 }
890
891 if (!trans->bytes_reserved)
892 return;
893
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;
900}
901
902static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
903 int throttle)
904{
905 struct btrfs_fs_info *info = trans->fs_info;
906 struct btrfs_transaction *cur_trans = trans->transaction;
907 int err = 0;
908
909 if (refcount_read(&trans->use_count) > 1) {
910 refcount_dec(&trans->use_count);
911 trans->block_rsv = trans->orig_rsv;
912 return 0;
913 }
914
915 btrfs_trans_release_metadata(trans);
916 trans->block_rsv = NULL;
917
918 btrfs_create_pending_block_groups(trans);
919
920 btrfs_trans_release_chunk_metadata(trans);
921
922 if (trans->type & __TRANS_FREEZABLE)
923 sb_end_intwrite(info->sb);
924
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);
929
930 cond_wake_up(&cur_trans->writer_wait);
931 btrfs_put_transaction(cur_trans);
932
933 if (current->journal_info == trans)
934 current->journal_info = NULL;
935
936 if (throttle)
937 btrfs_run_delayed_iputs(info);
938
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;
944 else
945 err = -EROFS;
946 }
947
948 kmem_cache_free(btrfs_trans_handle_cachep, trans);
949 return err;
950}
951
952int btrfs_end_transaction(struct btrfs_trans_handle *trans)
953{
954 return __btrfs_end_transaction(trans, 0);
955}
956
957int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
958{
959 return __btrfs_end_transaction(trans, 1);
960}
961
962
963
964
965
966
967int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
968 struct extent_io_tree *dirty_pages, int mark)
969{
970 int err = 0;
971 int werr = 0;
972 struct address_space *mapping = fs_info->btree_inode->i_mapping;
973 struct extent_state *cached_state = NULL;
974 u64 start = 0;
975 u64 end;
976
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;
981
982 err = convert_extent_bit(dirty_pages, start, end,
983 EXTENT_NEED_WAIT,
984 mark, &cached_state);
985
986
987
988
989
990
991
992
993
994
995
996
997
998 if (err == -ENOMEM) {
999 err = 0;
1000 wait_writeback = true;
1001 }
1002 if (!err)
1003 err = filemap_fdatawrite_range(mapping, start, end);
1004 if (err)
1005 werr = err;
1006 else if (wait_writeback)
1007 werr = filemap_fdatawait_range(mapping, start, end);
1008 free_extent_state(cached_state);
1009 cached_state = NULL;
1010 cond_resched();
1011 start = end + 1;
1012 }
1013 atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
1014 return werr;
1015}
1016
1017
1018
1019
1020
1021
1022
1023static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
1024 struct extent_io_tree *dirty_pages)
1025{
1026 int err = 0;
1027 int werr = 0;
1028 struct address_space *mapping = fs_info->btree_inode->i_mapping;
1029 struct extent_state *cached_state = NULL;
1030 u64 start = 0;
1031 u64 end;
1032
1033 while (!find_first_extent_bit(dirty_pages, start, &start, &end,
1034 EXTENT_NEED_WAIT, &cached_state)) {
1035
1036
1037
1038
1039
1040
1041
1042
1043 err = clear_extent_bit(dirty_pages, start, end,
1044 EXTENT_NEED_WAIT, 0, 0, &cached_state);
1045 if (err == -ENOMEM)
1046 err = 0;
1047 if (!err)
1048 err = filemap_fdatawait_range(mapping, start, end);
1049 if (err)
1050 werr = err;
1051 free_extent_state(cached_state);
1052 cached_state = NULL;
1053 cond_resched();
1054 start = end + 1;
1055 }
1056 if (err)
1057 werr = err;
1058 return werr;
1059}
1060
1061static int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
1062 struct extent_io_tree *dirty_pages)
1063{
1064 bool errors = false;
1065 int err;
1066
1067 err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
1068 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1069 errors = true;
1070
1071 if (errors && !err)
1072 err = -EIO;
1073 return err;
1074}
1075
1076int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
1077{
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;
1081 int err;
1082
1083 ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
1084
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))
1088 errors = true;
1089
1090 if ((mark & EXTENT_NEW) &&
1091 test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1092 errors = true;
1093
1094 if (errors && !err)
1095 err = -EIO;
1096 return err;
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
1107{
1108 int ret;
1109 int ret2;
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;
1113
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);
1118
1119 extent_io_tree_release(&trans->transaction->dirty_pages);
1120
1121 if (ret)
1122 return ret;
1123 else if (ret2)
1124 return ret2;
1125 else
1126 return 0;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139static int update_cowonly_root(struct btrfs_trans_handle *trans,
1140 struct btrfs_root *root)
1141{
1142 int ret;
1143 u64 old_root_bytenr;
1144 u64 old_root_used;
1145 struct btrfs_fs_info *fs_info = root->fs_info;
1146 struct btrfs_root *tree_root = fs_info->tree_root;
1147
1148 old_root_used = btrfs_root_used(&root->root_item);
1149
1150 while (1) {
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))
1154 break;
1155
1156 btrfs_set_root_node(&root->root_item, root->node);
1157 ret = btrfs_update_root(trans, tree_root,
1158 &root->root_key,
1159 &root->root_item);
1160 if (ret)
1161 return ret;
1162
1163 old_root_used = btrfs_root_used(&root->root_item);
1164 }
1165
1166 return 0;
1167}
1168
1169
1170
1171
1172
1173
1174
1175
1176static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
1177{
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;
1183 int ret;
1184
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);
1190
1191 if (ret)
1192 return ret;
1193
1194 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1195 if (ret)
1196 return ret;
1197
1198 ret = btrfs_run_dev_stats(trans);
1199 if (ret)
1200 return ret;
1201 ret = btrfs_run_dev_replace(trans);
1202 if (ret)
1203 return ret;
1204 ret = btrfs_run_qgroups(trans);
1205 if (ret)
1206 return ret;
1207
1208 ret = btrfs_setup_space_cache(trans);
1209 if (ret)
1210 return ret;
1211
1212
1213 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1214 if (ret)
1215 return ret;
1216again:
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);
1223
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);
1228 if (ret)
1229 return ret;
1230 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1231 if (ret)
1232 return ret;
1233 }
1234
1235 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1236 ret = btrfs_write_dirty_block_groups(trans);
1237 if (ret)
1238 return ret;
1239 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1240 if (ret)
1241 return ret;
1242 }
1243
1244 if (!list_empty(&fs_info->dirty_cowonly_roots))
1245 goto again;
1246
1247 list_add_tail(&fs_info->extent_root->dirty_list,
1248 &trans->transaction->switch_commits);
1249
1250
1251 fs_info->dev_replace.committed_cursor_left =
1252 fs_info->dev_replace.cursor_left_last_write_of_item;
1253
1254 return 0;
1255}
1256
1257
1258
1259
1260
1261
1262void btrfs_add_dead_root(struct btrfs_root *root)
1263{
1264 struct btrfs_fs_info *fs_info = root->fs_info;
1265
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);
1270 }
1271 spin_unlock(&fs_info->trans_lock);
1272}
1273
1274
1275
1276
1277static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
1278{
1279 struct btrfs_fs_info *fs_info = trans->fs_info;
1280 struct btrfs_root *gang[8];
1281 int i;
1282 int ret;
1283 int err = 0;
1284
1285 spin_lock(&fs_info->fs_roots_radix_lock);
1286 while (1) {
1287 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
1288 (void **)gang, 0,
1289 ARRAY_SIZE(gang),
1290 BTRFS_ROOT_TRANS_TAG);
1291 if (ret == 0)
1292 break;
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);
1299
1300 btrfs_free_log(trans, root);
1301 btrfs_update_reloc_root(trans, root);
1302
1303 btrfs_save_ino_cache(root, trans);
1304
1305
1306 clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1307 smp_mb__after_atomic();
1308
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,
1313 root->node);
1314 }
1315
1316 err = btrfs_update_root(trans, fs_info->tree_root,
1317 &root->root_key,
1318 &root->root_item);
1319 spin_lock(&fs_info->fs_roots_radix_lock);
1320 if (err)
1321 break;
1322 btrfs_qgroup_free_meta_all_pertrans(root);
1323 }
1324 }
1325 spin_unlock(&fs_info->fs_roots_radix_lock);
1326 return err;
1327}
1328
1329
1330
1331
1332
1333int btrfs_defrag_root(struct btrfs_root *root)
1334{
1335 struct btrfs_fs_info *info = root->fs_info;
1336 struct btrfs_trans_handle *trans;
1337 int ret;
1338
1339 if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
1340 return 0;
1341
1342 while (1) {
1343 trans = btrfs_start_transaction(root, 0);
1344 if (IS_ERR(trans))
1345 return PTR_ERR(trans);
1346
1347 ret = btrfs_defrag_leaves(trans, root);
1348
1349 btrfs_end_transaction(trans);
1350 btrfs_btree_balance_dirty(info);
1351 cond_resched();
1352
1353 if (btrfs_fs_closing(info) || ret != -EAGAIN)
1354 break;
1355
1356 if (btrfs_defrag_cancelled(info)) {
1357 btrfs_debug(info, "defrag_root cancelled");
1358 ret = -EAGAIN;
1359 break;
1360 }
1361 }
1362 clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
1363 return ret;
1364}
1365
1366
1367
1368
1369
1370
1371
1372
1373static 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,
1377 u64 dst_objectid)
1378{
1379 struct btrfs_fs_info *fs_info = src->fs_info;
1380 int ret;
1381
1382
1383
1384
1385
1386
1387 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1388 return 0;
1389
1390
1391
1392
1393
1394
1395
1396 record_root_in_trans(trans, src, 1);
1397
1398
1399
1400
1401
1402 mutex_lock(&fs_info->tree_log_mutex);
1403
1404 ret = commit_fs_roots(trans);
1405 if (ret)
1406 goto out;
1407 ret = btrfs_qgroup_account_extents(trans);
1408 if (ret < 0)
1409 goto out;
1410
1411
1412 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1413 inherit);
1414 if (ret < 0)
1415 goto out;
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 ret = commit_cowonly_roots(trans);
1430 if (ret)
1431 goto out;
1432 switch_commit_roots(trans);
1433 ret = btrfs_write_and_wait_transaction(trans);
1434 if (ret)
1435 btrfs_handle_fs_error(fs_info, ret,
1436 "Error while writing out transaction for qgroup");
1437
1438out:
1439 mutex_unlock(&fs_info->tree_log_mutex);
1440
1441
1442
1443
1444
1445
1446
1447 if (!ret)
1448 record_root_in_trans(trans, parent, 1);
1449 return ret;
1450}
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1462 struct btrfs_pending_snapshot *pending)
1463{
1464
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;
1479 int ret = 0;
1480 u64 to_reserve = 0;
1481 u64 index = 0;
1482 u64 objectid;
1483 u64 root_flags;
1484
1485 ASSERT(pending->path);
1486 path = pending->path;
1487
1488 ASSERT(pending->root_item);
1489 new_root_item = pending->root_item;
1490
1491 pending->error = btrfs_find_free_objectid(tree_root, &objectid);
1492 if (pending->error)
1493 goto no_free_objectid;
1494
1495
1496
1497
1498
1499 btrfs_set_skip_qgroup(trans, objectid);
1500
1501 btrfs_reloc_pre_snapshot(pending, &to_reserve);
1502
1503 if (to_reserve > 0) {
1504 pending->error = btrfs_block_rsv_add(root,
1505 &pending->block_rsv,
1506 to_reserve,
1507 BTRFS_RESERVE_NO_FLUSH);
1508 if (pending->error)
1509 goto clear_skip_qgroup;
1510 }
1511
1512 key.objectid = objectid;
1513 key.offset = (u64)-1;
1514 key.type = BTRFS_ROOT_ITEM_KEY;
1515
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",
1520 trans->transid,
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);
1526
1527 cur_time = current_time(parent_inode);
1528
1529
1530
1531
1532 ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
1533 BUG_ON(ret);
1534
1535
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);
1546 goto fail;
1547 }
1548 btrfs_release_path(path);
1549
1550
1551
1552
1553
1554
1555
1556 ret = btrfs_run_delayed_items(trans);
1557 if (ret) {
1558 btrfs_abort_transaction(trans, ret);
1559 goto fail;
1560 }
1561
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);
1566
1567 root_flags = btrfs_root_flags(new_root_item);
1568 if (pending->readonly)
1569 root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
1570 else
1571 root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
1572 btrfs_set_root_flags(new_root_item, root_flags);
1573
1574 btrfs_set_root_generation_v2(new_root_item,
1575 trans->transid);
1576 generate_random_guid(new_root_item->uuid);
1577 memcpy(new_root_item->parent_uuid, root->root_item.uuid,
1578 BTRFS_UUID_SIZE);
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);
1586 }
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);
1590
1591 old = btrfs_lock_root_node(root);
1592 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old,
1593 BTRFS_NESTING_COW);
1594 if (ret) {
1595 btrfs_tree_unlock(old);
1596 free_extent_buffer(old);
1597 btrfs_abort_transaction(trans, ret);
1598 goto fail;
1599 }
1600
1601 btrfs_set_lock_blocking_write(old);
1602
1603 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
1604
1605 btrfs_tree_unlock(old);
1606 free_extent_buffer(old);
1607 if (ret) {
1608 btrfs_abort_transaction(trans, ret);
1609 goto fail;
1610 }
1611
1612 set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
1613 smp_wmb();
1614
1615 btrfs_set_root_node(new_root_item, tmp);
1616
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);
1621 if (ret) {
1622 btrfs_abort_transaction(trans, ret);
1623 goto fail;
1624 }
1625
1626
1627
1628
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);
1633 if (ret) {
1634 btrfs_abort_transaction(trans, ret);
1635 goto fail;
1636 }
1637
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);
1644 goto fail;
1645 }
1646
1647 ret = btrfs_reloc_post_snapshot(trans, pending);
1648 if (ret) {
1649 btrfs_abort_transaction(trans, ret);
1650 goto fail;
1651 }
1652
1653 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1654 if (ret) {
1655 btrfs_abort_transaction(trans, ret);
1656 goto fail;
1657 }
1658
1659
1660
1661
1662
1663
1664
1665 ret = qgroup_account_snapshot(trans, root, parent_root,
1666 pending->inherit, objectid);
1667 if (ret < 0)
1668 goto fail;
1669
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
1674 BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
1675 if (ret) {
1676 btrfs_abort_transaction(trans, ret);
1677 goto fail;
1678 }
1679
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);
1685 if (ret) {
1686 btrfs_abort_transaction(trans, ret);
1687 goto fail;
1688 }
1689 ret = btrfs_uuid_tree_add(trans, new_root_item->uuid,
1690 BTRFS_UUID_KEY_SUBVOL,
1691 objectid);
1692 if (ret) {
1693 btrfs_abort_transaction(trans, ret);
1694 goto fail;
1695 }
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,
1699 objectid);
1700 if (ret && ret != -EEXIST) {
1701 btrfs_abort_transaction(trans, ret);
1702 goto fail;
1703 }
1704 }
1705
1706 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
1707 if (ret) {
1708 btrfs_abort_transaction(trans, ret);
1709 goto fail;
1710 }
1711
1712fail:
1713 pending->error = ret;
1714dir_item_existed:
1715 trans->block_rsv = rsv;
1716 trans->bytes_reserved = 0;
1717clear_skip_qgroup:
1718 btrfs_clear_skip_qgroup(trans);
1719no_free_objectid:
1720 kfree(new_root_item);
1721 pending->root_item = NULL;
1722 btrfs_free_path(path);
1723 pending->path = NULL;
1724
1725 return ret;
1726}
1727
1728
1729
1730
1731static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
1732{
1733 struct btrfs_pending_snapshot *pending, *next;
1734 struct list_head *head = &trans->transaction->pending_snapshots;
1735 int ret = 0;
1736
1737 list_for_each_entry_safe(pending, next, head, list) {
1738 list_del(&pending->list);
1739 ret = create_pending_snapshot(trans, pending);
1740 if (ret)
1741 break;
1742 }
1743 return ret;
1744}
1745
1746static void update_super_roots(struct btrfs_fs_info *fs_info)
1747{
1748 struct btrfs_root_item *root_item;
1749 struct btrfs_super_block *super;
1750
1751 super = fs_info->super_copy;
1752
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;
1757
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;
1766}
1767
1768int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1769{
1770 struct btrfs_transaction *trans;
1771 int ret = 0;
1772
1773 spin_lock(&info->trans_lock);
1774 trans = info->running_transaction;
1775 if (trans)
1776 ret = (trans->state >= TRANS_STATE_COMMIT_START);
1777 spin_unlock(&info->trans_lock);
1778 return ret;
1779}
1780
1781int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1782{
1783 struct btrfs_transaction *trans;
1784 int ret = 0;
1785
1786 spin_lock(&info->trans_lock);
1787 trans = info->running_transaction;
1788 if (trans)
1789 ret = is_transaction_blocked(trans);
1790 spin_unlock(&info->trans_lock);
1791 return ret;
1792}
1793
1794
1795
1796
1797
1798static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1799 struct btrfs_transaction *trans)
1800{
1801 wait_event(fs_info->transaction_blocked_wait,
1802 trans->state >= TRANS_STATE_COMMIT_START ||
1803 TRANS_ABORTED(trans));
1804}
1805
1806
1807
1808
1809
1810static void wait_current_trans_commit_start_and_unblock(
1811 struct btrfs_fs_info *fs_info,
1812 struct btrfs_transaction *trans)
1813{
1814 wait_event(fs_info->transaction_wait,
1815 trans->state >= TRANS_STATE_UNBLOCKED ||
1816 TRANS_ABORTED(trans));
1817}
1818
1819
1820
1821
1822
1823struct btrfs_async_commit {
1824 struct btrfs_trans_handle *newtrans;
1825 struct work_struct work;
1826};
1827
1828static void do_async_commit(struct work_struct *work)
1829{
1830 struct btrfs_async_commit *ac =
1831 container_of(work, struct btrfs_async_commit, work);
1832
1833
1834
1835
1836
1837 if (ac->newtrans->type & __TRANS_FREEZABLE)
1838 __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
1839
1840 current->journal_info = ac->newtrans;
1841
1842 btrfs_commit_transaction(ac->newtrans);
1843 kfree(ac);
1844}
1845
1846int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1847 int wait_for_unblock)
1848{
1849 struct btrfs_fs_info *fs_info = trans->fs_info;
1850 struct btrfs_async_commit *ac;
1851 struct btrfs_transaction *cur_trans;
1852
1853 ac = kmalloc(sizeof(*ac), GFP_NOFS);
1854 if (!ac)
1855 return -ENOMEM;
1856
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);
1861 kfree(ac);
1862 return err;
1863 }
1864
1865
1866 cur_trans = trans->transaction;
1867 refcount_inc(&cur_trans->use_count);
1868
1869 btrfs_end_transaction(trans);
1870
1871
1872
1873
1874
1875 if (ac->newtrans->type & __TRANS_FREEZABLE)
1876 __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
1877
1878 schedule_work(&ac->work);
1879
1880
1881 if (wait_for_unblock)
1882 wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1883 else
1884 wait_current_trans_commit_start(fs_info, cur_trans);
1885
1886 if (current->journal_info == trans)
1887 current->journal_info = NULL;
1888
1889 btrfs_put_transaction(cur_trans);
1890 return 0;
1891}
1892
1893
1894static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
1895{
1896 struct btrfs_fs_info *fs_info = trans->fs_info;
1897 struct btrfs_transaction *cur_trans = trans->transaction;
1898
1899 WARN_ON(refcount_read(&trans->use_count) > 1);
1900
1901 btrfs_abort_transaction(trans, err);
1902
1903 spin_lock(&fs_info->trans_lock);
1904
1905
1906
1907
1908
1909
1910 BUG_ON(list_empty(&cur_trans->list));
1911
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);
1918
1919 spin_lock(&fs_info->trans_lock);
1920 }
1921 spin_unlock(&fs_info->trans_lock);
1922
1923 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
1924
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);
1929
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);
1934
1935 trace_btrfs_transaction_commit(trans->root);
1936
1937 if (current->journal_info == trans)
1938 current->journal_info = NULL;
1939 btrfs_scrub_cancel(fs_info);
1940
1941 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1942}
1943
1944
1945
1946
1947
1948static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans)
1949{
1950 struct btrfs_fs_info *fs_info = trans->fs_info;
1951 struct btrfs_block_group *block_group, *tmp;
1952
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);
1956 }
1957}
1958
1959static inline int btrfs_start_delalloc_flush(struct btrfs_trans_handle *trans)
1960{
1961 struct btrfs_fs_info *fs_info = trans->fs_info;
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
1973 writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
1974 } else {
1975 struct btrfs_pending_snapshot *pending;
1976 struct list_head *head = &trans->transaction->pending_snapshots;
1977
1978
1979
1980
1981
1982
1983
1984
1985 list_for_each_entry(pending, head, list) {
1986 int ret;
1987
1988 ret = btrfs_start_delalloc_snapshot(pending->root);
1989 if (ret)
1990 return ret;
1991 }
1992 }
1993 return 0;
1994}
1995
1996static inline void btrfs_wait_delalloc_flush(struct btrfs_trans_handle *trans)
1997{
1998 struct btrfs_fs_info *fs_info = trans->fs_info;
1999
2000 if (btrfs_test_opt(fs_info, FLUSHONCOMMIT)) {
2001 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
2002 } else {
2003 struct btrfs_pending_snapshot *pending;
2004 struct list_head *head = &trans->transaction->pending_snapshots;
2005
2006
2007
2008
2009
2010
2011
2012 list_for_each_entry(pending, head, list)
2013 btrfs_wait_ordered_extents(pending->root,
2014 U64_MAX, 0, U64_MAX);
2015 }
2016}
2017
2018int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
2019{
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;
2023 int ret;
2024
2025 ASSERT(refcount_read(&trans->use_count) == 1);
2026
2027
2028
2029
2030
2031
2032
2033 trans->dirty = true;
2034
2035
2036 if (TRANS_ABORTED(cur_trans)) {
2037 ret = cur_trans->aborted;
2038 btrfs_end_transaction(trans);
2039 return ret;
2040 }
2041
2042 btrfs_trans_release_metadata(trans);
2043 trans->block_rsv = NULL;
2044
2045
2046
2047
2048 ret = btrfs_run_delayed_refs(trans, 0);
2049 if (ret) {
2050 btrfs_end_transaction(trans);
2051 return ret;
2052 }
2053
2054 cur_trans = trans->transaction;
2055
2056
2057
2058
2059
2060 cur_trans->delayed_refs.flushing = 1;
2061 smp_wmb();
2062
2063 btrfs_create_pending_block_groups(trans);
2064
2065 ret = btrfs_run_delayed_refs(trans, 0);
2066 if (ret) {
2067 btrfs_end_transaction(trans);
2068 return ret;
2069 }
2070
2071 if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
2072 int run_it = 0;
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087 mutex_lock(&fs_info->ro_block_group_mutex);
2088 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
2089 &cur_trans->flags))
2090 run_it = 1;
2091 mutex_unlock(&fs_info->ro_block_group_mutex);
2092
2093 if (run_it) {
2094 ret = btrfs_start_dirty_block_groups(trans);
2095 if (ret) {
2096 btrfs_end_transaction(trans);
2097 return ret;
2098 }
2099 }
2100 }
2101
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);
2107
2108 wait_for_commit(cur_trans);
2109
2110 if (TRANS_ABORTED(cur_trans))
2111 ret = cur_trans->aborted;
2112
2113 btrfs_put_transaction(cur_trans);
2114
2115 return ret;
2116 }
2117
2118 cur_trans->state = TRANS_STATE_COMMIT_START;
2119 wake_up(&fs_info->transaction_blocked_wait);
2120
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);
2127
2128 wait_for_commit(prev_trans);
2129 ret = READ_ONCE(prev_trans->aborted);
2130
2131 btrfs_put_transaction(prev_trans);
2132 if (ret)
2133 goto cleanup_transaction;
2134 } else {
2135 spin_unlock(&fs_info->trans_lock);
2136 }
2137 } else {
2138 spin_unlock(&fs_info->trans_lock);
2139
2140
2141
2142
2143
2144
2145 if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
2146 ret = -EROFS;
2147 goto cleanup_transaction;
2148 }
2149 }
2150
2151 extwriter_counter_dec(cur_trans, trans->type);
2152
2153 ret = btrfs_start_delalloc_flush(trans);
2154 if (ret)
2155 goto cleanup_transaction;
2156
2157 ret = btrfs_run_delayed_items(trans);
2158 if (ret)
2159 goto cleanup_transaction;
2160
2161 wait_event(cur_trans->writer_wait,
2162 extwriter_counter_read(cur_trans) == 0);
2163
2164
2165 ret = btrfs_run_delayed_items(trans);
2166 if (ret)
2167 goto cleanup_transaction;
2168
2169 btrfs_wait_delalloc_flush(trans);
2170
2171
2172
2173
2174
2175
2176 wait_event(cur_trans->pending_wait,
2177 atomic_read(&cur_trans->pending_ordered) == 0);
2178
2179 btrfs_scrub_pause(fs_info);
2180
2181
2182
2183
2184
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);
2190
2191 if (TRANS_ABORTED(cur_trans)) {
2192 ret = cur_trans->aborted;
2193 goto scrub_continue;
2194 }
2195
2196
2197
2198
2199
2200 mutex_lock(&fs_info->reloc_mutex);
2201
2202
2203
2204
2205
2206
2207 ret = create_pending_snapshots(trans);
2208 if (ret)
2209 goto unlock_reloc;
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221 ret = btrfs_run_delayed_items(trans);
2222 if (ret)
2223 goto unlock_reloc;
2224
2225 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2226 if (ret)
2227 goto unlock_reloc;
2228
2229
2230
2231
2232
2233 btrfs_assert_delayed_root_empty(fs_info);
2234
2235 WARN_ON(cur_trans != trans->transaction);
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250 mutex_lock(&fs_info->tree_log_mutex);
2251
2252 ret = commit_fs_roots(trans);
2253 if (ret)
2254 goto unlock_tree_log;
2255
2256
2257
2258
2259
2260 btrfs_apply_pending_changes(fs_info);
2261
2262
2263
2264
2265 btrfs_free_log_root_tree(trans, fs_info);
2266
2267
2268
2269
2270
2271 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
2272 if (ret)
2273 goto unlock_tree_log;
2274
2275
2276
2277
2278
2279 ret = btrfs_qgroup_account_extents(trans);
2280 if (ret < 0)
2281 goto unlock_tree_log;
2282
2283 ret = commit_cowonly_roots(trans);
2284 if (ret)
2285 goto unlock_tree_log;
2286
2287
2288
2289
2290
2291 if (TRANS_ABORTED(cur_trans)) {
2292 ret = cur_trans->aborted;
2293 goto unlock_tree_log;
2294 }
2295
2296 btrfs_prepare_extent_commit(fs_info);
2297
2298 cur_trans = fs_info->running_transaction;
2299
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);
2304
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);
2309
2310 switch_commit_roots(trans);
2311
2312 ASSERT(list_empty(&cur_trans->dirty_bgs));
2313 ASSERT(list_empty(&cur_trans->io_bgs));
2314 update_super_roots(fs_info);
2315
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));
2320
2321 btrfs_commit_device_sizes(cur_trans);
2322
2323 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2324 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2325
2326 btrfs_trans_release_chunk_metadata(trans);
2327
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);
2333
2334 wake_up(&fs_info->transaction_wait);
2335
2336 ret = btrfs_write_and_wait_transaction(trans);
2337 if (ret) {
2338 btrfs_handle_fs_error(fs_info, ret,
2339 "Error while writing out transaction");
2340
2341
2342
2343
2344 mutex_unlock(&fs_info->tree_log_mutex);
2345 goto scrub_continue;
2346 }
2347
2348 ret = write_all_supers(fs_info, 0);
2349
2350
2351
2352
2353 mutex_unlock(&fs_info->tree_log_mutex);
2354 if (ret)
2355 goto scrub_continue;
2356
2357 btrfs_finish_extent_commit(trans);
2358
2359 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2360 btrfs_clear_space_info_full(fs_info);
2361
2362 fs_info->last_trans_committed = cur_trans->transid;
2363
2364
2365
2366
2367 cur_trans->state = TRANS_STATE_COMPLETED;
2368 wake_up(&cur_trans->commit_wait);
2369
2370 spin_lock(&fs_info->trans_lock);
2371 list_del_init(&cur_trans->list);
2372 spin_unlock(&fs_info->trans_lock);
2373
2374 btrfs_put_transaction(cur_trans);
2375 btrfs_put_transaction(cur_trans);
2376
2377 if (trans->type & __TRANS_FREEZABLE)
2378 sb_end_intwrite(fs_info->sb);
2379
2380 trace_btrfs_transaction_commit(trans->root);
2381
2382 btrfs_scrub_continue(fs_info);
2383
2384 if (current->journal_info == trans)
2385 current->journal_info = NULL;
2386
2387 kmem_cache_free(btrfs_trans_handle_cachep, trans);
2388
2389 return ret;
2390
2391unlock_tree_log:
2392 mutex_unlock(&fs_info->tree_log_mutex);
2393unlock_reloc:
2394 mutex_unlock(&fs_info->reloc_mutex);
2395scrub_continue:
2396 btrfs_scrub_continue(fs_info);
2397cleanup_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);
2406
2407 return ret;
2408}
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
2421{
2422 int ret;
2423 struct btrfs_fs_info *fs_info = root->fs_info;
2424
2425 spin_lock(&fs_info->trans_lock);
2426 if (list_empty(&fs_info->dead_roots)) {
2427 spin_unlock(&fs_info->trans_lock);
2428 return 0;
2429 }
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);
2434
2435 btrfs_debug(fs_info, "cleaner removing %llu", root->root_key.objectid);
2436
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;
2441 }
2442
2443 if (btrfs_header_backref_rev(root->node) <
2444 BTRFS_MIXED_BACKREF_REV)
2445 ret = btrfs_drop_snapshot(root, 0, 0);
2446 else
2447 ret = btrfs_drop_snapshot(root, 1, 0);
2448
2449 btrfs_put_root(root);
2450 return (ret < 0) ? 0 : 1;
2451}
2452
2453void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
2454{
2455 unsigned long prev;
2456 unsigned long bit;
2457
2458 prev = xchg(&fs_info->pending_changes, 0);
2459 if (!prev)
2460 return;
2461
2462 bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
2463 if (prev & bit)
2464 btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2465 prev &= ~bit;
2466
2467 bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
2468 if (prev & bit)
2469 btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
2470 prev &= ~bit;
2471
2472 bit = 1 << BTRFS_PENDING_COMMIT;
2473 if (prev & bit)
2474 btrfs_debug(fs_info, "pending commit done");
2475 prev &= ~bit;
2476
2477 if (prev)
2478 btrfs_warn(fs_info,
2479 "unknown pending changes left 0x%lx, ignoring", prev);
2480}
2481