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