nodemanager.c 25 KB

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