sysfs.h 15 KB

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