xref: /openbmc/linux/fs/fs_context.c (revision 22b6e7f3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Provide a way to create a superblock configuration context within the kernel
3  * that allows a superblock to be set up prior to mounting.
4  *
5  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
6  * Written by David Howells (dhowells@redhat.com)
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/fs_context.h>
12 #include <linux/fs_parser.h>
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/nsproxy.h>
16 #include <linux/slab.h>
17 #include <linux/magic.h>
18 #include <linux/security.h>
19 #include <linux/mnt_namespace.h>
20 #include <linux/pid_namespace.h>
21 #include <linux/user_namespace.h>
22 #include <net/net_namespace.h>
23 #include <asm/sections.h>
24 #include "mount.h"
25 #include "internal.h"
26 
27 enum legacy_fs_param {
28 	LEGACY_FS_UNSET_PARAMS,
29 	LEGACY_FS_MONOLITHIC_PARAMS,
30 	LEGACY_FS_INDIVIDUAL_PARAMS,
31 };
32 
33 struct legacy_fs_context {
34 	char			*legacy_data;	/* Data page for legacy filesystems */
35 	size_t			data_size;
36 	enum legacy_fs_param	param_type;
37 };
38 
39 static int legacy_init_fs_context(struct fs_context *fc);
40 
41 static const struct constant_table common_set_sb_flag[] = {
42 	{ "dirsync",	SB_DIRSYNC },
43 	{ "lazytime",	SB_LAZYTIME },
44 	{ "mand",	SB_MANDLOCK },
45 	{ "ro",		SB_RDONLY },
46 	{ "sync",	SB_SYNCHRONOUS },
47 	{ },
48 };
49 
50 static const struct constant_table common_clear_sb_flag[] = {
51 	{ "async",	SB_SYNCHRONOUS },
52 	{ "nolazytime",	SB_LAZYTIME },
53 	{ "nomand",	SB_MANDLOCK },
54 	{ "rw",		SB_RDONLY },
55 	{ },
56 };
57 
58 /*
59  * Check for a common mount option that manipulates s_flags.
60  */
61 static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
62 {
63 	unsigned int token;
64 
65 	token = lookup_constant(common_set_sb_flag, key, 0);
66 	if (token) {
67 		fc->sb_flags |= token;
68 		fc->sb_flags_mask |= token;
69 		return 0;
70 	}
71 
72 	token = lookup_constant(common_clear_sb_flag, key, 0);
73 	if (token) {
74 		fc->sb_flags &= ~token;
75 		fc->sb_flags_mask |= token;
76 		return 0;
77 	}
78 
79 	return -ENOPARAM;
80 }
81 
82 /**
83  * vfs_parse_fs_param_source - Handle setting "source" via parameter
84  * @fc: The filesystem context to modify
85  * @param: The parameter
86  *
87  * This is a simple helper for filesystems to verify that the "source" they
88  * accept is sane.
89  *
90  * Returns 0 on success, -ENOPARAM if this is not  "source" parameter, and
91  * -EINVAL otherwise. In the event of failure, supplementary error information
92  *  is logged.
93  */
94 int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param)
95 {
96 	if (strcmp(param->key, "source") != 0)
97 		return -ENOPARAM;
98 
99 	if (param->type != fs_value_is_string)
100 		return invalf(fc, "Non-string source");
101 
102 	if (fc->source)
103 		return invalf(fc, "Multiple sources");
104 
105 	fc->source = param->string;
106 	param->string = NULL;
107 	return 0;
108 }
109 EXPORT_SYMBOL(vfs_parse_fs_param_source);
110 
111 /**
112  * vfs_parse_fs_param - Add a single parameter to a superblock config
113  * @fc: The filesystem context to modify
114  * @param: The parameter
115  *
116  * A single mount option in string form is applied to the filesystem context
117  * being set up.  Certain standard options (for example "ro") are translated
118  * into flag bits without going to the filesystem.  The active security module
119  * is allowed to observe and poach options.  Any other options are passed over
120  * to the filesystem to parse.
121  *
122  * This may be called multiple times for a context.
123  *
124  * Returns 0 on success and a negative error code on failure.  In the event of
125  * failure, supplementary error information may have been set.
126  */
127 int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
128 {
129 	int ret;
130 
131 	if (!param->key)
132 		return invalf(fc, "Unnamed parameter\n");
133 
134 	ret = vfs_parse_sb_flag(fc, param->key);
135 	if (ret != -ENOPARAM)
136 		return ret;
137 
138 	ret = security_fs_context_parse_param(fc, param);
139 	if (ret != -ENOPARAM)
140 		/* Param belongs to the LSM or is disallowed by the LSM; so
141 		 * don't pass to the FS.
142 		 */
143 		return ret;
144 
145 	if (fc->ops->parse_param) {
146 		ret = fc->ops->parse_param(fc, param);
147 		if (ret != -ENOPARAM)
148 			return ret;
149 	}
150 
151 	/* If the filesystem doesn't take any arguments, give it the
152 	 * default handling of source.
153 	 */
154 	ret = vfs_parse_fs_param_source(fc, param);
155 	if (ret != -ENOPARAM)
156 		return ret;
157 
158 	return invalf(fc, "%s: Unknown parameter '%s'",
159 		      fc->fs_type->name, param->key);
160 }
161 EXPORT_SYMBOL(vfs_parse_fs_param);
162 
163 /**
164  * vfs_parse_fs_string - Convenience function to just parse a string.
165  * @fc: Filesystem context.
166  * @key: Parameter name.
167  * @value: Default value.
168  * @v_size: Maximum number of bytes in the value.
169  */
170 int vfs_parse_fs_string(struct fs_context *fc, const char *key,
171 			const char *value, size_t v_size)
172 {
173 	int ret;
174 
175 	struct fs_parameter param = {
176 		.key	= key,
177 		.type	= fs_value_is_flag,
178 		.size	= v_size,
179 	};
180 
181 	if (value) {
182 		param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
183 		if (!param.string)
184 			return -ENOMEM;
185 		param.type = fs_value_is_string;
186 	}
187 
188 	ret = vfs_parse_fs_param(fc, &param);
189 	kfree(param.string);
190 	return ret;
191 }
192 EXPORT_SYMBOL(vfs_parse_fs_string);
193 
194 /**
195  * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
196  * @fc: The superblock configuration to fill in.
197  * @data: The data to parse
198  *
199  * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
200  * called from the ->monolithic_mount_data() fs_context operation.
201  *
202  * Returns 0 on success or the error returned by the ->parse_option() fs_context
203  * operation on failure.
204  */
205 int generic_parse_monolithic(struct fs_context *fc, void *data)
206 {
207 	char *options = data, *key;
208 	int ret = 0;
209 
210 	if (!options)
211 		return 0;
212 
213 	ret = security_sb_eat_lsm_opts(options, &fc->security);
214 	if (ret)
215 		return ret;
216 
217 	while ((key = strsep(&options, ",")) != NULL) {
218 		if (*key) {
219 			size_t v_len = 0;
220 			char *value = strchr(key, '=');
221 
222 			if (value) {
223 				if (value == key)
224 					continue;
225 				*value++ = 0;
226 				v_len = strlen(value);
227 			}
228 			ret = vfs_parse_fs_string(fc, key, value, v_len);
229 			if (ret < 0)
230 				break;
231 		}
232 	}
233 
234 	return ret;
235 }
236 EXPORT_SYMBOL(generic_parse_monolithic);
237 
238 /**
239  * alloc_fs_context - Create a filesystem context.
240  * @fs_type: The filesystem type.
241  * @reference: The dentry from which this one derives (or NULL)
242  * @sb_flags: Filesystem/superblock flags (SB_*)
243  * @sb_flags_mask: Applicable members of @sb_flags
244  * @purpose: The purpose that this configuration shall be used for.
245  *
246  * Open a filesystem and create a mount context.  The mount context is
247  * initialised with the supplied flags and, if a submount/automount from
248  * another superblock (referred to by @reference) is supplied, may have
249  * parameters such as namespaces copied across from that superblock.
250  */
251 static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
252 				      struct dentry *reference,
253 				      unsigned int sb_flags,
254 				      unsigned int sb_flags_mask,
255 				      enum fs_context_purpose purpose)
256 {
257 	int (*init_fs_context)(struct fs_context *);
258 	struct fs_context *fc;
259 	int ret = -ENOMEM;
260 
261 	fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT);
262 	if (!fc)
263 		return ERR_PTR(-ENOMEM);
264 
265 	fc->purpose	= purpose;
266 	fc->sb_flags	= sb_flags;
267 	fc->sb_flags_mask = sb_flags_mask;
268 	fc->fs_type	= get_filesystem(fs_type);
269 	fc->cred	= get_current_cred();
270 	fc->net_ns	= get_net(current->nsproxy->net_ns);
271 	fc->log.prefix	= fs_type->name;
272 
273 	mutex_init(&fc->uapi_mutex);
274 
275 	switch (purpose) {
276 	case FS_CONTEXT_FOR_MOUNT:
277 		fc->user_ns = get_user_ns(fc->cred->user_ns);
278 		break;
279 	case FS_CONTEXT_FOR_SUBMOUNT:
280 		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
281 		break;
282 	case FS_CONTEXT_FOR_RECONFIGURE:
283 		atomic_inc(&reference->d_sb->s_active);
284 		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
285 		fc->root = dget(reference);
286 		break;
287 	}
288 
289 	/* TODO: Make all filesystems support this unconditionally */
290 	init_fs_context = fc->fs_type->init_fs_context;
291 	if (!init_fs_context)
292 		init_fs_context = legacy_init_fs_context;
293 
294 	ret = init_fs_context(fc);
295 	if (ret < 0)
296 		goto err_fc;
297 	fc->need_free = true;
298 	return fc;
299 
300 err_fc:
301 	put_fs_context(fc);
302 	return ERR_PTR(ret);
303 }
304 
305 struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
306 					unsigned int sb_flags)
307 {
308 	return alloc_fs_context(fs_type, NULL, sb_flags, 0,
309 					FS_CONTEXT_FOR_MOUNT);
310 }
311 EXPORT_SYMBOL(fs_context_for_mount);
312 
313 struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
314 					unsigned int sb_flags,
315 					unsigned int sb_flags_mask)
316 {
317 	return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
318 				sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
319 }
320 EXPORT_SYMBOL(fs_context_for_reconfigure);
321 
322 /**
323  * fs_context_for_submount: allocate a new fs_context for a submount
324  * @type: file_system_type of the new context
325  * @reference: reference dentry from which to copy relevant info
326  *
327  * Allocate a new fs_context suitable for a submount. This also ensures that
328  * the fc->security object is inherited from @reference (if needed).
329  */
330 struct fs_context *fs_context_for_submount(struct file_system_type *type,
331 					   struct dentry *reference)
332 {
333 	struct fs_context *fc;
334 	int ret;
335 
336 	fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
337 	if (IS_ERR(fc))
338 		return fc;
339 
340 	ret = security_fs_context_submount(fc, reference->d_sb);
341 	if (ret) {
342 		put_fs_context(fc);
343 		return ERR_PTR(ret);
344 	}
345 
346 	return fc;
347 }
348 EXPORT_SYMBOL(fs_context_for_submount);
349 
350 void fc_drop_locked(struct fs_context *fc)
351 {
352 	struct super_block *sb = fc->root->d_sb;
353 	dput(fc->root);
354 	fc->root = NULL;
355 	deactivate_locked_super(sb);
356 }
357 
358 static void legacy_fs_context_free(struct fs_context *fc);
359 
360 /**
361  * vfs_dup_fs_context - Duplicate a filesystem context.
362  * @src_fc: The context to copy.
363  */
364 struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
365 {
366 	struct fs_context *fc;
367 	int ret;
368 
369 	if (!src_fc->ops->dup)
370 		return ERR_PTR(-EOPNOTSUPP);
371 
372 	fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
373 	if (!fc)
374 		return ERR_PTR(-ENOMEM);
375 
376 	mutex_init(&fc->uapi_mutex);
377 
378 	fc->fs_private	= NULL;
379 	fc->s_fs_info	= NULL;
380 	fc->source	= NULL;
381 	fc->security	= NULL;
382 	get_filesystem(fc->fs_type);
383 	get_net(fc->net_ns);
384 	get_user_ns(fc->user_ns);
385 	get_cred(fc->cred);
386 	if (fc->log.log)
387 		refcount_inc(&fc->log.log->usage);
388 
389 	/* Can't call put until we've called ->dup */
390 	ret = fc->ops->dup(fc, src_fc);
391 	if (ret < 0)
392 		goto err_fc;
393 
394 	ret = security_fs_context_dup(fc, src_fc);
395 	if (ret < 0)
396 		goto err_fc;
397 	return fc;
398 
399 err_fc:
400 	put_fs_context(fc);
401 	return ERR_PTR(ret);
402 }
403 EXPORT_SYMBOL(vfs_dup_fs_context);
404 
405 /**
406  * logfc - Log a message to a filesystem context
407  * @log: The filesystem context to log to, or NULL to use printk.
408  * @prefix: A string to prefix the output with, or NULL.
409  * @level: 'w' for a warning, 'e' for an error.  Anything else is a notice.
410  * @fmt: The format of the buffer.
411  */
412 void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
413 {
414 	va_list va;
415 	struct va_format vaf = {.fmt = fmt, .va = &va};
416 
417 	va_start(va, fmt);
418 	if (!log) {
419 		switch (level) {
420 		case 'w':
421 			printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
422 						prefix ? ": " : "", &vaf);
423 			break;
424 		case 'e':
425 			printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
426 						prefix ? ": " : "", &vaf);
427 			break;
428 		default:
429 			printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
430 						prefix ? ": " : "", &vaf);
431 			break;
432 		}
433 	} else {
434 		unsigned int logsize = ARRAY_SIZE(log->buffer);
435 		u8 index;
436 		char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
437 						prefix ? prefix : "",
438 						prefix ? ": " : "", &vaf);
439 
440 		index = log->head & (logsize - 1);
441 		BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
442 			     sizeof(log->tail) != sizeof(u8));
443 		if ((u8)(log->head - log->tail) == logsize) {
444 			/* The buffer is full, discard the oldest message */
445 			if (log->need_free & (1 << index))
446 				kfree(log->buffer[index]);
447 			log->tail++;
448 		}
449 
450 		log->buffer[index] = q ? q : "OOM: Can't store error string";
451 		if (q)
452 			log->need_free |= 1 << index;
453 		else
454 			log->need_free &= ~(1 << index);
455 		log->head++;
456 	}
457 	va_end(va);
458 }
459 EXPORT_SYMBOL(logfc);
460 
461 /*
462  * Free a logging structure.
463  */
464 static void put_fc_log(struct fs_context *fc)
465 {
466 	struct fc_log *log = fc->log.log;
467 	int i;
468 
469 	if (log) {
470 		if (refcount_dec_and_test(&log->usage)) {
471 			fc->log.log = NULL;
472 			for (i = 0; i <= 7; i++)
473 				if (log->need_free & (1 << i))
474 					kfree(log->buffer[i]);
475 			kfree(log);
476 		}
477 	}
478 }
479 
480 /**
481  * put_fs_context - Dispose of a superblock configuration context.
482  * @fc: The context to dispose of.
483  */
484 void put_fs_context(struct fs_context *fc)
485 {
486 	struct super_block *sb;
487 
488 	if (fc->root) {
489 		sb = fc->root->d_sb;
490 		dput(fc->root);
491 		fc->root = NULL;
492 		deactivate_super(sb);
493 	}
494 
495 	if (fc->need_free && fc->ops && fc->ops->free)
496 		fc->ops->free(fc);
497 
498 	security_free_mnt_opts(&fc->security);
499 	put_net(fc->net_ns);
500 	put_user_ns(fc->user_ns);
501 	put_cred(fc->cred);
502 	put_fc_log(fc);
503 	put_filesystem(fc->fs_type);
504 	kfree(fc->source);
505 	kfree(fc);
506 }
507 EXPORT_SYMBOL(put_fs_context);
508 
509 /*
510  * Free the config for a filesystem that doesn't support fs_context.
511  */
512 static void legacy_fs_context_free(struct fs_context *fc)
513 {
514 	struct legacy_fs_context *ctx = fc->fs_private;
515 
516 	if (ctx) {
517 		if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
518 			kfree(ctx->legacy_data);
519 		kfree(ctx);
520 	}
521 }
522 
523 /*
524  * Duplicate a legacy config.
525  */
526 static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
527 {
528 	struct legacy_fs_context *ctx;
529 	struct legacy_fs_context *src_ctx = src_fc->fs_private;
530 
531 	ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
532 	if (!ctx)
533 		return -ENOMEM;
534 
535 	if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
536 		ctx->legacy_data = kmemdup(src_ctx->legacy_data,
537 					   src_ctx->data_size, GFP_KERNEL);
538 		if (!ctx->legacy_data) {
539 			kfree(ctx);
540 			return -ENOMEM;
541 		}
542 	}
543 
544 	fc->fs_private = ctx;
545 	return 0;
546 }
547 
548 /*
549  * Add a parameter to a legacy config.  We build up a comma-separated list of
550  * options.
551  */
552 static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
553 {
554 	struct legacy_fs_context *ctx = fc->fs_private;
555 	unsigned int size = ctx->data_size;
556 	size_t len = 0;
557 	int ret;
558 
559 	ret = vfs_parse_fs_param_source(fc, param);
560 	if (ret != -ENOPARAM)
561 		return ret;
562 
563 	if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
564 		return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
565 
566 	switch (param->type) {
567 	case fs_value_is_string:
568 		len = 1 + param->size;
569 		fallthrough;
570 	case fs_value_is_flag:
571 		len += strlen(param->key);
572 		break;
573 	default:
574 		return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
575 			      param->key);
576 	}
577 
578 	if (size + len + 2 > PAGE_SIZE)
579 		return invalf(fc, "VFS: Legacy: Cumulative options too large");
580 	if (strchr(param->key, ',') ||
581 	    (param->type == fs_value_is_string &&
582 	     memchr(param->string, ',', param->size)))
583 		return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
584 			      param->key);
585 	if (!ctx->legacy_data) {
586 		ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
587 		if (!ctx->legacy_data)
588 			return -ENOMEM;
589 	}
590 
591 	if (size)
592 		ctx->legacy_data[size++] = ',';
593 	len = strlen(param->key);
594 	memcpy(ctx->legacy_data + size, param->key, len);
595 	size += len;
596 	if (param->type == fs_value_is_string) {
597 		ctx->legacy_data[size++] = '=';
598 		memcpy(ctx->legacy_data + size, param->string, param->size);
599 		size += param->size;
600 	}
601 	ctx->legacy_data[size] = '\0';
602 	ctx->data_size = size;
603 	ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
604 	return 0;
605 }
606 
607 /*
608  * Add monolithic mount data.
609  */
610 static int legacy_parse_monolithic(struct fs_context *fc, void *data)
611 {
612 	struct legacy_fs_context *ctx = fc->fs_private;
613 
614 	if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
615 		pr_warn("VFS: Can't mix monolithic and individual options\n");
616 		return -EINVAL;
617 	}
618 
619 	ctx->legacy_data = data;
620 	ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
621 	if (!ctx->legacy_data)
622 		return 0;
623 
624 	if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
625 		return 0;
626 	return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
627 }
628 
629 /*
630  * Get a mountable root with the legacy mount command.
631  */
632 static int legacy_get_tree(struct fs_context *fc)
633 {
634 	struct legacy_fs_context *ctx = fc->fs_private;
635 	struct super_block *sb;
636 	struct dentry *root;
637 
638 	root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
639 				      fc->source, ctx->legacy_data);
640 	if (IS_ERR(root))
641 		return PTR_ERR(root);
642 
643 	sb = root->d_sb;
644 	BUG_ON(!sb);
645 
646 	fc->root = root;
647 	return 0;
648 }
649 
650 /*
651  * Handle remount.
652  */
653 static int legacy_reconfigure(struct fs_context *fc)
654 {
655 	struct legacy_fs_context *ctx = fc->fs_private;
656 	struct super_block *sb = fc->root->d_sb;
657 
658 	if (!sb->s_op->remount_fs)
659 		return 0;
660 
661 	return sb->s_op->remount_fs(sb, &fc->sb_flags,
662 				    ctx ? ctx->legacy_data : NULL);
663 }
664 
665 const struct fs_context_operations legacy_fs_context_ops = {
666 	.free			= legacy_fs_context_free,
667 	.dup			= legacy_fs_context_dup,
668 	.parse_param		= legacy_parse_param,
669 	.parse_monolithic	= legacy_parse_monolithic,
670 	.get_tree		= legacy_get_tree,
671 	.reconfigure		= legacy_reconfigure,
672 };
673 
674 /*
675  * Initialise a legacy context for a filesystem that doesn't support
676  * fs_context.
677  */
678 static int legacy_init_fs_context(struct fs_context *fc)
679 {
680 	fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT);
681 	if (!fc->fs_private)
682 		return -ENOMEM;
683 	fc->ops = &legacy_fs_context_ops;
684 	return 0;
685 }
686 
687 int parse_monolithic_mount_data(struct fs_context *fc, void *data)
688 {
689 	int (*monolithic_mount_data)(struct fs_context *, void *);
690 
691 	monolithic_mount_data = fc->ops->parse_monolithic;
692 	if (!monolithic_mount_data)
693 		monolithic_mount_data = generic_parse_monolithic;
694 
695 	return monolithic_mount_data(fc, data);
696 }
697 
698 /*
699  * Clean up a context after performing an action on it and put it into a state
700  * from where it can be used to reconfigure a superblock.
701  *
702  * Note that here we do only the parts that can't fail; the rest is in
703  * finish_clean_context() below and in between those fs_context is marked
704  * FS_CONTEXT_AWAITING_RECONF.  The reason for splitup is that after
705  * successful mount or remount we need to report success to userland.
706  * Trying to do full reinit (for the sake of possible subsequent remount)
707  * and failing to allocate memory would've put us into a nasty situation.
708  * So here we only discard the old state and reinitialization is left
709  * until we actually try to reconfigure.
710  */
711 void vfs_clean_context(struct fs_context *fc)
712 {
713 	if (fc->need_free && fc->ops && fc->ops->free)
714 		fc->ops->free(fc);
715 	fc->need_free = false;
716 	fc->fs_private = NULL;
717 	fc->s_fs_info = NULL;
718 	fc->sb_flags = 0;
719 	security_free_mnt_opts(&fc->security);
720 	kfree(fc->source);
721 	fc->source = NULL;
722 	fc->exclusive = false;
723 
724 	fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
725 	fc->phase = FS_CONTEXT_AWAITING_RECONF;
726 }
727 
728 int finish_clean_context(struct fs_context *fc)
729 {
730 	int error;
731 
732 	if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
733 		return 0;
734 
735 	if (fc->fs_type->init_fs_context)
736 		error = fc->fs_type->init_fs_context(fc);
737 	else
738 		error = legacy_init_fs_context(fc);
739 	if (unlikely(error)) {
740 		fc->phase = FS_CONTEXT_FAILED;
741 		return error;
742 	}
743 	fc->need_free = true;
744 	fc->phase = FS_CONTEXT_RECONF_PARAMS;
745 	return 0;
746 }
747