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