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