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 "delayed-inode.h"
32 #include "transaction.h"
33 #include "btrfs_inode.h"
34 #include "print-tree.h"
39 #include "compression.h"
40 #include "rcu-string.h"
41 #include "dev-replace.h"
42 #include "free-space-cache.h"
44 #include "space-info.h"
47 #include "tests/btrfs-tests.h"
48 #include "block-group.h"
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/btrfs.h>
54 static const struct super_operations btrfs_super_ops;
57 * Types for mounting the default subvolume and a subvolume explicitly
58 * requested by subvol=/path. That way the callchain is straightforward and we
59 * don't have to play tricks with the mount options and recursive calls to
62 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
64 static struct file_system_type btrfs_fs_type;
65 static struct file_system_type btrfs_root_fs_type;
67 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
71 #define STATE_STRING_PREFACE ": state "
72 #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
75 * Characters to print to indicate error conditions or uncommon filesystem sate.
78 static const char fs_state_chars[] = {
79 [BTRFS_FS_STATE_ERROR] = 'E',
80 [BTRFS_FS_STATE_REMOUNTING] = 'M',
81 [BTRFS_FS_STATE_RO] = 0,
82 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A',
83 [BTRFS_FS_STATE_DEV_REPLACING] = 'R',
84 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0,
85 [BTRFS_FS_STATE_NO_CSUMS] = 'C',
86 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L',
89 static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
92 bool states_printed = false;
93 unsigned long fs_state = READ_ONCE(info->fs_state);
96 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
97 curr += sizeof(STATE_STRING_PREFACE) - 1;
99 for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
100 WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
101 if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
102 *curr++ = fs_state_chars[bit];
103 states_printed = true;
107 /* If no states were printed, reset the buffer */
116 * Generally the error codes correspond to their respective errors, but there
117 * are a few special cases.
119 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
120 * instance will return EUCLEAN if any of the blocks are corrupted in
121 * a way that is problematic. We want to reserve EUCLEAN for these
122 * sort of corruptions.
124 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
125 * need to use EROFS for this case. We will have no idea of the
126 * original failure, that will have been reported at the time we tripped
127 * over the error. Each subsequent error that doesn't have any context
128 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
130 const char * __attribute_const__ btrfs_decode_error(int errno)
132 char *errstr = "unknown";
135 case -ENOENT: /* -2 */
136 errstr = "No such entry";
139 errstr = "IO failure";
141 case -ENOMEM: /* -12*/
142 errstr = "Out of memory";
144 case -EEXIST: /* -17 */
145 errstr = "Object already exists";
147 case -ENOSPC: /* -28 */
148 errstr = "No space left";
150 case -EROFS: /* -30 */
151 errstr = "Readonly filesystem";
153 case -EOPNOTSUPP: /* -95 */
154 errstr = "Operation not supported";
156 case -EUCLEAN: /* -117 */
157 errstr = "Filesystem corrupted";
159 case -EDQUOT: /* -122 */
160 errstr = "Quota exceeded";
168 * __btrfs_handle_fs_error decodes expected errors from the caller and
169 * invokes the appropriate error response.
172 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
173 unsigned int line, int errno, const char *fmt, ...)
175 struct super_block *sb = fs_info->sb;
177 char statestr[STATE_STRING_BUF_LEN];
182 * Special case: if the error is EROFS, and we're already
183 * under SB_RDONLY, then it is safe here.
185 if (errno == -EROFS && sb_rdonly(sb))
189 errstr = btrfs_decode_error(errno);
190 btrfs_state_to_string(fs_info, statestr);
192 struct va_format vaf;
199 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
200 sb->s_id, statestr, function, line, errno, errstr, &vaf);
203 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
204 sb->s_id, statestr, function, line, errno, errstr);
209 * Today we only save the error info to memory. Long term we'll
210 * also send it down to the disk
212 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
214 /* Don't go through full error handling during mount */
215 if (!(sb->s_flags & SB_BORN))
221 btrfs_discard_stop(fs_info);
223 /* btrfs handle error by forcing the filesystem readonly */
224 btrfs_set_sb_rdonly(sb);
225 btrfs_info(fs_info, "forced readonly");
227 * Note that a running device replace operation is not canceled here
228 * although there is no way to update the progress. It would add the
229 * risk of a deadlock, therefore the canceling is omitted. The only
230 * penalty is that some I/O remains active until the procedure
231 * completes. The next time when the filesystem is mounted writable
232 * again, the device replace operation continues.
237 static const char * const logtypes[] = {
250 * Use one ratelimit state per log level so that a flood of less important
251 * messages doesn't cause more important ones to be dropped.
253 static struct ratelimit_state printk_limits[] = {
254 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
255 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
256 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
257 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
258 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
259 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
260 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
261 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
264 void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
266 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
267 struct va_format vaf;
270 const char *type = logtypes[4];
271 struct ratelimit_state *ratelimit = &printk_limits[4];
275 while ((kern_level = printk_get_level(fmt)) != 0) {
276 size_t size = printk_skip_level(fmt) - fmt;
278 if (kern_level >= '0' && kern_level <= '7') {
279 memcpy(lvl, fmt, size);
281 type = logtypes[kern_level - '0'];
282 ratelimit = &printk_limits[kern_level - '0'];
290 if (__ratelimit(ratelimit)) {
292 char statestr[STATE_STRING_BUF_LEN];
294 btrfs_state_to_string(fs_info, statestr);
295 _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
296 fs_info->sb->s_id, statestr, &vaf);
298 _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
306 #if BITS_PER_LONG == 32
307 void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
309 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
310 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
312 "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
313 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
315 "please consider upgrading to 64bit kernel/hardware");
319 void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
321 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
322 btrfs_err(fs_info, "reached 32bit limit for logical addresses");
324 "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
325 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
327 "please consider upgrading to 64bit kernel/hardware");
333 * We only mark the transaction aborted and then set the file system read-only.
334 * This will prevent new transactions from starting or trying to join this
337 * This means that error recovery at the call site is limited to freeing
338 * any local memory allocations and passing the error code up without
339 * further cleanup. The transaction should complete as it normally would
340 * in the call path but will return -EIO.
342 * We'll complete the cleanup in btrfs_end_transaction and
343 * btrfs_commit_transaction.
346 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
347 const char *function,
348 unsigned int line, int errno)
350 struct btrfs_fs_info *fs_info = trans->fs_info;
352 WRITE_ONCE(trans->aborted, errno);
353 WRITE_ONCE(trans->transaction->aborted, errno);
354 /* Wake up anybody who may be waiting on this transaction */
355 wake_up(&fs_info->transaction_wait);
356 wake_up(&fs_info->transaction_blocked_wait);
357 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
360 * __btrfs_panic decodes unexpected, fatal errors from the caller,
361 * issues an alert, and either panics or BUGs, depending on mount options.
364 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
365 unsigned int line, int errno, const char *fmt, ...)
367 char *s_id = "<unknown>";
369 struct va_format vaf = { .fmt = fmt };
373 s_id = fs_info->sb->s_id;
378 errstr = btrfs_decode_error(errno);
379 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
380 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
381 s_id, function, line, &vaf, errno, errstr);
383 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
384 function, line, &vaf, errno, errstr);
386 /* Caller calls BUG() */
389 static void btrfs_put_super(struct super_block *sb)
391 close_ctree(btrfs_sb(sb));
400 Opt_compress_force_type,
405 Opt_flushoncommit, Opt_noflushoncommit,
407 Opt_barrier, Opt_nobarrier,
408 Opt_datacow, Opt_nodatacow,
409 Opt_datasum, Opt_nodatasum,
410 Opt_defrag, Opt_nodefrag,
411 Opt_discard, Opt_nodiscard,
415 Opt_rescan_uuid_tree,
417 Opt_space_cache, Opt_no_space_cache,
418 Opt_space_cache_version,
420 Opt_ssd_spread, Opt_nossd_spread,
425 Opt_treelog, Opt_notreelog,
426 Opt_user_subvol_rm_allowed,
436 /* Deprecated options */
438 Opt_inode_cache, Opt_noinode_cache,
440 /* Debugging options */
442 Opt_check_integrity_including_extent_data,
443 Opt_check_integrity_print_mask,
444 Opt_enospc_debug, Opt_noenospc_debug,
445 #ifdef CONFIG_BTRFS_DEBUG
446 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
448 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
454 static const match_table_t tokens = {
456 {Opt_noacl, "noacl"},
457 {Opt_clear_cache, "clear_cache"},
458 {Opt_commit_interval, "commit=%u"},
459 {Opt_compress, "compress"},
460 {Opt_compress_type, "compress=%s"},
461 {Opt_compress_force, "compress-force"},
462 {Opt_compress_force_type, "compress-force=%s"},
463 {Opt_degraded, "degraded"},
464 {Opt_device, "device=%s"},
465 {Opt_fatal_errors, "fatal_errors=%s"},
466 {Opt_flushoncommit, "flushoncommit"},
467 {Opt_noflushoncommit, "noflushoncommit"},
468 {Opt_inode_cache, "inode_cache"},
469 {Opt_noinode_cache, "noinode_cache"},
470 {Opt_max_inline, "max_inline=%s"},
471 {Opt_barrier, "barrier"},
472 {Opt_nobarrier, "nobarrier"},
473 {Opt_datacow, "datacow"},
474 {Opt_nodatacow, "nodatacow"},
475 {Opt_datasum, "datasum"},
476 {Opt_nodatasum, "nodatasum"},
477 {Opt_defrag, "autodefrag"},
478 {Opt_nodefrag, "noautodefrag"},
479 {Opt_discard, "discard"},
480 {Opt_discard_mode, "discard=%s"},
481 {Opt_nodiscard, "nodiscard"},
482 {Opt_norecovery, "norecovery"},
483 {Opt_ratio, "metadata_ratio=%u"},
484 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
485 {Opt_skip_balance, "skip_balance"},
486 {Opt_space_cache, "space_cache"},
487 {Opt_no_space_cache, "nospace_cache"},
488 {Opt_space_cache_version, "space_cache=%s"},
490 {Opt_nossd, "nossd"},
491 {Opt_ssd_spread, "ssd_spread"},
492 {Opt_nossd_spread, "nossd_spread"},
493 {Opt_subvol, "subvol=%s"},
494 {Opt_subvol_empty, "subvol="},
495 {Opt_subvolid, "subvolid=%s"},
496 {Opt_thread_pool, "thread_pool=%u"},
497 {Opt_treelog, "treelog"},
498 {Opt_notreelog, "notreelog"},
499 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
502 {Opt_rescue, "rescue=%s"},
503 /* Deprecated, with alias rescue=nologreplay */
504 {Opt_nologreplay, "nologreplay"},
505 /* Deprecated, with alias rescue=usebackuproot */
506 {Opt_usebackuproot, "usebackuproot"},
508 /* Deprecated options */
509 {Opt_recovery, "recovery"},
511 /* Debugging options */
512 {Opt_check_integrity, "check_int"},
513 {Opt_check_integrity_including_extent_data, "check_int_data"},
514 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
515 {Opt_enospc_debug, "enospc_debug"},
516 {Opt_noenospc_debug, "noenospc_debug"},
517 #ifdef CONFIG_BTRFS_DEBUG
518 {Opt_fragment_data, "fragment=data"},
519 {Opt_fragment_metadata, "fragment=metadata"},
520 {Opt_fragment_all, "fragment=all"},
522 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
523 {Opt_ref_verify, "ref_verify"},
528 static const match_table_t rescue_tokens = {
529 {Opt_usebackuproot, "usebackuproot"},
530 {Opt_nologreplay, "nologreplay"},
531 {Opt_ignorebadroots, "ignorebadroots"},
532 {Opt_ignorebadroots, "ibadroots"},
533 {Opt_ignoredatacsums, "ignoredatacsums"},
534 {Opt_ignoredatacsums, "idatacsums"},
535 {Opt_rescue_all, "all"},
539 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
540 const char *opt_name)
542 if (fs_info->mount_opt & opt) {
543 btrfs_err(fs_info, "%s must be used with ro mount option",
550 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
555 substring_t args[MAX_OPT_ARGS];
558 opts = kstrdup(options, GFP_KERNEL);
563 while ((p = strsep(&opts, ":")) != NULL) {
568 token = match_token(p, rescue_tokens, args);
570 case Opt_usebackuproot:
572 "trying to use backup root at mount time");
573 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
575 case Opt_nologreplay:
576 btrfs_set_and_info(info, NOLOGREPLAY,
577 "disabling log replay at mount time");
579 case Opt_ignorebadroots:
580 btrfs_set_and_info(info, IGNOREBADROOTS,
581 "ignoring bad roots");
583 case Opt_ignoredatacsums:
584 btrfs_set_and_info(info, IGNOREDATACSUMS,
585 "ignoring data csums");
588 btrfs_info(info, "enabling all of the rescue options");
589 btrfs_set_and_info(info, IGNOREDATACSUMS,
590 "ignoring data csums");
591 btrfs_set_and_info(info, IGNOREBADROOTS,
592 "ignoring bad roots");
593 btrfs_set_and_info(info, NOLOGREPLAY,
594 "disabling log replay at mount time");
597 btrfs_info(info, "unrecognized rescue option '%s'", p);
611 * Regular mount options parser. Everything that is needed only when
612 * reading in a new superblock is parsed here.
613 * XXX JDM: This needs to be cleaned up for remount.
615 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
616 unsigned long new_flags)
618 substring_t args[MAX_OPT_ARGS];
623 bool compress_force = false;
624 enum btrfs_compression_type saved_compress_type;
625 int saved_compress_level;
626 bool saved_compress_force;
629 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
630 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
631 else if (btrfs_free_space_cache_v1_active(info)) {
632 if (btrfs_is_zoned(info)) {
634 "zoned: clearing existing space cache");
635 btrfs_set_super_cache_generation(info->super_copy, 0);
637 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
642 * Even the options are empty, we still need to do extra check
648 while ((p = strsep(&options, ",")) != NULL) {
653 token = match_token(p, tokens, args);
656 btrfs_info(info, "allowing degraded mounts");
657 btrfs_set_opt(info->mount_opt, DEGRADED);
660 case Opt_subvol_empty:
664 * These are parsed by btrfs_parse_subvol_options or
665 * btrfs_parse_device_options and can be ignored here.
669 btrfs_set_and_info(info, NODATASUM,
670 "setting nodatasum");
673 if (btrfs_test_opt(info, NODATASUM)) {
674 if (btrfs_test_opt(info, NODATACOW))
676 "setting datasum, datacow enabled");
678 btrfs_info(info, "setting datasum");
680 btrfs_clear_opt(info->mount_opt, NODATACOW);
681 btrfs_clear_opt(info->mount_opt, NODATASUM);
684 if (!btrfs_test_opt(info, NODATACOW)) {
685 if (!btrfs_test_opt(info, COMPRESS) ||
686 !btrfs_test_opt(info, FORCE_COMPRESS)) {
688 "setting nodatacow, compression disabled");
690 btrfs_info(info, "setting nodatacow");
693 btrfs_clear_opt(info->mount_opt, COMPRESS);
694 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
695 btrfs_set_opt(info->mount_opt, NODATACOW);
696 btrfs_set_opt(info->mount_opt, NODATASUM);
699 btrfs_clear_and_info(info, NODATACOW,
702 case Opt_compress_force:
703 case Opt_compress_force_type:
704 compress_force = true;
707 case Opt_compress_type:
708 saved_compress_type = btrfs_test_opt(info,
710 info->compress_type : BTRFS_COMPRESS_NONE;
711 saved_compress_force =
712 btrfs_test_opt(info, FORCE_COMPRESS);
713 saved_compress_level = info->compress_level;
714 if (token == Opt_compress ||
715 token == Opt_compress_force ||
716 strncmp(args[0].from, "zlib", 4) == 0) {
717 compress_type = "zlib";
719 info->compress_type = BTRFS_COMPRESS_ZLIB;
720 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
722 * args[0] contains uninitialized data since
723 * for these tokens we don't expect any
726 if (token != Opt_compress &&
727 token != Opt_compress_force)
728 info->compress_level =
729 btrfs_compress_str2level(
732 btrfs_set_opt(info->mount_opt, COMPRESS);
733 btrfs_clear_opt(info->mount_opt, NODATACOW);
734 btrfs_clear_opt(info->mount_opt, NODATASUM);
736 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
737 compress_type = "lzo";
738 info->compress_type = BTRFS_COMPRESS_LZO;
739 info->compress_level = 0;
740 btrfs_set_opt(info->mount_opt, COMPRESS);
741 btrfs_clear_opt(info->mount_opt, NODATACOW);
742 btrfs_clear_opt(info->mount_opt, NODATASUM);
743 btrfs_set_fs_incompat(info, COMPRESS_LZO);
745 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
746 compress_type = "zstd";
747 info->compress_type = BTRFS_COMPRESS_ZSTD;
748 info->compress_level =
749 btrfs_compress_str2level(
752 btrfs_set_opt(info->mount_opt, COMPRESS);
753 btrfs_clear_opt(info->mount_opt, NODATACOW);
754 btrfs_clear_opt(info->mount_opt, NODATASUM);
755 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
757 } else if (strncmp(args[0].from, "no", 2) == 0) {
758 compress_type = "no";
759 info->compress_level = 0;
760 info->compress_type = 0;
761 btrfs_clear_opt(info->mount_opt, COMPRESS);
762 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
763 compress_force = false;
766 btrfs_err(info, "unrecognized compression value %s",
772 if (compress_force) {
773 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
776 * If we remount from compress-force=xxx to
777 * compress=xxx, we need clear FORCE_COMPRESS
778 * flag, otherwise, there is no way for users
779 * to disable forcible compression separately.
781 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
783 if (no_compress == 1) {
784 btrfs_info(info, "use no compression");
785 } else if ((info->compress_type != saved_compress_type) ||
786 (compress_force != saved_compress_force) ||
787 (info->compress_level != saved_compress_level)) {
788 btrfs_info(info, "%s %s compression, level %d",
789 (compress_force) ? "force" : "use",
790 compress_type, info->compress_level);
792 compress_force = false;
795 btrfs_set_and_info(info, SSD,
796 "enabling ssd optimizations");
797 btrfs_clear_opt(info->mount_opt, NOSSD);
800 btrfs_set_and_info(info, SSD,
801 "enabling ssd optimizations");
802 btrfs_set_and_info(info, SSD_SPREAD,
803 "using spread ssd allocation scheme");
804 btrfs_clear_opt(info->mount_opt, NOSSD);
807 btrfs_set_opt(info->mount_opt, NOSSD);
808 btrfs_clear_and_info(info, SSD,
809 "not using ssd optimizations");
811 case Opt_nossd_spread:
812 btrfs_clear_and_info(info, SSD_SPREAD,
813 "not using spread ssd allocation scheme");
816 btrfs_clear_and_info(info, NOBARRIER,
817 "turning on barriers");
820 btrfs_set_and_info(info, NOBARRIER,
821 "turning off barriers");
823 case Opt_thread_pool:
824 ret = match_int(&args[0], &intarg);
826 btrfs_err(info, "unrecognized thread_pool value %s",
829 } else if (intarg == 0) {
830 btrfs_err(info, "invalid value 0 for thread_pool");
834 info->thread_pool_size = intarg;
837 num = match_strdup(&args[0]);
839 info->max_inline = memparse(num, NULL);
842 if (info->max_inline) {
843 info->max_inline = min_t(u64,
847 btrfs_info(info, "max_inline at %llu",
855 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
856 info->sb->s_flags |= SB_POSIXACL;
859 btrfs_err(info, "support for ACL not compiled in!");
864 info->sb->s_flags &= ~SB_POSIXACL;
867 btrfs_set_and_info(info, NOTREELOG,
868 "disabling tree log");
871 btrfs_clear_and_info(info, NOTREELOG,
872 "enabling tree log");
875 case Opt_nologreplay:
877 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
878 btrfs_set_and_info(info, NOLOGREPLAY,
879 "disabling log replay at mount time");
881 case Opt_flushoncommit:
882 btrfs_set_and_info(info, FLUSHONCOMMIT,
883 "turning on flush-on-commit");
885 case Opt_noflushoncommit:
886 btrfs_clear_and_info(info, FLUSHONCOMMIT,
887 "turning off flush-on-commit");
890 ret = match_int(&args[0], &intarg);
892 btrfs_err(info, "unrecognized metadata_ratio value %s",
896 info->metadata_ratio = intarg;
897 btrfs_info(info, "metadata ratio %u",
898 info->metadata_ratio);
901 case Opt_discard_mode:
902 if (token == Opt_discard ||
903 strcmp(args[0].from, "sync") == 0) {
904 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
905 btrfs_set_and_info(info, DISCARD_SYNC,
906 "turning on sync discard");
907 } else if (strcmp(args[0].from, "async") == 0) {
908 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
909 btrfs_set_and_info(info, DISCARD_ASYNC,
910 "turning on async discard");
912 btrfs_err(info, "unrecognized discard mode value %s",
919 btrfs_clear_and_info(info, DISCARD_SYNC,
920 "turning off discard");
921 btrfs_clear_and_info(info, DISCARD_ASYNC,
922 "turning off async discard");
924 case Opt_space_cache:
925 case Opt_space_cache_version:
927 * We already set FREE_SPACE_TREE above because we have
928 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
929 * to allow v1 to be set for extent tree v2, simply
930 * ignore this setting if we're extent tree v2.
932 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
934 if (token == Opt_space_cache ||
935 strcmp(args[0].from, "v1") == 0) {
936 btrfs_clear_opt(info->mount_opt,
938 btrfs_set_and_info(info, SPACE_CACHE,
939 "enabling disk space caching");
940 } else if (strcmp(args[0].from, "v2") == 0) {
941 btrfs_clear_opt(info->mount_opt,
943 btrfs_set_and_info(info, FREE_SPACE_TREE,
944 "enabling free space tree");
946 btrfs_err(info, "unrecognized space_cache value %s",
952 case Opt_rescan_uuid_tree:
953 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
955 case Opt_no_space_cache:
957 * We cannot operate without the free space tree with
958 * extent tree v2, ignore this option.
960 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
962 if (btrfs_test_opt(info, SPACE_CACHE)) {
963 btrfs_clear_and_info(info, SPACE_CACHE,
964 "disabling disk space caching");
966 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
967 btrfs_clear_and_info(info, FREE_SPACE_TREE,
968 "disabling free space tree");
971 case Opt_inode_cache:
972 case Opt_noinode_cache:
974 "the 'inode_cache' option is deprecated and has no effect since 5.11");
976 case Opt_clear_cache:
978 * We cannot clear the free space tree with extent tree
979 * v2, ignore this option.
981 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
983 btrfs_set_and_info(info, CLEAR_CACHE,
984 "force clearing of disk cache");
986 case Opt_user_subvol_rm_allowed:
987 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
989 case Opt_enospc_debug:
990 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
992 case Opt_noenospc_debug:
993 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
996 btrfs_set_and_info(info, AUTO_DEFRAG,
997 "enabling auto defrag");
1000 btrfs_clear_and_info(info, AUTO_DEFRAG,
1001 "disabling auto defrag");
1004 case Opt_usebackuproot:
1006 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
1007 token == Opt_recovery ? "recovery" :
1010 "trying to use backup root at mount time");
1011 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
1013 case Opt_skip_balance:
1014 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
1016 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1017 case Opt_check_integrity_including_extent_data:
1019 "enabling check integrity including extent data");
1020 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
1021 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1023 case Opt_check_integrity:
1024 btrfs_info(info, "enabling check integrity");
1025 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1027 case Opt_check_integrity_print_mask:
1028 ret = match_int(&args[0], &intarg);
1031 "unrecognized check_integrity_print_mask value %s",
1035 info->check_integrity_print_mask = intarg;
1036 btrfs_info(info, "check_integrity_print_mask 0x%x",
1037 info->check_integrity_print_mask);
1040 case Opt_check_integrity_including_extent_data:
1041 case Opt_check_integrity:
1042 case Opt_check_integrity_print_mask:
1044 "support for check_integrity* not compiled in!");
1048 case Opt_fatal_errors:
1049 if (strcmp(args[0].from, "panic") == 0) {
1050 btrfs_set_opt(info->mount_opt,
1051 PANIC_ON_FATAL_ERROR);
1052 } else if (strcmp(args[0].from, "bug") == 0) {
1053 btrfs_clear_opt(info->mount_opt,
1054 PANIC_ON_FATAL_ERROR);
1056 btrfs_err(info, "unrecognized fatal_errors value %s",
1062 case Opt_commit_interval:
1064 ret = match_int(&args[0], &intarg);
1066 btrfs_err(info, "unrecognized commit_interval value %s",
1073 "using default commit interval %us",
1074 BTRFS_DEFAULT_COMMIT_INTERVAL);
1075 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1076 } else if (intarg > 300) {
1077 btrfs_warn(info, "excessive commit interval %d",
1080 info->commit_interval = intarg;
1083 ret = parse_rescue_options(info, args[0].from);
1085 btrfs_err(info, "unrecognized rescue value %s",
1090 #ifdef CONFIG_BTRFS_DEBUG
1091 case Opt_fragment_all:
1092 btrfs_info(info, "fragmenting all space");
1093 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1094 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1096 case Opt_fragment_metadata:
1097 btrfs_info(info, "fragmenting metadata");
1098 btrfs_set_opt(info->mount_opt,
1101 case Opt_fragment_data:
1102 btrfs_info(info, "fragmenting data");
1103 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1106 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1107 case Opt_ref_verify:
1108 btrfs_info(info, "doing ref verification");
1109 btrfs_set_opt(info->mount_opt, REF_VERIFY);
1113 btrfs_err(info, "unrecognized mount option '%s'", p);
1121 /* We're read-only, don't have to check. */
1122 if (new_flags & SB_RDONLY)
1125 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1126 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1127 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
1130 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
1131 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
1132 !btrfs_test_opt(info, CLEAR_CACHE)) {
1133 btrfs_err(info, "cannot disable free space tree");
1138 ret = btrfs_check_mountopts_zoned(info);
1139 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
1140 btrfs_info(info, "disk space caching is enabled");
1141 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
1142 btrfs_info(info, "using free space tree");
1147 * Parse mount options that are required early in the mount process.
1149 * All other options will be parsed on much later in the mount process and
1150 * only when we need to allocate a new super block.
1152 static int btrfs_parse_device_options(const char *options, fmode_t flags,
1155 substring_t args[MAX_OPT_ARGS];
1156 char *device_name, *opts, *orig, *p;
1157 struct btrfs_device *device = NULL;
1160 lockdep_assert_held(&uuid_mutex);
1166 * strsep changes the string, duplicate it because btrfs_parse_options
1169 opts = kstrdup(options, GFP_KERNEL);
1174 while ((p = strsep(&opts, ",")) != NULL) {
1180 token = match_token(p, tokens, args);
1181 if (token == Opt_device) {
1182 device_name = match_strdup(&args[0]);
1187 device = btrfs_scan_one_device(device_name, flags,
1190 if (IS_ERR(device)) {
1191 error = PTR_ERR(device);
1203 * Parse mount options that are related to subvolume id
1205 * The value is later passed to mount_subvol()
1207 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
1208 u64 *subvol_objectid)
1210 substring_t args[MAX_OPT_ARGS];
1211 char *opts, *orig, *p;
1219 * strsep changes the string, duplicate it because
1220 * btrfs_parse_device_options gets called later
1222 opts = kstrdup(options, GFP_KERNEL);
1227 while ((p = strsep(&opts, ",")) != NULL) {
1232 token = match_token(p, tokens, args);
1235 kfree(*subvol_name);
1236 *subvol_name = match_strdup(&args[0]);
1237 if (!*subvol_name) {
1243 error = match_u64(&args[0], &subvolid);
1247 /* we want the original fs_tree */
1249 subvolid = BTRFS_FS_TREE_OBJECTID;
1251 *subvol_objectid = subvolid;
1263 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1264 u64 subvol_objectid)
1266 struct btrfs_root *root = fs_info->tree_root;
1267 struct btrfs_root *fs_root = NULL;
1268 struct btrfs_root_ref *root_ref;
1269 struct btrfs_inode_ref *inode_ref;
1270 struct btrfs_key key;
1271 struct btrfs_path *path = NULL;
1272 char *name = NULL, *ptr;
1277 path = btrfs_alloc_path();
1283 name = kmalloc(PATH_MAX, GFP_KERNEL);
1288 ptr = name + PATH_MAX - 1;
1292 * Walk up the subvolume trees in the tree of tree roots by root
1293 * backrefs until we hit the top-level subvolume.
1295 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1296 key.objectid = subvol_objectid;
1297 key.type = BTRFS_ROOT_BACKREF_KEY;
1298 key.offset = (u64)-1;
1300 ret = btrfs_search_backwards(root, &key, path);
1303 } else if (ret > 0) {
1308 subvol_objectid = key.offset;
1310 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1311 struct btrfs_root_ref);
1312 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1315 ret = -ENAMETOOLONG;
1318 read_extent_buffer(path->nodes[0], ptr + 1,
1319 (unsigned long)(root_ref + 1), len);
1321 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1322 btrfs_release_path(path);
1324 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1325 if (IS_ERR(fs_root)) {
1326 ret = PTR_ERR(fs_root);
1332 * Walk up the filesystem tree by inode refs until we hit the
1335 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1336 key.objectid = dirid;
1337 key.type = BTRFS_INODE_REF_KEY;
1338 key.offset = (u64)-1;
1340 ret = btrfs_search_backwards(fs_root, &key, path);
1343 } else if (ret > 0) {
1350 inode_ref = btrfs_item_ptr(path->nodes[0],
1352 struct btrfs_inode_ref);
1353 len = btrfs_inode_ref_name_len(path->nodes[0],
1357 ret = -ENAMETOOLONG;
1360 read_extent_buffer(path->nodes[0], ptr + 1,
1361 (unsigned long)(inode_ref + 1), len);
1363 btrfs_release_path(path);
1365 btrfs_put_root(fs_root);
1369 btrfs_free_path(path);
1370 if (ptr == name + PATH_MAX - 1) {
1374 memmove(name, ptr, name + PATH_MAX - ptr);
1379 btrfs_put_root(fs_root);
1380 btrfs_free_path(path);
1382 return ERR_PTR(ret);
1385 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1387 struct btrfs_root *root = fs_info->tree_root;
1388 struct btrfs_dir_item *di;
1389 struct btrfs_path *path;
1390 struct btrfs_key location;
1393 path = btrfs_alloc_path();
1398 * Find the "default" dir item which points to the root item that we
1399 * will mount by default if we haven't been given a specific subvolume
1402 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1403 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1405 btrfs_free_path(path);
1410 * Ok the default dir item isn't there. This is weird since
1411 * it's always been there, but don't freak out, just try and
1412 * mount the top-level subvolume.
1414 btrfs_free_path(path);
1415 *objectid = BTRFS_FS_TREE_OBJECTID;
1419 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1420 btrfs_free_path(path);
1421 *objectid = location.objectid;
1425 static int btrfs_fill_super(struct super_block *sb,
1426 struct btrfs_fs_devices *fs_devices,
1429 struct inode *inode;
1430 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1433 sb->s_maxbytes = MAX_LFS_FILESIZE;
1434 sb->s_magic = BTRFS_SUPER_MAGIC;
1435 sb->s_op = &btrfs_super_ops;
1436 sb->s_d_op = &btrfs_dentry_operations;
1437 sb->s_export_op = &btrfs_export_ops;
1438 #ifdef CONFIG_FS_VERITY
1439 sb->s_vop = &btrfs_verityops;
1441 sb->s_xattr = btrfs_xattr_handlers;
1442 sb->s_time_gran = 1;
1443 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1444 sb->s_flags |= SB_POSIXACL;
1446 sb->s_flags |= SB_I_VERSION;
1447 sb->s_iflags |= SB_I_CGROUPWB;
1449 err = super_setup_bdi(sb);
1451 btrfs_err(fs_info, "super_setup_bdi failed");
1455 err = open_ctree(sb, fs_devices, (char *)data);
1457 btrfs_err(fs_info, "open_ctree failed");
1461 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1462 if (IS_ERR(inode)) {
1463 err = PTR_ERR(inode);
1467 sb->s_root = d_make_root(inode);
1473 sb->s_flags |= SB_ACTIVE;
1477 close_ctree(fs_info);
1481 int btrfs_sync_fs(struct super_block *sb, int wait)
1483 struct btrfs_trans_handle *trans;
1484 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1485 struct btrfs_root *root = fs_info->tree_root;
1487 trace_btrfs_sync_fs(fs_info, wait);
1490 filemap_flush(fs_info->btree_inode->i_mapping);
1494 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1496 trans = btrfs_attach_transaction_barrier(root);
1497 if (IS_ERR(trans)) {
1498 /* no transaction, don't bother */
1499 if (PTR_ERR(trans) == -ENOENT) {
1501 * Exit unless we have some pending changes
1502 * that need to go through commit
1504 if (fs_info->pending_changes == 0)
1507 * A non-blocking test if the fs is frozen. We must not
1508 * start a new transaction here otherwise a deadlock
1509 * happens. The pending operations are delayed to the
1510 * next commit after thawing.
1512 if (sb_start_write_trylock(sb))
1516 trans = btrfs_start_transaction(root, 0);
1519 return PTR_ERR(trans);
1521 return btrfs_commit_transaction(trans);
1524 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1526 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1530 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1532 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1533 const char *compress_type;
1534 const char *subvol_name;
1535 bool printed = false;
1537 if (btrfs_test_opt(info, DEGRADED))
1538 seq_puts(seq, ",degraded");
1539 if (btrfs_test_opt(info, NODATASUM))
1540 seq_puts(seq, ",nodatasum");
1541 if (btrfs_test_opt(info, NODATACOW))
1542 seq_puts(seq, ",nodatacow");
1543 if (btrfs_test_opt(info, NOBARRIER))
1544 seq_puts(seq, ",nobarrier");
1545 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1546 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1547 if (info->thread_pool_size != min_t(unsigned long,
1548 num_online_cpus() + 2, 8))
1549 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1550 if (btrfs_test_opt(info, COMPRESS)) {
1551 compress_type = btrfs_compress_type2str(info->compress_type);
1552 if (btrfs_test_opt(info, FORCE_COMPRESS))
1553 seq_printf(seq, ",compress-force=%s", compress_type);
1555 seq_printf(seq, ",compress=%s", compress_type);
1556 if (info->compress_level)
1557 seq_printf(seq, ":%d", info->compress_level);
1559 if (btrfs_test_opt(info, NOSSD))
1560 seq_puts(seq, ",nossd");
1561 if (btrfs_test_opt(info, SSD_SPREAD))
1562 seq_puts(seq, ",ssd_spread");
1563 else if (btrfs_test_opt(info, SSD))
1564 seq_puts(seq, ",ssd");
1565 if (btrfs_test_opt(info, NOTREELOG))
1566 seq_puts(seq, ",notreelog");
1567 if (btrfs_test_opt(info, NOLOGREPLAY))
1568 print_rescue_option(seq, "nologreplay", &printed);
1569 if (btrfs_test_opt(info, USEBACKUPROOT))
1570 print_rescue_option(seq, "usebackuproot", &printed);
1571 if (btrfs_test_opt(info, IGNOREBADROOTS))
1572 print_rescue_option(seq, "ignorebadroots", &printed);
1573 if (btrfs_test_opt(info, IGNOREDATACSUMS))
1574 print_rescue_option(seq, "ignoredatacsums", &printed);
1575 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1576 seq_puts(seq, ",flushoncommit");
1577 if (btrfs_test_opt(info, DISCARD_SYNC))
1578 seq_puts(seq, ",discard");
1579 if (btrfs_test_opt(info, DISCARD_ASYNC))
1580 seq_puts(seq, ",discard=async");
1581 if (!(info->sb->s_flags & SB_POSIXACL))
1582 seq_puts(seq, ",noacl");
1583 if (btrfs_free_space_cache_v1_active(info))
1584 seq_puts(seq, ",space_cache");
1585 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
1586 seq_puts(seq, ",space_cache=v2");
1588 seq_puts(seq, ",nospace_cache");
1589 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1590 seq_puts(seq, ",rescan_uuid_tree");
1591 if (btrfs_test_opt(info, CLEAR_CACHE))
1592 seq_puts(seq, ",clear_cache");
1593 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1594 seq_puts(seq, ",user_subvol_rm_allowed");
1595 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1596 seq_puts(seq, ",enospc_debug");
1597 if (btrfs_test_opt(info, AUTO_DEFRAG))
1598 seq_puts(seq, ",autodefrag");
1599 if (btrfs_test_opt(info, SKIP_BALANCE))
1600 seq_puts(seq, ",skip_balance");
1601 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1602 if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
1603 seq_puts(seq, ",check_int_data");
1604 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1605 seq_puts(seq, ",check_int");
1606 if (info->check_integrity_print_mask)
1607 seq_printf(seq, ",check_int_print_mask=%d",
1608 info->check_integrity_print_mask);
1610 if (info->metadata_ratio)
1611 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1612 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1613 seq_puts(seq, ",fatal_errors=panic");
1614 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1615 seq_printf(seq, ",commit=%u", info->commit_interval);
1616 #ifdef CONFIG_BTRFS_DEBUG
1617 if (btrfs_test_opt(info, FRAGMENT_DATA))
1618 seq_puts(seq, ",fragment=data");
1619 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1620 seq_puts(seq, ",fragment=metadata");
1622 if (btrfs_test_opt(info, REF_VERIFY))
1623 seq_puts(seq, ",ref_verify");
1624 seq_printf(seq, ",subvolid=%llu",
1625 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1626 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1627 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1628 if (!IS_ERR(subvol_name)) {
1629 seq_puts(seq, ",subvol=");
1630 seq_escape(seq, subvol_name, " \t\n\\");
1636 static int btrfs_test_super(struct super_block *s, void *data)
1638 struct btrfs_fs_info *p = data;
1639 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1641 return fs_info->fs_devices == p->fs_devices;
1644 static int btrfs_set_super(struct super_block *s, void *data)
1646 int err = set_anon_super(s, data);
1648 s->s_fs_info = data;
1653 * subvolumes are identified by ino 256
1655 static inline int is_subvolume_inode(struct inode *inode)
1657 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1662 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1663 struct vfsmount *mnt)
1665 struct dentry *root;
1669 if (!subvol_objectid) {
1670 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1673 root = ERR_PTR(ret);
1677 subvol_name = btrfs_get_subvol_name_from_objectid(
1678 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1679 if (IS_ERR(subvol_name)) {
1680 root = ERR_CAST(subvol_name);
1687 root = mount_subtree(mnt, subvol_name);
1688 /* mount_subtree() drops our reference on the vfsmount. */
1691 if (!IS_ERR(root)) {
1692 struct super_block *s = root->d_sb;
1693 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1694 struct inode *root_inode = d_inode(root);
1695 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1698 if (!is_subvolume_inode(root_inode)) {
1699 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1703 if (subvol_objectid && root_objectid != subvol_objectid) {
1705 * This will also catch a race condition where a
1706 * subvolume which was passed by ID is renamed and
1707 * another subvolume is renamed over the old location.
1710 "subvol '%s' does not match subvolid %llu",
1711 subvol_name, subvol_objectid);
1716 root = ERR_PTR(ret);
1717 deactivate_locked_super(s);
1728 * Find a superblock for the given device / mount point.
1730 * Note: This is based on mount_bdev from fs/super.c with a few additions
1731 * for multiple device setup. Make sure to keep it in sync.
1733 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1734 int flags, const char *device_name, void *data)
1736 struct block_device *bdev = NULL;
1737 struct super_block *s;
1738 struct btrfs_device *device = NULL;
1739 struct btrfs_fs_devices *fs_devices = NULL;
1740 struct btrfs_fs_info *fs_info = NULL;
1741 void *new_sec_opts = NULL;
1742 fmode_t mode = FMODE_READ;
1745 if (!(flags & SB_RDONLY))
1746 mode |= FMODE_WRITE;
1749 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1751 return ERR_PTR(error);
1755 * Setup a dummy root and fs_info for test/set super. This is because
1756 * we don't actually fill this stuff out until open_ctree, but we need
1757 * then open_ctree will properly initialize the file system specific
1758 * settings later. btrfs_init_fs_info initializes the static elements
1759 * of the fs_info (locks and such) to make cleanup easier if we find a
1760 * superblock with our given fs_devices later on at sget() time.
1762 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1765 goto error_sec_opts;
1767 btrfs_init_fs_info(fs_info);
1769 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1770 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1771 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1776 mutex_lock(&uuid_mutex);
1777 error = btrfs_parse_device_options(data, mode, fs_type);
1779 mutex_unlock(&uuid_mutex);
1783 device = btrfs_scan_one_device(device_name, mode, fs_type);
1784 if (IS_ERR(device)) {
1785 mutex_unlock(&uuid_mutex);
1786 error = PTR_ERR(device);
1790 fs_devices = device->fs_devices;
1791 fs_info->fs_devices = fs_devices;
1793 error = btrfs_open_devices(fs_devices, mode, fs_type);
1794 mutex_unlock(&uuid_mutex);
1798 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1800 goto error_close_devices;
1803 bdev = fs_devices->latest_dev->bdev;
1804 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1808 goto error_close_devices;
1812 btrfs_close_devices(fs_devices);
1813 btrfs_free_fs_info(fs_info);
1814 if ((flags ^ s->s_flags) & SB_RDONLY)
1817 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1818 btrfs_sb(s)->bdev_holder = fs_type;
1819 if (!strstr(crc32c_impl(), "generic"))
1820 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
1821 error = btrfs_fill_super(s, fs_devices, data);
1824 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1825 security_free_mnt_opts(&new_sec_opts);
1827 deactivate_locked_super(s);
1828 return ERR_PTR(error);
1831 return dget(s->s_root);
1833 error_close_devices:
1834 btrfs_close_devices(fs_devices);
1836 btrfs_free_fs_info(fs_info);
1838 security_free_mnt_opts(&new_sec_opts);
1839 return ERR_PTR(error);
1843 * Mount function which is called by VFS layer.
1845 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1846 * which needs vfsmount* of device's root (/). This means device's root has to
1847 * be mounted internally in any case.
1850 * 1. Parse subvol id related options for later use in mount_subvol().
1852 * 2. Mount device's root (/) by calling vfs_kern_mount().
1854 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1855 * first place. In order to avoid calling btrfs_mount() again, we use
1856 * different file_system_type which is not registered to VFS by
1857 * register_filesystem() (btrfs_root_fs_type). As a result,
1858 * btrfs_mount_root() is called. The return value will be used by
1859 * mount_subtree() in mount_subvol().
1861 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1862 * "btrfs subvolume set-default", mount_subvol() is called always.
1864 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1865 const char *device_name, void *data)
1867 struct vfsmount *mnt_root;
1868 struct dentry *root;
1869 char *subvol_name = NULL;
1870 u64 subvol_objectid = 0;
1873 error = btrfs_parse_subvol_options(data, &subvol_name,
1877 return ERR_PTR(error);
1880 /* mount device's root (/) */
1881 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1882 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1883 if (flags & SB_RDONLY) {
1884 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1885 flags & ~SB_RDONLY, device_name, data);
1887 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1888 flags | SB_RDONLY, device_name, data);
1889 if (IS_ERR(mnt_root)) {
1890 root = ERR_CAST(mnt_root);
1895 down_write(&mnt_root->mnt_sb->s_umount);
1896 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1897 up_write(&mnt_root->mnt_sb->s_umount);
1899 root = ERR_PTR(error);
1906 if (IS_ERR(mnt_root)) {
1907 root = ERR_CAST(mnt_root);
1912 /* mount_subvol() will free subvol_name and mnt_root */
1913 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1919 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1920 u32 new_pool_size, u32 old_pool_size)
1922 if (new_pool_size == old_pool_size)
1925 fs_info->thread_pool_size = new_pool_size;
1927 btrfs_info(fs_info, "resize thread pool %d -> %d",
1928 old_pool_size, new_pool_size);
1930 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1931 btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size);
1932 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1933 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1934 btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1935 btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1936 btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1938 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1939 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1940 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1943 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1944 unsigned long old_opts, int flags)
1946 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1947 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1948 (flags & SB_RDONLY))) {
1949 /* wait for any defraggers to finish */
1950 wait_event(fs_info->transaction_wait,
1951 (atomic_read(&fs_info->defrag_running) == 0));
1952 if (flags & SB_RDONLY)
1953 sync_filesystem(fs_info->sb);
1957 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1958 unsigned long old_opts)
1960 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1963 * We need to cleanup all defragable inodes if the autodefragment is
1964 * close or the filesystem is read only.
1966 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1967 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1968 btrfs_cleanup_defrag_inodes(fs_info);
1971 /* If we toggled discard async */
1972 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1973 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1974 btrfs_discard_resume(fs_info);
1975 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1976 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1977 btrfs_discard_cleanup(fs_info);
1979 /* If we toggled space cache */
1980 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1981 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1984 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1986 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1987 unsigned old_flags = sb->s_flags;
1988 unsigned long old_opts = fs_info->mount_opt;
1989 unsigned long old_compress_type = fs_info->compress_type;
1990 u64 old_max_inline = fs_info->max_inline;
1991 u32 old_thread_pool_size = fs_info->thread_pool_size;
1992 u32 old_metadata_ratio = fs_info->metadata_ratio;
1995 sync_filesystem(sb);
1996 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1999 void *new_sec_opts = NULL;
2001 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
2003 ret = security_sb_remount(sb, new_sec_opts);
2004 security_free_mnt_opts(&new_sec_opts);
2009 ret = btrfs_parse_options(fs_info, data, *flags);
2013 /* V1 cache is not supported for subpage mount. */
2014 if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
2016 "v1 space cache is not supported for page size %lu with sectorsize %u",
2017 PAGE_SIZE, fs_info->sectorsize);
2021 btrfs_remount_begin(fs_info, old_opts, *flags);
2022 btrfs_resize_thread_pool(fs_info,
2023 fs_info->thread_pool_size, old_thread_pool_size);
2025 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
2026 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
2027 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
2029 "remount supports changing free space tree only from ro to rw");
2030 /* Make sure free space cache options match the state on disk */
2031 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2032 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2033 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
2035 if (btrfs_free_space_cache_v1_active(fs_info)) {
2036 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2037 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
2041 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
2044 if (*flags & SB_RDONLY) {
2046 * this also happens on 'umount -rf' or on shutdown, when
2047 * the filesystem is busy.
2049 cancel_work_sync(&fs_info->async_reclaim_work);
2050 cancel_work_sync(&fs_info->async_data_reclaim_work);
2052 btrfs_discard_cleanup(fs_info);
2054 /* wait for the uuid_scan task to finish */
2055 down(&fs_info->uuid_tree_rescan_sem);
2056 /* avoid complains from lockdep et al. */
2057 up(&fs_info->uuid_tree_rescan_sem);
2059 btrfs_set_sb_rdonly(sb);
2062 * Setting SB_RDONLY will put the cleaner thread to
2063 * sleep at the next loop if it's already active.
2064 * If it's already asleep, we'll leave unused block
2065 * groups on disk until we're mounted read-write again
2066 * unless we clean them up here.
2068 btrfs_delete_unused_bgs(fs_info);
2071 * The cleaner task could be already running before we set the
2072 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
2073 * We must make sure that after we finish the remount, i.e. after
2074 * we call btrfs_commit_super(), the cleaner can no longer start
2075 * a transaction - either because it was dropping a dead root,
2076 * running delayed iputs or deleting an unused block group (the
2077 * cleaner picked a block group from the list of unused block
2078 * groups before we were able to in the previous call to
2079 * btrfs_delete_unused_bgs()).
2081 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
2082 TASK_UNINTERRUPTIBLE);
2085 * We've set the superblock to RO mode, so we might have made
2086 * the cleaner task sleep without running all pending delayed
2087 * iputs. Go through all the delayed iputs here, so that if an
2088 * unmount happens without remounting RW we don't end up at
2089 * finishing close_ctree() with a non-empty list of delayed
2092 btrfs_run_delayed_iputs(fs_info);
2094 btrfs_dev_replace_suspend_for_unmount(fs_info);
2095 btrfs_scrub_cancel(fs_info);
2096 btrfs_pause_balance(fs_info);
2099 * Pause the qgroup rescan worker if it is running. We don't want
2100 * it to be still running after we are in RO mode, as after that,
2101 * by the time we unmount, it might have left a transaction open,
2102 * so we would leak the transaction and/or crash.
2104 btrfs_qgroup_wait_for_completion(fs_info, false);
2106 ret = btrfs_commit_super(fs_info);
2110 if (BTRFS_FS_ERROR(fs_info)) {
2112 "Remounting read-write after error is not allowed");
2116 if (fs_info->fs_devices->rw_devices == 0) {
2121 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2123 "too many missing devices, writable remount is not allowed");
2128 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
2130 "mount required to replay tree-log, cannot remount read-write");
2136 * NOTE: when remounting with a change that does writes, don't
2137 * put it anywhere above this point, as we are not sure to be
2138 * safe to write until we pass the above checks.
2140 ret = btrfs_start_pre_rw_mount(fs_info);
2144 btrfs_clear_sb_rdonly(sb);
2146 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2150 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2151 * since the absence of the flag means it can be toggled off by remount.
2153 *flags |= SB_I_VERSION;
2155 wake_up_process(fs_info->transaction_kthread);
2156 btrfs_remount_cleanup(fs_info, old_opts);
2157 btrfs_clear_oneshot_options(fs_info);
2158 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2163 /* We've hit an error - don't reset SB_RDONLY */
2165 old_flags |= SB_RDONLY;
2166 if (!(old_flags & SB_RDONLY))
2167 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
2168 sb->s_flags = old_flags;
2169 fs_info->mount_opt = old_opts;
2170 fs_info->compress_type = old_compress_type;
2171 fs_info->max_inline = old_max_inline;
2172 btrfs_resize_thread_pool(fs_info,
2173 old_thread_pool_size, fs_info->thread_pool_size);
2174 fs_info->metadata_ratio = old_metadata_ratio;
2175 btrfs_remount_cleanup(fs_info, old_opts);
2176 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2181 /* Used to sort the devices by max_avail(descending sort) */
2182 static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2184 const struct btrfs_device_info *dev_info1 = a;
2185 const struct btrfs_device_info *dev_info2 = b;
2187 if (dev_info1->max_avail > dev_info2->max_avail)
2189 else if (dev_info1->max_avail < dev_info2->max_avail)
2195 * sort the devices by max_avail, in which max free extent size of each device
2196 * is stored.(Descending Sort)
2198 static inline void btrfs_descending_sort_devices(
2199 struct btrfs_device_info *devices,
2202 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2203 btrfs_cmp_device_free_bytes, NULL);
2207 * The helper to calc the free space on the devices that can be used to store
2210 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
2213 struct btrfs_device_info *devices_info;
2214 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2215 struct btrfs_device *device;
2218 u64 min_stripe_size;
2219 int num_stripes = 1;
2220 int i = 0, nr_devices;
2221 const struct btrfs_raid_attr *rattr;
2224 * We aren't under the device list lock, so this is racy-ish, but good
2225 * enough for our purposes.
2227 nr_devices = fs_info->fs_devices->open_devices;
2230 nr_devices = fs_info->fs_devices->open_devices;
2238 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
2243 /* calc min stripe number for data space allocation */
2244 type = btrfs_data_alloc_profile(fs_info);
2245 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
2247 if (type & BTRFS_BLOCK_GROUP_RAID0)
2248 num_stripes = nr_devices;
2249 else if (type & BTRFS_BLOCK_GROUP_RAID1)
2251 else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
2253 else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
2255 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2258 /* Adjust for more than 1 stripe per device */
2259 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
2262 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2263 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2264 &device->dev_state) ||
2266 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
2269 if (i >= nr_devices)
2272 avail_space = device->total_bytes - device->bytes_used;
2274 /* align with stripe_len */
2275 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
2278 * In order to avoid overwriting the superblock on the drive,
2279 * btrfs starts at an offset of at least 1MB when doing chunk
2282 * This ensures we have at least min_stripe_size free space
2283 * after excluding 1MB.
2285 if (avail_space <= SZ_1M + min_stripe_size)
2288 avail_space -= SZ_1M;
2290 devices_info[i].dev = device;
2291 devices_info[i].max_avail = avail_space;
2299 btrfs_descending_sort_devices(devices_info, nr_devices);
2303 while (nr_devices >= rattr->devs_min) {
2304 num_stripes = min(num_stripes, nr_devices);
2306 if (devices_info[i].max_avail >= min_stripe_size) {
2310 avail_space += devices_info[i].max_avail * num_stripes;
2311 alloc_size = devices_info[i].max_avail;
2312 for (j = i + 1 - num_stripes; j <= i; j++)
2313 devices_info[j].max_avail -= alloc_size;
2319 kfree(devices_info);
2320 *free_bytes = avail_space;
2325 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2327 * If there's a redundant raid level at DATA block groups, use the respective
2328 * multiplier to scale the sizes.
2330 * Unused device space usage is based on simulating the chunk allocator
2331 * algorithm that respects the device sizes and order of allocations. This is
2332 * a close approximation of the actual use but there are other factors that may
2333 * change the result (like a new metadata chunk).
2335 * If metadata is exhausted, f_bavail will be 0.
2337 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2339 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2340 struct btrfs_super_block *disk_super = fs_info->super_copy;
2341 struct btrfs_space_info *found;
2343 u64 total_free_data = 0;
2344 u64 total_free_meta = 0;
2345 u32 bits = fs_info->sectorsize_bits;
2346 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2347 unsigned factor = 1;
2348 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2353 list_for_each_entry(found, &fs_info->space_info, list) {
2354 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2357 total_free_data += found->disk_total - found->disk_used;
2359 btrfs_account_ro_block_groups_free_space(found);
2361 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2362 if (!list_empty(&found->block_groups[i]))
2363 factor = btrfs_bg_type_to_factor(
2364 btrfs_raid_array[i].bg_flag);
2369 * Metadata in mixed block goup profiles are accounted in data
2371 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2372 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2375 total_free_meta += found->disk_total -
2379 total_used += found->disk_used;
2382 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2383 buf->f_blocks >>= bits;
2384 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2386 /* Account global block reserve as used, it's in logical size already */
2387 spin_lock(&block_rsv->lock);
2388 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2389 if (buf->f_bfree >= block_rsv->size >> bits)
2390 buf->f_bfree -= block_rsv->size >> bits;
2393 spin_unlock(&block_rsv->lock);
2395 buf->f_bavail = div_u64(total_free_data, factor);
2396 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2399 buf->f_bavail += div_u64(total_free_data, factor);
2400 buf->f_bavail = buf->f_bavail >> bits;
2403 * We calculate the remaining metadata space minus global reserve. If
2404 * this is (supposedly) smaller than zero, there's no space. But this
2405 * does not hold in practice, the exhausted state happens where's still
2406 * some positive delta. So we apply some guesswork and compare the
2407 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2409 * We probably cannot calculate the exact threshold value because this
2410 * depends on the internal reservations requested by various
2411 * operations, so some operations that consume a few metadata will
2412 * succeed even if the Avail is zero. But this is better than the other
2418 * We only want to claim there's no available space if we can no longer
2419 * allocate chunks for our metadata profile and our global reserve will
2420 * not fit in the free metadata space. If we aren't ->full then we
2421 * still can allocate chunks and thus are fine using the currently
2422 * calculated f_bavail.
2424 if (!mixed && block_rsv->space_info->full &&
2425 total_free_meta - thresh < block_rsv->size)
2428 buf->f_type = BTRFS_SUPER_MAGIC;
2429 buf->f_bsize = dentry->d_sb->s_blocksize;
2430 buf->f_namelen = BTRFS_NAME_LEN;
2432 /* We treat it as constant endianness (it doesn't matter _which_)
2433 because we want the fsid to come out the same whether mounted
2434 on a big-endian or little-endian host */
2435 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2436 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2437 /* Mask in the root object ID too, to disambiguate subvols */
2438 buf->f_fsid.val[0] ^=
2439 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2440 buf->f_fsid.val[1] ^=
2441 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2446 static void btrfs_kill_super(struct super_block *sb)
2448 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2449 kill_anon_super(sb);
2450 btrfs_free_fs_info(fs_info);
2453 static struct file_system_type btrfs_fs_type = {
2454 .owner = THIS_MODULE,
2456 .mount = btrfs_mount,
2457 .kill_sb = btrfs_kill_super,
2458 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2461 static struct file_system_type btrfs_root_fs_type = {
2462 .owner = THIS_MODULE,
2464 .mount = btrfs_mount_root,
2465 .kill_sb = btrfs_kill_super,
2466 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2469 MODULE_ALIAS_FS("btrfs");
2471 static int btrfs_control_open(struct inode *inode, struct file *file)
2474 * The control file's private_data is used to hold the
2475 * transaction when it is started and is used to keep
2476 * track of whether a transaction is already in progress.
2478 file->private_data = NULL;
2483 * Used by /dev/btrfs-control for devices ioctls.
2485 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2488 struct btrfs_ioctl_vol_args *vol;
2489 struct btrfs_device *device = NULL;
2493 if (!capable(CAP_SYS_ADMIN))
2496 vol = memdup_user((void __user *)arg, sizeof(*vol));
2498 return PTR_ERR(vol);
2499 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2502 case BTRFS_IOC_SCAN_DEV:
2503 mutex_lock(&uuid_mutex);
2504 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2505 &btrfs_root_fs_type);
2506 ret = PTR_ERR_OR_ZERO(device);
2507 mutex_unlock(&uuid_mutex);
2509 case BTRFS_IOC_FORGET_DEV:
2510 if (vol->name[0] != 0) {
2511 ret = lookup_bdev(vol->name, &devt);
2515 ret = btrfs_forget_devices(devt);
2517 case BTRFS_IOC_DEVICES_READY:
2518 mutex_lock(&uuid_mutex);
2519 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2520 &btrfs_root_fs_type);
2521 if (IS_ERR(device)) {
2522 mutex_unlock(&uuid_mutex);
2523 ret = PTR_ERR(device);
2526 ret = !(device->fs_devices->num_devices ==
2527 device->fs_devices->total_devices);
2528 mutex_unlock(&uuid_mutex);
2530 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2531 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2539 static int btrfs_freeze(struct super_block *sb)
2541 struct btrfs_trans_handle *trans;
2542 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2543 struct btrfs_root *root = fs_info->tree_root;
2545 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2547 * We don't need a barrier here, we'll wait for any transaction that
2548 * could be in progress on other threads (and do delayed iputs that
2549 * we want to avoid on a frozen filesystem), or do the commit
2552 trans = btrfs_attach_transaction_barrier(root);
2553 if (IS_ERR(trans)) {
2554 /* no transaction, don't bother */
2555 if (PTR_ERR(trans) == -ENOENT)
2557 return PTR_ERR(trans);
2559 return btrfs_commit_transaction(trans);
2562 static int btrfs_unfreeze(struct super_block *sb)
2564 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2566 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2570 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2572 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2575 * There should be always a valid pointer in latest_dev, it may be stale
2576 * for a short moment in case it's being deleted but still valid until
2577 * the end of RCU grace period.
2580 seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
2586 static const struct super_operations btrfs_super_ops = {
2587 .drop_inode = btrfs_drop_inode,
2588 .evict_inode = btrfs_evict_inode,
2589 .put_super = btrfs_put_super,
2590 .sync_fs = btrfs_sync_fs,
2591 .show_options = btrfs_show_options,
2592 .show_devname = btrfs_show_devname,
2593 .alloc_inode = btrfs_alloc_inode,
2594 .destroy_inode = btrfs_destroy_inode,
2595 .free_inode = btrfs_free_inode,
2596 .statfs = btrfs_statfs,
2597 .remount_fs = btrfs_remount,
2598 .freeze_fs = btrfs_freeze,
2599 .unfreeze_fs = btrfs_unfreeze,
2602 static const struct file_operations btrfs_ctl_fops = {
2603 .open = btrfs_control_open,
2604 .unlocked_ioctl = btrfs_control_ioctl,
2605 .compat_ioctl = compat_ptr_ioctl,
2606 .owner = THIS_MODULE,
2607 .llseek = noop_llseek,
2610 static struct miscdevice btrfs_misc = {
2611 .minor = BTRFS_MINOR,
2612 .name = "btrfs-control",
2613 .fops = &btrfs_ctl_fops
2616 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2617 MODULE_ALIAS("devname:btrfs-control");
2619 static int __init btrfs_interface_init(void)
2621 return misc_register(&btrfs_misc);
2624 static __cold void btrfs_interface_exit(void)
2626 misc_deregister(&btrfs_misc);
2629 static void __init btrfs_print_mod_info(void)
2631 static const char options[] = ""
2632 #ifdef CONFIG_BTRFS_DEBUG
2635 #ifdef CONFIG_BTRFS_ASSERT
2638 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2639 ", integrity-checker=on"
2641 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2644 #ifdef CONFIG_BLK_DEV_ZONED
2649 #ifdef CONFIG_FS_VERITY
2655 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
2658 static int __init init_btrfs_fs(void)
2664 err = btrfs_init_sysfs();
2668 btrfs_init_compress();
2670 err = btrfs_init_cachep();
2674 err = extent_io_init();
2678 err = extent_state_cache_init();
2680 goto free_extent_io;
2682 err = extent_map_init();
2684 goto free_extent_state_cache;
2686 err = ordered_data_init();
2688 goto free_extent_map;
2690 err = btrfs_delayed_inode_init();
2692 goto free_ordered_data;
2694 err = btrfs_auto_defrag_init();
2696 goto free_delayed_inode;
2698 err = btrfs_delayed_ref_init();
2700 goto free_auto_defrag;
2702 err = btrfs_prelim_ref_init();
2704 goto free_delayed_ref;
2706 err = btrfs_end_io_wq_init();
2708 goto free_prelim_ref;
2710 err = btrfs_interface_init();
2712 goto free_end_io_wq;
2714 btrfs_print_mod_info();
2716 err = btrfs_run_sanity_tests();
2718 goto unregister_ioctl;
2720 err = register_filesystem(&btrfs_fs_type);
2722 goto unregister_ioctl;
2727 btrfs_interface_exit();
2729 btrfs_end_io_wq_exit();
2731 btrfs_prelim_ref_exit();
2733 btrfs_delayed_ref_exit();
2735 btrfs_auto_defrag_exit();
2737 btrfs_delayed_inode_exit();
2739 ordered_data_exit();
2742 free_extent_state_cache:
2743 extent_state_cache_exit();
2747 btrfs_destroy_cachep();
2749 btrfs_exit_compress();
2755 static void __exit exit_btrfs_fs(void)
2757 btrfs_destroy_cachep();
2758 btrfs_delayed_ref_exit();
2759 btrfs_auto_defrag_exit();
2760 btrfs_delayed_inode_exit();
2761 btrfs_prelim_ref_exit();
2762 ordered_data_exit();
2764 extent_state_cache_exit();
2766 btrfs_interface_exit();
2767 btrfs_end_io_wq_exit();
2768 unregister_filesystem(&btrfs_fs_type);
2770 btrfs_cleanup_fs_uuids();
2771 btrfs_exit_compress();
2774 late_initcall(init_btrfs_fs);
2775 module_exit(exit_btrfs_fs)
2777 MODULE_LICENSE("GPL");
2778 MODULE_SOFTDEP("pre: crc32c");
2779 MODULE_SOFTDEP("pre: xxhash64");
2780 MODULE_SOFTDEP("pre: sha256");
2781 MODULE_SOFTDEP("pre: blake2b-256");