nodemanager.c 24 KB

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