projects/linux·blob assoc-array-fixes · refs

654 lines · 20.2 KiB

1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * kernfs.h - pseudo filesystem decoupled from vfs locking
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#include <linux/cache.h>
22
23struct file;
24struct dentry;
25struct iattr;
26struct ns_common;
27struct seq_file;
28struct vm_area_struct;
29struct vm_operations_struct;
30struct super_block;
31struct file_system_type;
32struct poll_table_struct;
33struct fs_context;
34
35struct kernfs_fs_context;
36struct kernfs_open_node;
37struct kernfs_iattrs;
38
39/*
40 * NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash
41 * table of locks.
42 * Having a small hash table would impact scalability, since
43 * more and more kernfs_node objects will end up using same lock
44 * and having a very large hash table would waste memory.
45 *
46 * At the moment size of hash table of locks is being set based on
47 * the number of CPUs as follows:
48 *
49 * NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS
50 * 1 1 2
51 * 2-3 2 4
52 * 4-7 4 16
53 * 8-15 6 64
54 * 16-31 8 256
55 * 32 and more 10 1024
56 *
57 * The above relation between NR_CPU and number of locks is based
58 * on some internal experimentation which involved booting qemu
59 * with different values of smp, performing some sysfs operations
60 * on all CPUs and observing how increase in number of locks impacts
61 * completion time of these sysfs operations on each CPU.
62 */
63#ifdef CONFIG_SMP
64#define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32)))
65#else
66#define NR_KERNFS_LOCK_BITS 1
67#endif
68
69#define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS)
70
71/*
72 * There's one kernfs_open_file for each open file and one kernfs_open_node
73 * for each kernfs_node with one or more open files.
74 *
75 * filp->private_data points to seq_file whose ->private points to
76 * kernfs_open_file.
77 *
78 * kernfs_open_files are chained at kernfs_open_node->files, which is
79 * protected by kernfs_global_locks.node_mutex[i].
80 *
81 * To reduce possible contention in sysfs access, arising due to single
82 * locks, use an array of locks (e.g. node_mutex) and use kernfs_node
83 * object address as hash keys to get the index of these locks.
84 *
85 * Hashed mutexes are safe to use here because operations using these don't
86 * rely on global exclusion.
87 *
88 * The hashed mutex array protects per-node data: the kernfs_open_node for
89 * open file management, and kernfs_node xattr operations (necessary because
90 * multiple superblocks with different namespaces can share the same
91 * kernfs_node, making per-inode locking insufficient).
92 *
93 * In future we intend to replace other global locks with hashed ones as well.
94 * kernfs_global_locks acts as a holder for all such hash tables.
95 */
96struct kernfs_global_locks {
97 struct mutex node_mutex[NR_KERNFS_LOCKS];
98};
99
100enum kernfs_node_type {
101 KERNFS_DIR = 0x0001,
102 KERNFS_FILE = 0x0002,
103 KERNFS_LINK = 0x0004,
104};
105
106#define KERNFS_TYPE_MASK 0x000f
107#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
108
109enum kernfs_node_flag {
110 KERNFS_ACTIVATED = 0x0010,
111 KERNFS_NS = 0x0020,
112 KERNFS_HAS_SEQ_SHOW = 0x0040,
113 KERNFS_HAS_MMAP = 0x0080,
114 KERNFS_LOCKDEP = 0x0100,
115 KERNFS_HIDDEN = 0x0200,
116 KERNFS_SUICIDAL = 0x0400,
117 KERNFS_SUICIDED = 0x0800,
118 KERNFS_EMPTY_DIR = 0x1000,
119 KERNFS_HAS_RELEASE = 0x2000,
120 KERNFS_REMOVING = 0x4000,
121};
122
123/* @flags for kernfs_create_root() */
124enum kernfs_root_flag {
125 /*
126 * kernfs_nodes are created in the deactivated state and invisible.
127 * They require explicit kernfs_activate() to become visible. This
128 * can be used to make related nodes become visible atomically
129 * after all nodes are created successfully.
130 */
131 KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
132
133 /*
134 * For regular files, if the opener has CAP_DAC_OVERRIDE, open(2)
135 * succeeds regardless of the RW permissions. sysfs had an extra
136 * layer of enforcement where open(2) fails with -EACCES regardless
137 * of CAP_DAC_OVERRIDE if the permission doesn't have the
138 * respective read or write access at all (none of S_IRUGO or
139 * S_IWUGO) or the respective operation isn't implemented. The
140 * following flag enables that behavior.
141 */
142 KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
143
144 /*
145 * The filesystem supports exportfs operation, so userspace can use
146 * fhandle to access nodes of the fs.
147 */
148 KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
149
150 /*
151 * Support user xattrs to be written to nodes rooted at this root.
152 */
153 KERNFS_ROOT_SUPPORT_USER_XATTR = 0x0008,
154
155 /*
156 * Renames must not change the parent node.
157 */
158 KERNFS_ROOT_INVARIANT_PARENT = 0x0010,
159};
160
161/* type-specific structures for kernfs_node union members */
162struct kernfs_elem_dir {
163 unsigned long subdirs;
164 /* children rbtree starts here and goes through kn->rb */
165 struct rb_root children;
166
167 /*
168 * The kernfs hierarchy this directory belongs to. This fits
169 * better directly in kernfs_node but is here to save space.
170 */
171 struct kernfs_root *root;
172 /*
173 * Monotonic revision counter, used to identify if a directory
174 * node has changed during negative dentry revalidation.
175 */
176 unsigned long rev;
177};
178
179struct kernfs_elem_symlink {
180 struct kernfs_node *target_kn;
181};
182
183struct kernfs_elem_attr {
184 const struct kernfs_ops *ops;
185 struct kernfs_open_node __rcu *open;
186 loff_t size;
187 struct kernfs_node *notify_next; /* for kernfs_notify() */
188};
189
190/*
191 * kernfs_node - the building block of kernfs hierarchy. Each and every
192 * kernfs node is represented by single kernfs_node. Most fields are
193 * private to kernfs and shouldn't be accessed directly by kernfs users.
194 *
195 * As long as count reference is held, the kernfs_node itself is
196 * accessible. Dereferencing elem or any other outer entity requires
197 * active reference.
198 */
199struct kernfs_node {
200 atomic_t count;
201 atomic_t active;
202#ifdef CONFIG_DEBUG_LOCK_ALLOC
203 struct lockdep_map dep_map;
204#endif
205 /*
206 * Use kernfs_get_parent() and kernfs_name/path() instead of
207 * accessing the following two fields directly. If the node is
208 * never moved to a different parent, it is safe to access the
209 * parent directly.
210 */
211 struct kernfs_node __rcu *__parent;
212 const char __rcu *name;
213
214 struct rb_node rb;
215
216 const struct ns_common *ns; /* namespace tag */
217 unsigned int hash; /* ns + name hash */
218 unsigned short flags;
219 umode_t mode;
220
221 union {
222 struct kernfs_elem_dir dir;
223 struct kernfs_elem_symlink symlink;
224 struct kernfs_elem_attr attr;
225 };
226
227 /*
228 * 64bit unique ID. On 64bit ino setups, id is the ino. On 32bit,
229 * the low 32bits are ino and upper generation.
230 */
231 u64 id;
232
233 void *priv;
234 struct kernfs_iattrs *iattr;
235
236 struct rcu_head rcu;
237};
238
239/*
240 * kernfs_syscall_ops may be specified on kernfs_create_root() to support
241 * syscalls. These optional callbacks are invoked on the matching syscalls
242 * and can perform any kernfs operations which don't necessarily have to be
243 * the exact operation requested. An active reference is held for each
244 * kernfs_node parameter.
245 */
246struct kernfs_syscall_ops {
247 int (*show_options)(struct seq_file *sf, struct kernfs_root *root);
248
249 int (*mkdir)(struct kernfs_node *parent, const char *name,
250 umode_t mode);
251 int (*rmdir)(struct kernfs_node *kn);
252 int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
253 const char *new_name);
254 int (*show_path)(struct seq_file *sf, struct kernfs_node *kn,
255 struct kernfs_root *root);
256};
257
258struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root);
259
260struct kernfs_open_file {
261 /* published fields */
262 struct kernfs_node *kn;
263 struct file *file;
264 struct seq_file *seq_file;
265 void *priv;
266
267 /* private fields, do not use outside kernfs proper */
268 struct mutex mutex;
269 struct mutex prealloc_mutex;
270 int event;
271 struct list_head list;
272 char *prealloc_buf;
273
274 size_t atomic_write_len;
275 bool mmapped:1;
276 bool released:1;
277 const struct vm_operations_struct *vm_ops;
278};
279
280struct kernfs_ops {
281 /*
282 * Optional open/release methods. Both are called with
283 * @of->seq_file populated.
284 */
285 int (*open)(struct kernfs_open_file *of);
286 void (*release)(struct kernfs_open_file *of);
287
288 /*
289 * Read is handled by either seq_file or raw_read().
290 *
291 * If seq_show() is present, seq_file path is active. Other seq
292 * operations are optional and if not implemented, the behavior is
293 * equivalent to single_open(). @sf->private points to the
294 * associated kernfs_open_file.
295 *
296 * read() is bounced through kernel buffer and a read larger than
297 * PAGE_SIZE results in partial operation of PAGE_SIZE.
298 */
299 int (*seq_show)(struct seq_file *sf, void *v);
300
301 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
302 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
303 void (*seq_stop)(struct seq_file *sf, void *v);
304
305 ssize_t (*read)(struct kernfs_open_file *of, char *buf, size_t bytes,
306 loff_t off);
307
308 /*
309 * write() is bounced through kernel buffer. If atomic_write_len
310 * is not set, a write larger than PAGE_SIZE results in partial
311 * operations of PAGE_SIZE chunks. If atomic_write_len is set,
312 * writes upto the specified size are executed atomically but
313 * larger ones are rejected with -E2BIG.
314 */
315 size_t atomic_write_len;
316 /*
317 * "prealloc" causes a buffer to be allocated at open for
318 * all read/write requests. As ->seq_show uses seq_read()
319 * which does its own allocation, it is incompatible with
320 * ->prealloc. Provide ->read and ->write with ->prealloc.
321 */
322 bool prealloc;
323 ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
324 loff_t off);
325
326 __poll_t (*poll)(struct kernfs_open_file *of,
327 struct poll_table_struct *pt);
328
329 int (*mmap)(struct kernfs_open_file *of, struct vm_area_struct *vma);
330 loff_t (*llseek)(struct kernfs_open_file *of, loff_t offset, int whence);
331};
332
333/*
334 * The kernfs superblock creation/mount parameter context.
335 */
336struct kernfs_fs_context {
337 struct kernfs_root *root; /* Root of the hierarchy being mounted */
338 struct ns_common *ns_tag; /* Namespace tag of the mount (or NULL) */
339 unsigned long magic; /* File system specific magic number */
340
341 /* The following are set/used by kernfs_mount() */
342 bool new_sb_created; /* Set to T if we allocated a new sb */
343};
344
345#ifdef CONFIG_KERNFS
346
347static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
348{
349 return kn->flags & KERNFS_TYPE_MASK;
350}
351
352static inline ino_t kernfs_id_ino(u64 id)
353{
354 /* id is ino if ino_t is 64bit; otherwise, low 32bits */
355 if (sizeof(ino_t) >= sizeof(u64))
356 return id;
357 else
358 return (u32)id;
359}
360
361static inline u32 kernfs_id_gen(u64 id)
362{
363 /* gen is fixed at 1 if ino_t is 64bit; otherwise, high 32bits */
364 if (sizeof(ino_t) >= sizeof(u64))
365 return 1;
366 else
367 return id >> 32;
368}
369
370static inline ino_t kernfs_ino(struct kernfs_node *kn)
371{
372 return kernfs_id_ino(kn->id);
373}
374
375static inline ino_t kernfs_gen(struct kernfs_node *kn)
376{
377 return kernfs_id_gen(kn->id);
378}
379
380/**
381 * kernfs_enable_ns - enable namespace under a directory
382 * @kn: directory of interest, should be empty
383 *
384 * This is to be called right after @kn is created to enable namespace
385 * under it. All children of @kn must have non-NULL namespace tags and
386 * only the ones which match the super_block's tag will be visible.
387 */
388static inline void kernfs_enable_ns(struct kernfs_node *kn)
389{
390 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
391 WARN_ON_ONCE(!RB_EMPTY_ROOT(&kn->dir.children));
392 kn->flags |= KERNFS_NS;
393}
394
395/**
396 * kernfs_ns_enabled - test whether namespace is enabled
397 * @kn: the node to test
398 *
399 * Test whether namespace filtering is enabled for the children of @ns.
400 */
401static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
402{
403 return kn->flags & KERNFS_NS;
404}
405
406int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
407int kernfs_path_from_node(struct kernfs_node *kn_to, struct kernfs_node *kn_from,
408 char *buf, size_t buflen);
409void pr_cont_kernfs_name(struct kernfs_node *kn);
410void pr_cont_kernfs_path(struct kernfs_node *kn);
411struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
412struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
413 const char *name,
414 const struct ns_common *ns);
415struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
416 const char *path,
417 const struct ns_common *ns);
418void kernfs_get(struct kernfs_node *kn);
419void kernfs_put(struct kernfs_node *kn);
420
421struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry);
422struct kernfs_root *kernfs_root_from_sb(struct super_block *sb);
423struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
424
425struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
426 struct super_block *sb);
427struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
428 unsigned int flags, void *priv);
429void kernfs_destroy_root(struct kernfs_root *root);
430unsigned int kernfs_root_flags(struct kernfs_node *kn);
431
432struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
433 const char *name, umode_t mode,
434 kuid_t uid, kgid_t gid,
435 void *priv,
436 const struct ns_common *ns);
437struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
438 const char *name);
439struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
440 const char *name, umode_t mode,
441 kuid_t uid, kgid_t gid,
442 loff_t size,
443 const struct kernfs_ops *ops,
444 void *priv,
445 const struct ns_common *ns,
446 struct lock_class_key *key);
447struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
448 const char *name,
449 struct kernfs_node *target);
450void kernfs_activate(struct kernfs_node *kn);
451void kernfs_show(struct kernfs_node *kn, bool show);
452void kernfs_remove(struct kernfs_node *kn);
453void kernfs_break_active_protection(struct kernfs_node *kn);
454void kernfs_unbreak_active_protection(struct kernfs_node *kn);
455bool kernfs_remove_self(struct kernfs_node *kn);
456int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
457 const struct ns_common *ns);
458int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
459 const char *new_name, const struct ns_common *new_ns);
460int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
461__poll_t kernfs_generic_poll(struct kernfs_open_file *of,
462 struct poll_table_struct *pt);
463void kernfs_notify(struct kernfs_node *kn);
464
465int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
466 void *value, size_t size);
467int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
468 const void *value, size_t size, int flags);
469
470const struct ns_common *kernfs_super_ns(struct super_block *sb);
471int kernfs_get_tree(struct fs_context *fc);
472void kernfs_free_fs_context(struct fs_context *fc);
473void kernfs_kill_sb(struct super_block *sb);
474
475void kernfs_init(void);
476
477struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
478 u64 id);
479#else /* CONFIG_KERNFS */
480
481static inline enum kernfs_node_type kernfs_type(struct kernfs_node *kn)
482{ return 0; } /* whatever */
483
484static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
485
486static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
487{ return false; }
488
489static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
490{ return -ENOSYS; }
491
492static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
493 struct kernfs_node *kn,
494 char *buf, size_t buflen)
495{ return -ENOSYS; }
496
497static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
498static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
499
500static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
501{ return NULL; }
502
503static inline struct kernfs_node *
504kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
505 const struct ns_common *ns)
506{ return NULL; }
507static inline struct kernfs_node *
508kernfs_walk_and_get_ns(struct kernfs_node *parent, const char *path,
509 const struct ns_common *ns)
510{ return NULL; }
511
512static inline void kernfs_get(struct kernfs_node *kn) { }
513static inline void kernfs_put(struct kernfs_node *kn) { }
514
515static inline struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
516{ return NULL; }
517
518static inline struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
519{ return NULL; }
520
521static inline struct inode *
522kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
523{ return NULL; }
524
525static inline struct kernfs_root *
526kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags,
527 void *priv)
528{ return ERR_PTR(-ENOSYS); }
529
530static inline void kernfs_destroy_root(struct kernfs_root *root) { }
531static inline unsigned int kernfs_root_flags(struct kernfs_node *kn)
532{ return 0; }
533
534static inline struct kernfs_node *
535kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
536 umode_t mode, kuid_t uid, kgid_t gid,
537 void *priv, const struct ns_common *ns)
538{ return ERR_PTR(-ENOSYS); }
539
540static inline struct kernfs_node *
541__kernfs_create_file(struct kernfs_node *parent, const char *name,
542 umode_t mode, kuid_t uid, kgid_t gid,
543 loff_t size, const struct kernfs_ops *ops,
544 void *priv, const struct ns_common *ns,
545 struct lock_class_key *key)
546{ return ERR_PTR(-ENOSYS); }
547
548static inline struct kernfs_node *
549kernfs_create_link(struct kernfs_node *parent, const char *name,
550 struct kernfs_node *target)
551{ return ERR_PTR(-ENOSYS); }
552
553static inline void kernfs_activate(struct kernfs_node *kn) { }
554
555static inline void kernfs_remove(struct kernfs_node *kn) { }
556
557static inline bool kernfs_remove_self(struct kernfs_node *kn)
558{ return false; }
559
560static inline int kernfs_remove_by_name_ns(struct kernfs_node *kn,
561 const char *name,
562 const struct ns_common *ns)
563{ return -ENOSYS; }
564
565static inline int kernfs_rename_ns(struct kernfs_node *kn,
566 struct kernfs_node *new_parent,
567 const char *new_name,
568 const struct ns_common *new_ns)
569{ return -ENOSYS; }
570
571static inline int kernfs_setattr(struct kernfs_node *kn,
572 const struct iattr *iattr)
573{ return -ENOSYS; }
574
575static inline __poll_t kernfs_generic_poll(struct kernfs_open_file *of,
576 struct poll_table_struct *pt)
577{ return -ENOSYS; }
578
579static inline void kernfs_notify(struct kernfs_node *kn) { }
580
581static inline int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
582 void *value, size_t size)
583{ return -ENOSYS; }
584
585static inline int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
586 const void *value, size_t size, int flags)
587{ return -ENOSYS; }
588
589static inline const struct ns_common *kernfs_super_ns(struct super_block *sb)
590{ return NULL; }
591
592static inline int kernfs_get_tree(struct fs_context *fc)
593{ return -ENOSYS; }
594
595static inline void kernfs_free_fs_context(struct fs_context *fc) { }
596
597static inline void kernfs_kill_sb(struct super_block *sb) { }
598
599static inline void kernfs_init(void) { }
600
601#endif /* CONFIG_KERNFS */
602
603/**
604 * kernfs_path - build full path of a given node
605 * @kn: kernfs_node of interest
606 * @buf: buffer to copy @kn's name into
607 * @buflen: size of @buf
608 *
609 * If @kn is NULL result will be "(null)".
610 *
611 * Returns the length of the full path. If the full length is equal to or
612 * greater than @buflen, @buf contains the truncated path with the trailing
613 * '\0'. On error, -errno is returned.
614 */
615static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
616{
617 return kernfs_path_from_node(kn, NULL, buf, buflen);
618}
619
620static inline struct kernfs_node *
621kernfs_find_and_get(struct kernfs_node *kn, const char *name)
622{
623 return kernfs_find_and_get_ns(kn, name, NULL);
624}
625
626static inline struct kernfs_node *
627kernfs_walk_and_get(struct kernfs_node *kn, const char *path)
628{
629 return kernfs_walk_and_get_ns(kn, path, NULL);
630}
631
632static inline struct kernfs_node *
633kernfs_create_dir(struct kernfs_node *parent, const char *name, umode_t mode,
634 void *priv)
635{
636 return kernfs_create_dir_ns(parent, name, mode,
637 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
638 priv, NULL);
639}
640
641static inline int kernfs_remove_by_name(struct kernfs_node *parent,
642 const char *name)
643{
644 return kernfs_remove_by_name_ns(parent, name, NULL);
645}
646
647static inline int kernfs_rename(struct kernfs_node *kn,
648 struct kernfs_node *new_parent,
649 const char *new_name)
650{
651 return kernfs_rename_ns(kn, new_parent, new_name, NULL);
652}
653
654#endif /* __LINUX_KERNFS_H */