Blame view

fs/fs_context.c 17.2 KB
b4d0d230c   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
9bc61ab18   David Howells   vfs: Introduce fs...
2
3
4
5
6
  /* Provide a way to create a superblock configuration context within the kernel
   * that allows a superblock to be set up prior to mounting.
   *
   * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
9bc61ab18   David Howells   vfs: Introduce fs...
7
8
9
   */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
007ec26cd   David Howells   vfs: Implement lo...
10
  #include <linux/module.h>
9bc61ab18   David Howells   vfs: Introduce fs...
11
  #include <linux/fs_context.h>
3e1aeb00e   David Howells   vfs: Implement a ...
12
  #include <linux/fs_parser.h>
9bc61ab18   David Howells   vfs: Introduce fs...
13
14
15
16
17
18
19
20
21
22
  #include <linux/fs.h>
  #include <linux/mount.h>
  #include <linux/nsproxy.h>
  #include <linux/slab.h>
  #include <linux/magic.h>
  #include <linux/security.h>
  #include <linux/mnt_namespace.h>
  #include <linux/pid_namespace.h>
  #include <linux/user_namespace.h>
  #include <net/net_namespace.h>
007ec26cd   David Howells   vfs: Implement lo...
23
  #include <asm/sections.h>
9bc61ab18   David Howells   vfs: Introduce fs...
24
25
  #include "mount.h"
  #include "internal.h"
3e1aeb00e   David Howells   vfs: Implement a ...
26
27
28
29
30
  enum legacy_fs_param {
  	LEGACY_FS_UNSET_PARAMS,
  	LEGACY_FS_MONOLITHIC_PARAMS,
  	LEGACY_FS_INDIVIDUAL_PARAMS,
  };
9bc61ab18   David Howells   vfs: Introduce fs...
31
32
33
  struct legacy_fs_context {
  	char			*legacy_data;	/* Data page for legacy filesystems */
  	size_t			data_size;
3e1aeb00e   David Howells   vfs: Implement a ...
34
  	enum legacy_fs_param	param_type;
9bc61ab18   David Howells   vfs: Introduce fs...
35
36
37
  };
  
  static int legacy_init_fs_context(struct fs_context *fc);
3e1aeb00e   David Howells   vfs: Implement a ...
38
39
40
41
  static const struct constant_table common_set_sb_flag[] = {
  	{ "dirsync",	SB_DIRSYNC },
  	{ "lazytime",	SB_LAZYTIME },
  	{ "mand",	SB_MANDLOCK },
3e1aeb00e   David Howells   vfs: Implement a ...
42
43
  	{ "ro",		SB_RDONLY },
  	{ "sync",	SB_SYNCHRONOUS },
34264ae3f   Al Viro   don't bother with...
44
  	{ },
3e1aeb00e   David Howells   vfs: Implement a ...
45
46
47
48
49
50
51
  };
  
  static const struct constant_table common_clear_sb_flag[] = {
  	{ "async",	SB_SYNCHRONOUS },
  	{ "nolazytime",	SB_LAZYTIME },
  	{ "nomand",	SB_MANDLOCK },
  	{ "rw",		SB_RDONLY },
34264ae3f   Al Viro   don't bother with...
52
  	{ },
3e1aeb00e   David Howells   vfs: Implement a ...
53
  };
3e1aeb00e   David Howells   vfs: Implement a ...
54
55
56
57
58
59
  /*
   * Check for a common mount option that manipulates s_flags.
   */
  static int vfs_parse_sb_flag(struct fs_context *fc, const char *key)
  {
  	unsigned int token;
3e1aeb00e   David Howells   vfs: Implement a ...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  
  	token = lookup_constant(common_set_sb_flag, key, 0);
  	if (token) {
  		fc->sb_flags |= token;
  		fc->sb_flags_mask |= token;
  		return 0;
  	}
  
  	token = lookup_constant(common_clear_sb_flag, key, 0);
  	if (token) {
  		fc->sb_flags &= ~token;
  		fc->sb_flags_mask |= token;
  		return 0;
  	}
  
  	return -ENOPARAM;
  }
  
  /**
   * vfs_parse_fs_param - Add a single parameter to a superblock config
   * @fc: The filesystem context to modify
   * @param: The parameter
   *
   * A single mount option in string form is applied to the filesystem context
   * being set up.  Certain standard options (for example "ro") are translated
   * into flag bits without going to the filesystem.  The active security module
   * is allowed to observe and poach options.  Any other options are passed over
   * to the filesystem to parse.
   *
   * This may be called multiple times for a context.
   *
   * Returns 0 on success and a negative error code on failure.  In the event of
   * failure, supplementary error information may have been set.
   */
  int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param)
  {
  	int ret;
  
  	if (!param->key)
  		return invalf(fc, "Unnamed parameter
  ");
  
  	ret = vfs_parse_sb_flag(fc, param->key);
  	if (ret != -ENOPARAM)
  		return ret;
  
  	ret = security_fs_context_parse_param(fc, param);
  	if (ret != -ENOPARAM)
  		/* Param belongs to the LSM or is disallowed by the LSM; so
  		 * don't pass to the FS.
  		 */
  		return ret;
  
  	if (fc->ops->parse_param) {
  		ret = fc->ops->parse_param(fc, param);
  		if (ret != -ENOPARAM)
  			return ret;
  	}
  
  	/* If the filesystem doesn't take any arguments, give it the
  	 * default handling of source.
  	 */
  	if (strcmp(param->key, "source") == 0) {
  		if (param->type != fs_value_is_string)
  			return invalf(fc, "VFS: Non-string source");
  		if (fc->source)
  			return invalf(fc, "VFS: Multiple sources");
  		fc->source = param->string;
  		param->string = NULL;
  		return 0;
  	}
  
  	return invalf(fc, "%s: Unknown parameter '%s'",
  		      fc->fs_type->name, param->key);
  }
  EXPORT_SYMBOL(vfs_parse_fs_param);
  
  /**
   * vfs_parse_fs_string - Convenience function to just parse a string.
   */
  int vfs_parse_fs_string(struct fs_context *fc, const char *key,
  			const char *value, size_t v_size)
  {
  	int ret;
  
  	struct fs_parameter param = {
  		.key	= key,
0f89589a8   Al Viro   Pass consistent p...
147
  		.type	= fs_value_is_flag,
3e1aeb00e   David Howells   vfs: Implement a ...
148
149
  		.size	= v_size,
  	};
0f89589a8   Al Viro   Pass consistent p...
150
  	if (value) {
3e1aeb00e   David Howells   vfs: Implement a ...
151
152
153
  		param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
  		if (!param.string)
  			return -ENOMEM;
0f89589a8   Al Viro   Pass consistent p...
154
  		param.type = fs_value_is_string;
3e1aeb00e   David Howells   vfs: Implement a ...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  	}
  
  	ret = vfs_parse_fs_param(fc, &param);
  	kfree(param.string);
  	return ret;
  }
  EXPORT_SYMBOL(vfs_parse_fs_string);
  
  /**
   * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
   * @ctx: The superblock configuration to fill in.
   * @data: The data to parse
   *
   * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
   * called from the ->monolithic_mount_data() fs_context operation.
   *
   * Returns 0 on success or the error returned by the ->parse_option() fs_context
   * operation on failure.
   */
  int generic_parse_monolithic(struct fs_context *fc, void *data)
  {
  	char *options = data, *key;
  	int ret = 0;
  
  	if (!options)
  		return 0;
  
  	ret = security_sb_eat_lsm_opts(options, &fc->security);
  	if (ret)
  		return ret;
  
  	while ((key = strsep(&options, ",")) != NULL) {
  		if (*key) {
  			size_t v_len = 0;
  			char *value = strchr(key, '=');
  
  			if (value) {
  				if (value == key)
  					continue;
  				*value++ = 0;
  				v_len = strlen(value);
  			}
  			ret = vfs_parse_fs_string(fc, key, value, v_len);
  			if (ret < 0)
  				break;
  		}
  	}
  
  	return ret;
  }
  EXPORT_SYMBOL(generic_parse_monolithic);
9bc61ab18   David Howells   vfs: Introduce fs...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  /**
   * alloc_fs_context - Create a filesystem context.
   * @fs_type: The filesystem type.
   * @reference: The dentry from which this one derives (or NULL)
   * @sb_flags: Filesystem/superblock flags (SB_*)
   * @sb_flags_mask: Applicable members of @sb_flags
   * @purpose: The purpose that this configuration shall be used for.
   *
   * Open a filesystem and create a mount context.  The mount context is
   * initialised with the supplied flags and, if a submount/automount from
   * another superblock (referred to by @reference) is supplied, may have
   * parameters such as namespaces copied across from that superblock.
   */
  static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
  				      struct dentry *reference,
  				      unsigned int sb_flags,
  				      unsigned int sb_flags_mask,
  				      enum fs_context_purpose purpose)
  {
f3a09c920   Al Viro   introduce fs_cont...
225
  	int (*init_fs_context)(struct fs_context *);
9bc61ab18   David Howells   vfs: Introduce fs...
226
227
228
229
230
231
232
233
234
235
236
237
238
  	struct fs_context *fc;
  	int ret = -ENOMEM;
  
  	fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
  	if (!fc)
  		return ERR_PTR(-ENOMEM);
  
  	fc->purpose	= purpose;
  	fc->sb_flags	= sb_flags;
  	fc->sb_flags_mask = sb_flags_mask;
  	fc->fs_type	= get_filesystem(fs_type);
  	fc->cred	= get_current_cred();
  	fc->net_ns	= get_net(current->nsproxy->net_ns);
cc3c0b533   Al Viro   add prefix to fs_...
239
  	fc->log.prefix	= fs_type->name;
9bc61ab18   David Howells   vfs: Introduce fs...
240

24dcb3d90   David Howells   vfs: syscall: Add...
241
  	mutex_init(&fc->uapi_mutex);
9bc61ab18   David Howells   vfs: Introduce fs...
242
243
244
245
  	switch (purpose) {
  	case FS_CONTEXT_FOR_MOUNT:
  		fc->user_ns = get_user_ns(fc->cred->user_ns);
  		break;
e1a91586d   Al Viro   fs_context flavou...
246
247
248
  	case FS_CONTEXT_FOR_SUBMOUNT:
  		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
  		break;
8d0347f6c   David Howells   convert do_remoun...
249
  	case FS_CONTEXT_FOR_RECONFIGURE:
8d0347f6c   David Howells   convert do_remoun...
250
  		atomic_inc(&reference->d_sb->s_active);
1dd9bc08c   Eric Biggers   vfs: set fs_conte...
251
  		fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
8d0347f6c   David Howells   convert do_remoun...
252
253
  		fc->root = dget(reference);
  		break;
9bc61ab18   David Howells   vfs: Introduce fs...
254
  	}
f3a09c920   Al Viro   introduce fs_cont...
255
256
257
258
259
260
  	/* TODO: Make all filesystems support this unconditionally */
  	init_fs_context = fc->fs_type->init_fs_context;
  	if (!init_fs_context)
  		init_fs_context = legacy_init_fs_context;
  
  	ret = init_fs_context(fc);
9bc61ab18   David Howells   vfs: Introduce fs...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  	if (ret < 0)
  		goto err_fc;
  	fc->need_free = true;
  	return fc;
  
  err_fc:
  	put_fs_context(fc);
  	return ERR_PTR(ret);
  }
  
  struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
  					unsigned int sb_flags)
  {
  	return alloc_fs_context(fs_type, NULL, sb_flags, 0,
  					FS_CONTEXT_FOR_MOUNT);
  }
  EXPORT_SYMBOL(fs_context_for_mount);
8d0347f6c   David Howells   convert do_remoun...
278
279
280
281
282
283
284
285
  struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
  					unsigned int sb_flags,
  					unsigned int sb_flags_mask)
  {
  	return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
  				sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
  }
  EXPORT_SYMBOL(fs_context_for_reconfigure);
e1a91586d   Al Viro   fs_context flavou...
286
287
288
289
290
291
  struct fs_context *fs_context_for_submount(struct file_system_type *type,
  					   struct dentry *reference)
  {
  	return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
  }
  EXPORT_SYMBOL(fs_context_for_submount);
c9ce29ed7   Al Viro   vfs_get_tree(): e...
292
293
294
295
296
297
298
  void fc_drop_locked(struct fs_context *fc)
  {
  	struct super_block *sb = fc->root->d_sb;
  	dput(fc->root);
  	fc->root = NULL;
  	deactivate_locked_super(sb);
  }
9bc61ab18   David Howells   vfs: Introduce fs...
299
  static void legacy_fs_context_free(struct fs_context *fc);
8d0347f6c   David Howells   convert do_remoun...
300

9bc61ab18   David Howells   vfs: Introduce fs...
301
  /**
0b52075ee   Al Viro   introduce cloning...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
   * vfs_dup_fc_config: Duplicate a filesystem context.
   * @src_fc: The context to copy.
   */
  struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
  {
  	struct fs_context *fc;
  	int ret;
  
  	if (!src_fc->ops->dup)
  		return ERR_PTR(-EOPNOTSUPP);
  
  	fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
  	if (!fc)
  		return ERR_PTR(-ENOMEM);
24dcb3d90   David Howells   vfs: syscall: Add...
316
  	mutex_init(&fc->uapi_mutex);
0b52075ee   Al Viro   introduce cloning...
317
318
319
320
321
322
323
324
  	fc->fs_private	= NULL;
  	fc->s_fs_info	= NULL;
  	fc->source	= NULL;
  	fc->security	= NULL;
  	get_filesystem(fc->fs_type);
  	get_net(fc->net_ns);
  	get_user_ns(fc->user_ns);
  	get_cred(fc->cred);
cc3c0b533   Al Viro   add prefix to fs_...
325
326
  	if (fc->log.log)
  		refcount_inc(&fc->log.log->usage);
0b52075ee   Al Viro   introduce cloning...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  
  	/* Can't call put until we've called ->dup */
  	ret = fc->ops->dup(fc, src_fc);
  	if (ret < 0)
  		goto err_fc;
  
  	ret = security_fs_context_dup(fc, src_fc);
  	if (ret < 0)
  		goto err_fc;
  	return fc;
  
  err_fc:
  	put_fs_context(fc);
  	return ERR_PTR(ret);
  }
  EXPORT_SYMBOL(vfs_dup_fs_context);
e7582e16a   David Howells   vfs: Implement lo...
343
344
345
346
347
  /**
   * logfc - Log a message to a filesystem context
   * @fc: The filesystem context to log to.
   * @fmt: The format of the buffer.
   */
9f09f649c   Al Viro   teach logfc() to ...
348
  void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
e7582e16a   David Howells   vfs: Implement lo...
349
350
  {
  	va_list va;
9f09f649c   Al Viro   teach logfc() to ...
351
  	struct va_format vaf = {.fmt = fmt, .va = &va};
e7582e16a   David Howells   vfs: Implement lo...
352
353
  
  	va_start(va, fmt);
007ec26cd   David Howells   vfs: Implement lo...
354
  	if (!log) {
9f09f649c   Al Viro   teach logfc() to ...
355
  		switch (level) {
007ec26cd   David Howells   vfs: Implement lo...
356
  		case 'w':
9f09f649c   Al Viro   teach logfc() to ...
357
358
359
  			printk(KERN_WARNING "%s%s%pV
  ", prefix ? prefix : "",
  						prefix ? ": " : "", &vaf);
007ec26cd   David Howells   vfs: Implement lo...
360
361
  			break;
  		case 'e':
9f09f649c   Al Viro   teach logfc() to ...
362
363
364
  			printk(KERN_ERR "%s%s%pV
  ", prefix ? prefix : "",
  						prefix ? ": " : "", &vaf);
007ec26cd   David Howells   vfs: Implement lo...
365
366
  			break;
  		default:
9f09f649c   Al Viro   teach logfc() to ...
367
368
369
  			printk(KERN_NOTICE "%s%s%pV
  ", prefix ? prefix : "",
  						prefix ? ": " : "", &vaf);
007ec26cd   David Howells   vfs: Implement lo...
370
371
  			break;
  		}
007ec26cd   David Howells   vfs: Implement lo...
372
373
374
  	} else {
  		unsigned int logsize = ARRAY_SIZE(log->buffer);
  		u8 index;
9f09f649c   Al Viro   teach logfc() to ...
375
376
377
378
  		char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV
  ", level,
  						prefix ? prefix : "",
  						prefix ? ": " : "", &vaf);
007ec26cd   David Howells   vfs: Implement lo...
379
380
381
382
383
384
385
386
387
388
  
  		index = log->head & (logsize - 1);
  		BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
  			     sizeof(log->tail) != sizeof(u8));
  		if ((u8)(log->head - log->tail) == logsize) {
  			/* The buffer is full, discard the oldest message */
  			if (log->need_free & (1 << index))
  				kfree(log->buffer[index]);
  			log->tail++;
  		}
e7582e16a   David Howells   vfs: Implement lo...
389

9f09f649c   Al Viro   teach logfc() to ...
390
391
392
393
394
  		log->buffer[index] = q ? q : "OOM: Can't store error string";
  		if (q)
  			log->need_free |= 1 << index;
  		else
  			log->need_free &= ~(1 << index);
007ec26cd   David Howells   vfs: Implement lo...
395
396
  		log->head++;
  	}
e7582e16a   David Howells   vfs: Implement lo...
397
398
399
  	va_end(va);
  }
  EXPORT_SYMBOL(logfc);
007ec26cd   David Howells   vfs: Implement lo...
400
401
402
403
404
405
  
  /*
   * Free a logging structure.
   */
  static void put_fc_log(struct fs_context *fc)
  {
cc3c0b533   Al Viro   add prefix to fs_...
406
  	struct fc_log *log = fc->log.log;
007ec26cd   David Howells   vfs: Implement lo...
407
408
409
410
  	int i;
  
  	if (log) {
  		if (refcount_dec_and_test(&log->usage)) {
cc3c0b533   Al Viro   add prefix to fs_...
411
  			fc->log.log = NULL;
007ec26cd   David Howells   vfs: Implement lo...
412
413
414
415
416
417
418
  			for (i = 0; i <= 7; i++)
  				if (log->need_free & (1 << i))
  					kfree(log->buffer[i]);
  			kfree(log);
  		}
  	}
  }
e7582e16a   David Howells   vfs: Implement lo...
419

0b52075ee   Al Viro   introduce cloning...
420
  /**
9bc61ab18   David Howells   vfs: Introduce fs...
421
422
423
424
425
426
427
428
429
430
431
432
433
   * put_fs_context - Dispose of a superblock configuration context.
   * @fc: The context to dispose of.
   */
  void put_fs_context(struct fs_context *fc)
  {
  	struct super_block *sb;
  
  	if (fc->root) {
  		sb = fc->root->d_sb;
  		dput(fc->root);
  		fc->root = NULL;
  		deactivate_super(sb);
  	}
f3a09c920   Al Viro   introduce fs_cont...
434
435
  	if (fc->need_free && fc->ops && fc->ops->free)
  		fc->ops->free(fc);
9bc61ab18   David Howells   vfs: Introduce fs...
436
437
  
  	security_free_mnt_opts(&fc->security);
8d0347f6c   David Howells   convert do_remoun...
438
  	put_net(fc->net_ns);
9bc61ab18   David Howells   vfs: Introduce fs...
439
440
  	put_user_ns(fc->user_ns);
  	put_cred(fc->cred);
007ec26cd   David Howells   vfs: Implement lo...
441
  	put_fc_log(fc);
9bc61ab18   David Howells   vfs: Introduce fs...
442
443
444
445
446
447
448
449
450
451
452
  	put_filesystem(fc->fs_type);
  	kfree(fc->source);
  	kfree(fc);
  }
  EXPORT_SYMBOL(put_fs_context);
  
  /*
   * Free the config for a filesystem that doesn't support fs_context.
   */
  static void legacy_fs_context_free(struct fs_context *fc)
  {
3e1aeb00e   David Howells   vfs: Implement a ...
453
454
455
456
457
458
459
460
461
462
  	struct legacy_fs_context *ctx = fc->fs_private;
  
  	if (ctx) {
  		if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
  			kfree(ctx->legacy_data);
  		kfree(ctx);
  	}
  }
  
  /*
0b52075ee   Al Viro   introduce cloning...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
   * Duplicate a legacy config.
   */
  static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
  {
  	struct legacy_fs_context *ctx;
  	struct legacy_fs_context *src_ctx = src_fc->fs_private;
  
  	ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
  
  	if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
  		ctx->legacy_data = kmemdup(src_ctx->legacy_data,
  					   src_ctx->data_size, GFP_KERNEL);
  		if (!ctx->legacy_data) {
  			kfree(ctx);
  			return -ENOMEM;
  		}
  	}
  
  	fc->fs_private = ctx;
  	return 0;
  }
  
  /*
3e1aeb00e   David Howells   vfs: Implement a ...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
   * Add a parameter to a legacy config.  We build up a comma-separated list of
   * options.
   */
  static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param)
  {
  	struct legacy_fs_context *ctx = fc->fs_private;
  	unsigned int size = ctx->data_size;
  	size_t len = 0;
  
  	if (strcmp(param->key, "source") == 0) {
  		if (param->type != fs_value_is_string)
  			return invalf(fc, "VFS: Legacy: Non-string source");
  		if (fc->source)
  			return invalf(fc, "VFS: Legacy: Multiple sources");
  		fc->source = param->string;
  		param->string = NULL;
  		return 0;
  	}
3e1aeb00e   David Howells   vfs: Implement a ...
506
507
508
509
510
511
  	if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
  		return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
  
  	switch (param->type) {
  	case fs_value_is_string:
  		len = 1 + param->size;
df561f668   Gustavo A. R. Silva   treewide: Use fal...
512
  		fallthrough;
3e1aeb00e   David Howells   vfs: Implement a ...
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  	case fs_value_is_flag:
  		len += strlen(param->key);
  		break;
  	default:
  		return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
  			      param->key);
  	}
  
  	if (len > PAGE_SIZE - 2 - size)
  		return invalf(fc, "VFS: Legacy: Cumulative options too large");
  	if (strchr(param->key, ',') ||
  	    (param->type == fs_value_is_string &&
  	     memchr(param->string, ',', param->size)))
  		return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
  			      param->key);
  	if (!ctx->legacy_data) {
  		ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  		if (!ctx->legacy_data)
  			return -ENOMEM;
  	}
  
  	ctx->legacy_data[size++] = ',';
  	len = strlen(param->key);
  	memcpy(ctx->legacy_data + size, param->key, len);
  	size += len;
  	if (param->type == fs_value_is_string) {
  		ctx->legacy_data[size++] = '=';
  		memcpy(ctx->legacy_data + size, param->string, param->size);
  		size += param->size;
  	}
  	ctx->legacy_data[size] = '\0';
  	ctx->data_size = size;
  	ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
  	return 0;
9bc61ab18   David Howells   vfs: Introduce fs...
547
548
549
550
551
552
553
554
  }
  
  /*
   * Add monolithic mount data.
   */
  static int legacy_parse_monolithic(struct fs_context *fc, void *data)
  {
  	struct legacy_fs_context *ctx = fc->fs_private;
3e1aeb00e   David Howells   vfs: Implement a ...
555
556
557
558
559
560
  
  	if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
  		pr_warn("VFS: Can't mix monolithic and individual options
  ");
  		return -EINVAL;
  	}
9bc61ab18   David Howells   vfs: Introduce fs...
561
  	ctx->legacy_data = data;
3e1aeb00e   David Howells   vfs: Implement a ...
562
  	ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
9bc61ab18   David Howells   vfs: Introduce fs...
563
564
  	if (!ctx->legacy_data)
  		return 0;
3e1aeb00e   David Howells   vfs: Implement a ...
565

9bc61ab18   David Howells   vfs: Introduce fs...
566
567
568
569
570
571
572
573
  	if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
  		return 0;
  	return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
  }
  
  /*
   * Get a mountable root with the legacy mount command.
   */
f3a09c920   Al Viro   introduce fs_cont...
574
  static int legacy_get_tree(struct fs_context *fc)
9bc61ab18   David Howells   vfs: Introduce fs...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  {
  	struct legacy_fs_context *ctx = fc->fs_private;
  	struct super_block *sb;
  	struct dentry *root;
  
  	root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
  				      fc->source, ctx->legacy_data);
  	if (IS_ERR(root))
  		return PTR_ERR(root);
  
  	sb = root->d_sb;
  	BUG_ON(!sb);
  
  	fc->root = root;
  	return 0;
  }
  
  /*
8d0347f6c   David Howells   convert do_remoun...
593
594
   * Handle remount.
   */
f3a09c920   Al Viro   introduce fs_cont...
595
  static int legacy_reconfigure(struct fs_context *fc)
8d0347f6c   David Howells   convert do_remoun...
596
597
598
599
600
601
602
603
604
605
  {
  	struct legacy_fs_context *ctx = fc->fs_private;
  	struct super_block *sb = fc->root->d_sb;
  
  	if (!sb->s_op->remount_fs)
  		return 0;
  
  	return sb->s_op->remount_fs(sb, &fc->sb_flags,
  				    ctx ? ctx->legacy_data : NULL);
  }
f3a09c920   Al Viro   introduce fs_cont...
606
607
  const struct fs_context_operations legacy_fs_context_ops = {
  	.free			= legacy_fs_context_free,
0b52075ee   Al Viro   introduce cloning...
608
  	.dup			= legacy_fs_context_dup,
3e1aeb00e   David Howells   vfs: Implement a ...
609
  	.parse_param		= legacy_parse_param,
f3a09c920   Al Viro   introduce fs_cont...
610
611
612
613
  	.parse_monolithic	= legacy_parse_monolithic,
  	.get_tree		= legacy_get_tree,
  	.reconfigure		= legacy_reconfigure,
  };
8d0347f6c   David Howells   convert do_remoun...
614
  /*
9bc61ab18   David Howells   vfs: Introduce fs...
615
616
617
618
619
620
621
622
   * Initialise a legacy context for a filesystem that doesn't support
   * fs_context.
   */
  static int legacy_init_fs_context(struct fs_context *fc)
  {
  	fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
  	if (!fc->fs_private)
  		return -ENOMEM;
f3a09c920   Al Viro   introduce fs_cont...
623
  	fc->ops = &legacy_fs_context_ops;
9bc61ab18   David Howells   vfs: Introduce fs...
624
625
626
627
628
  	return 0;
  }
  
  int parse_monolithic_mount_data(struct fs_context *fc, void *data)
  {
f3a09c920   Al Viro   introduce fs_cont...
629
  	int (*monolithic_mount_data)(struct fs_context *, void *);
3e1aeb00e   David Howells   vfs: Implement a ...
630

f3a09c920   Al Viro   introduce fs_cont...
631
  	monolithic_mount_data = fc->ops->parse_monolithic;
3e1aeb00e   David Howells   vfs: Implement a ...
632
633
  	if (!monolithic_mount_data)
  		monolithic_mount_data = generic_parse_monolithic;
f3a09c920   Al Viro   introduce fs_cont...
634
  	return monolithic_mount_data(fc, data);
9bc61ab18   David Howells   vfs: Introduce fs...
635
  }
ecdab150f   David Howells   vfs: syscall: Add...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  
  /*
   * Clean up a context after performing an action on it and put it into a state
   * from where it can be used to reconfigure a superblock.
   *
   * Note that here we do only the parts that can't fail; the rest is in
   * finish_clean_context() below and in between those fs_context is marked
   * FS_CONTEXT_AWAITING_RECONF.  The reason for splitup is that after
   * successful mount or remount we need to report success to userland.
   * Trying to do full reinit (for the sake of possible subsequent remount)
   * and failing to allocate memory would've put us into a nasty situation.
   * So here we only discard the old state and reinitialization is left
   * until we actually try to reconfigure.
   */
  void vfs_clean_context(struct fs_context *fc)
  {
  	if (fc->need_free && fc->ops && fc->ops->free)
  		fc->ops->free(fc);
  	fc->need_free = false;
  	fc->fs_private = NULL;
  	fc->s_fs_info = NULL;
  	fc->sb_flags = 0;
  	security_free_mnt_opts(&fc->security);
ecdab150f   David Howells   vfs: syscall: Add...
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  	kfree(fc->source);
  	fc->source = NULL;
  
  	fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
  	fc->phase = FS_CONTEXT_AWAITING_RECONF;
  }
  
  int finish_clean_context(struct fs_context *fc)
  {
  	int error;
  
  	if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
  		return 0;
  
  	if (fc->fs_type->init_fs_context)
  		error = fc->fs_type->init_fs_context(fc);
  	else
  		error = legacy_init_fs_context(fc);
  	if (unlikely(error)) {
  		fc->phase = FS_CONTEXT_FAILED;
  		return error;
  	}
  	fc->need_free = true;
  	fc->phase = FS_CONTEXT_RECONF_PARAMS;
  	return 0;
  }