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
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/bug.h>
17 #include <linux/types.h>
18 #include <linux/uidgid.h>
19 #include <linux/wait.h>
20 #include <linux/rwsem.h>
21 #include <linux/cache.h>
27 struct vm_area_struct;
28 struct vm_operations_struct;
30 struct file_system_type;
31 struct poll_table_struct;
34 struct kernfs_fs_context;
35 struct kernfs_open_node;
39 * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash
41 * Having a small hash table would impact scalability, since
42 * more and more kernfs_node objects will end up using same lock
43 * and having a very large hash table would waste memory.
45 * At the moment size of hash table of locks is being set based on
46 * the number of CPUs as follows:
48 * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS
56 * The above relation between NR_CPU and number of locks is based
57 * on some internal experimentation which involved booting qemu
58 * with different values of smp, performing some sysfs operations
59 * on all CPUs and observing how increase in number of locks impacts
60 * completion time of these sysfs operations on each CPU.
63 #define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32)))
65 #define NR_KERNFS_LOCK_BITS 1
68 #define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS)
71 * There's one kernfs_open_file for each open file and one kernfs_open_node
72 * for each kernfs_node with one or more open files.
74 * filp->private_data points to seq_file whose ->private points to
77 * kernfs_open_files are chained at kernfs_open_node->files, which is
78 * protected by kernfs_global_locks.open_file_mutex[i].
80 * To reduce possible contention in sysfs access, arising due to single
81 * locks, use an array of locks (e.g. open_file_mutex) and use kernfs_node
82 * object address as hash keys to get the index of these locks.
84 * Hashed mutexes are safe to use here because operations using these don't
85 * rely on global exclusion.
87 * In future we intend to replace other global locks with hashed ones as well.
88 * kernfs_global_locks acts as a holder for all such hash tables.
90 struct kernfs_global_locks {
91 struct mutex open_file_mutex[NR_KERNFS_LOCKS];
94 enum kernfs_node_type {
100 #define KERNFS_TYPE_MASK 0x000f
101 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
102 #define KERNFS_MAX_USER_XATTRS 128
103 #define KERNFS_USER_XATTR_SIZE_LIMIT (128 << 10)
105 enum kernfs_node_flag {
106 KERNFS_ACTIVATED = 0x0010,
108 KERNFS_HAS_SEQ_SHOW = 0x0040,
109 KERNFS_HAS_MMAP = 0x0080,
110 KERNFS_LOCKDEP = 0x0100,
111 KERNFS_SUICIDAL = 0x0400,
112 KERNFS_SUICIDED = 0x0800,
113 KERNFS_EMPTY_DIR = 0x1000,
114 KERNFS_HAS_RELEASE = 0x2000,
117 /* @flags for kernfs_create_root() */
118 enum kernfs_root_flag {
120 * kernfs_nodes are created in the deactivated state and invisible.
121 * They require explicit kernfs_activate() to become visible. This
122 * can be used to make related nodes become visible atomically
123 * after all nodes are created successfully.
125 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
128 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
129 * succeeds regardless of the RW permissions. sysfs had an extra
130 * layer of enforcement where open(2) fails with -EACCES regardless
131 * of CAP_DAC_OVERRIDE if the permission doesn't have the
132 * respective read or write access at all (none of S_IRUGO or
133 * S_IWUGO) or the respective operation isn't implemented. The
134 * following flag enables that behavior.
136 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
139 * The filesystem supports exportfs operation, so userspace can use
140 * fhandle to access nodes of the fs.
142 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
145 * Support user xattrs to be written to nodes rooted at this root.
147 KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008,
150 /* type-specific structures for kernfs_node union members */
151 struct kernfs_elem_dir {
152 unsigned long subdirs;
153 /* children rbtree starts here and goes through kn->rb */
154 struct rb_root children;
157 * The kernfs hierarchy this directory belongs to. This fits
158 * better directly in kernfs_node but is here to save space.
160 struct kernfs_root *root;
162 * Monotonic revision counter, used to identify if a directory
163 * node has changed during negative dentry revalidation.
168 struct kernfs_elem_symlink {
169 struct kernfs_node *target_kn;
172 struct kernfs_elem_attr {
173 const struct kernfs_ops *ops;
174 struct kernfs_open_node __rcu *open;
176 struct kernfs_node *notify_next; /* for kernfs_notify() */
180 * kernfs_node - the building block of kernfs hierarchy. Each and every
181 * kernfs node is represented by single kernfs_node. Most fields are
182 * private to kernfs and shouldn't be accessed directly by kernfs users.
184 * As long as count reference is held, the kernfs_node itself is
185 * accessible. Dereferencing elem or any other outer entity requires
191 #ifdef CONFIG_DEBUG_LOCK_ALLOC
192 struct lockdep_map dep_map;
195 * Use kernfs_get_parent() and kernfs_name/path() instead of
196 * accessing the following two fields directly. If the node is
197 * never moved to a different parent, it is safe to access the
200 struct kernfs_node *parent;
205 const void *ns; /* namespace tag */
206 unsigned int hash; /* ns + name hash */
208 struct kernfs_elem_dir dir;
209 struct kernfs_elem_symlink symlink;
210 struct kernfs_elem_attr attr;
216 * 64bit unique ID. On 64bit ino setups, id is the ino. On 32bit,
217 * the low 32bits are ino and upper generation.
221 unsigned short flags;
223 struct kernfs_iattrs *iattr;
227 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
228 * syscalls. These optional callbacks are invoked on the matching syscalls
229 * and can perform any kernfs operations which don't necessarily have to be
230 * the exact operation requested. An active reference is held for each
231 * kernfs_node parameter.
233 struct kernfs_syscall_ops {
234 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
236 int (*mkdir)(struct kernfs_node *parent, const char *name,
238 int (*rmdir)(struct kernfs_node *kn);
239 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
240 const char *new_name);
241 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
242 struct kernfs_root *root);
245 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root);
247 struct kernfs_open_file {
248 /* published fields */
249 struct kernfs_node *kn;
251 struct seq_file *seq_file;
254 /* private fields, do not use outside kernfs proper */
256 struct mutex prealloc_mutex;
258 struct list_head list;
261 size_t atomic_write_len;
264 const struct vm_operations_struct *vm_ops;
269 * Optional open/release methods. Both are called with
270 * @of->seq_file populated.
272 int (*open)(struct kernfs_open_file *of);
273 void (*release)(struct kernfs_open_file *of);
276 * Read is handled by either seq_file or raw_read().
278 * If seq_show() is present, seq_file path is active. Other seq
279 * operations are optional and if not implemented, the behavior is
280 * equivalent to single_open(). @sf->private points to the
281 * associated kernfs_open_file.
283 * read() is bounced through kernel buffer and a read larger than
284 * PAGE_SIZE results in partial operation of PAGE_SIZE.
286 int (*seq_show)(struct seq_file *sf, void *v);
288 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
289 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
290 void (*seq_stop)(struct seq_file *sf, void *v);
292 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
296 * write() is bounced through kernel buffer. If atomic_write_len
297 * is not set, a write larger than PAGE_SIZE results in partial
298 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
299 * writes upto the specified size are executed atomically but
300 * larger ones are rejected with -E2BIG.
302 size_t atomic_write_len;
304 * "prealloc" causes a buffer to be allocated at open for
305 * all read/write requests. As ->seq_show uses seq_read()
306 * which does its own allocation, it is incompatible with
307 * ->prealloc. Provide ->read and ->write with ->prealloc.
310 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
313 __poll_t (*poll)(struct kernfs_open_file *of,
314 struct poll_table_struct *pt);
316 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
320 * The kernfs superblock creation/mount parameter context.
322 struct kernfs_fs_context {
323 struct kernfs_root *root; /* Root of the hierarchy being mounted */
324 void *ns_tag; /* Namespace tag of the mount (or NULL) */
325 unsigned long magic; /* File system specific magic number */
327 /* The following are set/used by kernfs_mount() */
328 bool new_sb_created; /* Set to T if we allocated a new sb */
333 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
335 return kn->flags & KERNFS_TYPE_MASK;
338 static inline ino_t kernfs_id_ino(u64 id)
340 /* id is ino if ino_t is 64bit; otherwise, low 32bits */
341 if (sizeof(ino_t) >= sizeof(u64))
347 static inline u32 kernfs_id_gen(u64 id)
349 /* gen is fixed at 1 if ino_t is 64bit; otherwise, high 32bits */
350 if (sizeof(ino_t) >= sizeof(u64))
356 static inline ino_t kernfs_ino(struct kernfs_node *kn)
358 return kernfs_id_ino(kn->id);
361 static inline ino_t kernfs_gen(struct kernfs_node *kn)
363 return kernfs_id_gen(kn->id);
367 * kernfs_enable_ns - enable namespace under a directory
368 * @kn: directory of interest, should be empty
370 * This is to be called right after @kn is created to enable namespace
371 * under it. All children of @kn must have non-NULL namespace tags and
372 * only the ones which match the super_block's tag will be visible.
374 static inline void kernfs_enable_ns(struct kernfs_node *kn)
376 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
377 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
378 kn->flags |= KERNFS_NS;
382 * kernfs_ns_enabled - test whether namespace is enabled
383 * @kn: the node to test
385 * Test whether namespace filtering is enabled for the children of @ns.
387 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
389 return kn->flags & KERNFS_NS;
392 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
393 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
394 char *buf, size_t buflen);
395 void pr_cont_kernfs_name(struct kernfs_node *kn);
396 void pr_cont_kernfs_path(struct kernfs_node *kn);
397 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
398 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
399 const char *name, const void *ns);
400 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
401 const char *path, const void *ns);
402 void kernfs_get(struct kernfs_node *kn);
403 void kernfs_put(struct kernfs_node *kn);
405 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
406 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
407 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
409 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
410 struct super_block *sb);
411 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
412 unsigned int flags, void *priv);
413 void kernfs_destroy_root(struct kernfs_root *root);
415 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
416 const char *name, umode_t mode,
417 kuid_t uid, kgid_t gid,
418 void *priv, const void *ns);
419 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
421 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
422 const char *name, umode_t mode,
423 kuid_t uid, kgid_t gid,
425 const struct kernfs_ops *ops,
426 void *priv, const void *ns,
427 struct lock_class_key *key);
428 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
430 struct kernfs_node *target);
431 void kernfs_activate(struct kernfs_node *kn);
432 void kernfs_remove(struct kernfs_node *kn);
433 void kernfs_break_active_protection(struct kernfs_node *kn);
434 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
435 bool kernfs_remove_self(struct kernfs_node *kn);
436 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
438 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
439 const char *new_name, const void *new_ns);
440 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
441 __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
442 struct poll_table_struct *pt);
443 void kernfs_notify(struct kernfs_node *kn);
445 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
446 void *value, size_t size);
447 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
448 const void *value, size_t size, int flags);
450 const void *kernfs_super_ns(struct super_block *sb);
451 int kernfs_get_tree(struct fs_context *fc);
452 void kernfs_free_fs_context(struct fs_context *fc);
453 void kernfs_kill_sb(struct super_block *sb);
455 void kernfs_init(void);
457 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
459 #else /* CONFIG_KERNFS */
461 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
462 { return 0; } /* whatever */
464 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
466 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
469 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
472 static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
473 struct kernfs_node *kn,
474 char *buf, size_t buflen)
477 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
478 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
480 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
483 static inline struct kernfs_node *
484 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
487 static inline struct kernfs_node *
488 kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
492 static inline void kernfs_get(struct kernfs_node *kn) { }
493 static inline void kernfs_put(struct kernfs_node *kn) { }
495 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
498 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
501 static inline struct inode *
502 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
505 static inline struct kernfs_root *
506 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
508 { return ERR_PTR(-ENOSYS); }
510 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
512 static inline struct kernfs_node *
513 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
514 umode_t mode, kuid_t uid, kgid_t gid,
515 void *priv, const void *ns)
516 { return ERR_PTR(-ENOSYS); }
518 static inline struct kernfs_node *
519 __kernfs_create_file(struct kernfs_node *parent, const char *name,
520 umode_t mode, kuid_t uid, kgid_t gid,
521 loff_t size, const struct kernfs_ops *ops,
522 void *priv, const void *ns, struct lock_class_key *key)
523 { return ERR_PTR(-ENOSYS); }
525 static inline struct kernfs_node *
526 kernfs_create_link(struct kernfs_node *parent, const char *name,
527 struct kernfs_node *target)
528 { return ERR_PTR(-ENOSYS); }
530 static inline void kernfs_activate(struct kernfs_node *kn) { }
532 static inline void kernfs_remove(struct kernfs_node *kn) { }
534 static inline bool kernfs_remove_self(struct kernfs_node *kn)
537 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
538 const char *name, const void *ns)
541 static inline int kernfs_rename_ns(struct kernfs_node *kn,
542 struct kernfs_node *new_parent,
543 const char *new_name, const void *new_ns)
546 static inline int kernfs_setattr(struct kernfs_node *kn,
547 const struct iattr *iattr)
550 static inline void kernfs_notify(struct kernfs_node *kn) { }
552 static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
553 void *value, size_t size)
556 static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
557 const void *value, size_t size, int flags)
560 static inline const void *kernfs_super_ns(struct super_block *sb)
563 static inline int kernfs_get_tree(struct fs_context *fc)
566 static inline void kernfs_free_fs_context(struct fs_context *fc) { }
568 static inline void kernfs_kill_sb(struct super_block *sb) { }
570 static inline void kernfs_init(void) { }
572 #endif /* CONFIG_KERNFS */
575 * kernfs_path - build full path of a given node
576 * @kn: kernfs_node of interest
577 * @buf: buffer to copy @kn's name into
578 * @buflen: size of @buf
580 * If @kn is NULL result will be "(null)".
582 * Returns the length of the full path. If the full length is equal to or
583 * greater than @buflen, @buf contains the truncated path with the trailing
584 * '\0'. On error, -errno is returned.
586 static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
588 return kernfs_path_from_node(kn, NULL, buf, buflen);
591 static inline struct kernfs_node *
592 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
594 return kernfs_find_and_get_ns(kn, name, NULL);
597 static inline struct kernfs_node *
598 kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
600 return kernfs_walk_and_get_ns(kn, path, NULL);
603 static inline struct kernfs_node *
604 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
607 return kernfs_create_dir_ns(parent, name, mode,
608 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
612 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
615 return kernfs_remove_by_name_ns(parent, name, NULL);
618 static inline int kernfs_rename(struct kernfs_node *kn,
619 struct kernfs_node *new_parent,
620 const char *new_name)
622 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
625 #endif /* __LINUX_KERNFS_H */