blob: 492d8caaaf2509355c32c7c7b62dcf89b22f1124 [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
Joel Becker11c3b792008-06-12 14:00:18 -070044static int make_cluster(struct config_group *, const char *,
45 struct config_group **);
David Teiglande7fd4172006-01-18 09:30:29 +000046static void drop_cluster(struct config_group *, struct config_item *);
47static void release_cluster(struct config_item *);
Joel Becker11c3b792008-06-12 14:00:18 -070048static int make_space(struct config_group *, const char *,
49 struct config_group **);
David Teiglande7fd4172006-01-18 09:30:29 +000050static void drop_space(struct config_group *, struct config_item *);
51static void release_space(struct config_item *);
Joel Becker11c3b792008-06-12 14:00:18 -070052static int make_comm(struct config_group *, const char *,
53 struct config_item **);
David Teiglande7fd4172006-01-18 09:30:29 +000054static void drop_comm(struct config_group *, struct config_item *);
55static void release_comm(struct config_item *);
Joel Becker11c3b792008-06-12 14:00:18 -070056static int make_node(struct config_group *, const char *,
57 struct config_item **);
David Teiglande7fd4172006-01-18 09:30:29 +000058static void drop_node(struct config_group *, struct config_item *);
59static void release_node(struct config_item *);
60
David Teiglandd2007782007-01-09 09:46:02 -060061static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
62 char *buf);
63static ssize_t store_cluster(struct config_item *i,
64 struct configfs_attribute *a,
65 const char *buf, size_t len);
David Teiglande7fd4172006-01-18 09:30:29 +000066static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
67 char *buf);
68static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
69 const char *buf, size_t len);
70static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
71 char *buf);
72static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
73 const char *buf, size_t len);
74
75static ssize_t comm_nodeid_read(struct comm *cm, char *buf);
76static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len);
77static ssize_t comm_local_read(struct comm *cm, char *buf);
78static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len);
79static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len);
80static ssize_t node_nodeid_read(struct node *nd, char *buf);
81static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len);
82static ssize_t node_weight_read(struct node *nd, char *buf);
83static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len);
84
David Teiglandd2007782007-01-09 09:46:02 -060085struct cluster {
86 struct config_group group;
87 unsigned int cl_tcp_port;
88 unsigned int cl_buffer_size;
89 unsigned int cl_rsbtbl_size;
90 unsigned int cl_lkbtbl_size;
91 unsigned int cl_dirtbl_size;
92 unsigned int cl_recover_timer;
93 unsigned int cl_toss_secs;
94 unsigned int cl_scan_secs;
95 unsigned int cl_log_debug;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010096 unsigned int cl_protocol;
David Teigland3ae1acf2007-05-18 08:59:31 -050097 unsigned int cl_timewarn_cs;
David Teiglandd2007782007-01-09 09:46:02 -060098};
99
100enum {
101 CLUSTER_ATTR_TCP_PORT = 0,
102 CLUSTER_ATTR_BUFFER_SIZE,
103 CLUSTER_ATTR_RSBTBL_SIZE,
104 CLUSTER_ATTR_LKBTBL_SIZE,
105 CLUSTER_ATTR_DIRTBL_SIZE,
106 CLUSTER_ATTR_RECOVER_TIMER,
107 CLUSTER_ATTR_TOSS_SECS,
108 CLUSTER_ATTR_SCAN_SECS,
109 CLUSTER_ATTR_LOG_DEBUG,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100110 CLUSTER_ATTR_PROTOCOL,
David Teigland3ae1acf2007-05-18 08:59:31 -0500111 CLUSTER_ATTR_TIMEWARN_CS,
David Teiglandd2007782007-01-09 09:46:02 -0600112};
113
114struct cluster_attribute {
115 struct configfs_attribute attr;
116 ssize_t (*show)(struct cluster *, char *);
117 ssize_t (*store)(struct cluster *, const char *, size_t);
118};
119
120static ssize_t cluster_set(struct cluster *cl, unsigned int *cl_field,
Harvey Harrison5416b702008-02-13 16:54:29 -0800121 int *info_field, int check_zero,
David Teiglandd2007782007-01-09 09:46:02 -0600122 const char *buf, size_t len)
123{
124 unsigned int x;
125
126 if (!capable(CAP_SYS_ADMIN))
127 return -EACCES;
128
129 x = simple_strtoul(buf, NULL, 0);
130
131 if (check_zero && !x)
132 return -EINVAL;
133
134 *cl_field = x;
135 *info_field = x;
136
137 return len;
138}
139
David Teiglandd2007782007-01-09 09:46:02 -0600140#define CLUSTER_ATTR(name, check_zero) \
141static ssize_t name##_write(struct cluster *cl, const char *buf, size_t len) \
142{ \
143 return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
144 check_zero, buf, len); \
145} \
146static ssize_t name##_read(struct cluster *cl, char *buf) \
147{ \
148 return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \
149} \
150static struct cluster_attribute cluster_attr_##name = \
151__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)
152
153CLUSTER_ATTR(tcp_port, 1);
154CLUSTER_ATTR(buffer_size, 1);
155CLUSTER_ATTR(rsbtbl_size, 1);
156CLUSTER_ATTR(lkbtbl_size, 1);
157CLUSTER_ATTR(dirtbl_size, 1);
158CLUSTER_ATTR(recover_timer, 1);
159CLUSTER_ATTR(toss_secs, 1);
160CLUSTER_ATTR(scan_secs, 1);
161CLUSTER_ATTR(log_debug, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100162CLUSTER_ATTR(protocol, 0);
David Teigland3ae1acf2007-05-18 08:59:31 -0500163CLUSTER_ATTR(timewarn_cs, 1);
David Teiglandd2007782007-01-09 09:46:02 -0600164
165static struct configfs_attribute *cluster_attrs[] = {
166 [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
167 [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
168 [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
169 [CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr,
170 [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
171 [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
172 [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
173 [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
174 [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100175 [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
David Teigland3ae1acf2007-05-18 08:59:31 -0500176 [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
David Teiglandd2007782007-01-09 09:46:02 -0600177 NULL,
178};
179
David Teiglande7fd4172006-01-18 09:30:29 +0000180enum {
181 COMM_ATTR_NODEID = 0,
182 COMM_ATTR_LOCAL,
183 COMM_ATTR_ADDR,
184};
185
186struct comm_attribute {
187 struct configfs_attribute attr;
188 ssize_t (*show)(struct comm *, char *);
189 ssize_t (*store)(struct comm *, const char *, size_t);
190};
191
192static struct comm_attribute comm_attr_nodeid = {
193 .attr = { .ca_owner = THIS_MODULE,
194 .ca_name = "nodeid",
195 .ca_mode = S_IRUGO | S_IWUSR },
196 .show = comm_nodeid_read,
197 .store = comm_nodeid_write,
198};
199
200static struct comm_attribute comm_attr_local = {
201 .attr = { .ca_owner = THIS_MODULE,
202 .ca_name = "local",
203 .ca_mode = S_IRUGO | S_IWUSR },
204 .show = comm_local_read,
205 .store = comm_local_write,
206};
207
208static struct comm_attribute comm_attr_addr = {
209 .attr = { .ca_owner = THIS_MODULE,
210 .ca_name = "addr",
211 .ca_mode = S_IRUGO | S_IWUSR },
212 .store = comm_addr_write,
213};
214
215static struct configfs_attribute *comm_attrs[] = {
216 [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
217 [COMM_ATTR_LOCAL] = &comm_attr_local.attr,
218 [COMM_ATTR_ADDR] = &comm_attr_addr.attr,
219 NULL,
220};
221
222enum {
223 NODE_ATTR_NODEID = 0,
224 NODE_ATTR_WEIGHT,
225};
226
227struct node_attribute {
228 struct configfs_attribute attr;
229 ssize_t (*show)(struct node *, char *);
230 ssize_t (*store)(struct node *, const char *, size_t);
231};
232
233static struct node_attribute node_attr_nodeid = {
234 .attr = { .ca_owner = THIS_MODULE,
235 .ca_name = "nodeid",
236 .ca_mode = S_IRUGO | S_IWUSR },
237 .show = node_nodeid_read,
238 .store = node_nodeid_write,
239};
240
241static struct node_attribute node_attr_weight = {
242 .attr = { .ca_owner = THIS_MODULE,
243 .ca_name = "weight",
244 .ca_mode = S_IRUGO | S_IWUSR },
245 .show = node_weight_read,
246 .store = node_weight_write,
247};
248
249static struct configfs_attribute *node_attrs[] = {
250 [NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
251 [NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
252 NULL,
253};
254
255struct clusters {
256 struct configfs_subsystem subsys;
257};
258
David Teiglande7fd4172006-01-18 09:30:29 +0000259struct spaces {
260 struct config_group ss_group;
261};
262
263struct space {
264 struct config_group group;
265 struct list_head members;
David Teigland90135922006-01-20 08:47:07 +0000266 struct mutex members_lock;
David Teiglande7fd4172006-01-18 09:30:29 +0000267 int members_count;
268};
269
270struct comms {
271 struct config_group cs_group;
272};
273
274struct comm {
275 struct config_item item;
276 int nodeid;
277 int local;
278 int addr_count;
279 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
280};
281
282struct nodes {
283 struct config_group ns_group;
284};
285
286struct node {
287 struct config_item item;
288 struct list_head list; /* space->members */
289 int nodeid;
290 int weight;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500291 int new;
David Teiglande7fd4172006-01-18 09:30:29 +0000292};
293
294static struct configfs_group_operations clusters_ops = {
295 .make_group = make_cluster,
296 .drop_item = drop_cluster,
297};
298
299static struct configfs_item_operations cluster_ops = {
300 .release = release_cluster,
David Teiglandd2007782007-01-09 09:46:02 -0600301 .show_attribute = show_cluster,
302 .store_attribute = store_cluster,
David Teiglande7fd4172006-01-18 09:30:29 +0000303};
304
305static struct configfs_group_operations spaces_ops = {
306 .make_group = make_space,
307 .drop_item = drop_space,
308};
309
310static struct configfs_item_operations space_ops = {
311 .release = release_space,
312};
313
314static struct configfs_group_operations comms_ops = {
315 .make_item = make_comm,
316 .drop_item = drop_comm,
317};
318
319static struct configfs_item_operations comm_ops = {
320 .release = release_comm,
321 .show_attribute = show_comm,
322 .store_attribute = store_comm,
323};
324
325static struct configfs_group_operations nodes_ops = {
326 .make_item = make_node,
327 .drop_item = drop_node,
328};
329
330static struct configfs_item_operations node_ops = {
331 .release = release_node,
332 .show_attribute = show_node,
333 .store_attribute = store_node,
334};
335
336static struct config_item_type clusters_type = {
337 .ct_group_ops = &clusters_ops,
338 .ct_owner = THIS_MODULE,
339};
340
341static struct config_item_type cluster_type = {
342 .ct_item_ops = &cluster_ops,
David Teiglandd2007782007-01-09 09:46:02 -0600343 .ct_attrs = cluster_attrs,
David Teiglande7fd4172006-01-18 09:30:29 +0000344 .ct_owner = THIS_MODULE,
345};
346
347static struct config_item_type spaces_type = {
348 .ct_group_ops = &spaces_ops,
349 .ct_owner = THIS_MODULE,
350};
351
352static struct config_item_type space_type = {
353 .ct_item_ops = &space_ops,
354 .ct_owner = THIS_MODULE,
355};
356
357static struct config_item_type comms_type = {
358 .ct_group_ops = &comms_ops,
359 .ct_owner = THIS_MODULE,
360};
361
362static struct config_item_type comm_type = {
363 .ct_item_ops = &comm_ops,
364 .ct_attrs = comm_attrs,
365 .ct_owner = THIS_MODULE,
366};
367
368static struct config_item_type nodes_type = {
369 .ct_group_ops = &nodes_ops,
370 .ct_owner = THIS_MODULE,
371};
372
373static struct config_item_type node_type = {
374 .ct_item_ops = &node_ops,
375 .ct_attrs = node_attrs,
376 .ct_owner = THIS_MODULE,
377};
378
379static struct cluster *to_cluster(struct config_item *i)
380{
381 return i ? container_of(to_config_group(i), struct cluster, group):NULL;
382}
383
384static struct space *to_space(struct config_item *i)
385{
386 return i ? container_of(to_config_group(i), struct space, group) : NULL;
387}
388
389static struct comm *to_comm(struct config_item *i)
390{
391 return i ? container_of(i, struct comm, item) : NULL;
392}
393
394static struct node *to_node(struct config_item *i)
395{
396 return i ? container_of(i, struct node, item) : NULL;
397}
398
Joel Becker11c3b792008-06-12 14:00:18 -0700399static int make_cluster(struct config_group *g, const char *name,
400 struct config_group **new_g)
David Teiglande7fd4172006-01-18 09:30:29 +0000401{
402 struct cluster *cl = NULL;
403 struct spaces *sps = NULL;
404 struct comms *cms = NULL;
405 void *gps = NULL;
406
407 cl = kzalloc(sizeof(struct cluster), GFP_KERNEL);
408 gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
409 sps = kzalloc(sizeof(struct spaces), GFP_KERNEL);
410 cms = kzalloc(sizeof(struct comms), GFP_KERNEL);
411
412 if (!cl || !gps || !sps || !cms)
413 goto fail;
414
415 config_group_init_type_name(&cl->group, name, &cluster_type);
416 config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
417 config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
418
419 cl->group.default_groups = gps;
420 cl->group.default_groups[0] = &sps->ss_group;
421 cl->group.default_groups[1] = &cms->cs_group;
422 cl->group.default_groups[2] = NULL;
423
David Teiglandd2007782007-01-09 09:46:02 -0600424 cl->cl_tcp_port = dlm_config.ci_tcp_port;
425 cl->cl_buffer_size = dlm_config.ci_buffer_size;
426 cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
427 cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size;
428 cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
429 cl->cl_recover_timer = dlm_config.ci_recover_timer;
430 cl->cl_toss_secs = dlm_config.ci_toss_secs;
431 cl->cl_scan_secs = dlm_config.ci_scan_secs;
432 cl->cl_log_debug = dlm_config.ci_log_debug;
David Teigland0b7cac02007-05-29 08:47:51 -0500433 cl->cl_protocol = dlm_config.ci_protocol;
David Teigland84d8cd62007-05-29 08:44:23 -0500434 cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
David Teiglandd2007782007-01-09 09:46:02 -0600435
David Teiglande7fd4172006-01-18 09:30:29 +0000436 space_list = &sps->ss_group;
437 comm_list = &cms->cs_group;
Joel Becker11c3b792008-06-12 14:00:18 -0700438 *new_g = &cl->group;
439 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000440
441 fail:
442 kfree(cl);
443 kfree(gps);
444 kfree(sps);
445 kfree(cms);
Joel Becker11c3b792008-06-12 14:00:18 -0700446 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000447}
448
449static void drop_cluster(struct config_group *g, struct config_item *i)
450{
451 struct cluster *cl = to_cluster(i);
452 struct config_item *tmp;
453 int j;
454
455 for (j = 0; cl->group.default_groups[j]; j++) {
456 tmp = &cl->group.default_groups[j]->cg_item;
457 cl->group.default_groups[j] = NULL;
458 config_item_put(tmp);
459 }
460
461 space_list = NULL;
462 comm_list = NULL;
463
464 config_item_put(i);
465}
466
467static void release_cluster(struct config_item *i)
468{
469 struct cluster *cl = to_cluster(i);
470 kfree(cl->group.default_groups);
471 kfree(cl);
472}
473
Joel Becker11c3b792008-06-12 14:00:18 -0700474static int make_space(struct config_group *g, const char *name,
475 struct config_group **new_g)
David Teiglande7fd4172006-01-18 09:30:29 +0000476{
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;
Joel Becker11c3b792008-06-12 14:00:18 -0700498 *new_g = &sp->group;
499 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000500
501 fail:
502 kfree(sp);
503 kfree(gps);
504 kfree(nds);
Joel Becker11c3b792008-06-12 14:00:18 -0700505 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000506}
507
508static void drop_space(struct config_group *g, struct config_item *i)
509{
510 struct space *sp = to_space(i);
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{
527 struct space *sp = to_space(i);
528 kfree(sp->group.default_groups);
529 kfree(sp);
530}
531
Joel Becker11c3b792008-06-12 14:00:18 -0700532static int make_comm(struct config_group *g, const char *name,
533 struct config_item **new_i)
David Teiglande7fd4172006-01-18 09:30:29 +0000534{
535 struct comm *cm;
536
537 cm = kzalloc(sizeof(struct comm), GFP_KERNEL);
538 if (!cm)
Joel Becker11c3b792008-06-12 14:00:18 -0700539 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000540
541 config_item_init_type_name(&cm->item, name, &comm_type);
542 cm->nodeid = -1;
543 cm->local = 0;
544 cm->addr_count = 0;
Joel Becker11c3b792008-06-12 14:00:18 -0700545 *new_i = &cm->item;
546 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000547}
548
549static void drop_comm(struct config_group *g, struct config_item *i)
550{
551 struct comm *cm = to_comm(i);
552 if (local_comm == cm)
553 local_comm = NULL;
David Teigland1c032c02006-04-28 10:50:41 -0400554 dlm_lowcomms_close(cm->nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000555 while (cm->addr_count--)
556 kfree(cm->addr[cm->addr_count]);
557 config_item_put(i);
558}
559
560static void release_comm(struct config_item *i)
561{
562 struct comm *cm = to_comm(i);
563 kfree(cm);
564}
565
Joel Becker11c3b792008-06-12 14:00:18 -0700566static int make_node(struct config_group *g, const char *name,
567 struct config_item **new_i)
David Teiglande7fd4172006-01-18 09:30:29 +0000568{
569 struct space *sp = to_space(g->cg_item.ci_parent);
570 struct node *nd;
571
572 nd = kzalloc(sizeof(struct node), GFP_KERNEL);
573 if (!nd)
Joel Becker11c3b792008-06-12 14:00:18 -0700574 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000575
576 config_item_init_type_name(&nd->item, name, &node_type);
577 nd->nodeid = -1;
578 nd->weight = 1; /* default weight of 1 if none is set */
David Teiglandd44e0fc2008-03-18 14:22:11 -0500579 nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
David Teiglande7fd4172006-01-18 09:30:29 +0000580
David Teigland90135922006-01-20 08:47:07 +0000581 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000582 list_add(&nd->list, &sp->members);
583 sp->members_count++;
David Teigland90135922006-01-20 08:47:07 +0000584 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000585
Joel Becker11c3b792008-06-12 14:00:18 -0700586 *new_i = &nd->item;
587 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000588}
589
590static void drop_node(struct config_group *g, struct config_item *i)
591{
592 struct space *sp = to_space(g->cg_item.ci_parent);
593 struct node *nd = to_node(i);
594
David Teigland90135922006-01-20 08:47:07 +0000595 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000596 list_del(&nd->list);
597 sp->members_count--;
David Teigland90135922006-01-20 08:47:07 +0000598 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000599
600 config_item_put(i);
601}
602
603static void release_node(struct config_item *i)
604{
605 struct node *nd = to_node(i);
606 kfree(nd);
607}
608
609static struct clusters clusters_root = {
610 .subsys = {
611 .su_group = {
612 .cg_item = {
613 .ci_namebuf = "dlm",
614 .ci_type = &clusters_type,
615 },
616 },
617 },
618};
619
Denis Cheng30727172008-02-02 01:53:46 +0800620int __init dlm_config_init(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000621{
622 config_group_init(&clusters_root.subsys.su_group);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700623 mutex_init(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000624 return configfs_register_subsystem(&clusters_root.subsys);
625}
626
627void dlm_config_exit(void)
628{
629 configfs_unregister_subsystem(&clusters_root.subsys);
630}
631
632/*
633 * Functions for user space to read/write attributes
634 */
635
David Teiglandd2007782007-01-09 09:46:02 -0600636static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
637 char *buf)
638{
639 struct cluster *cl = to_cluster(i);
640 struct cluster_attribute *cla =
641 container_of(a, struct cluster_attribute, attr);
642 return cla->show ? cla->show(cl, buf) : 0;
643}
644
645static ssize_t store_cluster(struct config_item *i,
646 struct configfs_attribute *a,
647 const char *buf, size_t len)
648{
649 struct cluster *cl = to_cluster(i);
650 struct cluster_attribute *cla =
651 container_of(a, struct cluster_attribute, attr);
652 return cla->store ? cla->store(cl, buf, len) : -EINVAL;
653}
654
David Teiglande7fd4172006-01-18 09:30:29 +0000655static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
656 char *buf)
657{
658 struct comm *cm = to_comm(i);
659 struct comm_attribute *cma =
660 container_of(a, struct comm_attribute, attr);
661 return cma->show ? cma->show(cm, buf) : 0;
662}
663
664static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
665 const char *buf, size_t len)
666{
667 struct comm *cm = to_comm(i);
668 struct comm_attribute *cma =
669 container_of(a, struct comm_attribute, attr);
670 return cma->store ? cma->store(cm, buf, len) : -EINVAL;
671}
672
673static ssize_t comm_nodeid_read(struct comm *cm, char *buf)
674{
675 return sprintf(buf, "%d\n", cm->nodeid);
676}
677
678static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len)
679{
680 cm->nodeid = simple_strtol(buf, NULL, 0);
681 return len;
682}
683
684static ssize_t comm_local_read(struct comm *cm, char *buf)
685{
686 return sprintf(buf, "%d\n", cm->local);
687}
688
689static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len)
690{
691 cm->local= simple_strtol(buf, NULL, 0);
692 if (cm->local && !local_comm)
693 local_comm = cm;
694 return len;
695}
696
697static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len)
698{
699 struct sockaddr_storage *addr;
700
701 if (len != sizeof(struct sockaddr_storage))
702 return -EINVAL;
703
704 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
705 return -ENOSPC;
706
707 addr = kzalloc(sizeof(*addr), GFP_KERNEL);
708 if (!addr)
709 return -ENOMEM;
710
711 memcpy(addr, buf, len);
712 cm->addr[cm->addr_count++] = addr;
713 return len;
714}
715
716static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
717 char *buf)
718{
719 struct node *nd = to_node(i);
720 struct node_attribute *nda =
721 container_of(a, struct node_attribute, attr);
722 return nda->show ? nda->show(nd, buf) : 0;
723}
724
725static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
726 const char *buf, size_t len)
727{
728 struct node *nd = to_node(i);
729 struct node_attribute *nda =
730 container_of(a, struct node_attribute, attr);
731 return nda->store ? nda->store(nd, buf, len) : -EINVAL;
732}
733
734static ssize_t node_nodeid_read(struct node *nd, char *buf)
735{
736 return sprintf(buf, "%d\n", nd->nodeid);
737}
738
739static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len)
740{
741 nd->nodeid = simple_strtol(buf, NULL, 0);
742 return len;
743}
744
745static ssize_t node_weight_read(struct node *nd, char *buf)
746{
747 return sprintf(buf, "%d\n", nd->weight);
748}
749
750static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len)
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
760static struct space *get_space(char *name)
761{
Satyam Sharma3168b072007-05-08 09:18:58 +0100762 struct config_item *i;
763
David Teiglande7fd4172006-01-18 09:30:29 +0000764 if (!space_list)
765 return NULL;
Satyam Sharma3168b072007-05-08 09:18:58 +0100766
Joel Beckere6bd07a2007-07-06 23:33:17 -0700767 mutex_lock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530768 i = config_group_find_item(space_list, name);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700769 mutex_unlock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3168b072007-05-08 09:18:58 +0100770
771 return to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000772}
773
774static void put_space(struct space *sp)
775{
776 config_item_put(&sp->group.cg_item);
777}
778
779static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr)
780{
781 struct config_item *i;
782 struct comm *cm = NULL;
783 int found = 0;
784
785 if (!comm_list)
786 return NULL;
787
Joel Beckere6bd07a2007-07-06 23:33:17 -0700788 mutex_lock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000789
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;
Satyam Sharma3168b072007-05-08 09:18:58 +0100797 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000798 break;
799 } else {
800 if (!cm->addr_count ||
801 memcmp(cm->addr[0], addr, sizeof(*addr)))
802 continue;
803 found = 1;
Satyam Sharma3168b072007-05-08 09:18:58 +0100804 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000805 break;
806 }
807 }
Joel Beckere6bd07a2007-07-06 23:33:17 -0700808 mutex_unlock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000809
Satyam Sharma3168b072007-05-08 09:18:58 +0100810 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +0000811 cm = NULL;
812 return cm;
813}
814
815static void put_comm(struct comm *cm)
816{
817 config_item_put(&cm->item);
818}
819
820/* caller must free mem */
David Teiglandd44e0fc2008-03-18 14:22:11 -0500821int dlm_nodeid_list(char *lsname, int **ids_out, int *ids_count_out,
822 int **new_out, int *new_count_out)
David Teiglande7fd4172006-01-18 09:30:29 +0000823{
824 struct space *sp;
825 struct node *nd;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500826 int i = 0, rv = 0, ids_count = 0, new_count = 0;
827 int *ids, *new;
David Teiglande7fd4172006-01-18 09:30:29 +0000828
829 sp = get_space(lsname);
830 if (!sp)
831 return -EEXIST;
832
David Teigland90135922006-01-20 08:47:07 +0000833 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000834 if (!sp->members_count) {
David Teiglandd44e0fc2008-03-18 14:22:11 -0500835 rv = -EINVAL;
836 printk(KERN_ERR "dlm: zero members_count\n");
David Teiglande7fd4172006-01-18 09:30:29 +0000837 goto out;
838 }
839
David Teiglandd44e0fc2008-03-18 14:22:11 -0500840 ids_count = sp->members_count;
841
842 ids = kcalloc(ids_count, sizeof(int), GFP_KERNEL);
David Teiglande7fd4172006-01-18 09:30:29 +0000843 if (!ids) {
844 rv = -ENOMEM;
845 goto out;
846 }
847
David Teiglandd44e0fc2008-03-18 14:22:11 -0500848 list_for_each_entry(nd, &sp->members, list) {
David Teiglande7fd4172006-01-18 09:30:29 +0000849 ids[i++] = nd->nodeid;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500850 if (nd->new)
851 new_count++;
852 }
David Teiglande7fd4172006-01-18 09:30:29 +0000853
David Teiglandd44e0fc2008-03-18 14:22:11 -0500854 if (ids_count != i)
855 printk(KERN_ERR "dlm: bad nodeid count %d %d\n", ids_count, i);
David Teiglande7fd4172006-01-18 09:30:29 +0000856
David Teiglandd44e0fc2008-03-18 14:22:11 -0500857 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 }
866
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;
876
877 out_ids:
878 *ids_count_out = ids_count;
David Teiglande7fd4172006-01-18 09:30:29 +0000879 *ids_out = ids;
880 out:
David Teigland90135922006-01-20 08:47:07 +0000881 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000882 put_space(sp);
883 return rv;
884}
885
886int dlm_node_weight(char *lsname, int nodeid)
887{
888 struct space *sp;
889 struct node *nd;
890 int w = -EEXIST;
891
892 sp = get_space(lsname);
893 if (!sp)
894 goto out;
895
David Teigland90135922006-01-20 08:47:07 +0000896 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000897 list_for_each_entry(nd, &sp->members, list) {
898 if (nd->nodeid != nodeid)
899 continue;
900 w = nd->weight;
901 break;
902 }
David Teigland90135922006-01-20 08:47:07 +0000903 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000904 put_space(sp);
905 out:
906 return w;
907}
908
909int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
910{
911 struct comm *cm = get_comm(nodeid, NULL);
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{
923 struct comm *cm = get_comm(0, addr);
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
David Teigland99fc6482007-01-09 09:44:01 -0600956#define DEFAULT_LOG_DEBUG 0
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100957#define DEFAULT_PROTOCOL 0
David Teigland3ae1acf2007-05-18 08:59:31 -0500958#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
David Teiglande7fd4172006-01-18 09:30:29 +0000959
960struct dlm_config_info dlm_config = {
David Teigland68c817a2007-01-09 09:41:48 -0600961 .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,
David Teigland99fc6482007-01-09 09:44:01 -0600968 .ci_scan_secs = DEFAULT_SCAN_SECS,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100969 .ci_log_debug = DEFAULT_LOG_DEBUG,
David Teigland3ae1acf2007-05-18 08:59:31 -0500970 .ci_protocol = DEFAULT_PROTOCOL,
971 .ci_timewarn_cs = DEFAULT_TIMEWARN_CS
David Teiglande7fd4172006-01-18 09:30:29 +0000972};
973