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