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