config.c 25 KB

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