2 * sysfs.h - definitions for the device driver filesystem
4 * Copyright (c) 2001,2002 Patrick Mochel
5 * Copyright (c) 2004 Silicon Graphics, Inc.
6 * Copyright (c) 2007 SUSE Linux Products GmbH
9 * Please see Documentation/filesystems/sysfs.txt for more information.
15 #include <linux/kernfs.h>
16 #include <linux/compiler.h>
17 #include <linux/errno.h>
18 #include <linux/list.h>
19 #include <linux/lockdep.h>
20 #include <linux/kobject_ns.h>
21 #include <linux/stat.h>
22 #include <linux/atomic.h>
32 #ifdef CONFIG_DEBUG_LOCK_ALLOC
33 bool ignore_lockdep:1;
34 struct lock_class_key *key;
35 struct lock_class_key skey;
40 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
41 * @attr: struct attribute to initialize
43 * Initialize a dynamically allocated struct attribute so we can
44 * make lockdep happy. This is a new requirement for attributes
45 * and initially this is only needed when lockdep is enabled.
46 * Lockdep gives a nice error when your attribute is added to
47 * sysfs if you don't have this.
49 #ifdef CONFIG_DEBUG_LOCK_ALLOC
50 #define sysfs_attr_init(attr) \
52 static struct lock_class_key __key; \
54 (attr)->key = &__key; \
57 #define sysfs_attr_init(attr) do {} while (0)
61 * struct attribute_group - data structure used to declare an attribute group.
62 * @name: Optional: Attribute group name
63 * If specified, the attribute group will be created in
64 * a new subdirectory with this name.
65 * @is_visible: Optional: Function to return permissions associated with an
66 * attribute of the group. Will be called repeatedly for each
67 * attribute in the group. Only read/write permissions as well as
68 * SYSFS_PREALLOC are accepted. Must return 0 if an attribute is
69 * not visible. The returned value will replace static permissions
70 * defined in struct attribute or struct bin_attribute.
71 * @attrs: Pointer to NULL terminated list of attributes.
72 * @bin_attrs: Pointer to NULL terminated list of binary attributes.
73 * Either attrs or bin_attrs or both must be provided.
75 struct attribute_group {
77 umode_t (*is_visible)(struct kobject *,
78 struct attribute *, int);
79 struct attribute **attrs;
80 struct bin_attribute **bin_attrs;
84 * Use these macros to make defining attributes easier. See include/linux/device.h
88 #define SYSFS_PREALLOC 010000
90 #define __ATTR(_name, _mode, _show, _store) { \
91 .attr = {.name = __stringify(_name), \
92 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
97 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
98 .attr = {.name = __stringify(_name), \
99 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
104 #define __ATTR_RO(_name) { \
105 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
106 .show = _name##_show, \
109 #define __ATTR_WO(_name) { \
110 .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
111 .store = _name##_store, \
114 #define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
115 _name##_show, _name##_store)
117 #define __ATTR_NULL { .attr = { .name = NULL } }
119 #ifdef CONFIG_DEBUG_LOCK_ALLOC
120 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
121 .attr = {.name = __stringify(_name), .mode = _mode, \
122 .ignore_lockdep = true }, \
127 #define __ATTR_IGNORE_LOCKDEP __ATTR
130 #define __ATTRIBUTE_GROUPS(_name) \
131 static const struct attribute_group *_name##_groups[] = { \
136 #define ATTRIBUTE_GROUPS(_name) \
137 static const struct attribute_group _name##_group = { \
138 .attrs = _name##_attrs, \
140 __ATTRIBUTE_GROUPS(_name)
143 struct vm_area_struct;
145 struct bin_attribute {
146 struct attribute attr;
149 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
150 char *, loff_t, size_t);
151 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
152 char *, loff_t, size_t);
153 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
154 struct vm_area_struct *vma);
158 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
159 * @attr: struct bin_attribute to initialize
161 * Initialize a dynamically allocated struct bin_attribute so we
162 * can make lockdep happy. This is a new requirement for
163 * attributes and initially this is only needed when lockdep is
164 * enabled. Lockdep gives a nice error when your attribute is
165 * added to sysfs if you don't have this.
167 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
169 /* macros to create static binary attributes easier */
170 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
171 .attr = { .name = __stringify(_name), .mode = _mode }, \
177 #define __BIN_ATTR_RO(_name, _size) { \
178 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
179 .read = _name##_read, \
183 #define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \
184 (S_IWUSR | S_IRUGO), _name##_read, \
185 _name##_write, _size)
187 #define __BIN_ATTR_NULL __ATTR_NULL
189 #define BIN_ATTR(_name, _mode, _read, _write, _size) \
190 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
193 #define BIN_ATTR_RO(_name, _size) \
194 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
196 #define BIN_ATTR_RW(_name, _size) \
197 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
200 ssize_t (*show)(struct kobject *, struct attribute *, char *);
201 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
206 int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
207 void sysfs_remove_dir(struct kobject *kobj);
208 int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
210 int __must_check sysfs_move_dir_ns(struct kobject *kobj,
211 struct kobject *new_parent_kobj,
214 int __must_check sysfs_create_file_ns(struct kobject *kobj,
215 const struct attribute *attr,
217 int __must_check sysfs_create_files(struct kobject *kobj,
218 const struct attribute **attr);
219 int __must_check sysfs_chmod_file(struct kobject *kobj,
220 const struct attribute *attr, umode_t mode);
221 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
223 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
224 void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
226 int __must_check sysfs_create_bin_file(struct kobject *kobj,
227 const struct bin_attribute *attr);
228 void sysfs_remove_bin_file(struct kobject *kobj,
229 const struct bin_attribute *attr);
231 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
233 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
234 struct kobject *target,
236 void sysfs_remove_link(struct kobject *kobj, const char *name);
238 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
239 const char *old_name, const char *new_name,
242 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
245 int __must_check sysfs_create_group(struct kobject *kobj,
246 const struct attribute_group *grp);
247 int __must_check sysfs_create_groups(struct kobject *kobj,
248 const struct attribute_group **groups);
249 int sysfs_update_group(struct kobject *kobj,
250 const struct attribute_group *grp);
251 void sysfs_remove_group(struct kobject *kobj,
252 const struct attribute_group *grp);
253 void sysfs_remove_groups(struct kobject *kobj,
254 const struct attribute_group **groups);
255 int sysfs_add_file_to_group(struct kobject *kobj,
256 const struct attribute *attr, const char *group);
257 void sysfs_remove_file_from_group(struct kobject *kobj,
258 const struct attribute *attr, const char *group);
259 int sysfs_merge_group(struct kobject *kobj,
260 const struct attribute_group *grp);
261 void sysfs_unmerge_group(struct kobject *kobj,
262 const struct attribute_group *grp);
263 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
264 struct kobject *target, const char *link_name);
265 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
266 const char *link_name);
268 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
270 int __must_check sysfs_init(void);
272 static inline void sysfs_enable_ns(struct kernfs_node *kn)
274 return kernfs_enable_ns(kn);
277 #else /* CONFIG_SYSFS */
279 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
284 static inline void sysfs_remove_dir(struct kobject *kobj)
288 static inline int sysfs_rename_dir_ns(struct kobject *kobj,
289 const char *new_name, const void *new_ns)
294 static inline int sysfs_move_dir_ns(struct kobject *kobj,
295 struct kobject *new_parent_kobj,
301 static inline int sysfs_create_file_ns(struct kobject *kobj,
302 const struct attribute *attr,
308 static inline int sysfs_create_files(struct kobject *kobj,
309 const struct attribute **attr)
314 static inline int sysfs_chmod_file(struct kobject *kobj,
315 const struct attribute *attr, umode_t mode)
320 static inline void sysfs_remove_file_ns(struct kobject *kobj,
321 const struct attribute *attr,
326 static inline bool sysfs_remove_file_self(struct kobject *kobj,
327 const struct attribute *attr)
332 static inline void sysfs_remove_files(struct kobject *kobj,
333 const struct attribute **attr)
337 static inline int sysfs_create_bin_file(struct kobject *kobj,
338 const struct bin_attribute *attr)
343 static inline void sysfs_remove_bin_file(struct kobject *kobj,
344 const struct bin_attribute *attr)
348 static inline int sysfs_create_link(struct kobject *kobj,
349 struct kobject *target, const char *name)
354 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
355 struct kobject *target,
361 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
365 static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
366 const char *old_name,
367 const char *new_name, const void *ns)
372 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
377 static inline int sysfs_create_group(struct kobject *kobj,
378 const struct attribute_group *grp)
383 static inline int sysfs_create_groups(struct kobject *kobj,
384 const struct attribute_group **groups)
389 static inline int sysfs_update_group(struct kobject *kobj,
390 const struct attribute_group *grp)
395 static inline void sysfs_remove_group(struct kobject *kobj,
396 const struct attribute_group *grp)
400 static inline void sysfs_remove_groups(struct kobject *kobj,
401 const struct attribute_group **groups)
405 static inline int sysfs_add_file_to_group(struct kobject *kobj,
406 const struct attribute *attr, const char *group)
411 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
412 const struct attribute *attr, const char *group)
416 static inline int sysfs_merge_group(struct kobject *kobj,
417 const struct attribute_group *grp)
422 static inline void sysfs_unmerge_group(struct kobject *kobj,
423 const struct attribute_group *grp)
427 static inline int sysfs_add_link_to_group(struct kobject *kobj,
428 const char *group_name, struct kobject *target,
429 const char *link_name)
434 static inline void sysfs_remove_link_from_group(struct kobject *kobj,
435 const char *group_name, const char *link_name)
439 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
444 static inline int __must_check sysfs_init(void)
449 static inline void sysfs_enable_ns(struct kernfs_node *kn)
453 #endif /* CONFIG_SYSFS */
455 static inline int __must_check sysfs_create_file(struct kobject *kobj,
456 const struct attribute *attr)
458 return sysfs_create_file_ns(kobj, attr, NULL);
461 static inline void sysfs_remove_file(struct kobject *kobj,
462 const struct attribute *attr)
464 sysfs_remove_file_ns(kobj, attr, NULL);
467 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
468 const char *old_name, const char *new_name)
470 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
473 static inline void sysfs_notify_dirent(struct kernfs_node *kn)
478 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
479 const unsigned char *name)
481 return kernfs_find_and_get(parent, name);
484 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
490 static inline void sysfs_put(struct kernfs_node *kn)
495 #endif /* _SYSFS_H_ */