1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * kernfs.h - pseudo filesystem decoupled from vfs locking
6 #ifndef __LINUX_KERNFS_H
7 #define __LINUX_KERNFS_H
9 #include <linux/kernel.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/idr.h>
14 #include <linux/lockdep.h>
15 #include <linux/rbtree.h>
16 #include <linux/atomic.h>
17 #include <linux/uidgid.h>
18 #include <linux/wait.h>
24 struct vm_area_struct;
26 struct file_system_type;
27 struct poll_table_struct;
30 struct kernfs_fs_context;
31 struct kernfs_open_node;
34 enum kernfs_node_type {
40 #define KERNFS_TYPE_MASK 0x000f
41 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
42 #define KERNFS_MAX_USER_XATTRS 128
43 #define KERNFS_USER_XATTR_SIZE_LIMIT (128 << 10)
45 enum kernfs_node_flag {
46 KERNFS_ACTIVATED = 0x0010,
48 KERNFS_HAS_SEQ_SHOW = 0x0040,
49 KERNFS_HAS_MMAP = 0x0080,
50 KERNFS_LOCKDEP = 0x0100,
51 KERNFS_SUICIDAL = 0x0400,
52 KERNFS_SUICIDED = 0x0800,
53 KERNFS_EMPTY_DIR = 0x1000,
54 KERNFS_HAS_RELEASE = 0x2000,
57 /* @flags for kernfs_create_root() */
58 enum kernfs_root_flag {
60 * kernfs_nodes are created in the deactivated state and invisible.
61 * They require explicit kernfs_activate() to become visible. This
62 * can be used to make related nodes become visible atomically
63 * after all nodes are created successfully.
65 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
68 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
69 * succeeds regardless of the RW permissions. sysfs had an extra
70 * layer of enforcement where open(2) fails with -EACCES regardless
71 * of CAP_DAC_OVERRIDE if the permission doesn't have the
72 * respective read or write access at all (none of S_IRUGO or
73 * S_IWUGO) or the respective operation isn't implemented. The
74 * following flag enables that behavior.
76 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
79 * The filesystem supports exportfs operation, so userspace can use
80 * fhandle to access nodes of the fs.
82 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
85 * Support user xattrs to be written to nodes rooted at this root.
87 KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008,
90 /* type-specific structures for kernfs_node union members */
91 struct kernfs_elem_dir {
92 unsigned long subdirs;
93 /* children rbtree starts here and goes through kn->rb */
94 struct rb_root children;
97 * The kernfs hierarchy this directory belongs to. This fits
98 * better directly in kernfs_node but is here to save space.
100 struct kernfs_root *root;
102 * Monotonic revision counter, used to identify if a directory
103 * node has changed during negative dentry revalidation.
108 struct kernfs_elem_symlink {
109 struct kernfs_node *target_kn;
112 struct kernfs_elem_attr {
113 const struct kernfs_ops *ops;
114 struct kernfs_open_node *open;
116 struct kernfs_node *notify_next; /* for kernfs_notify() */
120 * kernfs_node - the building block of kernfs hierarchy. Each and every
121 * kernfs node is represented by single kernfs_node. Most fields are
122 * private to kernfs and shouldn't be accessed directly by kernfs users.
124 * As long as count reference is held, the kernfs_node itself is
125 * accessible. Dereferencing elem or any other outer entity requires
131 #ifdef CONFIG_DEBUG_LOCK_ALLOC
132 struct lockdep_map dep_map;
135 * Use kernfs_get_parent() and kernfs_name/path() instead of
136 * accessing the following two fields directly. If the node is
137 * never moved to a different parent, it is safe to access the
140 struct kernfs_node *parent;
145 const void *ns; /* namespace tag */
146 unsigned int hash; /* ns + name hash */
148 struct kernfs_elem_dir dir;
149 struct kernfs_elem_symlink symlink;
150 struct kernfs_elem_attr attr;
156 * 64bit unique ID. On 64bit ino setups, id is the ino. On 32bit,
157 * the low 32bits are ino and upper generation.
161 unsigned short flags;
163 struct kernfs_iattrs *iattr;
167 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
168 * syscalls. These optional callbacks are invoked on the matching syscalls
169 * and can perform any kernfs operations which don't necessarily have to be
170 * the exact operation requested. An active reference is held for each
171 * kernfs_node parameter.
173 struct kernfs_syscall_ops {
174 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
176 int (*mkdir)(struct kernfs_node *parent, const char *name,
178 int (*rmdir)(struct kernfs_node *kn);
179 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
180 const char *new_name);
181 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
182 struct kernfs_root *root);
186 /* published fields */
187 struct kernfs_node *kn;
188 unsigned int flags; /* KERNFS_ROOT_* flags */
190 /* private fields, do not use outside kernfs proper */
194 struct kernfs_syscall_ops *syscall_ops;
196 /* list of kernfs_super_info of this root, protected by kernfs_rwsem */
197 struct list_head supers;
199 wait_queue_head_t deactivate_waitq;
202 struct kernfs_open_file {
203 /* published fields */
204 struct kernfs_node *kn;
206 struct seq_file *seq_file;
209 /* private fields, do not use outside kernfs proper */
211 struct mutex prealloc_mutex;
213 struct list_head list;
216 size_t atomic_write_len;
219 const struct vm_operations_struct *vm_ops;
224 * Optional open/release methods. Both are called with
225 * @of->seq_file populated.
227 int (*open)(struct kernfs_open_file *of);
228 void (*release)(struct kernfs_open_file *of);
231 * Read is handled by either seq_file or raw_read().
233 * If seq_show() is present, seq_file path is active. Other seq
234 * operations are optional and if not implemented, the behavior is
235 * equivalent to single_open(). @sf->private points to the
236 * associated kernfs_open_file.
238 * read() is bounced through kernel buffer and a read larger than
239 * PAGE_SIZE results in partial operation of PAGE_SIZE.
241 int (*seq_show)(struct seq_file *sf, void *v);
243 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
244 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
245 void (*seq_stop)(struct seq_file *sf, void *v);
247 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
251 * write() is bounced through kernel buffer. If atomic_write_len
252 * is not set, a write larger than PAGE_SIZE results in partial
253 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
254 * writes upto the specified size are executed atomically but
255 * larger ones are rejected with -E2BIG.
257 size_t atomic_write_len;
259 * "prealloc" causes a buffer to be allocated at open for
260 * all read/write requests. As ->seq_show uses seq_read()
261 * which does its own allocation, it is incompatible with
262 * ->prealloc. Provide ->read and ->write with ->prealloc.
265 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
268 __poll_t (*poll)(struct kernfs_open_file *of,
269 struct poll_table_struct *pt);
271 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
273 #ifdef CONFIG_DEBUG_LOCK_ALLOC
274 struct lock_class_key lockdep_key;
279 * The kernfs superblock creation/mount parameter context.
281 struct kernfs_fs_context {
282 struct kernfs_root *root; /* Root of the hierarchy being mounted */
283 void *ns_tag; /* Namespace tag of the mount (or NULL) */
284 unsigned long magic; /* File system specific magic number */
286 /* The following are set/used by kernfs_mount() */
287 bool new_sb_created; /* Set to T if we allocated a new sb */
292 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
294 return kn->flags & KERNFS_TYPE_MASK;
297 static inline ino_t kernfs_id_ino(u64 id)
299 /* id is ino if ino_t is 64bit; otherwise, low 32bits */
300 if (sizeof(ino_t) >= sizeof(u64))
306 static inline u32 kernfs_id_gen(u64 id)
308 /* gen is fixed at 1 if ino_t is 64bit; otherwise, high 32bits */
309 if (sizeof(ino_t) >= sizeof(u64))
315 static inline ino_t kernfs_ino(struct kernfs_node *kn)
317 return kernfs_id_ino(kn->id);
320 static inline ino_t kernfs_gen(struct kernfs_node *kn)
322 return kernfs_id_gen(kn->id);
326 * kernfs_enable_ns - enable namespace under a directory
327 * @kn: directory of interest, should be empty
329 * This is to be called right after @kn is created to enable namespace
330 * under it. All children of @kn must have non-NULL namespace tags and
331 * only the ones which match the super_block's tag will be visible.
333 static inline void kernfs_enable_ns(struct kernfs_node *kn)
335 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
336 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
337 kn->flags |= KERNFS_NS;
341 * kernfs_ns_enabled - test whether namespace is enabled
342 * @kn: the node to test
344 * Test whether namespace filtering is enabled for the children of @ns.
346 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
348 return kn->flags & KERNFS_NS;
351 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
352 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
353 char *buf, size_t buflen);
354 void pr_cont_kernfs_name(struct kernfs_node *kn);
355 void pr_cont_kernfs_path(struct kernfs_node *kn);
356 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
357 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
358 const char *name, const void *ns);
359 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
360 const char *path, const void *ns);
361 void kernfs_get(struct kernfs_node *kn);
362 void kernfs_put(struct kernfs_node *kn);
364 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
365 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
366 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
368 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
369 struct super_block *sb);
370 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
371 unsigned int flags, void *priv);
372 void kernfs_destroy_root(struct kernfs_root *root);
374 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
375 const char *name, umode_t mode,
376 kuid_t uid, kgid_t gid,
377 void *priv, const void *ns);
378 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
380 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
381 const char *name, umode_t mode,
382 kuid_t uid, kgid_t gid,
384 const struct kernfs_ops *ops,
385 void *priv, const void *ns,
386 struct lock_class_key *key);
387 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
389 struct kernfs_node *target);
390 void kernfs_activate(struct kernfs_node *kn);
391 void kernfs_remove(struct kernfs_node *kn);
392 void kernfs_break_active_protection(struct kernfs_node *kn);
393 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
394 bool kernfs_remove_self(struct kernfs_node *kn);
395 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
397 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
398 const char *new_name, const void *new_ns);
399 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
400 __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
401 struct poll_table_struct *pt);
402 void kernfs_notify(struct kernfs_node *kn);
404 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
405 void *value, size_t size);
406 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
407 const void *value, size_t size, int flags);
409 const void *kernfs_super_ns(struct super_block *sb);
410 int kernfs_get_tree(struct fs_context *fc);
411 void kernfs_free_fs_context(struct fs_context *fc);
412 void kernfs_kill_sb(struct super_block *sb);
414 void kernfs_init(void);
416 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
418 #else /* CONFIG_KERNFS */
420 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
421 { return 0; } /* whatever */
423 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
425 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
428 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
431 static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
432 struct kernfs_node *kn,
433 char *buf, size_t buflen)
436 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
437 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
439 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
442 static inline struct kernfs_node *
443 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
446 static inline struct kernfs_node *
447 kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
451 static inline void kernfs_get(struct kernfs_node *kn) { }
452 static inline void kernfs_put(struct kernfs_node *kn) { }
454 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
457 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
460 static inline struct inode *
461 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
464 static inline struct kernfs_root *
465 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
467 { return ERR_PTR(-ENOSYS); }
469 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
471 static inline struct kernfs_node *
472 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
473 umode_t mode, kuid_t uid, kgid_t gid,
474 void *priv, const void *ns)
475 { return ERR_PTR(-ENOSYS); }
477 static inline struct kernfs_node *
478 __kernfs_create_file(struct kernfs_node *parent, const char *name,
479 umode_t mode, kuid_t uid, kgid_t gid,
480 loff_t size, const struct kernfs_ops *ops,
481 void *priv, const void *ns, struct lock_class_key *key)
482 { return ERR_PTR(-ENOSYS); }
484 static inline struct kernfs_node *
485 kernfs_create_link(struct kernfs_node *parent, const char *name,
486 struct kernfs_node *target)
487 { return ERR_PTR(-ENOSYS); }
489 static inline void kernfs_activate(struct kernfs_node *kn) { }
491 static inline void kernfs_remove(struct kernfs_node *kn) { }
493 static inline bool kernfs_remove_self(struct kernfs_node *kn)
496 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
497 const char *name, const void *ns)
500 static inline int kernfs_rename_ns(struct kernfs_node *kn,
501 struct kernfs_node *new_parent,
502 const char *new_name, const void *new_ns)
505 static inline int kernfs_setattr(struct kernfs_node *kn,
506 const struct iattr *iattr)
509 static inline void kernfs_notify(struct kernfs_node *kn) { }
511 static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
512 void *value, size_t size)
515 static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
516 const void *value, size_t size, int flags)
519 static inline const void *kernfs_super_ns(struct super_block *sb)
522 static inline int kernfs_get_tree(struct fs_context *fc)
525 static inline void kernfs_free_fs_context(struct fs_context *fc) { }
527 static inline void kernfs_kill_sb(struct super_block *sb) { }
529 static inline void kernfs_init(void) { }
531 #endif /* CONFIG_KERNFS */
534 * kernfs_path - build full path of a given node
535 * @kn: kernfs_node of interest
536 * @buf: buffer to copy @kn's name into
537 * @buflen: size of @buf
539 * If @kn is NULL result will be "(null)".
541 * Returns the length of the full path. If the full length is equal to or
542 * greater than @buflen, @buf contains the truncated path with the trailing
543 * '\0'. On error, -errno is returned.
545 static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
547 return kernfs_path_from_node(kn, NULL, buf, buflen);
550 static inline struct kernfs_node *
551 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
553 return kernfs_find_and_get_ns(kn, name, NULL);
556 static inline struct kernfs_node *
557 kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
559 return kernfs_walk_and_get_ns(kn, path, NULL);
562 static inline struct kernfs_node *
563 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
566 return kernfs_create_dir_ns(parent, name, mode,
567 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
571 static inline struct kernfs_node *
572 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
573 umode_t mode, kuid_t uid, kgid_t gid,
574 loff_t size, const struct kernfs_ops *ops,
575 void *priv, const void *ns)
577 struct lock_class_key *key = NULL;
579 #ifdef CONFIG_DEBUG_LOCK_ALLOC
580 key = (struct lock_class_key *)&ops->lockdep_key;
582 return __kernfs_create_file(parent, name, mode, uid, gid,
583 size, ops, priv, ns, key);
586 static inline struct kernfs_node *
587 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
588 loff_t size, const struct kernfs_ops *ops, void *priv)
590 return kernfs_create_file_ns(parent, name, mode,
591 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
592 size, ops, priv, NULL);
595 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
598 return kernfs_remove_by_name_ns(parent, name, NULL);
601 static inline int kernfs_rename(struct kernfs_node *kn,
602 struct kernfs_node *new_parent,
603 const char *new_name)
605 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
608 #endif /* __LINUX_KERNFS_H */