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