Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / fs / ocfs2 / cluster / nodemanager.c
blob709fba25bf7e1b4ed4b1f89b12f6fb632450a5b8
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * Copyright (C) 2004, 2005 Oracle. All rights reserved.
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.
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.
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
25 #include <linux/configfs.h>
27 #include "tcp.h"
28 #include "nodemanager.h"
29 #include "heartbeat.h"
30 #include "masklog.h"
31 #include "sys.h"
32 #include "ver.h"
34 /* for now we operate under the assertion that there can be only one
35 * cluster active at a time. Changing this will require trickling
36 * cluster references throughout where nodes are looked up */
37 struct o2nm_cluster *o2nm_single_cluster = NULL;
39 #define OCFS2_MAX_HB_CTL_PATH 256
40 static char ocfs2_hb_ctl_path[OCFS2_MAX_HB_CTL_PATH] = "/sbin/ocfs2_hb_ctl";
42 static ctl_table ocfs2_nm_table[] = {
44 .ctl_name = 1,
45 .procname = "hb_ctl_path",
46 .data = ocfs2_hb_ctl_path,
47 .maxlen = OCFS2_MAX_HB_CTL_PATH,
48 .mode = 0644,
49 .proc_handler = &proc_dostring,
50 .strategy = &sysctl_string,
52 { .ctl_name = 0 }
55 static ctl_table ocfs2_mod_table[] = {
57 .ctl_name = FS_OCFS2_NM,
58 .procname = "nm",
59 .data = NULL,
60 .maxlen = 0,
61 .mode = 0555,
62 .child = ocfs2_nm_table
64 { .ctl_name = 0}
67 static ctl_table ocfs2_kern_table[] = {
69 .ctl_name = FS_OCFS2,
70 .procname = "ocfs2",
71 .data = NULL,
72 .maxlen = 0,
73 .mode = 0555,
74 .child = ocfs2_mod_table
76 { .ctl_name = 0}
79 static ctl_table ocfs2_root_table[] = {
81 .ctl_name = CTL_FS,
82 .procname = "fs",
83 .data = NULL,
84 .maxlen = 0,
85 .mode = 0555,
86 .child = ocfs2_kern_table
88 { .ctl_name = 0 }
91 static struct ctl_table_header *ocfs2_table_header = NULL;
93 const char *o2nm_get_hb_ctl_path(void)
95 return ocfs2_hb_ctl_path;
97 EXPORT_SYMBOL_GPL(o2nm_get_hb_ctl_path);
99 struct o2nm_node *o2nm_get_node_by_num(u8 node_num)
101 struct o2nm_node *node = NULL;
103 if (node_num >= O2NM_MAX_NODES || o2nm_single_cluster == NULL)
104 goto out;
106 read_lock(&o2nm_single_cluster->cl_nodes_lock);
107 node = o2nm_single_cluster->cl_nodes[node_num];
108 if (node)
109 config_item_get(&node->nd_item);
110 read_unlock(&o2nm_single_cluster->cl_nodes_lock);
111 out:
112 return node;
114 EXPORT_SYMBOL_GPL(o2nm_get_node_by_num);
116 int o2nm_configured_node_map(unsigned long *map, unsigned bytes)
118 struct o2nm_cluster *cluster = o2nm_single_cluster;
120 BUG_ON(bytes < (sizeof(cluster->cl_nodes_bitmap)));
122 if (cluster == NULL)
123 return -EINVAL;
125 read_lock(&cluster->cl_nodes_lock);
126 memcpy(map, cluster->cl_nodes_bitmap, sizeof(cluster->cl_nodes_bitmap));
127 read_unlock(&cluster->cl_nodes_lock);
129 return 0;
131 EXPORT_SYMBOL_GPL(o2nm_configured_node_map);
133 static struct o2nm_node *o2nm_node_ip_tree_lookup(struct o2nm_cluster *cluster,
134 __be32 ip_needle,
135 struct rb_node ***ret_p,
136 struct rb_node **ret_parent)
138 struct rb_node **p = &cluster->cl_node_ip_tree.rb_node;
139 struct rb_node *parent = NULL;
140 struct o2nm_node *node, *ret = NULL;
142 while (*p) {
143 int cmp;
145 parent = *p;
146 node = rb_entry(parent, struct o2nm_node, nd_ip_node);
148 cmp = memcmp(&ip_needle, &node->nd_ipv4_address,
149 sizeof(ip_needle));
150 if (cmp < 0)
151 p = &(*p)->rb_left;
152 else if (cmp > 0)
153 p = &(*p)->rb_right;
154 else {
155 ret = node;
156 break;
160 if (ret_p != NULL)
161 *ret_p = p;
162 if (ret_parent != NULL)
163 *ret_parent = parent;
165 return ret;
168 struct o2nm_node *o2nm_get_node_by_ip(__be32 addr)
170 struct o2nm_node *node = NULL;
171 struct o2nm_cluster *cluster = o2nm_single_cluster;
173 if (cluster == NULL)
174 goto out;
176 read_lock(&cluster->cl_nodes_lock);
177 node = o2nm_node_ip_tree_lookup(cluster, addr, NULL, NULL);
178 if (node)
179 config_item_get(&node->nd_item);
180 read_unlock(&cluster->cl_nodes_lock);
182 out:
183 return node;
185 EXPORT_SYMBOL_GPL(o2nm_get_node_by_ip);
187 void o2nm_node_put(struct o2nm_node *node)
189 config_item_put(&node->nd_item);
191 EXPORT_SYMBOL_GPL(o2nm_node_put);
193 void o2nm_node_get(struct o2nm_node *node)
195 config_item_get(&node->nd_item);
197 EXPORT_SYMBOL_GPL(o2nm_node_get);
199 u8 o2nm_this_node(void)
201 u8 node_num = O2NM_MAX_NODES;
203 if (o2nm_single_cluster && o2nm_single_cluster->cl_has_local)
204 node_num = o2nm_single_cluster->cl_local_node;
206 return node_num;
208 EXPORT_SYMBOL_GPL(o2nm_this_node);
210 /* node configfs bits */
212 static struct o2nm_cluster *to_o2nm_cluster(struct config_item *item)
214 return item ?
215 container_of(to_config_group(item), struct o2nm_cluster,
216 cl_group)
217 : NULL;
220 static struct o2nm_node *to_o2nm_node(struct config_item *item)
222 return item ? container_of(item, struct o2nm_node, nd_item) : NULL;
225 static void o2nm_node_release(struct config_item *item)
227 struct o2nm_node *node = to_o2nm_node(item);
228 kfree(node);
231 static ssize_t o2nm_node_num_read(struct o2nm_node *node, char *page)
233 return sprintf(page, "%d\n", node->nd_num);
236 static struct o2nm_cluster *to_o2nm_cluster_from_node(struct o2nm_node *node)
238 /* through the first node_set .parent
239 * mycluster/nodes/mynode == o2nm_cluster->o2nm_node_group->o2nm_node */
240 return to_o2nm_cluster(node->nd_item.ci_parent->ci_parent);
243 enum {
244 O2NM_NODE_ATTR_NUM = 0,
245 O2NM_NODE_ATTR_PORT,
246 O2NM_NODE_ATTR_ADDRESS,
247 O2NM_NODE_ATTR_LOCAL,
250 static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,
251 size_t count)
253 struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
254 unsigned long tmp;
255 char *p = (char *)page;
257 tmp = simple_strtoul(p, &p, 0);
258 if (!p || (*p && (*p != '\n')))
259 return -EINVAL;
261 if (tmp >= O2NM_MAX_NODES)
262 return -ERANGE;
264 /* once we're in the cl_nodes tree networking can look us up by
265 * node number and try to use our address and port attributes
266 * to connect to this node.. make sure that they've been set
267 * before writing the node attribute? */
268 if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
269 !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
270 return -EINVAL; /* XXX */
272 write_lock(&cluster->cl_nodes_lock);
273 if (cluster->cl_nodes[tmp])
274 p = NULL;
275 else {
276 cluster->cl_nodes[tmp] = node;
277 node->nd_num = tmp;
278 set_bit(tmp, cluster->cl_nodes_bitmap);
280 write_unlock(&cluster->cl_nodes_lock);
281 if (p == NULL)
282 return -EEXIST;
284 return count;
286 static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
288 return sprintf(page, "%u\n", ntohs(node->nd_ipv4_port));
291 static ssize_t o2nm_node_ipv4_port_write(struct o2nm_node *node,
292 const char *page, size_t count)
294 unsigned long tmp;
295 char *p = (char *)page;
297 tmp = simple_strtoul(p, &p, 0);
298 if (!p || (*p && (*p != '\n')))
299 return -EINVAL;
301 if (tmp == 0)
302 return -EINVAL;
303 if (tmp >= (u16)-1)
304 return -ERANGE;
306 node->nd_ipv4_port = htons(tmp);
308 return count;
311 static ssize_t o2nm_node_ipv4_address_read(struct o2nm_node *node, char *page)
313 return sprintf(page, "%u.%u.%u.%u\n", NIPQUAD(node->nd_ipv4_address));
316 static ssize_t o2nm_node_ipv4_address_write(struct o2nm_node *node,
317 const char *page,
318 size_t count)
320 struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
321 int ret, i;
322 struct rb_node **p, *parent;
323 unsigned int octets[4];
324 __be32 ipv4_addr = 0;
326 ret = sscanf(page, "%3u.%3u.%3u.%3u", &octets[3], &octets[2],
327 &octets[1], &octets[0]);
328 if (ret != 4)
329 return -EINVAL;
331 for (i = 0; i < ARRAY_SIZE(octets); i++) {
332 if (octets[i] > 255)
333 return -ERANGE;
334 be32_add_cpu(&ipv4_addr, octets[i] << (i * 8));
337 ret = 0;
338 write_lock(&cluster->cl_nodes_lock);
339 if (o2nm_node_ip_tree_lookup(cluster, ipv4_addr, &p, &parent))
340 ret = -EEXIST;
341 else {
342 rb_link_node(&node->nd_ip_node, parent, p);
343 rb_insert_color(&node->nd_ip_node, &cluster->cl_node_ip_tree);
345 write_unlock(&cluster->cl_nodes_lock);
346 if (ret)
347 return ret;
349 memcpy(&node->nd_ipv4_address, &ipv4_addr, sizeof(ipv4_addr));
351 return count;
354 static ssize_t o2nm_node_local_read(struct o2nm_node *node, char *page)
356 return sprintf(page, "%d\n", node->nd_local);
359 static ssize_t o2nm_node_local_write(struct o2nm_node *node, const char *page,
360 size_t count)
362 struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
363 unsigned long tmp;
364 char *p = (char *)page;
365 ssize_t ret;
367 tmp = simple_strtoul(p, &p, 0);
368 if (!p || (*p && (*p != '\n')))
369 return -EINVAL;
371 tmp = !!tmp; /* boolean of whether this node wants to be local */
373 /* setting local turns on networking rx for now so we require having
374 * set everything else first */
375 if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
376 !test_bit(O2NM_NODE_ATTR_NUM, &node->nd_set_attributes) ||
377 !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
378 return -EINVAL; /* XXX */
380 /* the only failure case is trying to set a new local node
381 * when a different one is already set */
382 if (tmp && tmp == cluster->cl_has_local &&
383 cluster->cl_local_node != node->nd_num)
384 return -EBUSY;
386 /* bring up the rx thread if we're setting the new local node. */
387 if (tmp && !cluster->cl_has_local) {
388 ret = o2net_start_listening(node);
389 if (ret)
390 return ret;
393 if (!tmp && cluster->cl_has_local &&
394 cluster->cl_local_node == node->nd_num) {
395 o2net_stop_listening(node);
396 cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
399 node->nd_local = tmp;
400 if (node->nd_local) {
401 cluster->cl_has_local = tmp;
402 cluster->cl_local_node = node->nd_num;
405 return count;
408 struct o2nm_node_attribute {
409 struct configfs_attribute attr;
410 ssize_t (*show)(struct o2nm_node *, char *);
411 ssize_t (*store)(struct o2nm_node *, const char *, size_t);
414 static struct o2nm_node_attribute o2nm_node_attr_num = {
415 .attr = { .ca_owner = THIS_MODULE,
416 .ca_name = "num",
417 .ca_mode = S_IRUGO | S_IWUSR },
418 .show = o2nm_node_num_read,
419 .store = o2nm_node_num_write,
422 static struct o2nm_node_attribute o2nm_node_attr_ipv4_port = {
423 .attr = { .ca_owner = THIS_MODULE,
424 .ca_name = "ipv4_port",
425 .ca_mode = S_IRUGO | S_IWUSR },
426 .show = o2nm_node_ipv4_port_read,
427 .store = o2nm_node_ipv4_port_write,
430 static struct o2nm_node_attribute o2nm_node_attr_ipv4_address = {
431 .attr = { .ca_owner = THIS_MODULE,
432 .ca_name = "ipv4_address",
433 .ca_mode = S_IRUGO | S_IWUSR },
434 .show = o2nm_node_ipv4_address_read,
435 .store = o2nm_node_ipv4_address_write,
438 static struct o2nm_node_attribute o2nm_node_attr_local = {
439 .attr = { .ca_owner = THIS_MODULE,
440 .ca_name = "local",
441 .ca_mode = S_IRUGO | S_IWUSR },
442 .show = o2nm_node_local_read,
443 .store = o2nm_node_local_write,
446 static struct configfs_attribute *o2nm_node_attrs[] = {
447 [O2NM_NODE_ATTR_NUM] = &o2nm_node_attr_num.attr,
448 [O2NM_NODE_ATTR_PORT] = &o2nm_node_attr_ipv4_port.attr,
449 [O2NM_NODE_ATTR_ADDRESS] = &o2nm_node_attr_ipv4_address.attr,
450 [O2NM_NODE_ATTR_LOCAL] = &o2nm_node_attr_local.attr,
451 NULL,
454 static int o2nm_attr_index(struct configfs_attribute *attr)
456 int i;
457 for (i = 0; i < ARRAY_SIZE(o2nm_node_attrs); i++) {
458 if (attr == o2nm_node_attrs[i])
459 return i;
461 BUG();
462 return 0;
465 static ssize_t o2nm_node_show(struct config_item *item,
466 struct configfs_attribute *attr,
467 char *page)
469 struct o2nm_node *node = to_o2nm_node(item);
470 struct o2nm_node_attribute *o2nm_node_attr =
471 container_of(attr, struct o2nm_node_attribute, attr);
472 ssize_t ret = 0;
474 if (o2nm_node_attr->show)
475 ret = o2nm_node_attr->show(node, page);
476 return ret;
479 static ssize_t o2nm_node_store(struct config_item *item,
480 struct configfs_attribute *attr,
481 const char *page, size_t count)
483 struct o2nm_node *node = to_o2nm_node(item);
484 struct o2nm_node_attribute *o2nm_node_attr =
485 container_of(attr, struct o2nm_node_attribute, attr);
486 ssize_t ret;
487 int attr_index = o2nm_attr_index(attr);
489 if (o2nm_node_attr->store == NULL) {
490 ret = -EINVAL;
491 goto out;
494 if (test_bit(attr_index, &node->nd_set_attributes))
495 return -EBUSY;
497 ret = o2nm_node_attr->store(node, page, count);
498 if (ret < count)
499 goto out;
501 set_bit(attr_index, &node->nd_set_attributes);
502 out:
503 return ret;
506 static struct configfs_item_operations o2nm_node_item_ops = {
507 .release = o2nm_node_release,
508 .show_attribute = o2nm_node_show,
509 .store_attribute = o2nm_node_store,
512 static struct config_item_type o2nm_node_type = {
513 .ct_item_ops = &o2nm_node_item_ops,
514 .ct_attrs = o2nm_node_attrs,
515 .ct_owner = THIS_MODULE,
518 /* node set */
520 struct o2nm_node_group {
521 struct config_group ns_group;
522 /* some stuff? */
525 #if 0
526 static struct o2nm_node_group *to_o2nm_node_group(struct config_group *group)
528 return group ?
529 container_of(group, struct o2nm_node_group, ns_group)
530 : NULL;
532 #endif
534 struct o2nm_cluster_attribute {
535 struct configfs_attribute attr;
536 ssize_t (*show)(struct o2nm_cluster *, char *);
537 ssize_t (*store)(struct o2nm_cluster *, const char *, size_t);
540 static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
541 unsigned int *val)
543 unsigned long tmp;
544 char *p = (char *)page;
546 tmp = simple_strtoul(p, &p, 0);
547 if (!p || (*p && (*p != '\n')))
548 return -EINVAL;
550 if (tmp == 0)
551 return -EINVAL;
552 if (tmp >= (u32)-1)
553 return -ERANGE;
555 *val = tmp;
557 return count;
560 static ssize_t o2nm_cluster_attr_idle_timeout_ms_read(
561 struct o2nm_cluster *cluster, char *page)
563 return sprintf(page, "%u\n", cluster->cl_idle_timeout_ms);
566 static ssize_t o2nm_cluster_attr_idle_timeout_ms_write(
567 struct o2nm_cluster *cluster, const char *page, size_t count)
569 ssize_t ret;
570 unsigned int val;
572 ret = o2nm_cluster_attr_write(page, count, &val);
574 if (ret > 0) {
575 if (cluster->cl_idle_timeout_ms != val
576 && o2net_num_connected_peers()) {
577 mlog(ML_NOTICE,
578 "o2net: cannot change idle timeout after "
579 "the first peer has agreed to it."
580 " %d connected peers\n",
581 o2net_num_connected_peers());
582 ret = -EINVAL;
583 } else if (val <= cluster->cl_keepalive_delay_ms) {
584 mlog(ML_NOTICE, "o2net: idle timeout must be larger "
585 "than keepalive delay\n");
586 ret = -EINVAL;
587 } else {
588 cluster->cl_idle_timeout_ms = val;
592 return ret;
595 static ssize_t o2nm_cluster_attr_keepalive_delay_ms_read(
596 struct o2nm_cluster *cluster, char *page)
598 return sprintf(page, "%u\n", cluster->cl_keepalive_delay_ms);
601 static ssize_t o2nm_cluster_attr_keepalive_delay_ms_write(
602 struct o2nm_cluster *cluster, const char *page, size_t count)
604 ssize_t ret;
605 unsigned int val;
607 ret = o2nm_cluster_attr_write(page, count, &val);
609 if (ret > 0) {
610 if (cluster->cl_keepalive_delay_ms != val
611 && o2net_num_connected_peers()) {
612 mlog(ML_NOTICE,
613 "o2net: cannot change keepalive delay after"
614 " the first peer has agreed to it."
615 " %d connected peers\n",
616 o2net_num_connected_peers());
617 ret = -EINVAL;
618 } else if (val >= cluster->cl_idle_timeout_ms) {
619 mlog(ML_NOTICE, "o2net: keepalive delay must be "
620 "smaller than idle timeout\n");
621 ret = -EINVAL;
622 } else {
623 cluster->cl_keepalive_delay_ms = val;
627 return ret;
630 static ssize_t o2nm_cluster_attr_reconnect_delay_ms_read(
631 struct o2nm_cluster *cluster, char *page)
633 return sprintf(page, "%u\n", cluster->cl_reconnect_delay_ms);
636 static ssize_t o2nm_cluster_attr_reconnect_delay_ms_write(
637 struct o2nm_cluster *cluster, const char *page, size_t count)
639 return o2nm_cluster_attr_write(page, count,
640 &cluster->cl_reconnect_delay_ms);
642 static struct o2nm_cluster_attribute o2nm_cluster_attr_idle_timeout_ms = {
643 .attr = { .ca_owner = THIS_MODULE,
644 .ca_name = "idle_timeout_ms",
645 .ca_mode = S_IRUGO | S_IWUSR },
646 .show = o2nm_cluster_attr_idle_timeout_ms_read,
647 .store = o2nm_cluster_attr_idle_timeout_ms_write,
650 static struct o2nm_cluster_attribute o2nm_cluster_attr_keepalive_delay_ms = {
651 .attr = { .ca_owner = THIS_MODULE,
652 .ca_name = "keepalive_delay_ms",
653 .ca_mode = S_IRUGO | S_IWUSR },
654 .show = o2nm_cluster_attr_keepalive_delay_ms_read,
655 .store = o2nm_cluster_attr_keepalive_delay_ms_write,
658 static struct o2nm_cluster_attribute o2nm_cluster_attr_reconnect_delay_ms = {
659 .attr = { .ca_owner = THIS_MODULE,
660 .ca_name = "reconnect_delay_ms",
661 .ca_mode = S_IRUGO | S_IWUSR },
662 .show = o2nm_cluster_attr_reconnect_delay_ms_read,
663 .store = o2nm_cluster_attr_reconnect_delay_ms_write,
666 static struct configfs_attribute *o2nm_cluster_attrs[] = {
667 &o2nm_cluster_attr_idle_timeout_ms.attr,
668 &o2nm_cluster_attr_keepalive_delay_ms.attr,
669 &o2nm_cluster_attr_reconnect_delay_ms.attr,
670 NULL,
672 static ssize_t o2nm_cluster_show(struct config_item *item,
673 struct configfs_attribute *attr,
674 char *page)
676 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
677 struct o2nm_cluster_attribute *o2nm_cluster_attr =
678 container_of(attr, struct o2nm_cluster_attribute, attr);
679 ssize_t ret = 0;
681 if (o2nm_cluster_attr->show)
682 ret = o2nm_cluster_attr->show(cluster, page);
683 return ret;
686 static ssize_t o2nm_cluster_store(struct config_item *item,
687 struct configfs_attribute *attr,
688 const char *page, size_t count)
690 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
691 struct o2nm_cluster_attribute *o2nm_cluster_attr =
692 container_of(attr, struct o2nm_cluster_attribute, attr);
693 ssize_t ret;
695 if (o2nm_cluster_attr->store == NULL) {
696 ret = -EINVAL;
697 goto out;
700 ret = o2nm_cluster_attr->store(cluster, page, count);
701 if (ret < count)
702 goto out;
703 out:
704 return ret;
707 static struct config_item *o2nm_node_group_make_item(struct config_group *group,
708 const char *name)
710 struct o2nm_node *node = NULL;
711 struct config_item *ret = NULL;
713 if (strlen(name) > O2NM_MAX_NAME_LEN)
714 goto out; /* ENAMETOOLONG */
716 node = kzalloc(sizeof(struct o2nm_node), GFP_KERNEL);
717 if (node == NULL)
718 goto out; /* ENOMEM */
720 strcpy(node->nd_name, name); /* use item.ci_namebuf instead? */
721 config_item_init_type_name(&node->nd_item, name, &o2nm_node_type);
722 spin_lock_init(&node->nd_lock);
724 ret = &node->nd_item;
726 out:
727 if (ret == NULL)
728 kfree(node);
730 return ret;
733 static void o2nm_node_group_drop_item(struct config_group *group,
734 struct config_item *item)
736 struct o2nm_node *node = to_o2nm_node(item);
737 struct o2nm_cluster *cluster = to_o2nm_cluster(group->cg_item.ci_parent);
739 o2net_disconnect_node(node);
741 if (cluster->cl_has_local &&
742 (cluster->cl_local_node == node->nd_num)) {
743 cluster->cl_has_local = 0;
744 cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
745 o2net_stop_listening(node);
748 /* XXX call into net to stop this node from trading messages */
750 write_lock(&cluster->cl_nodes_lock);
752 /* XXX sloppy */
753 if (node->nd_ipv4_address)
754 rb_erase(&node->nd_ip_node, &cluster->cl_node_ip_tree);
756 /* nd_num might be 0 if the node number hasn't been set.. */
757 if (cluster->cl_nodes[node->nd_num] == node) {
758 cluster->cl_nodes[node->nd_num] = NULL;
759 clear_bit(node->nd_num, cluster->cl_nodes_bitmap);
761 write_unlock(&cluster->cl_nodes_lock);
763 config_item_put(item);
766 static struct configfs_group_operations o2nm_node_group_group_ops = {
767 .make_item = o2nm_node_group_make_item,
768 .drop_item = o2nm_node_group_drop_item,
771 static struct config_item_type o2nm_node_group_type = {
772 .ct_group_ops = &o2nm_node_group_group_ops,
773 .ct_owner = THIS_MODULE,
776 /* cluster */
778 static void o2nm_cluster_release(struct config_item *item)
780 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
782 kfree(cluster->cl_group.default_groups);
783 kfree(cluster);
786 static struct configfs_item_operations o2nm_cluster_item_ops = {
787 .release = o2nm_cluster_release,
788 .show_attribute = o2nm_cluster_show,
789 .store_attribute = o2nm_cluster_store,
792 static struct config_item_type o2nm_cluster_type = {
793 .ct_item_ops = &o2nm_cluster_item_ops,
794 .ct_attrs = o2nm_cluster_attrs,
795 .ct_owner = THIS_MODULE,
798 /* cluster set */
800 struct o2nm_cluster_group {
801 struct configfs_subsystem cs_subsys;
802 /* some stuff? */
805 #if 0
806 static struct o2nm_cluster_group *to_o2nm_cluster_group(struct config_group *group)
808 return group ?
809 container_of(to_configfs_subsystem(group), struct o2nm_cluster_group, cs_subsys)
810 : NULL;
812 #endif
814 static struct config_group *o2nm_cluster_group_make_group(struct config_group *group,
815 const char *name)
817 struct o2nm_cluster *cluster = NULL;
818 struct o2nm_node_group *ns = NULL;
819 struct config_group *o2hb_group = NULL, *ret = NULL;
820 void *defs = NULL;
822 /* this runs under the parent dir's i_mutex; there can be only
823 * one caller in here at a time */
824 if (o2nm_single_cluster)
825 goto out; /* ENOSPC */
827 cluster = kzalloc(sizeof(struct o2nm_cluster), GFP_KERNEL);
828 ns = kzalloc(sizeof(struct o2nm_node_group), GFP_KERNEL);
829 defs = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
830 o2hb_group = o2hb_alloc_hb_set();
831 if (cluster == NULL || ns == NULL || o2hb_group == NULL || defs == NULL)
832 goto out;
834 config_group_init_type_name(&cluster->cl_group, name,
835 &o2nm_cluster_type);
836 config_group_init_type_name(&ns->ns_group, "node",
837 &o2nm_node_group_type);
839 cluster->cl_group.default_groups = defs;
840 cluster->cl_group.default_groups[0] = &ns->ns_group;
841 cluster->cl_group.default_groups[1] = o2hb_group;
842 cluster->cl_group.default_groups[2] = NULL;
843 rwlock_init(&cluster->cl_nodes_lock);
844 cluster->cl_node_ip_tree = RB_ROOT;
845 cluster->cl_reconnect_delay_ms = O2NET_RECONNECT_DELAY_MS_DEFAULT;
846 cluster->cl_idle_timeout_ms = O2NET_IDLE_TIMEOUT_MS_DEFAULT;
847 cluster->cl_keepalive_delay_ms = O2NET_KEEPALIVE_DELAY_MS_DEFAULT;
849 ret = &cluster->cl_group;
850 o2nm_single_cluster = cluster;
852 out:
853 if (ret == NULL) {
854 kfree(cluster);
855 kfree(ns);
856 o2hb_free_hb_set(o2hb_group);
857 kfree(defs);
860 return ret;
863 static void o2nm_cluster_group_drop_item(struct config_group *group, struct config_item *item)
865 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
866 int i;
867 struct config_item *killme;
869 BUG_ON(o2nm_single_cluster != cluster);
870 o2nm_single_cluster = NULL;
872 for (i = 0; cluster->cl_group.default_groups[i]; i++) {
873 killme = &cluster->cl_group.default_groups[i]->cg_item;
874 cluster->cl_group.default_groups[i] = NULL;
875 config_item_put(killme);
878 config_item_put(item);
881 static struct configfs_group_operations o2nm_cluster_group_group_ops = {
882 .make_group = o2nm_cluster_group_make_group,
883 .drop_item = o2nm_cluster_group_drop_item,
886 static struct config_item_type o2nm_cluster_group_type = {
887 .ct_group_ops = &o2nm_cluster_group_group_ops,
888 .ct_owner = THIS_MODULE,
891 static struct o2nm_cluster_group o2nm_cluster_group = {
892 .cs_subsys = {
893 .su_group = {
894 .cg_item = {
895 .ci_namebuf = "cluster",
896 .ci_type = &o2nm_cluster_group_type,
902 int o2nm_depend_item(struct config_item *item)
904 return configfs_depend_item(&o2nm_cluster_group.cs_subsys, item);
907 void o2nm_undepend_item(struct config_item *item)
909 configfs_undepend_item(&o2nm_cluster_group.cs_subsys, item);
912 int o2nm_depend_this_node(void)
914 int ret = 0;
915 struct o2nm_node *local_node;
917 local_node = o2nm_get_node_by_num(o2nm_this_node());
918 if (!local_node) {
919 ret = -EINVAL;
920 goto out;
923 ret = o2nm_depend_item(&local_node->nd_item);
924 o2nm_node_put(local_node);
926 out:
927 return ret;
930 void o2nm_undepend_this_node(void)
932 struct o2nm_node *local_node;
934 local_node = o2nm_get_node_by_num(o2nm_this_node());
935 BUG_ON(!local_node);
937 o2nm_undepend_item(&local_node->nd_item);
938 o2nm_node_put(local_node);
942 static void __exit exit_o2nm(void)
944 if (ocfs2_table_header)
945 unregister_sysctl_table(ocfs2_table_header);
947 /* XXX sync with hb callbacks and shut down hb? */
948 o2net_unregister_hb_callbacks();
949 configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
950 o2cb_sys_shutdown();
952 o2net_exit();
955 static int __init init_o2nm(void)
957 int ret = -1;
959 cluster_print_version();
961 o2hb_init();
962 o2net_init();
964 ocfs2_table_header = register_sysctl_table(ocfs2_root_table);
965 if (!ocfs2_table_header) {
966 printk(KERN_ERR "nodemanager: unable to register sysctl\n");
967 ret = -ENOMEM; /* or something. */
968 goto out_o2net;
971 ret = o2net_register_hb_callbacks();
972 if (ret)
973 goto out_sysctl;
975 config_group_init(&o2nm_cluster_group.cs_subsys.su_group);
976 mutex_init(&o2nm_cluster_group.cs_subsys.su_mutex);
977 ret = configfs_register_subsystem(&o2nm_cluster_group.cs_subsys);
978 if (ret) {
979 printk(KERN_ERR "nodemanager: Registration returned %d\n", ret);
980 goto out_callbacks;
983 ret = o2cb_sys_init();
984 if (!ret)
985 goto out;
987 configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
988 out_callbacks:
989 o2net_unregister_hb_callbacks();
990 out_sysctl:
991 unregister_sysctl_table(ocfs2_table_header);
992 out_o2net:
993 o2net_exit();
994 out:
995 return ret;
998 MODULE_AUTHOR("Oracle");
999 MODULE_LICENSE("GPL");
1001 module_init(init_o2nm)
1002 module_exit(exit_o2nm)