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