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