nodemanager.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. #include <linux/slab.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/configfs.h>
  25. #include "tcp.h"
  26. #include "nodemanager.h"
  27. #include "heartbeat.h"
  28. #include "masklog.h"
  29. #include "sys.h"
  30. /* for now we operate under the assertion that there can be only one
  31. * cluster active at a time. Changing this will require trickling
  32. * cluster references throughout where nodes are looked up */
  33. struct o2nm_cluster *o2nm_single_cluster = NULL;
  34. char *o2nm_fence_method_desc[O2NM_FENCE_METHODS] = {
  35. "reset", /* O2NM_FENCE_RESET */
  36. "panic", /* O2NM_FENCE_PANIC */
  37. };
  38. struct o2nm_node *o2nm_get_node_by_num(u8 node_num)
  39. {
  40. struct o2nm_node *node = NULL;
  41. if (node_num >= O2NM_MAX_NODES || o2nm_single_cluster == NULL)
  42. goto out;
  43. read_lock(&o2nm_single_cluster->cl_nodes_lock);
  44. node = o2nm_single_cluster->cl_nodes[node_num];
  45. if (node)
  46. config_item_get(&node->nd_item);
  47. read_unlock(&o2nm_single_cluster->cl_nodes_lock);
  48. out:
  49. return node;
  50. }
  51. EXPORT_SYMBOL_GPL(o2nm_get_node_by_num);
  52. int o2nm_configured_node_map(unsigned long *map, unsigned bytes)
  53. {
  54. struct o2nm_cluster *cluster = o2nm_single_cluster;
  55. BUG_ON(bytes < (sizeof(cluster->cl_nodes_bitmap)));
  56. if (cluster == NULL)
  57. return -EINVAL;
  58. read_lock(&cluster->cl_nodes_lock);
  59. memcpy(map, cluster->cl_nodes_bitmap, sizeof(cluster->cl_nodes_bitmap));
  60. read_unlock(&cluster->cl_nodes_lock);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(o2nm_configured_node_map);
  64. static struct o2nm_node *o2nm_node_ip_tree_lookup(struct o2nm_cluster *cluster,
  65. __be32 ip_needle,
  66. struct rb_node ***ret_p,
  67. struct rb_node **ret_parent)
  68. {
  69. struct rb_node **p = &cluster->cl_node_ip_tree.rb_node;
  70. struct rb_node *parent = NULL;
  71. struct o2nm_node *node, *ret = NULL;
  72. while (*p) {
  73. int cmp;
  74. parent = *p;
  75. node = rb_entry(parent, struct o2nm_node, nd_ip_node);
  76. cmp = memcmp(&ip_needle, &node->nd_ipv4_address,
  77. sizeof(ip_needle));
  78. if (cmp < 0)
  79. p = &(*p)->rb_left;
  80. else if (cmp > 0)
  81. p = &(*p)->rb_right;
  82. else {
  83. ret = node;
  84. break;
  85. }
  86. }
  87. if (ret_p != NULL)
  88. *ret_p = p;
  89. if (ret_parent != NULL)
  90. *ret_parent = parent;
  91. return ret;
  92. }
  93. struct o2nm_node *o2nm_get_node_by_ip(__be32 addr)
  94. {
  95. struct o2nm_node *node = NULL;
  96. struct o2nm_cluster *cluster = o2nm_single_cluster;
  97. if (cluster == NULL)
  98. goto out;
  99. read_lock(&cluster->cl_nodes_lock);
  100. node = o2nm_node_ip_tree_lookup(cluster, addr, NULL, NULL);
  101. if (node)
  102. config_item_get(&node->nd_item);
  103. read_unlock(&cluster->cl_nodes_lock);
  104. out:
  105. return node;
  106. }
  107. EXPORT_SYMBOL_GPL(o2nm_get_node_by_ip);
  108. void o2nm_node_put(struct o2nm_node *node)
  109. {
  110. config_item_put(&node->nd_item);
  111. }
  112. EXPORT_SYMBOL_GPL(o2nm_node_put);
  113. void o2nm_node_get(struct o2nm_node *node)
  114. {
  115. config_item_get(&node->nd_item);
  116. }
  117. EXPORT_SYMBOL_GPL(o2nm_node_get);
  118. u8 o2nm_this_node(void)
  119. {
  120. u8 node_num = O2NM_MAX_NODES;
  121. if (o2nm_single_cluster && o2nm_single_cluster->cl_has_local)
  122. node_num = o2nm_single_cluster->cl_local_node;
  123. return node_num;
  124. }
  125. EXPORT_SYMBOL_GPL(o2nm_this_node);
  126. /* node configfs bits */
  127. static struct o2nm_cluster *to_o2nm_cluster(struct config_item *item)
  128. {
  129. return item ?
  130. container_of(to_config_group(item), struct o2nm_cluster,
  131. cl_group)
  132. : NULL;
  133. }
  134. static struct o2nm_node *to_o2nm_node(struct config_item *item)
  135. {
  136. return item ? container_of(item, struct o2nm_node, nd_item) : NULL;
  137. }
  138. static void o2nm_node_release(struct config_item *item)
  139. {
  140. struct o2nm_node *node = to_o2nm_node(item);
  141. kfree(node);
  142. }
  143. static ssize_t o2nm_node_num_show(struct config_item *item, char *page)
  144. {
  145. return sprintf(page, "%d\n", to_o2nm_node(item)->nd_num);
  146. }
  147. static struct o2nm_cluster *to_o2nm_cluster_from_node(struct o2nm_node *node)
  148. {
  149. /* through the first node_set .parent
  150. * mycluster/nodes/mynode == o2nm_cluster->o2nm_node_group->o2nm_node */
  151. return to_o2nm_cluster(node->nd_item.ci_parent->ci_parent);
  152. }
  153. enum {
  154. O2NM_NODE_ATTR_NUM = 0,
  155. O2NM_NODE_ATTR_PORT,
  156. O2NM_NODE_ATTR_ADDRESS,
  157. };
  158. static ssize_t o2nm_node_num_store(struct config_item *item, const char *page,
  159. size_t count)
  160. {
  161. struct o2nm_node *node = to_o2nm_node(item);
  162. struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
  163. unsigned long tmp;
  164. char *p = (char *)page;
  165. int ret = 0;
  166. tmp = simple_strtoul(p, &p, 0);
  167. if (!p || (*p && (*p != '\n')))
  168. return -EINVAL;
  169. if (tmp >= O2NM_MAX_NODES)
  170. return -ERANGE;
  171. /* once we're in the cl_nodes tree networking can look us up by
  172. * node number and try to use our address and port attributes
  173. * to connect to this node.. make sure that they've been set
  174. * before writing the node attribute? */
  175. if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
  176. !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
  177. return -EINVAL; /* XXX */
  178. write_lock(&cluster->cl_nodes_lock);
  179. if (cluster->cl_nodes[tmp])
  180. ret = -EEXIST;
  181. else if (test_and_set_bit(O2NM_NODE_ATTR_NUM,
  182. &node->nd_set_attributes))
  183. ret = -EBUSY;
  184. else {
  185. cluster->cl_nodes[tmp] = node;
  186. node->nd_num = tmp;
  187. set_bit(tmp, cluster->cl_nodes_bitmap);
  188. }
  189. write_unlock(&cluster->cl_nodes_lock);
  190. if (ret)
  191. return ret;
  192. return count;
  193. }
  194. static ssize_t o2nm_node_ipv4_port_show(struct config_item *item, char *page)
  195. {
  196. return sprintf(page, "%u\n", ntohs(to_o2nm_node(item)->nd_ipv4_port));
  197. }
  198. static ssize_t o2nm_node_ipv4_port_store(struct config_item *item,
  199. const char *page, size_t count)
  200. {
  201. struct o2nm_node *node = to_o2nm_node(item);
  202. unsigned long tmp;
  203. char *p = (char *)page;
  204. tmp = simple_strtoul(p, &p, 0);
  205. if (!p || (*p && (*p != '\n')))
  206. return -EINVAL;
  207. if (tmp == 0)
  208. return -EINVAL;
  209. if (tmp >= (u16)-1)
  210. return -ERANGE;
  211. if (test_and_set_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
  212. return -EBUSY;
  213. node->nd_ipv4_port = htons(tmp);
  214. return count;
  215. }
  216. static ssize_t o2nm_node_ipv4_address_show(struct config_item *item, char *page)
  217. {
  218. return sprintf(page, "%pI4\n", &to_o2nm_node(item)->nd_ipv4_address);
  219. }
  220. static ssize_t o2nm_node_ipv4_address_store(struct config_item *item,
  221. const char *page,
  222. size_t count)
  223. {
  224. struct o2nm_node *node = to_o2nm_node(item);
  225. struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
  226. int ret, i;
  227. struct rb_node **p, *parent;
  228. unsigned int octets[4];
  229. __be32 ipv4_addr = 0;
  230. ret = sscanf(page, "%3u.%3u.%3u.%3u", &octets[3], &octets[2],
  231. &octets[1], &octets[0]);
  232. if (ret != 4)
  233. return -EINVAL;
  234. for (i = 0; i < ARRAY_SIZE(octets); i++) {
  235. if (octets[i] > 255)
  236. return -ERANGE;
  237. be32_add_cpu(&ipv4_addr, octets[i] << (i * 8));
  238. }
  239. ret = 0;
  240. write_lock(&cluster->cl_nodes_lock);
  241. if (o2nm_node_ip_tree_lookup(cluster, ipv4_addr, &p, &parent))
  242. ret = -EEXIST;
  243. else if (test_and_set_bit(O2NM_NODE_ATTR_ADDRESS,
  244. &node->nd_set_attributes))
  245. ret = -EBUSY;
  246. else {
  247. rb_link_node(&node->nd_ip_node, parent, p);
  248. rb_insert_color(&node->nd_ip_node, &cluster->cl_node_ip_tree);
  249. }
  250. write_unlock(&cluster->cl_nodes_lock);
  251. if (ret)
  252. return ret;
  253. memcpy(&node->nd_ipv4_address, &ipv4_addr, sizeof(ipv4_addr));
  254. return count;
  255. }
  256. static ssize_t o2nm_node_local_show(struct config_item *item, char *page)
  257. {
  258. return sprintf(page, "%d\n", to_o2nm_node(item)->nd_local);
  259. }
  260. static ssize_t o2nm_node_local_store(struct config_item *item, const char *page,
  261. size_t count)
  262. {
  263. struct o2nm_node *node = to_o2nm_node(item);
  264. struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
  265. unsigned long tmp;
  266. char *p = (char *)page;
  267. ssize_t ret;
  268. tmp = simple_strtoul(p, &p, 0);
  269. if (!p || (*p && (*p != '\n')))
  270. return -EINVAL;
  271. tmp = !!tmp; /* boolean of whether this node wants to be local */
  272. /* setting local turns on networking rx for now so we require having
  273. * set everything else first */
  274. if (!test_bit(O2NM_NODE_ATTR_ADDRESS, &node->nd_set_attributes) ||
  275. !test_bit(O2NM_NODE_ATTR_NUM, &node->nd_set_attributes) ||
  276. !test_bit(O2NM_NODE_ATTR_PORT, &node->nd_set_attributes))
  277. return -EINVAL; /* XXX */
  278. /* the only failure case is trying to set a new local node
  279. * when a different one is already set */
  280. if (tmp && tmp == cluster->cl_has_local &&
  281. cluster->cl_local_node != node->nd_num)
  282. return -EBUSY;
  283. /* bring up the rx thread if we're setting the new local node. */
  284. if (tmp && !cluster->cl_has_local) {
  285. ret = o2net_start_listening(node);
  286. if (ret)
  287. return ret;
  288. }
  289. if (!tmp && cluster->cl_has_local &&
  290. cluster->cl_local_node == node->nd_num) {
  291. o2net_stop_listening(node);
  292. cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
  293. }
  294. node->nd_local = tmp;
  295. if (node->nd_local) {
  296. cluster->cl_has_local = tmp;
  297. cluster->cl_local_node = node->nd_num;
  298. }
  299. return count;
  300. }
  301. CONFIGFS_ATTR(o2nm_node_, num);
  302. CONFIGFS_ATTR(o2nm_node_, ipv4_port);
  303. CONFIGFS_ATTR(o2nm_node_, ipv4_address);
  304. CONFIGFS_ATTR(o2nm_node_, local);
  305. static struct configfs_attribute *o2nm_node_attrs[] = {
  306. &o2nm_node_attr_num,
  307. &o2nm_node_attr_ipv4_port,
  308. &o2nm_node_attr_ipv4_address,
  309. &o2nm_node_attr_local,
  310. NULL,
  311. };
  312. static struct configfs_item_operations o2nm_node_item_ops = {
  313. .release = o2nm_node_release,
  314. };
  315. static struct config_item_type o2nm_node_type = {
  316. .ct_item_ops = &o2nm_node_item_ops,
  317. .ct_attrs = o2nm_node_attrs,
  318. .ct_owner = THIS_MODULE,
  319. };
  320. /* node set */
  321. struct o2nm_node_group {
  322. struct config_group ns_group;
  323. /* some stuff? */
  324. };
  325. #if 0
  326. static struct o2nm_node_group *to_o2nm_node_group(struct config_group *group)
  327. {
  328. return group ?
  329. container_of(group, struct o2nm_node_group, ns_group)
  330. : NULL;
  331. }
  332. #endif
  333. static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
  334. unsigned int *val)
  335. {
  336. unsigned long tmp;
  337. char *p = (char *)page;
  338. tmp = simple_strtoul(p, &p, 0);
  339. if (!p || (*p && (*p != '\n')))
  340. return -EINVAL;
  341. if (tmp == 0)
  342. return -EINVAL;
  343. if (tmp >= (u32)-1)
  344. return -ERANGE;
  345. *val = tmp;
  346. return count;
  347. }
  348. static ssize_t o2nm_cluster_idle_timeout_ms_show(struct config_item *item,
  349. char *page)
  350. {
  351. return sprintf(page, "%u\n", to_o2nm_cluster(item)->cl_idle_timeout_ms);
  352. }
  353. static ssize_t o2nm_cluster_idle_timeout_ms_store(struct config_item *item,
  354. const char *page, size_t count)
  355. {
  356. struct o2nm_cluster *cluster = to_o2nm_cluster(item);
  357. ssize_t ret;
  358. unsigned int val;
  359. ret = o2nm_cluster_attr_write(page, count, &val);
  360. if (ret > 0) {
  361. if (cluster->cl_idle_timeout_ms != val
  362. && o2net_num_connected_peers()) {
  363. mlog(ML_NOTICE,
  364. "o2net: cannot change idle timeout after "
  365. "the first peer has agreed to it."
  366. " %d connected peers\n",
  367. o2net_num_connected_peers());
  368. ret = -EINVAL;
  369. } else if (val <= cluster->cl_keepalive_delay_ms) {
  370. mlog(ML_NOTICE, "o2net: idle timeout must be larger "
  371. "than keepalive delay\n");
  372. ret = -EINVAL;
  373. } else {
  374. cluster->cl_idle_timeout_ms = val;
  375. }
  376. }
  377. return ret;
  378. }
  379. static ssize_t o2nm_cluster_keepalive_delay_ms_show(
  380. struct config_item *item, char *page)
  381. {
  382. return sprintf(page, "%u\n",
  383. to_o2nm_cluster(item)->cl_keepalive_delay_ms);
  384. }
  385. static ssize_t o2nm_cluster_keepalive_delay_ms_store(
  386. struct config_item *item, const char *page, size_t count)
  387. {
  388. struct o2nm_cluster *cluster = to_o2nm_cluster(item);
  389. ssize_t ret;
  390. unsigned int val;
  391. ret = o2nm_cluster_attr_write(page, count, &val);
  392. if (ret > 0) {
  393. if (cluster->cl_keepalive_delay_ms != val
  394. && o2net_num_connected_peers()) {
  395. mlog(ML_NOTICE,
  396. "o2net: cannot change keepalive delay after"
  397. " the first peer has agreed to it."
  398. " %d connected peers\n",
  399. o2net_num_connected_peers());
  400. ret = -EINVAL;
  401. } else if (val >= cluster->cl_idle_timeout_ms) {
  402. mlog(ML_NOTICE, "o2net: keepalive delay must be "
  403. "smaller than idle timeout\n");
  404. ret = -EINVAL;
  405. } else {
  406. cluster->cl_keepalive_delay_ms = val;
  407. }
  408. }
  409. return ret;
  410. }
  411. static ssize_t o2nm_cluster_reconnect_delay_ms_show(
  412. struct config_item *item, char *page)
  413. {
  414. return sprintf(page, "%u\n",
  415. to_o2nm_cluster(item)->cl_reconnect_delay_ms);
  416. }
  417. static ssize_t o2nm_cluster_reconnect_delay_ms_store(
  418. struct config_item *item, const char *page, size_t count)
  419. {
  420. return o2nm_cluster_attr_write(page, count,
  421. &to_o2nm_cluster(item)->cl_reconnect_delay_ms);
  422. }
  423. static ssize_t o2nm_cluster_fence_method_show(
  424. struct config_item *item, char *page)
  425. {
  426. struct o2nm_cluster *cluster = to_o2nm_cluster(item);
  427. ssize_t ret = 0;
  428. if (cluster)
  429. ret = sprintf(page, "%s\n",
  430. o2nm_fence_method_desc[cluster->cl_fence_method]);
  431. return ret;
  432. }
  433. static ssize_t o2nm_cluster_fence_method_store(
  434. struct config_item *item, const char *page, size_t count)
  435. {
  436. unsigned int i;
  437. if (page[count - 1] != '\n')
  438. goto bail;
  439. for (i = 0; i < O2NM_FENCE_METHODS; ++i) {
  440. if (count != strlen(o2nm_fence_method_desc[i]) + 1)
  441. continue;
  442. if (strncasecmp(page, o2nm_fence_method_desc[i], count - 1))
  443. continue;
  444. if (to_o2nm_cluster(item)->cl_fence_method != i) {
  445. printk(KERN_INFO "ocfs2: Changing fence method to %s\n",
  446. o2nm_fence_method_desc[i]);
  447. to_o2nm_cluster(item)->cl_fence_method = i;
  448. }
  449. return count;
  450. }
  451. bail:
  452. return -EINVAL;
  453. }
  454. CONFIGFS_ATTR(o2nm_cluster_, idle_timeout_ms);
  455. CONFIGFS_ATTR(o2nm_cluster_, keepalive_delay_ms);
  456. CONFIGFS_ATTR(o2nm_cluster_, reconnect_delay_ms);
  457. CONFIGFS_ATTR(o2nm_cluster_, fence_method);
  458. static struct configfs_attribute *o2nm_cluster_attrs[] = {
  459. &o2nm_cluster_attr_idle_timeout_ms,
  460. &o2nm_cluster_attr_keepalive_delay_ms,
  461. &o2nm_cluster_attr_reconnect_delay_ms,
  462. &o2nm_cluster_attr_fence_method,
  463. NULL,
  464. };
  465. static struct config_item *o2nm_node_group_make_item(struct config_group *group,
  466. const char *name)
  467. {
  468. struct o2nm_node *node = NULL;
  469. if (strlen(name) > O2NM_MAX_NAME_LEN)
  470. return ERR_PTR(-ENAMETOOLONG);
  471. node = kzalloc(sizeof(struct o2nm_node), GFP_KERNEL);
  472. if (node == NULL)
  473. return ERR_PTR(-ENOMEM);
  474. strcpy(node->nd_name, name); /* use item.ci_namebuf instead? */
  475. config_item_init_type_name(&node->nd_item, name, &o2nm_node_type);
  476. spin_lock_init(&node->nd_lock);
  477. mlog(ML_CLUSTER, "o2nm: Registering node %s\n", name);
  478. return &node->nd_item;
  479. }
  480. static void o2nm_node_group_drop_item(struct config_group *group,
  481. struct config_item *item)
  482. {
  483. struct o2nm_node *node = to_o2nm_node(item);
  484. struct o2nm_cluster *cluster = to_o2nm_cluster(group->cg_item.ci_parent);
  485. o2net_disconnect_node(node);
  486. if (cluster->cl_has_local &&
  487. (cluster->cl_local_node == node->nd_num)) {
  488. cluster->cl_has_local = 0;
  489. cluster->cl_local_node = O2NM_INVALID_NODE_NUM;
  490. o2net_stop_listening(node);
  491. }
  492. /* XXX call into net to stop this node from trading messages */
  493. write_lock(&cluster->cl_nodes_lock);
  494. /* XXX sloppy */
  495. if (node->nd_ipv4_address)
  496. rb_erase(&node->nd_ip_node, &cluster->cl_node_ip_tree);
  497. /* nd_num might be 0 if the node number hasn't been set.. */
  498. if (cluster->cl_nodes[node->nd_num] == node) {
  499. cluster->cl_nodes[node->nd_num] = NULL;
  500. clear_bit(node->nd_num, cluster->cl_nodes_bitmap);
  501. }
  502. write_unlock(&cluster->cl_nodes_lock);
  503. mlog(ML_CLUSTER, "o2nm: Unregistered node %s\n",
  504. config_item_name(&node->nd_item));
  505. config_item_put(item);
  506. }
  507. static struct configfs_group_operations o2nm_node_group_group_ops = {
  508. .make_item = o2nm_node_group_make_item,
  509. .drop_item = o2nm_node_group_drop_item,
  510. };
  511. static struct config_item_type o2nm_node_group_type = {
  512. .ct_group_ops = &o2nm_node_group_group_ops,
  513. .ct_owner = THIS_MODULE,
  514. };
  515. /* cluster */
  516. static void o2nm_cluster_release(struct config_item *item)
  517. {
  518. struct o2nm_cluster *cluster = to_o2nm_cluster(item);
  519. kfree(cluster);
  520. }
  521. static struct configfs_item_operations o2nm_cluster_item_ops = {
  522. .release = o2nm_cluster_release,
  523. };
  524. static struct config_item_type o2nm_cluster_type = {
  525. .ct_item_ops = &o2nm_cluster_item_ops,
  526. .ct_attrs = o2nm_cluster_attrs,
  527. .ct_owner = THIS_MODULE,
  528. };
  529. /* cluster set */
  530. struct o2nm_cluster_group {
  531. struct configfs_subsystem cs_subsys;
  532. /* some stuff? */
  533. };
  534. #if 0
  535. static struct o2nm_cluster_group *to_o2nm_cluster_group(struct config_group *group)
  536. {
  537. return group ?
  538. container_of(to_configfs_subsystem(group), struct o2nm_cluster_group, cs_subsys)
  539. : NULL;
  540. }
  541. #endif
  542. static struct config_group *o2nm_cluster_group_make_group(struct config_group *group,
  543. const char *name)
  544. {
  545. struct o2nm_cluster *cluster = NULL;
  546. struct o2nm_node_group *ns = NULL;
  547. struct config_group *o2hb_group = NULL, *ret = NULL;
  548. /* this runs under the parent dir's i_mutex; there can be only
  549. * one caller in here at a time */
  550. if (o2nm_single_cluster)
  551. return ERR_PTR(-ENOSPC);
  552. cluster = kzalloc(sizeof(struct o2nm_cluster), GFP_KERNEL);
  553. ns = kzalloc(sizeof(struct o2nm_node_group), GFP_KERNEL);
  554. o2hb_group = o2hb_alloc_hb_set();
  555. if (cluster == NULL || ns == NULL || o2hb_group == NULL)
  556. goto out;
  557. config_group_init_type_name(&cluster->cl_group, name,
  558. &o2nm_cluster_type);
  559. configfs_add_default_group(&ns->ns_group, &cluster->cl_group);
  560. config_group_init_type_name(&ns->ns_group, "node",
  561. &o2nm_node_group_type);
  562. configfs_add_default_group(o2hb_group, &cluster->cl_group);
  563. rwlock_init(&cluster->cl_nodes_lock);
  564. cluster->cl_node_ip_tree = RB_ROOT;
  565. cluster->cl_reconnect_delay_ms = O2NET_RECONNECT_DELAY_MS_DEFAULT;
  566. cluster->cl_idle_timeout_ms = O2NET_IDLE_TIMEOUT_MS_DEFAULT;
  567. cluster->cl_keepalive_delay_ms = O2NET_KEEPALIVE_DELAY_MS_DEFAULT;
  568. cluster->cl_fence_method = O2NM_FENCE_RESET;
  569. ret = &cluster->cl_group;
  570. o2nm_single_cluster = cluster;
  571. out:
  572. if (ret == NULL) {
  573. kfree(cluster);
  574. kfree(ns);
  575. o2hb_free_hb_set(o2hb_group);
  576. ret = ERR_PTR(-ENOMEM);
  577. }
  578. return ret;
  579. }
  580. static void o2nm_cluster_group_drop_item(struct config_group *group, struct config_item *item)
  581. {
  582. struct o2nm_cluster *cluster = to_o2nm_cluster(item);
  583. BUG_ON(o2nm_single_cluster != cluster);
  584. o2nm_single_cluster = NULL;
  585. configfs_remove_default_groups(&cluster->cl_group);
  586. config_item_put(item);
  587. }
  588. static struct configfs_group_operations o2nm_cluster_group_group_ops = {
  589. .make_group = o2nm_cluster_group_make_group,
  590. .drop_item = o2nm_cluster_group_drop_item,
  591. };
  592. static struct config_item_type o2nm_cluster_group_type = {
  593. .ct_group_ops = &o2nm_cluster_group_group_ops,
  594. .ct_owner = THIS_MODULE,
  595. };
  596. static struct o2nm_cluster_group o2nm_cluster_group = {
  597. .cs_subsys = {
  598. .su_group = {
  599. .cg_item = {
  600. .ci_namebuf = "cluster",
  601. .ci_type = &o2nm_cluster_group_type,
  602. },
  603. },
  604. },
  605. };
  606. int o2nm_depend_item(struct config_item *item)
  607. {
  608. return configfs_depend_item(&o2nm_cluster_group.cs_subsys, item);
  609. }
  610. void o2nm_undepend_item(struct config_item *item)
  611. {
  612. configfs_undepend_item(item);
  613. }
  614. int o2nm_depend_this_node(void)
  615. {
  616. int ret = 0;
  617. struct o2nm_node *local_node;
  618. local_node = o2nm_get_node_by_num(o2nm_this_node());
  619. if (!local_node) {
  620. ret = -EINVAL;
  621. goto out;
  622. }
  623. ret = o2nm_depend_item(&local_node->nd_item);
  624. o2nm_node_put(local_node);
  625. out:
  626. return ret;
  627. }
  628. void o2nm_undepend_this_node(void)
  629. {
  630. struct o2nm_node *local_node;
  631. local_node = o2nm_get_node_by_num(o2nm_this_node());
  632. BUG_ON(!local_node);
  633. o2nm_undepend_item(&local_node->nd_item);
  634. o2nm_node_put(local_node);
  635. }
  636. static void __exit exit_o2nm(void)
  637. {
  638. /* XXX sync with hb callbacks and shut down hb? */
  639. o2net_unregister_hb_callbacks();
  640. configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
  641. o2cb_sys_shutdown();
  642. o2net_exit();
  643. o2hb_exit();
  644. }
  645. static int __init init_o2nm(void)
  646. {
  647. int ret = -1;
  648. ret = o2hb_init();
  649. if (ret)
  650. goto out;
  651. ret = o2net_init();
  652. if (ret)
  653. goto out_o2hb;
  654. ret = o2net_register_hb_callbacks();
  655. if (ret)
  656. goto out_o2net;
  657. config_group_init(&o2nm_cluster_group.cs_subsys.su_group);
  658. mutex_init(&o2nm_cluster_group.cs_subsys.su_mutex);
  659. ret = configfs_register_subsystem(&o2nm_cluster_group.cs_subsys);
  660. if (ret) {
  661. printk(KERN_ERR "nodemanager: Registration returned %d\n", ret);
  662. goto out_callbacks;
  663. }
  664. ret = o2cb_sys_init();
  665. if (!ret)
  666. goto out;
  667. configfs_unregister_subsystem(&o2nm_cluster_group.cs_subsys);
  668. out_callbacks:
  669. o2net_unregister_hb_callbacks();
  670. out_o2net:
  671. o2net_exit();
  672. out_o2hb:
  673. o2hb_exit();
  674. out:
  675. return ret;
  676. }
  677. MODULE_AUTHOR("Oracle");
  678. MODULE_LICENSE("GPL");
  679. MODULE_DESCRIPTION("OCFS2 cluster management");
  680. module_init(init_o2nm)
  681. module_exit(exit_o2nm)