nodemanager.c 25 KB

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