fs_context.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 (dhowells@redhat.com)
  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 void fc_drop_locked(struct fs_context *fc);
  131. int reconfigure_single(struct super_block *s,
  132. int flags, void *data);
  133. /*
  134. * sget() wrappers to be called from the ->get_tree() op.
  135. */
  136. enum vfs_get_super_keying {
  137. vfs_get_single_super, /* Only one such superblock may exist */
  138. vfs_get_single_reconf_super, /* As above, but reconfigure if it exists */
  139. vfs_get_keyed_super, /* Superblocks with different s_fs_info keys may exist */
  140. vfs_get_independent_super, /* Multiple independent superblocks may exist */
  141. };
  142. extern int vfs_get_super(struct fs_context *fc,
  143. enum vfs_get_super_keying keying,
  144. int (*fill_super)(struct super_block *sb,
  145. struct fs_context *fc));
  146. extern int get_tree_nodev(struct fs_context *fc,
  147. int (*fill_super)(struct super_block *sb,
  148. struct fs_context *fc));
  149. extern int get_tree_single(struct fs_context *fc,
  150. int (*fill_super)(struct super_block *sb,
  151. struct fs_context *fc));
  152. extern int get_tree_single_reconf(struct fs_context *fc,
  153. int (*fill_super)(struct super_block *sb,
  154. struct fs_context *fc));
  155. extern int get_tree_keyed(struct fs_context *fc,
  156. int (*fill_super)(struct super_block *sb,
  157. struct fs_context *fc),
  158. void *key);
  159. extern int get_tree_bdev(struct fs_context *fc,
  160. int (*fill_super)(struct super_block *sb,
  161. struct fs_context *fc));
  162. extern const struct file_operations fscontext_fops;
  163. /*
  164. * Mount error, warning and informational message logging. This structure is
  165. * shareable between a mount and a subordinate mount.
  166. */
  167. struct fc_log {
  168. refcount_t usage;
  169. u8 head; /* Insertion index in buffer[] */
  170. u8 tail; /* Removal index in buffer[] */
  171. u8 need_free; /* Mask of kfree'able items in buffer[] */
  172. struct module *owner; /* Owner module for strings that don't then need freeing */
  173. char *buffer[8];
  174. };
  175. extern __attribute__((format(printf, 4, 5)))
  176. void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...);
  177. #define __logfc(fc, l, fmt, ...) logfc((fc)->log.log, NULL, \
  178. l, fmt, ## __VA_ARGS__)
  179. #define __plog(p, l, fmt, ...) logfc((p)->log, (p)->prefix, \
  180. l, fmt, ## __VA_ARGS__)
  181. /**
  182. * infof - Store supplementary informational message
  183. * @fc: The context in which to log the informational message
  184. * @fmt: The format string
  185. *
  186. * Store the supplementary informational message for the process if the process
  187. * has enabled the facility.
  188. */
  189. #define infof(fc, fmt, ...) __logfc(fc, 'i', fmt, ## __VA_ARGS__)
  190. #define info_plog(p, fmt, ...) __plog(p, 'i', fmt, ## __VA_ARGS__)
  191. #define infofc(p, fmt, ...) __plog((&(fc)->log), 'i', fmt, ## __VA_ARGS__)
  192. /**
  193. * warnf - Store supplementary warning message
  194. * @fc: The context in which to log the error message
  195. * @fmt: The format string
  196. *
  197. * Store the supplementary warning message for the process if the process has
  198. * enabled the facility.
  199. */
  200. #define warnf(fc, fmt, ...) __logfc(fc, 'w', fmt, ## __VA_ARGS__)
  201. #define warn_plog(p, fmt, ...) __plog(p, 'w', fmt, ## __VA_ARGS__)
  202. #define warnfc(fc, fmt, ...) __plog((&(fc)->log), 'w', fmt, ## __VA_ARGS__)
  203. /**
  204. * errorf - Store supplementary error message
  205. * @fc: The context in which to log the error message
  206. * @fmt: The format string
  207. *
  208. * Store the supplementary error message for the process if the process has
  209. * enabled the facility.
  210. */
  211. #define errorf(fc, fmt, ...) __logfc(fc, 'e', fmt, ## __VA_ARGS__)
  212. #define error_plog(p, fmt, ...) __plog(p, 'e', fmt, ## __VA_ARGS__)
  213. #define errorfc(fc, fmt, ...) __plog((&(fc)->log), 'e', fmt, ## __VA_ARGS__)
  214. /**
  215. * invalf - Store supplementary invalid argument error message
  216. * @fc: The context in which to log the error message
  217. * @fmt: The format string
  218. *
  219. * Store the supplementary error message for the process if the process has
  220. * enabled the facility and return -EINVAL.
  221. */
  222. #define invalf(fc, fmt, ...) (errorf(fc, fmt, ## __VA_ARGS__), -EINVAL)
  223. #define inval_plog(p, fmt, ...) (error_plog(p, fmt, ## __VA_ARGS__), -EINVAL)
  224. #define invalfc(fc, fmt, ...) (errorfc(fc, fmt, ## __VA_ARGS__), -EINVAL)
  225. #endif /* _LINUX_FS_CONTEXT_H */