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