fs_context.c 17 KB

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