xref: /openbmc/linux/fs/btrfs/super.c (revision 9aa2cba7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5 
6 #include <linux/blkdev.h>
7 #include <linux/module.h>
8 #include <linux/fs.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 "messages.h"
30 #include "delayed-inode.h"
31 #include "ctree.h"
32 #include "disk-io.h"
33 #include "transaction.h"
34 #include "btrfs_inode.h"
35 #include "print-tree.h"
36 #include "props.h"
37 #include "xattr.h"
38 #include "bio.h"
39 #include "export.h"
40 #include "compression.h"
41 #include "rcu-string.h"
42 #include "dev-replace.h"
43 #include "free-space-cache.h"
44 #include "backref.h"
45 #include "space-info.h"
46 #include "sysfs.h"
47 #include "zoned.h"
48 #include "tests/btrfs-tests.h"
49 #include "block-group.h"
50 #include "discard.h"
51 #include "qgroup.h"
52 #include "raid56.h"
53 #include "fs.h"
54 #include "accessors.h"
55 #include "defrag.h"
56 #include "dir-item.h"
57 #include "ioctl.h"
58 #include "scrub.h"
59 #include "verity.h"
60 #include "super.h"
61 #include "extent-tree.h"
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/btrfs.h>
64 
65 static const struct super_operations btrfs_super_ops;
66 
67 /*
68  * Types for mounting the default subvolume and a subvolume explicitly
69  * requested by subvol=/path. That way the callchain is straightforward and we
70  * don't have to play tricks with the mount options and recursive calls to
71  * btrfs_mount.
72  *
73  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
74  */
75 static struct file_system_type btrfs_fs_type;
76 static struct file_system_type btrfs_root_fs_type;
77 
78 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
79 
80 static void btrfs_put_super(struct super_block *sb)
81 {
82 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
83 
84 	btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
85 	close_ctree(fs_info);
86 }
87 
88 enum {
89 	Opt_acl, Opt_noacl,
90 	Opt_clear_cache,
91 	Opt_commit_interval,
92 	Opt_compress,
93 	Opt_compress_force,
94 	Opt_compress_force_type,
95 	Opt_compress_type,
96 	Opt_degraded,
97 	Opt_device,
98 	Opt_fatal_errors,
99 	Opt_flushoncommit, Opt_noflushoncommit,
100 	Opt_max_inline,
101 	Opt_barrier, Opt_nobarrier,
102 	Opt_datacow, Opt_nodatacow,
103 	Opt_datasum, Opt_nodatasum,
104 	Opt_defrag, Opt_nodefrag,
105 	Opt_discard, Opt_nodiscard,
106 	Opt_discard_mode,
107 	Opt_norecovery,
108 	Opt_ratio,
109 	Opt_rescan_uuid_tree,
110 	Opt_skip_balance,
111 	Opt_space_cache, Opt_no_space_cache,
112 	Opt_space_cache_version,
113 	Opt_ssd, Opt_nossd,
114 	Opt_ssd_spread, Opt_nossd_spread,
115 	Opt_subvol,
116 	Opt_subvol_empty,
117 	Opt_subvolid,
118 	Opt_thread_pool,
119 	Opt_treelog, Opt_notreelog,
120 	Opt_user_subvol_rm_allowed,
121 
122 	/* Rescue options */
123 	Opt_rescue,
124 	Opt_usebackuproot,
125 	Opt_nologreplay,
126 	Opt_ignorebadroots,
127 	Opt_ignoredatacsums,
128 	Opt_rescue_all,
129 
130 	/* Deprecated options */
131 	Opt_recovery,
132 	Opt_inode_cache, Opt_noinode_cache,
133 
134 	/* Debugging options */
135 	Opt_check_integrity,
136 	Opt_check_integrity_including_extent_data,
137 	Opt_check_integrity_print_mask,
138 	Opt_enospc_debug, Opt_noenospc_debug,
139 #ifdef CONFIG_BTRFS_DEBUG
140 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
141 #endif
142 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
143 	Opt_ref_verify,
144 #endif
145 	Opt_err,
146 };
147 
148 static const match_table_t tokens = {
149 	{Opt_acl, "acl"},
150 	{Opt_noacl, "noacl"},
151 	{Opt_clear_cache, "clear_cache"},
152 	{Opt_commit_interval, "commit=%u"},
153 	{Opt_compress, "compress"},
154 	{Opt_compress_type, "compress=%s"},
155 	{Opt_compress_force, "compress-force"},
156 	{Opt_compress_force_type, "compress-force=%s"},
157 	{Opt_degraded, "degraded"},
158 	{Opt_device, "device=%s"},
159 	{Opt_fatal_errors, "fatal_errors=%s"},
160 	{Opt_flushoncommit, "flushoncommit"},
161 	{Opt_noflushoncommit, "noflushoncommit"},
162 	{Opt_inode_cache, "inode_cache"},
163 	{Opt_noinode_cache, "noinode_cache"},
164 	{Opt_max_inline, "max_inline=%s"},
165 	{Opt_barrier, "barrier"},
166 	{Opt_nobarrier, "nobarrier"},
167 	{Opt_datacow, "datacow"},
168 	{Opt_nodatacow, "nodatacow"},
169 	{Opt_datasum, "datasum"},
170 	{Opt_nodatasum, "nodatasum"},
171 	{Opt_defrag, "autodefrag"},
172 	{Opt_nodefrag, "noautodefrag"},
173 	{Opt_discard, "discard"},
174 	{Opt_discard_mode, "discard=%s"},
175 	{Opt_nodiscard, "nodiscard"},
176 	{Opt_norecovery, "norecovery"},
177 	{Opt_ratio, "metadata_ratio=%u"},
178 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
179 	{Opt_skip_balance, "skip_balance"},
180 	{Opt_space_cache, "space_cache"},
181 	{Opt_no_space_cache, "nospace_cache"},
182 	{Opt_space_cache_version, "space_cache=%s"},
183 	{Opt_ssd, "ssd"},
184 	{Opt_nossd, "nossd"},
185 	{Opt_ssd_spread, "ssd_spread"},
186 	{Opt_nossd_spread, "nossd_spread"},
187 	{Opt_subvol, "subvol=%s"},
188 	{Opt_subvol_empty, "subvol="},
189 	{Opt_subvolid, "subvolid=%s"},
190 	{Opt_thread_pool, "thread_pool=%u"},
191 	{Opt_treelog, "treelog"},
192 	{Opt_notreelog, "notreelog"},
193 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
194 
195 	/* Rescue options */
196 	{Opt_rescue, "rescue=%s"},
197 	/* Deprecated, with alias rescue=nologreplay */
198 	{Opt_nologreplay, "nologreplay"},
199 	/* Deprecated, with alias rescue=usebackuproot */
200 	{Opt_usebackuproot, "usebackuproot"},
201 
202 	/* Deprecated options */
203 	{Opt_recovery, "recovery"},
204 
205 	/* Debugging options */
206 	{Opt_check_integrity, "check_int"},
207 	{Opt_check_integrity_including_extent_data, "check_int_data"},
208 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
209 	{Opt_enospc_debug, "enospc_debug"},
210 	{Opt_noenospc_debug, "noenospc_debug"},
211 #ifdef CONFIG_BTRFS_DEBUG
212 	{Opt_fragment_data, "fragment=data"},
213 	{Opt_fragment_metadata, "fragment=metadata"},
214 	{Opt_fragment_all, "fragment=all"},
215 #endif
216 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
217 	{Opt_ref_verify, "ref_verify"},
218 #endif
219 	{Opt_err, NULL},
220 };
221 
222 static const match_table_t rescue_tokens = {
223 	{Opt_usebackuproot, "usebackuproot"},
224 	{Opt_nologreplay, "nologreplay"},
225 	{Opt_ignorebadroots, "ignorebadroots"},
226 	{Opt_ignorebadroots, "ibadroots"},
227 	{Opt_ignoredatacsums, "ignoredatacsums"},
228 	{Opt_ignoredatacsums, "idatacsums"},
229 	{Opt_rescue_all, "all"},
230 	{Opt_err, NULL},
231 };
232 
233 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
234 			    const char *opt_name)
235 {
236 	if (fs_info->mount_opt & opt) {
237 		btrfs_err(fs_info, "%s must be used with ro mount option",
238 			  opt_name);
239 		return true;
240 	}
241 	return false;
242 }
243 
244 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
245 {
246 	char *opts;
247 	char *orig;
248 	char *p;
249 	substring_t args[MAX_OPT_ARGS];
250 	int ret = 0;
251 
252 	opts = kstrdup(options, GFP_KERNEL);
253 	if (!opts)
254 		return -ENOMEM;
255 	orig = opts;
256 
257 	while ((p = strsep(&opts, ":")) != NULL) {
258 		int token;
259 
260 		if (!*p)
261 			continue;
262 		token = match_token(p, rescue_tokens, args);
263 		switch (token){
264 		case Opt_usebackuproot:
265 			btrfs_info(info,
266 				   "trying to use backup root at mount time");
267 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
268 			break;
269 		case Opt_nologreplay:
270 			btrfs_set_and_info(info, NOLOGREPLAY,
271 					   "disabling log replay at mount time");
272 			break;
273 		case Opt_ignorebadroots:
274 			btrfs_set_and_info(info, IGNOREBADROOTS,
275 					   "ignoring bad roots");
276 			break;
277 		case Opt_ignoredatacsums:
278 			btrfs_set_and_info(info, IGNOREDATACSUMS,
279 					   "ignoring data csums");
280 			break;
281 		case Opt_rescue_all:
282 			btrfs_info(info, "enabling all of the rescue options");
283 			btrfs_set_and_info(info, IGNOREDATACSUMS,
284 					   "ignoring data csums");
285 			btrfs_set_and_info(info, IGNOREBADROOTS,
286 					   "ignoring bad roots");
287 			btrfs_set_and_info(info, NOLOGREPLAY,
288 					   "disabling log replay at mount time");
289 			break;
290 		case Opt_err:
291 			btrfs_info(info, "unrecognized rescue option '%s'", p);
292 			ret = -EINVAL;
293 			goto out;
294 		default:
295 			break;
296 		}
297 
298 	}
299 out:
300 	kfree(orig);
301 	return ret;
302 }
303 
304 /*
305  * Regular mount options parser.  Everything that is needed only when
306  * reading in a new superblock is parsed here.
307  * XXX JDM: This needs to be cleaned up for remount.
308  */
309 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
310 			unsigned long new_flags)
311 {
312 	substring_t args[MAX_OPT_ARGS];
313 	char *p, *num;
314 	int intarg;
315 	int ret = 0;
316 	char *compress_type;
317 	bool compress_force = false;
318 	enum btrfs_compression_type saved_compress_type;
319 	int saved_compress_level;
320 	bool saved_compress_force;
321 	int no_compress = 0;
322 	const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
323 
324 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
325 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
326 	else if (btrfs_free_space_cache_v1_active(info)) {
327 		if (btrfs_is_zoned(info)) {
328 			btrfs_info(info,
329 			"zoned: clearing existing space cache");
330 			btrfs_set_super_cache_generation(info->super_copy, 0);
331 		} else {
332 			btrfs_set_opt(info->mount_opt, SPACE_CACHE);
333 		}
334 	}
335 
336 	/*
337 	 * Even the options are empty, we still need to do extra check
338 	 * against new flags
339 	 */
340 	if (!options)
341 		goto check;
342 
343 	while ((p = strsep(&options, ",")) != NULL) {
344 		int token;
345 		if (!*p)
346 			continue;
347 
348 		token = match_token(p, tokens, args);
349 		switch (token) {
350 		case Opt_degraded:
351 			btrfs_info(info, "allowing degraded mounts");
352 			btrfs_set_opt(info->mount_opt, DEGRADED);
353 			break;
354 		case Opt_subvol:
355 		case Opt_subvol_empty:
356 		case Opt_subvolid:
357 		case Opt_device:
358 			/*
359 			 * These are parsed by btrfs_parse_subvol_options or
360 			 * btrfs_parse_device_options and can be ignored here.
361 			 */
362 			break;
363 		case Opt_nodatasum:
364 			btrfs_set_and_info(info, NODATASUM,
365 					   "setting nodatasum");
366 			break;
367 		case Opt_datasum:
368 			if (btrfs_test_opt(info, NODATASUM)) {
369 				if (btrfs_test_opt(info, NODATACOW))
370 					btrfs_info(info,
371 						   "setting datasum, datacow enabled");
372 				else
373 					btrfs_info(info, "setting datasum");
374 			}
375 			btrfs_clear_opt(info->mount_opt, NODATACOW);
376 			btrfs_clear_opt(info->mount_opt, NODATASUM);
377 			break;
378 		case Opt_nodatacow:
379 			if (!btrfs_test_opt(info, NODATACOW)) {
380 				if (!btrfs_test_opt(info, COMPRESS) ||
381 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
382 					btrfs_info(info,
383 						   "setting nodatacow, compression disabled");
384 				} else {
385 					btrfs_info(info, "setting nodatacow");
386 				}
387 			}
388 			btrfs_clear_opt(info->mount_opt, COMPRESS);
389 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
390 			btrfs_set_opt(info->mount_opt, NODATACOW);
391 			btrfs_set_opt(info->mount_opt, NODATASUM);
392 			break;
393 		case Opt_datacow:
394 			btrfs_clear_and_info(info, NODATACOW,
395 					     "setting datacow");
396 			break;
397 		case Opt_compress_force:
398 		case Opt_compress_force_type:
399 			compress_force = true;
400 			fallthrough;
401 		case Opt_compress:
402 		case Opt_compress_type:
403 			saved_compress_type = btrfs_test_opt(info,
404 							     COMPRESS) ?
405 				info->compress_type : BTRFS_COMPRESS_NONE;
406 			saved_compress_force =
407 				btrfs_test_opt(info, FORCE_COMPRESS);
408 			saved_compress_level = info->compress_level;
409 			if (token == Opt_compress ||
410 			    token == Opt_compress_force ||
411 			    strncmp(args[0].from, "zlib", 4) == 0) {
412 				compress_type = "zlib";
413 
414 				info->compress_type = BTRFS_COMPRESS_ZLIB;
415 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
416 				/*
417 				 * args[0] contains uninitialized data since
418 				 * for these tokens we don't expect any
419 				 * parameter.
420 				 */
421 				if (token != Opt_compress &&
422 				    token != Opt_compress_force)
423 					info->compress_level =
424 					  btrfs_compress_str2level(
425 							BTRFS_COMPRESS_ZLIB,
426 							args[0].from + 4);
427 				btrfs_set_opt(info->mount_opt, COMPRESS);
428 				btrfs_clear_opt(info->mount_opt, NODATACOW);
429 				btrfs_clear_opt(info->mount_opt, NODATASUM);
430 				no_compress = 0;
431 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
432 				compress_type = "lzo";
433 				info->compress_type = BTRFS_COMPRESS_LZO;
434 				info->compress_level = 0;
435 				btrfs_set_opt(info->mount_opt, COMPRESS);
436 				btrfs_clear_opt(info->mount_opt, NODATACOW);
437 				btrfs_clear_opt(info->mount_opt, NODATASUM);
438 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
439 				no_compress = 0;
440 			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
441 				compress_type = "zstd";
442 				info->compress_type = BTRFS_COMPRESS_ZSTD;
443 				info->compress_level =
444 					btrfs_compress_str2level(
445 							 BTRFS_COMPRESS_ZSTD,
446 							 args[0].from + 4);
447 				btrfs_set_opt(info->mount_opt, COMPRESS);
448 				btrfs_clear_opt(info->mount_opt, NODATACOW);
449 				btrfs_clear_opt(info->mount_opt, NODATASUM);
450 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
451 				no_compress = 0;
452 			} else if (strncmp(args[0].from, "no", 2) == 0) {
453 				compress_type = "no";
454 				info->compress_level = 0;
455 				info->compress_type = 0;
456 				btrfs_clear_opt(info->mount_opt, COMPRESS);
457 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
458 				compress_force = false;
459 				no_compress++;
460 			} else {
461 				btrfs_err(info, "unrecognized compression value %s",
462 					  args[0].from);
463 				ret = -EINVAL;
464 				goto out;
465 			}
466 
467 			if (compress_force) {
468 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
469 			} else {
470 				/*
471 				 * If we remount from compress-force=xxx to
472 				 * compress=xxx, we need clear FORCE_COMPRESS
473 				 * flag, otherwise, there is no way for users
474 				 * to disable forcible compression separately.
475 				 */
476 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
477 			}
478 			if (no_compress == 1) {
479 				btrfs_info(info, "use no compression");
480 			} else if ((info->compress_type != saved_compress_type) ||
481 				   (compress_force != saved_compress_force) ||
482 				   (info->compress_level != saved_compress_level)) {
483 				btrfs_info(info, "%s %s compression, level %d",
484 					   (compress_force) ? "force" : "use",
485 					   compress_type, info->compress_level);
486 			}
487 			compress_force = false;
488 			break;
489 		case Opt_ssd:
490 			btrfs_set_and_info(info, SSD,
491 					   "enabling ssd optimizations");
492 			btrfs_clear_opt(info->mount_opt, NOSSD);
493 			break;
494 		case Opt_ssd_spread:
495 			btrfs_set_and_info(info, SSD,
496 					   "enabling ssd optimizations");
497 			btrfs_set_and_info(info, SSD_SPREAD,
498 					   "using spread ssd allocation scheme");
499 			btrfs_clear_opt(info->mount_opt, NOSSD);
500 			break;
501 		case Opt_nossd:
502 			btrfs_set_opt(info->mount_opt, NOSSD);
503 			btrfs_clear_and_info(info, SSD,
504 					     "not using ssd optimizations");
505 			fallthrough;
506 		case Opt_nossd_spread:
507 			btrfs_clear_and_info(info, SSD_SPREAD,
508 					     "not using spread ssd allocation scheme");
509 			break;
510 		case Opt_barrier:
511 			btrfs_clear_and_info(info, NOBARRIER,
512 					     "turning on barriers");
513 			break;
514 		case Opt_nobarrier:
515 			btrfs_set_and_info(info, NOBARRIER,
516 					   "turning off barriers");
517 			break;
518 		case Opt_thread_pool:
519 			ret = match_int(&args[0], &intarg);
520 			if (ret) {
521 				btrfs_err(info, "unrecognized thread_pool value %s",
522 					  args[0].from);
523 				goto out;
524 			} else if (intarg == 0) {
525 				btrfs_err(info, "invalid value 0 for thread_pool");
526 				ret = -EINVAL;
527 				goto out;
528 			}
529 			info->thread_pool_size = intarg;
530 			break;
531 		case Opt_max_inline:
532 			num = match_strdup(&args[0]);
533 			if (num) {
534 				info->max_inline = memparse(num, NULL);
535 				kfree(num);
536 
537 				if (info->max_inline) {
538 					info->max_inline = min_t(u64,
539 						info->max_inline,
540 						info->sectorsize);
541 				}
542 				btrfs_info(info, "max_inline at %llu",
543 					   info->max_inline);
544 			} else {
545 				ret = -ENOMEM;
546 				goto out;
547 			}
548 			break;
549 		case Opt_acl:
550 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
551 			info->sb->s_flags |= SB_POSIXACL;
552 			break;
553 #else
554 			btrfs_err(info, "support for ACL not compiled in!");
555 			ret = -EINVAL;
556 			goto out;
557 #endif
558 		case Opt_noacl:
559 			info->sb->s_flags &= ~SB_POSIXACL;
560 			break;
561 		case Opt_notreelog:
562 			btrfs_set_and_info(info, NOTREELOG,
563 					   "disabling tree log");
564 			break;
565 		case Opt_treelog:
566 			btrfs_clear_and_info(info, NOTREELOG,
567 					     "enabling tree log");
568 			break;
569 		case Opt_norecovery:
570 		case Opt_nologreplay:
571 			btrfs_warn(info,
572 		"'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
573 			btrfs_set_and_info(info, NOLOGREPLAY,
574 					   "disabling log replay at mount time");
575 			break;
576 		case Opt_flushoncommit:
577 			btrfs_set_and_info(info, FLUSHONCOMMIT,
578 					   "turning on flush-on-commit");
579 			break;
580 		case Opt_noflushoncommit:
581 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
582 					     "turning off flush-on-commit");
583 			break;
584 		case Opt_ratio:
585 			ret = match_int(&args[0], &intarg);
586 			if (ret) {
587 				btrfs_err(info, "unrecognized metadata_ratio value %s",
588 					  args[0].from);
589 				goto out;
590 			}
591 			info->metadata_ratio = intarg;
592 			btrfs_info(info, "metadata ratio %u",
593 				   info->metadata_ratio);
594 			break;
595 		case Opt_discard:
596 		case Opt_discard_mode:
597 			if (token == Opt_discard ||
598 			    strcmp(args[0].from, "sync") == 0) {
599 				btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
600 				btrfs_set_and_info(info, DISCARD_SYNC,
601 						   "turning on sync discard");
602 			} else if (strcmp(args[0].from, "async") == 0) {
603 				btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
604 				btrfs_set_and_info(info, DISCARD_ASYNC,
605 						   "turning on async discard");
606 			} else {
607 				btrfs_err(info, "unrecognized discard mode value %s",
608 					  args[0].from);
609 				ret = -EINVAL;
610 				goto out;
611 			}
612 			btrfs_clear_opt(info->mount_opt, NODISCARD);
613 			break;
614 		case Opt_nodiscard:
615 			btrfs_clear_and_info(info, DISCARD_SYNC,
616 					     "turning off discard");
617 			btrfs_clear_and_info(info, DISCARD_ASYNC,
618 					     "turning off async discard");
619 			btrfs_set_opt(info->mount_opt, NODISCARD);
620 			break;
621 		case Opt_space_cache:
622 		case Opt_space_cache_version:
623 			/*
624 			 * We already set FREE_SPACE_TREE above because we have
625 			 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
626 			 * to allow v1 to be set for extent tree v2, simply
627 			 * ignore this setting if we're extent tree v2.
628 			 */
629 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
630 				break;
631 			if (token == Opt_space_cache ||
632 			    strcmp(args[0].from, "v1") == 0) {
633 				btrfs_clear_opt(info->mount_opt,
634 						FREE_SPACE_TREE);
635 				btrfs_set_and_info(info, SPACE_CACHE,
636 					   "enabling disk space caching");
637 			} else if (strcmp(args[0].from, "v2") == 0) {
638 				btrfs_clear_opt(info->mount_opt,
639 						SPACE_CACHE);
640 				btrfs_set_and_info(info, FREE_SPACE_TREE,
641 						   "enabling free space tree");
642 			} else {
643 				btrfs_err(info, "unrecognized space_cache value %s",
644 					  args[0].from);
645 				ret = -EINVAL;
646 				goto out;
647 			}
648 			break;
649 		case Opt_rescan_uuid_tree:
650 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
651 			break;
652 		case Opt_no_space_cache:
653 			/*
654 			 * We cannot operate without the free space tree with
655 			 * extent tree v2, ignore this option.
656 			 */
657 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
658 				break;
659 			if (btrfs_test_opt(info, SPACE_CACHE)) {
660 				btrfs_clear_and_info(info, SPACE_CACHE,
661 					     "disabling disk space caching");
662 			}
663 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
664 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
665 					     "disabling free space tree");
666 			}
667 			break;
668 		case Opt_inode_cache:
669 		case Opt_noinode_cache:
670 			btrfs_warn(info,
671 	"the 'inode_cache' option is deprecated and has no effect since 5.11");
672 			break;
673 		case Opt_clear_cache:
674 			/*
675 			 * We cannot clear the free space tree with extent tree
676 			 * v2, ignore this option.
677 			 */
678 			if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
679 				break;
680 			btrfs_set_and_info(info, CLEAR_CACHE,
681 					   "force clearing of disk cache");
682 			break;
683 		case Opt_user_subvol_rm_allowed:
684 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
685 			break;
686 		case Opt_enospc_debug:
687 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
688 			break;
689 		case Opt_noenospc_debug:
690 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
691 			break;
692 		case Opt_defrag:
693 			btrfs_set_and_info(info, AUTO_DEFRAG,
694 					   "enabling auto defrag");
695 			break;
696 		case Opt_nodefrag:
697 			btrfs_clear_and_info(info, AUTO_DEFRAG,
698 					     "disabling auto defrag");
699 			break;
700 		case Opt_recovery:
701 		case Opt_usebackuproot:
702 			btrfs_warn(info,
703 			"'%s' is deprecated, use 'rescue=usebackuproot' instead",
704 				   token == Opt_recovery ? "recovery" :
705 				   "usebackuproot");
706 			btrfs_info(info,
707 				   "trying to use backup root at mount time");
708 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
709 			break;
710 		case Opt_skip_balance:
711 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
712 			break;
713 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
714 		case Opt_check_integrity_including_extent_data:
715 			btrfs_warn(info,
716 	"integrity checker is deprecated and will be removed in 6.7");
717 			btrfs_info(info,
718 				   "enabling check integrity including extent data");
719 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
720 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
721 			break;
722 		case Opt_check_integrity:
723 			btrfs_warn(info,
724 	"integrity checker is deprecated and will be removed in 6.7");
725 			btrfs_info(info, "enabling check integrity");
726 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
727 			break;
728 		case Opt_check_integrity_print_mask:
729 			ret = match_int(&args[0], &intarg);
730 			if (ret) {
731 				btrfs_err(info,
732 				"unrecognized check_integrity_print_mask value %s",
733 					args[0].from);
734 				goto out;
735 			}
736 			info->check_integrity_print_mask = intarg;
737 			btrfs_warn(info,
738 	"integrity checker is deprecated and will be removed in 6.7");
739 			btrfs_info(info, "check_integrity_print_mask 0x%x",
740 				   info->check_integrity_print_mask);
741 			break;
742 #else
743 		case Opt_check_integrity_including_extent_data:
744 		case Opt_check_integrity:
745 		case Opt_check_integrity_print_mask:
746 			btrfs_err(info,
747 				  "support for check_integrity* not compiled in!");
748 			ret = -EINVAL;
749 			goto out;
750 #endif
751 		case Opt_fatal_errors:
752 			if (strcmp(args[0].from, "panic") == 0) {
753 				btrfs_set_opt(info->mount_opt,
754 					      PANIC_ON_FATAL_ERROR);
755 			} else if (strcmp(args[0].from, "bug") == 0) {
756 				btrfs_clear_opt(info->mount_opt,
757 					      PANIC_ON_FATAL_ERROR);
758 			} else {
759 				btrfs_err(info, "unrecognized fatal_errors value %s",
760 					  args[0].from);
761 				ret = -EINVAL;
762 				goto out;
763 			}
764 			break;
765 		case Opt_commit_interval:
766 			intarg = 0;
767 			ret = match_int(&args[0], &intarg);
768 			if (ret) {
769 				btrfs_err(info, "unrecognized commit_interval value %s",
770 					  args[0].from);
771 				ret = -EINVAL;
772 				goto out;
773 			}
774 			if (intarg == 0) {
775 				btrfs_info(info,
776 					   "using default commit interval %us",
777 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
778 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
779 			} else if (intarg > 300) {
780 				btrfs_warn(info, "excessive commit interval %d",
781 					   intarg);
782 			}
783 			info->commit_interval = intarg;
784 			break;
785 		case Opt_rescue:
786 			ret = parse_rescue_options(info, args[0].from);
787 			if (ret < 0) {
788 				btrfs_err(info, "unrecognized rescue value %s",
789 					  args[0].from);
790 				goto out;
791 			}
792 			break;
793 #ifdef CONFIG_BTRFS_DEBUG
794 		case Opt_fragment_all:
795 			btrfs_info(info, "fragmenting all space");
796 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
797 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
798 			break;
799 		case Opt_fragment_metadata:
800 			btrfs_info(info, "fragmenting metadata");
801 			btrfs_set_opt(info->mount_opt,
802 				      FRAGMENT_METADATA);
803 			break;
804 		case Opt_fragment_data:
805 			btrfs_info(info, "fragmenting data");
806 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
807 			break;
808 #endif
809 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
810 		case Opt_ref_verify:
811 			btrfs_info(info, "doing ref verification");
812 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
813 			break;
814 #endif
815 		case Opt_err:
816 			btrfs_err(info, "unrecognized mount option '%s'", p);
817 			ret = -EINVAL;
818 			goto out;
819 		default:
820 			break;
821 		}
822 	}
823 check:
824 	/* We're read-only, don't have to check. */
825 	if (new_flags & SB_RDONLY)
826 		goto out;
827 
828 	if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
829 	    check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
830 	    check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
831 		ret = -EINVAL;
832 out:
833 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
834 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
835 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
836 		btrfs_err(info, "cannot disable free space tree");
837 		ret = -EINVAL;
838 	}
839 	if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
840 	     !btrfs_test_opt(info, FREE_SPACE_TREE)) {
841 		btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
842 		ret = -EINVAL;
843 	}
844 	if (!ret)
845 		ret = btrfs_check_mountopts_zoned(info);
846 	if (!ret && !remounting) {
847 		if (btrfs_test_opt(info, SPACE_CACHE))
848 			btrfs_info(info, "disk space caching is enabled");
849 		if (btrfs_test_opt(info, FREE_SPACE_TREE))
850 			btrfs_info(info, "using free space tree");
851 	}
852 	return ret;
853 }
854 
855 /*
856  * Parse mount options that are required early in the mount process.
857  *
858  * All other options will be parsed on much later in the mount process and
859  * only when we need to allocate a new super block.
860  */
861 static int btrfs_parse_device_options(const char *options, blk_mode_t flags)
862 {
863 	substring_t args[MAX_OPT_ARGS];
864 	char *device_name, *opts, *orig, *p;
865 	struct btrfs_device *device = NULL;
866 	int error = 0;
867 
868 	lockdep_assert_held(&uuid_mutex);
869 
870 	if (!options)
871 		return 0;
872 
873 	/*
874 	 * strsep changes the string, duplicate it because btrfs_parse_options
875 	 * gets called later
876 	 */
877 	opts = kstrdup(options, GFP_KERNEL);
878 	if (!opts)
879 		return -ENOMEM;
880 	orig = opts;
881 
882 	while ((p = strsep(&opts, ",")) != NULL) {
883 		int token;
884 
885 		if (!*p)
886 			continue;
887 
888 		token = match_token(p, tokens, args);
889 		if (token == Opt_device) {
890 			device_name = match_strdup(&args[0]);
891 			if (!device_name) {
892 				error = -ENOMEM;
893 				goto out;
894 			}
895 			device = btrfs_scan_one_device(device_name, flags);
896 			kfree(device_name);
897 			if (IS_ERR(device)) {
898 				error = PTR_ERR(device);
899 				goto out;
900 			}
901 		}
902 	}
903 
904 out:
905 	kfree(orig);
906 	return error;
907 }
908 
909 /*
910  * Parse mount options that are related to subvolume id
911  *
912  * The value is later passed to mount_subvol()
913  */
914 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
915 		u64 *subvol_objectid)
916 {
917 	substring_t args[MAX_OPT_ARGS];
918 	char *opts, *orig, *p;
919 	int error = 0;
920 	u64 subvolid;
921 
922 	if (!options)
923 		return 0;
924 
925 	/*
926 	 * strsep changes the string, duplicate it because
927 	 * btrfs_parse_device_options gets called later
928 	 */
929 	opts = kstrdup(options, GFP_KERNEL);
930 	if (!opts)
931 		return -ENOMEM;
932 	orig = opts;
933 
934 	while ((p = strsep(&opts, ",")) != NULL) {
935 		int token;
936 		if (!*p)
937 			continue;
938 
939 		token = match_token(p, tokens, args);
940 		switch (token) {
941 		case Opt_subvol:
942 			kfree(*subvol_name);
943 			*subvol_name = match_strdup(&args[0]);
944 			if (!*subvol_name) {
945 				error = -ENOMEM;
946 				goto out;
947 			}
948 			break;
949 		case Opt_subvolid:
950 			error = match_u64(&args[0], &subvolid);
951 			if (error)
952 				goto out;
953 
954 			/* we want the original fs_tree */
955 			if (subvolid == 0)
956 				subvolid = BTRFS_FS_TREE_OBJECTID;
957 
958 			*subvol_objectid = subvolid;
959 			break;
960 		default:
961 			break;
962 		}
963 	}
964 
965 out:
966 	kfree(orig);
967 	return error;
968 }
969 
970 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
971 					  u64 subvol_objectid)
972 {
973 	struct btrfs_root *root = fs_info->tree_root;
974 	struct btrfs_root *fs_root = NULL;
975 	struct btrfs_root_ref *root_ref;
976 	struct btrfs_inode_ref *inode_ref;
977 	struct btrfs_key key;
978 	struct btrfs_path *path = NULL;
979 	char *name = NULL, *ptr;
980 	u64 dirid;
981 	int len;
982 	int ret;
983 
984 	path = btrfs_alloc_path();
985 	if (!path) {
986 		ret = -ENOMEM;
987 		goto err;
988 	}
989 
990 	name = kmalloc(PATH_MAX, GFP_KERNEL);
991 	if (!name) {
992 		ret = -ENOMEM;
993 		goto err;
994 	}
995 	ptr = name + PATH_MAX - 1;
996 	ptr[0] = '\0';
997 
998 	/*
999 	 * Walk up the subvolume trees in the tree of tree roots by root
1000 	 * backrefs until we hit the top-level subvolume.
1001 	 */
1002 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1003 		key.objectid = subvol_objectid;
1004 		key.type = BTRFS_ROOT_BACKREF_KEY;
1005 		key.offset = (u64)-1;
1006 
1007 		ret = btrfs_search_backwards(root, &key, path);
1008 		if (ret < 0) {
1009 			goto err;
1010 		} else if (ret > 0) {
1011 			ret = -ENOENT;
1012 			goto err;
1013 		}
1014 
1015 		subvol_objectid = key.offset;
1016 
1017 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1018 					  struct btrfs_root_ref);
1019 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1020 		ptr -= len + 1;
1021 		if (ptr < name) {
1022 			ret = -ENAMETOOLONG;
1023 			goto err;
1024 		}
1025 		read_extent_buffer(path->nodes[0], ptr + 1,
1026 				   (unsigned long)(root_ref + 1), len);
1027 		ptr[0] = '/';
1028 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1029 		btrfs_release_path(path);
1030 
1031 		fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1032 		if (IS_ERR(fs_root)) {
1033 			ret = PTR_ERR(fs_root);
1034 			fs_root = NULL;
1035 			goto err;
1036 		}
1037 
1038 		/*
1039 		 * Walk up the filesystem tree by inode refs until we hit the
1040 		 * root directory.
1041 		 */
1042 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1043 			key.objectid = dirid;
1044 			key.type = BTRFS_INODE_REF_KEY;
1045 			key.offset = (u64)-1;
1046 
1047 			ret = btrfs_search_backwards(fs_root, &key, path);
1048 			if (ret < 0) {
1049 				goto err;
1050 			} else if (ret > 0) {
1051 				ret = -ENOENT;
1052 				goto err;
1053 			}
1054 
1055 			dirid = key.offset;
1056 
1057 			inode_ref = btrfs_item_ptr(path->nodes[0],
1058 						   path->slots[0],
1059 						   struct btrfs_inode_ref);
1060 			len = btrfs_inode_ref_name_len(path->nodes[0],
1061 						       inode_ref);
1062 			ptr -= len + 1;
1063 			if (ptr < name) {
1064 				ret = -ENAMETOOLONG;
1065 				goto err;
1066 			}
1067 			read_extent_buffer(path->nodes[0], ptr + 1,
1068 					   (unsigned long)(inode_ref + 1), len);
1069 			ptr[0] = '/';
1070 			btrfs_release_path(path);
1071 		}
1072 		btrfs_put_root(fs_root);
1073 		fs_root = NULL;
1074 	}
1075 
1076 	btrfs_free_path(path);
1077 	if (ptr == name + PATH_MAX - 1) {
1078 		name[0] = '/';
1079 		name[1] = '\0';
1080 	} else {
1081 		memmove(name, ptr, name + PATH_MAX - ptr);
1082 	}
1083 	return name;
1084 
1085 err:
1086 	btrfs_put_root(fs_root);
1087 	btrfs_free_path(path);
1088 	kfree(name);
1089 	return ERR_PTR(ret);
1090 }
1091 
1092 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1093 {
1094 	struct btrfs_root *root = fs_info->tree_root;
1095 	struct btrfs_dir_item *di;
1096 	struct btrfs_path *path;
1097 	struct btrfs_key location;
1098 	struct fscrypt_str name = FSTR_INIT("default", 7);
1099 	u64 dir_id;
1100 
1101 	path = btrfs_alloc_path();
1102 	if (!path)
1103 		return -ENOMEM;
1104 
1105 	/*
1106 	 * Find the "default" dir item which points to the root item that we
1107 	 * will mount by default if we haven't been given a specific subvolume
1108 	 * to mount.
1109 	 */
1110 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
1111 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
1112 	if (IS_ERR(di)) {
1113 		btrfs_free_path(path);
1114 		return PTR_ERR(di);
1115 	}
1116 	if (!di) {
1117 		/*
1118 		 * Ok the default dir item isn't there.  This is weird since
1119 		 * it's always been there, but don't freak out, just try and
1120 		 * mount the top-level subvolume.
1121 		 */
1122 		btrfs_free_path(path);
1123 		*objectid = BTRFS_FS_TREE_OBJECTID;
1124 		return 0;
1125 	}
1126 
1127 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1128 	btrfs_free_path(path);
1129 	*objectid = location.objectid;
1130 	return 0;
1131 }
1132 
1133 static int btrfs_fill_super(struct super_block *sb,
1134 			    struct btrfs_fs_devices *fs_devices,
1135 			    void *data)
1136 {
1137 	struct inode *inode;
1138 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1139 	int err;
1140 
1141 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1142 	sb->s_magic = BTRFS_SUPER_MAGIC;
1143 	sb->s_op = &btrfs_super_ops;
1144 	sb->s_d_op = &btrfs_dentry_operations;
1145 	sb->s_export_op = &btrfs_export_ops;
1146 #ifdef CONFIG_FS_VERITY
1147 	sb->s_vop = &btrfs_verityops;
1148 #endif
1149 	sb->s_xattr = btrfs_xattr_handlers;
1150 	sb->s_time_gran = 1;
1151 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1152 	sb->s_flags |= SB_POSIXACL;
1153 #endif
1154 	sb->s_flags |= SB_I_VERSION;
1155 	sb->s_iflags |= SB_I_CGROUPWB;
1156 
1157 	err = super_setup_bdi(sb);
1158 	if (err) {
1159 		btrfs_err(fs_info, "super_setup_bdi failed");
1160 		return err;
1161 	}
1162 
1163 	err = open_ctree(sb, fs_devices, (char *)data);
1164 	if (err) {
1165 		btrfs_err(fs_info, "open_ctree failed");
1166 		return err;
1167 	}
1168 
1169 	inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1170 	if (IS_ERR(inode)) {
1171 		err = PTR_ERR(inode);
1172 		btrfs_handle_fs_error(fs_info, err, NULL);
1173 		goto fail_close;
1174 	}
1175 
1176 	sb->s_root = d_make_root(inode);
1177 	if (!sb->s_root) {
1178 		err = -ENOMEM;
1179 		goto fail_close;
1180 	}
1181 
1182 	sb->s_flags |= SB_ACTIVE;
1183 	return 0;
1184 
1185 fail_close:
1186 	close_ctree(fs_info);
1187 	return err;
1188 }
1189 
1190 int btrfs_sync_fs(struct super_block *sb, int wait)
1191 {
1192 	struct btrfs_trans_handle *trans;
1193 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1194 	struct btrfs_root *root = fs_info->tree_root;
1195 
1196 	trace_btrfs_sync_fs(fs_info, wait);
1197 
1198 	if (!wait) {
1199 		filemap_flush(fs_info->btree_inode->i_mapping);
1200 		return 0;
1201 	}
1202 
1203 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1204 
1205 	trans = btrfs_attach_transaction_barrier(root);
1206 	if (IS_ERR(trans)) {
1207 		/* no transaction, don't bother */
1208 		if (PTR_ERR(trans) == -ENOENT) {
1209 			/*
1210 			 * Exit unless we have some pending changes
1211 			 * that need to go through commit
1212 			 */
1213 			if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
1214 				      &fs_info->flags))
1215 				return 0;
1216 			/*
1217 			 * A non-blocking test if the fs is frozen. We must not
1218 			 * start a new transaction here otherwise a deadlock
1219 			 * happens. The pending operations are delayed to the
1220 			 * next commit after thawing.
1221 			 */
1222 			if (sb_start_write_trylock(sb))
1223 				sb_end_write(sb);
1224 			else
1225 				return 0;
1226 			trans = btrfs_start_transaction(root, 0);
1227 		}
1228 		if (IS_ERR(trans))
1229 			return PTR_ERR(trans);
1230 	}
1231 	return btrfs_commit_transaction(trans);
1232 }
1233 
1234 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1235 {
1236 	seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1237 	*printed = true;
1238 }
1239 
1240 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1241 {
1242 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1243 	const char *compress_type;
1244 	const char *subvol_name;
1245 	bool printed = false;
1246 
1247 	if (btrfs_test_opt(info, DEGRADED))
1248 		seq_puts(seq, ",degraded");
1249 	if (btrfs_test_opt(info, NODATASUM))
1250 		seq_puts(seq, ",nodatasum");
1251 	if (btrfs_test_opt(info, NODATACOW))
1252 		seq_puts(seq, ",nodatacow");
1253 	if (btrfs_test_opt(info, NOBARRIER))
1254 		seq_puts(seq, ",nobarrier");
1255 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1256 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1257 	if (info->thread_pool_size !=  min_t(unsigned long,
1258 					     num_online_cpus() + 2, 8))
1259 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1260 	if (btrfs_test_opt(info, COMPRESS)) {
1261 		compress_type = btrfs_compress_type2str(info->compress_type);
1262 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1263 			seq_printf(seq, ",compress-force=%s", compress_type);
1264 		else
1265 			seq_printf(seq, ",compress=%s", compress_type);
1266 		if (info->compress_level)
1267 			seq_printf(seq, ":%d", info->compress_level);
1268 	}
1269 	if (btrfs_test_opt(info, NOSSD))
1270 		seq_puts(seq, ",nossd");
1271 	if (btrfs_test_opt(info, SSD_SPREAD))
1272 		seq_puts(seq, ",ssd_spread");
1273 	else if (btrfs_test_opt(info, SSD))
1274 		seq_puts(seq, ",ssd");
1275 	if (btrfs_test_opt(info, NOTREELOG))
1276 		seq_puts(seq, ",notreelog");
1277 	if (btrfs_test_opt(info, NOLOGREPLAY))
1278 		print_rescue_option(seq, "nologreplay", &printed);
1279 	if (btrfs_test_opt(info, USEBACKUPROOT))
1280 		print_rescue_option(seq, "usebackuproot", &printed);
1281 	if (btrfs_test_opt(info, IGNOREBADROOTS))
1282 		print_rescue_option(seq, "ignorebadroots", &printed);
1283 	if (btrfs_test_opt(info, IGNOREDATACSUMS))
1284 		print_rescue_option(seq, "ignoredatacsums", &printed);
1285 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
1286 		seq_puts(seq, ",flushoncommit");
1287 	if (btrfs_test_opt(info, DISCARD_SYNC))
1288 		seq_puts(seq, ",discard");
1289 	if (btrfs_test_opt(info, DISCARD_ASYNC))
1290 		seq_puts(seq, ",discard=async");
1291 	if (!(info->sb->s_flags & SB_POSIXACL))
1292 		seq_puts(seq, ",noacl");
1293 	if (btrfs_free_space_cache_v1_active(info))
1294 		seq_puts(seq, ",space_cache");
1295 	else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
1296 		seq_puts(seq, ",space_cache=v2");
1297 	else
1298 		seq_puts(seq, ",nospace_cache");
1299 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1300 		seq_puts(seq, ",rescan_uuid_tree");
1301 	if (btrfs_test_opt(info, CLEAR_CACHE))
1302 		seq_puts(seq, ",clear_cache");
1303 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1304 		seq_puts(seq, ",user_subvol_rm_allowed");
1305 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
1306 		seq_puts(seq, ",enospc_debug");
1307 	if (btrfs_test_opt(info, AUTO_DEFRAG))
1308 		seq_puts(seq, ",autodefrag");
1309 	if (btrfs_test_opt(info, SKIP_BALANCE))
1310 		seq_puts(seq, ",skip_balance");
1311 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1312 	if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
1313 		seq_puts(seq, ",check_int_data");
1314 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1315 		seq_puts(seq, ",check_int");
1316 	if (info->check_integrity_print_mask)
1317 		seq_printf(seq, ",check_int_print_mask=%d",
1318 				info->check_integrity_print_mask);
1319 #endif
1320 	if (info->metadata_ratio)
1321 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1322 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1323 		seq_puts(seq, ",fatal_errors=panic");
1324 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1325 		seq_printf(seq, ",commit=%u", info->commit_interval);
1326 #ifdef CONFIG_BTRFS_DEBUG
1327 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1328 		seq_puts(seq, ",fragment=data");
1329 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1330 		seq_puts(seq, ",fragment=metadata");
1331 #endif
1332 	if (btrfs_test_opt(info, REF_VERIFY))
1333 		seq_puts(seq, ",ref_verify");
1334 	seq_printf(seq, ",subvolid=%llu",
1335 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1336 	subvol_name = btrfs_get_subvol_name_from_objectid(info,
1337 			BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1338 	if (!IS_ERR(subvol_name)) {
1339 		seq_puts(seq, ",subvol=");
1340 		seq_escape(seq, subvol_name, " \t\n\\");
1341 		kfree(subvol_name);
1342 	}
1343 	return 0;
1344 }
1345 
1346 static int btrfs_test_super(struct super_block *s, void *data)
1347 {
1348 	struct btrfs_fs_info *p = data;
1349 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
1350 
1351 	return fs_info->fs_devices == p->fs_devices;
1352 }
1353 
1354 static int btrfs_set_super(struct super_block *s, void *data)
1355 {
1356 	int err = set_anon_super(s, data);
1357 	if (!err)
1358 		s->s_fs_info = data;
1359 	return err;
1360 }
1361 
1362 /*
1363  * subvolumes are identified by ino 256
1364  */
1365 static inline int is_subvolume_inode(struct inode *inode)
1366 {
1367 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1368 		return 1;
1369 	return 0;
1370 }
1371 
1372 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1373 				   struct vfsmount *mnt)
1374 {
1375 	struct dentry *root;
1376 	int ret;
1377 
1378 	if (!subvol_name) {
1379 		if (!subvol_objectid) {
1380 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1381 							  &subvol_objectid);
1382 			if (ret) {
1383 				root = ERR_PTR(ret);
1384 				goto out;
1385 			}
1386 		}
1387 		subvol_name = btrfs_get_subvol_name_from_objectid(
1388 					btrfs_sb(mnt->mnt_sb), subvol_objectid);
1389 		if (IS_ERR(subvol_name)) {
1390 			root = ERR_CAST(subvol_name);
1391 			subvol_name = NULL;
1392 			goto out;
1393 		}
1394 
1395 	}
1396 
1397 	root = mount_subtree(mnt, subvol_name);
1398 	/* mount_subtree() drops our reference on the vfsmount. */
1399 	mnt = NULL;
1400 
1401 	if (!IS_ERR(root)) {
1402 		struct super_block *s = root->d_sb;
1403 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1404 		struct inode *root_inode = d_inode(root);
1405 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1406 
1407 		ret = 0;
1408 		if (!is_subvolume_inode(root_inode)) {
1409 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1410 			       subvol_name);
1411 			ret = -EINVAL;
1412 		}
1413 		if (subvol_objectid && root_objectid != subvol_objectid) {
1414 			/*
1415 			 * This will also catch a race condition where a
1416 			 * subvolume which was passed by ID is renamed and
1417 			 * another subvolume is renamed over the old location.
1418 			 */
1419 			btrfs_err(fs_info,
1420 				  "subvol '%s' does not match subvolid %llu",
1421 				  subvol_name, subvol_objectid);
1422 			ret = -EINVAL;
1423 		}
1424 		if (ret) {
1425 			dput(root);
1426 			root = ERR_PTR(ret);
1427 			deactivate_locked_super(s);
1428 		}
1429 	}
1430 
1431 out:
1432 	mntput(mnt);
1433 	kfree(subvol_name);
1434 	return root;
1435 }
1436 
1437 /*
1438  * Find a superblock for the given device / mount point.
1439  *
1440  * Note: This is based on mount_bdev from fs/super.c with a few additions
1441  *       for multiple device setup.  Make sure to keep it in sync.
1442  */
1443 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1444 		int flags, const char *device_name, void *data)
1445 {
1446 	struct block_device *bdev = NULL;
1447 	struct super_block *s;
1448 	struct btrfs_device *device = NULL;
1449 	struct btrfs_fs_devices *fs_devices = NULL;
1450 	struct btrfs_fs_info *fs_info = NULL;
1451 	void *new_sec_opts = NULL;
1452 	blk_mode_t mode = sb_open_mode(flags);
1453 	int error = 0;
1454 
1455 	if (data) {
1456 		error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1457 		if (error)
1458 			return ERR_PTR(error);
1459 	}
1460 
1461 	/*
1462 	 * Setup a dummy root and fs_info for test/set super.  This is because
1463 	 * we don't actually fill this stuff out until open_ctree, but we need
1464 	 * then open_ctree will properly initialize the file system specific
1465 	 * settings later.  btrfs_init_fs_info initializes the static elements
1466 	 * of the fs_info (locks and such) to make cleanup easier if we find a
1467 	 * superblock with our given fs_devices later on at sget() time.
1468 	 */
1469 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1470 	if (!fs_info) {
1471 		error = -ENOMEM;
1472 		goto error_sec_opts;
1473 	}
1474 	btrfs_init_fs_info(fs_info);
1475 
1476 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1477 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1478 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
1479 		error = -ENOMEM;
1480 		goto error_fs_info;
1481 	}
1482 
1483 	mutex_lock(&uuid_mutex);
1484 	error = btrfs_parse_device_options(data, mode);
1485 	if (error) {
1486 		mutex_unlock(&uuid_mutex);
1487 		goto error_fs_info;
1488 	}
1489 
1490 	device = btrfs_scan_one_device(device_name, mode);
1491 	if (IS_ERR(device)) {
1492 		mutex_unlock(&uuid_mutex);
1493 		error = PTR_ERR(device);
1494 		goto error_fs_info;
1495 	}
1496 
1497 	fs_devices = device->fs_devices;
1498 	fs_info->fs_devices = fs_devices;
1499 
1500 	error = btrfs_open_devices(fs_devices, mode, fs_type);
1501 	mutex_unlock(&uuid_mutex);
1502 	if (error)
1503 		goto error_fs_info;
1504 
1505 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1506 		error = -EACCES;
1507 		goto error_close_devices;
1508 	}
1509 
1510 	bdev = fs_devices->latest_dev->bdev;
1511 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1512 		 fs_info);
1513 	if (IS_ERR(s)) {
1514 		error = PTR_ERR(s);
1515 		goto error_close_devices;
1516 	}
1517 
1518 	if (s->s_root) {
1519 		btrfs_close_devices(fs_devices);
1520 		btrfs_free_fs_info(fs_info);
1521 		if ((flags ^ s->s_flags) & SB_RDONLY)
1522 			error = -EBUSY;
1523 	} else {
1524 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1525 		shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1526 					s->s_id);
1527 		btrfs_sb(s)->bdev_holder = fs_type;
1528 		error = btrfs_fill_super(s, fs_devices, data);
1529 	}
1530 	if (!error)
1531 		error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1532 	security_free_mnt_opts(&new_sec_opts);
1533 	if (error) {
1534 		deactivate_locked_super(s);
1535 		return ERR_PTR(error);
1536 	}
1537 
1538 	return dget(s->s_root);
1539 
1540 error_close_devices:
1541 	btrfs_close_devices(fs_devices);
1542 error_fs_info:
1543 	btrfs_free_fs_info(fs_info);
1544 error_sec_opts:
1545 	security_free_mnt_opts(&new_sec_opts);
1546 	return ERR_PTR(error);
1547 }
1548 
1549 /*
1550  * Mount function which is called by VFS layer.
1551  *
1552  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1553  * which needs vfsmount* of device's root (/).  This means device's root has to
1554  * be mounted internally in any case.
1555  *
1556  * Operation flow:
1557  *   1. Parse subvol id related options for later use in mount_subvol().
1558  *
1559  *   2. Mount device's root (/) by calling vfs_kern_mount().
1560  *
1561  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1562  *      first place. In order to avoid calling btrfs_mount() again, we use
1563  *      different file_system_type which is not registered to VFS by
1564  *      register_filesystem() (btrfs_root_fs_type). As a result,
1565  *      btrfs_mount_root() is called. The return value will be used by
1566  *      mount_subtree() in mount_subvol().
1567  *
1568  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1569  *      "btrfs subvolume set-default", mount_subvol() is called always.
1570  */
1571 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1572 		const char *device_name, void *data)
1573 {
1574 	struct vfsmount *mnt_root;
1575 	struct dentry *root;
1576 	char *subvol_name = NULL;
1577 	u64 subvol_objectid = 0;
1578 	int error = 0;
1579 
1580 	error = btrfs_parse_subvol_options(data, &subvol_name,
1581 					&subvol_objectid);
1582 	if (error) {
1583 		kfree(subvol_name);
1584 		return ERR_PTR(error);
1585 	}
1586 
1587 	/* mount device's root (/) */
1588 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1589 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1590 		if (flags & SB_RDONLY) {
1591 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1592 				flags & ~SB_RDONLY, device_name, data);
1593 		} else {
1594 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1595 				flags | SB_RDONLY, device_name, data);
1596 			if (IS_ERR(mnt_root)) {
1597 				root = ERR_CAST(mnt_root);
1598 				kfree(subvol_name);
1599 				goto out;
1600 			}
1601 
1602 			down_write(&mnt_root->mnt_sb->s_umount);
1603 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1604 			up_write(&mnt_root->mnt_sb->s_umount);
1605 			if (error < 0) {
1606 				root = ERR_PTR(error);
1607 				mntput(mnt_root);
1608 				kfree(subvol_name);
1609 				goto out;
1610 			}
1611 		}
1612 	}
1613 	if (IS_ERR(mnt_root)) {
1614 		root = ERR_CAST(mnt_root);
1615 		kfree(subvol_name);
1616 		goto out;
1617 	}
1618 
1619 	/* mount_subvol() will free subvol_name and mnt_root */
1620 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1621 
1622 out:
1623 	return root;
1624 }
1625 
1626 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1627 				     u32 new_pool_size, u32 old_pool_size)
1628 {
1629 	if (new_pool_size == old_pool_size)
1630 		return;
1631 
1632 	fs_info->thread_pool_size = new_pool_size;
1633 
1634 	btrfs_info(fs_info, "resize thread pool %d -> %d",
1635 	       old_pool_size, new_pool_size);
1636 
1637 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1638 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1639 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1640 	workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1641 	workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1642 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1643 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1644 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1645 }
1646 
1647 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1648 				       unsigned long old_opts, int flags)
1649 {
1650 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1651 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1652 	     (flags & SB_RDONLY))) {
1653 		/* wait for any defraggers to finish */
1654 		wait_event(fs_info->transaction_wait,
1655 			   (atomic_read(&fs_info->defrag_running) == 0));
1656 		if (flags & SB_RDONLY)
1657 			sync_filesystem(fs_info->sb);
1658 	}
1659 }
1660 
1661 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1662 					 unsigned long old_opts)
1663 {
1664 	const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1665 
1666 	/*
1667 	 * We need to cleanup all defragable inodes if the autodefragment is
1668 	 * close or the filesystem is read only.
1669 	 */
1670 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1671 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1672 		btrfs_cleanup_defrag_inodes(fs_info);
1673 	}
1674 
1675 	/* If we toggled discard async */
1676 	if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1677 	    btrfs_test_opt(fs_info, DISCARD_ASYNC))
1678 		btrfs_discard_resume(fs_info);
1679 	else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1680 		 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1681 		btrfs_discard_cleanup(fs_info);
1682 
1683 	/* If we toggled space cache */
1684 	if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1685 		btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1686 }
1687 
1688 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1689 {
1690 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1691 	unsigned old_flags = sb->s_flags;
1692 	unsigned long old_opts = fs_info->mount_opt;
1693 	unsigned long old_compress_type = fs_info->compress_type;
1694 	u64 old_max_inline = fs_info->max_inline;
1695 	u32 old_thread_pool_size = fs_info->thread_pool_size;
1696 	u32 old_metadata_ratio = fs_info->metadata_ratio;
1697 	int ret;
1698 
1699 	sync_filesystem(sb);
1700 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1701 
1702 	if (data) {
1703 		void *new_sec_opts = NULL;
1704 
1705 		ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1706 		if (!ret)
1707 			ret = security_sb_remount(sb, new_sec_opts);
1708 		security_free_mnt_opts(&new_sec_opts);
1709 		if (ret)
1710 			goto restore;
1711 	}
1712 
1713 	ret = btrfs_parse_options(fs_info, data, *flags);
1714 	if (ret)
1715 		goto restore;
1716 
1717 	ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
1718 	if (ret < 0)
1719 		goto restore;
1720 
1721 	btrfs_remount_begin(fs_info, old_opts, *flags);
1722 	btrfs_resize_thread_pool(fs_info,
1723 		fs_info->thread_pool_size, old_thread_pool_size);
1724 
1725 	if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
1726 	    (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
1727 	    (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
1728 		btrfs_warn(fs_info,
1729 		"remount supports changing free space tree only from ro to rw");
1730 		/* Make sure free space cache options match the state on disk */
1731 		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1732 			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1733 			btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
1734 		}
1735 		if (btrfs_free_space_cache_v1_active(fs_info)) {
1736 			btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
1737 			btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
1738 		}
1739 	}
1740 
1741 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1742 		goto out;
1743 
1744 	if (*flags & SB_RDONLY) {
1745 		/*
1746 		 * this also happens on 'umount -rf' or on shutdown, when
1747 		 * the filesystem is busy.
1748 		 */
1749 		cancel_work_sync(&fs_info->async_reclaim_work);
1750 		cancel_work_sync(&fs_info->async_data_reclaim_work);
1751 
1752 		btrfs_discard_cleanup(fs_info);
1753 
1754 		/* wait for the uuid_scan task to finish */
1755 		down(&fs_info->uuid_tree_rescan_sem);
1756 		/* avoid complains from lockdep et al. */
1757 		up(&fs_info->uuid_tree_rescan_sem);
1758 
1759 		btrfs_set_sb_rdonly(sb);
1760 
1761 		/*
1762 		 * Setting SB_RDONLY will put the cleaner thread to
1763 		 * sleep at the next loop if it's already active.
1764 		 * If it's already asleep, we'll leave unused block
1765 		 * groups on disk until we're mounted read-write again
1766 		 * unless we clean them up here.
1767 		 */
1768 		btrfs_delete_unused_bgs(fs_info);
1769 
1770 		/*
1771 		 * The cleaner task could be already running before we set the
1772 		 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
1773 		 * We must make sure that after we finish the remount, i.e. after
1774 		 * we call btrfs_commit_super(), the cleaner can no longer start
1775 		 * a transaction - either because it was dropping a dead root,
1776 		 * running delayed iputs or deleting an unused block group (the
1777 		 * cleaner picked a block group from the list of unused block
1778 		 * groups before we were able to in the previous call to
1779 		 * btrfs_delete_unused_bgs()).
1780 		 */
1781 		wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
1782 			    TASK_UNINTERRUPTIBLE);
1783 
1784 		/*
1785 		 * We've set the superblock to RO mode, so we might have made
1786 		 * the cleaner task sleep without running all pending delayed
1787 		 * iputs. Go through all the delayed iputs here, so that if an
1788 		 * unmount happens without remounting RW we don't end up at
1789 		 * finishing close_ctree() with a non-empty list of delayed
1790 		 * iputs.
1791 		 */
1792 		btrfs_run_delayed_iputs(fs_info);
1793 
1794 		btrfs_dev_replace_suspend_for_unmount(fs_info);
1795 		btrfs_scrub_cancel(fs_info);
1796 		btrfs_pause_balance(fs_info);
1797 
1798 		/*
1799 		 * Pause the qgroup rescan worker if it is running. We don't want
1800 		 * it to be still running after we are in RO mode, as after that,
1801 		 * by the time we unmount, it might have left a transaction open,
1802 		 * so we would leak the transaction and/or crash.
1803 		 */
1804 		btrfs_qgroup_wait_for_completion(fs_info, false);
1805 
1806 		ret = btrfs_commit_super(fs_info);
1807 		if (ret)
1808 			goto restore;
1809 	} else {
1810 		if (BTRFS_FS_ERROR(fs_info)) {
1811 			btrfs_err(fs_info,
1812 				"Remounting read-write after error is not allowed");
1813 			ret = -EINVAL;
1814 			goto restore;
1815 		}
1816 		if (fs_info->fs_devices->rw_devices == 0) {
1817 			ret = -EACCES;
1818 			goto restore;
1819 		}
1820 
1821 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1822 			btrfs_warn(fs_info,
1823 		"too many missing devices, writable remount is not allowed");
1824 			ret = -EACCES;
1825 			goto restore;
1826 		}
1827 
1828 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1829 			btrfs_warn(fs_info,
1830 		"mount required to replay tree-log, cannot remount read-write");
1831 			ret = -EINVAL;
1832 			goto restore;
1833 		}
1834 
1835 		/*
1836 		 * NOTE: when remounting with a change that does writes, don't
1837 		 * put it anywhere above this point, as we are not sure to be
1838 		 * safe to write until we pass the above checks.
1839 		 */
1840 		ret = btrfs_start_pre_rw_mount(fs_info);
1841 		if (ret)
1842 			goto restore;
1843 
1844 		btrfs_clear_sb_rdonly(sb);
1845 
1846 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1847 
1848 		/*
1849 		 * If we've gone from readonly -> read/write, we need to get
1850 		 * our sync/async discard lists in the right state.
1851 		 */
1852 		btrfs_discard_resume(fs_info);
1853 	}
1854 out:
1855 	/*
1856 	 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1857 	 * since the absence of the flag means it can be toggled off by remount.
1858 	 */
1859 	*flags |= SB_I_VERSION;
1860 
1861 	wake_up_process(fs_info->transaction_kthread);
1862 	btrfs_remount_cleanup(fs_info, old_opts);
1863 	btrfs_clear_oneshot_options(fs_info);
1864 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1865 
1866 	return 0;
1867 
1868 restore:
1869 	/* We've hit an error - don't reset SB_RDONLY */
1870 	if (sb_rdonly(sb))
1871 		old_flags |= SB_RDONLY;
1872 	if (!(old_flags & SB_RDONLY))
1873 		clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
1874 	sb->s_flags = old_flags;
1875 	fs_info->mount_opt = old_opts;
1876 	fs_info->compress_type = old_compress_type;
1877 	fs_info->max_inline = old_max_inline;
1878 	btrfs_resize_thread_pool(fs_info,
1879 		old_thread_pool_size, fs_info->thread_pool_size);
1880 	fs_info->metadata_ratio = old_metadata_ratio;
1881 	btrfs_remount_cleanup(fs_info, old_opts);
1882 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1883 
1884 	return ret;
1885 }
1886 
1887 /* Used to sort the devices by max_avail(descending sort) */
1888 static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
1889 {
1890 	const struct btrfs_device_info *dev_info1 = a;
1891 	const struct btrfs_device_info *dev_info2 = b;
1892 
1893 	if (dev_info1->max_avail > dev_info2->max_avail)
1894 		return -1;
1895 	else if (dev_info1->max_avail < dev_info2->max_avail)
1896 		return 1;
1897 	return 0;
1898 }
1899 
1900 /*
1901  * sort the devices by max_avail, in which max free extent size of each device
1902  * is stored.(Descending Sort)
1903  */
1904 static inline void btrfs_descending_sort_devices(
1905 					struct btrfs_device_info *devices,
1906 					size_t nr_devices)
1907 {
1908 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1909 	     btrfs_cmp_device_free_bytes, NULL);
1910 }
1911 
1912 /*
1913  * The helper to calc the free space on the devices that can be used to store
1914  * file data.
1915  */
1916 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
1917 					      u64 *free_bytes)
1918 {
1919 	struct btrfs_device_info *devices_info;
1920 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1921 	struct btrfs_device *device;
1922 	u64 type;
1923 	u64 avail_space;
1924 	u64 min_stripe_size;
1925 	int num_stripes = 1;
1926 	int i = 0, nr_devices;
1927 	const struct btrfs_raid_attr *rattr;
1928 
1929 	/*
1930 	 * We aren't under the device list lock, so this is racy-ish, but good
1931 	 * enough for our purposes.
1932 	 */
1933 	nr_devices = fs_info->fs_devices->open_devices;
1934 	if (!nr_devices) {
1935 		smp_mb();
1936 		nr_devices = fs_info->fs_devices->open_devices;
1937 		ASSERT(nr_devices);
1938 		if (!nr_devices) {
1939 			*free_bytes = 0;
1940 			return 0;
1941 		}
1942 	}
1943 
1944 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
1945 			       GFP_KERNEL);
1946 	if (!devices_info)
1947 		return -ENOMEM;
1948 
1949 	/* calc min stripe number for data space allocation */
1950 	type = btrfs_data_alloc_profile(fs_info);
1951 	rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
1952 
1953 	if (type & BTRFS_BLOCK_GROUP_RAID0)
1954 		num_stripes = nr_devices;
1955 	else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
1956 		num_stripes = rattr->ncopies;
1957 	else if (type & BTRFS_BLOCK_GROUP_RAID10)
1958 		num_stripes = 4;
1959 
1960 	/* Adjust for more than 1 stripe per device */
1961 	min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
1962 
1963 	rcu_read_lock();
1964 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1965 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1966 						&device->dev_state) ||
1967 		    !device->bdev ||
1968 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1969 			continue;
1970 
1971 		if (i >= nr_devices)
1972 			break;
1973 
1974 		avail_space = device->total_bytes - device->bytes_used;
1975 
1976 		/* align with stripe_len */
1977 		avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
1978 
1979 		/*
1980 		 * Ensure we have at least min_stripe_size on top of the
1981 		 * reserved space on the device.
1982 		 */
1983 		if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
1984 			continue;
1985 
1986 		avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
1987 
1988 		devices_info[i].dev = device;
1989 		devices_info[i].max_avail = avail_space;
1990 
1991 		i++;
1992 	}
1993 	rcu_read_unlock();
1994 
1995 	nr_devices = i;
1996 
1997 	btrfs_descending_sort_devices(devices_info, nr_devices);
1998 
1999 	i = nr_devices - 1;
2000 	avail_space = 0;
2001 	while (nr_devices >= rattr->devs_min) {
2002 		num_stripes = min(num_stripes, nr_devices);
2003 
2004 		if (devices_info[i].max_avail >= min_stripe_size) {
2005 			int j;
2006 			u64 alloc_size;
2007 
2008 			avail_space += devices_info[i].max_avail * num_stripes;
2009 			alloc_size = devices_info[i].max_avail;
2010 			for (j = i + 1 - num_stripes; j <= i; j++)
2011 				devices_info[j].max_avail -= alloc_size;
2012 		}
2013 		i--;
2014 		nr_devices--;
2015 	}
2016 
2017 	kfree(devices_info);
2018 	*free_bytes = avail_space;
2019 	return 0;
2020 }
2021 
2022 /*
2023  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2024  *
2025  * If there's a redundant raid level at DATA block groups, use the respective
2026  * multiplier to scale the sizes.
2027  *
2028  * Unused device space usage is based on simulating the chunk allocator
2029  * algorithm that respects the device sizes and order of allocations.  This is
2030  * a close approximation of the actual use but there are other factors that may
2031  * change the result (like a new metadata chunk).
2032  *
2033  * If metadata is exhausted, f_bavail will be 0.
2034  */
2035 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2036 {
2037 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2038 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2039 	struct btrfs_space_info *found;
2040 	u64 total_used = 0;
2041 	u64 total_free_data = 0;
2042 	u64 total_free_meta = 0;
2043 	u32 bits = fs_info->sectorsize_bits;
2044 	__be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2045 	unsigned factor = 1;
2046 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2047 	int ret;
2048 	u64 thresh = 0;
2049 	int mixed = 0;
2050 
2051 	list_for_each_entry(found, &fs_info->space_info, list) {
2052 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2053 			int i;
2054 
2055 			total_free_data += found->disk_total - found->disk_used;
2056 			total_free_data -=
2057 				btrfs_account_ro_block_groups_free_space(found);
2058 
2059 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2060 				if (!list_empty(&found->block_groups[i]))
2061 					factor = btrfs_bg_type_to_factor(
2062 						btrfs_raid_array[i].bg_flag);
2063 			}
2064 		}
2065 
2066 		/*
2067 		 * Metadata in mixed block group profiles are accounted in data
2068 		 */
2069 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2070 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2071 				mixed = 1;
2072 			else
2073 				total_free_meta += found->disk_total -
2074 					found->disk_used;
2075 		}
2076 
2077 		total_used += found->disk_used;
2078 	}
2079 
2080 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2081 	buf->f_blocks >>= bits;
2082 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2083 
2084 	/* Account global block reserve as used, it's in logical size already */
2085 	spin_lock(&block_rsv->lock);
2086 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
2087 	if (buf->f_bfree >= block_rsv->size >> bits)
2088 		buf->f_bfree -= block_rsv->size >> bits;
2089 	else
2090 		buf->f_bfree = 0;
2091 	spin_unlock(&block_rsv->lock);
2092 
2093 	buf->f_bavail = div_u64(total_free_data, factor);
2094 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2095 	if (ret)
2096 		return ret;
2097 	buf->f_bavail += div_u64(total_free_data, factor);
2098 	buf->f_bavail = buf->f_bavail >> bits;
2099 
2100 	/*
2101 	 * We calculate the remaining metadata space minus global reserve. If
2102 	 * this is (supposedly) smaller than zero, there's no space. But this
2103 	 * does not hold in practice, the exhausted state happens where's still
2104 	 * some positive delta. So we apply some guesswork and compare the
2105 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2106 	 *
2107 	 * We probably cannot calculate the exact threshold value because this
2108 	 * depends on the internal reservations requested by various
2109 	 * operations, so some operations that consume a few metadata will
2110 	 * succeed even if the Avail is zero. But this is better than the other
2111 	 * way around.
2112 	 */
2113 	thresh = SZ_4M;
2114 
2115 	/*
2116 	 * We only want to claim there's no available space if we can no longer
2117 	 * allocate chunks for our metadata profile and our global reserve will
2118 	 * not fit in the free metadata space.  If we aren't ->full then we
2119 	 * still can allocate chunks and thus are fine using the currently
2120 	 * calculated f_bavail.
2121 	 */
2122 	if (!mixed && block_rsv->space_info->full &&
2123 	    (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
2124 		buf->f_bavail = 0;
2125 
2126 	buf->f_type = BTRFS_SUPER_MAGIC;
2127 	buf->f_bsize = dentry->d_sb->s_blocksize;
2128 	buf->f_namelen = BTRFS_NAME_LEN;
2129 
2130 	/* We treat it as constant endianness (it doesn't matter _which_)
2131 	   because we want the fsid to come out the same whether mounted
2132 	   on a big-endian or little-endian host */
2133 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2134 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2135 	/* Mask in the root object ID too, to disambiguate subvols */
2136 	buf->f_fsid.val[0] ^=
2137 		BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2138 	buf->f_fsid.val[1] ^=
2139 		BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2140 
2141 	return 0;
2142 }
2143 
2144 static void btrfs_kill_super(struct super_block *sb)
2145 {
2146 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2147 	kill_anon_super(sb);
2148 	btrfs_free_fs_info(fs_info);
2149 }
2150 
2151 static struct file_system_type btrfs_fs_type = {
2152 	.owner		= THIS_MODULE,
2153 	.name		= "btrfs",
2154 	.mount		= btrfs_mount,
2155 	.kill_sb	= btrfs_kill_super,
2156 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2157 };
2158 
2159 static struct file_system_type btrfs_root_fs_type = {
2160 	.owner		= THIS_MODULE,
2161 	.name		= "btrfs",
2162 	.mount		= btrfs_mount_root,
2163 	.kill_sb	= btrfs_kill_super,
2164 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2165 };
2166 
2167 MODULE_ALIAS_FS("btrfs");
2168 
2169 static int btrfs_control_open(struct inode *inode, struct file *file)
2170 {
2171 	/*
2172 	 * The control file's private_data is used to hold the
2173 	 * transaction when it is started and is used to keep
2174 	 * track of whether a transaction is already in progress.
2175 	 */
2176 	file->private_data = NULL;
2177 	return 0;
2178 }
2179 
2180 /*
2181  * Used by /dev/btrfs-control for devices ioctls.
2182  */
2183 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2184 				unsigned long arg)
2185 {
2186 	struct btrfs_ioctl_vol_args *vol;
2187 	struct btrfs_device *device = NULL;
2188 	dev_t devt = 0;
2189 	int ret = -ENOTTY;
2190 
2191 	if (!capable(CAP_SYS_ADMIN))
2192 		return -EPERM;
2193 
2194 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2195 	if (IS_ERR(vol))
2196 		return PTR_ERR(vol);
2197 	vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2198 
2199 	switch (cmd) {
2200 	case BTRFS_IOC_SCAN_DEV:
2201 		mutex_lock(&uuid_mutex);
2202 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2203 		ret = PTR_ERR_OR_ZERO(device);
2204 		mutex_unlock(&uuid_mutex);
2205 		break;
2206 	case BTRFS_IOC_FORGET_DEV:
2207 		if (vol->name[0] != 0) {
2208 			ret = lookup_bdev(vol->name, &devt);
2209 			if (ret)
2210 				break;
2211 		}
2212 		ret = btrfs_forget_devices(devt);
2213 		break;
2214 	case BTRFS_IOC_DEVICES_READY:
2215 		mutex_lock(&uuid_mutex);
2216 		device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);
2217 		if (IS_ERR(device)) {
2218 			mutex_unlock(&uuid_mutex);
2219 			ret = PTR_ERR(device);
2220 			break;
2221 		}
2222 		ret = !(device->fs_devices->num_devices ==
2223 			device->fs_devices->total_devices);
2224 		mutex_unlock(&uuid_mutex);
2225 		break;
2226 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2227 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2228 		break;
2229 	}
2230 
2231 	kfree(vol);
2232 	return ret;
2233 }
2234 
2235 static int btrfs_freeze(struct super_block *sb)
2236 {
2237 	struct btrfs_trans_handle *trans;
2238 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2239 	struct btrfs_root *root = fs_info->tree_root;
2240 
2241 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2242 	/*
2243 	 * We don't need a barrier here, we'll wait for any transaction that
2244 	 * could be in progress on other threads (and do delayed iputs that
2245 	 * we want to avoid on a frozen filesystem), or do the commit
2246 	 * ourselves.
2247 	 */
2248 	trans = btrfs_attach_transaction_barrier(root);
2249 	if (IS_ERR(trans)) {
2250 		/* no transaction, don't bother */
2251 		if (PTR_ERR(trans) == -ENOENT)
2252 			return 0;
2253 		return PTR_ERR(trans);
2254 	}
2255 	return btrfs_commit_transaction(trans);
2256 }
2257 
2258 static int check_dev_super(struct btrfs_device *dev)
2259 {
2260 	struct btrfs_fs_info *fs_info = dev->fs_info;
2261 	struct btrfs_super_block *sb;
2262 	u16 csum_type;
2263 	int ret = 0;
2264 
2265 	/* This should be called with fs still frozen. */
2266 	ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2267 
2268 	/* Missing dev, no need to check. */
2269 	if (!dev->bdev)
2270 		return 0;
2271 
2272 	/* Only need to check the primary super block. */
2273 	sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2274 	if (IS_ERR(sb))
2275 		return PTR_ERR(sb);
2276 
2277 	/* Verify the checksum. */
2278 	csum_type = btrfs_super_csum_type(sb);
2279 	if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
2280 		btrfs_err(fs_info, "csum type changed, has %u expect %u",
2281 			  csum_type, btrfs_super_csum_type(fs_info->super_copy));
2282 		ret = -EUCLEAN;
2283 		goto out;
2284 	}
2285 
2286 	if (btrfs_check_super_csum(fs_info, sb)) {
2287 		btrfs_err(fs_info, "csum for on-disk super block no longer matches");
2288 		ret = -EUCLEAN;
2289 		goto out;
2290 	}
2291 
2292 	/* Btrfs_validate_super() includes fsid check against super->fsid. */
2293 	ret = btrfs_validate_super(fs_info, sb, 0);
2294 	if (ret < 0)
2295 		goto out;
2296 
2297 	if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2298 		btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2299 			btrfs_super_generation(sb),
2300 			fs_info->last_trans_committed);
2301 		ret = -EUCLEAN;
2302 		goto out;
2303 	}
2304 out:
2305 	btrfs_release_disk_super(sb);
2306 	return ret;
2307 }
2308 
2309 static int btrfs_unfreeze(struct super_block *sb)
2310 {
2311 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2312 	struct btrfs_device *device;
2313 	int ret = 0;
2314 
2315 	/*
2316 	 * Make sure the fs is not changed by accident (like hibernation then
2317 	 * modified by other OS).
2318 	 * If we found anything wrong, we mark the fs error immediately.
2319 	 *
2320 	 * And since the fs is frozen, no one can modify the fs yet, thus
2321 	 * we don't need to hold device_list_mutex.
2322 	 */
2323 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2324 		ret = check_dev_super(device);
2325 		if (ret < 0) {
2326 			btrfs_handle_fs_error(fs_info, ret,
2327 				"super block on devid %llu got modified unexpectedly",
2328 				device->devid);
2329 			break;
2330 		}
2331 	}
2332 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2333 
2334 	/*
2335 	 * We still return 0, to allow VFS layer to unfreeze the fs even the
2336 	 * above checks failed. Since the fs is either fine or read-only, we're
2337 	 * safe to continue, without causing further damage.
2338 	 */
2339 	return 0;
2340 }
2341 
2342 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2343 {
2344 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2345 
2346 	/*
2347 	 * There should be always a valid pointer in latest_dev, it may be stale
2348 	 * for a short moment in case it's being deleted but still valid until
2349 	 * the end of RCU grace period.
2350 	 */
2351 	rcu_read_lock();
2352 	seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
2353 	rcu_read_unlock();
2354 
2355 	return 0;
2356 }
2357 
2358 static const struct super_operations btrfs_super_ops = {
2359 	.drop_inode	= btrfs_drop_inode,
2360 	.evict_inode	= btrfs_evict_inode,
2361 	.put_super	= btrfs_put_super,
2362 	.sync_fs	= btrfs_sync_fs,
2363 	.show_options	= btrfs_show_options,
2364 	.show_devname	= btrfs_show_devname,
2365 	.alloc_inode	= btrfs_alloc_inode,
2366 	.destroy_inode	= btrfs_destroy_inode,
2367 	.free_inode	= btrfs_free_inode,
2368 	.statfs		= btrfs_statfs,
2369 	.remount_fs	= btrfs_remount,
2370 	.freeze_fs	= btrfs_freeze,
2371 	.unfreeze_fs	= btrfs_unfreeze,
2372 };
2373 
2374 static const struct file_operations btrfs_ctl_fops = {
2375 	.open = btrfs_control_open,
2376 	.unlocked_ioctl	 = btrfs_control_ioctl,
2377 	.compat_ioctl = compat_ptr_ioctl,
2378 	.owner	 = THIS_MODULE,
2379 	.llseek = noop_llseek,
2380 };
2381 
2382 static struct miscdevice btrfs_misc = {
2383 	.minor		= BTRFS_MINOR,
2384 	.name		= "btrfs-control",
2385 	.fops		= &btrfs_ctl_fops
2386 };
2387 
2388 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2389 MODULE_ALIAS("devname:btrfs-control");
2390 
2391 static int __init btrfs_interface_init(void)
2392 {
2393 	return misc_register(&btrfs_misc);
2394 }
2395 
2396 static __cold void btrfs_interface_exit(void)
2397 {
2398 	misc_deregister(&btrfs_misc);
2399 }
2400 
2401 static int __init btrfs_print_mod_info(void)
2402 {
2403 	static const char options[] = ""
2404 #ifdef CONFIG_BTRFS_DEBUG
2405 			", debug=on"
2406 #endif
2407 #ifdef CONFIG_BTRFS_ASSERT
2408 			", assert=on"
2409 #endif
2410 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2411 			", integrity-checker=on"
2412 #endif
2413 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2414 			", ref-verify=on"
2415 #endif
2416 #ifdef CONFIG_BLK_DEV_ZONED
2417 			", zoned=yes"
2418 #else
2419 			", zoned=no"
2420 #endif
2421 #ifdef CONFIG_FS_VERITY
2422 			", fsverity=yes"
2423 #else
2424 			", fsverity=no"
2425 #endif
2426 			;
2427 	pr_info("Btrfs loaded%s\n", options);
2428 	return 0;
2429 }
2430 
2431 static int register_btrfs(void)
2432 {
2433 	return register_filesystem(&btrfs_fs_type);
2434 }
2435 
2436 static void unregister_btrfs(void)
2437 {
2438 	unregister_filesystem(&btrfs_fs_type);
2439 }
2440 
2441 /* Helper structure for long init/exit functions. */
2442 struct init_sequence {
2443 	int (*init_func)(void);
2444 	/* Can be NULL if the init_func doesn't need cleanup. */
2445 	void (*exit_func)(void);
2446 };
2447 
2448 static const struct init_sequence mod_init_seq[] = {
2449 	{
2450 		.init_func = btrfs_props_init,
2451 		.exit_func = NULL,
2452 	}, {
2453 		.init_func = btrfs_init_sysfs,
2454 		.exit_func = btrfs_exit_sysfs,
2455 	}, {
2456 		.init_func = btrfs_init_compress,
2457 		.exit_func = btrfs_exit_compress,
2458 	}, {
2459 		.init_func = btrfs_init_cachep,
2460 		.exit_func = btrfs_destroy_cachep,
2461 	}, {
2462 		.init_func = btrfs_transaction_init,
2463 		.exit_func = btrfs_transaction_exit,
2464 	}, {
2465 		.init_func = btrfs_ctree_init,
2466 		.exit_func = btrfs_ctree_exit,
2467 	}, {
2468 		.init_func = btrfs_free_space_init,
2469 		.exit_func = btrfs_free_space_exit,
2470 	}, {
2471 		.init_func = extent_state_init_cachep,
2472 		.exit_func = extent_state_free_cachep,
2473 	}, {
2474 		.init_func = extent_buffer_init_cachep,
2475 		.exit_func = extent_buffer_free_cachep,
2476 	}, {
2477 		.init_func = btrfs_bioset_init,
2478 		.exit_func = btrfs_bioset_exit,
2479 	}, {
2480 		.init_func = extent_map_init,
2481 		.exit_func = extent_map_exit,
2482 	}, {
2483 		.init_func = ordered_data_init,
2484 		.exit_func = ordered_data_exit,
2485 	}, {
2486 		.init_func = btrfs_delayed_inode_init,
2487 		.exit_func = btrfs_delayed_inode_exit,
2488 	}, {
2489 		.init_func = btrfs_auto_defrag_init,
2490 		.exit_func = btrfs_auto_defrag_exit,
2491 	}, {
2492 		.init_func = btrfs_delayed_ref_init,
2493 		.exit_func = btrfs_delayed_ref_exit,
2494 	}, {
2495 		.init_func = btrfs_prelim_ref_init,
2496 		.exit_func = btrfs_prelim_ref_exit,
2497 	}, {
2498 		.init_func = btrfs_interface_init,
2499 		.exit_func = btrfs_interface_exit,
2500 	}, {
2501 		.init_func = btrfs_print_mod_info,
2502 		.exit_func = NULL,
2503 	}, {
2504 		.init_func = btrfs_run_sanity_tests,
2505 		.exit_func = NULL,
2506 	}, {
2507 		.init_func = register_btrfs,
2508 		.exit_func = unregister_btrfs,
2509 	}
2510 };
2511 
2512 static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
2513 
2514 static __always_inline void btrfs_exit_btrfs_fs(void)
2515 {
2516 	int i;
2517 
2518 	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
2519 		if (!mod_init_result[i])
2520 			continue;
2521 		if (mod_init_seq[i].exit_func)
2522 			mod_init_seq[i].exit_func();
2523 		mod_init_result[i] = false;
2524 	}
2525 }
2526 
2527 static void __exit exit_btrfs_fs(void)
2528 {
2529 	btrfs_exit_btrfs_fs();
2530 	btrfs_cleanup_fs_uuids();
2531 }
2532 
2533 static int __init init_btrfs_fs(void)
2534 {
2535 	int ret;
2536 	int i;
2537 
2538 	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
2539 		ASSERT(!mod_init_result[i]);
2540 		ret = mod_init_seq[i].init_func();
2541 		if (ret < 0) {
2542 			btrfs_exit_btrfs_fs();
2543 			return ret;
2544 		}
2545 		mod_init_result[i] = true;
2546 	}
2547 	return 0;
2548 }
2549 
2550 late_initcall(init_btrfs_fs);
2551 module_exit(exit_btrfs_fs)
2552 
2553 MODULE_LICENSE("GPL");
2554 MODULE_SOFTDEP("pre: crc32c");
2555 MODULE_SOFTDEP("pre: xxhash64");
2556 MODULE_SOFTDEP("pre: sha256");
2557 MODULE_SOFTDEP("pre: blake2b-256");
2558