This source file includes following definitions.
- kernfs_type
- kernfs_enable_ns
- kernfs_ns_enabled
- kernfs_type
- kernfs_enable_ns
- kernfs_ns_enabled
- kernfs_name
- kernfs_path_from_node
- pr_cont_kernfs_name
- pr_cont_kernfs_path
- kernfs_get_parent
- kernfs_find_and_get_ns
- kernfs_walk_and_get_ns
- kernfs_get
- kernfs_put
- kernfs_node_from_dentry
- kernfs_root_from_sb
- kernfs_get_inode
- kernfs_create_root
- kernfs_destroy_root
- kernfs_create_dir_ns
- __kernfs_create_file
- kernfs_create_link
- kernfs_activate
- kernfs_remove
- kernfs_remove_self
- kernfs_remove_by_name_ns
- kernfs_rename_ns
- kernfs_setattr
- kernfs_notify
- kernfs_xattr_get
- kernfs_xattr_set
- kernfs_super_ns
- kernfs_get_tree
- kernfs_free_fs_context
- kernfs_kill_sb
- kernfs_init
- kernfs_path
- kernfs_find_and_get
- kernfs_walk_and_get
- kernfs_create_dir
- kernfs_create_file_ns
- kernfs_create_file
- kernfs_remove_by_name
- kernfs_rename
1
2
3
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
20 struct file;
21 struct dentry;
22 struct iattr;
23 struct seq_file;
24 struct vm_area_struct;
25 struct super_block;
26 struct file_system_type;
27 struct poll_table_struct;
28 struct fs_context;
29
30 struct kernfs_fs_context;
31 struct kernfs_open_node;
32 struct kernfs_iattrs;
33
34 enum kernfs_node_type {
35 KERNFS_DIR = 0x0001,
36 KERNFS_FILE = 0x0002,
37 KERNFS_LINK = 0x0004,
38 };
39
40 #define KERNFS_TYPE_MASK 0x000f
41 #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
42
43 enum kernfs_node_flag {
44 KERNFS_ACTIVATED = 0x0010,
45 KERNFS_NS = 0x0020,
46 KERNFS_HAS_SEQ_SHOW = 0x0040,
47 KERNFS_HAS_MMAP = 0x0080,
48 KERNFS_LOCKDEP = 0x0100,
49 KERNFS_SUICIDAL = 0x0400,
50 KERNFS_SUICIDED = 0x0800,
51 KERNFS_EMPTY_DIR = 0x1000,
52 KERNFS_HAS_RELEASE = 0x2000,
53 };
54
55
56 enum kernfs_root_flag {
57
58
59
60
61
62
63 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
64
65
66
67
68
69
70
71
72
73
74 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
75
76
77
78
79
80 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
81 };
82
83
84 struct kernfs_elem_dir {
85 unsigned long subdirs;
86
87 struct rb_root children;
88
89
90
91
92
93 struct kernfs_root *root;
94 };
95
96 struct kernfs_elem_symlink {
97 struct kernfs_node *target_kn;
98 };
99
100 struct kernfs_elem_attr {
101 const struct kernfs_ops *ops;
102 struct kernfs_open_node *open;
103 loff_t size;
104 struct kernfs_node *notify_next;
105 };
106
107
108 union kernfs_node_id {
109 struct {
110
111
112
113
114
115
116 u32 ino;
117 u32 generation;
118 };
119 u64 id;
120 };
121
122
123
124
125
126
127
128
129
130
131 struct kernfs_node {
132 atomic_t count;
133 atomic_t active;
134 #ifdef CONFIG_DEBUG_LOCK_ALLOC
135 struct lockdep_map dep_map;
136 #endif
137
138
139
140
141
142
143 struct kernfs_node *parent;
144 const char *name;
145
146 struct rb_node rb;
147
148 const void *ns;
149 unsigned int hash;
150 union {
151 struct kernfs_elem_dir dir;
152 struct kernfs_elem_symlink symlink;
153 struct kernfs_elem_attr attr;
154 };
155
156 void *priv;
157
158 union kernfs_node_id id;
159 unsigned short flags;
160 umode_t mode;
161 struct kernfs_iattrs *iattr;
162 };
163
164
165
166
167
168
169
170
171 struct kernfs_syscall_ops {
172 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
173
174 int (*mkdir)(struct kernfs_node *parent, const char *name,
175 umode_t mode);
176 int (*rmdir)(struct kernfs_node *kn);
177 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
178 const char *new_name);
179 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
180 struct kernfs_root *root);
181 };
182
183 struct kernfs_root {
184
185 struct kernfs_node *kn;
186 unsigned int flags;
187
188
189 struct idr ino_idr;
190 u32 last_ino;
191 u32 next_generation;
192 struct kernfs_syscall_ops *syscall_ops;
193
194
195 struct list_head supers;
196
197 wait_queue_head_t deactivate_waitq;
198 };
199
200 struct kernfs_open_file {
201
202 struct kernfs_node *kn;
203 struct file *file;
204 struct seq_file *seq_file;
205 void *priv;
206
207
208 struct mutex mutex;
209 struct mutex prealloc_mutex;
210 int event;
211 struct list_head list;
212 char *prealloc_buf;
213
214 size_t atomic_write_len;
215 bool mmapped:1;
216 bool released:1;
217 const struct vm_operations_struct *vm_ops;
218 };
219
220 struct kernfs_ops {
221
222
223
224
225 int (*open)(struct kernfs_open_file *of);
226 void (*release)(struct kernfs_open_file *of);
227
228
229
230
231
232
233
234
235
236
237
238
239 int (*seq_show)(struct seq_file *sf, void *v);
240
241 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
242 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
243 void (*seq_stop)(struct seq_file *sf, void *v);
244
245 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
246 loff_t off);
247
248
249
250
251
252
253
254
255 size_t atomic_write_len;
256
257
258
259
260
261
262 bool prealloc;
263 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
264 loff_t off);
265
266 __poll_t (*poll)(struct kernfs_open_file *of,
267 struct poll_table_struct *pt);
268
269 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
270
271 #ifdef CONFIG_DEBUG_LOCK_ALLOC
272 struct lock_class_key lockdep_key;
273 #endif
274 };
275
276
277
278
279 struct kernfs_fs_context {
280 struct kernfs_root *root;
281 void *ns_tag;
282 unsigned long magic;
283
284
285 bool new_sb_created;
286 };
287
288 #ifdef CONFIG_KERNFS
289
290 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
291 {
292 return kn->flags & KERNFS_TYPE_MASK;
293 }
294
295
296
297
298
299
300
301
302
303 static inline void kernfs_enable_ns(struct kernfs_node *kn)
304 {
305 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
306 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
307 kn->flags |= KERNFS_NS;
308 }
309
310
311
312
313
314
315
316 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
317 {
318 return kn->flags & KERNFS_NS;
319 }
320
321 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
322 int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
323 char *buf, size_t buflen);
324 void pr_cont_kernfs_name(struct kernfs_node *kn);
325 void pr_cont_kernfs_path(struct kernfs_node *kn);
326 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
327 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
328 const char *name, const void *ns);
329 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
330 const char *path, const void *ns);
331 void kernfs_get(struct kernfs_node *kn);
332 void kernfs_put(struct kernfs_node *kn);
333
334 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
335 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
336 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
337
338 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
339 struct super_block *sb);
340 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
341 unsigned int flags, void *priv);
342 void kernfs_destroy_root(struct kernfs_root *root);
343
344 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
345 const char *name, umode_t mode,
346 kuid_t uid, kgid_t gid,
347 void *priv, const void *ns);
348 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
349 const char *name);
350 struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
351 const char *name, umode_t mode,
352 kuid_t uid, kgid_t gid,
353 loff_t size,
354 const struct kernfs_ops *ops,
355 void *priv, const void *ns,
356 struct lock_class_key *key);
357 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
358 const char *name,
359 struct kernfs_node *target);
360 void kernfs_activate(struct kernfs_node *kn);
361 void kernfs_remove(struct kernfs_node *kn);
362 void kernfs_break_active_protection(struct kernfs_node *kn);
363 void kernfs_unbreak_active_protection(struct kernfs_node *kn);
364 bool kernfs_remove_self(struct kernfs_node *kn);
365 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
366 const void *ns);
367 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
368 const char *new_name, const void *new_ns);
369 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
370 __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
371 struct poll_table_struct *pt);
372 void kernfs_notify(struct kernfs_node *kn);
373
374 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
375 void *value, size_t size);
376 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
377 const void *value, size_t size, int flags);
378
379 const void *kernfs_super_ns(struct super_block *sb);
380 int kernfs_get_tree(struct fs_context *fc);
381 void kernfs_free_fs_context(struct fs_context *fc);
382 void kernfs_kill_sb(struct super_block *sb);
383
384 void kernfs_init(void);
385
386 struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
387 const union kernfs_node_id *id);
388 #else
389
390 static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
391 { return 0; }
392
393 static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
394
395 static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
396 { return false; }
397
398 static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
399 { return -ENOSYS; }
400
401 static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
402 struct kernfs_node *kn,
403 char *buf, size_t buflen)
404 { return -ENOSYS; }
405
406 static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
407 static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
408
409 static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
410 { return NULL; }
411
412 static inline struct kernfs_node *
413 kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
414 const void *ns)
415 { return NULL; }
416 static inline struct kernfs_node *
417 kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
418 const void *ns)
419 { return NULL; }
420
421 static inline void kernfs_get(struct kernfs_node *kn) { }
422 static inline void kernfs_put(struct kernfs_node *kn) { }
423
424 static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
425 { return NULL; }
426
427 static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
428 { return NULL; }
429
430 static inline struct inode *
431 kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
432 { return NULL; }
433
434 static inline struct kernfs_root *
435 kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
436 void *priv)
437 { return ERR_PTR(-ENOSYS); }
438
439 static inline void kernfs_destroy_root(struct kernfs_root *root) { }
440
441 static inline struct kernfs_node *
442 kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
443 umode_t mode, kuid_t uid, kgid_t gid,
444 void *priv, const void *ns)
445 { return ERR_PTR(-ENOSYS); }
446
447 static inline struct kernfs_node *
448 __kernfs_create_file(struct kernfs_node *parent, const char *name,
449 umode_t mode, kuid_t uid, kgid_t gid,
450 loff_t size, const struct kernfs_ops *ops,
451 void *priv, const void *ns, struct lock_class_key *key)
452 { return ERR_PTR(-ENOSYS); }
453
454 static inline struct kernfs_node *
455 kernfs_create_link(struct kernfs_node *parent, const char *name,
456 struct kernfs_node *target)
457 { return ERR_PTR(-ENOSYS); }
458
459 static inline void kernfs_activate(struct kernfs_node *kn) { }
460
461 static inline void kernfs_remove(struct kernfs_node *kn) { }
462
463 static inline bool kernfs_remove_self(struct kernfs_node *kn)
464 { return false; }
465
466 static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
467 const char *name, const void *ns)
468 { return -ENOSYS; }
469
470 static inline int kernfs_rename_ns(struct kernfs_node *kn,
471 struct kernfs_node *new_parent,
472 const char *new_name, const void *new_ns)
473 { return -ENOSYS; }
474
475 static inline int kernfs_setattr(struct kernfs_node *kn,
476 const struct iattr *iattr)
477 { return -ENOSYS; }
478
479 static inline void kernfs_notify(struct kernfs_node *kn) { }
480
481 static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
482 void *value, size_t size)
483 { return -ENOSYS; }
484
485 static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
486 const void *value, size_t size, int flags)
487 { return -ENOSYS; }
488
489 static inline const void *kernfs_super_ns(struct super_block *sb)
490 { return NULL; }
491
492 static inline int kernfs_get_tree(struct fs_context *fc)
493 { return -ENOSYS; }
494
495 static inline void kernfs_free_fs_context(struct fs_context *fc) { }
496
497 static inline void kernfs_kill_sb(struct super_block *sb) { }
498
499 static inline void kernfs_init(void) { }
500
501 #endif
502
503
504
505
506
507
508
509
510
511
512
513
514
515 static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
516 {
517 return kernfs_path_from_node(kn, NULL, buf, buflen);
518 }
519
520 static inline struct kernfs_node *
521 kernfs_find_and_get(struct kernfs_node *kn, const char *name)
522 {
523 return kernfs_find_and_get_ns(kn, name, NULL);
524 }
525
526 static inline struct kernfs_node *
527 kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
528 {
529 return kernfs_walk_and_get_ns(kn, path, NULL);
530 }
531
532 static inline struct kernfs_node *
533 kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
534 void *priv)
535 {
536 return kernfs_create_dir_ns(parent, name, mode,
537 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
538 priv, NULL);
539 }
540
541 static inline struct kernfs_node *
542 kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
543 umode_t mode, kuid_t uid, kgid_t gid,
544 loff_t size, const struct kernfs_ops *ops,
545 void *priv, const void *ns)
546 {
547 struct lock_class_key *key = NULL;
548
549 #ifdef CONFIG_DEBUG_LOCK_ALLOC
550 key = (struct lock_class_key *)&ops->lockdep_key;
551 #endif
552 return __kernfs_create_file(parent, name, mode, uid, gid,
553 size, ops, priv, ns, key);
554 }
555
556 static inline struct kernfs_node *
557 kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
558 loff_t size, const struct kernfs_ops *ops, void *priv)
559 {
560 return kernfs_create_file_ns(parent, name, mode,
561 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
562 size, ops, priv, NULL);
563 }
564
565 static inline int kernfs_remove_by_name(struct kernfs_node *parent,
566 const char *name)
567 {
568 return kernfs_remove_by_name_ns(parent, name, NULL);
569 }
570
571 static inline int kernfs_rename(struct kernfs_node *kn,
572 struct kernfs_node *new_parent,
573 const char *new_name)
574 {
575 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
576 }
577
578 #endif