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