xref: /linux-6.15/include/linux/kernfs.h (revision 11ef5c77)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * kernfs.h - pseudo filesystem decoupled from vfs locking
4  */
5 
6 #ifndef __LINUX_KERNFS_H
7 #define __LINUX_KERNFS_H
8 
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/bug.h>
17 #include <linux/types.h>
18 #include <linux/uidgid.h>
19 #include <linux/wait.h>
20 #include <linux/rwsem.h>
21 
22 struct file;
23 struct dentry;
24 struct iattr;
25 struct seq_file;
26 struct vm_area_struct;
27 struct vm_operations_struct;
28 struct super_block;
29 struct file_system_type;
30 struct poll_table_struct;
31 struct fs_context;
32 
33 struct kernfs_fs_context;
34 struct kernfs_open_node;
35 struct kernfs_iattrs;
36 
37 enum kernfs_node_type {
38 	KERNFS_DIR		= 0x0001,
39 	KERNFS_FILE		= 0x0002,
40 	KERNFS_LINK		= 0x0004,
41 };
42 
43 #define KERNFS_TYPE_MASK		0x000f
44 #define KERNFS_FLAG_MASK		~KERNFS_TYPE_MASK
45 #define KERNFS_MAX_USER_XATTRS		128
46 #define KERNFS_USER_XATTR_SIZE_LIMIT	(128 << 10)
47 
48 enum kernfs_node_flag {
49 	KERNFS_ACTIVATED	= 0x0010,
50 	KERNFS_NS		= 0x0020,
51 	KERNFS_HAS_SEQ_SHOW	= 0x0040,
52 	KERNFS_HAS_MMAP		= 0x0080,
53 	KERNFS_LOCKDEP		= 0x0100,
54 	KERNFS_SUICIDAL		= 0x0400,
55 	KERNFS_SUICIDED		= 0x0800,
56 	KERNFS_EMPTY_DIR	= 0x1000,
57 	KERNFS_HAS_RELEASE	= 0x2000,
58 };
59 
60 /* @flags for kernfs_create_root() */
61 enum kernfs_root_flag {
62 	/*
63 	 * kernfs_nodes are created in the deactivated state and invisible.
64 	 * They require explicit kernfs_activate() to become visible.  This
65 	 * can be used to make related nodes become visible atomically
66 	 * after all nodes are created successfully.
67 	 */
68 	KERNFS_ROOT_CREATE_DEACTIVATED		= 0x0001,
69 
70 	/*
71 	 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
72 	 * succeeds regardless of the RW permissions.  sysfs had an extra
73 	 * layer of enforcement where open(2) fails with -EACCES regardless
74 	 * of CAP_DAC_OVERRIDE if the permission doesn't have the
75 	 * respective read or write access at all (none of S_IRUGO or
76 	 * S_IWUGO) or the respective operation isn't implemented.  The
77 	 * following flag enables that behavior.
78 	 */
79 	KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK	= 0x0002,
80 
81 	/*
82 	 * The filesystem supports exportfs operation, so userspace can use
83 	 * fhandle to access nodes of the fs.
84 	 */
85 	KERNFS_ROOT_SUPPORT_EXPORTOP		= 0x0004,
86 
87 	/*
88 	 * Support user xattrs to be written to nodes rooted at this root.
89 	 */
90 	KERNFS_ROOT_SUPPORT_USER_XATTR		= 0x0008,
91 };
92 
93 /* type-specific structures for kernfs_node union members */
94 struct kernfs_elem_dir {
95 	unsigned long		subdirs;
96 	/* children rbtree starts here and goes through kn->rb */
97 	struct rb_root		children;
98 
99 	/*
100 	 * The kernfs hierarchy this directory belongs to.  This fits
101 	 * better directly in kernfs_node but is here to save space.
102 	 */
103 	struct kernfs_root	*root;
104 	/*
105 	 * Monotonic revision counter, used to identify if a directory
106 	 * node has changed during negative dentry revalidation.
107 	 */
108 	unsigned long		rev;
109 };
110 
111 struct kernfs_elem_symlink {
112 	struct kernfs_node	*target_kn;
113 };
114 
115 struct kernfs_elem_attr {
116 	const struct kernfs_ops	*ops;
117 	struct kernfs_open_node	*open;
118 	loff_t			size;
119 	struct kernfs_node	*notify_next;	/* for kernfs_notify() */
120 };
121 
122 /*
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.
126  *
127  * As long as count reference is held, the kernfs_node itself is
128  * accessible.  Dereferencing elem or any other outer entity requires
129  * active reference.
130  */
131 struct kernfs_node {
132 	atomic_t		count;
133 	atomic_t		active;
134 #ifdef CONFIG_DEBUG_LOCK_ALLOC
135 	struct lockdep_map	dep_map;
136 #endif
137 	/*
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
141 	 * parent directly.
142 	 */
143 	struct kernfs_node	*parent;
144 	const char		*name;
145 
146 	struct rb_node		rb;
147 
148 	const void		*ns;	/* namespace tag */
149 	unsigned int		hash;	/* ns + name hash */
150 	union {
151 		struct kernfs_elem_dir		dir;
152 		struct kernfs_elem_symlink	symlink;
153 		struct kernfs_elem_attr		attr;
154 	};
155 
156 	void			*priv;
157 
158 	/*
159 	 * 64bit unique ID.  On 64bit ino setups, id is the ino.  On 32bit,
160 	 * the low 32bits are ino and upper generation.
161 	 */
162 	u64			id;
163 
164 	unsigned short		flags;
165 	umode_t			mode;
166 	struct kernfs_iattrs	*iattr;
167 };
168 
169 /*
170  * kernfs_syscall_ops may be specified on kernfs_create_root() to support
171  * syscalls.  These optional callbacks are invoked on the matching syscalls
172  * and can perform any kernfs operations which don't necessarily have to be
173  * the exact operation requested.  An active reference is held for each
174  * kernfs_node parameter.
175  */
176 struct kernfs_syscall_ops {
177 	int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
178 
179 	int (*mkdir)(struct kernfs_node *parent, const char *name,
180 		     umode_t mode);
181 	int (*rmdir)(struct kernfs_node *kn);
182 	int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
183 		      const char *new_name);
184 	int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
185 			 struct kernfs_root *root);
186 };
187 
188 #if 0
189 struct kernfs_root {
190 	/* published fields */
191 	struct kernfs_node	*kn;
192 	unsigned int		flags;	/* KERNFS_ROOT_* flags */
193 
194 	/* private fields, do not use outside kernfs proper */
195 	struct idr		ino_idr;
196 	u32			last_id_lowbits;
197 	u32			id_highbits;
198 	struct kernfs_syscall_ops *syscall_ops;
199 
200 	/* list of kernfs_super_info of this root, protected by kernfs_rwsem */
201 	struct list_head	supers;
202 
203 	wait_queue_head_t	deactivate_waitq;
204 	struct rw_semaphore	kernfs_rwsem;
205 };
206 #endif
207 
208 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root);
209 
210 struct kernfs_open_file {
211 	/* published fields */
212 	struct kernfs_node	*kn;
213 	struct file		*file;
214 	struct seq_file		*seq_file;
215 	void			*priv;
216 
217 	/* private fields, do not use outside kernfs proper */
218 	struct mutex		mutex;
219 	struct mutex		prealloc_mutex;
220 	int			event;
221 	struct list_head	list;
222 	char			*prealloc_buf;
223 
224 	size_t			atomic_write_len;
225 	bool			mmapped:1;
226 	bool			released:1;
227 	const struct vm_operations_struct *vm_ops;
228 };
229 
230 struct kernfs_ops {
231 	/*
232 	 * Optional open/release methods.  Both are called with
233 	 * @of->seq_file populated.
234 	 */
235 	int (*open)(struct kernfs_open_file *of);
236 	void (*release)(struct kernfs_open_file *of);
237 
238 	/*
239 	 * Read is handled by either seq_file or raw_read().
240 	 *
241 	 * If seq_show() is present, seq_file path is active.  Other seq
242 	 * operations are optional and if not implemented, the behavior is
243 	 * equivalent to single_open().  @sf->private points to the
244 	 * associated kernfs_open_file.
245 	 *
246 	 * read() is bounced through kernel buffer and a read larger than
247 	 * PAGE_SIZE results in partial operation of PAGE_SIZE.
248 	 */
249 	int (*seq_show)(struct seq_file *sf, void *v);
250 
251 	void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
252 	void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
253 	void (*seq_stop)(struct seq_file *sf, void *v);
254 
255 	ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
256 			loff_t off);
257 
258 	/*
259 	 * write() is bounced through kernel buffer.  If atomic_write_len
260 	 * is not set, a write larger than PAGE_SIZE results in partial
261 	 * operations of PAGE_SIZE chunks.  If atomic_write_len is set,
262 	 * writes upto the specified size are executed atomically but
263 	 * larger ones are rejected with -E2BIG.
264 	 */
265 	size_t atomic_write_len;
266 	/*
267 	 * "prealloc" causes a buffer to be allocated at open for
268 	 * all read/write requests.  As ->seq_show uses seq_read()
269 	 * which does its own allocation, it is incompatible with
270 	 * ->prealloc.  Provide ->read and ->write with ->prealloc.
271 	 */
272 	bool prealloc;
273 	ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
274 			 loff_t off);
275 
276 	__poll_t (*poll)(struct kernfs_open_file *of,
277 			 struct poll_table_struct *pt);
278 
279 	int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
280 };
281 
282 /*
283  * The kernfs superblock creation/mount parameter context.
284  */
285 struct kernfs_fs_context {
286 	struct kernfs_root	*root;		/* Root of the hierarchy being mounted */
287 	void			*ns_tag;	/* Namespace tag of the mount (or NULL) */
288 	unsigned long		magic;		/* File system specific magic number */
289 
290 	/* The following are set/used by kernfs_mount() */
291 	bool			new_sb_created;	/* Set to T if we allocated a new sb */
292 };
293 
294 #ifdef CONFIG_KERNFS
295 
296 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
297 {
298 	return kn->flags & KERNFS_TYPE_MASK;
299 }
300 
301 static inline ino_t kernfs_id_ino(u64 id)
302 {
303 	/* id is ino if ino_t is 64bit; otherwise, low 32bits */
304 	if (sizeof(ino_t) >= sizeof(u64))
305 		return id;
306 	else
307 		return (u32)id;
308 }
309 
310 static inline u32 kernfs_id_gen(u64 id)
311 {
312 	/* gen is fixed at 1 if ino_t is 64bit; otherwise, high 32bits */
313 	if (sizeof(ino_t) >= sizeof(u64))
314 		return 1;
315 	else
316 		return id >> 32;
317 }
318 
319 static inline ino_t kernfs_ino(struct kernfs_node *kn)
320 {
321 	return kernfs_id_ino(kn->id);
322 }
323 
324 static inline ino_t kernfs_gen(struct kernfs_node *kn)
325 {
326 	return kernfs_id_gen(kn->id);
327 }
328 
329 /**
330  * kernfs_enable_ns - enable namespace under a directory
331  * @kn: directory of interest, should be empty
332  *
333  * This is to be called right after @kn is created to enable namespace
334  * under it.  All children of @kn must have non-NULL namespace tags and
335  * only the ones which match the super_block's tag will be visible.
336  */
337 static inline void kernfs_enable_ns(struct kernfs_node *kn)
338 {
339 	WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
340 	WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
341 	kn->flags |= KERNFS_NS;
342 }
343 
344 /**
345  * kernfs_ns_enabled - test whether namespace is enabled
346  * @kn: the node to test
347  *
348  * Test whether namespace filtering is enabled for the children of @ns.
349  */
350 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
351 {
352 	return kn->flags & KERNFS_NS;
353 }
354 
355 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
356 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
357 			  char *buf, size_t buflen);
358 void pr_cont_kernfs_name(struct kernfs_node *kn);
359 void pr_cont_kernfs_path(struct kernfs_node *kn);
360 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
361 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
362 					   const char *name, const void *ns);
363 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
364 					   const char *path, const void *ns);
365 void kernfs_get(struct kernfs_node *kn);
366 void kernfs_put(struct kernfs_node *kn);
367 
368 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
369 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
370 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
371 
372 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
373 				  struct super_block *sb);
374 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
375 				       unsigned int flags, void *priv);
376 void kernfs_destroy_root(struct kernfs_root *root);
377 
378 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
379 					 const char *name, umode_t mode,
380 					 kuid_t uid, kgid_t gid,
381 					 void *priv, const void *ns);
382 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
383 					    const char *name);
384 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
385 					 const char *name, umode_t mode,
386 					 kuid_t uid, kgid_t gid,
387 					 loff_t size,
388 					 const struct kernfs_ops *ops,
389 					 void *priv, const void *ns,
390 					 struct lock_class_key *key);
391 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
392 				       const char *name,
393 				       struct kernfs_node *target);
394 void kernfs_activate(struct kernfs_node *kn);
395 void kernfs_remove(struct kernfs_node *kn);
396 void kernfs_break_active_protection(struct kernfs_node *kn);
397 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
398 bool kernfs_remove_self(struct kernfs_node *kn);
399 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
400 			     const void *ns);
401 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
402 		     const char *new_name, const void *new_ns);
403 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
404 __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
405 			     struct poll_table_struct *pt);
406 void kernfs_notify(struct kernfs_node *kn);
407 
408 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
409 		     void *value, size_t size);
410 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
411 		     const void *value, size_t size, int flags);
412 
413 const void *kernfs_super_ns(struct super_block *sb);
414 int kernfs_get_tree(struct fs_context *fc);
415 void kernfs_free_fs_context(struct fs_context *fc);
416 void kernfs_kill_sb(struct super_block *sb);
417 
418 void kernfs_init(void);
419 
420 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
421 						   u64 id);
422 #else	/* CONFIG_KERNFS */
423 
424 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
425 { return 0; }	/* whatever */
426 
427 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
428 
429 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
430 { return false; }
431 
432 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
433 { return -ENOSYS; }
434 
435 static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
436 					struct kernfs_node *kn,
437 					char *buf, size_t buflen)
438 { return -ENOSYS; }
439 
440 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
441 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
442 
443 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
444 { return NULL; }
445 
446 static inline struct kernfs_node *
447 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
448 		       const void *ns)
449 { return NULL; }
450 static inline struct kernfs_node *
451 kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
452 		       const void *ns)
453 { return NULL; }
454 
455 static inline void kernfs_get(struct kernfs_node *kn) { }
456 static inline void kernfs_put(struct kernfs_node *kn) { }
457 
458 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
459 { return NULL; }
460 
461 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
462 { return NULL; }
463 
464 static inline struct inode *
465 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
466 { return NULL; }
467 
468 static inline struct kernfs_root *
469 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
470 		   void *priv)
471 { return ERR_PTR(-ENOSYS); }
472 
473 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
474 
475 static inline struct kernfs_node *
476 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
477 		     umode_t mode, kuid_t uid, kgid_t gid,
478 		     void *priv, const void *ns)
479 { return ERR_PTR(-ENOSYS); }
480 
481 static inline struct kernfs_node *
482 __kernfs_create_file(struct kernfs_node *parent, const char *name,
483 		     umode_t mode, kuid_t uid, kgid_t gid,
484 		     loff_t size, const struct kernfs_ops *ops,
485 		     void *priv, const void *ns, struct lock_class_key *key)
486 { return ERR_PTR(-ENOSYS); }
487 
488 static inline struct kernfs_node *
489 kernfs_create_link(struct kernfs_node *parent, const char *name,
490 		   struct kernfs_node *target)
491 { return ERR_PTR(-ENOSYS); }
492 
493 static inline void kernfs_activate(struct kernfs_node *kn) { }
494 
495 static inline void kernfs_remove(struct kernfs_node *kn) { }
496 
497 static inline bool kernfs_remove_self(struct kernfs_node *kn)
498 { return false; }
499 
500 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
501 					   const char *name, const void *ns)
502 { return -ENOSYS; }
503 
504 static inline int kernfs_rename_ns(struct kernfs_node *kn,
505 				   struct kernfs_node *new_parent,
506 				   const char *new_name, const void *new_ns)
507 { return -ENOSYS; }
508 
509 static inline int kernfs_setattr(struct kernfs_node *kn,
510 				 const struct iattr *iattr)
511 { return -ENOSYS; }
512 
513 static inline void kernfs_notify(struct kernfs_node *kn) { }
514 
515 static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
516 				   void *value, size_t size)
517 { return -ENOSYS; }
518 
519 static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
520 				   const void *value, size_t size, int flags)
521 { return -ENOSYS; }
522 
523 static inline const void *kernfs_super_ns(struct super_block *sb)
524 { return NULL; }
525 
526 static inline int kernfs_get_tree(struct fs_context *fc)
527 { return -ENOSYS; }
528 
529 static inline void kernfs_free_fs_context(struct fs_context *fc) { }
530 
531 static inline void kernfs_kill_sb(struct super_block *sb) { }
532 
533 static inline void kernfs_init(void) { }
534 
535 #endif	/* CONFIG_KERNFS */
536 
537 /**
538  * kernfs_path - build full path of a given node
539  * @kn: kernfs_node of interest
540  * @buf: buffer to copy @kn's name into
541  * @buflen: size of @buf
542  *
543  * If @kn is NULL result will be "(null)".
544  *
545  * Returns the length of the full path.  If the full length is equal to or
546  * greater than @buflen, @buf contains the truncated path with the trailing
547  * '\0'.  On error, -errno is returned.
548  */
549 static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
550 {
551 	return kernfs_path_from_node(kn, NULL, buf, buflen);
552 }
553 
554 static inline struct kernfs_node *
555 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
556 {
557 	return kernfs_find_and_get_ns(kn, name, NULL);
558 }
559 
560 static inline struct kernfs_node *
561 kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
562 {
563 	return kernfs_walk_and_get_ns(kn, path, NULL);
564 }
565 
566 static inline struct kernfs_node *
567 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
568 		  void *priv)
569 {
570 	return kernfs_create_dir_ns(parent, name, mode,
571 				    GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
572 				    priv, NULL);
573 }
574 
575 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
576 					const char *name)
577 {
578 	return kernfs_remove_by_name_ns(parent, name, NULL);
579 }
580 
581 static inline int kernfs_rename(struct kernfs_node *kn,
582 				struct kernfs_node *new_parent,
583 				const char *new_name)
584 {
585 	return kernfs_rename_ns(kn, new_parent, new_name, NULL);
586 }
587 
588 #endif	/* __LINUX_KERNFS_H */
589