sysfs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. struct attribute_group {
  55. const char *name;
  56. umode_t (*is_visible)(struct kobject *,
  57. struct attribute *, int);
  58. struct attribute **attrs;
  59. struct bin_attribute **bin_attrs;
  60. };
  61. /**
  62. * Use these macros to make defining attributes easier. See include/linux/device.h
  63. * for examples..
  64. */
  65. #define __ATTR(_name, _mode, _show, _store) { \
  66. .attr = {.name = __stringify(_name), .mode = _mode }, \
  67. .show = _show, \
  68. .store = _store, \
  69. }
  70. #define __ATTR_RO(_name) { \
  71. .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
  72. .show = _name##_show, \
  73. }
  74. #define __ATTR_WO(_name) { \
  75. .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
  76. .store = _name##_store, \
  77. }
  78. #define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
  79. _name##_show, _name##_store)
  80. #define __ATTR_NULL { .attr = { .name = NULL } }
  81. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  82. #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
  83. .attr = {.name = __stringify(_name), .mode = _mode, \
  84. .ignore_lockdep = true }, \
  85. .show = _show, \
  86. .store = _store, \
  87. }
  88. #else
  89. #define __ATTR_IGNORE_LOCKDEP __ATTR
  90. #endif
  91. #define __ATTRIBUTE_GROUPS(_name) \
  92. static const struct attribute_group *_name##_groups[] = { \
  93. &_name##_group, \
  94. NULL, \
  95. }
  96. #define ATTRIBUTE_GROUPS(_name) \
  97. static const struct attribute_group _name##_group = { \
  98. .attrs = _name##_attrs, \
  99. }; \
  100. __ATTRIBUTE_GROUPS(_name)
  101. struct file;
  102. struct vm_area_struct;
  103. struct bin_attribute {
  104. struct attribute attr;
  105. size_t size;
  106. void *private;
  107. ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
  108. char *, loff_t, size_t);
  109. ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
  110. char *, loff_t, size_t);
  111. int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
  112. struct vm_area_struct *vma);
  113. };
  114. /**
  115. * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
  116. * @attr: struct bin_attribute to initialize
  117. *
  118. * Initialize a dynamically allocated struct bin_attribute so we
  119. * can make lockdep happy. This is a new requirement for
  120. * attributes and initially this is only needed when lockdep is
  121. * enabled. Lockdep gives a nice error when your attribute is
  122. * added to sysfs if you don't have this.
  123. */
  124. #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
  125. /* macros to create static binary attributes easier */
  126. #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
  127. .attr = { .name = __stringify(_name), .mode = _mode }, \
  128. .read = _read, \
  129. .write = _write, \
  130. .size = _size, \
  131. }
  132. #define __BIN_ATTR_RO(_name, _size) { \
  133. .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
  134. .read = _name##_read, \
  135. .size = _size, \
  136. }
  137. #define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \
  138. (S_IWUSR | S_IRUGO), _name##_read, \
  139. _name##_write, _size)
  140. #define __BIN_ATTR_NULL __ATTR_NULL
  141. #define BIN_ATTR(_name, _mode, _read, _write, _size) \
  142. struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
  143. _write, _size)
  144. #define BIN_ATTR_RO(_name, _size) \
  145. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
  146. #define BIN_ATTR_RW(_name, _size) \
  147. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
  148. struct sysfs_ops {
  149. ssize_t (*show)(struct kobject *, struct attribute *, char *);
  150. ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
  151. };
  152. #ifdef CONFIG_SYSFS
  153. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  154. void *data, struct module *owner);
  155. int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
  156. void sysfs_remove_dir(struct kobject *kobj);
  157. int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  158. const void *new_ns);
  159. int __must_check sysfs_move_dir_ns(struct kobject *kobj,
  160. struct kobject *new_parent_kobj,
  161. const void *new_ns);
  162. int __must_check sysfs_create_file_ns(struct kobject *kobj,
  163. const struct attribute *attr,
  164. const void *ns);
  165. int __must_check sysfs_create_files(struct kobject *kobj,
  166. const struct attribute **attr);
  167. int __must_check sysfs_chmod_file(struct kobject *kobj,
  168. const struct attribute *attr, umode_t mode);
  169. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  170. const void *ns);
  171. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
  172. void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
  173. int __must_check sysfs_create_bin_file(struct kobject *kobj,
  174. const struct bin_attribute *attr);
  175. void sysfs_remove_bin_file(struct kobject *kobj,
  176. const struct bin_attribute *attr);
  177. int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
  178. const char *name);
  179. int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
  180. struct kobject *target,
  181. const char *name);
  182. void sysfs_remove_link(struct kobject *kobj, const char *name);
  183. int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
  184. const char *old_name, const char *new_name,
  185. const void *new_ns);
  186. void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
  187. const char *name);
  188. int __must_check sysfs_create_group(struct kobject *kobj,
  189. const struct attribute_group *grp);
  190. int __must_check sysfs_create_groups(struct kobject *kobj,
  191. const struct attribute_group **groups);
  192. int sysfs_update_group(struct kobject *kobj,
  193. const struct attribute_group *grp);
  194. void sysfs_remove_group(struct kobject *kobj,
  195. const struct attribute_group *grp);
  196. void sysfs_remove_groups(struct kobject *kobj,
  197. const struct attribute_group **groups);
  198. int sysfs_add_file_to_group(struct kobject *kobj,
  199. const struct attribute *attr, const char *group);
  200. void sysfs_remove_file_from_group(struct kobject *kobj,
  201. const struct attribute *attr, const char *group);
  202. int sysfs_merge_group(struct kobject *kobj,
  203. const struct attribute_group *grp);
  204. void sysfs_unmerge_group(struct kobject *kobj,
  205. const struct attribute_group *grp);
  206. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  207. struct kobject *target, const char *link_name);
  208. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  209. const char *link_name);
  210. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
  211. int __must_check sysfs_init(void);
  212. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  213. {
  214. return kernfs_enable_ns(kn);
  215. }
  216. #else /* CONFIG_SYSFS */
  217. static inline int sysfs_schedule_callback(struct kobject *kobj,
  218. void (*func)(void *), void *data, struct module *owner)
  219. {
  220. return -ENOSYS;
  221. }
  222. static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  223. {
  224. return 0;
  225. }
  226. static inline void sysfs_remove_dir(struct kobject *kobj)
  227. {
  228. }
  229. static inline int sysfs_rename_dir_ns(struct kobject *kobj,
  230. const char *new_name, const void *new_ns)
  231. {
  232. return 0;
  233. }
  234. static inline int sysfs_move_dir_ns(struct kobject *kobj,
  235. struct kobject *new_parent_kobj,
  236. const void *new_ns)
  237. {
  238. return 0;
  239. }
  240. static inline int sysfs_create_file_ns(struct kobject *kobj,
  241. const struct attribute *attr,
  242. const void *ns)
  243. {
  244. return 0;
  245. }
  246. static inline int sysfs_create_files(struct kobject *kobj,
  247. const struct attribute **attr)
  248. {
  249. return 0;
  250. }
  251. static inline int sysfs_chmod_file(struct kobject *kobj,
  252. const struct attribute *attr, umode_t mode)
  253. {
  254. return 0;
  255. }
  256. static inline void sysfs_remove_file_ns(struct kobject *kobj,
  257. const struct attribute *attr,
  258. const void *ns)
  259. {
  260. }
  261. static inline bool sysfs_remove_file_self(struct kobject *kobj,
  262. const struct attribute *attr)
  263. {
  264. return false;
  265. }
  266. static inline void sysfs_remove_files(struct kobject *kobj,
  267. const struct attribute **attr)
  268. {
  269. }
  270. static inline int sysfs_create_bin_file(struct kobject *kobj,
  271. const struct bin_attribute *attr)
  272. {
  273. return 0;
  274. }
  275. static inline void sysfs_remove_bin_file(struct kobject *kobj,
  276. const struct bin_attribute *attr)
  277. {
  278. }
  279. static inline int sysfs_create_link(struct kobject *kobj,
  280. struct kobject *target, const char *name)
  281. {
  282. return 0;
  283. }
  284. static inline int sysfs_create_link_nowarn(struct kobject *kobj,
  285. struct kobject *target,
  286. const char *name)
  287. {
  288. return 0;
  289. }
  290. static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
  291. {
  292. }
  293. static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
  294. const char *old_name,
  295. const char *new_name, const void *ns)
  296. {
  297. return 0;
  298. }
  299. static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
  300. const char *name)
  301. {
  302. }
  303. static inline int sysfs_create_group(struct kobject *kobj,
  304. const struct attribute_group *grp)
  305. {
  306. return 0;
  307. }
  308. static inline int sysfs_create_groups(struct kobject *kobj,
  309. const struct attribute_group **groups)
  310. {
  311. return 0;
  312. }
  313. static inline int sysfs_update_group(struct kobject *kobj,
  314. const struct attribute_group *grp)
  315. {
  316. return 0;
  317. }
  318. static inline void sysfs_remove_group(struct kobject *kobj,
  319. const struct attribute_group *grp)
  320. {
  321. }
  322. static inline void sysfs_remove_groups(struct kobject *kobj,
  323. const struct attribute_group **groups)
  324. {
  325. }
  326. static inline int sysfs_add_file_to_group(struct kobject *kobj,
  327. const struct attribute *attr, const char *group)
  328. {
  329. return 0;
  330. }
  331. static inline void sysfs_remove_file_from_group(struct kobject *kobj,
  332. const struct attribute *attr, const char *group)
  333. {
  334. }
  335. static inline int sysfs_merge_group(struct kobject *kobj,
  336. const struct attribute_group *grp)
  337. {
  338. return 0;
  339. }
  340. static inline void sysfs_unmerge_group(struct kobject *kobj,
  341. const struct attribute_group *grp)
  342. {
  343. }
  344. static inline int sysfs_add_link_to_group(struct kobject *kobj,
  345. const char *group_name, struct kobject *target,
  346. const char *link_name)
  347. {
  348. return 0;
  349. }
  350. static inline void sysfs_remove_link_from_group(struct kobject *kobj,
  351. const char *group_name, const char *link_name)
  352. {
  353. }
  354. static inline void sysfs_notify(struct kobject *kobj, const char *dir,
  355. const char *attr)
  356. {
  357. }
  358. static inline int __must_check sysfs_init(void)
  359. {
  360. return 0;
  361. }
  362. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  363. {
  364. }
  365. #endif /* CONFIG_SYSFS */
  366. static inline int __must_check sysfs_create_file(struct kobject *kobj,
  367. const struct attribute *attr)
  368. {
  369. return sysfs_create_file_ns(kobj, attr, NULL);
  370. }
  371. static inline void sysfs_remove_file(struct kobject *kobj,
  372. const struct attribute *attr)
  373. {
  374. return sysfs_remove_file_ns(kobj, attr, NULL);
  375. }
  376. static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
  377. const char *old_name, const char *new_name)
  378. {
  379. return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
  380. }
  381. static inline void sysfs_notify_dirent(struct kernfs_node *kn)
  382. {
  383. kernfs_notify(kn);
  384. }
  385. static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
  386. const unsigned char *name)
  387. {
  388. return kernfs_find_and_get(parent, name);
  389. }
  390. static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
  391. {
  392. kernfs_get(kn);
  393. return kn;
  394. }
  395. static inline void sysfs_put(struct kernfs_node *kn)
  396. {
  397. kernfs_put(kn);
  398. }
  399. #endif /* _SYSFS_H_ */