xref: /openbmc/linux/fs/dlm/rcom.c (revision 93ff2971e99c90d1c4d39d242ef6050d2dc853d3)
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 
4144f487a5SPatrick Caulfield 	mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, ls->ls_allocation, &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 {
81*93ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
82*93ff2971SAl 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;
889e971b71SDavid Teigland 
899e971b71SDavid Teigland 	if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
909e971b71SDavid Teigland 		log_error(ls, "version mismatch: %x nodeid %d: %x",
919e971b71SDavid Teigland 			  DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
929e971b71SDavid Teigland 			  rc->rc_header.h_version);
938b0e7b2cSDavid Teigland 		return -EPROTO;
949e971b71SDavid Teigland 	}
959e971b71SDavid Teigland 
96*93ff2971SAl Viro 	if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
97*93ff2971SAl Viro 	    le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
98e7fd4179SDavid Teigland 		log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
99*93ff2971SAl Viro 			  ls->ls_lvblen, ls->ls_exflags, nodeid,
100*93ff2971SAl Viro 			  le32_to_cpu(rf->rf_lvblen),
101*93ff2971SAl Viro 			  le32_to_cpu(rf->rf_lsflags));
1028b0e7b2cSDavid Teigland 		return -EPROTO;
103e7fd4179SDavid Teigland 	}
104e7fd4179SDavid Teigland 	return 0;
105e7fd4179SDavid Teigland }
106e7fd4179SDavid Teigland 
10798f176fbSDavid Teigland static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
10898f176fbSDavid Teigland {
10998f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
11098f176fbSDavid Teigland 	*new_seq = ++ls->ls_rcom_seq;
11198f176fbSDavid Teigland 	set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
11298f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
11398f176fbSDavid Teigland }
11498f176fbSDavid Teigland 
11598f176fbSDavid Teigland static void disallow_sync_reply(struct dlm_ls *ls)
11698f176fbSDavid Teigland {
11798f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
11898f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
11998f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
12098f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
12198f176fbSDavid Teigland }
12298f176fbSDavid Teigland 
123e7fd4179SDavid Teigland int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
124e7fd4179SDavid Teigland {
125e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
126e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
127e7fd4179SDavid Teigland 	int error = 0;
128e7fd4179SDavid Teigland 
129faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
130e7fd4179SDavid Teigland 
131e7fd4179SDavid Teigland 	if (nodeid == dlm_our_nodeid()) {
132e7fd4179SDavid Teigland 		rc = (struct dlm_rcom *) ls->ls_recover_buf;
133e7fd4179SDavid Teigland 		rc->rc_result = dlm_recover_status(ls);
134e7fd4179SDavid Teigland 		goto out;
135e7fd4179SDavid Teigland 	}
136e7fd4179SDavid Teigland 
137e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
138e7fd4179SDavid Teigland 	if (error)
139e7fd4179SDavid Teigland 		goto out;
14098f176fbSDavid Teigland 
14198f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
14268c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
143e7fd4179SDavid Teigland 
144e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
145e7fd4179SDavid Teigland 
146e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
14798f176fbSDavid Teigland 	disallow_sync_reply(ls);
148e7fd4179SDavid Teigland 	if (error)
149e7fd4179SDavid Teigland 		goto out;
150e7fd4179SDavid Teigland 
151e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) ls->ls_recover_buf;
152e7fd4179SDavid Teigland 
153e7fd4179SDavid Teigland 	if (rc->rc_result == -ESRCH) {
154e7fd4179SDavid Teigland 		/* we pretend the remote lockspace exists with 0 status */
155e7fd4179SDavid Teigland 		log_debug(ls, "remote node %d not ready", nodeid);
156e7fd4179SDavid Teigland 		rc->rc_result = 0;
157e7fd4179SDavid Teigland 	} else
1589e971b71SDavid Teigland 		error = check_config(ls, rc, nodeid);
159e7fd4179SDavid Teigland 	/* the caller looks at rc_result for the remote recovery status */
160e7fd4179SDavid Teigland  out:
161e7fd4179SDavid Teigland 	return error;
162e7fd4179SDavid Teigland }
163e7fd4179SDavid Teigland 
164e7fd4179SDavid Teigland static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
165e7fd4179SDavid Teigland {
166e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
167e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
168e7fd4179SDavid Teigland 	int error, nodeid = rc_in->rc_header.h_nodeid;
169e7fd4179SDavid Teigland 
170e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
171e7fd4179SDavid Teigland 			    sizeof(struct rcom_config), &rc, &mh);
172e7fd4179SDavid Teigland 	if (error)
173e7fd4179SDavid Teigland 		return;
1744a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
17538aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
176e7fd4179SDavid Teigland 	rc->rc_result = dlm_recover_status(ls);
177e7fd4179SDavid Teigland 	make_config(ls, (struct rcom_config *) rc->rc_buf);
178e7fd4179SDavid Teigland 
179e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
180e7fd4179SDavid Teigland }
181e7fd4179SDavid Teigland 
1824a99c3d9SDavid Teigland static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
183e7fd4179SDavid Teigland {
18498f176fbSDavid Teigland 	spin_lock(&ls->ls_rcom_spin);
18598f176fbSDavid Teigland 	if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
18698f176fbSDavid Teigland 	    rc_in->rc_id != ls->ls_rcom_seq) {
18798f176fbSDavid Teigland 		log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
18898f176fbSDavid Teigland 			  rc_in->rc_type, rc_in->rc_header.h_nodeid,
18957adf7eeSRyusuke Konishi 			  (unsigned long long)rc_in->rc_id,
19057adf7eeSRyusuke Konishi 			  (unsigned long long)ls->ls_rcom_seq);
19198f176fbSDavid Teigland 		goto out;
1924a99c3d9SDavid Teigland 	}
193e7fd4179SDavid Teigland 	memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
194e7fd4179SDavid Teigland 	set_bit(LSFL_RCOM_READY, &ls->ls_flags);
19598f176fbSDavid Teigland 	clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
196e7fd4179SDavid Teigland 	wake_up(&ls->ls_wait_general);
19798f176fbSDavid Teigland  out:
19898f176fbSDavid Teigland 	spin_unlock(&ls->ls_rcom_spin);
199e7fd4179SDavid Teigland }
200e7fd4179SDavid Teigland 
201e7fd4179SDavid Teigland int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
202e7fd4179SDavid Teigland {
203e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
204e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
205e7fd4179SDavid Teigland 	int error = 0, len = sizeof(struct dlm_rcom);
206e7fd4179SDavid Teigland 
207faa0f267SDavid Teigland 	ls->ls_recover_nodeid = nodeid;
208e7fd4179SDavid Teigland 
209e7fd4179SDavid Teigland 	if (nodeid == dlm_our_nodeid()) {
210e7fd4179SDavid Teigland 		dlm_copy_master_names(ls, last_name, last_len,
211e7fd4179SDavid Teigland 		                      ls->ls_recover_buf + len,
21268c817a1SDavid Teigland 		                      dlm_config.ci_buffer_size - len, nodeid);
213e7fd4179SDavid Teigland 		goto out;
214e7fd4179SDavid Teigland 	}
215e7fd4179SDavid Teigland 
216e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
217e7fd4179SDavid Teigland 	if (error)
218e7fd4179SDavid Teigland 		goto out;
219e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, last_name, last_len);
22098f176fbSDavid Teigland 
22198f176fbSDavid Teigland 	allow_sync_reply(ls, &rc->rc_id);
22268c817a1SDavid Teigland 	memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
223e7fd4179SDavid Teigland 
224e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
225e7fd4179SDavid Teigland 
226e7fd4179SDavid Teigland 	error = dlm_wait_function(ls, &rcom_response);
22798f176fbSDavid Teigland 	disallow_sync_reply(ls);
228e7fd4179SDavid Teigland  out:
229e7fd4179SDavid Teigland 	return error;
230e7fd4179SDavid Teigland }
231e7fd4179SDavid Teigland 
232e7fd4179SDavid Teigland static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
233e7fd4179SDavid Teigland {
234e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
235e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
23638aa8b0cSDavid Teigland 	int error, inlen, outlen, nodeid;
237e7fd4179SDavid Teigland 
238e7fd4179SDavid Teigland 	nodeid = rc_in->rc_header.h_nodeid;
239e7fd4179SDavid Teigland 	inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
24068c817a1SDavid Teigland 	outlen = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
241e7fd4179SDavid Teigland 
242e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
243e7fd4179SDavid Teigland 	if (error)
244e7fd4179SDavid Teigland 		return;
2454a99c3d9SDavid Teigland 	rc->rc_id = rc_in->rc_id;
24638aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
247e7fd4179SDavid Teigland 
248e7fd4179SDavid Teigland 	dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
249e7fd4179SDavid Teigland 			      nodeid);
250e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
251e7fd4179SDavid Teigland }
252e7fd4179SDavid Teigland 
253e7fd4179SDavid Teigland int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
254e7fd4179SDavid Teigland {
255e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
256e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
257e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
258e7fd4179SDavid Teigland 	int error;
259e7fd4179SDavid Teigland 
260e7fd4179SDavid Teigland 	error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
261e7fd4179SDavid Teigland 			    &rc, &mh);
262e7fd4179SDavid Teigland 	if (error)
263e7fd4179SDavid Teigland 		goto out;
264e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, r->res_name, r->res_length);
265e7fd4179SDavid Teigland 	rc->rc_id = (unsigned long) r;
266e7fd4179SDavid Teigland 
267e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
268e7fd4179SDavid Teigland  out:
269e7fd4179SDavid Teigland 	return error;
270e7fd4179SDavid Teigland }
271e7fd4179SDavid Teigland 
272e7fd4179SDavid Teigland static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
273e7fd4179SDavid Teigland {
274e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
275e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
276e7fd4179SDavid Teigland 	int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
277e7fd4179SDavid Teigland 	int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
278e7fd4179SDavid Teigland 
279e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
280e7fd4179SDavid Teigland 	if (error)
281e7fd4179SDavid Teigland 		return;
282e7fd4179SDavid Teigland 
283e7fd4179SDavid Teigland 	error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
284e7fd4179SDavid Teigland 	if (error)
285e7fd4179SDavid Teigland 		ret_nodeid = error;
286e7fd4179SDavid Teigland 	rc->rc_result = ret_nodeid;
287e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
28838aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
289e7fd4179SDavid Teigland 
290e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
291e7fd4179SDavid Teigland }
292e7fd4179SDavid Teigland 
293e7fd4179SDavid Teigland static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
294e7fd4179SDavid Teigland {
295e7fd4179SDavid Teigland 	dlm_recover_master_reply(ls, rc_in);
296e7fd4179SDavid Teigland }
297e7fd4179SDavid Teigland 
298e7fd4179SDavid Teigland static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
299e7fd4179SDavid Teigland 			   struct rcom_lock *rl)
300e7fd4179SDavid Teigland {
301e7fd4179SDavid Teigland 	memset(rl, 0, sizeof(*rl));
302e7fd4179SDavid Teigland 
303163a1859SAl Viro 	rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
304163a1859SAl Viro 	rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
305163a1859SAl Viro 	rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
306163a1859SAl Viro 	rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
307163a1859SAl Viro 	rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
308e7fd4179SDavid Teigland 	rl->rl_rqmode = lkb->lkb_rqmode;
309e7fd4179SDavid Teigland 	rl->rl_grmode = lkb->lkb_grmode;
310e7fd4179SDavid Teigland 	rl->rl_status = lkb->lkb_status;
311163a1859SAl Viro 	rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
312e7fd4179SDavid Teigland 
313e7fd4179SDavid Teigland 	if (lkb->lkb_bastaddr)
314e7fd4179SDavid Teigland 		rl->rl_asts |= AST_BAST;
315e7fd4179SDavid Teigland 	if (lkb->lkb_astaddr)
316e7fd4179SDavid Teigland 		rl->rl_asts |= AST_COMP;
317e7fd4179SDavid Teigland 
318163a1859SAl Viro 	rl->rl_namelen = cpu_to_le16(r->res_length);
319e7fd4179SDavid Teigland 	memcpy(rl->rl_name, r->res_name, r->res_length);
320e7fd4179SDavid Teigland 
321e7fd4179SDavid Teigland 	/* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
322e7fd4179SDavid Teigland 	   If so, receive_rcom_lock_args() won't take this copy. */
323e7fd4179SDavid Teigland 
324e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
325e7fd4179SDavid Teigland 		memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
326e7fd4179SDavid Teigland }
327e7fd4179SDavid Teigland 
328e7fd4179SDavid Teigland int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
329e7fd4179SDavid Teigland {
330e7fd4179SDavid Teigland 	struct dlm_ls *ls = r->res_ls;
331e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
332e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
333e7fd4179SDavid Teigland 	struct rcom_lock *rl;
334e7fd4179SDavid Teigland 	int error, len = sizeof(struct rcom_lock);
335e7fd4179SDavid Teigland 
336e7fd4179SDavid Teigland 	if (lkb->lkb_lvbptr)
337e7fd4179SDavid Teigland 		len += ls->ls_lvblen;
338e7fd4179SDavid Teigland 
339e7fd4179SDavid Teigland 	error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
340e7fd4179SDavid Teigland 	if (error)
341e7fd4179SDavid Teigland 		goto out;
342e7fd4179SDavid Teigland 
343e7fd4179SDavid Teigland 	rl = (struct rcom_lock *) rc->rc_buf;
344e7fd4179SDavid Teigland 	pack_rcom_lock(r, lkb, rl);
345e7fd4179SDavid Teigland 	rc->rc_id = (unsigned long) r;
346e7fd4179SDavid Teigland 
347e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
348e7fd4179SDavid Teigland  out:
349e7fd4179SDavid Teigland 	return error;
350e7fd4179SDavid Teigland }
351e7fd4179SDavid Teigland 
352e7fd4179SDavid Teigland static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
353e7fd4179SDavid Teigland {
354e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
355e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
356e7fd4179SDavid Teigland 	int error, nodeid = rc_in->rc_header.h_nodeid;
357e7fd4179SDavid Teigland 
358e7fd4179SDavid Teigland 	dlm_recover_master_copy(ls, rc_in);
359e7fd4179SDavid Teigland 
360e7fd4179SDavid Teigland 	error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
361e7fd4179SDavid Teigland 			    sizeof(struct rcom_lock), &rc, &mh);
362e7fd4179SDavid Teigland 	if (error)
363e7fd4179SDavid Teigland 		return;
364e7fd4179SDavid Teigland 
365e7fd4179SDavid Teigland 	/* We send back the same rcom_lock struct we received, but
366e7fd4179SDavid Teigland 	   dlm_recover_master_copy() has filled in rl_remid and rl_result */
367e7fd4179SDavid Teigland 
368e7fd4179SDavid Teigland 	memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
369e7fd4179SDavid Teigland 	rc->rc_id = rc_in->rc_id;
37038aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
371e7fd4179SDavid Teigland 
372e7fd4179SDavid Teigland 	send_rcom(ls, mh, rc);
373e7fd4179SDavid Teigland }
374e7fd4179SDavid Teigland 
375c36258b5SDavid Teigland /* If the lockspace doesn't exist then still send a status message
376c36258b5SDavid Teigland    back; it's possible that it just doesn't have its global_id yet. */
377c36258b5SDavid Teigland 
378c36258b5SDavid Teigland int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
379e7fd4179SDavid Teigland {
380e7fd4179SDavid Teigland 	struct dlm_rcom *rc;
3811babdb45SDavid Teigland 	struct rcom_config *rf;
382e7fd4179SDavid Teigland 	struct dlm_mhandle *mh;
383e7fd4179SDavid Teigland 	char *mb;
3841babdb45SDavid Teigland 	int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
385e7fd4179SDavid Teigland 
38641684f95SDavid Teigland 	mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_NOFS, &mb);
387e7fd4179SDavid Teigland 	if (!mh)
388e7fd4179SDavid Teigland 		return -ENOBUFS;
389e7fd4179SDavid Teigland 	memset(mb, 0, mb_len);
390e7fd4179SDavid Teigland 
391e7fd4179SDavid Teigland 	rc = (struct dlm_rcom *) mb;
392e7fd4179SDavid Teigland 
393e7fd4179SDavid Teigland 	rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
394e7fd4179SDavid Teigland 	rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
395e7fd4179SDavid Teigland 	rc->rc_header.h_nodeid = dlm_our_nodeid();
396e7fd4179SDavid Teigland 	rc->rc_header.h_length = mb_len;
397e7fd4179SDavid Teigland 	rc->rc_header.h_cmd = DLM_RCOM;
398e7fd4179SDavid Teigland 
399e7fd4179SDavid Teigland 	rc->rc_type = DLM_RCOM_STATUS_REPLY;
400f5888750SDavid Teigland 	rc->rc_id = rc_in->rc_id;
40138aa8b0cSDavid Teigland 	rc->rc_seq_reply = rc_in->rc_seq;
402e7fd4179SDavid Teigland 	rc->rc_result = -ESRCH;
403e7fd4179SDavid Teigland 
4041babdb45SDavid Teigland 	rf = (struct rcom_config *) rc->rc_buf;
405*93ff2971SAl Viro 	rf->rf_lvblen = cpu_to_le32(~0U);
4061babdb45SDavid Teigland 
407e7fd4179SDavid Teigland 	dlm_rcom_out(rc);
408e7fd4179SDavid Teigland 	dlm_lowcomms_commit_buffer(mh);
409e7fd4179SDavid Teigland 
410e7fd4179SDavid Teigland 	return 0;
411e7fd4179SDavid Teigland }
412e7fd4179SDavid Teigland 
41338aa8b0cSDavid Teigland static int is_old_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
41438aa8b0cSDavid Teigland {
41538aa8b0cSDavid Teigland 	uint64_t seq;
41638aa8b0cSDavid Teigland 	int rv = 0;
41738aa8b0cSDavid Teigland 
41838aa8b0cSDavid Teigland 	switch (rc->rc_type) {
41938aa8b0cSDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
42038aa8b0cSDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
42138aa8b0cSDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
42238aa8b0cSDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
42338aa8b0cSDavid Teigland 		spin_lock(&ls->ls_recover_lock);
42438aa8b0cSDavid Teigland 		seq = ls->ls_recover_seq;
42538aa8b0cSDavid Teigland 		spin_unlock(&ls->ls_recover_lock);
42638aa8b0cSDavid Teigland 		if (rc->rc_seq_reply != seq) {
4278ec68867SDavid Teigland 			log_debug(ls, "ignoring old reply %x from %d "
42838aa8b0cSDavid Teigland 				      "seq_reply %llx expect %llx",
42938aa8b0cSDavid Teigland 				      rc->rc_type, rc->rc_header.h_nodeid,
43038aa8b0cSDavid Teigland 				      (unsigned long long)rc->rc_seq_reply,
43138aa8b0cSDavid Teigland 				      (unsigned long long)seq);
43238aa8b0cSDavid Teigland 			rv = 1;
43338aa8b0cSDavid Teigland 		}
43438aa8b0cSDavid Teigland 	}
43538aa8b0cSDavid Teigland 	return rv;
43638aa8b0cSDavid Teigland }
43738aa8b0cSDavid Teigland 
438c36258b5SDavid Teigland /* Called by dlm_recv; corresponds to dlm_receive_message() but special
439e7fd4179SDavid Teigland    recovery-only comms are sent through here. */
440e7fd4179SDavid Teigland 
441c36258b5SDavid Teigland void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
442e7fd4179SDavid Teigland {
443e7fd4179SDavid Teigland 	if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
4448ec68867SDavid Teigland 		log_debug(ls, "ignoring recovery message %x from %d",
445e7fd4179SDavid Teigland 			  rc->rc_type, nodeid);
446e7fd4179SDavid Teigland 		goto out;
447e7fd4179SDavid Teigland 	}
448e7fd4179SDavid Teigland 
44938aa8b0cSDavid Teigland 	if (is_old_reply(ls, rc))
45038aa8b0cSDavid Teigland 		goto out;
45138aa8b0cSDavid Teigland 
452e7fd4179SDavid Teigland 	switch (rc->rc_type) {
453e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS:
454e7fd4179SDavid Teigland 		receive_rcom_status(ls, rc);
455e7fd4179SDavid Teigland 		break;
456e7fd4179SDavid Teigland 
457e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES:
458e7fd4179SDavid Teigland 		receive_rcom_names(ls, rc);
459e7fd4179SDavid Teigland 		break;
460e7fd4179SDavid Teigland 
461e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP:
462e7fd4179SDavid Teigland 		receive_rcom_lookup(ls, rc);
463e7fd4179SDavid Teigland 		break;
464e7fd4179SDavid Teigland 
465e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK:
466e7fd4179SDavid Teigland 		receive_rcom_lock(ls, rc);
467e7fd4179SDavid Teigland 		break;
468e7fd4179SDavid Teigland 
469e7fd4179SDavid Teigland 	case DLM_RCOM_STATUS_REPLY:
470dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
471e7fd4179SDavid Teigland 		break;
472e7fd4179SDavid Teigland 
473e7fd4179SDavid Teigland 	case DLM_RCOM_NAMES_REPLY:
474dbcfc347SDavid Teigland 		receive_sync_reply(ls, rc);
475e7fd4179SDavid Teigland 		break;
476e7fd4179SDavid Teigland 
477e7fd4179SDavid Teigland 	case DLM_RCOM_LOOKUP_REPLY:
478e7fd4179SDavid Teigland 		receive_rcom_lookup_reply(ls, rc);
479e7fd4179SDavid Teigland 		break;
480e7fd4179SDavid Teigland 
481e7fd4179SDavid Teigland 	case DLM_RCOM_LOCK_REPLY:
482dbcfc347SDavid Teigland 		dlm_recover_process_copy(ls, rc);
483e7fd4179SDavid Teigland 		break;
484e7fd4179SDavid Teigland 
485e7fd4179SDavid Teigland 	default:
486dbcfc347SDavid Teigland 		log_error(ls, "receive_rcom bad type %d", rc->rc_type);
487e7fd4179SDavid Teigland 	}
488e7fd4179SDavid Teigland  out:
489c36258b5SDavid Teigland 	return;
490e7fd4179SDavid Teigland }
491e7fd4179SDavid Teigland 
492