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;
94 /* the following two fields are published */
95 struct kernfs_node *parent;
100 const void *ns; /* namespace tag */
101 unsigned int hash; /* ns + name hash */
103 struct kernfs_elem_dir dir;
104 struct kernfs_elem_symlink symlink;
105 struct kernfs_elem_attr attr;
110 unsigned short flags;
113 struct kernfs_iattrs *iattr;
117 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
118 * syscalls. These optional callbacks are invoked on the matching syscalls
119 * and can perform any kernfs operations which don't necessarily have to be
120 * the exact operation requested. An active reference is held for each
121 * kernfs_node parameter.
123 struct kernfs_syscall_ops {
124 int (*remount_fs)(struct kernfs_root *root, int *flags, char *data);
125 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
127 int (*mkdir)(struct kernfs_node *parent, const char *name,
129 int (*rmdir)(struct kernfs_node *kn);
130 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
131 const char *new_name);
135 /* published fields */
136 struct kernfs_node *kn;
137 unsigned int flags; /* KERNFS_ROOT_* flags */
139 /* private fields, do not use outside kernfs proper */
141 struct kernfs_syscall_ops *syscall_ops;
142 wait_queue_head_t deactivate_waitq;
145 struct kernfs_open_file {
146 /* published fields */
147 struct kernfs_node *kn;
150 /* private fields, do not use outside kernfs proper */
153 struct list_head list;
156 const struct vm_operations_struct *vm_ops;
161 * Read is handled by either seq_file or raw_read().
163 * If seq_show() is present, seq_file path is active. Other seq
164 * operations are optional and if not implemented, the behavior is
165 * equivalent to single_open(). @sf->private points to the
166 * associated kernfs_open_file.
168 * read() is bounced through kernel buffer and a read larger than
169 * PAGE_SIZE results in partial operation of PAGE_SIZE.
171 int (*seq_show)(struct seq_file *sf, void *v);
173 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
174 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
175 void (*seq_stop)(struct seq_file *sf, void *v);
177 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
181 * write() is bounced through kernel buffer. If atomic_write_len
182 * is not set, a write larger than PAGE_SIZE results in partial
183 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
184 * writes upto the specified size are executed atomically but
185 * larger ones are rejected with -E2BIG.
187 size_t atomic_write_len;
188 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
191 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
193 #ifdef CONFIG_DEBUG_LOCK_ALLOC
194 struct lock_class_key lockdep_key;
200 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
202 return kn->flags & KERNFS_TYPE_MASK;
206 * kernfs_enable_ns - enable namespace under a directory
207 * @kn: directory of interest, should be empty
209 * This is to be called right after @kn is created to enable namespace
210 * under it. All children of @kn must have non-NULL namespace tags and
211 * only the ones which match the super_block's tag will be visible.
213 static inline void kernfs_enable_ns(struct kernfs_node *kn)
215 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
216 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
217 kn->flags |= KERNFS_NS;
221 * kernfs_ns_enabled - test whether namespace is enabled
222 * @kn: the node to test
224 * Test whether namespace filtering is enabled for the children of @ns.
226 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
228 return kn->flags & KERNFS_NS;
231 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
232 const char *name, const void *ns);
233 void kernfs_get(struct kernfs_node *kn);
234 void kernfs_put(struct kernfs_node *kn);
236 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
237 unsigned int flags, void *priv);
238 void kernfs_destroy_root(struct kernfs_root *root);
240 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
241 const char *name, umode_t mode,
242 void *priv, const void *ns);
243 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
245 umode_t mode, loff_t size,
246 const struct kernfs_ops *ops,
247 void *priv, const void *ns,
249 struct lock_class_key *key);
250 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
252 struct kernfs_node *target);
253 void kernfs_activate(struct kernfs_node *kn);
254 void kernfs_remove(struct kernfs_node *kn);
255 void kernfs_break_active_protection(struct kernfs_node *kn);
256 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
257 bool kernfs_remove_self(struct kernfs_node *kn);
258 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
260 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
261 const char *new_name, const void *new_ns);
262 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
263 void kernfs_notify(struct kernfs_node *kn);
265 const void *kernfs_super_ns(struct super_block *sb);
266 struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
267 struct kernfs_root *root, const void *ns);
268 void kernfs_kill_sb(struct super_block *sb);
270 void kernfs_init(void);
272 #else /* CONFIG_SYSFS */
274 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
275 { return 0; } /* whatever */
277 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
279 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
282 static inline struct kernfs_node *
283 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
287 static inline void kernfs_get(struct kernfs_node *kn) { }
288 static inline void kernfs_put(struct kernfs_node *kn) { }
290 static inline struct kernfs_root *
291 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
293 { return ERR_PTR(-ENOSYS); }
295 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
297 static inline struct kernfs_node *
298 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
299 umode_t mode, void *priv, const void *ns)
300 { return ERR_PTR(-ENOSYS); }
302 static inline struct kernfs_node *
303 __kernfs_create_file(struct kernfs_node *parent, const char *name,
304 umode_t mode, loff_t size, const struct kernfs_ops *ops,
305 void *priv, const void *ns, bool name_is_static,
306 struct lock_class_key *key)
307 { return ERR_PTR(-ENOSYS); }
309 static inline struct kernfs_node *
310 kernfs_create_link(struct kernfs_node *parent, const char *name,
311 struct kernfs_node *target)
312 { return ERR_PTR(-ENOSYS); }
314 static inline void kernfs_activate(struct kernfs_node *kn) { }
316 static inline void kernfs_remove(struct kernfs_node *kn) { }
318 static inline bool kernfs_remove_self(struct kernfs_node *kn)
321 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
322 const char *name, const void *ns)
325 static inline int kernfs_rename_ns(struct kernfs_node *kn,
326 struct kernfs_node *new_parent,
327 const char *new_name, const void *new_ns)
330 static inline int kernfs_setattr(struct kernfs_node *kn,
331 const struct iattr *iattr)
334 static inline void kernfs_notify(struct kernfs_node *kn) { }
336 static inline const void *kernfs_super_ns(struct super_block *sb)
339 static inline struct dentry *
340 kernfs_mount_ns(struct file_system_type *fs_type, int flags,
341 struct kernfs_root *root, const void *ns)
342 { return ERR_PTR(-ENOSYS); }
344 static inline void kernfs_kill_sb(struct super_block *sb) { }
346 static inline void kernfs_init(void) { }
348 #endif /* CONFIG_SYSFS */
350 static inline struct kernfs_node *
351 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
353 return kernfs_find_and_get_ns(kn, name, NULL);
356 static inline struct kernfs_node *
357 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
360 return kernfs_create_dir_ns(parent, name, mode, priv, NULL);
363 static inline struct kernfs_node *
364 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
365 umode_t mode, loff_t size, const struct kernfs_ops *ops,
366 void *priv, const void *ns)
368 struct lock_class_key *key = NULL;
370 #ifdef CONFIG_DEBUG_LOCK_ALLOC
371 key = (struct lock_class_key *)&ops->lockdep_key;
373 return __kernfs_create_file(parent, name, mode, size, ops, priv, ns,
377 static inline struct kernfs_node *
378 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
379 loff_t size, const struct kernfs_ops *ops, void *priv)
381 return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL);
384 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
387 return kernfs_remove_by_name_ns(parent, name, NULL);
390 static inline struct dentry *
391 kernfs_mount(struct file_system_type *fs_type, int flags,
392 struct kernfs_root *root)
394 return kernfs_mount_ns(fs_type, flags, root, NULL);
397 #endif /* __LINUX_KERNFS_H */