blob: 18a8ffcea0aa48b622f3dfb76b165d9fd80b1952 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglande7fd4172006-01-18 09:30:29 +00002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland60f98d12011-11-02 14:30:58 -05006** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00007**
David Teiglande7fd4172006-01-18 09:30:29 +00008**
9*******************************************************************************
10******************************************************************************/
11
12#include <linux/kernel.h>
Paul Gortmaker7963b8a52016-09-19 16:44:50 -040013#include <linux/init.h>
David Teiglande7fd4172006-01-18 09:30:29 +000014#include <linux/configfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Teigland44be6fd2008-08-28 11:36:19 -050016#include <linux/in.h>
17#include <linux/in6.h>
David Teigland60f98d12011-11-02 14:30:58 -050018#include <linux/dlmconstants.h>
David Teigland44be6fd2008-08-28 11:36:19 -050019#include <net/ipv6.h>
David Teiglande7fd4172006-01-18 09:30:29 +000020#include <net/sock.h>
21
22#include "config.h"
David Teigland1c032c02006-04-28 10:50:41 -040023#include "lowcomms.h"
David Teiglande7fd4172006-01-18 09:30:29 +000024
25/*
26 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
27 * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
28 * /config/dlm/<cluster>/comms/<comm>/nodeid
29 * /config/dlm/<cluster>/comms/<comm>/local
Masatake YAMATO55b32862011-06-30 21:37:10 +090030 * /config/dlm/<cluster>/comms/<comm>/addr (write only)
31 * /config/dlm/<cluster>/comms/<comm>/addr_list (read only)
David Teiglande7fd4172006-01-18 09:30:29 +000032 * 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;
David Teigland51409342008-07-31 09:31:53 -050037static struct dlm_comm *local_comm;
David Teigland60f98d12011-11-02 14:30:58 -050038static uint32_t dlm_comm_count;
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
Christoph Hellwig9ae0f362015-10-03 15:32:53 +020062static struct configfs_attribute *comm_attrs[];
63static struct configfs_attribute *node_attrs[];
David Teiglande7fd4172006-01-18 09:30:29 +000064
David Teigland51409342008-07-31 09:31:53 -050065struct dlm_cluster {
David Teiglandd2007782007-01-09 09:46:02 -060066 struct config_group group;
67 unsigned int cl_tcp_port;
68 unsigned int cl_buffer_size;
69 unsigned int cl_rsbtbl_size;
David Teiglandd2007782007-01-09 09:46:02 -060070 unsigned int cl_recover_timer;
71 unsigned int cl_toss_secs;
72 unsigned int cl_scan_secs;
73 unsigned int cl_log_debug;
Zhilong Liu505ee522016-06-19 23:52:46 -050074 unsigned int cl_log_info;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010075 unsigned int cl_protocol;
Alexander Aringa5b7ab62020-06-26 13:26:49 -040076 unsigned int cl_mark;
David Teigland3ae1acf2007-05-18 08:59:31 -050077 unsigned int cl_timewarn_cs;
David Teiglandc6ff6692011-03-28 14:17:26 -050078 unsigned int cl_waitwarn_us;
David Teigland3881ac02011-07-07 14:05:03 -050079 unsigned int cl_new_rsb_count;
David Teigland60f98d12011-11-02 14:30:58 -050080 unsigned int cl_recover_callbacks;
81 char cl_cluster_name[DLM_LOCKSPACE_LEN];
Alexander Aring2ebbe3a2021-06-02 09:45:16 -040082
83 struct dlm_spaces *sps;
84 struct dlm_comms *cms;
David Teiglandd2007782007-01-09 09:46:02 -060085};
86
Christoph Hellwig9ae0f362015-10-03 15:32:53 +020087static struct dlm_cluster *config_item_to_cluster(struct config_item *i)
88{
89 return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
90 NULL;
91}
92
David Teiglandd2007782007-01-09 09:46:02 -060093enum {
94 CLUSTER_ATTR_TCP_PORT = 0,
95 CLUSTER_ATTR_BUFFER_SIZE,
96 CLUSTER_ATTR_RSBTBL_SIZE,
David Teiglandd2007782007-01-09 09:46:02 -060097 CLUSTER_ATTR_RECOVER_TIMER,
98 CLUSTER_ATTR_TOSS_SECS,
99 CLUSTER_ATTR_SCAN_SECS,
100 CLUSTER_ATTR_LOG_DEBUG,
Zhilong Liu505ee522016-06-19 23:52:46 -0500101 CLUSTER_ATTR_LOG_INFO,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100102 CLUSTER_ATTR_PROTOCOL,
Alexander Aringa5b7ab62020-06-26 13:26:49 -0400103 CLUSTER_ATTR_MARK,
David Teigland3ae1acf2007-05-18 08:59:31 -0500104 CLUSTER_ATTR_TIMEWARN_CS,
David Teiglandc6ff6692011-03-28 14:17:26 -0500105 CLUSTER_ATTR_WAITWARN_US,
David Teigland3881ac02011-07-07 14:05:03 -0500106 CLUSTER_ATTR_NEW_RSB_COUNT,
David Teigland60f98d12011-11-02 14:30:58 -0500107 CLUSTER_ATTR_RECOVER_CALLBACKS,
108 CLUSTER_ATTR_CLUSTER_NAME,
David Teiglandd2007782007-01-09 09:46:02 -0600109};
110
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200111static ssize_t cluster_cluster_name_show(struct config_item *item, char *buf)
David Teigland60f98d12011-11-02 14:30:58 -0500112{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200113 struct dlm_cluster *cl = config_item_to_cluster(item);
David Teigland60f98d12011-11-02 14:30:58 -0500114 return sprintf(buf, "%s\n", cl->cl_cluster_name);
115}
116
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200117static ssize_t cluster_cluster_name_store(struct config_item *item,
David Teigland60f98d12011-11-02 14:30:58 -0500118 const char *buf, size_t len)
119{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200120 struct dlm_cluster *cl = config_item_to_cluster(item);
121
Zhao Hongjiangad917e72013-06-20 18:59:51 +0800122 strlcpy(dlm_config.ci_cluster_name, buf,
123 sizeof(dlm_config.ci_cluster_name));
124 strlcpy(cl->cl_cluster_name, buf, sizeof(cl->cl_cluster_name));
David Teigland60f98d12011-11-02 14:30:58 -0500125 return len;
126}
127
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200128CONFIGFS_ATTR(cluster_, cluster_name);
David Teigland60f98d12011-11-02 14:30:58 -0500129
David Teigland51409342008-07-31 09:31:53 -0500130static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
Alexander Aringca973d22021-03-01 17:05:12 -0500131 int *info_field, int (*check_cb)(unsigned int x),
David Teiglandd2007782007-01-09 09:46:02 -0600132 const char *buf, size_t len)
133{
134 unsigned int x;
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700135 int rc;
David Teiglandd2007782007-01-09 09:46:02 -0600136
137 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100138 return -EPERM;
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700139 rc = kstrtouint(buf, 0, &x);
140 if (rc)
141 return rc;
David Teiglandd2007782007-01-09 09:46:02 -0600142
Alexander Aringca973d22021-03-01 17:05:12 -0500143 if (check_cb) {
144 rc = check_cb(x);
145 if (rc)
146 return rc;
147 }
David Teiglandd2007782007-01-09 09:46:02 -0600148
149 *cl_field = x;
150 *info_field = x;
151
152 return len;
153}
154
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400155#define CLUSTER_ATTR(name, check_cb) \
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200156static ssize_t cluster_##name##_store(struct config_item *item, \
157 const char *buf, size_t len) \
David Teiglandd2007782007-01-09 09:46:02 -0600158{ \
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200159 struct dlm_cluster *cl = config_item_to_cluster(item); \
David Teiglandd2007782007-01-09 09:46:02 -0600160 return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400161 check_cb, buf, len); \
David Teiglandd2007782007-01-09 09:46:02 -0600162} \
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200163static ssize_t cluster_##name##_show(struct config_item *item, char *buf) \
David Teiglandd2007782007-01-09 09:46:02 -0600164{ \
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200165 struct dlm_cluster *cl = config_item_to_cluster(item); \
David Teiglandd2007782007-01-09 09:46:02 -0600166 return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \
167} \
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200168CONFIGFS_ATTR(cluster_, name);
David Teiglandd2007782007-01-09 09:46:02 -0600169
Alexander Aringca973d22021-03-01 17:05:12 -0500170static int dlm_check_zero(unsigned int x)
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400171{
Alexander Aringca973d22021-03-01 17:05:12 -0500172 if (!x)
173 return -EINVAL;
174
175 return 0;
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400176}
177
Alexander Aringca973d22021-03-01 17:05:12 -0500178static int dlm_check_buffer_size(unsigned int x)
Alexander Aring4e192ee2020-09-24 10:31:25 -0400179{
Alexander Aringca973d22021-03-01 17:05:12 -0500180 if (x < DEFAULT_BUFFER_SIZE)
181 return -EINVAL;
182
183 return 0;
Alexander Aring4e192ee2020-09-24 10:31:25 -0400184}
185
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400186CLUSTER_ATTR(tcp_port, dlm_check_zero);
Alexander Aring4e192ee2020-09-24 10:31:25 -0400187CLUSTER_ATTR(buffer_size, dlm_check_buffer_size);
Alexander Aringe1a0ec32020-09-24 10:31:24 -0400188CLUSTER_ATTR(rsbtbl_size, dlm_check_zero);
189CLUSTER_ATTR(recover_timer, dlm_check_zero);
190CLUSTER_ATTR(toss_secs, dlm_check_zero);
191CLUSTER_ATTR(scan_secs, dlm_check_zero);
192CLUSTER_ATTR(log_debug, NULL);
193CLUSTER_ATTR(log_info, NULL);
194CLUSTER_ATTR(protocol, NULL);
195CLUSTER_ATTR(mark, NULL);
196CLUSTER_ATTR(timewarn_cs, dlm_check_zero);
197CLUSTER_ATTR(waitwarn_us, NULL);
198CLUSTER_ATTR(new_rsb_count, NULL);
199CLUSTER_ATTR(recover_callbacks, NULL);
David Teiglandd2007782007-01-09 09:46:02 -0600200
201static struct configfs_attribute *cluster_attrs[] = {
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200202 [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port,
203 [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size,
204 [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size,
205 [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer,
206 [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs,
207 [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs,
208 [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug,
Zhilong Liu505ee522016-06-19 23:52:46 -0500209 [CLUSTER_ATTR_LOG_INFO] = &cluster_attr_log_info,
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200210 [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol,
Alexander Aringa5b7ab62020-06-26 13:26:49 -0400211 [CLUSTER_ATTR_MARK] = &cluster_attr_mark,
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200212 [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs,
213 [CLUSTER_ATTR_WAITWARN_US] = &cluster_attr_waitwarn_us,
214 [CLUSTER_ATTR_NEW_RSB_COUNT] = &cluster_attr_new_rsb_count,
215 [CLUSTER_ATTR_RECOVER_CALLBACKS] = &cluster_attr_recover_callbacks,
216 [CLUSTER_ATTR_CLUSTER_NAME] = &cluster_attr_cluster_name,
David Teiglandd2007782007-01-09 09:46:02 -0600217 NULL,
218};
219
David Teiglande7fd4172006-01-18 09:30:29 +0000220enum {
221 COMM_ATTR_NODEID = 0,
222 COMM_ATTR_LOCAL,
223 COMM_ATTR_ADDR,
Masatake YAMATO55b32862011-06-30 21:37:10 +0900224 COMM_ATTR_ADDR_LIST,
Alexander Aring9c9f1682020-06-26 13:26:50 -0400225 COMM_ATTR_MARK,
David Teiglande7fd4172006-01-18 09:30:29 +0000226};
227
David Teiglande7fd4172006-01-18 09:30:29 +0000228enum {
229 NODE_ATTR_NODEID = 0,
230 NODE_ATTR_WEIGHT,
231};
232
David Teigland51409342008-07-31 09:31:53 -0500233struct dlm_clusters {
David Teiglande7fd4172006-01-18 09:30:29 +0000234 struct configfs_subsystem subsys;
235};
236
David Teigland51409342008-07-31 09:31:53 -0500237struct dlm_spaces {
David Teiglande7fd4172006-01-18 09:30:29 +0000238 struct config_group ss_group;
239};
240
David Teigland51409342008-07-31 09:31:53 -0500241struct dlm_space {
David Teiglande7fd4172006-01-18 09:30:29 +0000242 struct config_group group;
243 struct list_head members;
David Teigland90135922006-01-20 08:47:07 +0000244 struct mutex members_lock;
David Teiglande7fd4172006-01-18 09:30:29 +0000245 int members_count;
Alexander Aring3d2825c2020-08-27 15:02:51 -0400246 struct dlm_nodes *nds;
David Teiglande7fd4172006-01-18 09:30:29 +0000247};
248
David Teigland51409342008-07-31 09:31:53 -0500249struct dlm_comms {
David Teiglande7fd4172006-01-18 09:30:29 +0000250 struct config_group cs_group;
251};
252
David Teigland51409342008-07-31 09:31:53 -0500253struct dlm_comm {
David Teiglande7fd4172006-01-18 09:30:29 +0000254 struct config_item item;
David Teigland60f98d12011-11-02 14:30:58 -0500255 int seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000256 int nodeid;
257 int local;
258 int addr_count;
Alexander Aring9c9f1682020-06-26 13:26:50 -0400259 unsigned int mark;
David Teiglande7fd4172006-01-18 09:30:29 +0000260 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
261};
262
David Teigland51409342008-07-31 09:31:53 -0500263struct dlm_nodes {
David Teiglande7fd4172006-01-18 09:30:29 +0000264 struct config_group ns_group;
265};
266
David Teigland51409342008-07-31 09:31:53 -0500267struct dlm_node {
David Teiglande7fd4172006-01-18 09:30:29 +0000268 struct config_item item;
269 struct list_head list; /* space->members */
270 int nodeid;
271 int weight;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500272 int new;
David Teigland60f98d12011-11-02 14:30:58 -0500273 int comm_seq; /* copy of cm->seq when nd->nodeid is set */
David Teiglande7fd4172006-01-18 09:30:29 +0000274};
275
276static struct configfs_group_operations clusters_ops = {
277 .make_group = make_cluster,
278 .drop_item = drop_cluster,
279};
280
281static struct configfs_item_operations cluster_ops = {
282 .release = release_cluster,
283};
284
285static struct configfs_group_operations spaces_ops = {
286 .make_group = make_space,
287 .drop_item = drop_space,
288};
289
290static struct configfs_item_operations space_ops = {
291 .release = release_space,
292};
293
294static struct configfs_group_operations comms_ops = {
295 .make_item = make_comm,
296 .drop_item = drop_comm,
297};
298
299static struct configfs_item_operations comm_ops = {
300 .release = release_comm,
David Teiglande7fd4172006-01-18 09:30:29 +0000301};
302
303static struct configfs_group_operations nodes_ops = {
304 .make_item = make_node,
305 .drop_item = drop_node,
306};
307
308static struct configfs_item_operations node_ops = {
309 .release = release_node,
David Teiglande7fd4172006-01-18 09:30:29 +0000310};
311
Bhumika Goyal761594b2017-10-16 17:18:53 +0200312static const struct config_item_type clusters_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000313 .ct_group_ops = &clusters_ops,
314 .ct_owner = THIS_MODULE,
315};
316
Bhumika Goyal761594b2017-10-16 17:18:53 +0200317static const struct config_item_type cluster_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000318 .ct_item_ops = &cluster_ops,
David Teiglandd2007782007-01-09 09:46:02 -0600319 .ct_attrs = cluster_attrs,
David Teiglande7fd4172006-01-18 09:30:29 +0000320 .ct_owner = THIS_MODULE,
321};
322
Bhumika Goyal761594b2017-10-16 17:18:53 +0200323static const struct config_item_type spaces_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000324 .ct_group_ops = &spaces_ops,
325 .ct_owner = THIS_MODULE,
326};
327
Bhumika Goyal761594b2017-10-16 17:18:53 +0200328static const struct config_item_type space_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000329 .ct_item_ops = &space_ops,
330 .ct_owner = THIS_MODULE,
331};
332
Bhumika Goyal761594b2017-10-16 17:18:53 +0200333static const struct config_item_type comms_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000334 .ct_group_ops = &comms_ops,
335 .ct_owner = THIS_MODULE,
336};
337
Bhumika Goyal761594b2017-10-16 17:18:53 +0200338static const struct config_item_type comm_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000339 .ct_item_ops = &comm_ops,
340 .ct_attrs = comm_attrs,
341 .ct_owner = THIS_MODULE,
342};
343
Bhumika Goyal761594b2017-10-16 17:18:53 +0200344static const struct config_item_type nodes_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000345 .ct_group_ops = &nodes_ops,
346 .ct_owner = THIS_MODULE,
347};
348
Bhumika Goyal761594b2017-10-16 17:18:53 +0200349static const struct config_item_type node_type = {
David Teiglande7fd4172006-01-18 09:30:29 +0000350 .ct_item_ops = &node_ops,
351 .ct_attrs = node_attrs,
352 .ct_owner = THIS_MODULE,
353};
354
Andrew Morton27eccf42008-09-05 08:42:08 -0500355static struct dlm_space *config_item_to_space(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000356{
David Teigland51409342008-07-31 09:31:53 -0500357 return i ? container_of(to_config_group(i), struct dlm_space, group) :
358 NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000359}
360
Andrew Morton27eccf42008-09-05 08:42:08 -0500361static struct dlm_comm *config_item_to_comm(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000362{
David Teigland51409342008-07-31 09:31:53 -0500363 return i ? container_of(i, struct dlm_comm, item) : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000364}
365
Andrew Morton27eccf42008-09-05 08:42:08 -0500366static struct dlm_node *config_item_to_node(struct config_item *i)
David Teiglande7fd4172006-01-18 09:30:29 +0000367{
David Teigland51409342008-07-31 09:31:53 -0500368 return i ? container_of(i, struct dlm_node, item) : NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000369}
370
Joel Beckerf89ab862008-07-17 14:53:48 -0700371static struct config_group *make_cluster(struct config_group *g,
372 const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000373{
David Teigland51409342008-07-31 09:31:53 -0500374 struct dlm_cluster *cl = NULL;
375 struct dlm_spaces *sps = NULL;
376 struct dlm_comms *cms = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000377
David Teigland573c24c2009-11-30 16:34:43 -0600378 cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
David Teigland573c24c2009-11-30 16:34:43 -0600379 sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
380 cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000381
Andrew Price82c7d822016-03-22 17:36:34 +0000382 if (!cl || !sps || !cms)
David Teiglande7fd4172006-01-18 09:30:29 +0000383 goto fail;
384
Alexander Aring2ebbe3a2021-06-02 09:45:16 -0400385 cl->sps = sps;
386 cl->cms = cms;
387
David Teiglande7fd4172006-01-18 09:30:29 +0000388 config_group_init_type_name(&cl->group, name, &cluster_type);
389 config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
390 config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
391
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100392 configfs_add_default_group(&sps->ss_group, &cl->group);
393 configfs_add_default_group(&cms->cs_group, &cl->group);
David Teiglande7fd4172006-01-18 09:30:29 +0000394
David Teiglandd2007782007-01-09 09:46:02 -0600395 cl->cl_tcp_port = dlm_config.ci_tcp_port;
396 cl->cl_buffer_size = dlm_config.ci_buffer_size;
397 cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
David Teiglandd2007782007-01-09 09:46:02 -0600398 cl->cl_recover_timer = dlm_config.ci_recover_timer;
399 cl->cl_toss_secs = dlm_config.ci_toss_secs;
400 cl->cl_scan_secs = dlm_config.ci_scan_secs;
401 cl->cl_log_debug = dlm_config.ci_log_debug;
Zhilong Liu505ee522016-06-19 23:52:46 -0500402 cl->cl_log_info = dlm_config.ci_log_info;
David Teigland0b7cac02007-05-29 08:47:51 -0500403 cl->cl_protocol = dlm_config.ci_protocol;
David Teigland84d8cd62007-05-29 08:44:23 -0500404 cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
David Teiglandc6ff6692011-03-28 14:17:26 -0500405 cl->cl_waitwarn_us = dlm_config.ci_waitwarn_us;
David Teigland3881ac02011-07-07 14:05:03 -0500406 cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
David Teigland60f98d12011-11-02 14:30:58 -0500407 cl->cl_recover_callbacks = dlm_config.ci_recover_callbacks;
408 memcpy(cl->cl_cluster_name, dlm_config.ci_cluster_name,
409 DLM_LOCKSPACE_LEN);
David Teiglandd2007782007-01-09 09:46:02 -0600410
David Teiglande7fd4172006-01-18 09:30:29 +0000411 space_list = &sps->ss_group;
412 comm_list = &cms->cs_group;
Joel Beckerf89ab862008-07-17 14:53:48 -0700413 return &cl->group;
David Teiglande7fd4172006-01-18 09:30:29 +0000414
415 fail:
416 kfree(cl);
David Teiglande7fd4172006-01-18 09:30:29 +0000417 kfree(sps);
418 kfree(cms);
Joel Beckera6795e92008-07-17 15:21:29 -0700419 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000420}
421
422static void drop_cluster(struct config_group *g, struct config_item *i)
423{
Andrew Morton27eccf42008-09-05 08:42:08 -0500424 struct dlm_cluster *cl = config_item_to_cluster(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000425
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100426 configfs_remove_default_groups(&cl->group);
David Teiglande7fd4172006-01-18 09:30:29 +0000427
428 space_list = NULL;
429 comm_list = NULL;
430
431 config_item_put(i);
432}
433
434static void release_cluster(struct config_item *i)
435{
Andrew Morton27eccf42008-09-05 08:42:08 -0500436 struct dlm_cluster *cl = config_item_to_cluster(i);
Alexander Aring2ebbe3a2021-06-02 09:45:16 -0400437
438 kfree(cl->sps);
439 kfree(cl->cms);
David Teiglande7fd4172006-01-18 09:30:29 +0000440 kfree(cl);
441}
442
Joel Beckerf89ab862008-07-17 14:53:48 -0700443static struct config_group *make_space(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000444{
David Teigland51409342008-07-31 09:31:53 -0500445 struct dlm_space *sp = NULL;
446 struct dlm_nodes *nds = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000447
David Teigland573c24c2009-11-30 16:34:43 -0600448 sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
David Teigland573c24c2009-11-30 16:34:43 -0600449 nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000450
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100451 if (!sp || !nds)
David Teiglande7fd4172006-01-18 09:30:29 +0000452 goto fail;
453
454 config_group_init_type_name(&sp->group, name, &space_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000455
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100456 config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
457 configfs_add_default_group(&nds->ns_group, &sp->group);
David Teiglande7fd4172006-01-18 09:30:29 +0000458
459 INIT_LIST_HEAD(&sp->members);
David Teigland90135922006-01-20 08:47:07 +0000460 mutex_init(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000461 sp->members_count = 0;
Alexander Aring3d2825c2020-08-27 15:02:51 -0400462 sp->nds = nds;
Joel Beckerf89ab862008-07-17 14:53:48 -0700463 return &sp->group;
David Teiglande7fd4172006-01-18 09:30:29 +0000464
465 fail:
466 kfree(sp);
David Teiglande7fd4172006-01-18 09:30:29 +0000467 kfree(nds);
Joel Beckera6795e92008-07-17 15:21:29 -0700468 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000469}
470
471static void drop_space(struct config_group *g, struct config_item *i)
472{
Andrew Morton27eccf42008-09-05 08:42:08 -0500473 struct dlm_space *sp = config_item_to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000474
475 /* assert list_empty(&sp->members) */
476
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100477 configfs_remove_default_groups(&sp->group);
David Teiglande7fd4172006-01-18 09:30:29 +0000478 config_item_put(i);
479}
480
481static void release_space(struct config_item *i)
482{
Andrew Morton27eccf42008-09-05 08:42:08 -0500483 struct dlm_space *sp = config_item_to_space(i);
Alexander Aring3d2825c2020-08-27 15:02:51 -0400484 kfree(sp->nds);
David Teiglande7fd4172006-01-18 09:30:29 +0000485 kfree(sp);
486}
487
Joel Beckerf89ab862008-07-17 14:53:48 -0700488static struct config_item *make_comm(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000489{
David Teigland51409342008-07-31 09:31:53 -0500490 struct dlm_comm *cm;
David Teiglande7fd4172006-01-18 09:30:29 +0000491
David Teigland573c24c2009-11-30 16:34:43 -0600492 cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000493 if (!cm)
Joel Beckera6795e92008-07-17 15:21:29 -0700494 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000495
496 config_item_init_type_name(&cm->item, name, &comm_type);
David Teigland60f98d12011-11-02 14:30:58 -0500497
498 cm->seq = dlm_comm_count++;
499 if (!cm->seq)
500 cm->seq = dlm_comm_count++;
501
David Teiglande7fd4172006-01-18 09:30:29 +0000502 cm->nodeid = -1;
503 cm->local = 0;
504 cm->addr_count = 0;
Alexander Aring9c9f1682020-06-26 13:26:50 -0400505 cm->mark = 0;
Joel Beckerf89ab862008-07-17 14:53:48 -0700506 return &cm->item;
David Teiglande7fd4172006-01-18 09:30:29 +0000507}
508
509static void drop_comm(struct config_group *g, struct config_item *i)
510{
Andrew Morton27eccf42008-09-05 08:42:08 -0500511 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000512 if (local_comm == cm)
513 local_comm = NULL;
David Teigland1c032c02006-04-28 10:50:41 -0400514 dlm_lowcomms_close(cm->nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000515 while (cm->addr_count--)
516 kfree(cm->addr[cm->addr_count]);
517 config_item_put(i);
518}
519
520static void release_comm(struct config_item *i)
521{
Andrew Morton27eccf42008-09-05 08:42:08 -0500522 struct dlm_comm *cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000523 kfree(cm);
524}
525
Joel Beckerf89ab862008-07-17 14:53:48 -0700526static struct config_item *make_node(struct config_group *g, const char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000527{
Andrew Morton27eccf42008-09-05 08:42:08 -0500528 struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
David Teigland51409342008-07-31 09:31:53 -0500529 struct dlm_node *nd;
David Teiglande7fd4172006-01-18 09:30:29 +0000530
David Teigland573c24c2009-11-30 16:34:43 -0600531 nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000532 if (!nd)
Joel Beckera6795e92008-07-17 15:21:29 -0700533 return ERR_PTR(-ENOMEM);
David Teiglande7fd4172006-01-18 09:30:29 +0000534
535 config_item_init_type_name(&nd->item, name, &node_type);
536 nd->nodeid = -1;
537 nd->weight = 1; /* default weight of 1 if none is set */
David Teiglandd44e0fc2008-03-18 14:22:11 -0500538 nd->new = 1; /* set to 0 once it's been read by dlm_nodeid_list() */
David Teiglande7fd4172006-01-18 09:30:29 +0000539
David Teigland90135922006-01-20 08:47:07 +0000540 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000541 list_add(&nd->list, &sp->members);
542 sp->members_count++;
David Teigland90135922006-01-20 08:47:07 +0000543 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000544
Joel Beckerf89ab862008-07-17 14:53:48 -0700545 return &nd->item;
David Teiglande7fd4172006-01-18 09:30:29 +0000546}
547
548static void drop_node(struct config_group *g, struct config_item *i)
549{
Andrew Morton27eccf42008-09-05 08:42:08 -0500550 struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
551 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000552
David Teigland90135922006-01-20 08:47:07 +0000553 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000554 list_del(&nd->list);
555 sp->members_count--;
David Teigland90135922006-01-20 08:47:07 +0000556 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000557
558 config_item_put(i);
559}
560
561static void release_node(struct config_item *i)
562{
Andrew Morton27eccf42008-09-05 08:42:08 -0500563 struct dlm_node *nd = config_item_to_node(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000564 kfree(nd);
565}
566
David Teigland51409342008-07-31 09:31:53 -0500567static struct dlm_clusters clusters_root = {
David Teiglande7fd4172006-01-18 09:30:29 +0000568 .subsys = {
569 .su_group = {
570 .cg_item = {
571 .ci_namebuf = "dlm",
572 .ci_type = &clusters_type,
573 },
574 },
575 },
576};
577
Denis Cheng30727172008-02-02 01:53:46 +0800578int __init dlm_config_init(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000579{
580 config_group_init(&clusters_root.subsys.su_group);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700581 mutex_init(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000582 return configfs_register_subsystem(&clusters_root.subsys);
583}
584
585void dlm_config_exit(void)
586{
587 configfs_unregister_subsystem(&clusters_root.subsys);
588}
589
590/*
591 * Functions for user space to read/write attributes
592 */
593
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200594static ssize_t comm_nodeid_show(struct config_item *item, char *buf)
David Teiglandd2007782007-01-09 09:46:02 -0600595{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200596 return sprintf(buf, "%d\n", config_item_to_comm(item)->nodeid);
David Teiglandd2007782007-01-09 09:46:02 -0600597}
598
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200599static ssize_t comm_nodeid_store(struct config_item *item, const char *buf,
David Teigland51409342008-07-31 09:31:53 -0500600 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000601{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200602 int rc = kstrtoint(buf, 0, &config_item_to_comm(item)->nodeid);
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700603
604 if (rc)
605 return rc;
David Teiglande7fd4172006-01-18 09:30:29 +0000606 return len;
607}
608
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200609static ssize_t comm_local_show(struct config_item *item, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000610{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200611 return sprintf(buf, "%d\n", config_item_to_comm(item)->local);
David Teiglande7fd4172006-01-18 09:30:29 +0000612}
613
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200614static ssize_t comm_local_store(struct config_item *item, const char *buf,
David Teigland51409342008-07-31 09:31:53 -0500615 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000616{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200617 struct dlm_comm *cm = config_item_to_comm(item);
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700618 int rc = kstrtoint(buf, 0, &cm->local);
619
620 if (rc)
621 return rc;
David Teiglande7fd4172006-01-18 09:30:29 +0000622 if (cm->local && !local_comm)
623 local_comm = cm;
624 return len;
625}
626
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200627static ssize_t comm_addr_store(struct config_item *item, const char *buf,
628 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000629{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200630 struct dlm_comm *cm = config_item_to_comm(item);
David Teiglande7fd4172006-01-18 09:30:29 +0000631 struct sockaddr_storage *addr;
David Teigland36b71a82012-07-26 12:44:30 -0500632 int rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000633
634 if (len != sizeof(struct sockaddr_storage))
635 return -EINVAL;
636
637 if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
638 return -ENOSPC;
639
David Teigland573c24c2009-11-30 16:34:43 -0600640 addr = kzalloc(sizeof(*addr), GFP_NOFS);
David Teiglande7fd4172006-01-18 09:30:29 +0000641 if (!addr)
642 return -ENOMEM;
643
644 memcpy(addr, buf, len);
David Teigland36b71a82012-07-26 12:44:30 -0500645
646 rv = dlm_lowcomms_addr(cm->nodeid, addr, len);
647 if (rv) {
648 kfree(addr);
649 return rv;
650 }
651
David Teiglande7fd4172006-01-18 09:30:29 +0000652 cm->addr[cm->addr_count++] = addr;
653 return len;
654}
655
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200656static ssize_t comm_addr_list_show(struct config_item *item, char *buf)
Masatake YAMATO55b32862011-06-30 21:37:10 +0900657{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200658 struct dlm_comm *cm = config_item_to_comm(item);
Masatake YAMATO55b32862011-06-30 21:37:10 +0900659 ssize_t s;
660 ssize_t allowance;
661 int i;
662 struct sockaddr_storage *addr;
663 struct sockaddr_in *addr_in;
664 struct sockaddr_in6 *addr_in6;
665
666 /* Taken from ip6_addr_string() defined in lib/vsprintf.c */
667 char buf0[sizeof("AF_INET6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
668
669
670 /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
671 allowance = 4096;
672 buf[0] = '\0';
673
674 for (i = 0; i < cm->addr_count; i++) {
675 addr = cm->addr[i];
676
677 switch(addr->ss_family) {
678 case AF_INET:
679 addr_in = (struct sockaddr_in *)addr;
680 s = sprintf(buf0, "AF_INET %pI4\n", &addr_in->sin_addr.s_addr);
681 break;
682 case AF_INET6:
683 addr_in6 = (struct sockaddr_in6 *)addr;
684 s = sprintf(buf0, "AF_INET6 %pI6\n", &addr_in6->sin6_addr);
685 break;
686 default:
687 s = sprintf(buf0, "%s\n", "<UNKNOWN>");
688 break;
689 }
690 allowance -= s;
691 if (allowance >= 0)
692 strcat(buf, buf0);
693 else {
694 allowance += s;
695 break;
696 }
697 }
698 return 4096 - allowance;
699}
700
Alexander Aring9c9f1682020-06-26 13:26:50 -0400701static ssize_t comm_mark_show(struct config_item *item, char *buf)
702{
703 return sprintf(buf, "%u\n", config_item_to_comm(item)->mark);
704}
705
706static ssize_t comm_mark_store(struct config_item *item, const char *buf,
707 size_t len)
708{
709 unsigned int mark;
710 int rc;
711
712 rc = kstrtouint(buf, 0, &mark);
713 if (rc)
714 return rc;
715
716 config_item_to_comm(item)->mark = mark;
717 return len;
718}
719
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200720CONFIGFS_ATTR(comm_, nodeid);
721CONFIGFS_ATTR(comm_, local);
Alexander Aring9c9f1682020-06-26 13:26:50 -0400722CONFIGFS_ATTR(comm_, mark);
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200723CONFIGFS_ATTR_WO(comm_, addr);
724CONFIGFS_ATTR_RO(comm_, addr_list);
725
726static struct configfs_attribute *comm_attrs[] = {
727 [COMM_ATTR_NODEID] = &comm_attr_nodeid,
728 [COMM_ATTR_LOCAL] = &comm_attr_local,
729 [COMM_ATTR_ADDR] = &comm_attr_addr,
730 [COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list,
Alexander Aring9c9f1682020-06-26 13:26:50 -0400731 [COMM_ATTR_MARK] = &comm_attr_mark,
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200732 NULL,
733};
734
735static ssize_t node_nodeid_show(struct config_item *item, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000736{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200737 return sprintf(buf, "%d\n", config_item_to_node(item)->nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000738}
739
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200740static ssize_t node_nodeid_store(struct config_item *item, const char *buf,
David Teigland51409342008-07-31 09:31:53 -0500741 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000742{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200743 struct dlm_node *nd = config_item_to_node(item);
David Teigland60f98d12011-11-02 14:30:58 -0500744 uint32_t seq = 0;
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700745 int rc = kstrtoint(buf, 0, &nd->nodeid);
746
747 if (rc)
748 return rc;
David Teigland60f98d12011-11-02 14:30:58 -0500749 dlm_comm_seq(nd->nodeid, &seq);
750 nd->comm_seq = seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000751 return len;
752}
753
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200754static ssize_t node_weight_show(struct config_item *item, char *buf)
David Teiglande7fd4172006-01-18 09:30:29 +0000755{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200756 return sprintf(buf, "%d\n", config_item_to_node(item)->weight);
David Teiglande7fd4172006-01-18 09:30:29 +0000757}
758
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200759static ssize_t node_weight_store(struct config_item *item, const char *buf,
David Teigland51409342008-07-31 09:31:53 -0500760 size_t len)
David Teiglande7fd4172006-01-18 09:30:29 +0000761{
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200762 int rc = kstrtoint(buf, 0, &config_item_to_node(item)->weight);
Fabian Frederick4f4c3372014-06-06 14:38:24 -0700763
764 if (rc)
765 return rc;
David Teiglande7fd4172006-01-18 09:30:29 +0000766 return len;
767}
768
Christoph Hellwig9ae0f362015-10-03 15:32:53 +0200769CONFIGFS_ATTR(node_, nodeid);
770CONFIGFS_ATTR(node_, weight);
771
772static struct configfs_attribute *node_attrs[] = {
773 [NODE_ATTR_NODEID] = &node_attr_nodeid,
774 [NODE_ATTR_WEIGHT] = &node_attr_weight,
775 NULL,
776};
777
David Teiglande7fd4172006-01-18 09:30:29 +0000778/*
779 * Functions for the dlm to get the info that's been configured
780 */
781
David Teigland51409342008-07-31 09:31:53 -0500782static struct dlm_space *get_space(char *name)
David Teiglande7fd4172006-01-18 09:30:29 +0000783{
Satyam Sharma3168b072007-05-08 09:18:58 +0100784 struct config_item *i;
785
David Teiglande7fd4172006-01-18 09:30:29 +0000786 if (!space_list)
787 return NULL;
Satyam Sharma3168b072007-05-08 09:18:58 +0100788
Joel Beckere6bd07a2007-07-06 23:33:17 -0700789 mutex_lock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530790 i = config_group_find_item(space_list, name);
Joel Beckere6bd07a2007-07-06 23:33:17 -0700791 mutex_unlock(&space_list->cg_subsys->su_mutex);
Satyam Sharma3168b072007-05-08 09:18:58 +0100792
Andrew Morton27eccf42008-09-05 08:42:08 -0500793 return config_item_to_space(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000794}
795
David Teigland51409342008-07-31 09:31:53 -0500796static void put_space(struct dlm_space *sp)
David Teiglande7fd4172006-01-18 09:30:29 +0000797{
798 config_item_put(&sp->group.cg_item);
799}
800
David Teigland36b71a82012-07-26 12:44:30 -0500801static struct dlm_comm *get_comm(int nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000802{
803 struct config_item *i;
David Teigland51409342008-07-31 09:31:53 -0500804 struct dlm_comm *cm = NULL;
David Teiglande7fd4172006-01-18 09:30:29 +0000805 int found = 0;
806
807 if (!comm_list)
808 return NULL;
809
Joel Beckere6bd07a2007-07-06 23:33:17 -0700810 mutex_lock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000811
812 list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
Andrew Morton27eccf42008-09-05 08:42:08 -0500813 cm = config_item_to_comm(i);
David Teiglande7fd4172006-01-18 09:30:29 +0000814
David Teigland36b71a82012-07-26 12:44:30 -0500815 if (cm->nodeid != nodeid)
816 continue;
817 found = 1;
818 config_item_get(i);
819 break;
David Teiglande7fd4172006-01-18 09:30:29 +0000820 }
Joel Beckere6bd07a2007-07-06 23:33:17 -0700821 mutex_unlock(&clusters_root.subsys.su_mutex);
David Teiglande7fd4172006-01-18 09:30:29 +0000822
Satyam Sharma3168b072007-05-08 09:18:58 +0100823 if (!found)
David Teiglande7fd4172006-01-18 09:30:29 +0000824 cm = NULL;
825 return cm;
826}
827
David Teigland51409342008-07-31 09:31:53 -0500828static void put_comm(struct dlm_comm *cm)
David Teiglande7fd4172006-01-18 09:30:29 +0000829{
830 config_item_put(&cm->item);
831}
832
833/* caller must free mem */
David Teigland60f98d12011-11-02 14:30:58 -0500834int dlm_config_nodes(char *lsname, struct dlm_config_node **nodes_out,
835 int *count_out)
David Teiglande7fd4172006-01-18 09:30:29 +0000836{
David Teigland51409342008-07-31 09:31:53 -0500837 struct dlm_space *sp;
838 struct dlm_node *nd;
David Teigland60f98d12011-11-02 14:30:58 -0500839 struct dlm_config_node *nodes, *node;
840 int rv, count;
David Teiglande7fd4172006-01-18 09:30:29 +0000841
842 sp = get_space(lsname);
843 if (!sp)
844 return -EEXIST;
845
David Teigland90135922006-01-20 08:47:07 +0000846 mutex_lock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000847 if (!sp->members_count) {
David Teiglandd44e0fc2008-03-18 14:22:11 -0500848 rv = -EINVAL;
849 printk(KERN_ERR "dlm: zero members_count\n");
David Teiglande7fd4172006-01-18 09:30:29 +0000850 goto out;
851 }
852
David Teigland60f98d12011-11-02 14:30:58 -0500853 count = sp->members_count;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500854
David Teigland60f98d12011-11-02 14:30:58 -0500855 nodes = kcalloc(count, sizeof(struct dlm_config_node), GFP_NOFS);
856 if (!nodes) {
David Teiglande7fd4172006-01-18 09:30:29 +0000857 rv = -ENOMEM;
858 goto out;
859 }
860
David Teigland60f98d12011-11-02 14:30:58 -0500861 node = nodes;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500862 list_for_each_entry(nd, &sp->members, list) {
David Teigland60f98d12011-11-02 14:30:58 -0500863 node->nodeid = nd->nodeid;
864 node->weight = nd->weight;
865 node->new = nd->new;
866 node->comm_seq = nd->comm_seq;
867 node++;
868
869 nd->new = 0;
David Teiglandd44e0fc2008-03-18 14:22:11 -0500870 }
David Teiglande7fd4172006-01-18 09:30:29 +0000871
David Teigland60f98d12011-11-02 14:30:58 -0500872 *count_out = count;
873 *nodes_out = nodes;
874 rv = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000875 out:
David Teigland90135922006-01-20 08:47:07 +0000876 mutex_unlock(&sp->members_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000877 put_space(sp);
878 return rv;
879}
880
David Teigland60f98d12011-11-02 14:30:58 -0500881int dlm_comm_seq(int nodeid, uint32_t *seq)
David Teiglande7fd4172006-01-18 09:30:29 +0000882{
David Teigland36b71a82012-07-26 12:44:30 -0500883 struct dlm_comm *cm = get_comm(nodeid);
David Teigland60f98d12011-11-02 14:30:58 -0500884 if (!cm)
885 return -EEXIST;
886 *seq = cm->seq;
887 put_comm(cm);
888 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000889}
890
Alexander Aring3f78cd72020-09-24 10:31:23 -0400891void dlm_comm_mark(int nodeid, unsigned int *mark)
Alexander Aring9c9f1682020-06-26 13:26:50 -0400892{
893 struct dlm_comm *cm;
894
895 cm = get_comm(nodeid);
Alexander Aring3f78cd72020-09-24 10:31:23 -0400896 if (!cm) {
897 *mark = dlm_config.ci_mark;
898 return;
899 }
Alexander Aring9c9f1682020-06-26 13:26:50 -0400900
Alexander Aring3f78cd72020-09-24 10:31:23 -0400901 if (cm->mark)
902 *mark = cm->mark;
903 else
904 *mark = dlm_config.ci_mark;
905
Alexander Aring9c9f1682020-06-26 13:26:50 -0400906 put_comm(cm);
Alexander Aring9c9f1682020-06-26 13:26:50 -0400907}
908
David Teiglande7fd4172006-01-18 09:30:29 +0000909int dlm_our_nodeid(void)
910{
911 return local_comm ? local_comm->nodeid : 0;
912}
913
914/* num 0 is first addr, num 1 is second addr */
915int dlm_our_addr(struct sockaddr_storage *addr, int num)
916{
917 if (!local_comm)
918 return -1;
919 if (num + 1 > local_comm->addr_count)
920 return -1;
921 memcpy(addr, local_comm->addr[num], sizeof(*addr));
922 return 0;
923}
924
925/* Config file defaults */
926#define DEFAULT_TCP_PORT 21064
David Teiglande3853a92011-03-10 13:07:17 -0600927#define DEFAULT_RSBTBL_SIZE 1024
David Teiglande7fd4172006-01-18 09:30:29 +0000928#define DEFAULT_RECOVER_TIMER 5
929#define DEFAULT_TOSS_SECS 10
930#define DEFAULT_SCAN_SECS 5
David Teigland99fc6482007-01-09 09:44:01 -0600931#define DEFAULT_LOG_DEBUG 0
Zhilong Liu505ee522016-06-19 23:52:46 -0500932#define DEFAULT_LOG_INFO 1
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100933#define DEFAULT_PROTOCOL 0
Alexander Aringa5b7ab62020-06-26 13:26:49 -0400934#define DEFAULT_MARK 0
David Teigland3ae1acf2007-05-18 08:59:31 -0500935#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
David Teiglandc6ff6692011-03-28 14:17:26 -0500936#define DEFAULT_WAITWARN_US 0
David Teigland3881ac02011-07-07 14:05:03 -0500937#define DEFAULT_NEW_RSB_COUNT 128
David Teigland60f98d12011-11-02 14:30:58 -0500938#define DEFAULT_RECOVER_CALLBACKS 0
939#define DEFAULT_CLUSTER_NAME ""
David Teiglande7fd4172006-01-18 09:30:29 +0000940
941struct dlm_config_info dlm_config = {
David Teigland68c817a2007-01-09 09:41:48 -0600942 .ci_tcp_port = DEFAULT_TCP_PORT,
943 .ci_buffer_size = DEFAULT_BUFFER_SIZE,
944 .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
David Teigland68c817a2007-01-09 09:41:48 -0600945 .ci_recover_timer = DEFAULT_RECOVER_TIMER,
946 .ci_toss_secs = DEFAULT_TOSS_SECS,
David Teigland99fc6482007-01-09 09:44:01 -0600947 .ci_scan_secs = DEFAULT_SCAN_SECS,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100948 .ci_log_debug = DEFAULT_LOG_DEBUG,
Zhilong Liu505ee522016-06-19 23:52:46 -0500949 .ci_log_info = DEFAULT_LOG_INFO,
David Teigland3ae1acf2007-05-18 08:59:31 -0500950 .ci_protocol = DEFAULT_PROTOCOL,
Alexander Aringa5b7ab62020-06-26 13:26:49 -0400951 .ci_mark = DEFAULT_MARK,
David Teiglandc6ff6692011-03-28 14:17:26 -0500952 .ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
David Teigland3881ac02011-07-07 14:05:03 -0500953 .ci_waitwarn_us = DEFAULT_WAITWARN_US,
David Teigland60f98d12011-11-02 14:30:58 -0500954 .ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT,
955 .ci_recover_callbacks = DEFAULT_RECOVER_CALLBACKS,
956 .ci_cluster_name = DEFAULT_CLUSTER_NAME
David Teiglande7fd4172006-01-18 09:30:29 +0000957};
958