blob: 4e20f9317156bed5941d8ee769d453c0e604ce78 [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland51409342008-07-31 09:31:53 -05005** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00006**
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Teigland44be6fd2008-08-28 11:36:19 -050018#include <linux/in.h>
19#include <linux/in6.h>
20#include <net/ipv6.h>
David Teiglande7fd4172006-01-18 09:30:29 +000021#include <net/sock.h>
22
23#include "config.h"
David Teigland1c032c02006-04-28 10:50:41 -040024#include "lowcomms.h"
David Teiglande7fd4172006-01-18 09:30:29 +000025
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
Masatake YAMATO55b32862011-06-30 21:37:10 +090031 * /config/dlm/<cluster>/comms/<comm>/addr (write only)
32 * /config/dlm/<cluster>/comms/<comm>/addr_list (read only)
David Teiglande7fd4172006-01-18 09:30:29 +000033 * The <cluster> level is useless, but I haven't figured out how to avoid it.
34 */
35
36static struct config_group *space_list;
37static struct config_group *comm_list;
David Teigland51409342008-07-31 09:31:53 -050038static struct dlm_comm *local_comm;
David Teiglande7fd4172006-01-18 09:30:29 +000039
David Teigland51409342008-07-31 09:31:53 -050040struct dlm_clusters;
41struct dlm_cluster;
42struct dlm_spaces;
43struct dlm_space;
44struct dlm_comms;
45struct dlm_comm;
46struct dlm_nodes;
47struct dlm_node;
David Teiglande7fd4172006-01-18 09:30:29 +000048
Joel Beckerf89ab862008-07-17 14:53:48 -070049static struct config_group *make_cluster(struct config_group *, const char *);
David Teiglande7fd4172006-01-18 09:30:29 +000050static void drop_cluster(struct config_group *, struct config_item *);
51static void release_cluster(struct config_item *);
Joel Beckerf89ab862008-07-17 14:53:48 -070052static struct config_group *make_space(struct config_group *, const char *);
David Teiglande7fd4172006-01-18 09:30:29 +000053static void drop_space(struct config_group *, struct config_item *);
54static void release_space(struct config_item *);
Joel Beckerf89ab862008-07-17 14:53:48 -070055static struct config_item *make_comm(struct config_group *, const char *);
David Teiglande7fd4172006-01-18 09:30:29 +000056static void drop_comm(struct config_group *, struct config_item *);
57static void release_comm(struct config_item *);
Joel Beckerf89ab862008-07-17 14:53:48 -070058static struct config_item *make_node(struct config_group *, const char *);
David Teiglande7fd4172006-01-18 09:30:29 +000059static void drop_node(struct config_group *, struct config_item *);
60static void release_node(struct config_item *);
61
David Teiglandd2007782007-01-09 09:46:02 -060062static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
63 char *buf);
64static ssize_t store_cluster(struct config_item *i,
65 struct configfs_attribute *a,
66 const char *buf, size_t len);
David Teiglande7fd4172006-01-18 09:30:29 +000067static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
68 char *buf);
69static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
70 const char *buf, size_t len);
71static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
72 char *buf);
73static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
74 const char *buf, size_t len);
75
David Teigland51409342008-07-31 09:31:53 -050076static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf);
77static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
78 size_t len);
79static ssize_t comm_local_read(struct dlm_comm *cm, char *buf);
80static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
81 size_t len);
82static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf,
83 size_t len);
Masatake YAMATO55b32862011-06-30 21:37:10 +090084static ssize_t comm_addr_list_read(struct dlm_comm *cm, char *buf);
David Teigland51409342008-07-31 09:31:53 -050085static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf);
86static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
87 size_t len);
88static ssize_t node_weight_read(struct dlm_node *nd, char *buf);
89static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
90 size_t len);
David Teiglande7fd4172006-01-18 09:30:29 +000091
David Teigland51409342008-07-31 09:31:53 -050092struct dlm_cluster {
David Teiglandd2007782007-01-09 09:46:02 -060093 struct config_group group;
94 unsigned int cl_tcp_port;
95 unsigned int cl_buffer_size;
96 unsigned int cl_rsbtbl_size;
David Teiglandd2007782007-01-09 09:46:02 -060097 unsigned int cl_dirtbl_size;
98 unsigned int cl_recover_timer;
99 unsigned int cl_toss_secs;
100 unsigned int cl_scan_secs;
101 unsigned int cl_log_debug;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100102 unsigned int cl_protocol;
David Teigland3ae1acf2007-05-18 08:59:31 -0500103 unsigned int cl_timewarn_cs;
David Teiglandc6ff6692011-03-28 14:17:26 -0500104 unsigned int cl_waitwarn_us;
David Teiglandd2007782007-01-09 09:46:02 -0600105};
106
107enum {
108 CLUSTER_ATTR_TCP_PORT = 0,
109 CLUSTER_ATTR_BUFFER_SIZE,
110 CLUSTER_ATTR_RSBTBL_SIZE,
David Teiglandd2007782007-01-09 09:46:02 -0600111 CLUSTER_ATTR_DIRTBL_SIZE,
112 CLUSTER_ATTR_RECOVER_TIMER,
113 CLUSTER_ATTR_TOSS_SECS,
114 CLUSTER_ATTR_SCAN_SECS,
115 CLUSTER_ATTR_LOG_DEBUG,
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100116 CLUSTER_ATTR_PROTOCOL,
David Teigland3ae1acf2007-05-18 08:59:31 -0500117 CLUSTER_ATTR_TIMEWARN_CS,
David Teiglandc6ff6692011-03-28 14:17:26 -0500118 CLUSTER_ATTR_WAITWARN_US,
David Teiglandd2007782007-01-09 09:46:02 -0600119};
120
121struct cluster_attribute {
122 struct configfs_attribute attr;
David Teigland51409342008-07-31 09:31:53 -0500123 ssize_t (*show)(struct dlm_cluster *, char *);
124 ssize_t (*store)(struct dlm_cluster *, const char *, size_t);
David Teiglandd2007782007-01-09 09:46:02 -0600125};
126
David Teigland51409342008-07-31 09:31:53 -0500127static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
Harvey Harrison5416b702008-02-13 16:54:29 -0800128 int *info_field, int check_zero,
David Teiglandd2007782007-01-09 09:46:02 -0600129 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
David Teiglandd2007782007-01-09 09:46:02 -0600147#define CLUSTER_ATTR(name, check_zero) \
David Teigland51409342008-07-31 09:31:53 -0500148static ssize_t name##_write(struct dlm_cluster *cl, const char *buf, size_t len) \
David Teiglandd2007782007-01-09 09:46:02 -0600149{ \
150 return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
151 check_zero, buf, len); \
152} \
David Teigland51409342008-07-31 09:31:53 -0500153static ssize_t name##_read(struct dlm_cluster *cl, char *buf) \
David Teiglandd2007782007-01-09 09:46:02 -0600154{ \
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);
David Teiglandd2007782007-01-09 09:46:02 -0600163CLUSTER_ATTR(dirtbl_size, 1);
164CLUSTER_ATTR(recover_timer, 1);
165CLUSTER_ATTR(toss_secs, 1);
166CLUSTER_ATTR(scan_secs, 1);
167CLUSTER_ATTR(log_debug, 0);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100168CLUSTER_ATTR(protocol, 0);
David Teigland3ae1acf2007-05-18 08:59:31 -0500169CLUSTER_ATTR(timewarn_cs, 1);
David Teiglandc6ff6692011-03-28 14:17:26 -0500170CLUSTER_ATTR(waitwarn_us, 0);
David Teiglandd2007782007-01-09 09:46:02 -0600171
172static struct configfs_attribute *cluster_attrs[] = {
173 [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
174 [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr,
175 [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr,
David Teiglandd2007782007-01-09 09:46:02 -0600176 [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr,
177 [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr,
178 [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr,
179 [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr,
180 [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr,
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100181 [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
David Teigland3ae1acf2007-05-18 08:59:31 -0500182 [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
David Teiglandc6ff6692011-03-28 14:17:26 -0500183 [CLUSTER_ATTR_WAITWARN_US] = &cluster_attr_waitwarn_us.attr,
David Teiglandd2007782007-01-09 09:46:02 -0600184 NULL,
185};
186
David Teiglande7fd4172006-01-18 09:30:29 +0000187enum {
188 COMM_ATTR_NODEID = 0,
189 COMM_ATTR_LOCAL,
190 COMM_ATTR_ADDR,
Masatake YAMATO55b32862011-06-30 21:37:10 +0900191 COMM_ATTR_ADDR_LIST,
David Teiglande7fd4172006-01-18 09:30:29 +0000192};
193
194struct comm_attribute {
195 struct configfs_attribute attr;
David Teigland51409342008-07-31 09:31:53 -0500196 ssize_t (*show)(struct dlm_comm *, char *);
197 ssize_t (*store)(struct dlm_comm *, const char *, size_t);
David Teiglande7fd4172006-01-18 09:30:29 +0000198};
199
200static struct comm_attribute comm_attr_nodeid = {
201 .attr = { .ca_owner = THIS_MODULE,
202 .ca_name = "nodeid",
203 .ca_mode = S_IRUGO | S_IWUSR },
204 .show = comm_nodeid_read,
205 .store = comm_nodeid_write,
206};
207
208static struct comm_attribute comm_attr_local = {
209 .attr = { .ca_owner = THIS_MODULE,
210 .ca_name = "local",
211 .ca_mode = S_IRUGO | S_IWUSR },
212 .show = comm_local_read,
213 .store = comm_local_write,
214};
215
216static struct comm_attribute comm_attr_addr = {
217 .attr = { .ca_owner = THIS_MODULE,
218 .ca_name = "addr",
Masatake YAMATO55b32862011-06-30 21:37:10 +0900219 .ca_mode = S_IWUSR },
David Teiglande7fd4172006-01-18 09:30:29 +0000220 .store = comm_addr_write,
221};
222
Masatake YAMATO55b32862011-06-30 21:37:10 +0900223static struct comm_attribute comm_attr_addr_list = {
224 .attr = { .ca_owner = THIS_MODULE,
225 .ca_name = "addr_list",
226 .ca_mode = S_IRUGO },
227 .show = comm_addr_list_read,
228};
229
David Teiglande7fd4172006-01-18 09:30:29 +0000230static struct configfs_attribute *comm_attrs[] = {
231 [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr,
232 [COMM_ATTR_LOCAL] = &comm_attr_local.attr,
233 [COMM_ATTR_ADDR] = &comm_attr_addr.attr,
Masatake YAMATO55b32862011-06-30 21:37:10 +0900234 [COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list.attr,
David Teiglande7fd4172006-01-18 09:30:29 +0000235 NULL,
236};
237
238enum {
239 NODE_ATTR_NODEID = 0,
240 NODE_ATTR_WEIGHT,
241};
242
243struct node_attribute {
244 struct configfs_attribute attr;
David Teigland51409342008-07-31 09:31:53 -0500245 ssize_t (*show)(struct dlm_node *, char *);
246 ssize_t (*store)(struct dlm_node *, const char *, size_t);
David Teiglande7fd4172006-01-18 09:30:29 +0000247};
248
249static struct node_attribute node_attr_nodeid = {
250 .attr = { .ca_owner = THIS_MODULE,
251 .ca_name = "nodeid",
252 .ca_mode = S_IRUGO | S_IWUSR },
253 .show = node_nodeid_read,
254 .store = node_nodeid_write,
255};
256
257static struct node_attribute node_attr_weight = {
258 .attr = { .ca_owner = THIS_MODULE,
259 .ca_name = "weight",
260 .ca_mode = S_IRUGO | S_IWUSR },
261 .show = node_weight_read,
262 .store = node_weight_write,
263};
264
265static struct configfs_attribute *node_attrs[] = {
266 [NODE_ATTR_NODEID] = &node_attr_nodeid.attr,
267 [NODE_ATTR_WEIGHT] = &node_attr_weight.attr,
268 NULL,
269};
270
David Teigland51409342008-07-31 09:31:53 -0500271struct dlm_clusters {
David Teiglande7fd4172006-01-18 09:30:29 +0000272 struct configfs_subsystem subsys;
273};
274
David Teigland51409342008-07-31 09:31:53 -0500275struct dlm_spaces {
David Teiglande7fd4172006-01-18 09:30:29 +0000276 struct config_group ss_group;
277};
278
David Teigland51409342008-07-31 09:31:53 -0500279struct dlm_space {
David Teiglande7fd4172006-01-18 09:30:29 +0000280 struct config_group group;
281 struct list_head members;
David Teigland90135922006-01-20 08:47:07 +0000282 struct mutex members_lock;
David Teiglande7fd4172006-01-18 09:30:29 +0000283 int members_count;
284};
285
David Teigland51409342008-07-31 09:31:53 -0500286struct dlm_comms {
David Teiglande7fd4172006-01-18 09:30:29 +0000287 struct config_group cs_group;
288};
289
David Teigland51409342008-07-31 09:31:53 -0500290struct dlm_comm {
David Teiglande7fd4172006-01-18 09:30:29 +0000291 struct config_item item;
292 int nodeid;
293 int local;
294 int addr_count;
295 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
296};
297
David Teigland51409342008-07-31 09:31:53 -0500298struct dlm_nodes {
David Teiglande7fd4172006-01-18 09:30:29 +0000299 struct config_group ns_group;
300};
301
David Teigland51409342008-07-31 09:31:53 -0500302struct dlm_node {
David Teiglande7fd4172006-01-18 09:30:29 +0000303 struct config_item item;
304 struct list_head list; /* space->members */
305 int nodeid;
306 int weight;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500307 int new;
David Teiglande7fd4172006-01-18 09:30:29 +0000308};
309
310static struct configfs_group_operations clusters_ops = {
311 .make_group = make_cluster,
312 .drop_item = drop_cluster,
313};
314
315static struct configfs_item_operations cluster_ops = {
316 .release = release_cluster,
David Teiglandd2007782007-01-09 09:46:02 -0600317 .show_attribute = show_cluster,
318 .store_attribute = store_cluster,
David Teiglande7fd4172006-01-18 09:30:29 +0000319};
320
321static struct configfs_group_operations spaces_ops = {
322 .make_group = make_space,
323 .drop_item = drop_space,
324};
325
326static struct configfs_item_operations space_ops = {
327 .release = release_space,
328};
329
330static struct configfs_group_operations comms_ops = {
331 .make_item = make_comm,
332 .drop_item = drop_comm,
333};
334
335static struct configfs_item_operations comm_ops = {
336 .release = release_comm,
337 .show_attribute = show_comm,
338 .store_attribute = store_comm,
339};
340
341static struct configfs_group_operations nodes_ops = {
342 .make_item = make_node,
343 .drop_item = drop_node,
344};
345
346static struct configfs_item_operations node_ops = {
347 .release = release_node,
348 .show_attribute = show_node,
349 .store_attribute = store_node,
350};
351
352static struct config_item_type clusters_type = {
353 .ct_group_ops = &clusters_ops,
354 .ct_owner = THIS_MODULE,
355};
356
357static struct config_item_type cluster_type = {
358 .ct_item_ops = &cluster_ops,
David Teiglandd2007782007-01-09 09:46:02 -0600359 .ct_attrs = cluster_attrs,
David Teiglande7fd4172006-01-18 09:30:29 +0000360 .ct_owner = THIS_MODULE,
361};
362
363static struct config_item_type spaces_type = {
364 .ct_group_ops = &spaces_ops,
365 .ct_owner = THIS_MODULE,
366};
367
368static struct config_item_type space_type = {
369 .ct_item_ops = &space_ops,
370 .ct_owner = THIS_MODULE,
371};
372
373static struct config_item_type comms_type = {
374 .ct_group_ops = &comms_ops,
375 .ct_owner = THIS_MODULE,
376};
377
378static struct config_item_type comm_type = {
379 .ct_item_ops = &comm_ops,
380 .ct_attrs = comm_attrs,
381 .ct_owner = THIS_MODULE,
382};
383
384static struct config_item_type nodes_type = {
385 .ct_group_ops = &nodes_ops,
386 .ct_owner = THIS_MODULE,
387};
388
389static struct config_item_type node_type = {
390 .ct_item_ops = &node_ops,
391 .ct_attrs = node_attrs,
392 .ct_owner = THIS_MODULE,
393};
394
Andrew Morton27eccf42008-09-05 08:42:08 -0500395static struct dlm_cluster *config_item_to_cluster(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000396{
David Teigland51409342008-07-31 09:31:53 -0500397 return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
398 NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000399}
400
Andrew Morton27eccf42008-09-05 08:42:08 -0500401static struct dlm_space *config_item_to_space(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000402{
David Teigland51409342008-07-31 09:31:53 -0500403 return i ? container_of(to_config_group(i), struct dlm_space, group) :
404 NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000405}
406
Andrew Morton27eccf42008-09-05 08:42:08 -0500407static struct dlm_comm *config_item_to_comm(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000408{
David Teigland51409342008-07-31 09:31:53 -0500409 return i ? container_of(i, struct dlm_comm, item) : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000410}
411
Andrew Morton27eccf42008-09-05 08:42:08 -0500412static struct dlm_node *config_item_to_node(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000413{
David Teigland51409342008-07-31 09:31:53 -0500414 return i ? container_of(i, struct dlm_node, item) : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000415}
416
Joel Beckerf89ab862008-07-17 14:53:48 -0700417static struct config_group *make_cluster(struct config_group *g,
418 const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000419{
David Teigland51409342008-07-31 09:31:53 -0500420 struct dlm_cluster *cl = NULL;
421 struct dlm_spaces *sps = NULL;
422 struct dlm_comms *cms = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000423 void *gps = NULL;
424
David Teigland573c24c2009-11-30 16:34:43 -0600425 cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
426 gps = kcalloc(3, sizeof(struct config_group *), GFP_NOFS);
427 sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
428 cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000429
430 if (!cl || !gps || !sps || !cms)
431 goto fail;
432
433 config_group_init_type_name(&cl->group, name, &cluster_type);
434 config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
435 config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
436
437 cl->group.default_groups = gps;
438 cl->group.default_groups[0] = &sps->ss_group;
439 cl->group.default_groups[1] = &cms->cs_group;
440 cl->group.default_groups[2] = NULL;
441
David Teiglandd2007782007-01-09 09:46:02 -0600442 cl->cl_tcp_port = dlm_config.ci_tcp_port;
443 cl->cl_buffer_size = dlm_config.ci_buffer_size;
444 cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
David Teiglandd2007782007-01-09 09:46:02 -0600445 cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size;
446 cl->cl_recover_timer = dlm_config.ci_recover_timer;
447 cl->cl_toss_secs = dlm_config.ci_toss_secs;
448 cl->cl_scan_secs = dlm_config.ci_scan_secs;
449 cl->cl_log_debug = dlm_config.ci_log_debug;
David Teigland0b7cac02007-05-29 08:47:51 -0500450 cl->cl_protocol = dlm_config.ci_protocol;
David Teigland84d8cd62007-05-29 08:44:23 -0500451 cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
David Teiglandc6ff6692011-03-28 14:17:26 -0500452 cl->cl_waitwarn_us = dlm_config.ci_waitwarn_us;
David Teiglandd2007782007-01-09 09:46:02 -0600453
David Teiglande7fd4172006-01-18 09:30:29 +0000454 space_list = &sps->ss_group;
455 comm_list = &cms->cs_group;
Joel Beckerf89ab862008-07-17 14:53:48 -0700456 return &cl->group;
David Teiglande7fd4172006-01-18 09:30:29 +0000457
458 fail:
459 kfree(cl);
460 kfree(gps);
461 kfree(sps);
462 kfree(cms);
Joel Beckera6795e92008-07-17 15:21:29 -0700463 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000464}
465
466static void drop_cluster(struct config_group *g, struct config_item *i)
467{
Andrew Morton27eccf42008-09-05 08:42:08 -0500468 struct dlm_cluster *cl = config_item_to_cluster(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000469 struct config_item *tmp;
470 int j;
471
472 for (j = 0; cl->group.default_groups[j]; j++) {
473 tmp = &cl->group.default_groups[j]->cg_item;
474 cl->group.default_groups[j] = NULL;
475 config_item_put(tmp);
476 }
477
478 space_list = NULL;
479 comm_list = NULL;
480
481 config_item_put(i);
482}
483
484static void release_cluster(struct config_item *i)
485{
Andrew Morton27eccf42008-09-05 08:42:08 -0500486 struct dlm_cluster *cl = config_item_to_cluster(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000487 kfree(cl->group.default_groups);
488 kfree(cl);
489}
490
Joel Beckerf89ab862008-07-17 14:53:48 -0700491static struct config_group *make_space(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000492{
David Teigland51409342008-07-31 09:31:53 -0500493 struct dlm_space *sp = NULL;
494 struct dlm_nodes *nds = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000495 void *gps = NULL;
496
David Teigland573c24c2009-11-30 16:34:43 -0600497 sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
498 gps = kcalloc(2, sizeof(struct config_group *), GFP_NOFS);
499 nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000500
501 if (!sp || !gps || !nds)
502 goto fail;
503
504 config_group_init_type_name(&sp->group, name, &space_type);
505 config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
506
507 sp->group.default_groups = gps;
508 sp->group.default_groups[0] = &nds->ns_group;
509 sp->group.default_groups[1] = NULL;
510
511 INIT_LIST_HEAD(&sp->members);
David Teigland90135922006-01-20 08:47:07 +0000512 mutex_init(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000513 sp->members_count = 0;
Joel Beckerf89ab862008-07-17 14:53:48 -0700514 return &sp->group;
David Teiglande7fd4172006-01-18 09:30:29 +0000515
516 fail:
517 kfree(sp);
518 kfree(gps);
519 kfree(nds);
Joel Beckera6795e92008-07-17 15:21:29 -0700520 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000521}
522
523static void drop_space(struct config_group *g, struct config_item *i)
524{
Andrew Morton27eccf42008-09-05 08:42:08 -0500525 struct dlm_space *sp = config_item_to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000526 struct config_item *tmp;
527 int j;
528
529 /* assert list_empty(&sp->members) */
530
531 for (j = 0; sp->group.default_groups[j]; j++) {
532 tmp = &sp->group.default_groups[j]->cg_item;
533 sp->group.default_groups[j] = NULL;
534 config_item_put(tmp);
535 }
536
537 config_item_put(i);
538}
539
540static void release_space(struct config_item *i)
541{
Andrew Morton27eccf42008-09-05 08:42:08 -0500542 struct dlm_space *sp = config_item_to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000543 kfree(sp->group.default_groups);
544 kfree(sp);
545}
546
Joel Beckerf89ab862008-07-17 14:53:48 -0700547static struct config_item *make_comm(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000548{
David Teigland51409342008-07-31 09:31:53 -0500549 struct dlm_comm *cm;
David Teiglande7fd4172006-01-18 09:30:29 +0000550
David Teigland573c24c2009-11-30 16:34:43 -0600551 cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000552 if (!cm)
Joel Beckera6795e92008-07-17 15:21:29 -0700553 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000554
555 config_item_init_type_name(&cm->item, name, &comm_type);
556 cm->nodeid = -1;
557 cm->local = 0;
558 cm->addr_count = 0;
Joel Beckerf89ab862008-07-17 14:53:48 -0700559 return &cm->item;
David Teiglande7fd4172006-01-18 09:30:29 +0000560}
561
562static void drop_comm(struct config_group *g, struct config_item *i)
563{
Andrew Morton27eccf42008-09-05 08:42:08 -0500564 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000565 if (local_comm == cm)
566 local_comm = NULL;
David Teigland1c032c02006-04-28 10:50:41 -0400567 dlm_lowcomms_close(cm->nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000568 while (cm->addr_count--)
569 kfree(cm->addr[cm->addr_count]);
570 config_item_put(i);
571}
572
573static void release_comm(struct config_item *i)
574{
Andrew Morton27eccf42008-09-05 08:42:08 -0500575 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000576 kfree(cm);
577}
578
Joel Beckerf89ab862008-07-17 14:53:48 -0700579static struct config_item *make_node(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000580{
Andrew Morton27eccf42008-09-05 08:42:08 -0500581 struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
David Teigland51409342008-07-31 09:31:53 -0500582 struct dlm_node *nd;
David Teiglande7fd4172006-01-18 09:30:29 +0000583
David Teigland573c24c2009-11-30 16:34:43 -0600584 nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000585 if (!nd)
Joel Beckera6795e92008-07-17 15:21:29 -0700586 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000587
588 config_item_init_type_name(&nd->item, name, &node_type);
589 nd->nodeid = -1;
590 nd->weight = 1; /* default weight of 1 if none is set */
David Teiglandd44e0fc2008-03-18 14:22:11 -0500591 nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
David Teiglande7fd4172006-01-18 09:30:29 +0000592
David Teigland90135922006-01-20 08:47:07 +0000593 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000594 list_add(&nd->list, &sp->members);
595 sp->members_count++;
David Teigland90135922006-01-20 08:47:07 +0000596 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000597
Joel Beckerf89ab862008-07-17 14:53:48 -0700598 return &nd->item;
David Teiglande7fd4172006-01-18 09:30:29 +0000599}
600
601static void drop_node(struct config_group *g, struct config_item *i)
602{
Andrew Morton27eccf42008-09-05 08:42:08 -0500603 struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
604 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000605
David Teigland90135922006-01-20 08:47:07 +0000606 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000607 list_del(&nd->list);
608 sp->members_count--;
David Teigland90135922006-01-20 08:47:07 +0000609 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000610
611 config_item_put(i);
612}
613
614static void release_node(struct config_item *i)
615{
Andrew Morton27eccf42008-09-05 08:42:08 -0500616 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000617 kfree(nd);
618}
619
David Teigland51409342008-07-31 09:31:53 -0500620static struct dlm_clusters clusters_root = {
David Teiglande7fd4172006-01-18 09:30:29 +0000621 .subsys = {
622 .su_group = {
623 .cg_item = {
624 .ci_namebuf = "dlm",
625 .ci_type = &clusters_type,
626 },
627 },
628 },
629};
630
Denis Cheng30727172008-02-02 01:53:46 +0800631int __init dlm_config_init(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000632{
633 config_group_init(&clusters_root.subsys.su_group);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700634 mutex_init(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000635 return configfs_register_subsystem(&clusters_root.subsys);
636}
637
638void dlm_config_exit(void)
639{
640 configfs_unregister_subsystem(&clusters_root.subsys);
641}
642
643/*
644 * Functions for user space to read/write attributes
645 */
646
David Teiglandd2007782007-01-09 09:46:02 -0600647static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,
648 char *buf)
649{
Andrew Morton27eccf42008-09-05 08:42:08 -0500650 struct dlm_cluster *cl = config_item_to_cluster(i);
David Teiglandd2007782007-01-09 09:46:02 -0600651 struct cluster_attribute *cla =
652 container_of(a, struct cluster_attribute, attr);
653 return cla->show ? cla->show(cl, buf) : 0;
654}
655
656static ssize_t store_cluster(struct config_item *i,
657 struct configfs_attribute *a,
658 const char *buf, size_t len)
659{
Andrew Morton27eccf42008-09-05 08:42:08 -0500660 struct dlm_cluster *cl = config_item_to_cluster(i);
David Teiglandd2007782007-01-09 09:46:02 -0600661 struct cluster_attribute *cla =
662 container_of(a, struct cluster_attribute, attr);
663 return cla->store ? cla->store(cl, buf, len) : -EINVAL;
664}
665
David Teiglande7fd4172006-01-18 09:30:29 +0000666static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,
667 char *buf)
668{
Andrew Morton27eccf42008-09-05 08:42:08 -0500669 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000670 struct comm_attribute *cma =
671 container_of(a, struct comm_attribute, attr);
672 return cma->show ? cma->show(cm, buf) : 0;
673}
674
675static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,
676 const char *buf, size_t len)
677{
Andrew Morton27eccf42008-09-05 08:42:08 -0500678 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000679 struct comm_attribute *cma =
680 container_of(a, struct comm_attribute, attr);
681 return cma->store ? cma->store(cm, buf, len) : -EINVAL;
682}
683
David Teigland51409342008-07-31 09:31:53 -0500684static ssize_t comm_nodeid_read(struct dlm_comm *cm, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000685{
686 return sprintf(buf, "%d\n", cm->nodeid);
687}
688
David Teigland51409342008-07-31 09:31:53 -0500689static ssize_t comm_nodeid_write(struct dlm_comm *cm, const char *buf,
690 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000691{
692 cm->nodeid = simple_strtol(buf, NULL, 0);
693 return len;
694}
695
David Teigland51409342008-07-31 09:31:53 -0500696static ssize_t comm_local_read(struct dlm_comm *cm, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000697{
698 return sprintf(buf, "%d\n", cm->local);
699}
700
David Teigland51409342008-07-31 09:31:53 -0500701static ssize_t comm_local_write(struct dlm_comm *cm, const char *buf,
702 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000703{
704 cm->local= simple_strtol(buf, NULL, 0);
705 if (cm->local && !local_comm)
706 local_comm = cm;
707 return len;
708}
709
David Teigland51409342008-07-31 09:31:53 -0500710static ssize_t comm_addr_write(struct dlm_comm *cm, const char *buf, size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000711{
712 struct sockaddr_storage *addr;
713
714 if (len != sizeof(struct sockaddr_storage))
715 return -EINVAL;
716
717 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
718 return -ENOSPC;
719
David Teigland573c24c2009-11-30 16:34:43 -0600720 addr = kzalloc(sizeof(*addr), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000721 if (!addr)
722 return -ENOMEM;
723
724 memcpy(addr, buf, len);
725 cm->addr[cm->addr_count++] = addr;
726 return len;
727}
728
Masatake YAMATO55b32862011-06-30 21:37:10 +0900729static ssize_t comm_addr_list_read(struct dlm_comm *cm, char *buf)
730{
731 ssize_t s;
732 ssize_t allowance;
733 int i;
734 struct sockaddr_storage *addr;
735 struct sockaddr_in *addr_in;
736 struct sockaddr_in6 *addr_in6;
737
738 /* Taken from ip6_addr_string() defined in lib/vsprintf.c */
739 char buf0[sizeof("AF_INET6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
740
741
742 /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
743 allowance = 4096;
744 buf[0] = '\0';
745
746 for (i = 0; i < cm->addr_count; i++) {
747 addr = cm->addr[i];
748
749 switch(addr->ss_family) {
750 case AF_INET:
751 addr_in = (struct sockaddr_in *)addr;
752 s = sprintf(buf0, "AF_INET %pI4\n", &addr_in->sin_addr.s_addr);
753 break;
754 case AF_INET6:
755 addr_in6 = (struct sockaddr_in6 *)addr;
756 s = sprintf(buf0, "AF_INET6 %pI6\n", &addr_in6->sin6_addr);
757 break;
758 default:
759 s = sprintf(buf0, "%s\n", "<UNKNOWN>");
760 break;
761 }
762 allowance -= s;
763 if (allowance >= 0)
764 strcat(buf, buf0);
765 else {
766 allowance += s;
767 break;
768 }
769 }
770 return 4096 - allowance;
771}
772
David Teiglande7fd4172006-01-18 09:30:29 +0000773static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,
774 char *buf)
775{
Andrew Morton27eccf42008-09-05 08:42:08 -0500776 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000777 struct node_attribute *nda =
778 container_of(a, struct node_attribute, attr);
779 return nda->show ? nda->show(nd, buf) : 0;
780}
781
782static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,
783 const char *buf, size_t len)
784{
Andrew Morton27eccf42008-09-05 08:42:08 -0500785 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000786 struct node_attribute *nda =
787 container_of(a, struct node_attribute, attr);
788 return nda->store ? nda->store(nd, buf, len) : -EINVAL;
789}
790
David Teigland51409342008-07-31 09:31:53 -0500791static ssize_t node_nodeid_read(struct dlm_node *nd, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000792{
793 return sprintf(buf, "%d\n", nd->nodeid);
794}
795
David Teigland51409342008-07-31 09:31:53 -0500796static ssize_t node_nodeid_write(struct dlm_node *nd, const char *buf,
797 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000798{
799 nd->nodeid = simple_strtol(buf, NULL, 0);
800 return len;
801}
802
David Teigland51409342008-07-31 09:31:53 -0500803static ssize_t node_weight_read(struct dlm_node *nd, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000804{
805 return sprintf(buf, "%d\n", nd->weight);
806}
807
David Teigland51409342008-07-31 09:31:53 -0500808static ssize_t node_weight_write(struct dlm_node *nd, const char *buf,
809 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000810{
811 nd->weight = simple_strtol(buf, NULL, 0);
812 return len;
813}
814
815/*
816 * Functions for the dlm to get the info that's been configured
817 */
818
David Teigland51409342008-07-31 09:31:53 -0500819static struct dlm_space *get_space(char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000820{
Satyam Sharma3168b072007-05-08 09:18:58 +0100821 struct config_item *i;
822
David Teiglande7fd4172006-01-18 09:30:29 +0000823 if (!space_list)
824 return NULL;
Satyam Sharma3168b072007-05-08 09:18:58 +0100825
Joel Beckere6bd07a2007-07-06 23:33:17 -0700826 mutex_lock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530827 i = config_group_find_item(space_list, name);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700828 mutex_unlock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3168b072007-05-08 09:18:58 +0100829
Andrew Morton27eccf42008-09-05 08:42:08 -0500830 return config_item_to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000831}
832
David Teigland51409342008-07-31 09:31:53 -0500833static void put_space(struct dlm_space *sp)
David Teiglande7fd4172006-01-18 09:30:29 +0000834{
835 config_item_put(&sp->group.cg_item);
836}
837
David Teigland44be6fd2008-08-28 11:36:19 -0500838static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
839{
840 switch (x->ss_family) {
841 case AF_INET: {
842 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
843 struct sockaddr_in *siny = (struct sockaddr_in *)y;
844 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
845 return 0;
846 if (sinx->sin_port != siny->sin_port)
847 return 0;
848 break;
849 }
850 case AF_INET6: {
851 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
852 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
853 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
854 return 0;
855 if (sinx->sin6_port != siny->sin6_port)
856 return 0;
857 break;
858 }
859 default:
860 return 0;
861 }
862 return 1;
863}
864
David Teigland51409342008-07-31 09:31:53 -0500865static struct dlm_comm *get_comm(int nodeid, struct sockaddr_storage *addr)
David Teiglande7fd4172006-01-18 09:30:29 +0000866{
867 struct config_item *i;
David Teigland51409342008-07-31 09:31:53 -0500868 struct dlm_comm *cm = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000869 int found = 0;
870
871 if (!comm_list)
872 return NULL;
873
Joel Beckere6bd07a2007-07-06 23:33:17 -0700874 mutex_lock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000875
876 list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
Andrew Morton27eccf42008-09-05 08:42:08 -0500877 cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000878
879 if (nodeid) {
880 if (cm->nodeid != nodeid)
881 continue;
882 found = 1;
Satyam Sharma3168b072007-05-08 09:18:58 +0100883 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000884 break;
885 } else {
David Teigland44be6fd2008-08-28 11:36:19 -0500886 if (!cm->addr_count || !addr_compare(cm->addr[0], addr))
David Teiglande7fd4172006-01-18 09:30:29 +0000887 continue;
888 found = 1;
Satyam Sharma3168b072007-05-08 09:18:58 +0100889 config_item_get(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000890 break;
891 }
892 }
Joel Beckere6bd07a2007-07-06 23:33:17 -0700893 mutex_unlock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000894
Satyam Sharma3168b072007-05-08 09:18:58 +0100895 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +0000896 cm = NULL;
897 return cm;
898}
899
David Teigland51409342008-07-31 09:31:53 -0500900static void put_comm(struct dlm_comm *cm)
David Teiglande7fd4172006-01-18 09:30:29 +0000901{
902 config_item_put(&cm->item);
903}
904
905/* caller must free mem */
David Teiglandd44e0fc2008-03-18 14:22:11 -0500906int dlm_nodeid_list(char *lsname, int **ids_out, int *ids_count_out,
907 int **new_out, int *new_count_out)
David Teiglande7fd4172006-01-18 09:30:29 +0000908{
David Teigland51409342008-07-31 09:31:53 -0500909 struct dlm_space *sp;
910 struct dlm_node *nd;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500911 int i = 0, rv = 0, ids_count = 0, new_count = 0;
912 int *ids, *new;
David Teiglande7fd4172006-01-18 09:30:29 +0000913
914 sp = get_space(lsname);
915 if (!sp)
916 return -EEXIST;
917
David Teigland90135922006-01-20 08:47:07 +0000918 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000919 if (!sp->members_count) {
David Teiglandd44e0fc2008-03-18 14:22:11 -0500920 rv = -EINVAL;
921 printk(KERN_ERR "dlm: zero members_count\n");
David Teiglande7fd4172006-01-18 09:30:29 +0000922 goto out;
923 }
924
David Teiglandd44e0fc2008-03-18 14:22:11 -0500925 ids_count = sp->members_count;
926
David Teigland573c24c2009-11-30 16:34:43 -0600927 ids = kcalloc(ids_count, sizeof(int), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000928 if (!ids) {
929 rv = -ENOMEM;
930 goto out;
931 }
932
David Teiglandd44e0fc2008-03-18 14:22:11 -0500933 list_for_each_entry(nd, &sp->members, list) {
David Teiglande7fd4172006-01-18 09:30:29 +0000934 ids[i++] = nd->nodeid;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500935 if (nd->new)
936 new_count++;
937 }
David Teiglande7fd4172006-01-18 09:30:29 +0000938
David Teiglandd44e0fc2008-03-18 14:22:11 -0500939 if (ids_count != i)
940 printk(KERN_ERR "dlm: bad nodeid count %d %d\n", ids_count, i);
David Teiglande7fd4172006-01-18 09:30:29 +0000941
David Teiglandd44e0fc2008-03-18 14:22:11 -0500942 if (!new_count)
943 goto out_ids;
944
David Teigland573c24c2009-11-30 16:34:43 -0600945 new = kcalloc(new_count, sizeof(int), GFP_NOFS);
David Teiglandd44e0fc2008-03-18 14:22:11 -0500946 if (!new) {
947 kfree(ids);
948 rv = -ENOMEM;
949 goto out;
950 }
951
952 i = 0;
953 list_for_each_entry(nd, &sp->members, list) {
954 if (nd->new) {
955 new[i++] = nd->nodeid;
956 nd->new = 0;
957 }
958 }
959 *new_count_out = new_count;
960 *new_out = new;
961
962 out_ids:
963 *ids_count_out = ids_count;
David Teiglande7fd4172006-01-18 09:30:29 +0000964 *ids_out = ids;
965 out:
David Teigland90135922006-01-20 08:47:07 +0000966 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000967 put_space(sp);
968 return rv;
969}
970
971int dlm_node_weight(char *lsname, int nodeid)
972{
David Teigland51409342008-07-31 09:31:53 -0500973 struct dlm_space *sp;
974 struct dlm_node *nd;
David Teiglande7fd4172006-01-18 09:30:29 +0000975 int w = -EEXIST;
976
977 sp = get_space(lsname);
978 if (!sp)
979 goto out;
980
David Teigland90135922006-01-20 08:47:07 +0000981 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000982 list_for_each_entry(nd, &sp->members, list) {
983 if (nd->nodeid != nodeid)
984 continue;
985 w = nd->weight;
986 break;
987 }
David Teigland90135922006-01-20 08:47:07 +0000988 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000989 put_space(sp);
990 out:
991 return w;
992}
993
994int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr)
995{
David Teigland51409342008-07-31 09:31:53 -0500996 struct dlm_comm *cm = get_comm(nodeid, NULL);
David Teiglande7fd4172006-01-18 09:30:29 +0000997 if (!cm)
998 return -EEXIST;
999 if (!cm->addr_count)
1000 return -ENOENT;
1001 memcpy(addr, cm->addr[0], sizeof(*addr));
1002 put_comm(cm);
1003 return 0;
1004}
1005
1006int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
1007{
David Teigland51409342008-07-31 09:31:53 -05001008 struct dlm_comm *cm = get_comm(0, addr);
David Teiglande7fd4172006-01-18 09:30:29 +00001009 if (!cm)
1010 return -EEXIST;
1011 *nodeid = cm->nodeid;
1012 put_comm(cm);
1013 return 0;
1014}
1015
1016int dlm_our_nodeid(void)
1017{
1018 return local_comm ? local_comm->nodeid : 0;
1019}
1020
1021/* num 0 is first addr, num 1 is second addr */
1022int dlm_our_addr(struct sockaddr_storage *addr, int num)
1023{
1024 if (!local_comm)
1025 return -1;
1026 if (num + 1 > local_comm->addr_count)
1027 return -1;
1028 memcpy(addr, local_comm->addr[num], sizeof(*addr));
1029 return 0;
1030}
1031
1032/* Config file defaults */
1033#define DEFAULT_TCP_PORT 21064
1034#define DEFAULT_BUFFER_SIZE 4096
David Teiglande3853a92011-03-10 13:07:17 -06001035#define DEFAULT_RSBTBL_SIZE 1024
David Teiglande3853a92011-03-10 13:07:17 -06001036#define DEFAULT_DIRTBL_SIZE 1024
David Teiglande7fd4172006-01-18 09:30:29 +00001037#define DEFAULT_RECOVER_TIMER 5
1038#define DEFAULT_TOSS_SECS 10
1039#define DEFAULT_SCAN_SECS 5
David Teigland99fc6482007-01-09 09:44:01 -06001040#define DEFAULT_LOG_DEBUG 0
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001041#define DEFAULT_PROTOCOL 0
David Teigland3ae1acf2007-05-18 08:59:31 -05001042#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
David Teiglandc6ff6692011-03-28 14:17:26 -05001043#define DEFAULT_WAITWARN_US 0
David Teiglande7fd4172006-01-18 09:30:29 +00001044
1045struct dlm_config_info dlm_config = {
David Teigland68c817a2007-01-09 09:41:48 -06001046 .ci_tcp_port = DEFAULT_TCP_PORT,
1047 .ci_buffer_size = DEFAULT_BUFFER_SIZE,
1048 .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
David Teigland68c817a2007-01-09 09:41:48 -06001049 .ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,
1050 .ci_recover_timer = DEFAULT_RECOVER_TIMER,
1051 .ci_toss_secs = DEFAULT_TOSS_SECS,
David Teigland99fc6482007-01-09 09:44:01 -06001052 .ci_scan_secs = DEFAULT_SCAN_SECS,
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001053 .ci_log_debug = DEFAULT_LOG_DEBUG,
David Teigland3ae1acf2007-05-18 08:59:31 -05001054 .ci_protocol = DEFAULT_PROTOCOL,
David Teiglandc6ff6692011-03-28 14:17:26 -05001055 .ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
1056 .ci_waitwarn_us = DEFAULT_WAITWARN_US
David Teiglande7fd4172006-01-18 09:30:29 +00001057};
1058