xref: /openbmc/linux/fs/dlm/rcom.c (revision 8304d6f24cc1221392b6d61fa9d16631cbd6beb7)
1e7fd4179SDavid Teigland /******************************************************************************
2e7fd4179SDavid Teigland *******************************************************************************
3e7fd4179SDavid Teigland **
4e7fd4179SDavid Teigland **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5dbcfc347SDavid Teigland **  Copyright (C) 2005-2008 Red Hat, Inc.  All rights reserved.
6e7fd4179SDavid Teigland **
7e7fd4179SDavid Teigland **  This copyrighted material is made available to anyone wishing to use,
8e7fd4179SDavid Teigland **  modify, copy, or redistribute it subject to the terms and conditions
9e7fd4179SDavid Teigland **  of the GNU General Public License v.2.
10e7fd4179SDavid Teigland **
11e7fd4179SDavid Teigland *******************************************************************************
12e7fd4179SDavid Teigland ******************************************************************************/
13e7fd4179SDavid Teigland 
14e7fd4179SDavid Teigland #include "dlm_internal.h"
15e7fd4179SDavid Teigland #include "lockspace.h"
16e7fd4179SDavid Teigland #include "member.h"
17e7fd4179SDavid Teigland #include "lowcomms.h"
18e7fd4179SDavid Teigland #include "midcomms.h"
19e7fd4179SDavid Teigland #include "rcom.h"
20e7fd4179SDavid Teigland #include "recover.h"
21e7fd4179SDavid Teigland #include "dir.h"
22e7fd4179SDavid Teigland #include "config.h"
23e7fd4179SDavid Teigland #include "memory.h"
24e7fd4179SDavid Teigland #include "lock.h"
25e7fd4179SDavid Teigland #include "util.h"
26e7fd4179SDavid Teigland 
27e7fd4179SDavid Teigland 
28e7fd4179SDavid Teigland static int rcom_response(struct dlm_ls *ls)
29e7fd4179SDavid Teigland {
30e7fd4179SDavid Teigland 	return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
31e7fd4179SDavid Teigland }
32e7fd4179SDavid Teigland 
33e7fd4179SDavid Teigland static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
34e7fd4179SDavid Teigland 		       struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret)
35e7fd4179SDavid Teigland {
36e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
37e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
38e7fd4179SDavid Teigland 	char *mb;
39e7fd4179SDavid Teigland 	int mb_len = sizeof(struct dlm_rcom) + len;
40e7fd4179SDavid Teigland 
41573c24c4SDavid Teigland 	mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
42e7fd4179SDavid Teigland 	if (!mh) {
43e7fd4179SDavid Teigland 		log_print("create_rcom to %d type %d len %d ENOBUFS",
44e7fd4179SDavid Teigland 			  to_nodeid, type, len);
45e7fd4179SDavid Teigland 		return -ENOBUFS;
46e7fd4179SDavid Teigland 	}
47e7fd4179SDavid Teigland 	memset(mb, 0, mb_len);
48e7fd4179SDavid Teigland 
49e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) mb;
50e7fd4179SDavid Teigland 
51e7fd4179SDavid Teigland 	rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
52e7fd4179SDavid Teigland 	rc->rc_header.h_lockspace = ls->ls_global_id;
53e7fd4179SDavid Teigland 	rc->rc_header.h_nodeid = dlm_our_nodeid();
54e7fd4179SDavid Teigland 	rc->rc_header.h_length = mb_len;
55e7fd4179SDavid Teigland 	rc->rc_header.h_cmd = DLM_RCOM;
56e7fd4179SDavid Teigland 
57e7fd4179SDavid Teigland 	rc->rc_type = type;
58e7fd4179SDavid Teigland 
5938aa8b0cSDavid Teigland 	spin_lock(&ls->ls_recover_lock);
6038aa8b0cSDavid Teigland 	rc->rc_seq = ls->ls_recover_seq;
6138aa8b0cSDavid Teigland 	spin_unlock(&ls->ls_recover_lock);
6238aa8b0cSDavid Teigland 
63e7fd4179SDavid Teigland 	*mh_ret = mh;
64e7fd4179SDavid Teigland 	*rc_ret = rc;
65e7fd4179SDavid Teigland 	return 0;
66e7fd4179SDavid Teigland }
67e7fd4179SDavid Teigland 
68e7fd4179SDavid Teigland static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
69e7fd4179SDavid Teigland 		      struct dlm_rcom *rc)
70e7fd4179SDavid Teigland {
71e7fd4179SDavid Teigland 	dlm_rcom_out(rc);
72e7fd4179SDavid Teigland 	dlm_lowcomms_commit_buffer(mh);
73e7fd4179SDavid Teigland }
74e7fd4179SDavid Teigland 
75e7fd4179SDavid Teigland /* When replying to a status request, a node also sends back its
76e7fd4179SDavid Teigland    configuration values.  The requesting node then checks that the remote
77e7fd4179SDavid Teigland    node is configured the same way as itself. */
78e7fd4179SDavid Teigland 
79e7fd4179SDavid Teigland static void make_config(struct dlm_ls *ls, struct rcom_config *rf)
80e7fd4179SDavid Teigland {
8193ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
8293ff2971SAl Viro 	rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
83e7fd4179SDavid Teigland }
84e7fd4179SDavid Teigland 
859e971b71SDavid Teigland static int check_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
86e7fd4179SDavid Teigland {
879e971b71SDavid Teigland 	struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
8802ed16b6SAl Viro 	size_t conf_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
899e971b71SDavid Teigland 
909e971b71SDavid Teigland 	if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
919e971b71SDavid Teigland 		log_error(ls, "version mismatch: %x nodeid %d: %x",
929e971b71SDavid Teigland 			  DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
939e971b71SDavid Teigland 			  rc->rc_header.h_version);
948b0e7b2cSDavid Teigland 		return -EPROTO;
959e971b71SDavid Teigland 	}
969e971b71SDavid Teigland 
9702ed16b6SAl Viro 	if (rc->rc_header.h_length < conf_size) {
9802ed16b6SAl Viro 		log_error(ls, "config too short: %d nodeid %d",
9902ed16b6SAl Viro 			  rc->rc_header.h_length, nodeid);
10002ed16b6SAl Viro 		return -EPROTO;
10102ed16b6SAl Viro 	}
10202ed16b6SAl Viro 
10393ff2971SAl Viro 	if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
10493ff2971SAl Viro 	    le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
105e7fd4179SDavid Teigland 		log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
10693ff2971SAl Viro 			  ls->ls_lvblen, ls->ls_exflags, nodeid,
10793ff2971SAl Viro 			  le32_to_cpu(rf->rf_lvblen),
10893ff2971SAl Viro 			  le32_to_cpu(rf->rf_lsflags));
1098b0e7b2cSDavid Teigland 		return -EPROTO;
110e7fd4179SDavid Teigland 	}
111e7fd4179SDavid Teigland 	return 0;
112e7fd4179SDavid Teigland }
113e7fd4179SDavid Teigland 
11498f176fbSDavid Teigland static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
11598f176fbSDavid Teigland {
11698f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
11798f176fbSDavid Teigland 	*new_seq = ++ls->ls_rcom_seq;
11898f176fbSDavid Teigland 	set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
11998f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
12098f176fbSDavid Teigland }
12198f176fbSDavid Teigland 
12298f176fbSDavid Teigland static void disallow_sync_reply(struct dlm_ls *ls)
12398f176fbSDavid Teigland {
12498f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
12598f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
12698f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
12798f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
12898f176fbSDavid Teigland }
12998f176fbSDavid Teigland 
130e7fd4179SDavid Teigland int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
131e7fd4179SDavid Teigland {
132e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
133e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
134e7fd4179SDavid Teigland 	int error = 0;
135e7fd4179SDavid Teigland 
136faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
137e7fd4179SDavid Teigland 
138e7fd4179SDavid Teigland 	if (nodeid == dlm_our_nodeid()) {
1394007685cSAl Viro 		rc = ls->ls_recover_buf;
140e7fd4179SDavid Teigland 		rc->rc_result = dlm_recover_status(ls);
141e7fd4179SDavid Teigland 		goto out;
142e7fd4179SDavid Teigland 	}
143e7fd4179SDavid Teigland 
144e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
145e7fd4179SDavid Teigland 	if (error)
146e7fd4179SDavid Teigland 		goto out;
14798f176fbSDavid Teigland 
14898f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
14968c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
150e7fd4179SDavid Teigland 
151e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
152e7fd4179SDavid Teigland 
153e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
15498f176fbSDavid Teigland 	disallow_sync_reply(ls);
155e7fd4179SDavid Teigland 	if (error)
156e7fd4179SDavid Teigland 		goto out;
157e7fd4179SDavid Teigland 
1584007685cSAl Viro 	rc = ls->ls_recover_buf;
159e7fd4179SDavid Teigland 
160e7fd4179SDavid Teigland 	if (rc->rc_result == -ESRCH) {
161e7fd4179SDavid Teigland 		/* we pretend the remote lockspace exists with 0 status */
162e7fd4179SDavid Teigland 		log_debug(ls, "remote node %d not ready", nodeid);
163e7fd4179SDavid Teigland 		rc->rc_result = 0;
164e7fd4179SDavid Teigland 	} else
1659e971b71SDavid Teigland 		error = check_config(ls, rc, nodeid);
166e7fd4179SDavid Teigland 	/* the caller looks at rc_result for the remote recovery status */
167e7fd4179SDavid Teigland  out:
168e7fd4179SDavid Teigland 	return error;
169e7fd4179SDavid Teigland }
170e7fd4179SDavid Teigland 
171e7fd4179SDavid Teigland static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
172e7fd4179SDavid Teigland {
173e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
174e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
175e7fd4179SDavid Teigland 	int error, nodeid = rc_in->rc_header.h_nodeid;
176e7fd4179SDavid Teigland 
177e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
178e7fd4179SDavid Teigland 			    sizeof(struct rcom_config), &rc, &mh);
179e7fd4179SDavid Teigland 	if (error)
180e7fd4179SDavid Teigland 		return;
1814a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
18238aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
183e7fd4179SDavid Teigland 	rc->rc_result = dlm_recover_status(ls);
184e7fd4179SDavid Teigland 	make_config(ls, (struct rcom_config *) rc->rc_buf);
185e7fd4179SDavid Teigland 
186e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
187e7fd4179SDavid Teigland }
188e7fd4179SDavid Teigland 
1894a99c3d9SDavid Teigland static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
190e7fd4179SDavid Teigland {
19198f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
19298f176fbSDavid Teigland 	if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
19398f176fbSDavid Teigland 	    rc_in->rc_id != ls->ls_rcom_seq) {
19498f176fbSDavid Teigland 		log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
19598f176fbSDavid Teigland 			  rc_in->rc_type, rc_in->rc_header.h_nodeid,
19657adf7eeSRyusuke Konishi 			  (unsigned long long)rc_in->rc_id,
19757adf7eeSRyusuke Konishi 			  (unsigned long long)ls->ls_rcom_seq);
19898f176fbSDavid Teigland 		goto out;
1994a99c3d9SDavid Teigland 	}
200e7fd4179SDavid Teigland 	memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
201e7fd4179SDavid Teigland 	set_bit(LSFL_RCOM_READY, &ls->ls_flags);
20298f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
203e7fd4179SDavid Teigland 	wake_up(&ls->ls_wait_general);
20498f176fbSDavid Teigland  out:
20598f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
206e7fd4179SDavid Teigland }
207e7fd4179SDavid Teigland 
208e7fd4179SDavid Teigland int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
209e7fd4179SDavid Teigland {
210e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
211e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
2124007685cSAl Viro 	int error = 0;
2134007685cSAl Viro 	int max_size = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
214e7fd4179SDavid Teigland 
215faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
216e7fd4179SDavid Teigland 
217e7fd4179SDavid Teigland 	if (nodeid == dlm_our_nodeid()) {
218599e0f58SDavid Teigland 		ls->ls_recover_buf->rc_header.h_length =
219599e0f58SDavid Teigland 			dlm_config.ci_buffer_size;
220e7fd4179SDavid Teigland 		dlm_copy_master_names(ls, last_name, last_len,
2214007685cSAl Viro 		                      ls->ls_recover_buf->rc_buf,
2224007685cSAl Viro 		                      max_size, nodeid);
223e7fd4179SDavid Teigland 		goto out;
224e7fd4179SDavid Teigland 	}
225e7fd4179SDavid Teigland 
226e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
227e7fd4179SDavid Teigland 	if (error)
228e7fd4179SDavid Teigland 		goto out;
229e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, last_name, last_len);
23098f176fbSDavid Teigland 
23198f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
23268c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
233e7fd4179SDavid Teigland 
234e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
235e7fd4179SDavid Teigland 
236e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
23798f176fbSDavid Teigland 	disallow_sync_reply(ls);
238e7fd4179SDavid Teigland  out:
239e7fd4179SDavid Teigland 	return error;
240e7fd4179SDavid Teigland }
241e7fd4179SDavid Teigland 
242e7fd4179SDavid Teigland static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
243e7fd4179SDavid Teigland {
244e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
245e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
24638aa8b0cSDavid Teigland 	int error, inlen, outlen, nodeid;
247e7fd4179SDavid Teigland 
248e7fd4179SDavid Teigland 	nodeid = rc_in->rc_header.h_nodeid;
249e7fd4179SDavid Teigland 	inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
25068c817a1SDavid Teigland 	outlen = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
251e7fd4179SDavid Teigland 
252e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
253e7fd4179SDavid Teigland 	if (error)
254e7fd4179SDavid Teigland 		return;
2554a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
25638aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
257e7fd4179SDavid Teigland 
258e7fd4179SDavid Teigland 	dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
259e7fd4179SDavid Teigland 			      nodeid);
260e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
261e7fd4179SDavid Teigland }
262e7fd4179SDavid Teigland 
263e7fd4179SDavid Teigland int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
264e7fd4179SDavid Teigland {
265e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
266e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
267e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
268e7fd4179SDavid Teigland 	int error;
269e7fd4179SDavid Teigland 
270e7fd4179SDavid Teigland 	error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
271e7fd4179SDavid Teigland 			    &rc, &mh);
272e7fd4179SDavid Teigland 	if (error)
273e7fd4179SDavid Teigland 		goto out;
274e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, r->res_name, r->res_length);
275e7fd4179SDavid Teigland 	rc->rc_id = (unsigned long) r;
276e7fd4179SDavid Teigland 
277e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
278e7fd4179SDavid Teigland  out:
279e7fd4179SDavid Teigland 	return error;
280e7fd4179SDavid Teigland }
281e7fd4179SDavid Teigland 
282e7fd4179SDavid Teigland static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
283e7fd4179SDavid Teigland {
284e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
285e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
286e7fd4179SDavid Teigland 	int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
287e7fd4179SDavid Teigland 	int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
288e7fd4179SDavid Teigland 
289e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
290e7fd4179SDavid Teigland 	if (error)
291e7fd4179SDavid Teigland 		return;
292e7fd4179SDavid Teigland 
293e7fd4179SDavid Teigland 	error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
294e7fd4179SDavid Teigland 	if (error)
295e7fd4179SDavid Teigland 		ret_nodeid = error;
296e7fd4179SDavid Teigland 	rc->rc_result = ret_nodeid;
297e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
29838aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
299e7fd4179SDavid Teigland 
300e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
301e7fd4179SDavid Teigland }
302e7fd4179SDavid Teigland 
303e7fd4179SDavid Teigland static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
304e7fd4179SDavid Teigland {
305e7fd4179SDavid Teigland 	dlm_recover_master_reply(ls, rc_in);
306e7fd4179SDavid Teigland }
307e7fd4179SDavid Teigland 
308e7fd4179SDavid Teigland static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
309e7fd4179SDavid Teigland 			   struct rcom_lock *rl)
310e7fd4179SDavid Teigland {
311e7fd4179SDavid Teigland 	memset(rl, 0, sizeof(*rl));
312e7fd4179SDavid Teigland 
313163a1859SAl Viro 	rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
314163a1859SAl Viro 	rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
315163a1859SAl Viro 	rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
316163a1859SAl Viro 	rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
317163a1859SAl Viro 	rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
318e7fd4179SDavid Teigland 	rl->rl_rqmode = lkb->lkb_rqmode;
319e7fd4179SDavid Teigland 	rl->rl_grmode = lkb->lkb_grmode;
320e7fd4179SDavid Teigland 	rl->rl_status = lkb->lkb_status;
321163a1859SAl Viro 	rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
322e7fd4179SDavid Teigland 
323e5dae548SDavid Teigland 	if (lkb->lkb_bastfn)
324*8304d6f2SDavid Teigland 		rl->rl_asts |= DLM_CB_BAST;
325e5dae548SDavid Teigland 	if (lkb->lkb_astfn)
326*8304d6f2SDavid Teigland 		rl->rl_asts |= DLM_CB_CAST;
327e7fd4179SDavid Teigland 
328163a1859SAl Viro 	rl->rl_namelen = cpu_to_le16(r->res_length);
329e7fd4179SDavid Teigland 	memcpy(rl->rl_name, r->res_name, r->res_length);
330e7fd4179SDavid Teigland 
331e7fd4179SDavid Teigland 	/* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
332e7fd4179SDavid Teigland 	   If so, receive_rcom_lock_args() won't take this copy. */
333e7fd4179SDavid Teigland 
334e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
335e7fd4179SDavid Teigland 		memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
336e7fd4179SDavid Teigland }
337e7fd4179SDavid Teigland 
338e7fd4179SDavid Teigland int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
339e7fd4179SDavid Teigland {
340e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
341e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
342e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
343e7fd4179SDavid Teigland 	struct rcom_lock *rl;
344e7fd4179SDavid Teigland 	int error, len = sizeof(struct rcom_lock);
345e7fd4179SDavid Teigland 
346e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
347e7fd4179SDavid Teigland 		len += ls->ls_lvblen;
348e7fd4179SDavid Teigland 
349e7fd4179SDavid Teigland 	error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
350e7fd4179SDavid Teigland 	if (error)
351e7fd4179SDavid Teigland 		goto out;
352e7fd4179SDavid Teigland 
353e7fd4179SDavid Teigland 	rl = (struct rcom_lock *) rc->rc_buf;
354e7fd4179SDavid Teigland 	pack_rcom_lock(r, lkb, rl);
355e7fd4179SDavid Teigland 	rc->rc_id = (unsigned long) r;
356e7fd4179SDavid Teigland 
357e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
358e7fd4179SDavid Teigland  out:
359e7fd4179SDavid Teigland 	return error;
360e7fd4179SDavid Teigland }
361e7fd4179SDavid Teigland 
362ae773d0bSAl Viro /* needs at least dlm_rcom + rcom_lock */
363e7fd4179SDavid Teigland static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
364e7fd4179SDavid Teigland {
365e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
366e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
367e7fd4179SDavid Teigland 	int error, nodeid = rc_in->rc_header.h_nodeid;
368e7fd4179SDavid Teigland 
369e7fd4179SDavid Teigland 	dlm_recover_master_copy(ls, rc_in);
370e7fd4179SDavid Teigland 
371e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
372e7fd4179SDavid Teigland 			    sizeof(struct rcom_lock), &rc, &mh);
373e7fd4179SDavid Teigland 	if (error)
374e7fd4179SDavid Teigland 		return;
375e7fd4179SDavid Teigland 
376e7fd4179SDavid Teigland 	/* We send back the same rcom_lock struct we received, but
377e7fd4179SDavid Teigland 	   dlm_recover_master_copy() has filled in rl_remid and rl_result */
378e7fd4179SDavid Teigland 
379e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
380e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
38138aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
382e7fd4179SDavid Teigland 
383e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
384e7fd4179SDavid Teigland }
385e7fd4179SDavid Teigland 
386c36258b5SDavid Teigland /* If the lockspace doesn't exist then still send a status message
387c36258b5SDavid Teigland    back; it's possible that it just doesn't have its global_id yet. */
388c36258b5SDavid Teigland 
389c36258b5SDavid Teigland int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
390e7fd4179SDavid Teigland {
391e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
3921babdb45SDavid Teigland 	struct rcom_config *rf;
393e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
394e7fd4179SDavid Teigland 	char *mb;
3951babdb45SDavid Teigland 	int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
396e7fd4179SDavid Teigland 
39741684f95SDavid Teigland 	mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_NOFS, &mb);
398e7fd4179SDavid Teigland 	if (!mh)
399e7fd4179SDavid Teigland 		return -ENOBUFS;
400e7fd4179SDavid Teigland 	memset(mb, 0, mb_len);
401e7fd4179SDavid Teigland 
402e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) mb;
403e7fd4179SDavid Teigland 
404e7fd4179SDavid Teigland 	rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
405e7fd4179SDavid Teigland 	rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
406e7fd4179SDavid Teigland 	rc->rc_header.h_nodeid = dlm_our_nodeid();
407e7fd4179SDavid Teigland 	rc->rc_header.h_length = mb_len;
408e7fd4179SDavid Teigland 	rc->rc_header.h_cmd = DLM_RCOM;
409e7fd4179SDavid Teigland 
410e7fd4179SDavid Teigland 	rc->rc_type = DLM_RCOM_STATUS_REPLY;
411f5888750SDavid Teigland 	rc->rc_id = rc_in->rc_id;
41238aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
413e7fd4179SDavid Teigland 	rc->rc_result = -ESRCH;
414e7fd4179SDavid Teigland 
4151babdb45SDavid Teigland 	rf = (struct rcom_config *) rc->rc_buf;
41693ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(~0U);
4171babdb45SDavid Teigland 
418e7fd4179SDavid Teigland 	dlm_rcom_out(rc);
419e7fd4179SDavid Teigland 	dlm_lowcomms_commit_buffer(mh);
420e7fd4179SDavid Teigland 
421e7fd4179SDavid Teigland 	return 0;
422e7fd4179SDavid Teigland }
423e7fd4179SDavid Teigland 
42438aa8b0cSDavid Teigland static int is_old_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
42538aa8b0cSDavid Teigland {
42638aa8b0cSDavid Teigland 	uint64_t seq;
42738aa8b0cSDavid Teigland 	int rv = 0;
42838aa8b0cSDavid Teigland 
42938aa8b0cSDavid Teigland 	switch (rc->rc_type) {
43038aa8b0cSDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
43138aa8b0cSDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
43238aa8b0cSDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
43338aa8b0cSDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
43438aa8b0cSDavid Teigland 		spin_lock(&ls->ls_recover_lock);
43538aa8b0cSDavid Teigland 		seq = ls->ls_recover_seq;
43638aa8b0cSDavid Teigland 		spin_unlock(&ls->ls_recover_lock);
43738aa8b0cSDavid Teigland 		if (rc->rc_seq_reply != seq) {
4388ec68867SDavid Teigland 			log_debug(ls, "ignoring old reply %x from %d "
43938aa8b0cSDavid Teigland 				      "seq_reply %llx expect %llx",
44038aa8b0cSDavid Teigland 				      rc->rc_type, rc->rc_header.h_nodeid,
44138aa8b0cSDavid Teigland 				      (unsigned long long)rc->rc_seq_reply,
44238aa8b0cSDavid Teigland 				      (unsigned long long)seq);
44338aa8b0cSDavid Teigland 			rv = 1;
44438aa8b0cSDavid Teigland 		}
44538aa8b0cSDavid Teigland 	}
44638aa8b0cSDavid Teigland 	return rv;
44738aa8b0cSDavid Teigland }
44838aa8b0cSDavid Teigland 
449c36258b5SDavid Teigland /* Called by dlm_recv; corresponds to dlm_receive_message() but special
450e7fd4179SDavid Teigland    recovery-only comms are sent through here. */
451e7fd4179SDavid Teigland 
452c36258b5SDavid Teigland void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
453e7fd4179SDavid Teigland {
454ae773d0bSAl Viro 	int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
455ae773d0bSAl Viro 
456e7fd4179SDavid Teigland 	if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
4578ec68867SDavid Teigland 		log_debug(ls, "ignoring recovery message %x from %d",
458e7fd4179SDavid Teigland 			  rc->rc_type, nodeid);
459e7fd4179SDavid Teigland 		goto out;
460e7fd4179SDavid Teigland 	}
461e7fd4179SDavid Teigland 
46238aa8b0cSDavid Teigland 	if (is_old_reply(ls, rc))
46338aa8b0cSDavid Teigland 		goto out;
46438aa8b0cSDavid Teigland 
465e7fd4179SDavid Teigland 	switch (rc->rc_type) {
466e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS:
467e7fd4179SDavid Teigland 		receive_rcom_status(ls, rc);
468e7fd4179SDavid Teigland 		break;
469e7fd4179SDavid Teigland 
470e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES:
471e7fd4179SDavid Teigland 		receive_rcom_names(ls, rc);
472e7fd4179SDavid Teigland 		break;
473e7fd4179SDavid Teigland 
474e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP:
475e7fd4179SDavid Teigland 		receive_rcom_lookup(ls, rc);
476e7fd4179SDavid Teigland 		break;
477e7fd4179SDavid Teigland 
478e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK:
479ae773d0bSAl Viro 		if (rc->rc_header.h_length < lock_size)
480ae773d0bSAl Viro 			goto Eshort;
481e7fd4179SDavid Teigland 		receive_rcom_lock(ls, rc);
482e7fd4179SDavid Teigland 		break;
483e7fd4179SDavid Teigland 
484e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
485dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
486e7fd4179SDavid Teigland 		break;
487e7fd4179SDavid Teigland 
488e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
489dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
490e7fd4179SDavid Teigland 		break;
491e7fd4179SDavid Teigland 
492e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
493e7fd4179SDavid Teigland 		receive_rcom_lookup_reply(ls, rc);
494e7fd4179SDavid Teigland 		break;
495e7fd4179SDavid Teigland 
496e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
497ae773d0bSAl Viro 		if (rc->rc_header.h_length < lock_size)
498ae773d0bSAl Viro 			goto Eshort;
499dbcfc347SDavid Teigland 		dlm_recover_process_copy(ls, rc);
500e7fd4179SDavid Teigland 		break;
501e7fd4179SDavid Teigland 
502e7fd4179SDavid Teigland 	default:
503dbcfc347SDavid Teigland 		log_error(ls, "receive_rcom bad type %d", rc->rc_type);
504e7fd4179SDavid Teigland 	}
505e7fd4179SDavid Teigland out:
506c36258b5SDavid Teigland 	return;
507ae773d0bSAl Viro Eshort:
508ae773d0bSAl Viro 	log_error(ls, "recovery message %x from %d is too short",
509ae773d0bSAl Viro 			  rc->rc_type, nodeid);
510e7fd4179SDavid Teigland }
511e7fd4179SDavid Teigland 
512