config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/configfs.h>
  16. #include <linux/slab.h>
  17. #include <linux/in.h>
  18. #include <linux/in6.h>
  19. #include <linux/dlmconstants.h>
  20. #include <net/ipv6.h>
  21. #include <net/sock.h>
  22. #include "config.h"
  23. #include "lowcomms.h"
  24. /*
  25. * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
  26. * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
  27. * /config/dlm/<cluster>/comms/<comm>/nodeid
  28. * /config/dlm/<cluster>/comms/<comm>/local
  29. * /config/dlm/<cluster>/comms/<comm>/addr (write only)
  30. * /config/dlm/<cluster>/comms/<comm>/addr_list (read only)
  31. * The <cluster> level is useless, but I haven't figured out how to avoid it.
  32. */
  33. static struct config_group *space_list;
  34. static struct config_group *comm_list;
  35. static struct dlm_comm *local_comm;
  36. static uint32_t dlm_comm_count;
  37. struct dlm_clusters;
  38. struct dlm_cluster;
  39. struct dlm_spaces;
  40. struct dlm_space;
  41. struct dlm_comms;
  42. struct dlm_comm;
  43. struct dlm_nodes;
  44. struct dlm_node;
  45. static struct config_group *make_cluster(struct config_group *, const char *);
  46. static void drop_cluster(struct config_group *, struct config_item *);
  47. static void release_cluster(struct config_item *);
  48. static struct config_group *make_space(struct config_group *, const char *);
  49. static void drop_space(struct config_group *, struct config_item *);
  50. static void release_space(struct config_item *);
  51. static struct config_item *make_comm(struct config_group *, const char *);
  52. static void drop_comm(struct config_group *, struct config_item *);
  53. static void release_comm(struct config_item *);
  54. static struct config_item *make_node(struct config_group *, const char *);
  55. static void drop_node(struct config_group *, struct config_item *);
  56. static void release_node(struct config_item *);
  57. static struct configfs_attribute *comm_attrs[];
  58. static struct configfs_attribute *node_attrs[];
  59. struct dlm_cluster {
  60. struct config_group group;
  61. unsigned int cl_tcp_port;
  62. unsigned int cl_buffer_size;
  63. unsigned int cl_rsbtbl_size;
  64. unsigned int cl_recover_timer;
  65. unsigned int cl_toss_secs;
  66. unsigned int cl_scan_secs;
  67. unsigned int cl_log_debug;
  68. unsigned int cl_log_info;
  69. unsigned int cl_protocol;
  70. unsigned int cl_timewarn_cs;
  71. unsigned int cl_waitwarn_us;
  72. unsigned int cl_new_rsb_count;
  73. unsigned int cl_recover_callbacks;
  74. char cl_cluster_name[DLM_LOCKSPACE_LEN];
  75. };
  76. static struct dlm_cluster *config_item_to_cluster(struct config_item *i)
  77. {
  78. return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
  79. NULL;
  80. }
  81. enum {
  82. CLUSTER_ATTR_TCP_PORT = 0,
  83. CLUSTER_ATTR_BUFFER_SIZE,
  84. CLUSTER_ATTR_RSBTBL_SIZE,
  85. CLUSTER_ATTR_RECOVER_TIMER,
  86. CLUSTER_ATTR_TOSS_SECS,
  87. CLUSTER_ATTR_SCAN_SECS,
  88. CLUSTER_ATTR_LOG_DEBUG,
  89. CLUSTER_ATTR_LOG_INFO,
  90. CLUSTER_ATTR_PROTOCOL,
  91. CLUSTER_ATTR_TIMEWARN_CS,
  92. CLUSTER_ATTR_WAITWARN_US,
  93. CLUSTER_ATTR_NEW_RSB_COUNT,
  94. CLUSTER_ATTR_RECOVER_CALLBACKS,
  95. CLUSTER_ATTR_CLUSTER_NAME,
  96. };
  97. static ssize_t cluster_cluster_name_show(struct config_item *item, char *buf)
  98. {
  99. struct dlm_cluster *cl = config_item_to_cluster(item);
  100. return sprintf(buf, "%s\n", cl->cl_cluster_name);
  101. }
  102. static ssize_t cluster_cluster_name_store(struct config_item *item,
  103. const char *buf, size_t len)
  104. {
  105. struct dlm_cluster *cl = config_item_to_cluster(item);
  106. strlcpy(dlm_config.ci_cluster_name, buf,
  107. sizeof(dlm_config.ci_cluster_name));
  108. strlcpy(cl->cl_cluster_name, buf, sizeof(cl->cl_cluster_name));
  109. return len;
  110. }
  111. CONFIGFS_ATTR(cluster_, cluster_name);
  112. static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
  113. int *info_field, int check_zero,
  114. const char *buf, size_t len)
  115. {
  116. unsigned int x;
  117. int rc;
  118. if (!capable(CAP_SYS_ADMIN))
  119. return -EPERM;
  120. rc = kstrtouint(buf, 0, &x);
  121. if (rc)
  122. return rc;
  123. if (check_zero && !x)
  124. return -EINVAL;
  125. *cl_field = x;
  126. *info_field = x;
  127. return len;
  128. }
  129. #define CLUSTER_ATTR(name, check_zero) \
  130. static ssize_t cluster_##name##_store(struct config_item *item, \
  131. const char *buf, size_t len) \
  132. { \
  133. struct dlm_cluster *cl = config_item_to_cluster(item); \
  134. return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
  135. check_zero, buf, len); \
  136. } \
  137. static ssize_t cluster_##name##_show(struct config_item *item, char *buf) \
  138. { \
  139. struct dlm_cluster *cl = config_item_to_cluster(item); \
  140. return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \
  141. } \
  142. CONFIGFS_ATTR(cluster_, name);
  143. CLUSTER_ATTR(tcp_port, 1);
  144. CLUSTER_ATTR(buffer_size, 1);
  145. CLUSTER_ATTR(rsbtbl_size, 1);
  146. CLUSTER_ATTR(recover_timer, 1);
  147. CLUSTER_ATTR(toss_secs, 1);
  148. CLUSTER_ATTR(scan_secs, 1);
  149. CLUSTER_ATTR(log_debug, 0);
  150. CLUSTER_ATTR(log_info, 0);
  151. CLUSTER_ATTR(protocol, 0);
  152. CLUSTER_ATTR(timewarn_cs, 1);
  153. CLUSTER_ATTR(waitwarn_us, 0);
  154. CLUSTER_ATTR(new_rsb_count, 0);
  155. CLUSTER_ATTR(recover_callbacks, 0);
  156. static struct configfs_attribute *cluster_attrs[] = {
  157. [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port,
  158. [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size,
  159. [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size,
  160. [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer,
  161. [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs,
  162. [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs,
  163. [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug,
  164. [CLUSTER_ATTR_LOG_INFO] = &cluster_attr_log_info,
  165. [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol,
  166. [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs,
  167. [CLUSTER_ATTR_WAITWARN_US] = &cluster_attr_waitwarn_us,
  168. [CLUSTER_ATTR_NEW_RSB_COUNT] = &cluster_attr_new_rsb_count,
  169. [CLUSTER_ATTR_RECOVER_CALLBACKS] = &cluster_attr_recover_callbacks,
  170. [CLUSTER_ATTR_CLUSTER_NAME] = &cluster_attr_cluster_name,
  171. NULL,
  172. };
  173. enum {
  174. COMM_ATTR_NODEID = 0,
  175. COMM_ATTR_LOCAL,
  176. COMM_ATTR_ADDR,
  177. COMM_ATTR_ADDR_LIST,
  178. };
  179. enum {
  180. NODE_ATTR_NODEID = 0,
  181. NODE_ATTR_WEIGHT,
  182. };
  183. struct dlm_clusters {
  184. struct configfs_subsystem subsys;
  185. };
  186. struct dlm_spaces {
  187. struct config_group ss_group;
  188. };
  189. struct dlm_space {
  190. struct config_group group;
  191. struct list_head members;
  192. struct mutex members_lock;
  193. int members_count;
  194. struct dlm_nodes *nds;
  195. };
  196. struct dlm_comms {
  197. struct config_group cs_group;
  198. };
  199. struct dlm_comm {
  200. struct config_item item;
  201. int seq;
  202. int nodeid;
  203. int local;
  204. int addr_count;
  205. struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
  206. };
  207. struct dlm_nodes {
  208. struct config_group ns_group;
  209. };
  210. struct dlm_node {
  211. struct config_item item;
  212. struct list_head list; /* space->members */
  213. int nodeid;
  214. int weight;
  215. int new;
  216. int comm_seq; /* copy of cm->seq when nd->nodeid is set */
  217. };
  218. static struct configfs_group_operations clusters_ops = {
  219. .make_group = make_cluster,
  220. .drop_item = drop_cluster,
  221. };
  222. static struct configfs_item_operations cluster_ops = {
  223. .release = release_cluster,
  224. };
  225. static struct configfs_group_operations spaces_ops = {
  226. .make_group = make_space,
  227. .drop_item = drop_space,
  228. };
  229. static struct configfs_item_operations space_ops = {
  230. .release = release_space,
  231. };
  232. static struct configfs_group_operations comms_ops = {
  233. .make_item = make_comm,
  234. .drop_item = drop_comm,
  235. };
  236. static struct configfs_item_operations comm_ops = {
  237. .release = release_comm,
  238. };
  239. static struct configfs_group_operations nodes_ops = {
  240. .make_item = make_node,
  241. .drop_item = drop_node,
  242. };
  243. static struct configfs_item_operations node_ops = {
  244. .release = release_node,
  245. };
  246. static const struct config_item_type clusters_type = {
  247. .ct_group_ops = &clusters_ops,
  248. .ct_owner = THIS_MODULE,
  249. };
  250. static const struct config_item_type cluster_type = {
  251. .ct_item_ops = &cluster_ops,
  252. .ct_attrs = cluster_attrs,
  253. .ct_owner = THIS_MODULE,
  254. };
  255. static const struct config_item_type spaces_type = {
  256. .ct_group_ops = &spaces_ops,
  257. .ct_owner = THIS_MODULE,
  258. };
  259. static const struct config_item_type space_type = {
  260. .ct_item_ops = &space_ops,
  261. .ct_owner = THIS_MODULE,
  262. };
  263. static const struct config_item_type comms_type = {
  264. .ct_group_ops = &comms_ops,
  265. .ct_owner = THIS_MODULE,
  266. };
  267. static const struct config_item_type comm_type = {
  268. .ct_item_ops = &comm_ops,
  269. .ct_attrs = comm_attrs,
  270. .ct_owner = THIS_MODULE,
  271. };
  272. static const struct config_item_type nodes_type = {
  273. .ct_group_ops = &nodes_ops,
  274. .ct_owner = THIS_MODULE,
  275. };
  276. static const struct config_item_type node_type = {
  277. .ct_item_ops = &node_ops,
  278. .ct_attrs = node_attrs,
  279. .ct_owner = THIS_MODULE,
  280. };
  281. static struct dlm_space *config_item_to_space(struct config_item *i)
  282. {
  283. return i ? container_of(to_config_group(i), struct dlm_space, group) :
  284. NULL;
  285. }
  286. static struct dlm_comm *config_item_to_comm(struct config_item *i)
  287. {
  288. return i ? container_of(i, struct dlm_comm, item) : NULL;
  289. }
  290. static struct dlm_node *config_item_to_node(struct config_item *i)
  291. {
  292. return i ? container_of(i, struct dlm_node, item) : NULL;
  293. }
  294. static struct config_group *make_cluster(struct config_group *g,
  295. const char *name)
  296. {
  297. struct dlm_cluster *cl = NULL;
  298. struct dlm_spaces *sps = NULL;
  299. struct dlm_comms *cms = NULL;
  300. cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
  301. sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
  302. cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
  303. if (!cl || !sps || !cms)
  304. goto fail;
  305. config_group_init_type_name(&cl->group, name, &cluster_type);
  306. config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
  307. config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
  308. configfs_add_default_group(&sps->ss_group, &cl->group);
  309. configfs_add_default_group(&cms->cs_group, &cl->group);
  310. cl->cl_tcp_port = dlm_config.ci_tcp_port;
  311. cl->cl_buffer_size = dlm_config.ci_buffer_size;
  312. cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
  313. cl->cl_recover_timer = dlm_config.ci_recover_timer;
  314. cl->cl_toss_secs = dlm_config.ci_toss_secs;
  315. cl->cl_scan_secs = dlm_config.ci_scan_secs;
  316. cl->cl_log_debug = dlm_config.ci_log_debug;
  317. cl->cl_log_info = dlm_config.ci_log_info;
  318. cl->cl_protocol = dlm_config.ci_protocol;
  319. cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
  320. cl->cl_waitwarn_us = dlm_config.ci_waitwarn_us;
  321. cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
  322. cl->cl_recover_callbacks = dlm_config.ci_recover_callbacks;
  323. memcpy(cl->cl_cluster_name, dlm_config.ci_cluster_name,
  324. DLM_LOCKSPACE_LEN);
  325. space_list = &sps->ss_group;
  326. comm_list = &cms->cs_group;
  327. return &cl->group;
  328. fail:
  329. kfree(cl);
  330. kfree(sps);
  331. kfree(cms);
  332. return ERR_PTR(-ENOMEM);
  333. }
  334. static void drop_cluster(struct config_group *g, struct config_item *i)
  335. {
  336. struct dlm_cluster *cl = config_item_to_cluster(i);
  337. configfs_remove_default_groups(&cl->group);
  338. space_list = NULL;
  339. comm_list = NULL;
  340. config_item_put(i);
  341. }
  342. static void release_cluster(struct config_item *i)
  343. {
  344. struct dlm_cluster *cl = config_item_to_cluster(i);
  345. kfree(cl);
  346. }
  347. static struct config_group *make_space(struct config_group *g, const char *name)
  348. {
  349. struct dlm_space *sp = NULL;
  350. struct dlm_nodes *nds = NULL;
  351. sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
  352. nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
  353. if (!sp || !nds)
  354. goto fail;
  355. config_group_init_type_name(&sp->group, name, &space_type);
  356. config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
  357. configfs_add_default_group(&nds->ns_group, &sp->group);
  358. INIT_LIST_HEAD(&sp->members);
  359. mutex_init(&sp->members_lock);
  360. sp->members_count = 0;
  361. sp->nds = nds;
  362. return &sp->group;
  363. fail:
  364. kfree(sp);
  365. kfree(nds);
  366. return ERR_PTR(-ENOMEM);
  367. }
  368. static void drop_space(struct config_group *g, struct config_item *i)
  369. {
  370. struct dlm_space *sp = config_item_to_space(i);
  371. /* assert list_empty(&sp->members) */
  372. configfs_remove_default_groups(&sp->group);
  373. config_item_put(i);
  374. }
  375. static void release_space(struct config_item *i)
  376. {
  377. struct dlm_space *sp = config_item_to_space(i);
  378. kfree(sp->nds);
  379. kfree(sp);
  380. }
  381. static struct config_item *make_comm(struct config_group *g, const char *name)
  382. {
  383. struct dlm_comm *cm;
  384. cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
  385. if (!cm)
  386. return ERR_PTR(-ENOMEM);
  387. config_item_init_type_name(&cm->item, name, &comm_type);
  388. cm->seq = dlm_comm_count++;
  389. if (!cm->seq)
  390. cm->seq = dlm_comm_count++;
  391. cm->nodeid = -1;
  392. cm->local = 0;
  393. cm->addr_count = 0;
  394. return &cm->item;
  395. }
  396. static void drop_comm(struct config_group *g, struct config_item *i)
  397. {
  398. struct dlm_comm *cm = config_item_to_comm(i);
  399. if (local_comm == cm)
  400. local_comm = NULL;
  401. dlm_lowcomms_close(cm->nodeid);
  402. while (cm->addr_count--)
  403. kfree(cm->addr[cm->addr_count]);
  404. config_item_put(i);
  405. }
  406. static void release_comm(struct config_item *i)
  407. {
  408. struct dlm_comm *cm = config_item_to_comm(i);
  409. kfree(cm);
  410. }
  411. static struct config_item *make_node(struct config_group *g, const char *name)
  412. {
  413. struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
  414. struct dlm_node *nd;
  415. nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
  416. if (!nd)
  417. return ERR_PTR(-ENOMEM);
  418. config_item_init_type_name(&nd->item, name, &node_type);
  419. nd->nodeid = -1;
  420. nd->weight = 1; /* default weight of 1 if none is set */
  421. nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
  422. mutex_lock(&sp->members_lock);
  423. list_add(&nd->list, &sp->members);
  424. sp->members_count++;
  425. mutex_unlock(&sp->members_lock);
  426. return &nd->item;
  427. }
  428. static void drop_node(struct config_group *g, struct config_item *i)
  429. {
  430. struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
  431. struct dlm_node *nd = config_item_to_node(i);
  432. mutex_lock(&sp->members_lock);
  433. list_del(&nd->list);
  434. sp->members_count--;
  435. mutex_unlock(&sp->members_lock);
  436. config_item_put(i);
  437. }
  438. static void release_node(struct config_item *i)
  439. {
  440. struct dlm_node *nd = config_item_to_node(i);
  441. kfree(nd);
  442. }
  443. static struct dlm_clusters clusters_root = {
  444. .subsys = {
  445. .su_group = {
  446. .cg_item = {
  447. .ci_namebuf = "dlm",
  448. .ci_type = &clusters_type,
  449. },
  450. },
  451. },
  452. };
  453. int __init dlm_config_init(void)
  454. {
  455. config_group_init(&clusters_root.subsys.su_group);
  456. mutex_init(&clusters_root.subsys.su_mutex);
  457. return configfs_register_subsystem(&clusters_root.subsys);
  458. }
  459. void dlm_config_exit(void)
  460. {
  461. configfs_unregister_subsystem(&clusters_root.subsys);
  462. }
  463. /*
  464. * Functions for user space to read/write attributes
  465. */
  466. static ssize_t comm_nodeid_show(struct config_item *item, char *buf)
  467. {
  468. return sprintf(buf, "%d\n", config_item_to_comm(item)->nodeid);
  469. }
  470. static ssize_t comm_nodeid_store(struct config_item *item, const char *buf,
  471. size_t len)
  472. {
  473. int rc = kstrtoint(buf, 0, &config_item_to_comm(item)->nodeid);
  474. if (rc)
  475. return rc;
  476. return len;
  477. }
  478. static ssize_t comm_local_show(struct config_item *item, char *buf)
  479. {
  480. return sprintf(buf, "%d\n", config_item_to_comm(item)->local);
  481. }
  482. static ssize_t comm_local_store(struct config_item *item, const char *buf,
  483. size_t len)
  484. {
  485. struct dlm_comm *cm = config_item_to_comm(item);
  486. int rc = kstrtoint(buf, 0, &cm->local);
  487. if (rc)
  488. return rc;
  489. if (cm->local && !local_comm)
  490. local_comm = cm;
  491. return len;
  492. }
  493. static ssize_t comm_addr_store(struct config_item *item, const char *buf,
  494. size_t len)
  495. {
  496. struct dlm_comm *cm = config_item_to_comm(item);
  497. struct sockaddr_storage *addr;
  498. int rv;
  499. if (len != sizeof(struct sockaddr_storage))
  500. return -EINVAL;
  501. if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
  502. return -ENOSPC;
  503. addr = kzalloc(sizeof(*addr), GFP_NOFS);
  504. if (!addr)
  505. return -ENOMEM;
  506. memcpy(addr, buf, len);
  507. rv = dlm_lowcomms_addr(cm->nodeid, addr, len);
  508. if (rv) {
  509. kfree(addr);
  510. return rv;
  511. }
  512. cm->addr[cm->addr_count++] = addr;
  513. return len;
  514. }
  515. static ssize_t comm_addr_list_show(struct config_item *item, char *buf)
  516. {
  517. struct dlm_comm *cm = config_item_to_comm(item);
  518. ssize_t s;
  519. ssize_t allowance;
  520. int i;
  521. struct sockaddr_storage *addr;
  522. struct sockaddr_in *addr_in;
  523. struct sockaddr_in6 *addr_in6;
  524. /* Taken from ip6_addr_string() defined in lib/vsprintf.c */
  525. char buf0[sizeof("AF_INET6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
  526. /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
  527. allowance = 4096;
  528. buf[0] = '\0';
  529. for (i = 0; i < cm->addr_count; i++) {
  530. addr = cm->addr[i];
  531. switch(addr->ss_family) {
  532. case AF_INET:
  533. addr_in = (struct sockaddr_in *)addr;
  534. s = sprintf(buf0, "AF_INET %pI4\n", &addr_in->sin_addr.s_addr);
  535. break;
  536. case AF_INET6:
  537. addr_in6 = (struct sockaddr_in6 *)addr;
  538. s = sprintf(buf0, "AF_INET6 %pI6\n", &addr_in6->sin6_addr);
  539. break;
  540. default:
  541. s = sprintf(buf0, "%s\n", "<UNKNOWN>");
  542. break;
  543. }
  544. allowance -= s;
  545. if (allowance >= 0)
  546. strcat(buf, buf0);
  547. else {
  548. allowance += s;
  549. break;
  550. }
  551. }
  552. return 4096 - allowance;
  553. }
  554. CONFIGFS_ATTR(comm_, nodeid);
  555. CONFIGFS_ATTR(comm_, local);
  556. CONFIGFS_ATTR_WO(comm_, addr);
  557. CONFIGFS_ATTR_RO(comm_, addr_list);
  558. static struct configfs_attribute *comm_attrs[] = {
  559. [COMM_ATTR_NODEID] = &comm_attr_nodeid,
  560. [COMM_ATTR_LOCAL] = &comm_attr_local,
  561. [COMM_ATTR_ADDR] = &comm_attr_addr,
  562. [COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list,
  563. NULL,
  564. };
  565. static ssize_t node_nodeid_show(struct config_item *item, char *buf)
  566. {
  567. return sprintf(buf, "%d\n", config_item_to_node(item)->nodeid);
  568. }
  569. static ssize_t node_nodeid_store(struct config_item *item, const char *buf,
  570. size_t len)
  571. {
  572. struct dlm_node *nd = config_item_to_node(item);
  573. uint32_t seq = 0;
  574. int rc = kstrtoint(buf, 0, &nd->nodeid);
  575. if (rc)
  576. return rc;
  577. dlm_comm_seq(nd->nodeid, &seq);
  578. nd->comm_seq = seq;
  579. return len;
  580. }
  581. static ssize_t node_weight_show(struct config_item *item, char *buf)
  582. {
  583. return sprintf(buf, "%d\n", config_item_to_node(item)->weight);
  584. }
  585. static ssize_t node_weight_store(struct config_item *item, const char *buf,
  586. size_t len)
  587. {
  588. int rc = kstrtoint(buf, 0, &config_item_to_node(item)->weight);
  589. if (rc)
  590. return rc;
  591. return len;
  592. }
  593. CONFIGFS_ATTR(node_, nodeid);
  594. CONFIGFS_ATTR(node_, weight);
  595. static struct configfs_attribute *node_attrs[] = {
  596. [NODE_ATTR_NODEID] = &node_attr_nodeid,
  597. [NODE_ATTR_WEIGHT] = &node_attr_weight,
  598. NULL,
  599. };
  600. /*
  601. * Functions for the dlm to get the info that's been configured
  602. */
  603. static struct dlm_space *get_space(char *name)
  604. {
  605. struct config_item *i;
  606. if (!space_list)
  607. return NULL;
  608. mutex_lock(&space_list->cg_subsys->su_mutex);
  609. i = config_group_find_item(space_list, name);
  610. mutex_unlock(&space_list->cg_subsys->su_mutex);
  611. return config_item_to_space(i);
  612. }
  613. static void put_space(struct dlm_space *sp)
  614. {
  615. config_item_put(&sp->group.cg_item);
  616. }
  617. static struct dlm_comm *get_comm(int nodeid)
  618. {
  619. struct config_item *i;
  620. struct dlm_comm *cm = NULL;
  621. int found = 0;
  622. if (!comm_list)
  623. return NULL;
  624. mutex_lock(&clusters_root.subsys.su_mutex);
  625. list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
  626. cm = config_item_to_comm(i);
  627. if (cm->nodeid != nodeid)
  628. continue;
  629. found = 1;
  630. config_item_get(i);
  631. break;
  632. }
  633. mutex_unlock(&clusters_root.subsys.su_mutex);
  634. if (!found)
  635. cm = NULL;
  636. return cm;
  637. }
  638. static void put_comm(struct dlm_comm *cm)
  639. {
  640. config_item_put(&cm->item);
  641. }
  642. /* caller must free mem */
  643. int dlm_config_nodes(char *lsname, struct dlm_config_node **nodes_out,
  644. int *count_out)
  645. {
  646. struct dlm_space *sp;
  647. struct dlm_node *nd;
  648. struct dlm_config_node *nodes, *node;
  649. int rv, count;
  650. sp = get_space(lsname);
  651. if (!sp)
  652. return -EEXIST;
  653. mutex_lock(&sp->members_lock);
  654. if (!sp->members_count) {
  655. rv = -EINVAL;
  656. printk(KERN_ERR "dlm: zero members_count\n");
  657. goto out;
  658. }
  659. count = sp->members_count;
  660. nodes = kcalloc(count, sizeof(struct dlm_config_node), GFP_NOFS);
  661. if (!nodes) {
  662. rv = -ENOMEM;
  663. goto out;
  664. }
  665. node = nodes;
  666. list_for_each_entry(nd, &sp->members, list) {
  667. node->nodeid = nd->nodeid;
  668. node->weight = nd->weight;
  669. node->new = nd->new;
  670. node->comm_seq = nd->comm_seq;
  671. node++;
  672. nd->new = 0;
  673. }
  674. *count_out = count;
  675. *nodes_out = nodes;
  676. rv = 0;
  677. out:
  678. mutex_unlock(&sp->members_lock);
  679. put_space(sp);
  680. return rv;
  681. }
  682. int dlm_comm_seq(int nodeid, uint32_t *seq)
  683. {
  684. struct dlm_comm *cm = get_comm(nodeid);
  685. if (!cm)
  686. return -EEXIST;
  687. *seq = cm->seq;
  688. put_comm(cm);
  689. return 0;
  690. }
  691. int dlm_our_nodeid(void)
  692. {
  693. return local_comm ? local_comm->nodeid : 0;
  694. }
  695. /* num 0 is first addr, num 1 is second addr */
  696. int dlm_our_addr(struct sockaddr_storage *addr, int num)
  697. {
  698. if (!local_comm)
  699. return -1;
  700. if (num + 1 > local_comm->addr_count)
  701. return -1;
  702. memcpy(addr, local_comm->addr[num], sizeof(*addr));
  703. return 0;
  704. }
  705. /* Config file defaults */
  706. #define DEFAULT_TCP_PORT 21064
  707. #define DEFAULT_BUFFER_SIZE 4096
  708. #define DEFAULT_RSBTBL_SIZE 1024
  709. #define DEFAULT_RECOVER_TIMER 5
  710. #define DEFAULT_TOSS_SECS 10
  711. #define DEFAULT_SCAN_SECS 5
  712. #define DEFAULT_LOG_DEBUG 0
  713. #define DEFAULT_LOG_INFO 1
  714. #define DEFAULT_PROTOCOL 0
  715. #define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
  716. #define DEFAULT_WAITWARN_US 0
  717. #define DEFAULT_NEW_RSB_COUNT 128
  718. #define DEFAULT_RECOVER_CALLBACKS 0
  719. #define DEFAULT_CLUSTER_NAME ""
  720. struct dlm_config_info dlm_config = {
  721. .ci_tcp_port = DEFAULT_TCP_PORT,
  722. .ci_buffer_size = DEFAULT_BUFFER_SIZE,
  723. .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
  724. .ci_recover_timer = DEFAULT_RECOVER_TIMER,
  725. .ci_toss_secs = DEFAULT_TOSS_SECS,
  726. .ci_scan_secs = DEFAULT_SCAN_SECS,
  727. .ci_log_debug = DEFAULT_LOG_DEBUG,
  728. .ci_log_info = DEFAULT_LOG_INFO,
  729. .ci_protocol = DEFAULT_PROTOCOL,
  730. .ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
  731. .ci_waitwarn_us = DEFAULT_WAITWARN_US,
  732. .ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT,
  733. .ci_recover_callbacks = DEFAULT_RECOVER_CALLBACKS,
  734. .ci_cluster_name = DEFAULT_CLUSTER_NAME
  735. };