config.c 24 KB

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