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