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
43 enum kernfs_node_flag
{
44 KERNFS_ACTIVATED
= 0x0010,
46 KERNFS_HAS_SEQ_SHOW
= 0x0040,
47 KERNFS_HAS_MMAP
= 0x0080,
48 KERNFS_LOCKDEP
= 0x0100,
49 KERNFS_SUICIDAL
= 0x0400,
50 KERNFS_SUICIDED
= 0x0800,
51 KERNFS_EMPTY_DIR
= 0x1000,
52 KERNFS_HAS_RELEASE
= 0x2000,
55 /* @flags for kernfs_create_root() */
56 enum kernfs_root_flag
{
58 * kernfs_nodes are created in the deactivated state and invisible.
59 * They require explicit kernfs_activate() to become visible. This
60 * can be used to make related nodes become visible atomically
61 * after all nodes are created successfully.
63 KERNFS_ROOT_CREATE_DEACTIVATED
= 0x0001,
66 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
67 * succeeds regardless of the RW permissions. sysfs had an extra
68 * layer of enforcement where open(2) fails with -EACCES regardless
69 * of CAP_DAC_OVERRIDE if the permission doesn't have the
70 * respective read or write access at all (none of S_IRUGO or
71 * S_IWUGO) or the respective operation isn't implemented. The
72 * following flag enables that behavior.
74 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK
= 0x0002,
77 * The filesystem supports exportfs operation, so userspace can use
78 * fhandle to access nodes of the fs.
80 KERNFS_ROOT_SUPPORT_EXPORTOP
= 0x0004,
83 /* type-specific structures for kernfs_node union members */
84 struct kernfs_elem_dir
{
85 unsigned long subdirs
;
86 /* children rbtree starts here and goes through kn->rb */
87 struct rb_root children
;
90 * The kernfs hierarchy this directory belongs to. This fits
91 * better directly in kernfs_node but is here to save space.
93 struct kernfs_root
*root
;
96 struct kernfs_elem_symlink
{
97 struct kernfs_node
*target_kn
;
100 struct kernfs_elem_attr
{
101 const struct kernfs_ops
*ops
;
102 struct kernfs_open_node
*open
;
104 struct kernfs_node
*notify_next
; /* for kernfs_notify() */
107 /* represent a kernfs node */
108 union kernfs_node_id
{
111 * blktrace will export this struct as a simplified 'struct
112 * fid' (which is a big data struction), so userspace can use
113 * it to find kernfs node. The layout must match the first two
114 * fields of 'struct fid' exactly.
123 * kernfs_node - the building block of kernfs hierarchy. Each and every
124 * kernfs node is represented by single kernfs_node. Most fields are
125 * private to kernfs and shouldn't be accessed directly by kernfs users.
127 * As long as s_count reference is held, the kernfs_node itself is
128 * accessible. Dereferencing elem or any other outer entity requires
134 #ifdef CONFIG_DEBUG_LOCK_ALLOC
135 struct lockdep_map dep_map
;
138 * Use kernfs_get_parent() and kernfs_name/path() instead of
139 * accessing the following two fields directly. If the node is
140 * never moved to a different parent, it is safe to access the
143 struct kernfs_node
*parent
;
148 const void *ns
; /* namespace tag */
149 unsigned int hash
; /* ns + name hash */
151 struct kernfs_elem_dir dir
;
152 struct kernfs_elem_symlink symlink
;
153 struct kernfs_elem_attr attr
;
158 union kernfs_node_id id
;
159 unsigned short flags
;
161 struct kernfs_iattrs
*iattr
;
165 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
166 * syscalls. These optional callbacks are invoked on the matching syscalls
167 * and can perform any kernfs operations which don't necessarily have to be
168 * the exact operation requested. An active reference is held for each
169 * kernfs_node parameter.
171 struct kernfs_syscall_ops
{
172 int (*show_options
)(struct seq_file
*sf
, struct kernfs_root
*root
);
174 int (*mkdir
)(struct kernfs_node
*parent
, const char *name
,
176 int (*rmdir
)(struct kernfs_node
*kn
);
177 int (*rename
)(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
178 const char *new_name
);
179 int (*show_path
)(struct seq_file
*sf
, struct kernfs_node
*kn
,
180 struct kernfs_root
*root
);
184 /* published fields */
185 struct kernfs_node
*kn
;
186 unsigned int flags
; /* KERNFS_ROOT_* flags */
188 /* private fields, do not use outside kernfs proper */
191 struct kernfs_syscall_ops
*syscall_ops
;
193 /* list of kernfs_super_info of this root, protected by kernfs_mutex */
194 struct list_head supers
;
196 wait_queue_head_t deactivate_waitq
;
199 struct kernfs_open_file
{
200 /* published fields */
201 struct kernfs_node
*kn
;
203 struct seq_file
*seq_file
;
206 /* private fields, do not use outside kernfs proper */
208 struct mutex prealloc_mutex
;
210 struct list_head list
;
213 size_t atomic_write_len
;
216 const struct vm_operations_struct
*vm_ops
;
221 * Optional open/release methods. Both are called with
222 * @of->seq_file populated.
224 int (*open
)(struct kernfs_open_file
*of
);
225 void (*release
)(struct kernfs_open_file
*of
);
228 * Read is handled by either seq_file or raw_read().
230 * If seq_show() is present, seq_file path is active. Other seq
231 * operations are optional and if not implemented, the behavior is
232 * equivalent to single_open(). @sf->private points to the
233 * associated kernfs_open_file.
235 * read() is bounced through kernel buffer and a read larger than
236 * PAGE_SIZE results in partial operation of PAGE_SIZE.
238 int (*seq_show
)(struct seq_file
*sf
, void *v
);
240 void *(*seq_start
)(struct seq_file
*sf
, loff_t
*ppos
);
241 void *(*seq_next
)(struct seq_file
*sf
, void *v
, loff_t
*ppos
);
242 void (*seq_stop
)(struct seq_file
*sf
, void *v
);
244 ssize_t (*read
)(struct kernfs_open_file
*of
, char *buf
, size_t bytes
,
248 * write() is bounced through kernel buffer. If atomic_write_len
249 * is not set, a write larger than PAGE_SIZE results in partial
250 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
251 * writes upto the specified size are executed atomically but
252 * larger ones are rejected with -E2BIG.
254 size_t atomic_write_len
;
256 * "prealloc" causes a buffer to be allocated at open for
257 * all read/write requests. As ->seq_show uses seq_read()
258 * which does its own allocation, it is incompatible with
259 * ->prealloc. Provide ->read and ->write with ->prealloc.
262 ssize_t (*write
)(struct kernfs_open_file
*of
, char *buf
, size_t bytes
,
265 __poll_t (*poll
)(struct kernfs_open_file
*of
,
266 struct poll_table_struct
*pt
);
268 int (*mmap
)(struct kernfs_open_file
*of
, struct vm_area_struct
*vma
);
270 #ifdef CONFIG_DEBUG_LOCK_ALLOC
271 struct lock_class_key lockdep_key
;
276 * The kernfs superblock creation/mount parameter context.
278 struct kernfs_fs_context
{
279 struct kernfs_root
*root
; /* Root of the hierarchy being mounted */
280 void *ns_tag
; /* Namespace tag of the mount (or NULL) */
281 unsigned long magic
; /* File system specific magic number */
283 /* The following are set/used by kernfs_mount() */
284 bool new_sb_created
; /* Set to T if we allocated a new sb */
289 static inline enum kernfs_node_type
kernfs_type(struct kernfs_node
*kn
)
291 return kn
->flags
& KERNFS_TYPE_MASK
;
295 * kernfs_enable_ns - enable namespace under a directory
296 * @kn: directory of interest, should be empty
298 * This is to be called right after @kn is created to enable namespace
299 * under it. All children of @kn must have non-NULL namespace tags and
300 * only the ones which match the super_block's tag will be visible.
302 static inline void kernfs_enable_ns(struct kernfs_node
*kn
)
304 WARN_ON_ONCE(kernfs_type(kn
) != KERNFS_DIR
);
305 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn
->dir
.children
));
306 kn
->flags
|= KERNFS_NS
;
310 * kernfs_ns_enabled - test whether namespace is enabled
311 * @kn: the node to test
313 * Test whether namespace filtering is enabled for the children of @ns.
315 static inline bool kernfs_ns_enabled(struct kernfs_node
*kn
)
317 return kn
->flags
& KERNFS_NS
;
320 int kernfs_name(struct kernfs_node
*kn
, char *buf
, size_t buflen
);
321 int kernfs_path_from_node(struct kernfs_node
*root_kn
, struct kernfs_node
*kn
,
322 char *buf
, size_t buflen
);
323 void pr_cont_kernfs_name(struct kernfs_node
*kn
);
324 void pr_cont_kernfs_path(struct kernfs_node
*kn
);
325 struct kernfs_node
*kernfs_get_parent(struct kernfs_node
*kn
);
326 struct kernfs_node
*kernfs_find_and_get_ns(struct kernfs_node
*parent
,
327 const char *name
, const void *ns
);
328 struct kernfs_node
*kernfs_walk_and_get_ns(struct kernfs_node
*parent
,
329 const char *path
, const void *ns
);
330 void kernfs_get(struct kernfs_node
*kn
);
331 void kernfs_put(struct kernfs_node
*kn
);
333 struct kernfs_node
*kernfs_node_from_dentry(struct dentry
*dentry
);
334 struct kernfs_root
*kernfs_root_from_sb(struct super_block
*sb
);
335 struct inode
*kernfs_get_inode(struct super_block
*sb
, struct kernfs_node
*kn
);
337 struct dentry
*kernfs_node_dentry(struct kernfs_node
*kn
,
338 struct super_block
*sb
);
339 struct kernfs_root
*kernfs_create_root(struct kernfs_syscall_ops
*scops
,
340 unsigned int flags
, void *priv
);
341 void kernfs_destroy_root(struct kernfs_root
*root
);
343 struct kernfs_node
*kernfs_create_dir_ns(struct kernfs_node
*parent
,
344 const char *name
, umode_t mode
,
345 kuid_t uid
, kgid_t gid
,
346 void *priv
, const void *ns
);
347 struct kernfs_node
*kernfs_create_empty_dir(struct kernfs_node
*parent
,
349 struct kernfs_node
*__kernfs_create_file(struct kernfs_node
*parent
,
350 const char *name
, umode_t mode
,
351 kuid_t uid
, kgid_t gid
,
353 const struct kernfs_ops
*ops
,
354 void *priv
, const void *ns
,
355 struct lock_class_key
*key
);
356 struct kernfs_node
*kernfs_create_link(struct kernfs_node
*parent
,
358 struct kernfs_node
*target
);
359 void kernfs_activate(struct kernfs_node
*kn
);
360 void kernfs_remove(struct kernfs_node
*kn
);
361 void kernfs_break_active_protection(struct kernfs_node
*kn
);
362 void kernfs_unbreak_active_protection(struct kernfs_node
*kn
);
363 bool kernfs_remove_self(struct kernfs_node
*kn
);
364 int kernfs_remove_by_name_ns(struct kernfs_node
*parent
, const char *name
,
366 int kernfs_rename_ns(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
367 const char *new_name
, const void *new_ns
);
368 int kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
);
369 __poll_t
kernfs_generic_poll(struct kernfs_open_file
*of
,
370 struct poll_table_struct
*pt
);
371 void kernfs_notify(struct kernfs_node
*kn
);
373 int kernfs_xattr_get(struct kernfs_node
*kn
, const char *name
,
374 void *value
, size_t size
);
375 int kernfs_xattr_set(struct kernfs_node
*kn
, const char *name
,
376 const void *value
, size_t size
, int flags
);
378 const void *kernfs_super_ns(struct super_block
*sb
);
379 int kernfs_get_tree(struct fs_context
*fc
);
380 void kernfs_free_fs_context(struct fs_context
*fc
);
381 void kernfs_kill_sb(struct super_block
*sb
);
383 void kernfs_init(void);
385 struct kernfs_node
*kernfs_get_node_by_id(struct kernfs_root
*root
,
386 const union kernfs_node_id
*id
);
387 #else /* CONFIG_KERNFS */
389 static inline enum kernfs_node_type
kernfs_type(struct kernfs_node
*kn
)
390 { return 0; } /* whatever */
392 static inline void kernfs_enable_ns(struct kernfs_node
*kn
) { }
394 static inline bool kernfs_ns_enabled(struct kernfs_node
*kn
)
397 static inline int kernfs_name(struct kernfs_node
*kn
, char *buf
, size_t buflen
)
400 static inline int kernfs_path_from_node(struct kernfs_node
*root_kn
,
401 struct kernfs_node
*kn
,
402 char *buf
, size_t buflen
)
405 static inline void pr_cont_kernfs_name(struct kernfs_node
*kn
) { }
406 static inline void pr_cont_kernfs_path(struct kernfs_node
*kn
) { }
408 static inline struct kernfs_node
*kernfs_get_parent(struct kernfs_node
*kn
)
411 static inline struct kernfs_node
*
412 kernfs_find_and_get_ns(struct kernfs_node
*parent
, const char *name
,
415 static inline struct kernfs_node
*
416 kernfs_walk_and_get_ns(struct kernfs_node
*parent
, const char *path
,
420 static inline void kernfs_get(struct kernfs_node
*kn
) { }
421 static inline void kernfs_put(struct kernfs_node
*kn
) { }
423 static inline struct kernfs_node
*kernfs_node_from_dentry(struct dentry
*dentry
)
426 static inline struct kernfs_root
*kernfs_root_from_sb(struct super_block
*sb
)
429 static inline struct inode
*
430 kernfs_get_inode(struct super_block
*sb
, struct kernfs_node
*kn
)
433 static inline struct kernfs_root
*
434 kernfs_create_root(struct kernfs_syscall_ops
*scops
, unsigned int flags
,
436 { return ERR_PTR(-ENOSYS
); }
438 static inline void kernfs_destroy_root(struct kernfs_root
*root
) { }
440 static inline struct kernfs_node
*
441 kernfs_create_dir_ns(struct kernfs_node
*parent
, const char *name
,
442 umode_t mode
, kuid_t uid
, kgid_t gid
,
443 void *priv
, const void *ns
)
444 { return ERR_PTR(-ENOSYS
); }
446 static inline struct kernfs_node
*
447 __kernfs_create_file(struct kernfs_node
*parent
, const char *name
,
448 umode_t mode
, kuid_t uid
, kgid_t gid
,
449 loff_t size
, const struct kernfs_ops
*ops
,
450 void *priv
, const void *ns
, struct lock_class_key
*key
)
451 { return ERR_PTR(-ENOSYS
); }
453 static inline struct kernfs_node
*
454 kernfs_create_link(struct kernfs_node
*parent
, const char *name
,
455 struct kernfs_node
*target
)
456 { return ERR_PTR(-ENOSYS
); }
458 static inline void kernfs_activate(struct kernfs_node
*kn
) { }
460 static inline void kernfs_remove(struct kernfs_node
*kn
) { }
462 static inline bool kernfs_remove_self(struct kernfs_node
*kn
)
465 static inline int kernfs_remove_by_name_ns(struct kernfs_node
*kn
,
466 const char *name
, const void *ns
)
469 static inline int kernfs_rename_ns(struct kernfs_node
*kn
,
470 struct kernfs_node
*new_parent
,
471 const char *new_name
, const void *new_ns
)
474 static inline int kernfs_setattr(struct kernfs_node
*kn
,
475 const struct iattr
*iattr
)
478 static inline void kernfs_notify(struct kernfs_node
*kn
) { }
480 static inline int kernfs_xattr_get(struct kernfs_node
*kn
, const char *name
,
481 void *value
, size_t size
)
484 static inline int kernfs_xattr_set(struct kernfs_node
*kn
, const char *name
,
485 const void *value
, size_t size
, int flags
)
488 static inline const void *kernfs_super_ns(struct super_block
*sb
)
491 static inline int kernfs_get_tree(struct fs_context
*fc
)
494 static inline void kernfs_free_fs_context(struct fs_context
*fc
) { }
496 static inline void kernfs_kill_sb(struct super_block
*sb
) { }
498 static inline void kernfs_init(void) { }
500 #endif /* CONFIG_KERNFS */
503 * kernfs_path - build full path of a given node
504 * @kn: kernfs_node of interest
505 * @buf: buffer to copy @kn's name into
506 * @buflen: size of @buf
508 * If @kn is NULL result will be "(null)".
510 * Returns the length of the full path. If the full length is equal to or
511 * greater than @buflen, @buf contains the truncated path with the trailing
512 * '\0'. On error, -errno is returned.
514 static inline int kernfs_path(struct kernfs_node
*kn
, char *buf
, size_t buflen
)
516 return kernfs_path_from_node(kn
, NULL
, buf
, buflen
);
519 static inline struct kernfs_node
*
520 kernfs_find_and_get(struct kernfs_node
*kn
, const char *name
)
522 return kernfs_find_and_get_ns(kn
, name
, NULL
);
525 static inline struct kernfs_node
*
526 kernfs_walk_and_get(struct kernfs_node
*kn
, const char *path
)
528 return kernfs_walk_and_get_ns(kn
, path
, NULL
);
531 static inline struct kernfs_node
*
532 kernfs_create_dir(struct kernfs_node
*parent
, const char *name
, umode_t mode
,
535 return kernfs_create_dir_ns(parent
, name
, mode
,
536 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
,
540 static inline struct kernfs_node
*
541 kernfs_create_file_ns(struct kernfs_node
*parent
, const char *name
,
542 umode_t mode
, kuid_t uid
, kgid_t gid
,
543 loff_t size
, const struct kernfs_ops
*ops
,
544 void *priv
, const void *ns
)
546 struct lock_class_key
*key
= NULL
;
548 #ifdef CONFIG_DEBUG_LOCK_ALLOC
549 key
= (struct lock_class_key
*)&ops
->lockdep_key
;
551 return __kernfs_create_file(parent
, name
, mode
, uid
, gid
,
552 size
, ops
, priv
, ns
, key
);
555 static inline struct kernfs_node
*
556 kernfs_create_file(struct kernfs_node
*parent
, const char *name
, umode_t mode
,
557 loff_t size
, const struct kernfs_ops
*ops
, void *priv
)
559 return kernfs_create_file_ns(parent
, name
, mode
,
560 GLOBAL_ROOT_UID
, GLOBAL_ROOT_GID
,
561 size
, ops
, priv
, NULL
);
564 static inline int kernfs_remove_by_name(struct kernfs_node
*parent
,
567 return kernfs_remove_by_name_ns(parent
, name
, NULL
);
570 static inline int kernfs_rename(struct kernfs_node
*kn
,
571 struct kernfs_node
*new_parent
,
572 const char *new_name
)
574 return kernfs_rename_ns(kn
, new_parent
, new_name
, NULL
);
577 #endif /* __LINUX_KERNFS_H */