fs_context.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Filesystem superblock creation and reconfiguration context.
  3. *
  4. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #ifndef _LINUX_FS_CONTEXT_H
  8. #define _LINUX_FS_CONTEXT_H
  9. #include <linux/kernel.h>
  10. #include <linux/refcount.h>
  11. #include <linux/errno.h>
  12. #include <linux/security.h>
  13. #include <linux/mutex.h>
  14. struct cred;
  15. struct dentry;
  16. struct file_operations;
  17. struct file_system_type;
  18. struct mnt_namespace;
  19. struct net;
  20. struct pid_namespace;
  21. struct super_block;
  22. struct user_namespace;
  23. struct vfsmount;
  24. struct path;
  25. enum fs_context_purpose {
  26. FS_CONTEXT_FOR_MOUNT, /* New superblock for explicit mount */
  27. FS_CONTEXT_FOR_SUBMOUNT, /* New superblock for automatic submount */
  28. FS_CONTEXT_FOR_RECONFIGURE, /* Superblock reconfiguration (remount) */
  29. };
  30. /*
  31. * Userspace usage phase for fsopen/fspick.
  32. */
  33. enum fs_context_phase {
  34. FS_CONTEXT_CREATE_PARAMS, /* Loading params for sb creation */
  35. FS_CONTEXT_CREATING, /* A superblock is being created */
  36. FS_CONTEXT_AWAITING_MOUNT, /* Superblock created, awaiting fsmount() */
  37. FS_CONTEXT_AWAITING_RECONF, /* Awaiting initialisation for reconfiguration */
  38. FS_CONTEXT_RECONF_PARAMS, /* Loading params for reconfiguration */
  39. FS_CONTEXT_RECONFIGURING, /* Reconfiguring the superblock */
  40. FS_CONTEXT_FAILED, /* Failed to correctly transition a context */
  41. };
  42. /*
  43. * Type of parameter value.
  44. */
  45. enum fs_value_type {
  46. fs_value_is_undefined,
  47. fs_value_is_flag, /* Value not given a value */
  48. fs_value_is_string, /* Value is a string */
  49. fs_value_is_blob, /* Value is a binary blob */
  50. fs_value_is_filename, /* Value is a filename* + dirfd */
  51. fs_value_is_file, /* Value is a file* */
  52. };
  53. /*
  54. * Configuration parameter.
  55. */
  56. struct fs_parameter {
  57. const char *key; /* Parameter name */
  58. enum fs_value_type type:8; /* The type of value here */
  59. union {
  60. char *string;
  61. void *blob;
  62. struct filename *name;
  63. struct file *file;
  64. };
  65. size_t size;
  66. int dirfd;
  67. };
  68. struct p_log {
  69. const char *prefix;
  70. struct fc_log *log;
  71. };
  72. /*
  73. * Filesystem context for holding the parameters used in the creation or
  74. * reconfiguration of a superblock.
  75. *
  76. * Superblock creation fills in ->root whereas reconfiguration begins with this
  77. * already set.
  78. *
  79. * See Documentation/filesystems/mount_api.rst
  80. */
  81. struct fs_context {
  82. const struct fs_context_operations *ops;
  83. struct mutex uapi_mutex; /* Userspace access mutex */
  84. struct file_system_type *fs_type;
  85. void *fs_private; /* The filesystem's context */
  86. void *sget_key;
  87. struct dentry *root; /* The root and superblock */
  88. struct user_namespace *user_ns; /* The user namespace for this mount */
  89. struct net *net_ns; /* The network namespace for this mount */
  90. const struct cred *cred; /* The mounter's credentials */
  91. struct p_log log; /* Logging buffer */
  92. const char *source; /* The source name (eg. dev path) */
  93. void *security; /* Linux S&M options */
  94. void *s_fs_info; /* Proposed s_fs_info */
  95. unsigned int sb_flags; /* Proposed superblock flags (SB_*) */
  96. unsigned int sb_flags_mask; /* Superblock flags that were changed */
  97. unsigned int s_iflags; /* OR'd with sb->s_iflags */
  98. unsigned int lsm_flags; /* Information flags from the fs to the LSM */
  99. enum fs_context_purpose purpose:8;
  100. enum fs_context_phase phase:8; /* The phase the context is in */
  101. bool need_free:1; /* Need to call ops->free() */
  102. bool global:1; /* Goes into &init_user_ns */
  103. bool oldapi:1; /* Coming from mount(2) */
  104. };
  105. struct fs_context_operations {
  106. void (*free)(struct fs_context *fc);
  107. int (*dup)(struct fs_context *fc, struct fs_context *src_fc);
  108. int (*parse_param)(struct fs_context *fc, struct fs_parameter *param);
  109. int (*parse_monolithic)(struct fs_context *fc, void *data);
  110. int (*get_tree)(struct fs_context *fc);
  111. int (*reconfigure)(struct fs_context *fc);
  112. };
  113. /*
  114. * fs_context manipulation functions.
  115. */
  116. extern struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
  117. unsigned int sb_flags);
  118. extern struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
  119. unsigned int sb_flags,
  120. unsigned int sb_flags_mask);
  121. extern struct fs_context *fs_context_for_submount(struct file_system_type *fs_type,
  122. struct dentry *reference);
  123. extern struct fs_context *vfs_dup_fs_context(struct fs_context *fc);
  124. extern int vfs_parse_fs_param(struct fs_context *fc, struct fs_parameter *param);
  125. extern int vfs_parse_fs_string(struct fs_context *fc, const char *key,
  126. const char *value, size_t v_size);
  127. extern int generic_parse_monolithic(struct fs_context *fc, void *data);
  128. extern int vfs_get_tree(struct fs_context *fc);
  129. extern void put_fs_context(struct fs_context *fc);
  130. extern int vfs_parse_fs_param_source(struct fs_context *fc,
  131. struct fs_parameter *param);
  132. extern void fc_drop_locked(struct fs_context *fc);
  133. int reconfigure_single(struct super_block *s,
  134. int flags, void *data);
  135. /*
  136. * sget() wrappers to be called from the ->get_tree() op.
  137. */
  138. enum vfs_get_super_keying {
  139. vfs_get_single_super, /* Only one such superblock may exist */
  140. vfs_get_single_reconf_super, /* As above, but reconfigure if it exists */
  141. vfs_get_keyed_super, /* Superblocks with different s_fs_info keys may exist */
  142. vfs_get_independent_super, /* Multiple independent superblocks may exist */
  143. };
  144. extern int vfs_get_super(struct fs_context *fc,
  145. enum vfs_get_super_keying keying,
  146. int (*fill_super)(struct super_block *sb,
  147. struct fs_context *fc));
  148. extern int get_tree_nodev(struct fs_context *fc,
  149. int (*fill_super)(struct super_block *sb,
  150. struct fs_context *fc));
  151. extern int get_tree_single(struct fs_context *fc,
  152. int (*fill_super)(struct super_block *sb,
  153. struct fs_context *fc));
  154. extern int get_tree_single_reconf(struct fs_context *fc,
  155. int (*fill_super)(struct super_block *sb,
  156. struct fs_context *fc));
  157. extern int get_tree_keyed(struct fs_context *fc,
  158. int (*fill_super)(struct super_block *sb,
  159. struct fs_context *fc),
  160. void *key);
  161. extern int get_tree_bdev(struct fs_context *fc,
  162. int (*fill_super)(struct super_block *sb,
  163. struct fs_context *fc));
  164. extern const struct file_operations fscontext_fops;
  165. /*
  166. * Mount error, warning and informational message logging. This structure is
  167. * shareable between a mount and a subordinate mount.
  168. */
  169. struct fc_log {
  170. refcount_t usage;
  171. u8 head; /* Insertion index in buffer[] */
  172. u8 tail; /* Removal index in buffer[] */
  173. u8 need_free; /* Mask of kfree'able items in buffer[] */
  174. struct module *owner; /* Owner module for strings that don't then need freeing */
  175. char *buffer[8];
  176. };
  177. extern __attribute__((format(printf, 4, 5)))
  178. void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...);
  179. #define __logfc(fc, l, fmt, ...) logfc((fc)->log.log, NULL, \
  180. l, fmt, ## __VA_ARGS__)
  181. #define __plog(p, l, fmt, ...) logfc((p)->log, (p)->prefix, \
  182. l, fmt, ## __VA_ARGS__)
  183. /**
  184. * infof - Store supplementary informational message
  185. * @fc: The context in which to log the informational message
  186. * @fmt: The format string
  187. *
  188. * Store the supplementary informational message for the process if the process
  189. * has enabled the facility.
  190. */
  191. #define infof(fc, fmt, ...) __logfc(fc, 'i', fmt, ## __VA_ARGS__)
  192. #define info_plog(p, fmt, ...) __plog(p, 'i', fmt, ## __VA_ARGS__)
  193. #define infofc(p, fmt, ...) __plog((&(fc)->log), 'i', fmt, ## __VA_ARGS__)
  194. /**
  195. * warnf - Store supplementary warning message
  196. * @fc: The context in which to log the error message
  197. * @fmt: The format string
  198. *
  199. * Store the supplementary warning message for the process if the process has
  200. * enabled the facility.
  201. */
  202. #define warnf(fc, fmt, ...) __logfc(fc, 'w', fmt, ## __VA_ARGS__)
  203. #define warn_plog(p, fmt, ...) __plog(p, 'w', fmt, ## __VA_ARGS__)
  204. #define warnfc(fc, fmt, ...) __plog((&(fc)->log), 'w', fmt, ## __VA_ARGS__)
  205. /**
  206. * errorf - Store supplementary error message
  207. * @fc: The context in which to log the error message
  208. * @fmt: The format string
  209. *
  210. * Store the supplementary error message for the process if the process has
  211. * enabled the facility.
  212. */
  213. #define errorf(fc, fmt, ...) __logfc(fc, 'e', fmt, ## __VA_ARGS__)
  214. #define error_plog(p, fmt, ...) __plog(p, 'e', fmt, ## __VA_ARGS__)
  215. #define errorfc(fc, fmt, ...) __plog((&(fc)->log), 'e', fmt, ## __VA_ARGS__)
  216. /**
  217. * invalf - Store supplementary invalid argument error message
  218. * @fc: The context in which to log the error message
  219. * @fmt: The format string
  220. *
  221. * Store the supplementary error message for the process if the process has
  222. * enabled the facility and return -EINVAL.
  223. */
  224. #define invalf(fc, fmt, ...) (errorf(fc, fmt, ## __VA_ARGS__), -EINVAL)
  225. #define inval_plog(p, fmt, ...) (error_plog(p, fmt, ## __VA_ARGS__), -EINVAL)
  226. #define invalfc(fc, fmt, ...) (errorfc(fc, fmt, ## __VA_ARGS__), -EINVAL)
  227. #endif /* _LINUX_FS_CONTEXT_H */