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
20struct file;
21struct dentry;
22struct iattr;
23struct seq_file;
24struct vm_area_struct;
25struct super_block;
26struct file_system_type;
27struct poll_table_struct;
28struct fs_context;
29
30struct kernfs_fs_context;
31struct kernfs_open_node;
32struct kernfs_iattrs;
33
34enum 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
43enum 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
56enum 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
84struct 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
96struct kernfs_elem_symlink {
97 struct kernfs_node *target_kn;
98};
99
100struct 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
108union 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
131struct 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
171struct 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
183struct kernfs_root {
184
185 struct kernfs_node *kn;
186 unsigned int flags;
187
188
189 struct idr ino_idr;
190 u32 next_generation;
191 struct kernfs_syscall_ops *syscall_ops;
192
193
194 struct list_head supers;
195
196 wait_queue_head_t deactivate_waitq;
197};
198
199struct kernfs_open_file {
200
201 struct kernfs_node *kn;
202 struct file *file;
203 struct seq_file *seq_file;
204 void *priv;
205
206
207 struct mutex mutex;
208 struct mutex prealloc_mutex;
209 int event;
210 struct list_head list;
211 char *prealloc_buf;
212
213 size_t atomic_write_len;
214 bool mmapped:1;
215 bool released:1;
216 const struct vm_operations_struct *vm_ops;
217};
218
219struct kernfs_ops {
220
221
222
223
224 int (*open)(struct kernfs_open_file *of);
225 void (*release)(struct kernfs_open_file *of);
226
227
228
229
230
231
232
233
234
235
236
237
238 int (*seq_show)(struct seq_file *sf, void *v);
239
240 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
241 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
242 void (*seq_stop)(struct seq_file *sf, void *v);
243
244 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
245 loff_t off);
246
247
248
249
250
251
252
253
254 size_t atomic_write_len;
255
256
257
258
259
260
261 bool prealloc;
262 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
263 loff_t off);
264
265 __poll_t (*poll)(struct kernfs_open_file *of,
266 struct poll_table_struct *pt);
267
268 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
269
270#ifdef CONFIG_DEBUG_LOCK_ALLOC
271 struct lock_class_key lockdep_key;
272#endif
273};
274
275
276
277
278struct kernfs_fs_context {
279 struct kernfs_root *root;
280 void *ns_tag;
281 unsigned long magic;
282
283
284 bool new_sb_created;
285};
286
287#ifdef CONFIG_KERNFS
288
289static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
290{
291 return kn->flags & KERNFS_TYPE_MASK;
292}
293
294
295
296
297
298
299
300
301
302static inline void kernfs_enable_ns(struct kernfs_node *kn)
303{
304 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
305 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
306 kn->flags |= KERNFS_NS;
307}
308
309
310
311
312
313
314
315static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
316{
317 return kn->flags & KERNFS_NS;
318}
319
320int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
321int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
322 char *buf, size_t buflen);
323void pr_cont_kernfs_name(struct kernfs_node *kn);
324void pr_cont_kernfs_path(struct kernfs_node *kn);
325struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
326struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
327 const char *name, const void *ns);
328struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
329 const char *path, const void *ns);
330void kernfs_get(struct kernfs_node *kn);
331void kernfs_put(struct kernfs_node *kn);
332
333struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
334struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
335struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
336
337struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
338 struct super_block *sb);
339struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
340 unsigned int flags, void *priv);
341void kernfs_destroy_root(struct kernfs_root *root);
342
343struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
344 const char *name, umode_t mode,
345 kuid_t uid, kgid_t gid,
346 void *priv, const void *ns);
347struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
348 const char *name);
349struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
350 const char *name, umode_t mode,
351 kuid_t uid, kgid_t gid,
352 loff_t size,
353 const struct kernfs_ops *ops,
354 void *priv, const void *ns,
355 struct lock_class_key *key);
356struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
357 const char *name,
358 struct kernfs_node *target);
359void kernfs_activate(struct kernfs_node *kn);
360void kernfs_remove(struct kernfs_node *kn);
361void kernfs_break_active_protection(struct kernfs_node *kn);
362void kernfs_unbreak_active_protection(struct kernfs_node *kn);
363bool kernfs_remove_self(struct kernfs_node *kn);
364int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
365 const void *ns);
366int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
367 const char *new_name, const void *new_ns);
368int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
369__poll_t kernfs_generic_poll(struct kernfs_open_file *of,
370 struct poll_table_struct *pt);
371void kernfs_notify(struct kernfs_node *kn);
372
373int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
374 void *value, size_t size);
375int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
376 const void *value, size_t size, int flags);
377
378const void *kernfs_super_ns(struct super_block *sb);
379int kernfs_get_tree(struct fs_context *fc);
380void kernfs_free_fs_context(struct fs_context *fc);
381void kernfs_kill_sb(struct super_block *sb);
382
383void kernfs_init(void);
384
385struct kernfs_node *kernfs_get_node_by_id(struct kernfs_root *root,
386 const union kernfs_node_id *id);
387#else
388
389static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
390{ return 0; }
391
392static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
393
394static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
395{ return false; }
396
397static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
398{ return -ENOSYS; }
399
400static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
401 struct kernfs_node *kn,
402 char *buf, size_t buflen)
403{ return -ENOSYS; }
404
405static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
406static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
407
408static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
409{ return NULL; }
410
411static inline struct kernfs_node *
412kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
413 const void *ns)
414{ return NULL; }
415static inline struct kernfs_node *
416kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
417 const void *ns)
418{ return NULL; }
419
420static inline void kernfs_get(struct kernfs_node *kn) { }
421static inline void kernfs_put(struct kernfs_node *kn) { }
422
423static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
424{ return NULL; }
425
426static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
427{ return NULL; }
428
429static inline struct inode *
430kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
431{ return NULL; }
432
433static inline struct kernfs_root *
434kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
435 void *priv)
436{ return ERR_PTR(-ENOSYS); }
437
438static inline void kernfs_destroy_root(struct kernfs_root *root) { }
439
440static inline struct kernfs_node *
441kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
442 umode_t mode, kuid_t uid, kgid_t gid,
443 void *priv, const void *ns)
444{ return ERR_PTR(-ENOSYS); }
445
446static inline struct kernfs_node *
447__kernfs_create_file(struct kernfs_node *parent, const char *name,
448 umode_t mode, kuid_t uid, kgid_t gid,
449 loff_t size, const struct kernfs_ops *ops,
450 void *priv, const void *ns, struct lock_class_key *key)
451{ return ERR_PTR(-ENOSYS); }
452
453static inline struct kernfs_node *
454kernfs_create_link(struct kernfs_node *parent, const char *name,
455 struct kernfs_node *target)
456{ return ERR_PTR(-ENOSYS); }
457
458static inline void kernfs_activate(struct kernfs_node *kn) { }
459
460static inline void kernfs_remove(struct kernfs_node *kn) { }
461
462static inline bool kernfs_remove_self(struct kernfs_node *kn)
463{ return false; }
464
465static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
466 const char *name, const void *ns)
467{ return -ENOSYS; }
468
469static inline int kernfs_rename_ns(struct kernfs_node *kn,
470 struct kernfs_node *new_parent,
471 const char *new_name, const void *new_ns)
472{ return -ENOSYS; }
473
474static inline int kernfs_setattr(struct kernfs_node *kn,
475 const struct iattr *iattr)
476{ return -ENOSYS; }
477
478static inline void kernfs_notify(struct kernfs_node *kn) { }
479
480static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
481 void *value, size_t size)
482{ return -ENOSYS; }
483
484static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
485 const void *value, size_t size, int flags)
486{ return -ENOSYS; }
487
488static inline const void *kernfs_super_ns(struct super_block *sb)
489{ return NULL; }
490
491static inline int kernfs_get_tree(struct fs_context *fc)
492{ return -ENOSYS; }
493
494static inline void kernfs_free_fs_context(struct fs_context *fc) { }
495
496static inline void kernfs_kill_sb(struct super_block *sb) { }
497
498static inline void kernfs_init(void) { }
499
500#endif
501
502
503
504
505
506
507
508
509
510
511
512
513
514static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
515{
516 return kernfs_path_from_node(kn, NULL, buf, buflen);
517}
518
519static inline struct kernfs_node *
520kernfs_find_and_get(struct kernfs_node *kn, const char *name)
521{
522 return kernfs_find_and_get_ns(kn, name, NULL);
523}
524
525static inline struct kernfs_node *
526kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
527{
528 return kernfs_walk_and_get_ns(kn, path, NULL);
529}
530
531static inline struct kernfs_node *
532kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
533 void *priv)
534{
535 return kernfs_create_dir_ns(parent, name, mode,
536 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
537 priv, NULL);
538}
539
540static inline struct kernfs_node *
541kernfs_create_file_ns(struct kernfs_node *parent, const char *name,
542 umode_t mode, kuid_t uid, kgid_t gid,
543 loff_t size, const struct kernfs_ops *ops,
544 void *priv, const void *ns)
545{
546 struct lock_class_key *key = NULL;
547
548#ifdef CONFIG_DEBUG_LOCK_ALLOC
549 key = (struct lock_class_key *)&ops->lockdep_key;
550#endif
551 return __kernfs_create_file(parent, name, mode, uid, gid,
552 size, ops, priv, ns, key);
553}
554
555static inline struct kernfs_node *
556kernfs_create_file(struct kernfs_node *parent, const char *name, umode_t mode,
557 loff_t size, const struct kernfs_ops *ops, void *priv)
558{
559 return kernfs_create_file_ns(parent, name, mode,
560 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
561 size, ops, priv, NULL);
562}
563
564static inline int kernfs_remove_by_name(struct kernfs_node *parent,
565 const char *name)
566{
567 return kernfs_remove_by_name_ns(parent, name, NULL);
568}
569
570static inline int kernfs_rename(struct kernfs_node *kn,
571 struct kernfs_node *new_parent,
572 const char *new_name)
573{
574 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
575}
576
577#endif
578