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>
23 struct vm_area_struct
;
25 struct file_system_type
;
27 struct kernfs_open_node
;
30 enum kernfs_node_type
{
36 #define KERNFS_TYPE_MASK 0x000f
37 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
39 enum kernfs_node_flag
{
40 KERNFS_JUST_DEACTIVATED
= 0x0010, /* used to aid lockdep annotation */
42 KERNFS_HAS_SEQ_SHOW
= 0x0040,
43 KERNFS_HAS_MMAP
= 0x0080,
44 KERNFS_LOCKDEP
= 0x0100,
45 KERNFS_STATIC_NAME
= 0x0200,
48 /* type-specific structures for kernfs_node union members */
49 struct kernfs_elem_dir
{
50 unsigned long subdirs
;
51 /* children rbtree starts here and goes through kn->rb */
52 struct rb_root children
;
55 * The kernfs hierarchy this directory belongs to. This fits
56 * better directly in kernfs_node but is here to save space.
58 struct kernfs_root
*root
;
61 struct kernfs_elem_symlink
{
62 struct kernfs_node
*target_kn
;
65 struct kernfs_elem_attr
{
66 const struct kernfs_ops
*ops
;
67 struct kernfs_open_node
*open
;
72 * kernfs_node - the building block of kernfs hierarchy. Each and every
73 * kernfs node is represented by single kernfs_node. Most fields are
74 * private to kernfs and shouldn't be accessed directly by kernfs users.
76 * As long as s_count reference is held, the kernfs_node itself is
77 * accessible. Dereferencing elem or any other outer entity requires
83 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84 struct lockdep_map dep_map
;
86 /* the following two fields are published */
87 struct kernfs_node
*parent
;
92 const void *ns
; /* namespace tag */
93 unsigned int hash
; /* ns + name hash */
95 struct kernfs_elem_dir dir
;
96 struct kernfs_elem_symlink symlink
;
97 struct kernfs_elem_attr attr
;
102 unsigned short flags
;
105 struct kernfs_iattrs
*iattr
;
109 * kernfs_dir_ops may be specified on kernfs_create_root() to support
110 * directory manipulation syscalls. These optional callbacks are invoked
111 * on the matching syscalls and can perform any kernfs operations which
112 * don't necessarily have to be the exact operation requested.
114 struct kernfs_dir_ops
{
115 int (*mkdir
)(struct kernfs_node
*parent
, const char *name
,
117 int (*rmdir
)(struct kernfs_node
*kn
);
118 int (*rename
)(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
119 const char *new_name
);
123 /* published fields */
124 struct kernfs_node
*kn
;
126 /* private fields, do not use outside kernfs proper */
128 struct kernfs_dir_ops
*dir_ops
;
129 wait_queue_head_t deactivate_waitq
;
132 struct kernfs_open_file
{
133 /* published fields */
134 struct kernfs_node
*kn
;
137 /* private fields, do not use outside kernfs proper */
140 struct list_head list
;
143 const struct vm_operations_struct
*vm_ops
;
148 * Read is handled by either seq_file or raw_read().
150 * If seq_show() is present, seq_file path is active. Other seq
151 * operations are optional and if not implemented, the behavior is
152 * equivalent to single_open(). @sf->private points to the
153 * associated kernfs_open_file.
155 * read() is bounced through kernel buffer and a read larger than
156 * PAGE_SIZE results in partial operation of PAGE_SIZE.
158 int (*seq_show
)(struct seq_file
*sf
, void *v
);
160 void *(*seq_start
)(struct seq_file
*sf
, loff_t
*ppos
);
161 void *(*seq_next
)(struct seq_file
*sf
, void *v
, loff_t
*ppos
);
162 void (*seq_stop
)(struct seq_file
*sf
, void *v
);
164 ssize_t (*read
)(struct kernfs_open_file
*of
, char *buf
, size_t bytes
,
168 * write() is bounced through kernel buffer and a write larger than
169 * PAGE_SIZE results in partial operation of PAGE_SIZE.
171 ssize_t (*write
)(struct kernfs_open_file
*of
, char *buf
, size_t bytes
,
174 int (*mmap
)(struct kernfs_open_file
*of
, struct vm_area_struct
*vma
);
176 #ifdef CONFIG_DEBUG_LOCK_ALLOC
177 struct lock_class_key lockdep_key
;
183 static inline enum kernfs_node_type
kernfs_type(struct kernfs_node
*kn
)
185 return kn
->flags
& KERNFS_TYPE_MASK
;
189 * kernfs_enable_ns - enable namespace under a directory
190 * @kn: directory of interest, should be empty
192 * This is to be called right after @kn is created to enable namespace
193 * under it. All children of @kn must have non-NULL namespace tags and
194 * only the ones which match the super_block's tag will be visible.
196 static inline void kernfs_enable_ns(struct kernfs_node
*kn
)
198 WARN_ON_ONCE(kernfs_type(kn
) != KERNFS_DIR
);
199 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn
->dir
.children
));
200 kn
->flags
|= KERNFS_NS
;
204 * kernfs_ns_enabled - test whether namespace is enabled
205 * @kn: the node to test
207 * Test whether namespace filtering is enabled for the children of @ns.
209 static inline bool kernfs_ns_enabled(struct kernfs_node
*kn
)
211 return kn
->flags
& KERNFS_NS
;
214 struct kernfs_node
*kernfs_find_and_get_ns(struct kernfs_node
*parent
,
215 const char *name
, const void *ns
);
216 void kernfs_get(struct kernfs_node
*kn
);
217 void kernfs_put(struct kernfs_node
*kn
);
219 struct kernfs_root
*kernfs_create_root(struct kernfs_dir_ops
*kdops
,
221 void kernfs_destroy_root(struct kernfs_root
*root
);
223 struct kernfs_node
*kernfs_create_dir_ns(struct kernfs_node
*parent
,
224 const char *name
, umode_t mode
,
225 void *priv
, const void *ns
);
226 struct kernfs_node
*__kernfs_create_file(struct kernfs_node
*parent
,
228 umode_t mode
, loff_t size
,
229 const struct kernfs_ops
*ops
,
230 void *priv
, const void *ns
,
232 struct lock_class_key
*key
);
233 struct kernfs_node
*kernfs_create_link(struct kernfs_node
*parent
,
235 struct kernfs_node
*target
);
236 void kernfs_remove(struct kernfs_node
*kn
);
237 int kernfs_remove_by_name_ns(struct kernfs_node
*parent
, const char *name
,
239 int kernfs_rename_ns(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
240 const char *new_name
, const void *new_ns
);
241 int kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
);
242 void kernfs_notify(struct kernfs_node
*kn
);
244 const void *kernfs_super_ns(struct super_block
*sb
);
245 struct dentry
*kernfs_mount_ns(struct file_system_type
*fs_type
, int flags
,
246 struct kernfs_root
*root
, const void *ns
);
247 void kernfs_kill_sb(struct super_block
*sb
);
249 void kernfs_init(void);
251 #else /* CONFIG_SYSFS */
253 static inline enum kernfs_node_type
kernfs_type(struct kernfs_node
*kn
)
254 { return 0; } /* whatever */
256 static inline void kernfs_enable_ns(struct kernfs_node
*kn
) { }
258 static inline bool kernfs_ns_enabled(struct kernfs_node
*kn
)
261 static inline struct kernfs_node
*
262 kernfs_find_and_get_ns(struct kernfs_node
*parent
, const char *name
,
266 static inline void kernfs_get(struct kernfs_node
*kn
) { }
267 static inline void kernfs_put(struct kernfs_node
*kn
) { }
269 static inline struct kernfs_root
*
270 kernfs_create_root(struct kernfs_dir_ops
*kdops
, void *priv
)
271 { return ERR_PTR(-ENOSYS
); }
273 static inline void kernfs_destroy_root(struct kernfs_root
*root
) { }
275 static inline struct kernfs_node
*
276 kernfs_create_dir_ns(struct kernfs_node
*parent
, const char *name
,
277 umode_t mode
, void *priv
, const void *ns
)
278 { return ERR_PTR(-ENOSYS
); }
280 static inline struct kernfs_node
*
281 __kernfs_create_file(struct kernfs_node
*parent
, const char *name
,
282 umode_t mode
, loff_t size
, const struct kernfs_ops
*ops
,
283 void *priv
, const void *ns
, bool name_is_static
,
284 struct lock_class_key
*key
)
285 { return ERR_PTR(-ENOSYS
); }
287 static inline struct kernfs_node
*
288 kernfs_create_link(struct kernfs_node
*parent
, const char *name
,
289 struct kernfs_node
*target
)
290 { return ERR_PTR(-ENOSYS
); }
292 static inline void kernfs_remove(struct kernfs_node
*kn
) { }
294 static inline int kernfs_remove_by_name_ns(struct kernfs_node
*kn
,
295 const char *name
, const void *ns
)
298 static inline int kernfs_rename_ns(struct kernfs_node
*kn
,
299 struct kernfs_node
*new_parent
,
300 const char *new_name
, const void *new_ns
)
303 static inline int kernfs_setattr(struct kernfs_node
*kn
,
304 const struct iattr
*iattr
)
307 static inline void kernfs_notify(struct kernfs_node
*kn
) { }
309 static inline const void *kernfs_super_ns(struct super_block
*sb
)
312 static inline struct dentry
*
313 kernfs_mount_ns(struct file_system_type
*fs_type
, int flags
,
314 struct kernfs_root
*root
, const void *ns
)
315 { return ERR_PTR(-ENOSYS
); }
317 static inline void kernfs_kill_sb(struct super_block
*sb
) { }
319 static inline void kernfs_init(void) { }
321 #endif /* CONFIG_SYSFS */
323 static inline struct kernfs_node
*
324 kernfs_find_and_get(struct kernfs_node
*kn
, const char *name
)
326 return kernfs_find_and_get_ns(kn
, name
, NULL
);
329 static inline struct kernfs_node
*
330 kernfs_create_dir(struct kernfs_node
*parent
, const char *name
, umode_t mode
,
333 return kernfs_create_dir_ns(parent
, name
, mode
, priv
, NULL
);
336 static inline struct kernfs_node
*
337 kernfs_create_file_ns(struct kernfs_node
*parent
, const char *name
,
338 umode_t mode
, loff_t size
, const struct kernfs_ops
*ops
,
339 void *priv
, const void *ns
)
341 struct lock_class_key
*key
= NULL
;
343 #ifdef CONFIG_DEBUG_LOCK_ALLOC
344 key
= (struct lock_class_key
*)&ops
->lockdep_key
;
346 return __kernfs_create_file(parent
, name
, mode
, size
, ops
, priv
, ns
,
350 static inline struct kernfs_node
*
351 kernfs_create_file(struct kernfs_node
*parent
, const char *name
, umode_t mode
,
352 loff_t size
, const struct kernfs_ops
*ops
, void *priv
)
354 return kernfs_create_file_ns(parent
, name
, mode
, size
, ops
, priv
, NULL
);
357 static inline int kernfs_remove_by_name(struct kernfs_node
*parent
,
360 return kernfs_remove_by_name_ns(parent
, name
, NULL
);
363 static inline struct dentry
*
364 kernfs_mount(struct file_system_type
*fs_type
, int flags
,
365 struct kernfs_root
*root
)
367 return kernfs_mount_ns(fs_type
, flags
, root
, NULL
);
370 #endif /* __LINUX_KERNFS_H */