Merge branch 'master' into 85xx
[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-2005 Red Hat, Inc.  All rights reserved.
6 **
7 **  This copyrighted material is made available to anyone wishing to use,
8 **  modify, copy, or redistribute it subject to the terms and conditions
9 **  of the GNU General Public License v.2.
10 **
11 *******************************************************************************
12 ******************************************************************************/
13
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 };
93
94 enum {
95         CLUSTER_ATTR_TCP_PORT = 0,
96         CLUSTER_ATTR_BUFFER_SIZE,
97         CLUSTER_ATTR_RSBTBL_SIZE,
98         CLUSTER_ATTR_LKBTBL_SIZE,
99         CLUSTER_ATTR_DIRTBL_SIZE,
100         CLUSTER_ATTR_RECOVER_TIMER,
101         CLUSTER_ATTR_TOSS_SECS,
102         CLUSTER_ATTR_SCAN_SECS,
103         CLUSTER_ATTR_LOG_DEBUG,
104 };
105
106 struct cluster_attribute {
107         struct configfs_attribute attr;
108         ssize_t (*show)(struct cluster *, char *);
109         ssize_t (*store)(struct cluster *, const char *, size_t);
110 };
111
112 static ssize_t cluster_set(struct cluster *cl, unsigned int *cl_field,
113                            unsigned int *info_field, int check_zero,
114                            const char *buf, size_t len)
115 {
116         unsigned int x;
117
118         if (!capable(CAP_SYS_ADMIN))
119                 return -EACCES;
120
121         x = simple_strtoul(buf, NULL, 0);
122
123         if (check_zero && !x)
124                 return -EINVAL;
125
126         *cl_field = x;
127         *info_field = x;
128
129         return len;
130 }
131
132 #define __CONFIGFS_ATTR(_name,_mode,_read,_write) {                           \
133         .attr   = { .ca_name = __stringify(_name),                            \
134                     .ca_mode = _mode,                                         \
135                     .ca_owner = THIS_MODULE },                                \
136         .show   = _read,                                                      \
137         .store  = _write,                                                     \
138 }
139
140 #define CLUSTER_ATTR(name, check_zero)                                        \
141 static ssize_t name##_write(struct cluster *cl, const char *buf, size_t len)  \
142 {                                                                             \
143         return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name,         \
144                            check_zero, buf, len);                             \
145 }                                                                             \
146 static ssize_t name##_read(struct cluster *cl, char *buf)                     \
147 {                                                                             \
148         return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name);               \
149 }                                                                             \
150 static struct cluster_attribute cluster_attr_##name =                         \
151 __CONFIGFS_ATTR(name, 0644, name##_read, name##_write)
152
153 CLUSTER_ATTR(tcp_port, 1);
154 CLUSTER_ATTR(buffer_size, 1);
155 CLUSTER_ATTR(rsbtbl_size, 1);
156 CLUSTER_ATTR(lkbtbl_size, 1);
157 CLUSTER_ATTR(dirtbl_size, 1);
158 CLUSTER_ATTR(recover_timer, 1);
159 CLUSTER_ATTR(toss_secs, 1);
160 CLUSTER_ATTR(scan_secs, 1);
161 CLUSTER_ATTR(log_debug, 0);
162
163 static struct configfs_attribute *cluster_attrs[] = {
164         [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
165         [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
166         [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
167         [CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr,
168         [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
169         [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
170         [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
171         [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
172         [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.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 };
288
289 static struct configfs_group_operations clusters_ops = {
290         .make_group = make_cluster,
291         .drop_item = drop_cluster,
292 };
293
294 static struct configfs_item_operations cluster_ops = {
295         .release = release_cluster,
296         .show_attribute = show_cluster,
297         .store_attribute = store_cluster,
298 };
299
300 static struct configfs_group_operations spaces_ops = {
301         .make_group = make_space,
302         .drop_item = drop_space,
303 };
304
305 static struct configfs_item_operations space_ops = {
306         .release = release_space,
307 };
308
309 static struct configfs_group_operations comms_ops = {
310         .make_item = make_comm,
311         .drop_item = drop_comm,
312 };
313
314 static struct configfs_item_operations comm_ops = {
315         .release = release_comm,
316         .show_attribute = show_comm,
317         .store_attribute = store_comm,
318 };
319
320 static struct configfs_group_operations nodes_ops = {
321         .make_item = make_node,
322         .drop_item = drop_node,
323 };
324
325 static struct configfs_item_operations node_ops = {
326         .release = release_node,
327         .show_attribute = show_node,
328         .store_attribute = store_node,
329 };
330
331 static struct config_item_type clusters_type = {
332         .ct_group_ops = &clusters_ops,
333         .ct_owner = THIS_MODULE,
334 };
335
336 static struct config_item_type cluster_type = {
337         .ct_item_ops = &cluster_ops,
338         .ct_attrs = cluster_attrs,
339         .ct_owner = THIS_MODULE,
340 };
341
342 static struct config_item_type spaces_type = {
343         .ct_group_ops = &spaces_ops,
344         .ct_owner = THIS_MODULE,
345 };
346
347 static struct config_item_type space_type = {
348         .ct_item_ops = &space_ops,
349         .ct_owner = THIS_MODULE,
350 };
351
352 static struct config_item_type comms_type = {
353         .ct_group_ops = &comms_ops,
354         .ct_owner = THIS_MODULE,
355 };
356
357 static struct config_item_type comm_type = {
358         .ct_item_ops = &comm_ops,
359         .ct_attrs = comm_attrs,
360         .ct_owner = THIS_MODULE,
361 };
362
363 static struct config_item_type nodes_type = {
364         .ct_group_ops = &nodes_ops,
365         .ct_owner = THIS_MODULE,
366 };
367
368 static struct config_item_type node_type = {
369         .ct_item_ops = &node_ops,
370         .ct_attrs = node_attrs,
371         .ct_owner = THIS_MODULE,
372 };
373
374 static struct cluster *to_cluster(struct config_item *i)
375 {
376         return i ? container_of(to_config_group(i), struct cluster, group):NULL;
377 }
378
379 static struct space *to_space(struct config_item *i)
380 {
381         return i ? container_of(to_config_group(i), struct space, group) : NULL;
382 }
383
384 static struct comm *to_comm(struct config_item *i)
385 {
386         return i ? container_of(i, struct comm, item) : NULL;
387 }
388
389 static struct node *to_node(struct config_item *i)
390 {
391         return i ? container_of(i, struct node, item) : NULL;
392 }
393
394 static struct config_group *make_cluster(struct config_group *g,
395                                          const char *name)
396 {
397         struct cluster *cl = NULL;
398         struct spaces *sps = NULL;
399         struct comms *cms = NULL;
400         void *gps = NULL;
401
402         cl = kzalloc(sizeof(struct cluster), GFP_KERNEL);
403         gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL);
404         sps = kzalloc(sizeof(struct spaces), GFP_KERNEL);
405         cms = kzalloc(sizeof(struct comms), GFP_KERNEL);
406
407         if (!cl || !gps || !sps || !cms)
408                 goto fail;
409
410         config_group_init_type_name(&cl->group, name, &cluster_type);
411         config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
412         config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
413
414         cl->group.default_groups = gps;
415         cl->group.default_groups[0] = &sps->ss_group;
416         cl->group.default_groups[1] = &cms->cs_group;
417         cl->group.default_groups[2] = NULL;
418
419         cl->cl_tcp_port = dlm_config.ci_tcp_port;
420         cl->cl_buffer_size = dlm_config.ci_buffer_size;
421         cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
422         cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size;
423         cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
424         cl->cl_recover_timer = dlm_config.ci_recover_timer;
425         cl->cl_toss_secs = dlm_config.ci_toss_secs;
426         cl->cl_scan_secs = dlm_config.ci_scan_secs;
427         cl->cl_log_debug = dlm_config.ci_log_debug;
428
429         space_list = &sps->ss_group;
430         comm_list = &cms->cs_group;
431         return &cl->group;
432
433  fail:
434         kfree(cl);
435         kfree(gps);
436         kfree(sps);
437         kfree(cms);
438         return NULL;
439 }
440
441 static void drop_cluster(struct config_group *g, struct config_item *i)
442 {
443         struct cluster *cl = to_cluster(i);
444         struct config_item *tmp;
445         int j;
446
447         for (j = 0; cl->group.default_groups[j]; j++) {
448                 tmp = &cl->group.default_groups[j]->cg_item;
449                 cl->group.default_groups[j] = NULL;
450                 config_item_put(tmp);
451         }
452
453         space_list = NULL;
454         comm_list = NULL;
455
456         config_item_put(i);
457 }
458
459 static void release_cluster(struct config_item *i)
460 {
461         struct cluster *cl = to_cluster(i);
462         kfree(cl->group.default_groups);
463         kfree(cl);
464 }
465
466 static struct config_group *make_space(struct config_group *g, const char *name)
467 {
468         struct space *sp = NULL;
469         struct nodes *nds = NULL;
470         void *gps = NULL;
471
472         sp = kzalloc(sizeof(struct space), GFP_KERNEL);
473         gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);
474         nds = kzalloc(sizeof(struct nodes), GFP_KERNEL);
475
476         if (!sp || !gps || !nds)
477                 goto fail;
478
479         config_group_init_type_name(&sp->group, name, &space_type);
480         config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
481
482         sp->group.default_groups = gps;
483         sp->group.default_groups[0] = &nds->ns_group;
484         sp->group.default_groups[1] = NULL;
485
486         INIT_LIST_HEAD(&sp->members);
487         mutex_init(&sp->members_lock);
488         sp->members_count = 0;
489         return &sp->group;
490
491  fail:
492         kfree(sp);
493         kfree(gps);
494         kfree(nds);
495         return NULL;
496 }
497
498 static void drop_space(struct config_group *g, struct config_item *i)
499 {
500         struct space *sp = to_space(i);
501         struct config_item *tmp;
502         int j;
503
504         /* assert list_empty(&sp->members) */
505
506         for (j = 0; sp->group.default_groups[j]; j++) {
507                 tmp = &sp->group.default_groups[j]->cg_item;
508                 sp->group.default_groups[j] = NULL;
509                 config_item_put(tmp);
510         }
511
512         config_item_put(i);
513 }
514
515 static void release_space(struct config_item *i)
516 {
517         struct space *sp = to_space(i);
518         kfree(sp->group.default_groups);
519         kfree(sp);
520 }
521
522 static struct config_item *make_comm(struct config_group *g, const char *name)
523 {
524         struct comm *cm;
525
526         cm = kzalloc(sizeof(struct comm), GFP_KERNEL);
527         if (!cm)
528                 return NULL;
529
530         config_item_init_type_name(&cm->item, name, &comm_type);
531         cm->nodeid = -1;
532         cm->local = 0;
533         cm->addr_count = 0;
534         return &cm->item;
535 }
536
537 static void drop_comm(struct config_group *g, struct config_item *i)
538 {
539         struct comm *cm = to_comm(i);
540         if (local_comm == cm)
541                 local_comm = NULL;
542         dlm_lowcomms_close(cm->nodeid);
543         while (cm->addr_count--)
544                 kfree(cm->addr[cm->addr_count]);
545         config_item_put(i);
546 }
547
548 static void release_comm(struct config_item *i)
549 {
550         struct comm *cm = to_comm(i);
551         kfree(cm);
552 }
553
554 static struct config_item *make_node(struct config_group *g, const char *name)
555 {
556         struct space *sp = to_space(g->cg_item.ci_parent);
557         struct node *nd;
558
559         nd = kzalloc(sizeof(struct node), GFP_KERNEL);
560         if (!nd)
561                 return NULL;
562
563         config_item_init_type_name(&nd->item, name, &node_type);
564         nd->nodeid = -1;
565         nd->weight = 1;  /* default weight of 1 if none is set */
566
567         mutex_lock(&sp->members_lock);
568         list_add(&nd->list, &sp->members);
569         sp->members_count++;
570         mutex_unlock(&sp->members_lock);
571
572         return &nd->item;
573 }
574
575 static void drop_node(struct config_group *g, struct config_item *i)
576 {
577         struct space *sp = to_space(g->cg_item.ci_parent);
578         struct node *nd = to_node(i);
579
580         mutex_lock(&sp->members_lock);
581         list_del(&nd->list);
582         sp->members_count--;
583         mutex_unlock(&sp->members_lock);
584
585         config_item_put(i);
586 }
587
588 static void release_node(struct config_item *i)
589 {
590         struct node *nd = to_node(i);
591         kfree(nd);
592 }
593
594 static struct clusters clusters_root = {
595         .subsys = {
596                 .su_group = {
597                         .cg_item = {
598                                 .ci_namebuf = "dlm",
599                                 .ci_type = &clusters_type,
600                         },
601                 },
602         },
603 };
604
605 int dlm_config_init(void)
606 {
607         config_group_init(&clusters_root.subsys.su_group);
608         init_MUTEX(&clusters_root.subsys.su_sem);
609         return configfs_register_subsystem(&clusters_root.subsys);
610 }
611
612 void dlm_config_exit(void)
613 {
614         configfs_unregister_subsystem(&clusters_root.subsys);
615 }
616
617 /*
618  * Functions for user space to read/write attributes
619  */
620
621 static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
622                             char *buf)
623 {
624         struct cluster *cl = to_cluster(i);
625         struct cluster_attribute *cla =
626                         container_of(a, struct cluster_attribute, attr);
627         return cla->show ? cla->show(cl, buf) : 0;
628 }
629
630 static ssize_t store_cluster(struct config_item *i,
631                              struct configfs_attribute *a,
632                              const char *buf, size_t len)
633 {
634         struct cluster *cl = to_cluster(i);
635         struct cluster_attribute *cla =
636                 container_of(a, struct cluster_attribute, attr);
637         return cla->store ? cla->store(cl, buf, len) : -EINVAL;
638 }
639
640 static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
641                          char *buf)
642 {
643         struct comm *cm = to_comm(i);
644         struct comm_attribute *cma =
645                         container_of(a, struct comm_attribute, attr);
646         return cma->show ? cma->show(cm, buf) : 0;
647 }
648
649 static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
650                           const char *buf, size_t len)
651 {
652         struct comm *cm = to_comm(i);
653         struct comm_attribute *cma =
654                 container_of(a, struct comm_attribute, attr);
655         return cma->store ? cma->store(cm, buf, len) : -EINVAL;
656 }
657
658 static ssize_t comm_nodeid_read(struct comm *cm, char *buf)
659 {
660         return sprintf(buf, "%d\n", cm->nodeid);
661 }
662
663 static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len)
664 {
665         cm->nodeid = simple_strtol(buf, NULL, 0);
666         return len;
667 }
668
669 static ssize_t comm_local_read(struct comm *cm, char *buf)
670 {
671         return sprintf(buf, "%d\n", cm->local);
672 }
673
674 static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len)
675 {
676         cm->local= simple_strtol(buf, NULL, 0);
677         if (cm->local && !local_comm)
678                 local_comm = cm;
679         return len;
680 }
681
682 static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len)
683 {
684         struct sockaddr_storage *addr;
685
686         if (len != sizeof(struct sockaddr_storage))
687                 return -EINVAL;
688
689         if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
690                 return -ENOSPC;
691
692         addr = kzalloc(sizeof(*addr), GFP_KERNEL);
693         if (!addr)
694                 return -ENOMEM;
695
696         memcpy(addr, buf, len);
697         cm->addr[cm->addr_count++] = addr;
698         return len;
699 }
700
701 static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
702                          char *buf)
703 {
704         struct node *nd = to_node(i);
705         struct node_attribute *nda =
706                         container_of(a, struct node_attribute, attr);
707         return nda->show ? nda->show(nd, buf) : 0;
708 }
709
710 static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
711                           const char *buf, size_t len)
712 {
713         struct node *nd = to_node(i);
714         struct node_attribute *nda =
715                 container_of(a, struct node_attribute, attr);
716         return nda->store ? nda->store(nd, buf, len) : -EINVAL;
717 }
718
719 static ssize_t node_nodeid_read(struct node *nd, char *buf)
720 {
721         return sprintf(buf, "%d\n", nd->nodeid);
722 }
723
724 static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len)
725 {
726         nd->nodeid = simple_strtol(buf, NULL, 0);
727         return len;
728 }
729
730 static ssize_t node_weight_read(struct node *nd, char *buf)
731 {
732         return sprintf(buf, "%d\n", nd->weight);
733 }
734
735 static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len)
736 {
737         nd->weight = simple_strtol(buf, NULL, 0);
738         return len;
739 }
740
741 /*
742  * Functions for the dlm to get the info that's been configured
743  */
744
745 static struct space *get_space(char *name)
746 {
747         if (!space_list)
748                 return NULL;
749         return to_space(config_group_find_obj(space_list, name));
750 }
751
752 static void put_space(struct space *sp)
753 {
754         config_item_put(&sp->group.cg_item);
755 }
756
757 static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr)
758 {
759         struct config_item *i;
760         struct comm *cm = NULL;
761         int found = 0;
762
763         if (!comm_list)
764                 return NULL;
765
766         down(&clusters_root.subsys.su_sem);
767
768         list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
769                 cm = to_comm(i);
770
771                 if (nodeid) {
772                         if (cm->nodeid != nodeid)
773                                 continue;
774                         found = 1;
775                         break;
776                 } else {
777                         if (!cm->addr_count ||
778                             memcmp(cm->addr[0], addr, sizeof(*addr)))
779                                 continue;
780                         found = 1;
781                         break;
782                 }
783         }
784         up(&clusters_root.subsys.su_sem);
785
786         if (found)
787                 config_item_get(i);
788         else
789                 cm = NULL;
790         return cm;
791 }
792
793 static void put_comm(struct comm *cm)
794 {
795         config_item_put(&cm->item);
796 }
797
798 /* caller must free mem */
799 int dlm_nodeid_list(char *lsname, int **ids_out)
800 {
801         struct space *sp;
802         struct node *nd;
803         int i = 0, rv = 0;
804         int *ids;
805
806         sp = get_space(lsname);
807         if (!sp)
808                 return -EEXIST;
809
810         mutex_lock(&sp->members_lock);
811         if (!sp->members_count) {
812                 rv = 0;
813                 goto out;
814         }
815
816         ids = kcalloc(sp->members_count, sizeof(int), GFP_KERNEL);
817         if (!ids) {
818                 rv = -ENOMEM;
819                 goto out;
820         }
821
822         rv = sp->members_count;
823         list_for_each_entry(nd, &sp->members, list)
824                 ids[i++] = nd->nodeid;
825
826         if (rv != i)
827                 printk("bad nodeid count %d %d\n", rv, i);
828
829         *ids_out = ids;
830  out:
831         mutex_unlock(&sp->members_lock);
832         put_space(sp);
833         return rv;
834 }
835
836 int dlm_node_weight(char *lsname, int nodeid)
837 {
838         struct space *sp;
839         struct node *nd;
840         int w = -EEXIST;
841
842         sp = get_space(lsname);
843         if (!sp)
844                 goto out;
845
846         mutex_lock(&sp->members_lock);
847         list_for_each_entry(nd, &sp->members, list) {
848                 if (nd->nodeid != nodeid)
849                         continue;
850                 w = nd->weight;
851                 break;
852         }
853         mutex_unlock(&sp->members_lock);
854         put_space(sp);
855  out:
856         return w;
857 }
858
859 int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
860 {
861         struct comm *cm = get_comm(nodeid, NULL);
862         if (!cm)
863                 return -EEXIST;
864         if (!cm->addr_count)
865                 return -ENOENT;
866         memcpy(addr, cm->addr[0], sizeof(*addr));
867         put_comm(cm);
868         return 0;
869 }
870
871 int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
872 {
873         struct comm *cm = get_comm(0, addr);
874         if (!cm)
875                 return -EEXIST;
876         *nodeid = cm->nodeid;
877         put_comm(cm);
878         return 0;
879 }
880
881 int dlm_our_nodeid(void)
882 {
883         return local_comm ? local_comm->nodeid : 0;
884 }
885
886 /* num 0 is first addr, num 1 is second addr */
887 int dlm_our_addr(struct sockaddr_storage *addr, int num)
888 {
889         if (!local_comm)
890                 return -1;
891         if (num + 1 > local_comm->addr_count)
892                 return -1;
893         memcpy(addr, local_comm->addr[num], sizeof(*addr));
894         return 0;
895 }
896
897 /* Config file defaults */
898 #define DEFAULT_TCP_PORT       21064
899 #define DEFAULT_BUFFER_SIZE     4096
900 #define DEFAULT_RSBTBL_SIZE      256
901 #define DEFAULT_LKBTBL_SIZE     1024
902 #define DEFAULT_DIRTBL_SIZE      512
903 #define DEFAULT_RECOVER_TIMER      5
904 #define DEFAULT_TOSS_SECS         10
905 #define DEFAULT_SCAN_SECS          5
906 #define DEFAULT_LOG_DEBUG          0
907
908 struct dlm_config_info dlm_config = {
909         .ci_tcp_port = DEFAULT_TCP_PORT,
910         .ci_buffer_size = DEFAULT_BUFFER_SIZE,
911         .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
912         .ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,
913         .ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
914         .ci_recover_timer = DEFAULT_RECOVER_TIMER,
915         .ci_toss_secs = DEFAULT_TOSS_SECS,
916         .ci_scan_secs = DEFAULT_SCAN_SECS,
917         .ci_log_debug = DEFAULT_LOG_DEBUG
918 };
919