blob: 2834d1afa6e8053e93c6e78122be8d58107a36c9 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells9bc61ab2018-11-04 03:19:03 -05002/* 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)
David Howells9bc61ab2018-11-04 03:19:03 -05007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
David Howells007ec262018-11-01 23:34:29 +000010#include <linux/module.h>
David Howells9bc61ab2018-11-04 03:19:03 -050011#include <linux/fs_context.h>
David Howells3e1aeb02018-11-01 23:07:25 +000012#include <linux/fs_parser.h>
David Howells9bc61ab2018-11-04 03:19:03 -050013#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>
David Howells007ec262018-11-01 23:34:29 +000023#include <asm/sections.h>
David Howells9bc61ab2018-11-04 03:19:03 -050024#include "mount.h"
25#include "internal.h"
26
David Howells3e1aeb02018-11-01 23:07:25 +000027enum legacy_fs_param {
28 LEGACY_FS_UNSET_PARAMS,
29 LEGACY_FS_MONOLITHIC_PARAMS,
30 LEGACY_FS_INDIVIDUAL_PARAMS,
31};
32
David Howells9bc61ab2018-11-04 03:19:03 -050033struct legacy_fs_context {
34 char *legacy_data; /* Data page for legacy filesystems */
35 size_t data_size;
David Howells3e1aeb02018-11-01 23:07:25 +000036 enum legacy_fs_param param_type;
David Howells9bc61ab2018-11-04 03:19:03 -050037};
38
39static int legacy_init_fs_context(struct fs_context *fc);
40
David Howells3e1aeb02018-11-01 23:07:25 +000041static const struct constant_table common_set_sb_flag[] = {
42 { "dirsync", SB_DIRSYNC },
43 { "lazytime", SB_LAZYTIME },
44 { "mand", SB_MANDLOCK },
David Howells3e1aeb02018-11-01 23:07:25 +000045 { "ro", SB_RDONLY },
46 { "sync", SB_SYNCHRONOUS },
Al Viro34264ae2019-12-16 13:45:41 -050047 { },
David Howells3e1aeb02018-11-01 23:07:25 +000048};
49
50static const struct constant_table common_clear_sb_flag[] = {
51 { "async", SB_SYNCHRONOUS },
52 { "nolazytime", SB_LAZYTIME },
53 { "nomand", SB_MANDLOCK },
54 { "rw", SB_RDONLY },
Al Viro34264ae2019-12-16 13:45:41 -050055 { },
David Howells3e1aeb02018-11-01 23:07:25 +000056};
57
David Howells3e1aeb02018-11-01 23:07:25 +000058/*
59 * Check for a common mount option that manipulates s_flags.
60 */
61static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
62{
63 unsigned int token;
David Howells3e1aeb02018-11-01 23:07:25 +000064
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 - Add a single parameter to a superblock config
84 * @fc: The filesystem context to modify
85 * @param: The parameter
86 *
87 * A single mount option in string form is applied to the filesystem context
88 * being set up. Certain standard options (for example "ro") are translated
89 * into flag bits without going to the filesystem. The active security module
90 * is allowed to observe and poach options. Any other options are passed over
91 * to the filesystem to parse.
92 *
93 * This may be called multiple times for a context.
94 *
95 * Returns 0 on success and a negative error code on failure. In the event of
96 * failure, supplementary error information may have been set.
97 */
98int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
99{
100 int ret;
101
102 if (!param->key)
103 return invalf(fc, "Unnamed parameter\n");
104
105 ret = vfs_parse_sb_flag(fc, param->key);
106 if (ret != -ENOPARAM)
107 return ret;
108
109 ret = security_fs_context_parse_param(fc, param);
110 if (ret != -ENOPARAM)
111 /* Param belongs to the LSM or is disallowed by the LSM; so
112 * don't pass to the FS.
113 */
114 return ret;
115
116 if (fc->ops->parse_param) {
117 ret = fc->ops->parse_param(fc, param);
118 if (ret != -ENOPARAM)
119 return ret;
120 }
121
122 /* If the filesystem doesn't take any arguments, give it the
123 * default handling of source.
124 */
125 if (strcmp(param->key, "source") == 0) {
126 if (param->type != fs_value_is_string)
127 return invalf(fc, "VFS: Non-string source");
128 if (fc->source)
129 return invalf(fc, "VFS: Multiple sources");
130 fc->source = param->string;
131 param->string = NULL;
132 return 0;
133 }
134
135 return invalf(fc, "%s: Unknown parameter '%s'",
136 fc->fs_type->name, param->key);
137}
138EXPORT_SYMBOL(vfs_parse_fs_param);
139
140/**
141 * vfs_parse_fs_string - Convenience function to just parse a string.
142 */
143int vfs_parse_fs_string(struct fs_context *fc, const char *key,
144 const char *value, size_t v_size)
145{
146 int ret;
147
148 struct fs_parameter param = {
149 .key = key,
Al Viro0f895892019-12-17 14:15:04 -0500150 .type = fs_value_is_flag,
David Howells3e1aeb02018-11-01 23:07:25 +0000151 .size = v_size,
152 };
153
Al Viro0f895892019-12-17 14:15:04 -0500154 if (value) {
David Howells3e1aeb02018-11-01 23:07:25 +0000155 param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
156 if (!param.string)
157 return -ENOMEM;
Al Viro0f895892019-12-17 14:15:04 -0500158 param.type = fs_value_is_string;
David Howells3e1aeb02018-11-01 23:07:25 +0000159 }
160
161 ret = vfs_parse_fs_param(fc, &param);
162 kfree(param.string);
163 return ret;
164}
165EXPORT_SYMBOL(vfs_parse_fs_string);
166
167/**
168 * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
169 * @ctx: The superblock configuration to fill in.
170 * @data: The data to parse
171 *
172 * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
173 * called from the ->monolithic_mount_data() fs_context operation.
174 *
175 * Returns 0 on success or the error returned by the ->parse_option() fs_context
176 * operation on failure.
177 */
178int generic_parse_monolithic(struct fs_context *fc, void *data)
179{
180 char *options = data, *key;
181 int ret = 0;
182
183 if (!options)
184 return 0;
185
186 ret = security_sb_eat_lsm_opts(options, &fc->security);
187 if (ret)
188 return ret;
189
190 while ((key = strsep(&options, ",")) != NULL) {
191 if (*key) {
192 size_t v_len = 0;
193 char *value = strchr(key, '=');
194
195 if (value) {
196 if (value == key)
197 continue;
198 *value++ = 0;
199 v_len = strlen(value);
200 }
201 ret = vfs_parse_fs_string(fc, key, value, v_len);
202 if (ret < 0)
203 break;
204 }
205 }
206
207 return ret;
208}
209EXPORT_SYMBOL(generic_parse_monolithic);
210
David Howells9bc61ab2018-11-04 03:19:03 -0500211/**
212 * alloc_fs_context - Create a filesystem context.
213 * @fs_type: The filesystem type.
214 * @reference: The dentry from which this one derives (or NULL)
215 * @sb_flags: Filesystem/superblock flags (SB_*)
216 * @sb_flags_mask: Applicable members of @sb_flags
217 * @purpose: The purpose that this configuration shall be used for.
218 *
219 * Open a filesystem and create a mount context. The mount context is
220 * initialised with the supplied flags and, if a submount/automount from
221 * another superblock (referred to by @reference) is supplied, may have
222 * parameters such as namespaces copied across from that superblock.
223 */
224static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
225 struct dentry *reference,
226 unsigned int sb_flags,
227 unsigned int sb_flags_mask,
228 enum fs_context_purpose purpose)
229{
Al Virof3a09c92018-12-23 18:55:56 -0500230 int (*init_fs_context)(struct fs_context *);
David Howells9bc61ab2018-11-04 03:19:03 -0500231 struct fs_context *fc;
232 int ret = -ENOMEM;
233
234 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
235 if (!fc)
236 return ERR_PTR(-ENOMEM);
237
238 fc->purpose = purpose;
239 fc->sb_flags = sb_flags;
240 fc->sb_flags_mask = sb_flags_mask;
241 fc->fs_type = get_filesystem(fs_type);
242 fc->cred = get_current_cred();
243 fc->net_ns = get_net(current->nsproxy->net_ns);
Al Virocc3c0b52019-12-21 00:16:49 -0500244 fc->log.prefix = fs_type->name;
David Howells9bc61ab2018-11-04 03:19:03 -0500245
David Howells24dcb3d2018-11-01 23:33:31 +0000246 mutex_init(&fc->uapi_mutex);
247
David Howells9bc61ab2018-11-04 03:19:03 -0500248 switch (purpose) {
249 case FS_CONTEXT_FOR_MOUNT:
250 fc->user_ns = get_user_ns(fc->cred->user_ns);
251 break;
Al Viroe1a91582018-12-23 16:25:31 -0500252 case FS_CONTEXT_FOR_SUBMOUNT:
253 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
254 break;
David Howells8d0347f2018-11-04 09:28:36 -0500255 case FS_CONTEXT_FOR_RECONFIGURE:
David Howells8d0347f2018-11-04 09:28:36 -0500256 atomic_inc(&reference->d_sb->s_active);
Eric Biggers1dd9bc02019-08-21 22:16:33 -0700257 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
David Howells8d0347f2018-11-04 09:28:36 -0500258 fc->root = dget(reference);
259 break;
David Howells9bc61ab2018-11-04 03:19:03 -0500260 }
261
Al Virof3a09c92018-12-23 18:55:56 -0500262 /* TODO: Make all filesystems support this unconditionally */
263 init_fs_context = fc->fs_type->init_fs_context;
264 if (!init_fs_context)
265 init_fs_context = legacy_init_fs_context;
266
267 ret = init_fs_context(fc);
David Howells9bc61ab2018-11-04 03:19:03 -0500268 if (ret < 0)
269 goto err_fc;
270 fc->need_free = true;
271 return fc;
272
273err_fc:
274 put_fs_context(fc);
275 return ERR_PTR(ret);
276}
277
278struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
279 unsigned int sb_flags)
280{
281 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
282 FS_CONTEXT_FOR_MOUNT);
283}
284EXPORT_SYMBOL(fs_context_for_mount);
285
David Howells8d0347f2018-11-04 09:28:36 -0500286struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
287 unsigned int sb_flags,
288 unsigned int sb_flags_mask)
289{
290 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
291 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
292}
293EXPORT_SYMBOL(fs_context_for_reconfigure);
294
Al Viroe1a91582018-12-23 16:25:31 -0500295struct fs_context *fs_context_for_submount(struct file_system_type *type,
296 struct dentry *reference)
297{
298 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
299}
300EXPORT_SYMBOL(fs_context_for_submount);
301
Al Viroc9ce29e2018-12-20 15:04:50 -0500302void fc_drop_locked(struct fs_context *fc)
303{
304 struct super_block *sb = fc->root->d_sb;
305 dput(fc->root);
306 fc->root = NULL;
307 deactivate_locked_super(sb);
308}
309
David Howells9bc61ab2018-11-04 03:19:03 -0500310static void legacy_fs_context_free(struct fs_context *fc);
David Howells8d0347f2018-11-04 09:28:36 -0500311
David Howells9bc61ab2018-11-04 03:19:03 -0500312/**
Al Viro0b520752018-12-23 16:02:47 -0500313 * vfs_dup_fc_config: Duplicate a filesystem context.
314 * @src_fc: The context to copy.
315 */
316struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
317{
318 struct fs_context *fc;
319 int ret;
320
321 if (!src_fc->ops->dup)
322 return ERR_PTR(-EOPNOTSUPP);
323
324 fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
325 if (!fc)
326 return ERR_PTR(-ENOMEM);
327
David Howells24dcb3d2018-11-01 23:33:31 +0000328 mutex_init(&fc->uapi_mutex);
329
Al Viro0b520752018-12-23 16:02:47 -0500330 fc->fs_private = NULL;
331 fc->s_fs_info = NULL;
332 fc->source = NULL;
333 fc->security = NULL;
334 get_filesystem(fc->fs_type);
335 get_net(fc->net_ns);
336 get_user_ns(fc->user_ns);
337 get_cred(fc->cred);
Al Virocc3c0b52019-12-21 00:16:49 -0500338 if (fc->log.log)
339 refcount_inc(&fc->log.log->usage);
Al Viro0b520752018-12-23 16:02:47 -0500340
341 /* Can't call put until we've called ->dup */
342 ret = fc->ops->dup(fc, src_fc);
343 if (ret < 0)
344 goto err_fc;
345
346 ret = security_fs_context_dup(fc, src_fc);
347 if (ret < 0)
348 goto err_fc;
349 return fc;
350
351err_fc:
352 put_fs_context(fc);
353 return ERR_PTR(ret);
354}
355EXPORT_SYMBOL(vfs_dup_fs_context);
356
David Howellse7582e162018-11-01 23:07:26 +0000357/**
358 * logfc - Log a message to a filesystem context
359 * @fc: The filesystem context to log to.
360 * @fmt: The format of the buffer.
361 */
Al Viro9f09f642019-12-20 22:10:36 -0500362void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
David Howellse7582e162018-11-01 23:07:26 +0000363{
364 va_list va;
Al Viro9f09f642019-12-20 22:10:36 -0500365 struct va_format vaf = {.fmt = fmt, .va = &va};
David Howellse7582e162018-11-01 23:07:26 +0000366
367 va_start(va, fmt);
David Howells007ec262018-11-01 23:34:29 +0000368 if (!log) {
Al Viro9f09f642019-12-20 22:10:36 -0500369 switch (level) {
David Howells007ec262018-11-01 23:34:29 +0000370 case 'w':
Al Viro9f09f642019-12-20 22:10:36 -0500371 printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
372 prefix ? ": " : "", &vaf);
David Howells007ec262018-11-01 23:34:29 +0000373 break;
374 case 'e':
Al Viro9f09f642019-12-20 22:10:36 -0500375 printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
376 prefix ? ": " : "", &vaf);
David Howells007ec262018-11-01 23:34:29 +0000377 break;
378 default:
Al Viro9f09f642019-12-20 22:10:36 -0500379 printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
380 prefix ? ": " : "", &vaf);
David Howells007ec262018-11-01 23:34:29 +0000381 break;
382 }
David Howells007ec262018-11-01 23:34:29 +0000383 } else {
384 unsigned int logsize = ARRAY_SIZE(log->buffer);
385 u8 index;
Al Viro9f09f642019-12-20 22:10:36 -0500386 char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
387 prefix ? prefix : "",
388 prefix ? ": " : "", &vaf);
David Howells007ec262018-11-01 23:34:29 +0000389
390 index = log->head & (logsize - 1);
391 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
392 sizeof(log->tail) != sizeof(u8));
393 if ((u8)(log->head - log->tail) == logsize) {
394 /* The buffer is full, discard the oldest message */
395 if (log->need_free & (1 << index))
396 kfree(log->buffer[index]);
397 log->tail++;
398 }
399
Al Viro9f09f642019-12-20 22:10:36 -0500400 log->buffer[index] = q ? q : "OOM: Can't store error string";
401 if (q)
402 log->need_free |= 1 << index;
403 else
404 log->need_free &= ~(1 << index);
David Howells007ec262018-11-01 23:34:29 +0000405 log->head++;
406 }
David Howellse7582e162018-11-01 23:07:26 +0000407 va_end(va);
408}
409EXPORT_SYMBOL(logfc);
David Howells007ec262018-11-01 23:34:29 +0000410
411/*
412 * Free a logging structure.
413 */
414static void put_fc_log(struct fs_context *fc)
415{
Al Virocc3c0b52019-12-21 00:16:49 -0500416 struct fc_log *log = fc->log.log;
David Howells007ec262018-11-01 23:34:29 +0000417 int i;
418
419 if (log) {
420 if (refcount_dec_and_test(&log->usage)) {
Al Virocc3c0b52019-12-21 00:16:49 -0500421 fc->log.log = NULL;
David Howells007ec262018-11-01 23:34:29 +0000422 for (i = 0; i <= 7; i++)
423 if (log->need_free & (1 << i))
424 kfree(log->buffer[i]);
425 kfree(log);
426 }
427 }
428}
David Howellse7582e162018-11-01 23:07:26 +0000429
Al Viro0b520752018-12-23 16:02:47 -0500430/**
David Howells9bc61ab2018-11-04 03:19:03 -0500431 * put_fs_context - Dispose of a superblock configuration context.
432 * @fc: The context to dispose of.
433 */
434void put_fs_context(struct fs_context *fc)
435{
436 struct super_block *sb;
437
438 if (fc->root) {
439 sb = fc->root->d_sb;
440 dput(fc->root);
441 fc->root = NULL;
442 deactivate_super(sb);
443 }
444
Al Virof3a09c92018-12-23 18:55:56 -0500445 if (fc->need_free && fc->ops && fc->ops->free)
446 fc->ops->free(fc);
David Howells9bc61ab2018-11-04 03:19:03 -0500447
448 security_free_mnt_opts(&fc->security);
David Howells8d0347f2018-11-04 09:28:36 -0500449 put_net(fc->net_ns);
David Howells9bc61ab2018-11-04 03:19:03 -0500450 put_user_ns(fc->user_ns);
451 put_cred(fc->cred);
David Howells007ec262018-11-01 23:34:29 +0000452 put_fc_log(fc);
David Howells9bc61ab2018-11-04 03:19:03 -0500453 put_filesystem(fc->fs_type);
454 kfree(fc->source);
455 kfree(fc);
456}
457EXPORT_SYMBOL(put_fs_context);
458
459/*
460 * Free the config for a filesystem that doesn't support fs_context.
461 */
462static void legacy_fs_context_free(struct fs_context *fc)
463{
David Howells3e1aeb02018-11-01 23:07:25 +0000464 struct legacy_fs_context *ctx = fc->fs_private;
465
466 if (ctx) {
467 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
468 kfree(ctx->legacy_data);
469 kfree(ctx);
470 }
471}
472
473/*
Al Viro0b520752018-12-23 16:02:47 -0500474 * Duplicate a legacy config.
475 */
476static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
477{
478 struct legacy_fs_context *ctx;
479 struct legacy_fs_context *src_ctx = src_fc->fs_private;
480
481 ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
482 if (!ctx)
483 return -ENOMEM;
484
485 if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
486 ctx->legacy_data = kmemdup(src_ctx->legacy_data,
487 src_ctx->data_size, GFP_KERNEL);
488 if (!ctx->legacy_data) {
489 kfree(ctx);
490 return -ENOMEM;
491 }
492 }
493
494 fc->fs_private = ctx;
495 return 0;
496}
497
498/*
David Howells3e1aeb02018-11-01 23:07:25 +0000499 * Add a parameter to a legacy config. We build up a comma-separated list of
500 * options.
501 */
502static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
503{
504 struct legacy_fs_context *ctx = fc->fs_private;
505 unsigned int size = ctx->data_size;
506 size_t len = 0;
507
508 if (strcmp(param->key, "source") == 0) {
509 if (param->type != fs_value_is_string)
510 return invalf(fc, "VFS: Legacy: Non-string source");
511 if (fc->source)
512 return invalf(fc, "VFS: Legacy: Multiple sources");
513 fc->source = param->string;
514 param->string = NULL;
515 return 0;
516 }
517
David Howells3e1aeb02018-11-01 23:07:25 +0000518 if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
519 return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
520
521 switch (param->type) {
522 case fs_value_is_string:
523 len = 1 + param->size;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500524 fallthrough;
David Howells3e1aeb02018-11-01 23:07:25 +0000525 case fs_value_is_flag:
526 len += strlen(param->key);
527 break;
528 default:
529 return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
530 param->key);
531 }
532
533 if (len > PAGE_SIZE - 2 - size)
534 return invalf(fc, "VFS: Legacy: Cumulative options too large");
535 if (strchr(param->key, ',') ||
536 (param->type == fs_value_is_string &&
537 memchr(param->string, ',', param->size)))
538 return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
539 param->key);
540 if (!ctx->legacy_data) {
541 ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
542 if (!ctx->legacy_data)
543 return -ENOMEM;
544 }
545
546 ctx->legacy_data[size++] = ',';
547 len = strlen(param->key);
548 memcpy(ctx->legacy_data + size, param->key, len);
549 size += len;
550 if (param->type == fs_value_is_string) {
551 ctx->legacy_data[size++] = '=';
552 memcpy(ctx->legacy_data + size, param->string, param->size);
553 size += param->size;
554 }
555 ctx->legacy_data[size] = '\0';
556 ctx->data_size = size;
557 ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
558 return 0;
David Howells9bc61ab2018-11-04 03:19:03 -0500559}
560
561/*
562 * Add monolithic mount data.
563 */
564static int legacy_parse_monolithic(struct fs_context *fc, void *data)
565{
566 struct legacy_fs_context *ctx = fc->fs_private;
David Howells3e1aeb02018-11-01 23:07:25 +0000567
568 if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
569 pr_warn("VFS: Can't mix monolithic and individual options\n");
570 return -EINVAL;
571 }
572
David Howells9bc61ab2018-11-04 03:19:03 -0500573 ctx->legacy_data = data;
David Howells3e1aeb02018-11-01 23:07:25 +0000574 ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
David Howells9bc61ab2018-11-04 03:19:03 -0500575 if (!ctx->legacy_data)
576 return 0;
David Howells3e1aeb02018-11-01 23:07:25 +0000577
David Howells9bc61ab2018-11-04 03:19:03 -0500578 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
579 return 0;
580 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
581}
582
583/*
584 * Get a mountable root with the legacy mount command.
585 */
Al Virof3a09c92018-12-23 18:55:56 -0500586static int legacy_get_tree(struct fs_context *fc)
David Howells9bc61ab2018-11-04 03:19:03 -0500587{
588 struct legacy_fs_context *ctx = fc->fs_private;
589 struct super_block *sb;
590 struct dentry *root;
591
592 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
593 fc->source, ctx->legacy_data);
594 if (IS_ERR(root))
595 return PTR_ERR(root);
596
597 sb = root->d_sb;
598 BUG_ON(!sb);
599
600 fc->root = root;
601 return 0;
602}
603
604/*
David Howells8d0347f2018-11-04 09:28:36 -0500605 * Handle remount.
606 */
Al Virof3a09c92018-12-23 18:55:56 -0500607static int legacy_reconfigure(struct fs_context *fc)
David Howells8d0347f2018-11-04 09:28:36 -0500608{
609 struct legacy_fs_context *ctx = fc->fs_private;
610 struct super_block *sb = fc->root->d_sb;
611
612 if (!sb->s_op->remount_fs)
613 return 0;
614
615 return sb->s_op->remount_fs(sb, &fc->sb_flags,
616 ctx ? ctx->legacy_data : NULL);
617}
618
Al Virof3a09c92018-12-23 18:55:56 -0500619const struct fs_context_operations legacy_fs_context_ops = {
620 .free = legacy_fs_context_free,
Al Viro0b520752018-12-23 16:02:47 -0500621 .dup = legacy_fs_context_dup,
David Howells3e1aeb02018-11-01 23:07:25 +0000622 .parse_param = legacy_parse_param,
Al Virof3a09c92018-12-23 18:55:56 -0500623 .parse_monolithic = legacy_parse_monolithic,
624 .get_tree = legacy_get_tree,
625 .reconfigure = legacy_reconfigure,
626};
627
David Howells8d0347f2018-11-04 09:28:36 -0500628/*
David Howells9bc61ab2018-11-04 03:19:03 -0500629 * Initialise a legacy context for a filesystem that doesn't support
630 * fs_context.
631 */
632static int legacy_init_fs_context(struct fs_context *fc)
633{
634 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
635 if (!fc->fs_private)
636 return -ENOMEM;
Al Virof3a09c92018-12-23 18:55:56 -0500637 fc->ops = &legacy_fs_context_ops;
David Howells9bc61ab2018-11-04 03:19:03 -0500638 return 0;
639}
640
641int parse_monolithic_mount_data(struct fs_context *fc, void *data)
642{
Al Virof3a09c92018-12-23 18:55:56 -0500643 int (*monolithic_mount_data)(struct fs_context *, void *);
David Howells3e1aeb02018-11-01 23:07:25 +0000644
Al Virof3a09c92018-12-23 18:55:56 -0500645 monolithic_mount_data = fc->ops->parse_monolithic;
David Howells3e1aeb02018-11-01 23:07:25 +0000646 if (!monolithic_mount_data)
647 monolithic_mount_data = generic_parse_monolithic;
648
Al Virof3a09c92018-12-23 18:55:56 -0500649 return monolithic_mount_data(fc, data);
David Howells9bc61ab2018-11-04 03:19:03 -0500650}
David Howellsecdab1502018-11-01 23:36:09 +0000651
652/*
653 * Clean up a context after performing an action on it and put it into a state
654 * from where it can be used to reconfigure a superblock.
655 *
656 * Note that here we do only the parts that can't fail; the rest is in
657 * finish_clean_context() below and in between those fs_context is marked
658 * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
659 * successful mount or remount we need to report success to userland.
660 * Trying to do full reinit (for the sake of possible subsequent remount)
661 * and failing to allocate memory would've put us into a nasty situation.
662 * So here we only discard the old state and reinitialization is left
663 * until we actually try to reconfigure.
664 */
665void vfs_clean_context(struct fs_context *fc)
666{
667 if (fc->need_free && fc->ops && fc->ops->free)
668 fc->ops->free(fc);
669 fc->need_free = false;
670 fc->fs_private = NULL;
671 fc->s_fs_info = NULL;
672 fc->sb_flags = 0;
673 security_free_mnt_opts(&fc->security);
David Howellsecdab1502018-11-01 23:36:09 +0000674 kfree(fc->source);
675 fc->source = NULL;
676
677 fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
678 fc->phase = FS_CONTEXT_AWAITING_RECONF;
679}
680
681int finish_clean_context(struct fs_context *fc)
682{
683 int error;
684
685 if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
686 return 0;
687
688 if (fc->fs_type->init_fs_context)
689 error = fc->fs_type->init_fs_context(fc);
690 else
691 error = legacy_init_fs_context(fc);
692 if (unlikely(error)) {
693 fc->phase = FS_CONTEXT_FAILED;
694 return error;
695 }
696 fc->need_free = true;
697 fc->phase = FS_CONTEXT_RECONF_PARAMS;
698 return 0;
699}