kernfs.h 16 KB

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