1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * sysfs.h - definitions for the device driver filesystem 4 * 5 * Copyright (c) 2001,2002 Patrick Mochel 6 * Copyright (c) 2004 Silicon Graphics, Inc. 7 * Copyright (c) 2007 SUSE Linux Products GmbH 8 * Copyright (c) 2007 Tejun Heo <[email protected]> 9 * 10 * Please see Documentation/filesystems/sysfs.rst for more information. 11 */ 12 13 #ifndef _SYSFS_H_ 14 #define _SYSFS_H_ 15 16 #include <linux/kernfs.h> 17 #include <linux/compiler.h> 18 #include <linux/errno.h> 19 #include <linux/list.h> 20 #include <linux/lockdep.h> 21 #include <linux/kobject_ns.h> 22 #include <linux/stat.h> 23 #include <linux/atomic.h> 24 25 struct kobject; 26 struct module; 27 struct bin_attribute; 28 enum kobj_ns_type; 29 30 struct attribute { 31 const char *name; 32 umode_t mode; 33 #ifdef CONFIG_DEBUG_LOCK_ALLOC 34 bool ignore_lockdep:1; 35 struct lock_class_key *key; 36 struct lock_class_key skey; 37 #endif 38 }; 39 40 /** 41 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute 42 * @attr: struct attribute to initialize 43 * 44 * Initialize a dynamically allocated struct attribute so we can 45 * make lockdep happy. This is a new requirement for attributes 46 * and initially this is only needed when lockdep is enabled. 47 * Lockdep gives a nice error when your attribute is added to 48 * sysfs if you don't have this. 49 */ 50 #ifdef CONFIG_DEBUG_LOCK_ALLOC 51 #define sysfs_attr_init(attr) \ 52 do { \ 53 static struct lock_class_key __key; \ 54 \ 55 (attr)->key = &__key; \ 56 } while (0) 57 #else 58 #define sysfs_attr_init(attr) do {} while (0) 59 #endif 60 61 /** 62 * struct attribute_group - data structure used to declare an attribute group. 63 * @name: Optional: Attribute group name 64 * If specified, the attribute group will be created in a 65 * new subdirectory with this name. Additionally when a 66 * group is named, @is_visible and @is_bin_visible may 67 * return SYSFS_GROUP_INVISIBLE to control visibility of 68 * the directory itself. 69 * @is_visible: Optional: Function to return permissions associated with an 70 * attribute of the group. Will be called repeatedly for 71 * each non-binary attribute in the group. Only read/write 72 * permissions as well as SYSFS_PREALLOC are accepted. Must 73 * return 0 if an attribute is not visible. The returned 74 * value will replace static permissions defined in struct 75 * attribute. Use SYSFS_GROUP_VISIBLE() when assigning this 76 * callback to specify separate _group_visible() and 77 * _attr_visible() handlers. 78 * @is_bin_visible: 79 * Optional: Function to return permissions associated with a 80 * binary attribute of the group. Will be called repeatedly 81 * for each binary attribute in the group. Only read/write 82 * permissions as well as SYSFS_PREALLOC (and the 83 * visibility flags for named groups) are accepted. Must 84 * return 0 if a binary attribute is not visible. The 85 * returned value will replace static permissions defined 86 * in struct bin_attribute. If @is_visible is not set, Use 87 * SYSFS_GROUP_VISIBLE() when assigning this callback to 88 * specify separate _group_visible() and _attr_visible() 89 * handlers. 90 * @attrs: Pointer to NULL terminated list of attributes. 91 * @bin_attrs: Pointer to NULL terminated list of binary attributes. 92 * Either attrs or bin_attrs or both must be provided. 93 */ 94 struct attribute_group { 95 const char *name; 96 umode_t (*is_visible)(struct kobject *, 97 struct attribute *, int); 98 umode_t (*is_bin_visible)(struct kobject *, 99 struct bin_attribute *, int); 100 struct attribute **attrs; 101 struct bin_attribute **bin_attrs; 102 }; 103 104 #define SYSFS_PREALLOC 010000 105 #define SYSFS_GROUP_INVISIBLE 020000 106 107 /* 108 * The first call to is_visible() in the create / update path may 109 * indicate visibility for the entire group 110 */ 111 #define DEFINE_SYSFS_GROUP_VISIBLE(name) \ 112 static inline umode_t sysfs_group_visible_##name( \ 113 struct kobject *kobj, struct attribute *attr, int n) \ 114 { \ 115 if (n == 0 && !name##_group_visible(kobj)) \ 116 return SYSFS_GROUP_INVISIBLE; \ 117 return name##_attr_visible(kobj, attr, n); \ 118 } 119 120 /* 121 * Same as DEFINE_SYSFS_GROUP_VISIBLE, but for groups with only binary 122 * attributes 123 */ 124 #define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name) \ 125 static inline umode_t sysfs_group_visible_##name( \ 126 struct kobject *kobj, struct bin_attribute *attr, int n) \ 127 { \ 128 if (n == 0 && !name##_group_visible(kobj)) \ 129 return SYSFS_GROUP_INVISIBLE; \ 130 return name##_attr_visible(kobj, attr, n); \ 131 } 132 133 #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn 134 135 /* 136 * Use these macros to make defining attributes easier. 137 * See include/linux/device.h for examples.. 138 */ 139 140 #define __ATTR(_name, _mode, _show, _store) { \ 141 .attr = {.name = __stringify(_name), \ 142 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ 143 .show = _show, \ 144 .store = _store, \ 145 } 146 147 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \ 148 .attr = {.name = __stringify(_name), \ 149 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\ 150 .show = _show, \ 151 .store = _store, \ 152 } 153 154 #define __ATTR_RO(_name) { \ 155 .attr = { .name = __stringify(_name), .mode = 0444 }, \ 156 .show = _name##_show, \ 157 } 158 159 #define __ATTR_RO_MODE(_name, _mode) { \ 160 .attr = { .name = __stringify(_name), \ 161 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ 162 .show = _name##_show, \ 163 } 164 165 #define __ATTR_RW_MODE(_name, _mode) { \ 166 .attr = { .name = __stringify(_name), \ 167 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ 168 .show = _name##_show, \ 169 .store = _name##_store, \ 170 } 171 172 #define __ATTR_WO(_name) { \ 173 .attr = { .name = __stringify(_name), .mode = 0200 }, \ 174 .store = _name##_store, \ 175 } 176 177 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store) 178 179 #define __ATTR_NULL { .attr = { .name = NULL } } 180 181 #ifdef CONFIG_DEBUG_LOCK_ALLOC 182 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \ 183 .attr = {.name = __stringify(_name), .mode = _mode, \ 184 .ignore_lockdep = true }, \ 185 .show = _show, \ 186 .store = _store, \ 187 } 188 #else 189 #define __ATTR_IGNORE_LOCKDEP __ATTR 190 #endif 191 192 #define __ATTRIBUTE_GROUPS(_name) \ 193 static const struct attribute_group *_name##_groups[] = { \ 194 &_name##_group, \ 195 NULL, \ 196 } 197 198 #define ATTRIBUTE_GROUPS(_name) \ 199 static const struct attribute_group _name##_group = { \ 200 .attrs = _name##_attrs, \ 201 }; \ 202 __ATTRIBUTE_GROUPS(_name) 203 204 #define BIN_ATTRIBUTE_GROUPS(_name) \ 205 static const struct attribute_group _name##_group = { \ 206 .bin_attrs = _name##_attrs, \ 207 }; \ 208 __ATTRIBUTE_GROUPS(_name) 209 210 struct file; 211 struct vm_area_struct; 212 struct address_space; 213 214 struct bin_attribute { 215 struct attribute attr; 216 size_t size; 217 void *private; 218 struct address_space *(*f_mapping)(void); 219 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *, 220 char *, loff_t, size_t); 221 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *, 222 char *, loff_t, size_t); 223 loff_t (*llseek)(struct file *, struct kobject *, struct bin_attribute *, 224 loff_t, int); 225 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr, 226 struct vm_area_struct *vma); 227 }; 228 229 /** 230 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute 231 * @attr: struct bin_attribute to initialize 232 * 233 * Initialize a dynamically allocated struct bin_attribute so we 234 * can make lockdep happy. This is a new requirement for 235 * attributes and initially this is only needed when lockdep is 236 * enabled. Lockdep gives a nice error when your attribute is 237 * added to sysfs if you don't have this. 238 */ 239 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr) 240 241 /* macros to create static binary attributes easier */ 242 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \ 243 .attr = { .name = __stringify(_name), .mode = _mode }, \ 244 .read = _read, \ 245 .write = _write, \ 246 .size = _size, \ 247 } 248 249 #define __BIN_ATTR_RO(_name, _size) { \ 250 .attr = { .name = __stringify(_name), .mode = 0444 }, \ 251 .read = _name##_read, \ 252 .size = _size, \ 253 } 254 255 #define __BIN_ATTR_WO(_name, _size) { \ 256 .attr = { .name = __stringify(_name), .mode = 0200 }, \ 257 .write = _name##_write, \ 258 .size = _size, \ 259 } 260 261 #define __BIN_ATTR_RW(_name, _size) \ 262 __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size) 263 264 #define __BIN_ATTR_NULL __ATTR_NULL 265 266 #define BIN_ATTR(_name, _mode, _read, _write, _size) \ 267 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \ 268 _write, _size) 269 270 #define BIN_ATTR_RO(_name, _size) \ 271 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size) 272 273 #define BIN_ATTR_WO(_name, _size) \ 274 struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size) 275 276 #define BIN_ATTR_RW(_name, _size) \ 277 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) 278 279 280 #define __BIN_ATTR_ADMIN_RO(_name, _size) { \ 281 .attr = { .name = __stringify(_name), .mode = 0400 }, \ 282 .read = _name##_read, \ 283 .size = _size, \ 284 } 285 286 #define __BIN_ATTR_ADMIN_RW(_name, _size) \ 287 __BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size) 288 289 #define BIN_ATTR_ADMIN_RO(_name, _size) \ 290 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size) 291 292 #define BIN_ATTR_ADMIN_RW(_name, _size) \ 293 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size) 294 295 struct sysfs_ops { 296 ssize_t (*show)(struct kobject *, struct attribute *, char *); 297 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); 298 }; 299 300 #ifdef CONFIG_SYSFS 301 302 int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns); 303 void sysfs_remove_dir(struct kobject *kobj); 304 int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, 305 const void *new_ns); 306 int __must_check sysfs_move_dir_ns(struct kobject *kobj, 307 struct kobject *new_parent_kobj, 308 const void *new_ns); 309 int __must_check sysfs_create_mount_point(struct kobject *parent_kobj, 310 const char *name); 311 void sysfs_remove_mount_point(struct kobject *parent_kobj, 312 const char *name); 313 314 int __must_check sysfs_create_file_ns(struct kobject *kobj, 315 const struct attribute *attr, 316 const void *ns); 317 int __must_check sysfs_create_files(struct kobject *kobj, 318 const struct attribute * const *attr); 319 int __must_check sysfs_chmod_file(struct kobject *kobj, 320 const struct attribute *attr, umode_t mode); 321 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj, 322 const struct attribute *attr); 323 void sysfs_unbreak_active_protection(struct kernfs_node *kn); 324 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr, 325 const void *ns); 326 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr); 327 void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attr); 328 329 int __must_check sysfs_create_bin_file(struct kobject *kobj, 330 const struct bin_attribute *attr); 331 void sysfs_remove_bin_file(struct kobject *kobj, 332 const struct bin_attribute *attr); 333 334 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target, 335 const char *name); 336 int __must_check sysfs_create_link_nowarn(struct kobject *kobj, 337 struct kobject *target, 338 const char *name); 339 void sysfs_remove_link(struct kobject *kobj, const char *name); 340 341 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target, 342 const char *old_name, const char *new_name, 343 const void *new_ns); 344 345 void sysfs_delete_link(struct kobject *dir, struct kobject *targ, 346 const char *name); 347 348 int __must_check sysfs_create_group(struct kobject *kobj, 349 const struct attribute_group *grp); 350 int __must_check sysfs_create_groups(struct kobject *kobj, 351 const struct attribute_group **groups); 352 int __must_check sysfs_update_groups(struct kobject *kobj, 353 const struct attribute_group **groups); 354 int sysfs_update_group(struct kobject *kobj, 355 const struct attribute_group *grp); 356 void sysfs_remove_group(struct kobject *kobj, 357 const struct attribute_group *grp); 358 void sysfs_remove_groups(struct kobject *kobj, 359 const struct attribute_group **groups); 360 int sysfs_add_file_to_group(struct kobject *kobj, 361 const struct attribute *attr, const char *group); 362 void sysfs_remove_file_from_group(struct kobject *kobj, 363 const struct attribute *attr, const char *group); 364 int sysfs_merge_group(struct kobject *kobj, 365 const struct attribute_group *grp); 366 void sysfs_unmerge_group(struct kobject *kobj, 367 const struct attribute_group *grp); 368 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, 369 struct kobject *target, const char *link_name); 370 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, 371 const char *link_name); 372 int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, 373 struct kobject *target_kobj, 374 const char *target_name, 375 const char *symlink_name); 376 377 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr); 378 379 int __must_check sysfs_init(void); 380 381 static inline void sysfs_enable_ns(struct kernfs_node *kn) 382 { 383 return kernfs_enable_ns(kn); 384 } 385 386 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid, 387 kgid_t kgid); 388 int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid); 389 int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ, 390 const char *name, kuid_t kuid, kgid_t kgid); 391 int sysfs_groups_change_owner(struct kobject *kobj, 392 const struct attribute_group **groups, 393 kuid_t kuid, kgid_t kgid); 394 int sysfs_group_change_owner(struct kobject *kobj, 395 const struct attribute_group *groups, kuid_t kuid, 396 kgid_t kgid); 397 __printf(2, 3) 398 int sysfs_emit(char *buf, const char *fmt, ...); 399 __printf(3, 4) 400 int sysfs_emit_at(char *buf, int at, const char *fmt, ...); 401 402 #else /* CONFIG_SYSFS */ 403 404 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) 405 { 406 return 0; 407 } 408 409 static inline void sysfs_remove_dir(struct kobject *kobj) 410 { 411 } 412 413 static inline int sysfs_rename_dir_ns(struct kobject *kobj, 414 const char *new_name, const void *new_ns) 415 { 416 return 0; 417 } 418 419 static inline int sysfs_move_dir_ns(struct kobject *kobj, 420 struct kobject *new_parent_kobj, 421 const void *new_ns) 422 { 423 return 0; 424 } 425 426 static inline int sysfs_create_mount_point(struct kobject *parent_kobj, 427 const char *name) 428 { 429 return 0; 430 } 431 432 static inline void sysfs_remove_mount_point(struct kobject *parent_kobj, 433 const char *name) 434 { 435 } 436 437 static inline int sysfs_create_file_ns(struct kobject *kobj, 438 const struct attribute *attr, 439 const void *ns) 440 { 441 return 0; 442 } 443 444 static inline int sysfs_create_files(struct kobject *kobj, 445 const struct attribute * const *attr) 446 { 447 return 0; 448 } 449 450 static inline int sysfs_chmod_file(struct kobject *kobj, 451 const struct attribute *attr, umode_t mode) 452 { 453 return 0; 454 } 455 456 static inline struct kernfs_node * 457 sysfs_break_active_protection(struct kobject *kobj, 458 const struct attribute *attr) 459 { 460 return NULL; 461 } 462 463 static inline void sysfs_unbreak_active_protection(struct kernfs_node *kn) 464 { 465 } 466 467 static inline void sysfs_remove_file_ns(struct kobject *kobj, 468 const struct attribute *attr, 469 const void *ns) 470 { 471 } 472 473 static inline bool sysfs_remove_file_self(struct kobject *kobj, 474 const struct attribute *attr) 475 { 476 return false; 477 } 478 479 static inline void sysfs_remove_files(struct kobject *kobj, 480 const struct attribute * const *attr) 481 { 482 } 483 484 static inline int sysfs_create_bin_file(struct kobject *kobj, 485 const struct bin_attribute *attr) 486 { 487 return 0; 488 } 489 490 static inline void sysfs_remove_bin_file(struct kobject *kobj, 491 const struct bin_attribute *attr) 492 { 493 } 494 495 static inline int sysfs_create_link(struct kobject *kobj, 496 struct kobject *target, const char *name) 497 { 498 return 0; 499 } 500 501 static inline int sysfs_create_link_nowarn(struct kobject *kobj, 502 struct kobject *target, 503 const char *name) 504 { 505 return 0; 506 } 507 508 static inline void sysfs_remove_link(struct kobject *kobj, const char *name) 509 { 510 } 511 512 static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t, 513 const char *old_name, 514 const char *new_name, const void *ns) 515 { 516 return 0; 517 } 518 519 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t, 520 const char *name) 521 { 522 } 523 524 static inline int sysfs_create_group(struct kobject *kobj, 525 const struct attribute_group *grp) 526 { 527 return 0; 528 } 529 530 static inline int sysfs_create_groups(struct kobject *kobj, 531 const struct attribute_group **groups) 532 { 533 return 0; 534 } 535 536 static inline int sysfs_update_groups(struct kobject *kobj, 537 const struct attribute_group **groups) 538 { 539 return 0; 540 } 541 542 static inline int sysfs_update_group(struct kobject *kobj, 543 const struct attribute_group *grp) 544 { 545 return 0; 546 } 547 548 static inline void sysfs_remove_group(struct kobject *kobj, 549 const struct attribute_group *grp) 550 { 551 } 552 553 static inline void sysfs_remove_groups(struct kobject *kobj, 554 const struct attribute_group **groups) 555 { 556 } 557 558 static inline int sysfs_add_file_to_group(struct kobject *kobj, 559 const struct attribute *attr, const char *group) 560 { 561 return 0; 562 } 563 564 static inline void sysfs_remove_file_from_group(struct kobject *kobj, 565 const struct attribute *attr, const char *group) 566 { 567 } 568 569 static inline int sysfs_merge_group(struct kobject *kobj, 570 const struct attribute_group *grp) 571 { 572 return 0; 573 } 574 575 static inline void sysfs_unmerge_group(struct kobject *kobj, 576 const struct attribute_group *grp) 577 { 578 } 579 580 static inline int sysfs_add_link_to_group(struct kobject *kobj, 581 const char *group_name, struct kobject *target, 582 const char *link_name) 583 { 584 return 0; 585 } 586 587 static inline void sysfs_remove_link_from_group(struct kobject *kobj, 588 const char *group_name, const char *link_name) 589 { 590 } 591 592 static inline int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, 593 struct kobject *target_kobj, 594 const char *target_name, 595 const char *symlink_name) 596 { 597 return 0; 598 } 599 600 static inline void sysfs_notify(struct kobject *kobj, const char *dir, 601 const char *attr) 602 { 603 } 604 605 static inline int __must_check sysfs_init(void) 606 { 607 return 0; 608 } 609 610 static inline void sysfs_enable_ns(struct kernfs_node *kn) 611 { 612 } 613 614 static inline int sysfs_file_change_owner(struct kobject *kobj, 615 const char *name, kuid_t kuid, 616 kgid_t kgid) 617 { 618 return 0; 619 } 620 621 static inline int sysfs_link_change_owner(struct kobject *kobj, 622 struct kobject *targ, 623 const char *name, kuid_t kuid, 624 kgid_t kgid) 625 { 626 return 0; 627 } 628 629 static inline int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid) 630 { 631 return 0; 632 } 633 634 static inline int sysfs_groups_change_owner(struct kobject *kobj, 635 const struct attribute_group **groups, 636 kuid_t kuid, kgid_t kgid) 637 { 638 return 0; 639 } 640 641 static inline int sysfs_group_change_owner(struct kobject *kobj, 642 const struct attribute_group *groups, 643 kuid_t kuid, kgid_t kgid) 644 { 645 return 0; 646 } 647 648 __printf(2, 3) 649 static inline int sysfs_emit(char *buf, const char *fmt, ...) 650 { 651 return 0; 652 } 653 654 __printf(3, 4) 655 static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...) 656 { 657 return 0; 658 } 659 #endif /* CONFIG_SYSFS */ 660 661 static inline int __must_check sysfs_create_file(struct kobject *kobj, 662 const struct attribute *attr) 663 { 664 return sysfs_create_file_ns(kobj, attr, NULL); 665 } 666 667 static inline void sysfs_remove_file(struct kobject *kobj, 668 const struct attribute *attr) 669 { 670 sysfs_remove_file_ns(kobj, attr, NULL); 671 } 672 673 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target, 674 const char *old_name, const char *new_name) 675 { 676 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL); 677 } 678 679 static inline void sysfs_notify_dirent(struct kernfs_node *kn) 680 { 681 kernfs_notify(kn); 682 } 683 684 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent, 685 const char *name) 686 { 687 return kernfs_find_and_get(parent, name); 688 } 689 690 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn) 691 { 692 kernfs_get(kn); 693 return kn; 694 } 695 696 static inline void sysfs_put(struct kernfs_node *kn) 697 { 698 kernfs_put(kn); 699 } 700 701 #endif /* _SYSFS_H_ */ 702