config.c 23 KB

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