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 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
84 btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
94 Opt_compress_force_type,
99 Opt_flushoncommit, Opt_noflushoncommit,
101 Opt_barrier, Opt_nobarrier,
102 Opt_datacow, Opt_nodatacow,
103 Opt_datasum, Opt_nodatasum,
104 Opt_defrag, Opt_nodefrag,
105 Opt_discard, Opt_nodiscard,
109 Opt_rescan_uuid_tree,
111 Opt_space_cache, Opt_no_space_cache,
112 Opt_space_cache_version,
114 Opt_ssd_spread, Opt_nossd_spread,
119 Opt_treelog, Opt_notreelog,
120 Opt_user_subvol_rm_allowed,
130 /* Deprecated options */
132 Opt_inode_cache, Opt_noinode_cache,
134 /* Debugging options */
136 Opt_check_integrity_including_extent_data,
137 Opt_check_integrity_print_mask,
138 Opt_enospc_debug, Opt_noenospc_debug,
139 #ifdef CONFIG_BTRFS_DEBUG
140 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
142 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
148 static const match_table_t tokens = {
150 {Opt_noacl, "noacl"},
151 {Opt_clear_cache, "clear_cache"},
152 {Opt_commit_interval, "commit=%u"},
153 {Opt_compress, "compress"},
154 {Opt_compress_type, "compress=%s"},
155 {Opt_compress_force, "compress-force"},
156 {Opt_compress_force_type, "compress-force=%s"},
157 {Opt_degraded, "degraded"},
158 {Opt_device, "device=%s"},
159 {Opt_fatal_errors, "fatal_errors=%s"},
160 {Opt_flushoncommit, "flushoncommit"},
161 {Opt_noflushoncommit, "noflushoncommit"},
162 {Opt_inode_cache, "inode_cache"},
163 {Opt_noinode_cache, "noinode_cache"},
164 {Opt_max_inline, "max_inline=%s"},
165 {Opt_barrier, "barrier"},
166 {Opt_nobarrier, "nobarrier"},
167 {Opt_datacow, "datacow"},
168 {Opt_nodatacow, "nodatacow"},
169 {Opt_datasum, "datasum"},
170 {Opt_nodatasum, "nodatasum"},
171 {Opt_defrag, "autodefrag"},
172 {Opt_nodefrag, "noautodefrag"},
173 {Opt_discard, "discard"},
174 {Opt_discard_mode, "discard=%s"},
175 {Opt_nodiscard, "nodiscard"},
176 {Opt_norecovery, "norecovery"},
177 {Opt_ratio, "metadata_ratio=%u"},
178 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
179 {Opt_skip_balance, "skip_balance"},
180 {Opt_space_cache, "space_cache"},
181 {Opt_no_space_cache, "nospace_cache"},
182 {Opt_space_cache_version, "space_cache=%s"},
184 {Opt_nossd, "nossd"},
185 {Opt_ssd_spread, "ssd_spread"},
186 {Opt_nossd_spread, "nossd_spread"},
187 {Opt_subvol, "subvol=%s"},
188 {Opt_subvol_empty, "subvol="},
189 {Opt_subvolid, "subvolid=%s"},
190 {Opt_thread_pool, "thread_pool=%u"},
191 {Opt_treelog, "treelog"},
192 {Opt_notreelog, "notreelog"},
193 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
196 {Opt_rescue, "rescue=%s"},
197 /* Deprecated, with alias rescue=nologreplay */
198 {Opt_nologreplay, "nologreplay"},
199 /* Deprecated, with alias rescue=usebackuproot */
200 {Opt_usebackuproot, "usebackuproot"},
202 /* Deprecated options */
203 {Opt_recovery, "recovery"},
205 /* Debugging options */
206 {Opt_check_integrity, "check_int"},
207 {Opt_check_integrity_including_extent_data, "check_int_data"},
208 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
209 {Opt_enospc_debug, "enospc_debug"},
210 {Opt_noenospc_debug, "noenospc_debug"},
211 #ifdef CONFIG_BTRFS_DEBUG
212 {Opt_fragment_data, "fragment=data"},
213 {Opt_fragment_metadata, "fragment=metadata"},
214 {Opt_fragment_all, "fragment=all"},
216 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
217 {Opt_ref_verify, "ref_verify"},
222 static const match_table_t rescue_tokens = {
223 {Opt_usebackuproot, "usebackuproot"},
224 {Opt_nologreplay, "nologreplay"},
225 {Opt_ignorebadroots, "ignorebadroots"},
226 {Opt_ignorebadroots, "ibadroots"},
227 {Opt_ignoredatacsums, "ignoredatacsums"},
228 {Opt_ignoredatacsums, "idatacsums"},
229 {Opt_rescue_all, "all"},
233 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
234 const char *opt_name)
236 if (fs_info->mount_opt & opt) {
237 btrfs_err(fs_info, "%s must be used with ro mount option",
244 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
249 substring_t args[MAX_OPT_ARGS];
252 opts = kstrdup(options, GFP_KERNEL);
257 while ((p = strsep(&opts, ":")) != NULL) {
262 token = match_token(p, rescue_tokens, args);
264 case Opt_usebackuproot:
266 "trying to use backup root at mount time");
267 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
269 case Opt_nologreplay:
270 btrfs_set_and_info(info, NOLOGREPLAY,
271 "disabling log replay at mount time");
273 case Opt_ignorebadroots:
274 btrfs_set_and_info(info, IGNOREBADROOTS,
275 "ignoring bad roots");
277 case Opt_ignoredatacsums:
278 btrfs_set_and_info(info, IGNOREDATACSUMS,
279 "ignoring data csums");
282 btrfs_info(info, "enabling all of the rescue options");
283 btrfs_set_and_info(info, IGNOREDATACSUMS,
284 "ignoring data csums");
285 btrfs_set_and_info(info, IGNOREBADROOTS,
286 "ignoring bad roots");
287 btrfs_set_and_info(info, NOLOGREPLAY,
288 "disabling log replay at mount time");
291 btrfs_info(info, "unrecognized rescue option '%s'", p);
305 * Regular mount options parser. Everything that is needed only when
306 * reading in a new superblock is parsed here.
307 * XXX JDM: This needs to be cleaned up for remount.
309 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
310 unsigned long new_flags)
312 substring_t args[MAX_OPT_ARGS];
317 bool compress_force = false;
318 enum btrfs_compression_type saved_compress_type;
319 int saved_compress_level;
320 bool saved_compress_force;
322 const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
324 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
325 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
326 else if (btrfs_free_space_cache_v1_active(info)) {
327 if (btrfs_is_zoned(info)) {
329 "zoned: clearing existing space cache");
330 btrfs_set_super_cache_generation(info->super_copy, 0);
332 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
337 * Even the options are empty, we still need to do extra check
343 while ((p = strsep(&options, ",")) != NULL) {
348 token = match_token(p, tokens, args);
351 btrfs_info(info, "allowing degraded mounts");
352 btrfs_set_opt(info->mount_opt, DEGRADED);
355 case Opt_subvol_empty:
359 * These are parsed by btrfs_parse_subvol_options or
360 * btrfs_parse_device_options and can be ignored here.
364 btrfs_set_and_info(info, NODATASUM,
365 "setting nodatasum");
368 if (btrfs_test_opt(info, NODATASUM)) {
369 if (btrfs_test_opt(info, NODATACOW))
371 "setting datasum, datacow enabled");
373 btrfs_info(info, "setting datasum");
375 btrfs_clear_opt(info->mount_opt, NODATACOW);
376 btrfs_clear_opt(info->mount_opt, NODATASUM);
379 if (!btrfs_test_opt(info, NODATACOW)) {
380 if (!btrfs_test_opt(info, COMPRESS) ||
381 !btrfs_test_opt(info, FORCE_COMPRESS)) {
383 "setting nodatacow, compression disabled");
385 btrfs_info(info, "setting nodatacow");
388 btrfs_clear_opt(info->mount_opt, COMPRESS);
389 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
390 btrfs_set_opt(info->mount_opt, NODATACOW);
391 btrfs_set_opt(info->mount_opt, NODATASUM);
394 btrfs_clear_and_info(info, NODATACOW,
397 case Opt_compress_force:
398 case Opt_compress_force_type:
399 compress_force = true;
402 case Opt_compress_type:
403 saved_compress_type = btrfs_test_opt(info,
405 info->compress_type : BTRFS_COMPRESS_NONE;
406 saved_compress_force =
407 btrfs_test_opt(info, FORCE_COMPRESS);
408 saved_compress_level = info->compress_level;
409 if (token == Opt_compress ||
410 token == Opt_compress_force ||
411 strncmp(args[0].from, "zlib", 4) == 0) {
412 compress_type = "zlib";
414 info->compress_type = BTRFS_COMPRESS_ZLIB;
415 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
417 * args[0] contains uninitialized data since
418 * for these tokens we don't expect any
421 if (token != Opt_compress &&
422 token != Opt_compress_force)
423 info->compress_level =
424 btrfs_compress_str2level(
427 btrfs_set_opt(info->mount_opt, COMPRESS);
428 btrfs_clear_opt(info->mount_opt, NODATACOW);
429 btrfs_clear_opt(info->mount_opt, NODATASUM);
431 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
432 compress_type = "lzo";
433 info->compress_type = BTRFS_COMPRESS_LZO;
434 info->compress_level = 0;
435 btrfs_set_opt(info->mount_opt, COMPRESS);
436 btrfs_clear_opt(info->mount_opt, NODATACOW);
437 btrfs_clear_opt(info->mount_opt, NODATASUM);
438 btrfs_set_fs_incompat(info, COMPRESS_LZO);
440 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
441 compress_type = "zstd";
442 info->compress_type = BTRFS_COMPRESS_ZSTD;
443 info->compress_level =
444 btrfs_compress_str2level(
447 btrfs_set_opt(info->mount_opt, COMPRESS);
448 btrfs_clear_opt(info->mount_opt, NODATACOW);
449 btrfs_clear_opt(info->mount_opt, NODATASUM);
450 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
452 } else if (strncmp(args[0].from, "no", 2) == 0) {
453 compress_type = "no";
454 info->compress_level = 0;
455 info->compress_type = 0;
456 btrfs_clear_opt(info->mount_opt, COMPRESS);
457 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
458 compress_force = false;
461 btrfs_err(info, "unrecognized compression value %s",
467 if (compress_force) {
468 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
471 * If we remount from compress-force=xxx to
472 * compress=xxx, we need clear FORCE_COMPRESS
473 * flag, otherwise, there is no way for users
474 * to disable forcible compression separately.
476 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
478 if (no_compress == 1) {
479 btrfs_info(info, "use no compression");
480 } else if ((info->compress_type != saved_compress_type) ||
481 (compress_force != saved_compress_force) ||
482 (info->compress_level != saved_compress_level)) {
483 btrfs_info(info, "%s %s compression, level %d",
484 (compress_force) ? "force" : "use",
485 compress_type, info->compress_level);
487 compress_force = false;
490 btrfs_set_and_info(info, SSD,
491 "enabling ssd optimizations");
492 btrfs_clear_opt(info->mount_opt, NOSSD);
495 btrfs_set_and_info(info, SSD,
496 "enabling ssd optimizations");
497 btrfs_set_and_info(info, SSD_SPREAD,
498 "using spread ssd allocation scheme");
499 btrfs_clear_opt(info->mount_opt, NOSSD);
502 btrfs_set_opt(info->mount_opt, NOSSD);
503 btrfs_clear_and_info(info, SSD,
504 "not using ssd optimizations");
506 case Opt_nossd_spread:
507 btrfs_clear_and_info(info, SSD_SPREAD,
508 "not using spread ssd allocation scheme");
511 btrfs_clear_and_info(info, NOBARRIER,
512 "turning on barriers");
515 btrfs_set_and_info(info, NOBARRIER,
516 "turning off barriers");
518 case Opt_thread_pool:
519 ret = match_int(&args[0], &intarg);
521 btrfs_err(info, "unrecognized thread_pool value %s",
524 } else if (intarg == 0) {
525 btrfs_err(info, "invalid value 0 for thread_pool");
529 info->thread_pool_size = intarg;
532 num = match_strdup(&args[0]);
534 info->max_inline = memparse(num, NULL);
537 if (info->max_inline) {
538 info->max_inline = min_t(u64,
542 btrfs_info(info, "max_inline at %llu",
550 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
551 info->sb->s_flags |= SB_POSIXACL;
554 btrfs_err(info, "support for ACL not compiled in!");
559 info->sb->s_flags &= ~SB_POSIXACL;
562 btrfs_set_and_info(info, NOTREELOG,
563 "disabling tree log");
566 btrfs_clear_and_info(info, NOTREELOG,
567 "enabling tree log");
570 case Opt_nologreplay:
572 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
573 btrfs_set_and_info(info, NOLOGREPLAY,
574 "disabling log replay at mount time");
576 case Opt_flushoncommit:
577 btrfs_set_and_info(info, FLUSHONCOMMIT,
578 "turning on flush-on-commit");
580 case Opt_noflushoncommit:
581 btrfs_clear_and_info(info, FLUSHONCOMMIT,
582 "turning off flush-on-commit");
585 ret = match_int(&args[0], &intarg);
587 btrfs_err(info, "unrecognized metadata_ratio value %s",
591 info->metadata_ratio = intarg;
592 btrfs_info(info, "metadata ratio %u",
593 info->metadata_ratio);
596 case Opt_discard_mode:
597 if (token == Opt_discard ||
598 strcmp(args[0].from, "sync") == 0) {
599 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
600 btrfs_set_and_info(info, DISCARD_SYNC,
601 "turning on sync discard");
602 } else if (strcmp(args[0].from, "async") == 0) {
603 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
604 btrfs_set_and_info(info, DISCARD_ASYNC,
605 "turning on async discard");
607 btrfs_err(info, "unrecognized discard mode value %s",
612 btrfs_clear_opt(info->mount_opt, NODISCARD);
615 btrfs_clear_and_info(info, DISCARD_SYNC,
616 "turning off discard");
617 btrfs_clear_and_info(info, DISCARD_ASYNC,
618 "turning off async discard");
619 btrfs_set_opt(info->mount_opt, NODISCARD);
621 case Opt_space_cache:
622 case Opt_space_cache_version:
624 * We already set FREE_SPACE_TREE above because we have
625 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
626 * to allow v1 to be set for extent tree v2, simply
627 * ignore this setting if we're extent tree v2.
629 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
631 if (token == Opt_space_cache ||
632 strcmp(args[0].from, "v1") == 0) {
633 btrfs_clear_opt(info->mount_opt,
635 btrfs_set_and_info(info, SPACE_CACHE,
636 "enabling disk space caching");
637 } else if (strcmp(args[0].from, "v2") == 0) {
638 btrfs_clear_opt(info->mount_opt,
640 btrfs_set_and_info(info, FREE_SPACE_TREE,
641 "enabling free space tree");
643 btrfs_err(info, "unrecognized space_cache value %s",
649 case Opt_rescan_uuid_tree:
650 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
652 case Opt_no_space_cache:
654 * We cannot operate without the free space tree with
655 * extent tree v2, ignore this option.
657 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
659 if (btrfs_test_opt(info, SPACE_CACHE)) {
660 btrfs_clear_and_info(info, SPACE_CACHE,
661 "disabling disk space caching");
663 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
664 btrfs_clear_and_info(info, FREE_SPACE_TREE,
665 "disabling free space tree");
668 case Opt_inode_cache:
669 case Opt_noinode_cache:
671 "the 'inode_cache' option is deprecated and has no effect since 5.11");
673 case Opt_clear_cache:
675 * We cannot clear the free space tree with extent tree
676 * v2, ignore this option.
678 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
680 btrfs_set_and_info(info, CLEAR_CACHE,
681 "force clearing of disk cache");
683 case Opt_user_subvol_rm_allowed:
684 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
686 case Opt_enospc_debug:
687 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
689 case Opt_noenospc_debug:
690 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
693 btrfs_set_and_info(info, AUTO_DEFRAG,
694 "enabling auto defrag");
697 btrfs_clear_and_info(info, AUTO_DEFRAG,
698 "disabling auto defrag");
701 case Opt_usebackuproot:
703 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
704 token == Opt_recovery ? "recovery" :
707 "trying to use backup root at mount time");
708 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
710 case Opt_skip_balance:
711 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
713 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
714 case Opt_check_integrity_including_extent_data:
716 "integrity checker is deprecated and will be removed in 6.7");
718 "enabling check integrity including extent data");
719 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
720 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
722 case Opt_check_integrity:
724 "integrity checker is deprecated and will be removed in 6.7");
725 btrfs_info(info, "enabling check integrity");
726 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
728 case Opt_check_integrity_print_mask:
729 ret = match_int(&args[0], &intarg);
732 "unrecognized check_integrity_print_mask value %s",
736 info->check_integrity_print_mask = intarg;
738 "integrity checker is deprecated and will be removed in 6.7");
739 btrfs_info(info, "check_integrity_print_mask 0x%x",
740 info->check_integrity_print_mask);
743 case Opt_check_integrity_including_extent_data:
744 case Opt_check_integrity:
745 case Opt_check_integrity_print_mask:
747 "support for check_integrity* not compiled in!");
751 case Opt_fatal_errors:
752 if (strcmp(args[0].from, "panic") == 0) {
753 btrfs_set_opt(info->mount_opt,
754 PANIC_ON_FATAL_ERROR);
755 } else if (strcmp(args[0].from, "bug") == 0) {
756 btrfs_clear_opt(info->mount_opt,
757 PANIC_ON_FATAL_ERROR);
759 btrfs_err(info, "unrecognized fatal_errors value %s",
765 case Opt_commit_interval:
767 ret = match_int(&args[0], &intarg);
769 btrfs_err(info, "unrecognized commit_interval value %s",
776 "using default commit interval %us",
777 BTRFS_DEFAULT_COMMIT_INTERVAL);
778 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
779 } else if (intarg > 300) {
780 btrfs_warn(info, "excessive commit interval %d",
783 info->commit_interval = intarg;
786 ret = parse_rescue_options(info, args[0].from);
788 btrfs_err(info, "unrecognized rescue value %s",
793 #ifdef CONFIG_BTRFS_DEBUG
794 case Opt_fragment_all:
795 btrfs_info(info, "fragmenting all space");
796 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
797 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
799 case Opt_fragment_metadata:
800 btrfs_info(info, "fragmenting metadata");
801 btrfs_set_opt(info->mount_opt,
804 case Opt_fragment_data:
805 btrfs_info(info, "fragmenting data");
806 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
809 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
811 btrfs_info(info, "doing ref verification");
812 btrfs_set_opt(info->mount_opt, REF_VERIFY);
816 btrfs_err(info, "unrecognized mount option '%s'", p);
824 /* We're read-only, don't have to check. */
825 if (new_flags & SB_RDONLY)
828 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
829 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
830 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
833 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
834 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
835 !btrfs_test_opt(info, CLEAR_CACHE)) {
836 btrfs_err(info, "cannot disable free space tree");
839 if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
840 !btrfs_test_opt(info, FREE_SPACE_TREE)) {
841 btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
845 ret = btrfs_check_mountopts_zoned(info);
846 if (!ret && !remounting) {
847 if (btrfs_test_opt(info, SPACE_CACHE))
848 btrfs_info(info, "disk space caching is enabled");
849 if (btrfs_test_opt(info, FREE_SPACE_TREE))
850 btrfs_info(info, "using free space tree");
856 * Parse mount options that are required early in the mount process.
858 * All other options will be parsed on much later in the mount process and
859 * only when we need to allocate a new super block.
861 static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
863 substring_t args[MAX_OPT_ARGS];
864 char *device_name, *opts, *orig, *p;
865 struct btrfs_device *device = NULL;
868 lockdep_assert_held(&uuid_mutex);
874 * strsep changes the string, duplicate it because btrfs_parse_options
877 opts = kstrdup(options, GFP_KERNEL);
882 while ((p = strsep(&opts, ",")) != NULL) {
888 token = match_token(p, tokens, args);
889 if (token == Opt_device) {
890 device_name = match_strdup(&args[0]);
895 device = btrfs_scan_one_device(device_name, flags);
897 if (IS_ERR(device)) {
898 error = PTR_ERR(device);
910 * Parse mount options that are related to subvolume id
912 * The value is later passed to mount_subvol()
914 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
915 u64 *subvol_objectid)
917 substring_t args[MAX_OPT_ARGS];
918 char *opts, *orig, *p;
926 * strsep changes the string, duplicate it because
927 * btrfs_parse_device_options gets called later
929 opts = kstrdup(options, GFP_KERNEL);
934 while ((p = strsep(&opts, ",")) != NULL) {
939 token = match_token(p, tokens, args);
943 *subvol_name = match_strdup(&args[0]);
950 error = match_u64(&args[0], &subvolid);
954 /* we want the original fs_tree */
956 subvolid = BTRFS_FS_TREE_OBJECTID;
958 *subvol_objectid = subvolid;
970 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
973 struct btrfs_root *root = fs_info->tree_root;
974 struct btrfs_root *fs_root = NULL;
975 struct btrfs_root_ref *root_ref;
976 struct btrfs_inode_ref *inode_ref;
977 struct btrfs_key key;
978 struct btrfs_path *path = NULL;
979 char *name = NULL, *ptr;
984 path = btrfs_alloc_path();
990 name = kmalloc(PATH_MAX, GFP_KERNEL);
995 ptr = name + PATH_MAX - 1;
999 * Walk up the subvolume trees in the tree of tree roots by root
1000 * backrefs until we hit the top-level subvolume.
1002 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1003 key.objectid = subvol_objectid;
1004 key.type = BTRFS_ROOT_BACKREF_KEY;
1005 key.offset = (u64)-1;
1007 ret = btrfs_search_backwards(root, &key, path);
1010 } else if (ret > 0) {
1015 subvol_objectid = key.offset;
1017 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1018 struct btrfs_root_ref);
1019 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1022 ret = -ENAMETOOLONG;
1025 read_extent_buffer(path->nodes[0], ptr + 1,
1026 (unsigned long)(root_ref + 1), len);
1028 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1029 btrfs_release_path(path);
1031 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1032 if (IS_ERR(fs_root)) {
1033 ret = PTR_ERR(fs_root);
1039 * Walk up the filesystem tree by inode refs until we hit the
1042 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1043 key.objectid = dirid;
1044 key.type = BTRFS_INODE_REF_KEY;
1045 key.offset = (u64)-1;
1047 ret = btrfs_search_backwards(fs_root, &key, path);
1050 } else if (ret > 0) {
1057 inode_ref = btrfs_item_ptr(path->nodes[0],
1059 struct btrfs_inode_ref);
1060 len = btrfs_inode_ref_name_len(path->nodes[0],
1064 ret = -ENAMETOOLONG;
1067 read_extent_buffer(path->nodes[0], ptr + 1,
1068 (unsigned long)(inode_ref + 1), len);
1070 btrfs_release_path(path);
1072 btrfs_put_root(fs_root);
1076 btrfs_free_path(path);
1077 if (ptr == name + PATH_MAX - 1) {
1081 memmove(name, ptr, name + PATH_MAX - ptr);
1086 btrfs_put_root(fs_root);
1087 btrfs_free_path(path);
1089 return ERR_PTR(ret);
1092 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1094 struct btrfs_root *root = fs_info->tree_root;
1095 struct btrfs_dir_item *di;
1096 struct btrfs_path *path;
1097 struct btrfs_key location;
1098 struct fscrypt_str name = FSTR_INIT("default", 7);
1101 path = btrfs_alloc_path();
1106 * Find the "default" dir item which points to the root item that we
1107 * will mount by default if we haven't been given a specific subvolume
1110 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1111 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
1113 btrfs_free_path(path);
1118 * Ok the default dir item isn't there. This is weird since
1119 * it's always been there, but don't freak out, just try and
1120 * mount the top-level subvolume.
1122 btrfs_free_path(path);
1123 *objectid = BTRFS_FS_TREE_OBJECTID;
1127 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1128 btrfs_free_path(path);
1129 *objectid = location.objectid;
1133 static int btrfs_fill_super(struct super_block *sb,
1134 struct btrfs_fs_devices *fs_devices,
1137 struct inode *inode;
1138 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1141 sb->s_maxbytes = MAX_LFS_FILESIZE;
1142 sb->s_magic = BTRFS_SUPER_MAGIC;
1143 sb->s_op = &btrfs_super_ops;
1144 sb->s_d_op = &btrfs_dentry_operations;
1145 sb->s_export_op = &btrfs_export_ops;
1146 #ifdef CONFIG_FS_VERITY
1147 sb->s_vop = &btrfs_verityops;
1149 sb->s_xattr = btrfs_xattr_handlers;
1150 sb->s_time_gran = 1;
1151 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1152 sb->s_flags |= SB_POSIXACL;
1154 sb->s_flags |= SB_I_VERSION;
1155 sb->s_iflags |= SB_I_CGROUPWB;
1157 err = super_setup_bdi(sb);
1159 btrfs_err(fs_info, "super_setup_bdi failed");
1163 err = open_ctree(sb, fs_devices, (char *)data);
1165 btrfs_err(fs_info, "open_ctree failed");
1169 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1170 if (IS_ERR(inode)) {
1171 err = PTR_ERR(inode);
1172 btrfs_handle_fs_error(fs_info, err, NULL);
1176 sb->s_root = d_make_root(inode);
1182 sb->s_flags |= SB_ACTIVE;
1186 close_ctree(fs_info);
1190 int btrfs_sync_fs(struct super_block *sb, int wait)
1192 struct btrfs_trans_handle *trans;
1193 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1194 struct btrfs_root *root = fs_info->tree_root;
1196 trace_btrfs_sync_fs(fs_info, wait);
1199 filemap_flush(fs_info->btree_inode->i_mapping);
1203 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1205 trans = btrfs_attach_transaction_barrier(root);
1206 if (IS_ERR(trans)) {
1207 /* no transaction, don't bother */
1208 if (PTR_ERR(trans) == -ENOENT) {
1210 * Exit unless we have some pending changes
1211 * that need to go through commit
1213 if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
1217 * A non-blocking test if the fs is frozen. We must not
1218 * start a new transaction here otherwise a deadlock
1219 * happens. The pending operations are delayed to the
1220 * next commit after thawing.
1222 if (sb_start_write_trylock(sb))
1226 trans = btrfs_start_transaction(root, 0);
1229 return PTR_ERR(trans);
1231 return btrfs_commit_transaction(trans);
1234 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1236 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1240 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1242 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1243 const char *compress_type;
1244 const char *subvol_name;
1245 bool printed = false;
1247 if (btrfs_test_opt(info, DEGRADED))
1248 seq_puts(seq, ",degraded");
1249 if (btrfs_test_opt(info, NODATASUM))
1250 seq_puts(seq, ",nodatasum");
1251 if (btrfs_test_opt(info, NODATACOW))
1252 seq_puts(seq, ",nodatacow");
1253 if (btrfs_test_opt(info, NOBARRIER))
1254 seq_puts(seq, ",nobarrier");
1255 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1256 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1257 if (info->thread_pool_size != min_t(unsigned long,
1258 num_online_cpus() + 2, 8))
1259 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1260 if (btrfs_test_opt(info, COMPRESS)) {
1261 compress_type = btrfs_compress_type2str(info->compress_type);
1262 if (btrfs_test_opt(info, FORCE_COMPRESS))
1263 seq_printf(seq, ",compress-force=%s", compress_type);
1265 seq_printf(seq, ",compress=%s", compress_type);
1266 if (info->compress_level)
1267 seq_printf(seq, ":%d", info->compress_level);
1269 if (btrfs_test_opt(info, NOSSD))
1270 seq_puts(seq, ",nossd");
1271 if (btrfs_test_opt(info, SSD_SPREAD))
1272 seq_puts(seq, ",ssd_spread");
1273 else if (btrfs_test_opt(info, SSD))
1274 seq_puts(seq, ",ssd");
1275 if (btrfs_test_opt(info, NOTREELOG))
1276 seq_puts(seq, ",notreelog");
1277 if (btrfs_test_opt(info, NOLOGREPLAY))
1278 print_rescue_option(seq, "nologreplay", &printed);
1279 if (btrfs_test_opt(info, USEBACKUPROOT))
1280 print_rescue_option(seq, "usebackuproot", &printed);
1281 if (btrfs_test_opt(info, IGNOREBADROOTS))
1282 print_rescue_option(seq, "ignorebadroots", &printed);
1283 if (btrfs_test_opt(info, IGNOREDATACSUMS))
1284 print_rescue_option(seq, "ignoredatacsums", &printed);
1285 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1286 seq_puts(seq, ",flushoncommit");
1287 if (btrfs_test_opt(info, DISCARD_SYNC))
1288 seq_puts(seq, ",discard");
1289 if (btrfs_test_opt(info, DISCARD_ASYNC))
1290 seq_puts(seq, ",discard=async");
1291 if (!(info->sb->s_flags & SB_POSIXACL))
1292 seq_puts(seq, ",noacl");
1293 if (btrfs_free_space_cache_v1_active(info))
1294 seq_puts(seq, ",space_cache");
1295 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
1296 seq_puts(seq, ",space_cache=v2");
1298 seq_puts(seq, ",nospace_cache");
1299 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1300 seq_puts(seq, ",rescan_uuid_tree");
1301 if (btrfs_test_opt(info, CLEAR_CACHE))
1302 seq_puts(seq, ",clear_cache");
1303 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1304 seq_puts(seq, ",user_subvol_rm_allowed");
1305 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1306 seq_puts(seq, ",enospc_debug");
1307 if (btrfs_test_opt(info, AUTO_DEFRAG))
1308 seq_puts(seq, ",autodefrag");
1309 if (btrfs_test_opt(info, SKIP_BALANCE))
1310 seq_puts(seq, ",skip_balance");
1311 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1312 if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
1313 seq_puts(seq, ",check_int_data");
1314 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1315 seq_puts(seq, ",check_int");
1316 if (info->check_integrity_print_mask)
1317 seq_printf(seq, ",check_int_print_mask=%d",
1318 info->check_integrity_print_mask);
1320 if (info->metadata_ratio)
1321 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1322 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1323 seq_puts(seq, ",fatal_errors=panic");
1324 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1325 seq_printf(seq, ",commit=%u", info->commit_interval);
1326 #ifdef CONFIG_BTRFS_DEBUG
1327 if (btrfs_test_opt(info, FRAGMENT_DATA))
1328 seq_puts(seq, ",fragment=data");
1329 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1330 seq_puts(seq, ",fragment=metadata");
1332 if (btrfs_test_opt(info, REF_VERIFY))
1333 seq_puts(seq, ",ref_verify");
1334 seq_printf(seq, ",subvolid=%llu",
1335 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1336 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1337 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1338 if (!IS_ERR(subvol_name)) {
1339 seq_puts(seq, ",subvol=");
1340 seq_escape(seq, subvol_name, " \t\n\\");
1346 static int btrfs_test_super(struct super_block *s, void *data)
1348 struct btrfs_fs_info *p = data;
1349 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1351 return fs_info->fs_devices == p->fs_devices;
1354 static int btrfs_set_super(struct super_block *s, void *data)
1356 int err = set_anon_super(s, data);
1358 s->s_fs_info = data;
1363 * subvolumes are identified by ino 256
1365 static inline int is_subvolume_inode(struct inode *inode)
1367 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1372 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1373 struct vfsmount *mnt)
1375 struct dentry *root;
1379 if (!subvol_objectid) {
1380 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1383 root = ERR_PTR(ret);
1387 subvol_name = btrfs_get_subvol_name_from_objectid(
1388 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1389 if (IS_ERR(subvol_name)) {
1390 root = ERR_CAST(subvol_name);
1397 root = mount_subtree(mnt, subvol_name);
1398 /* mount_subtree() drops our reference on the vfsmount. */
1401 if (!IS_ERR(root)) {
1402 struct super_block *s = root->d_sb;
1403 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1404 struct inode *root_inode = d_inode(root);
1405 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1408 if (!is_subvolume_inode(root_inode)) {
1409 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1413 if (subvol_objectid && root_objectid != subvol_objectid) {
1415 * This will also catch a race condition where a
1416 * subvolume which was passed by ID is renamed and
1417 * another subvolume is renamed over the old location.
1420 "subvol '%s' does not match subvolid %llu",
1421 subvol_name, subvol_objectid);
1426 root = ERR_PTR(ret);
1427 deactivate_locked_super(s);
1438 * Find a superblock for the given device / mount point.
1440 * Note: This is based on mount_bdev from fs/super.c with a few additions
1441 * for multiple device setup. Make sure to keep it in sync.
1443 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1444 int flags, const char *device_name, void *data)
1446 struct block_device *bdev = NULL;
1447 struct super_block *s;
1448 struct btrfs_device *device = NULL;
1449 struct btrfs_fs_devices *fs_devices = NULL;
1450 struct btrfs_fs_info *fs_info = NULL;
1451 void *new_sec_opts = NULL;
1452 blk_mode_t mode = sb_open_mode(flags);
1456 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1458 return ERR_PTR(error);
1462 * Setup a dummy root and fs_info for test/set super. This is because
1463 * we don't actually fill this stuff out until open_ctree, but we need
1464 * then open_ctree will properly initialize the file system specific
1465 * settings later. btrfs_init_fs_info initializes the static elements
1466 * of the fs_info (locks and such) to make cleanup easier if we find a
1467 * superblock with our given fs_devices later on at sget() time.
1469 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1472 goto error_sec_opts;
1474 btrfs_init_fs_info(fs_info);
1476 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1477 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1478 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1483 mutex_lock(&uuid_mutex);
1484 error = btrfs_parse_device_options(data, mode);
1486 mutex_unlock(&uuid_mutex);
1490 device = btrfs_scan_one_device(device_name, mode);
1491 if (IS_ERR(device)) {
1492 mutex_unlock(&uuid_mutex);
1493 error = PTR_ERR(device);
1497 fs_devices = device->fs_devices;
1498 fs_info->fs_devices = fs_devices;
1500 error = btrfs_open_devices(fs_devices, mode, fs_type);
1501 mutex_unlock(&uuid_mutex);
1505 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1507 goto error_close_devices;
1510 bdev = fs_devices->latest_dev->bdev;
1511 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1515 goto error_close_devices;
1519 btrfs_close_devices(fs_devices);
1520 btrfs_free_fs_info(fs_info);
1521 if ((flags ^ s->s_flags) & SB_RDONLY)
1524 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1525 shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1527 btrfs_sb(s)->bdev_holder = fs_type;
1528 error = btrfs_fill_super(s, fs_devices, data);
1531 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1532 security_free_mnt_opts(&new_sec_opts);
1534 deactivate_locked_super(s);
1535 return ERR_PTR(error);
1538 return dget(s->s_root);
1540 error_close_devices:
1541 btrfs_close_devices(fs_devices);
1543 btrfs_free_fs_info(fs_info);
1545 security_free_mnt_opts(&new_sec_opts);
1546 return ERR_PTR(error);
1550 * Mount function which is called by VFS layer.
1552 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1553 * which needs vfsmount* of device's root (/). This means device's root has to
1554 * be mounted internally in any case.
1557 * 1. Parse subvol id related options for later use in mount_subvol().
1559 * 2. Mount device's root (/) by calling vfs_kern_mount().
1561 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1562 * first place. In order to avoid calling btrfs_mount() again, we use
1563 * different file_system_type which is not registered to VFS by
1564 * register_filesystem() (btrfs_root_fs_type). As a result,
1565 * btrfs_mount_root() is called. The return value will be used by
1566 * mount_subtree() in mount_subvol().
1568 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1569 * "btrfs subvolume set-default", mount_subvol() is called always.
1571 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1572 const char *device_name, void *data)
1574 struct vfsmount *mnt_root;
1575 struct dentry *root;
1576 char *subvol_name = NULL;
1577 u64 subvol_objectid = 0;
1580 error = btrfs_parse_subvol_options(data, &subvol_name,
1584 return ERR_PTR(error);
1587 /* mount device's root (/) */
1588 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1589 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1590 if (flags & SB_RDONLY) {
1591 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1592 flags & ~SB_RDONLY, device_name, data);
1594 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1595 flags | SB_RDONLY, device_name, data);
1596 if (IS_ERR(mnt_root)) {
1597 root = ERR_CAST(mnt_root);
1602 down_write(&mnt_root->mnt_sb->s_umount);
1603 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1604 up_write(&mnt_root->mnt_sb->s_umount);
1606 root = ERR_PTR(error);
1613 if (IS_ERR(mnt_root)) {
1614 root = ERR_CAST(mnt_root);
1619 /* mount_subvol() will free subvol_name and mnt_root */
1620 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1626 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1627 u32 new_pool_size, u32 old_pool_size)
1629 if (new_pool_size == old_pool_size)
1632 fs_info->thread_pool_size = new_pool_size;
1634 btrfs_info(fs_info, "resize thread pool %d -> %d",
1635 old_pool_size, new_pool_size);
1637 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1638 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1639 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1640 workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1641 workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1642 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1643 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1644 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1647 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1648 unsigned long old_opts, int flags)
1650 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1651 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1652 (flags & SB_RDONLY))) {
1653 /* wait for any defraggers to finish */
1654 wait_event(fs_info->transaction_wait,
1655 (atomic_read(&fs_info->defrag_running) == 0));
1656 if (flags & SB_RDONLY)
1657 sync_filesystem(fs_info->sb);
1661 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1662 unsigned long old_opts)
1664 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1667 * We need to cleanup all defragable inodes if the autodefragment is
1668 * close or the filesystem is read only.
1670 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1671 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1672 btrfs_cleanup_defrag_inodes(fs_info);
1675 /* If we toggled discard async */
1676 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1677 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1678 btrfs_discard_resume(fs_info);
1679 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1680 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1681 btrfs_discard_cleanup(fs_info);
1683 /* If we toggled space cache */
1684 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1685 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1688 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1690 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1691 unsigned old_flags = sb->s_flags;
1692 unsigned long old_opts = fs_info->mount_opt;
1693 unsigned long old_compress_type = fs_info->compress_type;
1694 u64 old_max_inline = fs_info->max_inline;
1695 u32 old_thread_pool_size = fs_info->thread_pool_size;
1696 u32 old_metadata_ratio = fs_info->metadata_ratio;
1699 sync_filesystem(sb);
1700 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1703 void *new_sec_opts = NULL;
1705 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1707 ret = security_sb_remount(sb, new_sec_opts);
1708 security_free_mnt_opts(&new_sec_opts);
1713 ret = btrfs_parse_options(fs_info, data, *flags);
1717 ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
1721 btrfs_remount_begin(fs_info, old_opts, *flags);
1722 btrfs_resize_thread_pool(fs_info,
1723 fs_info->thread_pool_size, old_thread_pool_size);
1725 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1726 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1727 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
1729 "remount supports changing free space tree only from ro to rw");
1730 /* Make sure free space cache options match the state on disk */
1731 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1732 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1733 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
1735 if (btrfs_free_space_cache_v1_active(fs_info)) {
1736 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1737 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
1741 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1744 if (*flags & SB_RDONLY) {
1746 * this also happens on 'umount -rf' or on shutdown, when
1747 * the filesystem is busy.
1749 cancel_work_sync(&fs_info->async_reclaim_work);
1750 cancel_work_sync(&fs_info->async_data_reclaim_work);
1752 btrfs_discard_cleanup(fs_info);
1754 /* wait for the uuid_scan task to finish */
1755 down(&fs_info->uuid_tree_rescan_sem);
1756 /* avoid complains from lockdep et al. */
1757 up(&fs_info->uuid_tree_rescan_sem);
1759 btrfs_set_sb_rdonly(sb);
1762 * Setting SB_RDONLY will put the cleaner thread to
1763 * sleep at the next loop if it's already active.
1764 * If it's already asleep, we'll leave unused block
1765 * groups on disk until we're mounted read-write again
1766 * unless we clean them up here.
1768 btrfs_delete_unused_bgs(fs_info);
1771 * The cleaner task could be already running before we set the
1772 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1773 * We must make sure that after we finish the remount, i.e. after
1774 * we call btrfs_commit_super(), the cleaner can no longer start
1775 * a transaction - either because it was dropping a dead root,
1776 * running delayed iputs or deleting an unused block group (the
1777 * cleaner picked a block group from the list of unused block
1778 * groups before we were able to in the previous call to
1779 * btrfs_delete_unused_bgs()).
1781 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1782 TASK_UNINTERRUPTIBLE);
1785 * We've set the superblock to RO mode, so we might have made
1786 * the cleaner task sleep without running all pending delayed
1787 * iputs. Go through all the delayed iputs here, so that if an
1788 * unmount happens without remounting RW we don't end up at
1789 * finishing close_ctree() with a non-empty list of delayed
1792 btrfs_run_delayed_iputs(fs_info);
1794 btrfs_dev_replace_suspend_for_unmount(fs_info);
1795 btrfs_scrub_cancel(fs_info);
1796 btrfs_pause_balance(fs_info);
1799 * Pause the qgroup rescan worker if it is running. We don't want
1800 * it to be still running after we are in RO mode, as after that,
1801 * by the time we unmount, it might have left a transaction open,
1802 * so we would leak the transaction and/or crash.
1804 btrfs_qgroup_wait_for_completion(fs_info, false);
1806 ret = btrfs_commit_super(fs_info);
1810 if (BTRFS_FS_ERROR(fs_info)) {
1812 "Remounting read-write after error is not allowed");
1816 if (fs_info->fs_devices->rw_devices == 0) {
1821 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1823 "too many missing devices, writable remount is not allowed");
1828 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1830 "mount required to replay tree-log, cannot remount read-write");
1836 * NOTE: when remounting with a change that does writes, don't
1837 * put it anywhere above this point, as we are not sure to be
1838 * safe to write until we pass the above checks.
1840 ret = btrfs_start_pre_rw_mount(fs_info);
1844 btrfs_clear_sb_rdonly(sb);
1846 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1849 * If we've gone from readonly -> read/write, we need to get
1850 * our sync/async discard lists in the right state.
1852 btrfs_discard_resume(fs_info);
1856 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1857 * since the absence of the flag means it can be toggled off by remount.
1859 *flags |= SB_I_VERSION;
1861 wake_up_process(fs_info->transaction_kthread);
1862 btrfs_remount_cleanup(fs_info, old_opts);
1863 btrfs_clear_oneshot_options(fs_info);
1864 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1869 /* We've hit an error - don't reset SB_RDONLY */
1871 old_flags |= SB_RDONLY;
1872 if (!(old_flags & SB_RDONLY))
1873 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
1874 sb->s_flags = old_flags;
1875 fs_info->mount_opt = old_opts;
1876 fs_info->compress_type = old_compress_type;
1877 fs_info->max_inline = old_max_inline;
1878 btrfs_resize_thread_pool(fs_info,
1879 old_thread_pool_size, fs_info->thread_pool_size);
1880 fs_info->metadata_ratio = old_metadata_ratio;
1881 btrfs_remount_cleanup(fs_info, old_opts);
1882 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1887 /* Used to sort the devices by max_avail(descending sort) */
1888 static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1890 const struct btrfs_device_info *dev_info1 = a;
1891 const struct btrfs_device_info *dev_info2 = b;
1893 if (dev_info1->max_avail > dev_info2->max_avail)
1895 else if (dev_info1->max_avail < dev_info2->max_avail)
1901 * sort the devices by max_avail, in which max free extent size of each device
1902 * is stored.(Descending Sort)
1904 static inline void btrfs_descending_sort_devices(
1905 struct btrfs_device_info *devices,
1908 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1909 btrfs_cmp_device_free_bytes, NULL);
1913 * The helper to calc the free space on the devices that can be used to store
1916 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
1919 struct btrfs_device_info *devices_info;
1920 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1921 struct btrfs_device *device;
1924 u64 min_stripe_size;
1925 int num_stripes = 1;
1926 int i = 0, nr_devices;
1927 const struct btrfs_raid_attr *rattr;
1930 * We aren't under the device list lock, so this is racy-ish, but good
1931 * enough for our purposes.
1933 nr_devices = fs_info->fs_devices->open_devices;
1936 nr_devices = fs_info->fs_devices->open_devices;
1944 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
1949 /* calc min stripe number for data space allocation */
1950 type = btrfs_data_alloc_profile(fs_info);
1951 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
1953 if (type & BTRFS_BLOCK_GROUP_RAID0)
1954 num_stripes = nr_devices;
1955 else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1956 num_stripes = rattr->ncopies;
1957 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1960 /* Adjust for more than 1 stripe per device */
1961 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
1964 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1965 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1966 &device->dev_state) ||
1968 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1971 if (i >= nr_devices)
1974 avail_space = device->total_bytes - device->bytes_used;
1976 /* align with stripe_len */
1977 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
1980 * Ensure we have at least min_stripe_size on top of the
1981 * reserved space on the device.
1983 if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
1986 avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1988 devices_info[i].dev = device;
1989 devices_info[i].max_avail = avail_space;
1997 btrfs_descending_sort_devices(devices_info, nr_devices);
2001 while (nr_devices >= rattr->devs_min) {
2002 num_stripes = min(num_stripes, nr_devices);
2004 if (devices_info[i].max_avail >= min_stripe_size) {
2008 avail_space += devices_info[i].max_avail * num_stripes;
2009 alloc_size = devices_info[i].max_avail;
2010 for (j = i + 1 - num_stripes; j <= i; j++)
2011 devices_info[j].max_avail -= alloc_size;
2017 kfree(devices_info);
2018 *free_bytes = avail_space;
2023 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2025 * If there's a redundant raid level at DATA block groups, use the respective
2026 * multiplier to scale the sizes.
2028 * Unused device space usage is based on simulating the chunk allocator
2029 * algorithm that respects the device sizes and order of allocations. This is
2030 * a close approximation of the actual use but there are other factors that may
2031 * change the result (like a new metadata chunk).
2033 * If metadata is exhausted, f_bavail will be 0.
2035 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2037 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2038 struct btrfs_super_block *disk_super = fs_info->super_copy;
2039 struct btrfs_space_info *found;
2041 u64 total_free_data = 0;
2042 u64 total_free_meta = 0;
2043 u32 bits = fs_info->sectorsize_bits;
2044 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2045 unsigned factor = 1;
2046 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2051 list_for_each_entry(found, &fs_info->space_info, list) {
2052 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2055 total_free_data += found->disk_total - found->disk_used;
2057 btrfs_account_ro_block_groups_free_space(found);
2059 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2060 if (!list_empty(&found->block_groups[i]))
2061 factor = btrfs_bg_type_to_factor(
2062 btrfs_raid_array[i].bg_flag);
2067 * Metadata in mixed block group profiles are accounted in data
2069 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2070 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2073 total_free_meta += found->disk_total -
2077 total_used += found->disk_used;
2080 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2081 buf->f_blocks >>= bits;
2082 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2084 /* Account global block reserve as used, it's in logical size already */
2085 spin_lock(&block_rsv->lock);
2086 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2087 if (buf->f_bfree >= block_rsv->size >> bits)
2088 buf->f_bfree -= block_rsv->size >> bits;
2091 spin_unlock(&block_rsv->lock);
2093 buf->f_bavail = div_u64(total_free_data, factor);
2094 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2097 buf->f_bavail += div_u64(total_free_data, factor);
2098 buf->f_bavail = buf->f_bavail >> bits;
2101 * We calculate the remaining metadata space minus global reserve. If
2102 * this is (supposedly) smaller than zero, there's no space. But this
2103 * does not hold in practice, the exhausted state happens where's still
2104 * some positive delta. So we apply some guesswork and compare the
2105 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2107 * We probably cannot calculate the exact threshold value because this
2108 * depends on the internal reservations requested by various
2109 * operations, so some operations that consume a few metadata will
2110 * succeed even if the Avail is zero. But this is better than the other
2116 * We only want to claim there's no available space if we can no longer
2117 * allocate chunks for our metadata profile and our global reserve will
2118 * not fit in the free metadata space. If we aren't ->full then we
2119 * still can allocate chunks and thus are fine using the currently
2120 * calculated f_bavail.
2122 if (!mixed && block_rsv->space_info->full &&
2123 (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
2126 buf->f_type = BTRFS_SUPER_MAGIC;
2127 buf->f_bsize = dentry->d_sb->s_blocksize;
2128 buf->f_namelen = BTRFS_NAME_LEN;
2130 /* We treat it as constant endianness (it doesn't matter _which_)
2131 because we want the fsid to come out the same whether mounted
2132 on a big-endian or little-endian host */
2133 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2134 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2135 /* Mask in the root object ID too, to disambiguate subvols */
2136 buf->f_fsid.val[0] ^=
2137 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2138 buf->f_fsid.val[1] ^=
2139 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2144 static void btrfs_kill_super(struct super_block *sb)
2146 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2147 kill_anon_super(sb);
2148 btrfs_free_fs_info(fs_info);
2151 static struct file_system_type btrfs_fs_type = {
2152 .owner = THIS_MODULE,
2154 .mount = btrfs_mount,
2155 .kill_sb = btrfs_kill_super,
2156 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2159 static struct file_system_type btrfs_root_fs_type = {
2160 .owner = THIS_MODULE,
2162 .mount = btrfs_mount_root,
2163 .kill_sb = btrfs_kill_super,
2164 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2167 MODULE_ALIAS_FS("btrfs");
2169 static int btrfs_control_open(struct inode *inode, struct file *file)
2172 * The control file's private_data is used to hold the
2173 * transaction when it is started and is used to keep
2174 * track of whether a transaction is already in progress.
2176 file->private_data = NULL;
2181 * Used by /dev/btrfs-control for devices ioctls.
2183 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2186 struct btrfs_ioctl_vol_args *vol;
2187 struct btrfs_device *device = NULL;
2191 if (!capable(CAP_SYS_ADMIN))
2194 vol = memdup_user((void __user *)arg, sizeof(*vol));
2196 return PTR_ERR(vol);
2197 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2200 case BTRFS_IOC_SCAN_DEV:
2201 mutex_lock(&uuid_mutex);
2202 device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2203 ret = PTR_ERR_OR_ZERO(device);
2204 mutex_unlock(&uuid_mutex);
2206 case BTRFS_IOC_FORGET_DEV:
2207 if (vol->name[0] != 0) {
2208 ret = lookup_bdev(vol->name, &devt);
2212 ret = btrfs_forget_devices(devt);
2214 case BTRFS_IOC_DEVICES_READY:
2215 mutex_lock(&uuid_mutex);
2216 device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2217 if (IS_ERR(device)) {
2218 mutex_unlock(&uuid_mutex);
2219 ret = PTR_ERR(device);
2222 ret = !(device->fs_devices->num_devices ==
2223 device->fs_devices->total_devices);
2224 mutex_unlock(&uuid_mutex);
2226 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2227 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2235 static int btrfs_freeze(struct super_block *sb)
2237 struct btrfs_trans_handle *trans;
2238 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2239 struct btrfs_root *root = fs_info->tree_root;
2241 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2243 * We don't need a barrier here, we'll wait for any transaction that
2244 * could be in progress on other threads (and do delayed iputs that
2245 * we want to avoid on a frozen filesystem), or do the commit
2248 trans = btrfs_attach_transaction_barrier(root);
2249 if (IS_ERR(trans)) {
2250 /* no transaction, don't bother */
2251 if (PTR_ERR(trans) == -ENOENT)
2253 return PTR_ERR(trans);
2255 return btrfs_commit_transaction(trans);
2258 static int check_dev_super(struct btrfs_device *dev)
2260 struct btrfs_fs_info *fs_info = dev->fs_info;
2261 struct btrfs_super_block *sb;
2265 /* This should be called with fs still frozen. */
2266 ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2268 /* Missing dev, no need to check. */
2272 /* Only need to check the primary super block. */
2273 sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2277 /* Verify the checksum. */
2278 csum_type = btrfs_super_csum_type(sb);
2279 if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
2280 btrfs_err(fs_info, "csum type changed, has %u expect %u",
2281 csum_type, btrfs_super_csum_type(fs_info->super_copy));
2286 if (btrfs_check_super_csum(fs_info, sb)) {
2287 btrfs_err(fs_info, "csum for on-disk super block no longer matches");
2292 /* Btrfs_validate_super() includes fsid check against super->fsid. */
2293 ret = btrfs_validate_super(fs_info, sb, 0);
2297 if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2298 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2299 btrfs_super_generation(sb),
2300 fs_info->last_trans_committed);
2305 btrfs_release_disk_super(sb);
2309 static int btrfs_unfreeze(struct super_block *sb)
2311 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2312 struct btrfs_device *device;
2316 * Make sure the fs is not changed by accident (like hibernation then
2317 * modified by other OS).
2318 * If we found anything wrong, we mark the fs error immediately.
2320 * And since the fs is frozen, no one can modify the fs yet, thus
2321 * we don't need to hold device_list_mutex.
2323 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2324 ret = check_dev_super(device);
2326 btrfs_handle_fs_error(fs_info, ret,
2327 "super block on devid %llu got modified unexpectedly",
2332 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2335 * We still return 0, to allow VFS layer to unfreeze the fs even the
2336 * above checks failed. Since the fs is either fine or read-only, we're
2337 * safe to continue, without causing further damage.
2342 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2344 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2347 * There should be always a valid pointer in latest_dev, it may be stale
2348 * for a short moment in case it's being deleted but still valid until
2349 * the end of RCU grace period.
2352 seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
2358 static const struct super_operations btrfs_super_ops = {
2359 .drop_inode = btrfs_drop_inode,
2360 .evict_inode = btrfs_evict_inode,
2361 .put_super = btrfs_put_super,
2362 .sync_fs = btrfs_sync_fs,
2363 .show_options = btrfs_show_options,
2364 .show_devname = btrfs_show_devname,
2365 .alloc_inode = btrfs_alloc_inode,
2366 .destroy_inode = btrfs_destroy_inode,
2367 .free_inode = btrfs_free_inode,
2368 .statfs = btrfs_statfs,
2369 .remount_fs = btrfs_remount,
2370 .freeze_fs = btrfs_freeze,
2371 .unfreeze_fs = btrfs_unfreeze,
2374 static const struct file_operations btrfs_ctl_fops = {
2375 .open = btrfs_control_open,
2376 .unlocked_ioctl = btrfs_control_ioctl,
2377 .compat_ioctl = compat_ptr_ioctl,
2378 .owner = THIS_MODULE,
2379 .llseek = noop_llseek,
2382 static struct miscdevice btrfs_misc = {
2383 .minor = BTRFS_MINOR,
2384 .name = "btrfs-control",
2385 .fops = &btrfs_ctl_fops
2388 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2389 MODULE_ALIAS("devname:btrfs-control");
2391 static int __init btrfs_interface_init(void)
2393 return misc_register(&btrfs_misc);
2396 static __cold void btrfs_interface_exit(void)
2398 misc_deregister(&btrfs_misc);
2401 static int __init btrfs_print_mod_info(void)
2403 static const char options[] = ""
2404 #ifdef CONFIG_BTRFS_DEBUG
2407 #ifdef CONFIG_BTRFS_ASSERT
2410 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2411 ", integrity-checker=on"
2413 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2416 #ifdef CONFIG_BLK_DEV_ZONED
2421 #ifdef CONFIG_FS_VERITY
2427 pr_info("Btrfs loaded%s\n", options);
2431 static int register_btrfs(void)
2433 return register_filesystem(&btrfs_fs_type);
2436 static void unregister_btrfs(void)
2438 unregister_filesystem(&btrfs_fs_type);
2441 /* Helper structure for long init/exit functions. */
2442 struct init_sequence {
2443 int (*init_func)(void);
2444 /* Can be NULL if the init_func doesn't need cleanup. */
2445 void (*exit_func)(void);
2448 static const struct init_sequence mod_init_seq[] = {
2450 .init_func = btrfs_props_init,
2453 .init_func = btrfs_init_sysfs,
2454 .exit_func = btrfs_exit_sysfs,
2456 .init_func = btrfs_init_compress,
2457 .exit_func = btrfs_exit_compress,
2459 .init_func = btrfs_init_cachep,
2460 .exit_func = btrfs_destroy_cachep,
2462 .init_func = btrfs_transaction_init,
2463 .exit_func = btrfs_transaction_exit,
2465 .init_func = btrfs_ctree_init,
2466 .exit_func = btrfs_ctree_exit,
2468 .init_func = btrfs_free_space_init,
2469 .exit_func = btrfs_free_space_exit,
2471 .init_func = extent_state_init_cachep,
2472 .exit_func = extent_state_free_cachep,
2474 .init_func = extent_buffer_init_cachep,
2475 .exit_func = extent_buffer_free_cachep,
2477 .init_func = btrfs_bioset_init,
2478 .exit_func = btrfs_bioset_exit,
2480 .init_func = extent_map_init,
2481 .exit_func = extent_map_exit,
2483 .init_func = ordered_data_init,
2484 .exit_func = ordered_data_exit,
2486 .init_func = btrfs_delayed_inode_init,
2487 .exit_func = btrfs_delayed_inode_exit,
2489 .init_func = btrfs_auto_defrag_init,
2490 .exit_func = btrfs_auto_defrag_exit,
2492 .init_func = btrfs_delayed_ref_init,
2493 .exit_func = btrfs_delayed_ref_exit,
2495 .init_func = btrfs_prelim_ref_init,
2496 .exit_func = btrfs_prelim_ref_exit,
2498 .init_func = btrfs_interface_init,
2499 .exit_func = btrfs_interface_exit,
2501 .init_func = btrfs_print_mod_info,
2504 .init_func = btrfs_run_sanity_tests,
2507 .init_func = register_btrfs,
2508 .exit_func = unregister_btrfs,
2512 static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
2514 static __always_inline void btrfs_exit_btrfs_fs(void)
2518 for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
2519 if (!mod_init_result[i])
2521 if (mod_init_seq[i].exit_func)
2522 mod_init_seq[i].exit_func();
2523 mod_init_result[i] = false;
2527 static void __exit exit_btrfs_fs(void)
2529 btrfs_exit_btrfs_fs();
2530 btrfs_cleanup_fs_uuids();
2533 static int __init init_btrfs_fs(void)
2538 for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
2539 ASSERT(!mod_init_result[i]);
2540 ret = mod_init_seq[i].init_func();
2542 btrfs_exit_btrfs_fs();
2545 mod_init_result[i] = true;
2550 late_initcall(init_btrfs_fs);
2551 module_exit(exit_btrfs_fs)
2553 MODULE_LICENSE("GPL");
2554 MODULE_SOFTDEP("pre: crc32c");
2555 MODULE_SOFTDEP("pre: xxhash64");
2556 MODULE_SOFTDEP("pre: sha256");
2557 MODULE_SOFTDEP("pre: blake2b-256");