fs_context.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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_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. * @fc: Filesystem context.
  144. * @key: Parameter name.
  145. * @value: Default value.
  146. * @v_size: Maximum number of bytes in the value.
  147. */
  148. int vfs_parse_fs_string(struct fs_context *fc, const char *key,
  149. const char *value, size_t v_size)
  150. {
  151. int ret;
  152. struct fs_parameter param = {
  153. .key = key,
  154. .type = fs_value_is_flag,
  155. .size = v_size,
  156. };
  157. if (value) {
  158. param.string = kmemdup_nul(value, v_size, GFP_KERNEL);
  159. if (!param.string)
  160. return -ENOMEM;
  161. param.type = fs_value_is_string;
  162. }
  163. ret = vfs_parse_fs_param(fc, &param);
  164. kfree(param.string);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(vfs_parse_fs_string);
  168. /**
  169. * vfs_parse_monolithic_sep - Parse key[=val][,key[=val]]* mount data
  170. * @fc: The superblock configuration to fill in.
  171. * @data: The data to parse
  172. * @sep: callback for separating next option
  173. *
  174. * Parse a blob of data that's in key[=val][,key[=val]]* form with a custom
  175. * option separator callback.
  176. *
  177. * Returns 0 on success or the error returned by the ->parse_option() fs_context
  178. * operation on failure.
  179. */
  180. int vfs_parse_monolithic_sep(struct fs_context *fc, void *data,
  181. char *(*sep)(char **))
  182. {
  183. char *options = data, *key;
  184. int ret = 0;
  185. if (!options)
  186. return 0;
  187. ret = security_sb_eat_lsm_opts(options, &fc->security);
  188. if (ret)
  189. return ret;
  190. while ((key = sep(&options)) != NULL) {
  191. if (*key) {
  192. size_t v_len = 0;
  193. char *value = strchr(key, '=');
  194. if (value) {
  195. if (value == key)
  196. continue;
  197. *value++ = 0;
  198. v_len = strlen(value);
  199. }
  200. ret = vfs_parse_fs_string(fc, key, value, v_len);
  201. if (ret < 0)
  202. break;
  203. }
  204. }
  205. return ret;
  206. }
  207. EXPORT_SYMBOL(vfs_parse_monolithic_sep);
  208. static char *vfs_parse_comma_sep(char **s)
  209. {
  210. return strsep(s, ",");
  211. }
  212. /**
  213. * generic_parse_monolithic - Parse key[=val][,key[=val]]* mount data
  214. * @fc: The superblock configuration to fill in.
  215. * @data: The data to parse
  216. *
  217. * Parse a blob of data that's in key[=val][,key[=val]]* form. This can be
  218. * called from the ->monolithic_mount_data() fs_context operation.
  219. *
  220. * Returns 0 on success or the error returned by the ->parse_option() fs_context
  221. * operation on failure.
  222. */
  223. int generic_parse_monolithic(struct fs_context *fc, void *data)
  224. {
  225. return vfs_parse_monolithic_sep(fc, data, vfs_parse_comma_sep);
  226. }
  227. EXPORT_SYMBOL(generic_parse_monolithic);
  228. /**
  229. * alloc_fs_context - Create a filesystem context.
  230. * @fs_type: The filesystem type.
  231. * @reference: The dentry from which this one derives (or NULL)
  232. * @sb_flags: Filesystem/superblock flags (SB_*)
  233. * @sb_flags_mask: Applicable members of @sb_flags
  234. * @purpose: The purpose that this configuration shall be used for.
  235. *
  236. * Open a filesystem and create a mount context. The mount context is
  237. * initialised with the supplied flags and, if a submount/automount from
  238. * another superblock (referred to by @reference) is supplied, may have
  239. * parameters such as namespaces copied across from that superblock.
  240. */
  241. static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
  242. struct dentry *reference,
  243. unsigned int sb_flags,
  244. unsigned int sb_flags_mask,
  245. enum fs_context_purpose purpose)
  246. {
  247. int (*init_fs_context)(struct fs_context *);
  248. struct fs_context *fc;
  249. int ret = -ENOMEM;
  250. fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL_ACCOUNT);
  251. if (!fc)
  252. return ERR_PTR(-ENOMEM);
  253. fc->purpose = purpose;
  254. fc->sb_flags = sb_flags;
  255. fc->sb_flags_mask = sb_flags_mask;
  256. fc->fs_type = get_filesystem(fs_type);
  257. fc->cred = get_current_cred();
  258. fc->net_ns = get_net(current->nsproxy->net_ns);
  259. fc->log.prefix = fs_type->name;
  260. mutex_init(&fc->uapi_mutex);
  261. switch (purpose) {
  262. case FS_CONTEXT_FOR_MOUNT:
  263. fc->user_ns = get_user_ns(fc->cred->user_ns);
  264. break;
  265. case FS_CONTEXT_FOR_SUBMOUNT:
  266. fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
  267. break;
  268. case FS_CONTEXT_FOR_RECONFIGURE:
  269. atomic_inc(&reference->d_sb->s_active);
  270. fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
  271. fc->root = dget(reference);
  272. break;
  273. }
  274. /* TODO: Make all filesystems support this unconditionally */
  275. init_fs_context = fc->fs_type->init_fs_context;
  276. if (!init_fs_context)
  277. init_fs_context = legacy_init_fs_context;
  278. ret = init_fs_context(fc);
  279. if (ret < 0)
  280. goto err_fc;
  281. fc->need_free = true;
  282. return fc;
  283. err_fc:
  284. put_fs_context(fc);
  285. return ERR_PTR(ret);
  286. }
  287. struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
  288. unsigned int sb_flags)
  289. {
  290. return alloc_fs_context(fs_type, NULL, sb_flags, 0,
  291. FS_CONTEXT_FOR_MOUNT);
  292. }
  293. EXPORT_SYMBOL(fs_context_for_mount);
  294. struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
  295. unsigned int sb_flags,
  296. unsigned int sb_flags_mask)
  297. {
  298. return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
  299. sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
  300. }
  301. EXPORT_SYMBOL(fs_context_for_reconfigure);
  302. /**
  303. * fs_context_for_submount: allocate a new fs_context for a submount
  304. * @type: file_system_type of the new context
  305. * @reference: reference dentry from which to copy relevant info
  306. *
  307. * Allocate a new fs_context suitable for a submount. This also ensures that
  308. * the fc->security object is inherited from @reference (if needed).
  309. */
  310. struct fs_context *fs_context_for_submount(struct file_system_type *type,
  311. struct dentry *reference)
  312. {
  313. struct fs_context *fc;
  314. int ret;
  315. fc = alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
  316. if (IS_ERR(fc))
  317. return fc;
  318. ret = security_fs_context_submount(fc, reference->d_sb);
  319. if (ret) {
  320. put_fs_context(fc);
  321. return ERR_PTR(ret);
  322. }
  323. return fc;
  324. }
  325. EXPORT_SYMBOL(fs_context_for_submount);
  326. void fc_drop_locked(struct fs_context *fc)
  327. {
  328. struct super_block *sb = fc->root->d_sb;
  329. dput(fc->root);
  330. fc->root = NULL;
  331. deactivate_locked_super(sb);
  332. }
  333. static void legacy_fs_context_free(struct fs_context *fc);
  334. /**
  335. * vfs_dup_fs_context - Duplicate a filesystem context.
  336. * @src_fc: The context to copy.
  337. */
  338. struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc)
  339. {
  340. struct fs_context *fc;
  341. int ret;
  342. if (!src_fc->ops->dup)
  343. return ERR_PTR(-EOPNOTSUPP);
  344. fc = kmemdup(src_fc, sizeof(struct fs_context), GFP_KERNEL);
  345. if (!fc)
  346. return ERR_PTR(-ENOMEM);
  347. mutex_init(&fc->uapi_mutex);
  348. fc->fs_private = NULL;
  349. fc->s_fs_info = NULL;
  350. fc->source = NULL;
  351. fc->security = NULL;
  352. get_filesystem(fc->fs_type);
  353. get_net(fc->net_ns);
  354. get_user_ns(fc->user_ns);
  355. get_cred(fc->cred);
  356. if (fc->log.log)
  357. refcount_inc(&fc->log.log->usage);
  358. /* Can't call put until we've called ->dup */
  359. ret = fc->ops->dup(fc, src_fc);
  360. if (ret < 0)
  361. goto err_fc;
  362. ret = security_fs_context_dup(fc, src_fc);
  363. if (ret < 0)
  364. goto err_fc;
  365. return fc;
  366. err_fc:
  367. put_fs_context(fc);
  368. return ERR_PTR(ret);
  369. }
  370. EXPORT_SYMBOL(vfs_dup_fs_context);
  371. /**
  372. * logfc - Log a message to a filesystem context
  373. * @log: The filesystem context to log to, or NULL to use printk.
  374. * @prefix: A string to prefix the output with, or NULL.
  375. * @level: 'w' for a warning, 'e' for an error. Anything else is a notice.
  376. * @fmt: The format of the buffer.
  377. */
  378. void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
  379. {
  380. va_list va;
  381. struct va_format vaf = {.fmt = fmt, .va = &va};
  382. va_start(va, fmt);
  383. if (!log) {
  384. switch (level) {
  385. case 'w':
  386. printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
  387. prefix ? ": " : "", &vaf);
  388. break;
  389. case 'e':
  390. printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
  391. prefix ? ": " : "", &vaf);
  392. break;
  393. default:
  394. printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
  395. prefix ? ": " : "", &vaf);
  396. break;
  397. }
  398. } else {
  399. unsigned int logsize = ARRAY_SIZE(log->buffer);
  400. u8 index;
  401. char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
  402. prefix ? prefix : "",
  403. prefix ? ": " : "", &vaf);
  404. index = log->head & (logsize - 1);
  405. BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
  406. sizeof(log->tail) != sizeof(u8));
  407. if ((u8)(log->head - log->tail) == logsize) {
  408. /* The buffer is full, discard the oldest message */
  409. if (log->need_free & (1 << index))
  410. kfree(log->buffer[index]);
  411. log->tail++;
  412. }
  413. log->buffer[index] = q ? q : "OOM: Can't store error string";
  414. if (q)
  415. log->need_free |= 1 << index;
  416. else
  417. log->need_free &= ~(1 << index);
  418. log->head++;
  419. }
  420. va_end(va);
  421. }
  422. EXPORT_SYMBOL(logfc);
  423. /*
  424. * Free a logging structure.
  425. */
  426. static void put_fc_log(struct fs_context *fc)
  427. {
  428. struct fc_log *log = fc->log.log;
  429. int i;
  430. if (log) {
  431. if (refcount_dec_and_test(&log->usage)) {
  432. fc->log.log = NULL;
  433. for (i = 0; i <= 7; i++)
  434. if (log->need_free & (1 << i))
  435. kfree(log->buffer[i]);
  436. kfree(log);
  437. }
  438. }
  439. }
  440. /**
  441. * put_fs_context - Dispose of a superblock configuration context.
  442. * @fc: The context to dispose of.
  443. */
  444. void put_fs_context(struct fs_context *fc)
  445. {
  446. struct super_block *sb;
  447. if (fc->root) {
  448. sb = fc->root->d_sb;
  449. dput(fc->root);
  450. fc->root = NULL;
  451. deactivate_super(sb);
  452. }
  453. if (fc->need_free && fc->ops && fc->ops->free)
  454. fc->ops->free(fc);
  455. security_free_mnt_opts(&fc->security);
  456. put_net(fc->net_ns);
  457. put_user_ns(fc->user_ns);
  458. put_cred(fc->cred);
  459. put_fc_log(fc);
  460. put_filesystem(fc->fs_type);
  461. kfree(fc->source);
  462. kfree(fc);
  463. }
  464. EXPORT_SYMBOL(put_fs_context);
  465. /*
  466. * Free the config for a filesystem that doesn't support fs_context.
  467. */
  468. static void legacy_fs_context_free(struct fs_context *fc)
  469. {
  470. struct legacy_fs_context *ctx = fc->fs_private;
  471. if (ctx) {
  472. if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS)
  473. kfree(ctx->legacy_data);
  474. kfree(ctx);
  475. }
  476. }
  477. /*
  478. * Duplicate a legacy config.
  479. */
  480. static int legacy_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc)
  481. {
  482. struct legacy_fs_context *ctx;
  483. struct legacy_fs_context *src_ctx = src_fc->fs_private;
  484. ctx = kmemdup(src_ctx, sizeof(*src_ctx), GFP_KERNEL);
  485. if (!ctx)
  486. return -ENOMEM;
  487. if (ctx->param_type == LEGACY_FS_INDIVIDUAL_PARAMS) {
  488. ctx->legacy_data = kmemdup(src_ctx->legacy_data,
  489. src_ctx->data_size, GFP_KERNEL);
  490. if (!ctx->legacy_data) {
  491. kfree(ctx);
  492. return -ENOMEM;
  493. }
  494. }
  495. fc->fs_private = ctx;
  496. return 0;
  497. }
  498. /*
  499. * Add a parameter to a legacy config. We build up a comma-separated list of
  500. * options.
  501. */
  502. static 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. int ret;
  508. ret = vfs_parse_fs_param_source(fc, param);
  509. if (ret != -ENOPARAM)
  510. return ret;
  511. if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS)
  512. return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options");
  513. switch (param->type) {
  514. case fs_value_is_string:
  515. len = 1 + param->size;
  516. fallthrough;
  517. case fs_value_is_flag:
  518. len += strlen(param->key);
  519. break;
  520. default:
  521. return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported",
  522. param->key);
  523. }
  524. if (size + len + 2 > PAGE_SIZE)
  525. return invalf(fc, "VFS: Legacy: Cumulative options too large");
  526. if (strchr(param->key, ',') ||
  527. (param->type == fs_value_is_string &&
  528. memchr(param->string, ',', param->size)))
  529. return invalf(fc, "VFS: Legacy: Option '%s' contained comma",
  530. param->key);
  531. if (!ctx->legacy_data) {
  532. ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  533. if (!ctx->legacy_data)
  534. return -ENOMEM;
  535. }
  536. if (size)
  537. ctx->legacy_data[size++] = ',';
  538. len = strlen(param->key);
  539. memcpy(ctx->legacy_data + size, param->key, len);
  540. size += len;
  541. if (param->type == fs_value_is_string) {
  542. ctx->legacy_data[size++] = '=';
  543. memcpy(ctx->legacy_data + size, param->string, param->size);
  544. size += param->size;
  545. }
  546. ctx->legacy_data[size] = '\0';
  547. ctx->data_size = size;
  548. ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS;
  549. return 0;
  550. }
  551. /*
  552. * Add monolithic mount data.
  553. */
  554. static int legacy_parse_monolithic(struct fs_context *fc, void *data)
  555. {
  556. struct legacy_fs_context *ctx = fc->fs_private;
  557. if (ctx->param_type != LEGACY_FS_UNSET_PARAMS) {
  558. pr_warn("VFS: Can't mix monolithic and individual options\n");
  559. return -EINVAL;
  560. }
  561. ctx->legacy_data = data;
  562. ctx->param_type = LEGACY_FS_MONOLITHIC_PARAMS;
  563. if (!ctx->legacy_data)
  564. return 0;
  565. if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
  566. return 0;
  567. return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
  568. }
  569. /*
  570. * Get a mountable root with the legacy mount command.
  571. */
  572. static int legacy_get_tree(struct fs_context *fc)
  573. {
  574. struct legacy_fs_context *ctx = fc->fs_private;
  575. struct super_block *sb;
  576. struct dentry *root;
  577. root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
  578. fc->source, ctx->legacy_data);
  579. if (IS_ERR(root))
  580. return PTR_ERR(root);
  581. sb = root->d_sb;
  582. BUG_ON(!sb);
  583. fc->root = root;
  584. return 0;
  585. }
  586. /*
  587. * Handle remount.
  588. */
  589. static int legacy_reconfigure(struct fs_context *fc)
  590. {
  591. struct legacy_fs_context *ctx = fc->fs_private;
  592. struct super_block *sb = fc->root->d_sb;
  593. if (!sb->s_op->remount_fs)
  594. return 0;
  595. return sb->s_op->remount_fs(sb, &fc->sb_flags,
  596. ctx ? ctx->legacy_data : NULL);
  597. }
  598. const struct fs_context_operations legacy_fs_context_ops = {
  599. .free = legacy_fs_context_free,
  600. .dup = legacy_fs_context_dup,
  601. .parse_param = legacy_parse_param,
  602. .parse_monolithic = legacy_parse_monolithic,
  603. .get_tree = legacy_get_tree,
  604. .reconfigure = legacy_reconfigure,
  605. };
  606. /*
  607. * Initialise a legacy context for a filesystem that doesn't support
  608. * fs_context.
  609. */
  610. static int legacy_init_fs_context(struct fs_context *fc)
  611. {
  612. fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL_ACCOUNT);
  613. if (!fc->fs_private)
  614. return -ENOMEM;
  615. fc->ops = &legacy_fs_context_ops;
  616. return 0;
  617. }
  618. int parse_monolithic_mount_data(struct fs_context *fc, void *data)
  619. {
  620. int (*monolithic_mount_data)(struct fs_context *, void *);
  621. monolithic_mount_data = fc->ops->parse_monolithic;
  622. if (!monolithic_mount_data)
  623. monolithic_mount_data = generic_parse_monolithic;
  624. return monolithic_mount_data(fc, data);
  625. }
  626. /*
  627. * Clean up a context after performing an action on it and put it into a state
  628. * from where it can be used to reconfigure a superblock.
  629. *
  630. * Note that here we do only the parts that can't fail; the rest is in
  631. * finish_clean_context() below and in between those fs_context is marked
  632. * FS_CONTEXT_AWAITING_RECONF. The reason for splitup is that after
  633. * successful mount or remount we need to report success to userland.
  634. * Trying to do full reinit (for the sake of possible subsequent remount)
  635. * and failing to allocate memory would've put us into a nasty situation.
  636. * So here we only discard the old state and reinitialization is left
  637. * until we actually try to reconfigure.
  638. */
  639. void vfs_clean_context(struct fs_context *fc)
  640. {
  641. if (fc->need_free && fc->ops && fc->ops->free)
  642. fc->ops->free(fc);
  643. fc->need_free = false;
  644. fc->fs_private = NULL;
  645. fc->s_fs_info = NULL;
  646. fc->sb_flags = 0;
  647. security_free_mnt_opts(&fc->security);
  648. kfree(fc->source);
  649. fc->source = NULL;
  650. fc->exclusive = false;
  651. fc->purpose = FS_CONTEXT_FOR_RECONFIGURE;
  652. fc->phase = FS_CONTEXT_AWAITING_RECONF;
  653. }
  654. int finish_clean_context(struct fs_context *fc)
  655. {
  656. int error;
  657. if (fc->phase != FS_CONTEXT_AWAITING_RECONF)
  658. return 0;
  659. if (fc->fs_type->init_fs_context)
  660. error = fc->fs_type->init_fs_context(fc);
  661. else
  662. error = legacy_init_fs_context(fc);
  663. if (unlikely(error)) {
  664. fc->phase = FS_CONTEXT_FAILED;
  665. return error;
  666. }
  667. fc->need_free = true;
  668. fc->phase = FS_CONTEXT_RECONF_PARAMS;
  669. return 0;
  670. }