1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/blkdev.h>
7 #include <linux/module.h>
9 #include <linux/pagemap.h>
10 #include <linux/highmem.h>
11 #include <linux/time.h>
12 #include <linux/init.h>
13 #include <linux/seq_file.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/writeback.h>
18 #include <linux/statfs.h>
19 #include <linux/compat.h>
20 #include <linux/parser.h>
21 #include <linux/ctype.h>
22 #include <linux/namei.h>
23 #include <linux/miscdevice.h>
24 #include <linux/magic.h>
25 #include <linux/slab.h>
26 #include <linux/ratelimit.h>
27 #include <linux/crc32c.h>
28 #include <linux/btrfs.h>
29 #include <linux/security.h>
30 #include <linux/fs_parser.h>
31 #include <linux/swap.h>
33 #include "delayed-inode.h"
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "direct-io.h"
43 #include "compression.h"
44 #include "dev-replace.h"
45 #include "free-space-cache.h"
47 #include "space-info.h"
50 #include "tests/btrfs-tests.h"
51 #include "block-group.h"
56 #include "accessors.h"
63 #include "extent-tree.h"
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/btrfs.h>
67 static const struct super_operations btrfs_super_ops
;
68 static struct file_system_type btrfs_fs_type
;
70 static void btrfs_put_super(struct super_block
*sb
)
72 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
74 btrfs_info(fs_info
, "last unmount of filesystem %pU", fs_info
->fs_devices
->fsid
);
78 /* Store the mount options related information. */
79 struct btrfs_fs_context
{
86 unsigned long long mount_opt
;
87 unsigned long compress_type
:4;
88 unsigned int compress_level
;
98 Opt_compress_force_type
,
112 Opt_rescan_uuid_tree
,
115 Opt_space_cache_version
,
123 Opt_user_subvol_rm_allowed
,
131 /* Debugging options */
133 #ifdef CONFIG_BTRFS_DEBUG
134 Opt_fragment
, Opt_fragment_data
, Opt_fragment_metadata
, Opt_fragment_all
,
136 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
143 Opt_fatal_errors_panic
,
144 Opt_fatal_errors_bug
,
147 static const struct constant_table btrfs_parameter_fatal_errors
[] = {
148 { "panic", Opt_fatal_errors_panic
},
149 { "bug", Opt_fatal_errors_bug
},
158 static const struct constant_table btrfs_parameter_discard
[] = {
159 { "sync", Opt_discard_sync
},
160 { "async", Opt_discard_async
},
169 static const struct constant_table btrfs_parameter_space_cache
[] = {
170 { "v1", Opt_space_cache_v1
},
171 { "v2", Opt_space_cache_v2
},
176 Opt_rescue_usebackuproot
,
177 Opt_rescue_nologreplay
,
178 Opt_rescue_ignorebadroots
,
179 Opt_rescue_ignoredatacsums
,
180 Opt_rescue_ignoremetacsums
,
181 Opt_rescue_ignoresuperflags
,
182 Opt_rescue_parameter_all
,
185 static const struct constant_table btrfs_parameter_rescue
[] = {
186 { "usebackuproot", Opt_rescue_usebackuproot
},
187 { "nologreplay", Opt_rescue_nologreplay
},
188 { "ignorebadroots", Opt_rescue_ignorebadroots
},
189 { "ibadroots", Opt_rescue_ignorebadroots
},
190 { "ignoredatacsums", Opt_rescue_ignoredatacsums
},
191 { "ignoremetacsums", Opt_rescue_ignoremetacsums
},
192 { "ignoresuperflags", Opt_rescue_ignoresuperflags
},
193 { "idatacsums", Opt_rescue_ignoredatacsums
},
194 { "imetacsums", Opt_rescue_ignoremetacsums
},
195 { "isuperflags", Opt_rescue_ignoresuperflags
},
196 { "all", Opt_rescue_parameter_all
},
200 #ifdef CONFIG_BTRFS_DEBUG
202 Opt_fragment_parameter_data
,
203 Opt_fragment_parameter_metadata
,
204 Opt_fragment_parameter_all
,
207 static const struct constant_table btrfs_parameter_fragment
[] = {
208 { "data", Opt_fragment_parameter_data
},
209 { "metadata", Opt_fragment_parameter_metadata
},
210 { "all", Opt_fragment_parameter_all
},
215 static const struct fs_parameter_spec btrfs_fs_parameters
[] = {
216 fsparam_flag_no("acl", Opt_acl
),
217 fsparam_flag_no("autodefrag", Opt_defrag
),
218 fsparam_flag_no("barrier", Opt_barrier
),
219 fsparam_flag("clear_cache", Opt_clear_cache
),
220 fsparam_u32("commit", Opt_commit_interval
),
221 fsparam_flag("compress", Opt_compress
),
222 fsparam_string("compress", Opt_compress_type
),
223 fsparam_flag("compress-force", Opt_compress_force
),
224 fsparam_string("compress-force", Opt_compress_force_type
),
225 fsparam_flag_no("datacow", Opt_datacow
),
226 fsparam_flag_no("datasum", Opt_datasum
),
227 fsparam_flag("degraded", Opt_degraded
),
228 fsparam_string("device", Opt_device
),
229 fsparam_flag_no("discard", Opt_discard
),
230 fsparam_enum("discard", Opt_discard_mode
, btrfs_parameter_discard
),
231 fsparam_enum("fatal_errors", Opt_fatal_errors
, btrfs_parameter_fatal_errors
),
232 fsparam_flag_no("flushoncommit", Opt_flushoncommit
),
233 fsparam_string("max_inline", Opt_max_inline
),
234 fsparam_u32("metadata_ratio", Opt_ratio
),
235 fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree
),
236 fsparam_flag("skip_balance", Opt_skip_balance
),
237 fsparam_flag_no("space_cache", Opt_space_cache
),
238 fsparam_enum("space_cache", Opt_space_cache_version
, btrfs_parameter_space_cache
),
239 fsparam_flag_no("ssd", Opt_ssd
),
240 fsparam_flag_no("ssd_spread", Opt_ssd_spread
),
241 fsparam_string("subvol", Opt_subvol
),
242 fsparam_flag("subvol=", Opt_subvol_empty
),
243 fsparam_u64("subvolid", Opt_subvolid
),
244 fsparam_u32("thread_pool", Opt_thread_pool
),
245 fsparam_flag_no("treelog", Opt_treelog
),
246 fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed
),
248 /* Rescue options. */
249 fsparam_enum("rescue", Opt_rescue
, btrfs_parameter_rescue
),
250 /* Deprecated, with alias rescue=nologreplay */
251 __fsparam(NULL
, "nologreplay", Opt_nologreplay
, fs_param_deprecated
, NULL
),
252 /* Deprecated, with alias rescue=usebackuproot */
253 __fsparam(NULL
, "usebackuproot", Opt_usebackuproot
, fs_param_deprecated
, NULL
),
254 /* For compatibility only, alias for "rescue=nologreplay". */
255 fsparam_flag("norecovery", Opt_norecovery
),
257 /* Debugging options. */
258 fsparam_flag_no("enospc_debug", Opt_enospc_debug
),
259 #ifdef CONFIG_BTRFS_DEBUG
260 fsparam_enum("fragment", Opt_fragment
, btrfs_parameter_fragment
),
262 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
263 fsparam_flag("ref_verify", Opt_ref_verify
),
268 /* No support for restricting writes to btrfs devices yet... */
269 static inline blk_mode_t
btrfs_open_mode(struct fs_context
*fc
)
271 return sb_open_mode(fc
->sb_flags
) & ~BLK_OPEN_RESTRICT_WRITES
;
274 static int btrfs_parse_param(struct fs_context
*fc
, struct fs_parameter
*param
)
276 struct btrfs_fs_context
*ctx
= fc
->fs_private
;
277 struct fs_parse_result result
;
280 opt
= fs_parse(fc
, btrfs_fs_parameters
, param
, &result
);
286 btrfs_set_opt(ctx
->mount_opt
, DEGRADED
);
288 case Opt_subvol_empty
:
290 * This exists because we used to allow it on accident, so we're
291 * keeping it to maintain ABI. See 37becec95ac3 ("Btrfs: allow
292 * empty subvol= again").
296 kfree(ctx
->subvol_name
);
297 ctx
->subvol_name
= kstrdup(param
->string
, GFP_KERNEL
);
298 if (!ctx
->subvol_name
)
302 ctx
->subvol_objectid
= result
.uint_64
;
304 /* subvolid=0 means give me the original fs_tree. */
305 if (!ctx
->subvol_objectid
)
306 ctx
->subvol_objectid
= BTRFS_FS_TREE_OBJECTID
;
309 struct btrfs_device
*device
;
310 blk_mode_t mode
= btrfs_open_mode(fc
);
312 mutex_lock(&uuid_mutex
);
313 device
= btrfs_scan_one_device(param
->string
, mode
, false);
314 mutex_unlock(&uuid_mutex
);
316 return PTR_ERR(device
);
320 if (result
.negated
) {
321 btrfs_set_opt(ctx
->mount_opt
, NODATASUM
);
323 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
324 btrfs_clear_opt(ctx
->mount_opt
, NODATASUM
);
328 if (result
.negated
) {
329 btrfs_clear_opt(ctx
->mount_opt
, COMPRESS
);
330 btrfs_clear_opt(ctx
->mount_opt
, FORCE_COMPRESS
);
331 btrfs_set_opt(ctx
->mount_opt
, NODATACOW
);
332 btrfs_set_opt(ctx
->mount_opt
, NODATASUM
);
334 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
337 case Opt_compress_force
:
338 case Opt_compress_force_type
:
339 btrfs_set_opt(ctx
->mount_opt
, FORCE_COMPRESS
);
342 case Opt_compress_type
:
343 if (opt
== Opt_compress
|| opt
== Opt_compress_force
) {
344 ctx
->compress_type
= BTRFS_COMPRESS_ZLIB
;
345 ctx
->compress_level
= BTRFS_ZLIB_DEFAULT_LEVEL
;
346 btrfs_set_opt(ctx
->mount_opt
, COMPRESS
);
347 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
348 btrfs_clear_opt(ctx
->mount_opt
, NODATASUM
);
349 } else if (strncmp(param
->string
, "zlib", 4) == 0) {
350 ctx
->compress_type
= BTRFS_COMPRESS_ZLIB
;
351 ctx
->compress_level
=
352 btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB
,
354 btrfs_set_opt(ctx
->mount_opt
, COMPRESS
);
355 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
356 btrfs_clear_opt(ctx
->mount_opt
, NODATASUM
);
357 } else if (strncmp(param
->string
, "lzo", 3) == 0) {
358 ctx
->compress_type
= BTRFS_COMPRESS_LZO
;
359 ctx
->compress_level
= 0;
360 btrfs_set_opt(ctx
->mount_opt
, COMPRESS
);
361 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
362 btrfs_clear_opt(ctx
->mount_opt
, NODATASUM
);
363 } else if (strncmp(param
->string
, "zstd", 4) == 0) {
364 ctx
->compress_type
= BTRFS_COMPRESS_ZSTD
;
365 ctx
->compress_level
=
366 btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD
,
368 btrfs_set_opt(ctx
->mount_opt
, COMPRESS
);
369 btrfs_clear_opt(ctx
->mount_opt
, NODATACOW
);
370 btrfs_clear_opt(ctx
->mount_opt
, NODATASUM
);
371 } else if (strncmp(param
->string
, "no", 2) == 0) {
372 ctx
->compress_level
= 0;
373 ctx
->compress_type
= 0;
374 btrfs_clear_opt(ctx
->mount_opt
, COMPRESS
);
375 btrfs_clear_opt(ctx
->mount_opt
, FORCE_COMPRESS
);
377 btrfs_err(NULL
, "unrecognized compression value %s",
383 if (result
.negated
) {
384 btrfs_set_opt(ctx
->mount_opt
, NOSSD
);
385 btrfs_clear_opt(ctx
->mount_opt
, SSD
);
386 btrfs_clear_opt(ctx
->mount_opt
, SSD_SPREAD
);
388 btrfs_set_opt(ctx
->mount_opt
, SSD
);
389 btrfs_clear_opt(ctx
->mount_opt
, NOSSD
);
393 if (result
.negated
) {
394 btrfs_clear_opt(ctx
->mount_opt
, SSD_SPREAD
);
396 btrfs_set_opt(ctx
->mount_opt
, SSD
);
397 btrfs_set_opt(ctx
->mount_opt
, SSD_SPREAD
);
398 btrfs_clear_opt(ctx
->mount_opt
, NOSSD
);
403 btrfs_set_opt(ctx
->mount_opt
, NOBARRIER
);
405 btrfs_clear_opt(ctx
->mount_opt
, NOBARRIER
);
407 case Opt_thread_pool
:
408 if (result
.uint_32
== 0) {
409 btrfs_err(NULL
, "invalid value 0 for thread_pool");
412 ctx
->thread_pool_size
= result
.uint_32
;
415 ctx
->max_inline
= memparse(param
->string
, NULL
);
418 if (result
.negated
) {
419 fc
->sb_flags
&= ~SB_POSIXACL
;
421 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
422 fc
->sb_flags
|= SB_POSIXACL
;
424 btrfs_err(NULL
, "support for ACL not compiled in");
429 * VFS limits the ability to toggle ACL on and off via remount,
430 * despite every file system allowing this. This seems to be
431 * an oversight since we all do, but it'll fail if we're
432 * remounting. So don't set the mask here, we'll check it in
433 * btrfs_reconfigure and do the toggling ourselves.
435 if (fc
->purpose
!= FS_CONTEXT_FOR_RECONFIGURE
)
436 fc
->sb_flags_mask
|= SB_POSIXACL
;
440 btrfs_set_opt(ctx
->mount_opt
, NOTREELOG
);
442 btrfs_clear_opt(ctx
->mount_opt
, NOTREELOG
);
444 case Opt_nologreplay
:
446 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
447 btrfs_set_opt(ctx
->mount_opt
, NOLOGREPLAY
);
451 "'norecovery' is for compatibility only, recommended to use 'rescue=nologreplay'");
452 btrfs_set_opt(ctx
->mount_opt
, NOLOGREPLAY
);
454 case Opt_flushoncommit
:
456 btrfs_clear_opt(ctx
->mount_opt
, FLUSHONCOMMIT
);
458 btrfs_set_opt(ctx
->mount_opt
, FLUSHONCOMMIT
);
461 ctx
->metadata_ratio
= result
.uint_32
;
464 if (result
.negated
) {
465 btrfs_clear_opt(ctx
->mount_opt
, DISCARD_SYNC
);
466 btrfs_clear_opt(ctx
->mount_opt
, DISCARD_ASYNC
);
467 btrfs_set_opt(ctx
->mount_opt
, NODISCARD
);
469 btrfs_set_opt(ctx
->mount_opt
, DISCARD_SYNC
);
470 btrfs_clear_opt(ctx
->mount_opt
, DISCARD_ASYNC
);
473 case Opt_discard_mode
:
474 switch (result
.uint_32
) {
475 case Opt_discard_sync
:
476 btrfs_clear_opt(ctx
->mount_opt
, DISCARD_ASYNC
);
477 btrfs_set_opt(ctx
->mount_opt
, DISCARD_SYNC
);
479 case Opt_discard_async
:
480 btrfs_clear_opt(ctx
->mount_opt
, DISCARD_SYNC
);
481 btrfs_set_opt(ctx
->mount_opt
, DISCARD_ASYNC
);
484 btrfs_err(NULL
, "unrecognized discard mode value %s",
488 btrfs_clear_opt(ctx
->mount_opt
, NODISCARD
);
490 case Opt_space_cache
:
491 if (result
.negated
) {
492 btrfs_set_opt(ctx
->mount_opt
, NOSPACECACHE
);
493 btrfs_clear_opt(ctx
->mount_opt
, SPACE_CACHE
);
494 btrfs_clear_opt(ctx
->mount_opt
, FREE_SPACE_TREE
);
496 btrfs_clear_opt(ctx
->mount_opt
, FREE_SPACE_TREE
);
497 btrfs_set_opt(ctx
->mount_opt
, SPACE_CACHE
);
500 case Opt_space_cache_version
:
501 switch (result
.uint_32
) {
502 case Opt_space_cache_v1
:
503 btrfs_set_opt(ctx
->mount_opt
, SPACE_CACHE
);
504 btrfs_clear_opt(ctx
->mount_opt
, FREE_SPACE_TREE
);
506 case Opt_space_cache_v2
:
507 btrfs_clear_opt(ctx
->mount_opt
, SPACE_CACHE
);
508 btrfs_set_opt(ctx
->mount_opt
, FREE_SPACE_TREE
);
511 btrfs_err(NULL
, "unrecognized space_cache value %s",
516 case Opt_rescan_uuid_tree
:
517 btrfs_set_opt(ctx
->mount_opt
, RESCAN_UUID_TREE
);
519 case Opt_clear_cache
:
520 btrfs_set_opt(ctx
->mount_opt
, CLEAR_CACHE
);
522 case Opt_user_subvol_rm_allowed
:
523 btrfs_set_opt(ctx
->mount_opt
, USER_SUBVOL_RM_ALLOWED
);
525 case Opt_enospc_debug
:
527 btrfs_clear_opt(ctx
->mount_opt
, ENOSPC_DEBUG
);
529 btrfs_set_opt(ctx
->mount_opt
, ENOSPC_DEBUG
);
533 btrfs_clear_opt(ctx
->mount_opt
, AUTO_DEFRAG
);
535 btrfs_set_opt(ctx
->mount_opt
, AUTO_DEFRAG
);
537 case Opt_usebackuproot
:
539 "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
540 btrfs_set_opt(ctx
->mount_opt
, USEBACKUPROOT
);
542 /* If we're loading the backup roots we can't trust the space cache. */
543 btrfs_set_opt(ctx
->mount_opt
, CLEAR_CACHE
);
545 case Opt_skip_balance
:
546 btrfs_set_opt(ctx
->mount_opt
, SKIP_BALANCE
);
548 case Opt_fatal_errors
:
549 switch (result
.uint_32
) {
550 case Opt_fatal_errors_panic
:
551 btrfs_set_opt(ctx
->mount_opt
, PANIC_ON_FATAL_ERROR
);
553 case Opt_fatal_errors_bug
:
554 btrfs_clear_opt(ctx
->mount_opt
, PANIC_ON_FATAL_ERROR
);
557 btrfs_err(NULL
, "unrecognized fatal_errors value %s",
562 case Opt_commit_interval
:
563 ctx
->commit_interval
= result
.uint_32
;
564 if (ctx
->commit_interval
== 0)
565 ctx
->commit_interval
= BTRFS_DEFAULT_COMMIT_INTERVAL
;
568 switch (result
.uint_32
) {
569 case Opt_rescue_usebackuproot
:
570 btrfs_set_opt(ctx
->mount_opt
, USEBACKUPROOT
);
572 case Opt_rescue_nologreplay
:
573 btrfs_set_opt(ctx
->mount_opt
, NOLOGREPLAY
);
575 case Opt_rescue_ignorebadroots
:
576 btrfs_set_opt(ctx
->mount_opt
, IGNOREBADROOTS
);
578 case Opt_rescue_ignoredatacsums
:
579 btrfs_set_opt(ctx
->mount_opt
, IGNOREDATACSUMS
);
581 case Opt_rescue_ignoremetacsums
:
582 btrfs_set_opt(ctx
->mount_opt
, IGNOREMETACSUMS
);
584 case Opt_rescue_ignoresuperflags
:
585 btrfs_set_opt(ctx
->mount_opt
, IGNORESUPERFLAGS
);
587 case Opt_rescue_parameter_all
:
588 btrfs_set_opt(ctx
->mount_opt
, IGNOREDATACSUMS
);
589 btrfs_set_opt(ctx
->mount_opt
, IGNOREMETACSUMS
);
590 btrfs_set_opt(ctx
->mount_opt
, IGNORESUPERFLAGS
);
591 btrfs_set_opt(ctx
->mount_opt
, IGNOREBADROOTS
);
592 btrfs_set_opt(ctx
->mount_opt
, NOLOGREPLAY
);
595 btrfs_info(NULL
, "unrecognized rescue option '%s'",
600 #ifdef CONFIG_BTRFS_DEBUG
602 switch (result
.uint_32
) {
603 case Opt_fragment_parameter_all
:
604 btrfs_set_opt(ctx
->mount_opt
, FRAGMENT_DATA
);
605 btrfs_set_opt(ctx
->mount_opt
, FRAGMENT_METADATA
);
607 case Opt_fragment_parameter_metadata
:
608 btrfs_set_opt(ctx
->mount_opt
, FRAGMENT_METADATA
);
610 case Opt_fragment_parameter_data
:
611 btrfs_set_opt(ctx
->mount_opt
, FRAGMENT_DATA
);
614 btrfs_info(NULL
, "unrecognized fragment option '%s'",
620 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
622 btrfs_set_opt(ctx
->mount_opt
, REF_VERIFY
);
626 btrfs_err(NULL
, "unrecognized mount option '%s'", param
->key
);
634 * Some options only have meaning at mount time and shouldn't persist across
635 * remounts, or be displayed. Clear these at the end of mount and remount code
638 static void btrfs_clear_oneshot_options(struct btrfs_fs_info
*fs_info
)
640 btrfs_clear_opt(fs_info
->mount_opt
, USEBACKUPROOT
);
641 btrfs_clear_opt(fs_info
->mount_opt
, CLEAR_CACHE
);
642 btrfs_clear_opt(fs_info
->mount_opt
, NOSPACECACHE
);
645 static bool check_ro_option(const struct btrfs_fs_info
*fs_info
,
646 unsigned long long mount_opt
, unsigned long long opt
,
647 const char *opt_name
)
649 if (mount_opt
& opt
) {
650 btrfs_err(fs_info
, "%s must be used with ro mount option",
657 bool btrfs_check_options(const struct btrfs_fs_info
*info
,
658 unsigned long long *mount_opt
,
663 if (!(flags
& SB_RDONLY
) &&
664 (check_ro_option(info
, *mount_opt
, BTRFS_MOUNT_NOLOGREPLAY
, "nologreplay") ||
665 check_ro_option(info
, *mount_opt
, BTRFS_MOUNT_IGNOREBADROOTS
, "ignorebadroots") ||
666 check_ro_option(info
, *mount_opt
, BTRFS_MOUNT_IGNOREDATACSUMS
, "ignoredatacsums") ||
667 check_ro_option(info
, *mount_opt
, BTRFS_MOUNT_IGNOREMETACSUMS
, "ignoremetacsums") ||
668 check_ro_option(info
, *mount_opt
, BTRFS_MOUNT_IGNORESUPERFLAGS
, "ignoresuperflags")))
671 if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
) &&
672 !btrfs_raw_test_opt(*mount_opt
, FREE_SPACE_TREE
) &&
673 !btrfs_raw_test_opt(*mount_opt
, CLEAR_CACHE
)) {
674 btrfs_err(info
, "cannot disable free-space-tree");
677 if (btrfs_fs_compat_ro(info
, BLOCK_GROUP_TREE
) &&
678 !btrfs_raw_test_opt(*mount_opt
, FREE_SPACE_TREE
)) {
679 btrfs_err(info
, "cannot disable free-space-tree with block-group-tree feature");
683 if (btrfs_check_mountopts_zoned(info
, mount_opt
))
686 if (!test_bit(BTRFS_FS_STATE_REMOUNTING
, &info
->fs_state
)) {
687 if (btrfs_raw_test_opt(*mount_opt
, SPACE_CACHE
)) {
688 btrfs_info(info
, "disk space caching is enabled");
690 "space cache v1 is being deprecated and will be removed in a future release, please use -o space_cache=v2");
692 if (btrfs_raw_test_opt(*mount_opt
, FREE_SPACE_TREE
))
693 btrfs_info(info
, "using free-space-tree");
700 * This is subtle, we only call this during open_ctree(). We need to pre-load
701 * the mount options with the on-disk settings. Before the new mount API took
702 * effect we would do this on mount and remount. With the new mount API we'll
703 * only do this on the initial mount.
705 * This isn't a change in behavior, because we're using the current state of the
706 * file system to set the current mount options. If you mounted with special
707 * options to disable these features and then remounted we wouldn't revert the
708 * settings, because mounting without these features cleared the on-disk
709 * settings, so this being called on re-mount is not needed.
711 void btrfs_set_free_space_cache_settings(struct btrfs_fs_info
*fs_info
)
713 if (fs_info
->sectorsize
< PAGE_SIZE
) {
714 btrfs_clear_opt(fs_info
->mount_opt
, SPACE_CACHE
);
715 if (!btrfs_test_opt(fs_info
, FREE_SPACE_TREE
)) {
717 "forcing free space tree for sector size %u with page size %lu",
718 fs_info
->sectorsize
, PAGE_SIZE
);
719 btrfs_set_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
724 * At this point our mount options are populated, so we only mess with
725 * these settings if we don't have any settings already.
727 if (btrfs_test_opt(fs_info
, FREE_SPACE_TREE
))
730 if (btrfs_is_zoned(fs_info
) &&
731 btrfs_free_space_cache_v1_active(fs_info
)) {
732 btrfs_info(fs_info
, "zoned: clearing existing space cache");
733 btrfs_set_super_cache_generation(fs_info
->super_copy
, 0);
737 if (btrfs_test_opt(fs_info
, SPACE_CACHE
))
740 if (btrfs_test_opt(fs_info
, NOSPACECACHE
))
744 * At this point we don't have explicit options set by the user, set
745 * them ourselves based on the state of the file system.
747 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
))
748 btrfs_set_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
749 else if (btrfs_free_space_cache_v1_active(fs_info
))
750 btrfs_set_opt(fs_info
->mount_opt
, SPACE_CACHE
);
753 static void set_device_specific_options(struct btrfs_fs_info
*fs_info
)
755 if (!btrfs_test_opt(fs_info
, NOSSD
) &&
756 !fs_info
->fs_devices
->rotating
)
757 btrfs_set_opt(fs_info
->mount_opt
, SSD
);
760 * For devices supporting discard turn on discard=async automatically,
761 * unless it's already set or disabled. This could be turned off by
762 * nodiscard for the same mount.
764 * The zoned mode piggy backs on the discard functionality for
765 * resetting a zone. There is no reason to delay the zone reset as it is
766 * fast enough. So, do not enable async discard for zoned mode.
768 if (!(btrfs_test_opt(fs_info
, DISCARD_SYNC
) ||
769 btrfs_test_opt(fs_info
, DISCARD_ASYNC
) ||
770 btrfs_test_opt(fs_info
, NODISCARD
)) &&
771 fs_info
->fs_devices
->discardable
&&
772 !btrfs_is_zoned(fs_info
))
773 btrfs_set_opt(fs_info
->mount_opt
, DISCARD_ASYNC
);
776 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info
*fs_info
,
779 struct btrfs_root
*root
= fs_info
->tree_root
;
780 struct btrfs_root
*fs_root
= NULL
;
781 struct btrfs_root_ref
*root_ref
;
782 struct btrfs_inode_ref
*inode_ref
;
783 struct btrfs_key key
;
784 struct btrfs_path
*path
= NULL
;
785 char *name
= NULL
, *ptr
;
790 path
= btrfs_alloc_path();
796 name
= kmalloc(PATH_MAX
, GFP_KERNEL
);
801 ptr
= name
+ PATH_MAX
- 1;
805 * Walk up the subvolume trees in the tree of tree roots by root
806 * backrefs until we hit the top-level subvolume.
808 while (subvol_objectid
!= BTRFS_FS_TREE_OBJECTID
) {
809 key
.objectid
= subvol_objectid
;
810 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
811 key
.offset
= (u64
)-1;
813 ret
= btrfs_search_backwards(root
, &key
, path
);
816 } else if (ret
> 0) {
821 subvol_objectid
= key
.offset
;
823 root_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
824 struct btrfs_root_ref
);
825 len
= btrfs_root_ref_name_len(path
->nodes
[0], root_ref
);
831 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
832 (unsigned long)(root_ref
+ 1), len
);
834 dirid
= btrfs_root_ref_dirid(path
->nodes
[0], root_ref
);
835 btrfs_release_path(path
);
837 fs_root
= btrfs_get_fs_root(fs_info
, subvol_objectid
, true);
838 if (IS_ERR(fs_root
)) {
839 ret
= PTR_ERR(fs_root
);
845 * Walk up the filesystem tree by inode refs until we hit the
848 while (dirid
!= BTRFS_FIRST_FREE_OBJECTID
) {
849 key
.objectid
= dirid
;
850 key
.type
= BTRFS_INODE_REF_KEY
;
851 key
.offset
= (u64
)-1;
853 ret
= btrfs_search_backwards(fs_root
, &key
, path
);
856 } else if (ret
> 0) {
863 inode_ref
= btrfs_item_ptr(path
->nodes
[0],
865 struct btrfs_inode_ref
);
866 len
= btrfs_inode_ref_name_len(path
->nodes
[0],
873 read_extent_buffer(path
->nodes
[0], ptr
+ 1,
874 (unsigned long)(inode_ref
+ 1), len
);
876 btrfs_release_path(path
);
878 btrfs_put_root(fs_root
);
882 btrfs_free_path(path
);
883 if (ptr
== name
+ PATH_MAX
- 1) {
887 memmove(name
, ptr
, name
+ PATH_MAX
- ptr
);
892 btrfs_put_root(fs_root
);
893 btrfs_free_path(path
);
898 static int get_default_subvol_objectid(struct btrfs_fs_info
*fs_info
, u64
*objectid
)
900 struct btrfs_root
*root
= fs_info
->tree_root
;
901 struct btrfs_dir_item
*di
;
902 struct btrfs_path
*path
;
903 struct btrfs_key location
;
904 struct fscrypt_str name
= FSTR_INIT("default", 7);
907 path
= btrfs_alloc_path();
912 * Find the "default" dir item which points to the root item that we
913 * will mount by default if we haven't been given a specific subvolume
916 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
917 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir_id
, &name
, 0);
919 btrfs_free_path(path
);
924 * Ok the default dir item isn't there. This is weird since
925 * it's always been there, but don't freak out, just try and
926 * mount the top-level subvolume.
928 btrfs_free_path(path
);
929 *objectid
= BTRFS_FS_TREE_OBJECTID
;
933 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
934 btrfs_free_path(path
);
935 *objectid
= location
.objectid
;
939 static int btrfs_fill_super(struct super_block
*sb
,
940 struct btrfs_fs_devices
*fs_devices
,
944 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
947 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
948 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
949 sb
->s_op
= &btrfs_super_ops
;
950 sb
->s_d_op
= &btrfs_dentry_operations
;
951 sb
->s_export_op
= &btrfs_export_ops
;
952 #ifdef CONFIG_FS_VERITY
953 sb
->s_vop
= &btrfs_verityops
;
955 sb
->s_xattr
= btrfs_xattr_handlers
;
957 sb
->s_iflags
|= SB_I_CGROUPWB
;
959 err
= super_setup_bdi(sb
);
961 btrfs_err(fs_info
, "super_setup_bdi failed");
965 err
= open_ctree(sb
, fs_devices
, (char *)data
);
967 btrfs_err(fs_info
, "open_ctree failed");
971 inode
= btrfs_iget(BTRFS_FIRST_FREE_OBJECTID
, fs_info
->fs_root
);
973 err
= PTR_ERR(inode
);
974 btrfs_handle_fs_error(fs_info
, err
, NULL
);
978 sb
->s_root
= d_make_root(inode
);
984 sb
->s_flags
|= SB_ACTIVE
;
988 close_ctree(fs_info
);
992 int btrfs_sync_fs(struct super_block
*sb
, int wait
)
994 struct btrfs_trans_handle
*trans
;
995 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
996 struct btrfs_root
*root
= fs_info
->tree_root
;
998 trace_btrfs_sync_fs(fs_info
, wait
);
1001 filemap_flush(fs_info
->btree_inode
->i_mapping
);
1005 btrfs_wait_ordered_roots(fs_info
, U64_MAX
, NULL
);
1007 trans
= btrfs_attach_transaction_barrier(root
);
1008 if (IS_ERR(trans
)) {
1009 /* no transaction, don't bother */
1010 if (PTR_ERR(trans
) == -ENOENT
) {
1012 * Exit unless we have some pending changes
1013 * that need to go through commit
1015 if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT
,
1019 * A non-blocking test if the fs is frozen. We must not
1020 * start a new transaction here otherwise a deadlock
1021 * happens. The pending operations are delayed to the
1022 * next commit after thawing.
1024 if (sb_start_write_trylock(sb
))
1028 trans
= btrfs_start_transaction(root
, 0);
1031 return PTR_ERR(trans
);
1033 return btrfs_commit_transaction(trans
);
1036 static void print_rescue_option(struct seq_file
*seq
, const char *s
, bool *printed
)
1038 seq_printf(seq
, "%s%s", (*printed
) ? ":" : ",rescue=", s
);
1042 static int btrfs_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1044 struct btrfs_fs_info
*info
= btrfs_sb(dentry
->d_sb
);
1045 const char *compress_type
;
1046 const char *subvol_name
;
1047 bool printed
= false;
1049 if (btrfs_test_opt(info
, DEGRADED
))
1050 seq_puts(seq
, ",degraded");
1051 if (btrfs_test_opt(info
, NODATASUM
))
1052 seq_puts(seq
, ",nodatasum");
1053 if (btrfs_test_opt(info
, NODATACOW
))
1054 seq_puts(seq
, ",nodatacow");
1055 if (btrfs_test_opt(info
, NOBARRIER
))
1056 seq_puts(seq
, ",nobarrier");
1057 if (info
->max_inline
!= BTRFS_DEFAULT_MAX_INLINE
)
1058 seq_printf(seq
, ",max_inline=%llu", info
->max_inline
);
1059 if (info
->thread_pool_size
!= min_t(unsigned long,
1060 num_online_cpus() + 2, 8))
1061 seq_printf(seq
, ",thread_pool=%u", info
->thread_pool_size
);
1062 if (btrfs_test_opt(info
, COMPRESS
)) {
1063 compress_type
= btrfs_compress_type2str(info
->compress_type
);
1064 if (btrfs_test_opt(info
, FORCE_COMPRESS
))
1065 seq_printf(seq
, ",compress-force=%s", compress_type
);
1067 seq_printf(seq
, ",compress=%s", compress_type
);
1068 if (info
->compress_level
)
1069 seq_printf(seq
, ":%d", info
->compress_level
);
1071 if (btrfs_test_opt(info
, NOSSD
))
1072 seq_puts(seq
, ",nossd");
1073 if (btrfs_test_opt(info
, SSD_SPREAD
))
1074 seq_puts(seq
, ",ssd_spread");
1075 else if (btrfs_test_opt(info
, SSD
))
1076 seq_puts(seq
, ",ssd");
1077 if (btrfs_test_opt(info
, NOTREELOG
))
1078 seq_puts(seq
, ",notreelog");
1079 if (btrfs_test_opt(info
, NOLOGREPLAY
))
1080 print_rescue_option(seq
, "nologreplay", &printed
);
1081 if (btrfs_test_opt(info
, USEBACKUPROOT
))
1082 print_rescue_option(seq
, "usebackuproot", &printed
);
1083 if (btrfs_test_opt(info
, IGNOREBADROOTS
))
1084 print_rescue_option(seq
, "ignorebadroots", &printed
);
1085 if (btrfs_test_opt(info
, IGNOREDATACSUMS
))
1086 print_rescue_option(seq
, "ignoredatacsums", &printed
);
1087 if (btrfs_test_opt(info
, IGNOREMETACSUMS
))
1088 print_rescue_option(seq
, "ignoremetacsums", &printed
);
1089 if (btrfs_test_opt(info
, IGNORESUPERFLAGS
))
1090 print_rescue_option(seq
, "ignoresuperflags", &printed
);
1091 if (btrfs_test_opt(info
, FLUSHONCOMMIT
))
1092 seq_puts(seq
, ",flushoncommit");
1093 if (btrfs_test_opt(info
, DISCARD_SYNC
))
1094 seq_puts(seq
, ",discard");
1095 if (btrfs_test_opt(info
, DISCARD_ASYNC
))
1096 seq_puts(seq
, ",discard=async");
1097 if (!(info
->sb
->s_flags
& SB_POSIXACL
))
1098 seq_puts(seq
, ",noacl");
1099 if (btrfs_free_space_cache_v1_active(info
))
1100 seq_puts(seq
, ",space_cache");
1101 else if (btrfs_fs_compat_ro(info
, FREE_SPACE_TREE
))
1102 seq_puts(seq
, ",space_cache=v2");
1104 seq_puts(seq
, ",nospace_cache");
1105 if (btrfs_test_opt(info
, RESCAN_UUID_TREE
))
1106 seq_puts(seq
, ",rescan_uuid_tree");
1107 if (btrfs_test_opt(info
, CLEAR_CACHE
))
1108 seq_puts(seq
, ",clear_cache");
1109 if (btrfs_test_opt(info
, USER_SUBVOL_RM_ALLOWED
))
1110 seq_puts(seq
, ",user_subvol_rm_allowed");
1111 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
1112 seq_puts(seq
, ",enospc_debug");
1113 if (btrfs_test_opt(info
, AUTO_DEFRAG
))
1114 seq_puts(seq
, ",autodefrag");
1115 if (btrfs_test_opt(info
, SKIP_BALANCE
))
1116 seq_puts(seq
, ",skip_balance");
1117 if (info
->metadata_ratio
)
1118 seq_printf(seq
, ",metadata_ratio=%u", info
->metadata_ratio
);
1119 if (btrfs_test_opt(info
, PANIC_ON_FATAL_ERROR
))
1120 seq_puts(seq
, ",fatal_errors=panic");
1121 if (info
->commit_interval
!= BTRFS_DEFAULT_COMMIT_INTERVAL
)
1122 seq_printf(seq
, ",commit=%u", info
->commit_interval
);
1123 #ifdef CONFIG_BTRFS_DEBUG
1124 if (btrfs_test_opt(info
, FRAGMENT_DATA
))
1125 seq_puts(seq
, ",fragment=data");
1126 if (btrfs_test_opt(info
, FRAGMENT_METADATA
))
1127 seq_puts(seq
, ",fragment=metadata");
1129 if (btrfs_test_opt(info
, REF_VERIFY
))
1130 seq_puts(seq
, ",ref_verify");
1131 seq_printf(seq
, ",subvolid=%llu", btrfs_root_id(BTRFS_I(d_inode(dentry
))->root
));
1132 subvol_name
= btrfs_get_subvol_name_from_objectid(info
,
1133 btrfs_root_id(BTRFS_I(d_inode(dentry
))->root
));
1134 if (!IS_ERR(subvol_name
)) {
1135 seq_puts(seq
, ",subvol=");
1136 seq_escape(seq
, subvol_name
, " \t\n\\");
1143 * subvolumes are identified by ino 256
1145 static inline int is_subvolume_inode(struct inode
*inode
)
1147 if (inode
&& inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
1152 static struct dentry
*mount_subvol(const char *subvol_name
, u64 subvol_objectid
,
1153 struct vfsmount
*mnt
)
1155 struct dentry
*root
;
1159 if (!subvol_objectid
) {
1160 ret
= get_default_subvol_objectid(btrfs_sb(mnt
->mnt_sb
),
1163 root
= ERR_PTR(ret
);
1167 subvol_name
= btrfs_get_subvol_name_from_objectid(
1168 btrfs_sb(mnt
->mnt_sb
), subvol_objectid
);
1169 if (IS_ERR(subvol_name
)) {
1170 root
= ERR_CAST(subvol_name
);
1177 root
= mount_subtree(mnt
, subvol_name
);
1178 /* mount_subtree() drops our reference on the vfsmount. */
1181 if (!IS_ERR(root
)) {
1182 struct super_block
*s
= root
->d_sb
;
1183 struct btrfs_fs_info
*fs_info
= btrfs_sb(s
);
1184 struct inode
*root_inode
= d_inode(root
);
1185 u64 root_objectid
= btrfs_root_id(BTRFS_I(root_inode
)->root
);
1188 if (!is_subvolume_inode(root_inode
)) {
1189 btrfs_err(fs_info
, "'%s' is not a valid subvolume",
1193 if (subvol_objectid
&& root_objectid
!= subvol_objectid
) {
1195 * This will also catch a race condition where a
1196 * subvolume which was passed by ID is renamed and
1197 * another subvolume is renamed over the old location.
1200 "subvol '%s' does not match subvolid %llu",
1201 subvol_name
, subvol_objectid
);
1206 root
= ERR_PTR(ret
);
1207 deactivate_locked_super(s
);
1217 static void btrfs_resize_thread_pool(struct btrfs_fs_info
*fs_info
,
1218 u32 new_pool_size
, u32 old_pool_size
)
1220 if (new_pool_size
== old_pool_size
)
1223 fs_info
->thread_pool_size
= new_pool_size
;
1225 btrfs_info(fs_info
, "resize thread pool %d -> %d",
1226 old_pool_size
, new_pool_size
);
1228 btrfs_workqueue_set_max(fs_info
->workers
, new_pool_size
);
1229 btrfs_workqueue_set_max(fs_info
->delalloc_workers
, new_pool_size
);
1230 btrfs_workqueue_set_max(fs_info
->caching_workers
, new_pool_size
);
1231 workqueue_set_max_active(fs_info
->endio_workers
, new_pool_size
);
1232 workqueue_set_max_active(fs_info
->endio_meta_workers
, new_pool_size
);
1233 btrfs_workqueue_set_max(fs_info
->endio_write_workers
, new_pool_size
);
1234 btrfs_workqueue_set_max(fs_info
->endio_freespace_worker
, new_pool_size
);
1235 btrfs_workqueue_set_max(fs_info
->delayed_workers
, new_pool_size
);
1238 static inline void btrfs_remount_begin(struct btrfs_fs_info
*fs_info
,
1239 unsigned long long old_opts
, int flags
)
1241 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1242 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) ||
1243 (flags
& SB_RDONLY
))) {
1244 /* wait for any defraggers to finish */
1245 wait_event(fs_info
->transaction_wait
,
1246 (atomic_read(&fs_info
->defrag_running
) == 0));
1247 if (flags
& SB_RDONLY
)
1248 sync_filesystem(fs_info
->sb
);
1252 static inline void btrfs_remount_cleanup(struct btrfs_fs_info
*fs_info
,
1253 unsigned long long old_opts
)
1255 const bool cache_opt
= btrfs_test_opt(fs_info
, SPACE_CACHE
);
1258 * We need to cleanup all defragable inodes if the autodefragment is
1259 * close or the filesystem is read only.
1261 if (btrfs_raw_test_opt(old_opts
, AUTO_DEFRAG
) &&
1262 (!btrfs_raw_test_opt(fs_info
->mount_opt
, AUTO_DEFRAG
) || sb_rdonly(fs_info
->sb
))) {
1263 btrfs_cleanup_defrag_inodes(fs_info
);
1266 /* If we toggled discard async */
1267 if (!btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1268 btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1269 btrfs_discard_resume(fs_info
);
1270 else if (btrfs_raw_test_opt(old_opts
, DISCARD_ASYNC
) &&
1271 !btrfs_test_opt(fs_info
, DISCARD_ASYNC
))
1272 btrfs_discard_cleanup(fs_info
);
1274 /* If we toggled space cache */
1275 if (cache_opt
!= btrfs_free_space_cache_v1_active(fs_info
))
1276 btrfs_set_free_space_cache_v1_active(fs_info
, cache_opt
);
1279 static int btrfs_remount_rw(struct btrfs_fs_info
*fs_info
)
1283 if (BTRFS_FS_ERROR(fs_info
)) {
1285 "remounting read-write after error is not allowed");
1289 if (fs_info
->fs_devices
->rw_devices
== 0)
1292 if (!btrfs_check_rw_degradable(fs_info
, NULL
)) {
1294 "too many missing devices, writable remount is not allowed");
1298 if (btrfs_super_log_root(fs_info
->super_copy
) != 0) {
1300 "mount required to replay tree-log, cannot remount read-write");
1305 * NOTE: when remounting with a change that does writes, don't put it
1306 * anywhere above this point, as we are not sure to be safe to write
1307 * until we pass the above checks.
1309 ret
= btrfs_start_pre_rw_mount(fs_info
);
1313 btrfs_clear_sb_rdonly(fs_info
->sb
);
1315 set_bit(BTRFS_FS_OPEN
, &fs_info
->flags
);
1318 * If we've gone from readonly -> read-write, we need to get our
1319 * sync/async discard lists in the right state.
1321 btrfs_discard_resume(fs_info
);
1326 static int btrfs_remount_ro(struct btrfs_fs_info
*fs_info
)
1329 * This also happens on 'umount -rf' or on shutdown, when the
1330 * filesystem is busy.
1332 cancel_work_sync(&fs_info
->async_reclaim_work
);
1333 cancel_work_sync(&fs_info
->async_data_reclaim_work
);
1335 btrfs_discard_cleanup(fs_info
);
1337 /* Wait for the uuid_scan task to finish */
1338 down(&fs_info
->uuid_tree_rescan_sem
);
1339 /* Avoid complains from lockdep et al. */
1340 up(&fs_info
->uuid_tree_rescan_sem
);
1342 btrfs_set_sb_rdonly(fs_info
->sb
);
1345 * Setting SB_RDONLY will put the cleaner thread to sleep at the next
1346 * loop if it's already active. If it's already asleep, we'll leave
1347 * unused block groups on disk until we're mounted read-write again
1348 * unless we clean them up here.
1350 btrfs_delete_unused_bgs(fs_info
);
1353 * The cleaner task could be already running before we set the flag
1354 * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). We must make
1355 * sure that after we finish the remount, i.e. after we call
1356 * btrfs_commit_super(), the cleaner can no longer start a transaction
1357 * - either because it was dropping a dead root, running delayed iputs
1358 * or deleting an unused block group (the cleaner picked a block
1359 * group from the list of unused block groups before we were able to
1360 * in the previous call to btrfs_delete_unused_bgs()).
1362 wait_on_bit(&fs_info
->flags
, BTRFS_FS_CLEANER_RUNNING
, TASK_UNINTERRUPTIBLE
);
1365 * We've set the superblock to RO mode, so we might have made the
1366 * cleaner task sleep without running all pending delayed iputs. Go
1367 * through all the delayed iputs here, so that if an unmount happens
1368 * without remounting RW we don't end up at finishing close_ctree()
1369 * with a non-empty list of delayed iputs.
1371 btrfs_run_delayed_iputs(fs_info
);
1373 btrfs_dev_replace_suspend_for_unmount(fs_info
);
1374 btrfs_scrub_cancel(fs_info
);
1375 btrfs_pause_balance(fs_info
);
1378 * Pause the qgroup rescan worker if it is running. We don't want it to
1379 * be still running after we are in RO mode, as after that, by the time
1380 * we unmount, it might have left a transaction open, so we would leak
1381 * the transaction and/or crash.
1383 btrfs_qgroup_wait_for_completion(fs_info
, false);
1385 return btrfs_commit_super(fs_info
);
1388 static void btrfs_ctx_to_info(struct btrfs_fs_info
*fs_info
, struct btrfs_fs_context
*ctx
)
1390 fs_info
->max_inline
= ctx
->max_inline
;
1391 fs_info
->commit_interval
= ctx
->commit_interval
;
1392 fs_info
->metadata_ratio
= ctx
->metadata_ratio
;
1393 fs_info
->thread_pool_size
= ctx
->thread_pool_size
;
1394 fs_info
->mount_opt
= ctx
->mount_opt
;
1395 fs_info
->compress_type
= ctx
->compress_type
;
1396 fs_info
->compress_level
= ctx
->compress_level
;
1399 static void btrfs_info_to_ctx(struct btrfs_fs_info
*fs_info
, struct btrfs_fs_context
*ctx
)
1401 ctx
->max_inline
= fs_info
->max_inline
;
1402 ctx
->commit_interval
= fs_info
->commit_interval
;
1403 ctx
->metadata_ratio
= fs_info
->metadata_ratio
;
1404 ctx
->thread_pool_size
= fs_info
->thread_pool_size
;
1405 ctx
->mount_opt
= fs_info
->mount_opt
;
1406 ctx
->compress_type
= fs_info
->compress_type
;
1407 ctx
->compress_level
= fs_info
->compress_level
;
1410 #define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...) \
1412 if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
1413 btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
1414 btrfs_info(fs_info, fmt, ##args); \
1417 #define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...) \
1419 if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
1420 !btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
1421 btrfs_info(fs_info, fmt, ##args); \
1424 static void btrfs_emit_options(struct btrfs_fs_info
*info
,
1425 struct btrfs_fs_context
*old
)
1427 btrfs_info_if_set(info
, old
, NODATASUM
, "setting nodatasum");
1428 btrfs_info_if_set(info
, old
, DEGRADED
, "allowing degraded mounts");
1429 btrfs_info_if_set(info
, old
, NODATASUM
, "setting nodatasum");
1430 btrfs_info_if_set(info
, old
, SSD
, "enabling ssd optimizations");
1431 btrfs_info_if_set(info
, old
, SSD_SPREAD
, "using spread ssd allocation scheme");
1432 btrfs_info_if_set(info
, old
, NOBARRIER
, "turning off barriers");
1433 btrfs_info_if_set(info
, old
, NOTREELOG
, "disabling tree log");
1434 btrfs_info_if_set(info
, old
, NOLOGREPLAY
, "disabling log replay at mount time");
1435 btrfs_info_if_set(info
, old
, FLUSHONCOMMIT
, "turning on flush-on-commit");
1436 btrfs_info_if_set(info
, old
, DISCARD_SYNC
, "turning on sync discard");
1437 btrfs_info_if_set(info
, old
, DISCARD_ASYNC
, "turning on async discard");
1438 btrfs_info_if_set(info
, old
, FREE_SPACE_TREE
, "enabling free space tree");
1439 btrfs_info_if_set(info
, old
, SPACE_CACHE
, "enabling disk space caching");
1440 btrfs_info_if_set(info
, old
, CLEAR_CACHE
, "force clearing of disk cache");
1441 btrfs_info_if_set(info
, old
, AUTO_DEFRAG
, "enabling auto defrag");
1442 btrfs_info_if_set(info
, old
, FRAGMENT_DATA
, "fragmenting data");
1443 btrfs_info_if_set(info
, old
, FRAGMENT_METADATA
, "fragmenting metadata");
1444 btrfs_info_if_set(info
, old
, REF_VERIFY
, "doing ref verification");
1445 btrfs_info_if_set(info
, old
, USEBACKUPROOT
, "trying to use backup root at mount time");
1446 btrfs_info_if_set(info
, old
, IGNOREBADROOTS
, "ignoring bad roots");
1447 btrfs_info_if_set(info
, old
, IGNOREDATACSUMS
, "ignoring data csums");
1448 btrfs_info_if_set(info
, old
, IGNOREMETACSUMS
, "ignoring meta csums");
1449 btrfs_info_if_set(info
, old
, IGNORESUPERFLAGS
, "ignoring unknown super block flags");
1451 btrfs_info_if_unset(info
, old
, NODATACOW
, "setting datacow");
1452 btrfs_info_if_unset(info
, old
, SSD
, "not using ssd optimizations");
1453 btrfs_info_if_unset(info
, old
, SSD_SPREAD
, "not using spread ssd allocation scheme");
1454 btrfs_info_if_unset(info
, old
, NOBARRIER
, "turning off barriers");
1455 btrfs_info_if_unset(info
, old
, NOTREELOG
, "enabling tree log");
1456 btrfs_info_if_unset(info
, old
, SPACE_CACHE
, "disabling disk space caching");
1457 btrfs_info_if_unset(info
, old
, FREE_SPACE_TREE
, "disabling free space tree");
1458 btrfs_info_if_unset(info
, old
, AUTO_DEFRAG
, "disabling auto defrag");
1459 btrfs_info_if_unset(info
, old
, COMPRESS
, "use no compression");
1461 /* Did the compression settings change? */
1462 if (btrfs_test_opt(info
, COMPRESS
) &&
1464 old
->compress_type
!= info
->compress_type
||
1465 old
->compress_level
!= info
->compress_level
||
1466 (!btrfs_raw_test_opt(old
->mount_opt
, FORCE_COMPRESS
) &&
1467 btrfs_raw_test_opt(info
->mount_opt
, FORCE_COMPRESS
)))) {
1468 const char *compress_type
= btrfs_compress_type2str(info
->compress_type
);
1470 btrfs_info(info
, "%s %s compression, level %d",
1471 btrfs_test_opt(info
, FORCE_COMPRESS
) ? "force" : "use",
1472 compress_type
, info
->compress_level
);
1475 if (info
->max_inline
!= BTRFS_DEFAULT_MAX_INLINE
)
1476 btrfs_info(info
, "max_inline set to %llu", info
->max_inline
);
1479 static int btrfs_reconfigure(struct fs_context
*fc
)
1481 struct super_block
*sb
= fc
->root
->d_sb
;
1482 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1483 struct btrfs_fs_context
*ctx
= fc
->fs_private
;
1484 struct btrfs_fs_context old_ctx
;
1486 bool mount_reconfigure
= (fc
->s_fs_info
!= NULL
);
1488 btrfs_info_to_ctx(fs_info
, &old_ctx
);
1491 * This is our "bind mount" trick, we don't want to allow the user to do
1492 * anything other than mount a different ro/rw and a different subvol,
1493 * all of the mount options should be maintained.
1495 if (mount_reconfigure
)
1496 ctx
->mount_opt
= old_ctx
.mount_opt
;
1498 sync_filesystem(sb
);
1499 set_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1501 if (!mount_reconfigure
&&
1502 !btrfs_check_options(fs_info
, &ctx
->mount_opt
, fc
->sb_flags
))
1505 ret
= btrfs_check_features(fs_info
, !(fc
->sb_flags
& SB_RDONLY
));
1509 btrfs_ctx_to_info(fs_info
, ctx
);
1510 btrfs_remount_begin(fs_info
, old_ctx
.mount_opt
, fc
->sb_flags
);
1511 btrfs_resize_thread_pool(fs_info
, fs_info
->thread_pool_size
,
1512 old_ctx
.thread_pool_size
);
1514 if ((bool)btrfs_test_opt(fs_info
, FREE_SPACE_TREE
) !=
1515 (bool)btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
) &&
1516 (!sb_rdonly(sb
) || (fc
->sb_flags
& SB_RDONLY
))) {
1518 "remount supports changing free space tree only from RO to RW");
1519 /* Make sure free space cache options match the state on disk. */
1520 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
)) {
1521 btrfs_set_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1522 btrfs_clear_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1524 if (btrfs_free_space_cache_v1_active(fs_info
)) {
1525 btrfs_clear_opt(fs_info
->mount_opt
, FREE_SPACE_TREE
);
1526 btrfs_set_opt(fs_info
->mount_opt
, SPACE_CACHE
);
1531 if (!sb_rdonly(sb
) && (fc
->sb_flags
& SB_RDONLY
))
1532 ret
= btrfs_remount_ro(fs_info
);
1533 else if (sb_rdonly(sb
) && !(fc
->sb_flags
& SB_RDONLY
))
1534 ret
= btrfs_remount_rw(fs_info
);
1539 * If we set the mask during the parameter parsing VFS would reject the
1540 * remount. Here we can set the mask and the value will be updated
1543 if ((fc
->sb_flags
& SB_POSIXACL
) != (sb
->s_flags
& SB_POSIXACL
))
1544 fc
->sb_flags_mask
|= SB_POSIXACL
;
1546 btrfs_emit_options(fs_info
, &old_ctx
);
1547 wake_up_process(fs_info
->transaction_kthread
);
1548 btrfs_remount_cleanup(fs_info
, old_ctx
.mount_opt
);
1549 btrfs_clear_oneshot_options(fs_info
);
1550 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1554 btrfs_ctx_to_info(fs_info
, &old_ctx
);
1555 btrfs_remount_cleanup(fs_info
, old_ctx
.mount_opt
);
1556 clear_bit(BTRFS_FS_STATE_REMOUNTING
, &fs_info
->fs_state
);
1560 /* Used to sort the devices by max_avail(descending sort) */
1561 static int btrfs_cmp_device_free_bytes(const void *a
, const void *b
)
1563 const struct btrfs_device_info
*dev_info1
= a
;
1564 const struct btrfs_device_info
*dev_info2
= b
;
1566 if (dev_info1
->max_avail
> dev_info2
->max_avail
)
1568 else if (dev_info1
->max_avail
< dev_info2
->max_avail
)
1574 * sort the devices by max_avail, in which max free extent size of each device
1575 * is stored.(Descending Sort)
1577 static inline void btrfs_descending_sort_devices(
1578 struct btrfs_device_info
*devices
,
1581 sort(devices
, nr_devices
, sizeof(struct btrfs_device_info
),
1582 btrfs_cmp_device_free_bytes
, NULL
);
1586 * The helper to calc the free space on the devices that can be used to store
1589 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info
*fs_info
,
1592 struct btrfs_device_info
*devices_info
;
1593 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
1594 struct btrfs_device
*device
;
1597 u64 min_stripe_size
;
1598 int num_stripes
= 1;
1599 int i
= 0, nr_devices
;
1600 const struct btrfs_raid_attr
*rattr
;
1603 * We aren't under the device list lock, so this is racy-ish, but good
1604 * enough for our purposes.
1606 nr_devices
= fs_info
->fs_devices
->open_devices
;
1609 nr_devices
= fs_info
->fs_devices
->open_devices
;
1617 devices_info
= kmalloc_array(nr_devices
, sizeof(*devices_info
),
1622 /* calc min stripe number for data space allocation */
1623 type
= btrfs_data_alloc_profile(fs_info
);
1624 rattr
= &btrfs_raid_array
[btrfs_bg_flags_to_raid_index(type
)];
1626 if (type
& BTRFS_BLOCK_GROUP_RAID0
)
1627 num_stripes
= nr_devices
;
1628 else if (type
& BTRFS_BLOCK_GROUP_RAID1_MASK
)
1629 num_stripes
= rattr
->ncopies
;
1630 else if (type
& BTRFS_BLOCK_GROUP_RAID10
)
1633 /* Adjust for more than 1 stripe per device */
1634 min_stripe_size
= rattr
->dev_stripes
* BTRFS_STRIPE_LEN
;
1637 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
1638 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
1639 &device
->dev_state
) ||
1641 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
1644 if (i
>= nr_devices
)
1647 avail_space
= device
->total_bytes
- device
->bytes_used
;
1649 /* align with stripe_len */
1650 avail_space
= rounddown(avail_space
, BTRFS_STRIPE_LEN
);
1653 * Ensure we have at least min_stripe_size on top of the
1654 * reserved space on the device.
1656 if (avail_space
<= BTRFS_DEVICE_RANGE_RESERVED
+ min_stripe_size
)
1659 avail_space
-= BTRFS_DEVICE_RANGE_RESERVED
;
1661 devices_info
[i
].dev
= device
;
1662 devices_info
[i
].max_avail
= avail_space
;
1670 btrfs_descending_sort_devices(devices_info
, nr_devices
);
1674 while (nr_devices
>= rattr
->devs_min
) {
1675 num_stripes
= min(num_stripes
, nr_devices
);
1677 if (devices_info
[i
].max_avail
>= min_stripe_size
) {
1681 avail_space
+= devices_info
[i
].max_avail
* num_stripes
;
1682 alloc_size
= devices_info
[i
].max_avail
;
1683 for (j
= i
+ 1 - num_stripes
; j
<= i
; j
++)
1684 devices_info
[j
].max_avail
-= alloc_size
;
1690 kfree(devices_info
);
1691 *free_bytes
= avail_space
;
1696 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
1698 * If there's a redundant raid level at DATA block groups, use the respective
1699 * multiplier to scale the sizes.
1701 * Unused device space usage is based on simulating the chunk allocator
1702 * algorithm that respects the device sizes and order of allocations. This is
1703 * a close approximation of the actual use but there are other factors that may
1704 * change the result (like a new metadata chunk).
1706 * If metadata is exhausted, f_bavail will be 0.
1708 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1710 struct btrfs_fs_info
*fs_info
= btrfs_sb(dentry
->d_sb
);
1711 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
1712 struct btrfs_space_info
*found
;
1714 u64 total_free_data
= 0;
1715 u64 total_free_meta
= 0;
1716 u32 bits
= fs_info
->sectorsize_bits
;
1717 __be32
*fsid
= (__be32
*)fs_info
->fs_devices
->fsid
;
1718 unsigned factor
= 1;
1719 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
1724 list_for_each_entry(found
, &fs_info
->space_info
, list
) {
1725 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
1728 total_free_data
+= found
->disk_total
- found
->disk_used
;
1730 btrfs_account_ro_block_groups_free_space(found
);
1732 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1733 if (!list_empty(&found
->block_groups
[i
]))
1734 factor
= btrfs_bg_type_to_factor(
1735 btrfs_raid_array
[i
].bg_flag
);
1740 * Metadata in mixed block group profiles are accounted in data
1742 if (!mixed
&& found
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
1743 if (found
->flags
& BTRFS_BLOCK_GROUP_DATA
)
1746 total_free_meta
+= found
->disk_total
-
1750 total_used
+= found
->disk_used
;
1753 buf
->f_blocks
= div_u64(btrfs_super_total_bytes(disk_super
), factor
);
1754 buf
->f_blocks
>>= bits
;
1755 buf
->f_bfree
= buf
->f_blocks
- (div_u64(total_used
, factor
) >> bits
);
1757 /* Account global block reserve as used, it's in logical size already */
1758 spin_lock(&block_rsv
->lock
);
1759 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
1760 if (buf
->f_bfree
>= block_rsv
->size
>> bits
)
1761 buf
->f_bfree
-= block_rsv
->size
>> bits
;
1764 spin_unlock(&block_rsv
->lock
);
1766 buf
->f_bavail
= div_u64(total_free_data
, factor
);
1767 ret
= btrfs_calc_avail_data_space(fs_info
, &total_free_data
);
1770 buf
->f_bavail
+= div_u64(total_free_data
, factor
);
1771 buf
->f_bavail
= buf
->f_bavail
>> bits
;
1774 * We calculate the remaining metadata space minus global reserve. If
1775 * this is (supposedly) smaller than zero, there's no space. But this
1776 * does not hold in practice, the exhausted state happens where's still
1777 * some positive delta. So we apply some guesswork and compare the
1778 * delta to a 4M threshold. (Practically observed delta was ~2M.)
1780 * We probably cannot calculate the exact threshold value because this
1781 * depends on the internal reservations requested by various
1782 * operations, so some operations that consume a few metadata will
1783 * succeed even if the Avail is zero. But this is better than the other
1789 * We only want to claim there's no available space if we can no longer
1790 * allocate chunks for our metadata profile and our global reserve will
1791 * not fit in the free metadata space. If we aren't ->full then we
1792 * still can allocate chunks and thus are fine using the currently
1793 * calculated f_bavail.
1795 if (!mixed
&& block_rsv
->space_info
->full
&&
1796 (total_free_meta
< thresh
|| total_free_meta
- thresh
< block_rsv
->size
))
1799 buf
->f_type
= BTRFS_SUPER_MAGIC
;
1800 buf
->f_bsize
= fs_info
->sectorsize
;
1801 buf
->f_namelen
= BTRFS_NAME_LEN
;
1803 /* We treat it as constant endianness (it doesn't matter _which_)
1804 because we want the fsid to come out the same whether mounted
1805 on a big-endian or little-endian host */
1806 buf
->f_fsid
.val
[0] = be32_to_cpu(fsid
[0]) ^ be32_to_cpu(fsid
[2]);
1807 buf
->f_fsid
.val
[1] = be32_to_cpu(fsid
[1]) ^ be32_to_cpu(fsid
[3]);
1808 /* Mask in the root object ID too, to disambiguate subvols */
1809 buf
->f_fsid
.val
[0] ^= btrfs_root_id(BTRFS_I(d_inode(dentry
))->root
) >> 32;
1810 buf
->f_fsid
.val
[1] ^= btrfs_root_id(BTRFS_I(d_inode(dentry
))->root
);
1815 static int btrfs_fc_test_super(struct super_block
*sb
, struct fs_context
*fc
)
1817 struct btrfs_fs_info
*p
= fc
->s_fs_info
;
1818 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
1820 return fs_info
->fs_devices
== p
->fs_devices
;
1823 static int btrfs_get_tree_super(struct fs_context
*fc
)
1825 struct btrfs_fs_info
*fs_info
= fc
->s_fs_info
;
1826 struct btrfs_fs_context
*ctx
= fc
->fs_private
;
1827 struct btrfs_fs_devices
*fs_devices
= NULL
;
1828 struct block_device
*bdev
;
1829 struct btrfs_device
*device
;
1830 struct super_block
*sb
;
1831 blk_mode_t mode
= btrfs_open_mode(fc
);
1834 btrfs_ctx_to_info(fs_info
, ctx
);
1835 mutex_lock(&uuid_mutex
);
1838 * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
1839 * either a valid device or an error.
1841 device
= btrfs_scan_one_device(fc
->source
, mode
, true);
1842 ASSERT(device
!= NULL
);
1843 if (IS_ERR(device
)) {
1844 mutex_unlock(&uuid_mutex
);
1845 return PTR_ERR(device
);
1848 fs_devices
= device
->fs_devices
;
1849 fs_info
->fs_devices
= fs_devices
;
1851 ret
= btrfs_open_devices(fs_devices
, mode
, &btrfs_fs_type
);
1852 mutex_unlock(&uuid_mutex
);
1856 if (!(fc
->sb_flags
& SB_RDONLY
) && fs_devices
->rw_devices
== 0) {
1861 bdev
= fs_devices
->latest_dev
->bdev
;
1864 * From now on the error handling is not straightforward.
1866 * If successful, this will transfer the fs_info into the super block,
1867 * and fc->s_fs_info will be NULL. However if there's an existing
1868 * super, we'll still have fc->s_fs_info populated. If we error
1869 * completely out it'll be cleaned up when we drop the fs_context,
1870 * otherwise it's tied to the lifetime of the super_block.
1872 sb
= sget_fc(fc
, btrfs_fc_test_super
, set_anon_super_fc
);
1878 set_device_specific_options(fs_info
);
1881 btrfs_close_devices(fs_devices
);
1882 if ((fc
->sb_flags
^ sb
->s_flags
) & SB_RDONLY
)
1885 snprintf(sb
->s_id
, sizeof(sb
->s_id
), "%pg", bdev
);
1886 shrinker_debugfs_rename(sb
->s_shrink
, "sb-btrfs:%s", sb
->s_id
);
1887 btrfs_sb(sb
)->bdev_holder
= &btrfs_fs_type
;
1888 ret
= btrfs_fill_super(sb
, fs_devices
, NULL
);
1892 deactivate_locked_super(sb
);
1896 btrfs_clear_oneshot_options(fs_info
);
1898 fc
->root
= dget(sb
->s_root
);
1902 btrfs_close_devices(fs_devices
);
1907 * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
1908 * with different ro/rw options") the following works:
1910 * (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
1911 * (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
1913 * which looks nice and innocent but is actually pretty intricate and deserves
1916 * On another filesystem a subvolume mount is close to something like:
1918 * (iii) # create rw superblock + initial mount
1919 * mount -t xfs /dev/sdb /opt/
1921 * # create ro bind mount
1922 * mount --bind -o ro /opt/foo /mnt/foo
1924 * # unmount initial mount
1927 * Of course, there's some special subvolume sauce and there's the fact that the
1928 * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
1929 * it's very close and will help us understand the issue.
1931 * The old mount API didn't cleanly distinguish between a mount being made ro
1932 * and a superblock being made ro. The only way to change the ro state of
1933 * either object was by passing ms_rdonly. If a new mount was created via
1936 * mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
1938 * the MS_RDONLY flag being specified had two effects:
1940 * (1) MNT_READONLY was raised -> the resulting mount got
1941 * @mnt->mnt_flags |= MNT_READONLY raised.
1943 * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
1944 * made the superblock ro. Note, how SB_RDONLY has the same value as
1945 * ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
1947 * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
1948 * subtree mounted ro.
1950 * But consider the effect on the old mount API on btrfs subvolume mounting
1951 * which combines the distinct step in (iii) into a single step.
1953 * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
1954 * is issued the superblock is ro and thus even if the mount created for (ii) is
1955 * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
1956 * to rw for (ii) which it did using an internal remount call.
1958 * IOW, subvolume mounting was inherently complicated due to the ambiguity of
1959 * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
1960 * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
1961 * passed by mount(8) to mount(2).
1963 * Enter the new mount API. The new mount API disambiguates making a mount ro
1964 * and making a superblock ro.
1966 * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
1967 * fsmount() or mount_setattr() this is a pure VFS level change for a
1968 * specific mount or mount tree that is never seen by the filesystem itself.
1970 * (4) To turn a superblock ro the "ro" flag must be used with
1971 * fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
1974 * This disambiguation has rather positive consequences. Mounting a subvolume
1975 * ro will not also turn the superblock ro. Only the mount for the subvolume
1978 * So, if the superblock creation request comes from the new mount API the
1979 * caller must have explicitly done:
1981 * fsconfig(FSCONFIG_SET_FLAG, "ro")
1982 * fsmount/mount_setattr(MOUNT_ATTR_RDONLY)
1984 * IOW, at some point the caller must have explicitly turned the whole
1985 * superblock ro and we shouldn't just undo it like we did for the old mount
1986 * API. In any case, it lets us avoid the hack in the new mount API.
1988 * Consequently, the remounting hack must only be used for requests originating
1989 * from the old mount API and should be marked for full deprecation so it can be
1990 * turned off in a couple of years.
1992 * The new mount API has no reason to support this hack.
1994 static struct vfsmount
*btrfs_reconfigure_for_mount(struct fs_context
*fc
)
1996 struct vfsmount
*mnt
;
1998 const bool ro2rw
= !(fc
->sb_flags
& SB_RDONLY
);
2001 * We got an EBUSY because our SB_RDONLY flag didn't match the existing
2002 * super block, so invert our setting here and retry the mount so we
2003 * can get our vfsmount.
2006 fc
->sb_flags
|= SB_RDONLY
;
2008 fc
->sb_flags
&= ~SB_RDONLY
;
2014 if (!fc
->oldapi
|| !ro2rw
)
2017 /* We need to convert to rw, call reconfigure. */
2018 fc
->sb_flags
&= ~SB_RDONLY
;
2019 down_write(&mnt
->mnt_sb
->s_umount
);
2020 ret
= btrfs_reconfigure(fc
);
2021 up_write(&mnt
->mnt_sb
->s_umount
);
2024 return ERR_PTR(ret
);
2029 static int btrfs_get_tree_subvol(struct fs_context
*fc
)
2031 struct btrfs_fs_info
*fs_info
= NULL
;
2032 struct btrfs_fs_context
*ctx
= fc
->fs_private
;
2033 struct fs_context
*dup_fc
;
2034 struct dentry
*dentry
;
2035 struct vfsmount
*mnt
;
2038 * Setup a dummy root and fs_info for test/set super. This is because
2039 * we don't actually fill this stuff out until open_ctree, but we need
2040 * then open_ctree will properly initialize the file system specific
2041 * settings later. btrfs_init_fs_info initializes the static elements
2042 * of the fs_info (locks and such) to make cleanup easier if we find a
2043 * superblock with our given fs_devices later on at sget() time.
2045 fs_info
= kvzalloc(sizeof(struct btrfs_fs_info
), GFP_KERNEL
);
2049 fs_info
->super_copy
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
2050 fs_info
->super_for_commit
= kzalloc(BTRFS_SUPER_INFO_SIZE
, GFP_KERNEL
);
2051 if (!fs_info
->super_copy
|| !fs_info
->super_for_commit
) {
2052 btrfs_free_fs_info(fs_info
);
2055 btrfs_init_fs_info(fs_info
);
2057 dup_fc
= vfs_dup_fs_context(fc
);
2058 if (IS_ERR(dup_fc
)) {
2059 btrfs_free_fs_info(fs_info
);
2060 return PTR_ERR(dup_fc
);
2064 * When we do the sget_fc this gets transferred to the sb, so we only
2065 * need to set it on the dup_fc as this is what creates the super block.
2067 dup_fc
->s_fs_info
= fs_info
;
2070 * We'll do the security settings in our btrfs_get_tree_super() mount
2071 * loop, they were duplicated into dup_fc, we can drop the originals
2074 security_free_mnt_opts(&fc
->security
);
2075 fc
->security
= NULL
;
2077 mnt
= fc_mount(dup_fc
);
2078 if (PTR_ERR_OR_ZERO(mnt
) == -EBUSY
)
2079 mnt
= btrfs_reconfigure_for_mount(dup_fc
);
2080 put_fs_context(dup_fc
);
2082 return PTR_ERR(mnt
);
2085 * This free's ->subvol_name, because if it isn't set we have to
2086 * allocate a buffer to hold the subvol_name, so we just drop our
2087 * reference to it here.
2089 dentry
= mount_subvol(ctx
->subvol_name
, ctx
->subvol_objectid
, mnt
);
2090 ctx
->subvol_name
= NULL
;
2092 return PTR_ERR(dentry
);
2098 static int btrfs_get_tree(struct fs_context
*fc
)
2101 * Since we use mount_subtree to mount the default/specified subvol, we
2102 * have to do mounts in two steps.
2104 * First pass through we call btrfs_get_tree_subvol(), this is just a
2105 * wrapper around fc_mount() to call back into here again, and this time
2106 * we'll call btrfs_get_tree_super(). This will do the open_ctree() and
2107 * everything to open the devices and file system. Then we return back
2108 * with a fully constructed vfsmount in btrfs_get_tree_subvol(), and
2109 * from there we can do our mount_subvol() call, which will lookup
2110 * whichever subvol we're mounting and setup this fc with the
2111 * appropriate dentry for the subvol.
2114 return btrfs_get_tree_super(fc
);
2115 return btrfs_get_tree_subvol(fc
);
2118 static void btrfs_kill_super(struct super_block
*sb
)
2120 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2121 kill_anon_super(sb
);
2122 btrfs_free_fs_info(fs_info
);
2125 static void btrfs_free_fs_context(struct fs_context
*fc
)
2127 struct btrfs_fs_context
*ctx
= fc
->fs_private
;
2128 struct btrfs_fs_info
*fs_info
= fc
->s_fs_info
;
2131 btrfs_free_fs_info(fs_info
);
2133 if (ctx
&& refcount_dec_and_test(&ctx
->refs
)) {
2134 kfree(ctx
->subvol_name
);
2139 static int btrfs_dup_fs_context(struct fs_context
*fc
, struct fs_context
*src_fc
)
2141 struct btrfs_fs_context
*ctx
= src_fc
->fs_private
;
2144 * Give a ref to our ctx to this dup, as we want to keep it around for
2145 * our original fc so we can have the subvolume name or objectid.
2147 * We unset ->source in the original fc because the dup needs it for
2148 * mounting, and then once we free the dup it'll free ->source, so we
2149 * need to make sure we're only pointing to it in one fc.
2151 refcount_inc(&ctx
->refs
);
2152 fc
->fs_private
= ctx
;
2153 fc
->source
= src_fc
->source
;
2154 src_fc
->source
= NULL
;
2158 static const struct fs_context_operations btrfs_fs_context_ops
= {
2159 .parse_param
= btrfs_parse_param
,
2160 .reconfigure
= btrfs_reconfigure
,
2161 .get_tree
= btrfs_get_tree
,
2162 .dup
= btrfs_dup_fs_context
,
2163 .free
= btrfs_free_fs_context
,
2166 static int btrfs_init_fs_context(struct fs_context
*fc
)
2168 struct btrfs_fs_context
*ctx
;
2170 ctx
= kzalloc(sizeof(struct btrfs_fs_context
), GFP_KERNEL
);
2174 refcount_set(&ctx
->refs
, 1);
2175 fc
->fs_private
= ctx
;
2176 fc
->ops
= &btrfs_fs_context_ops
;
2178 if (fc
->purpose
== FS_CONTEXT_FOR_RECONFIGURE
) {
2179 btrfs_info_to_ctx(btrfs_sb(fc
->root
->d_sb
), ctx
);
2181 ctx
->thread_pool_size
=
2182 min_t(unsigned long, num_online_cpus() + 2, 8);
2183 ctx
->max_inline
= BTRFS_DEFAULT_MAX_INLINE
;
2184 ctx
->commit_interval
= BTRFS_DEFAULT_COMMIT_INTERVAL
;
2187 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
2188 fc
->sb_flags
|= SB_POSIXACL
;
2190 fc
->sb_flags
|= SB_I_VERSION
;
2195 static struct file_system_type btrfs_fs_type
= {
2196 .owner
= THIS_MODULE
,
2198 .init_fs_context
= btrfs_init_fs_context
,
2199 .parameters
= btrfs_fs_parameters
,
2200 .kill_sb
= btrfs_kill_super
,
2201 .fs_flags
= FS_REQUIRES_DEV
| FS_BINARY_MOUNTDATA
| FS_ALLOW_IDMAP
,
2204 MODULE_ALIAS_FS("btrfs");
2206 static int btrfs_control_open(struct inode
*inode
, struct file
*file
)
2209 * The control file's private_data is used to hold the
2210 * transaction when it is started and is used to keep
2211 * track of whether a transaction is already in progress.
2213 file
->private_data
= NULL
;
2218 * Used by /dev/btrfs-control for devices ioctls.
2220 static long btrfs_control_ioctl(struct file
*file
, unsigned int cmd
,
2223 struct btrfs_ioctl_vol_args
*vol
;
2224 struct btrfs_device
*device
= NULL
;
2228 if (!capable(CAP_SYS_ADMIN
))
2231 vol
= memdup_user((void __user
*)arg
, sizeof(*vol
));
2233 return PTR_ERR(vol
);
2234 ret
= btrfs_check_ioctl_vol_args_path(vol
);
2239 case BTRFS_IOC_SCAN_DEV
:
2240 mutex_lock(&uuid_mutex
);
2242 * Scanning outside of mount can return NULL which would turn
2243 * into 0 error code.
2245 device
= btrfs_scan_one_device(vol
->name
, BLK_OPEN_READ
, false);
2246 ret
= PTR_ERR_OR_ZERO(device
);
2247 mutex_unlock(&uuid_mutex
);
2249 case BTRFS_IOC_FORGET_DEV
:
2250 if (vol
->name
[0] != 0) {
2251 ret
= lookup_bdev(vol
->name
, &devt
);
2255 ret
= btrfs_forget_devices(devt
);
2257 case BTRFS_IOC_DEVICES_READY
:
2258 mutex_lock(&uuid_mutex
);
2260 * Scanning outside of mount can return NULL which would turn
2261 * into 0 error code.
2263 device
= btrfs_scan_one_device(vol
->name
, BLK_OPEN_READ
, false);
2264 if (IS_ERR_OR_NULL(device
)) {
2265 mutex_unlock(&uuid_mutex
);
2266 ret
= PTR_ERR(device
);
2269 ret
= !(device
->fs_devices
->num_devices
==
2270 device
->fs_devices
->total_devices
);
2271 mutex_unlock(&uuid_mutex
);
2273 case BTRFS_IOC_GET_SUPPORTED_FEATURES
:
2274 ret
= btrfs_ioctl_get_supported_features((void __user
*)arg
);
2283 static int btrfs_freeze(struct super_block
*sb
)
2285 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2287 set_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2289 * We don't need a barrier here, we'll wait for any transaction that
2290 * could be in progress on other threads (and do delayed iputs that
2291 * we want to avoid on a frozen filesystem), or do the commit
2294 return btrfs_commit_current_transaction(fs_info
->tree_root
);
2297 static int check_dev_super(struct btrfs_device
*dev
)
2299 struct btrfs_fs_info
*fs_info
= dev
->fs_info
;
2300 struct btrfs_super_block
*sb
;
2305 /* This should be called with fs still frozen. */
2306 ASSERT(test_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
));
2308 /* Missing dev, no need to check. */
2312 /* Only need to check the primary super block. */
2313 sb
= btrfs_read_dev_one_super(dev
->bdev
, 0, true);
2317 /* Verify the checksum. */
2318 csum_type
= btrfs_super_csum_type(sb
);
2319 if (csum_type
!= btrfs_super_csum_type(fs_info
->super_copy
)) {
2320 btrfs_err(fs_info
, "csum type changed, has %u expect %u",
2321 csum_type
, btrfs_super_csum_type(fs_info
->super_copy
));
2326 if (btrfs_check_super_csum(fs_info
, sb
)) {
2327 btrfs_err(fs_info
, "csum for on-disk super block no longer matches");
2332 /* Btrfs_validate_super() includes fsid check against super->fsid. */
2333 ret
= btrfs_validate_super(fs_info
, sb
, 0);
2337 last_trans
= btrfs_get_last_trans_committed(fs_info
);
2338 if (btrfs_super_generation(sb
) != last_trans
) {
2339 btrfs_err(fs_info
, "transid mismatch, has %llu expect %llu",
2340 btrfs_super_generation(sb
), last_trans
);
2345 btrfs_release_disk_super(sb
);
2349 static int btrfs_unfreeze(struct super_block
*sb
)
2351 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2352 struct btrfs_device
*device
;
2356 * Make sure the fs is not changed by accident (like hibernation then
2357 * modified by other OS).
2358 * If we found anything wrong, we mark the fs error immediately.
2360 * And since the fs is frozen, no one can modify the fs yet, thus
2361 * we don't need to hold device_list_mutex.
2363 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
, dev_list
) {
2364 ret
= check_dev_super(device
);
2366 btrfs_handle_fs_error(fs_info
, ret
,
2367 "super block on devid %llu got modified unexpectedly",
2372 clear_bit(BTRFS_FS_FROZEN
, &fs_info
->flags
);
2375 * We still return 0, to allow VFS layer to unfreeze the fs even the
2376 * above checks failed. Since the fs is either fine or read-only, we're
2377 * safe to continue, without causing further damage.
2382 static int btrfs_show_devname(struct seq_file
*m
, struct dentry
*root
)
2384 struct btrfs_fs_info
*fs_info
= btrfs_sb(root
->d_sb
);
2387 * There should be always a valid pointer in latest_dev, it may be stale
2388 * for a short moment in case it's being deleted but still valid until
2389 * the end of RCU grace period.
2392 seq_escape(m
, btrfs_dev_name(fs_info
->fs_devices
->latest_dev
), " \t\n\\");
2398 static long btrfs_nr_cached_objects(struct super_block
*sb
, struct shrink_control
*sc
)
2400 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2401 const s64 nr
= percpu_counter_sum_positive(&fs_info
->evictable_extent_maps
);
2403 trace_btrfs_extent_map_shrinker_count(fs_info
, nr
);
2406 * Only report the real number for DEBUG builds, as there are reports of
2407 * serious performance degradation caused by too frequent shrinks.
2409 if (IS_ENABLED(CONFIG_BTRFS_DEBUG
))
2414 static long btrfs_free_cached_objects(struct super_block
*sb
, struct shrink_control
*sc
)
2416 const long nr_to_scan
= min_t(unsigned long, LONG_MAX
, sc
->nr_to_scan
);
2417 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
2420 * We may be called from any task trying to allocate memory and we don't
2421 * want to slow it down with scanning and dropping extent maps. It would
2422 * also cause heavy lock contention if many tasks concurrently enter
2423 * here. Therefore only allow kswapd tasks to scan and drop extent maps.
2425 if (!current_is_kswapd())
2428 return btrfs_free_extent_maps(fs_info
, nr_to_scan
);
2431 static const struct super_operations btrfs_super_ops
= {
2432 .drop_inode
= btrfs_drop_inode
,
2433 .evict_inode
= btrfs_evict_inode
,
2434 .put_super
= btrfs_put_super
,
2435 .sync_fs
= btrfs_sync_fs
,
2436 .show_options
= btrfs_show_options
,
2437 .show_devname
= btrfs_show_devname
,
2438 .alloc_inode
= btrfs_alloc_inode
,
2439 .destroy_inode
= btrfs_destroy_inode
,
2440 .free_inode
= btrfs_free_inode
,
2441 .statfs
= btrfs_statfs
,
2442 .freeze_fs
= btrfs_freeze
,
2443 .unfreeze_fs
= btrfs_unfreeze
,
2444 .nr_cached_objects
= btrfs_nr_cached_objects
,
2445 .free_cached_objects
= btrfs_free_cached_objects
,
2448 static const struct file_operations btrfs_ctl_fops
= {
2449 .open
= btrfs_control_open
,
2450 .unlocked_ioctl
= btrfs_control_ioctl
,
2451 .compat_ioctl
= compat_ptr_ioctl
,
2452 .owner
= THIS_MODULE
,
2453 .llseek
= noop_llseek
,
2456 static struct miscdevice btrfs_misc
= {
2457 .minor
= BTRFS_MINOR
,
2458 .name
= "btrfs-control",
2459 .fops
= &btrfs_ctl_fops
2462 MODULE_ALIAS_MISCDEV(BTRFS_MINOR
);
2463 MODULE_ALIAS("devname:btrfs-control");
2465 static int __init
btrfs_interface_init(void)
2467 return misc_register(&btrfs_misc
);
2470 static __cold
void btrfs_interface_exit(void)
2472 misc_deregister(&btrfs_misc
);
2475 static int __init
btrfs_print_mod_info(void)
2477 static const char options
[] = ""
2478 #ifdef CONFIG_BTRFS_DEBUG
2481 #ifdef CONFIG_BTRFS_ASSERT
2484 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2487 #ifdef CONFIG_BLK_DEV_ZONED
2492 #ifdef CONFIG_FS_VERITY
2498 pr_info("Btrfs loaded%s\n", options
);
2502 static int register_btrfs(void)
2504 return register_filesystem(&btrfs_fs_type
);
2507 static void unregister_btrfs(void)
2509 unregister_filesystem(&btrfs_fs_type
);
2512 /* Helper structure for long init/exit functions. */
2513 struct init_sequence
{
2514 int (*init_func
)(void);
2515 /* Can be NULL if the init_func doesn't need cleanup. */
2516 void (*exit_func
)(void);
2519 static const struct init_sequence mod_init_seq
[] = {
2521 .init_func
= btrfs_props_init
,
2524 .init_func
= btrfs_init_sysfs
,
2525 .exit_func
= btrfs_exit_sysfs
,
2527 .init_func
= btrfs_init_compress
,
2528 .exit_func
= btrfs_exit_compress
,
2530 .init_func
= btrfs_init_cachep
,
2531 .exit_func
= btrfs_destroy_cachep
,
2533 .init_func
= btrfs_init_dio
,
2534 .exit_func
= btrfs_destroy_dio
,
2536 .init_func
= btrfs_transaction_init
,
2537 .exit_func
= btrfs_transaction_exit
,
2539 .init_func
= btrfs_ctree_init
,
2540 .exit_func
= btrfs_ctree_exit
,
2542 .init_func
= btrfs_free_space_init
,
2543 .exit_func
= btrfs_free_space_exit
,
2545 .init_func
= extent_state_init_cachep
,
2546 .exit_func
= extent_state_free_cachep
,
2548 .init_func
= extent_buffer_init_cachep
,
2549 .exit_func
= extent_buffer_free_cachep
,
2551 .init_func
= btrfs_bioset_init
,
2552 .exit_func
= btrfs_bioset_exit
,
2554 .init_func
= extent_map_init
,
2555 .exit_func
= extent_map_exit
,
2557 .init_func
= ordered_data_init
,
2558 .exit_func
= ordered_data_exit
,
2560 .init_func
= btrfs_delayed_inode_init
,
2561 .exit_func
= btrfs_delayed_inode_exit
,
2563 .init_func
= btrfs_auto_defrag_init
,
2564 .exit_func
= btrfs_auto_defrag_exit
,
2566 .init_func
= btrfs_delayed_ref_init
,
2567 .exit_func
= btrfs_delayed_ref_exit
,
2569 .init_func
= btrfs_prelim_ref_init
,
2570 .exit_func
= btrfs_prelim_ref_exit
,
2572 .init_func
= btrfs_interface_init
,
2573 .exit_func
= btrfs_interface_exit
,
2575 .init_func
= btrfs_print_mod_info
,
2578 .init_func
= btrfs_run_sanity_tests
,
2581 .init_func
= register_btrfs
,
2582 .exit_func
= unregister_btrfs
,
2586 static bool mod_init_result
[ARRAY_SIZE(mod_init_seq
)];
2588 static __always_inline
void btrfs_exit_btrfs_fs(void)
2592 for (i
= ARRAY_SIZE(mod_init_seq
) - 1; i
>= 0; i
--) {
2593 if (!mod_init_result
[i
])
2595 if (mod_init_seq
[i
].exit_func
)
2596 mod_init_seq
[i
].exit_func();
2597 mod_init_result
[i
] = false;
2601 static void __exit
exit_btrfs_fs(void)
2603 btrfs_exit_btrfs_fs();
2604 btrfs_cleanup_fs_uuids();
2607 static int __init
init_btrfs_fs(void)
2612 for (i
= 0; i
< ARRAY_SIZE(mod_init_seq
); i
++) {
2613 ASSERT(!mod_init_result
[i
]);
2614 ret
= mod_init_seq
[i
].init_func();
2616 btrfs_exit_btrfs_fs();
2619 mod_init_result
[i
] = true;
2624 late_initcall(init_btrfs_fs
);
2625 module_exit(exit_btrfs_fs
)
2627 MODULE_DESCRIPTION("B-Tree File System (BTRFS)");
2628 MODULE_LICENSE("GPL");
2629 MODULE_SOFTDEP("pre: crc32c");
2630 MODULE_SOFTDEP("pre: xxhash64");
2631 MODULE_SOFTDEP("pre: sha256");
2632 MODULE_SOFTDEP("pre: blake2b-256");