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