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