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