sysfs.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHE_SYSFS_H_
  3. #define _BCACHE_SYSFS_H_
  4. #define KTYPE(type) \
  5. struct kobj_type type ## _ktype = { \
  6. .release = type ## _release, \
  7. .sysfs_ops = &((const struct sysfs_ops) { \
  8. .show = type ## _show, \
  9. .store = type ## _store \
  10. }), \
  11. .default_attrs = type ## _files \
  12. }
  13. #define SHOW(fn) \
  14. static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
  15. char *buf) \
  16. #define STORE(fn) \
  17. static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
  18. const char *buf, size_t size) \
  19. #define SHOW_LOCKED(fn) \
  20. SHOW(fn) \
  21. { \
  22. ssize_t ret; \
  23. mutex_lock(&bch_register_lock); \
  24. ret = __ ## fn ## _show(kobj, attr, buf); \
  25. mutex_unlock(&bch_register_lock); \
  26. return ret; \
  27. }
  28. #define STORE_LOCKED(fn) \
  29. STORE(fn) \
  30. { \
  31. ssize_t ret; \
  32. mutex_lock(&bch_register_lock); \
  33. ret = __ ## fn ## _store(kobj, attr, buf, size); \
  34. mutex_unlock(&bch_register_lock); \
  35. return ret; \
  36. }
  37. #define __sysfs_attribute(_name, _mode) \
  38. static struct attribute sysfs_##_name = \
  39. { .name = #_name, .mode = _mode }
  40. #define write_attribute(n) __sysfs_attribute(n, 0200)
  41. #define read_attribute(n) __sysfs_attribute(n, 0444)
  42. #define rw_attribute(n) __sysfs_attribute(n, 0644)
  43. #define sysfs_printf(file, fmt, ...) \
  44. do { \
  45. if (attr == &sysfs_ ## file) \
  46. return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
  47. } while (0)
  48. #define sysfs_print(file, var) \
  49. do { \
  50. if (attr == &sysfs_ ## file) \
  51. return snprint(buf, PAGE_SIZE, var); \
  52. } while (0)
  53. #define sysfs_hprint(file, val) \
  54. do { \
  55. if (attr == &sysfs_ ## file) { \
  56. ssize_t ret = bch_hprint(buf, val); \
  57. strcat(buf, "\n"); \
  58. return ret + 1; \
  59. } \
  60. } while (0)
  61. #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
  62. #define var_print(_var) sysfs_print(_var, var(_var))
  63. #define var_hprint(_var) sysfs_hprint(_var, var(_var))
  64. #define sysfs_strtoul(file, var) \
  65. do { \
  66. if (attr == &sysfs_ ## file) \
  67. return strtoul_safe(buf, var) ?: (ssize_t) size; \
  68. } while (0)
  69. #define sysfs_strtoul_clamp(file, var, min, max) \
  70. do { \
  71. if (attr == &sysfs_ ## file) { \
  72. unsigned long v = 0; \
  73. ssize_t ret; \
  74. ret = strtoul_safe_clamp(buf, v, min, max); \
  75. if (!ret) { \
  76. var = v; \
  77. return size; \
  78. } \
  79. return ret; \
  80. } \
  81. } while (0)
  82. #define strtoul_or_return(cp) \
  83. ({ \
  84. unsigned long _v; \
  85. int _r = kstrtoul(cp, 10, &_v); \
  86. if (_r) \
  87. return _r; \
  88. _v; \
  89. })
  90. #define strtoi_h_or_return(cp, v) \
  91. do { \
  92. int _r = strtoi_h(cp, &v); \
  93. if (_r) \
  94. return _r; \
  95. } while (0)
  96. #define sysfs_hatoi(file, var) \
  97. do { \
  98. if (attr == &sysfs_ ## file) \
  99. return strtoi_h(buf, &var) ?: (ssize_t) size; \
  100. } while (0)
  101. #endif /* _BCACHE_SYSFS_H_ */