]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/sysfs.h
Merge branches 'for-4.4/upstream-fixes', 'for-4.5/async-suspend', 'for-4.5/container...
[mirror_ubuntu-artful-kernel.git] / include / linux / sysfs.h
CommitLineData
1da177e4
LT
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.
6d66f5cd
TH
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
1da177e4
LT
8 *
9 * Please see Documentation/filesystems/sysfs.txt for more information.
10 */
11
12#ifndef _SYSFS_H_
13#define _SYSFS_H_
14
b8441ed2 15#include <linux/kernfs.h>
4a7fb636 16#include <linux/compiler.h>
5851fadc 17#include <linux/errno.h>
bf0acc33 18#include <linux/list.h>
6992f533 19#include <linux/lockdep.h>
8488a38f 20#include <linux/kobject_ns.h>
3493f69f 21#include <linux/stat.h>
60063497 22#include <linux/atomic.h>
1da177e4
LT
23
24struct kobject;
25struct module;
6ab9cea1 26struct bin_attribute;
3ff195b0 27enum kobj_ns_type;
1da177e4
LT
28
29struct attribute {
59f69015 30 const char *name;
9104e427 31 umode_t mode;
6992f533 32#ifdef CONFIG_DEBUG_LOCK_ALLOC
356c05d5 33 bool ignore_lockdep:1;
6992f533
EB
34 struct lock_class_key *key;
35 struct lock_class_key skey;
36#endif
1da177e4
LT
37};
38
35960258
EB
39/**
40 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
41 * @attr: struct attribute to initialize
42 *
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.
48 */
6992f533
EB
49#ifdef CONFIG_DEBUG_LOCK_ALLOC
50#define sysfs_attr_init(attr) \
51do { \
52 static struct lock_class_key __key; \
53 \
54 (attr)->key = &__key; \
5da5c9c8 55} while (0)
6992f533 56#else
5da5c9c8 57#define sysfs_attr_init(attr) do {} while (0)
6992f533
EB
58#endif
59
ba61af6f
GR
60/**
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
7f5028cf
EL
67 * non-binary attribute in the group. Only read/write
68 * permissions as well as SYSFS_PREALLOC are accepted. Must
69 * return 0 if an attribute is not visible. The returned value
70 * will replace static permissions defined in struct attribute.
71 * @is_bin_visible:
72 * Optional: Function to return permissions associated with a
73 * binary attribute of the group. Will be called repeatedly
74 * for each binary attribute in the group. Only read/write
75 * permissions as well as SYSFS_PREALLOC are accepted. Must
76 * return 0 if a binary attribute is not visible. The returned
77 * value will replace static permissions defined in
78 * struct bin_attribute.
ba61af6f
GR
79 * @attrs: Pointer to NULL terminated list of attributes.
80 * @bin_attrs: Pointer to NULL terminated list of binary attributes.
81 * Either attrs or bin_attrs or both must be provided.
82 */
1da177e4 83struct attribute_group {
59f69015 84 const char *name;
587a1f16 85 umode_t (*is_visible)(struct kobject *,
d4acd722 86 struct attribute *, int);
7f5028cf
EL
87 umode_t (*is_bin_visible)(struct kobject *,
88 struct bin_attribute *, int);
59f69015 89 struct attribute **attrs;
6ab9cea1 90 struct bin_attribute **bin_attrs;
1da177e4
LT
91};
92
1da177e4
LT
93/**
94 * Use these macros to make defining attributes easier. See include/linux/device.h
95 * for examples..
96 */
97
2b75869b
N
98#define SYSFS_PREALLOC 010000
99
5da5c9c8 100#define __ATTR(_name, _mode, _show, _store) { \
58f86cc8
RR
101 .attr = {.name = __stringify(_name), \
102 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
aa01aa3c
OS
103 .show = _show, \
104 .store = _store, \
1da177e4
LT
105}
106
2b75869b
N
107#define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
108 .attr = {.name = __stringify(_name), \
109 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
110 .show = _show, \
111 .store = _store, \
112}
113
aa01aa3c
OS
114#define __ATTR_RO(_name) { \
115 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
116 .show = _name##_show, \
1da177e4
LT
117}
118
a65fcce7
GKH
119#define __ATTR_WO(_name) { \
120 .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
121 .store = _name##_store, \
122}
123
aa01aa3c
OS
124#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
125 _name##_show, _name##_store)
b9b32597 126
1da177e4 127#define __ATTR_NULL { .attr = { .name = NULL } }
356c05d5
AS
128
129#ifdef CONFIG_DEBUG_LOCK_ALLOC
130#define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
131 .attr = {.name = __stringify(_name), .mode = _mode, \
132 .ignore_lockdep = true }, \
133 .show = _show, \
134 .store = _store, \
135}
136#else
137#define __ATTR_IGNORE_LOCKDEP __ATTR
138#endif
1da177e4 139
3493f69f
OS
140#define __ATTRIBUTE_GROUPS(_name) \
141static const struct attribute_group *_name##_groups[] = { \
142 &_name##_group, \
f2f37f58
GKH
143 NULL, \
144}
145
3493f69f
OS
146#define ATTRIBUTE_GROUPS(_name) \
147static const struct attribute_group _name##_group = { \
148 .attrs = _name##_attrs, \
149}; \
150__ATTRIBUTE_GROUPS(_name)
151
2c3c8bea 152struct file;
1da177e4
LT
153struct vm_area_struct;
154
155struct bin_attribute {
156 struct attribute attr;
157 size_t size;
158 void *private;
2c3c8bea 159 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
91a69029 160 char *, loff_t, size_t);
5da5c9c8 161 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
91a69029 162 char *, loff_t, size_t);
2c3c8bea 163 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
1da177e4
LT
164 struct vm_area_struct *vma);
165};
166
35960258
EB
167/**
168 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
169 * @attr: struct bin_attribute to initialize
170 *
171 * Initialize a dynamically allocated struct bin_attribute so we
172 * can make lockdep happy. This is a new requirement for
173 * attributes and initially this is only needed when lockdep is
174 * enabled. Lockdep gives a nice error when your attribute is
175 * added to sysfs if you don't have this.
176 */
62e877b8 177#define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
6992f533 178
3493f69f
OS
179/* macros to create static binary attributes easier */
180#define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
181 .attr = { .name = __stringify(_name), .mode = _mode }, \
182 .read = _read, \
183 .write = _write, \
184 .size = _size, \
185}
186
187#define __BIN_ATTR_RO(_name, _size) { \
188 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
189 .read = _name##_read, \
190 .size = _size, \
e4b63603
GKH
191}
192
3493f69f
OS
193#define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \
194 (S_IWUSR | S_IRUGO), _name##_read, \
68531526 195 _name##_write, _size)
3493f69f
OS
196
197#define __BIN_ATTR_NULL __ATTR_NULL
198
199#define BIN_ATTR(_name, _mode, _read, _write, _size) \
200struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
201 _write, _size)
202
203#define BIN_ATTR_RO(_name, _size) \
204struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
205
206#define BIN_ATTR_RW(_name, _size) \
207struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
208
1da177e4 209struct sysfs_ops {
5da5c9c8
GKH
210 ssize_t (*show)(struct kobject *, struct attribute *, char *);
211 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
1da177e4
LT
212};
213
1da177e4
LT
214#ifdef CONFIG_SYSFS
215
e34ff490 216int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
59f69015 217void sysfs_remove_dir(struct kobject *kobj);
e34ff490
TH
218int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
219 const void *new_ns);
220int __must_check sysfs_move_dir_ns(struct kobject *kobj,
221 struct kobject *new_parent_kobj,
222 const void *new_ns);
87d2846f
EB
223int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
224 const char *name);
225void sysfs_remove_mount_point(struct kobject *parent_kobj,
226 const char *name);
31e5abe9 227
58292cbe
TH
228int __must_check sysfs_create_file_ns(struct kobject *kobj,
229 const struct attribute *attr,
230 const void *ns);
1c205ae1
AK
231int __must_check sysfs_create_files(struct kobject *kobj,
232 const struct attribute **attr);
49c19400 233int __must_check sysfs_chmod_file(struct kobject *kobj,
48176a97 234 const struct attribute *attr, umode_t mode);
58292cbe
TH
235void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
236 const void *ns);
6b0afc2a 237bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
1c205ae1 238void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
1da177e4 239
4a7fb636 240int __must_check sysfs_create_bin_file(struct kobject *kobj,
66ecb92b
PC
241 const struct bin_attribute *attr);
242void sysfs_remove_bin_file(struct kobject *kobj,
243 const struct bin_attribute *attr);
1da177e4 244
59f69015
TH
245int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
246 const char *name);
36ce6dad
CH
247int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
248 struct kobject *target,
249 const char *name);
59f69015
TH
250void sysfs_remove_link(struct kobject *kobj, const char *name);
251
4b30ee58
TH
252int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
253 const char *old_name, const char *new_name,
254 const void *new_ns);
7cb32942 255
746edb7a
EB
256void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
257 const char *name);
258
59f69015
TH
259int __must_check sysfs_create_group(struct kobject *kobj,
260 const struct attribute_group *grp);
3e9b2bae
GKH
261int __must_check sysfs_create_groups(struct kobject *kobj,
262 const struct attribute_group **groups);
0f423895
JB
263int sysfs_update_group(struct kobject *kobj,
264 const struct attribute_group *grp);
59f69015
TH
265void sysfs_remove_group(struct kobject *kobj,
266 const struct attribute_group *grp);
3e9b2bae
GKH
267void sysfs_remove_groups(struct kobject *kobj,
268 const struct attribute_group **groups);
dfa87c82 269int sysfs_add_file_to_group(struct kobject *kobj,
59f69015 270 const struct attribute *attr, const char *group);
dfa87c82 271void sysfs_remove_file_from_group(struct kobject *kobj,
59f69015 272 const struct attribute *attr, const char *group);
69d44ffb
AS
273int sysfs_merge_group(struct kobject *kobj,
274 const struct attribute_group *grp);
275void sysfs_unmerge_group(struct kobject *kobj,
276 const struct attribute_group *grp);
0bb8f3d6
RW
277int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
278 struct kobject *target, const char *link_name);
279void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
280 const char *link_name);
37c1c04c
JS
281int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
282 struct kobject *target_kobj,
283 const char *target_name);
dfa87c82 284
8c0e3998 285void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
3ff195b0 286
f1282c84 287int __must_check sysfs_init(void);
f20a9ead 288
fa4cd451
TH
289static inline void sysfs_enable_ns(struct kernfs_node *kn)
290{
291 return kernfs_enable_ns(kn);
292}
293
1da177e4
LT
294#else /* CONFIG_SYSFS */
295
e34ff490 296static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
1da177e4
LT
297{
298 return 0;
299}
300
59f69015 301static inline void sysfs_remove_dir(struct kobject *kobj)
1da177e4 302{
1da177e4
LT
303}
304
e34ff490
TH
305static inline int sysfs_rename_dir_ns(struct kobject *kobj,
306 const char *new_name, const void *new_ns)
1da177e4 307{
0b4a4fea 308 return 0;
1da177e4
LT
309}
310
e34ff490
TH
311static inline int sysfs_move_dir_ns(struct kobject *kobj,
312 struct kobject *new_parent_kobj,
313 const void *new_ns)
8a82472f
CH
314{
315 return 0;
316}
317
87d2846f
EB
318static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
319 const char *name)
320{
321 return 0;
322}
323
324static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
325 const char *name)
326{
327}
328
58292cbe
TH
329static inline int sysfs_create_file_ns(struct kobject *kobj,
330 const struct attribute *attr,
331 const void *ns)
1da177e4
LT
332{
333 return 0;
334}
335
1c205ae1
AK
336static inline int sysfs_create_files(struct kobject *kobj,
337 const struct attribute **attr)
338{
339 return 0;
340}
341
59f69015 342static inline int sysfs_chmod_file(struct kobject *kobj,
48176a97 343 const struct attribute *attr, umode_t mode)
31e5abe9
KS
344{
345 return 0;
346}
1da177e4 347
58292cbe
TH
348static inline void sysfs_remove_file_ns(struct kobject *kobj,
349 const struct attribute *attr,
350 const void *ns)
1da177e4 351{
1da177e4
LT
352}
353
6b0afc2a
TH
354static inline bool sysfs_remove_file_self(struct kobject *kobj,
355 const struct attribute *attr)
356{
357 return false;
358}
359
1c205ae1
AK
360static inline void sysfs_remove_files(struct kobject *kobj,
361 const struct attribute **attr)
362{
363}
364
59f69015 365static inline int sysfs_create_bin_file(struct kobject *kobj,
66ecb92b 366 const struct bin_attribute *attr)
1da177e4
LT
367{
368 return 0;
369}
370
3612e06b 371static inline void sysfs_remove_bin_file(struct kobject *kobj,
66ecb92b 372 const struct bin_attribute *attr)
1da177e4 373{
1da177e4
LT
374}
375
59f69015
TH
376static inline int sysfs_create_link(struct kobject *kobj,
377 struct kobject *target, const char *name)
1da177e4
LT
378{
379 return 0;
380}
381
36ce6dad
CH
382static inline int sysfs_create_link_nowarn(struct kobject *kobj,
383 struct kobject *target,
384 const char *name)
385{
386 return 0;
387}
388
59f69015 389static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
1da177e4 390{
1da177e4
LT
391}
392
4b30ee58
TH
393static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
394 const char *old_name,
395 const char *new_name, const void *ns)
7cb32942
EB
396{
397 return 0;
398}
399
746edb7a
EB
400static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
401 const char *name)
402{
403}
404
59f69015
TH
405static inline int sysfs_create_group(struct kobject *kobj,
406 const struct attribute_group *grp)
1da177e4
LT
407{
408 return 0;
409}
410
f7998780
GKH
411static inline int sysfs_create_groups(struct kobject *kobj,
412 const struct attribute_group **groups)
413{
574979c6 414 return 0;
f7998780
GKH
415}
416
1cbfb7a5
RD
417static inline int sysfs_update_group(struct kobject *kobj,
418 const struct attribute_group *grp)
419{
420 return 0;
421}
422
59f69015
TH
423static inline void sysfs_remove_group(struct kobject *kobj,
424 const struct attribute_group *grp)
1da177e4 425{
1da177e4
LT
426}
427
f7998780
GKH
428static inline void sysfs_remove_groups(struct kobject *kobj,
429 const struct attribute_group **groups)
430{
431}
432
dfa87c82
AS
433static inline int sysfs_add_file_to_group(struct kobject *kobj,
434 const struct attribute *attr, const char *group)
435{
436 return 0;
437}
438
439static inline void sysfs_remove_file_from_group(struct kobject *kobj,
d701d8a3 440 const struct attribute *attr, const char *group)
dfa87c82 441{
dfa87c82
AS
442}
443
69d44ffb
AS
444static inline int sysfs_merge_group(struct kobject *kobj,
445 const struct attribute_group *grp)
446{
447 return 0;
448}
449
450static inline void sysfs_unmerge_group(struct kobject *kobj,
451 const struct attribute_group *grp)
452{
453}
454
0bb8f3d6
RW
455static inline int sysfs_add_link_to_group(struct kobject *kobj,
456 const char *group_name, struct kobject *target,
457 const char *link_name)
458{
459 return 0;
460}
461
462static inline void sysfs_remove_link_from_group(struct kobject *kobj,
463 const char *group_name, const char *link_name)
464{
465}
466
37c1c04c
JS
467static inline int __compat_only_sysfs_link_entry_to_kobj(
468 struct kobject *kobj,
469 struct kobject *target_kobj,
470 const char *target_name)
471{
472 return 0;
473}
474
8c0e3998
TP
475static inline void sysfs_notify(struct kobject *kobj, const char *dir,
476 const char *attr)
4508a7a7
N
477{
478}
479
f20a9ead
AM
480static inline int __must_check sysfs_init(void)
481{
482 return 0;
483}
484
fa4cd451
TH
485static inline void sysfs_enable_ns(struct kernfs_node *kn)
486{
487}
488
1da177e4
LT
489#endif /* CONFIG_SYSFS */
490
58292cbe
TH
491static inline int __must_check sysfs_create_file(struct kobject *kobj,
492 const struct attribute *attr)
493{
494 return sysfs_create_file_ns(kobj, attr, NULL);
495}
496
497static inline void sysfs_remove_file(struct kobject *kobj,
498 const struct attribute *attr)
499{
78e1da62 500 sysfs_remove_file_ns(kobj, attr, NULL);
58292cbe
TH
501}
502
4b30ee58
TH
503static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
504 const char *old_name, const char *new_name)
505{
506 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
507}
508
324a56e1 509static inline void sysfs_notify_dirent(struct kernfs_node *kn)
ccf73cf3 510{
324a56e1 511 kernfs_notify(kn);
ccf73cf3
TH
512}
513
324a56e1
TH
514static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
515 const unsigned char *name)
388975cc 516{
324a56e1 517 return kernfs_find_and_get(parent, name);
388975cc
TH
518}
519
324a56e1 520static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
024f6471 521{
324a56e1
TH
522 kernfs_get(kn);
523 return kn;
ccf73cf3
TH
524}
525
324a56e1 526static inline void sysfs_put(struct kernfs_node *kn)
ccf73cf3 527{
324a56e1 528 kernfs_put(kn);
024f6471
TH
529}
530
1da177e4 531#endif /* _SYSFS_H_ */