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