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>
29 #include <linux/security.h>
31 #include "delayed-inode.h"
34 #include "transaction.h"
35 #include "btrfs_inode.h"
36 #include "print-tree.h"
41 #include "compression.h"
42 #include "rcu-string.h"
43 #include "dev-replace.h"
44 #include "free-space-cache.h"
46 #include "space-info.h"
49 #include "tests/btrfs-tests.h"
50 #include "block-group.h"
55 #include "accessors.h"
62 #include "extent-tree.h"
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/btrfs.h>
66 static const struct super_operations btrfs_super_ops;
69 * Types for mounting the default subvolume and a subvolume explicitly
70 * requested by subvol=/path. That way the callchain is straightforward and we
71 * don't have to play tricks with the mount options and recursive calls to
74 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
76 static struct file_system_type btrfs_fs_type;
77 static struct file_system_type btrfs_root_fs_type;
79 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
81 static void btrfs_put_super(struct super_block *sb)
83 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
85 btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
95 Opt_compress_force_type,
100 Opt_flushoncommit, Opt_noflushoncommit,
102 Opt_barrier, Opt_nobarrier,
103 Opt_datacow, Opt_nodatacow,
104 Opt_datasum, Opt_nodatasum,
105 Opt_defrag, Opt_nodefrag,
106 Opt_discard, Opt_nodiscard,
110 Opt_rescan_uuid_tree,
112 Opt_space_cache, Opt_no_space_cache,
113 Opt_space_cache_version,
115 Opt_ssd_spread, Opt_nossd_spread,
120 Opt_treelog, Opt_notreelog,
121 Opt_user_subvol_rm_allowed,
131 /* Deprecated options */
133 Opt_inode_cache, Opt_noinode_cache,
135 /* Debugging options */
136 Opt_enospc_debug, Opt_noenospc_debug,
137 #ifdef CONFIG_BTRFS_DEBUG
138 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
140 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
146 static const match_table_t tokens = {
148 {Opt_noacl, "noacl"},
149 {Opt_clear_cache, "clear_cache"},
150 {Opt_commit_interval, "commit=%u"},
151 {Opt_compress, "compress"},
152 {Opt_compress_type, "compress=%s"},
153 {Opt_compress_force, "compress-force"},
154 {Opt_compress_force_type, "compress-force=%s"},
155 {Opt_degraded, "degraded"},
156 {Opt_device, "device=%s"},
157 {Opt_fatal_errors, "fatal_errors=%s"},
158 {Opt_flushoncommit, "flushoncommit"},
159 {Opt_noflushoncommit, "noflushoncommit"},
160 {Opt_inode_cache, "inode_cache"},
161 {Opt_noinode_cache, "noinode_cache"},
162 {Opt_max_inline, "max_inline=%s"},
163 {Opt_barrier, "barrier"},
164 {Opt_nobarrier, "nobarrier"},
165 {Opt_datacow, "datacow"},
166 {Opt_nodatacow, "nodatacow"},
167 {Opt_datasum, "datasum"},
168 {Opt_nodatasum, "nodatasum"},
169 {Opt_defrag, "autodefrag"},
170 {Opt_nodefrag, "noautodefrag"},
171 {Opt_discard, "discard"},
172 {Opt_discard_mode, "discard=%s"},
173 {Opt_nodiscard, "nodiscard"},
174 {Opt_norecovery, "norecovery"},
175 {Opt_ratio, "metadata_ratio=%u"},
176 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
177 {Opt_skip_balance, "skip_balance"},
178 {Opt_space_cache, "space_cache"},
179 {Opt_no_space_cache, "nospace_cache"},
180 {Opt_space_cache_version, "space_cache=%s"},
182 {Opt_nossd, "nossd"},
183 {Opt_ssd_spread, "ssd_spread"},
184 {Opt_nossd_spread, "nossd_spread"},
185 {Opt_subvol, "subvol=%s"},
186 {Opt_subvol_empty, "subvol="},
187 {Opt_subvolid, "subvolid=%s"},
188 {Opt_thread_pool, "thread_pool=%u"},
189 {Opt_treelog, "treelog"},
190 {Opt_notreelog, "notreelog"},
191 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
194 {Opt_rescue, "rescue=%s"},
195 /* Deprecated, with alias rescue=nologreplay */
196 {Opt_nologreplay, "nologreplay"},
197 /* Deprecated, with alias rescue=usebackuproot */
198 {Opt_usebackuproot, "usebackuproot"},
200 /* Deprecated options */
201 {Opt_recovery, "recovery"},
203 /* Debugging options */
204 {Opt_enospc_debug, "enospc_debug"},
205 {Opt_noenospc_debug, "noenospc_debug"},
206 #ifdef CONFIG_BTRFS_DEBUG
207 {Opt_fragment_data, "fragment=data"},
208 {Opt_fragment_metadata, "fragment=metadata"},
209 {Opt_fragment_all, "fragment=all"},
211 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
212 {Opt_ref_verify, "ref_verify"},
217 static const match_table_t rescue_tokens = {
218 {Opt_usebackuproot, "usebackuproot"},
219 {Opt_nologreplay, "nologreplay"},
220 {Opt_ignorebadroots, "ignorebadroots"},
221 {Opt_ignorebadroots, "ibadroots"},
222 {Opt_ignoredatacsums, "ignoredatacsums"},
223 {Opt_ignoredatacsums, "idatacsums"},
224 {Opt_rescue_all, "all"},
228 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
229 const char *opt_name)
231 if (fs_info->mount_opt & opt) {
232 btrfs_err(fs_info, "%s must be used with ro mount option",
239 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
244 substring_t args[MAX_OPT_ARGS];
247 opts = kstrdup(options, GFP_KERNEL);
252 while ((p = strsep(&opts, ":")) != NULL) {
257 token = match_token(p, rescue_tokens, args);
259 case Opt_usebackuproot:
261 "trying to use backup root at mount time");
262 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
264 case Opt_nologreplay:
265 btrfs_set_and_info(info, NOLOGREPLAY,
266 "disabling log replay at mount time");
268 case Opt_ignorebadroots:
269 btrfs_set_and_info(info, IGNOREBADROOTS,
270 "ignoring bad roots");
272 case Opt_ignoredatacsums:
273 btrfs_set_and_info(info, IGNOREDATACSUMS,
274 "ignoring data csums");
277 btrfs_info(info, "enabling all of the rescue options");
278 btrfs_set_and_info(info, IGNOREDATACSUMS,
279 "ignoring data csums");
280 btrfs_set_and_info(info, IGNOREBADROOTS,
281 "ignoring bad roots");
282 btrfs_set_and_info(info, NOLOGREPLAY,
283 "disabling log replay at mount time");
286 btrfs_info(info, "unrecognized rescue option '%s'", p);
300 * Regular mount options parser. Everything that is needed only when
301 * reading in a new superblock is parsed here.
302 * XXX JDM: This needs to be cleaned up for remount.
304 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
305 unsigned long new_flags)
307 substring_t args[MAX_OPT_ARGS];
312 bool compress_force = false;
313 enum btrfs_compression_type saved_compress_type;
314 int saved_compress_level;
315 bool saved_compress_force;
317 const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
319 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
320 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
321 else if (btrfs_free_space_cache_v1_active(info)) {
322 if (btrfs_is_zoned(info)) {
324 "zoned: clearing existing space cache");
325 btrfs_set_super_cache_generation(info->super_copy, 0);
327 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
332 * Even the options are empty, we still need to do extra check
338 while ((p = strsep(&options, ",")) != NULL) {
343 token = match_token(p, tokens, args);
346 btrfs_info(info, "allowing degraded mounts");
347 btrfs_set_opt(info->mount_opt, DEGRADED);
350 case Opt_subvol_empty:
354 * These are parsed by btrfs_parse_subvol_options or
355 * btrfs_parse_device_options and can be ignored here.
359 btrfs_set_and_info(info, NODATASUM,
360 "setting nodatasum");
363 if (btrfs_test_opt(info, NODATASUM)) {
364 if (btrfs_test_opt(info, NODATACOW))
366 "setting datasum, datacow enabled");
368 btrfs_info(info, "setting datasum");
370 btrfs_clear_opt(info->mount_opt, NODATACOW);
371 btrfs_clear_opt(info->mount_opt, NODATASUM);
374 if (!btrfs_test_opt(info, NODATACOW)) {
375 if (!btrfs_test_opt(info, COMPRESS) ||
376 !btrfs_test_opt(info, FORCE_COMPRESS)) {
378 "setting nodatacow, compression disabled");
380 btrfs_info(info, "setting nodatacow");
383 btrfs_clear_opt(info->mount_opt, COMPRESS);
384 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
385 btrfs_set_opt(info->mount_opt, NODATACOW);
386 btrfs_set_opt(info->mount_opt, NODATASUM);
389 btrfs_clear_and_info(info, NODATACOW,
392 case Opt_compress_force:
393 case Opt_compress_force_type:
394 compress_force = true;
397 case Opt_compress_type:
398 saved_compress_type = btrfs_test_opt(info,
400 info->compress_type : BTRFS_COMPRESS_NONE;
401 saved_compress_force =
402 btrfs_test_opt(info, FORCE_COMPRESS);
403 saved_compress_level = info->compress_level;
404 if (token == Opt_compress ||
405 token == Opt_compress_force ||
406 strncmp(args[0].from, "zlib", 4) == 0) {
407 compress_type = "zlib";
409 info->compress_type = BTRFS_COMPRESS_ZLIB;
410 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
412 * args[0] contains uninitialized data since
413 * for these tokens we don't expect any
416 if (token != Opt_compress &&
417 token != Opt_compress_force)
418 info->compress_level =
419 btrfs_compress_str2level(
422 btrfs_set_opt(info->mount_opt, COMPRESS);
423 btrfs_clear_opt(info->mount_opt, NODATACOW);
424 btrfs_clear_opt(info->mount_opt, NODATASUM);
426 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
427 compress_type = "lzo";
428 info->compress_type = BTRFS_COMPRESS_LZO;
429 info->compress_level = 0;
430 btrfs_set_opt(info->mount_opt, COMPRESS);
431 btrfs_clear_opt(info->mount_opt, NODATACOW);
432 btrfs_clear_opt(info->mount_opt, NODATASUM);
433 btrfs_set_fs_incompat(info, COMPRESS_LZO);
435 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
436 compress_type = "zstd";
437 info->compress_type = BTRFS_COMPRESS_ZSTD;
438 info->compress_level =
439 btrfs_compress_str2level(
442 btrfs_set_opt(info->mount_opt, COMPRESS);
443 btrfs_clear_opt(info->mount_opt, NODATACOW);
444 btrfs_clear_opt(info->mount_opt, NODATASUM);
445 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
447 } else if (strncmp(args[0].from, "no", 2) == 0) {
448 compress_type = "no";
449 info->compress_level = 0;
450 info->compress_type = 0;
451 btrfs_clear_opt(info->mount_opt, COMPRESS);
452 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
453 compress_force = false;
456 btrfs_err(info, "unrecognized compression value %s",
462 if (compress_force) {
463 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
466 * If we remount from compress-force=xxx to
467 * compress=xxx, we need clear FORCE_COMPRESS
468 * flag, otherwise, there is no way for users
469 * to disable forcible compression separately.
471 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
473 if (no_compress == 1) {
474 btrfs_info(info, "use no compression");
475 } else if ((info->compress_type != saved_compress_type) ||
476 (compress_force != saved_compress_force) ||
477 (info->compress_level != saved_compress_level)) {
478 btrfs_info(info, "%s %s compression, level %d",
479 (compress_force) ? "force" : "use",
480 compress_type, info->compress_level);
482 compress_force = false;
485 btrfs_set_and_info(info, SSD,
486 "enabling ssd optimizations");
487 btrfs_clear_opt(info->mount_opt, NOSSD);
490 btrfs_set_and_info(info, SSD,
491 "enabling ssd optimizations");
492 btrfs_set_and_info(info, SSD_SPREAD,
493 "using spread ssd allocation scheme");
494 btrfs_clear_opt(info->mount_opt, NOSSD);
497 btrfs_set_opt(info->mount_opt, NOSSD);
498 btrfs_clear_and_info(info, SSD,
499 "not using ssd optimizations");
501 case Opt_nossd_spread:
502 btrfs_clear_and_info(info, SSD_SPREAD,
503 "not using spread ssd allocation scheme");
506 btrfs_clear_and_info(info, NOBARRIER,
507 "turning on barriers");
510 btrfs_set_and_info(info, NOBARRIER,
511 "turning off barriers");
513 case Opt_thread_pool:
514 ret = match_int(&args[0], &intarg);
516 btrfs_err(info, "unrecognized thread_pool value %s",
519 } else if (intarg == 0) {
520 btrfs_err(info, "invalid value 0 for thread_pool");
524 info->thread_pool_size = intarg;
527 num = match_strdup(&args[0]);
529 info->max_inline = memparse(num, NULL);
532 if (info->max_inline) {
533 info->max_inline = min_t(u64,
537 btrfs_info(info, "max_inline at %llu",
545 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
546 info->sb->s_flags |= SB_POSIXACL;
549 btrfs_err(info, "support for ACL not compiled in!");
554 info->sb->s_flags &= ~SB_POSIXACL;
557 btrfs_set_and_info(info, NOTREELOG,
558 "disabling tree log");
561 btrfs_clear_and_info(info, NOTREELOG,
562 "enabling tree log");
565 case Opt_nologreplay:
567 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
568 btrfs_set_and_info(info, NOLOGREPLAY,
569 "disabling log replay at mount time");
571 case Opt_flushoncommit:
572 btrfs_set_and_info(info, FLUSHONCOMMIT,
573 "turning on flush-on-commit");
575 case Opt_noflushoncommit:
576 btrfs_clear_and_info(info, FLUSHONCOMMIT,
577 "turning off flush-on-commit");
580 ret = match_int(&args[0], &intarg);
582 btrfs_err(info, "unrecognized metadata_ratio value %s",
586 info->metadata_ratio = intarg;
587 btrfs_info(info, "metadata ratio %u",
588 info->metadata_ratio);
591 case Opt_discard_mode:
592 if (token == Opt_discard ||
593 strcmp(args[0].from, "sync") == 0) {
594 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
595 btrfs_set_and_info(info, DISCARD_SYNC,
596 "turning on sync discard");
597 } else if (strcmp(args[0].from, "async") == 0) {
598 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
599 btrfs_set_and_info(info, DISCARD_ASYNC,
600 "turning on async discard");
602 btrfs_err(info, "unrecognized discard mode value %s",
607 btrfs_clear_opt(info->mount_opt, NODISCARD);
610 btrfs_clear_and_info(info, DISCARD_SYNC,
611 "turning off discard");
612 btrfs_clear_and_info(info, DISCARD_ASYNC,
613 "turning off async discard");
614 btrfs_set_opt(info->mount_opt, NODISCARD);
616 case Opt_space_cache:
617 case Opt_space_cache_version:
619 * We already set FREE_SPACE_TREE above because we have
620 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
621 * to allow v1 to be set for extent tree v2, simply
622 * ignore this setting if we're extent tree v2.
624 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
626 if (token == Opt_space_cache ||
627 strcmp(args[0].from, "v1") == 0) {
628 btrfs_clear_opt(info->mount_opt,
630 btrfs_set_and_info(info, SPACE_CACHE,
631 "enabling disk space caching");
632 } else if (strcmp(args[0].from, "v2") == 0) {
633 btrfs_clear_opt(info->mount_opt,
635 btrfs_set_and_info(info, FREE_SPACE_TREE,
636 "enabling free space tree");
638 btrfs_err(info, "unrecognized space_cache value %s",
644 case Opt_rescan_uuid_tree:
645 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
647 case Opt_no_space_cache:
649 * We cannot operate without the free space tree with
650 * extent tree v2, ignore this option.
652 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
654 if (btrfs_test_opt(info, SPACE_CACHE)) {
655 btrfs_clear_and_info(info, SPACE_CACHE,
656 "disabling disk space caching");
658 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
659 btrfs_clear_and_info(info, FREE_SPACE_TREE,
660 "disabling free space tree");
663 case Opt_inode_cache:
664 case Opt_noinode_cache:
666 "the 'inode_cache' option is deprecated and has no effect since 5.11");
668 case Opt_clear_cache:
670 * We cannot clear the free space tree with extent tree
671 * v2, ignore this option.
673 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
675 btrfs_set_and_info(info, CLEAR_CACHE,
676 "force clearing of disk cache");
678 case Opt_user_subvol_rm_allowed:
679 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
681 case Opt_enospc_debug:
682 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
684 case Opt_noenospc_debug:
685 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
688 btrfs_set_and_info(info, AUTO_DEFRAG,
689 "enabling auto defrag");
692 btrfs_clear_and_info(info, AUTO_DEFRAG,
693 "disabling auto defrag");
696 case Opt_usebackuproot:
698 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
699 token == Opt_recovery ? "recovery" :
702 "trying to use backup root at mount time");
703 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
705 case Opt_skip_balance:
706 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
708 case Opt_fatal_errors:
709 if (strcmp(args[0].from, "panic") == 0) {
710 btrfs_set_opt(info->mount_opt,
711 PANIC_ON_FATAL_ERROR);
712 } else if (strcmp(args[0].from, "bug") == 0) {
713 btrfs_clear_opt(info->mount_opt,
714 PANIC_ON_FATAL_ERROR);
716 btrfs_err(info, "unrecognized fatal_errors value %s",
722 case Opt_commit_interval:
724 ret = match_int(&args[0], &intarg);
726 btrfs_err(info, "unrecognized commit_interval value %s",
733 "using default commit interval %us",
734 BTRFS_DEFAULT_COMMIT_INTERVAL);
735 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
736 } else if (intarg > 300) {
737 btrfs_warn(info, "excessive commit interval %d",
740 info->commit_interval = intarg;
743 ret = parse_rescue_options(info, args[0].from);
745 btrfs_err(info, "unrecognized rescue value %s",
750 #ifdef CONFIG_BTRFS_DEBUG
751 case Opt_fragment_all:
752 btrfs_info(info, "fragmenting all space");
753 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
754 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
756 case Opt_fragment_metadata:
757 btrfs_info(info, "fragmenting metadata");
758 btrfs_set_opt(info->mount_opt,
761 case Opt_fragment_data:
762 btrfs_info(info, "fragmenting data");
763 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
766 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
768 btrfs_info(info, "doing ref verification");
769 btrfs_set_opt(info->mount_opt, REF_VERIFY);
773 btrfs_err(info, "unrecognized mount option '%s'", p);
781 /* We're read-only, don't have to check. */
782 if (new_flags & SB_RDONLY)
785 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
786 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
787 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
790 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
791 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
792 !btrfs_test_opt(info, CLEAR_CACHE)) {
793 btrfs_err(info, "cannot disable free space tree");
796 if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
797 !btrfs_test_opt(info, FREE_SPACE_TREE)) {
798 btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
802 ret = btrfs_check_mountopts_zoned(info);
803 if (!ret && !remounting) {
804 if (btrfs_test_opt(info, SPACE_CACHE))
805 btrfs_info(info, "disk space caching is enabled");
806 if (btrfs_test_opt(info, FREE_SPACE_TREE))
807 btrfs_info(info, "using free space tree");
813 * Parse mount options that are required early in the mount process.
815 * All other options will be parsed on much later in the mount process and
816 * only when we need to allocate a new super block.
818 static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
820 substring_t args[MAX_OPT_ARGS];
821 char *device_name, *opts, *orig, *p;
822 struct btrfs_device *device = NULL;
825 lockdep_assert_held(&uuid_mutex);
831 * strsep changes the string, duplicate it because btrfs_parse_options
834 opts = kstrdup(options, GFP_KERNEL);
839 while ((p = strsep(&opts, ",")) != NULL) {
845 token = match_token(p, tokens, args);
846 if (token == Opt_device) {
847 device_name = match_strdup(&args[0]);
852 device = btrfs_scan_one_device(device_name, flags, false);
854 if (IS_ERR(device)) {
855 error = PTR_ERR(device);
867 * Parse mount options that are related to subvolume id
869 * The value is later passed to mount_subvol()
871 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
872 u64 *subvol_objectid)
874 substring_t args[MAX_OPT_ARGS];
875 char *opts, *orig, *p;
883 * strsep changes the string, duplicate it because
884 * btrfs_parse_device_options gets called later
886 opts = kstrdup(options, GFP_KERNEL);
891 while ((p = strsep(&opts, ",")) != NULL) {
896 token = match_token(p, tokens, args);
900 *subvol_name = match_strdup(&args[0]);
907 error = match_u64(&args[0], &subvolid);
911 /* we want the original fs_tree */
913 subvolid = BTRFS_FS_TREE_OBJECTID;
915 *subvol_objectid = subvolid;
927 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
930 struct btrfs_root *root = fs_info->tree_root;
931 struct btrfs_root *fs_root = NULL;
932 struct btrfs_root_ref *root_ref;
933 struct btrfs_inode_ref *inode_ref;
934 struct btrfs_key key;
935 struct btrfs_path *path = NULL;
936 char *name = NULL, *ptr;
941 path = btrfs_alloc_path();
947 name = kmalloc(PATH_MAX, GFP_KERNEL);
952 ptr = name + PATH_MAX - 1;
956 * Walk up the subvolume trees in the tree of tree roots by root
957 * backrefs until we hit the top-level subvolume.
959 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
960 key.objectid = subvol_objectid;
961 key.type = BTRFS_ROOT_BACKREF_KEY;
962 key.offset = (u64)-1;
964 ret = btrfs_search_backwards(root, &key, path);
967 } else if (ret > 0) {
972 subvol_objectid = key.offset;
974 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
975 struct btrfs_root_ref);
976 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
982 read_extent_buffer(path->nodes[0], ptr + 1,
983 (unsigned long)(root_ref + 1), len);
985 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
986 btrfs_release_path(path);
988 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
989 if (IS_ERR(fs_root)) {
990 ret = PTR_ERR(fs_root);
996 * Walk up the filesystem tree by inode refs until we hit the
999 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1000 key.objectid = dirid;
1001 key.type = BTRFS_INODE_REF_KEY;
1002 key.offset = (u64)-1;
1004 ret = btrfs_search_backwards(fs_root, &key, path);
1007 } else if (ret > 0) {
1014 inode_ref = btrfs_item_ptr(path->nodes[0],
1016 struct btrfs_inode_ref);
1017 len = btrfs_inode_ref_name_len(path->nodes[0],
1021 ret = -ENAMETOOLONG;
1024 read_extent_buffer(path->nodes[0], ptr + 1,
1025 (unsigned long)(inode_ref + 1), len);
1027 btrfs_release_path(path);
1029 btrfs_put_root(fs_root);
1033 btrfs_free_path(path);
1034 if (ptr == name + PATH_MAX - 1) {
1038 memmove(name, ptr, name + PATH_MAX - ptr);
1043 btrfs_put_root(fs_root);
1044 btrfs_free_path(path);
1046 return ERR_PTR(ret);
1049 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1051 struct btrfs_root *root = fs_info->tree_root;
1052 struct btrfs_dir_item *di;
1053 struct btrfs_path *path;
1054 struct btrfs_key location;
1055 struct fscrypt_str name = FSTR_INIT("default", 7);
1058 path = btrfs_alloc_path();
1063 * Find the "default" dir item which points to the root item that we
1064 * will mount by default if we haven't been given a specific subvolume
1067 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1068 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
1070 btrfs_free_path(path);
1075 * Ok the default dir item isn't there. This is weird since
1076 * it's always been there, but don't freak out, just try and
1077 * mount the top-level subvolume.
1079 btrfs_free_path(path);
1080 *objectid = BTRFS_FS_TREE_OBJECTID;
1084 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1085 btrfs_free_path(path);
1086 *objectid = location.objectid;
1090 static int btrfs_fill_super(struct super_block *sb,
1091 struct btrfs_fs_devices *fs_devices,
1094 struct inode *inode;
1095 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1098 sb->s_maxbytes = MAX_LFS_FILESIZE;
1099 sb->s_magic = BTRFS_SUPER_MAGIC;
1100 sb->s_op = &btrfs_super_ops;
1101 sb->s_d_op = &btrfs_dentry_operations;
1102 sb->s_export_op = &btrfs_export_ops;
1103 #ifdef CONFIG_FS_VERITY
1104 sb->s_vop = &btrfs_verityops;
1106 sb->s_xattr = btrfs_xattr_handlers;
1107 sb->s_time_gran = 1;
1108 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1109 sb->s_flags |= SB_POSIXACL;
1111 sb->s_flags |= SB_I_VERSION;
1112 sb->s_iflags |= SB_I_CGROUPWB;
1114 err = super_setup_bdi(sb);
1116 btrfs_err(fs_info, "super_setup_bdi failed");
1120 err = open_ctree(sb, fs_devices, (char *)data);
1122 btrfs_err(fs_info, "open_ctree failed");
1126 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1127 if (IS_ERR(inode)) {
1128 err = PTR_ERR(inode);
1129 btrfs_handle_fs_error(fs_info, err, NULL);
1133 sb->s_root = d_make_root(inode);
1139 sb->s_flags |= SB_ACTIVE;
1143 close_ctree(fs_info);
1147 int btrfs_sync_fs(struct super_block *sb, int wait)
1149 struct btrfs_trans_handle *trans;
1150 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1151 struct btrfs_root *root = fs_info->tree_root;
1153 trace_btrfs_sync_fs(fs_info, wait);
1156 filemap_flush(fs_info->btree_inode->i_mapping);
1160 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1162 trans = btrfs_attach_transaction_barrier(root);
1163 if (IS_ERR(trans)) {
1164 /* no transaction, don't bother */
1165 if (PTR_ERR(trans) == -ENOENT) {
1167 * Exit unless we have some pending changes
1168 * that need to go through commit
1170 if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
1174 * A non-blocking test if the fs is frozen. We must not
1175 * start a new transaction here otherwise a deadlock
1176 * happens. The pending operations are delayed to the
1177 * next commit after thawing.
1179 if (sb_start_write_trylock(sb))
1183 trans = btrfs_start_transaction(root, 0);
1186 return PTR_ERR(trans);
1188 return btrfs_commit_transaction(trans);
1191 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1193 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1197 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1199 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1200 const char *compress_type;
1201 const char *subvol_name;
1202 bool printed = false;
1204 if (btrfs_test_opt(info, DEGRADED))
1205 seq_puts(seq, ",degraded");
1206 if (btrfs_test_opt(info, NODATASUM))
1207 seq_puts(seq, ",nodatasum");
1208 if (btrfs_test_opt(info, NODATACOW))
1209 seq_puts(seq, ",nodatacow");
1210 if (btrfs_test_opt(info, NOBARRIER))
1211 seq_puts(seq, ",nobarrier");
1212 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1213 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1214 if (info->thread_pool_size != min_t(unsigned long,
1215 num_online_cpus() + 2, 8))
1216 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1217 if (btrfs_test_opt(info, COMPRESS)) {
1218 compress_type = btrfs_compress_type2str(info->compress_type);
1219 if (btrfs_test_opt(info, FORCE_COMPRESS))
1220 seq_printf(seq, ",compress-force=%s", compress_type);
1222 seq_printf(seq, ",compress=%s", compress_type);
1223 if (info->compress_level)
1224 seq_printf(seq, ":%d", info->compress_level);
1226 if (btrfs_test_opt(info, NOSSD))
1227 seq_puts(seq, ",nossd");
1228 if (btrfs_test_opt(info, SSD_SPREAD))
1229 seq_puts(seq, ",ssd_spread");
1230 else if (btrfs_test_opt(info, SSD))
1231 seq_puts(seq, ",ssd");
1232 if (btrfs_test_opt(info, NOTREELOG))
1233 seq_puts(seq, ",notreelog");
1234 if (btrfs_test_opt(info, NOLOGREPLAY))
1235 print_rescue_option(seq, "nologreplay", &printed);
1236 if (btrfs_test_opt(info, USEBACKUPROOT))
1237 print_rescue_option(seq, "usebackuproot", &printed);
1238 if (btrfs_test_opt(info, IGNOREBADROOTS))
1239 print_rescue_option(seq, "ignorebadroots", &printed);
1240 if (btrfs_test_opt(info, IGNOREDATACSUMS))
1241 print_rescue_option(seq, "ignoredatacsums", &printed);
1242 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1243 seq_puts(seq, ",flushoncommit");
1244 if (btrfs_test_opt(info, DISCARD_SYNC))
1245 seq_puts(seq, ",discard");
1246 if (btrfs_test_opt(info, DISCARD_ASYNC))
1247 seq_puts(seq, ",discard=async");
1248 if (!(info->sb->s_flags & SB_POSIXACL))
1249 seq_puts(seq, ",noacl");
1250 if (btrfs_free_space_cache_v1_active(info))
1251 seq_puts(seq, ",space_cache");
1252 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
1253 seq_puts(seq, ",space_cache=v2");
1255 seq_puts(seq, ",nospace_cache");
1256 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1257 seq_puts(seq, ",rescan_uuid_tree");
1258 if (btrfs_test_opt(info, CLEAR_CACHE))
1259 seq_puts(seq, ",clear_cache");
1260 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1261 seq_puts(seq, ",user_subvol_rm_allowed");
1262 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1263 seq_puts(seq, ",enospc_debug");
1264 if (btrfs_test_opt(info, AUTO_DEFRAG))
1265 seq_puts(seq, ",autodefrag");
1266 if (btrfs_test_opt(info, SKIP_BALANCE))
1267 seq_puts(seq, ",skip_balance");
1268 if (info->metadata_ratio)
1269 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1270 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1271 seq_puts(seq, ",fatal_errors=panic");
1272 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1273 seq_printf(seq, ",commit=%u", info->commit_interval);
1274 #ifdef CONFIG_BTRFS_DEBUG
1275 if (btrfs_test_opt(info, FRAGMENT_DATA))
1276 seq_puts(seq, ",fragment=data");
1277 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1278 seq_puts(seq, ",fragment=metadata");
1280 if (btrfs_test_opt(info, REF_VERIFY))
1281 seq_puts(seq, ",ref_verify");
1282 seq_printf(seq, ",subvolid=%llu",
1283 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1284 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1285 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1286 if (!IS_ERR(subvol_name)) {
1287 seq_puts(seq, ",subvol=");
1288 seq_escape(seq, subvol_name, " \t\n\\");
1294 static int btrfs_test_super(struct super_block *s, void *data)
1296 struct btrfs_fs_info *p = data;
1297 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1299 return fs_info->fs_devices == p->fs_devices;
1302 static int btrfs_set_super(struct super_block *s, void *data)
1304 int err = set_anon_super(s, data);
1306 s->s_fs_info = data;
1311 * subvolumes are identified by ino 256
1313 static inline int is_subvolume_inode(struct inode *inode)
1315 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1320 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1321 struct vfsmount *mnt)
1323 struct dentry *root;
1327 if (!subvol_objectid) {
1328 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1331 root = ERR_PTR(ret);
1335 subvol_name = btrfs_get_subvol_name_from_objectid(
1336 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1337 if (IS_ERR(subvol_name)) {
1338 root = ERR_CAST(subvol_name);
1345 root = mount_subtree(mnt, subvol_name);
1346 /* mount_subtree() drops our reference on the vfsmount. */
1349 if (!IS_ERR(root)) {
1350 struct super_block *s = root->d_sb;
1351 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1352 struct inode *root_inode = d_inode(root);
1353 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1356 if (!is_subvolume_inode(root_inode)) {
1357 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1361 if (subvol_objectid && root_objectid != subvol_objectid) {
1363 * This will also catch a race condition where a
1364 * subvolume which was passed by ID is renamed and
1365 * another subvolume is renamed over the old location.
1368 "subvol '%s' does not match subvolid %llu",
1369 subvol_name, subvol_objectid);
1374 root = ERR_PTR(ret);
1375 deactivate_locked_super(s);
1386 * Find a superblock for the given device / mount point.
1388 * Note: This is based on mount_bdev from fs/super.c with a few additions
1389 * for multiple device setup. Make sure to keep it in sync.
1391 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1392 int flags, const char *device_name, void *data)
1394 struct block_device *bdev = NULL;
1395 struct super_block *s;
1396 struct btrfs_device *device = NULL;
1397 struct btrfs_fs_devices *fs_devices = NULL;
1398 struct btrfs_fs_info *fs_info = NULL;
1399 void *new_sec_opts = NULL;
1400 blk_mode_t mode = sb_open_mode(flags);
1404 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1406 return ERR_PTR(error);
1409 /* No support for restricting writes to btrfs devices yet... */
1410 mode &= ~BLK_OPEN_RESTRICT_WRITES;
1412 * Setup a dummy root and fs_info for test/set super. This is because
1413 * we don't actually fill this stuff out until open_ctree, but we need
1414 * then open_ctree will properly initialize the file system specific
1415 * settings later. btrfs_init_fs_info initializes the static elements
1416 * of the fs_info (locks and such) to make cleanup easier if we find a
1417 * superblock with our given fs_devices later on at sget() time.
1419 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1422 goto error_sec_opts;
1424 btrfs_init_fs_info(fs_info);
1426 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1427 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1428 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1433 mutex_lock(&uuid_mutex);
1434 error = btrfs_parse_device_options(data, mode);
1436 mutex_unlock(&uuid_mutex);
1441 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
1442 * either a valid device or an error.
1444 device = btrfs_scan_one_device(device_name, mode, true);
1445 ASSERT(device != NULL);
1446 if (IS_ERR(device)) {
1447 mutex_unlock(&uuid_mutex);
1448 error = PTR_ERR(device);
1452 fs_devices = device->fs_devices;
1453 fs_info->fs_devices = fs_devices;
1455 error = btrfs_open_devices(fs_devices, mode, fs_type);
1456 mutex_unlock(&uuid_mutex);
1460 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1462 goto error_close_devices;
1465 bdev = fs_devices->latest_dev->bdev;
1466 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1470 goto error_close_devices;
1474 btrfs_close_devices(fs_devices);
1475 btrfs_free_fs_info(fs_info);
1476 if ((flags ^ s->s_flags) & SB_RDONLY)
1479 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1480 shrinker_debugfs_rename(s->s_shrink, "sb-%s:%s", fs_type->name,
1482 btrfs_sb(s)->bdev_holder = fs_type;
1483 error = btrfs_fill_super(s, fs_devices, data);
1486 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1487 security_free_mnt_opts(&new_sec_opts);
1489 deactivate_locked_super(s);
1490 return ERR_PTR(error);
1493 return dget(s->s_root);
1495 error_close_devices:
1496 btrfs_close_devices(fs_devices);
1498 btrfs_free_fs_info(fs_info);
1500 security_free_mnt_opts(&new_sec_opts);
1501 return ERR_PTR(error);
1505 * Mount function which is called by VFS layer.
1507 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1508 * which needs vfsmount* of device's root (/). This means device's root has to
1509 * be mounted internally in any case.
1512 * 1. Parse subvol id related options for later use in mount_subvol().
1514 * 2. Mount device's root (/) by calling vfs_kern_mount().
1516 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1517 * first place. In order to avoid calling btrfs_mount() again, we use
1518 * different file_system_type which is not registered to VFS by
1519 * register_filesystem() (btrfs_root_fs_type). As a result,
1520 * btrfs_mount_root() is called. The return value will be used by
1521 * mount_subtree() in mount_subvol().
1523 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1524 * "btrfs subvolume set-default", mount_subvol() is called always.
1526 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1527 const char *device_name, void *data)
1529 struct vfsmount *mnt_root;
1530 struct dentry *root;
1531 char *subvol_name = NULL;
1532 u64 subvol_objectid = 0;
1535 error = btrfs_parse_subvol_options(data, &subvol_name,
1539 return ERR_PTR(error);
1542 /* mount device's root (/) */
1543 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1544 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1545 if (flags & SB_RDONLY) {
1546 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1547 flags & ~SB_RDONLY, device_name, data);
1549 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1550 flags | SB_RDONLY, device_name, data);
1551 if (IS_ERR(mnt_root)) {
1552 root = ERR_CAST(mnt_root);
1557 down_write(&mnt_root->mnt_sb->s_umount);
1558 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1559 up_write(&mnt_root->mnt_sb->s_umount);
1561 root = ERR_PTR(error);
1568 if (IS_ERR(mnt_root)) {
1569 root = ERR_CAST(mnt_root);
1574 /* mount_subvol() will free subvol_name and mnt_root */
1575 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1581 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1582 u32 new_pool_size, u32 old_pool_size)
1584 if (new_pool_size == old_pool_size)
1587 fs_info->thread_pool_size = new_pool_size;
1589 btrfs_info(fs_info, "resize thread pool %d -> %d",
1590 old_pool_size, new_pool_size);
1592 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1593 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1594 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1595 workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1596 workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1597 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1598 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1599 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1602 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1603 unsigned long old_opts, int flags)
1605 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1606 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1607 (flags & SB_RDONLY))) {
1608 /* wait for any defraggers to finish */
1609 wait_event(fs_info->transaction_wait,
1610 (atomic_read(&fs_info->defrag_running) == 0));
1611 if (flags & SB_RDONLY)
1612 sync_filesystem(fs_info->sb);
1616 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1617 unsigned long old_opts)
1619 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1622 * We need to cleanup all defragable inodes if the autodefragment is
1623 * close or the filesystem is read only.
1625 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1626 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1627 btrfs_cleanup_defrag_inodes(fs_info);
1630 /* If we toggled discard async */
1631 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1632 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1633 btrfs_discard_resume(fs_info);
1634 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1635 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1636 btrfs_discard_cleanup(fs_info);
1638 /* If we toggled space cache */
1639 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1640 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1643 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1645 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1646 unsigned old_flags = sb->s_flags;
1647 unsigned long old_opts = fs_info->mount_opt;
1648 unsigned long old_compress_type = fs_info->compress_type;
1649 u64 old_max_inline = fs_info->max_inline;
1650 u32 old_thread_pool_size = fs_info->thread_pool_size;
1651 u32 old_metadata_ratio = fs_info->metadata_ratio;
1654 sync_filesystem(sb);
1655 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1658 void *new_sec_opts = NULL;
1660 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1662 ret = security_sb_remount(sb, new_sec_opts);
1663 security_free_mnt_opts(&new_sec_opts);
1668 ret = btrfs_parse_options(fs_info, data, *flags);
1672 ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
1676 btrfs_remount_begin(fs_info, old_opts, *flags);
1677 btrfs_resize_thread_pool(fs_info,
1678 fs_info->thread_pool_size, old_thread_pool_size);
1680 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1681 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1682 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
1684 "remount supports changing free space tree only from ro to rw");
1685 /* Make sure free space cache options match the state on disk */
1686 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1687 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1688 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
1690 if (btrfs_free_space_cache_v1_active(fs_info)) {
1691 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1692 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
1696 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1699 if (*flags & SB_RDONLY) {
1701 * this also happens on 'umount -rf' or on shutdown, when
1702 * the filesystem is busy.
1704 cancel_work_sync(&fs_info->async_reclaim_work);
1705 cancel_work_sync(&fs_info->async_data_reclaim_work);
1707 btrfs_discard_cleanup(fs_info);
1709 /* wait for the uuid_scan task to finish */
1710 down(&fs_info->uuid_tree_rescan_sem);
1711 /* avoid complains from lockdep et al. */
1712 up(&fs_info->uuid_tree_rescan_sem);
1714 btrfs_set_sb_rdonly(sb);
1717 * Setting SB_RDONLY will put the cleaner thread to
1718 * sleep at the next loop if it's already active.
1719 * If it's already asleep, we'll leave unused block
1720 * groups on disk until we're mounted read-write again
1721 * unless we clean them up here.
1723 btrfs_delete_unused_bgs(fs_info);
1726 * The cleaner task could be already running before we set the
1727 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1728 * We must make sure that after we finish the remount, i.e. after
1729 * we call btrfs_commit_super(), the cleaner can no longer start
1730 * a transaction - either because it was dropping a dead root,
1731 * running delayed iputs or deleting an unused block group (the
1732 * cleaner picked a block group from the list of unused block
1733 * groups before we were able to in the previous call to
1734 * btrfs_delete_unused_bgs()).
1736 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1737 TASK_UNINTERRUPTIBLE);
1740 * We've set the superblock to RO mode, so we might have made
1741 * the cleaner task sleep without running all pending delayed
1742 * iputs. Go through all the delayed iputs here, so that if an
1743 * unmount happens without remounting RW we don't end up at
1744 * finishing close_ctree() with a non-empty list of delayed
1747 btrfs_run_delayed_iputs(fs_info);
1749 btrfs_dev_replace_suspend_for_unmount(fs_info);
1750 btrfs_scrub_cancel(fs_info);
1751 btrfs_pause_balance(fs_info);
1754 * Pause the qgroup rescan worker if it is running. We don't want
1755 * it to be still running after we are in RO mode, as after that,
1756 * by the time we unmount, it might have left a transaction open,
1757 * so we would leak the transaction and/or crash.
1759 btrfs_qgroup_wait_for_completion(fs_info, false);
1761 ret = btrfs_commit_super(fs_info);
1765 if (BTRFS_FS_ERROR(fs_info)) {
1767 "Remounting read-write after error is not allowed");
1771 if (fs_info->fs_devices->rw_devices == 0) {
1776 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1778 "too many missing devices, writable remount is not allowed");
1783 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1785 "mount required to replay tree-log, cannot remount read-write");
1791 * NOTE: when remounting with a change that does writes, don't
1792 * put it anywhere above this point, as we are not sure to be
1793 * safe to write until we pass the above checks.
1795 ret = btrfs_start_pre_rw_mount(fs_info);
1799 btrfs_clear_sb_rdonly(sb);
1801 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1804 * If we've gone from readonly -> read/write, we need to get
1805 * our sync/async discard lists in the right state.
1807 btrfs_discard_resume(fs_info);
1811 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1812 * since the absence of the flag means it can be toggled off by remount.
1814 *flags |= SB_I_VERSION;
1816 wake_up_process(fs_info->transaction_kthread);
1817 btrfs_remount_cleanup(fs_info, old_opts);
1818 btrfs_clear_oneshot_options(fs_info);
1819 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1824 /* We've hit an error - don't reset SB_RDONLY */
1826 old_flags |= SB_RDONLY;
1827 if (!(old_flags & SB_RDONLY))
1828 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
1829 sb->s_flags = old_flags;
1830 fs_info->mount_opt = old_opts;
1831 fs_info->compress_type = old_compress_type;
1832 fs_info->max_inline = old_max_inline;
1833 btrfs_resize_thread_pool(fs_info,
1834 old_thread_pool_size, fs_info->thread_pool_size);
1835 fs_info->metadata_ratio = old_metadata_ratio;
1836 btrfs_remount_cleanup(fs_info, old_opts);
1837 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1842 /* Used to sort the devices by max_avail(descending sort) */
1843 static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1845 const struct btrfs_device_info *dev_info1 = a;
1846 const struct btrfs_device_info *dev_info2 = b;
1848 if (dev_info1->max_avail > dev_info2->max_avail)
1850 else if (dev_info1->max_avail < dev_info2->max_avail)
1856 * sort the devices by max_avail, in which max free extent size of each device
1857 * is stored.(Descending Sort)
1859 static inline void btrfs_descending_sort_devices(
1860 struct btrfs_device_info *devices,
1863 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1864 btrfs_cmp_device_free_bytes, NULL);
1868 * The helper to calc the free space on the devices that can be used to store
1871 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
1874 struct btrfs_device_info *devices_info;
1875 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1876 struct btrfs_device *device;
1879 u64 min_stripe_size;
1880 int num_stripes = 1;
1881 int i = 0, nr_devices;
1882 const struct btrfs_raid_attr *rattr;
1885 * We aren't under the device list lock, so this is racy-ish, but good
1886 * enough for our purposes.
1888 nr_devices = fs_info->fs_devices->open_devices;
1891 nr_devices = fs_info->fs_devices->open_devices;
1899 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
1904 /* calc min stripe number for data space allocation */
1905 type = btrfs_data_alloc_profile(fs_info);
1906 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
1908 if (type & BTRFS_BLOCK_GROUP_RAID0)
1909 num_stripes = nr_devices;
1910 else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1911 num_stripes = rattr->ncopies;
1912 else if (type & BTRFS_BLOCK_GROUP_RAID10)
1915 /* Adjust for more than 1 stripe per device */
1916 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
1919 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1920 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1921 &device->dev_state) ||
1923 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1926 if (i >= nr_devices)
1929 avail_space = device->total_bytes - device->bytes_used;
1931 /* align with stripe_len */
1932 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
1935 * Ensure we have at least min_stripe_size on top of the
1936 * reserved space on the device.
1938 if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
1941 avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1943 devices_info[i].dev = device;
1944 devices_info[i].max_avail = avail_space;
1952 btrfs_descending_sort_devices(devices_info, nr_devices);
1956 while (nr_devices >= rattr->devs_min) {
1957 num_stripes = min(num_stripes, nr_devices);
1959 if (devices_info[i].max_avail >= min_stripe_size) {
1963 avail_space += devices_info[i].max_avail * num_stripes;
1964 alloc_size = devices_info[i].max_avail;
1965 for (j = i + 1 - num_stripes; j <= i; j++)
1966 devices_info[j].max_avail -= alloc_size;
1972 kfree(devices_info);
1973 *free_bytes = avail_space;
1978 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1980 * If there's a redundant raid level at DATA block groups, use the respective
1981 * multiplier to scale the sizes.
1983 * Unused device space usage is based on simulating the chunk allocator
1984 * algorithm that respects the device sizes and order of allocations. This is
1985 * a close approximation of the actual use but there are other factors that may
1986 * change the result (like a new metadata chunk).
1988 * If metadata is exhausted, f_bavail will be 0.
1990 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1992 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1993 struct btrfs_super_block *disk_super = fs_info->super_copy;
1994 struct btrfs_space_info *found;
1996 u64 total_free_data = 0;
1997 u64 total_free_meta = 0;
1998 u32 bits = fs_info->sectorsize_bits;
1999 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2000 unsigned factor = 1;
2001 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2006 list_for_each_entry(found, &fs_info->space_info, list) {
2007 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2010 total_free_data += found->disk_total - found->disk_used;
2012 btrfs_account_ro_block_groups_free_space(found);
2014 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2015 if (!list_empty(&found->block_groups[i]))
2016 factor = btrfs_bg_type_to_factor(
2017 btrfs_raid_array[i].bg_flag);
2022 * Metadata in mixed block group profiles are accounted in data
2024 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2025 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2028 total_free_meta += found->disk_total -
2032 total_used += found->disk_used;
2035 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2036 buf->f_blocks >>= bits;
2037 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2039 /* Account global block reserve as used, it's in logical size already */
2040 spin_lock(&block_rsv->lock);
2041 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2042 if (buf->f_bfree >= block_rsv->size >> bits)
2043 buf->f_bfree -= block_rsv->size >> bits;
2046 spin_unlock(&block_rsv->lock);
2048 buf->f_bavail = div_u64(total_free_data, factor);
2049 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2052 buf->f_bavail += div_u64(total_free_data, factor);
2053 buf->f_bavail = buf->f_bavail >> bits;
2056 * We calculate the remaining metadata space minus global reserve. If
2057 * this is (supposedly) smaller than zero, there's no space. But this
2058 * does not hold in practice, the exhausted state happens where's still
2059 * some positive delta. So we apply some guesswork and compare the
2060 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2062 * We probably cannot calculate the exact threshold value because this
2063 * depends on the internal reservations requested by various
2064 * operations, so some operations that consume a few metadata will
2065 * succeed even if the Avail is zero. But this is better than the other
2071 * We only want to claim there's no available space if we can no longer
2072 * allocate chunks for our metadata profile and our global reserve will
2073 * not fit in the free metadata space. If we aren't ->full then we
2074 * still can allocate chunks and thus are fine using the currently
2075 * calculated f_bavail.
2077 if (!mixed && block_rsv->space_info->full &&
2078 (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
2081 buf->f_type = BTRFS_SUPER_MAGIC;
2082 buf->f_bsize = dentry->d_sb->s_blocksize;
2083 buf->f_namelen = BTRFS_NAME_LEN;
2085 /* We treat it as constant endianness (it doesn't matter _which_)
2086 because we want the fsid to come out the same whether mounted
2087 on a big-endian or little-endian host */
2088 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2089 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2090 /* Mask in the root object ID too, to disambiguate subvols */
2091 buf->f_fsid.val[0] ^=
2092 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2093 buf->f_fsid.val[1] ^=
2094 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2099 static void btrfs_kill_super(struct super_block *sb)
2101 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2102 kill_anon_super(sb);
2103 btrfs_free_fs_info(fs_info);
2106 static struct file_system_type btrfs_fs_type = {
2107 .owner = THIS_MODULE,
2109 .mount = btrfs_mount,
2110 .kill_sb = btrfs_kill_super,
2111 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2114 static struct file_system_type btrfs_root_fs_type = {
2115 .owner = THIS_MODULE,
2117 .mount = btrfs_mount_root,
2118 .kill_sb = btrfs_kill_super,
2119 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2122 MODULE_ALIAS_FS("btrfs");
2124 static int btrfs_control_open(struct inode *inode, struct file *file)
2127 * The control file's private_data is used to hold the
2128 * transaction when it is started and is used to keep
2129 * track of whether a transaction is already in progress.
2131 file->private_data = NULL;
2136 * Used by /dev/btrfs-control for devices ioctls.
2138 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2141 struct btrfs_ioctl_vol_args *vol;
2142 struct btrfs_device *device = NULL;
2146 if (!capable(CAP_SYS_ADMIN))
2149 vol = memdup_user((void __user *)arg, sizeof(*vol));
2151 return PTR_ERR(vol);
2152 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2155 case BTRFS_IOC_SCAN_DEV:
2156 mutex_lock(&uuid_mutex);
2158 * Scanning outside of mount can return NULL which would turn
2159 * into 0 error code.
2161 device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2162 ret = PTR_ERR_OR_ZERO(device);
2163 mutex_unlock(&uuid_mutex);
2165 case BTRFS_IOC_FORGET_DEV:
2166 if (vol->name[0] != 0) {
2167 ret = lookup_bdev(vol->name, &devt);
2171 ret = btrfs_forget_devices(devt);
2173 case BTRFS_IOC_DEVICES_READY:
2174 mutex_lock(&uuid_mutex);
2176 * Scanning outside of mount can return NULL which would turn
2177 * into 0 error code.
2179 device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ, false);
2180 if (IS_ERR_OR_NULL(device)) {
2181 mutex_unlock(&uuid_mutex);
2182 ret = PTR_ERR(device);
2185 ret = !(device->fs_devices->num_devices ==
2186 device->fs_devices->total_devices);
2187 mutex_unlock(&uuid_mutex);
2189 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2190 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2198 static int btrfs_freeze(struct super_block *sb)
2200 struct btrfs_trans_handle *trans;
2201 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2202 struct btrfs_root *root = fs_info->tree_root;
2204 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2206 * We don't need a barrier here, we'll wait for any transaction that
2207 * could be in progress on other threads (and do delayed iputs that
2208 * we want to avoid on a frozen filesystem), or do the commit
2211 trans = btrfs_attach_transaction_barrier(root);
2212 if (IS_ERR(trans)) {
2213 /* no transaction, don't bother */
2214 if (PTR_ERR(trans) == -ENOENT)
2216 return PTR_ERR(trans);
2218 return btrfs_commit_transaction(trans);
2221 static int check_dev_super(struct btrfs_device *dev)
2223 struct btrfs_fs_info *fs_info = dev->fs_info;
2224 struct btrfs_super_block *sb;
2229 /* This should be called with fs still frozen. */
2230 ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2232 /* Missing dev, no need to check. */
2236 /* Only need to check the primary super block. */
2237 sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2241 /* Verify the checksum. */
2242 csum_type = btrfs_super_csum_type(sb);
2243 if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
2244 btrfs_err(fs_info, "csum type changed, has %u expect %u",
2245 csum_type, btrfs_super_csum_type(fs_info->super_copy));
2250 if (btrfs_check_super_csum(fs_info, sb)) {
2251 btrfs_err(fs_info, "csum for on-disk super block no longer matches");
2256 /* Btrfs_validate_super() includes fsid check against super->fsid. */
2257 ret = btrfs_validate_super(fs_info, sb, 0);
2261 last_trans = btrfs_get_last_trans_committed(fs_info);
2262 if (btrfs_super_generation(sb) != last_trans) {
2263 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2264 btrfs_super_generation(sb), last_trans);
2269 btrfs_release_disk_super(sb);
2273 static int btrfs_unfreeze(struct super_block *sb)
2275 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2276 struct btrfs_device *device;
2280 * Make sure the fs is not changed by accident (like hibernation then
2281 * modified by other OS).
2282 * If we found anything wrong, we mark the fs error immediately.
2284 * And since the fs is frozen, no one can modify the fs yet, thus
2285 * we don't need to hold device_list_mutex.
2287 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2288 ret = check_dev_super(device);
2290 btrfs_handle_fs_error(fs_info, ret,
2291 "super block on devid %llu got modified unexpectedly",
2296 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2299 * We still return 0, to allow VFS layer to unfreeze the fs even the
2300 * above checks failed. Since the fs is either fine or read-only, we're
2301 * safe to continue, without causing further damage.
2306 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2308 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2311 * There should be always a valid pointer in latest_dev, it may be stale
2312 * for a short moment in case it's being deleted but still valid until
2313 * the end of RCU grace period.
2316 seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
2322 static const struct super_operations btrfs_super_ops = {
2323 .drop_inode = btrfs_drop_inode,
2324 .evict_inode = btrfs_evict_inode,
2325 .put_super = btrfs_put_super,
2326 .sync_fs = btrfs_sync_fs,
2327 .show_options = btrfs_show_options,
2328 .show_devname = btrfs_show_devname,
2329 .alloc_inode = btrfs_alloc_inode,
2330 .destroy_inode = btrfs_destroy_inode,
2331 .free_inode = btrfs_free_inode,
2332 .statfs = btrfs_statfs,
2333 .remount_fs = btrfs_remount,
2334 .freeze_fs = btrfs_freeze,
2335 .unfreeze_fs = btrfs_unfreeze,
2338 static const struct file_operations btrfs_ctl_fops = {
2339 .open = btrfs_control_open,
2340 .unlocked_ioctl = btrfs_control_ioctl,
2341 .compat_ioctl = compat_ptr_ioctl,
2342 .owner = THIS_MODULE,
2343 .llseek = noop_llseek,
2346 static struct miscdevice btrfs_misc = {
2347 .minor = BTRFS_MINOR,
2348 .name = "btrfs-control",
2349 .fops = &btrfs_ctl_fops
2352 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2353 MODULE_ALIAS("devname:btrfs-control");
2355 static int __init btrfs_interface_init(void)
2357 return misc_register(&btrfs_misc);
2360 static __cold void btrfs_interface_exit(void)
2362 misc_deregister(&btrfs_misc);
2365 static int __init btrfs_print_mod_info(void)
2367 static const char options[] = ""
2368 #ifdef CONFIG_BTRFS_DEBUG
2371 #ifdef CONFIG_BTRFS_ASSERT
2374 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2377 #ifdef CONFIG_BLK_DEV_ZONED
2382 #ifdef CONFIG_FS_VERITY
2388 pr_info("Btrfs loaded%s\n", options);
2392 static int register_btrfs(void)
2394 return register_filesystem(&btrfs_fs_type);
2397 static void unregister_btrfs(void)
2399 unregister_filesystem(&btrfs_fs_type);
2402 /* Helper structure for long init/exit functions. */
2403 struct init_sequence {
2404 int (*init_func)(void);
2405 /* Can be NULL if the init_func doesn't need cleanup. */
2406 void (*exit_func)(void);
2409 static const struct init_sequence mod_init_seq[] = {
2411 .init_func = btrfs_props_init,
2414 .init_func = btrfs_init_sysfs,
2415 .exit_func = btrfs_exit_sysfs,
2417 .init_func = btrfs_init_compress,
2418 .exit_func = btrfs_exit_compress,
2420 .init_func = btrfs_init_cachep,
2421 .exit_func = btrfs_destroy_cachep,
2423 .init_func = btrfs_transaction_init,
2424 .exit_func = btrfs_transaction_exit,
2426 .init_func = btrfs_ctree_init,
2427 .exit_func = btrfs_ctree_exit,
2429 .init_func = btrfs_free_space_init,
2430 .exit_func = btrfs_free_space_exit,
2432 .init_func = extent_state_init_cachep,
2433 .exit_func = extent_state_free_cachep,
2435 .init_func = extent_buffer_init_cachep,
2436 .exit_func = extent_buffer_free_cachep,
2438 .init_func = btrfs_bioset_init,
2439 .exit_func = btrfs_bioset_exit,
2441 .init_func = extent_map_init,
2442 .exit_func = extent_map_exit,
2444 .init_func = ordered_data_init,
2445 .exit_func = ordered_data_exit,
2447 .init_func = btrfs_delayed_inode_init,
2448 .exit_func = btrfs_delayed_inode_exit,
2450 .init_func = btrfs_auto_defrag_init,
2451 .exit_func = btrfs_auto_defrag_exit,
2453 .init_func = btrfs_delayed_ref_init,
2454 .exit_func = btrfs_delayed_ref_exit,
2456 .init_func = btrfs_prelim_ref_init,
2457 .exit_func = btrfs_prelim_ref_exit,
2459 .init_func = btrfs_interface_init,
2460 .exit_func = btrfs_interface_exit,
2462 .init_func = btrfs_print_mod_info,
2465 .init_func = btrfs_run_sanity_tests,
2468 .init_func = register_btrfs,
2469 .exit_func = unregister_btrfs,
2473 static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
2475 static __always_inline void btrfs_exit_btrfs_fs(void)
2479 for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
2480 if (!mod_init_result[i])
2482 if (mod_init_seq[i].exit_func)
2483 mod_init_seq[i].exit_func();
2484 mod_init_result[i] = false;
2488 static void __exit exit_btrfs_fs(void)
2490 btrfs_exit_btrfs_fs();
2491 btrfs_cleanup_fs_uuids();
2494 static int __init init_btrfs_fs(void)
2499 for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
2500 ASSERT(!mod_init_result[i]);
2501 ret = mod_init_seq[i].init_func();
2503 btrfs_exit_btrfs_fs();
2506 mod_init_result[i] = true;
2511 late_initcall(init_btrfs_fs);
2512 module_exit(exit_btrfs_fs)
2514 MODULE_LICENSE("GPL");
2515 MODULE_SOFTDEP("pre: crc32c");
2516 MODULE_SOFTDEP("pre: xxhash64");
2517 MODULE_SOFTDEP("pre: sha256");
2518 MODULE_SOFTDEP("pre: blake2b-256");