summaryrefslogtreecommitdiff
path: root/drivers/md/bcache/sysfs.h
blob: a2ff6447b699f595f6b7a1b0e1fe2504972b067c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHE_SYSFS_H_
#define _BCACHE_SYSFS_H_

#define KTYPE(type)							\
struct kobj_type type ## _ktype = {					\
	.release	= type ## _release,				\
	.sysfs_ops	= &((const struct sysfs_ops) {			\
		.show	= type ## _show,				\
		.store	= type ## _store				\
	}),								\
	.default_groups	= type ## _groups				\
}

#define SHOW(fn)							\
static ssize_t fn ## _show(struct kobject *kobj, struct attribute *attr,\
			   char *buf)					\

#define STORE(fn)							\
static ssize_t fn ## _store(struct kobject *kobj, struct attribute *attr,\
			    const char *buf, size_t size)		\

#define SHOW_LOCKED(fn)							\
SHOW(fn)								\
{									\
	ssize_t ret;							\
	mutex_lock(&bch_register_lock);					\
	ret = __ ## fn ## _show(kobj, attr, buf);			\
	mutex_unlock(&bch_register_lock);				\
	return ret;							\
}

#define STORE_LOCKED(fn)						\
STORE(fn)								\
{									\
	ssize_t ret;							\
	mutex_lock(&bch_register_lock);					\
	ret = __ ## fn ## _store(kobj, attr, buf, size);		\
	mutex_unlock(&bch_register_lock);				\
	return ret;							\
}

#define __sysfs_attribute(_name, _mode)					\
	static struct attribute sysfs_##_name =				\
		{ .name = #_name, .mode = _mode }

#define write_attribute(n)	__sysfs_attribute(n, 0200)
#define read_attribute(n)	__sysfs_attribute(n, 0444)
#define rw_attribute(n)		__sysfs_attribute(n, 0644)

#define sysfs_printf(file, fmt, ...)					\
do {									\
	if (attr == &sysfs_ ## file)					\
		return sysfs_emit(buf, fmt "\n", __VA_ARGS__);	\
} while (0)

#define sysfs_print(file, var)						\
do {									\
	if (attr == &sysfs_ ## file)					\
		return sysfs_emit(buf,						\
				__builtin_types_compatible_p(typeof(var), int)		\
					 ? "%i\n" :				\
				__builtin_types_compatible_p(typeof(var), unsigned int)	\
					 ? "%u\n" :				\
				__builtin_types_compatible_p(typeof(var), long)		\
					 ? "%li\n" :			\
				__builtin_types_compatible_p(typeof(var), unsigned long)\
					 ? "%lu\n" :			\
				__builtin_types_compatible_p(typeof(var), int64_t)	\
					 ? "%lli\n" :			\
				__builtin_types_compatible_p(typeof(var), uint64_t)	\
					 ? "%llu\n" :			\
				__builtin_types_compatible_p(typeof(var), const char *)	\
					 ? "%s\n" : "%i\n", var);	\
} while (0)

#define sysfs_hprint(file, val)						\
do {									\
	if (attr == &sysfs_ ## file) {					\
		ssize_t ret = bch_hprint(buf, val);			\
		strcat(buf, "\n");					\
		return ret + 1;						\
	}								\
} while (0)

#define var_printf(_var, fmt)	sysfs_printf(_var, fmt, var(_var))
#define var_print(_var)		sysfs_print(_var, var(_var))
#define var_hprint(_var)	sysfs_hprint(_var, var(_var))

#define sysfs_strtoul(file, var)					\
do {									\
	if (attr == &sysfs_ ## file)					\
		return strtoul_safe(buf, var) ?: (ssize_t) size;	\
} while (0)

#define sysfs_strtoul_bool(file, var)					\
do {									\
	if (attr == &sysfs_ ## file) {					\
		unsigned long v = strtoul_or_return(buf);		\
									\
		var = v ? 1 : 0;					\
		return size;						\
	}								\
} while (0)

#define sysfs_strtoul_clamp(file, var, min, max)			\
do {									\
	if (attr == &sysfs_ ## file) {					\
		unsigned long v = 0;					\
		ssize_t ret;						\
		ret = strtoul_safe_clamp(buf, v, min, max);		\
		if (!ret) {						\
			var = v;					\
			return size;					\
		}							\
		return ret;						\
	}								\
} while (0)

#define strtoul_or_return(cp)						\
({									\
	unsigned long _v;						\
	int _r = kstrtoul(cp, 10, &_v);					\
	if (_r)								\
		return _r;						\
	_v;								\
})

#define strtoi_h_or_return(cp, v)					\
do {									\
	int _r = strtoi_h(cp, &v);					\
	if (_r)								\
		return _r;						\
} while (0)

#define sysfs_hatoi(file, var)						\
do {									\
	if (attr == &sysfs_ ## file)					\
		return strtoi_h(buf, &var) ?: (ssize_t) size;		\
} while (0)

#endif  /* _BCACHE_SYSFS_H_ */