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"
47 #include "tests/btrfs-tests.h"
48 #include "block-group.h"
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/btrfs.h>
55 static const struct super_operations btrfs_super_ops;
58 * Types for mounting the default subvolume and a subvolume explicitly
59 * requested by subvol=/path. That way the callchain is straightforward and we
60 * don't have to play tricks with the mount options and recursive calls to
63 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
65 static struct file_system_type btrfs_fs_type;
66 static struct file_system_type btrfs_root_fs_type;
68 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
70 const char * __attribute_const__ btrfs_decode_error(int errno)
72 char *errstr = "unknown";
75 case -ENOENT: /* -2 */
76 errstr = "No such entry";
79 errstr = "IO failure";
81 case -ENOMEM: /* -12*/
82 errstr = "Out of memory";
84 case -EEXIST: /* -17 */
85 errstr = "Object already exists";
87 case -ENOSPC: /* -28 */
88 errstr = "No space left";
90 case -EROFS: /* -30 */
91 errstr = "Readonly filesystem";
93 case -EOPNOTSUPP: /* -95 */
94 errstr = "Operation not supported";
96 case -EUCLEAN: /* -117 */
97 errstr = "Filesystem corrupted";
99 case -EDQUOT: /* -122 */
100 errstr = "Quota exceeded";
108 * __btrfs_handle_fs_error decodes expected errors from the caller and
109 * invokes the appropriate error response.
112 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
113 unsigned int line, int errno, const char *fmt, ...)
115 struct super_block *sb = fs_info->sb;
121 * Special case: if the error is EROFS, and we're already
122 * under SB_RDONLY, then it is safe here.
124 if (errno == -EROFS && sb_rdonly(sb))
128 errstr = btrfs_decode_error(errno);
130 struct va_format vaf;
137 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
138 sb->s_id, function, line, errno, errstr, &vaf);
141 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
142 sb->s_id, function, line, errno, errstr);
147 * Today we only save the error info to memory. Long term we'll
148 * also send it down to the disk
150 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
152 /* Don't go through full error handling during mount */
153 if (!(sb->s_flags & SB_BORN))
159 btrfs_discard_stop(fs_info);
161 /* btrfs handle error by forcing the filesystem readonly */
162 sb->s_flags |= SB_RDONLY;
163 btrfs_info(fs_info, "forced readonly");
165 * Note that a running device replace operation is not canceled here
166 * although there is no way to update the progress. It would add the
167 * risk of a deadlock, therefore the canceling is omitted. The only
168 * penalty is that some I/O remains active until the procedure
169 * completes. The next time when the filesystem is mounted writable
170 * again, the device replace operation continues.
175 static const char * const logtypes[] = {
188 * Use one ratelimit state per log level so that a flood of less important
189 * messages doesn't cause more important ones to be dropped.
191 static struct ratelimit_state printk_limits[] = {
192 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
193 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
194 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
195 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
196 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
197 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
198 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
199 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
202 void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
204 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
205 struct va_format vaf;
208 const char *type = logtypes[4];
209 struct ratelimit_state *ratelimit = &printk_limits[4];
213 while ((kern_level = printk_get_level(fmt)) != 0) {
214 size_t size = printk_skip_level(fmt) - fmt;
216 if (kern_level >= '0' && kern_level <= '7') {
217 memcpy(lvl, fmt, size);
219 type = logtypes[kern_level - '0'];
220 ratelimit = &printk_limits[kern_level - '0'];
228 if (__ratelimit(ratelimit))
229 printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
230 fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
237 * We only mark the transaction aborted and then set the file system read-only.
238 * This will prevent new transactions from starting or trying to join this
241 * This means that error recovery at the call site is limited to freeing
242 * any local memory allocations and passing the error code up without
243 * further cleanup. The transaction should complete as it normally would
244 * in the call path but will return -EIO.
246 * We'll complete the cleanup in btrfs_end_transaction and
247 * btrfs_commit_transaction.
250 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
251 const char *function,
252 unsigned int line, int errno)
254 struct btrfs_fs_info *fs_info = trans->fs_info;
256 WRITE_ONCE(trans->aborted, errno);
257 /* Nothing used. The other threads that have joined this
258 * transaction may be able to continue. */
259 if (!trans->dirty && list_empty(&trans->new_bgs)) {
262 errstr = btrfs_decode_error(errno);
264 "%s:%d: Aborting unused transaction(%s).",
265 function, line, errstr);
268 WRITE_ONCE(trans->transaction->aborted, errno);
269 /* Wake up anybody who may be waiting on this transaction */
270 wake_up(&fs_info->transaction_wait);
271 wake_up(&fs_info->transaction_blocked_wait);
272 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
275 * __btrfs_panic decodes unexpected, fatal errors from the caller,
276 * issues an alert, and either panics or BUGs, depending on mount options.
279 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
280 unsigned int line, int errno, const char *fmt, ...)
282 char *s_id = "<unknown>";
284 struct va_format vaf = { .fmt = fmt };
288 s_id = fs_info->sb->s_id;
293 errstr = btrfs_decode_error(errno);
294 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
295 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
296 s_id, function, line, &vaf, errno, errstr);
298 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
299 function, line, &vaf, errno, errstr);
301 /* Caller calls BUG() */
304 static void btrfs_put_super(struct super_block *sb)
306 close_ctree(btrfs_sb(sb));
315 Opt_compress_force_type,
320 Opt_flushoncommit, Opt_noflushoncommit,
321 Opt_inode_cache, Opt_noinode_cache,
323 Opt_barrier, Opt_nobarrier,
324 Opt_datacow, Opt_nodatacow,
325 Opt_datasum, Opt_nodatasum,
326 Opt_defrag, Opt_nodefrag,
327 Opt_discard, Opt_nodiscard,
331 Opt_rescan_uuid_tree,
333 Opt_space_cache, Opt_no_space_cache,
334 Opt_space_cache_version,
336 Opt_ssd_spread, Opt_nossd_spread,
341 Opt_treelog, Opt_notreelog,
342 Opt_user_subvol_rm_allowed,
349 /* Deprecated options */
354 /* Debugging options */
356 Opt_check_integrity_including_extent_data,
357 Opt_check_integrity_print_mask,
358 Opt_enospc_debug, Opt_noenospc_debug,
359 #ifdef CONFIG_BTRFS_DEBUG
360 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
362 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
368 static const match_table_t tokens = {
370 {Opt_noacl, "noacl"},
371 {Opt_clear_cache, "clear_cache"},
372 {Opt_commit_interval, "commit=%u"},
373 {Opt_compress, "compress"},
374 {Opt_compress_type, "compress=%s"},
375 {Opt_compress_force, "compress-force"},
376 {Opt_compress_force_type, "compress-force=%s"},
377 {Opt_degraded, "degraded"},
378 {Opt_device, "device=%s"},
379 {Opt_fatal_errors, "fatal_errors=%s"},
380 {Opt_flushoncommit, "flushoncommit"},
381 {Opt_noflushoncommit, "noflushoncommit"},
382 {Opt_inode_cache, "inode_cache"},
383 {Opt_noinode_cache, "noinode_cache"},
384 {Opt_max_inline, "max_inline=%s"},
385 {Opt_barrier, "barrier"},
386 {Opt_nobarrier, "nobarrier"},
387 {Opt_datacow, "datacow"},
388 {Opt_nodatacow, "nodatacow"},
389 {Opt_datasum, "datasum"},
390 {Opt_nodatasum, "nodatasum"},
391 {Opt_defrag, "autodefrag"},
392 {Opt_nodefrag, "noautodefrag"},
393 {Opt_discard, "discard"},
394 {Opt_discard_mode, "discard=%s"},
395 {Opt_nodiscard, "nodiscard"},
396 {Opt_norecovery, "norecovery"},
397 {Opt_ratio, "metadata_ratio=%u"},
398 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
399 {Opt_skip_balance, "skip_balance"},
400 {Opt_space_cache, "space_cache"},
401 {Opt_no_space_cache, "nospace_cache"},
402 {Opt_space_cache_version, "space_cache=%s"},
404 {Opt_nossd, "nossd"},
405 {Opt_ssd_spread, "ssd_spread"},
406 {Opt_nossd_spread, "nossd_spread"},
407 {Opt_subvol, "subvol=%s"},
408 {Opt_subvol_empty, "subvol="},
409 {Opt_subvolid, "subvolid=%s"},
410 {Opt_thread_pool, "thread_pool=%u"},
411 {Opt_treelog, "treelog"},
412 {Opt_notreelog, "notreelog"},
413 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
416 {Opt_rescue, "rescue=%s"},
417 /* Deprecated, with alias rescue=nologreplay */
418 {Opt_nologreplay, "nologreplay"},
419 /* Deprecated, with alias rescue=usebackuproot */
420 {Opt_usebackuproot, "usebackuproot"},
422 /* Deprecated options */
423 {Opt_alloc_start, "alloc_start=%s"},
424 {Opt_recovery, "recovery"},
425 {Opt_subvolrootid, "subvolrootid=%d"},
427 /* Debugging options */
428 {Opt_check_integrity, "check_int"},
429 {Opt_check_integrity_including_extent_data, "check_int_data"},
430 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
431 {Opt_enospc_debug, "enospc_debug"},
432 {Opt_noenospc_debug, "noenospc_debug"},
433 #ifdef CONFIG_BTRFS_DEBUG
434 {Opt_fragment_data, "fragment=data"},
435 {Opt_fragment_metadata, "fragment=metadata"},
436 {Opt_fragment_all, "fragment=all"},
438 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
439 {Opt_ref_verify, "ref_verify"},
444 static const match_table_t rescue_tokens = {
445 {Opt_usebackuproot, "usebackuproot"},
446 {Opt_nologreplay, "nologreplay"},
450 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
455 substring_t args[MAX_OPT_ARGS];
458 opts = kstrdup(options, GFP_KERNEL);
463 while ((p = strsep(&opts, ":")) != NULL) {
468 token = match_token(p, rescue_tokens, args);
470 case Opt_usebackuproot:
472 "trying to use backup root at mount time");
473 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
475 case Opt_nologreplay:
476 btrfs_set_and_info(info, NOLOGREPLAY,
477 "disabling log replay at mount time");
480 btrfs_info(info, "unrecognized rescue option '%s'", p);
494 * Regular mount options parser. Everything that is needed only when
495 * reading in a new superblock is parsed here.
496 * XXX JDM: This needs to be cleaned up for remount.
498 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
499 unsigned long new_flags)
501 substring_t args[MAX_OPT_ARGS];
507 bool compress_force = false;
508 enum btrfs_compression_type saved_compress_type;
509 bool saved_compress_force;
512 cache_gen = btrfs_super_cache_generation(info->super_copy);
513 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
514 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
516 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
519 * Even the options are empty, we still need to do extra check
525 while ((p = strsep(&options, ",")) != NULL) {
530 token = match_token(p, tokens, args);
533 btrfs_info(info, "allowing degraded mounts");
534 btrfs_set_opt(info->mount_opt, DEGRADED);
537 case Opt_subvol_empty:
539 case Opt_subvolrootid:
542 * These are parsed by btrfs_parse_subvol_options or
543 * btrfs_parse_device_options and can be ignored here.
547 btrfs_set_and_info(info, NODATASUM,
548 "setting nodatasum");
551 if (btrfs_test_opt(info, NODATASUM)) {
552 if (btrfs_test_opt(info, NODATACOW))
554 "setting datasum, datacow enabled");
556 btrfs_info(info, "setting datasum");
558 btrfs_clear_opt(info->mount_opt, NODATACOW);
559 btrfs_clear_opt(info->mount_opt, NODATASUM);
562 if (!btrfs_test_opt(info, NODATACOW)) {
563 if (!btrfs_test_opt(info, COMPRESS) ||
564 !btrfs_test_opt(info, FORCE_COMPRESS)) {
566 "setting nodatacow, compression disabled");
568 btrfs_info(info, "setting nodatacow");
571 btrfs_clear_opt(info->mount_opt, COMPRESS);
572 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
573 btrfs_set_opt(info->mount_opt, NODATACOW);
574 btrfs_set_opt(info->mount_opt, NODATASUM);
577 btrfs_clear_and_info(info, NODATACOW,
580 case Opt_compress_force:
581 case Opt_compress_force_type:
582 compress_force = true;
585 case Opt_compress_type:
586 saved_compress_type = btrfs_test_opt(info,
588 info->compress_type : BTRFS_COMPRESS_NONE;
589 saved_compress_force =
590 btrfs_test_opt(info, FORCE_COMPRESS);
591 if (token == Opt_compress ||
592 token == Opt_compress_force ||
593 strncmp(args[0].from, "zlib", 4) == 0) {
594 compress_type = "zlib";
596 info->compress_type = BTRFS_COMPRESS_ZLIB;
597 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
599 * args[0] contains uninitialized data since
600 * for these tokens we don't expect any
603 if (token != Opt_compress &&
604 token != Opt_compress_force)
605 info->compress_level =
606 btrfs_compress_str2level(
609 btrfs_set_opt(info->mount_opt, COMPRESS);
610 btrfs_clear_opt(info->mount_opt, NODATACOW);
611 btrfs_clear_opt(info->mount_opt, NODATASUM);
613 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
614 compress_type = "lzo";
615 info->compress_type = BTRFS_COMPRESS_LZO;
616 btrfs_set_opt(info->mount_opt, COMPRESS);
617 btrfs_clear_opt(info->mount_opt, NODATACOW);
618 btrfs_clear_opt(info->mount_opt, NODATASUM);
619 btrfs_set_fs_incompat(info, COMPRESS_LZO);
621 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
622 compress_type = "zstd";
623 info->compress_type = BTRFS_COMPRESS_ZSTD;
624 info->compress_level =
625 btrfs_compress_str2level(
628 btrfs_set_opt(info->mount_opt, COMPRESS);
629 btrfs_clear_opt(info->mount_opt, NODATACOW);
630 btrfs_clear_opt(info->mount_opt, NODATASUM);
631 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
633 } else if (strncmp(args[0].from, "no", 2) == 0) {
634 compress_type = "no";
635 btrfs_clear_opt(info->mount_opt, COMPRESS);
636 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
637 compress_force = false;
644 if (compress_force) {
645 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
648 * If we remount from compress-force=xxx to
649 * compress=xxx, we need clear FORCE_COMPRESS
650 * flag, otherwise, there is no way for users
651 * to disable forcible compression separately.
653 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
655 if ((btrfs_test_opt(info, COMPRESS) &&
656 (info->compress_type != saved_compress_type ||
657 compress_force != saved_compress_force)) ||
658 (!btrfs_test_opt(info, COMPRESS) &&
660 btrfs_info(info, "%s %s compression, level %d",
661 (compress_force) ? "force" : "use",
662 compress_type, info->compress_level);
664 compress_force = false;
667 btrfs_set_and_info(info, SSD,
668 "enabling ssd optimizations");
669 btrfs_clear_opt(info->mount_opt, NOSSD);
672 btrfs_set_and_info(info, SSD,
673 "enabling ssd optimizations");
674 btrfs_set_and_info(info, SSD_SPREAD,
675 "using spread ssd allocation scheme");
676 btrfs_clear_opt(info->mount_opt, NOSSD);
679 btrfs_set_opt(info->mount_opt, NOSSD);
680 btrfs_clear_and_info(info, SSD,
681 "not using ssd optimizations");
683 case Opt_nossd_spread:
684 btrfs_clear_and_info(info, SSD_SPREAD,
685 "not using spread ssd allocation scheme");
688 btrfs_clear_and_info(info, NOBARRIER,
689 "turning on barriers");
692 btrfs_set_and_info(info, NOBARRIER,
693 "turning off barriers");
695 case Opt_thread_pool:
696 ret = match_int(&args[0], &intarg);
699 } else if (intarg == 0) {
703 info->thread_pool_size = intarg;
706 num = match_strdup(&args[0]);
708 info->max_inline = memparse(num, NULL);
711 if (info->max_inline) {
712 info->max_inline = min_t(u64,
716 btrfs_info(info, "max_inline at %llu",
723 case Opt_alloc_start:
725 "option alloc_start is obsolete, ignored");
728 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
729 info->sb->s_flags |= SB_POSIXACL;
732 btrfs_err(info, "support for ACL not compiled in!");
737 info->sb->s_flags &= ~SB_POSIXACL;
740 btrfs_set_and_info(info, NOTREELOG,
741 "disabling tree log");
744 btrfs_clear_and_info(info, NOTREELOG,
745 "enabling tree log");
748 case Opt_nologreplay:
750 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
751 btrfs_set_and_info(info, NOLOGREPLAY,
752 "disabling log replay at mount time");
754 case Opt_flushoncommit:
755 btrfs_set_and_info(info, FLUSHONCOMMIT,
756 "turning on flush-on-commit");
758 case Opt_noflushoncommit:
759 btrfs_clear_and_info(info, FLUSHONCOMMIT,
760 "turning off flush-on-commit");
763 ret = match_int(&args[0], &intarg);
766 info->metadata_ratio = intarg;
767 btrfs_info(info, "metadata ratio %u",
768 info->metadata_ratio);
771 case Opt_discard_mode:
772 if (token == Opt_discard ||
773 strcmp(args[0].from, "sync") == 0) {
774 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
775 btrfs_set_and_info(info, DISCARD_SYNC,
776 "turning on sync discard");
777 } else if (strcmp(args[0].from, "async") == 0) {
778 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
779 btrfs_set_and_info(info, DISCARD_ASYNC,
780 "turning on async discard");
787 btrfs_clear_and_info(info, DISCARD_SYNC,
788 "turning off discard");
789 btrfs_clear_and_info(info, DISCARD_ASYNC,
790 "turning off async discard");
792 case Opt_space_cache:
793 case Opt_space_cache_version:
794 if (token == Opt_space_cache ||
795 strcmp(args[0].from, "v1") == 0) {
796 btrfs_clear_opt(info->mount_opt,
798 btrfs_set_and_info(info, SPACE_CACHE,
799 "enabling disk space caching");
800 } else if (strcmp(args[0].from, "v2") == 0) {
801 btrfs_clear_opt(info->mount_opt,
803 btrfs_set_and_info(info, FREE_SPACE_TREE,
804 "enabling free space tree");
810 case Opt_rescan_uuid_tree:
811 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
813 case Opt_no_space_cache:
814 if (btrfs_test_opt(info, SPACE_CACHE)) {
815 btrfs_clear_and_info(info, SPACE_CACHE,
816 "disabling disk space caching");
818 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
819 btrfs_clear_and_info(info, FREE_SPACE_TREE,
820 "disabling free space tree");
823 case Opt_inode_cache:
825 "the 'inode_cache' option is deprecated and will have no effect from 5.11");
826 btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
827 "enabling inode map caching");
829 case Opt_noinode_cache:
830 btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
831 "disabling inode map caching");
833 case Opt_clear_cache:
834 btrfs_set_and_info(info, CLEAR_CACHE,
835 "force clearing of disk cache");
837 case Opt_user_subvol_rm_allowed:
838 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
840 case Opt_enospc_debug:
841 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
843 case Opt_noenospc_debug:
844 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
847 btrfs_set_and_info(info, AUTO_DEFRAG,
848 "enabling auto defrag");
851 btrfs_clear_and_info(info, AUTO_DEFRAG,
852 "disabling auto defrag");
855 case Opt_usebackuproot:
857 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
858 token == Opt_recovery ? "recovery" :
861 "trying to use backup root at mount time");
862 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
864 case Opt_skip_balance:
865 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
867 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
868 case Opt_check_integrity_including_extent_data:
870 "enabling check integrity including extent data");
871 btrfs_set_opt(info->mount_opt,
872 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
873 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
875 case Opt_check_integrity:
876 btrfs_info(info, "enabling check integrity");
877 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
879 case Opt_check_integrity_print_mask:
880 ret = match_int(&args[0], &intarg);
883 info->check_integrity_print_mask = intarg;
884 btrfs_info(info, "check_integrity_print_mask 0x%x",
885 info->check_integrity_print_mask);
888 case Opt_check_integrity_including_extent_data:
889 case Opt_check_integrity:
890 case Opt_check_integrity_print_mask:
892 "support for check_integrity* not compiled in!");
896 case Opt_fatal_errors:
897 if (strcmp(args[0].from, "panic") == 0)
898 btrfs_set_opt(info->mount_opt,
899 PANIC_ON_FATAL_ERROR);
900 else if (strcmp(args[0].from, "bug") == 0)
901 btrfs_clear_opt(info->mount_opt,
902 PANIC_ON_FATAL_ERROR);
908 case Opt_commit_interval:
910 ret = match_int(&args[0], &intarg);
915 "using default commit interval %us",
916 BTRFS_DEFAULT_COMMIT_INTERVAL);
917 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
918 } else if (intarg > 300) {
919 btrfs_warn(info, "excessive commit interval %d",
922 info->commit_interval = intarg;
925 ret = parse_rescue_options(info, args[0].from);
929 #ifdef CONFIG_BTRFS_DEBUG
930 case Opt_fragment_all:
931 btrfs_info(info, "fragmenting all space");
932 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
933 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
935 case Opt_fragment_metadata:
936 btrfs_info(info, "fragmenting metadata");
937 btrfs_set_opt(info->mount_opt,
940 case Opt_fragment_data:
941 btrfs_info(info, "fragmenting data");
942 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
945 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
947 btrfs_info(info, "doing ref verification");
948 btrfs_set_opt(info->mount_opt, REF_VERIFY);
952 btrfs_err(info, "unrecognized mount option '%s'", p);
961 * Extra check for current option against current flag
963 if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
965 "nologreplay must be used with ro mount option");
969 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
970 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
971 !btrfs_test_opt(info, CLEAR_CACHE)) {
972 btrfs_err(info, "cannot disable free space tree");
976 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
977 btrfs_info(info, "disk space caching is enabled");
978 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
979 btrfs_info(info, "using free space tree");
984 * Parse mount options that are required early in the mount process.
986 * All other options will be parsed on much later in the mount process and
987 * only when we need to allocate a new super block.
989 static int btrfs_parse_device_options(const char *options, fmode_t flags,
992 substring_t args[MAX_OPT_ARGS];
993 char *device_name, *opts, *orig, *p;
994 struct btrfs_device *device = NULL;
997 lockdep_assert_held(&uuid_mutex);
1003 * strsep changes the string, duplicate it because btrfs_parse_options
1006 opts = kstrdup(options, GFP_KERNEL);
1011 while ((p = strsep(&opts, ",")) != NULL) {
1017 token = match_token(p, tokens, args);
1018 if (token == Opt_device) {
1019 device_name = match_strdup(&args[0]);
1024 device = btrfs_scan_one_device(device_name, flags,
1027 if (IS_ERR(device)) {
1028 error = PTR_ERR(device);
1040 * Parse mount options that are related to subvolume id
1042 * The value is later passed to mount_subvol()
1044 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
1045 u64 *subvol_objectid)
1047 substring_t args[MAX_OPT_ARGS];
1048 char *opts, *orig, *p;
1056 * strsep changes the string, duplicate it because
1057 * btrfs_parse_device_options gets called later
1059 opts = kstrdup(options, GFP_KERNEL);
1064 while ((p = strsep(&opts, ",")) != NULL) {
1069 token = match_token(p, tokens, args);
1072 kfree(*subvol_name);
1073 *subvol_name = match_strdup(&args[0]);
1074 if (!*subvol_name) {
1080 error = match_u64(&args[0], &subvolid);
1084 /* we want the original fs_tree */
1086 subvolid = BTRFS_FS_TREE_OBJECTID;
1088 *subvol_objectid = subvolid;
1090 case Opt_subvolrootid:
1091 pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
1103 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1104 u64 subvol_objectid)
1106 struct btrfs_root *root = fs_info->tree_root;
1107 struct btrfs_root *fs_root = NULL;
1108 struct btrfs_root_ref *root_ref;
1109 struct btrfs_inode_ref *inode_ref;
1110 struct btrfs_key key;
1111 struct btrfs_path *path = NULL;
1112 char *name = NULL, *ptr;
1117 path = btrfs_alloc_path();
1122 path->leave_spinning = 1;
1124 name = kmalloc(PATH_MAX, GFP_KERNEL);
1129 ptr = name + PATH_MAX - 1;
1133 * Walk up the subvolume trees in the tree of tree roots by root
1134 * backrefs until we hit the top-level subvolume.
1136 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1137 key.objectid = subvol_objectid;
1138 key.type = BTRFS_ROOT_BACKREF_KEY;
1139 key.offset = (u64)-1;
1141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1144 } else if (ret > 0) {
1145 ret = btrfs_previous_item(root, path, subvol_objectid,
1146 BTRFS_ROOT_BACKREF_KEY);
1149 } else if (ret > 0) {
1155 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1156 subvol_objectid = key.offset;
1158 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1159 struct btrfs_root_ref);
1160 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1163 ret = -ENAMETOOLONG;
1166 read_extent_buffer(path->nodes[0], ptr + 1,
1167 (unsigned long)(root_ref + 1), len);
1169 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1170 btrfs_release_path(path);
1172 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1173 if (IS_ERR(fs_root)) {
1174 ret = PTR_ERR(fs_root);
1180 * Walk up the filesystem tree by inode refs until we hit the
1183 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1184 key.objectid = dirid;
1185 key.type = BTRFS_INODE_REF_KEY;
1186 key.offset = (u64)-1;
1188 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1191 } else if (ret > 0) {
1192 ret = btrfs_previous_item(fs_root, path, dirid,
1193 BTRFS_INODE_REF_KEY);
1196 } else if (ret > 0) {
1202 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1205 inode_ref = btrfs_item_ptr(path->nodes[0],
1207 struct btrfs_inode_ref);
1208 len = btrfs_inode_ref_name_len(path->nodes[0],
1212 ret = -ENAMETOOLONG;
1215 read_extent_buffer(path->nodes[0], ptr + 1,
1216 (unsigned long)(inode_ref + 1), len);
1218 btrfs_release_path(path);
1220 btrfs_put_root(fs_root);
1224 btrfs_free_path(path);
1225 if (ptr == name + PATH_MAX - 1) {
1229 memmove(name, ptr, name + PATH_MAX - ptr);
1234 btrfs_put_root(fs_root);
1235 btrfs_free_path(path);
1237 return ERR_PTR(ret);
1240 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1242 struct btrfs_root *root = fs_info->tree_root;
1243 struct btrfs_dir_item *di;
1244 struct btrfs_path *path;
1245 struct btrfs_key location;
1248 path = btrfs_alloc_path();
1251 path->leave_spinning = 1;
1254 * Find the "default" dir item which points to the root item that we
1255 * will mount by default if we haven't been given a specific subvolume
1258 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1259 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1261 btrfs_free_path(path);
1266 * Ok the default dir item isn't there. This is weird since
1267 * it's always been there, but don't freak out, just try and
1268 * mount the top-level subvolume.
1270 btrfs_free_path(path);
1271 *objectid = BTRFS_FS_TREE_OBJECTID;
1275 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1276 btrfs_free_path(path);
1277 *objectid = location.objectid;
1281 static int btrfs_fill_super(struct super_block *sb,
1282 struct btrfs_fs_devices *fs_devices,
1285 struct inode *inode;
1286 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1289 sb->s_maxbytes = MAX_LFS_FILESIZE;
1290 sb->s_magic = BTRFS_SUPER_MAGIC;
1291 sb->s_op = &btrfs_super_ops;
1292 sb->s_d_op = &btrfs_dentry_operations;
1293 sb->s_export_op = &btrfs_export_ops;
1294 sb->s_xattr = btrfs_xattr_handlers;
1295 sb->s_time_gran = 1;
1296 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1297 sb->s_flags |= SB_POSIXACL;
1299 sb->s_flags |= SB_I_VERSION;
1300 sb->s_iflags |= SB_I_CGROUPWB;
1302 err = super_setup_bdi(sb);
1304 btrfs_err(fs_info, "super_setup_bdi failed");
1308 err = open_ctree(sb, fs_devices, (char *)data);
1310 btrfs_err(fs_info, "open_ctree failed");
1314 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1315 if (IS_ERR(inode)) {
1316 err = PTR_ERR(inode);
1320 sb->s_root = d_make_root(inode);
1326 cleancache_init_fs(sb);
1327 sb->s_flags |= SB_ACTIVE;
1331 close_ctree(fs_info);
1335 int btrfs_sync_fs(struct super_block *sb, int wait)
1337 struct btrfs_trans_handle *trans;
1338 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1339 struct btrfs_root *root = fs_info->tree_root;
1341 trace_btrfs_sync_fs(fs_info, wait);
1344 filemap_flush(fs_info->btree_inode->i_mapping);
1348 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1350 trans = btrfs_attach_transaction_barrier(root);
1351 if (IS_ERR(trans)) {
1352 /* no transaction, don't bother */
1353 if (PTR_ERR(trans) == -ENOENT) {
1355 * Exit unless we have some pending changes
1356 * that need to go through commit
1358 if (fs_info->pending_changes == 0)
1361 * A non-blocking test if the fs is frozen. We must not
1362 * start a new transaction here otherwise a deadlock
1363 * happens. The pending operations are delayed to the
1364 * next commit after thawing.
1366 if (sb_start_write_trylock(sb))
1370 trans = btrfs_start_transaction(root, 0);
1373 return PTR_ERR(trans);
1375 return btrfs_commit_transaction(trans);
1378 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1380 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1381 const char *compress_type;
1383 if (btrfs_test_opt(info, DEGRADED))
1384 seq_puts(seq, ",degraded");
1385 if (btrfs_test_opt(info, NODATASUM))
1386 seq_puts(seq, ",nodatasum");
1387 if (btrfs_test_opt(info, NODATACOW))
1388 seq_puts(seq, ",nodatacow");
1389 if (btrfs_test_opt(info, NOBARRIER))
1390 seq_puts(seq, ",nobarrier");
1391 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1392 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1393 if (info->thread_pool_size != min_t(unsigned long,
1394 num_online_cpus() + 2, 8))
1395 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1396 if (btrfs_test_opt(info, COMPRESS)) {
1397 compress_type = btrfs_compress_type2str(info->compress_type);
1398 if (btrfs_test_opt(info, FORCE_COMPRESS))
1399 seq_printf(seq, ",compress-force=%s", compress_type);
1401 seq_printf(seq, ",compress=%s", compress_type);
1402 if (info->compress_level)
1403 seq_printf(seq, ":%d", info->compress_level);
1405 if (btrfs_test_opt(info, NOSSD))
1406 seq_puts(seq, ",nossd");
1407 if (btrfs_test_opt(info, SSD_SPREAD))
1408 seq_puts(seq, ",ssd_spread");
1409 else if (btrfs_test_opt(info, SSD))
1410 seq_puts(seq, ",ssd");
1411 if (btrfs_test_opt(info, NOTREELOG))
1412 seq_puts(seq, ",notreelog");
1413 if (btrfs_test_opt(info, NOLOGREPLAY))
1414 seq_puts(seq, ",rescue=nologreplay");
1415 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1416 seq_puts(seq, ",flushoncommit");
1417 if (btrfs_test_opt(info, DISCARD_SYNC))
1418 seq_puts(seq, ",discard");
1419 if (btrfs_test_opt(info, DISCARD_ASYNC))
1420 seq_puts(seq, ",discard=async");
1421 if (!(info->sb->s_flags & SB_POSIXACL))
1422 seq_puts(seq, ",noacl");
1423 if (btrfs_test_opt(info, SPACE_CACHE))
1424 seq_puts(seq, ",space_cache");
1425 else if (btrfs_test_opt(info, FREE_SPACE_TREE))
1426 seq_puts(seq, ",space_cache=v2");
1428 seq_puts(seq, ",nospace_cache");
1429 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1430 seq_puts(seq, ",rescan_uuid_tree");
1431 if (btrfs_test_opt(info, CLEAR_CACHE))
1432 seq_puts(seq, ",clear_cache");
1433 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1434 seq_puts(seq, ",user_subvol_rm_allowed");
1435 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1436 seq_puts(seq, ",enospc_debug");
1437 if (btrfs_test_opt(info, AUTO_DEFRAG))
1438 seq_puts(seq, ",autodefrag");
1439 if (btrfs_test_opt(info, INODE_MAP_CACHE))
1440 seq_puts(seq, ",inode_cache");
1441 if (btrfs_test_opt(info, SKIP_BALANCE))
1442 seq_puts(seq, ",skip_balance");
1443 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1444 if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
1445 seq_puts(seq, ",check_int_data");
1446 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1447 seq_puts(seq, ",check_int");
1448 if (info->check_integrity_print_mask)
1449 seq_printf(seq, ",check_int_print_mask=%d",
1450 info->check_integrity_print_mask);
1452 if (info->metadata_ratio)
1453 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1454 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1455 seq_puts(seq, ",fatal_errors=panic");
1456 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1457 seq_printf(seq, ",commit=%u", info->commit_interval);
1458 #ifdef CONFIG_BTRFS_DEBUG
1459 if (btrfs_test_opt(info, FRAGMENT_DATA))
1460 seq_puts(seq, ",fragment=data");
1461 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1462 seq_puts(seq, ",fragment=metadata");
1464 if (btrfs_test_opt(info, REF_VERIFY))
1465 seq_puts(seq, ",ref_verify");
1466 seq_printf(seq, ",subvolid=%llu",
1467 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1468 seq_puts(seq, ",subvol=");
1469 seq_dentry(seq, dentry, " \t\n\\");
1473 static int btrfs_test_super(struct super_block *s, void *data)
1475 struct btrfs_fs_info *p = data;
1476 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1478 return fs_info->fs_devices == p->fs_devices;
1481 static int btrfs_set_super(struct super_block *s, void *data)
1483 int err = set_anon_super(s, data);
1485 s->s_fs_info = data;
1490 * subvolumes are identified by ino 256
1492 static inline int is_subvolume_inode(struct inode *inode)
1494 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1499 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1500 struct vfsmount *mnt)
1502 struct dentry *root;
1506 if (!subvol_objectid) {
1507 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1510 root = ERR_PTR(ret);
1514 subvol_name = btrfs_get_subvol_name_from_objectid(
1515 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1516 if (IS_ERR(subvol_name)) {
1517 root = ERR_CAST(subvol_name);
1524 root = mount_subtree(mnt, subvol_name);
1525 /* mount_subtree() drops our reference on the vfsmount. */
1528 if (!IS_ERR(root)) {
1529 struct super_block *s = root->d_sb;
1530 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1531 struct inode *root_inode = d_inode(root);
1532 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1535 if (!is_subvolume_inode(root_inode)) {
1536 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1540 if (subvol_objectid && root_objectid != subvol_objectid) {
1542 * This will also catch a race condition where a
1543 * subvolume which was passed by ID is renamed and
1544 * another subvolume is renamed over the old location.
1547 "subvol '%s' does not match subvolid %llu",
1548 subvol_name, subvol_objectid);
1553 root = ERR_PTR(ret);
1554 deactivate_locked_super(s);
1565 * Find a superblock for the given device / mount point.
1567 * Note: This is based on mount_bdev from fs/super.c with a few additions
1568 * for multiple device setup. Make sure to keep it in sync.
1570 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1571 int flags, const char *device_name, void *data)
1573 struct block_device *bdev = NULL;
1574 struct super_block *s;
1575 struct btrfs_device *device = NULL;
1576 struct btrfs_fs_devices *fs_devices = NULL;
1577 struct btrfs_fs_info *fs_info = NULL;
1578 void *new_sec_opts = NULL;
1579 fmode_t mode = FMODE_READ;
1582 if (!(flags & SB_RDONLY))
1583 mode |= FMODE_WRITE;
1586 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1588 return ERR_PTR(error);
1592 * Setup a dummy root and fs_info for test/set super. This is because
1593 * we don't actually fill this stuff out until open_ctree, but we need
1594 * then open_ctree will properly initialize the file system specific
1595 * settings later. btrfs_init_fs_info initializes the static elements
1596 * of the fs_info (locks and such) to make cleanup easier if we find a
1597 * superblock with our given fs_devices later on at sget() time.
1599 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1602 goto error_sec_opts;
1604 btrfs_init_fs_info(fs_info);
1606 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1607 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1608 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1613 mutex_lock(&uuid_mutex);
1614 error = btrfs_parse_device_options(data, mode, fs_type);
1616 mutex_unlock(&uuid_mutex);
1620 device = btrfs_scan_one_device(device_name, mode, fs_type);
1621 if (IS_ERR(device)) {
1622 mutex_unlock(&uuid_mutex);
1623 error = PTR_ERR(device);
1627 fs_devices = device->fs_devices;
1628 fs_info->fs_devices = fs_devices;
1630 error = btrfs_open_devices(fs_devices, mode, fs_type);
1631 mutex_unlock(&uuid_mutex);
1635 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1637 goto error_close_devices;
1640 bdev = fs_devices->latest_bdev;
1641 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1645 goto error_close_devices;
1649 btrfs_close_devices(fs_devices);
1650 btrfs_free_fs_info(fs_info);
1651 if ((flags ^ s->s_flags) & SB_RDONLY)
1654 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1655 btrfs_sb(s)->bdev_holder = fs_type;
1656 if (!strstr(crc32c_impl(), "generic"))
1657 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
1658 error = btrfs_fill_super(s, fs_devices, data);
1661 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1662 security_free_mnt_opts(&new_sec_opts);
1664 deactivate_locked_super(s);
1665 return ERR_PTR(error);
1668 return dget(s->s_root);
1670 error_close_devices:
1671 btrfs_close_devices(fs_devices);
1673 btrfs_free_fs_info(fs_info);
1675 security_free_mnt_opts(&new_sec_opts);
1676 return ERR_PTR(error);
1680 * Mount function which is called by VFS layer.
1682 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1683 * which needs vfsmount* of device's root (/). This means device's root has to
1684 * be mounted internally in any case.
1687 * 1. Parse subvol id related options for later use in mount_subvol().
1689 * 2. Mount device's root (/) by calling vfs_kern_mount().
1691 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1692 * first place. In order to avoid calling btrfs_mount() again, we use
1693 * different file_system_type which is not registered to VFS by
1694 * register_filesystem() (btrfs_root_fs_type). As a result,
1695 * btrfs_mount_root() is called. The return value will be used by
1696 * mount_subtree() in mount_subvol().
1698 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1699 * "btrfs subvolume set-default", mount_subvol() is called always.
1701 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1702 const char *device_name, void *data)
1704 struct vfsmount *mnt_root;
1705 struct dentry *root;
1706 char *subvol_name = NULL;
1707 u64 subvol_objectid = 0;
1710 error = btrfs_parse_subvol_options(data, &subvol_name,
1714 return ERR_PTR(error);
1717 /* mount device's root (/) */
1718 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1719 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1720 if (flags & SB_RDONLY) {
1721 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1722 flags & ~SB_RDONLY, device_name, data);
1724 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1725 flags | SB_RDONLY, device_name, data);
1726 if (IS_ERR(mnt_root)) {
1727 root = ERR_CAST(mnt_root);
1732 down_write(&mnt_root->mnt_sb->s_umount);
1733 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1734 up_write(&mnt_root->mnt_sb->s_umount);
1736 root = ERR_PTR(error);
1743 if (IS_ERR(mnt_root)) {
1744 root = ERR_CAST(mnt_root);
1749 /* mount_subvol() will free subvol_name and mnt_root */
1750 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1756 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1757 u32 new_pool_size, u32 old_pool_size)
1759 if (new_pool_size == old_pool_size)
1762 fs_info->thread_pool_size = new_pool_size;
1764 btrfs_info(fs_info, "resize thread pool %d -> %d",
1765 old_pool_size, new_pool_size);
1767 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1768 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1769 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1770 btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1771 btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1772 btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1774 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1775 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1776 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1777 btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
1778 btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1782 static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1784 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1787 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1788 unsigned long old_opts, int flags)
1790 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1791 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1792 (flags & SB_RDONLY))) {
1793 /* wait for any defraggers to finish */
1794 wait_event(fs_info->transaction_wait,
1795 (atomic_read(&fs_info->defrag_running) == 0));
1796 if (flags & SB_RDONLY)
1797 sync_filesystem(fs_info->sb);
1801 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1802 unsigned long old_opts)
1805 * We need to cleanup all defragable inodes if the autodefragment is
1806 * close or the filesystem is read only.
1808 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1809 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1810 btrfs_cleanup_defrag_inodes(fs_info);
1813 /* If we toggled discard async */
1814 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1815 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1816 btrfs_discard_resume(fs_info);
1817 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1818 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1819 btrfs_discard_cleanup(fs_info);
1821 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1824 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1826 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1827 struct btrfs_root *root = fs_info->tree_root;
1828 unsigned old_flags = sb->s_flags;
1829 unsigned long old_opts = fs_info->mount_opt;
1830 unsigned long old_compress_type = fs_info->compress_type;
1831 u64 old_max_inline = fs_info->max_inline;
1832 u32 old_thread_pool_size = fs_info->thread_pool_size;
1833 u32 old_metadata_ratio = fs_info->metadata_ratio;
1836 sync_filesystem(sb);
1837 btrfs_remount_prepare(fs_info);
1840 void *new_sec_opts = NULL;
1842 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1844 ret = security_sb_remount(sb, new_sec_opts);
1845 security_free_mnt_opts(&new_sec_opts);
1850 ret = btrfs_parse_options(fs_info, data, *flags);
1854 btrfs_remount_begin(fs_info, old_opts, *flags);
1855 btrfs_resize_thread_pool(fs_info,
1856 fs_info->thread_pool_size, old_thread_pool_size);
1858 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1861 if (*flags & SB_RDONLY) {
1863 * this also happens on 'umount -rf' or on shutdown, when
1864 * the filesystem is busy.
1866 cancel_work_sync(&fs_info->async_reclaim_work);
1868 btrfs_discard_cleanup(fs_info);
1870 /* wait for the uuid_scan task to finish */
1871 down(&fs_info->uuid_tree_rescan_sem);
1872 /* avoid complains from lockdep et al. */
1873 up(&fs_info->uuid_tree_rescan_sem);
1875 sb->s_flags |= SB_RDONLY;
1878 * Setting SB_RDONLY will put the cleaner thread to
1879 * sleep at the next loop if it's already active.
1880 * If it's already asleep, we'll leave unused block
1881 * groups on disk until we're mounted read-write again
1882 * unless we clean them up here.
1884 btrfs_delete_unused_bgs(fs_info);
1886 btrfs_dev_replace_suspend_for_unmount(fs_info);
1887 btrfs_scrub_cancel(fs_info);
1888 btrfs_pause_balance(fs_info);
1890 ret = btrfs_commit_super(fs_info);
1894 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
1896 "Remounting read-write after error is not allowed");
1900 if (fs_info->fs_devices->rw_devices == 0) {
1905 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1907 "too many missing devices, writable remount is not allowed");
1912 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1914 "mount required to replay tree-log, cannot remount read-write");
1919 ret = btrfs_cleanup_fs_roots(fs_info);
1923 /* recover relocation */
1924 mutex_lock(&fs_info->cleaner_mutex);
1925 ret = btrfs_recover_relocation(root);
1926 mutex_unlock(&fs_info->cleaner_mutex);
1930 ret = btrfs_resume_balance_async(fs_info);
1934 ret = btrfs_resume_dev_replace_async(fs_info);
1936 btrfs_warn(fs_info, "failed to resume dev_replace");
1940 btrfs_qgroup_rescan_resume(fs_info);
1942 if (!fs_info->uuid_root) {
1943 btrfs_info(fs_info, "creating UUID tree");
1944 ret = btrfs_create_uuid_tree(fs_info);
1947 "failed to create the UUID tree %d",
1952 sb->s_flags &= ~SB_RDONLY;
1954 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1957 wake_up_process(fs_info->transaction_kthread);
1958 btrfs_remount_cleanup(fs_info, old_opts);
1962 /* We've hit an error - don't reset SB_RDONLY */
1964 old_flags |= SB_RDONLY;
1965 sb->s_flags = old_flags;
1966 fs_info->mount_opt = old_opts;
1967 fs_info->compress_type = old_compress_type;
1968 fs_info->max_inline = old_max_inline;
1969 btrfs_resize_thread_pool(fs_info,
1970 old_thread_pool_size, fs_info->thread_pool_size);
1971 fs_info->metadata_ratio = old_metadata_ratio;
1972 btrfs_remount_cleanup(fs_info, old_opts);
1976 /* Used to sort the devices by max_avail(descending sort) */
1977 static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
1978 const void *dev_info2)
1980 if (((struct btrfs_device_info *)dev_info1)->max_avail >
1981 ((struct btrfs_device_info *)dev_info2)->max_avail)
1983 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1984 ((struct btrfs_device_info *)dev_info2)->max_avail)
1991 * sort the devices by max_avail, in which max free extent size of each device
1992 * is stored.(Descending Sort)
1994 static inline void btrfs_descending_sort_devices(
1995 struct btrfs_device_info *devices,
1998 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1999 btrfs_cmp_device_free_bytes, NULL);
2003 * The helper to calc the free space on the devices that can be used to store
2006 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
2009 struct btrfs_device_info *devices_info;
2010 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2011 struct btrfs_device *device;
2014 u64 min_stripe_size;
2015 int num_stripes = 1;
2016 int i = 0, nr_devices;
2017 const struct btrfs_raid_attr *rattr;
2020 * We aren't under the device list lock, so this is racy-ish, but good
2021 * enough for our purposes.
2023 nr_devices = fs_info->fs_devices->open_devices;
2026 nr_devices = fs_info->fs_devices->open_devices;
2034 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
2039 /* calc min stripe number for data space allocation */
2040 type = btrfs_data_alloc_profile(fs_info);
2041 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
2043 if (type & BTRFS_BLOCK_GROUP_RAID0)
2044 num_stripes = nr_devices;
2045 else if (type & BTRFS_BLOCK_GROUP_RAID1)
2047 else if (type & BTRFS_BLOCK_GROUP_RAID1C3)
2049 else if (type & BTRFS_BLOCK_GROUP_RAID1C4)
2051 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2054 /* Adjust for more than 1 stripe per device */
2055 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
2058 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2059 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2060 &device->dev_state) ||
2062 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
2065 if (i >= nr_devices)
2068 avail_space = device->total_bytes - device->bytes_used;
2070 /* align with stripe_len */
2071 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
2074 * In order to avoid overwriting the superblock on the drive,
2075 * btrfs starts at an offset of at least 1MB when doing chunk
2078 * This ensures we have at least min_stripe_size free space
2079 * after excluding 1MB.
2081 if (avail_space <= SZ_1M + min_stripe_size)
2084 avail_space -= SZ_1M;
2086 devices_info[i].dev = device;
2087 devices_info[i].max_avail = avail_space;
2095 btrfs_descending_sort_devices(devices_info, nr_devices);
2099 while (nr_devices >= rattr->devs_min) {
2100 num_stripes = min(num_stripes, nr_devices);
2102 if (devices_info[i].max_avail >= min_stripe_size) {
2106 avail_space += devices_info[i].max_avail * num_stripes;
2107 alloc_size = devices_info[i].max_avail;
2108 for (j = i + 1 - num_stripes; j <= i; j++)
2109 devices_info[j].max_avail -= alloc_size;
2115 kfree(devices_info);
2116 *free_bytes = avail_space;
2121 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2123 * If there's a redundant raid level at DATA block groups, use the respective
2124 * multiplier to scale the sizes.
2126 * Unused device space usage is based on simulating the chunk allocator
2127 * algorithm that respects the device sizes and order of allocations. This is
2128 * a close approximation of the actual use but there are other factors that may
2129 * change the result (like a new metadata chunk).
2131 * If metadata is exhausted, f_bavail will be 0.
2133 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2135 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2136 struct btrfs_super_block *disk_super = fs_info->super_copy;
2137 struct btrfs_space_info *found;
2139 u64 total_free_data = 0;
2140 u64 total_free_meta = 0;
2141 int bits = dentry->d_sb->s_blocksize_bits;
2142 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2143 unsigned factor = 1;
2144 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2150 list_for_each_entry_rcu(found, &fs_info->space_info, list) {
2151 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2154 total_free_data += found->disk_total - found->disk_used;
2156 btrfs_account_ro_block_groups_free_space(found);
2158 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2159 if (!list_empty(&found->block_groups[i]))
2160 factor = btrfs_bg_type_to_factor(
2161 btrfs_raid_array[i].bg_flag);
2166 * Metadata in mixed block goup profiles are accounted in data
2168 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2169 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2172 total_free_meta += found->disk_total -
2176 total_used += found->disk_used;
2181 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2182 buf->f_blocks >>= bits;
2183 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2185 /* Account global block reserve as used, it's in logical size already */
2186 spin_lock(&block_rsv->lock);
2187 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2188 if (buf->f_bfree >= block_rsv->size >> bits)
2189 buf->f_bfree -= block_rsv->size >> bits;
2192 spin_unlock(&block_rsv->lock);
2194 buf->f_bavail = div_u64(total_free_data, factor);
2195 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2198 buf->f_bavail += div_u64(total_free_data, factor);
2199 buf->f_bavail = buf->f_bavail >> bits;
2202 * We calculate the remaining metadata space minus global reserve. If
2203 * this is (supposedly) smaller than zero, there's no space. But this
2204 * does not hold in practice, the exhausted state happens where's still
2205 * some positive delta. So we apply some guesswork and compare the
2206 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2208 * We probably cannot calculate the exact threshold value because this
2209 * depends on the internal reservations requested by various
2210 * operations, so some operations that consume a few metadata will
2211 * succeed even if the Avail is zero. But this is better than the other
2217 * We only want to claim there's no available space if we can no longer
2218 * allocate chunks for our metadata profile and our global reserve will
2219 * not fit in the free metadata space. If we aren't ->full then we
2220 * still can allocate chunks and thus are fine using the currently
2221 * calculated f_bavail.
2223 if (!mixed && block_rsv->space_info->full &&
2224 total_free_meta - thresh < block_rsv->size)
2227 buf->f_type = BTRFS_SUPER_MAGIC;
2228 buf->f_bsize = dentry->d_sb->s_blocksize;
2229 buf->f_namelen = BTRFS_NAME_LEN;
2231 /* We treat it as constant endianness (it doesn't matter _which_)
2232 because we want the fsid to come out the same whether mounted
2233 on a big-endian or little-endian host */
2234 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2235 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2236 /* Mask in the root object ID too, to disambiguate subvols */
2237 buf->f_fsid.val[0] ^=
2238 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2239 buf->f_fsid.val[1] ^=
2240 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2245 static void btrfs_kill_super(struct super_block *sb)
2247 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2248 kill_anon_super(sb);
2249 btrfs_free_fs_info(fs_info);
2252 static struct file_system_type btrfs_fs_type = {
2253 .owner = THIS_MODULE,
2255 .mount = btrfs_mount,
2256 .kill_sb = btrfs_kill_super,
2257 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2260 static struct file_system_type btrfs_root_fs_type = {
2261 .owner = THIS_MODULE,
2263 .mount = btrfs_mount_root,
2264 .kill_sb = btrfs_kill_super,
2265 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2268 MODULE_ALIAS_FS("btrfs");
2270 static int btrfs_control_open(struct inode *inode, struct file *file)
2273 * The control file's private_data is used to hold the
2274 * transaction when it is started and is used to keep
2275 * track of whether a transaction is already in progress.
2277 file->private_data = NULL;
2282 * Used by /dev/btrfs-control for devices ioctls.
2284 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2287 struct btrfs_ioctl_vol_args *vol;
2288 struct btrfs_device *device = NULL;
2291 if (!capable(CAP_SYS_ADMIN))
2294 vol = memdup_user((void __user *)arg, sizeof(*vol));
2296 return PTR_ERR(vol);
2297 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2300 case BTRFS_IOC_SCAN_DEV:
2301 mutex_lock(&uuid_mutex);
2302 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2303 &btrfs_root_fs_type);
2304 ret = PTR_ERR_OR_ZERO(device);
2305 mutex_unlock(&uuid_mutex);
2307 case BTRFS_IOC_FORGET_DEV:
2308 ret = btrfs_forget_devices(vol->name);
2310 case BTRFS_IOC_DEVICES_READY:
2311 mutex_lock(&uuid_mutex);
2312 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2313 &btrfs_root_fs_type);
2314 if (IS_ERR(device)) {
2315 mutex_unlock(&uuid_mutex);
2316 ret = PTR_ERR(device);
2319 ret = !(device->fs_devices->num_devices ==
2320 device->fs_devices->total_devices);
2321 mutex_unlock(&uuid_mutex);
2323 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2324 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2332 static int btrfs_freeze(struct super_block *sb)
2334 struct btrfs_trans_handle *trans;
2335 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2336 struct btrfs_root *root = fs_info->tree_root;
2338 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2340 * We don't need a barrier here, we'll wait for any transaction that
2341 * could be in progress on other threads (and do delayed iputs that
2342 * we want to avoid on a frozen filesystem), or do the commit
2345 trans = btrfs_attach_transaction_barrier(root);
2346 if (IS_ERR(trans)) {
2347 /* no transaction, don't bother */
2348 if (PTR_ERR(trans) == -ENOENT)
2350 return PTR_ERR(trans);
2352 return btrfs_commit_transaction(trans);
2355 static int btrfs_unfreeze(struct super_block *sb)
2357 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2359 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2363 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2365 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2366 struct btrfs_fs_devices *cur_devices;
2367 struct btrfs_device *dev, *first_dev = NULL;
2368 struct list_head *head;
2371 * Lightweight locking of the devices. We should not need
2372 * device_list_mutex here as we only read the device data and the list
2373 * is protected by RCU. Even if a device is deleted during the list
2374 * traversals, we'll get valid data, the freeing callback will wait at
2375 * least until the rcu_read_unlock.
2378 cur_devices = fs_info->fs_devices;
2379 while (cur_devices) {
2380 head = &cur_devices->devices;
2381 list_for_each_entry_rcu(dev, head, dev_list) {
2382 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2386 if (!first_dev || dev->devid < first_dev->devid)
2389 cur_devices = cur_devices->seed;
2393 seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2400 static const struct super_operations btrfs_super_ops = {
2401 .drop_inode = btrfs_drop_inode,
2402 .evict_inode = btrfs_evict_inode,
2403 .put_super = btrfs_put_super,
2404 .sync_fs = btrfs_sync_fs,
2405 .show_options = btrfs_show_options,
2406 .show_devname = btrfs_show_devname,
2407 .alloc_inode = btrfs_alloc_inode,
2408 .destroy_inode = btrfs_destroy_inode,
2409 .free_inode = btrfs_free_inode,
2410 .statfs = btrfs_statfs,
2411 .remount_fs = btrfs_remount,
2412 .freeze_fs = btrfs_freeze,
2413 .unfreeze_fs = btrfs_unfreeze,
2416 static const struct file_operations btrfs_ctl_fops = {
2417 .open = btrfs_control_open,
2418 .unlocked_ioctl = btrfs_control_ioctl,
2419 .compat_ioctl = compat_ptr_ioctl,
2420 .owner = THIS_MODULE,
2421 .llseek = noop_llseek,
2424 static struct miscdevice btrfs_misc = {
2425 .minor = BTRFS_MINOR,
2426 .name = "btrfs-control",
2427 .fops = &btrfs_ctl_fops
2430 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2431 MODULE_ALIAS("devname:btrfs-control");
2433 static int __init btrfs_interface_init(void)
2435 return misc_register(&btrfs_misc);
2438 static __cold void btrfs_interface_exit(void)
2440 misc_deregister(&btrfs_misc);
2443 static void __init btrfs_print_mod_info(void)
2445 static const char options[] = ""
2446 #ifdef CONFIG_BTRFS_DEBUG
2449 #ifdef CONFIG_BTRFS_ASSERT
2452 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2453 ", integrity-checker=on"
2455 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2459 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
2462 static int __init init_btrfs_fs(void)
2468 err = btrfs_init_sysfs();
2472 btrfs_init_compress();
2474 err = btrfs_init_cachep();
2478 err = extent_io_init();
2482 err = extent_state_cache_init();
2484 goto free_extent_io;
2486 err = extent_map_init();
2488 goto free_extent_state_cache;
2490 err = ordered_data_init();
2492 goto free_extent_map;
2494 err = btrfs_delayed_inode_init();
2496 goto free_ordered_data;
2498 err = btrfs_auto_defrag_init();
2500 goto free_delayed_inode;
2502 err = btrfs_delayed_ref_init();
2504 goto free_auto_defrag;
2506 err = btrfs_prelim_ref_init();
2508 goto free_delayed_ref;
2510 err = btrfs_end_io_wq_init();
2512 goto free_prelim_ref;
2514 err = btrfs_interface_init();
2516 goto free_end_io_wq;
2518 btrfs_init_lockdep();
2520 btrfs_print_mod_info();
2522 err = btrfs_run_sanity_tests();
2524 goto unregister_ioctl;
2526 err = register_filesystem(&btrfs_fs_type);
2528 goto unregister_ioctl;
2533 btrfs_interface_exit();
2535 btrfs_end_io_wq_exit();
2537 btrfs_prelim_ref_exit();
2539 btrfs_delayed_ref_exit();
2541 btrfs_auto_defrag_exit();
2543 btrfs_delayed_inode_exit();
2545 ordered_data_exit();
2548 free_extent_state_cache:
2549 extent_state_cache_exit();
2553 btrfs_destroy_cachep();
2555 btrfs_exit_compress();
2561 static void __exit exit_btrfs_fs(void)
2563 btrfs_destroy_cachep();
2564 btrfs_delayed_ref_exit();
2565 btrfs_auto_defrag_exit();
2566 btrfs_delayed_inode_exit();
2567 btrfs_prelim_ref_exit();
2568 ordered_data_exit();
2570 extent_state_cache_exit();
2572 btrfs_interface_exit();
2573 btrfs_end_io_wq_exit();
2574 unregister_filesystem(&btrfs_fs_type);
2576 btrfs_cleanup_fs_uuids();
2577 btrfs_exit_compress();
2580 late_initcall(init_btrfs_fs);
2581 module_exit(exit_btrfs_fs)
2583 MODULE_LICENSE("GPL");
2584 MODULE_SOFTDEP("pre: crc32c");
2585 MODULE_SOFTDEP("pre: xxhash64");
2586 MODULE_SOFTDEP("pre: sha256");
2587 MODULE_SOFTDEP("pre: blake2b-256");