Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6-block.git] / fs / dlm / config.c
CommitLineData
e7fd4179
DT
1/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
51409340 5** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
e7fd4179
DT
6**
7** This copyrighted material is made available to anyone wishing to use,
8** modify, copy, or redistribute it subject to the terms and conditions
9** of the GNU General Public License v.2.
10**
11*******************************************************************************
12******************************************************************************/
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/configfs.h>
17#include <net/sock.h>
18
19#include "config.h"
1c032c03 20#include "lowcomms.h"
e7fd4179
DT
21
22/*
23 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
24 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
25 * /config/dlm/<cluster>/comms/<comm>/nodeid
26 * /config/dlm/<cluster>/comms/<comm>/local
27 * /config/dlm/<cluster>/comms/<comm>/addr
28 * The <cluster> level is useless, but I haven't figured out how to avoid it.
29 */
30
31static struct config_group *space_list;
32static struct config_group *comm_list;
51409340 33static struct dlm_comm *local_comm;
e7fd4179 34
51409340
DT
35struct dlm_clusters;
36struct dlm_cluster;
37struct dlm_spaces;
38struct dlm_space;
39struct dlm_comms;
40struct dlm_comm;
41struct dlm_nodes;
42struct dlm_node;
e7fd4179 43
f89ab861 44static struct config_group *make_cluster(struct config_group *, const char *);
e7fd4179
DT
45static void drop_cluster(struct config_group *, struct config_item *);
46static void release_cluster(struct config_item *);
f89ab861 47static struct config_group *make_space(struct config_group *, const char *);
e7fd4179
DT
48static void drop_space(struct config_group *, struct config_item *);
49static void release_space(struct config_item *);
f89ab861 50static struct config_item *make_comm(struct config_group *, const char *);
e7fd4179
DT
51static void drop_comm(struct config_group *, struct config_item *);
52static void release_comm(struct config_item *);
f89ab861 53static struct config_item *make_node(struct config_group *, const char *);
e7fd4179
DT
54static void drop_node(struct config_group *, struct config_item *);
55static void release_node(struct config_item *);
56
d200778e
DT
57static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
58 char *buf);
59static ssize_t store_cluster(struct config_item *i,
60 struct configfs_attribute *a,
61 const char *buf, size_t len);
e7fd4179
DT
62static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
63 char *buf);
64static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
65 const char *buf, size_t len);
66static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
67 char *buf);
68static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
69 const char *buf, size_t len);
70
51409340
DT
71static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf);
72static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
73 size_t len);
74static ssize_t comm_local_read(struct dlm_comm *cm, char *buf);
75static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
76 size_t len);
77static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf,
78 size_t len);
79static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf);
80static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
81 size_t len);
82static ssize_t node_weight_read(struct dlm_node *nd, char *buf);
83static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
84 size_t len);
85
86struct dlm_cluster {
d200778e
DT
87 struct config_group group;
88 unsigned int cl_tcp_port;
89 unsigned int cl_buffer_size;
90 unsigned int cl_rsbtbl_size;
91 unsigned int cl_lkbtbl_size;
92 unsigned int cl_dirtbl_size;
93 unsigned int cl_recover_timer;
94 unsigned int cl_toss_secs;
95 unsigned int cl_scan_secs;
96 unsigned int cl_log_debug;
6ed7257b 97 unsigned int cl_protocol;
3ae1acf9 98 unsigned int cl_timewarn_cs;
d200778e
DT
99};
100
101enum {
102 CLUSTER_ATTR_TCP_PORT = 0,
103 CLUSTER_ATTR_BUFFER_SIZE,
104 CLUSTER_ATTR_RSBTBL_SIZE,
105 CLUSTER_ATTR_LKBTBL_SIZE,
106 CLUSTER_ATTR_DIRTBL_SIZE,
107 CLUSTER_ATTR_RECOVER_TIMER,
108 CLUSTER_ATTR_TOSS_SECS,
109 CLUSTER_ATTR_SCAN_SECS,
110 CLUSTER_ATTR_LOG_DEBUG,
6ed7257b 111 CLUSTER_ATTR_PROTOCOL,
3ae1acf9 112 CLUSTER_ATTR_TIMEWARN_CS,
d200778e
DT
113};
114
115struct cluster_attribute {
116 struct configfs_attribute attr;
51409340
DT
117 ssize_t (*show)(struct dlm_cluster *, char *);
118 ssize_t (*store)(struct dlm_cluster *, const char *, size_t);
d200778e
DT
119};
120
51409340 121static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
5416b704 122 int *info_field, int check_zero,
d200778e
DT
123 const char *buf, size_t len)
124{
125 unsigned int x;
126
127 if (!capable(CAP_SYS_ADMIN))
128 return -EACCES;
129
130 x = simple_strtoul(buf, NULL, 0);
131
132 if (check_zero && !x)
133 return -EINVAL;
134
135 *cl_field = x;
136 *info_field = x;
137
138 return len;
139}
140
d200778e 141#define CLUSTER_ATTR(name, check_zero) \
51409340 142static ssize_t name##_write(struct dlm_cluster *cl, const char *buf, size_t len) \
d200778e
DT
143{ \
144 return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
145 check_zero, buf, len); \
146} \
51409340 147static ssize_t name##_read(struct dlm_cluster *cl, char *buf) \
d200778e
DT
148{ \
149 return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \
150} \
151static struct cluster_attribute cluster_attr_##name = \
152__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)
153
154CLUSTER_ATTR(tcp_port, 1);
155CLUSTER_ATTR(buffer_size, 1);
156CLUSTER_ATTR(rsbtbl_size, 1);
157CLUSTER_ATTR(lkbtbl_size, 1);
158CLUSTER_ATTR(dirtbl_size, 1);
159CLUSTER_ATTR(recover_timer, 1);
160CLUSTER_ATTR(toss_secs, 1);
161CLUSTER_ATTR(scan_secs, 1);
162CLUSTER_ATTR(log_debug, 0);
6ed7257b 163CLUSTER_ATTR(protocol, 0);
3ae1acf9 164CLUSTER_ATTR(timewarn_cs, 1);
d200778e
DT
165
166static struct configfs_attribute *cluster_attrs[] = {
167 [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
168 [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
169 [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
170 [CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr,
171 [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
172 [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
173 [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
174 [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
175 [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr,
6ed7257b 176 [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
3ae1acf9 177 [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
d200778e
DT
178 NULL,
179};
180
e7fd4179
DT
181enum {
182 COMM_ATTR_NODEID = 0,
183 COMM_ATTR_LOCAL,
184 COMM_ATTR_ADDR,
185};
186
187struct comm_attribute {
188 struct configfs_attribute attr;
51409340
DT
189 ssize_t (*show)(struct dlm_comm *, char *);
190 ssize_t (*store)(struct dlm_comm *, const char *, size_t);
e7fd4179
DT
191};
192
193static struct comm_attribute comm_attr_nodeid = {
194 .attr = { .ca_owner = THIS_MODULE,
195 .ca_name = "nodeid",
196 .ca_mode = S_IRUGO | S_IWUSR },
197 .show = comm_nodeid_read,
198 .store = comm_nodeid_write,
199};
200
201static struct comm_attribute comm_attr_local = {
202 .attr = { .ca_owner = THIS_MODULE,
203 .ca_name = "local",
204 .ca_mode = S_IRUGO | S_IWUSR },
205 .show = comm_local_read,
206 .store = comm_local_write,
207};
208
209static struct comm_attribute comm_attr_addr = {
210 .attr = { .ca_owner = THIS_MODULE,
211 .ca_name = "addr",
212 .ca_mode = S_IRUGO | S_IWUSR },
213 .store = comm_addr_write,
214};
215
216static struct configfs_attribute *comm_attrs[] = {
217 [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
218 [COMM_ATTR_LOCAL] = &comm_attr_local.attr,
219 [COMM_ATTR_ADDR] = &comm_attr_addr.attr,
220 NULL,
221};
222
223enum {
224 NODE_ATTR_NODEID = 0,
225 NODE_ATTR_WEIGHT,
226};
227
228struct node_attribute {
229 struct configfs_attribute attr;
51409340
DT
230 ssize_t (*show)(struct dlm_node *, char *);
231 ssize_t (*store)(struct dlm_node *, const char *, size_t);
e7fd4179
DT
232};
233
234static struct node_attribute node_attr_nodeid = {
235 .attr = { .ca_owner = THIS_MODULE,
236 .ca_name = "nodeid",
237 .ca_mode = S_IRUGO | S_IWUSR },
238 .show = node_nodeid_read,
239 .store = node_nodeid_write,
240};
241
242static struct node_attribute node_attr_weight = {
243 .attr = { .ca_owner = THIS_MODULE,
244 .ca_name = "weight",
245 .ca_mode = S_IRUGO | S_IWUSR },
246 .show = node_weight_read,
247 .store = node_weight_write,
248};
249
250static struct configfs_attribute *node_attrs[] = {
251 [NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
252 [NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
253 NULL,
254};
255
51409340 256struct dlm_clusters {
e7fd4179
DT
257 struct configfs_subsystem subsys;
258};
259
51409340 260struct dlm_spaces {
e7fd4179
DT
261 struct config_group ss_group;
262};
263
51409340 264struct dlm_space {
e7fd4179
DT
265 struct config_group group;
266 struct list_head members;
90135925 267 struct mutex members_lock;
e7fd4179
DT
268 int members_count;
269};
270
51409340 271struct dlm_comms {
e7fd4179
DT
272 struct config_group cs_group;
273};
274
51409340 275struct dlm_comm {
e7fd4179
DT
276 struct config_item item;
277 int nodeid;
278 int local;
279 int addr_count;
280 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
281};
282
51409340 283struct dlm_nodes {
e7fd4179
DT
284 struct config_group ns_group;
285};
286
51409340 287struct dlm_node {
e7fd4179
DT
288 struct config_item item;
289 struct list_head list; /* space->members */
290 int nodeid;
291 int weight;
d44e0fc7 292 int new;
e7fd4179
DT
293};
294
295static struct configfs_group_operations clusters_ops = {
296 .make_group = make_cluster,
297 .drop_item = drop_cluster,
298};
299
300static struct configfs_item_operations cluster_ops = {
301 .release = release_cluster,
d200778e
DT
302 .show_attribute = show_cluster,
303 .store_attribute = store_cluster,
e7fd4179
DT
304};
305
306static struct configfs_group_operations spaces_ops = {
307 .make_group = make_space,
308 .drop_item = drop_space,
309};
310
311static struct configfs_item_operations space_ops = {
312 .release = release_space,
313};
314
315static struct configfs_group_operations comms_ops = {
316 .make_item = make_comm,
317 .drop_item = drop_comm,
318};
319
320static struct configfs_item_operations comm_ops = {
321 .release = release_comm,
322 .show_attribute = show_comm,
323 .store_attribute = store_comm,
324};
325
326static struct configfs_group_operations nodes_ops = {
327 .make_item = make_node,
328 .drop_item = drop_node,
329};
330
331static struct configfs_item_operations node_ops = {
332 .release = release_node,
333 .show_attribute = show_node,
334 .store_attribute = store_node,
335};
336
337static struct config_item_type clusters_type = {
338 .ct_group_ops = &clusters_ops,
339 .ct_owner = THIS_MODULE,
340};
341
342static struct config_item_type cluster_type = {
343 .ct_item_ops = &cluster_ops,
d200778e 344 .ct_attrs = cluster_attrs,
e7fd4179
DT
345 .ct_owner = THIS_MODULE,
346};
347
348static struct config_item_type spaces_type = {
349 .ct_group_ops = &spaces_ops,
350 .ct_owner = THIS_MODULE,
351};
352
353static struct config_item_type space_type = {
354 .ct_item_ops = &space_ops,
355 .ct_owner = THIS_MODULE,
356};
357
358static struct config_item_type comms_type = {
359 .ct_group_ops = &comms_ops,
360 .ct_owner = THIS_MODULE,
361};
362
363static struct config_item_type comm_type = {
364 .ct_item_ops = &comm_ops,
365 .ct_attrs = comm_attrs,
366 .ct_owner = THIS_MODULE,
367};
368
369static struct config_item_type nodes_type = {
370 .ct_group_ops = &nodes_ops,
371 .ct_owner = THIS_MODULE,
372};
373
374static struct config_item_type node_type = {
375 .ct_item_ops = &node_ops,
376 .ct_attrs = node_attrs,
377 .ct_owner = THIS_MODULE,
378};
379
51409340 380static struct dlm_cluster *to_cluster(struct config_item *i)
e7fd4179 381{
51409340
DT
382 return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
383 NULL;
e7fd4179
DT
384}
385
51409340 386static struct dlm_space *to_space(struct config_item *i)
e7fd4179 387{
51409340
DT
388 return i ? container_of(to_config_group(i), struct dlm_space, group) :
389 NULL;
e7fd4179
DT
390}
391
51409340 392static struct dlm_comm *to_comm(struct config_item *i)
e7fd4179 393{
51409340 394 return i ? container_of(i, struct dlm_comm, item) : NULL;
e7fd4179
DT
395}
396
51409340 397static struct dlm_node *to_node(struct config_item *i)
e7fd4179 398{
51409340 399 return i ? container_of(i, struct dlm_node, item) : NULL;
e7fd4179
DT
400}
401
f89ab861
JB
402static struct config_group *make_cluster(struct config_group *g,
403 const char *name)
e7fd4179 404{
51409340
DT
405 struct dlm_cluster *cl = NULL;
406 struct dlm_spaces *sps = NULL;
407 struct dlm_comms *cms = NULL;
e7fd4179
DT
408 void *gps = NULL;
409
51409340 410 cl = kzalloc(sizeof(struct dlm_cluster), GFP_KERNEL);
e7fd4179 411 gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
51409340
DT
412 sps = kzalloc(sizeof(struct dlm_spaces), GFP_KERNEL);
413 cms = kzalloc(sizeof(struct dlm_comms), GFP_KERNEL);
e7fd4179
DT
414
415 if (!cl || !gps || !sps || !cms)
416 goto fail;
417
418 config_group_init_type_name(&cl->group, name, &cluster_type);
419 config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
420 config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
421
422 cl->group.default_groups = gps;
423 cl->group.default_groups[0] = &sps->ss_group;
424 cl->group.default_groups[1] = &cms->cs_group;
425 cl->group.default_groups[2] = NULL;
426
d200778e
DT
427 cl->cl_tcp_port = dlm_config.ci_tcp_port;
428 cl->cl_buffer_size = dlm_config.ci_buffer_size;
429 cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
430 cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size;
431 cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
432 cl->cl_recover_timer = dlm_config.ci_recover_timer;
433 cl->cl_toss_secs = dlm_config.ci_toss_secs;
434 cl->cl_scan_secs = dlm_config.ci_scan_secs;
435 cl->cl_log_debug = dlm_config.ci_log_debug;
0b7cac0f 436 cl->cl_protocol = dlm_config.ci_protocol;
84d8cd69 437 cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
d200778e 438
e7fd4179
DT
439 space_list = &sps->ss_group;
440 comm_list = &cms->cs_group;
f89ab861 441 return &cl->group;
e7fd4179
DT
442
443 fail:
444 kfree(cl);
445 kfree(gps);
446 kfree(sps);
447 kfree(cms);
a6795e9e 448 return ERR_PTR(-ENOMEM);
e7fd4179
DT
449}
450
451static void drop_cluster(struct config_group *g, struct config_item *i)
452{
51409340 453 struct dlm_cluster *cl = to_cluster(i);
e7fd4179
DT
454 struct config_item *tmp;
455 int j;
456
457 for (j = 0; cl->group.default_groups[j]; j++) {
458 tmp = &cl->group.default_groups[j]->cg_item;
459 cl->group.default_groups[j] = NULL;
460 config_item_put(tmp);
461 }
462
463 space_list = NULL;
464 comm_list = NULL;
465
466 config_item_put(i);
467}
468
469static void release_cluster(struct config_item *i)
470{
51409340 471 struct dlm_cluster *cl = to_cluster(i);
e7fd4179
DT
472 kfree(cl->group.default_groups);
473 kfree(cl);
474}
475
f89ab861 476static struct config_group *make_space(struct config_group *g, const char *name)
e7fd4179 477{
51409340
DT
478 struct dlm_space *sp = NULL;
479 struct dlm_nodes *nds = NULL;
e7fd4179
DT
480 void *gps = NULL;
481
51409340 482 sp = kzalloc(sizeof(struct dlm_space), GFP_KERNEL);
e7fd4179 483 gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
51409340 484 nds = kzalloc(sizeof(struct dlm_nodes), GFP_KERNEL);
e7fd4179
DT
485
486 if (!sp || !gps || !nds)
487 goto fail;
488
489 config_group_init_type_name(&sp->group, name, &space_type);
490 config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
491
492 sp->group.default_groups = gps;
493 sp->group.default_groups[0] = &nds->ns_group;
494 sp->group.default_groups[1] = NULL;
495
496 INIT_LIST_HEAD(&sp->members);
90135925 497 mutex_init(&sp->members_lock);
e7fd4179 498 sp->members_count = 0;
f89ab861 499 return &sp->group;
e7fd4179
DT
500
501 fail:
502 kfree(sp);
503 kfree(gps);
504 kfree(nds);
a6795e9e 505 return ERR_PTR(-ENOMEM);
e7fd4179
DT
506}
507
508static void drop_space(struct config_group *g, struct config_item *i)
509{
51409340 510 struct dlm_space *sp = to_space(i);
e7fd4179
DT
511 struct config_item *tmp;
512 int j;
513
514 /* assert list_empty(&sp->members) */
515
516 for (j = 0; sp->group.default_groups[j]; j++) {
517 tmp = &sp->group.default_groups[j]->cg_item;
518 sp->group.default_groups[j] = NULL;
519 config_item_put(tmp);
520 }
521
522 config_item_put(i);
523}
524
525static void release_space(struct config_item *i)
526{
51409340 527 struct dlm_space *sp = to_space(i);
e7fd4179
DT
528 kfree(sp->group.default_groups);
529 kfree(sp);
530}
531
f89ab861 532static struct config_item *make_comm(struct config_group *g, const char *name)
e7fd4179 533{
51409340 534 struct dlm_comm *cm;
e7fd4179 535
51409340 536 cm = kzalloc(sizeof(struct dlm_comm), GFP_KERNEL);
e7fd4179 537 if (!cm)
a6795e9e 538 return ERR_PTR(-ENOMEM);
e7fd4179
DT
539
540 config_item_init_type_name(&cm->item, name, &comm_type);
541 cm->nodeid = -1;
542 cm->local = 0;
543 cm->addr_count = 0;
f89ab861 544 return &cm->item;
e7fd4179
DT
545}
546
547static void drop_comm(struct config_group *g, struct config_item *i)
548{
51409340 549 struct dlm_comm *cm = to_comm(i);
e7fd4179
DT
550 if (local_comm == cm)
551 local_comm = NULL;
1c032c03 552 dlm_lowcomms_close(cm->nodeid);
e7fd4179
DT
553 while (cm->addr_count--)
554 kfree(cm->addr[cm->addr_count]);
555 config_item_put(i);
556}
557
558static void release_comm(struct config_item *i)
559{
51409340 560 struct dlm_comm *cm = to_comm(i);
e7fd4179
DT
561 kfree(cm);
562}
563
f89ab861 564static struct config_item *make_node(struct config_group *g, const char *name)
e7fd4179 565{
51409340
DT
566 struct dlm_space *sp = to_space(g->cg_item.ci_parent);
567 struct dlm_node *nd;
e7fd4179 568
51409340 569 nd = kzalloc(sizeof(struct dlm_node), GFP_KERNEL);
e7fd4179 570 if (!nd)
a6795e9e 571 return ERR_PTR(-ENOMEM);
e7fd4179
DT
572
573 config_item_init_type_name(&nd->item, name, &node_type);
574 nd->nodeid = -1;
575 nd->weight = 1; /* default weight of 1 if none is set */
d44e0fc7 576 nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
e7fd4179 577
90135925 578 mutex_lock(&sp->members_lock);
e7fd4179
DT
579 list_add(&nd->list, &sp->members);
580 sp->members_count++;
90135925 581 mutex_unlock(&sp->members_lock);
e7fd4179 582
f89ab861 583 return &nd->item;
e7fd4179
DT
584}
585
586static void drop_node(struct config_group *g, struct config_item *i)
587{
51409340
DT
588 struct dlm_space *sp = to_space(g->cg_item.ci_parent);
589 struct dlm_node *nd = to_node(i);
e7fd4179 590
90135925 591 mutex_lock(&sp->members_lock);
e7fd4179
DT
592 list_del(&nd->list);
593 sp->members_count--;
90135925 594 mutex_unlock(&sp->members_lock);
e7fd4179
DT
595
596 config_item_put(i);
597}
598
599static void release_node(struct config_item *i)
600{
51409340 601 struct dlm_node *nd = to_node(i);
e7fd4179
DT
602 kfree(nd);
603}
604
51409340 605static struct dlm_clusters clusters_root = {
e7fd4179
DT
606 .subsys = {
607 .su_group = {
608 .cg_item = {
609 .ci_namebuf = "dlm",
610 .ci_type = &clusters_type,
611 },
612 },
613 },
614};
615
30727174 616int __init dlm_config_init(void)
e7fd4179
DT
617{
618 config_group_init(&clusters_root.subsys.su_group);
e6bd07ae 619 mutex_init(&clusters_root.subsys.su_mutex);
e7fd4179
DT
620 return configfs_register_subsystem(&clusters_root.subsys);
621}
622
623void dlm_config_exit(void)
624{
625 configfs_unregister_subsystem(&clusters_root.subsys);
626}
627
628/*
629 * Functions for user space to read/write attributes
630 */
631
d200778e
DT
632static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
633 char *buf)
634{
51409340 635 struct dlm_cluster *cl = to_cluster(i);
d200778e
DT
636 struct cluster_attribute *cla =
637 container_of(a, struct cluster_attribute, attr);
638 return cla->show ? cla->show(cl, buf) : 0;
639}
640
641static ssize_t store_cluster(struct config_item *i,
642 struct configfs_attribute *a,
643 const char *buf, size_t len)
644{
51409340 645 struct dlm_cluster *cl = to_cluster(i);
d200778e
DT
646 struct cluster_attribute *cla =
647 container_of(a, struct cluster_attribute, attr);
648 return cla->store ? cla->store(cl, buf, len) : -EINVAL;
649}
650
e7fd4179
DT
651static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
652 char *buf)
653{
51409340 654 struct dlm_comm *cm = to_comm(i);
e7fd4179
DT
655 struct comm_attribute *cma =
656 container_of(a, struct comm_attribute, attr);
657 return cma->show ? cma->show(cm, buf) : 0;
658}
659
660static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
661 const char *buf, size_t len)
662{
51409340 663 struct dlm_comm *cm = to_comm(i);
e7fd4179
DT
664 struct comm_attribute *cma =
665 container_of(a, struct comm_attribute, attr);
666 return cma->store ? cma->store(cm, buf, len) : -EINVAL;
667}
668
51409340 669static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf)
e7fd4179
DT
670{
671 return sprintf(buf, "%d\n", cm->nodeid);
672}
673
51409340
DT
674static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
675 size_t len)
e7fd4179
DT
676{
677 cm->nodeid = simple_strtol(buf, NULL, 0);
678 return len;
679}
680
51409340 681static ssize_t comm_local_read(struct dlm_comm *cm, char *buf)
e7fd4179
DT
682{
683 return sprintf(buf, "%d\n", cm->local);
684}
685
51409340
DT
686static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
687 size_t len)
e7fd4179
DT
688{
689 cm->local= simple_strtol(buf, NULL, 0);
690 if (cm->local && !local_comm)
691 local_comm = cm;
692 return len;
693}
694
51409340 695static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf, size_t len)
e7fd4179
DT
696{
697 struct sockaddr_storage *addr;
698
699 if (len != sizeof(struct sockaddr_storage))
700 return -EINVAL;
701
702 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
703 return -ENOSPC;
704
705 addr = kzalloc(sizeof(*addr), GFP_KERNEL);
706 if (!addr)
707 return -ENOMEM;
708
709 memcpy(addr, buf, len);
710 cm->addr[cm->addr_count++] = addr;
711 return len;
712}
713
714static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
715 char *buf)
716{
51409340 717 struct dlm_node *nd = to_node(i);
e7fd4179
DT
718 struct node_attribute *nda =
719 container_of(a, struct node_attribute, attr);
720 return nda->show ? nda->show(nd, buf) : 0;
721}
722
723static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
724 const char *buf, size_t len)
725{
51409340 726 struct dlm_node *nd = to_node(i);
e7fd4179
DT
727 struct node_attribute *nda =
728 container_of(a, struct node_attribute, attr);
729 return nda->store ? nda->store(nd, buf, len) : -EINVAL;
730}
731
51409340 732static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf)
e7fd4179
DT
733{
734 return sprintf(buf, "%d\n", nd->nodeid);
735}
736
51409340
DT
737static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
738 size_t len)
e7fd4179
DT
739{
740 nd->nodeid = simple_strtol(buf, NULL, 0);
741 return len;
742}
743
51409340 744static ssize_t node_weight_read(struct dlm_node *nd, char *buf)
e7fd4179
DT
745{
746 return sprintf(buf, "%d\n", nd->weight);
747}
748
51409340
DT
749static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
750 size_t len)
e7fd4179
DT
751{
752 nd->weight = simple_strtol(buf, NULL, 0);
753 return len;
754}
755
756/*
757 * Functions for the dlm to get the info that's been configured
758 */
759
51409340 760static struct dlm_space *get_space(char *name)
e7fd4179 761{
3168b078
SS
762 struct config_item *i;
763
e7fd4179
DT
764 if (!space_list)
765 return NULL;
3168b078 766
e6bd07ae 767 mutex_lock(&space_list->cg_subsys->su_mutex);
3fe6c5ce 768 i = config_group_find_item(space_list, name);
e6bd07ae 769 mutex_unlock(&space_list->cg_subsys->su_mutex);
3168b078
SS
770
771 return to_space(i);
e7fd4179
DT
772}
773
51409340 774static void put_space(struct dlm_space *sp)
e7fd4179
DT
775{
776 config_item_put(&sp->group.cg_item);
777}
778
51409340 779static struct dlm_comm *get_comm(int nodeid, struct sockaddr_storage *addr)
e7fd4179
DT
780{
781 struct config_item *i;
51409340 782 struct dlm_comm *cm = NULL;
e7fd4179
DT
783 int found = 0;
784
785 if (!comm_list)
786 return NULL;
787
e6bd07ae 788 mutex_lock(&clusters_root.subsys.su_mutex);
e7fd4179
DT
789
790 list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
791 cm = to_comm(i);
792
793 if (nodeid) {
794 if (cm->nodeid != nodeid)
795 continue;
796 found = 1;
3168b078 797 config_item_get(i);
e7fd4179
DT
798 break;
799 } else {
800 if (!cm->addr_count ||
801 memcmp(cm->addr[0], addr, sizeof(*addr)))
802 continue;
803 found = 1;
3168b078 804 config_item_get(i);
e7fd4179
DT
805 break;
806 }
807 }
e6bd07ae 808 mutex_unlock(&clusters_root.subsys.su_mutex);
e7fd4179 809
3168b078 810 if (!found)
e7fd4179
DT
811 cm = NULL;
812 return cm;
813}
814
51409340 815static void put_comm(struct dlm_comm *cm)
e7fd4179
DT
816{
817 config_item_put(&cm->item);
818}
819
820/* caller must free mem */
d44e0fc7
DT
821int dlm_nodeid_list(char *lsname, int **ids_out, int *ids_count_out,
822 int **new_out, int *new_count_out)
e7fd4179 823{
51409340
DT
824 struct dlm_space *sp;
825 struct dlm_node *nd;
d44e0fc7
DT
826 int i = 0, rv = 0, ids_count = 0, new_count = 0;
827 int *ids, *new;
e7fd4179
DT
828
829 sp = get_space(lsname);
830 if (!sp)
831 return -EEXIST;
832
90135925 833 mutex_lock(&sp->members_lock);
e7fd4179 834 if (!sp->members_count) {
d44e0fc7
DT
835 rv = -EINVAL;
836 printk(KERN_ERR "dlm: zero members_count\n");
e7fd4179
DT
837 goto out;
838 }
839
d44e0fc7
DT
840 ids_count = sp->members_count;
841
842 ids = kcalloc(ids_count, sizeof(int), GFP_KERNEL);
e7fd4179
DT
843 if (!ids) {
844 rv = -ENOMEM;
845 goto out;
846 }
847
d44e0fc7 848 list_for_each_entry(nd, &sp->members, list) {
e7fd4179 849 ids[i++] = nd->nodeid;
d44e0fc7
DT
850 if (nd->new)
851 new_count++;
852 }
853
854 if (ids_count != i)
855 printk(KERN_ERR "dlm: bad nodeid count %d %d\n", ids_count, i);
856
857 if (!new_count)
858 goto out_ids;
859
860 new = kcalloc(new_count, sizeof(int), GFP_KERNEL);
861 if (!new) {
862 kfree(ids);
863 rv = -ENOMEM;
864 goto out;
865 }
e7fd4179 866
d44e0fc7
DT
867 i = 0;
868 list_for_each_entry(nd, &sp->members, list) {
869 if (nd->new) {
870 new[i++] = nd->nodeid;
871 nd->new = 0;
872 }
873 }
874 *new_count_out = new_count;
875 *new_out = new;
e7fd4179 876
d44e0fc7
DT
877 out_ids:
878 *ids_count_out = ids_count;
e7fd4179
DT
879 *ids_out = ids;
880 out:
90135925 881 mutex_unlock(&sp->members_lock);
e7fd4179
DT
882 put_space(sp);
883 return rv;
884}
885
886int dlm_node_weight(char *lsname, int nodeid)
887{
51409340
DT
888 struct dlm_space *sp;
889 struct dlm_node *nd;
e7fd4179
DT
890 int w = -EEXIST;
891
892 sp = get_space(lsname);
893 if (!sp)
894 goto out;
895
90135925 896 mutex_lock(&sp->members_lock);
e7fd4179
DT
897 list_for_each_entry(nd, &sp->members, list) {
898 if (nd->nodeid != nodeid)
899 continue;
900 w = nd->weight;
901 break;
902 }
90135925 903 mutex_unlock(&sp->members_lock);
e7fd4179
DT
904 put_space(sp);
905 out:
906 return w;
907}
908
909int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
910{
51409340 911 struct dlm_comm *cm = get_comm(nodeid, NULL);
e7fd4179
DT
912 if (!cm)
913 return -EEXIST;
914 if (!cm->addr_count)
915 return -ENOENT;
916 memcpy(addr, cm->addr[0], sizeof(*addr));
917 put_comm(cm);
918 return 0;
919}
920
921int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
922{
51409340 923 struct dlm_comm *cm = get_comm(0, addr);
e7fd4179
DT
924 if (!cm)
925 return -EEXIST;
926 *nodeid = cm->nodeid;
927 put_comm(cm);
928 return 0;
929}
930
931int dlm_our_nodeid(void)
932{
933 return local_comm ? local_comm->nodeid : 0;
934}
935
936/* num 0 is first addr, num 1 is second addr */
937int dlm_our_addr(struct sockaddr_storage *addr, int num)
938{
939 if (!local_comm)
940 return -1;
941 if (num + 1 > local_comm->addr_count)
942 return -1;
943 memcpy(addr, local_comm->addr[num], sizeof(*addr));
944 return 0;
945}
946
947/* Config file defaults */
948#define DEFAULT_TCP_PORT 21064
949#define DEFAULT_BUFFER_SIZE 4096
950#define DEFAULT_RSBTBL_SIZE 256
951#define DEFAULT_LKBTBL_SIZE 1024
952#define DEFAULT_DIRTBL_SIZE 512
953#define DEFAULT_RECOVER_TIMER 5
954#define DEFAULT_TOSS_SECS 10
955#define DEFAULT_SCAN_SECS 5
99fc6487 956#define DEFAULT_LOG_DEBUG 0
6ed7257b 957#define DEFAULT_PROTOCOL 0
3ae1acf9 958#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
e7fd4179
DT
959
960struct dlm_config_info dlm_config = {
68c817a1
DT
961 .ci_tcp_port = DEFAULT_TCP_PORT,
962 .ci_buffer_size = DEFAULT_BUFFER_SIZE,
963 .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
964 .ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,
965 .ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
966 .ci_recover_timer = DEFAULT_RECOVER_TIMER,
967 .ci_toss_secs = DEFAULT_TOSS_SECS,
99fc6487 968 .ci_scan_secs = DEFAULT_SCAN_SECS,
6ed7257b 969 .ci_log_debug = DEFAULT_LOG_DEBUG,
3ae1acf9
DT
970 .ci_protocol = DEFAULT_PROTOCOL,
971 .ci_timewarn_cs = DEFAULT_TIMEWARN_CS
e7fd4179
DT
972};
973