sysfs.h 13 KB

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