kernfs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 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_FLAG_MASK ~KERNFS_TYPE_MASK
  33. enum kernfs_node_flag {
  34. KERNFS_ACTIVATED = 0x0010,
  35. KERNFS_NS = 0x0020,
  36. KERNFS_HAS_SEQ_SHOW = 0x0040,
  37. KERNFS_HAS_MMAP = 0x0080,
  38. KERNFS_LOCKDEP = 0x0100,
  39. KERNFS_SUICIDAL = 0x0400,
  40. KERNFS_SUICIDED = 0x0800,
  41. KERNFS_EMPTY_DIR = 0x1000,
  42. };
  43. /* @flags for kernfs_create_root() */
  44. enum kernfs_root_flag {
  45. /*
  46. * kernfs_nodes are created in the deactivated state and invisible.
  47. * They require explicit kernfs_activate() to become visible. This
  48. * can be used to make related nodes become visible atomically
  49. * after all nodes are created successfully.
  50. */
  51. KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
  52. /*
  53. * For regular flies, if the opener has CAP_DAC_OVERRIDE, open(2)
  54. * succeeds regardless of the RW permissions. sysfs had an extra
  55. * layer of enforcement where open(2) fails with -EACCES regardless
  56. * of CAP_DAC_OVERRIDE if the permission doesn't have the
  57. * respective read or write access at all (none of S_IRUGO or
  58. * S_IWUGO) or the respective operation isn't implemented. The
  59. * following flag enables that behavior.
  60. */
  61. KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
  62. };
  63. /* type-specific structures for kernfs_node union members */
  64. struct kernfs_elem_dir {
  65. unsigned long subdirs;
  66. /* children rbtree starts here and goes through kn->rb */
  67. struct rb_root children;
  68. /*
  69. * The kernfs hierarchy this directory belongs to. This fits
  70. * better directly in kernfs_node but is here to save space.
  71. */
  72. struct kernfs_root *root;
  73. };
  74. struct kernfs_elem_symlink {
  75. struct kernfs_node *target_kn;
  76. };
  77. struct kernfs_elem_attr {
  78. const struct kernfs_ops *ops;
  79. struct kernfs_open_node *open;
  80. loff_t size;
  81. struct kernfs_node *notify_next; /* for kernfs_notify() */
  82. };
  83. /*
  84. * kernfs_node - the building block of kernfs hierarchy. Each and every
  85. * kernfs node is represented by single kernfs_node. Most fields are
  86. * private to kernfs and shouldn't be accessed directly by kernfs users.
  87. *
  88. * As long as s_count reference is held, the kernfs_node itself is
  89. * accessible. Dereferencing elem or any other outer entity requires
  90. * active reference.
  91. */
  92. struct kernfs_node {
  93. atomic_t count;
  94. atomic_t active;
  95. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  96. struct lockdep_map dep_map;
  97. #endif
  98. /*
  99. * Use kernfs_get_parent() and kernfs_name/path() instead of
  100. * accessing the following two fields directly. If the node is
  101. * never moved to a different parent, it is safe to access the
  102. * parent directly.
  103. */
  104. struct kernfs_node *parent;
  105. const char *name;
  106. struct rb_node rb;
  107. const void *ns; /* namespace tag */
  108. unsigned int hash; /* ns + name hash */
  109. union {
  110. struct kernfs_elem_dir dir;
  111. struct kernfs_elem_symlink symlink;
  112. struct kernfs_elem_attr attr;
  113. };
  114. void *priv;
  115. unsigned short flags;
  116. umode_t mode;
  117. unsigned int ino;
  118. struct kernfs_iattrs *iattr;
  119. };
  120. /*
  121. * kernfs_syscall_ops may be specified on kernfs_create_root() to support
  122. * syscalls. These optional callbacks are invoked on the matching syscalls
  123. * and can perform any kernfs operations which don't necessarily have to be
  124. * the exact operation requested. An active reference is held for each
  125. * kernfs_node parameter.
  126. */
  127. struct kernfs_syscall_ops {
  128. int (*remount_fs)(struct kernfs_root *root, int *flags, char *data);
  129. int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
  130. int (*mkdir)(struct kernfs_node *parent, const char *name,
  131. umode_t mode);
  132. int (*rmdir)(struct kernfs_node *kn);
  133. int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
  134. const char *new_name);
  135. int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
  136. struct kernfs_root *root);
  137. };
  138. struct kernfs_root {
  139. /* published fields */
  140. struct kernfs_node *kn;
  141. unsigned int flags; /* KERNFS_ROOT_* flags */
  142. /* private fields, do not use outside kernfs proper */
  143. struct ida ino_ida;
  144. struct kernfs_syscall_ops *syscall_ops;
  145. /* list of kernfs_super_info of this root, protected by kernfs_mutex */
  146. struct list_head supers;
  147. wait_queue_head_t deactivate_waitq;
  148. };
  149. struct kernfs_open_file {
  150. /* published fields */
  151. struct kernfs_node *kn;
  152. struct file *file;
  153. void *priv;
  154. /* private fields, do not use outside kernfs proper */
  155. struct mutex mutex;
  156. struct mutex prealloc_mutex;
  157. int event;
  158. struct list_head list;
  159. char *prealloc_buf;
  160. size_t atomic_write_len;
  161. bool mmapped;
  162. const struct vm_operations_struct *vm_ops;
  163. };
  164. struct kernfs_ops {
  165. /*
  166. * Read is handled by either seq_file or raw_read().
  167. *
  168. * If seq_show() is present, seq_file path is active. Other seq
  169. * operations are optional and if not implemented, the behavior is
  170. * equivalent to single_open(). @sf->private points to the
  171. * associated kernfs_open_file.
  172. *
  173. * read() is bounced through kernel buffer and a read larger than
  174. * PAGE_SIZE results in partial operation of PAGE_SIZE.
  175. */
  176. int (*seq_show)(struct seq_file *sf, void *v);
  177. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  178. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  179. void (*seq_stop)(struct seq_file *sf, void *v);
  180. ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
  181. loff_t off);
  182. /*
  183. * write() is bounced through kernel buffer. If atomic_write_len
  184. * is not set, a write larger than PAGE_SIZE results in partial
  185. * operations of PAGE_SIZE chunks. If atomic_write_len is set,
  186. * writes upto the specified size are executed atomically but
  187. * larger ones are rejected with -E2BIG.
  188. */
  189. size_t atomic_write_len;
  190. /*
  191. * "prealloc" causes a buffer to be allocated at open for
  192. * all read/write requests. As ->seq_show uses seq_read()
  193. * which does its own allocation, it is incompatible with
  194. * ->prealloc. Provide ->read and ->write with ->prealloc.
  195. */
  196. bool prealloc;
  197. ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
  198. loff_t off);
  199. int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
  200. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  201. struct lock_class_key lockdep_key;
  202. #endif
  203. };
  204. #ifdef CONFIG_KERNFS
  205. static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
  206. {
  207. return kn->flags & KERNFS_TYPE_MASK;
  208. }
  209. /**
  210. * kernfs_enable_ns - enable namespace under a directory
  211. * @kn: directory of interest, should be empty
  212. *
  213. * This is to be called right after @kn is created to enable namespace
  214. * under it. All children of @kn must have non-NULL namespace tags and
  215. * only the ones which match the super_block's tag will be visible.
  216. */
  217. static inline void kernfs_enable_ns(struct kernfs_node *kn)
  218. {
  219. WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
  220. WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
  221. kn->flags |= KERNFS_NS;
  222. }
  223. /**
  224. * kernfs_ns_enabled - test whether namespace is enabled
  225. * @kn: the node to test
  226. *
  227. * Test whether namespace filtering is enabled for the children of @ns.
  228. */
  229. static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
  230. {
  231. return kn->flags & KERNFS_NS;
  232. }
  233. int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
  234. int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
  235. char *buf, size_t buflen);
  236. void pr_cont_kernfs_name(struct kernfs_node *kn);
  237. void pr_cont_kernfs_path(struct kernfs_node *kn);
  238. struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
  239. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  240. const char *name, const void *ns);
  241. struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
  242. const char *path, const void *ns);
  243. void kernfs_get(struct kernfs_node *kn);
  244. void kernfs_put(struct kernfs_node *kn);
  245. struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
  246. struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
  247. struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
  248. struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
  249. struct super_block *sb);
  250. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  251. unsigned int flags, void *priv);
  252. void kernfs_destroy_root(struct kernfs_root *root);
  253. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  254. const char *name, umode_t mode,
  255. void *priv, const void *ns);
  256. struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
  257. const char *name);
  258. struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
  259. const char *name,
  260. umode_t mode, loff_t size,
  261. const struct kernfs_ops *ops,
  262. void *priv, const void *ns,
  263. struct lock_class_key *key);
  264. struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
  265. const char *name,
  266. struct kernfs_node *target);
  267. void kernfs_activate(struct kernfs_node *kn);
  268. void kernfs_remove(struct kernfs_node *kn);
  269. void kernfs_break_active_protection(struct kernfs_node *kn);
  270. void kernfs_unbreak_active_protection(struct kernfs_node *kn);
  271. bool kernfs_remove_self(struct kernfs_node *kn);
  272. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  273. const void *ns);
  274. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  275. const char *new_name, const void *new_ns);
  276. int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
  277. void kernfs_notify(struct kernfs_node *kn);
  278. const void *kernfs_super_ns(struct super_block *sb);
  279. struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  280. struct kernfs_root *root, unsigned long magic,
  281. bool *new_sb_created, const void *ns);
  282. void kernfs_kill_sb(struct super_block *sb);
  283. struct super_block *kernfs_pin_sb(struct kernfs_root *root, const void *ns);
  284. void kernfs_init(void);
  285. #else /* CONFIG_KERNFS */
  286. static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
  287. { return 0; } /* whatever */
  288. static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
  289. static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
  290. { return false; }
  291. static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
  292. { return -ENOSYS; }
  293. static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
  294. struct kernfs_node *kn,
  295. char *buf, size_t buflen)
  296. { return -ENOSYS; }
  297. static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
  298. static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
  299. static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
  300. { return NULL; }
  301. static inline struct kernfs_node *
  302. kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
  303. const void *ns)
  304. { return NULL; }
  305. static inline struct kernfs_node *
  306. kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
  307. const void *ns)
  308. { return NULL; }
  309. static inline void kernfs_get(struct kernfs_node *kn) { }
  310. static inline void kernfs_put(struct kernfs_node *kn) { }
  311. static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
  312. { return NULL; }
  313. static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
  314. { return NULL; }
  315. static inline struct inode *
  316. kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
  317. { return NULL; }
  318. static inline struct kernfs_root *
  319. kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
  320. void *priv)
  321. { return ERR_PTR(-ENOSYS); }
  322. static inline void kernfs_destroy_root(struct kernfs_root *root) { }
  323. static inline struct kernfs_node *
  324. kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
  325. umode_t mode, void *priv, const void *ns)
  326. { return ERR_PTR(-ENOSYS); }
  327. static inline struct kernfs_node *
  328. __kernfs_create_file(struct kernfs_node *parent, const char *name,
  329. umode_t mode, loff_t size, const struct kernfs_ops *ops,
  330. void *priv, const void *ns, struct lock_class_key *key)
  331. { return ERR_PTR(-ENOSYS); }
  332. static inline struct kernfs_node *
  333. kernfs_create_link(struct kernfs_node *parent, const char *name,
  334. struct kernfs_node *target)
  335. { return ERR_PTR(-ENOSYS); }
  336. static inline void kernfs_activate(struct kernfs_node *kn) { }
  337. static inline void kernfs_remove(struct kernfs_node *kn) { }
  338. static inline bool kernfs_remove_self(struct kernfs_node *kn)
  339. { return false; }
  340. static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
  341. const char *name, const void *ns)
  342. { return -ENOSYS; }
  343. static inline int kernfs_rename_ns(struct kernfs_node *kn,
  344. struct kernfs_node *new_parent,
  345. const char *new_name, const void *new_ns)
  346. { return -ENOSYS; }
  347. static inline int kernfs_setattr(struct kernfs_node *kn,
  348. const struct iattr *iattr)
  349. { return -ENOSYS; }
  350. static inline void kernfs_notify(struct kernfs_node *kn) { }
  351. static inline const void *kernfs_super_ns(struct super_block *sb)
  352. { return NULL; }
  353. static inline struct dentry *
  354. kernfs_mount_ns(struct file_system_type *fs_type, int flags,
  355. struct kernfs_root *root, unsigned long magic,
  356. bool *new_sb_created, const void *ns)
  357. { return ERR_PTR(-ENOSYS); }
  358. static inline void kernfs_kill_sb(struct super_block *sb) { }
  359. static inline void kernfs_init(void) { }
  360. #endif /* CONFIG_KERNFS */
  361. /**
  362. * kernfs_path - build full path of a given node
  363. * @kn: kernfs_node of interest
  364. * @buf: buffer to copy @kn's name into
  365. * @buflen: size of @buf
  366. *
  367. * Builds and returns the full path of @kn in @buf of @buflen bytes. The
  368. * path is built from the end of @buf so the returned pointer usually
  369. * doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
  370. * and %NULL is returned.
  371. */
  372. static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
  373. {
  374. return kernfs_path_from_node(kn, NULL, buf, buflen);
  375. }
  376. static inline struct kernfs_node *
  377. kernfs_find_and_get(struct kernfs_node *kn, const char *name)
  378. {
  379. return kernfs_find_and_get_ns(kn, name, NULL);
  380. }
  381. static inline struct kernfs_node *
  382. kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
  383. {
  384. return kernfs_walk_and_get_ns(kn, path, NULL);
  385. }
  386. static inline struct kernfs_node *
  387. kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
  388. void *priv)
  389. {
  390. return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
  391. }
  392. static inline struct kernfs_node *
  393. kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
  394. umode_t mode, loff_t size, const struct kernfs_ops *ops,
  395. void *priv, const void *ns)
  396. {
  397. struct lock_class_key *key = NULL;
  398. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  399. key = (struct lock_class_key *)&ops->lockdep_key;
  400. #endif
  401. return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
  402. key);
  403. }
  404. static inline struct kernfs_node *
  405. kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
  406. loff_t size, const struct kernfs_ops *ops, void *priv)
  407. {
  408. return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
  409. }
  410. static inline int kernfs_remove_by_name(struct kernfs_node *parent,
  411. const char *name)
  412. {
  413. return kernfs_remove_by_name_ns(parent, name, NULL);
  414. }
  415. static inline int kernfs_rename(struct kernfs_node *kn,
  416. struct kernfs_node *new_parent,
  417. const char *new_name)
  418. {
  419. return kernfs_rename_ns(kn, new_parent, new_name, NULL);
  420. }
  421. static inline struct dentry *
  422. kernfs_mount(struct file_system_type *fs_type, int flags,
  423. struct kernfs_root *root, unsigned long magic,
  424. bool *new_sb_created)
  425. {
  426. return kernfs_mount_ns(fs_type, flags, root,
  427. magic, new_sb_created, NULL);
  428. }
  429. #endif /* __LINUX_KERNFS_H */