xref: /openbmc/linux/fs/ocfs2/cluster/nodemanager.c (revision a2cce7a9)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * Copyright (C) 2004, 2005 Oracle.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA.
20  */
21 
22 #include <linux/slab.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/configfs.h>
26 
27 #include "tcp.h"
28 #include "nodemanager.h"
29 #include "heartbeat.h"
30 #include "masklog.h"
31 #include "sys.h"
32 
33 /* for now we operate under the assertion that there can be only one
34  * cluster active at a time.  Changing this will require trickling
35  * cluster references throughout where nodes are looked up */
36 struct o2nm_cluster *o2nm_single_cluster = NULL;
37 
38 char *o2nm_fence_method_desc[O2NM_FENCE_METHODS] = {
39 		"reset",	/* O2NM_FENCE_RESET */
40 		"panic",	/* O2NM_FENCE_PANIC */
41 };
42 
43 struct o2nm_node *o2nm_get_node_by_num(u8 node_num)
44 {
45 	struct o2nm_node *node = NULL;
46 
47 	if (node_num >= O2NM_MAX_NODES || o2nm_single_cluster == NULL)
48 		goto out;
49 
50 	read_lock(&o2nm_single_cluster->cl_nodes_lock);
51 	node = o2nm_single_cluster->cl_nodes[node_num];
52 	if (node)
53 		config_item_get(&node->nd_item);
54 	read_unlock(&o2nm_single_cluster->cl_nodes_lock);
55 out:
56 	return node;
57 }
58 EXPORT_SYMBOL_GPL(o2nm_get_node_by_num);
59 
60 int o2nm_configured_node_map(unsigned long *map, unsigned bytes)
61 {
62 	struct o2nm_cluster *cluster = o2nm_single_cluster;
63 
64 	BUG_ON(bytes < (sizeof(cluster->cl_nodes_bitmap)));
65 
66 	if (cluster == NULL)
67 		return -EINVAL;
68 
69 	read_lock(&cluster->cl_nodes_lock);
70 	memcpy(map, cluster->cl_nodes_bitmap, sizeof(cluster->cl_nodes_bitmap));
71 	read_unlock(&cluster->cl_nodes_lock);
72 
73 	return 0;
74 }
75 EXPORT_SYMBOL_GPL(o2nm_configured_node_map);
76 
77 static struct o2nm_node *o2nm_node_ip_tree_lookup(struct o2nm_cluster *cluster,
78 						  __be32 ip_needle,
79 						  struct rb_node ***ret_p,
80 						  struct rb_node **ret_parent)
81 {
82 	struct rb_node **p = &cluster->cl_node_ip_tree.rb_node;
83 	struct rb_node *parent = NULL;
84 	struct o2nm_node *node, *ret = NULL;
85 
86 	while (*p) {
87 		int cmp;
88 
89 		parent = *p;
90 		node = rb_entry(parent, struct o2nm_node, nd_ip_node);
91 
92 		cmp = memcmp(&ip_needle, &node->nd_ipv4_address,
93 				sizeof(ip_needle));
94 		if (cmp < 0)
95 			p = &(*p)->rb_left;
96 		else if (cmp > 0)
97 			p = &(*p)->rb_right;
98 		else {
99 			ret = node;
100 			break;
101 		}
102 	}
103 
104 	if (ret_p != NULL)
105 		*ret_p = p;
106 	if (ret_parent != NULL)
107 		*ret_parent = parent;
108 
109 	return ret;
110 }
111 
112 struct o2nm_node *o2nm_get_node_by_ip(__be32 addr)
113 {
114 	struct o2nm_node *node = NULL;
115 	struct o2nm_cluster *cluster = o2nm_single_cluster;
116 
117 	if (cluster == NULL)
118 		goto out;
119 
120 	read_lock(&cluster->cl_nodes_lock);
121 	node = o2nm_node_ip_tree_lookup(cluster, addr, NULL, NULL);
122 	if (node)
123 		config_item_get(&node->nd_item);
124 	read_unlock(&cluster->cl_nodes_lock);
125 
126 out:
127 	return node;
128 }
129 EXPORT_SYMBOL_GPL(o2nm_get_node_by_ip);
130 
131 void o2nm_node_put(struct o2nm_node *node)
132 {
133 	config_item_put(&node->nd_item);
134 }
135 EXPORT_SYMBOL_GPL(o2nm_node_put);
136 
137 void o2nm_node_get(struct o2nm_node *node)
138 {
139 	config_item_get(&node->nd_item);
140 }
141 EXPORT_SYMBOL_GPL(o2nm_node_get);
142 
143 u8 o2nm_this_node(void)
144 {
145 	u8 node_num = O2NM_MAX_NODES;
146 
147 	if (o2nm_single_cluster && o2nm_single_cluster->cl_has_local)
148 		node_num = o2nm_single_cluster->cl_local_node;
149 
150 	return node_num;
151 }
152 EXPORT_SYMBOL_GPL(o2nm_this_node);
153 
154 /* node configfs bits */
155 
156 static struct o2nm_cluster *to_o2nm_cluster(struct config_item *item)
157 {
158 	return item ?
159 		container_of(to_config_group(item), struct o2nm_cluster,
160 			     cl_group)
161 		: NULL;
162 }
163 
164 static struct o2nm_node *to_o2nm_node(struct config_item *item)
165 {
166 	return item ? container_of(item, struct o2nm_node, nd_item) : NULL;
167 }
168 
169 static void o2nm_node_release(struct config_item *item)
170 {
171 	struct o2nm_node *node = to_o2nm_node(item);
172 	kfree(node);
173 }
174 
175 static ssize_t o2nm_node_num_read(struct o2nm_node *node, char *page)
176 {
177 	return sprintf(page, "%d\n", node->nd_num);
178 }
179 
180 static struct o2nm_cluster *to_o2nm_cluster_from_node(struct o2nm_node *node)
181 {
182 	/* through the first node_set .parent
183 	 * mycluster/nodes/mynode == o2nm_cluster->o2nm_node_group->o2nm_node */
184 	return to_o2nm_cluster(node->nd_item.ci_parent->ci_parent);
185 }
186 
187 enum {
188 	O2NM_NODE_ATTR_NUM = 0,
189 	O2NM_NODE_ATTR_PORT,
190 	O2NM_NODE_ATTR_ADDRESS,
191 	O2NM_NODE_ATTR_LOCAL,
192 };
193 
194 static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,
195 				   size_t count)
196 {
197 	struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
198 	unsigned long tmp;
199 	char *p = (char *)page;
200 
201 	tmp = simple_strtoul(p, &p, 0);
202 	if (!p || (*p && (*p != '\n')))
203 		return -EINVAL;
204 
205 	if (tmp >= O2NM_MAX_NODES)
206 		return -ERANGE;
207 
208 	/* once we're in the cl_nodes tree networking can look us up by
209 	 * node number and try to use our address and port attributes
210 	 * to connect to this node.. make sure that they've been set
211 	 * before writing the node attribute? */
212 	if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
213 	    !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
214 		return -EINVAL; /* XXX */
215 
216 	write_lock(&cluster->cl_nodes_lock);
217 	if (cluster->cl_nodes[tmp])
218 		p = NULL;
219 	else  {
220 		cluster->cl_nodes[tmp] = node;
221 		node->nd_num = tmp;
222 		set_bit(tmp, cluster->cl_nodes_bitmap);
223 	}
224 	write_unlock(&cluster->cl_nodes_lock);
225 	if (p == NULL)
226 		return -EEXIST;
227 
228 	return count;
229 }
230 static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
231 {
232 	return sprintf(page, "%u\n", ntohs(node->nd_ipv4_port));
233 }
234 
235 static ssize_t o2nm_node_ipv4_port_write(struct o2nm_node *node,
236 					 const char *page, size_t count)
237 {
238 	unsigned long tmp;
239 	char *p = (char *)page;
240 
241 	tmp = simple_strtoul(p, &p, 0);
242 	if (!p || (*p && (*p != '\n')))
243 		return -EINVAL;
244 
245 	if (tmp == 0)
246 		return -EINVAL;
247 	if (tmp >= (u16)-1)
248 		return -ERANGE;
249 
250 	node->nd_ipv4_port = htons(tmp);
251 
252 	return count;
253 }
254 
255 static ssize_t o2nm_node_ipv4_address_read(struct o2nm_node *node, char *page)
256 {
257 	return sprintf(page, "%pI4\n", &node->nd_ipv4_address);
258 }
259 
260 static ssize_t o2nm_node_ipv4_address_write(struct o2nm_node *node,
261 					    const char *page,
262 					    size_t count)
263 {
264 	struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
265 	int ret, i;
266 	struct rb_node **p, *parent;
267 	unsigned int octets[4];
268 	__be32 ipv4_addr = 0;
269 
270 	ret = sscanf(page, "%3u.%3u.%3u.%3u", &octets[3], &octets[2],
271 		     &octets[1], &octets[0]);
272 	if (ret != 4)
273 		return -EINVAL;
274 
275 	for (i = 0; i < ARRAY_SIZE(octets); i++) {
276 		if (octets[i] > 255)
277 			return -ERANGE;
278 		be32_add_cpu(&ipv4_addr, octets[i] << (i * 8));
279 	}
280 
281 	ret = 0;
282 	write_lock(&cluster->cl_nodes_lock);
283 	if (o2nm_node_ip_tree_lookup(cluster, ipv4_addr, &p, &parent))
284 		ret = -EEXIST;
285 	else {
286 		rb_link_node(&node->nd_ip_node, parent, p);
287 		rb_insert_color(&node->nd_ip_node, &cluster->cl_node_ip_tree);
288 	}
289 	write_unlock(&cluster->cl_nodes_lock);
290 	if (ret)
291 		return ret;
292 
293 	memcpy(&node->nd_ipv4_address, &ipv4_addr, sizeof(ipv4_addr));
294 
295 	return count;
296 }
297 
298 static ssize_t o2nm_node_local_read(struct o2nm_node *node, char *page)
299 {
300 	return sprintf(page, "%d\n", node->nd_local);
301 }
302 
303 static ssize_t o2nm_node_local_write(struct o2nm_node *node, const char *page,
304 				     size_t count)
305 {
306 	struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
307 	unsigned long tmp;
308 	char *p = (char *)page;
309 	ssize_t ret;
310 
311 	tmp = simple_strtoul(p, &p, 0);
312 	if (!p || (*p && (*p != '\n')))
313 		return -EINVAL;
314 
315 	tmp = !!tmp; /* boolean of whether this node wants to be local */
316 
317 	/* setting local turns on networking rx for now so we require having
318 	 * set everything else first */
319 	if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
320 	    !test_bit(O2NM_NODE_ATTR_NUM, &node->nd_set_attributes) ||
321 	    !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
322 		return -EINVAL; /* XXX */
323 
324 	/* the only failure case is trying to set a new local node
325 	 * when a different one is already set */
326 	if (tmp && tmp == cluster->cl_has_local &&
327 	    cluster->cl_local_node != node->nd_num)
328 		return -EBUSY;
329 
330 	/* bring up the rx thread if we're setting the new local node. */
331 	if (tmp && !cluster->cl_has_local) {
332 		ret = o2net_start_listening(node);
333 		if (ret)
334 			return ret;
335 	}
336 
337 	if (!tmp && cluster->cl_has_local &&
338 	    cluster->cl_local_node == node->nd_num) {
339 		o2net_stop_listening(node);
340 		cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
341 	}
342 
343 	node->nd_local = tmp;
344 	if (node->nd_local) {
345 		cluster->cl_has_local = tmp;
346 		cluster->cl_local_node = node->nd_num;
347 	}
348 
349 	return count;
350 }
351 
352 struct o2nm_node_attribute {
353 	struct configfs_attribute attr;
354 	ssize_t (*show)(struct o2nm_node *, char *);
355 	ssize_t (*store)(struct o2nm_node *, const char *, size_t);
356 };
357 
358 static struct o2nm_node_attribute o2nm_node_attr_num = {
359 	.attr	= { .ca_owner = THIS_MODULE,
360 		    .ca_name = "num",
361 		    .ca_mode = S_IRUGO | S_IWUSR },
362 	.show	= o2nm_node_num_read,
363 	.store	= o2nm_node_num_write,
364 };
365 
366 static struct o2nm_node_attribute o2nm_node_attr_ipv4_port = {
367 	.attr	= { .ca_owner = THIS_MODULE,
368 		    .ca_name = "ipv4_port",
369 		    .ca_mode = S_IRUGO | S_IWUSR },
370 	.show	= o2nm_node_ipv4_port_read,
371 	.store	= o2nm_node_ipv4_port_write,
372 };
373 
374 static struct o2nm_node_attribute o2nm_node_attr_ipv4_address = {
375 	.attr	= { .ca_owner = THIS_MODULE,
376 		    .ca_name = "ipv4_address",
377 		    .ca_mode = S_IRUGO | S_IWUSR },
378 	.show	= o2nm_node_ipv4_address_read,
379 	.store	= o2nm_node_ipv4_address_write,
380 };
381 
382 static struct o2nm_node_attribute o2nm_node_attr_local = {
383 	.attr	= { .ca_owner = THIS_MODULE,
384 		    .ca_name = "local",
385 		    .ca_mode = S_IRUGO | S_IWUSR },
386 	.show	= o2nm_node_local_read,
387 	.store	= o2nm_node_local_write,
388 };
389 
390 static struct configfs_attribute *o2nm_node_attrs[] = {
391 	[O2NM_NODE_ATTR_NUM] = &o2nm_node_attr_num.attr,
392 	[O2NM_NODE_ATTR_PORT] = &o2nm_node_attr_ipv4_port.attr,
393 	[O2NM_NODE_ATTR_ADDRESS] = &o2nm_node_attr_ipv4_address.attr,
394 	[O2NM_NODE_ATTR_LOCAL] = &o2nm_node_attr_local.attr,
395 	NULL,
396 };
397 
398 static int o2nm_attr_index(struct configfs_attribute *attr)
399 {
400 	int i;
401 	for (i = 0; i < ARRAY_SIZE(o2nm_node_attrs); i++) {
402 		if (attr == o2nm_node_attrs[i])
403 			return i;
404 	}
405 	BUG();
406 	return 0;
407 }
408 
409 static ssize_t o2nm_node_show(struct config_item *item,
410 			      struct configfs_attribute *attr,
411 			      char *page)
412 {
413 	struct o2nm_node *node = to_o2nm_node(item);
414 	struct o2nm_node_attribute *o2nm_node_attr =
415 		container_of(attr, struct o2nm_node_attribute, attr);
416 	ssize_t ret = 0;
417 
418 	if (o2nm_node_attr->show)
419 		ret = o2nm_node_attr->show(node, page);
420 	return ret;
421 }
422 
423 static ssize_t o2nm_node_store(struct config_item *item,
424 			       struct configfs_attribute *attr,
425 			       const char *page, size_t count)
426 {
427 	struct o2nm_node *node = to_o2nm_node(item);
428 	struct o2nm_node_attribute *o2nm_node_attr =
429 		container_of(attr, struct o2nm_node_attribute, attr);
430 	ssize_t ret;
431 	int attr_index = o2nm_attr_index(attr);
432 
433 	if (o2nm_node_attr->store == NULL) {
434 		ret = -EINVAL;
435 		goto out;
436 	}
437 
438 	if (test_bit(attr_index, &node->nd_set_attributes))
439 		return -EBUSY;
440 
441 	ret = o2nm_node_attr->store(node, page, count);
442 	if (ret < count)
443 		goto out;
444 
445 	set_bit(attr_index, &node->nd_set_attributes);
446 out:
447 	return ret;
448 }
449 
450 static struct configfs_item_operations o2nm_node_item_ops = {
451 	.release		= o2nm_node_release,
452 	.show_attribute		= o2nm_node_show,
453 	.store_attribute	= o2nm_node_store,
454 };
455 
456 static struct config_item_type o2nm_node_type = {
457 	.ct_item_ops	= &o2nm_node_item_ops,
458 	.ct_attrs	= o2nm_node_attrs,
459 	.ct_owner	= THIS_MODULE,
460 };
461 
462 /* node set */
463 
464 struct o2nm_node_group {
465 	struct config_group ns_group;
466 	/* some stuff? */
467 };
468 
469 #if 0
470 static struct o2nm_node_group *to_o2nm_node_group(struct config_group *group)
471 {
472 	return group ?
473 		container_of(group, struct o2nm_node_group, ns_group)
474 		: NULL;
475 }
476 #endif
477 
478 struct o2nm_cluster_attribute {
479 	struct configfs_attribute attr;
480 	ssize_t (*show)(struct o2nm_cluster *, char *);
481 	ssize_t (*store)(struct o2nm_cluster *, const char *, size_t);
482 };
483 
484 static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
485                                        unsigned int *val)
486 {
487 	unsigned long tmp;
488 	char *p = (char *)page;
489 
490 	tmp = simple_strtoul(p, &p, 0);
491 	if (!p || (*p && (*p != '\n')))
492 		return -EINVAL;
493 
494 	if (tmp == 0)
495 		return -EINVAL;
496 	if (tmp >= (u32)-1)
497 		return -ERANGE;
498 
499 	*val = tmp;
500 
501 	return count;
502 }
503 
504 static ssize_t o2nm_cluster_attr_idle_timeout_ms_read(
505 	struct o2nm_cluster *cluster, char *page)
506 {
507 	return sprintf(page, "%u\n", cluster->cl_idle_timeout_ms);
508 }
509 
510 static ssize_t o2nm_cluster_attr_idle_timeout_ms_write(
511 	struct o2nm_cluster *cluster, const char *page, size_t count)
512 {
513 	ssize_t ret;
514 	unsigned int val;
515 
516 	ret =  o2nm_cluster_attr_write(page, count, &val);
517 
518 	if (ret > 0) {
519 		if (cluster->cl_idle_timeout_ms != val
520 			&& o2net_num_connected_peers()) {
521 			mlog(ML_NOTICE,
522 			     "o2net: cannot change idle timeout after "
523 			     "the first peer has agreed to it."
524 			     "  %d connected peers\n",
525 			     o2net_num_connected_peers());
526 			ret = -EINVAL;
527 		} else if (val <= cluster->cl_keepalive_delay_ms) {
528 			mlog(ML_NOTICE, "o2net: idle timeout must be larger "
529 			     "than keepalive delay\n");
530 			ret = -EINVAL;
531 		} else {
532 			cluster->cl_idle_timeout_ms = val;
533 		}
534 	}
535 
536 	return ret;
537 }
538 
539 static ssize_t o2nm_cluster_attr_keepalive_delay_ms_read(
540 	struct o2nm_cluster *cluster, char *page)
541 {
542 	return sprintf(page, "%u\n", cluster->cl_keepalive_delay_ms);
543 }
544 
545 static ssize_t o2nm_cluster_attr_keepalive_delay_ms_write(
546 	struct o2nm_cluster *cluster, const char *page, size_t count)
547 {
548 	ssize_t ret;
549 	unsigned int val;
550 
551 	ret =  o2nm_cluster_attr_write(page, count, &val);
552 
553 	if (ret > 0) {
554 		if (cluster->cl_keepalive_delay_ms != val
555 		    && o2net_num_connected_peers()) {
556 			mlog(ML_NOTICE,
557 			     "o2net: cannot change keepalive delay after"
558 			     " the first peer has agreed to it."
559 			     "  %d connected peers\n",
560 			     o2net_num_connected_peers());
561 			ret = -EINVAL;
562 		} else if (val >= cluster->cl_idle_timeout_ms) {
563 			mlog(ML_NOTICE, "o2net: keepalive delay must be "
564 			     "smaller than idle timeout\n");
565 			ret = -EINVAL;
566 		} else {
567 			cluster->cl_keepalive_delay_ms = val;
568 		}
569 	}
570 
571 	return ret;
572 }
573 
574 static ssize_t o2nm_cluster_attr_reconnect_delay_ms_read(
575 	struct o2nm_cluster *cluster, char *page)
576 {
577 	return sprintf(page, "%u\n", cluster->cl_reconnect_delay_ms);
578 }
579 
580 static ssize_t o2nm_cluster_attr_reconnect_delay_ms_write(
581 	struct o2nm_cluster *cluster, const char *page, size_t count)
582 {
583 	return o2nm_cluster_attr_write(page, count,
584 	                               &cluster->cl_reconnect_delay_ms);
585 }
586 
587 static ssize_t o2nm_cluster_attr_fence_method_read(
588 	struct o2nm_cluster *cluster, char *page)
589 {
590 	ssize_t ret = 0;
591 
592 	if (cluster)
593 		ret = sprintf(page, "%s\n",
594 			      o2nm_fence_method_desc[cluster->cl_fence_method]);
595 	return ret;
596 }
597 
598 static ssize_t o2nm_cluster_attr_fence_method_write(
599 	struct o2nm_cluster *cluster, const char *page, size_t count)
600 {
601 	unsigned int i;
602 
603 	if (page[count - 1] != '\n')
604 		goto bail;
605 
606 	for (i = 0; i < O2NM_FENCE_METHODS; ++i) {
607 		if (count != strlen(o2nm_fence_method_desc[i]) + 1)
608 			continue;
609 		if (strncasecmp(page, o2nm_fence_method_desc[i], count - 1))
610 			continue;
611 		if (cluster->cl_fence_method != i) {
612 			printk(KERN_INFO "ocfs2: Changing fence method to %s\n",
613 			       o2nm_fence_method_desc[i]);
614 			cluster->cl_fence_method = i;
615 		}
616 		return count;
617 	}
618 
619 bail:
620 	return -EINVAL;
621 }
622 
623 static struct o2nm_cluster_attribute o2nm_cluster_attr_idle_timeout_ms = {
624 	.attr	= { .ca_owner = THIS_MODULE,
625 		    .ca_name = "idle_timeout_ms",
626 		    .ca_mode = S_IRUGO | S_IWUSR },
627 	.show	= o2nm_cluster_attr_idle_timeout_ms_read,
628 	.store	= o2nm_cluster_attr_idle_timeout_ms_write,
629 };
630 
631 static struct o2nm_cluster_attribute o2nm_cluster_attr_keepalive_delay_ms = {
632 	.attr	= { .ca_owner = THIS_MODULE,
633 		    .ca_name = "keepalive_delay_ms",
634 		    .ca_mode = S_IRUGO | S_IWUSR },
635 	.show	= o2nm_cluster_attr_keepalive_delay_ms_read,
636 	.store	= o2nm_cluster_attr_keepalive_delay_ms_write,
637 };
638 
639 static struct o2nm_cluster_attribute o2nm_cluster_attr_reconnect_delay_ms = {
640 	.attr	= { .ca_owner = THIS_MODULE,
641 		    .ca_name = "reconnect_delay_ms",
642 		    .ca_mode = S_IRUGO | S_IWUSR },
643 	.show	= o2nm_cluster_attr_reconnect_delay_ms_read,
644 	.store	= o2nm_cluster_attr_reconnect_delay_ms_write,
645 };
646 
647 static struct o2nm_cluster_attribute o2nm_cluster_attr_fence_method = {
648 	.attr	= { .ca_owner = THIS_MODULE,
649 		    .ca_name = "fence_method",
650 		    .ca_mode = S_IRUGO | S_IWUSR },
651 	.show	= o2nm_cluster_attr_fence_method_read,
652 	.store	= o2nm_cluster_attr_fence_method_write,
653 };
654 
655 static struct configfs_attribute *o2nm_cluster_attrs[] = {
656 	&o2nm_cluster_attr_idle_timeout_ms.attr,
657 	&o2nm_cluster_attr_keepalive_delay_ms.attr,
658 	&o2nm_cluster_attr_reconnect_delay_ms.attr,
659 	&o2nm_cluster_attr_fence_method.attr,
660 	NULL,
661 };
662 static ssize_t o2nm_cluster_show(struct config_item *item,
663                                  struct configfs_attribute *attr,
664                                  char *page)
665 {
666 	struct o2nm_cluster *cluster = to_o2nm_cluster(item);
667 	struct o2nm_cluster_attribute *o2nm_cluster_attr =
668 		container_of(attr, struct o2nm_cluster_attribute, attr);
669 	ssize_t ret = 0;
670 
671 	if (o2nm_cluster_attr->show)
672 		ret = o2nm_cluster_attr->show(cluster, page);
673 	return ret;
674 }
675 
676 static ssize_t o2nm_cluster_store(struct config_item *item,
677                                   struct configfs_attribute *attr,
678                                   const char *page, size_t count)
679 {
680 	struct o2nm_cluster *cluster = to_o2nm_cluster(item);
681 	struct o2nm_cluster_attribute *o2nm_cluster_attr =
682 		container_of(attr, struct o2nm_cluster_attribute, attr);
683 	ssize_t ret;
684 
685 	if (o2nm_cluster_attr->store == NULL) {
686 		ret = -EINVAL;
687 		goto out;
688 	}
689 
690 	ret = o2nm_cluster_attr->store(cluster, page, count);
691 	if (ret < count)
692 		goto out;
693 out:
694 	return ret;
695 }
696 
697 static struct config_item *o2nm_node_group_make_item(struct config_group *group,
698 						     const char *name)
699 {
700 	struct o2nm_node *node = NULL;
701 
702 	if (strlen(name) > O2NM_MAX_NAME_LEN)
703 		return ERR_PTR(-ENAMETOOLONG);
704 
705 	node = kzalloc(sizeof(struct o2nm_node), GFP_KERNEL);
706 	if (node == NULL)
707 		return ERR_PTR(-ENOMEM);
708 
709 	strcpy(node->nd_name, name); /* use item.ci_namebuf instead? */
710 	config_item_init_type_name(&node->nd_item, name, &o2nm_node_type);
711 	spin_lock_init(&node->nd_lock);
712 
713 	mlog(ML_CLUSTER, "o2nm: Registering node %s\n", name);
714 
715 	return &node->nd_item;
716 }
717 
718 static void o2nm_node_group_drop_item(struct config_group *group,
719 				      struct config_item *item)
720 {
721 	struct o2nm_node *node = to_o2nm_node(item);
722 	struct o2nm_cluster *cluster = to_o2nm_cluster(group->cg_item.ci_parent);
723 
724 	o2net_disconnect_node(node);
725 
726 	if (cluster->cl_has_local &&
727 	    (cluster->cl_local_node == node->nd_num)) {
728 		cluster->cl_has_local = 0;
729 		cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
730 		o2net_stop_listening(node);
731 	}
732 
733 	/* XXX call into net to stop this node from trading messages */
734 
735 	write_lock(&cluster->cl_nodes_lock);
736 
737 	/* XXX sloppy */
738 	if (node->nd_ipv4_address)
739 		rb_erase(&node->nd_ip_node, &cluster->cl_node_ip_tree);
740 
741 	/* nd_num might be 0 if the node number hasn't been set.. */
742 	if (cluster->cl_nodes[node->nd_num] == node) {
743 		cluster->cl_nodes[node->nd_num] = NULL;
744 		clear_bit(node->nd_num, cluster->cl_nodes_bitmap);
745 	}
746 	write_unlock(&cluster->cl_nodes_lock);
747 
748 	mlog(ML_CLUSTER, "o2nm: Unregistered node %s\n",
749 	     config_item_name(&node->nd_item));
750 
751 	config_item_put(item);
752 }
753 
754 static struct configfs_group_operations o2nm_node_group_group_ops = {
755 	.make_item	= o2nm_node_group_make_item,
756 	.drop_item	= o2nm_node_group_drop_item,
757 };
758 
759 static struct config_item_type o2nm_node_group_type = {
760 	.ct_group_ops	= &o2nm_node_group_group_ops,
761 	.ct_owner	= THIS_MODULE,
762 };
763 
764 /* cluster */
765 
766 static void o2nm_cluster_release(struct config_item *item)
767 {
768 	struct o2nm_cluster *cluster = to_o2nm_cluster(item);
769 
770 	kfree(cluster->cl_group.default_groups);
771 	kfree(cluster);
772 }
773 
774 static struct configfs_item_operations o2nm_cluster_item_ops = {
775 	.release	= o2nm_cluster_release,
776 	.show_attribute		= o2nm_cluster_show,
777 	.store_attribute	= o2nm_cluster_store,
778 };
779 
780 static struct config_item_type o2nm_cluster_type = {
781 	.ct_item_ops	= &o2nm_cluster_item_ops,
782 	.ct_attrs	= o2nm_cluster_attrs,
783 	.ct_owner	= THIS_MODULE,
784 };
785 
786 /* cluster set */
787 
788 struct o2nm_cluster_group {
789 	struct configfs_subsystem cs_subsys;
790 	/* some stuff? */
791 };
792 
793 #if 0
794 static struct o2nm_cluster_group *to_o2nm_cluster_group(struct config_group *group)
795 {
796 	return group ?
797 		container_of(to_configfs_subsystem(group), struct o2nm_cluster_group, cs_subsys)
798 	       : NULL;
799 }
800 #endif
801 
802 static struct config_group *o2nm_cluster_group_make_group(struct config_group *group,
803 							  const char *name)
804 {
805 	struct o2nm_cluster *cluster = NULL;
806 	struct o2nm_node_group *ns = NULL;
807 	struct config_group *o2hb_group = NULL, *ret = NULL;
808 	void *defs = NULL;
809 
810 	/* this runs under the parent dir's i_mutex; there can be only
811 	 * one caller in here at a time */
812 	if (o2nm_single_cluster)
813 		return ERR_PTR(-ENOSPC);
814 
815 	cluster = kzalloc(sizeof(struct o2nm_cluster), GFP_KERNEL);
816 	ns = kzalloc(sizeof(struct o2nm_node_group), GFP_KERNEL);
817 	defs = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
818 	o2hb_group = o2hb_alloc_hb_set();
819 	if (cluster == NULL || ns == NULL || o2hb_group == NULL || defs == NULL)
820 		goto out;
821 
822 	config_group_init_type_name(&cluster->cl_group, name,
823 				    &o2nm_cluster_type);
824 	config_group_init_type_name(&ns->ns_group, "node",
825 				    &o2nm_node_group_type);
826 
827 	cluster->cl_group.default_groups = defs;
828 	cluster->cl_group.default_groups[0] = &ns->ns_group;
829 	cluster->cl_group.default_groups[1] = o2hb_group;
830 	cluster->cl_group.default_groups[2] = NULL;
831 	rwlock_init(&cluster->cl_nodes_lock);
832 	cluster->cl_node_ip_tree = RB_ROOT;
833 	cluster->cl_reconnect_delay_ms = O2NET_RECONNECT_DELAY_MS_DEFAULT;
834 	cluster->cl_idle_timeout_ms    = O2NET_IDLE_TIMEOUT_MS_DEFAULT;
835 	cluster->cl_keepalive_delay_ms = O2NET_KEEPALIVE_DELAY_MS_DEFAULT;
836 	cluster->cl_fence_method       = O2NM_FENCE_RESET;
837 
838 	ret = &cluster->cl_group;
839 	o2nm_single_cluster = cluster;
840 
841 out:
842 	if (ret == NULL) {
843 		kfree(cluster);
844 		kfree(ns);
845 		o2hb_free_hb_set(o2hb_group);
846 		kfree(defs);
847 		ret = ERR_PTR(-ENOMEM);
848 	}
849 
850 	return ret;
851 }
852 
853 static void o2nm_cluster_group_drop_item(struct config_group *group, struct config_item *item)
854 {
855 	struct o2nm_cluster *cluster = to_o2nm_cluster(item);
856 	int i;
857 	struct config_item *killme;
858 
859 	BUG_ON(o2nm_single_cluster != cluster);
860 	o2nm_single_cluster = NULL;
861 
862 	for (i = 0; cluster->cl_group.default_groups[i]; i++) {
863 		killme = &cluster->cl_group.default_groups[i]->cg_item;
864 		cluster->cl_group.default_groups[i] = NULL;
865 		config_item_put(killme);
866 	}
867 
868 	config_item_put(item);
869 }
870 
871 static struct configfs_group_operations o2nm_cluster_group_group_ops = {
872 	.make_group	= o2nm_cluster_group_make_group,
873 	.drop_item	= o2nm_cluster_group_drop_item,
874 };
875 
876 static struct config_item_type o2nm_cluster_group_type = {
877 	.ct_group_ops	= &o2nm_cluster_group_group_ops,
878 	.ct_owner	= THIS_MODULE,
879 };
880 
881 static struct o2nm_cluster_group o2nm_cluster_group = {
882 	.cs_subsys = {
883 		.su_group = {
884 			.cg_item = {
885 				.ci_namebuf = "cluster",
886 				.ci_type = &o2nm_cluster_group_type,
887 			},
888 		},
889 	},
890 };
891 
892 int o2nm_depend_item(struct config_item *item)
893 {
894 	return configfs_depend_item(&o2nm_cluster_group.cs_subsys, item);
895 }
896 
897 void o2nm_undepend_item(struct config_item *item)
898 {
899 	configfs_undepend_item(&o2nm_cluster_group.cs_subsys, item);
900 }
901 
902 int o2nm_depend_this_node(void)
903 {
904 	int ret = 0;
905 	struct o2nm_node *local_node;
906 
907 	local_node = o2nm_get_node_by_num(o2nm_this_node());
908 	if (!local_node) {
909 		ret = -EINVAL;
910 		goto out;
911 	}
912 
913 	ret = o2nm_depend_item(&local_node->nd_item);
914 	o2nm_node_put(local_node);
915 
916 out:
917 	return ret;
918 }
919 
920 void o2nm_undepend_this_node(void)
921 {
922 	struct o2nm_node *local_node;
923 
924 	local_node = o2nm_get_node_by_num(o2nm_this_node());
925 	BUG_ON(!local_node);
926 
927 	o2nm_undepend_item(&local_node->nd_item);
928 	o2nm_node_put(local_node);
929 }
930 
931 
932 static void __exit exit_o2nm(void)
933 {
934 	/* XXX sync with hb callbacks and shut down hb? */
935 	o2net_unregister_hb_callbacks();
936 	configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
937 	o2cb_sys_shutdown();
938 
939 	o2net_exit();
940 	o2hb_exit();
941 }
942 
943 static int __init init_o2nm(void)
944 {
945 	int ret = -1;
946 
947 	ret = o2hb_init();
948 	if (ret)
949 		goto out;
950 
951 	ret = o2net_init();
952 	if (ret)
953 		goto out_o2hb;
954 
955 	ret = o2net_register_hb_callbacks();
956 	if (ret)
957 		goto out_o2net;
958 
959 	config_group_init(&o2nm_cluster_group.cs_subsys.su_group);
960 	mutex_init(&o2nm_cluster_group.cs_subsys.su_mutex);
961 	ret = configfs_register_subsystem(&o2nm_cluster_group.cs_subsys);
962 	if (ret) {
963 		printk(KERN_ERR "nodemanager: Registration returned %d\n", ret);
964 		goto out_callbacks;
965 	}
966 
967 	ret = o2cb_sys_init();
968 	if (!ret)
969 		goto out;
970 
971 	configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
972 out_callbacks:
973 	o2net_unregister_hb_callbacks();
974 out_o2net:
975 	o2net_exit();
976 out_o2hb:
977 	o2hb_exit();
978 out:
979 	return ret;
980 }
981 
982 MODULE_AUTHOR("Oracle");
983 MODULE_LICENSE("GPL");
984 MODULE_DESCRIPTION("OCFS2 cluster management");
985 
986 module_init(init_o2nm)
987 module_exit(exit_o2nm)
988