config.c 23 KB

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