config.c 23 KB

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