1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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.
11 *******************************************************************************
12 ******************************************************************************/
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/configfs.h>
22 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
23 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
24 * /config/dlm/<cluster>/comms/<comm>/nodeid
25 * /config/dlm/<cluster>/comms/<comm>/local
26 * /config/dlm/<cluster>/comms/<comm>/addr
27 * The <cluster> level is useless, but I haven't figured out how to avoid it.
30 static struct config_group *space_list;
31 static struct config_group *comm_list;
32 static struct comm *local_comm;
43 static struct config_group *make_cluster(struct config_group *, const char *);
44 static void drop_cluster(struct config_group *, struct config_item *);
45 static void release_cluster(struct config_item *);
46 static struct config_group *make_space(struct config_group *, const char *);
47 static void drop_space(struct config_group *, struct config_item *);
48 static void release_space(struct config_item *);
49 static struct config_item *make_comm(struct config_group *, const char *);
50 static void drop_comm(struct config_group *, struct config_item *);
51 static void release_comm(struct config_item *);
52 static struct config_item *make_node(struct config_group *, const char *);
53 static void drop_node(struct config_group *, struct config_item *);
54 static void release_node(struct config_item *);
56 static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
58 static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
59 const char *buf, size_t len);
60 static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
62 static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
63 const char *buf, size_t len);
65 static ssize_t comm_nodeid_read(struct comm *cm, char *buf);
66 static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len);
67 static ssize_t comm_local_read(struct comm *cm, char *buf);
68 static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len);
69 static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len);
70 static ssize_t node_nodeid_read(struct node *nd, char *buf);
71 static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len);
72 static ssize_t node_weight_read(struct node *nd, char *buf);
73 static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len);
81 struct comm_attribute {
82 struct configfs_attribute attr;
83 ssize_t (*show)(struct comm *, char *);
84 ssize_t (*store)(struct comm *, const char *, size_t);
87 static struct comm_attribute comm_attr_nodeid = {
88 .attr = { .ca_owner = THIS_MODULE,
90 .ca_mode = S_IRUGO | S_IWUSR },
91 .show = comm_nodeid_read,
92 .store = comm_nodeid_write,
95 static struct comm_attribute comm_attr_local = {
96 .attr = { .ca_owner = THIS_MODULE,
98 .ca_mode = S_IRUGO | S_IWUSR },
99 .show = comm_local_read,
100 .store = comm_local_write,
103 static struct comm_attribute comm_attr_addr = {
104 .attr = { .ca_owner = THIS_MODULE,
106 .ca_mode = S_IRUGO | S_IWUSR },
107 .store = comm_addr_write,
110 static struct configfs_attribute *comm_attrs[] = {
111 [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
112 [COMM_ATTR_LOCAL] = &comm_attr_local.attr,
113 [COMM_ATTR_ADDR] = &comm_attr_addr.attr,
118 NODE_ATTR_NODEID = 0,
122 struct node_attribute {
123 struct configfs_attribute attr;
124 ssize_t (*show)(struct node *, char *);
125 ssize_t (*store)(struct node *, const char *, size_t);
128 static struct node_attribute node_attr_nodeid = {
129 .attr = { .ca_owner = THIS_MODULE,
131 .ca_mode = S_IRUGO | S_IWUSR },
132 .show = node_nodeid_read,
133 .store = node_nodeid_write,
136 static struct node_attribute node_attr_weight = {
137 .attr = { .ca_owner = THIS_MODULE,
139 .ca_mode = S_IRUGO | S_IWUSR },
140 .show = node_weight_read,
141 .store = node_weight_write,
144 static struct configfs_attribute *node_attrs[] = {
145 [NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
146 [NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
151 struct configfs_subsystem subsys;
155 struct config_group group;
159 struct config_group ss_group;
163 struct config_group group;
164 struct list_head members;
165 struct mutex members_lock;
170 struct config_group cs_group;
174 struct config_item item;
178 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
182 struct config_group ns_group;
186 struct config_item item;
187 struct list_head list; /* space->members */
192 static struct configfs_group_operations clusters_ops = {
193 .make_group = make_cluster,
194 .drop_item = drop_cluster,
197 static struct configfs_item_operations cluster_ops = {
198 .release = release_cluster,
201 static struct configfs_group_operations spaces_ops = {
202 .make_group = make_space,
203 .drop_item = drop_space,
206 static struct configfs_item_operations space_ops = {
207 .release = release_space,
210 static struct configfs_group_operations comms_ops = {
211 .make_item = make_comm,
212 .drop_item = drop_comm,
215 static struct configfs_item_operations comm_ops = {
216 .release = release_comm,
217 .show_attribute = show_comm,
218 .store_attribute = store_comm,
221 static struct configfs_group_operations nodes_ops = {
222 .make_item = make_node,
223 .drop_item = drop_node,
226 static struct configfs_item_operations node_ops = {
227 .release = release_node,
228 .show_attribute = show_node,
229 .store_attribute = store_node,
232 static struct config_item_type clusters_type = {
233 .ct_group_ops = &clusters_ops,
234 .ct_owner = THIS_MODULE,
237 static struct config_item_type cluster_type = {
238 .ct_item_ops = &cluster_ops,
239 .ct_owner = THIS_MODULE,
242 static struct config_item_type spaces_type = {
243 .ct_group_ops = &spaces_ops,
244 .ct_owner = THIS_MODULE,
247 static struct config_item_type space_type = {
248 .ct_item_ops = &space_ops,
249 .ct_owner = THIS_MODULE,
252 static struct config_item_type comms_type = {
253 .ct_group_ops = &comms_ops,
254 .ct_owner = THIS_MODULE,
257 static struct config_item_type comm_type = {
258 .ct_item_ops = &comm_ops,
259 .ct_attrs = comm_attrs,
260 .ct_owner = THIS_MODULE,
263 static struct config_item_type nodes_type = {
264 .ct_group_ops = &nodes_ops,
265 .ct_owner = THIS_MODULE,
268 static struct config_item_type node_type = {
269 .ct_item_ops = &node_ops,
270 .ct_attrs = node_attrs,
271 .ct_owner = THIS_MODULE,
274 static struct cluster *to_cluster(struct config_item *i)
276 return i ? container_of(to_config_group(i), struct cluster, group):NULL;
279 static struct space *to_space(struct config_item *i)
281 return i ? container_of(to_config_group(i), struct space, group) : NULL;
284 static struct comm *to_comm(struct config_item *i)
286 return i ? container_of(i, struct comm, item) : NULL;
289 static struct node *to_node(struct config_item *i)
291 return i ? container_of(i, struct node, item) : NULL;
294 static struct config_group *make_cluster(struct config_group *g,
297 struct cluster *cl = NULL;
298 struct spaces *sps = NULL;
299 struct comms *cms = NULL;
302 cl = kzalloc(sizeof(struct cluster), GFP_KERNEL);
303 gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
304 sps = kzalloc(sizeof(struct spaces), GFP_KERNEL);
305 cms = kzalloc(sizeof(struct comms), GFP_KERNEL);
307 if (!cl || !gps || !sps || !cms)
310 config_group_init_type_name(&cl->group, name, &cluster_type);
311 config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
312 config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
314 cl->group.default_groups = gps;
315 cl->group.default_groups[0] = &sps->ss_group;
316 cl->group.default_groups[1] = &cms->cs_group;
317 cl->group.default_groups[2] = NULL;
319 space_list = &sps->ss_group;
320 comm_list = &cms->cs_group;
331 static void drop_cluster(struct config_group *g, struct config_item *i)
333 struct cluster *cl = to_cluster(i);
334 struct config_item *tmp;
337 for (j = 0; cl->group.default_groups[j]; j++) {
338 tmp = &cl->group.default_groups[j]->cg_item;
339 cl->group.default_groups[j] = NULL;
340 config_item_put(tmp);
349 static void release_cluster(struct config_item *i)
351 struct cluster *cl = to_cluster(i);
352 kfree(cl->group.default_groups);
356 static struct config_group *make_space(struct config_group *g, const char *name)
358 struct space *sp = NULL;
359 struct nodes *nds = NULL;
362 sp = kzalloc(sizeof(struct space), GFP_KERNEL);
363 gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
364 nds = kzalloc(sizeof(struct nodes), GFP_KERNEL);
366 if (!sp || !gps || !nds)
369 config_group_init_type_name(&sp->group, name, &space_type);
370 config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
372 sp->group.default_groups = gps;
373 sp->group.default_groups[0] = &nds->ns_group;
374 sp->group.default_groups[1] = NULL;
376 INIT_LIST_HEAD(&sp->members);
377 mutex_init(&sp->members_lock);
378 sp->members_count = 0;
388 static void drop_space(struct config_group *g, struct config_item *i)
390 struct space *sp = to_space(i);
391 struct config_item *tmp;
394 /* assert list_empty(&sp->members) */
396 for (j = 0; sp->group.default_groups[j]; j++) {
397 tmp = &sp->group.default_groups[j]->cg_item;
398 sp->group.default_groups[j] = NULL;
399 config_item_put(tmp);
405 static void release_space(struct config_item *i)
407 struct space *sp = to_space(i);
408 kfree(sp->group.default_groups);
412 static struct config_item *make_comm(struct config_group *g, const char *name)
416 cm = kzalloc(sizeof(struct comm), GFP_KERNEL);
420 config_item_init_type_name(&cm->item, name, &comm_type);
427 static void drop_comm(struct config_group *g, struct config_item *i)
429 struct comm *cm = to_comm(i);
430 if (local_comm == cm)
432 while (cm->addr_count--)
433 kfree(cm->addr[cm->addr_count]);
437 static void release_comm(struct config_item *i)
439 struct comm *cm = to_comm(i);
443 static struct config_item *make_node(struct config_group *g, const char *name)
445 struct space *sp = to_space(g->cg_item.ci_parent);
448 nd = kzalloc(sizeof(struct node), GFP_KERNEL);
452 config_item_init_type_name(&nd->item, name, &node_type);
454 nd->weight = 1; /* default weight of 1 if none is set */
456 mutex_lock(&sp->members_lock);
457 list_add(&nd->list, &sp->members);
459 mutex_unlock(&sp->members_lock);
464 static void drop_node(struct config_group *g, struct config_item *i)
466 struct space *sp = to_space(g->cg_item.ci_parent);
467 struct node *nd = to_node(i);
469 mutex_lock(&sp->members_lock);
472 mutex_unlock(&sp->members_lock);
477 static void release_node(struct config_item *i)
479 struct node *nd = to_node(i);
483 static struct clusters clusters_root = {
488 .ci_type = &clusters_type,
494 int dlm_config_init(void)
496 config_group_init(&clusters_root.subsys.su_group);
497 init_MUTEX(&clusters_root.subsys.su_sem);
498 return configfs_register_subsystem(&clusters_root.subsys);
501 void dlm_config_exit(void)
503 configfs_unregister_subsystem(&clusters_root.subsys);
507 * Functions for user space to read/write attributes
510 static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
513 struct comm *cm = to_comm(i);
514 struct comm_attribute *cma =
515 container_of(a, struct comm_attribute, attr);
516 return cma->show ? cma->show(cm, buf) : 0;
519 static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
520 const char *buf, size_t len)
522 struct comm *cm = to_comm(i);
523 struct comm_attribute *cma =
524 container_of(a, struct comm_attribute, attr);
525 return cma->store ? cma->store(cm, buf, len) : -EINVAL;
528 static ssize_t comm_nodeid_read(struct comm *cm, char *buf)
530 return sprintf(buf, "%d\n", cm->nodeid);
533 static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len)
535 cm->nodeid = simple_strtol(buf, NULL, 0);
539 static ssize_t comm_local_read(struct comm *cm, char *buf)
541 return sprintf(buf, "%d\n", cm->local);
544 static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len)
546 cm->local= simple_strtol(buf, NULL, 0);
547 if (cm->local && !local_comm)
552 static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len)
554 struct sockaddr_storage *addr;
556 if (len != sizeof(struct sockaddr_storage))
559 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
562 addr = kzalloc(sizeof(*addr), GFP_KERNEL);
566 memcpy(addr, buf, len);
567 cm->addr[cm->addr_count++] = addr;
571 static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
574 struct node *nd = to_node(i);
575 struct node_attribute *nda =
576 container_of(a, struct node_attribute, attr);
577 return nda->show ? nda->show(nd, buf) : 0;
580 static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
581 const char *buf, size_t len)
583 struct node *nd = to_node(i);
584 struct node_attribute *nda =
585 container_of(a, struct node_attribute, attr);
586 return nda->store ? nda->store(nd, buf, len) : -EINVAL;
589 static ssize_t node_nodeid_read(struct node *nd, char *buf)
591 return sprintf(buf, "%d\n", nd->nodeid);
594 static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len)
596 nd->nodeid = simple_strtol(buf, NULL, 0);
600 static ssize_t node_weight_read(struct node *nd, char *buf)
602 return sprintf(buf, "%d\n", nd->weight);
605 static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len)
607 nd->weight = simple_strtol(buf, NULL, 0);
612 * Functions for the dlm to get the info that's been configured
615 static struct space *get_space(char *name)
619 return to_space(config_group_find_obj(space_list, name));
622 static void put_space(struct space *sp)
624 config_item_put(&sp->group.cg_item);
627 static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr)
629 struct config_item *i;
630 struct comm *cm = NULL;
636 down(&clusters_root.subsys.su_sem);
638 list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
642 if (cm->nodeid != nodeid)
647 if (!cm->addr_count ||
648 memcmp(cm->addr[0], addr, sizeof(*addr)))
654 up(&clusters_root.subsys.su_sem);
663 static void put_comm(struct comm *cm)
665 config_item_put(&cm->item);
668 /* caller must free mem */
669 int dlm_nodeid_list(char *lsname, int **ids_out)
676 sp = get_space(lsname);
680 mutex_lock(&sp->members_lock);
681 if (!sp->members_count) {
686 ids = kcalloc(sp->members_count, sizeof(int), GFP_KERNEL);
692 rv = sp->members_count;
693 list_for_each_entry(nd, &sp->members, list)
694 ids[i++] = nd->nodeid;
697 printk("bad nodeid count %d %d\n", rv, i);
701 mutex_unlock(&sp->members_lock);
706 int dlm_node_weight(char *lsname, int nodeid)
712 sp = get_space(lsname);
716 mutex_lock(&sp->members_lock);
717 list_for_each_entry(nd, &sp->members, list) {
718 if (nd->nodeid != nodeid)
723 mutex_unlock(&sp->members_lock);
729 int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
731 struct comm *cm = get_comm(nodeid, NULL);
736 memcpy(addr, cm->addr[0], sizeof(*addr));
741 int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
743 struct comm *cm = get_comm(0, addr);
746 *nodeid = cm->nodeid;
751 int dlm_our_nodeid(void)
753 return local_comm ? local_comm->nodeid : 0;
756 /* num 0 is first addr, num 1 is second addr */
757 int dlm_our_addr(struct sockaddr_storage *addr, int num)
761 if (num + 1 > local_comm->addr_count)
763 memcpy(addr, local_comm->addr[num], sizeof(*addr));
767 /* Config file defaults */
768 #define DEFAULT_TCP_PORT 21064
769 #define DEFAULT_BUFFER_SIZE 4096
770 #define DEFAULT_RSBTBL_SIZE 256
771 #define DEFAULT_LKBTBL_SIZE 1024
772 #define DEFAULT_DIRTBL_SIZE 512
773 #define DEFAULT_RECOVER_TIMER 5
774 #define DEFAULT_TOSS_SECS 10
775 #define DEFAULT_SCAN_SECS 5
777 struct dlm_config_info dlm_config = {
778 .tcp_port = DEFAULT_TCP_PORT,
779 .buffer_size = DEFAULT_BUFFER_SIZE,
780 .rsbtbl_size = DEFAULT_RSBTBL_SIZE,
781 .lkbtbl_size = DEFAULT_LKBTBL_SIZE,
782 .dirtbl_size = DEFAULT_DIRTBL_SIZE,
783 .recover_timer = DEFAULT_RECOVER_TIMER,
784 .toss_secs = DEFAULT_TOSS_SECS,
785 .scan_secs = DEFAULT_SCAN_SECS