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()
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.
94 struct attribute_group {
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;
104 #define SYSFS_PREALLOC 010000
105 #define SYSFS_GROUP_INVISIBLE 020000
108 * The first call to is_visible() in the create / update path may
109 * indicate visibility for the entire group
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) \
115 if (n == 0 && !name##_group_visible(kobj)) \
116 return SYSFS_GROUP_INVISIBLE; \
117 return name##_attr_visible(kobj, attr, n); \
121 * Same as DEFINE_SYSFS_GROUP_VISIBLE, but for groups with only binary
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) \
128 if (n == 0 && !name##_group_visible(kobj)) \
129 return SYSFS_GROUP_INVISIBLE; \
130 return name##_attr_visible(kobj, attr, n); \
133 #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn
136 * Use these macros to make defining attributes easier.
137 * See include/linux/device.h for examples..
140 #define __ATTR(_name, _mode, _show, _store) { \
141 .attr = {.name = __stringify(_name), \
142 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
147 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
148 .attr = {.name = __stringify(_name), \
149 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
154 #define __ATTR_RO(_name) { \
155 .attr = { .name = __stringify(_name), .mode = 0444 }, \
156 .show = _name##_show, \
159 #define __ATTR_RO_MODE(_name, _mode) { \
160 .attr = { .name = __stringify(_name), \
161 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
162 .show = _name##_show, \
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, \
172 #define __ATTR_WO(_name) { \
173 .attr = { .name = __stringify(_name), .mode = 0200 }, \
174 .store = _name##_store, \
177 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
179 #define __ATTR_NULL { .attr = { .name = NULL } }
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 }, \
189 #define __ATTR_IGNORE_LOCKDEP __ATTR
192 #define __ATTRIBUTE_GROUPS(_name) \
193 static const struct attribute_group *_name##_groups[] = { \
198 #define ATTRIBUTE_GROUPS(_name) \
199 static const struct attribute_group _name##_group = { \
200 .attrs = _name##_attrs, \
202 __ATTRIBUTE_GROUPS(_name)
204 #define BIN_ATTRIBUTE_GROUPS(_name) \
205 static const struct attribute_group _name##_group = { \
206 .bin_attrs = _name##_attrs, \
208 __ATTRIBUTE_GROUPS(_name)
211 struct vm_area_struct;
212 struct address_space;
214 struct bin_attribute {
215 struct attribute attr;
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 *,
225 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
226 struct vm_area_struct *vma);
230 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
231 * @attr: struct bin_attribute to initialize
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.
239 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
241 /* macros to create static binary attributes easier */
242 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
243 .attr = { .name = __stringify(_name), .mode = _mode }, \
249 #define __BIN_ATTR_RO(_name, _size) { \
250 .attr = { .name = __stringify(_name), .mode = 0444 }, \
251 .read = _name##_read, \
255 #define __BIN_ATTR_WO(_name, _size) { \
256 .attr = { .name = __stringify(_name), .mode = 0200 }, \
257 .write = _name##_write, \
261 #define __BIN_ATTR_RW(_name, _size) \
262 __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size)
264 #define __BIN_ATTR_NULL __ATTR_NULL
266 #define BIN_ATTR(_name, _mode, _read, _write, _size) \
267 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
270 #define BIN_ATTR_RO(_name, _size) \
271 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
273 #define BIN_ATTR_WO(_name, _size) \
274 struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
276 #define BIN_ATTR_RW(_name, _size) \
277 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
280 #define __BIN_ATTR_ADMIN_RO(_name, _size) { \
281 .attr = { .name = __stringify(_name), .mode = 0400 }, \
282 .read = _name##_read, \
286 #define __BIN_ATTR_ADMIN_RW(_name, _size) \
287 __BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
289 #define BIN_ATTR_ADMIN_RO(_name, _size) \
290 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
292 #define BIN_ATTR_ADMIN_RW(_name, _size) \
293 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
296 ssize_t (*show)(struct kobject *, struct attribute *, char *);
297 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
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,
306 int __must_check sysfs_move_dir_ns(struct kobject *kobj,
307 struct kobject *new_parent_kobj,
309 int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
311 void sysfs_remove_mount_point(struct kobject *parent_kobj,
314 int __must_check sysfs_create_file_ns(struct kobject *kobj,
315 const struct attribute *attr,
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,
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);
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);
334 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
336 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
337 struct kobject *target,
339 void sysfs_remove_link(struct kobject *kobj, const char *name);
341 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
342 const char *old_name, const char *new_name,
345 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
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);
377 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
379 int __must_check sysfs_init(void);
381 static inline void sysfs_enable_ns(struct kernfs_node *kn)
383 return kernfs_enable_ns(kn);
386 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
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,
398 int sysfs_emit(char *buf, const char *fmt, ...);
400 int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
402 #else /* CONFIG_SYSFS */
404 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
409 static inline void sysfs_remove_dir(struct kobject *kobj)
413 static inline int sysfs_rename_dir_ns(struct kobject *kobj,
414 const char *new_name, const void *new_ns)
419 static inline int sysfs_move_dir_ns(struct kobject *kobj,
420 struct kobject *new_parent_kobj,
426 static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
432 static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
437 static inline int sysfs_create_file_ns(struct kobject *kobj,
438 const struct attribute *attr,
444 static inline int sysfs_create_files(struct kobject *kobj,
445 const struct attribute * const *attr)
450 static inline int sysfs_chmod_file(struct kobject *kobj,
451 const struct attribute *attr, umode_t mode)
456 static inline struct kernfs_node *
457 sysfs_break_active_protection(struct kobject *kobj,
458 const struct attribute *attr)
463 static inline void sysfs_unbreak_active_protection(struct kernfs_node *kn)
467 static inline void sysfs_remove_file_ns(struct kobject *kobj,
468 const struct attribute *attr,
473 static inline bool sysfs_remove_file_self(struct kobject *kobj,
474 const struct attribute *attr)
479 static inline void sysfs_remove_files(struct kobject *kobj,
480 const struct attribute * const *attr)
484 static inline int sysfs_create_bin_file(struct kobject *kobj,
485 const struct bin_attribute *attr)
490 static inline void sysfs_remove_bin_file(struct kobject *kobj,
491 const struct bin_attribute *attr)
495 static inline int sysfs_create_link(struct kobject *kobj,
496 struct kobject *target, const char *name)
501 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
502 struct kobject *target,
508 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
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)
519 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
524 static inline int sysfs_create_group(struct kobject *kobj,
525 const struct attribute_group *grp)
530 static inline int sysfs_create_groups(struct kobject *kobj,
531 const struct attribute_group **groups)
536 static inline int sysfs_update_groups(struct kobject *kobj,
537 const struct attribute_group **groups)
542 static inline int sysfs_update_group(struct kobject *kobj,
543 const struct attribute_group *grp)
548 static inline void sysfs_remove_group(struct kobject *kobj,
549 const struct attribute_group *grp)
553 static inline void sysfs_remove_groups(struct kobject *kobj,
554 const struct attribute_group **groups)
558 static inline int sysfs_add_file_to_group(struct kobject *kobj,
559 const struct attribute *attr, const char *group)
564 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
565 const struct attribute *attr, const char *group)
569 static inline int sysfs_merge_group(struct kobject *kobj,
570 const struct attribute_group *grp)
575 static inline void sysfs_unmerge_group(struct kobject *kobj,
576 const struct attribute_group *grp)
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)
587 static inline void sysfs_remove_link_from_group(struct kobject *kobj,
588 const char *group_name, const char *link_name)
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)
600 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
605 static inline int __must_check sysfs_init(void)
610 static inline void sysfs_enable_ns(struct kernfs_node *kn)
614 static inline int sysfs_file_change_owner(struct kobject *kobj,
615 const char *name, kuid_t kuid,
621 static inline int sysfs_link_change_owner(struct kobject *kobj,
622 struct kobject *targ,
623 const char *name, kuid_t kuid,
629 static inline int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
634 static inline int sysfs_groups_change_owner(struct kobject *kobj,
635 const struct attribute_group **groups,
636 kuid_t kuid, kgid_t kgid)
641 static inline int sysfs_group_change_owner(struct kobject *kobj,
642 const struct attribute_group *groups,
643 kuid_t kuid, kgid_t kgid)
649 static inline int sysfs_emit(char *buf, const char *fmt, ...)
655 static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
659 #endif /* CONFIG_SYSFS */
661 static inline int __must_check sysfs_create_file(struct kobject *kobj,
662 const struct attribute *attr)
664 return sysfs_create_file_ns(kobj, attr, NULL);
667 static inline void sysfs_remove_file(struct kobject *kobj,
668 const struct attribute *attr)
670 sysfs_remove_file_ns(kobj, attr, NULL);
673 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
674 const char *old_name, const char *new_name)
676 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
679 static inline void sysfs_notify_dirent(struct kernfs_node *kn)
684 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
687 return kernfs_find_and_get(parent, name);
690 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
696 static inline void sysfs_put(struct kernfs_node *kn)
701 #endif /* _SYSFS_H_ */