xref: /linux-6.15/include/linux/sysfs.h (revision 5bf2b193)
1 /*
2  * sysfs.h - definitions for the device driver filesystem
3  *
4  * Copyright (c) 2001,2002 Patrick Mochel
5  * Copyright (c) 2004 Silicon Graphics, Inc.
6  * Copyright (c) 2007 SUSE Linux Products GmbH
7  * Copyright (c) 2007 Tejun Heo <[email protected]>
8  *
9  * Please see Documentation/filesystems/sysfs.txt for more information.
10  */
11 
12 #ifndef _SYSFS_H_
13 #define _SYSFS_H_
14 
15 #include <linux/compiler.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/lockdep.h>
19 #include <asm/atomic.h>
20 
21 struct kobject;
22 struct module;
23 enum kobj_ns_type;
24 
25 struct attribute {
26 	const char		*name;
27 	mode_t			mode;
28 #ifdef CONFIG_DEBUG_LOCK_ALLOC
29 	struct lock_class_key	*key;
30 	struct lock_class_key	skey;
31 #endif
32 };
33 
34 /**
35  *	sysfs_attr_init - initialize a dynamically allocated sysfs attribute
36  *	@attr: struct attribute to initialize
37  *
38  *	Initialize a dynamically allocated struct attribute so we can
39  *	make lockdep happy.  This is a new requirement for attributes
40  *	and initially this is only needed when lockdep is enabled.
41  *	Lockdep gives a nice error when your attribute is added to
42  *	sysfs if you don't have this.
43  */
44 #ifdef CONFIG_DEBUG_LOCK_ALLOC
45 #define sysfs_attr_init(attr)				\
46 do {							\
47 	static struct lock_class_key __key;		\
48 							\
49 	(attr)->key = &__key;				\
50 } while(0)
51 #else
52 #define sysfs_attr_init(attr) do {} while(0)
53 #endif
54 
55 struct attribute_group {
56 	const char		*name;
57 	mode_t			(*is_visible)(struct kobject *,
58 					      struct attribute *, int);
59 	struct attribute	**attrs;
60 };
61 
62 
63 
64 /**
65  * Use these macros to make defining attributes easier. See include/linux/device.h
66  * for examples..
67  */
68 
69 #define __ATTR(_name,_mode,_show,_store) { \
70 	.attr = {.name = __stringify(_name), .mode = _mode },	\
71 	.show	= _show,					\
72 	.store	= _store,					\
73 }
74 
75 #define __ATTR_RO(_name) { \
76 	.attr	= { .name = __stringify(_name), .mode = 0444 },	\
77 	.show	= _name##_show,					\
78 }
79 
80 #define __ATTR_NULL { .attr = { .name = NULL } }
81 
82 #define attr_name(_attr) (_attr).attr.name
83 
84 struct file;
85 struct vm_area_struct;
86 
87 struct bin_attribute {
88 	struct attribute	attr;
89 	size_t			size;
90 	void			*private;
91 	ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
92 			char *, loff_t, size_t);
93 	ssize_t (*write)(struct file *,struct kobject *, struct bin_attribute *,
94 			 char *, loff_t, size_t);
95 	int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
96 		    struct vm_area_struct *vma);
97 };
98 
99 /**
100  *	sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
101  *	@attr: struct bin_attribute to initialize
102  *
103  *	Initialize a dynamically allocated struct bin_attribute so we
104  *	can make lockdep happy.  This is a new requirement for
105  *	attributes and initially this is only needed when lockdep is
106  *	enabled.  Lockdep gives a nice error when your attribute is
107  *	added to sysfs if you don't have this.
108  */
109 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
110 
111 struct sysfs_ops {
112 	ssize_t	(*show)(struct kobject *, struct attribute *,char *);
113 	ssize_t	(*store)(struct kobject *,struct attribute *,const char *, size_t);
114 };
115 
116 struct sysfs_dirent;
117 
118 #ifdef CONFIG_SYSFS
119 
120 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
121 			    void *data, struct module *owner);
122 
123 int __must_check sysfs_create_dir(struct kobject *kobj);
124 void sysfs_remove_dir(struct kobject *kobj);
125 int __must_check sysfs_rename_dir(struct kobject *kobj, const char *new_name);
126 int __must_check sysfs_move_dir(struct kobject *kobj,
127 				struct kobject *new_parent_kobj);
128 
129 int __must_check sysfs_create_file(struct kobject *kobj,
130 				   const struct attribute *attr);
131 int __must_check sysfs_create_files(struct kobject *kobj,
132 				   const struct attribute **attr);
133 int __must_check sysfs_chmod_file(struct kobject *kobj,
134 				  const struct attribute *attr, mode_t mode);
135 void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
136 void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
137 
138 int __must_check sysfs_create_bin_file(struct kobject *kobj,
139 				       const struct bin_attribute *attr);
140 void sysfs_remove_bin_file(struct kobject *kobj,
141 			   const struct bin_attribute *attr);
142 
143 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
144 				   const char *name);
145 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
146 					  struct kobject *target,
147 					  const char *name);
148 void sysfs_remove_link(struct kobject *kobj, const char *name);
149 
150 int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
151 			const char *old_name, const char *new_name);
152 
153 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
154 			const char *name);
155 
156 int __must_check sysfs_create_group(struct kobject *kobj,
157 				    const struct attribute_group *grp);
158 int sysfs_update_group(struct kobject *kobj,
159 		       const struct attribute_group *grp);
160 void sysfs_remove_group(struct kobject *kobj,
161 			const struct attribute_group *grp);
162 int sysfs_add_file_to_group(struct kobject *kobj,
163 			const struct attribute *attr, const char *group);
164 void sysfs_remove_file_from_group(struct kobject *kobj,
165 			const struct attribute *attr, const char *group);
166 
167 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
168 void sysfs_notify_dirent(struct sysfs_dirent *sd);
169 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
170 				      const void *ns,
171 				      const unsigned char *name);
172 struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
173 void sysfs_put(struct sysfs_dirent *sd);
174 void sysfs_printk_last_file(void);
175 
176 /* Called to clear a ns tag when it is no longer valid */
177 void sysfs_exit_ns(enum kobj_ns_type type, const void *tag);
178 
179 int __must_check sysfs_init(void);
180 
181 #else /* CONFIG_SYSFS */
182 
183 static inline int sysfs_schedule_callback(struct kobject *kobj,
184 		void (*func)(void *), void *data, struct module *owner)
185 {
186 	return -ENOSYS;
187 }
188 
189 static inline int sysfs_create_dir(struct kobject *kobj)
190 {
191 	return 0;
192 }
193 
194 static inline void sysfs_remove_dir(struct kobject *kobj)
195 {
196 }
197 
198 static inline int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
199 {
200 	return 0;
201 }
202 
203 static inline int sysfs_move_dir(struct kobject *kobj,
204 				 struct kobject *new_parent_kobj)
205 {
206 	return 0;
207 }
208 
209 static inline int sysfs_create_file(struct kobject *kobj,
210 				    const struct attribute *attr)
211 {
212 	return 0;
213 }
214 
215 static inline int sysfs_create_files(struct kobject *kobj,
216 				    const struct attribute **attr)
217 {
218 	return 0;
219 }
220 
221 static inline int sysfs_chmod_file(struct kobject *kobj,
222 				   const struct attribute *attr, mode_t mode)
223 {
224 	return 0;
225 }
226 
227 static inline void sysfs_remove_file(struct kobject *kobj,
228 				     const struct attribute *attr)
229 {
230 }
231 
232 static inline void sysfs_remove_files(struct kobject *kobj,
233 				     const struct attribute **attr)
234 {
235 }
236 
237 static inline int sysfs_create_bin_file(struct kobject *kobj,
238 					const struct bin_attribute *attr)
239 {
240 	return 0;
241 }
242 
243 static inline void sysfs_remove_bin_file(struct kobject *kobj,
244 					 const struct bin_attribute *attr)
245 {
246 }
247 
248 static inline int sysfs_create_link(struct kobject *kobj,
249 				    struct kobject *target, const char *name)
250 {
251 	return 0;
252 }
253 
254 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
255 					   struct kobject *target,
256 					   const char *name)
257 {
258 	return 0;
259 }
260 
261 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
262 {
263 }
264 
265 static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
266 				    const char *old_name, const char *new_name)
267 {
268 	return 0;
269 }
270 
271 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
272 				     const char *name)
273 {
274 }
275 
276 static inline int sysfs_create_group(struct kobject *kobj,
277 				     const struct attribute_group *grp)
278 {
279 	return 0;
280 }
281 
282 static inline int sysfs_update_group(struct kobject *kobj,
283 				const struct attribute_group *grp)
284 {
285 	return 0;
286 }
287 
288 static inline void sysfs_remove_group(struct kobject *kobj,
289 				      const struct attribute_group *grp)
290 {
291 }
292 
293 static inline int sysfs_add_file_to_group(struct kobject *kobj,
294 		const struct attribute *attr, const char *group)
295 {
296 	return 0;
297 }
298 
299 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
300 		const struct attribute *attr, const char *group)
301 {
302 }
303 
304 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
305 				const char *attr)
306 {
307 }
308 static inline void sysfs_notify_dirent(struct sysfs_dirent *sd)
309 {
310 }
311 static inline
312 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
313 				      const void *ns,
314 				      const unsigned char *name)
315 {
316 	return NULL;
317 }
318 static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
319 {
320 	return NULL;
321 }
322 static inline void sysfs_put(struct sysfs_dirent *sd)
323 {
324 }
325 
326 static inline void sysfs_exit_ns(int type, const void *tag)
327 {
328 }
329 
330 static inline int __must_check sysfs_init(void)
331 {
332 	return 0;
333 }
334 
335 static inline void sysfs_printk_last_file(void)
336 {
337 }
338 
339 #endif /* CONFIG_SYSFS */
340 
341 #endif /* _SYSFS_H_ */
342