kernfs.h 17 KB

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