Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
2 | * kernfs.h - pseudo filesystem decoupled from vfs locking | |
3 | * | |
4 | * This file is released under the GPLv2. | |
5 | */ | |
6 | ||
7 | #ifndef __LINUX_KERNFS_H | |
8 | #define __LINUX_KERNFS_H | |
9 | ||
879f40d1 | 10 | #include <linux/kernel.h> |
5d0e26bb | 11 | #include <linux/err.h> |
dd8a5b03 TH |
12 | #include <linux/list.h> |
13 | #include <linux/mutex.h> | |
bc755553 | 14 | #include <linux/idr.h> |
517e64f5 | 15 | #include <linux/lockdep.h> |
cf9e5a73 TH |
16 | #include <linux/rbtree.h> |
17 | #include <linux/atomic.h> | |
abd54f02 | 18 | #include <linux/wait.h> |
879f40d1 | 19 | |
5d60418e | 20 | struct file; |
917f56ca | 21 | struct dentry; |
5d60418e | 22 | struct iattr; |
dd8a5b03 TH |
23 | struct seq_file; |
24 | struct vm_area_struct; | |
4b93dc9b TH |
25 | struct super_block; |
26 | struct file_system_type; | |
5d60418e | 27 | |
c525aadd TH |
28 | struct kernfs_open_node; |
29 | struct kernfs_iattrs; | |
cf9e5a73 TH |
30 | |
31 | enum kernfs_node_type { | |
df23fc39 TH |
32 | KERNFS_DIR = 0x0001, |
33 | KERNFS_FILE = 0x0002, | |
34 | KERNFS_LINK = 0x0004, | |
cf9e5a73 TH |
35 | }; |
36 | ||
df23fc39 | 37 | #define KERNFS_TYPE_MASK 0x000f |
df23fc39 | 38 | #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK |
cf9e5a73 TH |
39 | |
40 | enum kernfs_node_flag { | |
d35258ef | 41 | KERNFS_ACTIVATED = 0x0010, |
df23fc39 TH |
42 | KERNFS_NS = 0x0020, |
43 | KERNFS_HAS_SEQ_SHOW = 0x0040, | |
44 | KERNFS_HAS_MMAP = 0x0080, | |
45 | KERNFS_LOCKDEP = 0x0100, | |
2063d608 | 46 | KERNFS_STATIC_NAME = 0x0200, |
6b0afc2a TH |
47 | KERNFS_SUICIDAL = 0x0400, |
48 | KERNFS_SUICIDED = 0x0800, | |
cf9e5a73 TH |
49 | }; |
50 | ||
d35258ef TH |
51 | /* @flags for kernfs_create_root() */ |
52 | enum kernfs_root_flag { | |
53 | KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001, | |
54 | }; | |
55 | ||
324a56e1 TH |
56 | /* type-specific structures for kernfs_node union members */ |
57 | struct kernfs_elem_dir { | |
cf9e5a73 | 58 | unsigned long subdirs; |
adc5e8b5 | 59 | /* children rbtree starts here and goes through kn->rb */ |
cf9e5a73 TH |
60 | struct rb_root children; |
61 | ||
62 | /* | |
63 | * The kernfs hierarchy this directory belongs to. This fits | |
324a56e1 | 64 | * better directly in kernfs_node but is here to save space. |
cf9e5a73 TH |
65 | */ |
66 | struct kernfs_root *root; | |
67 | }; | |
68 | ||
324a56e1 TH |
69 | struct kernfs_elem_symlink { |
70 | struct kernfs_node *target_kn; | |
cf9e5a73 TH |
71 | }; |
72 | ||
324a56e1 | 73 | struct kernfs_elem_attr { |
cf9e5a73 | 74 | const struct kernfs_ops *ops; |
c525aadd | 75 | struct kernfs_open_node *open; |
cf9e5a73 TH |
76 | loff_t size; |
77 | }; | |
78 | ||
79 | /* | |
324a56e1 TH |
80 | * kernfs_node - the building block of kernfs hierarchy. Each and every |
81 | * kernfs node is represented by single kernfs_node. Most fields are | |
cf9e5a73 TH |
82 | * private to kernfs and shouldn't be accessed directly by kernfs users. |
83 | * | |
324a56e1 TH |
84 | * As long as s_count reference is held, the kernfs_node itself is |
85 | * accessible. Dereferencing elem or any other outer entity requires | |
86 | * active reference. | |
cf9e5a73 | 87 | */ |
324a56e1 | 88 | struct kernfs_node { |
adc5e8b5 TH |
89 | atomic_t count; |
90 | atomic_t active; | |
cf9e5a73 TH |
91 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
92 | struct lockdep_map dep_map; | |
93 | #endif | |
94 | /* the following two fields are published */ | |
adc5e8b5 TH |
95 | struct kernfs_node *parent; |
96 | const char *name; | |
cf9e5a73 | 97 | |
adc5e8b5 | 98 | struct rb_node rb; |
cf9e5a73 | 99 | |
adc5e8b5 | 100 | const void *ns; /* namespace tag */ |
9b0925a6 | 101 | unsigned int hash; /* ns + name hash */ |
cf9e5a73 | 102 | union { |
adc5e8b5 TH |
103 | struct kernfs_elem_dir dir; |
104 | struct kernfs_elem_symlink symlink; | |
105 | struct kernfs_elem_attr attr; | |
cf9e5a73 TH |
106 | }; |
107 | ||
108 | void *priv; | |
109 | ||
adc5e8b5 TH |
110 | unsigned short flags; |
111 | umode_t mode; | |
112 | unsigned int ino; | |
c525aadd | 113 | struct kernfs_iattrs *iattr; |
cf9e5a73 | 114 | }; |
b8441ed2 | 115 | |
80b9bbef | 116 | /* |
90c07c89 TH |
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. | |
80b9bbef | 122 | */ |
90c07c89 | 123 | struct kernfs_syscall_ops { |
6a7fed4e TH |
124 | int (*remount_fs)(struct kernfs_root *root, int *flags, char *data); |
125 | int (*show_options)(struct seq_file *sf, struct kernfs_root *root); | |
126 | ||
80b9bbef TH |
127 | int (*mkdir)(struct kernfs_node *parent, const char *name, |
128 | umode_t mode); | |
129 | int (*rmdir)(struct kernfs_node *kn); | |
130 | int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent, | |
131 | const char *new_name); | |
132 | }; | |
133 | ||
ba7443bc TH |
134 | struct kernfs_root { |
135 | /* published fields */ | |
324a56e1 | 136 | struct kernfs_node *kn; |
d35258ef | 137 | unsigned int flags; /* KERNFS_ROOT_* flags */ |
bc755553 TH |
138 | |
139 | /* private fields, do not use outside kernfs proper */ | |
140 | struct ida ino_ida; | |
90c07c89 | 141 | struct kernfs_syscall_ops *syscall_ops; |
abd54f02 | 142 | wait_queue_head_t deactivate_waitq; |
ba7443bc TH |
143 | }; |
144 | ||
c525aadd | 145 | struct kernfs_open_file { |
dd8a5b03 | 146 | /* published fields */ |
324a56e1 | 147 | struct kernfs_node *kn; |
dd8a5b03 TH |
148 | struct file *file; |
149 | ||
150 | /* private fields, do not use outside kernfs proper */ | |
151 | struct mutex mutex; | |
152 | int event; | |
153 | struct list_head list; | |
154 | ||
155 | bool mmapped; | |
156 | const struct vm_operations_struct *vm_ops; | |
157 | }; | |
158 | ||
f6acf8bb TH |
159 | struct kernfs_ops { |
160 | /* | |
161 | * Read is handled by either seq_file or raw_read(). | |
162 | * | |
d19b9846 TH |
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 | |
c525aadd | 166 | * associated kernfs_open_file. |
f6acf8bb TH |
167 | * |
168 | * read() is bounced through kernel buffer and a read larger than | |
169 | * PAGE_SIZE results in partial operation of PAGE_SIZE. | |
170 | */ | |
171 | int (*seq_show)(struct seq_file *sf, void *v); | |
d19b9846 TH |
172 | |
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); | |
f6acf8bb | 176 | |
c525aadd | 177 | ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes, |
f6acf8bb TH |
178 | loff_t off); |
179 | ||
180 | /* | |
4d3773c4 TH |
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. | |
f6acf8bb | 186 | */ |
4d3773c4 | 187 | size_t atomic_write_len; |
c525aadd | 188 | ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes, |
f6acf8bb TH |
189 | loff_t off); |
190 | ||
c525aadd | 191 | int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma); |
517e64f5 TH |
192 | |
193 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
194 | struct lock_class_key lockdep_key; | |
195 | #endif | |
f6acf8bb TH |
196 | }; |
197 | ||
879f40d1 TH |
198 | #ifdef CONFIG_SYSFS |
199 | ||
df23fc39 | 200 | static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) |
cf9e5a73 | 201 | { |
df23fc39 | 202 | return kn->flags & KERNFS_TYPE_MASK; |
cf9e5a73 TH |
203 | } |
204 | ||
205 | /** | |
206 | * kernfs_enable_ns - enable namespace under a directory | |
324a56e1 | 207 | * @kn: directory of interest, should be empty |
cf9e5a73 | 208 | * |
324a56e1 TH |
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 | |
cf9e5a73 TH |
211 | * only the ones which match the super_block's tag will be visible. |
212 | */ | |
324a56e1 | 213 | static inline void kernfs_enable_ns(struct kernfs_node *kn) |
cf9e5a73 | 214 | { |
df23fc39 | 215 | WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); |
adc5e8b5 | 216 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children)); |
df23fc39 | 217 | kn->flags |= KERNFS_NS; |
cf9e5a73 TH |
218 | } |
219 | ||
ac9bba03 TH |
220 | /** |
221 | * kernfs_ns_enabled - test whether namespace is enabled | |
324a56e1 | 222 | * @kn: the node to test |
ac9bba03 TH |
223 | * |
224 | * Test whether namespace filtering is enabled for the children of @ns. | |
225 | */ | |
324a56e1 | 226 | static inline bool kernfs_ns_enabled(struct kernfs_node *kn) |
ac9bba03 | 227 | { |
df23fc39 | 228 | return kn->flags & KERNFS_NS; |
ac9bba03 TH |
229 | } |
230 | ||
324a56e1 TH |
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); | |
ccf73cf3 | 235 | |
90c07c89 | 236 | struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, |
d35258ef | 237 | unsigned int flags, void *priv); |
ba7443bc TH |
238 | void kernfs_destroy_root(struct kernfs_root *root); |
239 | ||
324a56e1 | 240 | struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, |
bb8b9d09 TH |
241 | const char *name, umode_t mode, |
242 | void *priv, const void *ns); | |
2063d608 TH |
243 | struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent, |
244 | const char *name, | |
245 | umode_t mode, loff_t size, | |
246 | const struct kernfs_ops *ops, | |
247 | void *priv, const void *ns, | |
248 | bool name_is_static, | |
249 | struct lock_class_key *key); | |
324a56e1 TH |
250 | struct kernfs_node *kernfs_create_link(struct kernfs_node *parent, |
251 | const char *name, | |
252 | struct kernfs_node *target); | |
d35258ef | 253 | void kernfs_activate(struct kernfs_node *kn); |
324a56e1 | 254 | void kernfs_remove(struct kernfs_node *kn); |
6b0afc2a TH |
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); | |
324a56e1 | 258 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, |
879f40d1 | 259 | const void *ns); |
324a56e1 | 260 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, |
890ece16 | 261 | const char *new_name, const void *new_ns); |
324a56e1 TH |
262 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr); |
263 | void kernfs_notify(struct kernfs_node *kn); | |
879f40d1 | 264 | |
4b93dc9b TH |
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); | |
269 | ||
270 | void kernfs_init(void); | |
271 | ||
879f40d1 TH |
272 | #else /* CONFIG_SYSFS */ |
273 | ||
df23fc39 | 274 | static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn) |
cf9e5a73 TH |
275 | { return 0; } /* whatever */ |
276 | ||
324a56e1 | 277 | static inline void kernfs_enable_ns(struct kernfs_node *kn) { } |
cf9e5a73 | 278 | |
324a56e1 | 279 | static inline bool kernfs_ns_enabled(struct kernfs_node *kn) |
ac9bba03 TH |
280 | { return false; } |
281 | ||
324a56e1 TH |
282 | static inline struct kernfs_node * |
283 | kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name, | |
ccf73cf3 TH |
284 | const void *ns) |
285 | { return NULL; } | |
286 | ||
324a56e1 TH |
287 | static inline void kernfs_get(struct kernfs_node *kn) { } |
288 | static inline void kernfs_put(struct kernfs_node *kn) { } | |
ccf73cf3 | 289 | |
80b9bbef | 290 | static inline struct kernfs_root * |
d35258ef TH |
291 | kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags, |
292 | void *priv) | |
ba7443bc TH |
293 | { return ERR_PTR(-ENOSYS); } |
294 | ||
295 | static inline void kernfs_destroy_root(struct kernfs_root *root) { } | |
296 | ||
324a56e1 | 297 | static inline struct kernfs_node * |
bb8b9d09 TH |
298 | kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, |
299 | umode_t mode, void *priv, const void *ns) | |
93b2b8e4 TH |
300 | { return ERR_PTR(-ENOSYS); } |
301 | ||
324a56e1 | 302 | static inline struct kernfs_node * |
2063d608 TH |
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) | |
496f7394 TH |
307 | { return ERR_PTR(-ENOSYS); } |
308 | ||
324a56e1 TH |
309 | static inline struct kernfs_node * |
310 | kernfs_create_link(struct kernfs_node *parent, const char *name, | |
311 | struct kernfs_node *target) | |
5d0e26bb TH |
312 | { return ERR_PTR(-ENOSYS); } |
313 | ||
d35258ef TH |
314 | static inline void kernfs_activate(struct kernfs_node *kn) { } |
315 | ||
324a56e1 | 316 | static inline void kernfs_remove(struct kernfs_node *kn) { } |
879f40d1 | 317 | |
6b0afc2a TH |
318 | static inline bool kernfs_remove_self(struct kernfs_node *kn) |
319 | { return false; } | |
320 | ||
324a56e1 | 321 | static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn, |
879f40d1 TH |
322 | const char *name, const void *ns) |
323 | { return -ENOSYS; } | |
324 | ||
324a56e1 TH |
325 | static inline int kernfs_rename_ns(struct kernfs_node *kn, |
326 | struct kernfs_node *new_parent, | |
890ece16 TH |
327 | const char *new_name, const void *new_ns) |
328 | { return -ENOSYS; } | |
329 | ||
324a56e1 | 330 | static inline int kernfs_setattr(struct kernfs_node *kn, |
5d60418e TH |
331 | const struct iattr *iattr) |
332 | { return -ENOSYS; } | |
333 | ||
324a56e1 | 334 | static inline void kernfs_notify(struct kernfs_node *kn) { } |
024f6471 | 335 | |
4b93dc9b TH |
336 | static inline const void *kernfs_super_ns(struct super_block *sb) |
337 | { return NULL; } | |
338 | ||
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); } | |
343 | ||
344 | static inline void kernfs_kill_sb(struct super_block *sb) { } | |
345 | ||
346 | static inline void kernfs_init(void) { } | |
347 | ||
879f40d1 TH |
348 | #endif /* CONFIG_SYSFS */ |
349 | ||
324a56e1 TH |
350 | static inline struct kernfs_node * |
351 | kernfs_find_and_get(struct kernfs_node *kn, const char *name) | |
ccf73cf3 | 352 | { |
324a56e1 | 353 | return kernfs_find_and_get_ns(kn, name, NULL); |
ccf73cf3 TH |
354 | } |
355 | ||
324a56e1 | 356 | static inline struct kernfs_node * |
bb8b9d09 TH |
357 | kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode, |
358 | void *priv) | |
93b2b8e4 | 359 | { |
bb8b9d09 | 360 | return kernfs_create_dir_ns(parent, name, mode, priv, NULL); |
93b2b8e4 TH |
361 | } |
362 | ||
324a56e1 TH |
363 | static inline struct kernfs_node * |
364 | kernfs_create_file_ns(struct kernfs_node *parent, const char *name, | |
517e64f5 TH |
365 | umode_t mode, loff_t size, const struct kernfs_ops *ops, |
366 | void *priv, const void *ns) | |
367 | { | |
368 | struct lock_class_key *key = NULL; | |
369 | ||
370 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
371 | key = (struct lock_class_key *)&ops->lockdep_key; | |
372 | #endif | |
2063d608 TH |
373 | return __kernfs_create_file(parent, name, mode, size, ops, priv, ns, |
374 | false, key); | |
517e64f5 TH |
375 | } |
376 | ||
324a56e1 TH |
377 | static inline struct kernfs_node * |
378 | kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode, | |
496f7394 TH |
379 | loff_t size, const struct kernfs_ops *ops, void *priv) |
380 | { | |
381 | return kernfs_create_file_ns(parent, name, mode, size, ops, priv, NULL); | |
382 | } | |
383 | ||
324a56e1 | 384 | static inline int kernfs_remove_by_name(struct kernfs_node *parent, |
879f40d1 TH |
385 | const char *name) |
386 | { | |
387 | return kernfs_remove_by_name_ns(parent, name, NULL); | |
388 | } | |
389 | ||
4b93dc9b TH |
390 | static inline struct dentry * |
391 | kernfs_mount(struct file_system_type *fs_type, int flags, | |
392 | struct kernfs_root *root) | |
393 | { | |
394 | return kernfs_mount_ns(fs_type, flags, root, NULL); | |
395 | } | |
396 | ||
b8441ed2 | 397 | #endif /* __LINUX_KERNFS_H */ |