1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHE_SYSFS_H_
3 #define _BCACHE_SYSFS_H_
6 struct kobj_type type ## _ktype = { \
7 .release = type ## _release, \
8 .sysfs_ops = &((const struct sysfs_ops) { \
9 .show = type ## _show, \
10 .store = type ## _store \
12 .default_attrs = type ## _files \
16 static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
20 static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
21 const char *buf, size_t size) \
23 #define SHOW_LOCKED(fn) \
27 mutex_lock(&bch_register_lock); \
28 ret = __ ## fn ## _show(kobj, attr, buf); \
29 mutex_unlock(&bch_register_lock); \
33 #define STORE_LOCKED(fn) \
37 mutex_lock(&bch_register_lock); \
38 ret = __ ## fn ## _store(kobj, attr, buf, size); \
39 mutex_unlock(&bch_register_lock); \
43 #define __sysfs_attribute(_name, _mode) \
44 static struct attribute sysfs_##_name = \
45 { .name = #_name, .mode = _mode }
47 #define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
48 #define read_attribute(n) __sysfs_attribute(n, S_IRUGO)
49 #define rw_attribute(n) __sysfs_attribute(n, S_IRUGO|S_IWUSR)
51 #define sysfs_printf(file, fmt, ...) \
53 if (attr == &sysfs_ ## file) \
54 return snprintf(buf, PAGE_SIZE, fmt "\n", __VA_ARGS__); \
57 #define sysfs_print(file, var) \
59 if (attr == &sysfs_ ## file) \
60 return snprint(buf, PAGE_SIZE, var); \
63 #define sysfs_hprint(file, val) \
65 if (attr == &sysfs_ ## file) { \
66 ssize_t ret = bch_hprint(buf, val); \
72 #define var_printf(_var, fmt) sysfs_printf(_var, fmt, var(_var))
73 #define var_print(_var) sysfs_print(_var, var(_var))
74 #define var_hprint(_var) sysfs_hprint(_var, var(_var))
76 #define sysfs_strtoul(file, var) \
78 if (attr == &sysfs_ ## file) \
79 return strtoul_safe(buf, var) ?: (ssize_t) size; \
82 #define sysfs_strtoul_clamp(file, var, min, max) \
84 if (attr == &sysfs_ ## file) \
85 return strtoul_safe_clamp(buf, var, min, max) \
89 #define strtoul_or_return(cp) \
92 int _r = kstrtoul(cp, 10, &_v); \
98 #define strtoi_h_or_return(cp, v) \
100 int _r = strtoi_h(cp, &v); \
105 #define sysfs_hatoi(file, var) \
107 if (attr == &sysfs_ ## file) \
108 return strtoi_h(buf, &var) ?: (ssize_t) size; \
111 #endif /* _BCACHE_SYSFS_H_ */