nodemanager.c 25 KB

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