blob: 1b59fa56a5995b3fceb444e1ae953c7cd7e19f03 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01005** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00006**
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"
David Teigland1c032c02006-04-28 10:50:41 -040020#include "lowcomms.h"
David Teiglande7fd4172006-01-18 09:30:29 +000021
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;
33static struct comm *local_comm;
34
35struct clusters;
36struct cluster;
37struct spaces;
38struct space;
39struct comms;
40struct comm;
41struct nodes;
42struct node;
43
44static struct config_group *make_cluster(struct config_group *, const char *);
45static void drop_cluster(struct config_group *, struct config_item *);
46static void release_cluster(struct config_item *);
47static struct config_group *make_space(struct config_group *, const char *);
48static void drop_space(struct config_group *, struct config_item *);
49static void release_space(struct config_item *);
50static struct config_item *make_comm(struct config_group *, const char *);
51static void drop_comm(struct config_group *, struct config_item *);
52static void release_comm(struct config_item *);
53static struct config_item *make_node(struct config_group *, const char *);
54static void drop_node(struct config_group *, struct config_item *);
55static void release_node(struct config_item *);
56
David Teiglandd2007782007-01-09 09:46:02 -060057static 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);
David Teiglande7fd4172006-01-18 09:30:29 +000062static 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
71static ssize_t comm_nodeid_read(struct comm *cm, char *buf);
72static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len);
73static ssize_t comm_local_read(struct comm *cm, char *buf);
74static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len);
75static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len);
76static ssize_t node_nodeid_read(struct node *nd, char *buf);
77static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len);
78static ssize_t node_weight_read(struct node *nd, char *buf);
79static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len);
80
David Teiglandd2007782007-01-09 09:46:02 -060081struct cluster {
82 struct config_group group;
83 unsigned int cl_tcp_port;
84 unsigned int cl_buffer_size;
85 unsigned int cl_rsbtbl_size;
86 unsigned int cl_lkbtbl_size;
87 unsigned int cl_dirtbl_size;
88 unsigned int cl_recover_timer;
89 unsigned int cl_toss_secs;
90 unsigned int cl_scan_secs;
91 unsigned int cl_log_debug;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010092 unsigned int cl_protocol;
David Teigland3ae1acf2007-05-18 08:59:31 -050093 unsigned int cl_timewarn_cs;
David Teiglandd2007782007-01-09 09:46:02 -060094};
95
96enum {
97 CLUSTER_ATTR_TCP_PORT = 0,
98 CLUSTER_ATTR_BUFFER_SIZE,
99 CLUSTER_ATTR_RSBTBL_SIZE,
100 CLUSTER_ATTR_LKBTBL_SIZE,
101 CLUSTER_ATTR_DIRTBL_SIZE,
102 CLUSTER_ATTR_RECOVER_TIMER,
103 CLUSTER_ATTR_TOSS_SECS,
104 CLUSTER_ATTR_SCAN_SECS,
105 CLUSTER_ATTR_LOG_DEBUG,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100106 CLUSTER_ATTR_PROTOCOL,
David Teigland3ae1acf2007-05-18 08:59:31 -0500107 CLUSTER_ATTR_TIMEWARN_CS,
David Teiglandd2007782007-01-09 09:46:02 -0600108};
109
110struct cluster_attribute {
111 struct configfs_attribute attr;
112 ssize_t (*show)(struct cluster *, char *);
113 ssize_t (*store)(struct cluster *, const char *, size_t);
114};
115
116static ssize_t cluster_set(struct cluster *cl, unsigned int *cl_field,
117 unsigned int *info_field, int check_zero,
118 const char *buf, size_t len)
119{
120 unsigned int x;
121
122 if (!capable(CAP_SYS_ADMIN))
123 return -EACCES;
124
125 x = simple_strtoul(buf, NULL, 0);
126
127 if (check_zero && !x)
128 return -EINVAL;
129
130 *cl_field = x;
131 *info_field = x;
132
133 return len;
134}
135
136#define __CONFIGFS_ATTR(_name,_mode,_read,_write) { \
137 .attr = { .ca_name = __stringify(_name), \
138 .ca_mode = _mode, \
139 .ca_owner = THIS_MODULE }, \
140 .show = _read, \
141 .store = _write, \
142}
143
144#define CLUSTER_ATTR(name, check_zero) \
145static ssize_t name##_write(struct cluster *cl, const char *buf, size_t len) \
146{ \
147 return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
148 check_zero, buf, len); \
149} \
150static ssize_t name##_read(struct cluster *cl, char *buf) \
151{ \
152 return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \
153} \
154static struct cluster_attribute cluster_attr_##name = \
155__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)
156
157CLUSTER_ATTR(tcp_port, 1);
158CLUSTER_ATTR(buffer_size, 1);
159CLUSTER_ATTR(rsbtbl_size, 1);
160CLUSTER_ATTR(lkbtbl_size, 1);
161CLUSTER_ATTR(dirtbl_size, 1);
162CLUSTER_ATTR(recover_timer, 1);
163CLUSTER_ATTR(toss_secs, 1);
164CLUSTER_ATTR(scan_secs, 1);
165CLUSTER_ATTR(log_debug, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100166CLUSTER_ATTR(protocol, 0);
David Teigland3ae1acf2007-05-18 08:59:31 -0500167CLUSTER_ATTR(timewarn_cs, 1);
David Teiglandd2007782007-01-09 09:46:02 -0600168
169static struct configfs_attribute *cluster_attrs[] = {
170 [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
171 [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
172 [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
173 [CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr,
174 [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
175 [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
176 [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
177 [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
178 [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100179 [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
David Teigland3ae1acf2007-05-18 08:59:31 -0500180 [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
David Teiglandd2007782007-01-09 09:46:02 -0600181 NULL,
182};
183
David Teiglande7fd4172006-01-18 09:30:29 +0000184enum {
185 COMM_ATTR_NODEID = 0,
186 COMM_ATTR_LOCAL,
187 COMM_ATTR_ADDR,
188};
189
190struct comm_attribute {
191 struct configfs_attribute attr;
192 ssize_t (*show)(struct comm *, char *);
193 ssize_t (*store)(struct comm *, const char *, size_t);
194};
195
196static struct comm_attribute comm_attr_nodeid = {
197 .attr = { .ca_owner = THIS_MODULE,
198 .ca_name = "nodeid",
199 .ca_mode = S_IRUGO | S_IWUSR },
200 .show = comm_nodeid_read,
201 .store = comm_nodeid_write,
202};
203
204static struct comm_attribute comm_attr_local = {
205 .attr = { .ca_owner = THIS_MODULE,
206 .ca_name = "local",
207 .ca_mode = S_IRUGO | S_IWUSR },
208 .show = comm_local_read,
209 .store = comm_local_write,
210};
211
212static struct comm_attribute comm_attr_addr = {
213 .attr = { .ca_owner = THIS_MODULE,
214 .ca_name = "addr",
215 .ca_mode = S_IRUGO | S_IWUSR },
216 .store = comm_addr_write,
217};
218
219static struct configfs_attribute *comm_attrs[] = {
220 [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
221 [COMM_ATTR_LOCAL] = &comm_attr_local.attr,
222 [COMM_ATTR_ADDR] = &comm_attr_addr.attr,
223 NULL,
224};
225
226enum {
227 NODE_ATTR_NODEID = 0,
228 NODE_ATTR_WEIGHT,
229};
230
231struct node_attribute {
232 struct configfs_attribute attr;
233 ssize_t (*show)(struct node *, char *);
234 ssize_t (*store)(struct node *, const char *, size_t);
235};
236
237static struct node_attribute node_attr_nodeid = {
238 .attr = { .ca_owner = THIS_MODULE,
239 .ca_name = "nodeid",
240 .ca_mode = S_IRUGO | S_IWUSR },
241 .show = node_nodeid_read,
242 .store = node_nodeid_write,
243};
244
245static struct node_attribute node_attr_weight = {
246 .attr = { .ca_owner = THIS_MODULE,
247 .ca_name = "weight",
248 .ca_mode = S_IRUGO | S_IWUSR },
249 .show = node_weight_read,
250 .store = node_weight_write,
251};
252
253static struct configfs_attribute *node_attrs[] = {
254 [NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
255 [NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
256 NULL,
257};
258
259struct clusters {
260 struct configfs_subsystem subsys;
261};
262
David Teiglande7fd4172006-01-18 09:30:29 +0000263struct spaces {
264 struct config_group ss_group;
265};
266
267struct space {
268 struct config_group group;
269 struct list_head members;
David Teigland90135922006-01-20 08:47:07 +0000270 struct mutex members_lock;
David Teiglande7fd4172006-01-18 09:30:29 +0000271 int members_count;
272};
273
274struct comms {
275 struct config_group cs_group;
276};
277
278struct comm {
279 struct config_item item;
280 int nodeid;
281 int local;
282 int addr_count;
283 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
284};
285
286struct nodes {
287 struct config_group ns_group;
288};
289
290struct node {
291 struct config_item item;
292 struct list_head list; /* space->members */
293 int nodeid;
294 int weight;
295};
296
297static struct configfs_group_operations clusters_ops = {
298 .make_group = make_cluster,
299 .drop_item = drop_cluster,
300};
301
302static struct configfs_item_operations cluster_ops = {
303 .release = release_cluster,
David Teiglandd2007782007-01-09 09:46:02 -0600304 .show_attribute = show_cluster,
305 .store_attribute = store_cluster,
David Teiglande7fd4172006-01-18 09:30:29 +0000306};
307
308static struct configfs_group_operations spaces_ops = {
309 .make_group = make_space,
310 .drop_item = drop_space,
311};
312
313static struct configfs_item_operations space_ops = {
314 .release = release_space,
315};
316
317static struct configfs_group_operations comms_ops = {
318 .make_item = make_comm,
319 .drop_item = drop_comm,
320};
321
322static struct configfs_item_operations comm_ops = {
323 .release = release_comm,
324 .show_attribute = show_comm,
325 .store_attribute = store_comm,
326};
327
328static struct configfs_group_operations nodes_ops = {
329 .make_item = make_node,
330 .drop_item = drop_node,
331};
332
333static struct configfs_item_operations node_ops = {
334 .release = release_node,
335 .show_attribute = show_node,
336 .store_attribute = store_node,
337};
338
339static struct config_item_type clusters_type = {
340 .ct_group_ops = &clusters_ops,
341 .ct_owner = THIS_MODULE,
342};
343
344static struct config_item_type cluster_type = {
345 .ct_item_ops = &cluster_ops,
David Teiglandd2007782007-01-09 09:46:02 -0600346 .ct_attrs = cluster_attrs,
David Teiglande7fd4172006-01-18 09:30:29 +0000347 .ct_owner = THIS_MODULE,
348};
349
350static struct config_item_type spaces_type = {
351 .ct_group_ops = &spaces_ops,
352 .ct_owner = THIS_MODULE,
353};
354
355static struct config_item_type space_type = {
356 .ct_item_ops = &space_ops,
357 .ct_owner = THIS_MODULE,
358};
359
360static struct config_item_type comms_type = {
361 .ct_group_ops = &comms_ops,
362 .ct_owner = THIS_MODULE,
363};
364
365static struct config_item_type comm_type = {
366 .ct_item_ops = &comm_ops,
367 .ct_attrs = comm_attrs,
368 .ct_owner = THIS_MODULE,
369};
370
371static struct config_item_type nodes_type = {
372 .ct_group_ops = &nodes_ops,
373 .ct_owner = THIS_MODULE,
374};
375
376static struct config_item_type node_type = {
377 .ct_item_ops = &node_ops,
378 .ct_attrs = node_attrs,
379 .ct_owner = THIS_MODULE,
380};
381
382static struct cluster *to_cluster(struct config_item *i)
383{
384 return i ? container_of(to_config_group(i), struct cluster, group):NULL;
385}
386
387static struct space *to_space(struct config_item *i)
388{
389 return i ? container_of(to_config_group(i), struct space, group) : NULL;
390}
391
392static struct comm *to_comm(struct config_item *i)
393{
394 return i ? container_of(i, struct comm, item) : NULL;
395}
396
397static struct node *to_node(struct config_item *i)
398{
399 return i ? container_of(i, struct node, item) : NULL;
400}
401
402static struct config_group *make_cluster(struct config_group *g,
403 const char *name)
404{
405 struct cluster *cl = NULL;
406 struct spaces *sps = NULL;
407 struct comms *cms = NULL;
408 void *gps = NULL;
409
410 cl = kzalloc(sizeof(struct cluster), GFP_KERNEL);
411 gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
412 sps = kzalloc(sizeof(struct spaces), GFP_KERNEL);
413 cms = kzalloc(sizeof(struct comms), GFP_KERNEL);
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
David Teiglandd2007782007-01-09 09:46:02 -0600427 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;
David Teigland84d8cd62007-05-29 08:44:23 -0500436 cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
David Teiglandd2007782007-01-09 09:46:02 -0600437
David Teiglande7fd4172006-01-18 09:30:29 +0000438 space_list = &sps->ss_group;
439 comm_list = &cms->cs_group;
440 return &cl->group;
441
442 fail:
443 kfree(cl);
444 kfree(gps);
445 kfree(sps);
446 kfree(cms);
447 return NULL;
448}
449
450static void drop_cluster(struct config_group *g, struct config_item *i)
451{
452 struct cluster *cl = to_cluster(i);
453 struct config_item *tmp;
454 int j;
455
456 for (j = 0; cl->group.default_groups[j]; j++) {
457 tmp = &cl->group.default_groups[j]->cg_item;
458 cl->group.default_groups[j] = NULL;
459 config_item_put(tmp);
460 }
461
462 space_list = NULL;
463 comm_list = NULL;
464
465 config_item_put(i);
466}
467
468static void release_cluster(struct config_item *i)
469{
470 struct cluster *cl = to_cluster(i);
471 kfree(cl->group.default_groups);
472 kfree(cl);
473}
474
475static struct config_group *make_space(struct config_group *g, const char *name)
476{
477 struct space *sp = NULL;
478 struct nodes *nds = NULL;
479 void *gps = NULL;
480
481 sp = kzalloc(sizeof(struct space), GFP_KERNEL);
482 gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
483 nds = kzalloc(sizeof(struct nodes), GFP_KERNEL);
484
485 if (!sp || !gps || !nds)
486 goto fail;
487
488 config_group_init_type_name(&sp->group, name, &space_type);
489 config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
490
491 sp->group.default_groups = gps;
492 sp->group.default_groups[0] = &nds->ns_group;
493 sp->group.default_groups[1] = NULL;
494
495 INIT_LIST_HEAD(&sp->members);
David Teigland90135922006-01-20 08:47:07 +0000496 mutex_init(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000497 sp->members_count = 0;
498 return &sp->group;
499
500 fail:
501 kfree(sp);
502 kfree(gps);
503 kfree(nds);
504 return NULL;
505}
506
507static void drop_space(struct config_group *g, struct config_item *i)
508{
509 struct space *sp = to_space(i);
510 struct config_item *tmp;
511 int j;
512
513 /* assert list_empty(&sp->members) */
514
515 for (j = 0; sp->group.default_groups[j]; j++) {
516 tmp = &sp->group.default_groups[j]->cg_item;
517 sp->group.default_groups[j] = NULL;
518 config_item_put(tmp);
519 }
520
521 config_item_put(i);
522}
523
524static void release_space(struct config_item *i)
525{
526 struct space *sp = to_space(i);
527 kfree(sp->group.default_groups);
528 kfree(sp);
529}
530
531static struct config_item *make_comm(struct config_group *g, const char *name)
532{
533 struct comm *cm;
534
535 cm = kzalloc(sizeof(struct comm), GFP_KERNEL);
536 if (!cm)
537 return NULL;
538
539 config_item_init_type_name(&cm->item, name, &comm_type);
540 cm->nodeid = -1;
541 cm->local = 0;
542 cm->addr_count = 0;
543 return &cm->item;
544}
545
546static void drop_comm(struct config_group *g, struct config_item *i)
547{
548 struct comm *cm = to_comm(i);
549 if (local_comm == cm)
550 local_comm = NULL;
David Teigland1c032c02006-04-28 10:50:41 -0400551 dlm_lowcomms_close(cm->nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000552 while (cm->addr_count--)
553 kfree(cm->addr[cm->addr_count]);
554 config_item_put(i);
555}
556
557static void release_comm(struct config_item *i)
558{
559 struct comm *cm = to_comm(i);
560 kfree(cm);
561}
562
563static struct config_item *make_node(struct config_group *g, const char *name)
564{
565 struct space *sp = to_space(g->cg_item.ci_parent);
566 struct node *nd;
567
568 nd = kzalloc(sizeof(struct node), GFP_KERNEL);
569 if (!nd)
570 return NULL;
571
572 config_item_init_type_name(&nd->item, name, &node_type);
573 nd->nodeid = -1;
574 nd->weight = 1; /* default weight of 1 if none is set */
575
David Teigland90135922006-01-20 08:47:07 +0000576 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000577 list_add(&nd->list, &sp->members);
578 sp->members_count++;
David Teigland90135922006-01-20 08:47:07 +0000579 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000580
581 return &nd->item;
582}
583
584static void drop_node(struct config_group *g, struct config_item *i)
585{
586 struct space *sp = to_space(g->cg_item.ci_parent);
587 struct node *nd = to_node(i);
588
David Teigland90135922006-01-20 08:47:07 +0000589 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000590 list_del(&nd->list);
591 sp->members_count--;
David Teigland90135922006-01-20 08:47:07 +0000592 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000593
594 config_item_put(i);
595}
596
597static void release_node(struct config_item *i)
598{
599 struct node *nd = to_node(i);
600 kfree(nd);
601}
602
603static struct clusters clusters_root = {
604 .subsys = {
605 .su_group = {
606 .cg_item = {
607 .ci_namebuf = "dlm",
608 .ci_type = &clusters_type,
609 },
610 },
611 },
612};
613
614int dlm_config_init(void)
615{
616 config_group_init(&clusters_root.subsys.su_group);
617 init_MUTEX(&clusters_root.subsys.su_sem);
618 return configfs_register_subsystem(&clusters_root.subsys);
619}
620
621void dlm_config_exit(void)
622{
623 configfs_unregister_subsystem(&clusters_root.subsys);
624}
625
626/*
627 * Functions for user space to read/write attributes
628 */
629
David Teiglandd2007782007-01-09 09:46:02 -0600630static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
631 char *buf)
632{
633 struct cluster *cl = to_cluster(i);
634 struct cluster_attribute *cla =
635 container_of(a, struct cluster_attribute, attr);
636 return cla->show ? cla->show(cl, buf) : 0;
637}
638
639static ssize_t store_cluster(struct config_item *i,
640 struct configfs_attribute *a,
641 const char *buf, size_t len)
642{
643 struct cluster *cl = to_cluster(i);
644 struct cluster_attribute *cla =
645 container_of(a, struct cluster_attribute, attr);
646 return cla->store ? cla->store(cl, buf, len) : -EINVAL;
647}
648
David Teiglande7fd4172006-01-18 09:30:29 +0000649static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
650 char *buf)
651{
652 struct comm *cm = to_comm(i);
653 struct comm_attribute *cma =
654 container_of(a, struct comm_attribute, attr);
655 return cma->show ? cma->show(cm, buf) : 0;
656}
657
658static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
659 const char *buf, size_t len)
660{
661 struct comm *cm = to_comm(i);
662 struct comm_attribute *cma =
663 container_of(a, struct comm_attribute, attr);
664 return cma->store ? cma->store(cm, buf, len) : -EINVAL;
665}
666
667static ssize_t comm_nodeid_read(struct comm *cm, char *buf)
668{
669 return sprintf(buf, "%d\n", cm->nodeid);
670}
671
672static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len)
673{
674 cm->nodeid = simple_strtol(buf, NULL, 0);
675 return len;
676}
677
678static ssize_t comm_local_read(struct comm *cm, char *buf)
679{
680 return sprintf(buf, "%d\n", cm->local);
681}
682
683static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len)
684{
685 cm->local= simple_strtol(buf, NULL, 0);
686 if (cm->local && !local_comm)
687 local_comm = cm;
688 return len;
689}
690
691static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len)
692{
693 struct sockaddr_storage *addr;
694
695 if (len != sizeof(struct sockaddr_storage))
696 return -EINVAL;
697
698 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
699 return -ENOSPC;
700
701 addr = kzalloc(sizeof(*addr), GFP_KERNEL);
702 if (!addr)
703 return -ENOMEM;
704
705 memcpy(addr, buf, len);
706 cm->addr[cm->addr_count++] = addr;
707 return len;
708}
709
710static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
711 char *buf)
712{
713 struct node *nd = to_node(i);
714 struct node_attribute *nda =
715 container_of(a, struct node_attribute, attr);
716 return nda->show ? nda->show(nd, buf) : 0;
717}
718
719static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
720 const char *buf, size_t len)
721{
722 struct node *nd = to_node(i);
723 struct node_attribute *nda =
724 container_of(a, struct node_attribute, attr);
725 return nda->store ? nda->store(nd, buf, len) : -EINVAL;
726}
727
728static ssize_t node_nodeid_read(struct node *nd, char *buf)
729{
730 return sprintf(buf, "%d\n", nd->nodeid);
731}
732
733static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len)
734{
735 nd->nodeid = simple_strtol(buf, NULL, 0);
736 return len;
737}
738
739static ssize_t node_weight_read(struct node *nd, char *buf)
740{
741 return sprintf(buf, "%d\n", nd->weight);
742}
743
744static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len)
745{
746 nd->weight = simple_strtol(buf, NULL, 0);
747 return len;
748}
749
750/*
751 * Functions for the dlm to get the info that's been configured
752 */
753
754static struct space *get_space(char *name)
755{
Satyam Sharma3168b072007-05-08 09:18:58 +0100756 struct config_item *i;
757
David Teiglande7fd4172006-01-18 09:30:29 +0000758 if (!space_list)
759 return NULL;
Satyam Sharma3168b072007-05-08 09:18:58 +0100760
761 down(&space_list->cg_subsys->su_sem);
762 i = config_group_find_obj(space_list, name);
763 up(&space_list->cg_subsys->su_sem);
764
765 return to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000766}
767
768static void put_space(struct space *sp)
769{
770 config_item_put(&sp->group.cg_item);
771}
772
773static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr)
774{
775 struct config_item *i;
776 struct comm *cm = NULL;
777 int found = 0;
778
779 if (!comm_list)
780 return NULL;
781
782 down(&clusters_root.subsys.su_sem);
783
784 list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
785 cm = to_comm(i);
786
787 if (nodeid) {
788 if (cm->nodeid != nodeid)
789 continue;
790 found = 1;
Satyam Sharma3168b072007-05-08 09:18:58 +0100791 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000792 break;
793 } else {
794 if (!cm->addr_count ||
795 memcmp(cm->addr[0], addr, sizeof(*addr)))
796 continue;
797 found = 1;
Satyam Sharma3168b072007-05-08 09:18:58 +0100798 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000799 break;
800 }
801 }
802 up(&clusters_root.subsys.su_sem);
803
Satyam Sharma3168b072007-05-08 09:18:58 +0100804 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +0000805 cm = NULL;
806 return cm;
807}
808
809static void put_comm(struct comm *cm)
810{
811 config_item_put(&cm->item);
812}
813
814/* caller must free mem */
815int dlm_nodeid_list(char *lsname, int **ids_out)
816{
817 struct space *sp;
818 struct node *nd;
819 int i = 0, rv = 0;
820 int *ids;
821
822 sp = get_space(lsname);
823 if (!sp)
824 return -EEXIST;
825
David Teigland90135922006-01-20 08:47:07 +0000826 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000827 if (!sp->members_count) {
828 rv = 0;
829 goto out;
830 }
831
832 ids = kcalloc(sp->members_count, sizeof(int), GFP_KERNEL);
833 if (!ids) {
834 rv = -ENOMEM;
835 goto out;
836 }
837
838 rv = sp->members_count;
839 list_for_each_entry(nd, &sp->members, list)
840 ids[i++] = nd->nodeid;
841
842 if (rv != i)
843 printk("bad nodeid count %d %d\n", rv, i);
844
845 *ids_out = ids;
846 out:
David Teigland90135922006-01-20 08:47:07 +0000847 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000848 put_space(sp);
849 return rv;
850}
851
852int dlm_node_weight(char *lsname, int nodeid)
853{
854 struct space *sp;
855 struct node *nd;
856 int w = -EEXIST;
857
858 sp = get_space(lsname);
859 if (!sp)
860 goto out;
861
David Teigland90135922006-01-20 08:47:07 +0000862 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000863 list_for_each_entry(nd, &sp->members, list) {
864 if (nd->nodeid != nodeid)
865 continue;
866 w = nd->weight;
867 break;
868 }
David Teigland90135922006-01-20 08:47:07 +0000869 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000870 put_space(sp);
871 out:
872 return w;
873}
874
875int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
876{
877 struct comm *cm = get_comm(nodeid, NULL);
878 if (!cm)
879 return -EEXIST;
880 if (!cm->addr_count)
881 return -ENOENT;
882 memcpy(addr, cm->addr[0], sizeof(*addr));
883 put_comm(cm);
884 return 0;
885}
886
887int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
888{
889 struct comm *cm = get_comm(0, addr);
890 if (!cm)
891 return -EEXIST;
892 *nodeid = cm->nodeid;
893 put_comm(cm);
894 return 0;
895}
896
897int dlm_our_nodeid(void)
898{
899 return local_comm ? local_comm->nodeid : 0;
900}
901
902/* num 0 is first addr, num 1 is second addr */
903int dlm_our_addr(struct sockaddr_storage *addr, int num)
904{
905 if (!local_comm)
906 return -1;
907 if (num + 1 > local_comm->addr_count)
908 return -1;
909 memcpy(addr, local_comm->addr[num], sizeof(*addr));
910 return 0;
911}
912
913/* Config file defaults */
914#define DEFAULT_TCP_PORT 21064
915#define DEFAULT_BUFFER_SIZE 4096
916#define DEFAULT_RSBTBL_SIZE 256
917#define DEFAULT_LKBTBL_SIZE 1024
918#define DEFAULT_DIRTBL_SIZE 512
919#define DEFAULT_RECOVER_TIMER 5
920#define DEFAULT_TOSS_SECS 10
921#define DEFAULT_SCAN_SECS 5
David Teigland99fc6482007-01-09 09:44:01 -0600922#define DEFAULT_LOG_DEBUG 0
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100923#define DEFAULT_PROTOCOL 0
David Teigland3ae1acf2007-05-18 08:59:31 -0500924#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
David Teiglande7fd4172006-01-18 09:30:29 +0000925
926struct dlm_config_info dlm_config = {
David Teigland68c817a2007-01-09 09:41:48 -0600927 .ci_tcp_port = DEFAULT_TCP_PORT,
928 .ci_buffer_size = DEFAULT_BUFFER_SIZE,
929 .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
930 .ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,
931 .ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
932 .ci_recover_timer = DEFAULT_RECOVER_TIMER,
933 .ci_toss_secs = DEFAULT_TOSS_SECS,
David Teigland99fc6482007-01-09 09:44:01 -0600934 .ci_scan_secs = DEFAULT_SCAN_SECS,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100935 .ci_log_debug = DEFAULT_LOG_DEBUG,
David Teigland3ae1acf2007-05-18 08:59:31 -0500936 .ci_protocol = DEFAULT_PROTOCOL,
937 .ci_timewarn_cs = DEFAULT_TIMEWARN_CS
David Teiglande7fd4172006-01-18 09:30:29 +0000938};
939