kernfs.h 10 KB

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