1 /* SPDX-License-Identifier: GPL-2.0 */
3 * sysfs.h - definitions for the device driver filesystem
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 <teheo@suse.de>
10 * Please see Documentation/filesystems/sysfs.rst for more information.
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>
33 #ifdef CONFIG_DEBUG_LOCK_ALLOC
34 bool ignore_lockdep:1;
35 struct lock_class_key *key;
36 struct lock_class_key skey;
41 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
42 * @attr: struct attribute to initialize
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.
50 #ifdef CONFIG_DEBUG_LOCK_ALLOC
51 #define sysfs_attr_init(attr) \
53 static struct lock_class_key __key; \
55 (attr)->key = &__key; \
58 #define sysfs_attr_init(attr) do {} while (0)
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.
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()
91 * Optional: Function to return the size of a binary attribute
92 * of the group. Will be called repeatedly for each binary
93 * attribute in the group. Overwrites the size field embedded
94 * inside the attribute itself.
95 * @attrs: Pointer to NULL terminated list of attributes.
96 * @bin_attrs: Pointer to NULL terminated list of binary attributes.
97 * Either attrs or bin_attrs or both must be provided.
99 struct attribute_group {
101 umode_t (*is_visible)(struct kobject *,
102 struct attribute *, int);
103 umode_t (*is_bin_visible)(struct kobject *,
104 const struct bin_attribute *, int);
105 size_t (*bin_size)(struct kobject *,
106 const struct bin_attribute *,
108 struct attribute **attrs;
110 const struct bin_attribute *const *bin_attrs;
111 const struct bin_attribute *const *bin_attrs_new;
115 #define SYSFS_PREALLOC 010000
116 #define SYSFS_GROUP_INVISIBLE 020000
119 * DEFINE_SYSFS_GROUP_VISIBLE(name):
120 * A helper macro to pair with the assignment of ".is_visible =
121 * SYSFS_GROUP_VISIBLE(name)", that arranges for the directory
122 * associated with a named attribute_group to optionally be hidden.
123 * This allows for static declaration of attribute_groups, and the
124 * simplification of attribute visibility lifetime that implies,
125 * without polluting sysfs with empty attribute directories.
128 * static umode_t example_attr_visible(struct kobject *kobj,
129 * struct attribute *attr, int n)
131 * if (example_attr_condition)
133 * else if (ro_attr_condition)
138 * static bool example_group_visible(struct kobject *kobj)
140 * if (example_group_condition)
145 * DEFINE_SYSFS_GROUP_VISIBLE(example);
147 * static struct attribute_group example_group = {
149 * .is_visible = SYSFS_GROUP_VISIBLE(example),
150 * .attrs = &example_attrs,
153 * Note that it expects <name>_attr_visible and <name>_group_visible to
154 * be defined. For cases where individual attributes do not need
155 * separate visibility consideration, only entire group visibility at
156 * once, see DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE().
158 #define DEFINE_SYSFS_GROUP_VISIBLE(name) \
159 static inline umode_t sysfs_group_visible_##name( \
160 struct kobject *kobj, struct attribute *attr, int n) \
162 if (n == 0 && !name##_group_visible(kobj)) \
163 return SYSFS_GROUP_INVISIBLE; \
164 return name##_attr_visible(kobj, attr, n); \
168 * DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(name):
169 * A helper macro to pair with SYSFS_GROUP_VISIBLE() that like
170 * DEFINE_SYSFS_GROUP_VISIBLE() controls group visibility, but does
171 * not require the implementation of a per-attribute visibility
175 * static bool example_group_visible(struct kobject *kobj)
177 * if (example_group_condition)
182 * DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(example);
184 * static struct attribute_group example_group = {
186 * .is_visible = SYSFS_GROUP_VISIBLE(example),
187 * .attrs = &example_attrs,
190 #define DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(name) \
191 static inline umode_t sysfs_group_visible_##name( \
192 struct kobject *kobj, struct attribute *a, int n) \
194 if (n == 0 && !name##_group_visible(kobj)) \
195 return SYSFS_GROUP_INVISIBLE; \
200 * Same as DEFINE_SYSFS_GROUP_VISIBLE, but for groups with only binary
201 * attributes. If an attribute_group defines both text and binary
202 * attributes, the group visibility is determined by the function
203 * specified to is_visible() not is_bin_visible()
205 #define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name) \
206 static inline umode_t sysfs_group_visible_##name( \
207 struct kobject *kobj, const struct bin_attribute *attr, int n) \
209 if (n == 0 && !name##_group_visible(kobj)) \
210 return SYSFS_GROUP_INVISIBLE; \
211 return name##_attr_visible(kobj, attr, n); \
214 #define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name) \
215 static inline umode_t sysfs_group_visible_##name( \
216 struct kobject *kobj, const struct bin_attribute *a, int n) \
218 if (n == 0 && !name##_group_visible(kobj)) \
219 return SYSFS_GROUP_INVISIBLE; \
223 #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn
226 * Use these macros to make defining attributes easier.
227 * See include/linux/device.h for examples..
230 #define __ATTR(_name, _mode, _show, _store) { \
231 .attr = {.name = __stringify(_name), \
232 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
237 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
238 .attr = {.name = __stringify(_name), \
239 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
244 #define __ATTR_RO(_name) { \
245 .attr = { .name = __stringify(_name), .mode = 0444 }, \
246 .show = _name##_show, \
249 #define __ATTR_RO_MODE(_name, _mode) { \
250 .attr = { .name = __stringify(_name), \
251 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
252 .show = _name##_show, \
255 #define __ATTR_RW_MODE(_name, _mode) { \
256 .attr = { .name = __stringify(_name), \
257 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
258 .show = _name##_show, \
259 .store = _name##_store, \
262 #define __ATTR_WO(_name) { \
263 .attr = { .name = __stringify(_name), .mode = 0200 }, \
264 .store = _name##_store, \
267 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
269 #define __ATTR_NULL { .attr = { .name = NULL } }
271 #ifdef CONFIG_DEBUG_LOCK_ALLOC
272 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
273 .attr = {.name = __stringify(_name), .mode = _mode, \
274 .ignore_lockdep = true }, \
279 #define __ATTR_IGNORE_LOCKDEP __ATTR
282 #define __ATTRIBUTE_GROUPS(_name) \
283 static const struct attribute_group *_name##_groups[] = { \
288 #define ATTRIBUTE_GROUPS(_name) \
289 static const struct attribute_group _name##_group = { \
290 .attrs = _name##_attrs, \
292 __ATTRIBUTE_GROUPS(_name)
294 #define BIN_ATTRIBUTE_GROUPS(_name) \
295 static const struct attribute_group _name##_group = { \
296 .bin_attrs_new = _name##_attrs, \
298 __ATTRIBUTE_GROUPS(_name)
301 struct vm_area_struct;
302 struct address_space;
304 struct bin_attribute {
305 struct attribute attr;
308 struct address_space *(*f_mapping)(void);
309 ssize_t (*read)(struct file *, struct kobject *, const struct bin_attribute *,
310 char *, loff_t, size_t);
311 ssize_t (*read_new)(struct file *, struct kobject *, const struct bin_attribute *,
312 char *, loff_t, size_t);
313 ssize_t (*write)(struct file *, struct kobject *, const struct bin_attribute *,
314 char *, loff_t, size_t);
315 ssize_t (*write_new)(struct file *, struct kobject *,
316 const struct bin_attribute *, char *, loff_t, size_t);
317 loff_t (*llseek)(struct file *, struct kobject *, const struct bin_attribute *,
319 int (*mmap)(struct file *, struct kobject *, const struct bin_attribute *attr,
320 struct vm_area_struct *vma);
324 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
325 * @attr: struct bin_attribute to initialize
327 * Initialize a dynamically allocated struct bin_attribute so we
328 * can make lockdep happy. This is a new requirement for
329 * attributes and initially this is only needed when lockdep is
330 * enabled. Lockdep gives a nice error when your attribute is
331 * added to sysfs if you don't have this.
333 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
335 /* macros to create static binary attributes easier */
336 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
337 .attr = { .name = __stringify(_name), .mode = _mode }, \
343 #define __BIN_ATTR_RO(_name, _size) \
344 __BIN_ATTR(_name, 0444, _name##_read, NULL, _size)
346 #define __BIN_ATTR_WO(_name, _size) \
347 __BIN_ATTR(_name, 0200, NULL, _name##_write, _size)
349 #define __BIN_ATTR_RW(_name, _size) \
350 __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size)
352 #define __BIN_ATTR_NULL __ATTR_NULL
354 #define BIN_ATTR(_name, _mode, _read, _write, _size) \
355 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
358 #define BIN_ATTR_RO(_name, _size) \
359 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
361 #define BIN_ATTR_WO(_name, _size) \
362 struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
364 #define BIN_ATTR_RW(_name, _size) \
365 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
368 #define __BIN_ATTR_ADMIN_RO(_name, _size) \
369 __BIN_ATTR(_name, 0400, _name##_read, NULL, _size)
371 #define __BIN_ATTR_ADMIN_RW(_name, _size) \
372 __BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
374 #define BIN_ATTR_ADMIN_RO(_name, _size) \
375 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
377 #define BIN_ATTR_ADMIN_RW(_name, _size) \
378 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
380 #define __BIN_ATTR_SIMPLE_RO(_name, _mode) \
381 __BIN_ATTR(_name, _mode, sysfs_bin_attr_simple_read, NULL, 0)
383 #define BIN_ATTR_SIMPLE_RO(_name) \
384 struct bin_attribute bin_attr_##_name = __BIN_ATTR_SIMPLE_RO(_name, 0444)
386 #define BIN_ATTR_SIMPLE_ADMIN_RO(_name) \
387 struct bin_attribute bin_attr_##_name = __BIN_ATTR_SIMPLE_RO(_name, 0400)
390 ssize_t (*show)(struct kobject *, struct attribute *, char *);
391 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
396 int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
397 void sysfs_remove_dir(struct kobject *kobj);
398 int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
400 int __must_check sysfs_move_dir_ns(struct kobject *kobj,
401 struct kobject *new_parent_kobj,
403 int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
405 void sysfs_remove_mount_point(struct kobject *parent_kobj,
408 int __must_check sysfs_create_file_ns(struct kobject *kobj,
409 const struct attribute *attr,
411 int __must_check sysfs_create_files(struct kobject *kobj,
412 const struct attribute * const *attr);
413 int __must_check sysfs_chmod_file(struct kobject *kobj,
414 const struct attribute *attr, umode_t mode);
415 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
416 const struct attribute *attr);
417 void sysfs_unbreak_active_protection(struct kernfs_node *kn);
418 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
420 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
421 void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attr);
423 int __must_check sysfs_create_bin_file(struct kobject *kobj,
424 const struct bin_attribute *attr);
425 void sysfs_remove_bin_file(struct kobject *kobj,
426 const struct bin_attribute *attr);
428 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
430 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
431 struct kobject *target,
433 void sysfs_remove_link(struct kobject *kobj, const char *name);
435 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
436 const char *old_name, const char *new_name,
439 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
442 int __must_check sysfs_create_group(struct kobject *kobj,
443 const struct attribute_group *grp);
444 int __must_check sysfs_create_groups(struct kobject *kobj,
445 const struct attribute_group **groups);
446 int __must_check sysfs_update_groups(struct kobject *kobj,
447 const struct attribute_group **groups);
448 int sysfs_update_group(struct kobject *kobj,
449 const struct attribute_group *grp);
450 void sysfs_remove_group(struct kobject *kobj,
451 const struct attribute_group *grp);
452 void sysfs_remove_groups(struct kobject *kobj,
453 const struct attribute_group **groups);
454 int sysfs_add_file_to_group(struct kobject *kobj,
455 const struct attribute *attr, const char *group);
456 void sysfs_remove_file_from_group(struct kobject *kobj,
457 const struct attribute *attr, const char *group);
458 int sysfs_merge_group(struct kobject *kobj,
459 const struct attribute_group *grp);
460 void sysfs_unmerge_group(struct kobject *kobj,
461 const struct attribute_group *grp);
462 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
463 struct kobject *target, const char *link_name);
464 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
465 const char *link_name);
466 int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
467 struct kobject *target_kobj,
468 const char *target_name,
469 const char *symlink_name);
471 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
473 int __must_check sysfs_init(void);
475 static inline void sysfs_enable_ns(struct kernfs_node *kn)
477 return kernfs_enable_ns(kn);
480 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
482 int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid);
483 int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
484 const char *name, kuid_t kuid, kgid_t kgid);
485 int sysfs_groups_change_owner(struct kobject *kobj,
486 const struct attribute_group **groups,
487 kuid_t kuid, kgid_t kgid);
488 int sysfs_group_change_owner(struct kobject *kobj,
489 const struct attribute_group *groups, kuid_t kuid,
492 int sysfs_emit(char *buf, const char *fmt, ...);
494 int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
496 ssize_t sysfs_bin_attr_simple_read(struct file *file, struct kobject *kobj,
497 const struct bin_attribute *attr, char *buf,
498 loff_t off, size_t count);
500 #else /* CONFIG_SYSFS */
502 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
507 static inline void sysfs_remove_dir(struct kobject *kobj)
511 static inline int sysfs_rename_dir_ns(struct kobject *kobj,
512 const char *new_name, const void *new_ns)
517 static inline int sysfs_move_dir_ns(struct kobject *kobj,
518 struct kobject *new_parent_kobj,
524 static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
530 static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
535 static inline int sysfs_create_file_ns(struct kobject *kobj,
536 const struct attribute *attr,
542 static inline int sysfs_create_files(struct kobject *kobj,
543 const struct attribute * const *attr)
548 static inline int sysfs_chmod_file(struct kobject *kobj,
549 const struct attribute *attr, umode_t mode)
554 static inline struct kernfs_node *
555 sysfs_break_active_protection(struct kobject *kobj,
556 const struct attribute *attr)
561 static inline void sysfs_unbreak_active_protection(struct kernfs_node *kn)
565 static inline void sysfs_remove_file_ns(struct kobject *kobj,
566 const struct attribute *attr,
571 static inline bool sysfs_remove_file_self(struct kobject *kobj,
572 const struct attribute *attr)
577 static inline void sysfs_remove_files(struct kobject *kobj,
578 const struct attribute * const *attr)
582 static inline int sysfs_create_bin_file(struct kobject *kobj,
583 const struct bin_attribute *attr)
588 static inline void sysfs_remove_bin_file(struct kobject *kobj,
589 const struct bin_attribute *attr)
593 static inline int sysfs_create_link(struct kobject *kobj,
594 struct kobject *target, const char *name)
599 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
600 struct kobject *target,
606 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
610 static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
611 const char *old_name,
612 const char *new_name, const void *ns)
617 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
622 static inline int sysfs_create_group(struct kobject *kobj,
623 const struct attribute_group *grp)
628 static inline int sysfs_create_groups(struct kobject *kobj,
629 const struct attribute_group **groups)
634 static inline int sysfs_update_groups(struct kobject *kobj,
635 const struct attribute_group **groups)
640 static inline int sysfs_update_group(struct kobject *kobj,
641 const struct attribute_group *grp)
646 static inline void sysfs_remove_group(struct kobject *kobj,
647 const struct attribute_group *grp)
651 static inline void sysfs_remove_groups(struct kobject *kobj,
652 const struct attribute_group **groups)
656 static inline int sysfs_add_file_to_group(struct kobject *kobj,
657 const struct attribute *attr, const char *group)
662 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
663 const struct attribute *attr, const char *group)
667 static inline int sysfs_merge_group(struct kobject *kobj,
668 const struct attribute_group *grp)
673 static inline void sysfs_unmerge_group(struct kobject *kobj,
674 const struct attribute_group *grp)
678 static inline int sysfs_add_link_to_group(struct kobject *kobj,
679 const char *group_name, struct kobject *target,
680 const char *link_name)
685 static inline void sysfs_remove_link_from_group(struct kobject *kobj,
686 const char *group_name, const char *link_name)
690 static inline int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
691 struct kobject *target_kobj,
692 const char *target_name,
693 const char *symlink_name)
698 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
703 static inline int __must_check sysfs_init(void)
708 static inline void sysfs_enable_ns(struct kernfs_node *kn)
712 static inline int sysfs_file_change_owner(struct kobject *kobj,
713 const char *name, kuid_t kuid,
719 static inline int sysfs_link_change_owner(struct kobject *kobj,
720 struct kobject *targ,
721 const char *name, kuid_t kuid,
727 static inline int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
732 static inline int sysfs_groups_change_owner(struct kobject *kobj,
733 const struct attribute_group **groups,
734 kuid_t kuid, kgid_t kgid)
739 static inline int sysfs_group_change_owner(struct kobject *kobj,
740 const struct attribute_group *groups,
741 kuid_t kuid, kgid_t kgid)
747 static inline int sysfs_emit(char *buf, const char *fmt, ...)
753 static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
758 static inline ssize_t sysfs_bin_attr_simple_read(struct file *file,
759 struct kobject *kobj,
760 const struct bin_attribute *attr,
761 char *buf, loff_t off,
766 #endif /* CONFIG_SYSFS */
768 static inline int __must_check sysfs_create_file(struct kobject *kobj,
769 const struct attribute *attr)
771 return sysfs_create_file_ns(kobj, attr, NULL);
774 static inline void sysfs_remove_file(struct kobject *kobj,
775 const struct attribute *attr)
777 sysfs_remove_file_ns(kobj, attr, NULL);
780 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
781 const char *old_name, const char *new_name)
783 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
786 static inline void sysfs_notify_dirent(struct kernfs_node *kn)
791 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
794 return kernfs_find_and_get(parent, name);
797 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
803 static inline void sysfs_put(struct kernfs_node *kn)
808 #endif /* _SYSFS_H_ */