config.c 24 KB

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