0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef _SYSFS_H_
0014 #define _SYSFS_H_
0015
0016 #include <linux/kernfs.h>
0017 #include <linux/compiler.h>
0018 #include <linux/errno.h>
0019 #include <linux/list.h>
0020 #include <linux/lockdep.h>
0021 #include <linux/kobject_ns.h>
0022 #include <linux/stat.h>
0023 #include <linux/atomic.h>
0024
0025 struct kobject;
0026 struct module;
0027 struct bin_attribute;
0028 enum kobj_ns_type;
0029
0030 struct attribute {
0031 const char *name;
0032 umode_t mode;
0033 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0034 bool ignore_lockdep:1;
0035 struct lock_class_key *key;
0036 struct lock_class_key skey;
0037 #endif
0038 };
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0051 #define sysfs_attr_init(attr) \
0052 do { \
0053 static struct lock_class_key __key; \
0054 \
0055 (attr)->key = &__key; \
0056 } while (0)
0057 #else
0058 #define sysfs_attr_init(attr) do {} while (0)
0059 #endif
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 struct attribute_group {
0085 const char *name;
0086 umode_t (*is_visible)(struct kobject *,
0087 struct attribute *, int);
0088 umode_t (*is_bin_visible)(struct kobject *,
0089 struct bin_attribute *, int);
0090 struct attribute **attrs;
0091 struct bin_attribute **bin_attrs;
0092 };
0093
0094
0095
0096
0097
0098
0099 #define SYSFS_PREALLOC 010000
0100
0101 #define __ATTR(_name, _mode, _show, _store) { \
0102 .attr = {.name = __stringify(_name), \
0103 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
0104 .show = _show, \
0105 .store = _store, \
0106 }
0107
0108 #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
0109 .attr = {.name = __stringify(_name), \
0110 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
0111 .show = _show, \
0112 .store = _store, \
0113 }
0114
0115 #define __ATTR_RO(_name) { \
0116 .attr = { .name = __stringify(_name), .mode = 0444 }, \
0117 .show = _name##_show, \
0118 }
0119
0120 #define __ATTR_RO_MODE(_name, _mode) { \
0121 .attr = { .name = __stringify(_name), \
0122 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
0123 .show = _name##_show, \
0124 }
0125
0126 #define __ATTR_RW_MODE(_name, _mode) { \
0127 .attr = { .name = __stringify(_name), \
0128 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
0129 .show = _name##_show, \
0130 .store = _name##_store, \
0131 }
0132
0133 #define __ATTR_WO(_name) { \
0134 .attr = { .name = __stringify(_name), .mode = 0200 }, \
0135 .store = _name##_store, \
0136 }
0137
0138 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
0139
0140 #define __ATTR_NULL { .attr = { .name = NULL } }
0141
0142 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0143 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
0144 .attr = {.name = __stringify(_name), .mode = _mode, \
0145 .ignore_lockdep = true }, \
0146 .show = _show, \
0147 .store = _store, \
0148 }
0149 #else
0150 #define __ATTR_IGNORE_LOCKDEP __ATTR
0151 #endif
0152
0153 #define __ATTRIBUTE_GROUPS(_name) \
0154 static const struct attribute_group *_name##_groups[] = { \
0155 &_name##_group, \
0156 NULL, \
0157 }
0158
0159 #define ATTRIBUTE_GROUPS(_name) \
0160 static const struct attribute_group _name##_group = { \
0161 .attrs = _name##_attrs, \
0162 }; \
0163 __ATTRIBUTE_GROUPS(_name)
0164
0165 #define BIN_ATTRIBUTE_GROUPS(_name) \
0166 static const struct attribute_group _name##_group = { \
0167 .bin_attrs = _name##_attrs, \
0168 }; \
0169 __ATTRIBUTE_GROUPS(_name)
0170
0171 struct file;
0172 struct vm_area_struct;
0173 struct address_space;
0174
0175 struct bin_attribute {
0176 struct attribute attr;
0177 size_t size;
0178 void *private;
0179 struct address_space *(*f_mapping)(void);
0180 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
0181 char *, loff_t, size_t);
0182 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
0183 char *, loff_t, size_t);
0184 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
0185 struct vm_area_struct *vma);
0186 };
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
0199
0200
0201 #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
0202 .attr = { .name = __stringify(_name), .mode = _mode }, \
0203 .read = _read, \
0204 .write = _write, \
0205 .size = _size, \
0206 }
0207
0208 #define __BIN_ATTR_RO(_name, _size) { \
0209 .attr = { .name = __stringify(_name), .mode = 0444 }, \
0210 .read = _name##_read, \
0211 .size = _size, \
0212 }
0213
0214 #define __BIN_ATTR_WO(_name, _size) { \
0215 .attr = { .name = __stringify(_name), .mode = 0200 }, \
0216 .write = _name##_write, \
0217 .size = _size, \
0218 }
0219
0220 #define __BIN_ATTR_RW(_name, _size) \
0221 __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size)
0222
0223 #define __BIN_ATTR_NULL __ATTR_NULL
0224
0225 #define BIN_ATTR(_name, _mode, _read, _write, _size) \
0226 struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
0227 _write, _size)
0228
0229 #define BIN_ATTR_RO(_name, _size) \
0230 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
0231
0232 #define BIN_ATTR_WO(_name, _size) \
0233 struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
0234
0235 #define BIN_ATTR_RW(_name, _size) \
0236 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
0237
0238
0239 #define __BIN_ATTR_ADMIN_RO(_name, _size) { \
0240 .attr = { .name = __stringify(_name), .mode = 0400 }, \
0241 .read = _name##_read, \
0242 .size = _size, \
0243 }
0244
0245 #define __BIN_ATTR_ADMIN_RW(_name, _size) \
0246 __BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
0247
0248 #define BIN_ATTR_ADMIN_RO(_name, _size) \
0249 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
0250
0251 #define BIN_ATTR_ADMIN_RW(_name, _size) \
0252 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
0253
0254 struct sysfs_ops {
0255 ssize_t (*show)(struct kobject *, struct attribute *, char *);
0256 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
0257 };
0258
0259 #ifdef CONFIG_SYSFS
0260
0261 int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
0262 void sysfs_remove_dir(struct kobject *kobj);
0263 int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
0264 const void *new_ns);
0265 int __must_check sysfs_move_dir_ns(struct kobject *kobj,
0266 struct kobject *new_parent_kobj,
0267 const void *new_ns);
0268 int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
0269 const char *name);
0270 void sysfs_remove_mount_point(struct kobject *parent_kobj,
0271 const char *name);
0272
0273 int __must_check sysfs_create_file_ns(struct kobject *kobj,
0274 const struct attribute *attr,
0275 const void *ns);
0276 int __must_check sysfs_create_files(struct kobject *kobj,
0277 const struct attribute * const *attr);
0278 int __must_check sysfs_chmod_file(struct kobject *kobj,
0279 const struct attribute *attr, umode_t mode);
0280 struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
0281 const struct attribute *attr);
0282 void sysfs_unbreak_active_protection(struct kernfs_node *kn);
0283 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
0284 const void *ns);
0285 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
0286 void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attr);
0287
0288 int __must_check sysfs_create_bin_file(struct kobject *kobj,
0289 const struct bin_attribute *attr);
0290 void sysfs_remove_bin_file(struct kobject *kobj,
0291 const struct bin_attribute *attr);
0292
0293 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
0294 const char *name);
0295 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
0296 struct kobject *target,
0297 const char *name);
0298 void sysfs_remove_link(struct kobject *kobj, const char *name);
0299
0300 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
0301 const char *old_name, const char *new_name,
0302 const void *new_ns);
0303
0304 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
0305 const char *name);
0306
0307 int __must_check sysfs_create_group(struct kobject *kobj,
0308 const struct attribute_group *grp);
0309 int __must_check sysfs_create_groups(struct kobject *kobj,
0310 const struct attribute_group **groups);
0311 int __must_check sysfs_update_groups(struct kobject *kobj,
0312 const struct attribute_group **groups);
0313 int sysfs_update_group(struct kobject *kobj,
0314 const struct attribute_group *grp);
0315 void sysfs_remove_group(struct kobject *kobj,
0316 const struct attribute_group *grp);
0317 void sysfs_remove_groups(struct kobject *kobj,
0318 const struct attribute_group **groups);
0319 int sysfs_add_file_to_group(struct kobject *kobj,
0320 const struct attribute *attr, const char *group);
0321 void sysfs_remove_file_from_group(struct kobject *kobj,
0322 const struct attribute *attr, const char *group);
0323 int sysfs_merge_group(struct kobject *kobj,
0324 const struct attribute_group *grp);
0325 void sysfs_unmerge_group(struct kobject *kobj,
0326 const struct attribute_group *grp);
0327 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
0328 struct kobject *target, const char *link_name);
0329 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
0330 const char *link_name);
0331 int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
0332 struct kobject *target_kobj,
0333 const char *target_name,
0334 const char *symlink_name);
0335
0336 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
0337
0338 int __must_check sysfs_init(void);
0339
0340 static inline void sysfs_enable_ns(struct kernfs_node *kn)
0341 {
0342 return kernfs_enable_ns(kn);
0343 }
0344
0345 int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
0346 kgid_t kgid);
0347 int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid);
0348 int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
0349 const char *name, kuid_t kuid, kgid_t kgid);
0350 int sysfs_groups_change_owner(struct kobject *kobj,
0351 const struct attribute_group **groups,
0352 kuid_t kuid, kgid_t kgid);
0353 int sysfs_group_change_owner(struct kobject *kobj,
0354 const struct attribute_group *groups, kuid_t kuid,
0355 kgid_t kgid);
0356 __printf(2, 3)
0357 int sysfs_emit(char *buf, const char *fmt, ...);
0358 __printf(3, 4)
0359 int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
0360
0361 #else
0362
0363 static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
0364 {
0365 return 0;
0366 }
0367
0368 static inline void sysfs_remove_dir(struct kobject *kobj)
0369 {
0370 }
0371
0372 static inline int sysfs_rename_dir_ns(struct kobject *kobj,
0373 const char *new_name, const void *new_ns)
0374 {
0375 return 0;
0376 }
0377
0378 static inline int sysfs_move_dir_ns(struct kobject *kobj,
0379 struct kobject *new_parent_kobj,
0380 const void *new_ns)
0381 {
0382 return 0;
0383 }
0384
0385 static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
0386 const char *name)
0387 {
0388 return 0;
0389 }
0390
0391 static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
0392 const char *name)
0393 {
0394 }
0395
0396 static inline int sysfs_create_file_ns(struct kobject *kobj,
0397 const struct attribute *attr,
0398 const void *ns)
0399 {
0400 return 0;
0401 }
0402
0403 static inline int sysfs_create_files(struct kobject *kobj,
0404 const struct attribute * const *attr)
0405 {
0406 return 0;
0407 }
0408
0409 static inline int sysfs_chmod_file(struct kobject *kobj,
0410 const struct attribute *attr, umode_t mode)
0411 {
0412 return 0;
0413 }
0414
0415 static inline struct kernfs_node *
0416 sysfs_break_active_protection(struct kobject *kobj,
0417 const struct attribute *attr)
0418 {
0419 return NULL;
0420 }
0421
0422 static inline void sysfs_unbreak_active_protection(struct kernfs_node *kn)
0423 {
0424 }
0425
0426 static inline void sysfs_remove_file_ns(struct kobject *kobj,
0427 const struct attribute *attr,
0428 const void *ns)
0429 {
0430 }
0431
0432 static inline bool sysfs_remove_file_self(struct kobject *kobj,
0433 const struct attribute *attr)
0434 {
0435 return false;
0436 }
0437
0438 static inline void sysfs_remove_files(struct kobject *kobj,
0439 const struct attribute * const *attr)
0440 {
0441 }
0442
0443 static inline int sysfs_create_bin_file(struct kobject *kobj,
0444 const struct bin_attribute *attr)
0445 {
0446 return 0;
0447 }
0448
0449 static inline void sysfs_remove_bin_file(struct kobject *kobj,
0450 const struct bin_attribute *attr)
0451 {
0452 }
0453
0454 static inline int sysfs_create_link(struct kobject *kobj,
0455 struct kobject *target, const char *name)
0456 {
0457 return 0;
0458 }
0459
0460 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
0461 struct kobject *target,
0462 const char *name)
0463 {
0464 return 0;
0465 }
0466
0467 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
0468 {
0469 }
0470
0471 static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
0472 const char *old_name,
0473 const char *new_name, const void *ns)
0474 {
0475 return 0;
0476 }
0477
0478 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
0479 const char *name)
0480 {
0481 }
0482
0483 static inline int sysfs_create_group(struct kobject *kobj,
0484 const struct attribute_group *grp)
0485 {
0486 return 0;
0487 }
0488
0489 static inline int sysfs_create_groups(struct kobject *kobj,
0490 const struct attribute_group **groups)
0491 {
0492 return 0;
0493 }
0494
0495 static inline int sysfs_update_groups(struct kobject *kobj,
0496 const struct attribute_group **groups)
0497 {
0498 return 0;
0499 }
0500
0501 static inline int sysfs_update_group(struct kobject *kobj,
0502 const struct attribute_group *grp)
0503 {
0504 return 0;
0505 }
0506
0507 static inline void sysfs_remove_group(struct kobject *kobj,
0508 const struct attribute_group *grp)
0509 {
0510 }
0511
0512 static inline void sysfs_remove_groups(struct kobject *kobj,
0513 const struct attribute_group **groups)
0514 {
0515 }
0516
0517 static inline int sysfs_add_file_to_group(struct kobject *kobj,
0518 const struct attribute *attr, const char *group)
0519 {
0520 return 0;
0521 }
0522
0523 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
0524 const struct attribute *attr, const char *group)
0525 {
0526 }
0527
0528 static inline int sysfs_merge_group(struct kobject *kobj,
0529 const struct attribute_group *grp)
0530 {
0531 return 0;
0532 }
0533
0534 static inline void sysfs_unmerge_group(struct kobject *kobj,
0535 const struct attribute_group *grp)
0536 {
0537 }
0538
0539 static inline int sysfs_add_link_to_group(struct kobject *kobj,
0540 const char *group_name, struct kobject *target,
0541 const char *link_name)
0542 {
0543 return 0;
0544 }
0545
0546 static inline void sysfs_remove_link_from_group(struct kobject *kobj,
0547 const char *group_name, const char *link_name)
0548 {
0549 }
0550
0551 static inline int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
0552 struct kobject *target_kobj,
0553 const char *target_name,
0554 const char *symlink_name)
0555 {
0556 return 0;
0557 }
0558
0559 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
0560 const char *attr)
0561 {
0562 }
0563
0564 static inline int __must_check sysfs_init(void)
0565 {
0566 return 0;
0567 }
0568
0569 static inline void sysfs_enable_ns(struct kernfs_node *kn)
0570 {
0571 }
0572
0573 static inline int sysfs_file_change_owner(struct kobject *kobj,
0574 const char *name, kuid_t kuid,
0575 kgid_t kgid)
0576 {
0577 return 0;
0578 }
0579
0580 static inline int sysfs_link_change_owner(struct kobject *kobj,
0581 struct kobject *targ,
0582 const char *name, kuid_t kuid,
0583 kgid_t kgid)
0584 {
0585 return 0;
0586 }
0587
0588 static inline int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
0589 {
0590 return 0;
0591 }
0592
0593 static inline int sysfs_groups_change_owner(struct kobject *kobj,
0594 const struct attribute_group **groups,
0595 kuid_t kuid, kgid_t kgid)
0596 {
0597 return 0;
0598 }
0599
0600 static inline int sysfs_group_change_owner(struct kobject *kobj,
0601 const struct attribute_group *groups,
0602 kuid_t kuid, kgid_t kgid)
0603 {
0604 return 0;
0605 }
0606
0607 __printf(2, 3)
0608 static inline int sysfs_emit(char *buf, const char *fmt, ...)
0609 {
0610 return 0;
0611 }
0612
0613 __printf(3, 4)
0614 static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
0615 {
0616 return 0;
0617 }
0618 #endif
0619
0620 static inline int __must_check sysfs_create_file(struct kobject *kobj,
0621 const struct attribute *attr)
0622 {
0623 return sysfs_create_file_ns(kobj, attr, NULL);
0624 }
0625
0626 static inline void sysfs_remove_file(struct kobject *kobj,
0627 const struct attribute *attr)
0628 {
0629 sysfs_remove_file_ns(kobj, attr, NULL);
0630 }
0631
0632 static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
0633 const char *old_name, const char *new_name)
0634 {
0635 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
0636 }
0637
0638 static inline void sysfs_notify_dirent(struct kernfs_node *kn)
0639 {
0640 kernfs_notify(kn);
0641 }
0642
0643 static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
0644 const char *name)
0645 {
0646 return kernfs_find_and_get(parent, name);
0647 }
0648
0649 static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
0650 {
0651 kernfs_get(kn);
0652 return kn;
0653 }
0654
0655 static inline void sysfs_put(struct kernfs_node *kn)
0656 {
0657 kernfs_put(kn);
0658 }
0659
0660 #endif