config.c 24 KB

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