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