sysfs.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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.rst 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_RW_MODE(_name, _mode) { \
  114. .attr = { .name = __stringify(_name), \
  115. .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
  116. .show = _name##_show, \
  117. .store = _name##_store, \
  118. }
  119. #define __ATTR_WO(_name) { \
  120. .attr = { .name = __stringify(_name), .mode = 0200 }, \
  121. .store = _name##_store, \
  122. }
  123. #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
  124. #define __ATTR_NULL { .attr = { .name = NULL } }
  125. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  126. #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
  127. .attr = {.name = __stringify(_name), .mode = _mode, \
  128. .ignore_lockdep = true }, \
  129. .show = _show, \
  130. .store = _store, \
  131. }
  132. #else
  133. #define __ATTR_IGNORE_LOCKDEP __ATTR
  134. #endif
  135. #define __ATTRIBUTE_GROUPS(_name) \
  136. static const struct attribute_group *_name##_groups[] = { \
  137. &_name##_group, \
  138. NULL, \
  139. }
  140. #define ATTRIBUTE_GROUPS(_name) \
  141. static const struct attribute_group _name##_group = { \
  142. .attrs = _name##_attrs, \
  143. }; \
  144. __ATTRIBUTE_GROUPS(_name)
  145. struct file;
  146. struct vm_area_struct;
  147. struct bin_attribute {
  148. struct attribute attr;
  149. size_t size;
  150. void *private;
  151. ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
  152. char *, loff_t, size_t);
  153. ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
  154. char *, loff_t, size_t);
  155. int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
  156. struct vm_area_struct *vma);
  157. };
  158. /**
  159. * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
  160. * @attr: struct bin_attribute to initialize
  161. *
  162. * Initialize a dynamically allocated struct bin_attribute so we
  163. * can make lockdep happy. This is a new requirement for
  164. * attributes and initially this is only needed when lockdep is
  165. * enabled. Lockdep gives a nice error when your attribute is
  166. * added to sysfs if you don't have this.
  167. */
  168. #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
  169. /* macros to create static binary attributes easier */
  170. #define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
  171. .attr = { .name = __stringify(_name), .mode = _mode }, \
  172. .read = _read, \
  173. .write = _write, \
  174. .size = _size, \
  175. }
  176. #define __BIN_ATTR_RO(_name, _size) { \
  177. .attr = { .name = __stringify(_name), .mode = 0444 }, \
  178. .read = _name##_read, \
  179. .size = _size, \
  180. }
  181. #define __BIN_ATTR_WO(_name, _size) { \
  182. .attr = { .name = __stringify(_name), .mode = 0200 }, \
  183. .write = _name##_write, \
  184. .size = _size, \
  185. }
  186. #define __BIN_ATTR_RW(_name, _size) \
  187. __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size)
  188. #define __BIN_ATTR_NULL __ATTR_NULL
  189. #define BIN_ATTR(_name, _mode, _read, _write, _size) \
  190. struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
  191. _write, _size)
  192. #define BIN_ATTR_RO(_name, _size) \
  193. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
  194. #define BIN_ATTR_WO(_name, _size) \
  195. struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
  196. #define BIN_ATTR_RW(_name, _size) \
  197. struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
  198. struct sysfs_ops {
  199. ssize_t (*show)(struct kobject *, struct attribute *, char *);
  200. ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
  201. };
  202. #ifdef CONFIG_SYSFS
  203. int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
  204. void sysfs_remove_dir(struct kobject *kobj);
  205. int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
  206. const void *new_ns);
  207. int __must_check sysfs_move_dir_ns(struct kobject *kobj,
  208. struct kobject *new_parent_kobj,
  209. const void *new_ns);
  210. int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
  211. const char *name);
  212. void sysfs_remove_mount_point(struct kobject *parent_kobj,
  213. const char *name);
  214. int __must_check sysfs_create_file_ns(struct kobject *kobj,
  215. const struct attribute *attr,
  216. const void *ns);
  217. int __must_check sysfs_create_files(struct kobject *kobj,
  218. const struct attribute * const *attr);
  219. int __must_check sysfs_chmod_file(struct kobject *kobj,
  220. const struct attribute *attr, umode_t mode);
  221. struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
  222. const struct attribute *attr);
  223. void sysfs_unbreak_active_protection(struct kernfs_node *kn);
  224. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  225. const void *ns);
  226. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
  227. void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *attr);
  228. int __must_check sysfs_create_bin_file(struct kobject *kobj,
  229. const struct bin_attribute *attr);
  230. void sysfs_remove_bin_file(struct kobject *kobj,
  231. const struct bin_attribute *attr);
  232. int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
  233. const char *name);
  234. int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
  235. struct kobject *target,
  236. const char *name);
  237. void sysfs_remove_link(struct kobject *kobj, const char *name);
  238. int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
  239. const char *old_name, const char *new_name,
  240. const void *new_ns);
  241. void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
  242. const char *name);
  243. int __must_check sysfs_create_group(struct kobject *kobj,
  244. const struct attribute_group *grp);
  245. int __must_check sysfs_create_groups(struct kobject *kobj,
  246. const struct attribute_group **groups);
  247. int __must_check sysfs_update_groups(struct kobject *kobj,
  248. const struct attribute_group **groups);
  249. int sysfs_update_group(struct kobject *kobj,
  250. const struct attribute_group *grp);
  251. void sysfs_remove_group(struct kobject *kobj,
  252. const struct attribute_group *grp);
  253. void sysfs_remove_groups(struct kobject *kobj,
  254. const struct attribute_group **groups);
  255. int sysfs_add_file_to_group(struct kobject *kobj,
  256. const struct attribute *attr, const char *group);
  257. void sysfs_remove_file_from_group(struct kobject *kobj,
  258. const struct attribute *attr, const char *group);
  259. int sysfs_merge_group(struct kobject *kobj,
  260. const struct attribute_group *grp);
  261. void sysfs_unmerge_group(struct kobject *kobj,
  262. const struct attribute_group *grp);
  263. int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
  264. struct kobject *target, const char *link_name);
  265. void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
  266. const char *link_name);
  267. int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
  268. struct kobject *target_kobj,
  269. const char *target_name,
  270. const char *symlink_name);
  271. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
  272. int __must_check sysfs_init(void);
  273. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  274. {
  275. return kernfs_enable_ns(kn);
  276. }
  277. int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
  278. kgid_t kgid);
  279. int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid);
  280. int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
  281. const char *name, kuid_t kuid, kgid_t kgid);
  282. int sysfs_groups_change_owner(struct kobject *kobj,
  283. const struct attribute_group **groups,
  284. kuid_t kuid, kgid_t kgid);
  285. int sysfs_group_change_owner(struct kobject *kobj,
  286. const struct attribute_group *groups, kuid_t kuid,
  287. kgid_t kgid);
  288. __printf(2, 3)
  289. int sysfs_emit(char *buf, const char *fmt, ...);
  290. __printf(3, 4)
  291. int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
  292. #else /* CONFIG_SYSFS */
  293. static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  294. {
  295. return 0;
  296. }
  297. static inline void sysfs_remove_dir(struct kobject *kobj)
  298. {
  299. }
  300. static inline int sysfs_rename_dir_ns(struct kobject *kobj,
  301. const char *new_name, const void *new_ns)
  302. {
  303. return 0;
  304. }
  305. static inline int sysfs_move_dir_ns(struct kobject *kobj,
  306. struct kobject *new_parent_kobj,
  307. const void *new_ns)
  308. {
  309. return 0;
  310. }
  311. static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
  312. const char *name)
  313. {
  314. return 0;
  315. }
  316. static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
  317. const char *name)
  318. {
  319. }
  320. static inline int sysfs_create_file_ns(struct kobject *kobj,
  321. const struct attribute *attr,
  322. const void *ns)
  323. {
  324. return 0;
  325. }
  326. static inline int sysfs_create_files(struct kobject *kobj,
  327. const struct attribute * const *attr)
  328. {
  329. return 0;
  330. }
  331. static inline int sysfs_chmod_file(struct kobject *kobj,
  332. const struct attribute *attr, umode_t mode)
  333. {
  334. return 0;
  335. }
  336. static inline struct kernfs_node *
  337. sysfs_break_active_protection(struct kobject *kobj,
  338. const struct attribute *attr)
  339. {
  340. return NULL;
  341. }
  342. static inline void sysfs_unbreak_active_protection(struct kernfs_node *kn)
  343. {
  344. }
  345. static inline void sysfs_remove_file_ns(struct kobject *kobj,
  346. const struct attribute *attr,
  347. const void *ns)
  348. {
  349. }
  350. static inline bool sysfs_remove_file_self(struct kobject *kobj,
  351. const struct attribute *attr)
  352. {
  353. return false;
  354. }
  355. static inline void sysfs_remove_files(struct kobject *kobj,
  356. const struct attribute * const *attr)
  357. {
  358. }
  359. static inline int sysfs_create_bin_file(struct kobject *kobj,
  360. const struct bin_attribute *attr)
  361. {
  362. return 0;
  363. }
  364. static inline void sysfs_remove_bin_file(struct kobject *kobj,
  365. const struct bin_attribute *attr)
  366. {
  367. }
  368. static inline int sysfs_create_link(struct kobject *kobj,
  369. struct kobject *target, const char *name)
  370. {
  371. return 0;
  372. }
  373. static inline int sysfs_create_link_nowarn(struct kobject *kobj,
  374. struct kobject *target,
  375. const char *name)
  376. {
  377. return 0;
  378. }
  379. static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
  380. {
  381. }
  382. static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
  383. const char *old_name,
  384. const char *new_name, const void *ns)
  385. {
  386. return 0;
  387. }
  388. static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
  389. const char *name)
  390. {
  391. }
  392. static inline int sysfs_create_group(struct kobject *kobj,
  393. const struct attribute_group *grp)
  394. {
  395. return 0;
  396. }
  397. static inline int sysfs_create_groups(struct kobject *kobj,
  398. const struct attribute_group **groups)
  399. {
  400. return 0;
  401. }
  402. static inline int sysfs_update_groups(struct kobject *kobj,
  403. const struct attribute_group **groups)
  404. {
  405. return 0;
  406. }
  407. static inline int sysfs_update_group(struct kobject *kobj,
  408. const struct attribute_group *grp)
  409. {
  410. return 0;
  411. }
  412. static inline void sysfs_remove_group(struct kobject *kobj,
  413. const struct attribute_group *grp)
  414. {
  415. }
  416. static inline void sysfs_remove_groups(struct kobject *kobj,
  417. const struct attribute_group **groups)
  418. {
  419. }
  420. static inline int sysfs_add_file_to_group(struct kobject *kobj,
  421. const struct attribute *attr, const char *group)
  422. {
  423. return 0;
  424. }
  425. static inline void sysfs_remove_file_from_group(struct kobject *kobj,
  426. const struct attribute *attr, const char *group)
  427. {
  428. }
  429. static inline int sysfs_merge_group(struct kobject *kobj,
  430. const struct attribute_group *grp)
  431. {
  432. return 0;
  433. }
  434. static inline void sysfs_unmerge_group(struct kobject *kobj,
  435. const struct attribute_group *grp)
  436. {
  437. }
  438. static inline int sysfs_add_link_to_group(struct kobject *kobj,
  439. const char *group_name, struct kobject *target,
  440. const char *link_name)
  441. {
  442. return 0;
  443. }
  444. static inline void sysfs_remove_link_from_group(struct kobject *kobj,
  445. const char *group_name, const char *link_name)
  446. {
  447. }
  448. static inline int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj,
  449. struct kobject *target_kobj,
  450. const char *target_name,
  451. const char *symlink_name)
  452. {
  453. return 0;
  454. }
  455. static inline void sysfs_notify(struct kobject *kobj, const char *dir,
  456. const char *attr)
  457. {
  458. }
  459. static inline int __must_check sysfs_init(void)
  460. {
  461. return 0;
  462. }
  463. static inline void sysfs_enable_ns(struct kernfs_node *kn)
  464. {
  465. }
  466. static inline int sysfs_file_change_owner(struct kobject *kobj,
  467. const char *name, kuid_t kuid,
  468. kgid_t kgid)
  469. {
  470. return 0;
  471. }
  472. static inline int sysfs_link_change_owner(struct kobject *kobj,
  473. struct kobject *targ,
  474. const char *name, kuid_t kuid,
  475. kgid_t kgid)
  476. {
  477. return 0;
  478. }
  479. static inline int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
  480. {
  481. return 0;
  482. }
  483. static inline int sysfs_groups_change_owner(struct kobject *kobj,
  484. const struct attribute_group **groups,
  485. kuid_t kuid, kgid_t kgid)
  486. {
  487. return 0;
  488. }
  489. static inline int sysfs_group_change_owner(struct kobject *kobj,
  490. const struct attribute_group *groups,
  491. kuid_t kuid, kgid_t kgid)
  492. {
  493. return 0;
  494. }
  495. __printf(2, 3)
  496. static inline int sysfs_emit(char *buf, const char *fmt, ...)
  497. {
  498. return 0;
  499. }
  500. __printf(3, 4)
  501. static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
  502. {
  503. return 0;
  504. }
  505. #endif /* CONFIG_SYSFS */
  506. static inline int __must_check sysfs_create_file(struct kobject *kobj,
  507. const struct attribute *attr)
  508. {
  509. return sysfs_create_file_ns(kobj, attr, NULL);
  510. }
  511. static inline void sysfs_remove_file(struct kobject *kobj,
  512. const struct attribute *attr)
  513. {
  514. sysfs_remove_file_ns(kobj, attr, NULL);
  515. }
  516. static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
  517. const char *old_name, const char *new_name)
  518. {
  519. return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
  520. }
  521. static inline void sysfs_notify_dirent(struct kernfs_node *kn)
  522. {
  523. kernfs_notify(kn);
  524. }
  525. static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
  526. const char *name)
  527. {
  528. return kernfs_find_and_get(parent, name);
  529. }
  530. static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
  531. {
  532. kernfs_get(kn);
  533. return kn;
  534. }
  535. static inline void sysfs_put(struct kernfs_node *kn)
  536. {
  537. kernfs_put(kn);
  538. }
  539. #endif /* _SYSFS_H_ */