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