config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. };
  195. struct dlm_comms {
  196. struct config_group cs_group;
  197. };
  198. struct dlm_comm {
  199. struct config_item item;
  200. int seq;
  201. int nodeid;
  202. int local;
  203. int addr_count;
  204. struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
  205. };
  206. struct dlm_nodes {
  207. struct config_group ns_group;
  208. };
  209. struct dlm_node {
  210. struct config_item item;
  211. struct list_head list; /* space->members */
  212. int nodeid;
  213. int weight;
  214. int new;
  215. int comm_seq; /* copy of cm->seq when nd->nodeid is set */
  216. };
  217. static struct configfs_group_operations clusters_ops = {
  218. .make_group = make_cluster,
  219. .drop_item = drop_cluster,
  220. };
  221. static struct configfs_item_operations cluster_ops = {
  222. .release = release_cluster,
  223. };
  224. static struct configfs_group_operations spaces_ops = {
  225. .make_group = make_space,
  226. .drop_item = drop_space,
  227. };
  228. static struct configfs_item_operations space_ops = {
  229. .release = release_space,
  230. };
  231. static struct configfs_group_operations comms_ops = {
  232. .make_item = make_comm,
  233. .drop_item = drop_comm,
  234. };
  235. static struct configfs_item_operations comm_ops = {
  236. .release = release_comm,
  237. };
  238. static struct configfs_group_operations nodes_ops = {
  239. .make_item = make_node,
  240. .drop_item = drop_node,
  241. };
  242. static struct configfs_item_operations node_ops = {
  243. .release = release_node,
  244. };
  245. static const struct config_item_type clusters_type = {
  246. .ct_group_ops = &clusters_ops,
  247. .ct_owner = THIS_MODULE,
  248. };
  249. static const struct config_item_type cluster_type = {
  250. .ct_item_ops = &cluster_ops,
  251. .ct_attrs = cluster_attrs,
  252. .ct_owner = THIS_MODULE,
  253. };
  254. static const struct config_item_type spaces_type = {
  255. .ct_group_ops = &spaces_ops,
  256. .ct_owner = THIS_MODULE,
  257. };
  258. static const struct config_item_type space_type = {
  259. .ct_item_ops = &space_ops,
  260. .ct_owner = THIS_MODULE,
  261. };
  262. static const struct config_item_type comms_type = {
  263. .ct_group_ops = &comms_ops,
  264. .ct_owner = THIS_MODULE,
  265. };
  266. static const struct config_item_type comm_type = {
  267. .ct_item_ops = &comm_ops,
  268. .ct_attrs = comm_attrs,
  269. .ct_owner = THIS_MODULE,
  270. };
  271. static const struct config_item_type nodes_type = {
  272. .ct_group_ops = &nodes_ops,
  273. .ct_owner = THIS_MODULE,
  274. };
  275. static const struct config_item_type node_type = {
  276. .ct_item_ops = &node_ops,
  277. .ct_attrs = node_attrs,
  278. .ct_owner = THIS_MODULE,
  279. };
  280. static struct dlm_space *config_item_to_space(struct config_item *i)
  281. {
  282. return i ? container_of(to_config_group(i), struct dlm_space, group) :
  283. NULL;
  284. }
  285. static struct dlm_comm *config_item_to_comm(struct config_item *i)
  286. {
  287. return i ? container_of(i, struct dlm_comm, item) : NULL;
  288. }
  289. static struct dlm_node *config_item_to_node(struct config_item *i)
  290. {
  291. return i ? container_of(i, struct dlm_node, item) : NULL;
  292. }
  293. static struct config_group *make_cluster(struct config_group *g,
  294. const char *name)
  295. {
  296. struct dlm_cluster *cl = NULL;
  297. struct dlm_spaces *sps = NULL;
  298. struct dlm_comms *cms = NULL;
  299. cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
  300. sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
  301. cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
  302. if (!cl || !sps || !cms)
  303. goto fail;
  304. config_group_init_type_name(&cl->group, name, &cluster_type);
  305. config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
  306. config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
  307. configfs_add_default_group(&sps->ss_group, &cl->group);
  308. configfs_add_default_group(&cms->cs_group, &cl->group);
  309. cl->cl_tcp_port = dlm_config.ci_tcp_port;
  310. cl->cl_buffer_size = dlm_config.ci_buffer_size;
  311. cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
  312. cl->cl_recover_timer = dlm_config.ci_recover_timer;
  313. cl->cl_toss_secs = dlm_config.ci_toss_secs;
  314. cl->cl_scan_secs = dlm_config.ci_scan_secs;
  315. cl->cl_log_debug = dlm_config.ci_log_debug;
  316. cl->cl_log_info = dlm_config.ci_log_info;
  317. cl->cl_protocol = dlm_config.ci_protocol;
  318. cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
  319. cl->cl_waitwarn_us = dlm_config.ci_waitwarn_us;
  320. cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
  321. cl->cl_recover_callbacks = dlm_config.ci_recover_callbacks;
  322. memcpy(cl->cl_cluster_name, dlm_config.ci_cluster_name,
  323. DLM_LOCKSPACE_LEN);
  324. space_list = &sps->ss_group;
  325. comm_list = &cms->cs_group;
  326. return &cl->group;
  327. fail:
  328. kfree(cl);
  329. kfree(sps);
  330. kfree(cms);
  331. return ERR_PTR(-ENOMEM);
  332. }
  333. static void drop_cluster(struct config_group *g, struct config_item *i)
  334. {
  335. struct dlm_cluster *cl = config_item_to_cluster(i);
  336. configfs_remove_default_groups(&cl->group);
  337. space_list = NULL;
  338. comm_list = NULL;
  339. config_item_put(i);
  340. }
  341. static void release_cluster(struct config_item *i)
  342. {
  343. struct dlm_cluster *cl = config_item_to_cluster(i);
  344. kfree(cl);
  345. }
  346. static struct config_group *make_space(struct config_group *g, const char *name)
  347. {
  348. struct dlm_space *sp = NULL;
  349. struct dlm_nodes *nds = NULL;
  350. sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
  351. nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
  352. if (!sp || !nds)
  353. goto fail;
  354. config_group_init_type_name(&sp->group, name, &space_type);
  355. config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
  356. configfs_add_default_group(&nds->ns_group, &sp->group);
  357. INIT_LIST_HEAD(&sp->members);
  358. mutex_init(&sp->members_lock);
  359. sp->members_count = 0;
  360. return &sp->group;
  361. fail:
  362. kfree(sp);
  363. kfree(nds);
  364. return ERR_PTR(-ENOMEM);
  365. }
  366. static void drop_space(struct config_group *g, struct config_item *i)
  367. {
  368. struct dlm_space *sp = config_item_to_space(i);
  369. /* assert list_empty(&sp->members) */
  370. configfs_remove_default_groups(&sp->group);
  371. config_item_put(i);
  372. }
  373. static void release_space(struct config_item *i)
  374. {
  375. struct dlm_space *sp = config_item_to_space(i);
  376. kfree(sp);
  377. }
  378. static struct config_item *make_comm(struct config_group *g, const char *name)
  379. {
  380. struct dlm_comm *cm;
  381. cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
  382. if (!cm)
  383. return ERR_PTR(-ENOMEM);
  384. config_item_init_type_name(&cm->item, name, &comm_type);
  385. cm->seq = dlm_comm_count++;
  386. if (!cm->seq)
  387. cm->seq = dlm_comm_count++;
  388. cm->nodeid = -1;
  389. cm->local = 0;
  390. cm->addr_count = 0;
  391. return &cm->item;
  392. }
  393. static void drop_comm(struct config_group *g, struct config_item *i)
  394. {
  395. struct dlm_comm *cm = config_item_to_comm(i);
  396. if (local_comm == cm)
  397. local_comm = NULL;
  398. dlm_lowcomms_close(cm->nodeid);
  399. while (cm->addr_count--)
  400. kfree(cm->addr[cm->addr_count]);
  401. config_item_put(i);
  402. }
  403. static void release_comm(struct config_item *i)
  404. {
  405. struct dlm_comm *cm = config_item_to_comm(i);
  406. kfree(cm);
  407. }
  408. static struct config_item *make_node(struct config_group *g, const char *name)
  409. {
  410. struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
  411. struct dlm_node *nd;
  412. nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
  413. if (!nd)
  414. return ERR_PTR(-ENOMEM);
  415. config_item_init_type_name(&nd->item, name, &node_type);
  416. nd->nodeid = -1;
  417. nd->weight = 1; /* default weight of 1 if none is set */
  418. nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
  419. mutex_lock(&sp->members_lock);
  420. list_add(&nd->list, &sp->members);
  421. sp->members_count++;
  422. mutex_unlock(&sp->members_lock);
  423. return &nd->item;
  424. }
  425. static void drop_node(struct config_group *g, struct config_item *i)
  426. {
  427. struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
  428. struct dlm_node *nd = config_item_to_node(i);
  429. mutex_lock(&sp->members_lock);
  430. list_del(&nd->list);
  431. sp->members_count--;
  432. mutex_unlock(&sp->members_lock);
  433. config_item_put(i);
  434. }
  435. static void release_node(struct config_item *i)
  436. {
  437. struct dlm_node *nd = config_item_to_node(i);
  438. kfree(nd);
  439. }
  440. static struct dlm_clusters clusters_root = {
  441. .subsys = {
  442. .su_group = {
  443. .cg_item = {
  444. .ci_namebuf = "dlm",
  445. .ci_type = &clusters_type,
  446. },
  447. },
  448. },
  449. };
  450. int __init dlm_config_init(void)
  451. {
  452. config_group_init(&clusters_root.subsys.su_group);
  453. mutex_init(&clusters_root.subsys.su_mutex);
  454. return configfs_register_subsystem(&clusters_root.subsys);
  455. }
  456. void dlm_config_exit(void)
  457. {
  458. configfs_unregister_subsystem(&clusters_root.subsys);
  459. }
  460. /*
  461. * Functions for user space to read/write attributes
  462. */
  463. static ssize_t comm_nodeid_show(struct config_item *item, char *buf)
  464. {
  465. return sprintf(buf, "%d\n", config_item_to_comm(item)->nodeid);
  466. }
  467. static ssize_t comm_nodeid_store(struct config_item *item, const char *buf,
  468. size_t len)
  469. {
  470. int rc = kstrtoint(buf, 0, &config_item_to_comm(item)->nodeid);
  471. if (rc)
  472. return rc;
  473. return len;
  474. }
  475. static ssize_t comm_local_show(struct config_item *item, char *buf)
  476. {
  477. return sprintf(buf, "%d\n", config_item_to_comm(item)->local);
  478. }
  479. static ssize_t comm_local_store(struct config_item *item, const char *buf,
  480. size_t len)
  481. {
  482. struct dlm_comm *cm = config_item_to_comm(item);
  483. int rc = kstrtoint(buf, 0, &cm->local);
  484. if (rc)
  485. return rc;
  486. if (cm->local && !local_comm)
  487. local_comm = cm;
  488. return len;
  489. }
  490. static ssize_t comm_addr_store(struct config_item *item, const char *buf,
  491. size_t len)
  492. {
  493. struct dlm_comm *cm = config_item_to_comm(item);
  494. struct sockaddr_storage *addr;
  495. int rv;
  496. if (len != sizeof(struct sockaddr_storage))
  497. return -EINVAL;
  498. if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
  499. return -ENOSPC;
  500. addr = kzalloc(sizeof(*addr), GFP_NOFS);
  501. if (!addr)
  502. return -ENOMEM;
  503. memcpy(addr, buf, len);
  504. rv = dlm_lowcomms_addr(cm->nodeid, addr, len);
  505. if (rv) {
  506. kfree(addr);
  507. return rv;
  508. }
  509. cm->addr[cm->addr_count++] = addr;
  510. return len;
  511. }
  512. static ssize_t comm_addr_list_show(struct config_item *item, char *buf)
  513. {
  514. struct dlm_comm *cm = config_item_to_comm(item);
  515. ssize_t s;
  516. ssize_t allowance;
  517. int i;
  518. struct sockaddr_storage *addr;
  519. struct sockaddr_in *addr_in;
  520. struct sockaddr_in6 *addr_in6;
  521. /* Taken from ip6_addr_string() defined in lib/vsprintf.c */
  522. char buf0[sizeof("AF_INET6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
  523. /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
  524. allowance = 4096;
  525. buf[0] = '\0';
  526. for (i = 0; i < cm->addr_count; i++) {
  527. addr = cm->addr[i];
  528. switch(addr->ss_family) {
  529. case AF_INET:
  530. addr_in = (struct sockaddr_in *)addr;
  531. s = sprintf(buf0, "AF_INET %pI4\n", &addr_in->sin_addr.s_addr);
  532. break;
  533. case AF_INET6:
  534. addr_in6 = (struct sockaddr_in6 *)addr;
  535. s = sprintf(buf0, "AF_INET6 %pI6\n", &addr_in6->sin6_addr);
  536. break;
  537. default:
  538. s = sprintf(buf0, "%s\n", "<UNKNOWN>");
  539. break;
  540. }
  541. allowance -= s;
  542. if (allowance >= 0)
  543. strcat(buf, buf0);
  544. else {
  545. allowance += s;
  546. break;
  547. }
  548. }
  549. return 4096 - allowance;
  550. }
  551. CONFIGFS_ATTR(comm_, nodeid);
  552. CONFIGFS_ATTR(comm_, local);
  553. CONFIGFS_ATTR_WO(comm_, addr);
  554. CONFIGFS_ATTR_RO(comm_, addr_list);
  555. static struct configfs_attribute *comm_attrs[] = {
  556. [COMM_ATTR_NODEID] = &comm_attr_nodeid,
  557. [COMM_ATTR_LOCAL] = &comm_attr_local,
  558. [COMM_ATTR_ADDR] = &comm_attr_addr,
  559. [COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list,
  560. NULL,
  561. };
  562. static ssize_t node_nodeid_show(struct config_item *item, char *buf)
  563. {
  564. return sprintf(buf, "%d\n", config_item_to_node(item)->nodeid);
  565. }
  566. static ssize_t node_nodeid_store(struct config_item *item, const char *buf,
  567. size_t len)
  568. {
  569. struct dlm_node *nd = config_item_to_node(item);
  570. uint32_t seq = 0;
  571. int rc = kstrtoint(buf, 0, &nd->nodeid);
  572. if (rc)
  573. return rc;
  574. dlm_comm_seq(nd->nodeid, &seq);
  575. nd->comm_seq = seq;
  576. return len;
  577. }
  578. static ssize_t node_weight_show(struct config_item *item, char *buf)
  579. {
  580. return sprintf(buf, "%d\n", config_item_to_node(item)->weight);
  581. }
  582. static ssize_t node_weight_store(struct config_item *item, const char *buf,
  583. size_t len)
  584. {
  585. int rc = kstrtoint(buf, 0, &config_item_to_node(item)->weight);
  586. if (rc)
  587. return rc;
  588. return len;
  589. }
  590. CONFIGFS_ATTR(node_, nodeid);
  591. CONFIGFS_ATTR(node_, weight);
  592. static struct configfs_attribute *node_attrs[] = {
  593. [NODE_ATTR_NODEID] = &node_attr_nodeid,
  594. [NODE_ATTR_WEIGHT] = &node_attr_weight,
  595. NULL,
  596. };
  597. /*
  598. * Functions for the dlm to get the info that's been configured
  599. */
  600. static struct dlm_space *get_space(char *name)
  601. {
  602. struct config_item *i;
  603. if (!space_list)
  604. return NULL;
  605. mutex_lock(&space_list->cg_subsys->su_mutex);
  606. i = config_group_find_item(space_list, name);
  607. mutex_unlock(&space_list->cg_subsys->su_mutex);
  608. return config_item_to_space(i);
  609. }
  610. static void put_space(struct dlm_space *sp)
  611. {
  612. config_item_put(&sp->group.cg_item);
  613. }
  614. static struct dlm_comm *get_comm(int nodeid)
  615. {
  616. struct config_item *i;
  617. struct dlm_comm *cm = NULL;
  618. int found = 0;
  619. if (!comm_list)
  620. return NULL;
  621. mutex_lock(&clusters_root.subsys.su_mutex);
  622. list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
  623. cm = config_item_to_comm(i);
  624. if (cm->nodeid != nodeid)
  625. continue;
  626. found = 1;
  627. config_item_get(i);
  628. break;
  629. }
  630. mutex_unlock(&clusters_root.subsys.su_mutex);
  631. if (!found)
  632. cm = NULL;
  633. return cm;
  634. }
  635. static void put_comm(struct dlm_comm *cm)
  636. {
  637. config_item_put(&cm->item);
  638. }
  639. /* caller must free mem */
  640. int dlm_config_nodes(char *lsname, struct dlm_config_node **nodes_out,
  641. int *count_out)
  642. {
  643. struct dlm_space *sp;
  644. struct dlm_node *nd;
  645. struct dlm_config_node *nodes, *node;
  646. int rv, count;
  647. sp = get_space(lsname);
  648. if (!sp)
  649. return -EEXIST;
  650. mutex_lock(&sp->members_lock);
  651. if (!sp->members_count) {
  652. rv = -EINVAL;
  653. printk(KERN_ERR "dlm: zero members_count\n");
  654. goto out;
  655. }
  656. count = sp->members_count;
  657. nodes = kcalloc(count, sizeof(struct dlm_config_node), GFP_NOFS);
  658. if (!nodes) {
  659. rv = -ENOMEM;
  660. goto out;
  661. }
  662. node = nodes;
  663. list_for_each_entry(nd, &sp->members, list) {
  664. node->nodeid = nd->nodeid;
  665. node->weight = nd->weight;
  666. node->new = nd->new;
  667. node->comm_seq = nd->comm_seq;
  668. node++;
  669. nd->new = 0;
  670. }
  671. *count_out = count;
  672. *nodes_out = nodes;
  673. rv = 0;
  674. out:
  675. mutex_unlock(&sp->members_lock);
  676. put_space(sp);
  677. return rv;
  678. }
  679. int dlm_comm_seq(int nodeid, uint32_t *seq)
  680. {
  681. struct dlm_comm *cm = get_comm(nodeid);
  682. if (!cm)
  683. return -EEXIST;
  684. *seq = cm->seq;
  685. put_comm(cm);
  686. return 0;
  687. }
  688. int dlm_our_nodeid(void)
  689. {
  690. return local_comm ? local_comm->nodeid : 0;
  691. }
  692. /* num 0 is first addr, num 1 is second addr */
  693. int dlm_our_addr(struct sockaddr_storage *addr, int num)
  694. {
  695. if (!local_comm)
  696. return -1;
  697. if (num + 1 > local_comm->addr_count)
  698. return -1;
  699. memcpy(addr, local_comm->addr[num], sizeof(*addr));
  700. return 0;
  701. }
  702. /* Config file defaults */
  703. #define DEFAULT_TCP_PORT 21064
  704. #define DEFAULT_BUFFER_SIZE 4096
  705. #define DEFAULT_RSBTBL_SIZE 1024
  706. #define DEFAULT_RECOVER_TIMER 5
  707. #define DEFAULT_TOSS_SECS 10
  708. #define DEFAULT_SCAN_SECS 5
  709. #define DEFAULT_LOG_DEBUG 0
  710. #define DEFAULT_LOG_INFO 1
  711. #define DEFAULT_PROTOCOL 0
  712. #define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
  713. #define DEFAULT_WAITWARN_US 0
  714. #define DEFAULT_NEW_RSB_COUNT 128
  715. #define DEFAULT_RECOVER_CALLBACKS 0
  716. #define DEFAULT_CLUSTER_NAME ""
  717. struct dlm_config_info dlm_config = {
  718. .ci_tcp_port = DEFAULT_TCP_PORT,
  719. .ci_buffer_size = DEFAULT_BUFFER_SIZE,
  720. .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
  721. .ci_recover_timer = DEFAULT_RECOVER_TIMER,
  722. .ci_toss_secs = DEFAULT_TOSS_SECS,
  723. .ci_scan_secs = DEFAULT_SCAN_SECS,
  724. .ci_log_debug = DEFAULT_LOG_DEBUG,
  725. .ci_log_info = DEFAULT_LOG_INFO,
  726. .ci_protocol = DEFAULT_PROTOCOL,
  727. .ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
  728. .ci_waitwarn_us = DEFAULT_WAITWARN_US,
  729. .ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT,
  730. .ci_recover_callbacks = DEFAULT_RECOVER_CALLBACKS,
  731. .ci_cluster_name = DEFAULT_CLUSTER_NAME
  732. };