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