kernfs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * kernfs.h - pseudo filesystem decoupled from vfs locking
  3. *
  4. * This file is released under the GPLv2.
  5. */
  6. #ifndef __LINUX_KERNFS_H
  7. #define __LINUX_KERNFS_H
  8. #include <linux/kernel.h>
  9. #include <linux/err.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/idr.h>
  13. #include <linux/lockdep.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/atomic.h>
  16. #include <linux/completion.h>
  17. struct file;
  18. struct dentry;
  19. struct iattr;
  20. struct seq_file;
  21. struct vm_area_struct;
  22. struct super_block;
  23. struct file_system_type;
  24. struct kernfs_open_node;
  25. struct kernfs_iattrs;
  26. enum kernfs_node_type {
  27. KERNFS_DIR = 0x0001,
  28. KERNFS_FILE = 0x0002,
  29. KERNFS_LINK = 0x0004,
  30. };
  31. #define KERNFS_TYPE_MASK 0x000f
  32. #define KERNFS_ACTIVE_REF KERNFS_FILE
  33. #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
  34. enum kernfs_node_flag {
  35. KERNFS_REMOVED = 0x0010,
  36. KERNFS_NS = 0x0020,
  37. KERNFS_HAS_SEQ_SHOW = 0x0040,
  38. KERNFS_HAS_MMAP = 0x0080,
  39. KERNFS_LOCKDEP = 0x0100,
  40. KERNFS_STATIC_NAME = 0x0200,
  41. };
  42. /* type-specific structures for kernfs_node union members */
  43. struct kernfs_elem_dir {
  44. unsigned long subdirs;
  45. /* children rbtree starts here and goes through kn->rb */
  46. struct rb_root children;
  47. /*
  48. * The kernfs hierarchy this directory belongs to. This fits
  49. * better directly in kernfs_node but is here to save space.
  50. */
  51. struct kernfs_root *root;
  52. };
  53. struct kernfs_elem_symlink {
  54. struct kernfs_node *target_kn;
  55. };
  56. struct kernfs_elem_attr {
  57. const struct kernfs_ops *ops;
  58. struct kernfs_open_node *open;
  59. loff_t size;
  60. };
  61. /*
  62. * kernfs_node - the building block of kernfs hierarchy. Each and every
  63. * kernfs node is represented by single kernfs_node. Most fields are
  64. * private to kernfs and shouldn't be accessed directly by kernfs users.
  65. *
  66. * As long as s_count reference is held, the kernfs_node itself is
  67. * accessible. Dereferencing elem or any other outer entity requires
  68. * active reference.
  69. */
  70. struct kernfs_node {
  71. atomic_t count;
  72. atomic_t active;
  73. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  74. struct lockdep_map dep_map;
  75. #endif
  76. /* the following two fields are published */
  77. struct kernfs_node *parent;
  78. const char *name;
  79. struct rb_node rb;
  80. union {
  81. struct completion *completion;
  82. struct kernfs_node *removed_list;
  83. } u;
  84. const void *ns; /* namespace tag */
  85. unsigned int hash; /* ns + name hash */
  86. union {
  87. struct kernfs_elem_dir dir;
  88. struct kernfs_elem_symlink symlink;
  89. struct kernfs_elem_attr attr;
  90. };
  91. void *priv;
  92. unsigned short flags;
  93. umode_t mode;
  94. unsigned int ino;
  95. struct kernfs_iattrs *iattr;
  96. };
  97. /*
  98. * kernfs_dir_ops may be specified on kernfs_create_root() to support
  99. * directory manipulation syscalls. These optional callbacks are invoked
  100. * on the matching syscalls and can perform any kernfs operations which
  101. * don't necessarily have to be the exact operation requested.
  102. */
  103. struct kernfs_dir_ops {
  104. int (*mkdir)(struct kernfs_node *parent, const char *name,
  105. umode_t mode);
  106. int (*rmdir)(struct kernfs_node *kn);
  107. int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
  108. const char *new_name);
  109. };
  110. struct kernfs_root {
  111. /* published fields */
  112. struct kernfs_node *kn;
  113. /* private fields, do not use outside kernfs proper */
  114. struct ida ino_ida;
  115. struct kernfs_dir_ops *dir_ops;
  116. };
  117. struct kernfs_open_file {
  118. /* published fields */
  119. struct kernfs_node *kn;
  120. struct file *file;
  121. /* private fields, do not use outside kernfs proper */
  122. struct mutex mutex;
  123. int event;
  124. struct list_head list;
  125. bool mmapped;
  126. const struct vm_operations_struct *vm_ops;
  127. };
  128. struct kernfs_ops {
  129. /*
  130. * Read is handled by either seq_file or raw_read().
  131. *
  132. * If seq_show() is present, seq_file path is active. Other seq
  133. * operations are optional and if not implemented, the behavior is
  134. * equivalent to single_open(). @sf->private points to the
  135. * associated kernfs_open_file.
  136. *
  137. * read() is bounced through kernel buffer and a read larger than
  138. * PAGE_SIZE results in partial operation of PAGE_SIZE.
  139. */
  140. int (*seq_show)(struct seq_file *sf, void *v);
  141. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  142. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  143. void (*seq_stop)(struct seq_file *sf, void *v);
  144. ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
  145. loff_t off);
  146. /*
  147. * write() is bounced through kernel buffer and a write larger than
  148. * PAGE_SIZE results in partial operation of PAGE_SIZE.
  149. */
  150. ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
  151. loff_t off);
  152. int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
  153. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  154. struct lock_class_key lockdep_key;
  155. #endif
  156. };
  157. #ifdef CONFIG_SYSFS
  158. static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
  159. {
  160. return kn->flags & KERNFS_TYPE_MASK;
  161. }
  162. /**
  163. * kernfs_enable_ns - enable namespace under a directory
  164. * @kn: directory of interest, should be empty
  165. *
  166. * This is to be called right after @kn is created to enable namespace
  167. * under it. All children of @kn must have non-NULL namespace tags and
  168. * only the ones which match the super_block's tag will be visible.
  169. */
  170. static inline void kernfs_enable_ns(struct kernfs_node *kn)
  171. {
  172. WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
  173. WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
  174. kn->flags |= KERNFS_NS;
  175. }
  176. /**
  177. * kernfs_ns_enabled - test whether namespace is enabled
  178. * @kn: the node to test
  179. *
  180. * Test whether namespace filtering is enabled for the children of @ns.
  181. */
  182. static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
  183. {
  184. return kn->flags & KERNFS_NS;
  185. }
  186. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  187. const char *name, const void *ns);
  188. void kernfs_get(struct kernfs_node *kn);
  189. void kernfs_put(struct kernfs_node *kn);
  190. struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
  191. void *priv);
  192. void kernfs_destroy_root(struct kernfs_root *root);
  193. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  194. const char *name, umode_t mode,
  195. void *priv, const void *ns);
  196. struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
  197. const char *name,
  198. umode_t mode, loff_t size,
  199. const struct kernfs_ops *ops,
  200. void *priv, const void *ns,
  201. bool name_is_static,
  202. struct lock_class_key *key);
  203. struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
  204. const char *name,
  205. struct kernfs_node *target);
  206. void kernfs_remove(struct kernfs_node *kn);
  207. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  208. const void *ns);
  209. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  210. const char *new_name, const void *new_ns);
  211. int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
  212. void kernfs_notify(struct kernfs_node *kn);
  213. const void *kernfs_super_ns(struct super_block *sb);
  214. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  215. struct kernfs_root *root, const void *ns);
  216. void kernfs_kill_sb(struct super_block *sb);
  217. void kernfs_init(void);
  218. #else /* CONFIG_SYSFS */
  219. static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
  220. { return 0; } /* whatever */
  221. static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
  222. static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
  223. { return false; }
  224. static inline struct kernfs_node *
  225. kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
  226. const void *ns)
  227. { return NULL; }
  228. static inline void kernfs_get(struct kernfs_node *kn) { }
  229. static inline void kernfs_put(struct kernfs_node *kn) { }
  230. static inline struct kernfs_root *
  231. kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
  232. { return ERR_PTR(-ENOSYS); }
  233. static inline void kernfs_destroy_root(struct kernfs_root *root) { }
  234. static inline struct kernfs_node *
  235. kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
  236. umode_t mode, void *priv, const void *ns)
  237. { return ERR_PTR(-ENOSYS); }
  238. static inline struct kernfs_node *
  239. __kernfs_create_file(struct kernfs_node *parent, const char *name,
  240. umode_t mode, loff_t size, const struct kernfs_ops *ops,
  241. void *priv, const void *ns, bool name_is_static,
  242. struct lock_class_key *key)
  243. { return ERR_PTR(-ENOSYS); }
  244. static inline struct kernfs_node *
  245. kernfs_create_link(struct kernfs_node *parent, const char *name,
  246. struct kernfs_node *target)
  247. { return ERR_PTR(-ENOSYS); }
  248. static inline void kernfs_remove(struct kernfs_node *kn) { }
  249. static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
  250. const char *name, const void *ns)
  251. { return -ENOSYS; }
  252. static inline int kernfs_rename_ns(struct kernfs_node *kn,
  253. struct kernfs_node *new_parent,
  254. const char *new_name, const void *new_ns)
  255. { return -ENOSYS; }
  256. static inline int kernfs_setattr(struct kernfs_node *kn,
  257. const struct iattr *iattr)
  258. { return -ENOSYS; }
  259. static inline void kernfs_notify(struct kernfs_node *kn) { }
  260. static inline const void *kernfs_super_ns(struct super_block *sb)
  261. { return NULL; }
  262. static inline struct dentry *
  263. kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  264. struct kernfs_root *root, const void *ns)
  265. { return ERR_PTR(-ENOSYS); }
  266. static inline void kernfs_kill_sb(struct super_block *sb) { }
  267. static inline void kernfs_init(void) { }
  268. #endif /* CONFIG_SYSFS */
  269. static inline struct kernfs_node *
  270. kernfs_find_and_get(struct kernfs_node *kn, const char *name)
  271. {
  272. return kernfs_find_and_get_ns(kn, name, NULL);
  273. }
  274. static inline struct kernfs_node *
  275. kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
  276. void *priv)
  277. {
  278. return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
  279. }
  280. static inline struct kernfs_node *
  281. kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
  282. umode_t mode, loff_t size, const struct kernfs_ops *ops,
  283. void *priv, const void *ns)
  284. {
  285. struct lock_class_key *key = NULL;
  286. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  287. key = (struct lock_class_key *)&ops->lockdep_key;
  288. #endif
  289. return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
  290. false, key);
  291. }
  292. static inline struct kernfs_node *
  293. kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
  294. loff_t size, const struct kernfs_ops *ops, void *priv)
  295. {
  296. return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
  297. }
  298. static inline int kernfs_remove_by_name(struct kernfs_node *parent,
  299. const char *name)
  300. {
  301. return kernfs_remove_by_name_ns(parent, name, NULL);
  302. }
  303. static inline struct dentry *
  304. kernfs_mount(struct file_system_type *fs_type, int flags,
  305. struct kernfs_root *root)
  306. {
  307. return kernfs_mount_ns(fs_type, flags, root, NULL);
  308. }
  309. #endif /* __LINUX_KERNFS_H */