fs_context.c 18 KB

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