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/cleancache.h>
27 #include <linux/ratelimit.h>
28 #include <linux/crc32c.h>
29 #include <linux/btrfs.h>
30 #include "delayed-inode.h"
33 #include "transaction.h"
34 #include "btrfs_inode.h"
35 #include "print-tree.h"
40 #include "compression.h"
41 #include "rcu-string.h"
42 #include "dev-replace.h"
43 #include "free-space-cache.h"
45 #include "space-info.h"
48 #include "tests/btrfs-tests.h"
49 #include "block-group.h"
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/btrfs.h>
56 static const struct super_operations btrfs_super_ops
;
59 * Types for mounting the default subvolume and a subvolume explicitly
60 * requested by subvol=/path. That way the callchain is straightforward and we
61 * don't have to play tricks with the mount options and recursive calls to
64 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
66 static struct file_system_type btrfs_fs_type
;
67 static struct file_system_type btrfs_root_fs_type
;
69 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
);
72 * Generally the error codes correspond to their respective errors, but there
73 * are a few special cases.
75 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
76 * instance will return EUCLEAN if any of the blocks are corrupted in
77 * a way that is problematic. We want to reserve EUCLEAN for these
78 * sort of corruptions.
80 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
81 * need to use EROFS for this case. We will have no idea of the
82 * original failure, that will have been reported at the time we tripped
83 * over the error. Each subsequent error that doesn't have any context
84 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
86 const char * __attribute_const__
btrfs_decode_error(int errno
)
88 char *errstr
= "unknown";
91 case -ENOENT
: /* -2 */
92 errstr
= "No such entry";
95 errstr
= "IO failure";
97 case -ENOMEM
: /* -12*/
98 errstr
= "Out of memory";
100 case -EEXIST
: /* -17 */
101 errstr
= "Object already exists";
103 case -ENOSPC
: /* -28 */
104 errstr
= "No space left";
106 case -EROFS
: /* -30 */
107 errstr
= "Readonly filesystem";
109 case -EOPNOTSUPP
: /* -95 */
110 errstr
= "Operation not supported";
112 case -EUCLEAN
: /* -117 */
113 errstr
= "Filesystem corrupted";
115 case -EDQUOT
: /* -122 */
116 errstr
= "Quota exceeded";
124 * __btrfs_handle_fs_error decodes expected errors from the caller and
125 * invokes the appropriate error response.
128 void __btrfs_handle_fs_error(struct btrfs_fs_info
*fs_info
, const char *function
,
129 unsigned int line
, int errno
, const char *fmt
, ...)
131 struct super_block
*sb
= fs_info
->sb
;
137 * Special case: if the error is EROFS, and we're already
138 * under SB_RDONLY, then it is safe here.
140 if (errno
== -EROFS
&& sb_rdonly(sb
))
144 errstr
= btrfs_decode_error(errno
);
146 struct va_format vaf
;
153 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
154 sb
->s_id
, function
, line
, errno
, errstr
, &vaf
);
157 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
158 sb
->s_id
, function
, line
, errno
, errstr
);
163 * Today we only save the error info to memory. Long term we'll
164 * also send it down to the disk
166 set_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
);
168 /* Don't go through full error handling during mount */
169 if (!(sb
->s_flags
& SB_BORN
))
175 btrfs_discard_stop(fs_info
);
177 /* btrfs handle error by forcing the filesystem readonly */
178 btrfs_set_sb_rdonly(sb
);
179 btrfs_info(fs_info
, "forced readonly");
181 * Note that a running device replace operation is not canceled here
182 * although there is no way to update the progress. It would add the
183 * risk of a deadlock, therefore the canceling is omitted. The only
184 * penalty is that some I/O remains active until the procedure
185 * completes. The next time when the filesystem is mounted writable
186 * again, the device replace operation continues.
191 static const char * const logtypes
[] = {
204 * Use one ratelimit state per log level so that a flood of less important
205 * messages doesn't cause more important ones to be dropped.
207 static struct ratelimit_state printk_limits
[] = {
208 RATELIMIT_STATE_INIT(printk_limits
[0], DEFAULT_RATELIMIT_INTERVAL
, 100),
209 RATELIMIT_STATE_INIT(printk_limits
[1], DEFAULT_RATELIMIT_INTERVAL
, 100),
210 RATELIMIT_STATE_INIT(printk_limits
[2], DEFAULT_RATELIMIT_INTERVAL
, 100),
211 RATELIMIT_STATE_INIT(printk_limits
[3], DEFAULT_RATELIMIT_INTERVAL
, 100),
212 RATELIMIT_STATE_INIT(printk_limits
[4], DEFAULT_RATELIMIT_INTERVAL
, 100),
213 RATELIMIT_STATE_INIT(printk_limits
[5], DEFAULT_RATELIMIT_INTERVAL
, 100),
214 RATELIMIT_STATE_INIT(printk_limits
[6], DEFAULT_RATELIMIT_INTERVAL
, 100),
215 RATELIMIT_STATE_INIT(printk_limits
[7], DEFAULT_RATELIMIT_INTERVAL
, 100),
218 void __cold
btrfs_printk(const struct btrfs_fs_info
*fs_info
, const char *fmt
, ...)
220 char lvl
[PRINTK_MAX_SINGLE_HEADER_LEN
+ 1] = "\0";
221 struct va_format vaf
;
224 const char *type
= logtypes
[4];
225 struct ratelimit_state
*ratelimit
= &printk_limits
[4];
229 while ((kern_level
= printk_get_level(fmt
)) != 0) {
230 size_t size
= printk_skip_level(fmt
) - fmt
;
232 if (kern_level
>= '0' && kern_level
<= '7') {
233 memcpy(lvl
, fmt
, size
);
235 type
= logtypes
[kern_level
- '0'];
236 ratelimit
= &printk_limits
[kern_level
- '0'];
244 if (__ratelimit(ratelimit
)) {
246 printk("%sBTRFS %s (device %s): %pV\n", lvl
, type
,
247 fs_info
->sb
->s_id
, &vaf
);
249 printk("%sBTRFS %s: %pV\n", lvl
, type
, &vaf
);
257 * We only mark the transaction aborted and then set the file system read-only.
258 * This will prevent new transactions from starting or trying to join this
261 * This means that error recovery at the call site is limited to freeing
262 * any local memory allocations and passing the error code up without
263 * further cleanup. The transaction should complete as it normally would
264 * in the call path but will return -EIO.
266 * We'll complete the cleanup in btrfs_end_transaction and
267 * btrfs_commit_transaction.
270 void __btrfs_abort_transaction(struct btrfs_trans_handle
*trans
,
271 const char *function
,
272 unsigned int line
, int errno
)
274 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
276 WRITE_ONCE(trans
->aborted
, errno
);
277 /* Nothing used. The other threads that have joined this
278 * transaction may be able to continue. */
279 if (!trans
->dirty
&& list_empty(&trans
->new_bgs
)) {
282 errstr
= btrfs_decode_error(errno
);
284 "%s:%d: Aborting unused transaction(%s).",
285 function
, line
, errstr
);
288 WRITE_ONCE(trans
->transaction
->aborted
, errno
);
289 /* Wake up anybody who may be waiting on this transaction */
290 wake_up(&fs_info
->transaction_wait
);
291 wake_up(&fs_info
->transaction_blocked_wait
);
292 __btrfs_handle_fs_error(fs_info
, function
, line
, errno
, NULL
);
295 * __btrfs_panic decodes unexpected, fatal errors from the caller,
296 * issues an alert, and either panics or BUGs, depending on mount options.
299 void __btrfs_panic(struct btrfs_fs_info
*fs_info
, const char *function
,
300 unsigned int line
, int errno
, const char *fmt
, ...)
302 char *s_id
= "<unknown>";
304 struct va_format vaf
= { .fmt
= fmt
};
308 s_id
= fs_info
->sb
->s_id
;
313 errstr
= btrfs_decode_error(errno
);
314 if (fs_info
&& (btrfs_test_opt(fs_info
, PANIC_ON_FATAL_ERROR
)))
315 panic(KERN_CRIT
"BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
316 s_id
, function
, line
, &vaf
, errno
, errstr
);
318 btrfs_crit(fs_info
, "panic in %s:%d: %pV (errno=%d %s)",
319 function
, line
, &vaf
, errno
, errstr
);
321 /* Caller calls BUG() */
324 static void btrfs_put_super(struct super_block
*sb
)
326 close_ctree(btrfs_sb(sb
));
335 Opt_compress_force_type
,
340 Opt_flushoncommit
, Opt_noflushoncommit
,
342 Opt_barrier
, Opt_nobarrier
,
343 Opt_datacow
, Opt_nodatacow
,
344 Opt_datasum
, Opt_nodatasum
,
345 Opt_defrag
, Opt_nodefrag
,
346 Opt_discard
, Opt_nodiscard
,
350 Opt_rescan_uuid_tree
,
352 Opt_space_cache
, Opt_no_space_cache
,
353 Opt_space_cache_version
,
355 Opt_ssd_spread
, Opt_nossd_spread
,
360 Opt_treelog
, Opt_notreelog
,
361 Opt_user_subvol_rm_allowed
,
371 /* Deprecated options */
373 Opt_inode_cache
, Opt_noinode_cache
,
375 /* Debugging options */
377 Opt_check_integrity_including_extent_data
,
378 Opt_check_integrity_print_mask
,
379 Opt_enospc_debug
, Opt_noenospc_debug
,
380 #ifdef CONFIG_BTRFS_DEBUG
381 Opt_fragment_data
, Opt_fragment_metadata
, Opt_fragment_all
,
383 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
389 static const match_table_t tokens
= {
391 {Opt_noacl
, "noacl"},
392 {Opt_clear_cache
, "clear_cache"},
393 {Opt_commit_interval
, "commit=%u"},
394 {Opt_compress
, "compress"},
395 {Opt_compress_type
, "compress=%s"},
396 {Opt_compress_force
, "compress-force"},
397 {Opt_compress_force_type
, "compress-force=%s"},
398 {Opt_degraded
, "degraded"},
399 {Opt_device
, "device=%s"},
400 {Opt_fatal_errors
, "fatal_errors=%s"},
401 {Opt_flushoncommit
, "flushoncommit"},
402 {Opt_noflushoncommit
, "noflushoncommit"},
403 {Opt_inode_cache
, "inode_cache"},
404 {Opt_noinode_cache
, "noinode_cache"},
405 {Opt_max_inline
, "max_inline=%s"},
406 {Opt_barrier
, "barrier"},
407 {Opt_nobarrier
, "nobarrier"},
408 {Opt_datacow
, "datacow"},
409 {Opt_nodatacow
, "nodatacow"},
410 {Opt_datasum
, "datasum"},
411 {Opt_nodatasum
, "nodatasum"},
412 {Opt_defrag
, "autodefrag"},
413 {Opt_nodefrag
, "noautodefrag"},
414 {Opt_discard
, "discard"},
415 {Opt_discard_mode
, "discard=%s"},
416 {Opt_nodiscard
, "nodiscard"},
417 {Opt_norecovery
, "norecovery"},
418 {Opt_ratio
, "metadata_ratio=%u"},
419 {Opt_rescan_uuid_tree
, "rescan_uuid_tree"},
420 {Opt_skip_balance
, "skip_balance"},
421 {Opt_space_cache
, "space_cache"},
422 {Opt_no_space_cache
, "nospace_cache"},
423 {Opt_space_cache_version
, "space_cache=%s"},
425 {Opt_nossd
, "nossd"},
426 {Opt_ssd_spread
, "ssd_spread"},
427 {Opt_nossd_spread
, "nossd_spread"},
428 {Opt_subvol
, "subvol=%s"},
429 {Opt_subvol_empty
, "subvol="},
430 {Opt_subvolid
, "subvolid=%s"},
431 {Opt_thread_pool
, "thread_pool=%u"},
432 {Opt_treelog
, "treelog"},
433 {Opt_notreelog
, "notreelog"},
434 {Opt_user_subvol_rm_allowed
, "user_subvol_rm_allowed"},
437 {Opt_rescue
, "rescue=%s"},
438 /* Deprecated, with alias rescue=nologreplay */
439 {Opt_nologreplay
, "nologreplay"},
440 /* Deprecated, with alias rescue=usebackuproot */
441 {Opt_usebackuproot
, "usebackuproot"},
443 /* Deprecated options */
444 {Opt_recovery
, "recovery"},
446 /* Debugging options */
447 {Opt_check_integrity
, "check_int"},
448 {Opt_check_integrity_including_extent_data
, "check_int_data"},
449 {Opt_check_integrity_print_mask
, "check_int_print_mask=%u"},
450 {Opt_enospc_debug
, "enospc_debug"},
451 {Opt_noenospc_debug
, "noenospc_debug"},
452 #ifdef CONFIG_BTRFS_DEBUG
453 {Opt_fragment_data
, "fragment=data"},
454 {Opt_fragment_metadata
, "fragment=metadata"},
455 {Opt_fragment_all
, "fragment=all"},
457 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
458 {Opt_ref_verify
, "ref_verify"},
463 static const match_table_t rescue_tokens
= {
464 {Opt_usebackuproot
, "usebackuproot"},
465 {Opt_nologreplay
, "nologreplay"},
466 {Opt_ignorebadroots
, "ignorebadroots"},
467 {Opt_ignorebadroots
, "ibadroots"},
468 {Opt_ignoredatacsums
, "ignoredatacsums"},
469 {Opt_ignoredatacsums
, "idatacsums"},
470 {Opt_rescue_all
, "all"},
474 static bool check_ro_option(struct btrfs_fs_info
*fs_info
, unsigned long opt
,
475 const char *opt_name
)
477 if (fs_info
->mount_opt
& opt
) {
478 btrfs_err(fs_info
, "%s must be used with ro mount option",
485 static int parse_rescue_options(struct btrfs_fs_info
*info
, const char *options
)
490 substring_t args
[MAX_OPT_ARGS
];
493 opts
= kstrdup(options
, GFP_KERNEL
);
498 while ((p
= strsep(&opts
, ":")) != NULL
) {
503 token
= match_token(p
, rescue_tokens
, args
);
505 case Opt_usebackuproot
:
507 "trying to use backup root at mount time");
508 btrfs_set_opt(info
->mount_opt
, USEBACKUPROOT
);
510 case Opt_nologreplay
:
511 btrfs_set_and_info(info
, NOLOGREPLAY
,
512 "disabling log replay at mount time");
514 case Opt_ignorebadroots
:
515 btrfs_set_and_info(info
, IGNOREBADROOTS
,
516 "ignoring bad roots");
518 case Opt_ignoredatacsums
:
519 btrfs_set_and_info(info
, IGNOREDATACSUMS
,
520 "ignoring data csums");
523 btrfs_info(info
, "enabling all of the rescue options");
524 btrfs_set_and_info(info
, IGNOREDATACSUMS
,
525 "ignoring data csums");
526 btrfs_set_and_info(info
, IGNOREBADROOTS
,
527 "ignoring bad roots");
528 btrfs_set_and_info(info
, NOLOGREPLAY
,
529 "disabling log replay at mount time");
532 btrfs_info(info
, "unrecognized rescue option '%s'", p
);
546 * Regular mount options parser. Everything that is needed only when
547 * reading in a new superblock is parsed here.
548 * XXX JDM: This needs to be cleaned up for remount.
550 int btrfs_parse_options(struct btrfs_fs_info
*info
, char *options
,
551 unsigned long new_flags
)
553 substring_t args
[MAX_OPT_ARGS
];
558 bool compress_force
= false;
559 enum btrfs_compression_type saved_compress_type
;
560 int saved_compress_level
;
561 bool saved_compress_force
;
564 if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
))
565 btrfs_set_opt(info
->mount_opt
, FREE_SPACE_TREE
);
566 else if (btrfs_free_space_cache_v1_active(info
)) {
567 if (btrfs_is_zoned(info
)) {
569 "zoned: clearing existing space cache");
570 btrfs_set_super_cache_generation(info
->super_copy
, 0);
572 btrfs_set_opt(info
->mount_opt
, SPACE_CACHE
);
577 * Even the options are empty, we still need to do extra check
583 while ((p
= strsep(&options
, ",")) != NULL
) {
588 token
= match_token(p
, tokens
, args
);
591 btrfs_info(info
, "allowing degraded mounts");
592 btrfs_set_opt(info
->mount_opt
, DEGRADED
);
595 case Opt_subvol_empty
:
599 * These are parsed by btrfs_parse_subvol_options or
600 * btrfs_parse_device_options and can be ignored here.
604 btrfs_set_and_info(info
, NODATASUM
,
605 "setting nodatasum");
608 if (btrfs_test_opt(info
, NODATASUM
)) {
609 if (btrfs_test_opt(info
, NODATACOW
))
611 "setting datasum, datacow enabled");
613 btrfs_info(info
, "setting datasum");
615 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
616 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
619 if (!btrfs_test_opt(info
, NODATACOW
)) {
620 if (!btrfs_test_opt(info
, COMPRESS
) ||
621 !btrfs_test_opt(info
, FORCE_COMPRESS
)) {
623 "setting nodatacow, compression disabled");
625 btrfs_info(info
, "setting nodatacow");
628 btrfs_clear_opt(info
->mount_opt
, COMPRESS
);
629 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
630 btrfs_set_opt(info
->mount_opt
, NODATACOW
);
631 btrfs_set_opt(info
->mount_opt
, NODATASUM
);
634 btrfs_clear_and_info(info
, NODATACOW
,
637 case Opt_compress_force
:
638 case Opt_compress_force_type
:
639 compress_force
= true;
642 case Opt_compress_type
:
643 saved_compress_type
= btrfs_test_opt(info
,
645 info
->compress_type
: BTRFS_COMPRESS_NONE
;
646 saved_compress_force
=
647 btrfs_test_opt(info
, FORCE_COMPRESS
);
648 saved_compress_level
= info
->compress_level
;
649 if (token
== Opt_compress
||
650 token
== Opt_compress_force
||
651 strncmp(args
[0].from
, "zlib", 4) == 0) {
652 compress_type
= "zlib";
654 info
->compress_type
= BTRFS_COMPRESS_ZLIB
;
655 info
->compress_level
= BTRFS_ZLIB_DEFAULT_LEVEL
;
657 * args[0] contains uninitialized data since
658 * for these tokens we don't expect any
661 if (token
!= Opt_compress
&&
662 token
!= Opt_compress_force
)
663 info
->compress_level
=
664 btrfs_compress_str2level(
667 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
668 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
669 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
671 } else if (strncmp(args
[0].from
, "lzo", 3) == 0) {
672 compress_type
= "lzo";
673 info
->compress_type
= BTRFS_COMPRESS_LZO
;
674 info
->compress_level
= 0;
675 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
676 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
677 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
678 btrfs_set_fs_incompat(info
, COMPRESS_LZO
);
680 } else if (strncmp(args
[0].from
, "zstd", 4) == 0) {
681 compress_type
= "zstd";
682 info
->compress_type
= BTRFS_COMPRESS_ZSTD
;
683 info
->compress_level
=
684 btrfs_compress_str2level(
687 btrfs_set_opt(info
->mount_opt
, COMPRESS
);
688 btrfs_clear_opt(info
->mount_opt
, NODATACOW
);
689 btrfs_clear_opt(info
->mount_opt
, NODATASUM
);
690 btrfs_set_fs_incompat(info
, COMPRESS_ZSTD
);
692 } else if (strncmp(args
[0].from
, "no", 2) == 0) {
693 compress_type
= "no";
694 info
->compress_level
= 0;
695 info
->compress_type
= 0;
696 btrfs_clear_opt(info
->mount_opt
, COMPRESS
);
697 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
698 compress_force
= false;
705 if (compress_force
) {
706 btrfs_set_opt(info
->mount_opt
, FORCE_COMPRESS
);
709 * If we remount from compress-force=xxx to
710 * compress=xxx, we need clear FORCE_COMPRESS
711 * flag, otherwise, there is no way for users
712 * to disable forcible compression separately.
714 btrfs_clear_opt(info
->mount_opt
, FORCE_COMPRESS
);
716 if (no_compress
== 1) {
717 btrfs_info(info
, "use no compression");
718 } else if ((info
->compress_type
!= saved_compress_type
) ||
719 (compress_force
!= saved_compress_force
) ||
720 (info
->compress_level
!= saved_compress_level
)) {
721 btrfs_info(info
, "%s %s compression, level %d",
722 (compress_force
) ? "force" : "use",
723 compress_type
, info
->compress_level
);
725 compress_force
= false;
728 btrfs_set_and_info(info
, SSD
,
729 "enabling ssd optimizations");
730 btrfs_clear_opt(info
->mount_opt
, NOSSD
);
733 btrfs_set_and_info(info
, SSD
,
734 "enabling ssd optimizations");
735 btrfs_set_and_info(info
, SSD_SPREAD
,
736 "using spread ssd allocation scheme");
737 btrfs_clear_opt(info
->mount_opt
, NOSSD
);
740 btrfs_set_opt(info
->mount_opt
, NOSSD
);
741 btrfs_clear_and_info(info
, SSD
,
742 "not using ssd optimizations");
744 case Opt_nossd_spread
:
745 btrfs_clear_and_info(info
, SSD_SPREAD
,
746 "not using spread ssd allocation scheme");
749 btrfs_clear_and_info(info
, NOBARRIER
,
750 "turning on barriers");
753 btrfs_set_and_info(info
, NOBARRIER
,
754 "turning off barriers");
756 case Opt_thread_pool
:
757 ret
= match_int(&args
[0], &intarg
);
760 } else if (intarg
== 0) {
764 info
->thread_pool_size
= intarg
;
767 num
= match_strdup(&args
[0]);
769 info
->max_inline
= memparse(num
, NULL
);
772 if (info
->max_inline
) {
773 info
->max_inline
= min_t(u64
,
777 btrfs_info(info
, "max_inline at %llu",
785 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
786 info
->sb
->s_flags
|= SB_POSIXACL
;
789 btrfs_err(info
, "support for ACL not compiled in!");
794 info
->sb
->s_flags
&= ~SB_POSIXACL
;
797 btrfs_set_and_info(info
, NOTREELOG
,
798 "disabling tree log");
801 btrfs_clear_and_info(info
, NOTREELOG
,
802 "enabling tree log");
805 case Opt_nologreplay
:
807 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
808 btrfs_set_and_info(info
, NOLOGREPLAY
,
809 "disabling log replay at mount time");
811 case Opt_flushoncommit
:
812 btrfs_set_and_info(info
, FLUSHONCOMMIT
,
813 "turning on flush-on-commit");
815 case Opt_noflushoncommit
:
816 btrfs_clear_and_info(info
, FLUSHONCOMMIT
,
817 "turning off flush-on-commit");
820 ret
= match_int(&args
[0], &intarg
);
823 info
->metadata_ratio
= intarg
;
824 btrfs_info(info
, "metadata ratio %u",
825 info
->metadata_ratio
);
828 case Opt_discard_mode
:
829 if (token
== Opt_discard
||
830 strcmp(args
[0].from
, "sync") == 0) {
831 btrfs_clear_opt(info
->mount_opt
, DISCARD_ASYNC
);
832 btrfs_set_and_info(info
, DISCARD_SYNC
,
833 "turning on sync discard");
834 } else if (strcmp(args
[0].from
, "async") == 0) {
835 btrfs_clear_opt(info
->mount_opt
, DISCARD_SYNC
);
836 btrfs_set_and_info(info
, DISCARD_ASYNC
,
837 "turning on async discard");
844 btrfs_clear_and_info(info
, DISCARD_SYNC
,
845 "turning off discard");
846 btrfs_clear_and_info(info
, DISCARD_ASYNC
,
847 "turning off async discard");
849 case Opt_space_cache
:
850 case Opt_space_cache_version
:
851 if (token
== Opt_space_cache
||
852 strcmp(args
[0].from
, "v1") == 0) {
853 btrfs_clear_opt(info
->mount_opt
,
855 btrfs_set_and_info(info
, SPACE_CACHE
,
856 "enabling disk space caching");
857 } else if (strcmp(args
[0].from
, "v2") == 0) {
858 btrfs_clear_opt(info
->mount_opt
,
860 btrfs_set_and_info(info
, FREE_SPACE_TREE
,
861 "enabling free space tree");
867 case Opt_rescan_uuid_tree
:
868 btrfs_set_opt(info
->mount_opt
, RESCAN_UUID_TREE
);
870 case Opt_no_space_cache
:
871 if (btrfs_test_opt(info
, SPACE_CACHE
)) {
872 btrfs_clear_and_info(info
, SPACE_CACHE
,
873 "disabling disk space caching");
875 if (btrfs_test_opt(info
, FREE_SPACE_TREE
)) {
876 btrfs_clear_and_info(info
, FREE_SPACE_TREE
,
877 "disabling free space tree");
880 case Opt_inode_cache
:
881 case Opt_noinode_cache
:
883 "the 'inode_cache' option is deprecated and has no effect since 5.11");
885 case Opt_clear_cache
:
886 btrfs_set_and_info(info
, CLEAR_CACHE
,
887 "force clearing of disk cache");
889 case Opt_user_subvol_rm_allowed
:
890 btrfs_set_opt(info
->mount_opt
, USER_SUBVOL_RM_ALLOWED
);
892 case Opt_enospc_debug
:
893 btrfs_set_opt(info
->mount_opt
, ENOSPC_DEBUG
);
895 case Opt_noenospc_debug
:
896 btrfs_clear_opt(info
->mount_opt
, ENOSPC_DEBUG
);
899 btrfs_set_and_info(info
, AUTO_DEFRAG
,
900 "enabling auto defrag");
903 btrfs_clear_and_info(info
, AUTO_DEFRAG
,
904 "disabling auto defrag");
907 case Opt_usebackuproot
:
909 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
910 token
== Opt_recovery
? "recovery" :
913 "trying to use backup root at mount time");
914 btrfs_set_opt(info
->mount_opt
, USEBACKUPROOT
);
916 case Opt_skip_balance
:
917 btrfs_set_opt(info
->mount_opt
, SKIP_BALANCE
);
919 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
920 case Opt_check_integrity_including_extent_data
:
922 "enabling check integrity including extent data");
923 btrfs_set_opt(info
->mount_opt
,
924 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA
);
925 btrfs_set_opt(info
->mount_opt
, CHECK_INTEGRITY
);
927 case Opt_check_integrity
:
928 btrfs_info(info
, "enabling check integrity");
929 btrfs_set_opt(info
->mount_opt
, CHECK_INTEGRITY
);
931 case Opt_check_integrity_print_mask
:
932 ret
= match_int(&args
[0], &intarg
);
935 info
->check_integrity_print_mask
= intarg
;
936 btrfs_info(info
, "check_integrity_print_mask 0x%x",
937 info
->check_integrity_print_mask
);
940 case Opt_check_integrity_including_extent_data
:
941 case Opt_check_integrity
:
942 case Opt_check_integrity_print_mask
:
944 "support for check_integrity* not compiled in!");
948 case Opt_fatal_errors
:
949 if (strcmp(args
[0].from
, "panic") == 0)
950 btrfs_set_opt(info
->mount_opt
,
951 PANIC_ON_FATAL_ERROR
);
952 else if (strcmp(args
[0].from
, "bug") == 0)
953 btrfs_clear_opt(info
->mount_opt
,
954 PANIC_ON_FATAL_ERROR
);
960 case Opt_commit_interval
:
962 ret
= match_int(&args
[0], &intarg
);
967 "using default commit interval %us",
968 BTRFS_DEFAULT_COMMIT_INTERVAL
);
969 intarg
= BTRFS_DEFAULT_COMMIT_INTERVAL
;
970 } else if (intarg
> 300) {
971 btrfs_warn(info
, "excessive commit interval %d",
974 info
->commit_interval
= intarg
;
977 ret
= parse_rescue_options(info
, args
[0].from
);
981 #ifdef CONFIG_BTRFS_DEBUG
982 case Opt_fragment_all
:
983 btrfs_info(info
, "fragmenting all space");
984 btrfs_set_opt(info
->mount_opt
, FRAGMENT_DATA
);
985 btrfs_set_opt(info
->mount_opt
, FRAGMENT_METADATA
);
987 case Opt_fragment_metadata
:
988 btrfs_info(info
, "fragmenting metadata");
989 btrfs_set_opt(info
->mount_opt
,
992 case Opt_fragment_data
:
993 btrfs_info(info
, "fragmenting data");
994 btrfs_set_opt(info
->mount_opt
, FRAGMENT_DATA
);
997 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
999 btrfs_info(info
, "doing ref verification");
1000 btrfs_set_opt(info
->mount_opt
, REF_VERIFY
);
1004 btrfs_err(info
, "unrecognized mount option '%s'", p
);
1012 /* We're read-only, don't have to check. */
1013 if (new_flags
& SB_RDONLY
)
1016 if (check_ro_option(info
, BTRFS_MOUNT_NOLOGREPLAY
, "nologreplay") ||
1017 check_ro_option(info
, BTRFS_MOUNT_IGNOREBADROOTS
, "ignorebadroots") ||
1018 check_ro_option(info
, BTRFS_MOUNT_IGNOREDATACSUMS
, "ignoredatacsums"))
1021 if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
) &&
1022 !btrfs_test_opt(info
, FREE_SPACE_TREE
) &&
1023 !btrfs_test_opt(info
, CLEAR_CACHE
)) {
1024 btrfs_err(info
, "cannot disable free space tree");
1029 ret
= btrfs_check_mountopts_zoned(info
);
1030 if (!ret
&& btrfs_test_opt(info
, SPACE_CACHE
))
1031 btrfs_info(info
, "disk space caching is enabled");
1032 if (!ret
&& btrfs_test_opt(info
, FREE_SPACE_TREE
))
1033 btrfs_info(info
, "using free space tree");
1038 * Parse mount options that are required early in the mount process.
1040 * All other options will be parsed on much later in the mount process and
1041 * only when we need to allocate a new super block.
1043 static int btrfs_parse_device_options(const char *options
, fmode_t flags
,
1046 substring_t args
[MAX_OPT_ARGS
];
1047 char *device_name
, *opts
, *orig
, *p
;
1048 struct btrfs_device
*device
= NULL
;
1051 lockdep_assert_held(&uuid_mutex
);
1057 * strsep changes the string, duplicate it because btrfs_parse_options
1060 opts
= kstrdup(options
, GFP_KERNEL
);
1065 while ((p
= strsep(&opts
, ",")) != NULL
) {
1071 token
= match_token(p
, tokens
, args
);
1072 if (token
== Opt_device
) {
1073 device_name
= match_strdup(&args
[0]);
1078 device
= btrfs_scan_one_device(device_name
, flags
,
1081 if (IS_ERR(device
)) {
1082 error
= PTR_ERR(device
);
1094 * Parse mount options that are related to subvolume id
1096 * The value is later passed to mount_subvol()
1098 static int btrfs_parse_subvol_options(const char *options
, char **subvol_name
,
1099 u64
*subvol_objectid
)
1101 substring_t args
[MAX_OPT_ARGS
];
1102 char *opts
, *orig
, *p
;
1110 * strsep changes the string, duplicate it because
1111 * btrfs_parse_device_options gets called later
1113 opts
= kstrdup(options
, GFP_KERNEL
);
1118 while ((p
= strsep(&opts
, ",")) != NULL
) {
1123 token
= match_token(p
, tokens
, args
);
1126 kfree(*subvol_name
);
1127 *subvol_name
= match_strdup(&args
[0]);
1128 if (!*subvol_name
) {
1134 error
= match_u64(&args
[0], &subvolid
);
1138 /* we want the original fs_tree */
1140 subvolid
= BTRFS_FS_TREE_OBJECTID
;
1142 *subvol_objectid
= subvolid
;
1154 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info
*fs_info
,
1155 u64 subvol_objectid
)
1157 struct btrfs_root
*root
= fs_info
->tree_root
;
1158 struct btrfs_root
*fs_root
= NULL
;
1159 struct btrfs_root_ref
*root_ref
;
1160 struct btrfs_inode_ref
*inode_ref
;
1161 struct btrfs_key key
;
1162 struct btrfs_path
*path
= NULL
;
1163 char *name
= NULL
, *ptr
;
1168 path
= btrfs_alloc_path();
1174 name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1179 ptr
= name
+ PATH_MAX
- 1;
1183 * Walk up the subvolume trees in the tree of tree roots by root
1184 * backrefs until we hit the top-level subvolume.
1186 while (subvol_objectid
!= BTRFS_FS_TREE_OBJECTID
) {
1187 key
.objectid
= subvol_objectid
;
1188 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
1189 key
.offset
= (u64
)-1;
1191 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1194 } else if (ret
> 0) {
1195 ret
= btrfs_previous_item(root
, path
, subvol_objectid
,
1196 BTRFS_ROOT_BACKREF_KEY
);
1199 } else if (ret
> 0) {
1205 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1206 subvol_objectid
= key
.offset
;
1208 root_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1209 struct btrfs_root_ref
);
1210 len
= btrfs_root_ref_name_len(path
->nodes
[0], root_ref
);
1213 ret
= -ENAMETOOLONG
;
1216 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
1217 (unsigned long)(root_ref
+ 1), len
);
1219 dirid
= btrfs_root_ref_dirid(path
->nodes
[0], root_ref
);
1220 btrfs_release_path(path
);
1222 fs_root
= btrfs_get_fs_root(fs_info
, subvol_objectid
, true);
1223 if (IS_ERR(fs_root
)) {
1224 ret
= PTR_ERR(fs_root
);
1230 * Walk up the filesystem tree by inode refs until we hit the
1233 while (dirid
!= BTRFS_FIRST_FREE_OBJECTID
) {
1234 key
.objectid
= dirid
;
1235 key
.type
= BTRFS_INODE_REF_KEY
;
1236 key
.offset
= (u64
)-1;
1238 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
1241 } else if (ret
> 0) {
1242 ret
= btrfs_previous_item(fs_root
, path
, dirid
,
1243 BTRFS_INODE_REF_KEY
);
1246 } else if (ret
> 0) {
1252 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1255 inode_ref
= btrfs_item_ptr(path
->nodes
[0],
1257 struct btrfs_inode_ref
);
1258 len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1262 ret
= -ENAMETOOLONG
;
1265 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
1266 (unsigned long)(inode_ref
+ 1), len
);
1268 btrfs_release_path(path
);
1270 btrfs_put_root(fs_root
);
1274 btrfs_free_path(path
);
1275 if (ptr
== name
+ PATH_MAX
- 1) {
1279 memmove(name
, ptr
, name
+ PATH_MAX
- ptr
);
1284 btrfs_put_root(fs_root
);
1285 btrfs_free_path(path
);
1287 return ERR_PTR(ret
);
1290 static int get_default_subvol_objectid(struct btrfs_fs_info
*fs_info
, u64
*objectid
)
1292 struct btrfs_root
*root
= fs_info
->tree_root
;
1293 struct btrfs_dir_item
*di
;
1294 struct btrfs_path
*path
;
1295 struct btrfs_key location
;
1298 path
= btrfs_alloc_path();
1303 * Find the "default" dir item which points to the root item that we
1304 * will mount by default if we haven't been given a specific subvolume
1307 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
1308 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir_id
, "default", 7, 0);
1310 btrfs_free_path(path
);
1315 * Ok the default dir item isn't there. This is weird since
1316 * it's always been there, but don't freak out, just try and
1317 * mount the top-level subvolume.
1319 btrfs_free_path(path
);
1320 *objectid
= BTRFS_FS_TREE_OBJECTID
;
1324 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
1325 btrfs_free_path(path
);
1326 *objectid
= location
.objectid
;
1330 static int btrfs_fill_super(struct super_block
*sb
,
1331 struct btrfs_fs_devices
*fs_devices
,
1334 struct inode
*inode
;
1335 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1338 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1339 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
1340 sb
->s_op
= &btrfs_super_ops
;
1341 sb
->s_d_op
= &btrfs_dentry_operations
;
1342 sb
->s_export_op
= &btrfs_export_ops
;
1343 sb
->s_xattr
= btrfs_xattr_handlers
;
1344 sb
->s_time_gran
= 1;
1345 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1346 sb
->s_flags
|= SB_POSIXACL
;
1348 sb
->s_flags
|= SB_I_VERSION
;
1349 sb
->s_iflags
|= SB_I_CGROUPWB
;
1351 err
= super_setup_bdi(sb
);
1353 btrfs_err(fs_info
, "super_setup_bdi failed");
1357 err
= open_ctree(sb
, fs_devices
, (char *)data
);
1359 btrfs_err(fs_info
, "open_ctree failed");
1363 inode
= btrfs_iget(sb
, BTRFS_FIRST_FREE_OBJECTID
, fs_info
->fs_root
);
1364 if (IS_ERR(inode
)) {
1365 err
= PTR_ERR(inode
);
1369 sb
->s_root
= d_make_root(inode
);
1375 cleancache_init_fs(sb
);
1376 sb
->s_flags
|= SB_ACTIVE
;
1380 close_ctree(fs_info
);
1384 int btrfs_sync_fs(struct super_block
*sb
, int wait
)
1386 struct btrfs_trans_handle
*trans
;
1387 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1388 struct btrfs_root
*root
= fs_info
->tree_root
;
1390 trace_btrfs_sync_fs(fs_info
, wait
);
1393 filemap_flush(fs_info
->btree_inode
->i_mapping
);
1397 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, 0, (u64
)-1);
1399 trans
= btrfs_attach_transaction_barrier(root
);
1400 if (IS_ERR(trans
)) {
1401 /* no transaction, don't bother */
1402 if (PTR_ERR(trans
) == -ENOENT
) {
1404 * Exit unless we have some pending changes
1405 * that need to go through commit
1407 if (fs_info
->pending_changes
== 0)
1410 * A non-blocking test if the fs is frozen. We must not
1411 * start a new transaction here otherwise a deadlock
1412 * happens. The pending operations are delayed to the
1413 * next commit after thawing.
1415 if (sb_start_write_trylock(sb
))
1419 trans
= btrfs_start_transaction(root
, 0);
1422 return PTR_ERR(trans
);
1424 return btrfs_commit_transaction(trans
);
1427 static void print_rescue_option(struct seq_file
*seq
, const char *s
, bool *printed
)
1429 seq_printf(seq
, "%s%s", (*printed
) ? ":" : ",rescue=", s
);
1433 static int btrfs_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1435 struct btrfs_fs_info
*info
= btrfs_sb(dentry
->d_sb
);
1436 const char *compress_type
;
1437 const char *subvol_name
;
1438 bool printed
= false;
1440 if (btrfs_test_opt(info
, DEGRADED
))
1441 seq_puts(seq
, ",degraded");
1442 if (btrfs_test_opt(info
, NODATASUM
))
1443 seq_puts(seq
, ",nodatasum");
1444 if (btrfs_test_opt(info
, NODATACOW
))
1445 seq_puts(seq
, ",nodatacow");
1446 if (btrfs_test_opt(info
, NOBARRIER
))
1447 seq_puts(seq
, ",nobarrier");
1448 if (info
->max_inline
!= BTRFS_DEFAULT_MAX_INLINE
)
1449 seq_printf(seq
, ",max_inline=%llu", info
->max_inline
);
1450 if (info
->thread_pool_size
!= min_t(unsigned long,
1451 num_online_cpus() + 2, 8))
1452 seq_printf(seq
, ",thread_pool=%u", info
->thread_pool_size
);
1453 if (btrfs_test_opt(info
, COMPRESS
)) {
1454 compress_type
= btrfs_compress_type2str(info
->compress_type
);
1455 if (btrfs_test_opt(info
, FORCE_COMPRESS
))
1456 seq_printf(seq
, ",compress-force=%s", compress_type
);
1458 seq_printf(seq
, ",compress=%s", compress_type
);
1459 if (info
->compress_level
)
1460 seq_printf(seq
, ":%d", info
->compress_level
);
1462 if (btrfs_test_opt(info
, NOSSD
))
1463 seq_puts(seq
, ",nossd");
1464 if (btrfs_test_opt(info
, SSD_SPREAD
))
1465 seq_puts(seq
, ",ssd_spread");
1466 else if (btrfs_test_opt(info
, SSD
))
1467 seq_puts(seq
, ",ssd");
1468 if (btrfs_test_opt(info
, NOTREELOG
))
1469 seq_puts(seq
, ",notreelog");
1470 if (btrfs_test_opt(info
, NOLOGREPLAY
))
1471 print_rescue_option(seq
, "nologreplay", &printed
);
1472 if (btrfs_test_opt(info
, USEBACKUPROOT
))
1473 print_rescue_option(seq
, "usebackuproot", &printed
);
1474 if (btrfs_test_opt(info
, IGNOREBADROOTS
))
1475 print_rescue_option(seq
, "ignorebadroots", &printed
);
1476 if (btrfs_test_opt(info
, IGNOREDATACSUMS
))
1477 print_rescue_option(seq
, "ignoredatacsums", &printed
);
1478 if (btrfs_test_opt(info
, FLUSHONCOMMIT
))
1479 seq_puts(seq
, ",flushoncommit");
1480 if (btrfs_test_opt(info
, DISCARD_SYNC
))
1481 seq_puts(seq
, ",discard");
1482 if (btrfs_test_opt(info
, DISCARD_ASYNC
))
1483 seq_puts(seq
, ",discard=async");
1484 if (!(info
->sb
->s_flags
& SB_POSIXACL
))
1485 seq_puts(seq
, ",noacl");
1486 if (btrfs_free_space_cache_v1_active(info
))
1487 seq_puts(seq
, ",space_cache");
1488 else if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
))
1489 seq_puts(seq
, ",space_cache=v2");
1491 seq_puts(seq
, ",nospace_cache");
1492 if (btrfs_test_opt(info
, RESCAN_UUID_TREE
))
1493 seq_puts(seq
, ",rescan_uuid_tree");
1494 if (btrfs_test_opt(info
, CLEAR_CACHE
))
1495 seq_puts(seq
, ",clear_cache");
1496 if (btrfs_test_opt(info
, USER_SUBVOL_RM_ALLOWED
))
1497 seq_puts(seq
, ",user_subvol_rm_allowed");
1498 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
1499 seq_puts(seq
, ",enospc_debug");
1500 if (btrfs_test_opt(info
, AUTO_DEFRAG
))
1501 seq_puts(seq
, ",autodefrag");
1502 if (btrfs_test_opt(info
, SKIP_BALANCE
))
1503 seq_puts(seq
, ",skip_balance");
1504 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1505 if (btrfs_test_opt(info
, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA
))
1506 seq_puts(seq
, ",check_int_data");
1507 else if (btrfs_test_opt(info
, CHECK_INTEGRITY
))
1508 seq_puts(seq
, ",check_int");
1509 if (info
->check_integrity_print_mask
)
1510 seq_printf(seq
, ",check_int_print_mask=%d",
1511 info
->check_integrity_print_mask
);
1513 if (info
->metadata_ratio
)
1514 seq_printf(seq
, ",metadata_ratio=%u", info
->metadata_ratio
);
1515 if (btrfs_test_opt(info
, PANIC_ON_FATAL_ERROR
))
1516 seq_puts(seq
, ",fatal_errors=panic");
1517 if (info
->commit_interval
!= BTRFS_DEFAULT_COMMIT_INTERVAL
)
1518 seq_printf(seq
, ",commit=%u", info
->commit_interval
);
1519 #ifdef CONFIG_BTRFS_DEBUG
1520 if (btrfs_test_opt(info
, FRAGMENT_DATA
))
1521 seq_puts(seq
, ",fragment=data");
1522 if (btrfs_test_opt(info
, FRAGMENT_METADATA
))
1523 seq_puts(seq
, ",fragment=metadata");
1525 if (btrfs_test_opt(info
, REF_VERIFY
))
1526 seq_puts(seq
, ",ref_verify");
1527 seq_printf(seq
, ",subvolid=%llu",
1528 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
);
1529 subvol_name
= btrfs_get_subvol_name_from_objectid(info
,
1530 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
);
1531 if (!IS_ERR(subvol_name
)) {
1532 seq_puts(seq
, ",subvol=");
1533 seq_escape(seq
, subvol_name
, " \t\n\\");
1539 static int btrfs_test_super(struct super_block
*s
, void *data
)
1541 struct btrfs_fs_info
*p
= data
;
1542 struct btrfs_fs_info
*fs_info
= btrfs_sb(s
);
1544 return fs_info
->fs_devices
== p
->fs_devices
;
1547 static int btrfs_set_super(struct super_block
*s
, void *data
)
1549 int err
= set_anon_super(s
, data
);
1551 s
->s_fs_info
= data
;
1556 * subvolumes are identified by ino 256
1558 static inline int is_subvolume_inode(struct inode
*inode
)
1560 if (inode
&& inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
1565 static struct dentry
*mount_subvol(const char *subvol_name
, u64 subvol_objectid
,
1566 struct vfsmount
*mnt
)
1568 struct dentry
*root
;
1572 if (!subvol_objectid
) {
1573 ret
= get_default_subvol_objectid(btrfs_sb(mnt
->mnt_sb
),
1576 root
= ERR_PTR(ret
);
1580 subvol_name
= btrfs_get_subvol_name_from_objectid(
1581 btrfs_sb(mnt
->mnt_sb
), subvol_objectid
);
1582 if (IS_ERR(subvol_name
)) {
1583 root
= ERR_CAST(subvol_name
);
1590 root
= mount_subtree(mnt
, subvol_name
);
1591 /* mount_subtree() drops our reference on the vfsmount. */
1594 if (!IS_ERR(root
)) {
1595 struct super_block
*s
= root
->d_sb
;
1596 struct btrfs_fs_info
*fs_info
= btrfs_sb(s
);
1597 struct inode
*root_inode
= d_inode(root
);
1598 u64 root_objectid
= BTRFS_I(root_inode
)->root
->root_key
.objectid
;
1601 if (!is_subvolume_inode(root_inode
)) {
1602 btrfs_err(fs_info
, "'%s' is not a valid subvolume",
1606 if (subvol_objectid
&& root_objectid
!= subvol_objectid
) {
1608 * This will also catch a race condition where a
1609 * subvolume which was passed by ID is renamed and
1610 * another subvolume is renamed over the old location.
1613 "subvol '%s' does not match subvolid %llu",
1614 subvol_name
, subvol_objectid
);
1619 root
= ERR_PTR(ret
);
1620 deactivate_locked_super(s
);
1631 * Find a superblock for the given device / mount point.
1633 * Note: This is based on mount_bdev from fs/super.c with a few additions
1634 * for multiple device setup. Make sure to keep it in sync.
1636 static struct dentry
*btrfs_mount_root(struct file_system_type
*fs_type
,
1637 int flags
, const char *device_name
, void *data
)
1639 struct block_device
*bdev
= NULL
;
1640 struct super_block
*s
;
1641 struct btrfs_device
*device
= NULL
;
1642 struct btrfs_fs_devices
*fs_devices
= NULL
;
1643 struct btrfs_fs_info
*fs_info
= NULL
;
1644 void *new_sec_opts
= NULL
;
1645 fmode_t mode
= FMODE_READ
;
1648 if (!(flags
& SB_RDONLY
))
1649 mode
|= FMODE_WRITE
;
1652 error
= security_sb_eat_lsm_opts(data
, &new_sec_opts
);
1654 return ERR_PTR(error
);
1658 * Setup a dummy root and fs_info for test/set super. This is because
1659 * we don't actually fill this stuff out until open_ctree, but we need
1660 * then open_ctree will properly initialize the file system specific
1661 * settings later. btrfs_init_fs_info initializes the static elements
1662 * of the fs_info (locks and such) to make cleanup easier if we find a
1663 * superblock with our given fs_devices later on at sget() time.
1665 fs_info
= kvzalloc(sizeof(struct btrfs_fs_info
), GFP_KERNEL
);
1668 goto error_sec_opts
;
1670 btrfs_init_fs_info(fs_info
);
1672 fs_info
->super_copy
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
1673 fs_info
->super_for_commit
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
1674 if (!fs_info
->super_copy
|| !fs_info
->super_for_commit
) {
1679 mutex_lock(&uuid_mutex
);
1680 error
= btrfs_parse_device_options(data
, mode
, fs_type
);
1682 mutex_unlock(&uuid_mutex
);
1686 device
= btrfs_scan_one_device(device_name
, mode
, fs_type
);
1687 if (IS_ERR(device
)) {
1688 mutex_unlock(&uuid_mutex
);
1689 error
= PTR_ERR(device
);
1693 fs_devices
= device
->fs_devices
;
1694 fs_info
->fs_devices
= fs_devices
;
1696 error
= btrfs_open_devices(fs_devices
, mode
, fs_type
);
1697 mutex_unlock(&uuid_mutex
);
1701 if (!(flags
& SB_RDONLY
) && fs_devices
->rw_devices
== 0) {
1703 goto error_close_devices
;
1706 bdev
= fs_devices
->latest_bdev
;
1707 s
= sget(fs_type
, btrfs_test_super
, btrfs_set_super
, flags
| SB_NOSEC
,
1711 goto error_close_devices
;
1715 btrfs_close_devices(fs_devices
);
1716 btrfs_free_fs_info(fs_info
);
1717 if ((flags
^ s
->s_flags
) & SB_RDONLY
)
1720 snprintf(s
->s_id
, sizeof(s
->s_id
), "%pg", bdev
);
1721 btrfs_sb(s
)->bdev_holder
= fs_type
;
1722 if (!strstr(crc32c_impl(), "generic"))
1723 set_bit(BTRFS_FS_CSUM_IMPL_FAST
, &fs_info
->flags
);
1724 error
= btrfs_fill_super(s
, fs_devices
, data
);
1727 error
= security_sb_set_mnt_opts(s
, new_sec_opts
, 0, NULL
);
1728 security_free_mnt_opts(&new_sec_opts
);
1730 deactivate_locked_super(s
);
1731 return ERR_PTR(error
);
1734 return dget(s
->s_root
);
1736 error_close_devices
:
1737 btrfs_close_devices(fs_devices
);
1739 btrfs_free_fs_info(fs_info
);
1741 security_free_mnt_opts(&new_sec_opts
);
1742 return ERR_PTR(error
);
1746 * Mount function which is called by VFS layer.
1748 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1749 * which needs vfsmount* of device's root (/). This means device's root has to
1750 * be mounted internally in any case.
1753 * 1. Parse subvol id related options for later use in mount_subvol().
1755 * 2. Mount device's root (/) by calling vfs_kern_mount().
1757 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1758 * first place. In order to avoid calling btrfs_mount() again, we use
1759 * different file_system_type which is not registered to VFS by
1760 * register_filesystem() (btrfs_root_fs_type). As a result,
1761 * btrfs_mount_root() is called. The return value will be used by
1762 * mount_subtree() in mount_subvol().
1764 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1765 * "btrfs subvolume set-default", mount_subvol() is called always.
1767 static struct dentry
*btrfs_mount(struct file_system_type
*fs_type
, int flags
,
1768 const char *device_name
, void *data
)
1770 struct vfsmount
*mnt_root
;
1771 struct dentry
*root
;
1772 char *subvol_name
= NULL
;
1773 u64 subvol_objectid
= 0;
1776 error
= btrfs_parse_subvol_options(data
, &subvol_name
,
1780 return ERR_PTR(error
);
1783 /* mount device's root (/) */
1784 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
, flags
, device_name
, data
);
1785 if (PTR_ERR_OR_ZERO(mnt_root
) == -EBUSY
) {
1786 if (flags
& SB_RDONLY
) {
1787 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
,
1788 flags
& ~SB_RDONLY
, device_name
, data
);
1790 mnt_root
= vfs_kern_mount(&btrfs_root_fs_type
,
1791 flags
| SB_RDONLY
, device_name
, data
);
1792 if (IS_ERR(mnt_root
)) {
1793 root
= ERR_CAST(mnt_root
);
1798 down_write(&mnt_root
->mnt_sb
->s_umount
);
1799 error
= btrfs_remount(mnt_root
->mnt_sb
, &flags
, NULL
);
1800 up_write(&mnt_root
->mnt_sb
->s_umount
);
1802 root
= ERR_PTR(error
);
1809 if (IS_ERR(mnt_root
)) {
1810 root
= ERR_CAST(mnt_root
);
1815 /* mount_subvol() will free subvol_name and mnt_root */
1816 root
= mount_subvol(subvol_name
, subvol_objectid
, mnt_root
);
1822 static void btrfs_resize_thread_pool(struct btrfs_fs_info
*fs_info
,
1823 u32 new_pool_size
, u32 old_pool_size
)
1825 if (new_pool_size
== old_pool_size
)
1828 fs_info
->thread_pool_size
= new_pool_size
;
1830 btrfs_info(fs_info
, "resize thread pool %d -> %d",
1831 old_pool_size
, new_pool_size
);
1833 btrfs_workqueue_set_max(fs_info
->workers
, new_pool_size
);
1834 btrfs_workqueue_set_max(fs_info
->delalloc_workers
, new_pool_size
);
1835 btrfs_workqueue_set_max(fs_info
->caching_workers
, new_pool_size
);
1836 btrfs_workqueue_set_max(fs_info
->endio_workers
, new_pool_size
);
1837 btrfs_workqueue_set_max(fs_info
->endio_meta_workers
, new_pool_size
);
1838 btrfs_workqueue_set_max(fs_info
->endio_meta_write_workers
,
1840 btrfs_workqueue_set_max(fs_info
->endio_write_workers
, new_pool_size
);
1841 btrfs_workqueue_set_max(fs_info
->endio_freespace_worker
, new_pool_size
);
1842 btrfs_workqueue_set_max(fs_info
->delayed_workers
, new_pool_size
);
1843 btrfs_workqueue_set_max(fs_info
->readahead_workers
, new_pool_size
);
1844 btrfs_workqueue_set_max(fs_info
->scrub_wr_completion_workers
,
1848 static inline void btrfs_remount_begin(struct btrfs_fs_info
*fs_info
,
1849 unsigned long old_opts
, int flags
)
1851 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1852 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) ||
1853 (flags
& SB_RDONLY
))) {
1854 /* wait for any defraggers to finish */
1855 wait_event(fs_info
->transaction_wait
,
1856 (atomic_read(&fs_info
->defrag_running
) == 0));
1857 if (flags
& SB_RDONLY
)
1858 sync_filesystem(fs_info
->sb
);
1862 static inline void btrfs_remount_cleanup(struct btrfs_fs_info
*fs_info
,
1863 unsigned long old_opts
)
1865 const bool cache_opt
= btrfs_test_opt(fs_info
, SPACE_CACHE
);
1868 * We need to cleanup all defragable inodes if the autodefragment is
1869 * close or the filesystem is read only.
1871 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1872 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) || sb_rdonly(fs_info
->sb
))) {
1873 btrfs_cleanup_defrag_inodes(fs_info
);
1876 /* If we toggled discard async */
1877 if (!btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1878 btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1879 btrfs_discard_resume(fs_info
);
1880 else if (btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1881 !btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1882 btrfs_discard_cleanup(fs_info
);
1884 /* If we toggled space cache */
1885 if (cache_opt
!= btrfs_free_space_cache_v1_active(fs_info
))
1886 btrfs_set_free_space_cache_v1_active(fs_info
, cache_opt
);
1889 static int btrfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1891 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1892 unsigned old_flags
= sb
->s_flags
;
1893 unsigned long old_opts
= fs_info
->mount_opt
;
1894 unsigned long old_compress_type
= fs_info
->compress_type
;
1895 u64 old_max_inline
= fs_info
->max_inline
;
1896 u32 old_thread_pool_size
= fs_info
->thread_pool_size
;
1897 u32 old_metadata_ratio
= fs_info
->metadata_ratio
;
1900 sync_filesystem(sb
);
1901 set_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1904 void *new_sec_opts
= NULL
;
1906 ret
= security_sb_eat_lsm_opts(data
, &new_sec_opts
);
1908 ret
= security_sb_remount(sb
, new_sec_opts
);
1909 security_free_mnt_opts(&new_sec_opts
);
1914 ret
= btrfs_parse_options(fs_info
, data
, *flags
);
1918 btrfs_remount_begin(fs_info
, old_opts
, *flags
);
1919 btrfs_resize_thread_pool(fs_info
,
1920 fs_info
->thread_pool_size
, old_thread_pool_size
);
1922 if (btrfs_test_opt(fs_info
, FREE_SPACE_TREE
) !=
1923 btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
) &&
1924 (!sb_rdonly(sb
) || (*flags
& SB_RDONLY
))) {
1926 "remount supports changing free space tree only from ro to rw");
1927 /* Make sure free space cache options match the state on disk */
1928 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
)) {
1929 btrfs_set_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1930 btrfs_clear_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1932 if (btrfs_free_space_cache_v1_active(fs_info
)) {
1933 btrfs_clear_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1934 btrfs_set_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1938 if ((bool)(*flags
& SB_RDONLY
) == sb_rdonly(sb
))
1941 if (*flags
& SB_RDONLY
) {
1943 * this also happens on 'umount -rf' or on shutdown, when
1944 * the filesystem is busy.
1946 cancel_work_sync(&fs_info
->async_reclaim_work
);
1947 cancel_work_sync(&fs_info
->async_data_reclaim_work
);
1949 btrfs_discard_cleanup(fs_info
);
1951 /* wait for the uuid_scan task to finish */
1952 down(&fs_info
->uuid_tree_rescan_sem
);
1953 /* avoid complains from lockdep et al. */
1954 up(&fs_info
->uuid_tree_rescan_sem
);
1956 btrfs_set_sb_rdonly(sb
);
1959 * Setting SB_RDONLY will put the cleaner thread to
1960 * sleep at the next loop if it's already active.
1961 * If it's already asleep, we'll leave unused block
1962 * groups on disk until we're mounted read-write again
1963 * unless we clean them up here.
1965 btrfs_delete_unused_bgs(fs_info
);
1968 * The cleaner task could be already running before we set the
1969 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1970 * We must make sure that after we finish the remount, i.e. after
1971 * we call btrfs_commit_super(), the cleaner can no longer start
1972 * a transaction - either because it was dropping a dead root,
1973 * running delayed iputs or deleting an unused block group (the
1974 * cleaner picked a block group from the list of unused block
1975 * groups before we were able to in the previous call to
1976 * btrfs_delete_unused_bgs()).
1978 wait_on_bit(&fs_info
->flags
, BTRFS_FS_CLEANER_RUNNING
,
1979 TASK_UNINTERRUPTIBLE
);
1982 * We've set the superblock to RO mode, so we might have made
1983 * the cleaner task sleep without running all pending delayed
1984 * iputs. Go through all the delayed iputs here, so that if an
1985 * unmount happens without remounting RW we don't end up at
1986 * finishing close_ctree() with a non-empty list of delayed
1989 btrfs_run_delayed_iputs(fs_info
);
1991 btrfs_dev_replace_suspend_for_unmount(fs_info
);
1992 btrfs_scrub_cancel(fs_info
);
1993 btrfs_pause_balance(fs_info
);
1996 * Pause the qgroup rescan worker if it is running. We don't want
1997 * it to be still running after we are in RO mode, as after that,
1998 * by the time we unmount, it might have left a transaction open,
1999 * so we would leak the transaction and/or crash.
2001 btrfs_qgroup_wait_for_completion(fs_info
, false);
2003 ret
= btrfs_commit_super(fs_info
);
2007 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
)) {
2009 "Remounting read-write after error is not allowed");
2013 if (fs_info
->fs_devices
->rw_devices
== 0) {
2018 if (!btrfs_check_rw_degradable(fs_info
, NULL
)) {
2020 "too many missing devices, writable remount is not allowed");
2025 if (btrfs_super_log_root(fs_info
->super_copy
) != 0) {
2027 "mount required to replay tree-log, cannot remount read-write");
2033 * NOTE: when remounting with a change that does writes, don't
2034 * put it anywhere above this point, as we are not sure to be
2035 * safe to write until we pass the above checks.
2037 ret
= btrfs_start_pre_rw_mount(fs_info
);
2041 btrfs_clear_sb_rdonly(sb
);
2043 set_bit(BTRFS_FS_OPEN
, &fs_info
->flags
);
2047 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2048 * since the absence of the flag means it can be toggled off by remount.
2050 *flags
|= SB_I_VERSION
;
2052 wake_up_process(fs_info
->transaction_kthread
);
2053 btrfs_remount_cleanup(fs_info
, old_opts
);
2054 btrfs_clear_oneshot_options(fs_info
);
2055 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
2060 /* We've hit an error - don't reset SB_RDONLY */
2062 old_flags
|= SB_RDONLY
;
2063 if (!(old_flags
& SB_RDONLY
))
2064 clear_bit(BTRFS_FS_STATE_RO
, &fs_info
->fs_state
);
2065 sb
->s_flags
= old_flags
;
2066 fs_info
->mount_opt
= old_opts
;
2067 fs_info
->compress_type
= old_compress_type
;
2068 fs_info
->max_inline
= old_max_inline
;
2069 btrfs_resize_thread_pool(fs_info
,
2070 old_thread_pool_size
, fs_info
->thread_pool_size
);
2071 fs_info
->metadata_ratio
= old_metadata_ratio
;
2072 btrfs_remount_cleanup(fs_info
, old_opts
);
2073 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
2078 /* Used to sort the devices by max_avail(descending sort) */
2079 static inline int btrfs_cmp_device_free_bytes(const void *dev_info1
,
2080 const void *dev_info2
)
2082 if (((struct btrfs_device_info
*)dev_info1
)->max_avail
>
2083 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2085 else if (((struct btrfs_device_info
*)dev_info1
)->max_avail
<
2086 ((struct btrfs_device_info
*)dev_info2
)->max_avail
)
2093 * sort the devices by max_avail, in which max free extent size of each device
2094 * is stored.(Descending Sort)
2096 static inline void btrfs_descending_sort_devices(
2097 struct btrfs_device_info
*devices
,
2100 sort(devices
, nr_devices
, sizeof(struct btrfs_device_info
),
2101 btrfs_cmp_device_free_bytes
, NULL
);
2105 * The helper to calc the free space on the devices that can be used to store
2108 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info
*fs_info
,
2111 struct btrfs_device_info
*devices_info
;
2112 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2113 struct btrfs_device
*device
;
2116 u64 min_stripe_size
;
2117 int num_stripes
= 1;
2118 int i
= 0, nr_devices
;
2119 const struct btrfs_raid_attr
*rattr
;
2122 * We aren't under the device list lock, so this is racy-ish, but good
2123 * enough for our purposes.
2125 nr_devices
= fs_info
->fs_devices
->open_devices
;
2128 nr_devices
= fs_info
->fs_devices
->open_devices
;
2136 devices_info
= kmalloc_array(nr_devices
, sizeof(*devices_info
),
2141 /* calc min stripe number for data space allocation */
2142 type
= btrfs_data_alloc_profile(fs_info
);
2143 rattr
= &btrfs_raid_array
[btrfs_bg_flags_to_raid_index(type
)];
2145 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
2146 num_stripes
= nr_devices
;
2147 else if (type
& BTRFS_BLOCK_GROUP_RAID1
)
2149 else if (type
& BTRFS_BLOCK_GROUP_RAID1C3
)
2151 else if (type
& BTRFS_BLOCK_GROUP_RAID1C4
)
2153 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
2156 /* Adjust for more than 1 stripe per device */
2157 min_stripe_size
= rattr
->dev_stripes
* BTRFS_STRIPE_LEN
;
2160 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
2161 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
2162 &device
->dev_state
) ||
2164 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
2167 if (i
>= nr_devices
)
2170 avail_space
= device
->total_bytes
- device
->bytes_used
;
2172 /* align with stripe_len */
2173 avail_space
= rounddown(avail_space
, BTRFS_STRIPE_LEN
);
2176 * In order to avoid overwriting the superblock on the drive,
2177 * btrfs starts at an offset of at least 1MB when doing chunk
2180 * This ensures we have at least min_stripe_size free space
2181 * after excluding 1MB.
2183 if (avail_space
<= SZ_1M
+ min_stripe_size
)
2186 avail_space
-= SZ_1M
;
2188 devices_info
[i
].dev
= device
;
2189 devices_info
[i
].max_avail
= avail_space
;
2197 btrfs_descending_sort_devices(devices_info
, nr_devices
);
2201 while (nr_devices
>= rattr
->devs_min
) {
2202 num_stripes
= min(num_stripes
, nr_devices
);
2204 if (devices_info
[i
].max_avail
>= min_stripe_size
) {
2208 avail_space
+= devices_info
[i
].max_avail
* num_stripes
;
2209 alloc_size
= devices_info
[i
].max_avail
;
2210 for (j
= i
+ 1 - num_stripes
; j
<= i
; j
++)
2211 devices_info
[j
].max_avail
-= alloc_size
;
2217 kfree(devices_info
);
2218 *free_bytes
= avail_space
;
2223 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2225 * If there's a redundant raid level at DATA block groups, use the respective
2226 * multiplier to scale the sizes.
2228 * Unused device space usage is based on simulating the chunk allocator
2229 * algorithm that respects the device sizes and order of allocations. This is
2230 * a close approximation of the actual use but there are other factors that may
2231 * change the result (like a new metadata chunk).
2233 * If metadata is exhausted, f_bavail will be 0.
2235 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2237 struct btrfs_fs_info
*fs_info
= btrfs_sb(dentry
->d_sb
);
2238 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2239 struct btrfs_space_info
*found
;
2241 u64 total_free_data
= 0;
2242 u64 total_free_meta
= 0;
2243 u32 bits
= fs_info
->sectorsize_bits
;
2244 __be32
*fsid
= (__be32
*)fs_info
->fs_devices
->fsid
;
2245 unsigned factor
= 1;
2246 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
2251 list_for_each_entry(found
, &fs_info
->space_info
, list
) {
2252 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2255 total_free_data
+= found
->disk_total
- found
->disk_used
;
2257 btrfs_account_ro_block_groups_free_space(found
);
2259 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
2260 if (!list_empty(&found
->block_groups
[i
]))
2261 factor
= btrfs_bg_type_to_factor(
2262 btrfs_raid_array
[i
].bg_flag
);
2267 * Metadata in mixed block goup profiles are accounted in data
2269 if (!mixed
&& found
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2270 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
)
2273 total_free_meta
+= found
->disk_total
-
2277 total_used
+= found
->disk_used
;
2280 buf
->f_blocks
= div_u64(btrfs_super_total_bytes(disk_super
), factor
);
2281 buf
->f_blocks
>>= bits
;
2282 buf
->f_bfree
= buf
->f_blocks
- (div_u64(total_used
, factor
) >> bits
);
2284 /* Account global block reserve as used, it's in logical size already */
2285 spin_lock(&block_rsv
->lock
);
2286 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2287 if (buf
->f_bfree
>= block_rsv
->size
>> bits
)
2288 buf
->f_bfree
-= block_rsv
->size
>> bits
;
2291 spin_unlock(&block_rsv
->lock
);
2293 buf
->f_bavail
= div_u64(total_free_data
, factor
);
2294 ret
= btrfs_calc_avail_data_space(fs_info
, &total_free_data
);
2297 buf
->f_bavail
+= div_u64(total_free_data
, factor
);
2298 buf
->f_bavail
= buf
->f_bavail
>> bits
;
2301 * We calculate the remaining metadata space minus global reserve. If
2302 * this is (supposedly) smaller than zero, there's no space. But this
2303 * does not hold in practice, the exhausted state happens where's still
2304 * some positive delta. So we apply some guesswork and compare the
2305 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2307 * We probably cannot calculate the exact threshold value because this
2308 * depends on the internal reservations requested by various
2309 * operations, so some operations that consume a few metadata will
2310 * succeed even if the Avail is zero. But this is better than the other
2316 * We only want to claim there's no available space if we can no longer
2317 * allocate chunks for our metadata profile and our global reserve will
2318 * not fit in the free metadata space. If we aren't ->full then we
2319 * still can allocate chunks and thus are fine using the currently
2320 * calculated f_bavail.
2322 if (!mixed
&& block_rsv
->space_info
->full
&&
2323 total_free_meta
- thresh
< block_rsv
->size
)
2326 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2327 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2328 buf
->f_namelen
= BTRFS_NAME_LEN
;
2330 /* We treat it as constant endianness (it doesn't matter _which_)
2331 because we want the fsid to come out the same whether mounted
2332 on a big-endian or little-endian host */
2333 buf
->f_fsid
.val
[0] = be32_to_cpu(fsid
[0]) ^ be32_to_cpu(fsid
[2]);
2334 buf
->f_fsid
.val
[1] = be32_to_cpu(fsid
[1]) ^ be32_to_cpu(fsid
[3]);
2335 /* Mask in the root object ID too, to disambiguate subvols */
2336 buf
->f_fsid
.val
[0] ^=
2337 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
>> 32;
2338 buf
->f_fsid
.val
[1] ^=
2339 BTRFS_I(d_inode(dentry
))->root
->root_key
.objectid
;
2344 static void btrfs_kill_super(struct super_block
*sb
)
2346 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2347 kill_anon_super(sb
);
2348 btrfs_free_fs_info(fs_info
);
2351 static struct file_system_type btrfs_fs_type
= {
2352 .owner
= THIS_MODULE
,
2354 .mount
= btrfs_mount
,
2355 .kill_sb
= btrfs_kill_super
,
2356 .fs_flags
= FS_REQUIRES_DEV
| FS_BINARY_MOUNTDATA
,
2359 static struct file_system_type btrfs_root_fs_type
= {
2360 .owner
= THIS_MODULE
,
2362 .mount
= btrfs_mount_root
,
2363 .kill_sb
= btrfs_kill_super
,
2364 .fs_flags
= FS_REQUIRES_DEV
| FS_BINARY_MOUNTDATA
,
2367 MODULE_ALIAS_FS("btrfs");
2369 static int btrfs_control_open(struct inode
*inode
, struct file
*file
)
2372 * The control file's private_data is used to hold the
2373 * transaction when it is started and is used to keep
2374 * track of whether a transaction is already in progress.
2376 file
->private_data
= NULL
;
2381 * Used by /dev/btrfs-control for devices ioctls.
2383 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
2386 struct btrfs_ioctl_vol_args
*vol
;
2387 struct btrfs_device
*device
= NULL
;
2390 if (!capable(CAP_SYS_ADMIN
))
2393 vol
= memdup_user((void __user
*)arg
, sizeof(*vol
));
2395 return PTR_ERR(vol
);
2396 vol
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2399 case BTRFS_IOC_SCAN_DEV
:
2400 mutex_lock(&uuid_mutex
);
2401 device
= btrfs_scan_one_device(vol
->name
, FMODE_READ
,
2402 &btrfs_root_fs_type
);
2403 ret
= PTR_ERR_OR_ZERO(device
);
2404 mutex_unlock(&uuid_mutex
);
2406 case BTRFS_IOC_FORGET_DEV
:
2407 ret
= btrfs_forget_devices(vol
->name
);
2409 case BTRFS_IOC_DEVICES_READY
:
2410 mutex_lock(&uuid_mutex
);
2411 device
= btrfs_scan_one_device(vol
->name
, FMODE_READ
,
2412 &btrfs_root_fs_type
);
2413 if (IS_ERR(device
)) {
2414 mutex_unlock(&uuid_mutex
);
2415 ret
= PTR_ERR(device
);
2418 ret
= !(device
->fs_devices
->num_devices
==
2419 device
->fs_devices
->total_devices
);
2420 mutex_unlock(&uuid_mutex
);
2422 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
2423 ret
= btrfs_ioctl_get_supported_features((void __user
*)arg
);
2431 static int btrfs_freeze(struct super_block
*sb
)
2433 struct btrfs_trans_handle
*trans
;
2434 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2435 struct btrfs_root
*root
= fs_info
->tree_root
;
2437 set_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2439 * We don't need a barrier here, we'll wait for any transaction that
2440 * could be in progress on other threads (and do delayed iputs that
2441 * we want to avoid on a frozen filesystem), or do the commit
2444 trans
= btrfs_attach_transaction_barrier(root
);
2445 if (IS_ERR(trans
)) {
2446 /* no transaction, don't bother */
2447 if (PTR_ERR(trans
) == -ENOENT
)
2449 return PTR_ERR(trans
);
2451 return btrfs_commit_transaction(trans
);
2454 static int btrfs_unfreeze(struct super_block
*sb
)
2456 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2458 clear_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2462 static int btrfs_show_devname(struct seq_file
*m
, struct dentry
*root
)
2464 struct btrfs_fs_info
*fs_info
= btrfs_sb(root
->d_sb
);
2465 struct btrfs_device
*dev
, *first_dev
= NULL
;
2468 * Lightweight locking of the devices. We should not need
2469 * device_list_mutex here as we only read the device data and the list
2470 * is protected by RCU. Even if a device is deleted during the list
2471 * traversals, we'll get valid data, the freeing callback will wait at
2472 * least until the rcu_read_unlock.
2475 list_for_each_entry_rcu(dev
, &fs_info
->fs_devices
->devices
, dev_list
) {
2476 if (test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
))
2480 if (!first_dev
|| dev
->devid
< first_dev
->devid
)
2485 seq_escape(m
, rcu_str_deref(first_dev
->name
), " \t\n\\");
2492 static const struct super_operations btrfs_super_ops
= {
2493 .drop_inode
= btrfs_drop_inode
,
2494 .evict_inode
= btrfs_evict_inode
,
2495 .put_super
= btrfs_put_super
,
2496 .sync_fs
= btrfs_sync_fs
,
2497 .show_options
= btrfs_show_options
,
2498 .show_devname
= btrfs_show_devname
,
2499 .alloc_inode
= btrfs_alloc_inode
,
2500 .destroy_inode
= btrfs_destroy_inode
,
2501 .free_inode
= btrfs_free_inode
,
2502 .statfs
= btrfs_statfs
,
2503 .remount_fs
= btrfs_remount
,
2504 .freeze_fs
= btrfs_freeze
,
2505 .unfreeze_fs
= btrfs_unfreeze
,
2508 static const struct file_operations btrfs_ctl_fops
= {
2509 .open
= btrfs_control_open
,
2510 .unlocked_ioctl
= btrfs_control_ioctl
,
2511 .compat_ioctl
= compat_ptr_ioctl
,
2512 .owner
= THIS_MODULE
,
2513 .llseek
= noop_llseek
,
2516 static struct miscdevice btrfs_misc
= {
2517 .minor
= BTRFS_MINOR
,
2518 .name
= "btrfs-control",
2519 .fops
= &btrfs_ctl_fops
2522 MODULE_ALIAS_MISCDEV(BTRFS_MINOR
);
2523 MODULE_ALIAS("devname:btrfs-control");
2525 static int __init
btrfs_interface_init(void)
2527 return misc_register(&btrfs_misc
);
2530 static __cold
void btrfs_interface_exit(void)
2532 misc_deregister(&btrfs_misc
);
2535 static void __init
btrfs_print_mod_info(void)
2537 static const char options
[] = ""
2538 #ifdef CONFIG_BTRFS_DEBUG
2541 #ifdef CONFIG_BTRFS_ASSERT
2544 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2545 ", integrity-checker=on"
2547 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2550 #ifdef CONFIG_BLK_DEV_ZONED
2556 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options
);
2559 static int __init
init_btrfs_fs(void)
2565 err
= btrfs_init_sysfs();
2569 btrfs_init_compress();
2571 err
= btrfs_init_cachep();
2575 err
= extent_io_init();
2579 err
= extent_state_cache_init();
2581 goto free_extent_io
;
2583 err
= extent_map_init();
2585 goto free_extent_state_cache
;
2587 err
= ordered_data_init();
2589 goto free_extent_map
;
2591 err
= btrfs_delayed_inode_init();
2593 goto free_ordered_data
;
2595 err
= btrfs_auto_defrag_init();
2597 goto free_delayed_inode
;
2599 err
= btrfs_delayed_ref_init();
2601 goto free_auto_defrag
;
2603 err
= btrfs_prelim_ref_init();
2605 goto free_delayed_ref
;
2607 err
= btrfs_end_io_wq_init();
2609 goto free_prelim_ref
;
2611 err
= btrfs_interface_init();
2613 goto free_end_io_wq
;
2615 btrfs_print_mod_info();
2617 err
= btrfs_run_sanity_tests();
2619 goto unregister_ioctl
;
2621 err
= register_filesystem(&btrfs_fs_type
);
2623 goto unregister_ioctl
;
2628 btrfs_interface_exit();
2630 btrfs_end_io_wq_exit();
2632 btrfs_prelim_ref_exit();
2634 btrfs_delayed_ref_exit();
2636 btrfs_auto_defrag_exit();
2638 btrfs_delayed_inode_exit();
2640 ordered_data_exit();
2643 free_extent_state_cache
:
2644 extent_state_cache_exit();
2648 btrfs_destroy_cachep();
2650 btrfs_exit_compress();
2656 static void __exit
exit_btrfs_fs(void)
2658 btrfs_destroy_cachep();
2659 btrfs_delayed_ref_exit();
2660 btrfs_auto_defrag_exit();
2661 btrfs_delayed_inode_exit();
2662 btrfs_prelim_ref_exit();
2663 ordered_data_exit();
2665 extent_state_cache_exit();
2667 btrfs_interface_exit();
2668 btrfs_end_io_wq_exit();
2669 unregister_filesystem(&btrfs_fs_type
);
2671 btrfs_cleanup_fs_uuids();
2672 btrfs_exit_compress();
2675 late_initcall(init_btrfs_fs
);
2676 module_exit(exit_btrfs_fs
)
2678 MODULE_LICENSE("GPL");
2679 MODULE_SOFTDEP("pre: crc32c");
2680 MODULE_SOFTDEP("pre: xxhash64");
2681 MODULE_SOFTDEP("pre: sha256");
2682 MODULE_SOFTDEP("pre: blake2b-256");