HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / ocfs2 / cluster / nodemanager.c
bloba87a08e1bfab0cdd58c7e283a7bd637825eea153
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/slab.h>
23 #include <linux/kernel.h>
24 #include <linux/module.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"
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;
38 char *o2nm_fence_method_desc[O2NM_FENCE_METHODS] = {
39 "reset", /* O2NM_FENCE_RESET */
40 "panic", /* O2NM_FENCE_PANIC */
43 static inline void o2nm_lock_subsystem(void);
44 static inline void o2nm_unlock_subsystem(void);
46 struct o2nm_node *o2nm_get_node_by_num(u8 node_num)
48 struct o2nm_node *node = NULL;
50 if (node_num >= O2NM_MAX_NODES || o2nm_single_cluster == NULL)
51 goto out;
53 read_lock(&o2nm_single_cluster->cl_nodes_lock);
54 node = o2nm_single_cluster->cl_nodes[node_num];
55 if (node)
56 config_item_get(&node->nd_item);
57 read_unlock(&o2nm_single_cluster->cl_nodes_lock);
58 out:
59 return node;
61 EXPORT_SYMBOL_GPL(o2nm_get_node_by_num);
63 int o2nm_configured_node_map(unsigned long *map, unsigned bytes)
65 struct o2nm_cluster *cluster = o2nm_single_cluster;
67 BUG_ON(bytes < (sizeof(cluster->cl_nodes_bitmap)));
69 if (cluster == NULL)
70 return -EINVAL;
72 read_lock(&cluster->cl_nodes_lock);
73 memcpy(map, cluster->cl_nodes_bitmap, sizeof(cluster->cl_nodes_bitmap));
74 read_unlock(&cluster->cl_nodes_lock);
76 return 0;
78 EXPORT_SYMBOL_GPL(o2nm_configured_node_map);
80 static struct o2nm_node *o2nm_node_ip_tree_lookup(struct o2nm_cluster *cluster,
81 __be32 ip_needle,
82 struct rb_node ***ret_p,
83 struct rb_node **ret_parent)
85 struct rb_node **p = &cluster->cl_node_ip_tree.rb_node;
86 struct rb_node *parent = NULL;
87 struct o2nm_node *node, *ret = NULL;
89 while (*p) {
90 int cmp;
92 parent = *p;
93 node = rb_entry(parent, struct o2nm_node, nd_ip_node);
95 cmp = memcmp(&ip_needle, &node->nd_ipv4_address,
96 sizeof(ip_needle));
97 if (cmp < 0)
98 p = &(*p)->rb_left;
99 else if (cmp > 0)
100 p = &(*p)->rb_right;
101 else {
102 ret = node;
103 break;
107 if (ret_p != NULL)
108 *ret_p = p;
109 if (ret_parent != NULL)
110 *ret_parent = parent;
112 return ret;
115 struct o2nm_node *o2nm_get_node_by_ip(__be32 addr)
117 struct o2nm_node *node = NULL;
118 struct o2nm_cluster *cluster = o2nm_single_cluster;
120 if (cluster == NULL)
121 goto out;
123 read_lock(&cluster->cl_nodes_lock);
124 node = o2nm_node_ip_tree_lookup(cluster, addr, NULL, NULL);
125 if (node)
126 config_item_get(&node->nd_item);
127 read_unlock(&cluster->cl_nodes_lock);
129 out:
130 return node;
132 EXPORT_SYMBOL_GPL(o2nm_get_node_by_ip);
134 void o2nm_node_put(struct o2nm_node *node)
136 config_item_put(&node->nd_item);
138 EXPORT_SYMBOL_GPL(o2nm_node_put);
140 void o2nm_node_get(struct o2nm_node *node)
142 config_item_get(&node->nd_item);
144 EXPORT_SYMBOL_GPL(o2nm_node_get);
146 u8 o2nm_this_node(void)
148 u8 node_num = O2NM_MAX_NODES;
150 if (o2nm_single_cluster && o2nm_single_cluster->cl_has_local)
151 node_num = o2nm_single_cluster->cl_local_node;
153 return node_num;
155 EXPORT_SYMBOL_GPL(o2nm_this_node);
157 /* node configfs bits */
159 static struct o2nm_cluster *to_o2nm_cluster(struct config_item *item)
161 return item ?
162 container_of(to_config_group(item), struct o2nm_cluster,
163 cl_group)
164 : NULL;
167 static struct o2nm_node *to_o2nm_node(struct config_item *item)
169 return item ? container_of(item, struct o2nm_node, nd_item) : NULL;
172 static void o2nm_node_release(struct config_item *item)
174 struct o2nm_node *node = to_o2nm_node(item);
175 kfree(node);
178 static ssize_t o2nm_node_num_show(struct config_item *item, char *page)
180 return sprintf(page, "%d\n", to_o2nm_node(item)->nd_num);
183 static struct o2nm_cluster *to_o2nm_cluster_from_node(struct o2nm_node *node)
185 /* through the first node_set .parent
186 * mycluster/nodes/mynode == o2nm_cluster->o2nm_node_group->o2nm_node */
187 if (node->nd_item.ci_parent)
188 return to_o2nm_cluster(node->nd_item.ci_parent->ci_parent);
189 else
190 return NULL;
193 enum {
194 O2NM_NODE_ATTR_NUM = 0,
195 O2NM_NODE_ATTR_PORT,
196 O2NM_NODE_ATTR_ADDRESS,
199 static ssize_t o2nm_node_num_store(struct config_item *item, const char *page,
200 size_t count)
202 struct o2nm_node *node = to_o2nm_node(item);
203 struct o2nm_cluster *cluster;
204 unsigned long tmp;
205 char *p = (char *)page;
206 int ret = 0;
208 tmp = simple_strtoul(p, &p, 0);
209 if (!p || (*p && (*p != '\n')))
210 return -EINVAL;
212 if (tmp >= O2NM_MAX_NODES)
213 return -ERANGE;
215 /* once we're in the cl_nodes tree networking can look us up by
216 * node number and try to use our address and port attributes
217 * to connect to this node.. make sure that they've been set
218 * before writing the node attribute? */
219 if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
220 !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
221 return -EINVAL; /* XXX */
223 o2nm_lock_subsystem();
224 cluster = to_o2nm_cluster_from_node(node);
225 if (!cluster) {
226 o2nm_unlock_subsystem();
227 return -EINVAL;
230 write_lock(&cluster->cl_nodes_lock);
231 if (cluster->cl_nodes[tmp])
232 ret = -EEXIST;
233 else if (test_and_set_bit(O2NM_NODE_ATTR_NUM,
234 &node->nd_set_attributes))
235 ret = -EBUSY;
236 else {
237 cluster->cl_nodes[tmp] = node;
238 node->nd_num = tmp;
239 set_bit(tmp, cluster->cl_nodes_bitmap);
241 write_unlock(&cluster->cl_nodes_lock);
242 o2nm_unlock_subsystem();
244 if (ret)
245 return ret;
247 return count;
249 static ssize_t o2nm_node_ipv4_port_show(struct config_item *item, char *page)
251 return sprintf(page, "%u\n", ntohs(to_o2nm_node(item)->nd_ipv4_port));
254 static ssize_t o2nm_node_ipv4_port_store(struct config_item *item,
255 const char *page, size_t count)
257 struct o2nm_node *node = to_o2nm_node(item);
258 unsigned long tmp;
259 char *p = (char *)page;
261 tmp = simple_strtoul(p, &p, 0);
262 if (!p || (*p && (*p != '\n')))
263 return -EINVAL;
265 if (tmp == 0)
266 return -EINVAL;
267 if (tmp >= (u16)-1)
268 return -ERANGE;
270 if (test_and_set_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
271 return -EBUSY;
272 node->nd_ipv4_port = htons(tmp);
274 return count;
277 static ssize_t o2nm_node_ipv4_address_show(struct config_item *item, char *page)
279 return sprintf(page, "%pI4\n", &to_o2nm_node(item)->nd_ipv4_address);
282 static ssize_t o2nm_node_ipv4_address_store(struct config_item *item,
283 const char *page,
284 size_t count)
286 struct o2nm_node *node = to_o2nm_node(item);
287 struct o2nm_cluster *cluster;
288 int ret, i;
289 struct rb_node **p, *parent;
290 unsigned int octets[4];
291 __be32 ipv4_addr = 0;
293 ret = sscanf(page, "%3u.%3u.%3u.%3u", &octets[3], &octets[2],
294 &octets[1], &octets[0]);
295 if (ret != 4)
296 return -EINVAL;
298 for (i = 0; i < ARRAY_SIZE(octets); i++) {
299 if (octets[i] > 255)
300 return -ERANGE;
301 be32_add_cpu(&ipv4_addr, octets[i] << (i * 8));
304 o2nm_lock_subsystem();
305 cluster = to_o2nm_cluster_from_node(node);
306 if (!cluster) {
307 o2nm_unlock_subsystem();
308 return -EINVAL;
311 ret = 0;
312 write_lock(&cluster->cl_nodes_lock);
313 if (o2nm_node_ip_tree_lookup(cluster, ipv4_addr, &p, &parent))
314 ret = -EEXIST;
315 else if (test_and_set_bit(O2NM_NODE_ATTR_ADDRESS,
316 &node->nd_set_attributes))
317 ret = -EBUSY;
318 else {
319 rb_link_node(&node->nd_ip_node, parent, p);
320 rb_insert_color(&node->nd_ip_node, &cluster->cl_node_ip_tree);
322 write_unlock(&cluster->cl_nodes_lock);
323 o2nm_unlock_subsystem();
325 if (ret)
326 return ret;
328 memcpy(&node->nd_ipv4_address, &ipv4_addr, sizeof(ipv4_addr));
330 return count;
333 static ssize_t o2nm_node_local_show(struct config_item *item, char *page)
335 return sprintf(page, "%d\n", to_o2nm_node(item)->nd_local);
338 static ssize_t o2nm_node_local_store(struct config_item *item, const char *page,
339 size_t count)
341 struct o2nm_node *node = to_o2nm_node(item);
342 struct o2nm_cluster *cluster;
343 unsigned long tmp;
344 char *p = (char *)page;
345 ssize_t ret;
347 tmp = simple_strtoul(p, &p, 0);
348 if (!p || (*p && (*p != '\n')))
349 return -EINVAL;
351 tmp = !!tmp; /* boolean of whether this node wants to be local */
353 /* setting local turns on networking rx for now so we require having
354 * set everything else first */
355 if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
356 !test_bit(O2NM_NODE_ATTR_NUM, &node->nd_set_attributes) ||
357 !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
358 return -EINVAL; /* XXX */
360 o2nm_lock_subsystem();
361 cluster = to_o2nm_cluster_from_node(node);
362 if (!cluster) {
363 ret = -EINVAL;
364 goto out;
367 /* the only failure case is trying to set a new local node
368 * when a different one is already set */
369 if (tmp && tmp == cluster->cl_has_local &&
370 cluster->cl_local_node != node->nd_num) {
371 ret = -EBUSY;
372 goto out;
375 /* bring up the rx thread if we're setting the new local node. */
376 if (tmp && !cluster->cl_has_local) {
377 ret = o2net_start_listening(node);
378 if (ret)
379 goto out;
382 if (!tmp && cluster->cl_has_local &&
383 cluster->cl_local_node == node->nd_num) {
384 o2net_stop_listening(node);
385 cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
388 node->nd_local = tmp;
389 if (node->nd_local) {
390 cluster->cl_has_local = tmp;
391 cluster->cl_local_node = node->nd_num;
394 ret = count;
396 out:
397 o2nm_unlock_subsystem();
398 return ret;
401 CONFIGFS_ATTR(o2nm_node_, num);
402 CONFIGFS_ATTR(o2nm_node_, ipv4_port);
403 CONFIGFS_ATTR(o2nm_node_, ipv4_address);
404 CONFIGFS_ATTR(o2nm_node_, local);
406 static struct configfs_attribute *o2nm_node_attrs[] = {
407 &o2nm_node_attr_num,
408 &o2nm_node_attr_ipv4_port,
409 &o2nm_node_attr_ipv4_address,
410 &o2nm_node_attr_local,
411 NULL,
414 static struct configfs_item_operations o2nm_node_item_ops = {
415 .release = o2nm_node_release,
418 static struct config_item_type o2nm_node_type = {
419 .ct_item_ops = &o2nm_node_item_ops,
420 .ct_attrs = o2nm_node_attrs,
421 .ct_owner = THIS_MODULE,
424 /* node set */
426 struct o2nm_node_group {
427 struct config_group ns_group;
428 /* some stuff? */
431 #if 0
432 static struct o2nm_node_group *to_o2nm_node_group(struct config_group *group)
434 return group ?
435 container_of(group, struct o2nm_node_group, ns_group)
436 : NULL;
438 #endif
440 static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
441 unsigned int *val)
443 unsigned long tmp;
444 char *p = (char *)page;
446 tmp = simple_strtoul(p, &p, 0);
447 if (!p || (*p && (*p != '\n')))
448 return -EINVAL;
450 if (tmp == 0)
451 return -EINVAL;
452 if (tmp >= (u32)-1)
453 return -ERANGE;
455 *val = tmp;
457 return count;
460 static ssize_t o2nm_cluster_idle_timeout_ms_show(struct config_item *item,
461 char *page)
463 return sprintf(page, "%u\n", to_o2nm_cluster(item)->cl_idle_timeout_ms);
466 static ssize_t o2nm_cluster_idle_timeout_ms_store(struct config_item *item,
467 const char *page, size_t count)
469 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
470 ssize_t ret;
471 unsigned int val;
473 ret = o2nm_cluster_attr_write(page, count, &val);
475 if (ret > 0) {
476 if (cluster->cl_idle_timeout_ms != val
477 && o2net_num_connected_peers()) {
478 mlog(ML_NOTICE,
479 "o2net: cannot change idle timeout after "
480 "the first peer has agreed to it."
481 " %d connected peers\n",
482 o2net_num_connected_peers());
483 ret = -EINVAL;
484 } else if (val <= cluster->cl_keepalive_delay_ms) {
485 mlog(ML_NOTICE, "o2net: idle timeout must be larger "
486 "than keepalive delay\n");
487 ret = -EINVAL;
488 } else {
489 cluster->cl_idle_timeout_ms = val;
493 return ret;
496 static ssize_t o2nm_cluster_keepalive_delay_ms_show(
497 struct config_item *item, char *page)
499 return sprintf(page, "%u\n",
500 to_o2nm_cluster(item)->cl_keepalive_delay_ms);
503 static ssize_t o2nm_cluster_keepalive_delay_ms_store(
504 struct config_item *item, const char *page, size_t count)
506 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
507 ssize_t ret;
508 unsigned int val;
510 ret = o2nm_cluster_attr_write(page, count, &val);
512 if (ret > 0) {
513 if (cluster->cl_keepalive_delay_ms != val
514 && o2net_num_connected_peers()) {
515 mlog(ML_NOTICE,
516 "o2net: cannot change keepalive delay after"
517 " the first peer has agreed to it."
518 " %d connected peers\n",
519 o2net_num_connected_peers());
520 ret = -EINVAL;
521 } else if (val >= cluster->cl_idle_timeout_ms) {
522 mlog(ML_NOTICE, "o2net: keepalive delay must be "
523 "smaller than idle timeout\n");
524 ret = -EINVAL;
525 } else {
526 cluster->cl_keepalive_delay_ms = val;
530 return ret;
533 static ssize_t o2nm_cluster_reconnect_delay_ms_show(
534 struct config_item *item, char *page)
536 return sprintf(page, "%u\n",
537 to_o2nm_cluster(item)->cl_reconnect_delay_ms);
540 static ssize_t o2nm_cluster_reconnect_delay_ms_store(
541 struct config_item *item, const char *page, size_t count)
543 return o2nm_cluster_attr_write(page, count,
544 &to_o2nm_cluster(item)->cl_reconnect_delay_ms);
547 static ssize_t o2nm_cluster_fence_method_show(
548 struct config_item *item, char *page)
550 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
551 ssize_t ret = 0;
553 if (cluster)
554 ret = sprintf(page, "%s\n",
555 o2nm_fence_method_desc[cluster->cl_fence_method]);
556 return ret;
559 static ssize_t o2nm_cluster_fence_method_store(
560 struct config_item *item, const char *page, size_t count)
562 unsigned int i;
564 if (page[count - 1] != '\n')
565 goto bail;
567 for (i = 0; i < O2NM_FENCE_METHODS; ++i) {
568 if (count != strlen(o2nm_fence_method_desc[i]) + 1)
569 continue;
570 if (strncasecmp(page, o2nm_fence_method_desc[i], count - 1))
571 continue;
572 if (to_o2nm_cluster(item)->cl_fence_method != i) {
573 printk(KERN_INFO "ocfs2: Changing fence method to %s\n",
574 o2nm_fence_method_desc[i]);
575 to_o2nm_cluster(item)->cl_fence_method = i;
577 return count;
580 bail:
581 return -EINVAL;
584 CONFIGFS_ATTR(o2nm_cluster_, idle_timeout_ms);
585 CONFIGFS_ATTR(o2nm_cluster_, keepalive_delay_ms);
586 CONFIGFS_ATTR(o2nm_cluster_, reconnect_delay_ms);
587 CONFIGFS_ATTR(o2nm_cluster_, fence_method);
589 static struct configfs_attribute *o2nm_cluster_attrs[] = {
590 &o2nm_cluster_attr_idle_timeout_ms,
591 &o2nm_cluster_attr_keepalive_delay_ms,
592 &o2nm_cluster_attr_reconnect_delay_ms,
593 &o2nm_cluster_attr_fence_method,
594 NULL,
597 static struct config_item *o2nm_node_group_make_item(struct config_group *group,
598 const char *name)
600 struct o2nm_node *node = NULL;
602 if (strlen(name) > O2NM_MAX_NAME_LEN)
603 return ERR_PTR(-ENAMETOOLONG);
605 node = kzalloc(sizeof(struct o2nm_node), GFP_KERNEL);
606 if (node == NULL)
607 return ERR_PTR(-ENOMEM);
609 strcpy(node->nd_name, name); /* use item.ci_namebuf instead? */
610 config_item_init_type_name(&node->nd_item, name, &o2nm_node_type);
611 spin_lock_init(&node->nd_lock);
613 mlog(ML_CLUSTER, "o2nm: Registering node %s\n", name);
615 return &node->nd_item;
618 static void o2nm_node_group_drop_item(struct config_group *group,
619 struct config_item *item)
621 struct o2nm_node *node = to_o2nm_node(item);
622 struct o2nm_cluster *cluster = to_o2nm_cluster(group->cg_item.ci_parent);
624 if (cluster->cl_nodes[node->nd_num] == node) {
625 o2net_disconnect_node(node);
627 if (cluster->cl_has_local &&
628 (cluster->cl_local_node == node->nd_num)) {
629 cluster->cl_has_local = 0;
630 cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
631 o2net_stop_listening(node);
635 /* XXX call into net to stop this node from trading messages */
637 write_lock(&cluster->cl_nodes_lock);
639 /* XXX sloppy */
640 if (node->nd_ipv4_address)
641 rb_erase(&node->nd_ip_node, &cluster->cl_node_ip_tree);
643 /* nd_num might be 0 if the node number hasn't been set.. */
644 if (cluster->cl_nodes[node->nd_num] == node) {
645 cluster->cl_nodes[node->nd_num] = NULL;
646 clear_bit(node->nd_num, cluster->cl_nodes_bitmap);
648 write_unlock(&cluster->cl_nodes_lock);
650 mlog(ML_CLUSTER, "o2nm: Unregistered node %s\n",
651 config_item_name(&node->nd_item));
653 config_item_put(item);
656 static struct configfs_group_operations o2nm_node_group_group_ops = {
657 .make_item = o2nm_node_group_make_item,
658 .drop_item = o2nm_node_group_drop_item,
661 static struct config_item_type o2nm_node_group_type = {
662 .ct_group_ops = &o2nm_node_group_group_ops,
663 .ct_owner = THIS_MODULE,
666 /* cluster */
668 static void o2nm_cluster_release(struct config_item *item)
670 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
672 kfree(cluster->cl_group.default_groups);
673 kfree(cluster);
676 static struct configfs_item_operations o2nm_cluster_item_ops = {
677 .release = o2nm_cluster_release,
680 static struct config_item_type o2nm_cluster_type = {
681 .ct_item_ops = &o2nm_cluster_item_ops,
682 .ct_attrs = o2nm_cluster_attrs,
683 .ct_owner = THIS_MODULE,
686 /* cluster set */
688 struct o2nm_cluster_group {
689 struct configfs_subsystem cs_subsys;
690 /* some stuff? */
693 #if 0
694 static struct o2nm_cluster_group *to_o2nm_cluster_group(struct config_group *group)
696 return group ?
697 container_of(to_configfs_subsystem(group), struct o2nm_cluster_group, cs_subsys)
698 : NULL;
700 #endif
702 static struct config_group *o2nm_cluster_group_make_group(struct config_group *group,
703 const char *name)
705 struct o2nm_cluster *cluster = NULL;
706 struct o2nm_node_group *ns = NULL;
707 struct config_group *o2hb_group = NULL, *ret = NULL;
708 void *defs = NULL;
710 /* this runs under the parent dir's i_mutex; there can be only
711 * one caller in here at a time */
712 if (o2nm_single_cluster)
713 return ERR_PTR(-ENOSPC);
715 cluster = kzalloc(sizeof(struct o2nm_cluster), GFP_KERNEL);
716 ns = kzalloc(sizeof(struct o2nm_node_group), GFP_KERNEL);
717 defs = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
718 o2hb_group = o2hb_alloc_hb_set();
719 if (cluster == NULL || ns == NULL || o2hb_group == NULL || defs == NULL)
720 goto out;
722 config_group_init_type_name(&cluster->cl_group, name,
723 &o2nm_cluster_type);
724 config_group_init_type_name(&ns->ns_group, "node",
725 &o2nm_node_group_type);
727 cluster->cl_group.default_groups = defs;
728 cluster->cl_group.default_groups[0] = &ns->ns_group;
729 cluster->cl_group.default_groups[1] = o2hb_group;
730 cluster->cl_group.default_groups[2] = NULL;
731 rwlock_init(&cluster->cl_nodes_lock);
732 cluster->cl_node_ip_tree = RB_ROOT;
733 cluster->cl_reconnect_delay_ms = O2NET_RECONNECT_DELAY_MS_DEFAULT;
734 cluster->cl_idle_timeout_ms = O2NET_IDLE_TIMEOUT_MS_DEFAULT;
735 cluster->cl_keepalive_delay_ms = O2NET_KEEPALIVE_DELAY_MS_DEFAULT;
736 cluster->cl_fence_method = O2NM_FENCE_RESET;
738 ret = &cluster->cl_group;
739 o2nm_single_cluster = cluster;
741 out:
742 if (ret == NULL) {
743 kfree(cluster);
744 kfree(ns);
745 o2hb_free_hb_set(o2hb_group);
746 kfree(defs);
747 ret = ERR_PTR(-ENOMEM);
750 return ret;
753 static void o2nm_cluster_group_drop_item(struct config_group *group, struct config_item *item)
755 struct o2nm_cluster *cluster = to_o2nm_cluster(item);
756 int i;
757 struct config_item *killme;
759 BUG_ON(o2nm_single_cluster != cluster);
760 o2nm_single_cluster = NULL;
762 for (i = 0; cluster->cl_group.default_groups[i]; i++) {
763 killme = &cluster->cl_group.default_groups[i]->cg_item;
764 cluster->cl_group.default_groups[i] = NULL;
765 config_item_put(killme);
768 config_item_put(item);
771 static struct configfs_group_operations o2nm_cluster_group_group_ops = {
772 .make_group = o2nm_cluster_group_make_group,
773 .drop_item = o2nm_cluster_group_drop_item,
776 static struct config_item_type o2nm_cluster_group_type = {
777 .ct_group_ops = &o2nm_cluster_group_group_ops,
778 .ct_owner = THIS_MODULE,
781 static struct o2nm_cluster_group o2nm_cluster_group = {
782 .cs_subsys = {
783 .su_group = {
784 .cg_item = {
785 .ci_namebuf = "cluster",
786 .ci_type = &o2nm_cluster_group_type,
792 static inline void o2nm_lock_subsystem(void)
794 mutex_lock(&o2nm_cluster_group.cs_subsys.su_mutex);
797 static inline void o2nm_unlock_subsystem(void)
799 mutex_unlock(&o2nm_cluster_group.cs_subsys.su_mutex);
802 int o2nm_depend_item(struct config_item *item)
804 return configfs_depend_item(&o2nm_cluster_group.cs_subsys, item);
807 void o2nm_undepend_item(struct config_item *item)
809 configfs_undepend_item(&o2nm_cluster_group.cs_subsys, item);
812 int o2nm_depend_this_node(void)
814 int ret = 0;
815 struct o2nm_node *local_node;
817 local_node = o2nm_get_node_by_num(o2nm_this_node());
818 if (!local_node) {
819 ret = -EINVAL;
820 goto out;
823 ret = o2nm_depend_item(&local_node->nd_item);
824 o2nm_node_put(local_node);
826 out:
827 return ret;
830 void o2nm_undepend_this_node(void)
832 struct o2nm_node *local_node;
834 local_node = o2nm_get_node_by_num(o2nm_this_node());
835 BUG_ON(!local_node);
837 o2nm_undepend_item(&local_node->nd_item);
838 o2nm_node_put(local_node);
842 static void __exit exit_o2nm(void)
844 /* XXX sync with hb callbacks and shut down hb? */
845 o2net_unregister_hb_callbacks();
846 configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
847 o2cb_sys_shutdown();
849 o2net_exit();
850 o2hb_exit();
853 static int __init init_o2nm(void)
855 int ret = -1;
857 ret = o2hb_init();
858 if (ret)
859 goto out;
861 ret = o2net_init();
862 if (ret)
863 goto out_o2hb;
865 ret = o2net_register_hb_callbacks();
866 if (ret)
867 goto out_o2net;
869 config_group_init(&o2nm_cluster_group.cs_subsys.su_group);
870 mutex_init(&o2nm_cluster_group.cs_subsys.su_mutex);
871 ret = configfs_register_subsystem(&o2nm_cluster_group.cs_subsys);
872 if (ret) {
873 printk(KERN_ERR "nodemanager: Registration returned %d\n", ret);
874 goto out_callbacks;
877 ret = o2cb_sys_init();
878 if (!ret)
879 goto out;
881 configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
882 out_callbacks:
883 o2net_unregister_hb_callbacks();
884 out_o2net:
885 o2net_exit();
886 out_o2hb:
887 o2hb_exit();
888 out:
889 return ret;
892 MODULE_AUTHOR("Oracle");
893 MODULE_LICENSE("GPL");
894 MODULE_DESCRIPTION("OCFS2 cluster management");
896 module_init(init_o2nm)
897 module_exit(exit_o2nm)