config.c 25.0 KB
Newer Older
1 2 3 4
/******************************************************************************
*******************************************************************************
**
**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
D
David Teigland 已提交
5
**  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
6 7 8 9 10 11 12 13 14 15 16
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/configfs.h>
D
David Teigland 已提交
17 18 19
#include <linux/in.h>
#include <linux/in6.h>
#include <net/ipv6.h>
20 21 22
#include <net/sock.h>

#include "config.h"
23
#include "lowcomms.h"
24 25 26 27 28 29 30 31 32 33 34 35

/*
 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
 * /config/dlm/<cluster>/comms/<comm>/nodeid
 * /config/dlm/<cluster>/comms/<comm>/local
 * /config/dlm/<cluster>/comms/<comm>/addr
 * The <cluster> level is useless, but I haven't figured out how to avoid it.
 */

static struct config_group *space_list;
static struct config_group *comm_list;
D
David Teigland 已提交
36
static struct dlm_comm *local_comm;
37

D
David Teigland 已提交
38 39 40 41 42 43 44 45
struct dlm_clusters;
struct dlm_cluster;
struct dlm_spaces;
struct dlm_space;
struct dlm_comms;
struct dlm_comm;
struct dlm_nodes;
struct dlm_node;
46

47
static struct config_group *make_cluster(struct config_group *, const char *);
48 49
static void drop_cluster(struct config_group *, struct config_item *);
static void release_cluster(struct config_item *);
50
static struct config_group *make_space(struct config_group *, const char *);
51 52
static void drop_space(struct config_group *, struct config_item *);
static void release_space(struct config_item *);
53
static struct config_item *make_comm(struct config_group *, const char *);
54 55
static void drop_comm(struct config_group *, struct config_item *);
static void release_comm(struct config_item *);
56
static struct config_item *make_node(struct config_group *, const char *);
57 58 59
static void drop_node(struct config_group *, struct config_item *);
static void release_node(struct config_item *);

60 61 62 63 64
static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
			    char *buf);
static ssize_t store_cluster(struct config_item *i,
			     struct configfs_attribute *a,
			     const char *buf, size_t len);
65 66 67 68 69 70 71 72 73
static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
			 char *buf);
static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
			  const char *buf, size_t len);
static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
			 char *buf);
static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
			  const char *buf, size_t len);

D
David Teigland 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf);
static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
				size_t len);
static ssize_t comm_local_read(struct dlm_comm *cm, char *buf);
static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
				size_t len);
static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf,
				size_t len);
static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf);
static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
				size_t len);
static ssize_t node_weight_read(struct dlm_node *nd, char *buf);
static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
				size_t len);

struct dlm_cluster {
90 91 92 93 94 95 96 97 98 99
	struct config_group group;
	unsigned int cl_tcp_port;
	unsigned int cl_buffer_size;
	unsigned int cl_rsbtbl_size;
	unsigned int cl_lkbtbl_size;
	unsigned int cl_dirtbl_size;
	unsigned int cl_recover_timer;
	unsigned int cl_toss_secs;
	unsigned int cl_scan_secs;
	unsigned int cl_log_debug;
100
	unsigned int cl_protocol;
101
	unsigned int cl_timewarn_cs;
102 103 104 105 106 107 108 109 110 111 112 113
};

enum {
	CLUSTER_ATTR_TCP_PORT = 0,
	CLUSTER_ATTR_BUFFER_SIZE,
	CLUSTER_ATTR_RSBTBL_SIZE,
	CLUSTER_ATTR_LKBTBL_SIZE,
	CLUSTER_ATTR_DIRTBL_SIZE,
	CLUSTER_ATTR_RECOVER_TIMER,
	CLUSTER_ATTR_TOSS_SECS,
	CLUSTER_ATTR_SCAN_SECS,
	CLUSTER_ATTR_LOG_DEBUG,
114
	CLUSTER_ATTR_PROTOCOL,
115
	CLUSTER_ATTR_TIMEWARN_CS,
116 117 118 119
};

struct cluster_attribute {
	struct configfs_attribute attr;
D
David Teigland 已提交
120 121
	ssize_t (*show)(struct dlm_cluster *, char *);
	ssize_t (*store)(struct dlm_cluster *, const char *, size_t);
122 123
};

D
David Teigland 已提交
124
static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
125
			   int *info_field, int check_zero,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			   const char *buf, size_t len)
{
	unsigned int x;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	x = simple_strtoul(buf, NULL, 0);

	if (check_zero && !x)
		return -EINVAL;

	*cl_field = x;
	*info_field = x;

	return len;
}

#define CLUSTER_ATTR(name, check_zero)                                        \
D
David Teigland 已提交
145
static ssize_t name##_write(struct dlm_cluster *cl, const char *buf, size_t len) \
146 147 148 149
{                                                                             \
	return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name,         \
			   check_zero, buf, len);                             \
}                                                                             \
D
David Teigland 已提交
150
static ssize_t name##_read(struct dlm_cluster *cl, char *buf)                 \
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{                                                                             \
	return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name);               \
}                                                                             \
static struct cluster_attribute cluster_attr_##name =                         \
__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)

CLUSTER_ATTR(tcp_port, 1);
CLUSTER_ATTR(buffer_size, 1);
CLUSTER_ATTR(rsbtbl_size, 1);
CLUSTER_ATTR(lkbtbl_size, 1);
CLUSTER_ATTR(dirtbl_size, 1);
CLUSTER_ATTR(recover_timer, 1);
CLUSTER_ATTR(toss_secs, 1);
CLUSTER_ATTR(scan_secs, 1);
CLUSTER_ATTR(log_debug, 0);
166
CLUSTER_ATTR(protocol, 0);
167
CLUSTER_ATTR(timewarn_cs, 1);
168 169 170 171 172 173 174 175 176 177 178

static struct configfs_attribute *cluster_attrs[] = {
	[CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
	[CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
	[CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
	[CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr,
	[CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
	[CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
	[CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
	[CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
	[CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr,
179
	[CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
180
	[CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
181 182 183
	NULL,
};

184 185 186 187 188 189 190 191
enum {
	COMM_ATTR_NODEID = 0,
	COMM_ATTR_LOCAL,
	COMM_ATTR_ADDR,
};

struct comm_attribute {
	struct configfs_attribute attr;
D
David Teigland 已提交
192 193
	ssize_t (*show)(struct dlm_comm *, char *);
	ssize_t (*store)(struct dlm_comm *, const char *, size_t);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
};

static struct comm_attribute comm_attr_nodeid = {
	.attr   = { .ca_owner = THIS_MODULE,
                    .ca_name = "nodeid",
                    .ca_mode = S_IRUGO | S_IWUSR },
	.show   = comm_nodeid_read,
	.store  = comm_nodeid_write,
};

static struct comm_attribute comm_attr_local = {
	.attr   = { .ca_owner = THIS_MODULE,
                    .ca_name = "local",
                    .ca_mode = S_IRUGO | S_IWUSR },
	.show   = comm_local_read,
	.store  = comm_local_write,
};

static struct comm_attribute comm_attr_addr = {
	.attr   = { .ca_owner = THIS_MODULE,
                    .ca_name = "addr",
                    .ca_mode = S_IRUGO | S_IWUSR },
	.store  = comm_addr_write,
};

static struct configfs_attribute *comm_attrs[] = {
	[COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
	[COMM_ATTR_LOCAL] = &comm_attr_local.attr,
	[COMM_ATTR_ADDR] = &comm_attr_addr.attr,
	NULL,
};

enum {
	NODE_ATTR_NODEID = 0,
	NODE_ATTR_WEIGHT,
};

struct node_attribute {
	struct configfs_attribute attr;
D
David Teigland 已提交
233 234
	ssize_t (*show)(struct dlm_node *, char *);
	ssize_t (*store)(struct dlm_node *, const char *, size_t);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
};

static struct node_attribute node_attr_nodeid = {
	.attr   = { .ca_owner = THIS_MODULE,
                    .ca_name = "nodeid",
                    .ca_mode = S_IRUGO | S_IWUSR },
	.show   = node_nodeid_read,
	.store  = node_nodeid_write,
};

static struct node_attribute node_attr_weight = {
	.attr   = { .ca_owner = THIS_MODULE,
                    .ca_name = "weight",
                    .ca_mode = S_IRUGO | S_IWUSR },
	.show   = node_weight_read,
	.store  = node_weight_write,
};

static struct configfs_attribute *node_attrs[] = {
	[NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
	[NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
	NULL,
};

D
David Teigland 已提交
259
struct dlm_clusters {
260 261 262
	struct configfs_subsystem subsys;
};

D
David Teigland 已提交
263
struct dlm_spaces {
264 265 266
	struct config_group ss_group;
};

D
David Teigland 已提交
267
struct dlm_space {
268 269
	struct config_group group;
	struct list_head members;
270
	struct mutex members_lock;
271 272 273
	int members_count;
};

D
David Teigland 已提交
274
struct dlm_comms {
275 276 277
	struct config_group cs_group;
};

D
David Teigland 已提交
278
struct dlm_comm {
279 280 281 282 283 284 285
	struct config_item item;
	int nodeid;
	int local;
	int addr_count;
	struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
};

D
David Teigland 已提交
286
struct dlm_nodes {
287 288 289
	struct config_group ns_group;
};

D
David Teigland 已提交
290
struct dlm_node {
291 292 293 294
	struct config_item item;
	struct list_head list; /* space->members */
	int nodeid;
	int weight;
295
	int new;
296 297 298 299 300 301 302 303 304
};

static struct configfs_group_operations clusters_ops = {
	.make_group = make_cluster,
	.drop_item = drop_cluster,
};

static struct configfs_item_operations cluster_ops = {
	.release = release_cluster,
305 306
	.show_attribute = show_cluster,
	.store_attribute = store_cluster,
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
};

static struct configfs_group_operations spaces_ops = {
	.make_group = make_space,
	.drop_item = drop_space,
};

static struct configfs_item_operations space_ops = {
	.release = release_space,
};

static struct configfs_group_operations comms_ops = {
	.make_item = make_comm,
	.drop_item = drop_comm,
};

static struct configfs_item_operations comm_ops = {
	.release = release_comm,
	.show_attribute = show_comm,
	.store_attribute = store_comm,
};

static struct configfs_group_operations nodes_ops = {
	.make_item = make_node,
	.drop_item = drop_node,
};

static struct configfs_item_operations node_ops = {
	.release = release_node,
	.show_attribute = show_node,
	.store_attribute = store_node,
};

static struct config_item_type clusters_type = {
	.ct_group_ops = &clusters_ops,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type cluster_type = {
	.ct_item_ops = &cluster_ops,
347
	.ct_attrs = cluster_attrs,
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	.ct_owner = THIS_MODULE,
};

static struct config_item_type spaces_type = {
	.ct_group_ops = &spaces_ops,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type space_type = {
	.ct_item_ops = &space_ops,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type comms_type = {
	.ct_group_ops = &comms_ops,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type comm_type = {
	.ct_item_ops = &comm_ops,
	.ct_attrs = comm_attrs,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type nodes_type = {
	.ct_group_ops = &nodes_ops,
	.ct_owner = THIS_MODULE,
};

static struct config_item_type node_type = {
	.ct_item_ops = &node_ops,
	.ct_attrs = node_attrs,
	.ct_owner = THIS_MODULE,
};

D
David Teigland 已提交
383
static struct dlm_cluster *to_cluster(struct config_item *i)
384
{
D
David Teigland 已提交
385 386
	return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
		   NULL;
387 388
}

D
David Teigland 已提交
389
static struct dlm_space *to_space(struct config_item *i)
390
{
D
David Teigland 已提交
391 392
	return i ? container_of(to_config_group(i), struct dlm_space, group) :
		   NULL;
393 394
}

D
David Teigland 已提交
395
static struct dlm_comm *to_comm(struct config_item *i)
396
{
D
David Teigland 已提交
397
	return i ? container_of(i, struct dlm_comm, item) : NULL;
398 399
}

D
David Teigland 已提交
400
static struct dlm_node *to_node(struct config_item *i)
401
{
D
David Teigland 已提交
402
	return i ? container_of(i, struct dlm_node, item) : NULL;
403 404
}

405 406
static struct config_group *make_cluster(struct config_group *g,
					 const char *name)
407
{
D
David Teigland 已提交
408 409 410
	struct dlm_cluster *cl = NULL;
	struct dlm_spaces *sps = NULL;
	struct dlm_comms *cms = NULL;
411 412
	void *gps = NULL;

D
David Teigland 已提交
413
	cl = kzalloc(sizeof(struct dlm_cluster), GFP_KERNEL);
414
	gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
D
David Teigland 已提交
415 416
	sps = kzalloc(sizeof(struct dlm_spaces), GFP_KERNEL);
	cms = kzalloc(sizeof(struct dlm_comms), GFP_KERNEL);
417 418 419 420 421 422 423 424 425 426 427 428 429

	if (!cl || !gps || !sps || !cms)
		goto fail;

	config_group_init_type_name(&cl->group, name, &cluster_type);
	config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
	config_group_init_type_name(&cms->cs_group, "comms", &comms_type);

	cl->group.default_groups = gps;
	cl->group.default_groups[0] = &sps->ss_group;
	cl->group.default_groups[1] = &cms->cs_group;
	cl->group.default_groups[2] = NULL;

430 431 432 433 434 435 436 437 438
	cl->cl_tcp_port = dlm_config.ci_tcp_port;
	cl->cl_buffer_size = dlm_config.ci_buffer_size;
	cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
	cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size;
	cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
	cl->cl_recover_timer = dlm_config.ci_recover_timer;
	cl->cl_toss_secs = dlm_config.ci_toss_secs;
	cl->cl_scan_secs = dlm_config.ci_scan_secs;
	cl->cl_log_debug = dlm_config.ci_log_debug;
D
David Teigland 已提交
439
	cl->cl_protocol = dlm_config.ci_protocol;
D
David Teigland 已提交
440
	cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
441

442 443
	space_list = &sps->ss_group;
	comm_list = &cms->cs_group;
444
	return &cl->group;
445 446 447 448 449 450

 fail:
	kfree(cl);
	kfree(gps);
	kfree(sps);
	kfree(cms);
451
	return ERR_PTR(-ENOMEM);
452 453 454 455
}

static void drop_cluster(struct config_group *g, struct config_item *i)
{
D
David Teigland 已提交
456
	struct dlm_cluster *cl = to_cluster(i);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	struct config_item *tmp;
	int j;

	for (j = 0; cl->group.default_groups[j]; j++) {
		tmp = &cl->group.default_groups[j]->cg_item;
		cl->group.default_groups[j] = NULL;
		config_item_put(tmp);
	}

	space_list = NULL;
	comm_list = NULL;

	config_item_put(i);
}

static void release_cluster(struct config_item *i)
{
D
David Teigland 已提交
474
	struct dlm_cluster *cl = to_cluster(i);
475 476 477 478
	kfree(cl->group.default_groups);
	kfree(cl);
}

479
static struct config_group *make_space(struct config_group *g, const char *name)
480
{
D
David Teigland 已提交
481 482
	struct dlm_space *sp = NULL;
	struct dlm_nodes *nds = NULL;
483 484
	void *gps = NULL;

D
David Teigland 已提交
485
	sp = kzalloc(sizeof(struct dlm_space), GFP_KERNEL);
486
	gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
D
David Teigland 已提交
487
	nds = kzalloc(sizeof(struct dlm_nodes), GFP_KERNEL);
488 489 490 491 492 493 494 495 496 497 498 499

	if (!sp || !gps || !nds)
		goto fail;

	config_group_init_type_name(&sp->group, name, &space_type);
	config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);

	sp->group.default_groups = gps;
	sp->group.default_groups[0] = &nds->ns_group;
	sp->group.default_groups[1] = NULL;

	INIT_LIST_HEAD(&sp->members);
500
	mutex_init(&sp->members_lock);
501
	sp->members_count = 0;
502
	return &sp->group;
503 504 505 506 507

 fail:
	kfree(sp);
	kfree(gps);
	kfree(nds);
508
	return ERR_PTR(-ENOMEM);
509 510 511 512
}

static void drop_space(struct config_group *g, struct config_item *i)
{
D
David Teigland 已提交
513
	struct dlm_space *sp = to_space(i);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	struct config_item *tmp;
	int j;

	/* assert list_empty(&sp->members) */

	for (j = 0; sp->group.default_groups[j]; j++) {
		tmp = &sp->group.default_groups[j]->cg_item;
		sp->group.default_groups[j] = NULL;
		config_item_put(tmp);
	}

	config_item_put(i);
}

static void release_space(struct config_item *i)
{
D
David Teigland 已提交
530
	struct dlm_space *sp = to_space(i);
531 532 533 534
	kfree(sp->group.default_groups);
	kfree(sp);
}

535
static struct config_item *make_comm(struct config_group *g, const char *name)
536
{
D
David Teigland 已提交
537
	struct dlm_comm *cm;
538

D
David Teigland 已提交
539
	cm = kzalloc(sizeof(struct dlm_comm), GFP_KERNEL);
540
	if (!cm)
541
		return ERR_PTR(-ENOMEM);
542 543 544 545 546

	config_item_init_type_name(&cm->item, name, &comm_type);
	cm->nodeid = -1;
	cm->local = 0;
	cm->addr_count = 0;
547
	return &cm->item;
548 549 550 551
}

static void drop_comm(struct config_group *g, struct config_item *i)
{
D
David Teigland 已提交
552
	struct dlm_comm *cm = to_comm(i);
553 554
	if (local_comm == cm)
		local_comm = NULL;
555
	dlm_lowcomms_close(cm->nodeid);
556 557 558 559 560 561 562
	while (cm->addr_count--)
		kfree(cm->addr[cm->addr_count]);
	config_item_put(i);
}

static void release_comm(struct config_item *i)
{
D
David Teigland 已提交
563
	struct dlm_comm *cm = to_comm(i);
564 565 566
	kfree(cm);
}

567
static struct config_item *make_node(struct config_group *g, const char *name)
568
{
D
David Teigland 已提交
569 570
	struct dlm_space *sp = to_space(g->cg_item.ci_parent);
	struct dlm_node *nd;
571

D
David Teigland 已提交
572
	nd = kzalloc(sizeof(struct dlm_node), GFP_KERNEL);
573
	if (!nd)
574
		return ERR_PTR(-ENOMEM);
575 576 577 578

	config_item_init_type_name(&nd->item, name, &node_type);
	nd->nodeid = -1;
	nd->weight = 1;  /* default weight of 1 if none is set */
579
	nd->new = 1;     /* set to 0 once it's been read by dlm_nodeid_list() */
580

581
	mutex_lock(&sp->members_lock);
582 583
	list_add(&nd->list, &sp->members);
	sp->members_count++;
584
	mutex_unlock(&sp->members_lock);
585

586
	return &nd->item;
587 588 589 590
}

static void drop_node(struct config_group *g, struct config_item *i)
{
D
David Teigland 已提交
591 592
	struct dlm_space *sp = to_space(g->cg_item.ci_parent);
	struct dlm_node *nd = to_node(i);
593

594
	mutex_lock(&sp->members_lock);
595 596
	list_del(&nd->list);
	sp->members_count--;
597
	mutex_unlock(&sp->members_lock);
598 599 600 601 602 603

	config_item_put(i);
}

static void release_node(struct config_item *i)
{
D
David Teigland 已提交
604
	struct dlm_node *nd = to_node(i);
605 606 607
	kfree(nd);
}

D
David Teigland 已提交
608
static struct dlm_clusters clusters_root = {
609 610 611 612 613 614 615 616 617 618
	.subsys = {
		.su_group = {
			.cg_item = {
				.ci_namebuf = "dlm",
				.ci_type = &clusters_type,
			},
		},
	},
};

619
int __init dlm_config_init(void)
620 621
{
	config_group_init(&clusters_root.subsys.su_group);
622
	mutex_init(&clusters_root.subsys.su_mutex);
623 624 625 626 627 628 629 630 631 632 633 634
	return configfs_register_subsystem(&clusters_root.subsys);
}

void dlm_config_exit(void)
{
	configfs_unregister_subsystem(&clusters_root.subsys);
}

/*
 * Functions for user space to read/write attributes
 */

635 636 637
static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
			    char *buf)
{
D
David Teigland 已提交
638
	struct dlm_cluster *cl = to_cluster(i);
639 640 641 642 643 644 645 646 647
	struct cluster_attribute *cla =
			container_of(a, struct cluster_attribute, attr);
	return cla->show ? cla->show(cl, buf) : 0;
}

static ssize_t store_cluster(struct config_item *i,
			     struct configfs_attribute *a,
			     const char *buf, size_t len)
{
D
David Teigland 已提交
648
	struct dlm_cluster *cl = to_cluster(i);
649 650 651 652 653
	struct cluster_attribute *cla =
		container_of(a, struct cluster_attribute, attr);
	return cla->store ? cla->store(cl, buf, len) : -EINVAL;
}

654 655 656
static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
			 char *buf)
{
D
David Teigland 已提交
657
	struct dlm_comm *cm = to_comm(i);
658 659 660 661 662 663 664 665
	struct comm_attribute *cma =
			container_of(a, struct comm_attribute, attr);
	return cma->show ? cma->show(cm, buf) : 0;
}

static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
			  const char *buf, size_t len)
{
D
David Teigland 已提交
666
	struct dlm_comm *cm = to_comm(i);
667 668 669 670 671
	struct comm_attribute *cma =
		container_of(a, struct comm_attribute, attr);
	return cma->store ? cma->store(cm, buf, len) : -EINVAL;
}

D
David Teigland 已提交
672
static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf)
673 674 675 676
{
	return sprintf(buf, "%d\n", cm->nodeid);
}

D
David Teigland 已提交
677 678
static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
				 size_t len)
679 680 681 682 683
{
	cm->nodeid = simple_strtol(buf, NULL, 0);
	return len;
}

D
David Teigland 已提交
684
static ssize_t comm_local_read(struct dlm_comm *cm, char *buf)
685 686 687 688
{
	return sprintf(buf, "%d\n", cm->local);
}

D
David Teigland 已提交
689 690
static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
				size_t len)
691 692 693 694 695 696 697
{
	cm->local= simple_strtol(buf, NULL, 0);
	if (cm->local && !local_comm)
		local_comm = cm;
	return len;
}

D
David Teigland 已提交
698
static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf, size_t len)
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
	struct sockaddr_storage *addr;

	if (len != sizeof(struct sockaddr_storage))
		return -EINVAL;

	if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
		return -ENOSPC;

	addr = kzalloc(sizeof(*addr), GFP_KERNEL);
	if (!addr)
		return -ENOMEM;

	memcpy(addr, buf, len);
	cm->addr[cm->addr_count++] = addr;
	return len;
}

static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
			 char *buf)
{
D
David Teigland 已提交
720
	struct dlm_node *nd = to_node(i);
721 722 723 724 725 726 727 728
	struct node_attribute *nda =
			container_of(a, struct node_attribute, attr);
	return nda->show ? nda->show(nd, buf) : 0;
}

static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
			  const char *buf, size_t len)
{
D
David Teigland 已提交
729
	struct dlm_node *nd = to_node(i);
730 731 732 733 734
	struct node_attribute *nda =
		container_of(a, struct node_attribute, attr);
	return nda->store ? nda->store(nd, buf, len) : -EINVAL;
}

D
David Teigland 已提交
735
static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf)
736 737 738 739
{
	return sprintf(buf, "%d\n", nd->nodeid);
}

D
David Teigland 已提交
740 741
static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
				 size_t len)
742 743 744 745 746
{
	nd->nodeid = simple_strtol(buf, NULL, 0);
	return len;
}

D
David Teigland 已提交
747
static ssize_t node_weight_read(struct dlm_node *nd, char *buf)
748 749 750 751
{
	return sprintf(buf, "%d\n", nd->weight);
}

D
David Teigland 已提交
752 753
static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
				 size_t len)
754 755 756 757 758 759 760 761 762
{
	nd->weight = simple_strtol(buf, NULL, 0);
	return len;
}

/*
 * Functions for the dlm to get the info that's been configured
 */

D
David Teigland 已提交
763
static struct dlm_space *get_space(char *name)
764
{
S
Satyam Sharma 已提交
765 766
	struct config_item *i;

767 768
	if (!space_list)
		return NULL;
S
Satyam Sharma 已提交
769

770
	mutex_lock(&space_list->cg_subsys->su_mutex);
771
	i = config_group_find_item(space_list, name);
772
	mutex_unlock(&space_list->cg_subsys->su_mutex);
S
Satyam Sharma 已提交
773 774

	return to_space(i);
775 776
}

D
David Teigland 已提交
777
static void put_space(struct dlm_space *sp)
778 779 780 781
{
	config_item_put(&sp->group.cg_item);
}

D
David Teigland 已提交
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
{
	switch (x->ss_family) {
	case AF_INET: {
		struct sockaddr_in *sinx = (struct sockaddr_in *)x;
		struct sockaddr_in *siny = (struct sockaddr_in *)y;
		if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
			return 0;
		if (sinx->sin_port != siny->sin_port)
			return 0;
		break;
	}
	case AF_INET6: {
		struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
		struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
		if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
			return 0;
		if (sinx->sin6_port != siny->sin6_port)
			return 0;
		break;
	}
	default:
		return 0;
	}
	return 1;
}

D
David Teigland 已提交
809
static struct dlm_comm *get_comm(int nodeid, struct sockaddr_storage *addr)
810 811
{
	struct config_item *i;
D
David Teigland 已提交
812
	struct dlm_comm *cm = NULL;
813 814 815 816 817
	int found = 0;

	if (!comm_list)
		return NULL;

818
	mutex_lock(&clusters_root.subsys.su_mutex);
819 820 821 822 823 824 825 826

	list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
		cm = to_comm(i);

		if (nodeid) {
			if (cm->nodeid != nodeid)
				continue;
			found = 1;
S
Satyam Sharma 已提交
827
			config_item_get(i);
828 829
			break;
		} else {
D
David Teigland 已提交
830
			if (!cm->addr_count || !addr_compare(cm->addr[0], addr))
831 832
				continue;
			found = 1;
S
Satyam Sharma 已提交
833
			config_item_get(i);
834 835 836
			break;
		}
	}
837
	mutex_unlock(&clusters_root.subsys.su_mutex);
838

S
Satyam Sharma 已提交
839
	if (!found)
840 841 842 843
		cm = NULL;
	return cm;
}

D
David Teigland 已提交
844
static void put_comm(struct dlm_comm *cm)
845 846 847 848 849
{
	config_item_put(&cm->item);
}

/* caller must free mem */
850 851
int dlm_nodeid_list(char *lsname, int **ids_out, int *ids_count_out,
		    int **new_out, int *new_count_out)
852
{
D
David Teigland 已提交
853 854
	struct dlm_space *sp;
	struct dlm_node *nd;
855 856
	int i = 0, rv = 0, ids_count = 0, new_count = 0;
	int *ids, *new;
857 858 859 860 861

	sp = get_space(lsname);
	if (!sp)
		return -EEXIST;

862
	mutex_lock(&sp->members_lock);
863
	if (!sp->members_count) {
864 865
		rv = -EINVAL;
		printk(KERN_ERR "dlm: zero members_count\n");
866 867 868
		goto out;
	}

869 870 871
	ids_count = sp->members_count;

	ids = kcalloc(ids_count, sizeof(int), GFP_KERNEL);
872 873 874 875 876
	if (!ids) {
		rv = -ENOMEM;
		goto out;
	}

877
	list_for_each_entry(nd, &sp->members, list) {
878
		ids[i++] = nd->nodeid;
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
		if (nd->new)
			new_count++;
	}

	if (ids_count != i)
		printk(KERN_ERR "dlm: bad nodeid count %d %d\n", ids_count, i);

	if (!new_count)
		goto out_ids;

	new = kcalloc(new_count, sizeof(int), GFP_KERNEL);
	if (!new) {
		kfree(ids);
		rv = -ENOMEM;
		goto out;
	}
895

896 897 898 899 900 901 902 903 904
	i = 0;
	list_for_each_entry(nd, &sp->members, list) {
		if (nd->new) {
			new[i++] = nd->nodeid;
			nd->new = 0;
		}
	}
	*new_count_out = new_count;
	*new_out = new;
905

906 907
 out_ids:
	*ids_count_out = ids_count;
908 909
	*ids_out = ids;
 out:
910
	mutex_unlock(&sp->members_lock);
911 912 913 914 915 916
	put_space(sp);
	return rv;
}

int dlm_node_weight(char *lsname, int nodeid)
{
D
David Teigland 已提交
917 918
	struct dlm_space *sp;
	struct dlm_node *nd;
919 920 921 922 923 924
	int w = -EEXIST;

	sp = get_space(lsname);
	if (!sp)
		goto out;

925
	mutex_lock(&sp->members_lock);
926 927 928 929 930 931
	list_for_each_entry(nd, &sp->members, list) {
		if (nd->nodeid != nodeid)
			continue;
		w = nd->weight;
		break;
	}
932
	mutex_unlock(&sp->members_lock);
933 934 935 936 937 938 939
	put_space(sp);
 out:
	return w;
}

int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
{
D
David Teigland 已提交
940
	struct dlm_comm *cm = get_comm(nodeid, NULL);
941 942 943 944 945 946 947 948 949 950 951
	if (!cm)
		return -EEXIST;
	if (!cm->addr_count)
		return -ENOENT;
	memcpy(addr, cm->addr[0], sizeof(*addr));
	put_comm(cm);
	return 0;
}

int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
{
D
David Teigland 已提交
952
	struct dlm_comm *cm = get_comm(0, addr);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	if (!cm)
		return -EEXIST;
	*nodeid = cm->nodeid;
	put_comm(cm);
	return 0;
}

int dlm_our_nodeid(void)
{
	return local_comm ? local_comm->nodeid : 0;
}

/* num 0 is first addr, num 1 is second addr */
int dlm_our_addr(struct sockaddr_storage *addr, int num)
{
	if (!local_comm)
		return -1;
	if (num + 1 > local_comm->addr_count)
		return -1;
	memcpy(addr, local_comm->addr[num], sizeof(*addr));
	return 0;
}

/* Config file defaults */
#define DEFAULT_TCP_PORT       21064
#define DEFAULT_BUFFER_SIZE     4096
#define DEFAULT_RSBTBL_SIZE      256
#define DEFAULT_LKBTBL_SIZE     1024
#define DEFAULT_DIRTBL_SIZE      512
#define DEFAULT_RECOVER_TIMER      5
#define DEFAULT_TOSS_SECS         10
#define DEFAULT_SCAN_SECS          5
985
#define DEFAULT_LOG_DEBUG          0
986
#define DEFAULT_PROTOCOL           0
987
#define DEFAULT_TIMEWARN_CS      500 /* 5 sec = 500 centiseconds */
988 989

struct dlm_config_info dlm_config = {
990 991 992 993 994 995 996
	.ci_tcp_port = DEFAULT_TCP_PORT,
	.ci_buffer_size = DEFAULT_BUFFER_SIZE,
	.ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
	.ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,
	.ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
	.ci_recover_timer = DEFAULT_RECOVER_TIMER,
	.ci_toss_secs = DEFAULT_TOSS_SECS,
997
	.ci_scan_secs = DEFAULT_SCAN_SECS,
998
	.ci_log_debug = DEFAULT_LOG_DEBUG,
999 1000
	.ci_protocol = DEFAULT_PROTOCOL,
	.ci_timewarn_cs = DEFAULT_TIMEWARN_CS
1001 1002
};