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