config.c 28 KB

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