1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/blkdev.h>
7 #include <linux/module.h>
9 #include <linux/pagemap.h>
10 #include <linux/highmem.h>
11 #include <linux/time.h>
12 #include <linux/init.h>
13 #include <linux/seq_file.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/writeback.h>
18 #include <linux/statfs.h>
19 #include <linux/compat.h>
20 #include <linux/parser.h>
21 #include <linux/ctype.h>
22 #include <linux/namei.h>
23 #include <linux/miscdevice.h>
24 #include <linux/magic.h>
25 #include <linux/slab.h>
26 #include <linux/ratelimit.h>
27 #include <linux/crc32c.h>
28 #include <linux/btrfs.h>
30 #include "delayed-inode.h"
33 #include "transaction.h"
34 #include "btrfs_inode.h"
35 #include "print-tree.h"
40 #include "compression.h"
41 #include "rcu-string.h"
42 #include "dev-replace.h"
43 #include "free-space-cache.h"
45 #include "space-info.h"
48 #include "tests/btrfs-tests.h"
49 #include "block-group.h"
54 #include "accessors.h"
61 #include "extent-tree.h"
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/btrfs.h>
65 static const struct super_operations btrfs_super_ops
;
68 * Types for mounting the default subvolume and a subvolume explicitly
69 * requested by subvol=/path. That way the callchain is straightforward and we
70 * don't have to play tricks with the mount options and recursive calls to
73 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
75 static struct file_system_type btrfs_fs_type
;
76 static struct file_system_type btrfs_root_fs_type
;
78 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
);
80 static void btrfs_put_super(struct super_block
*sb
)
82 close_ctree(btrfs_sb(sb
));
91 Opt_compress_force_type
,
96 Opt_flushoncommit
, Opt_noflushoncommit
,
98 Opt_barrier
, Opt_nobarrier
,
99 Opt_datacow
, Opt_nodatacow
,
100 Opt_datasum
, Opt_nodatasum
,
101 Opt_defrag
, Opt_nodefrag
,
102 Opt_discard
, Opt_nodiscard
,
106 Opt_rescan_uuid_tree
,
108 Opt_space_cache
, Opt_no_space_cache
,
109 Opt_space_cache_version
,
111 Opt_ssd_spread
, Opt_nossd_spread
,
116 Opt_treelog
, Opt_notreelog
,
117 Opt_user_subvol_rm_allowed
,
127 /* Deprecated options */
129 Opt_inode_cache
, Opt_noinode_cache
,
131 /* Debugging options */
133 Opt_check_integrity_including_extent_data
,
134 Opt_check_integrity_print_mask
,
135 Opt_enospc_debug
, Opt_noenospc_debug
,
136 #ifdef CONFIG_BTRFS_DEBUG
137 Opt_fragment_data
, Opt_fragment_metadata
, Opt_fragment_all
,
139 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
145 static const match_table_t tokens
= {
147 {Opt_noacl
, "noacl"},
148 {Opt_clear_cache
, "clear_cache"},
149 {Opt_commit_interval
, "commit=%u"},
150 {Opt_compress
, "compress"},
151 {Opt_compress_type
, "compress=%s"},
152 {Opt_compress_force
, "compress-force"},
153 {Opt_compress_force_type
, "compress-force=%s"},
154 {Opt_degraded
, "degraded"},
155 {Opt_device
, "device=%s"},
156 {Opt_fatal_errors
, "fatal_errors=%s"},
157 {Opt_flushoncommit
, "flushoncommit"},
158 {Opt_noflushoncommit
, "noflushoncommit"},
159 {Opt_inode_cache
, "inode_cache"},
160 {Opt_noinode_cache
, "noinode_cache"},
161 {Opt_max_inline
, "max_inline=%s"},
162 {Opt_barrier
, "barrier"},
163 {Opt_nobarrier
, "nobarrier"},
164 {Opt_datacow
, "datacow"},
165 {Opt_nodatacow
, "nodatacow"},
166 {Opt_datasum
, "datasum"},
167 {Opt_nodatasum
, "nodatasum"},
168 {Opt_defrag
, "autodefrag"},
169 {Opt_nodefrag
, "noautodefrag"},
170 {Opt_discard
, "discard"},
171 {Opt_discard_mode
, "discard=%s"},
172 {Opt_nodiscard
, "nodiscard"},
173 {Opt_norecovery
, "norecovery"},
174 {Opt_ratio
, "metadata_ratio=%u"},
175 {Opt_rescan_uuid_tree
, "rescan_uuid_tree"},
176 {Opt_skip_balance
, "skip_balance"},
177 {Opt_space_cache
, "space_cache"},
178 {Opt_no_space_cache
, "nospace_cache"},
179 {Opt_space_cache_version
, "space_cache=%s"},
181 {Opt_nossd
, "nossd"},
182 {Opt_ssd_spread
, "ssd_spread"},
183 {Opt_nossd_spread
, "nossd_spread"},
184 {Opt_subvol
, "subvol=%s"},
185 {Opt_subvol_empty
, "subvol="},
186 {Opt_subvolid
, "subvolid=%s"},
187 {Opt_thread_pool
, "thread_pool=%u"},
188 {Opt_treelog
, "treelog"},
189 {Opt_notreelog
, "notreelog"},
190 {Opt_user_subvol_rm_allowed
, "user_subvol_rm_allowed"},
193 {Opt_rescue
, "rescue=%s"},
194 /* Deprecated, with alias rescue=nologreplay */
195 {Opt_nologreplay
, "nologreplay"},
196 /* Deprecated, with alias rescue=usebackuproot */
197 {Opt_usebackuproot
, "usebackuproot"},
199 /* Deprecated options */
200 {Opt_recovery
, "recovery"},
202 /* Debugging options */
203 {Opt_check_integrity
, "check_int"},
204 {Opt_check_integrity_including_extent_data
, "check_int_data"},
205 {Opt_check_integrity_print_mask
, "check_int_print_mask=%u"},
206 {Opt_enospc_debug
, "enospc_debug"},
207 {Opt_noenospc_debug
, "noenospc_debug"},
208 #ifdef CONFIG_BTRFS_DEBUG
209 {Opt_fragment_data
, "fragment=data"},
210 {Opt_fragment_metadata
, "fragment=metadata"},
211 {Opt_fragment_all
, "fragment=all"},
213 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
214 {Opt_ref_verify
, "ref_verify"},
219 static const match_table_t rescue_tokens
= {
220 {Opt_usebackuproot
, "usebackuproot"},
221 {Opt_nologreplay
, "nologreplay"},
222 {Opt_ignorebadroots
, "ignorebadroots"},
223 {Opt_ignorebadroots
, "ibadroots"},
224 {Opt_ignoredatacsums
, "ignoredatacsums"},
225 {Opt_ignoredatacsums
, "idatacsums"},
226 {Opt_rescue_all
, "all"},
230 static bool check_ro_option(struct btrfs_fs_info
*fs_info
, unsigned long opt
,
231 const char *opt_name
)
233 if (fs_info
->mount_opt
& opt
) {
234 btrfs_err(fs_info
, "%s must be used with ro mount option",
241 static int parse_rescue_options(struct btrfs_fs_info
*info
, const char *options
)
246 substring_t args
[MAX_OPT_ARGS
];
249 opts
= kstrdup(options
, GFP_KERNEL
);
254 while ((p
= strsep(&opts
, ":")) != NULL
) {
259 token
= match_token(p
, rescue_tokens
, args
);
261 case Opt_usebackuproot
:
263 "trying to use backup root at mount time");
264 btrfs_set_opt(info
->mount_opt
, USEBACKUPROOT
);
266 case Opt_nologreplay
:
267 btrfs_set_and_info(info
, NOLOGREPLAY
,
268 "disabling log replay at mount time");
270 case Opt_ignorebadroots
:
271 btrfs_set_and_info(info
, IGNOREBADROOTS
,
272 "ignoring bad roots");
274 case Opt_ignoredatacsums
:
275 btrfs_set_and_info(info
, IGNOREDATACSUMS
,
276 "ignoring data csums");
279 btrfs_info(info
, "enabling all of the rescue options");
280 btrfs_set_and_info(info
, IGNOREDATACSUMS
,
281 "ignoring data csums");
282 btrfs_set_and_info(info
, IGNOREBADROOTS
,
283 "ignoring bad roots");
284 btrfs_set_and_info(info
, NOLOGREPLAY
,
285 "disabling log replay at mount time");
288 btrfs_info(info
, "unrecognized rescue option '%s'", p
);
302 * Regular mount options parser. Everything that is needed only when
303 * reading in a new superblock is parsed here.
304 * XXX JDM: This needs to be cleaned up for remount.
306 int btrfs_parse_options(struct btrfs_fs_info
*info
, char *options
,
307 unsigned long new_flags
)
309 substring_t args
[MAX_OPT_ARGS
];
314 bool compress_force
= false;
315 enum btrfs_compression_type saved_compress_type
;
316 int saved_compress_level
;
317 bool saved_compress_force
;
319 const bool remounting
= test_bit(BTRFS_FS_STATE_REMOUNTING
, &info
->fs_state
);
321 if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
))
322 btrfs_set_opt(info
->mount_opt
, FREE_SPACE_TREE
);
323 else if (btrfs_free_space_cache_v1_active(info
)) {
324 if (btrfs_is_zoned(info
)) {
326 "zoned: clearing existing space cache");
327 btrfs_set_super_cache_generation(info
->super_copy
, 0);
329 btrfs_set_opt(info
->mount_opt
, SPACE_CACHE
);
334 * Even the options are empty, we still need to do extra check
340 while ((p
= strsep(&options
, ",")) != NULL
) {
345 token
= match_token(p
, tokens
, args
);
348 btrfs_info(info
, "allowing degraded mounts");
349 btrfs_set_opt(info
->mount_opt
, DEGRADED
);
352 case Opt_subvol_empty
:
356 * These are parsed by btrfs_parse_subvol_options or
357 * btrfs_parse_device_options and can be ignored here.
361 btrfs_set_and_info(info
, NODATASUM
,
362 "setting nodatasum");
365 if (btrfs_test_opt(info
, NODATASUM
)) {
366 if (btrfs_test_opt(info
, NODATACOW
))
368 "setting datasum, datacow enabled");
370 btrfs_info(info
, "setting datasum");
372 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
373 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
376 if (!btrfs_test_opt(info
, NODATACOW
)) {
377 if (!btrfs_test_opt(info
, COMPRESS
) ||
378 !btrfs_test_opt(info
, FORCE_COMPRESS
)) {
380 "setting nodatacow, compression disabled");
382 btrfs_info(info
, "setting nodatacow");
385 btrfs_clear_opt(info
->mount_opt
, COMPRESS
);
386 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
387 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
388 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
391 btrfs_clear_and_info(info
, NODATACOW
,
394 case Opt_compress_force
:
395 case Opt_compress_force_type
:
396 compress_force
= true;
399 case Opt_compress_type
:
400 saved_compress_type
= btrfs_test_opt(info
,
402 info
->compress_type
: BTRFS_COMPRESS_NONE
;
403 saved_compress_force
=
404 btrfs_test_opt(info
, FORCE_COMPRESS
);
405 saved_compress_level
= info
->compress_level
;
406 if (token
== Opt_compress
||
407 token
== Opt_compress_force
||
408 strncmp(args
[0].from
, "zlib", 4) == 0) {
409 compress_type
= "zlib";
411 info
->compress_type
= BTRFS_COMPRESS_ZLIB
;
412 info
->compress_level
= BTRFS_ZLIB_DEFAULT_LEVEL
;
414 * args[0] contains uninitialized data since
415 * for these tokens we don't expect any
418 if (token
!= Opt_compress
&&
419 token
!= Opt_compress_force
)
420 info
->compress_level
=
421 btrfs_compress_str2level(
424 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
425 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
426 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
428 } else if (strncmp(args
[0].from
, "lzo", 3) == 0) {
429 compress_type
= "lzo";
430 info
->compress_type
= BTRFS_COMPRESS_LZO
;
431 info
->compress_level
= 0;
432 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
433 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
434 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
435 btrfs_set_fs_incompat(info
, COMPRESS_LZO
);
437 } else if (strncmp(args
[0].from
, "zstd", 4) == 0) {
438 compress_type
= "zstd";
439 info
->compress_type
= BTRFS_COMPRESS_ZSTD
;
440 info
->compress_level
=
441 btrfs_compress_str2level(
444 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
445 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
446 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
447 btrfs_set_fs_incompat(info
, COMPRESS_ZSTD
);
449 } else if (strncmp(args
[0].from
, "no", 2) == 0) {
450 compress_type
= "no";
451 info
->compress_level
= 0;
452 info
->compress_type
= 0;
453 btrfs_clear_opt(info
->mount_opt
, COMPRESS
);
454 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
455 compress_force
= false;
458 btrfs_err(info
, "unrecognized compression value %s",
464 if (compress_force
) {
465 btrfs_set_opt(info
->mount_opt
, FORCE_COMPRESS
);
468 * If we remount from compress-force=xxx to
469 * compress=xxx, we need clear FORCE_COMPRESS
470 * flag, otherwise, there is no way for users
471 * to disable forcible compression separately.
473 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
475 if (no_compress
== 1) {
476 btrfs_info(info
, "use no compression");
477 } else if ((info
->compress_type
!= saved_compress_type
) ||
478 (compress_force
!= saved_compress_force
) ||
479 (info
->compress_level
!= saved_compress_level
)) {
480 btrfs_info(info
, "%s %s compression, level %d",
481 (compress_force
) ? "force" : "use",
482 compress_type
, info
->compress_level
);
484 compress_force
= false;
487 btrfs_set_and_info(info
, SSD
,
488 "enabling ssd optimizations");
489 btrfs_clear_opt(info
->mount_opt
, NOSSD
);
492 btrfs_set_and_info(info
, SSD
,
493 "enabling ssd optimizations");
494 btrfs_set_and_info(info
, SSD_SPREAD
,
495 "using spread ssd allocation scheme");
496 btrfs_clear_opt(info
->mount_opt
, NOSSD
);
499 btrfs_set_opt(info
->mount_opt
, NOSSD
);
500 btrfs_clear_and_info(info
, SSD
,
501 "not using ssd optimizations");
503 case Opt_nossd_spread
:
504 btrfs_clear_and_info(info
, SSD_SPREAD
,
505 "not using spread ssd allocation scheme");
508 btrfs_clear_and_info(info
, NOBARRIER
,
509 "turning on barriers");
512 btrfs_set_and_info(info
, NOBARRIER
,
513 "turning off barriers");
515 case Opt_thread_pool
:
516 ret
= match_int(&args
[0], &intarg
);
518 btrfs_err(info
, "unrecognized thread_pool value %s",
521 } else if (intarg
== 0) {
522 btrfs_err(info
, "invalid value 0 for thread_pool");
526 info
->thread_pool_size
= intarg
;
529 num
= match_strdup(&args
[0]);
531 info
->max_inline
= memparse(num
, NULL
);
534 if (info
->max_inline
) {
535 info
->max_inline
= min_t(u64
,
539 btrfs_info(info
, "max_inline at %llu",
547 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
548 info
->sb
->s_flags
|= SB_POSIXACL
;
551 btrfs_err(info
, "support for ACL not compiled in!");
556 info
->sb
->s_flags
&= ~SB_POSIXACL
;
559 btrfs_set_and_info(info
, NOTREELOG
,
560 "disabling tree log");
563 btrfs_clear_and_info(info
, NOTREELOG
,
564 "enabling tree log");
567 case Opt_nologreplay
:
569 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
570 btrfs_set_and_info(info
, NOLOGREPLAY
,
571 "disabling log replay at mount time");
573 case Opt_flushoncommit
:
574 btrfs_set_and_info(info
, FLUSHONCOMMIT
,
575 "turning on flush-on-commit");
577 case Opt_noflushoncommit
:
578 btrfs_clear_and_info(info
, FLUSHONCOMMIT
,
579 "turning off flush-on-commit");
582 ret
= match_int(&args
[0], &intarg
);
584 btrfs_err(info
, "unrecognized metadata_ratio value %s",
588 info
->metadata_ratio
= intarg
;
589 btrfs_info(info
, "metadata ratio %u",
590 info
->metadata_ratio
);
593 case Opt_discard_mode
:
594 if (token
== Opt_discard
||
595 strcmp(args
[0].from
, "sync") == 0) {
596 btrfs_clear_opt(info
->mount_opt
, DISCARD_ASYNC
);
597 btrfs_set_and_info(info
, DISCARD_SYNC
,
598 "turning on sync discard");
599 } else if (strcmp(args
[0].from
, "async") == 0) {
600 btrfs_clear_opt(info
->mount_opt
, DISCARD_SYNC
);
601 btrfs_set_and_info(info
, DISCARD_ASYNC
,
602 "turning on async discard");
604 btrfs_err(info
, "unrecognized discard mode value %s",
609 btrfs_clear_opt(info
->mount_opt
, NODISCARD
);
612 btrfs_clear_and_info(info
, DISCARD_SYNC
,
613 "turning off discard");
614 btrfs_clear_and_info(info
, DISCARD_ASYNC
,
615 "turning off async discard");
616 btrfs_set_opt(info
->mount_opt
, NODISCARD
);
618 case Opt_space_cache
:
619 case Opt_space_cache_version
:
621 * We already set FREE_SPACE_TREE above because we have
622 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
623 * to allow v1 to be set for extent tree v2, simply
624 * ignore this setting if we're extent tree v2.
626 if (btrfs_fs_incompat(info
, EXTENT_TREE_V2
))
628 if (token
== Opt_space_cache
||
629 strcmp(args
[0].from
, "v1") == 0) {
630 btrfs_clear_opt(info
->mount_opt
,
632 btrfs_set_and_info(info
, SPACE_CACHE
,
633 "enabling disk space caching");
634 } else if (strcmp(args
[0].from
, "v2") == 0) {
635 btrfs_clear_opt(info
->mount_opt
,
637 btrfs_set_and_info(info
, FREE_SPACE_TREE
,
638 "enabling free space tree");
640 btrfs_err(info
, "unrecognized space_cache value %s",
646 case Opt_rescan_uuid_tree
:
647 btrfs_set_opt(info
->mount_opt
, RESCAN_UUID_TREE
);
649 case Opt_no_space_cache
:
651 * We cannot operate without the free space tree with
652 * extent tree v2, ignore this option.
654 if (btrfs_fs_incompat(info
, EXTENT_TREE_V2
))
656 if (btrfs_test_opt(info
, SPACE_CACHE
)) {
657 btrfs_clear_and_info(info
, SPACE_CACHE
,
658 "disabling disk space caching");
660 if (btrfs_test_opt(info
, FREE_SPACE_TREE
)) {
661 btrfs_clear_and_info(info
, FREE_SPACE_TREE
,
662 "disabling free space tree");
665 case Opt_inode_cache
:
666 case Opt_noinode_cache
:
668 "the 'inode_cache' option is deprecated and has no effect since 5.11");
670 case Opt_clear_cache
:
672 * We cannot clear the free space tree with extent tree
673 * v2, ignore this option.
675 if (btrfs_fs_incompat(info
, EXTENT_TREE_V2
))
677 btrfs_set_and_info(info
, CLEAR_CACHE
,
678 "force clearing of disk cache");
680 case Opt_user_subvol_rm_allowed
:
681 btrfs_set_opt(info
->mount_opt
, USER_SUBVOL_RM_ALLOWED
);
683 case Opt_enospc_debug
:
684 btrfs_set_opt(info
->mount_opt
, ENOSPC_DEBUG
);
686 case Opt_noenospc_debug
:
687 btrfs_clear_opt(info
->mount_opt
, ENOSPC_DEBUG
);
690 btrfs_set_and_info(info
, AUTO_DEFRAG
,
691 "enabling auto defrag");
694 btrfs_clear_and_info(info
, AUTO_DEFRAG
,
695 "disabling auto defrag");
698 case Opt_usebackuproot
:
700 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
701 token
== Opt_recovery
? "recovery" :
704 "trying to use backup root at mount time");
705 btrfs_set_opt(info
->mount_opt
, USEBACKUPROOT
);
707 case Opt_skip_balance
:
708 btrfs_set_opt(info
->mount_opt
, SKIP_BALANCE
);
710 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
711 case Opt_check_integrity_including_extent_data
:
713 "integrity checker is deprecated and will be removed in 6.7");
715 "enabling check integrity including extent data");
716 btrfs_set_opt(info
->mount_opt
, CHECK_INTEGRITY_DATA
);
717 btrfs_set_opt(info
->mount_opt
, CHECK_INTEGRITY
);
719 case Opt_check_integrity
:
721 "integrity checker is deprecated and will be removed in 6.7");
722 btrfs_info(info
, "enabling check integrity");
723 btrfs_set_opt(info
->mount_opt
, CHECK_INTEGRITY
);
725 case Opt_check_integrity_print_mask
:
726 ret
= match_int(&args
[0], &intarg
);
729 "unrecognized check_integrity_print_mask value %s",
733 info
->check_integrity_print_mask
= intarg
;
735 "integrity checker is deprecated and will be removed in 6.7");
736 btrfs_info(info
, "check_integrity_print_mask 0x%x",
737 info
->check_integrity_print_mask
);
740 case Opt_check_integrity_including_extent_data
:
741 case Opt_check_integrity
:
742 case Opt_check_integrity_print_mask
:
744 "support for check_integrity* not compiled in!");
748 case Opt_fatal_errors
:
749 if (strcmp(args
[0].from
, "panic") == 0) {
750 btrfs_set_opt(info
->mount_opt
,
751 PANIC_ON_FATAL_ERROR
);
752 } else if (strcmp(args
[0].from
, "bug") == 0) {
753 btrfs_clear_opt(info
->mount_opt
,
754 PANIC_ON_FATAL_ERROR
);
756 btrfs_err(info
, "unrecognized fatal_errors value %s",
762 case Opt_commit_interval
:
764 ret
= match_int(&args
[0], &intarg
);
766 btrfs_err(info
, "unrecognized commit_interval value %s",
773 "using default commit interval %us",
774 BTRFS_DEFAULT_COMMIT_INTERVAL
);
775 intarg
= BTRFS_DEFAULT_COMMIT_INTERVAL
;
776 } else if (intarg
> 300) {
777 btrfs_warn(info
, "excessive commit interval %d",
780 info
->commit_interval
= intarg
;
783 ret
= parse_rescue_options(info
, args
[0].from
);
785 btrfs_err(info
, "unrecognized rescue value %s",
790 #ifdef CONFIG_BTRFS_DEBUG
791 case Opt_fragment_all
:
792 btrfs_info(info
, "fragmenting all space");
793 btrfs_set_opt(info
->mount_opt
, FRAGMENT_DATA
);
794 btrfs_set_opt(info
->mount_opt
, FRAGMENT_METADATA
);
796 case Opt_fragment_metadata
:
797 btrfs_info(info
, "fragmenting metadata");
798 btrfs_set_opt(info
->mount_opt
,
801 case Opt_fragment_data
:
802 btrfs_info(info
, "fragmenting data");
803 btrfs_set_opt(info
->mount_opt
, FRAGMENT_DATA
);
806 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
808 btrfs_info(info
, "doing ref verification");
809 btrfs_set_opt(info
->mount_opt
, REF_VERIFY
);
813 btrfs_err(info
, "unrecognized mount option '%s'", p
);
821 /* We're read-only, don't have to check. */
822 if (new_flags
& SB_RDONLY
)
825 if (check_ro_option(info
, BTRFS_MOUNT_NOLOGREPLAY
, "nologreplay") ||
826 check_ro_option(info
, BTRFS_MOUNT_IGNOREBADROOTS
, "ignorebadroots") ||
827 check_ro_option(info
, BTRFS_MOUNT_IGNOREDATACSUMS
, "ignoredatacsums"))
830 if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
) &&
831 !btrfs_test_opt(info
, FREE_SPACE_TREE
) &&
832 !btrfs_test_opt(info
, CLEAR_CACHE
)) {
833 btrfs_err(info
, "cannot disable free space tree");
836 if (btrfs_fs_compat_ro(info
, BLOCK_GROUP_TREE
) &&
837 !btrfs_test_opt(info
, FREE_SPACE_TREE
)) {
838 btrfs_err(info
, "cannot disable free space tree with block-group-tree feature");
842 ret
= btrfs_check_mountopts_zoned(info
);
843 if (!ret
&& !remounting
) {
844 if (btrfs_test_opt(info
, SPACE_CACHE
))
845 btrfs_info(info
, "disk space caching is enabled");
846 if (btrfs_test_opt(info
, FREE_SPACE_TREE
))
847 btrfs_info(info
, "using free space tree");
853 * Parse mount options that are required early in the mount process.
855 * All other options will be parsed on much later in the mount process and
856 * only when we need to allocate a new super block.
858 static int btrfs_parse_device_options(const char *options
, blk_mode_t flags
)
860 substring_t args
[MAX_OPT_ARGS
];
861 char *device_name
, *opts
, *orig
, *p
;
862 struct btrfs_device
*device
= NULL
;
865 lockdep_assert_held(&uuid_mutex
);
871 * strsep changes the string, duplicate it because btrfs_parse_options
874 opts
= kstrdup(options
, GFP_KERNEL
);
879 while ((p
= strsep(&opts
, ",")) != NULL
) {
885 token
= match_token(p
, tokens
, args
);
886 if (token
== Opt_device
) {
887 device_name
= match_strdup(&args
[0]);
892 device
= btrfs_scan_one_device(device_name
, flags
);
894 if (IS_ERR(device
)) {
895 error
= PTR_ERR(device
);
907 * Parse mount options that are related to subvolume id
909 * The value is later passed to mount_subvol()
911 static int btrfs_parse_subvol_options(const char *options
, char **subvol_name
,
912 u64
*subvol_objectid
)
914 substring_t args
[MAX_OPT_ARGS
];
915 char *opts
, *orig
, *p
;
923 * strsep changes the string, duplicate it because
924 * btrfs_parse_device_options gets called later
926 opts
= kstrdup(options
, GFP_KERNEL
);
931 while ((p
= strsep(&opts
, ",")) != NULL
) {
936 token
= match_token(p
, tokens
, args
);
940 *subvol_name
= match_strdup(&args
[0]);
947 error
= match_u64(&args
[0], &subvolid
);
951 /* we want the original fs_tree */
953 subvolid
= BTRFS_FS_TREE_OBJECTID
;
955 *subvol_objectid
= subvolid
;
967 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info
*fs_info
,
970 struct btrfs_root
*root
= fs_info
->tree_root
;
971 struct btrfs_root
*fs_root
= NULL
;
972 struct btrfs_root_ref
*root_ref
;
973 struct btrfs_inode_ref
*inode_ref
;
974 struct btrfs_key key
;
975 struct btrfs_path
*path
= NULL
;
976 char *name
= NULL
, *ptr
;
981 path
= btrfs_alloc_path();
987 name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
992 ptr
= name
+ PATH_MAX
- 1;
996 * Walk up the subvolume trees in the tree of tree roots by root
997 * backrefs until we hit the top-level subvolume.
999 while (subvol_objectid
!= BTRFS_FS_TREE_OBJECTID
) {
1000 key
.objectid
= subvol_objectid
;
1001 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
1002 key
.offset
= (u64
)-1;
1004 ret
= btrfs_search_backwards(root
, &key
, path
);
1007 } else if (ret
> 0) {
1012 subvol_objectid
= key
.offset
;
1014 root_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1015 struct btrfs_root_ref
);
1016 len
= btrfs_root_ref_name_len(path
->nodes
[0], root_ref
);
1019 ret
= -ENAMETOOLONG
;
1022 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
1023 (unsigned long)(root_ref
+ 1), len
);
1025 dirid
= btrfs_root_ref_dirid(path
->nodes
[0], root_ref
);
1026 btrfs_release_path(path
);
1028 fs_root
= btrfs_get_fs_root(fs_info
, subvol_objectid
, true);
1029 if (IS_ERR(fs_root
)) {
1030 ret
= PTR_ERR(fs_root
);
1036 * Walk up the filesystem tree by inode refs until we hit the
1039 while (dirid
!= BTRFS_FIRST_FREE_OBJECTID
) {
1040 key
.objectid
= dirid
;
1041 key
.type
= BTRFS_INODE_REF_KEY
;
1042 key
.offset
= (u64
)-1;
1044 ret
= btrfs_search_backwards(fs_root
, &key
, path
);
1047 } else if (ret
> 0) {
1054 inode_ref
= btrfs_item_ptr(path
->nodes
[0],
1056 struct btrfs_inode_ref
);
1057 len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1061 ret
= -ENAMETOOLONG
;
1064 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
1065 (unsigned long)(inode_ref
+ 1), len
);
1067 btrfs_release_path(path
);
1069 btrfs_put_root(fs_root
);
1073 btrfs_free_path(path
);
1074 if (ptr
== name
+ PATH_MAX
- 1) {
1078 memmove(name
, ptr
, name
+ PATH_MAX
- ptr
);
1083 btrfs_put_root(fs_root
);
1084 btrfs_free_path(path
);
1086 return ERR_PTR(ret
);
1089 static int get_default_subvol_objectid(struct btrfs_fs_info
*fs_info
, u64
*objectid
)
1091 struct btrfs_root
*root
= fs_info
->tree_root
;
1092 struct btrfs_dir_item
*di
;
1093 struct btrfs_path
*path
;
1094 struct btrfs_key location
;
1095 struct fscrypt_str name
= FSTR_INIT("default", 7);
1098 path
= btrfs_alloc_path();
1103 * Find the "default" dir item which points to the root item that we
1104 * will mount by default if we haven't been given a specific subvolume
1107 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
1108 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir_id
, &name
, 0);
1110 btrfs_free_path(path
);
1115 * Ok the default dir item isn't there. This is weird since
1116 * it's always been there, but don't freak out, just try and
1117 * mount the top-level subvolume.
1119 btrfs_free_path(path
);
1120 *objectid
= BTRFS_FS_TREE_OBJECTID
;
1124 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
1125 btrfs_free_path(path
);
1126 *objectid
= location
.objectid
;
1130 static int btrfs_fill_super(struct super_block
*sb
,
1131 struct btrfs_fs_devices
*fs_devices
,
1134 struct inode
*inode
;
1135 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1138 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1139 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
1140 sb
->s_op
= &btrfs_super_ops
;
1141 sb
->s_d_op
= &btrfs_dentry_operations
;
1142 sb
->s_export_op
= &btrfs_export_ops
;
1143 #ifdef CONFIG_FS_VERITY
1144 sb
->s_vop
= &btrfs_verityops
;
1146 sb
->s_xattr
= btrfs_xattr_handlers
;
1147 sb
->s_time_gran
= 1;
1148 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1149 sb
->s_flags
|= SB_POSIXACL
;
1151 sb
->s_flags
|= SB_I_VERSION
;
1152 sb
->s_iflags
|= SB_I_CGROUPWB
;
1154 err
= super_setup_bdi(sb
);
1156 btrfs_err(fs_info
, "super_setup_bdi failed");
1160 err
= open_ctree(sb
, fs_devices
, (char *)data
);
1162 btrfs_err(fs_info
, "open_ctree failed");
1166 inode
= btrfs_iget(sb
, BTRFS_FIRST_FREE_OBJECTID
, fs_info
->fs_root
);
1167 if (IS_ERR(inode
)) {
1168 err
= PTR_ERR(inode
);
1169 btrfs_handle_fs_error(fs_info
, err
, NULL
);
1173 sb
->s_root
= d_make_root(inode
);
1179 sb
->s_flags
|= SB_ACTIVE
;
1183 close_ctree(fs_info
);
1187 int btrfs_sync_fs(struct super_block
*sb
, int wait
)
1189 struct btrfs_trans_handle
*trans
;
1190 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1191 struct btrfs_root
*root
= fs_info
->tree_root
;
1193 trace_btrfs_sync_fs(fs_info
, wait
);
1196 filemap_flush(fs_info
->btree_inode
->i_mapping
);
1200 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
1202 trans
= btrfs_attach_transaction_barrier(root
);
1203 if (IS_ERR(trans
)) {
1204 /* no transaction, don't bother */
1205 if (PTR_ERR(trans
) == -ENOENT
) {
1207 * Exit unless we have some pending changes
1208 * that need to go through commit
1210 if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT
,
1214 * A non-blocking test if the fs is frozen. We must not
1215 * start a new transaction here otherwise a deadlock
1216 * happens. The pending operations are delayed to the
1217 * next commit after thawing.
1219 if (sb_start_write_trylock(sb
))
1223 trans
= btrfs_start_transaction(root
, 0);
1226 return PTR_ERR(trans
);
1228 return btrfs_commit_transaction(trans
);
1231 static void print_rescue_option(struct seq_file
*seq
, const char *s
, bool *printed
)
1233 seq_printf(seq
, "%s%s", (*printed
) ? ":" : ",rescue=", s
);
1237 static int btrfs_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1239 struct btrfs_fs_info
*info
= btrfs_sb(dentry
->d_sb
);
1240 const char *compress_type
;
1241 const char *subvol_name
;
1242 bool printed
= false;
1244 if (btrfs_test_opt(info
, DEGRADED
))
1245 seq_puts(seq
, ",degraded");
1246 if (btrfs_test_opt(info
, NODATASUM
))
1247 seq_puts(seq
, ",nodatasum");
1248 if (btrfs_test_opt(info
, NODATACOW
))
1249 seq_puts(seq
, ",nodatacow");
1250 if (btrfs_test_opt(info
, NOBARRIER
))
1251 seq_puts(seq
, ",nobarrier");
1252 if (info
->max_inline
!= BTRFS_DEFAULT_MAX_INLINE
)
1253 seq_printf(seq
, ",max_inline=%llu", info
->max_inline
);
1254 if (info
->thread_pool_size
!= min_t(unsigned long,
1255 num_online_cpus() + 2, 8))
1256 seq_printf(seq
, ",thread_pool=%u", info
->thread_pool_size
);
1257 if (btrfs_test_opt(info
, COMPRESS
)) {
1258 compress_type
= btrfs_compress_type2str(info
->compress_type
);
1259 if (btrfs_test_opt(info
, FORCE_COMPRESS
))
1260 seq_printf(seq
, ",compress-force=%s", compress_type
);
1262 seq_printf(seq
, ",compress=%s", compress_type
);
1263 if (info
->compress_level
)
1264 seq_printf(seq
, ":%d", info
->compress_level
);
1266 if (btrfs_test_opt(info
, NOSSD
))
1267 seq_puts(seq
, ",nossd");
1268 if (btrfs_test_opt(info
, SSD_SPREAD
))
1269 seq_puts(seq
, ",ssd_spread");
1270 else if (btrfs_test_opt(info
, SSD
))
1271 seq_puts(seq
, ",ssd");
1272 if (btrfs_test_opt(info
, NOTREELOG
))
1273 seq_puts(seq
, ",notreelog");
1274 if (btrfs_test_opt(info
, NOLOGREPLAY
))
1275 print_rescue_option(seq
, "nologreplay", &printed
);
1276 if (btrfs_test_opt(info
, USEBACKUPROOT
))
1277 print_rescue_option(seq
, "usebackuproot", &printed
);
1278 if (btrfs_test_opt(info
, IGNOREBADROOTS
))
1279 print_rescue_option(seq
, "ignorebadroots", &printed
);
1280 if (btrfs_test_opt(info
, IGNOREDATACSUMS
))
1281 print_rescue_option(seq
, "ignoredatacsums", &printed
);
1282 if (btrfs_test_opt(info
, FLUSHONCOMMIT
))
1283 seq_puts(seq
, ",flushoncommit");
1284 if (btrfs_test_opt(info
, DISCARD_SYNC
))
1285 seq_puts(seq
, ",discard");
1286 if (btrfs_test_opt(info
, DISCARD_ASYNC
))
1287 seq_puts(seq
, ",discard=async");
1288 if (!(info
->sb
->s_flags
& SB_POSIXACL
))
1289 seq_puts(seq
, ",noacl");
1290 if (btrfs_free_space_cache_v1_active(info
))
1291 seq_puts(seq
, ",space_cache");
1292 else if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
))
1293 seq_puts(seq
, ",space_cache=v2");
1295 seq_puts(seq
, ",nospace_cache");
1296 if (btrfs_test_opt(info
, RESCAN_UUID_TREE
))
1297 seq_puts(seq
, ",rescan_uuid_tree");
1298 if (btrfs_test_opt(info
, CLEAR_CACHE
))
1299 seq_puts(seq
, ",clear_cache");
1300 if (btrfs_test_opt(info
, USER_SUBVOL_RM_ALLOWED
))
1301 seq_puts(seq
, ",user_subvol_rm_allowed");
1302 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
1303 seq_puts(seq
, ",enospc_debug");
1304 if (btrfs_test_opt(info
, AUTO_DEFRAG
))
1305 seq_puts(seq
, ",autodefrag");
1306 if (btrfs_test_opt(info
, SKIP_BALANCE
))
1307 seq_puts(seq
, ",skip_balance");
1308 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1309 if (btrfs_test_opt(info
, CHECK_INTEGRITY_DATA
))
1310 seq_puts(seq
, ",check_int_data");
1311 else if (btrfs_test_opt(info
, CHECK_INTEGRITY
))
1312 seq_puts(seq
, ",check_int");
1313 if (info
->check_integrity_print_mask
)
1314 seq_printf(seq
, ",check_int_print_mask=%d",
1315 info
->check_integrity_print_mask
);
1317 if (info
->metadata_ratio
)
1318 seq_printf(seq
, ",metadata_ratio=%u", info
->metadata_ratio
);
1319 if (btrfs_test_opt(info
, PANIC_ON_FATAL_ERROR
))
1320 seq_puts(seq
, ",fatal_errors=panic");
1321 if (info
->commit_interval
!= BTRFS_DEFAULT_COMMIT_INTERVAL
)
1322 seq_printf(seq
, ",commit=%u", info
->commit_interval
);
1323 #ifdef CONFIG_BTRFS_DEBUG
1324 if (btrfs_test_opt(info
, FRAGMENT_DATA
))
1325 seq_puts(seq
, ",fragment=data");
1326 if (btrfs_test_opt(info
, FRAGMENT_METADATA
))
1327 seq_puts(seq
, ",fragment=metadata");
1329 if (btrfs_test_opt(info
, REF_VERIFY
))
1330 seq_puts(seq
, ",ref_verify");
1331 seq_printf(seq
, ",subvolid=%llu",
1332 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
);
1333 subvol_name
= btrfs_get_subvol_name_from_objectid(info
,
1334 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
);
1335 if (!IS_ERR(subvol_name
)) {
1336 seq_puts(seq
, ",subvol=");
1337 seq_escape(seq
, subvol_name
, " \t\n\\");
1343 static int btrfs_test_super(struct super_block
*s
, void *data
)
1345 struct btrfs_fs_info
*p
= data
;
1346 struct btrfs_fs_info
*fs_info
= btrfs_sb(s
);
1348 return fs_info
->fs_devices
== p
->fs_devices
;
1351 static int btrfs_set_super(struct super_block
*s
, void *data
)
1353 int err
= set_anon_super(s
, data
);
1355 s
->s_fs_info
= data
;
1360 * subvolumes are identified by ino 256
1362 static inline int is_subvolume_inode(struct inode
*inode
)
1364 if (inode
&& inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
1369 static struct dentry
*mount_subvol(const char *subvol_name
, u64 subvol_objectid
,
1370 struct vfsmount
*mnt
)
1372 struct dentry
*root
;
1376 if (!subvol_objectid
) {
1377 ret
= get_default_subvol_objectid(btrfs_sb(mnt
->mnt_sb
),
1380 root
= ERR_PTR(ret
);
1384 subvol_name
= btrfs_get_subvol_name_from_objectid(
1385 btrfs_sb(mnt
->mnt_sb
), subvol_objectid
);
1386 if (IS_ERR(subvol_name
)) {
1387 root
= ERR_CAST(subvol_name
);
1394 root
= mount_subtree(mnt
, subvol_name
);
1395 /* mount_subtree() drops our reference on the vfsmount. */
1398 if (!IS_ERR(root
)) {
1399 struct super_block
*s
= root
->d_sb
;
1400 struct btrfs_fs_info
*fs_info
= btrfs_sb(s
);
1401 struct inode
*root_inode
= d_inode(root
);
1402 u64 root_objectid
= BTRFS_I(root_inode
)->root
->root_key
.objectid
;
1405 if (!is_subvolume_inode(root_inode
)) {
1406 btrfs_err(fs_info
, "'%s' is not a valid subvolume",
1410 if (subvol_objectid
&& root_objectid
!= subvol_objectid
) {
1412 * This will also catch a race condition where a
1413 * subvolume which was passed by ID is renamed and
1414 * another subvolume is renamed over the old location.
1417 "subvol '%s' does not match subvolid %llu",
1418 subvol_name
, subvol_objectid
);
1423 root
= ERR_PTR(ret
);
1424 deactivate_locked_super(s
);
1435 * Find a superblock for the given device / mount point.
1437 * Note: This is based on mount_bdev from fs/super.c with a few additions
1438 * for multiple device setup. Make sure to keep it in sync.
1440 static struct dentry
*btrfs_mount_root(struct file_system_type
*fs_type
,
1441 int flags
, const char *device_name
, void *data
)
1443 struct block_device
*bdev
= NULL
;
1444 struct super_block
*s
;
1445 struct btrfs_device
*device
= NULL
;
1446 struct btrfs_fs_devices
*fs_devices
= NULL
;
1447 struct btrfs_fs_info
*fs_info
= NULL
;
1448 void *new_sec_opts
= NULL
;
1449 blk_mode_t mode
= sb_open_mode(flags
);
1453 error
= security_sb_eat_lsm_opts(data
, &new_sec_opts
);
1455 return ERR_PTR(error
);
1459 * Setup a dummy root and fs_info for test/set super. This is because
1460 * we don't actually fill this stuff out until open_ctree, but we need
1461 * then open_ctree will properly initialize the file system specific
1462 * settings later. btrfs_init_fs_info initializes the static elements
1463 * of the fs_info (locks and such) to make cleanup easier if we find a
1464 * superblock with our given fs_devices later on at sget() time.
1466 fs_info
= kvzalloc(sizeof(struct btrfs_fs_info
), GFP_KERNEL
);
1469 goto error_sec_opts
;
1471 btrfs_init_fs_info(fs_info
);
1473 fs_info
->super_copy
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
1474 fs_info
->super_for_commit
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
1475 if (!fs_info
->super_copy
|| !fs_info
->super_for_commit
) {
1480 mutex_lock(&uuid_mutex
);
1481 error
= btrfs_parse_device_options(data
, mode
);
1483 mutex_unlock(&uuid_mutex
);
1487 device
= btrfs_scan_one_device(device_name
, mode
);
1488 if (IS_ERR(device
)) {
1489 mutex_unlock(&uuid_mutex
);
1490 error
= PTR_ERR(device
);
1494 fs_devices
= device
->fs_devices
;
1495 fs_info
->fs_devices
= fs_devices
;
1497 error
= btrfs_open_devices(fs_devices
, mode
, fs_type
);
1498 mutex_unlock(&uuid_mutex
);
1502 if (!(flags
& SB_RDONLY
) && fs_devices
->rw_devices
== 0) {
1504 goto error_close_devices
;
1507 bdev
= fs_devices
->latest_dev
->bdev
;
1508 s
= sget(fs_type
, btrfs_test_super
, btrfs_set_super
, flags
| SB_NOSEC
,
1512 goto error_close_devices
;
1516 btrfs_close_devices(fs_devices
);
1517 btrfs_free_fs_info(fs_info
);
1518 if ((flags
^ s
->s_flags
) & SB_RDONLY
)
1521 snprintf(s
->s_id
, sizeof(s
->s_id
), "%pg", bdev
);
1522 shrinker_debugfs_rename(&s
->s_shrink
, "sb-%s:%s", fs_type
->name
,
1524 btrfs_sb(s
)->bdev_holder
= fs_type
;
1525 error
= btrfs_fill_super(s
, fs_devices
, data
);
1528 error
= security_sb_set_mnt_opts(s
, new_sec_opts
, 0, NULL
);
1529 security_free_mnt_opts(&new_sec_opts
);
1531 deactivate_locked_super(s
);
1532 return ERR_PTR(error
);
1535 return dget(s
->s_root
);
1537 error_close_devices
:
1538 btrfs_close_devices(fs_devices
);
1540 btrfs_free_fs_info(fs_info
);
1542 security_free_mnt_opts(&new_sec_opts
);
1543 return ERR_PTR(error
);
1547 * Mount function which is called by VFS layer.
1549 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1550 * which needs vfsmount* of device's root (/). This means device's root has to
1551 * be mounted internally in any case.
1554 * 1. Parse subvol id related options for later use in mount_subvol().
1556 * 2. Mount device's root (/) by calling vfs_kern_mount().
1558 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1559 * first place. In order to avoid calling btrfs_mount() again, we use
1560 * different file_system_type which is not registered to VFS by
1561 * register_filesystem() (btrfs_root_fs_type). As a result,
1562 * btrfs_mount_root() is called. The return value will be used by
1563 * mount_subtree() in mount_subvol().
1565 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1566 * "btrfs subvolume set-default", mount_subvol() is called always.
1568 static struct dentry
*btrfs_mount(struct file_system_type
*fs_type
, int flags
,
1569 const char *device_name
, void *data
)
1571 struct vfsmount
*mnt_root
;
1572 struct dentry
*root
;
1573 char *subvol_name
= NULL
;
1574 u64 subvol_objectid
= 0;
1577 error
= btrfs_parse_subvol_options(data
, &subvol_name
,
1581 return ERR_PTR(error
);
1584 /* mount device's root (/) */
1585 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
, flags
, device_name
, data
);
1586 if (PTR_ERR_OR_ZERO(mnt_root
) == -EBUSY
) {
1587 if (flags
& SB_RDONLY
) {
1588 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
,
1589 flags
& ~SB_RDONLY
, device_name
, data
);
1591 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
,
1592 flags
| SB_RDONLY
, device_name
, data
);
1593 if (IS_ERR(mnt_root
)) {
1594 root
= ERR_CAST(mnt_root
);
1599 down_write(&mnt_root
->mnt_sb
->s_umount
);
1600 error
= btrfs_remount(mnt_root
->mnt_sb
, &flags
, NULL
);
1601 up_write(&mnt_root
->mnt_sb
->s_umount
);
1603 root
= ERR_PTR(error
);
1610 if (IS_ERR(mnt_root
)) {
1611 root
= ERR_CAST(mnt_root
);
1616 /* mount_subvol() will free subvol_name and mnt_root */
1617 root
= mount_subvol(subvol_name
, subvol_objectid
, mnt_root
);
1623 static void btrfs_resize_thread_pool(struct btrfs_fs_info
*fs_info
,
1624 u32 new_pool_size
, u32 old_pool_size
)
1626 if (new_pool_size
== old_pool_size
)
1629 fs_info
->thread_pool_size
= new_pool_size
;
1631 btrfs_info(fs_info
, "resize thread pool %d -> %d",
1632 old_pool_size
, new_pool_size
);
1634 btrfs_workqueue_set_max(fs_info
->workers
, new_pool_size
);
1635 btrfs_workqueue_set_max(fs_info
->delalloc_workers
, new_pool_size
);
1636 btrfs_workqueue_set_max(fs_info
->caching_workers
, new_pool_size
);
1637 workqueue_set_max_active(fs_info
->endio_workers
, new_pool_size
);
1638 workqueue_set_max_active(fs_info
->endio_meta_workers
, new_pool_size
);
1639 btrfs_workqueue_set_max(fs_info
->endio_write_workers
, new_pool_size
);
1640 btrfs_workqueue_set_max(fs_info
->endio_freespace_worker
, new_pool_size
);
1641 btrfs_workqueue_set_max(fs_info
->delayed_workers
, new_pool_size
);
1644 static inline void btrfs_remount_begin(struct btrfs_fs_info
*fs_info
,
1645 unsigned long old_opts
, int flags
)
1647 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1648 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) ||
1649 (flags
& SB_RDONLY
))) {
1650 /* wait for any defraggers to finish */
1651 wait_event(fs_info
->transaction_wait
,
1652 (atomic_read(&fs_info
->defrag_running
) == 0));
1653 if (flags
& SB_RDONLY
)
1654 sync_filesystem(fs_info
->sb
);
1658 static inline void btrfs_remount_cleanup(struct btrfs_fs_info
*fs_info
,
1659 unsigned long old_opts
)
1661 const bool cache_opt
= btrfs_test_opt(fs_info
, SPACE_CACHE
);
1664 * We need to cleanup all defragable inodes if the autodefragment is
1665 * close or the filesystem is read only.
1667 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1668 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) || sb_rdonly(fs_info
->sb
))) {
1669 btrfs_cleanup_defrag_inodes(fs_info
);
1672 /* If we toggled discard async */
1673 if (!btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1674 btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1675 btrfs_discard_resume(fs_info
);
1676 else if (btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1677 !btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1678 btrfs_discard_cleanup(fs_info
);
1680 /* If we toggled space cache */
1681 if (cache_opt
!= btrfs_free_space_cache_v1_active(fs_info
))
1682 btrfs_set_free_space_cache_v1_active(fs_info
, cache_opt
);
1685 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1687 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1688 unsigned old_flags
= sb
->s_flags
;
1689 unsigned long old_opts
= fs_info
->mount_opt
;
1690 unsigned long old_compress_type
= fs_info
->compress_type
;
1691 u64 old_max_inline
= fs_info
->max_inline
;
1692 u32 old_thread_pool_size
= fs_info
->thread_pool_size
;
1693 u32 old_metadata_ratio
= fs_info
->metadata_ratio
;
1696 sync_filesystem(sb
);
1697 set_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1700 void *new_sec_opts
= NULL
;
1702 ret
= security_sb_eat_lsm_opts(data
, &new_sec_opts
);
1704 ret
= security_sb_remount(sb
, new_sec_opts
);
1705 security_free_mnt_opts(&new_sec_opts
);
1710 ret
= btrfs_parse_options(fs_info
, data
, *flags
);
1714 ret
= btrfs_check_features(fs_info
, !(*flags
& SB_RDONLY
));
1718 btrfs_remount_begin(fs_info
, old_opts
, *flags
);
1719 btrfs_resize_thread_pool(fs_info
,
1720 fs_info
->thread_pool_size
, old_thread_pool_size
);
1722 if ((bool)btrfs_test_opt(fs_info
, FREE_SPACE_TREE
) !=
1723 (bool)btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
) &&
1724 (!sb_rdonly(sb
) || (*flags
& SB_RDONLY
))) {
1726 "remount supports changing free space tree only from ro to rw");
1727 /* Make sure free space cache options match the state on disk */
1728 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
)) {
1729 btrfs_set_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1730 btrfs_clear_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1732 if (btrfs_free_space_cache_v1_active(fs_info
)) {
1733 btrfs_clear_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1734 btrfs_set_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1738 if ((bool)(*flags
& SB_RDONLY
) == sb_rdonly(sb
))
1741 if (*flags
& SB_RDONLY
) {
1743 * this also happens on 'umount -rf' or on shutdown, when
1744 * the filesystem is busy.
1746 cancel_work_sync(&fs_info
->async_reclaim_work
);
1747 cancel_work_sync(&fs_info
->async_data_reclaim_work
);
1749 btrfs_discard_cleanup(fs_info
);
1751 /* wait for the uuid_scan task to finish */
1752 down(&fs_info
->uuid_tree_rescan_sem
);
1753 /* avoid complains from lockdep et al. */
1754 up(&fs_info
->uuid_tree_rescan_sem
);
1756 btrfs_set_sb_rdonly(sb
);
1759 * Setting SB_RDONLY will put the cleaner thread to
1760 * sleep at the next loop if it's already active.
1761 * If it's already asleep, we'll leave unused block
1762 * groups on disk until we're mounted read-write again
1763 * unless we clean them up here.
1765 btrfs_delete_unused_bgs(fs_info
);
1768 * The cleaner task could be already running before we set the
1769 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1770 * We must make sure that after we finish the remount, i.e. after
1771 * we call btrfs_commit_super(), the cleaner can no longer start
1772 * a transaction - either because it was dropping a dead root,
1773 * running delayed iputs or deleting an unused block group (the
1774 * cleaner picked a block group from the list of unused block
1775 * groups before we were able to in the previous call to
1776 * btrfs_delete_unused_bgs()).
1778 wait_on_bit(&fs_info
->flags
, BTRFS_FS_CLEANER_RUNNING
,
1779 TASK_UNINTERRUPTIBLE
);
1782 * We've set the superblock to RO mode, so we might have made
1783 * the cleaner task sleep without running all pending delayed
1784 * iputs. Go through all the delayed iputs here, so that if an
1785 * unmount happens without remounting RW we don't end up at
1786 * finishing close_ctree() with a non-empty list of delayed
1789 btrfs_run_delayed_iputs(fs_info
);
1791 btrfs_dev_replace_suspend_for_unmount(fs_info
);
1792 btrfs_scrub_cancel(fs_info
);
1793 btrfs_pause_balance(fs_info
);
1796 * Pause the qgroup rescan worker if it is running. We don't want
1797 * it to be still running after we are in RO mode, as after that,
1798 * by the time we unmount, it might have left a transaction open,
1799 * so we would leak the transaction and/or crash.
1801 btrfs_qgroup_wait_for_completion(fs_info
, false);
1803 ret
= btrfs_commit_super(fs_info
);
1807 if (BTRFS_FS_ERROR(fs_info
)) {
1809 "Remounting read-write after error is not allowed");
1813 if (fs_info
->fs_devices
->rw_devices
== 0) {
1818 if (!btrfs_check_rw_degradable(fs_info
, NULL
)) {
1820 "too many missing devices, writable remount is not allowed");
1825 if (btrfs_super_log_root(fs_info
->super_copy
) != 0) {
1827 "mount required to replay tree-log, cannot remount read-write");
1833 * NOTE: when remounting with a change that does writes, don't
1834 * put it anywhere above this point, as we are not sure to be
1835 * safe to write until we pass the above checks.
1837 ret
= btrfs_start_pre_rw_mount(fs_info
);
1841 btrfs_clear_sb_rdonly(sb
);
1843 set_bit(BTRFS_FS_OPEN
, &fs_info
->flags
);
1846 * If we've gone from readonly -> read/write, we need to get
1847 * our sync/async discard lists in the right state.
1849 btrfs_discard_resume(fs_info
);
1853 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1854 * since the absence of the flag means it can be toggled off by remount.
1856 *flags
|= SB_I_VERSION
;
1858 wake_up_process(fs_info
->transaction_kthread
);
1859 btrfs_remount_cleanup(fs_info
, old_opts
);
1860 btrfs_clear_oneshot_options(fs_info
);
1861 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1866 /* We've hit an error - don't reset SB_RDONLY */
1868 old_flags
|= SB_RDONLY
;
1869 if (!(old_flags
& SB_RDONLY
))
1870 clear_bit(BTRFS_FS_STATE_RO
, &fs_info
->fs_state
);
1871 sb
->s_flags
= old_flags
;
1872 fs_info
->mount_opt
= old_opts
;
1873 fs_info
->compress_type
= old_compress_type
;
1874 fs_info
->max_inline
= old_max_inline
;
1875 btrfs_resize_thread_pool(fs_info
,
1876 old_thread_pool_size
, fs_info
->thread_pool_size
);
1877 fs_info
->metadata_ratio
= old_metadata_ratio
;
1878 btrfs_remount_cleanup(fs_info
, old_opts
);
1879 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1884 /* Used to sort the devices by max_avail(descending sort) */
1885 static int btrfs_cmp_device_free_bytes(const void *a
, const void *b
)
1887 const struct btrfs_device_info
*dev_info1
= a
;
1888 const struct btrfs_device_info
*dev_info2
= b
;
1890 if (dev_info1
->max_avail
> dev_info2
->max_avail
)
1892 else if (dev_info1
->max_avail
< dev_info2
->max_avail
)
1898 * sort the devices by max_avail, in which max free extent size of each device
1899 * is stored.(Descending Sort)
1901 static inline void btrfs_descending_sort_devices(
1902 struct btrfs_device_info
*devices
,
1905 sort(devices
, nr_devices
, sizeof(struct btrfs_device_info
),
1906 btrfs_cmp_device_free_bytes
, NULL
);
1910 * The helper to calc the free space on the devices that can be used to store
1913 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info
*fs_info
,
1916 struct btrfs_device_info
*devices_info
;
1917 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
1918 struct btrfs_device
*device
;
1921 u64 min_stripe_size
;
1922 int num_stripes
= 1;
1923 int i
= 0, nr_devices
;
1924 const struct btrfs_raid_attr
*rattr
;
1927 * We aren't under the device list lock, so this is racy-ish, but good
1928 * enough for our purposes.
1930 nr_devices
= fs_info
->fs_devices
->open_devices
;
1933 nr_devices
= fs_info
->fs_devices
->open_devices
;
1941 devices_info
= kmalloc_array(nr_devices
, sizeof(*devices_info
),
1946 /* calc min stripe number for data space allocation */
1947 type
= btrfs_data_alloc_profile(fs_info
);
1948 rattr
= &btrfs_raid_array
[btrfs_bg_flags_to_raid_index(type
)];
1950 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
1951 num_stripes
= nr_devices
;
1952 else if (type
& BTRFS_BLOCK_GROUP_RAID1_MASK
)
1953 num_stripes
= rattr
->ncopies
;
1954 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1957 /* Adjust for more than 1 stripe per device */
1958 min_stripe_size
= rattr
->dev_stripes
* BTRFS_STRIPE_LEN
;
1961 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
1962 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
1963 &device
->dev_state
) ||
1965 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
1968 if (i
>= nr_devices
)
1971 avail_space
= device
->total_bytes
- device
->bytes_used
;
1973 /* align with stripe_len */
1974 avail_space
= rounddown(avail_space
, BTRFS_STRIPE_LEN
);
1977 * Ensure we have at least min_stripe_size on top of the
1978 * reserved space on the device.
1980 if (avail_space
<= BTRFS_DEVICE_RANGE_RESERVED
+ min_stripe_size
)
1983 avail_space
-= BTRFS_DEVICE_RANGE_RESERVED
;
1985 devices_info
[i
].dev
= device
;
1986 devices_info
[i
].max_avail
= avail_space
;
1994 btrfs_descending_sort_devices(devices_info
, nr_devices
);
1998 while (nr_devices
>= rattr
->devs_min
) {
1999 num_stripes
= min(num_stripes
, nr_devices
);
2001 if (devices_info
[i
].max_avail
>= min_stripe_size
) {
2005 avail_space
+= devices_info
[i
].max_avail
* num_stripes
;
2006 alloc_size
= devices_info
[i
].max_avail
;
2007 for (j
= i
+ 1 - num_stripes
; j
<= i
; j
++)
2008 devices_info
[j
].max_avail
-= alloc_size
;
2014 kfree(devices_info
);
2015 *free_bytes
= avail_space
;
2020 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2022 * If there's a redundant raid level at DATA block groups, use the respective
2023 * multiplier to scale the sizes.
2025 * Unused device space usage is based on simulating the chunk allocator
2026 * algorithm that respects the device sizes and order of allocations. This is
2027 * a close approximation of the actual use but there are other factors that may
2028 * change the result (like a new metadata chunk).
2030 * If metadata is exhausted, f_bavail will be 0.
2032 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2034 struct btrfs_fs_info
*fs_info
= btrfs_sb(dentry
->d_sb
);
2035 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2036 struct btrfs_space_info
*found
;
2038 u64 total_free_data
= 0;
2039 u64 total_free_meta
= 0;
2040 u32 bits
= fs_info
->sectorsize_bits
;
2041 __be32
*fsid
= (__be32
*)fs_info
->fs_devices
->fsid
;
2042 unsigned factor
= 1;
2043 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
2048 list_for_each_entry(found
, &fs_info
->space_info
, list
) {
2049 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2052 total_free_data
+= found
->disk_total
- found
->disk_used
;
2054 btrfs_account_ro_block_groups_free_space(found
);
2056 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
2057 if (!list_empty(&found
->block_groups
[i
]))
2058 factor
= btrfs_bg_type_to_factor(
2059 btrfs_raid_array
[i
].bg_flag
);
2064 * Metadata in mixed block group profiles are accounted in data
2066 if (!mixed
&& found
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2067 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
)
2070 total_free_meta
+= found
->disk_total
-
2074 total_used
+= found
->disk_used
;
2077 buf
->f_blocks
= div_u64(btrfs_super_total_bytes(disk_super
), factor
);
2078 buf
->f_blocks
>>= bits
;
2079 buf
->f_bfree
= buf
->f_blocks
- (div_u64(total_used
, factor
) >> bits
);
2081 /* Account global block reserve as used, it's in logical size already */
2082 spin_lock(&block_rsv
->lock
);
2083 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2084 if (buf
->f_bfree
>= block_rsv
->size
>> bits
)
2085 buf
->f_bfree
-= block_rsv
->size
>> bits
;
2088 spin_unlock(&block_rsv
->lock
);
2090 buf
->f_bavail
= div_u64(total_free_data
, factor
);
2091 ret
= btrfs_calc_avail_data_space(fs_info
, &total_free_data
);
2094 buf
->f_bavail
+= div_u64(total_free_data
, factor
);
2095 buf
->f_bavail
= buf
->f_bavail
>> bits
;
2098 * We calculate the remaining metadata space minus global reserve. If
2099 * this is (supposedly) smaller than zero, there's no space. But this
2100 * does not hold in practice, the exhausted state happens where's still
2101 * some positive delta. So we apply some guesswork and compare the
2102 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2104 * We probably cannot calculate the exact threshold value because this
2105 * depends on the internal reservations requested by various
2106 * operations, so some operations that consume a few metadata will
2107 * succeed even if the Avail is zero. But this is better than the other
2113 * We only want to claim there's no available space if we can no longer
2114 * allocate chunks for our metadata profile and our global reserve will
2115 * not fit in the free metadata space. If we aren't ->full then we
2116 * still can allocate chunks and thus are fine using the currently
2117 * calculated f_bavail.
2119 if (!mixed
&& block_rsv
->space_info
->full
&&
2120 total_free_meta
- thresh
< block_rsv
->size
)
2123 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2124 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2125 buf
->f_namelen
= BTRFS_NAME_LEN
;
2127 /* We treat it as constant endianness (it doesn't matter _which_)
2128 because we want the fsid to come out the same whether mounted
2129 on a big-endian or little-endian host */
2130 buf
->f_fsid
.val
[0] = be32_to_cpu(fsid
[0]) ^ be32_to_cpu(fsid
[2]);
2131 buf
->f_fsid
.val
[1] = be32_to_cpu(fsid
[1]) ^ be32_to_cpu(fsid
[3]);
2132 /* Mask in the root object ID too, to disambiguate subvols */
2133 buf
->f_fsid
.val
[0] ^=
2134 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
>> 32;
2135 buf
->f_fsid
.val
[1] ^=
2136 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
;
2141 static void btrfs_kill_super(struct super_block
*sb
)
2143 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2144 kill_anon_super(sb
);
2145 btrfs_free_fs_info(fs_info
);
2148 static struct file_system_type btrfs_fs_type
= {
2149 .owner
= THIS_MODULE
,
2151 .mount
= btrfs_mount
,
2152 .kill_sb
= btrfs_kill_super
,
2153 .fs_flags
= FS_REQUIRES_DEV
| FS_BINARY_MOUNTDATA
| FS_MGTIME
,
2156 static struct file_system_type btrfs_root_fs_type
= {
2157 .owner
= THIS_MODULE
,
2159 .mount
= btrfs_mount_root
,
2160 .kill_sb
= btrfs_kill_super
,
2161 .fs_flags
= FS_REQUIRES_DEV
| FS_BINARY_MOUNTDATA
|
2162 FS_ALLOW_IDMAP
| FS_MGTIME
,
2165 MODULE_ALIAS_FS("btrfs");
2167 static int btrfs_control_open(struct inode
*inode
, struct file
*file
)
2170 * The control file's private_data is used to hold the
2171 * transaction when it is started and is used to keep
2172 * track of whether a transaction is already in progress.
2174 file
->private_data
= NULL
;
2179 * Used by /dev/btrfs-control for devices ioctls.
2181 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
2184 struct btrfs_ioctl_vol_args
*vol
;
2185 struct btrfs_device
*device
= NULL
;
2189 if (!capable(CAP_SYS_ADMIN
))
2192 vol
= memdup_user((void __user
*)arg
, sizeof(*vol
));
2194 return PTR_ERR(vol
);
2195 vol
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2198 case BTRFS_IOC_SCAN_DEV
:
2199 mutex_lock(&uuid_mutex
);
2200 device
= btrfs_scan_one_device(vol
->name
, BLK_OPEN_READ
);
2201 ret
= PTR_ERR_OR_ZERO(device
);
2202 mutex_unlock(&uuid_mutex
);
2204 case BTRFS_IOC_FORGET_DEV
:
2205 if (vol
->name
[0] != 0) {
2206 ret
= lookup_bdev(vol
->name
, &devt
);
2210 ret
= btrfs_forget_devices(devt
);
2212 case BTRFS_IOC_DEVICES_READY
:
2213 mutex_lock(&uuid_mutex
);
2214 device
= btrfs_scan_one_device(vol
->name
, BLK_OPEN_READ
);
2215 if (IS_ERR(device
)) {
2216 mutex_unlock(&uuid_mutex
);
2217 ret
= PTR_ERR(device
);
2220 ret
= !(device
->fs_devices
->num_devices
==
2221 device
->fs_devices
->total_devices
);
2222 mutex_unlock(&uuid_mutex
);
2224 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
2225 ret
= btrfs_ioctl_get_supported_features((void __user
*)arg
);
2233 static int btrfs_freeze(struct super_block
*sb
)
2235 struct btrfs_trans_handle
*trans
;
2236 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2237 struct btrfs_root
*root
= fs_info
->tree_root
;
2239 set_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2241 * We don't need a barrier here, we'll wait for any transaction that
2242 * could be in progress on other threads (and do delayed iputs that
2243 * we want to avoid on a frozen filesystem), or do the commit
2246 trans
= btrfs_attach_transaction_barrier(root
);
2247 if (IS_ERR(trans
)) {
2248 /* no transaction, don't bother */
2249 if (PTR_ERR(trans
) == -ENOENT
)
2251 return PTR_ERR(trans
);
2253 return btrfs_commit_transaction(trans
);
2256 static int check_dev_super(struct btrfs_device
*dev
)
2258 struct btrfs_fs_info
*fs_info
= dev
->fs_info
;
2259 struct btrfs_super_block
*sb
;
2263 /* This should be called with fs still frozen. */
2264 ASSERT(test_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
));
2266 /* Missing dev, no need to check. */
2270 /* Only need to check the primary super block. */
2271 sb
= btrfs_read_dev_one_super(dev
->bdev
, 0, true);
2275 /* Verify the checksum. */
2276 csum_type
= btrfs_super_csum_type(sb
);
2277 if (csum_type
!= btrfs_super_csum_type(fs_info
->super_copy
)) {
2278 btrfs_err(fs_info
, "csum type changed, has %u expect %u",
2279 csum_type
, btrfs_super_csum_type(fs_info
->super_copy
));
2284 if (btrfs_check_super_csum(fs_info
, sb
)) {
2285 btrfs_err(fs_info
, "csum for on-disk super block no longer matches");
2290 /* Btrfs_validate_super() includes fsid check against super->fsid. */
2291 ret
= btrfs_validate_super(fs_info
, sb
, 0);
2295 if (btrfs_super_generation(sb
) != fs_info
->last_trans_committed
) {
2296 btrfs_err(fs_info
, "transid mismatch, has %llu expect %llu",
2297 btrfs_super_generation(sb
),
2298 fs_info
->last_trans_committed
);
2303 btrfs_release_disk_super(sb
);
2307 static int btrfs_unfreeze(struct super_block
*sb
)
2309 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2310 struct btrfs_device
*device
;
2314 * Make sure the fs is not changed by accident (like hibernation then
2315 * modified by other OS).
2316 * If we found anything wrong, we mark the fs error immediately.
2318 * And since the fs is frozen, no one can modify the fs yet, thus
2319 * we don't need to hold device_list_mutex.
2321 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
, dev_list
) {
2322 ret
= check_dev_super(device
);
2324 btrfs_handle_fs_error(fs_info
, ret
,
2325 "super block on devid %llu got modified unexpectedly",
2330 clear_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2333 * We still return 0, to allow VFS layer to unfreeze the fs even the
2334 * above checks failed. Since the fs is either fine or read-only, we're
2335 * safe to continue, without causing further damage.
2340 static int btrfs_show_devname(struct seq_file
*m
, struct dentry
*root
)
2342 struct btrfs_fs_info
*fs_info
= btrfs_sb(root
->d_sb
);
2345 * There should be always a valid pointer in latest_dev, it may be stale
2346 * for a short moment in case it's being deleted but still valid until
2347 * the end of RCU grace period.
2350 seq_escape(m
, btrfs_dev_name(fs_info
->fs_devices
->latest_dev
), " \t\n\\");
2356 static const struct super_operations btrfs_super_ops
= {
2357 .drop_inode
= btrfs_drop_inode
,
2358 .evict_inode
= btrfs_evict_inode
,
2359 .put_super
= btrfs_put_super
,
2360 .sync_fs
= btrfs_sync_fs
,
2361 .show_options
= btrfs_show_options
,
2362 .show_devname
= btrfs_show_devname
,
2363 .alloc_inode
= btrfs_alloc_inode
,
2364 .destroy_inode
= btrfs_destroy_inode
,
2365 .free_inode
= btrfs_free_inode
,
2366 .statfs
= btrfs_statfs
,
2367 .remount_fs
= btrfs_remount
,
2368 .freeze_fs
= btrfs_freeze
,
2369 .unfreeze_fs
= btrfs_unfreeze
,
2372 static const struct file_operations btrfs_ctl_fops
= {
2373 .open
= btrfs_control_open
,
2374 .unlocked_ioctl
= btrfs_control_ioctl
,
2375 .compat_ioctl
= compat_ptr_ioctl
,
2376 .owner
= THIS_MODULE
,
2377 .llseek
= noop_llseek
,
2380 static struct miscdevice btrfs_misc
= {
2381 .minor
= BTRFS_MINOR
,
2382 .name
= "btrfs-control",
2383 .fops
= &btrfs_ctl_fops
2386 MODULE_ALIAS_MISCDEV(BTRFS_MINOR
);
2387 MODULE_ALIAS("devname:btrfs-control");
2389 static int __init
btrfs_interface_init(void)
2391 return misc_register(&btrfs_misc
);
2394 static __cold
void btrfs_interface_exit(void)
2396 misc_deregister(&btrfs_misc
);
2399 static int __init
btrfs_print_mod_info(void)
2401 static const char options
[] = ""
2402 #ifdef CONFIG_BTRFS_DEBUG
2405 #ifdef CONFIG_BTRFS_ASSERT
2408 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2409 ", integrity-checker=on"
2411 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2414 #ifdef CONFIG_BLK_DEV_ZONED
2419 #ifdef CONFIG_FS_VERITY
2425 pr_info("Btrfs loaded%s\n", options
);
2429 static int register_btrfs(void)
2431 return register_filesystem(&btrfs_fs_type
);
2434 static void unregister_btrfs(void)
2436 unregister_filesystem(&btrfs_fs_type
);
2439 /* Helper structure for long init/exit functions. */
2440 struct init_sequence
{
2441 int (*init_func
)(void);
2442 /* Can be NULL if the init_func doesn't need cleanup. */
2443 void (*exit_func
)(void);
2446 static const struct init_sequence mod_init_seq
[] = {
2448 .init_func
= btrfs_props_init
,
2451 .init_func
= btrfs_init_sysfs
,
2452 .exit_func
= btrfs_exit_sysfs
,
2454 .init_func
= btrfs_init_compress
,
2455 .exit_func
= btrfs_exit_compress
,
2457 .init_func
= btrfs_init_cachep
,
2458 .exit_func
= btrfs_destroy_cachep
,
2460 .init_func
= btrfs_transaction_init
,
2461 .exit_func
= btrfs_transaction_exit
,
2463 .init_func
= btrfs_ctree_init
,
2464 .exit_func
= btrfs_ctree_exit
,
2466 .init_func
= btrfs_free_space_init
,
2467 .exit_func
= btrfs_free_space_exit
,
2469 .init_func
= extent_state_init_cachep
,
2470 .exit_func
= extent_state_free_cachep
,
2472 .init_func
= extent_buffer_init_cachep
,
2473 .exit_func
= extent_buffer_free_cachep
,
2475 .init_func
= btrfs_bioset_init
,
2476 .exit_func
= btrfs_bioset_exit
,
2478 .init_func
= extent_map_init
,
2479 .exit_func
= extent_map_exit
,
2481 .init_func
= ordered_data_init
,
2482 .exit_func
= ordered_data_exit
,
2484 .init_func
= btrfs_delayed_inode_init
,
2485 .exit_func
= btrfs_delayed_inode_exit
,
2487 .init_func
= btrfs_auto_defrag_init
,
2488 .exit_func
= btrfs_auto_defrag_exit
,
2490 .init_func
= btrfs_delayed_ref_init
,
2491 .exit_func
= btrfs_delayed_ref_exit
,
2493 .init_func
= btrfs_prelim_ref_init
,
2494 .exit_func
= btrfs_prelim_ref_exit
,
2496 .init_func
= btrfs_interface_init
,
2497 .exit_func
= btrfs_interface_exit
,
2499 .init_func
= btrfs_print_mod_info
,
2502 .init_func
= btrfs_run_sanity_tests
,
2505 .init_func
= register_btrfs
,
2506 .exit_func
= unregister_btrfs
,
2510 static bool mod_init_result
[ARRAY_SIZE(mod_init_seq
)];
2512 static __always_inline
void btrfs_exit_btrfs_fs(void)
2516 for (i
= ARRAY_SIZE(mod_init_seq
) - 1; i
>= 0; i
--) {
2517 if (!mod_init_result
[i
])
2519 if (mod_init_seq
[i
].exit_func
)
2520 mod_init_seq
[i
].exit_func();
2521 mod_init_result
[i
] = false;
2525 static void __exit
exit_btrfs_fs(void)
2527 btrfs_exit_btrfs_fs();
2528 btrfs_cleanup_fs_uuids();
2531 static int __init
init_btrfs_fs(void)
2536 for (i
= 0; i
< ARRAY_SIZE(mod_init_seq
); i
++) {
2537 ASSERT(!mod_init_result
[i
]);
2538 ret
= mod_init_seq
[i
].init_func();
2540 btrfs_exit_btrfs_fs();
2543 mod_init_result
[i
] = true;
2548 late_initcall(init_btrfs_fs
);
2549 module_exit(exit_btrfs_fs
)
2551 MODULE_LICENSE("GPL");
2552 MODULE_SOFTDEP("pre: crc32c");
2553 MODULE_SOFTDEP("pre: xxhash64");
2554 MODULE_SOFTDEP("pre: sha256");
2555 MODULE_SOFTDEP("pre: blake2b-256");