]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/sysfs.h
bcache in userspace; userspace fsck
[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 STORE_LOCKED(fn)                                                \
25 STORE(fn)                                                               \
26 {                                                                       \
27         ssize_t ret;                                                    \
28         mutex_lock(&bch_register_lock);                                 \
29         ret = __ ## fn ## _store(kobj, attr, buf, size);                \
30         mutex_unlock(&bch_register_lock);                               \
31         return ret;                                                     \
32 }
33
34 #define __sysfs_attribute(_name, _mode)                                 \
35         static struct attribute sysfs_##_name =                         \
36                 { .name = #_name, .mode = _mode }
37
38 #define write_attribute(n)      __sysfs_attribute(n, S_IWUSR)
39 #define read_attribute(n)       __sysfs_attribute(n, S_IRUGO)
40 #define rw_attribute(n)         __sysfs_attribute(n, S_IRUGO|S_IWUSR)
41
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
48 #define sysfs_print(file, var)                                          \
49 do {                                                                    \
50         if (attr == &sysfs_ ## file)                                    \
51                 return snprint(buf, PAGE_SIZE, var);                    \
52 } while (0)
53
54 #define sysfs_hprint(file, val)                                         \
55 do {                                                                    \
56         if (attr == &sysfs_ ## file) {                                  \
57                 ssize_t ret = bch_hprint(buf, val);                     \
58                 strcat(buf, "\n");                                      \
59                 return ret + 1;                                         \
60         }                                                               \
61 } while (0)
62
63 #define var_printf(_var, fmt)   sysfs_printf(_var, fmt, var(_var))
64 #define var_print(_var)         sysfs_print(_var, var(_var))
65 #define var_hprint(_var)        sysfs_hprint(_var, var(_var))
66
67 #define sysfs_strtoul(file, var)                                        \
68 do {                                                                    \
69         if (attr == &sysfs_ ## file)                                    \
70                 return strtoul_safe(buf, var) ?: (ssize_t) size;        \
71 } while (0)
72
73 #define sysfs_strtoul_clamp(file, var, min, max)                        \
74 do {                                                                    \
75         if (attr == &sysfs_ ## file)                                    \
76                 return strtoul_safe_clamp(buf, var, min, max)           \
77                         ?: (ssize_t) size;                              \
78 } while (0)
79
80 #define strtoul_or_return(cp)                                           \
81 ({                                                                      \
82         unsigned long _v;                                               \
83         int _r = kstrtoul(cp, 10, &_v);                                 \
84         if (_r)                                                         \
85                 return _r;                                              \
86         _v;                                                             \
87 })
88
89 #define strtoul_restrict_or_return(cp, min, max)                        \
90 ({                                                                      \
91         unsigned long __v = 0;                                          \
92         int _r = strtoul_safe_restrict(cp, __v, min, max);              \
93         if (_r)                                                         \
94                 return _r;                                              \
95         __v;                                                            \
96 })
97
98 #define strtoi_h_or_return(cp)                                          \
99 ({                                                                      \
100         u64 _v;                                                         \
101         int _r = strtoi_h(cp, &_v);                                     \
102         if (_r)                                                         \
103                 return _r;                                              \
104         _v;                                                             \
105 })
106
107 #define sysfs_hatoi(file, var)                                          \
108 do {                                                                    \
109         if (attr == &sysfs_ ## file)                                    \
110                 return strtoi_h(buf, &var) ?: (ssize_t) size;           \
111 } while (0)
112
113 #endif  /* _BCACHE_SYSFS_H_ */