kernfs.h 9.8 KB

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