sysfs.h 3.0 KB

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