1/******************************************************************************
2*******************************************************************************
3**
4**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5**  Copyright (C) 2004-2007 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 <net/sock.h>
18
19#include "config.h"
20#include "lowcomms.h"
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;
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
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);
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
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
81struct 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;
92	unsigned int cl_protocol;
93};
94
95enum {
96	CLUSTER_ATTR_TCP_PORT = 0,
97	CLUSTER_ATTR_BUFFER_SIZE,
98	CLUSTER_ATTR_RSBTBL_SIZE,
99	CLUSTER_ATTR_LKBTBL_SIZE,
100	CLUSTER_ATTR_DIRTBL_SIZE,
101	CLUSTER_ATTR_RECOVER_TIMER,
102	CLUSTER_ATTR_TOSS_SECS,
103	CLUSTER_ATTR_SCAN_SECS,
104	CLUSTER_ATTR_LOG_DEBUG,
105	CLUSTER_ATTR_PROTOCOL,
106};
107
108struct cluster_attribute {
109	struct configfs_attribute attr;
110	ssize_t (*show)(struct cluster *, char *);
111	ssize_t (*store)(struct cluster *, const char *, size_t);
112};
113
114static ssize_t cluster_set(struct cluster *cl, unsigned int *cl_field,
115			   unsigned int *info_field, int check_zero,
116			   const char *buf, size_t len)
117{
118	unsigned int x;
119
120	if (!capable(CAP_SYS_ADMIN))
121		return -EACCES;
122
123	x = simple_strtoul(buf, NULL, 0);
124
125	if (check_zero && !x)
126		return -EINVAL;
127
128	*cl_field = x;
129	*info_field = x;
130
131	return len;
132}
133
134#define __CONFIGFS_ATTR(_name,_mode,_read,_write) {                           \
135	.attr   = { .ca_name = __stringify(_name),                            \
136		    .ca_mode = _mode,                                         \
137		    .ca_owner = THIS_MODULE },                                \
138	.show   = _read,                                                      \
139	.store  = _write,                                                     \
140}
141
142#define CLUSTER_ATTR(name, check_zero)                                        \
143static ssize_t name##_write(struct cluster *cl, const char *buf, size_t len)  \
144{                                                                             \
145	return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name,         \
146			   check_zero, buf, len);                             \
147}                                                                             \
148static ssize_t name##_read(struct cluster *cl, char *buf)                     \
149{                                                                             \
150	return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name);               \
151}                                                                             \
152static struct cluster_attribute cluster_attr_##name =                         \
153__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)
154
155CLUSTER_ATTR(tcp_port, 1);
156CLUSTER_ATTR(buffer_size, 1);
157CLUSTER_ATTR(rsbtbl_size, 1);
158CLUSTER_ATTR(lkbtbl_size, 1);
159CLUSTER_ATTR(dirtbl_size, 1);
160CLUSTER_ATTR(recover_timer, 1);
161CLUSTER_ATTR(toss_secs, 1);
162CLUSTER_ATTR(scan_secs, 1);
163CLUSTER_ATTR(log_debug, 0);
164CLUSTER_ATTR(protocol, 0);
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,
176	[CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
177	NULL,
178};
179
180enum {
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
259struct spaces {
260	struct config_group ss_group;
261};
262
263struct space {
264	struct config_group group;
265	struct list_head members;
266	struct mutex members_lock;
267	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;
291};
292
293static struct configfs_group_operations clusters_ops = {
294	.make_group = make_cluster,
295	.drop_item = drop_cluster,
296};
297
298static struct configfs_item_operations cluster_ops = {
299	.release = release_cluster,
300	.show_attribute = show_cluster,
301	.store_attribute = store_cluster,
302};
303
304static struct configfs_group_operations spaces_ops = {
305	.make_group = make_space,
306	.drop_item = drop_space,
307};
308
309static struct configfs_item_operations space_ops = {
310	.release = release_space,
311};
312
313static struct configfs_group_operations comms_ops = {
314	.make_item = make_comm,
315	.drop_item = drop_comm,
316};
317
318static struct configfs_item_operations comm_ops = {
319	.release = release_comm,
320	.show_attribute = show_comm,
321	.store_attribute = store_comm,
322};
323
324static struct configfs_group_operations nodes_ops = {
325	.make_item = make_node,
326	.drop_item = drop_node,
327};
328
329static struct configfs_item_operations node_ops = {
330	.release = release_node,
331	.show_attribute = show_node,
332	.store_attribute = store_node,
333};
334
335static struct config_item_type clusters_type = {
336	.ct_group_ops = &clusters_ops,
337	.ct_owner = THIS_MODULE,
338};
339
340static struct config_item_type cluster_type = {
341	.ct_item_ops = &cluster_ops,
342	.ct_attrs = cluster_attrs,
343	.ct_owner = THIS_MODULE,
344};
345
346static struct config_item_type spaces_type = {
347	.ct_group_ops = &spaces_ops,
348	.ct_owner = THIS_MODULE,
349};
350
351static struct config_item_type space_type = {
352	.ct_item_ops = &space_ops,
353	.ct_owner = THIS_MODULE,
354};
355
356static struct config_item_type comms_type = {
357	.ct_group_ops = &comms_ops,
358	.ct_owner = THIS_MODULE,
359};
360
361static struct config_item_type comm_type = {
362	.ct_item_ops = &comm_ops,
363	.ct_attrs = comm_attrs,
364	.ct_owner = THIS_MODULE,
365};
366
367static struct config_item_type nodes_type = {
368	.ct_group_ops = &nodes_ops,
369	.ct_owner = THIS_MODULE,
370};
371
372static struct config_item_type node_type = {
373	.ct_item_ops = &node_ops,
374	.ct_attrs = node_attrs,
375	.ct_owner = THIS_MODULE,
376};
377
378static struct cluster *to_cluster(struct config_item *i)
379{
380	return i ? container_of(to_config_group(i), struct cluster, group):NULL;
381}
382
383static struct space *to_space(struct config_item *i)
384{
385	return i ? container_of(to_config_group(i), struct space, group) : NULL;
386}
387
388static struct comm *to_comm(struct config_item *i)
389{
390	return i ? container_of(i, struct comm, item) : NULL;
391}
392
393static struct node *to_node(struct config_item *i)
394{
395	return i ? container_of(i, struct node, item) : NULL;
396}
397
398static struct config_group *make_cluster(struct config_group *g,
399					 const char *name)
400{
401	struct cluster *cl = NULL;
402	struct spaces *sps = NULL;
403	struct comms *cms = NULL;
404	void *gps = NULL;
405
406	cl = kzalloc(sizeof(struct cluster), GFP_KERNEL);
407	gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
408	sps = kzalloc(sizeof(struct spaces), GFP_KERNEL);
409	cms = kzalloc(sizeof(struct comms), GFP_KERNEL);
410
411	if (!cl || !gps || !sps || !cms)
412		goto fail;
413
414	config_group_init_type_name(&cl->group, name, &cluster_type);
415	config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
416	config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
417
418	cl->group.default_groups = gps;
419	cl->group.default_groups[0] = &sps->ss_group;
420	cl->group.default_groups[1] = &cms->cs_group;
421	cl->group.default_groups[2] = NULL;
422
423	cl->cl_tcp_port = dlm_config.ci_tcp_port;
424	cl->cl_buffer_size = dlm_config.ci_buffer_size;
425	cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
426	cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size;
427	cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
428	cl->cl_recover_timer = dlm_config.ci_recover_timer;
429	cl->cl_toss_secs = dlm_config.ci_toss_secs;
430	cl->cl_scan_secs = dlm_config.ci_scan_secs;
431	cl->cl_log_debug = dlm_config.ci_log_debug;
432
433	space_list = &sps->ss_group;
434	comm_list = &cms->cs_group;
435	return &cl->group;
436
437 fail:
438	kfree(cl);
439	kfree(gps);
440	kfree(sps);
441	kfree(cms);
442	return NULL;
443}
444
445static void drop_cluster(struct config_group *g, struct config_item *i)
446{
447	struct cluster *cl = to_cluster(i);
448	struct config_item *tmp;
449	int j;
450
451	for (j = 0; cl->group.default_groups[j]; j++) {
452		tmp = &cl->group.default_groups[j]->cg_item;
453		cl->group.default_groups[j] = NULL;
454		config_item_put(tmp);
455	}
456
457	space_list = NULL;
458	comm_list = NULL;
459
460	config_item_put(i);
461}
462
463static void release_cluster(struct config_item *i)
464{
465	struct cluster *cl = to_cluster(i);
466	kfree(cl->group.default_groups);
467	kfree(cl);
468}
469
470static struct config_group *make_space(struct config_group *g, const char *name)
471{
472	struct space *sp = NULL;
473	struct nodes *nds = NULL;
474	void *gps = NULL;
475
476	sp = kzalloc(sizeof(struct space), GFP_KERNEL);
477	gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
478	nds = kzalloc(sizeof(struct nodes), GFP_KERNEL);
479
480	if (!sp || !gps || !nds)
481		goto fail;
482
483	config_group_init_type_name(&sp->group, name, &space_type);
484	config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
485
486	sp->group.default_groups = gps;
487	sp->group.default_groups[0] = &nds->ns_group;
488	sp->group.default_groups[1] = NULL;
489
490	INIT_LIST_HEAD(&sp->members);
491	mutex_init(&sp->members_lock);
492	sp->members_count = 0;
493	return &sp->group;
494
495 fail:
496	kfree(sp);
497	kfree(gps);
498	kfree(nds);
499	return NULL;
500}
501
502static void drop_space(struct config_group *g, struct config_item *i)
503{
504	struct space *sp = to_space(i);
505	struct config_item *tmp;
506	int j;
507
508	/* assert list_empty(&sp->members) */
509
510	for (j = 0; sp->group.default_groups[j]; j++) {
511		tmp = &sp->group.default_groups[j]->cg_item;
512		sp->group.default_groups[j] = NULL;
513		config_item_put(tmp);
514	}
515
516	config_item_put(i);
517}
518
519static void release_space(struct config_item *i)
520{
521	struct space *sp = to_space(i);
522	kfree(sp->group.default_groups);
523	kfree(sp);
524}
525
526static struct config_item *make_comm(struct config_group *g, const char *name)
527{
528	struct comm *cm;
529
530	cm = kzalloc(sizeof(struct comm), GFP_KERNEL);
531	if (!cm)
532		return NULL;
533
534	config_item_init_type_name(&cm->item, name, &comm_type);
535	cm->nodeid = -1;
536	cm->local = 0;
537	cm->addr_count = 0;
538	return &cm->item;
539}
540
541static void drop_comm(struct config_group *g, struct config_item *i)
542{
543	struct comm *cm = to_comm(i);
544	if (local_comm == cm)
545		local_comm = NULL;
546	dlm_lowcomms_close(cm->nodeid);
547	while (cm->addr_count--)
548		kfree(cm->addr[cm->addr_count]);
549	config_item_put(i);
550}
551
552static void release_comm(struct config_item *i)
553{
554	struct comm *cm = to_comm(i);
555	kfree(cm);
556}
557
558static struct config_item *make_node(struct config_group *g, const char *name)
559{
560	struct space *sp = to_space(g->cg_item.ci_parent);
561	struct node *nd;
562
563	nd = kzalloc(sizeof(struct node), GFP_KERNEL);
564	if (!nd)
565		return NULL;
566
567	config_item_init_type_name(&nd->item, name, &node_type);
568	nd->nodeid = -1;
569	nd->weight = 1;  /* default weight of 1 if none is set */
570
571	mutex_lock(&sp->members_lock);
572	list_add(&nd->list, &sp->members);
573	sp->members_count++;
574	mutex_unlock(&sp->members_lock);
575
576	return &nd->item;
577}
578
579static void drop_node(struct config_group *g, struct config_item *i)
580{
581	struct space *sp = to_space(g->cg_item.ci_parent);
582	struct node *nd = to_node(i);
583
584	mutex_lock(&sp->members_lock);
585	list_del(&nd->list);
586	sp->members_count--;
587	mutex_unlock(&sp->members_lock);
588
589	config_item_put(i);
590}
591
592static void release_node(struct config_item *i)
593{
594	struct node *nd = to_node(i);
595	kfree(nd);
596}
597
598static struct clusters clusters_root = {
599	.subsys = {
600		.su_group = {
601			.cg_item = {
602				.ci_namebuf = "dlm",
603				.ci_type = &clusters_type,
604			},
605		},
606	},
607};
608
609int dlm_config_init(void)
610{
611	config_group_init(&clusters_root.subsys.su_group);
612	init_MUTEX(&clusters_root.subsys.su_sem);
613	return configfs_register_subsystem(&clusters_root.subsys);
614}
615
616void dlm_config_exit(void)
617{
618	configfs_unregister_subsystem(&clusters_root.subsys);
619}
620
621/*
622 * Functions for user space to read/write attributes
623 */
624
625static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
626			    char *buf)
627{
628	struct cluster *cl = to_cluster(i);
629	struct cluster_attribute *cla =
630			container_of(a, struct cluster_attribute, attr);
631	return cla->show ? cla->show(cl, buf) : 0;
632}
633
634static ssize_t store_cluster(struct config_item *i,
635			     struct configfs_attribute *a,
636			     const char *buf, size_t len)
637{
638	struct cluster *cl = to_cluster(i);
639	struct cluster_attribute *cla =
640		container_of(a, struct cluster_attribute, attr);
641	return cla->store ? cla->store(cl, buf, len) : -EINVAL;
642}
643
644static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
645			 char *buf)
646{
647	struct comm *cm = to_comm(i);
648	struct comm_attribute *cma =
649			container_of(a, struct comm_attribute, attr);
650	return cma->show ? cma->show(cm, buf) : 0;
651}
652
653static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
654			  const char *buf, size_t len)
655{
656	struct comm *cm = to_comm(i);
657	struct comm_attribute *cma =
658		container_of(a, struct comm_attribute, attr);
659	return cma->store ? cma->store(cm, buf, len) : -EINVAL;
660}
661
662static ssize_t comm_nodeid_read(struct comm *cm, char *buf)
663{
664	return sprintf(buf, "%d\n", cm->nodeid);
665}
666
667static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len)
668{
669	cm->nodeid = simple_strtol(buf, NULL, 0);
670	return len;
671}
672
673static ssize_t comm_local_read(struct comm *cm, char *buf)
674{
675	return sprintf(buf, "%d\n", cm->local);
676}
677
678static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len)
679{
680	cm->local= simple_strtol(buf, NULL, 0);
681	if (cm->local && !local_comm)
682		local_comm = cm;
683	return len;
684}
685
686static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len)
687{
688	struct sockaddr_storage *addr;
689
690	if (len != sizeof(struct sockaddr_storage))
691		return -EINVAL;
692
693	if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
694		return -ENOSPC;
695
696	addr = kzalloc(sizeof(*addr), GFP_KERNEL);
697	if (!addr)
698		return -ENOMEM;
699
700	memcpy(addr, buf, len);
701	cm->addr[cm->addr_count++] = addr;
702	return len;
703}
704
705static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
706			 char *buf)
707{
708	struct node *nd = to_node(i);
709	struct node_attribute *nda =
710			container_of(a, struct node_attribute, attr);
711	return nda->show ? nda->show(nd, buf) : 0;
712}
713
714static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
715			  const char *buf, size_t len)
716{
717	struct node *nd = to_node(i);
718	struct node_attribute *nda =
719		container_of(a, struct node_attribute, attr);
720	return nda->store ? nda->store(nd, buf, len) : -EINVAL;
721}
722
723static ssize_t node_nodeid_read(struct node *nd, char *buf)
724{
725	return sprintf(buf, "%d\n", nd->nodeid);
726}
727
728static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len)
729{
730	nd->nodeid = simple_strtol(buf, NULL, 0);
731	return len;
732}
733
734static ssize_t node_weight_read(struct node *nd, char *buf)
735{
736	return sprintf(buf, "%d\n", nd->weight);
737}
738
739static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len)
740{
741	nd->weight = simple_strtol(buf, NULL, 0);
742	return len;
743}
744
745/*
746 * Functions for the dlm to get the info that's been configured
747 */
748
749static struct space *get_space(char *name)
750{
751	if (!space_list)
752		return NULL;
753	return to_space(config_group_find_obj(space_list, name));
754}
755
756static void put_space(struct space *sp)
757{
758	config_item_put(&sp->group.cg_item);
759}
760
761static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr)
762{
763	struct config_item *i;
764	struct comm *cm = NULL;
765	int found = 0;
766
767	if (!comm_list)
768		return NULL;
769
770	down(&clusters_root.subsys.su_sem);
771
772	list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
773		cm = to_comm(i);
774
775		if (nodeid) {
776			if (cm->nodeid != nodeid)
777				continue;
778			found = 1;
779			break;
780		} else {
781			if (!cm->addr_count ||
782			    memcmp(cm->addr[0], addr, sizeof(*addr)))
783				continue;
784			found = 1;
785			break;
786		}
787	}
788	up(&clusters_root.subsys.su_sem);
789
790	if (found)
791		config_item_get(i);
792	else
793		cm = NULL;
794	return cm;
795}
796
797static void put_comm(struct comm *cm)
798{
799	config_item_put(&cm->item);
800}
801
802/* caller must free mem */
803int dlm_nodeid_list(char *lsname, int **ids_out)
804{
805	struct space *sp;
806	struct node *nd;
807	int i = 0, rv = 0;
808	int *ids;
809
810	sp = get_space(lsname);
811	if (!sp)
812		return -EEXIST;
813
814	mutex_lock(&sp->members_lock);
815	if (!sp->members_count) {
816		rv = 0;
817		goto out;
818	}
819
820	ids = kcalloc(sp->members_count, sizeof(int), GFP_KERNEL);
821	if (!ids) {
822		rv = -ENOMEM;
823		goto out;
824	}
825
826	rv = sp->members_count;
827	list_for_each_entry(nd, &sp->members, list)
828		ids[i++] = nd->nodeid;
829
830	if (rv != i)
831		printk("bad nodeid count %d %d\n", rv, i);
832
833	*ids_out = ids;
834 out:
835	mutex_unlock(&sp->members_lock);
836	put_space(sp);
837	return rv;
838}
839
840int dlm_node_weight(char *lsname, int nodeid)
841{
842	struct space *sp;
843	struct node *nd;
844	int w = -EEXIST;
845
846	sp = get_space(lsname);
847	if (!sp)
848		goto out;
849
850	mutex_lock(&sp->members_lock);
851	list_for_each_entry(nd, &sp->members, list) {
852		if (nd->nodeid != nodeid)
853			continue;
854		w = nd->weight;
855		break;
856	}
857	mutex_unlock(&sp->members_lock);
858	put_space(sp);
859 out:
860	return w;
861}
862
863int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
864{
865	struct comm *cm = get_comm(nodeid, NULL);
866	if (!cm)
867		return -EEXIST;
868	if (!cm->addr_count)
869		return -ENOENT;
870	memcpy(addr, cm->addr[0], sizeof(*addr));
871	put_comm(cm);
872	return 0;
873}
874
875int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
876{
877	struct comm *cm = get_comm(0, addr);
878	if (!cm)
879		return -EEXIST;
880	*nodeid = cm->nodeid;
881	put_comm(cm);
882	return 0;
883}
884
885int dlm_our_nodeid(void)
886{
887	return local_comm ? local_comm->nodeid : 0;
888}
889
890/* num 0 is first addr, num 1 is second addr */
891int dlm_our_addr(struct sockaddr_storage *addr, int num)
892{
893	if (!local_comm)
894		return -1;
895	if (num + 1 > local_comm->addr_count)
896		return -1;
897	memcpy(addr, local_comm->addr[num], sizeof(*addr));
898	return 0;
899}
900
901/* Config file defaults */
902#define DEFAULT_TCP_PORT       21064
903#define DEFAULT_BUFFER_SIZE     4096
904#define DEFAULT_RSBTBL_SIZE      256
905#define DEFAULT_LKBTBL_SIZE     1024
906#define DEFAULT_DIRTBL_SIZE      512
907#define DEFAULT_RECOVER_TIMER      5
908#define DEFAULT_TOSS_SECS         10
909#define DEFAULT_SCAN_SECS          5
910#define DEFAULT_LOG_DEBUG          0
911#define DEFAULT_PROTOCOL           0
912
913struct dlm_config_info dlm_config = {
914	.ci_tcp_port = DEFAULT_TCP_PORT,
915	.ci_buffer_size = DEFAULT_BUFFER_SIZE,
916	.ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
917	.ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,
918	.ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
919	.ci_recover_timer = DEFAULT_RECOVER_TIMER,
920	.ci_toss_secs = DEFAULT_TOSS_SECS,
921	.ci_scan_secs = DEFAULT_SCAN_SECS,
922	.ci_log_debug = DEFAULT_LOG_DEBUG,
923	.ci_protocol = DEFAULT_PROTOCOL
924};
925