2 * kernfs.h - pseudo filesystem decoupled from vfs locking
4 * This file is released under the GPLv2.
7 #ifndef __LINUX_KERNFS_H
8 #define __LINUX_KERNFS_H
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/idr.h>
15 #include <linux/lockdep.h>
16 #include <linux/rbtree.h>
17 #include <linux/atomic.h>
18 #include <linux/wait.h>
24 struct vm_area_struct;
26 struct file_system_type;
28 struct kernfs_open_node;
31 enum kernfs_node_type {
37 #define KERNFS_TYPE_MASK 0x000f
38 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
40 enum kernfs_node_flag {
41 KERNFS_ACTIVATED = 0x0010,
43 KERNFS_HAS_SEQ_SHOW = 0x0040,
44 KERNFS_HAS_MMAP = 0x0080,
45 KERNFS_LOCKDEP = 0x0100,
46 KERNFS_STATIC_NAME = 0x0200,
47 KERNFS_SUICIDAL = 0x0400,
48 KERNFS_SUICIDED = 0x0800,
51 /* @flags for kernfs_create_root() */
52 enum kernfs_root_flag {
53 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
56 /* type-specific structures for kernfs_node union members */
57 struct kernfs_elem_dir {
58 unsigned long subdirs;
59 /* children rbtree starts here and goes through kn->rb */
60 struct rb_root children;
63 * The kernfs hierarchy this directory belongs to. This fits
64 * better directly in kernfs_node but is here to save space.
66 struct kernfs_root *root;
69 struct kernfs_elem_symlink {
70 struct kernfs_node *target_kn;
73 struct kernfs_elem_attr {
74 const struct kernfs_ops *ops;
75 struct kernfs_open_node *open;
80 * kernfs_node - the building block of kernfs hierarchy. Each and every
81 * kernfs node is represented by single kernfs_node. Most fields are
82 * private to kernfs and shouldn't be accessed directly by kernfs users.
84 * As long as s_count reference is held, the kernfs_node itself is
85 * accessible. Dereferencing elem or any other outer entity requires
91 #ifdef CONFIG_DEBUG_LOCK_ALLOC
92 struct lockdep_map dep_map;
95 * Use kernfs_get_parent() and kernfs_name/path() instead of
96 * accessing the following two fields directly. If the node is
97 * never moved to a different parent, it is safe to access the
100 struct kernfs_node *parent;
105 const void *ns; /* namespace tag */
106 unsigned int hash; /* ns + name hash */
108 struct kernfs_elem_dir dir;
109 struct kernfs_elem_symlink symlink;
110 struct kernfs_elem_attr attr;
115 unsigned short flags;
118 struct kernfs_iattrs *iattr;
122 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
123 * syscalls. These optional callbacks are invoked on the matching syscalls
124 * and can perform any kernfs operations which don't necessarily have to be
125 * the exact operation requested. An active reference is held for each
126 * kernfs_node parameter.
128 struct kernfs_syscall_ops {
129 int (*remount_fs)(struct kernfs_root *root, int *flags, char *data);
130 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
132 int (*mkdir)(struct kernfs_node *parent, const char *name,
134 int (*rmdir)(struct kernfs_node *kn);
135 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
136 const char *new_name);
140 /* published fields */
141 struct kernfs_node *kn;
142 unsigned int flags; /* KERNFS_ROOT_* flags */
144 /* private fields, do not use outside kernfs proper */
146 struct kernfs_syscall_ops *syscall_ops;
147 wait_queue_head_t deactivate_waitq;
150 struct kernfs_open_file {
151 /* published fields */
152 struct kernfs_node *kn;
156 /* private fields, do not use outside kernfs proper */
159 struct list_head list;
162 const struct vm_operations_struct *vm_ops;
167 * Read is handled by either seq_file or raw_read().
169 * If seq_show() is present, seq_file path is active. Other seq
170 * operations are optional and if not implemented, the behavior is
171 * equivalent to single_open(). @sf->private points to the
172 * associated kernfs_open_file.
174 * read() is bounced through kernel buffer and a read larger than
175 * PAGE_SIZE results in partial operation of PAGE_SIZE.
177 int (*seq_show)(struct seq_file *sf, void *v);
179 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
180 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
181 void (*seq_stop)(struct seq_file *sf, void *v);
183 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
187 * write() is bounced through kernel buffer. If atomic_write_len
188 * is not set, a write larger than PAGE_SIZE results in partial
189 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
190 * writes upto the specified size are executed atomically but
191 * larger ones are rejected with -E2BIG.
193 size_t atomic_write_len;
194 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
197 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
199 #ifdef CONFIG_DEBUG_LOCK_ALLOC
200 struct lock_class_key lockdep_key;
206 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
208 return kn->flags & KERNFS_TYPE_MASK;
212 * kernfs_enable_ns - enable namespace under a directory
213 * @kn: directory of interest, should be empty
215 * This is to be called right after @kn is created to enable namespace
216 * under it. All children of @kn must have non-NULL namespace tags and
217 * only the ones which match the super_block's tag will be visible.
219 static inline void kernfs_enable_ns(struct kernfs_node *kn)
221 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
222 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
223 kn->flags |= KERNFS_NS;
227 * kernfs_ns_enabled - test whether namespace is enabled
228 * @kn: the node to test
230 * Test whether namespace filtering is enabled for the children of @ns.
232 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
234 return kn->flags & KERNFS_NS;
237 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
238 char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
240 void pr_cont_kernfs_name(struct kernfs_node *kn);
241 void pr_cont_kernfs_path(struct kernfs_node *kn);
242 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
243 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
244 const char *name, const void *ns);
245 void kernfs_get(struct kernfs_node *kn);
246 void kernfs_put(struct kernfs_node *kn);
248 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
249 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
251 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
252 unsigned int flags, void *priv);
253 void kernfs_destroy_root(struct kernfs_root *root);
255 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
256 const char *name, umode_t mode,
257 void *priv, const void *ns);
258 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
260 umode_t mode, loff_t size,
261 const struct kernfs_ops *ops,
262 void *priv, const void *ns,
264 struct lock_class_key *key);
265 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
267 struct kernfs_node *target);
268 void kernfs_activate(struct kernfs_node *kn);
269 void kernfs_remove(struct kernfs_node *kn);
270 void kernfs_break_active_protection(struct kernfs_node *kn);
271 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
272 bool kernfs_remove_self(struct kernfs_node *kn);
273 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
275 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
276 const char *new_name, const void *new_ns);
277 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
278 void kernfs_notify(struct kernfs_node *kn);
280 const void *kernfs_super_ns(struct super_block *sb);
281 struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
282 struct kernfs_root *root, bool *new_sb_created,
284 void kernfs_kill_sb(struct super_block *sb);
286 void kernfs_init(void);
288 #else /* CONFIG_KERNFS */
290 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
291 { return 0; } /* whatever */
293 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
295 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
298 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
301 static inline char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
305 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
306 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
308 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
311 static inline struct kernfs_node *
312 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
316 static inline void kernfs_get(struct kernfs_node *kn) { }
317 static inline void kernfs_put(struct kernfs_node *kn) { }
319 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
322 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
325 static inline struct kernfs_root *
326 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
328 { return ERR_PTR(-ENOSYS); }
330 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
332 static inline struct kernfs_node *
333 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
334 umode_t mode, void *priv, const void *ns)
335 { return ERR_PTR(-ENOSYS); }
337 static inline struct kernfs_node *
338 __kernfs_create_file(struct kernfs_node *parent, const char *name,
339 umode_t mode, loff_t size, const struct kernfs_ops *ops,
340 void *priv, const void *ns, bool name_is_static,
341 struct lock_class_key *key)
342 { return ERR_PTR(-ENOSYS); }
344 static inline struct kernfs_node *
345 kernfs_create_link(struct kernfs_node *parent, const char *name,
346 struct kernfs_node *target)
347 { return ERR_PTR(-ENOSYS); }
349 static inline void kernfs_activate(struct kernfs_node *kn) { }
351 static inline void kernfs_remove(struct kernfs_node *kn) { }
353 static inline bool kernfs_remove_self(struct kernfs_node *kn)
356 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
357 const char *name, const void *ns)
360 static inline int kernfs_rename_ns(struct kernfs_node *kn,
361 struct kernfs_node *new_parent,
362 const char *new_name, const void *new_ns)
365 static inline int kernfs_setattr(struct kernfs_node *kn,
366 const struct iattr *iattr)
369 static inline void kernfs_notify(struct kernfs_node *kn) { }
371 static inline const void *kernfs_super_ns(struct super_block *sb)
374 static inline struct dentry *
375 kernfs_mount_ns(struct file_system_type *fs_type, int flags,
376 struct kernfs_root *root, bool *new_sb_created, const void *ns)
377 { return ERR_PTR(-ENOSYS); }
379 static inline void kernfs_kill_sb(struct super_block *sb) { }
381 static inline void kernfs_init(void) { }
383 #endif /* CONFIG_KERNFS */
385 static inline struct kernfs_node *
386 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
388 return kernfs_find_and_get_ns(kn, name, NULL);
391 static inline struct kernfs_node *
392 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
395 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
398 static inline struct kernfs_node *
399 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
400 umode_t mode, loff_t size, const struct kernfs_ops *ops,
401 void *priv, const void *ns)
403 struct lock_class_key *key = NULL;
405 #ifdef CONFIG_DEBUG_LOCK_ALLOC
406 key = (struct lock_class_key *)&ops->lockdep_key;
408 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
412 static inline struct kernfs_node *
413 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
414 loff_t size, const struct kernfs_ops *ops, void *priv)
416 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
419 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
422 return kernfs_remove_by_name_ns(parent, name, NULL);
425 static inline int kernfs_rename(struct kernfs_node *kn,
426 struct kernfs_node *new_parent,
427 const char *new_name)
429 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
432 static inline struct dentry *
433 kernfs_mount(struct file_system_type *fs_type, int flags,
434 struct kernfs_root *root, bool *new_sb_created)
436 return kernfs_mount_ns(fs_type, flags, root, new_sb_created, NULL);
439 #endif /* __LINUX_KERNFS_H */