xref: /openbmc/linux/fs/dlm/rcom.c (revision 2522fe45a186e6276583e02723b78e1d1987cdd5)
1*2522fe45SThomas 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.
6dbcfc347SDavid Teigland **  Copyright (C) 2005-2008 Red Hat, Inc.  All rights reserved.
7e7fd4179SDavid Teigland **
8e7fd4179SDavid Teigland **
9e7fd4179SDavid Teigland *******************************************************************************
10e7fd4179SDavid Teigland ******************************************************************************/
11e7fd4179SDavid Teigland 
12e7fd4179SDavid Teigland #include "dlm_internal.h"
13e7fd4179SDavid Teigland #include "lockspace.h"
14e7fd4179SDavid Teigland #include "member.h"
15e7fd4179SDavid Teigland #include "lowcomms.h"
16e7fd4179SDavid Teigland #include "midcomms.h"
17e7fd4179SDavid Teigland #include "rcom.h"
18e7fd4179SDavid Teigland #include "recover.h"
19e7fd4179SDavid Teigland #include "dir.h"
20e7fd4179SDavid Teigland #include "config.h"
21e7fd4179SDavid Teigland #include "memory.h"
22e7fd4179SDavid Teigland #include "lock.h"
23e7fd4179SDavid Teigland #include "util.h"
24e7fd4179SDavid Teigland 
25e7fd4179SDavid Teigland static int rcom_response(struct dlm_ls *ls)
26e7fd4179SDavid Teigland {
27e7fd4179SDavid Teigland 	return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
28e7fd4179SDavid Teigland }
29e7fd4179SDavid Teigland 
30e7fd4179SDavid Teigland static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
31e7fd4179SDavid Teigland 		       struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret)
32e7fd4179SDavid Teigland {
33e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
34e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
35e7fd4179SDavid Teigland 	char *mb;
36e7fd4179SDavid Teigland 	int mb_len = sizeof(struct dlm_rcom) + len;
37e7fd4179SDavid Teigland 
38573c24c4SDavid Teigland 	mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
39e7fd4179SDavid Teigland 	if (!mh) {
40e7fd4179SDavid Teigland 		log_print("create_rcom to %d type %d len %d ENOBUFS",
41e7fd4179SDavid Teigland 			  to_nodeid, type, len);
42e7fd4179SDavid Teigland 		return -ENOBUFS;
43e7fd4179SDavid Teigland 	}
44e7fd4179SDavid Teigland 	memset(mb, 0, mb_len);
45e7fd4179SDavid Teigland 
46e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) mb;
47e7fd4179SDavid Teigland 
48e7fd4179SDavid Teigland 	rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
49e7fd4179SDavid Teigland 	rc->rc_header.h_lockspace = ls->ls_global_id;
50e7fd4179SDavid Teigland 	rc->rc_header.h_nodeid = dlm_our_nodeid();
51e7fd4179SDavid Teigland 	rc->rc_header.h_length = mb_len;
52e7fd4179SDavid Teigland 	rc->rc_header.h_cmd = DLM_RCOM;
53e7fd4179SDavid Teigland 
54e7fd4179SDavid Teigland 	rc->rc_type = type;
55e7fd4179SDavid Teigland 
5638aa8b0cSDavid Teigland 	spin_lock(&ls->ls_recover_lock);
5738aa8b0cSDavid Teigland 	rc->rc_seq = ls->ls_recover_seq;
5838aa8b0cSDavid Teigland 	spin_unlock(&ls->ls_recover_lock);
5938aa8b0cSDavid Teigland 
60e7fd4179SDavid Teigland 	*mh_ret = mh;
61e7fd4179SDavid Teigland 	*rc_ret = rc;
62e7fd4179SDavid Teigland 	return 0;
63e7fd4179SDavid Teigland }
64e7fd4179SDavid Teigland 
65e7fd4179SDavid Teigland static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
66e7fd4179SDavid Teigland 		      struct dlm_rcom *rc)
67e7fd4179SDavid Teigland {
68e7fd4179SDavid Teigland 	dlm_rcom_out(rc);
69e7fd4179SDavid Teigland 	dlm_lowcomms_commit_buffer(mh);
70e7fd4179SDavid Teigland }
71e7fd4179SDavid Teigland 
72757a4271SDavid Teigland static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs,
73757a4271SDavid Teigland 			    uint32_t flags)
74757a4271SDavid Teigland {
75757a4271SDavid Teigland 	rs->rs_flags = cpu_to_le32(flags);
76757a4271SDavid Teigland }
77757a4271SDavid Teigland 
78e7fd4179SDavid Teigland /* When replying to a status request, a node also sends back its
79e7fd4179SDavid Teigland    configuration values.  The requesting node then checks that the remote
80e7fd4179SDavid Teigland    node is configured the same way as itself. */
81e7fd4179SDavid Teigland 
82757a4271SDavid Teigland static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf,
83757a4271SDavid Teigland 			    uint32_t num_slots)
84e7fd4179SDavid Teigland {
8593ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
8693ff2971SAl Viro 	rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
87757a4271SDavid Teigland 
88757a4271SDavid Teigland 	rf->rf_our_slot = cpu_to_le16(ls->ls_slot);
89757a4271SDavid Teigland 	rf->rf_num_slots = cpu_to_le16(num_slots);
90757a4271SDavid Teigland 	rf->rf_generation =  cpu_to_le32(ls->ls_generation);
91e7fd4179SDavid Teigland }
92e7fd4179SDavid Teigland 
93757a4271SDavid Teigland static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
94e7fd4179SDavid Teigland {
959e971b71SDavid Teigland 	struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
969e971b71SDavid Teigland 
979e971b71SDavid Teigland 	if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
989e971b71SDavid Teigland 		log_error(ls, "version mismatch: %x nodeid %d: %x",
999e971b71SDavid Teigland 			  DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
1009e971b71SDavid Teigland 			  rc->rc_header.h_version);
1018b0e7b2cSDavid Teigland 		return -EPROTO;
1029e971b71SDavid Teigland 	}
1039e971b71SDavid Teigland 
10493ff2971SAl Viro 	if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
10593ff2971SAl Viro 	    le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
106e7fd4179SDavid Teigland 		log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
10793ff2971SAl Viro 			  ls->ls_lvblen, ls->ls_exflags, nodeid,
10893ff2971SAl Viro 			  le32_to_cpu(rf->rf_lvblen),
10993ff2971SAl Viro 			  le32_to_cpu(rf->rf_lsflags));
1108b0e7b2cSDavid Teigland 		return -EPROTO;
111e7fd4179SDavid Teigland 	}
112e7fd4179SDavid Teigland 	return 0;
113e7fd4179SDavid Teigland }
114e7fd4179SDavid Teigland 
11598f176fbSDavid Teigland static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
11698f176fbSDavid Teigland {
11798f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
11898f176fbSDavid Teigland 	*new_seq = ++ls->ls_rcom_seq;
11998f176fbSDavid Teigland 	set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
12098f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
12198f176fbSDavid Teigland }
12298f176fbSDavid Teigland 
12398f176fbSDavid Teigland static void disallow_sync_reply(struct dlm_ls *ls)
12498f176fbSDavid Teigland {
12598f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
12698f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
12798f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
12898f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
12998f176fbSDavid Teigland }
13098f176fbSDavid Teigland 
131757a4271SDavid Teigland /*
132757a4271SDavid Teigland  * low nodeid gathers one slot value at a time from each node.
133757a4271SDavid Teigland  * it sets need_slots=0, and saves rf_our_slot returned from each
134757a4271SDavid Teigland  * rcom_config.
135757a4271SDavid Teigland  *
136757a4271SDavid Teigland  * other nodes gather all slot values at once from the low nodeid.
137757a4271SDavid Teigland  * they set need_slots=1, and ignore the rf_our_slot returned from each
138757a4271SDavid Teigland  * rcom_config.  they use the rf_num_slots returned from the low
139757a4271SDavid Teigland  * node's rcom_config.
140757a4271SDavid Teigland  */
141757a4271SDavid Teigland 
142757a4271SDavid Teigland int dlm_rcom_status(struct dlm_ls *ls, int nodeid, uint32_t status_flags)
143e7fd4179SDavid Teigland {
144e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
145e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
146e7fd4179SDavid Teigland 	int error = 0;
147e7fd4179SDavid Teigland 
148faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
149e7fd4179SDavid Teigland 
150e7fd4179SDavid Teigland 	if (nodeid == dlm_our_nodeid()) {
1514007685cSAl Viro 		rc = ls->ls_recover_buf;
152e7fd4179SDavid Teigland 		rc->rc_result = dlm_recover_status(ls);
153e7fd4179SDavid Teigland 		goto out;
154e7fd4179SDavid Teigland 	}
155e7fd4179SDavid Teigland 
15659661212Stsutomu.owa@toshiba.co.jp retry:
157757a4271SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS,
158757a4271SDavid Teigland 			    sizeof(struct rcom_status), &rc, &mh);
159e7fd4179SDavid Teigland 	if (error)
160e7fd4179SDavid Teigland 		goto out;
16198f176fbSDavid Teigland 
162757a4271SDavid Teigland 	set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags);
163757a4271SDavid Teigland 
16498f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
16568c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
166e7fd4179SDavid Teigland 
167e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
168e7fd4179SDavid Teigland 
169e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
17098f176fbSDavid Teigland 	disallow_sync_reply(ls);
17159661212Stsutomu.owa@toshiba.co.jp 	if (error == -ETIMEDOUT)
17259661212Stsutomu.owa@toshiba.co.jp 		goto retry;
173e7fd4179SDavid Teigland 	if (error)
174e7fd4179SDavid Teigland 		goto out;
175e7fd4179SDavid Teigland 
1764007685cSAl Viro 	rc = ls->ls_recover_buf;
177e7fd4179SDavid Teigland 
178e7fd4179SDavid Teigland 	if (rc->rc_result == -ESRCH) {
179e7fd4179SDavid Teigland 		/* we pretend the remote lockspace exists with 0 status */
180e7fd4179SDavid Teigland 		log_debug(ls, "remote node %d not ready", nodeid);
181e7fd4179SDavid Teigland 		rc->rc_result = 0;
182757a4271SDavid Teigland 		error = 0;
183757a4271SDavid Teigland 	} else {
184757a4271SDavid Teigland 		error = check_rcom_config(ls, rc, nodeid);
185757a4271SDavid Teigland 	}
186757a4271SDavid Teigland 
187e7fd4179SDavid Teigland 	/* the caller looks at rc_result for the remote recovery status */
188e7fd4179SDavid Teigland  out:
189e7fd4179SDavid Teigland 	return error;
190e7fd4179SDavid Teigland }
191e7fd4179SDavid Teigland 
192e7fd4179SDavid Teigland static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
193e7fd4179SDavid Teigland {
194e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
195e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
196757a4271SDavid Teigland 	struct rcom_status *rs;
197757a4271SDavid Teigland 	uint32_t status;
198757a4271SDavid Teigland 	int nodeid = rc_in->rc_header.h_nodeid;
199757a4271SDavid Teigland 	int len = sizeof(struct rcom_config);
200757a4271SDavid Teigland 	int num_slots = 0;
201757a4271SDavid Teigland 	int error;
202e7fd4179SDavid Teigland 
203757a4271SDavid Teigland 	if (!dlm_slots_version(&rc_in->rc_header)) {
204757a4271SDavid Teigland 		status = dlm_recover_status(ls);
205757a4271SDavid Teigland 		goto do_create;
206757a4271SDavid Teigland 	}
207757a4271SDavid Teigland 
208757a4271SDavid Teigland 	rs = (struct rcom_status *)rc_in->rc_buf;
209757a4271SDavid Teigland 
210c07127b4SNeale Ferguson 	if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) {
211757a4271SDavid Teigland 		status = dlm_recover_status(ls);
212757a4271SDavid Teigland 		goto do_create;
213757a4271SDavid Teigland 	}
214757a4271SDavid Teigland 
215757a4271SDavid Teigland 	spin_lock(&ls->ls_recover_lock);
216757a4271SDavid Teigland 	status = ls->ls_recover_status;
217757a4271SDavid Teigland 	num_slots = ls->ls_num_slots;
218757a4271SDavid Teigland 	spin_unlock(&ls->ls_recover_lock);
219757a4271SDavid Teigland 	len += num_slots * sizeof(struct rcom_slot);
220757a4271SDavid Teigland 
221757a4271SDavid Teigland  do_create:
222e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
223757a4271SDavid Teigland 			    len, &rc, &mh);
224e7fd4179SDavid Teigland 	if (error)
225e7fd4179SDavid Teigland 		return;
226757a4271SDavid Teigland 
2274a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
22838aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
229757a4271SDavid Teigland 	rc->rc_result = status;
230e7fd4179SDavid Teigland 
231757a4271SDavid Teigland 	set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots);
232757a4271SDavid Teigland 
233757a4271SDavid Teigland 	if (!num_slots)
234757a4271SDavid Teigland 		goto do_send;
235757a4271SDavid Teigland 
236757a4271SDavid Teigland 	spin_lock(&ls->ls_recover_lock);
237757a4271SDavid Teigland 	if (ls->ls_num_slots != num_slots) {
238757a4271SDavid Teigland 		spin_unlock(&ls->ls_recover_lock);
239757a4271SDavid Teigland 		log_debug(ls, "receive_rcom_status num_slots %d to %d",
240757a4271SDavid Teigland 			  num_slots, ls->ls_num_slots);
241757a4271SDavid Teigland 		rc->rc_result = 0;
242757a4271SDavid Teigland 		set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0);
243757a4271SDavid Teigland 		goto do_send;
244757a4271SDavid Teigland 	}
245757a4271SDavid Teigland 
246757a4271SDavid Teigland 	dlm_slots_copy_out(ls, rc);
247757a4271SDavid Teigland 	spin_unlock(&ls->ls_recover_lock);
248757a4271SDavid Teigland 
249757a4271SDavid Teigland  do_send:
250e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
251e7fd4179SDavid Teigland }
252e7fd4179SDavid Teigland 
2534a99c3d9SDavid Teigland static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
254e7fd4179SDavid Teigland {
25598f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
25698f176fbSDavid Teigland 	if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
25798f176fbSDavid Teigland 	    rc_in->rc_id != ls->ls_rcom_seq) {
25898f176fbSDavid Teigland 		log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
25998f176fbSDavid Teigland 			  rc_in->rc_type, rc_in->rc_header.h_nodeid,
26057adf7eeSRyusuke Konishi 			  (unsigned long long)rc_in->rc_id,
26157adf7eeSRyusuke Konishi 			  (unsigned long long)ls->ls_rcom_seq);
26298f176fbSDavid Teigland 		goto out;
2634a99c3d9SDavid Teigland 	}
264e7fd4179SDavid Teigland 	memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
265e7fd4179SDavid Teigland 	set_bit(LSFL_RCOM_READY, &ls->ls_flags);
26698f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
267e7fd4179SDavid Teigland 	wake_up(&ls->ls_wait_general);
26898f176fbSDavid Teigland  out:
26998f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
270e7fd4179SDavid Teigland }
271e7fd4179SDavid Teigland 
272e7fd4179SDavid Teigland int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
273e7fd4179SDavid Teigland {
274e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
275e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
2764007685cSAl Viro 	int error = 0;
277e7fd4179SDavid Teigland 
278faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
279e7fd4179SDavid Teigland 
28059661212Stsutomu.owa@toshiba.co.jp retry:
281e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
282e7fd4179SDavid Teigland 	if (error)
283e7fd4179SDavid Teigland 		goto out;
284e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, last_name, last_len);
28598f176fbSDavid Teigland 
28698f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
28768c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
288e7fd4179SDavid Teigland 
289e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
290e7fd4179SDavid Teigland 
291e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
29298f176fbSDavid Teigland 	disallow_sync_reply(ls);
29359661212Stsutomu.owa@toshiba.co.jp 	if (error == -ETIMEDOUT)
29459661212Stsutomu.owa@toshiba.co.jp 		goto retry;
295e7fd4179SDavid Teigland  out:
296e7fd4179SDavid Teigland 	return error;
297e7fd4179SDavid Teigland }
298e7fd4179SDavid Teigland 
299e7fd4179SDavid Teigland static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
300e7fd4179SDavid Teigland {
301e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
302e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
30338aa8b0cSDavid Teigland 	int error, inlen, outlen, nodeid;
304e7fd4179SDavid Teigland 
305e7fd4179SDavid Teigland 	nodeid = rc_in->rc_header.h_nodeid;
306e7fd4179SDavid Teigland 	inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
30768c817a1SDavid Teigland 	outlen = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
308e7fd4179SDavid Teigland 
309e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
310e7fd4179SDavid Teigland 	if (error)
311e7fd4179SDavid Teigland 		return;
3124a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
31338aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
314e7fd4179SDavid Teigland 
315e7fd4179SDavid Teigland 	dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
316e7fd4179SDavid Teigland 			      nodeid);
317e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
318e7fd4179SDavid Teigland }
319e7fd4179SDavid Teigland 
320e7fd4179SDavid Teigland int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
321e7fd4179SDavid Teigland {
322e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
323e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
324e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
325e7fd4179SDavid Teigland 	int error;
326e7fd4179SDavid Teigland 
327e7fd4179SDavid Teigland 	error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
328e7fd4179SDavid Teigland 			    &rc, &mh);
329e7fd4179SDavid Teigland 	if (error)
330e7fd4179SDavid Teigland 		goto out;
331e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, r->res_name, r->res_length);
3321d7c484eSDavid Teigland 	rc->rc_id = (unsigned long) r->res_id;
333e7fd4179SDavid Teigland 
334e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
335e7fd4179SDavid Teigland  out:
336e7fd4179SDavid Teigland 	return error;
337e7fd4179SDavid Teigland }
338e7fd4179SDavid Teigland 
339e7fd4179SDavid Teigland static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
340e7fd4179SDavid Teigland {
341e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
342e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
343e7fd4179SDavid Teigland 	int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
344e7fd4179SDavid Teigland 	int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
345e7fd4179SDavid Teigland 
346e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
347e7fd4179SDavid Teigland 	if (error)
348e7fd4179SDavid Teigland 		return;
349e7fd4179SDavid Teigland 
3509250e523SDavid Teigland 	/* Old code would send this special id to trigger a debug dump. */
351c04fecb4SDavid Teigland 	if (rc_in->rc_id == 0xFFFFFFFF) {
352c04fecb4SDavid Teigland 		log_error(ls, "receive_rcom_lookup dump from %d", nodeid);
353c04fecb4SDavid Teigland 		dlm_dump_rsb_name(ls, rc_in->rc_buf, len);
354c04fecb4SDavid Teigland 		return;
355c04fecb4SDavid Teigland 	}
356c04fecb4SDavid Teigland 
357c04fecb4SDavid Teigland 	error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len,
358c04fecb4SDavid Teigland 				  DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL);
359e7fd4179SDavid Teigland 	if (error)
360e7fd4179SDavid Teigland 		ret_nodeid = error;
361e7fd4179SDavid Teigland 	rc->rc_result = ret_nodeid;
362e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
36338aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
364e7fd4179SDavid Teigland 
365e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
366e7fd4179SDavid Teigland }
367e7fd4179SDavid Teigland 
368e7fd4179SDavid Teigland static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
369e7fd4179SDavid Teigland {
370e7fd4179SDavid Teigland 	dlm_recover_master_reply(ls, rc_in);
371e7fd4179SDavid Teigland }
372e7fd4179SDavid Teigland 
373e7fd4179SDavid Teigland static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
374e7fd4179SDavid Teigland 			   struct rcom_lock *rl)
375e7fd4179SDavid Teigland {
376e7fd4179SDavid Teigland 	memset(rl, 0, sizeof(*rl));
377e7fd4179SDavid Teigland 
378163a1859SAl Viro 	rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
379163a1859SAl Viro 	rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
380163a1859SAl Viro 	rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
381163a1859SAl Viro 	rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
382163a1859SAl Viro 	rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
383e7fd4179SDavid Teigland 	rl->rl_rqmode = lkb->lkb_rqmode;
384e7fd4179SDavid Teigland 	rl->rl_grmode = lkb->lkb_grmode;
385e7fd4179SDavid Teigland 	rl->rl_status = lkb->lkb_status;
386163a1859SAl Viro 	rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
387e7fd4179SDavid Teigland 
388e5dae548SDavid Teigland 	if (lkb->lkb_bastfn)
3898304d6f2SDavid Teigland 		rl->rl_asts |= DLM_CB_BAST;
390e5dae548SDavid Teigland 	if (lkb->lkb_astfn)
3918304d6f2SDavid Teigland 		rl->rl_asts |= DLM_CB_CAST;
392e7fd4179SDavid Teigland 
393163a1859SAl Viro 	rl->rl_namelen = cpu_to_le16(r->res_length);
394e7fd4179SDavid Teigland 	memcpy(rl->rl_name, r->res_name, r->res_length);
395e7fd4179SDavid Teigland 
396e7fd4179SDavid Teigland 	/* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
397e7fd4179SDavid Teigland 	   If so, receive_rcom_lock_args() won't take this copy. */
398e7fd4179SDavid Teigland 
399e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
400e7fd4179SDavid Teigland 		memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
401e7fd4179SDavid Teigland }
402e7fd4179SDavid Teigland 
403e7fd4179SDavid Teigland int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
404e7fd4179SDavid Teigland {
405e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
406e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
407e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
408e7fd4179SDavid Teigland 	struct rcom_lock *rl;
409e7fd4179SDavid Teigland 	int error, len = sizeof(struct rcom_lock);
410e7fd4179SDavid Teigland 
411e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
412e7fd4179SDavid Teigland 		len += ls->ls_lvblen;
413e7fd4179SDavid Teigland 
414e7fd4179SDavid Teigland 	error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
415e7fd4179SDavid Teigland 	if (error)
416e7fd4179SDavid Teigland 		goto out;
417e7fd4179SDavid Teigland 
418e7fd4179SDavid Teigland 	rl = (struct rcom_lock *) rc->rc_buf;
419e7fd4179SDavid Teigland 	pack_rcom_lock(r, lkb, rl);
420e7fd4179SDavid Teigland 	rc->rc_id = (unsigned long) r;
421e7fd4179SDavid Teigland 
422e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
423e7fd4179SDavid Teigland  out:
424e7fd4179SDavid Teigland 	return error;
425e7fd4179SDavid Teigland }
426e7fd4179SDavid Teigland 
427ae773d0bSAl Viro /* needs at least dlm_rcom + rcom_lock */
428e7fd4179SDavid Teigland static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
429e7fd4179SDavid Teigland {
430e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
431e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
432e7fd4179SDavid Teigland 	int error, nodeid = rc_in->rc_header.h_nodeid;
433e7fd4179SDavid Teigland 
434e7fd4179SDavid Teigland 	dlm_recover_master_copy(ls, rc_in);
435e7fd4179SDavid Teigland 
436e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
437e7fd4179SDavid Teigland 			    sizeof(struct rcom_lock), &rc, &mh);
438e7fd4179SDavid Teigland 	if (error)
439e7fd4179SDavid Teigland 		return;
440e7fd4179SDavid Teigland 
441e7fd4179SDavid Teigland 	/* We send back the same rcom_lock struct we received, but
442e7fd4179SDavid Teigland 	   dlm_recover_master_copy() has filled in rl_remid and rl_result */
443e7fd4179SDavid Teigland 
444e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
445e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
44638aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
447e7fd4179SDavid Teigland 
448e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
449e7fd4179SDavid Teigland }
450e7fd4179SDavid Teigland 
451c36258b5SDavid Teigland /* If the lockspace doesn't exist then still send a status message
452c36258b5SDavid Teigland    back; it's possible that it just doesn't have its global_id yet. */
453c36258b5SDavid Teigland 
454c36258b5SDavid Teigland int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
455e7fd4179SDavid Teigland {
456e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
4571babdb45SDavid Teigland 	struct rcom_config *rf;
458e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
459e7fd4179SDavid Teigland 	char *mb;
4601babdb45SDavid Teigland 	int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
461e7fd4179SDavid Teigland 
46241684f95SDavid Teigland 	mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_NOFS, &mb);
463e7fd4179SDavid Teigland 	if (!mh)
464e7fd4179SDavid Teigland 		return -ENOBUFS;
465e7fd4179SDavid Teigland 	memset(mb, 0, mb_len);
466e7fd4179SDavid Teigland 
467e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) mb;
468e7fd4179SDavid Teigland 
469e7fd4179SDavid Teigland 	rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
470e7fd4179SDavid Teigland 	rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
471e7fd4179SDavid Teigland 	rc->rc_header.h_nodeid = dlm_our_nodeid();
472e7fd4179SDavid Teigland 	rc->rc_header.h_length = mb_len;
473e7fd4179SDavid Teigland 	rc->rc_header.h_cmd = DLM_RCOM;
474e7fd4179SDavid Teigland 
475e7fd4179SDavid Teigland 	rc->rc_type = DLM_RCOM_STATUS_REPLY;
476f5888750SDavid Teigland 	rc->rc_id = rc_in->rc_id;
47738aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
478e7fd4179SDavid Teigland 	rc->rc_result = -ESRCH;
479e7fd4179SDavid Teigland 
4801babdb45SDavid Teigland 	rf = (struct rcom_config *) rc->rc_buf;
48193ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(~0U);
4821babdb45SDavid Teigland 
483e7fd4179SDavid Teigland 	dlm_rcom_out(rc);
484e7fd4179SDavid Teigland 	dlm_lowcomms_commit_buffer(mh);
485e7fd4179SDavid Teigland 
486e7fd4179SDavid Teigland 	return 0;
487e7fd4179SDavid Teigland }
488e7fd4179SDavid Teigland 
489c04fecb4SDavid Teigland /*
490c04fecb4SDavid Teigland  * Ignore messages for stage Y before we set
491c04fecb4SDavid Teigland  * recover_status bit for stage X:
492c04fecb4SDavid Teigland  *
493c04fecb4SDavid Teigland  * recover_status = 0
494c04fecb4SDavid Teigland  *
495c04fecb4SDavid Teigland  * dlm_recover_members()
496c04fecb4SDavid Teigland  * - send nothing
497c04fecb4SDavid Teigland  * - recv nothing
498c04fecb4SDavid Teigland  * - ignore NAMES, NAMES_REPLY
499c04fecb4SDavid Teigland  * - ignore LOOKUP, LOOKUP_REPLY
500c04fecb4SDavid Teigland  * - ignore LOCK, LOCK_REPLY
501c04fecb4SDavid Teigland  *
502c04fecb4SDavid Teigland  * recover_status |= NODES
503c04fecb4SDavid Teigland  *
504c04fecb4SDavid Teigland  * dlm_recover_members_wait()
505c04fecb4SDavid Teigland  *
506c04fecb4SDavid Teigland  * dlm_recover_directory()
507c04fecb4SDavid Teigland  * - send NAMES
508c04fecb4SDavid Teigland  * - recv NAMES_REPLY
509c04fecb4SDavid Teigland  * - ignore LOOKUP, LOOKUP_REPLY
510c04fecb4SDavid Teigland  * - ignore LOCK, LOCK_REPLY
511c04fecb4SDavid Teigland  *
512c04fecb4SDavid Teigland  * recover_status |= DIR
513c04fecb4SDavid Teigland  *
514c04fecb4SDavid Teigland  * dlm_recover_directory_wait()
515c04fecb4SDavid Teigland  *
516c04fecb4SDavid Teigland  * dlm_recover_masters()
517c04fecb4SDavid Teigland  * - send LOOKUP
518c04fecb4SDavid Teigland  * - recv LOOKUP_REPLY
519c04fecb4SDavid Teigland  *
520c04fecb4SDavid Teigland  * dlm_recover_locks()
521c04fecb4SDavid Teigland  * - send LOCKS
522c04fecb4SDavid Teigland  * - recv LOCKS_REPLY
523c04fecb4SDavid Teigland  *
524c04fecb4SDavid Teigland  * recover_status |= LOCKS
525c04fecb4SDavid Teigland  *
526c04fecb4SDavid Teigland  * dlm_recover_locks_wait()
527c04fecb4SDavid Teigland  *
528c04fecb4SDavid Teigland  * recover_status |= DONE
529c04fecb4SDavid Teigland  */
530c04fecb4SDavid Teigland 
531c36258b5SDavid Teigland /* Called by dlm_recv; corresponds to dlm_receive_message() but special
532e7fd4179SDavid Teigland    recovery-only comms are sent through here. */
533e7fd4179SDavid Teigland 
534c36258b5SDavid Teigland void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
535e7fd4179SDavid Teigland {
536ae773d0bSAl Viro 	int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
537c04fecb4SDavid Teigland 	int stop, reply = 0, names = 0, lookup = 0, lock = 0;
5384875647aSDavid Teigland 	uint32_t status;
539d6e24788SDavid Teigland 	uint64_t seq;
540ae773d0bSAl Viro 
541d6e24788SDavid Teigland 	switch (rc->rc_type) {
542c04fecb4SDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
543c04fecb4SDavid Teigland 		reply = 1;
544c04fecb4SDavid Teigland 		break;
545c04fecb4SDavid Teigland 	case DLM_RCOM_NAMES:
546c04fecb4SDavid Teigland 		names = 1;
547c04fecb4SDavid Teigland 		break;
548c04fecb4SDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
549c04fecb4SDavid Teigland 		names = 1;
550c04fecb4SDavid Teigland 		reply = 1;
551c04fecb4SDavid Teigland 		break;
552c04fecb4SDavid Teigland 	case DLM_RCOM_LOOKUP:
553c04fecb4SDavid Teigland 		lookup = 1;
554c04fecb4SDavid Teigland 		break;
555c04fecb4SDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
556c04fecb4SDavid Teigland 		lookup = 1;
557c04fecb4SDavid Teigland 		reply = 1;
558c04fecb4SDavid Teigland 		break;
5594875647aSDavid Teigland 	case DLM_RCOM_LOCK:
5604875647aSDavid Teigland 		lock = 1;
5614875647aSDavid Teigland 		break;
5624875647aSDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
5634875647aSDavid Teigland 		lock = 1;
5644875647aSDavid Teigland 		reply = 1;
5654875647aSDavid Teigland 		break;
566d6e24788SDavid Teigland 	};
567d6e24788SDavid Teigland 
568d6e24788SDavid Teigland 	spin_lock(&ls->ls_recover_lock);
5694875647aSDavid Teigland 	status = ls->ls_recover_status;
570475f230cSDavid Teigland 	stop = test_bit(LSFL_RECOVER_STOP, &ls->ls_flags);
571d6e24788SDavid Teigland 	seq = ls->ls_recover_seq;
572d6e24788SDavid Teigland 	spin_unlock(&ls->ls_recover_lock);
573d6e24788SDavid Teigland 
574c04fecb4SDavid Teigland 	if (stop && (rc->rc_type != DLM_RCOM_STATUS))
575c04fecb4SDavid Teigland 		goto ignore;
576c04fecb4SDavid Teigland 
577c04fecb4SDavid Teigland 	if (reply && (rc->rc_seq_reply != seq))
578c04fecb4SDavid Teigland 		goto ignore;
579c04fecb4SDavid Teigland 
580c04fecb4SDavid Teigland 	if (!(status & DLM_RS_NODES) && (names || lookup || lock))
581c04fecb4SDavid Teigland 		goto ignore;
582c04fecb4SDavid Teigland 
583c04fecb4SDavid Teigland 	if (!(status & DLM_RS_DIR) && (lookup || lock))
584c04fecb4SDavid Teigland 		goto ignore;
585e7fd4179SDavid Teigland 
586e7fd4179SDavid Teigland 	switch (rc->rc_type) {
587e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS:
588e7fd4179SDavid Teigland 		receive_rcom_status(ls, rc);
589e7fd4179SDavid Teigland 		break;
590e7fd4179SDavid Teigland 
591e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES:
592e7fd4179SDavid Teigland 		receive_rcom_names(ls, rc);
593e7fd4179SDavid Teigland 		break;
594e7fd4179SDavid Teigland 
595e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP:
596e7fd4179SDavid Teigland 		receive_rcom_lookup(ls, rc);
597e7fd4179SDavid Teigland 		break;
598e7fd4179SDavid Teigland 
599e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK:
600ae773d0bSAl Viro 		if (rc->rc_header.h_length < lock_size)
601ae773d0bSAl Viro 			goto Eshort;
602e7fd4179SDavid Teigland 		receive_rcom_lock(ls, rc);
603e7fd4179SDavid Teigland 		break;
604e7fd4179SDavid Teigland 
605e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
606dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
607e7fd4179SDavid Teigland 		break;
608e7fd4179SDavid Teigland 
609e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
610dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
611e7fd4179SDavid Teigland 		break;
612e7fd4179SDavid Teigland 
613e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
614e7fd4179SDavid Teigland 		receive_rcom_lookup_reply(ls, rc);
615e7fd4179SDavid Teigland 		break;
616e7fd4179SDavid Teigland 
617e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
618ae773d0bSAl Viro 		if (rc->rc_header.h_length < lock_size)
619ae773d0bSAl Viro 			goto Eshort;
620dbcfc347SDavid Teigland 		dlm_recover_process_copy(ls, rc);
621e7fd4179SDavid Teigland 		break;
622e7fd4179SDavid Teigland 
623e7fd4179SDavid Teigland 	default:
624dbcfc347SDavid Teigland 		log_error(ls, "receive_rcom bad type %d", rc->rc_type);
625e7fd4179SDavid Teigland 	}
626c04fecb4SDavid Teigland 	return;
627c04fecb4SDavid Teigland 
628c04fecb4SDavid Teigland ignore:
629c04fecb4SDavid Teigland 	log_limit(ls, "dlm_receive_rcom ignore msg %d "
630c04fecb4SDavid Teigland 		  "from %d %llu %llu recover seq %llu sts %x gen %u",
631c04fecb4SDavid Teigland 		   rc->rc_type,
632c04fecb4SDavid Teigland 		   nodeid,
633c04fecb4SDavid Teigland 		   (unsigned long long)rc->rc_seq,
634c04fecb4SDavid Teigland 		   (unsigned long long)rc->rc_seq_reply,
635c04fecb4SDavid Teigland 		   (unsigned long long)seq,
636c04fecb4SDavid Teigland 		   status, ls->ls_generation);
637c36258b5SDavid Teigland 	return;
638ae773d0bSAl Viro Eshort:
639c04fecb4SDavid Teigland 	log_error(ls, "recovery message %d from %d is too short",
640ae773d0bSAl Viro 		  rc->rc_type, nodeid);
641e7fd4179SDavid Teigland }
642e7fd4179SDavid Teigland 
643