xref: /openbmc/linux/fs/dlm/config.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
12522fe45SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e7fd4179SDavid Teigland /******************************************************************************
3e7fd4179SDavid Teigland *******************************************************************************
4e7fd4179SDavid Teigland **
5e7fd4179SDavid Teigland **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
660f98d18SDavid Teigland **  Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
7e7fd4179SDavid Teigland **
8e7fd4179SDavid Teigland **
9e7fd4179SDavid Teigland *******************************************************************************
10e7fd4179SDavid Teigland ******************************************************************************/
11e7fd4179SDavid Teigland 
12e7fd4179SDavid Teigland #include <linux/kernel.h>
137963b8a5SPaul Gortmaker #include <linux/init.h>
14e7fd4179SDavid Teigland #include <linux/configfs.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
1644be6fdfSDavid Teigland #include <linux/in.h>
1744be6fdfSDavid Teigland #include <linux/in6.h>
1860f98d18SDavid Teigland #include <linux/dlmconstants.h>
1944be6fdfSDavid Teigland #include <net/ipv6.h>
20e7fd4179SDavid Teigland #include <net/sock.h>
21e7fd4179SDavid Teigland 
22e7fd4179SDavid Teigland #include "config.h"
23a070a91cSAlexander Aring #include "midcomms.h"
241c032c03SDavid Teigland #include "lowcomms.h"
25e7fd4179SDavid Teigland 
26e7fd4179SDavid Teigland /*
27e7fd4179SDavid Teigland  * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
28e7fd4179SDavid Teigland  * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
29e7fd4179SDavid Teigland  * /config/dlm/<cluster>/comms/<comm>/nodeid
30e7fd4179SDavid Teigland  * /config/dlm/<cluster>/comms/<comm>/local
3155b3286dSMasatake YAMATO  * /config/dlm/<cluster>/comms/<comm>/addr      (write only)
3255b3286dSMasatake YAMATO  * /config/dlm/<cluster>/comms/<comm>/addr_list (read only)
33e7fd4179SDavid Teigland  * The <cluster> level is useless, but I haven't figured out how to avoid it.
34e7fd4179SDavid Teigland  */
35e7fd4179SDavid Teigland 
36e7fd4179SDavid Teigland static struct config_group *space_list;
37e7fd4179SDavid Teigland static struct config_group *comm_list;
3851409340SDavid Teigland static struct dlm_comm *local_comm;
3960f98d18SDavid Teigland static uint32_t dlm_comm_count;
40e7fd4179SDavid Teigland 
4151409340SDavid Teigland struct dlm_clusters;
4251409340SDavid Teigland struct dlm_cluster;
4351409340SDavid Teigland struct dlm_spaces;
4451409340SDavid Teigland struct dlm_space;
4551409340SDavid Teigland struct dlm_comms;
4651409340SDavid Teigland struct dlm_comm;
4751409340SDavid Teigland struct dlm_nodes;
4851409340SDavid Teigland struct dlm_node;
49e7fd4179SDavid Teigland 
50f89ab861SJoel Becker static struct config_group *make_cluster(struct config_group *, const char *);
51e7fd4179SDavid Teigland static void drop_cluster(struct config_group *, struct config_item *);
52e7fd4179SDavid Teigland static void release_cluster(struct config_item *);
53f89ab861SJoel Becker static struct config_group *make_space(struct config_group *, const char *);
54e7fd4179SDavid Teigland static void drop_space(struct config_group *, struct config_item *);
55e7fd4179SDavid Teigland static void release_space(struct config_item *);
56f89ab861SJoel Becker static struct config_item *make_comm(struct config_group *, const char *);
57e7fd4179SDavid Teigland static void drop_comm(struct config_group *, struct config_item *);
58e7fd4179SDavid Teigland static void release_comm(struct config_item *);
59f89ab861SJoel Becker static struct config_item *make_node(struct config_group *, const char *);
60e7fd4179SDavid Teigland static void drop_node(struct config_group *, struct config_item *);
61e7fd4179SDavid Teigland static void release_node(struct config_item *);
62e7fd4179SDavid Teigland 
639ae0f367SChristoph Hellwig static struct configfs_attribute *comm_attrs[];
649ae0f367SChristoph Hellwig static struct configfs_attribute *node_attrs[];
65e7fd4179SDavid Teigland 
6651409340SDavid Teigland struct dlm_cluster {
67d200778eSDavid Teigland 	struct config_group group;
68d200778eSDavid Teigland 	unsigned int cl_tcp_port;
69d200778eSDavid Teigland 	unsigned int cl_buffer_size;
70d200778eSDavid Teigland 	unsigned int cl_rsbtbl_size;
71d200778eSDavid Teigland 	unsigned int cl_recover_timer;
72d200778eSDavid Teigland 	unsigned int cl_toss_secs;
73d200778eSDavid Teigland 	unsigned int cl_scan_secs;
74d200778eSDavid Teigland 	unsigned int cl_log_debug;
75505ee528SZhilong Liu 	unsigned int cl_log_info;
766ed7257bSPatrick Caulfield 	unsigned int cl_protocol;
77a5b7ab63SAlexander Aring 	unsigned int cl_mark;
783881ac04SDavid Teigland 	unsigned int cl_new_rsb_count;
7960f98d18SDavid Teigland 	unsigned int cl_recover_callbacks;
8060f98d18SDavid Teigland 	char cl_cluster_name[DLM_LOCKSPACE_LEN];
81700ab1c3SAlexander Aring 
82700ab1c3SAlexander Aring 	struct dlm_spaces *sps;
83700ab1c3SAlexander Aring 	struct dlm_comms *cms;
84d200778eSDavid Teigland };
85d200778eSDavid Teigland 
config_item_to_cluster(struct config_item * i)869ae0f367SChristoph Hellwig static struct dlm_cluster *config_item_to_cluster(struct config_item *i)
879ae0f367SChristoph Hellwig {
889ae0f367SChristoph Hellwig 	return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
899ae0f367SChristoph Hellwig 		   NULL;
909ae0f367SChristoph Hellwig }
919ae0f367SChristoph Hellwig 
92d200778eSDavid Teigland enum {
93d200778eSDavid Teigland 	CLUSTER_ATTR_TCP_PORT = 0,
94d200778eSDavid Teigland 	CLUSTER_ATTR_BUFFER_SIZE,
95d200778eSDavid Teigland 	CLUSTER_ATTR_RSBTBL_SIZE,
96d200778eSDavid Teigland 	CLUSTER_ATTR_RECOVER_TIMER,
97d200778eSDavid Teigland 	CLUSTER_ATTR_TOSS_SECS,
98d200778eSDavid Teigland 	CLUSTER_ATTR_SCAN_SECS,
99d200778eSDavid Teigland 	CLUSTER_ATTR_LOG_DEBUG,
100505ee528SZhilong Liu 	CLUSTER_ATTR_LOG_INFO,
1016ed7257bSPatrick Caulfield 	CLUSTER_ATTR_PROTOCOL,
102a5b7ab63SAlexander Aring 	CLUSTER_ATTR_MARK,
1033881ac04SDavid Teigland 	CLUSTER_ATTR_NEW_RSB_COUNT,
10460f98d18SDavid Teigland 	CLUSTER_ATTR_RECOVER_CALLBACKS,
10560f98d18SDavid Teigland 	CLUSTER_ATTR_CLUSTER_NAME,
106d200778eSDavid Teigland };
107d200778eSDavid Teigland 
cluster_cluster_name_show(struct config_item * item,char * buf)1089ae0f367SChristoph Hellwig static ssize_t cluster_cluster_name_show(struct config_item *item, char *buf)
10960f98d18SDavid Teigland {
1109ae0f367SChristoph Hellwig 	struct dlm_cluster *cl = config_item_to_cluster(item);
11160f98d18SDavid Teigland 	return sprintf(buf, "%s\n", cl->cl_cluster_name);
11260f98d18SDavid Teigland }
11360f98d18SDavid Teigland 
cluster_cluster_name_store(struct config_item * item,const char * buf,size_t len)1149ae0f367SChristoph Hellwig static ssize_t cluster_cluster_name_store(struct config_item *item,
11560f98d18SDavid Teigland 					  const char *buf, size_t len)
11660f98d18SDavid Teigland {
1179ae0f367SChristoph Hellwig 	struct dlm_cluster *cl = config_item_to_cluster(item);
1189ae0f367SChristoph Hellwig 
11930ad0627SAzeem Shaikh 	strscpy(dlm_config.ci_cluster_name, buf,
120ad917e7fSZhao Hongjiang 				sizeof(dlm_config.ci_cluster_name));
12130ad0627SAzeem Shaikh 	strscpy(cl->cl_cluster_name, buf, sizeof(cl->cl_cluster_name));
12260f98d18SDavid Teigland 	return len;
12360f98d18SDavid Teigland }
12460f98d18SDavid Teigland 
1259ae0f367SChristoph Hellwig CONFIGFS_ATTR(cluster_, cluster_name);
12660f98d18SDavid Teigland 
cluster_set(struct dlm_cluster * cl,unsigned int * cl_field,int * info_field,int (* check_cb)(unsigned int x),const char * buf,size_t len)12751409340SDavid Teigland static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
1288aa9540bSAlexander Aring 			   int *info_field, int (*check_cb)(unsigned int x),
129d200778eSDavid Teigland 			   const char *buf, size_t len)
130d200778eSDavid Teigland {
131d200778eSDavid Teigland 	unsigned int x;
1324f4c337fSFabian Frederick 	int rc;
133d200778eSDavid Teigland 
134d200778eSDavid Teigland 	if (!capable(CAP_SYS_ADMIN))
13541735818SZhao Hongjiang 		return -EPERM;
1364f4c337fSFabian Frederick 	rc = kstrtouint(buf, 0, &x);
1374f4c337fSFabian Frederick 	if (rc)
1384f4c337fSFabian Frederick 		return rc;
139d200778eSDavid Teigland 
1408aa9540bSAlexander Aring 	if (check_cb) {
1418aa9540bSAlexander Aring 		rc = check_cb(x);
1428aa9540bSAlexander Aring 		if (rc)
1438aa9540bSAlexander Aring 			return rc;
1448aa9540bSAlexander Aring 	}
145d200778eSDavid Teigland 
146d200778eSDavid Teigland 	*cl_field = x;
147d200778eSDavid Teigland 	*info_field = x;
148d200778eSDavid Teigland 
149d200778eSDavid Teigland 	return len;
150d200778eSDavid Teigland }
151d200778eSDavid Teigland 
152e1a0ec30SAlexander Aring #define CLUSTER_ATTR(name, check_cb)                                          \
1539ae0f367SChristoph Hellwig static ssize_t cluster_##name##_store(struct config_item *item, \
1549ae0f367SChristoph Hellwig 		const char *buf, size_t len) \
155d200778eSDavid Teigland {                                                                             \
1569ae0f367SChristoph Hellwig 	struct dlm_cluster *cl = config_item_to_cluster(item);		      \
157d200778eSDavid Teigland 	return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name,         \
158e1a0ec30SAlexander Aring 			   check_cb, buf, len);                               \
159d200778eSDavid Teigland }                                                                             \
1609ae0f367SChristoph Hellwig static ssize_t cluster_##name##_show(struct config_item *item, char *buf)     \
161d200778eSDavid Teigland {                                                                             \
1629ae0f367SChristoph Hellwig 	struct dlm_cluster *cl = config_item_to_cluster(item);		      \
163d200778eSDavid Teigland 	return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name);               \
164d200778eSDavid Teigland }                                                                             \
1659ae0f367SChristoph Hellwig CONFIGFS_ATTR(cluster_, name);
166d200778eSDavid Teigland 
dlm_check_protocol_and_dlm_running(unsigned int x)16751746163SAlexander Aring static int dlm_check_protocol_and_dlm_running(unsigned int x)
16851746163SAlexander Aring {
16951746163SAlexander Aring 	switch (x) {
17051746163SAlexander Aring 	case 0:
17151746163SAlexander Aring 		/* TCP */
17251746163SAlexander Aring 		break;
17351746163SAlexander Aring 	case 1:
17451746163SAlexander Aring 		/* SCTP */
17551746163SAlexander Aring 		break;
17651746163SAlexander Aring 	default:
17751746163SAlexander Aring 		return -EINVAL;
17851746163SAlexander Aring 	}
17951746163SAlexander Aring 
1801037c2a9SAlexander Aring 	if (dlm_lowcomms_is_running())
18151746163SAlexander Aring 		return -EBUSY;
18251746163SAlexander Aring 
18351746163SAlexander Aring 	return 0;
18451746163SAlexander Aring }
18551746163SAlexander Aring 
dlm_check_zero_and_dlm_running(unsigned int x)18651746163SAlexander Aring static int dlm_check_zero_and_dlm_running(unsigned int x)
18751746163SAlexander Aring {
18851746163SAlexander Aring 	if (!x)
18951746163SAlexander Aring 		return -EINVAL;
19051746163SAlexander Aring 
1911037c2a9SAlexander Aring 	if (dlm_lowcomms_is_running())
19251746163SAlexander Aring 		return -EBUSY;
19351746163SAlexander Aring 
19451746163SAlexander Aring 	return 0;
19551746163SAlexander Aring }
19651746163SAlexander Aring 
dlm_check_zero(unsigned int x)1978aa9540bSAlexander Aring static int dlm_check_zero(unsigned int x)
198e1a0ec30SAlexander Aring {
1998aa9540bSAlexander Aring 	if (!x)
2008aa9540bSAlexander Aring 		return -EINVAL;
2018aa9540bSAlexander Aring 
2028aa9540bSAlexander Aring 	return 0;
203e1a0ec30SAlexander Aring }
204e1a0ec30SAlexander Aring 
dlm_check_buffer_size(unsigned int x)2058aa9540bSAlexander Aring static int dlm_check_buffer_size(unsigned int x)
2064e192ee6SAlexander Aring {
207d10a0b88SAlexander Aring 	if (x < DLM_MAX_SOCKET_BUFSIZE)
2088aa9540bSAlexander Aring 		return -EINVAL;
2098aa9540bSAlexander Aring 
2108aa9540bSAlexander Aring 	return 0;
2114e192ee6SAlexander Aring }
2124e192ee6SAlexander Aring 
21351746163SAlexander Aring CLUSTER_ATTR(tcp_port, dlm_check_zero_and_dlm_running);
2144e192ee6SAlexander Aring CLUSTER_ATTR(buffer_size, dlm_check_buffer_size);
215e1a0ec30SAlexander Aring CLUSTER_ATTR(rsbtbl_size, dlm_check_zero);
216e1a0ec30SAlexander Aring CLUSTER_ATTR(recover_timer, dlm_check_zero);
217e1a0ec30SAlexander Aring CLUSTER_ATTR(toss_secs, dlm_check_zero);
218e1a0ec30SAlexander Aring CLUSTER_ATTR(scan_secs, dlm_check_zero);
219e1a0ec30SAlexander Aring CLUSTER_ATTR(log_debug, NULL);
220e1a0ec30SAlexander Aring CLUSTER_ATTR(log_info, NULL);
22151746163SAlexander Aring CLUSTER_ATTR(protocol, dlm_check_protocol_and_dlm_running);
222e1a0ec30SAlexander Aring CLUSTER_ATTR(mark, NULL);
223e1a0ec30SAlexander Aring CLUSTER_ATTR(new_rsb_count, NULL);
224e1a0ec30SAlexander Aring CLUSTER_ATTR(recover_callbacks, NULL);
225d200778eSDavid Teigland 
226d200778eSDavid Teigland static struct configfs_attribute *cluster_attrs[] = {
2279ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port,
2289ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size,
2299ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size,
2309ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer,
2319ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs,
2329ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs,
2339ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug,
234505ee528SZhilong Liu 	[CLUSTER_ATTR_LOG_INFO] = &cluster_attr_log_info,
2359ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol,
236a5b7ab63SAlexander Aring 	[CLUSTER_ATTR_MARK] = &cluster_attr_mark,
2379ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_NEW_RSB_COUNT] = &cluster_attr_new_rsb_count,
2389ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_RECOVER_CALLBACKS] = &cluster_attr_recover_callbacks,
2399ae0f367SChristoph Hellwig 	[CLUSTER_ATTR_CLUSTER_NAME] = &cluster_attr_cluster_name,
240d200778eSDavid Teigland 	NULL,
241d200778eSDavid Teigland };
242d200778eSDavid Teigland 
243e7fd4179SDavid Teigland enum {
244e7fd4179SDavid Teigland 	COMM_ATTR_NODEID = 0,
245e7fd4179SDavid Teigland 	COMM_ATTR_LOCAL,
246e7fd4179SDavid Teigland 	COMM_ATTR_ADDR,
24755b3286dSMasatake YAMATO 	COMM_ATTR_ADDR_LIST,
2489c9f168fSAlexander Aring 	COMM_ATTR_MARK,
249e7fd4179SDavid Teigland };
250e7fd4179SDavid Teigland 
251e7fd4179SDavid Teigland enum {
252e7fd4179SDavid Teigland 	NODE_ATTR_NODEID = 0,
253e7fd4179SDavid Teigland 	NODE_ATTR_WEIGHT,
254e7fd4179SDavid Teigland };
255e7fd4179SDavid Teigland 
25651409340SDavid Teigland struct dlm_clusters {
257e7fd4179SDavid Teigland 	struct configfs_subsystem subsys;
258e7fd4179SDavid Teigland };
259e7fd4179SDavid Teigland 
26051409340SDavid Teigland struct dlm_spaces {
261e7fd4179SDavid Teigland 	struct config_group ss_group;
262e7fd4179SDavid Teigland };
263e7fd4179SDavid Teigland 
26451409340SDavid Teigland struct dlm_space {
265e7fd4179SDavid Teigland 	struct config_group group;
266e7fd4179SDavid Teigland 	struct list_head members;
26790135925SDavid Teigland 	struct mutex members_lock;
268e7fd4179SDavid Teigland 	int members_count;
2693d2825c8SAlexander Aring 	struct dlm_nodes *nds;
270e7fd4179SDavid Teigland };
271e7fd4179SDavid Teigland 
27251409340SDavid Teigland struct dlm_comms {
273e7fd4179SDavid Teigland 	struct config_group cs_group;
274e7fd4179SDavid Teigland };
275e7fd4179SDavid Teigland 
27651409340SDavid Teigland struct dlm_comm {
277e7fd4179SDavid Teigland 	struct config_item item;
27860f98d18SDavid Teigland 	int seq;
279e7fd4179SDavid Teigland 	int nodeid;
280e7fd4179SDavid Teigland 	int local;
281e7fd4179SDavid Teigland 	int addr_count;
2829c9f168fSAlexander Aring 	unsigned int mark;
283e7fd4179SDavid Teigland 	struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
284e7fd4179SDavid Teigland };
285e7fd4179SDavid Teigland 
28651409340SDavid Teigland struct dlm_nodes {
287e7fd4179SDavid Teigland 	struct config_group ns_group;
288e7fd4179SDavid Teigland };
289e7fd4179SDavid Teigland 
29051409340SDavid Teigland struct dlm_node {
291e7fd4179SDavid Teigland 	struct config_item item;
292e7fd4179SDavid Teigland 	struct list_head list; /* space->members */
293e7fd4179SDavid Teigland 	int nodeid;
294e7fd4179SDavid Teigland 	int weight;
295d44e0fc7SDavid Teigland 	int new;
29660f98d18SDavid Teigland 	int comm_seq; /* copy of cm->seq when nd->nodeid is set */
297e7fd4179SDavid Teigland };
298e7fd4179SDavid Teigland 
299e7fd4179SDavid Teigland static struct configfs_group_operations clusters_ops = {
300e7fd4179SDavid Teigland 	.make_group = make_cluster,
301e7fd4179SDavid Teigland 	.drop_item = drop_cluster,
302e7fd4179SDavid Teigland };
303e7fd4179SDavid Teigland 
304e7fd4179SDavid Teigland static struct configfs_item_operations cluster_ops = {
305e7fd4179SDavid Teigland 	.release = release_cluster,
306e7fd4179SDavid Teigland };
307e7fd4179SDavid Teigland 
308e7fd4179SDavid Teigland static struct configfs_group_operations spaces_ops = {
309e7fd4179SDavid Teigland 	.make_group = make_space,
310e7fd4179SDavid Teigland 	.drop_item = drop_space,
311e7fd4179SDavid Teigland };
312e7fd4179SDavid Teigland 
313e7fd4179SDavid Teigland static struct configfs_item_operations space_ops = {
314e7fd4179SDavid Teigland 	.release = release_space,
315e7fd4179SDavid Teigland };
316e7fd4179SDavid Teigland 
317e7fd4179SDavid Teigland static struct configfs_group_operations comms_ops = {
318e7fd4179SDavid Teigland 	.make_item = make_comm,
319e7fd4179SDavid Teigland 	.drop_item = drop_comm,
320e7fd4179SDavid Teigland };
321e7fd4179SDavid Teigland 
322e7fd4179SDavid Teigland static struct configfs_item_operations comm_ops = {
323e7fd4179SDavid Teigland 	.release = release_comm,
324e7fd4179SDavid Teigland };
325e7fd4179SDavid Teigland 
326e7fd4179SDavid Teigland static struct configfs_group_operations nodes_ops = {
327e7fd4179SDavid Teigland 	.make_item = make_node,
328e7fd4179SDavid Teigland 	.drop_item = drop_node,
329e7fd4179SDavid Teigland };
330e7fd4179SDavid Teigland 
331e7fd4179SDavid Teigland static struct configfs_item_operations node_ops = {
332e7fd4179SDavid Teigland 	.release = release_node,
333e7fd4179SDavid Teigland };
334e7fd4179SDavid Teigland 
335761594b7SBhumika Goyal static const struct config_item_type clusters_type = {
336e7fd4179SDavid Teigland 	.ct_group_ops = &clusters_ops,
337e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
338e7fd4179SDavid Teigland };
339e7fd4179SDavid Teigland 
340761594b7SBhumika Goyal static const struct config_item_type cluster_type = {
341e7fd4179SDavid Teigland 	.ct_item_ops = &cluster_ops,
342d200778eSDavid Teigland 	.ct_attrs = cluster_attrs,
343e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
344e7fd4179SDavid Teigland };
345e7fd4179SDavid Teigland 
346761594b7SBhumika Goyal static const struct config_item_type spaces_type = {
347e7fd4179SDavid Teigland 	.ct_group_ops = &spaces_ops,
348e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
349e7fd4179SDavid Teigland };
350e7fd4179SDavid Teigland 
351761594b7SBhumika Goyal static const struct config_item_type space_type = {
352e7fd4179SDavid Teigland 	.ct_item_ops = &space_ops,
353e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
354e7fd4179SDavid Teigland };
355e7fd4179SDavid Teigland 
356761594b7SBhumika Goyal static const struct config_item_type comms_type = {
357e7fd4179SDavid Teigland 	.ct_group_ops = &comms_ops,
358e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
359e7fd4179SDavid Teigland };
360e7fd4179SDavid Teigland 
361761594b7SBhumika Goyal static const struct config_item_type comm_type = {
362e7fd4179SDavid Teigland 	.ct_item_ops = &comm_ops,
363e7fd4179SDavid Teigland 	.ct_attrs = comm_attrs,
364e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
365e7fd4179SDavid Teigland };
366e7fd4179SDavid Teigland 
367761594b7SBhumika Goyal static const struct config_item_type nodes_type = {
368e7fd4179SDavid Teigland 	.ct_group_ops = &nodes_ops,
369e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
370e7fd4179SDavid Teigland };
371e7fd4179SDavid Teigland 
372761594b7SBhumika Goyal static const struct config_item_type node_type = {
373e7fd4179SDavid Teigland 	.ct_item_ops = &node_ops,
374e7fd4179SDavid Teigland 	.ct_attrs = node_attrs,
375e7fd4179SDavid Teigland 	.ct_owner = THIS_MODULE,
376e7fd4179SDavid Teigland };
377e7fd4179SDavid Teigland 
config_item_to_space(struct config_item * i)37827eccf46SAndrew Morton static struct dlm_space *config_item_to_space(struct config_item *i)
379e7fd4179SDavid Teigland {
38051409340SDavid Teigland 	return i ? container_of(to_config_group(i), struct dlm_space, group) :
38151409340SDavid Teigland 		   NULL;
382e7fd4179SDavid Teigland }
383e7fd4179SDavid Teigland 
config_item_to_comm(struct config_item * i)38427eccf46SAndrew Morton static struct dlm_comm *config_item_to_comm(struct config_item *i)
385e7fd4179SDavid Teigland {
38651409340SDavid Teigland 	return i ? container_of(i, struct dlm_comm, item) : NULL;
387e7fd4179SDavid Teigland }
388e7fd4179SDavid Teigland 
config_item_to_node(struct config_item * i)38927eccf46SAndrew Morton static struct dlm_node *config_item_to_node(struct config_item *i)
390e7fd4179SDavid Teigland {
39151409340SDavid Teigland 	return i ? container_of(i, struct dlm_node, item) : NULL;
392e7fd4179SDavid Teigland }
393e7fd4179SDavid Teigland 
make_cluster(struct config_group * g,const char * name)394f89ab861SJoel Becker static struct config_group *make_cluster(struct config_group *g,
395f89ab861SJoel Becker 					 const char *name)
396e7fd4179SDavid Teigland {
39751409340SDavid Teigland 	struct dlm_cluster *cl = NULL;
39851409340SDavid Teigland 	struct dlm_spaces *sps = NULL;
39951409340SDavid Teigland 	struct dlm_comms *cms = NULL;
400e7fd4179SDavid Teigland 
401573c24c4SDavid Teigland 	cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
402573c24c4SDavid Teigland 	sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
403573c24c4SDavid Teigland 	cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
404e7fd4179SDavid Teigland 
40582c7d823SAndrew Price 	if (!cl || !sps || !cms)
406e7fd4179SDavid Teigland 		goto fail;
407e7fd4179SDavid Teigland 
408700ab1c3SAlexander Aring 	cl->sps = sps;
409700ab1c3SAlexander Aring 	cl->cms = cms;
410700ab1c3SAlexander Aring 
411e7fd4179SDavid Teigland 	config_group_init_type_name(&cl->group, name, &cluster_type);
412e7fd4179SDavid Teigland 	config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
413e7fd4179SDavid Teigland 	config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
414e7fd4179SDavid Teigland 
4151ae1602dSChristoph Hellwig 	configfs_add_default_group(&sps->ss_group, &cl->group);
4161ae1602dSChristoph Hellwig 	configfs_add_default_group(&cms->cs_group, &cl->group);
417e7fd4179SDavid Teigland 
418d200778eSDavid Teigland 	cl->cl_tcp_port = dlm_config.ci_tcp_port;
419d200778eSDavid Teigland 	cl->cl_buffer_size = dlm_config.ci_buffer_size;
420d200778eSDavid Teigland 	cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
421d200778eSDavid Teigland 	cl->cl_recover_timer = dlm_config.ci_recover_timer;
422d200778eSDavid Teigland 	cl->cl_toss_secs = dlm_config.ci_toss_secs;
423d200778eSDavid Teigland 	cl->cl_scan_secs = dlm_config.ci_scan_secs;
424d200778eSDavid Teigland 	cl->cl_log_debug = dlm_config.ci_log_debug;
425505ee528SZhilong Liu 	cl->cl_log_info = dlm_config.ci_log_info;
4260b7cac0fSDavid Teigland 	cl->cl_protocol = dlm_config.ci_protocol;
4273881ac04SDavid Teigland 	cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
42860f98d18SDavid Teigland 	cl->cl_recover_callbacks = dlm_config.ci_recover_callbacks;
42960f98d18SDavid Teigland 	memcpy(cl->cl_cluster_name, dlm_config.ci_cluster_name,
43060f98d18SDavid Teigland 	       DLM_LOCKSPACE_LEN);
431d200778eSDavid Teigland 
432e7fd4179SDavid Teigland 	space_list = &sps->ss_group;
433e7fd4179SDavid Teigland 	comm_list = &cms->cs_group;
434f89ab861SJoel Becker 	return &cl->group;
435e7fd4179SDavid Teigland 
436e7fd4179SDavid Teigland  fail:
437e7fd4179SDavid Teigland 	kfree(cl);
438e7fd4179SDavid Teigland 	kfree(sps);
439e7fd4179SDavid Teigland 	kfree(cms);
440a6795e9eSJoel Becker 	return ERR_PTR(-ENOMEM);
441e7fd4179SDavid Teigland }
442e7fd4179SDavid Teigland 
drop_cluster(struct config_group * g,struct config_item * i)443e7fd4179SDavid Teigland static void drop_cluster(struct config_group *g, struct config_item *i)
444e7fd4179SDavid Teigland {
44527eccf46SAndrew Morton 	struct dlm_cluster *cl = config_item_to_cluster(i);
446e7fd4179SDavid Teigland 
4471ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&cl->group);
448e7fd4179SDavid Teigland 
449e7fd4179SDavid Teigland 	space_list = NULL;
450e7fd4179SDavid Teigland 	comm_list = NULL;
451e7fd4179SDavid Teigland 
452e7fd4179SDavid Teigland 	config_item_put(i);
453e7fd4179SDavid Teigland }
454e7fd4179SDavid Teigland 
release_cluster(struct config_item * i)455e7fd4179SDavid Teigland static void release_cluster(struct config_item *i)
456e7fd4179SDavid Teigland {
45727eccf46SAndrew Morton 	struct dlm_cluster *cl = config_item_to_cluster(i);
458700ab1c3SAlexander Aring 
459700ab1c3SAlexander Aring 	kfree(cl->sps);
460700ab1c3SAlexander Aring 	kfree(cl->cms);
461e7fd4179SDavid Teigland 	kfree(cl);
462e7fd4179SDavid Teigland }
463e7fd4179SDavid Teigland 
make_space(struct config_group * g,const char * name)464f89ab861SJoel Becker static struct config_group *make_space(struct config_group *g, const char *name)
465e7fd4179SDavid Teigland {
46651409340SDavid Teigland 	struct dlm_space *sp = NULL;
46751409340SDavid Teigland 	struct dlm_nodes *nds = NULL;
468e7fd4179SDavid Teigland 
469573c24c4SDavid Teigland 	sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
470573c24c4SDavid Teigland 	nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
471e7fd4179SDavid Teigland 
4721ae1602dSChristoph Hellwig 	if (!sp || !nds)
473e7fd4179SDavid Teigland 		goto fail;
474e7fd4179SDavid Teigland 
475e7fd4179SDavid Teigland 	config_group_init_type_name(&sp->group, name, &space_type);
476e7fd4179SDavid Teigland 
4771ae1602dSChristoph Hellwig 	config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
4781ae1602dSChristoph Hellwig 	configfs_add_default_group(&nds->ns_group, &sp->group);
479e7fd4179SDavid Teigland 
480e7fd4179SDavid Teigland 	INIT_LIST_HEAD(&sp->members);
48190135925SDavid Teigland 	mutex_init(&sp->members_lock);
482e7fd4179SDavid Teigland 	sp->members_count = 0;
4833d2825c8SAlexander Aring 	sp->nds = nds;
484f89ab861SJoel Becker 	return &sp->group;
485e7fd4179SDavid Teigland 
486e7fd4179SDavid Teigland  fail:
487e7fd4179SDavid Teigland 	kfree(sp);
488e7fd4179SDavid Teigland 	kfree(nds);
489a6795e9eSJoel Becker 	return ERR_PTR(-ENOMEM);
490e7fd4179SDavid Teigland }
491e7fd4179SDavid Teigland 
drop_space(struct config_group * g,struct config_item * i)492e7fd4179SDavid Teigland static void drop_space(struct config_group *g, struct config_item *i)
493e7fd4179SDavid Teigland {
49427eccf46SAndrew Morton 	struct dlm_space *sp = config_item_to_space(i);
495e7fd4179SDavid Teigland 
496e7fd4179SDavid Teigland 	/* assert list_empty(&sp->members) */
497e7fd4179SDavid Teigland 
4981ae1602dSChristoph Hellwig 	configfs_remove_default_groups(&sp->group);
499e7fd4179SDavid Teigland 	config_item_put(i);
500e7fd4179SDavid Teigland }
501e7fd4179SDavid Teigland 
release_space(struct config_item * i)502e7fd4179SDavid Teigland static void release_space(struct config_item *i)
503e7fd4179SDavid Teigland {
50427eccf46SAndrew Morton 	struct dlm_space *sp = config_item_to_space(i);
5053d2825c8SAlexander Aring 	kfree(sp->nds);
506e7fd4179SDavid Teigland 	kfree(sp);
507e7fd4179SDavid Teigland }
508e7fd4179SDavid Teigland 
make_comm(struct config_group * g,const char * name)509f89ab861SJoel Becker static struct config_item *make_comm(struct config_group *g, const char *name)
510e7fd4179SDavid Teigland {
51151409340SDavid Teigland 	struct dlm_comm *cm;
512e7fd4179SDavid Teigland 
513573c24c4SDavid Teigland 	cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
514e7fd4179SDavid Teigland 	if (!cm)
515a6795e9eSJoel Becker 		return ERR_PTR(-ENOMEM);
516e7fd4179SDavid Teigland 
517e7fd4179SDavid Teigland 	config_item_init_type_name(&cm->item, name, &comm_type);
51860f98d18SDavid Teigland 
51960f98d18SDavid Teigland 	cm->seq = dlm_comm_count++;
52060f98d18SDavid Teigland 	if (!cm->seq)
52160f98d18SDavid Teigland 		cm->seq = dlm_comm_count++;
52260f98d18SDavid Teigland 
523e7fd4179SDavid Teigland 	cm->nodeid = -1;
524e7fd4179SDavid Teigland 	cm->local = 0;
525e7fd4179SDavid Teigland 	cm->addr_count = 0;
5269c9f168fSAlexander Aring 	cm->mark = 0;
527f89ab861SJoel Becker 	return &cm->item;
528e7fd4179SDavid Teigland }
529e7fd4179SDavid Teigland 
drop_comm(struct config_group * g,struct config_item * i)530e7fd4179SDavid Teigland static void drop_comm(struct config_group *g, struct config_item *i)
531e7fd4179SDavid Teigland {
53227eccf46SAndrew Morton 	struct dlm_comm *cm = config_item_to_comm(i);
533e7fd4179SDavid Teigland 	if (local_comm == cm)
534e7fd4179SDavid Teigland 		local_comm = NULL;
535a070a91cSAlexander Aring 	dlm_midcomms_close(cm->nodeid);
536e7fd4179SDavid Teigland 	while (cm->addr_count--)
537e7fd4179SDavid Teigland 		kfree(cm->addr[cm->addr_count]);
538e7fd4179SDavid Teigland 	config_item_put(i);
539e7fd4179SDavid Teigland }
540e7fd4179SDavid Teigland 
release_comm(struct config_item * i)541e7fd4179SDavid Teigland static void release_comm(struct config_item *i)
542e7fd4179SDavid Teigland {
54327eccf46SAndrew Morton 	struct dlm_comm *cm = config_item_to_comm(i);
544e7fd4179SDavid Teigland 	kfree(cm);
545e7fd4179SDavid Teigland }
546e7fd4179SDavid Teigland 
make_node(struct config_group * g,const char * name)547f89ab861SJoel Becker static struct config_item *make_node(struct config_group *g, const char *name)
548e7fd4179SDavid Teigland {
54927eccf46SAndrew Morton 	struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
55051409340SDavid Teigland 	struct dlm_node *nd;
551e7fd4179SDavid Teigland 
552573c24c4SDavid Teigland 	nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
553e7fd4179SDavid Teigland 	if (!nd)
554a6795e9eSJoel Becker 		return ERR_PTR(-ENOMEM);
555e7fd4179SDavid Teigland 
556e7fd4179SDavid Teigland 	config_item_init_type_name(&nd->item, name, &node_type);
557e7fd4179SDavid Teigland 	nd->nodeid = -1;
558e7fd4179SDavid Teigland 	nd->weight = 1;  /* default weight of 1 if none is set */
559d44e0fc7SDavid Teigland 	nd->new = 1;     /* set to 0 once it's been read by dlm_nodeid_list() */
560e7fd4179SDavid Teigland 
56190135925SDavid Teigland 	mutex_lock(&sp->members_lock);
562e7fd4179SDavid Teigland 	list_add(&nd->list, &sp->members);
563e7fd4179SDavid Teigland 	sp->members_count++;
56490135925SDavid Teigland 	mutex_unlock(&sp->members_lock);
565e7fd4179SDavid Teigland 
566f89ab861SJoel Becker 	return &nd->item;
567e7fd4179SDavid Teigland }
568e7fd4179SDavid Teigland 
drop_node(struct config_group * g,struct config_item * i)569e7fd4179SDavid Teigland static void drop_node(struct config_group *g, struct config_item *i)
570e7fd4179SDavid Teigland {
57127eccf46SAndrew Morton 	struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
57227eccf46SAndrew Morton 	struct dlm_node *nd = config_item_to_node(i);
573e7fd4179SDavid Teigland 
57490135925SDavid Teigland 	mutex_lock(&sp->members_lock);
575e7fd4179SDavid Teigland 	list_del(&nd->list);
576e7fd4179SDavid Teigland 	sp->members_count--;
57790135925SDavid Teigland 	mutex_unlock(&sp->members_lock);
578e7fd4179SDavid Teigland 
579e7fd4179SDavid Teigland 	config_item_put(i);
580e7fd4179SDavid Teigland }
581e7fd4179SDavid Teigland 
release_node(struct config_item * i)582e7fd4179SDavid Teigland static void release_node(struct config_item *i)
583e7fd4179SDavid Teigland {
58427eccf46SAndrew Morton 	struct dlm_node *nd = config_item_to_node(i);
585e7fd4179SDavid Teigland 	kfree(nd);
586e7fd4179SDavid Teigland }
587e7fd4179SDavid Teigland 
58851409340SDavid Teigland static struct dlm_clusters clusters_root = {
589e7fd4179SDavid Teigland 	.subsys = {
590e7fd4179SDavid Teigland 		.su_group = {
591e7fd4179SDavid Teigland 			.cg_item = {
592e7fd4179SDavid Teigland 				.ci_namebuf = "dlm",
593e7fd4179SDavid Teigland 				.ci_type = &clusters_type,
594e7fd4179SDavid Teigland 			},
595e7fd4179SDavid Teigland 		},
596e7fd4179SDavid Teigland 	},
597e7fd4179SDavid Teigland };
598e7fd4179SDavid Teigland 
dlm_config_init(void)59930727174SDenis Cheng int __init dlm_config_init(void)
600e7fd4179SDavid Teigland {
601e7fd4179SDavid Teigland 	config_group_init(&clusters_root.subsys.su_group);
602e6bd07aeSJoel Becker 	mutex_init(&clusters_root.subsys.su_mutex);
603e7fd4179SDavid Teigland 	return configfs_register_subsystem(&clusters_root.subsys);
604e7fd4179SDavid Teigland }
605e7fd4179SDavid Teigland 
dlm_config_exit(void)606e7fd4179SDavid Teigland void dlm_config_exit(void)
607e7fd4179SDavid Teigland {
608e7fd4179SDavid Teigland 	configfs_unregister_subsystem(&clusters_root.subsys);
609e7fd4179SDavid Teigland }
610e7fd4179SDavid Teigland 
611e7fd4179SDavid Teigland /*
612e7fd4179SDavid Teigland  * Functions for user space to read/write attributes
613e7fd4179SDavid Teigland  */
614e7fd4179SDavid Teigland 
comm_nodeid_show(struct config_item * item,char * buf)6159ae0f367SChristoph Hellwig static ssize_t comm_nodeid_show(struct config_item *item, char *buf)
616d200778eSDavid Teigland {
6179ae0f367SChristoph Hellwig 	return sprintf(buf, "%d\n", config_item_to_comm(item)->nodeid);
618d200778eSDavid Teigland }
619d200778eSDavid Teigland 
comm_nodeid_store(struct config_item * item,const char * buf,size_t len)6209ae0f367SChristoph Hellwig static ssize_t comm_nodeid_store(struct config_item *item, const char *buf,
62151409340SDavid Teigland 				 size_t len)
622e7fd4179SDavid Teigland {
6239ae0f367SChristoph Hellwig 	int rc = kstrtoint(buf, 0, &config_item_to_comm(item)->nodeid);
6244f4c337fSFabian Frederick 
6254f4c337fSFabian Frederick 	if (rc)
6264f4c337fSFabian Frederick 		return rc;
627e7fd4179SDavid Teigland 	return len;
628e7fd4179SDavid Teigland }
629e7fd4179SDavid Teigland 
comm_local_show(struct config_item * item,char * buf)6309ae0f367SChristoph Hellwig static ssize_t comm_local_show(struct config_item *item, char *buf)
631e7fd4179SDavid Teigland {
6329ae0f367SChristoph Hellwig 	return sprintf(buf, "%d\n", config_item_to_comm(item)->local);
633e7fd4179SDavid Teigland }
634e7fd4179SDavid Teigland 
comm_local_store(struct config_item * item,const char * buf,size_t len)6359ae0f367SChristoph Hellwig static ssize_t comm_local_store(struct config_item *item, const char *buf,
63651409340SDavid Teigland 				size_t len)
637e7fd4179SDavid Teigland {
6389ae0f367SChristoph Hellwig 	struct dlm_comm *cm = config_item_to_comm(item);
6394f4c337fSFabian Frederick 	int rc = kstrtoint(buf, 0, &cm->local);
6404f4c337fSFabian Frederick 
6414f4c337fSFabian Frederick 	if (rc)
6424f4c337fSFabian Frederick 		return rc;
643e7fd4179SDavid Teigland 	if (cm->local && !local_comm)
644e7fd4179SDavid Teigland 		local_comm = cm;
645e7fd4179SDavid Teigland 	return len;
646e7fd4179SDavid Teigland }
647e7fd4179SDavid Teigland 
comm_addr_store(struct config_item * item,const char * buf,size_t len)6489ae0f367SChristoph Hellwig static ssize_t comm_addr_store(struct config_item *item, const char *buf,
6499ae0f367SChristoph Hellwig 		size_t len)
650e7fd4179SDavid Teigland {
6519ae0f367SChristoph Hellwig 	struct dlm_comm *cm = config_item_to_comm(item);
652e7fd4179SDavid Teigland 	struct sockaddr_storage *addr;
65336b71a8bSDavid Teigland 	int rv;
654e7fd4179SDavid Teigland 
655e7fd4179SDavid Teigland 	if (len != sizeof(struct sockaddr_storage))
656e7fd4179SDavid Teigland 		return -EINVAL;
657e7fd4179SDavid Teigland 
658e7fd4179SDavid Teigland 	if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
659e7fd4179SDavid Teigland 		return -ENOSPC;
660e7fd4179SDavid Teigland 
661573c24c4SDavid Teigland 	addr = kzalloc(sizeof(*addr), GFP_NOFS);
662e7fd4179SDavid Teigland 	if (!addr)
663e7fd4179SDavid Teigland 		return -ENOMEM;
664e7fd4179SDavid Teigland 
665e7fd4179SDavid Teigland 	memcpy(addr, buf, len);
66636b71a8bSDavid Teigland 
667*63e711b0SAlexander Aring 	rv = dlm_midcomms_addr(cm->nodeid, addr, len);
66836b71a8bSDavid Teigland 	if (rv) {
66936b71a8bSDavid Teigland 		kfree(addr);
67036b71a8bSDavid Teigland 		return rv;
67136b71a8bSDavid Teigland 	}
67236b71a8bSDavid Teigland 
673e7fd4179SDavid Teigland 	cm->addr[cm->addr_count++] = addr;
674e7fd4179SDavid Teigland 	return len;
675e7fd4179SDavid Teigland }
676e7fd4179SDavid Teigland 
comm_addr_list_show(struct config_item * item,char * buf)6779ae0f367SChristoph Hellwig static ssize_t comm_addr_list_show(struct config_item *item, char *buf)
67855b3286dSMasatake YAMATO {
6799ae0f367SChristoph Hellwig 	struct dlm_comm *cm = config_item_to_comm(item);
68055b3286dSMasatake YAMATO 	ssize_t s;
68155b3286dSMasatake YAMATO 	ssize_t allowance;
68255b3286dSMasatake YAMATO 	int i;
68355b3286dSMasatake YAMATO 	struct sockaddr_storage *addr;
68455b3286dSMasatake YAMATO 	struct sockaddr_in *addr_in;
68555b3286dSMasatake YAMATO 	struct sockaddr_in6 *addr_in6;
68655b3286dSMasatake YAMATO 
68755b3286dSMasatake YAMATO 	/* Taken from ip6_addr_string() defined in lib/vsprintf.c */
68855b3286dSMasatake YAMATO 	char buf0[sizeof("AF_INET6	xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
68955b3286dSMasatake YAMATO 
69055b3286dSMasatake YAMATO 
69155b3286dSMasatake YAMATO 	/* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
69255b3286dSMasatake YAMATO 	allowance = 4096;
69355b3286dSMasatake YAMATO 	buf[0] = '\0';
69455b3286dSMasatake YAMATO 
69555b3286dSMasatake YAMATO 	for (i = 0; i < cm->addr_count; i++) {
69655b3286dSMasatake YAMATO 		addr = cm->addr[i];
69755b3286dSMasatake YAMATO 
69855b3286dSMasatake YAMATO 		switch(addr->ss_family) {
69955b3286dSMasatake YAMATO 		case AF_INET:
70055b3286dSMasatake YAMATO 			addr_in = (struct sockaddr_in *)addr;
70155b3286dSMasatake YAMATO 			s = sprintf(buf0, "AF_INET	%pI4\n", &addr_in->sin_addr.s_addr);
70255b3286dSMasatake YAMATO 			break;
70355b3286dSMasatake YAMATO 		case AF_INET6:
70455b3286dSMasatake YAMATO 			addr_in6 = (struct sockaddr_in6 *)addr;
70555b3286dSMasatake YAMATO 			s = sprintf(buf0, "AF_INET6	%pI6\n", &addr_in6->sin6_addr);
70655b3286dSMasatake YAMATO 			break;
70755b3286dSMasatake YAMATO 		default:
70855b3286dSMasatake YAMATO 			s = sprintf(buf0, "%s\n", "<UNKNOWN>");
70955b3286dSMasatake YAMATO 			break;
71055b3286dSMasatake YAMATO 		}
71155b3286dSMasatake YAMATO 		allowance -= s;
71255b3286dSMasatake YAMATO 		if (allowance >= 0)
71355b3286dSMasatake YAMATO 			strcat(buf, buf0);
71455b3286dSMasatake YAMATO 		else {
71555b3286dSMasatake YAMATO 			allowance += s;
71655b3286dSMasatake YAMATO 			break;
71755b3286dSMasatake YAMATO 		}
71855b3286dSMasatake YAMATO 	}
71955b3286dSMasatake YAMATO 	return 4096 - allowance;
72055b3286dSMasatake YAMATO }
72155b3286dSMasatake YAMATO 
comm_mark_show(struct config_item * item,char * buf)7229c9f168fSAlexander Aring static ssize_t comm_mark_show(struct config_item *item, char *buf)
7239c9f168fSAlexander Aring {
7249c9f168fSAlexander Aring 	return sprintf(buf, "%u\n", config_item_to_comm(item)->mark);
7259c9f168fSAlexander Aring }
7269c9f168fSAlexander Aring 
comm_mark_store(struct config_item * item,const char * buf,size_t len)7279c9f168fSAlexander Aring static ssize_t comm_mark_store(struct config_item *item, const char *buf,
7289c9f168fSAlexander Aring 			       size_t len)
7299c9f168fSAlexander Aring {
730e125fbebSAlexander Aring 	struct dlm_comm *comm;
7319c9f168fSAlexander Aring 	unsigned int mark;
7329c9f168fSAlexander Aring 	int rc;
7339c9f168fSAlexander Aring 
7349c9f168fSAlexander Aring 	rc = kstrtouint(buf, 0, &mark);
7359c9f168fSAlexander Aring 	if (rc)
7369c9f168fSAlexander Aring 		return rc;
7379c9f168fSAlexander Aring 
738e125fbebSAlexander Aring 	if (mark == 0)
739e125fbebSAlexander Aring 		mark = dlm_config.ci_mark;
740e125fbebSAlexander Aring 
741e125fbebSAlexander Aring 	comm = config_item_to_comm(item);
742e125fbebSAlexander Aring 	rc = dlm_lowcomms_nodes_set_mark(comm->nodeid, mark);
743e125fbebSAlexander Aring 	if (rc)
744e125fbebSAlexander Aring 		return rc;
745e125fbebSAlexander Aring 
746e125fbebSAlexander Aring 	comm->mark = mark;
7479c9f168fSAlexander Aring 	return len;
7489c9f168fSAlexander Aring }
7499c9f168fSAlexander Aring 
7509ae0f367SChristoph Hellwig CONFIGFS_ATTR(comm_, nodeid);
7519ae0f367SChristoph Hellwig CONFIGFS_ATTR(comm_, local);
7529c9f168fSAlexander Aring CONFIGFS_ATTR(comm_, mark);
7539ae0f367SChristoph Hellwig CONFIGFS_ATTR_WO(comm_, addr);
7549ae0f367SChristoph Hellwig CONFIGFS_ATTR_RO(comm_, addr_list);
7559ae0f367SChristoph Hellwig 
7569ae0f367SChristoph Hellwig static struct configfs_attribute *comm_attrs[] = {
7579ae0f367SChristoph Hellwig 	[COMM_ATTR_NODEID] = &comm_attr_nodeid,
7589ae0f367SChristoph Hellwig 	[COMM_ATTR_LOCAL] = &comm_attr_local,
7599ae0f367SChristoph Hellwig 	[COMM_ATTR_ADDR] = &comm_attr_addr,
7609ae0f367SChristoph Hellwig 	[COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list,
7619c9f168fSAlexander Aring 	[COMM_ATTR_MARK] = &comm_attr_mark,
7629ae0f367SChristoph Hellwig 	NULL,
7639ae0f367SChristoph Hellwig };
7649ae0f367SChristoph Hellwig 
node_nodeid_show(struct config_item * item,char * buf)7659ae0f367SChristoph Hellwig static ssize_t node_nodeid_show(struct config_item *item, char *buf)
766e7fd4179SDavid Teigland {
7679ae0f367SChristoph Hellwig 	return sprintf(buf, "%d\n", config_item_to_node(item)->nodeid);
768e7fd4179SDavid Teigland }
769e7fd4179SDavid Teigland 
node_nodeid_store(struct config_item * item,const char * buf,size_t len)7709ae0f367SChristoph Hellwig static ssize_t node_nodeid_store(struct config_item *item, const char *buf,
77151409340SDavid Teigland 				 size_t len)
772e7fd4179SDavid Teigland {
7739ae0f367SChristoph Hellwig 	struct dlm_node *nd = config_item_to_node(item);
77460f98d18SDavid Teigland 	uint32_t seq = 0;
7754f4c337fSFabian Frederick 	int rc = kstrtoint(buf, 0, &nd->nodeid);
7764f4c337fSFabian Frederick 
7774f4c337fSFabian Frederick 	if (rc)
7784f4c337fSFabian Frederick 		return rc;
77960f98d18SDavid Teigland 	dlm_comm_seq(nd->nodeid, &seq);
78060f98d18SDavid Teigland 	nd->comm_seq = seq;
781e7fd4179SDavid Teigland 	return len;
782e7fd4179SDavid Teigland }
783e7fd4179SDavid Teigland 
node_weight_show(struct config_item * item,char * buf)7849ae0f367SChristoph Hellwig static ssize_t node_weight_show(struct config_item *item, char *buf)
785e7fd4179SDavid Teigland {
7869ae0f367SChristoph Hellwig 	return sprintf(buf, "%d\n", config_item_to_node(item)->weight);
787e7fd4179SDavid Teigland }
788e7fd4179SDavid Teigland 
node_weight_store(struct config_item * item,const char * buf,size_t len)7899ae0f367SChristoph Hellwig static ssize_t node_weight_store(struct config_item *item, const char *buf,
79051409340SDavid Teigland 				 size_t len)
791e7fd4179SDavid Teigland {
7929ae0f367SChristoph Hellwig 	int rc = kstrtoint(buf, 0, &config_item_to_node(item)->weight);
7934f4c337fSFabian Frederick 
7944f4c337fSFabian Frederick 	if (rc)
7954f4c337fSFabian Frederick 		return rc;
796e7fd4179SDavid Teigland 	return len;
797e7fd4179SDavid Teigland }
798e7fd4179SDavid Teigland 
7999ae0f367SChristoph Hellwig CONFIGFS_ATTR(node_, nodeid);
8009ae0f367SChristoph Hellwig CONFIGFS_ATTR(node_, weight);
8019ae0f367SChristoph Hellwig 
8029ae0f367SChristoph Hellwig static struct configfs_attribute *node_attrs[] = {
8039ae0f367SChristoph Hellwig 	[NODE_ATTR_NODEID] = &node_attr_nodeid,
8049ae0f367SChristoph Hellwig 	[NODE_ATTR_WEIGHT] = &node_attr_weight,
8059ae0f367SChristoph Hellwig 	NULL,
8069ae0f367SChristoph Hellwig };
8079ae0f367SChristoph Hellwig 
808e7fd4179SDavid Teigland /*
809e7fd4179SDavid Teigland  * Functions for the dlm to get the info that's been configured
810e7fd4179SDavid Teigland  */
811e7fd4179SDavid Teigland 
get_space(char * name)81251409340SDavid Teigland static struct dlm_space *get_space(char *name)
813e7fd4179SDavid Teigland {
8143168b078SSatyam Sharma 	struct config_item *i;
8153168b078SSatyam Sharma 
816e7fd4179SDavid Teigland 	if (!space_list)
817e7fd4179SDavid Teigland 		return NULL;
8183168b078SSatyam Sharma 
819e6bd07aeSJoel Becker 	mutex_lock(&space_list->cg_subsys->su_mutex);
8203fe6c5ceSSatyam Sharma 	i = config_group_find_item(space_list, name);
821e6bd07aeSJoel Becker 	mutex_unlock(&space_list->cg_subsys->su_mutex);
8223168b078SSatyam Sharma 
82327eccf46SAndrew Morton 	return config_item_to_space(i);
824e7fd4179SDavid Teigland }
825e7fd4179SDavid Teigland 
put_space(struct dlm_space * sp)82651409340SDavid Teigland static void put_space(struct dlm_space *sp)
827e7fd4179SDavid Teigland {
828e7fd4179SDavid Teigland 	config_item_put(&sp->group.cg_item);
829e7fd4179SDavid Teigland }
830e7fd4179SDavid Teigland 
get_comm(int nodeid)83136b71a8bSDavid Teigland static struct dlm_comm *get_comm(int nodeid)
832e7fd4179SDavid Teigland {
833e7fd4179SDavid Teigland 	struct config_item *i;
83451409340SDavid Teigland 	struct dlm_comm *cm = NULL;
835e7fd4179SDavid Teigland 	int found = 0;
836e7fd4179SDavid Teigland 
837e7fd4179SDavid Teigland 	if (!comm_list)
838e7fd4179SDavid Teigland 		return NULL;
839e7fd4179SDavid Teigland 
840e6bd07aeSJoel Becker 	mutex_lock(&clusters_root.subsys.su_mutex);
841e7fd4179SDavid Teigland 
842e7fd4179SDavid Teigland 	list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
84327eccf46SAndrew Morton 		cm = config_item_to_comm(i);
844e7fd4179SDavid Teigland 
845e7fd4179SDavid Teigland 		if (cm->nodeid != nodeid)
846e7fd4179SDavid Teigland 			continue;
847e7fd4179SDavid Teigland 		found = 1;
8483168b078SSatyam Sharma 		config_item_get(i);
849e7fd4179SDavid Teigland 		break;
850e7fd4179SDavid Teigland 	}
851e6bd07aeSJoel Becker 	mutex_unlock(&clusters_root.subsys.su_mutex);
852e7fd4179SDavid Teigland 
8533168b078SSatyam Sharma 	if (!found)
854e7fd4179SDavid Teigland 		cm = NULL;
855e7fd4179SDavid Teigland 	return cm;
856e7fd4179SDavid Teigland }
857e7fd4179SDavid Teigland 
put_comm(struct dlm_comm * cm)85851409340SDavid Teigland static void put_comm(struct dlm_comm *cm)
859e7fd4179SDavid Teigland {
860e7fd4179SDavid Teigland 	config_item_put(&cm->item);
861e7fd4179SDavid Teigland }
862e7fd4179SDavid Teigland 
863e7fd4179SDavid Teigland /* caller must free mem */
dlm_config_nodes(char * lsname,struct dlm_config_node ** nodes_out,int * count_out)86460f98d18SDavid Teigland int dlm_config_nodes(char *lsname, struct dlm_config_node **nodes_out,
86560f98d18SDavid Teigland 		     int *count_out)
866e7fd4179SDavid Teigland {
86751409340SDavid Teigland 	struct dlm_space *sp;
86851409340SDavid Teigland 	struct dlm_node *nd;
86960f98d18SDavid Teigland 	struct dlm_config_node *nodes, *node;
87060f98d18SDavid Teigland 	int rv, count;
871e7fd4179SDavid Teigland 
872e7fd4179SDavid Teigland 	sp = get_space(lsname);
873e7fd4179SDavid Teigland 	if (!sp)
874e7fd4179SDavid Teigland 		return -EEXIST;
875e7fd4179SDavid Teigland 
87690135925SDavid Teigland 	mutex_lock(&sp->members_lock);
877e7fd4179SDavid Teigland 	if (!sp->members_count) {
878d44e0fc7SDavid Teigland 		rv = -EINVAL;
879d44e0fc7SDavid Teigland 		printk(KERN_ERR "dlm: zero members_count\n");
880e7fd4179SDavid Teigland 		goto out;
881e7fd4179SDavid Teigland 	}
882e7fd4179SDavid Teigland 
88360f98d18SDavid Teigland 	count = sp->members_count;
884d44e0fc7SDavid Teigland 
88560f98d18SDavid Teigland 	nodes = kcalloc(count, sizeof(struct dlm_config_node), GFP_NOFS);
88660f98d18SDavid Teigland 	if (!nodes) {
887e7fd4179SDavid Teigland 		rv = -ENOMEM;
888e7fd4179SDavid Teigland 		goto out;
889e7fd4179SDavid Teigland 	}
890e7fd4179SDavid Teigland 
89160f98d18SDavid Teigland 	node = nodes;
892d44e0fc7SDavid Teigland 	list_for_each_entry(nd, &sp->members, list) {
89360f98d18SDavid Teigland 		node->nodeid = nd->nodeid;
89460f98d18SDavid Teigland 		node->weight = nd->weight;
89560f98d18SDavid Teigland 		node->new = nd->new;
89660f98d18SDavid Teigland 		node->comm_seq = nd->comm_seq;
89760f98d18SDavid Teigland 		node++;
898e7fd4179SDavid Teigland 
899d44e0fc7SDavid Teigland 		nd->new = 0;
900d44e0fc7SDavid Teigland 	}
901d44e0fc7SDavid Teigland 
90260f98d18SDavid Teigland 	*count_out = count;
90360f98d18SDavid Teigland 	*nodes_out = nodes;
90460f98d18SDavid Teigland 	rv = 0;
905e7fd4179SDavid Teigland  out:
90690135925SDavid Teigland 	mutex_unlock(&sp->members_lock);
907e7fd4179SDavid Teigland 	put_space(sp);
908e7fd4179SDavid Teigland 	return rv;
909e7fd4179SDavid Teigland }
910e7fd4179SDavid Teigland 
dlm_comm_seq(int nodeid,uint32_t * seq)91160f98d18SDavid Teigland int dlm_comm_seq(int nodeid, uint32_t *seq)
912e7fd4179SDavid Teigland {
91336b71a8bSDavid Teigland 	struct dlm_comm *cm = get_comm(nodeid);
91460f98d18SDavid Teigland 	if (!cm)
91560f98d18SDavid Teigland 		return -EEXIST;
91660f98d18SDavid Teigland 	*seq = cm->seq;
91760f98d18SDavid Teigland 	put_comm(cm);
91860f98d18SDavid Teigland 	return 0;
919e7fd4179SDavid Teigland }
920e7fd4179SDavid Teigland 
dlm_our_nodeid(void)921e7fd4179SDavid Teigland int dlm_our_nodeid(void)
922e7fd4179SDavid Teigland {
923e7fd4179SDavid Teigland 	return local_comm ? local_comm->nodeid : 0;
924e7fd4179SDavid Teigland }
925e7fd4179SDavid Teigland 
926e7fd4179SDavid Teigland /* num 0 is first addr, num 1 is second addr */
dlm_our_addr(struct sockaddr_storage * addr,int num)927e7fd4179SDavid Teigland int dlm_our_addr(struct sockaddr_storage *addr, int num)
928e7fd4179SDavid Teigland {
929e7fd4179SDavid Teigland 	if (!local_comm)
930e7fd4179SDavid Teigland 		return -1;
931e7fd4179SDavid Teigland 	if (num + 1 > local_comm->addr_count)
932e7fd4179SDavid Teigland 		return -1;
933e7fd4179SDavid Teigland 	memcpy(addr, local_comm->addr[num], sizeof(*addr));
934e7fd4179SDavid Teigland 	return 0;
935e7fd4179SDavid Teigland }
936e7fd4179SDavid Teigland 
937e7fd4179SDavid Teigland /* Config file defaults */
938e7fd4179SDavid Teigland #define DEFAULT_TCP_PORT       21064
939e3853a90SDavid Teigland #define DEFAULT_RSBTBL_SIZE     1024
940e7fd4179SDavid Teigland #define DEFAULT_RECOVER_TIMER      5
941e7fd4179SDavid Teigland #define DEFAULT_TOSS_SECS         10
942e7fd4179SDavid Teigland #define DEFAULT_SCAN_SECS          5
94399fc6487SDavid Teigland #define DEFAULT_LOG_DEBUG          0
944505ee528SZhilong Liu #define DEFAULT_LOG_INFO           1
945ac7d5d03SAlexander Aring #define DEFAULT_PROTOCOL           DLM_PROTO_TCP
946a5b7ab63SAlexander Aring #define DEFAULT_MARK               0
9473881ac04SDavid Teigland #define DEFAULT_NEW_RSB_COUNT    128
94860f98d18SDavid Teigland #define DEFAULT_RECOVER_CALLBACKS  0
94960f98d18SDavid Teigland #define DEFAULT_CLUSTER_NAME      ""
950e7fd4179SDavid Teigland 
951e7fd4179SDavid Teigland struct dlm_config_info dlm_config = {
95268c817a1SDavid Teigland 	.ci_tcp_port = DEFAULT_TCP_PORT,
953d10a0b88SAlexander Aring 	.ci_buffer_size = DLM_MAX_SOCKET_BUFSIZE,
95468c817a1SDavid Teigland 	.ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
95568c817a1SDavid Teigland 	.ci_recover_timer = DEFAULT_RECOVER_TIMER,
95668c817a1SDavid Teigland 	.ci_toss_secs = DEFAULT_TOSS_SECS,
95799fc6487SDavid Teigland 	.ci_scan_secs = DEFAULT_SCAN_SECS,
9586ed7257bSPatrick Caulfield 	.ci_log_debug = DEFAULT_LOG_DEBUG,
959505ee528SZhilong Liu 	.ci_log_info = DEFAULT_LOG_INFO,
9603ae1acf9SDavid Teigland 	.ci_protocol = DEFAULT_PROTOCOL,
961a5b7ab63SAlexander Aring 	.ci_mark = DEFAULT_MARK,
96260f98d18SDavid Teigland 	.ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT,
96360f98d18SDavid Teigland 	.ci_recover_callbacks = DEFAULT_RECOVER_CALLBACKS,
96460f98d18SDavid Teigland 	.ci_cluster_name = DEFAULT_CLUSTER_NAME
965e7fd4179SDavid Teigland };
966e7fd4179SDavid Teigland 
967