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