config.c 23 KB

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