sysfs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <teheo@suse.de>
  8. *
  9. * Please see Documentation/filesystems/sysfs.txt for more information.
  10. */
  11. #ifndef _SYSFS_H_
  12. #define _SYSFS_H_
  13. #include <linux/kernfs.h>
  14. #include <linux/compiler.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/lockdep.h>
  18. #include <linux/kobject_ns.h>
  19. #include <linux/stat.h>
  20. #include <linux/atomic.h>
  21. struct kobject;
  22. struct module;
  23. struct bin_attribute;
  24. enum kobj_ns_type;
  25. struct attribute {
  26. const char *name;
  27. umode_t mode;
  28. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  29. bool ignore_lockdep:1;
  30. struct lock_class_key *key;
  31. struct lock_class_key skey;
  32. #endif
  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 - data structure used to declare an attribute group.
  56. * @name: Optional: Attribute group name
  57. * If specified, the attribute group will be created in
  58. * a new subdirectory with this name.
  59. * @is_visible: Optional: Function to return permissions associated with an
  60. * attribute of the group. Will be called repeatedly for each
  61. * non-binary attribute in the group. Only read/write
  62. * permissions as well as SYSFS_PREALLOC are accepted. Must
  63. * return 0 if an attribute is not visible. The returned value
  64. * will replace static permissions defined in struct attribute.
  65. * @is_bin_visible:
  66. * Optional: Function to return permissions associated with a
  67. * binary attribute of the group. Will be called repeatedly
  68. * for each binary attribute in the group. Only read/write
  69. * permissions as well as SYSFS_PREALLOC are accepted. Must
  70. * return 0 if a binary attribute is not visible. The returned
  71. * value will replace static permissions defined in
  72. * struct bin_attribute.
  73. * @attrs: Pointer to NULL terminated list of attributes.
  74. * @bin_attrs: Pointer to NULL terminated list of binary attributes.
  75. * Either attrs or bin_attrs or both must be provided.
  76. */
  77. struct attribute_group {
  78. const char *name;
  79. umode_t (*is_visible)(struct kobject *,
  80. struct attribute *, int);
  81. umode_t (*is_bin_visible)(struct kobject *,
  82. struct bin_attribute *, int);
  83. struct attribute **attrs;
  84. struct bin_attribute **bin_attrs;
  85. };
  86. /**
  87. * Use these macros to make defining attributes easier. See include/linux/device.h
  88. * for examples..
  89. */
  90. #define SYSFS_PREALLOC 010000
  91. #define __ATTR(_name, _mode, _show, _store) { \
  92. .attr = {.name = __stringify(_name), \
  93. .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
  94. .show = _show, \
  95. .store = _store, \
  96. }
  97. #define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
  98. .attr = {.name = __stringify(_name), \
  99. .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
  100. .show = _show, \
  101. .store = _store, \
  102. }
  103. #define __ATTR_RO(_name) { \
  104. .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
  105. .show = _name##_show, \
  106. }
  107. #define __ATTR_WO(_name) { \
  108. .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
  109. .store = _name##_store, \
  110. }
  111. #define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
  112. _name##_show, _name##_store)
  113. #define __ATTR_NULL { .attr = { .name = NULL } }
  114. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  115. #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
  116. .attr = {.name = __stringify(_name), .mode = _mode, \
  117. .ignore_lockdep = true }, \
  118. .show = _show, \
  119. .store = _store, \
  120. }
  121. #else
  122. #define __ATTR_IGNORE_LOCKDEP __ATTR
  123. #endif
  124. #define __ATTRIBUTE_GROUPS(_name) \
  125. static const struct attribute_group *_name##_groups[] = { \
  126. &_name##_group, \
  127. NULL, \
  128. }
  129. #define ATTRIBUTE_GROUPS(_name) \
  130. static const struct attribute_group _name##_group = { \
  131. .attrs = _name##_attrs, \
  132. }; \
  133. __ATTRIBUTE_GROUPS(_name)
  134. struct file;
  135. struct vm_area_struct;
  136. struct bin_attribute {
  137. struct attribute attr;
  138. size_t size;
  139. void *private;
  140. ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
  141. char *, loff_t, size_t);
  142. ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
  143. char *, loff_t, size_t);
  144. int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
  145. struct vm_area_struct *vma);
  146. };
  147. /**
  148. * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
  149. * @attr: struct bin_attribute to initialize
  150. *
  151. * Initialize a dynamically allocated struct bin_attribute so we
  152. * can make lockdep happy. This is a new requirement for
  153. * attributes and initially this is only needed when lockdep is
  154. * enabled. Lockdep gives a nice error when your attribute is
  155. * added to sysfs if you don't have this.
  156. */
  157. #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
  158. /* macros to create static binary attributes easier */
  159. #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
  160. .attr = { .name = __stringify(_name), .mode = _mode }, \
  161. .read = _read, \
  162. .write = _write, \
  163. .size = _size, \
  164. }
  165. #define __BIN_ATTR_RO(_name, _size) { \
  166. .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
  167. .read = _name##_read, \
  168. .size = _size, \
  169. }
  170. #define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \
  171. (S_IWUSR | S_IRUGO), _name##_read, \
  172. _name##_write, _size)
  173. #define __BIN_ATTR_NULL __ATTR_NULL
  174. #define BIN_ATTR(_name, _mode, _read, _write, _size) \
  175. struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
  176. _write, _size)
  177. #define BIN_ATTR_RO(_name, _size) \
  178. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
  179. #define BIN_ATTR_RW(_name, _size) \
  180. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
  181. struct sysfs_ops {
  182. ssize_t (*show)(struct kobject *, struct attribute *, char *);
  183. ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
  184. };
  185. #ifdef CONFIG_SYSFS
  186. int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
  187. void sysfs_remove_dir(struct kobject *kobj);
  188. int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  189. const void *new_ns);
  190. int __must_check sysfs_move_dir_ns(struct kobject *kobj,
  191. struct kobject *new_parent_kobj,
  192. const void *new_ns);
  193. int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
  194. const char *name);
  195. void sysfs_remove_mount_point(struct kobject *parent_kobj,
  196. const char *name);
  197. int __must_check sysfs_create_file_ns(struct kobject *kobj,
  198. const struct attribute *attr,
  199. const void *ns);
  200. int __must_check sysfs_create_files(struct kobject *kobj,
  201. const struct attribute **attr);
  202. int __must_check sysfs_chmod_file(struct kobject *kobj,
  203. const struct attribute *attr, umode_t mode);
  204. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  205. const void *ns);
  206. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
  207. void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
  208. int __must_check sysfs_create_bin_file(struct kobject *kobj,
  209. const struct bin_attribute *attr);
  210. void sysfs_remove_bin_file(struct kobject *kobj,
  211. const struct bin_attribute *attr);
  212. int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
  213. const char *name);
  214. int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
  215. struct kobject *target,
  216. const char *name);
  217. void sysfs_remove_link(struct kobject *kobj, const char *name);
  218. int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
  219. const char *old_name, const char *new_name,
  220. const void *new_ns);
  221. void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
  222. const char *name);
  223. int __must_check sysfs_create_group(struct kobject *kobj,
  224. const struct attribute_group *grp);
  225. int __must_check sysfs_create_groups(struct kobject *kobj,
  226. const struct attribute_group **groups);
  227. int sysfs_update_group(struct kobject *kobj,
  228. const struct attribute_group *grp);
  229. void sysfs_remove_group(struct kobject *kobj,
  230. const struct attribute_group *grp);
  231. void sysfs_remove_groups(struct kobject *kobj,
  232. const struct attribute_group **groups);
  233. int sysfs_add_file_to_group(struct kobject *kobj,
  234. const struct attribute *attr, const char *group);
  235. void sysfs_remove_file_from_group(struct kobject *kobj,
  236. const struct attribute *attr, const char *group);
  237. int sysfs_merge_group(struct kobject *kobj,
  238. const struct attribute_group *grp);
  239. void sysfs_unmerge_group(struct kobject *kobj,
  240. const struct attribute_group *grp);
  241. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  242. struct kobject *target, const char *link_name);
  243. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  244. const char *link_name);
  245. int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
  246. struct kobject *target_kobj,
  247. const char *target_name);
  248. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
  249. int __must_check sysfs_init(void);
  250. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  251. {
  252. return kernfs_enable_ns(kn);
  253. }
  254. #else /* CONFIG_SYSFS */
  255. static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  256. {
  257. return 0;
  258. }
  259. static inline void sysfs_remove_dir(struct kobject *kobj)
  260. {
  261. }
  262. static inline int sysfs_rename_dir_ns(struct kobject *kobj,
  263. const char *new_name, const void *new_ns)
  264. {
  265. return 0;
  266. }
  267. static inline int sysfs_move_dir_ns(struct kobject *kobj,
  268. struct kobject *new_parent_kobj,
  269. const void *new_ns)
  270. {
  271. return 0;
  272. }
  273. static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
  274. const char *name)
  275. {
  276. return 0;
  277. }
  278. static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
  279. const char *name)
  280. {
  281. }
  282. static inline int sysfs_create_file_ns(struct kobject *kobj,
  283. const struct attribute *attr,
  284. const void *ns)
  285. {
  286. return 0;
  287. }
  288. static inline int sysfs_create_files(struct kobject *kobj,
  289. const struct attribute **attr)
  290. {
  291. return 0;
  292. }
  293. static inline int sysfs_chmod_file(struct kobject *kobj,
  294. const struct attribute *attr, umode_t mode)
  295. {
  296. return 0;
  297. }
  298. static inline void sysfs_remove_file_ns(struct kobject *kobj,
  299. const struct attribute *attr,
  300. const void *ns)
  301. {
  302. }
  303. static inline bool sysfs_remove_file_self(struct kobject *kobj,
  304. const struct attribute *attr)
  305. {
  306. return false;
  307. }
  308. static inline void sysfs_remove_files(struct kobject *kobj,
  309. const struct attribute **attr)
  310. {
  311. }
  312. static inline int sysfs_create_bin_file(struct kobject *kobj,
  313. const struct bin_attribute *attr)
  314. {
  315. return 0;
  316. }
  317. static inline void sysfs_remove_bin_file(struct kobject *kobj,
  318. const struct bin_attribute *attr)
  319. {
  320. }
  321. static inline int sysfs_create_link(struct kobject *kobj,
  322. struct kobject *target, const char *name)
  323. {
  324. return 0;
  325. }
  326. static inline int sysfs_create_link_nowarn(struct kobject *kobj,
  327. struct kobject *target,
  328. const char *name)
  329. {
  330. return 0;
  331. }
  332. static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
  333. {
  334. }
  335. static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
  336. const char *old_name,
  337. const char *new_name, const void *ns)
  338. {
  339. return 0;
  340. }
  341. static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
  342. const char *name)
  343. {
  344. }
  345. static inline int sysfs_create_group(struct kobject *kobj,
  346. const struct attribute_group *grp)
  347. {
  348. return 0;
  349. }
  350. static inline int sysfs_create_groups(struct kobject *kobj,
  351. const struct attribute_group **groups)
  352. {
  353. return 0;
  354. }
  355. static inline int sysfs_update_group(struct kobject *kobj,
  356. const struct attribute_group *grp)
  357. {
  358. return 0;
  359. }
  360. static inline void sysfs_remove_group(struct kobject *kobj,
  361. const struct attribute_group *grp)
  362. {
  363. }
  364. static inline void sysfs_remove_groups(struct kobject *kobj,
  365. const struct attribute_group **groups)
  366. {
  367. }
  368. static inline int sysfs_add_file_to_group(struct kobject *kobj,
  369. const struct attribute *attr, const char *group)
  370. {
  371. return 0;
  372. }
  373. static inline void sysfs_remove_file_from_group(struct kobject *kobj,
  374. const struct attribute *attr, const char *group)
  375. {
  376. }
  377. static inline int sysfs_merge_group(struct kobject *kobj,
  378. const struct attribute_group *grp)
  379. {
  380. return 0;
  381. }
  382. static inline void sysfs_unmerge_group(struct kobject *kobj,
  383. const struct attribute_group *grp)
  384. {
  385. }
  386. static inline int sysfs_add_link_to_group(struct kobject *kobj,
  387. const char *group_name, struct kobject *target,
  388. const char *link_name)
  389. {
  390. return 0;
  391. }
  392. static inline void sysfs_remove_link_from_group(struct kobject *kobj,
  393. const char *group_name, const char *link_name)
  394. {
  395. }
  396. static inline int __compat_only_sysfs_link_entry_to_kobj(
  397. struct kobject *kobj,
  398. struct kobject *target_kobj,
  399. const char *target_name)
  400. {
  401. return 0;
  402. }
  403. static inline void sysfs_notify(struct kobject *kobj, const char *dir,
  404. const char *attr)
  405. {
  406. }
  407. static inline int __must_check sysfs_init(void)
  408. {
  409. return 0;
  410. }
  411. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  412. {
  413. }
  414. #endif /* CONFIG_SYSFS */
  415. static inline int __must_check sysfs_create_file(struct kobject *kobj,
  416. const struct attribute *attr)
  417. {
  418. return sysfs_create_file_ns(kobj, attr, NULL);
  419. }
  420. static inline void sysfs_remove_file(struct kobject *kobj,
  421. const struct attribute *attr)
  422. {
  423. sysfs_remove_file_ns(kobj, attr, NULL);
  424. }
  425. static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
  426. const char *old_name, const char *new_name)
  427. {
  428. return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
  429. }
  430. static inline void sysfs_notify_dirent(struct kernfs_node *kn)
  431. {
  432. kernfs_notify(kn);
  433. }
  434. static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
  435. const unsigned char *name)
  436. {
  437. return kernfs_find_and_get(parent, name);
  438. }
  439. static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
  440. {
  441. kernfs_get(kn);
  442. return kn;
  443. }
  444. static inline void sysfs_put(struct kernfs_node *kn)
  445. {
  446. kernfs_put(kn);
  447. }
  448. #endif /* _SYSFS_H_ */