-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmap-sysfs.c
277 lines (225 loc) · 5.76 KB
/
dmap-sysfs.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "dmap-sysfs.h"
#include "dmap.h"
#include "dmap-server.h"
#include "dmap-neighbor.h"
#include <linux/sysfs.h>
#define DMAP_ATTR_RO(_name) \
struct dmap_sysfs_attr dmap_attr_##_name = \
__ATTR(_name, S_IRUGO, dmap_attr_##_name##_show, NULL)
#define DMAP_ATTR_RW(_name) \
struct dmap_sysfs_attr dmap_attr_##_name = \
__ATTR(_name, S_IRUGO | S_IWUSR, dmap_attr_##_name##_show, \
dmap_attr_##_name##_store)
struct dmap_sysfs_attr {
struct attribute attr;
ssize_t (*show)(struct dmap *, char *);
ssize_t (*store)(struct dmap *, const char *, size_t count);
};
static struct completion *dmap_get_completion_from_kobject(struct kobject *kobj)
{
return &container_of(kobj,
struct dmap_kobject_holder, kobj)->completion;
}
static void dmap_kobject_release(struct kobject *kobj)
{
complete(dmap_get_completion_from_kobject(kobj));
}
static struct dmap *dmap_from_kobject(struct kobject *kobj)
{
return container_of(kobj, struct dmap, kobj_holder.kobj);
}
int dmap_sysfs_init(struct dmap_kobject_holder *holder, struct kobject *root,
struct kobj_type *ktype, const char *fmt, ...)
{
char name[256];
va_list args;
ktype->release = dmap_kobject_release;
init_completion(&holder->completion);
va_start(args, fmt);
vsnprintf(name, ARRAY_SIZE(name), fmt, args);
va_end(args);
return kobject_init_and_add(&holder->kobj, ktype, root, "%s", name);
}
void dmap_sysfs_deinit(struct dmap_kobject_holder *holder)
{
struct kobject *kobj = &holder->kobj;
if (atomic_cmpxchg(&holder->deiniting, 0, 1) == 0) {
kobject_put(kobj);
wait_for_completion(dmap_get_completion_from_kobject(kobj));
}
}
static ssize_t dmap_attr_start_server_store(struct dmap *map,
const char *buf, size_t count)
{
char host[DMAP_PARAM_SIZE];
int r, port;
r = sscanf(buf, DMAP_PARAM_FMT" %d", host, &port);
if (r != 2)
return -EINVAL;
r = dmap_server_start(&map->server, host, port);
if (r)
return r;
return count;
}
static ssize_t dmap_attr_start_server_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "\n");
return strlen(buf);
}
static ssize_t dmap_attr_stop_server_store(struct dmap *map,
const char *buf, size_t count)
{
int r;
r = dmap_server_stop(&map->server);
if (r)
return r;
return count;
}
static ssize_t dmap_attr_stop_server_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "\n");
return strlen(buf);
}
static ssize_t dmap_attr_server_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "%s:%d\n", map->server.host, map->server.port);
return strlen(buf);
}
static ssize_t dmap_attr_add_neighbor_store(struct dmap *map,
const char *buf, size_t count)
{
char host[DMAP_PARAM_SIZE];
struct dmap_address addr = {0};
int r, port;
r = sscanf(buf, DMAP_PARAM_FMT" %d", host, &port);
if (r != 2)
return -EINVAL;
snprintf(addr.host, ARRAY_SIZE(addr.host), "%s", host);
addr.port = port;
r = dmap_add_neighbor(map, &addr, false);
if (r)
return r;
return count;
}
static ssize_t dmap_attr_add_neighbor_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "\n");
return strlen(buf);
}
static ssize_t dmap_attr_remove_neighbor_store(struct dmap *map,
const char *buf, size_t count)
{
char host[DMAP_PARAM_SIZE];
int r;
r = sscanf(buf, DMAP_PARAM_FMT, host);
if (r != 1)
return -EINVAL;
r = dmap_remove_neighbor(map, host);
if (r)
return r;
return count;
}
static ssize_t dmap_attr_remove_neighbor_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "\n");
return strlen(buf);
}
static ssize_t dmap_attr_neighbors_show(struct dmap *map,
char *buf)
{
struct dmap_neighbor *curr;
int n, off, r;
r = 0;
off = 0;
down_read(&map->rw_sem);
list_for_each_entry(curr, &map->neighbor_list, list) {
if (off >= PAGE_SIZE) {
r = -ENOMEM;
break;
}
n = snprintf((char *)buf + off, PAGE_SIZE - off,
"%s:%d %s %llu %d\n", curr->addr.host, curr->addr.port,
curr->addr.id_str, curr->ping_us, curr->state);
if (n <= 0) {
r = -ENOMEM;
break;
}
off += n;
}
up_read(&map->rw_sem);
if (r)
return r;
return strlen(buf);
}
static ssize_t dmap_attr_id_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "%s\n", map->id_str);
return strlen(buf);
}
static ssize_t dmap_attr_nr_keys_show(struct dmap *map,
char *buf)
{
snprintf(buf, PAGE_SIZE, "%lu\n", atomic64_read(&map->hash.nr_keys));
return strlen(buf);
}
static ssize_t dmap_attr_show(struct kobject *kobj,
struct attribute *attr,
char *page)
{
struct dmap_sysfs_attr *vattr;
struct dmap *map;
vattr = container_of(attr, struct dmap_sysfs_attr, attr);
if (!vattr->show)
return -EIO;
map = dmap_from_kobject(kobj);
if (!map)
return -EIO;
return vattr->show(map, page);
}
static ssize_t dmap_attr_store(struct kobject *kobj,
struct attribute *attr,
const char *page, size_t count)
{
struct dmap_sysfs_attr *vattr;
struct dmap *map;
vattr = container_of(attr, struct dmap_sysfs_attr, attr);
if (!vattr->store)
return -EIO;
map = dmap_from_kobject(kobj);
if (!map)
return -EIO;
return vattr->store(map, page, count);
}
static DMAP_ATTR_RW(start_server);
static DMAP_ATTR_RW(stop_server);
static DMAP_ATTR_RO(server);
static DMAP_ATTR_RW(add_neighbor);
static DMAP_ATTR_RW(remove_neighbor);
static DMAP_ATTR_RO(neighbors);
static DMAP_ATTR_RO(id);
static DMAP_ATTR_RO(nr_keys);
static struct attribute *dmap_attrs[] = {
&dmap_attr_start_server.attr,
&dmap_attr_stop_server.attr,
&dmap_attr_server.attr,
&dmap_attr_add_neighbor.attr,
&dmap_attr_remove_neighbor.attr,
&dmap_attr_neighbors.attr,
&dmap_attr_id.attr,
&dmap_attr_nr_keys.attr,
NULL,
};
static const struct sysfs_ops dmap_sysfs_ops = {
.show = dmap_attr_show,
.store = dmap_attr_store,
};
struct kobj_type dmap_ktype = {
.sysfs_ops = &dmap_sysfs_ops,
.default_attrs = dmap_attrs,
};