blob: 6f653a339beae05cb0760d0e8d5767a85881bee3 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglande7fd4172006-01-18 09:30:29 +00002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teiglanddbcfc342008-01-29 14:52:10 -06006** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00007**
David Teiglande7fd4172006-01-18 09:30:29 +00008**
9*******************************************************************************
10******************************************************************************/
11
12#include "dlm_internal.h"
13#include "lockspace.h"
14#include "member.h"
15#include "lowcomms.h"
16#include "midcomms.h"
17#include "rcom.h"
18#include "recover.h"
19#include "dir.h"
20#include "config.h"
21#include "memory.h"
22#include "lock.h"
23#include "util.h"
David Teiglande7fd4172006-01-18 09:30:29 +000024
25static int rcom_response(struct dlm_ls *ls)
26{
27 return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
28}
29
Alexander Aringa070a912021-05-21 15:08:41 -040030static void _create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
31 struct dlm_rcom **rc_ret, char *mb, int mb_len)
David Teiglande7fd4172006-01-18 09:30:29 +000032{
33 struct dlm_rcom *rc;
David Teiglande7fd4172006-01-18 09:30:29 +000034
35 rc = (struct dlm_rcom *) mb;
36
37 rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
38 rc->rc_header.h_lockspace = ls->ls_global_id;
39 rc->rc_header.h_nodeid = dlm_our_nodeid();
40 rc->rc_header.h_length = mb_len;
41 rc->rc_header.h_cmd = DLM_RCOM;
42
43 rc->rc_type = type;
44
David Teigland38aa8b02006-12-13 10:37:16 -060045 spin_lock(&ls->ls_recover_lock);
46 rc->rc_seq = ls->ls_recover_seq;
47 spin_unlock(&ls->ls_recover_lock);
48
David Teiglande7fd4172006-01-18 09:30:29 +000049 *rc_ret = rc;
Alexander Aringa070a912021-05-21 15:08:41 -040050}
51
52static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
53 struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret)
54{
55 int mb_len = sizeof(struct dlm_rcom) + len;
56 struct dlm_mhandle *mh;
57 char *mb;
58
59 mh = dlm_midcomms_get_mhandle(to_nodeid, mb_len, GFP_NOFS, &mb);
60 if (!mh) {
61 log_print("%s to %d type %d len %d ENOBUFS",
62 __func__, to_nodeid, type, len);
63 return -ENOBUFS;
64 }
65
66 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len);
67 *mh_ret = mh;
David Teiglande7fd4172006-01-18 09:30:29 +000068 return 0;
69}
70
Alexander Aringa070a912021-05-21 15:08:41 -040071static int create_rcom_stateless(struct dlm_ls *ls, int to_nodeid, int type,
72 int len, struct dlm_rcom **rc_ret,
Alexander Aring8f2dc782021-05-21 15:08:42 -040073 struct dlm_msg **msg_ret)
Alexander Aringa070a912021-05-21 15:08:41 -040074{
75 int mb_len = sizeof(struct dlm_rcom) + len;
Alexander Aring8f2dc782021-05-21 15:08:42 -040076 struct dlm_msg *msg;
Alexander Aringa070a912021-05-21 15:08:41 -040077 char *mb;
78
Alexander Aring8f2dc782021-05-21 15:08:42 -040079 msg = dlm_lowcomms_new_msg(to_nodeid, mb_len, GFP_NOFS, &mb,
80 NULL, NULL);
81 if (!msg) {
Alexander Aringa070a912021-05-21 15:08:41 -040082 log_print("create_rcom to %d type %d len %d ENOBUFS",
83 to_nodeid, type, len);
84 return -ENOBUFS;
85 }
86
87 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len);
Alexander Aring8f2dc782021-05-21 15:08:42 -040088 *msg_ret = msg;
Alexander Aringa070a912021-05-21 15:08:41 -040089 return 0;
90}
91
92static void _send_rcom(struct dlm_ls *ls, struct dlm_rcom *rc)
93{
94 dlm_rcom_out(rc);
95}
96
David Teiglande7fd4172006-01-18 09:30:29 +000097static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
98 struct dlm_rcom *rc)
99{
Alexander Aringa070a912021-05-21 15:08:41 -0400100 _send_rcom(ls, rc);
101 dlm_midcomms_commit_mhandle(mh);
102}
103
Alexander Aring8f2dc782021-05-21 15:08:42 -0400104static void send_rcom_stateless(struct dlm_ls *ls, struct dlm_msg *msg,
Alexander Aringa070a912021-05-21 15:08:41 -0400105 struct dlm_rcom *rc)
106{
107 _send_rcom(ls, rc);
Alexander Aring8f2dc782021-05-21 15:08:42 -0400108 dlm_lowcomms_commit_msg(msg);
109 dlm_lowcomms_put_msg(msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000110}
111
David Teigland757a4272011-10-20 13:26:28 -0500112static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs,
113 uint32_t flags)
114{
115 rs->rs_flags = cpu_to_le32(flags);
116}
117
David Teiglande7fd4172006-01-18 09:30:29 +0000118/* When replying to a status request, a node also sends back its
119 configuration values. The requesting node then checks that the remote
120 node is configured the same way as itself. */
121
David Teigland757a4272011-10-20 13:26:28 -0500122static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf,
123 uint32_t num_slots)
David Teiglande7fd4172006-01-18 09:30:29 +0000124{
Al Viro93ff2972008-01-25 02:34:00 -0500125 rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
126 rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
David Teigland757a4272011-10-20 13:26:28 -0500127
128 rf->rf_our_slot = cpu_to_le16(ls->ls_slot);
129 rf->rf_num_slots = cpu_to_le16(num_slots);
130 rf->rf_generation = cpu_to_le32(ls->ls_generation);
David Teiglande7fd4172006-01-18 09:30:29 +0000131}
132
David Teigland757a4272011-10-20 13:26:28 -0500133static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000134{
David Teigland9e971b72006-12-13 10:37:55 -0600135 struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
136
137 if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
138 log_error(ls, "version mismatch: %x nodeid %d: %x",
139 DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
140 rc->rc_header.h_version);
David Teigland8b0e7b22007-05-18 09:03:35 -0500141 return -EPROTO;
David Teigland9e971b72006-12-13 10:37:55 -0600142 }
143
Al Viro93ff2972008-01-25 02:34:00 -0500144 if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
145 le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
David Teiglande7fd4172006-01-18 09:30:29 +0000146 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
Al Viro93ff2972008-01-25 02:34:00 -0500147 ls->ls_lvblen, ls->ls_exflags, nodeid,
148 le32_to_cpu(rf->rf_lvblen),
149 le32_to_cpu(rf->rf_lsflags));
David Teigland8b0e7b22007-05-18 09:03:35 -0500150 return -EPROTO;
David Teiglande7fd4172006-01-18 09:30:29 +0000151 }
152 return 0;
153}
154
David Teigland98f176f2006-11-27 13:19:28 -0600155static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
156{
157 spin_lock(&ls->ls_rcom_spin);
158 *new_seq = ++ls->ls_rcom_seq;
159 set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
160 spin_unlock(&ls->ls_rcom_spin);
161}
162
163static void disallow_sync_reply(struct dlm_ls *ls)
164{
165 spin_lock(&ls->ls_rcom_spin);
166 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
167 clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
168 spin_unlock(&ls->ls_rcom_spin);
169}
170
David Teigland757a4272011-10-20 13:26:28 -0500171/*
172 * low nodeid gathers one slot value at a time from each node.
173 * it sets need_slots=0, and saves rf_our_slot returned from each
174 * rcom_config.
175 *
176 * other nodes gather all slot values at once from the low nodeid.
177 * they set need_slots=1, and ignore the rf_our_slot returned from each
178 * rcom_config. they use the rf_num_slots returned from the low
179 * node's rcom_config.
180 */
181
182int dlm_rcom_status(struct dlm_ls *ls, int nodeid, uint32_t status_flags)
David Teiglande7fd4172006-01-18 09:30:29 +0000183{
184 struct dlm_rcom *rc;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400185 struct dlm_msg *msg;
David Teiglande7fd4172006-01-18 09:30:29 +0000186 int error = 0;
187
David Teiglandfaa0f262006-08-08 17:08:42 -0500188 ls->ls_recover_nodeid = nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000189
190 if (nodeid == dlm_our_nodeid()) {
Al Viro40076852008-01-25 03:01:51 -0500191 rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000192 rc->rc_result = dlm_recover_status(ls);
193 goto out;
194 }
195
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000196retry:
Alexander Aringa070a912021-05-21 15:08:41 -0400197 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400198 sizeof(struct rcom_status), &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000199 if (error)
200 goto out;
David Teigland98f176f2006-11-27 13:19:28 -0600201
David Teigland757a4272011-10-20 13:26:28 -0500202 set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags);
203
David Teigland98f176f2006-11-27 13:19:28 -0600204 allow_sync_reply(ls, &rc->rc_id);
Alexander Aring9f8f9c72020-11-02 20:04:17 -0500205 memset(ls->ls_recover_buf, 0, LOWCOMMS_MAX_TX_BUFFER_LEN);
David Teiglande7fd4172006-01-18 09:30:29 +0000206
Alexander Aring8f2dc782021-05-21 15:08:42 -0400207 send_rcom_stateless(ls, msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000208
209 error = dlm_wait_function(ls, &rcom_response);
David Teigland98f176f2006-11-27 13:19:28 -0600210 disallow_sync_reply(ls);
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000211 if (error == -ETIMEDOUT)
212 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000213 if (error)
214 goto out;
215
Al Viro40076852008-01-25 03:01:51 -0500216 rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000217
218 if (rc->rc_result == -ESRCH) {
219 /* we pretend the remote lockspace exists with 0 status */
220 log_debug(ls, "remote node %d not ready", nodeid);
221 rc->rc_result = 0;
David Teigland757a4272011-10-20 13:26:28 -0500222 error = 0;
223 } else {
224 error = check_rcom_config(ls, rc, nodeid);
225 }
226
David Teiglande7fd4172006-01-18 09:30:29 +0000227 /* the caller looks at rc_result for the remote recovery status */
228 out:
229 return error;
230}
231
232static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
233{
234 struct dlm_rcom *rc;
David Teigland757a4272011-10-20 13:26:28 -0500235 struct rcom_status *rs;
236 uint32_t status;
237 int nodeid = rc_in->rc_header.h_nodeid;
238 int len = sizeof(struct rcom_config);
Alexander Aring8f2dc782021-05-21 15:08:42 -0400239 struct dlm_msg *msg;
David Teigland757a4272011-10-20 13:26:28 -0500240 int num_slots = 0;
241 int error;
David Teiglande7fd4172006-01-18 09:30:29 +0000242
David Teigland757a4272011-10-20 13:26:28 -0500243 if (!dlm_slots_version(&rc_in->rc_header)) {
244 status = dlm_recover_status(ls);
245 goto do_create;
246 }
247
248 rs = (struct rcom_status *)rc_in->rc_buf;
249
Neale Fergusonc07127b2014-10-14 15:10:48 -0500250 if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) {
David Teigland757a4272011-10-20 13:26:28 -0500251 status = dlm_recover_status(ls);
252 goto do_create;
253 }
254
255 spin_lock(&ls->ls_recover_lock);
256 status = ls->ls_recover_status;
257 num_slots = ls->ls_num_slots;
258 spin_unlock(&ls->ls_recover_lock);
259 len += num_slots * sizeof(struct rcom_slot);
260
261 do_create:
Alexander Aringa070a912021-05-21 15:08:41 -0400262 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS_REPLY,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400263 len, &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000264 if (error)
265 return;
David Teigland757a4272011-10-20 13:26:28 -0500266
David Teigland4a99c3d2006-08-09 11:20:15 -0500267 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600268 rc->rc_seq_reply = rc_in->rc_seq;
David Teigland757a4272011-10-20 13:26:28 -0500269 rc->rc_result = status;
David Teiglande7fd4172006-01-18 09:30:29 +0000270
David Teigland757a4272011-10-20 13:26:28 -0500271 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots);
272
273 if (!num_slots)
274 goto do_send;
275
276 spin_lock(&ls->ls_recover_lock);
277 if (ls->ls_num_slots != num_slots) {
278 spin_unlock(&ls->ls_recover_lock);
279 log_debug(ls, "receive_rcom_status num_slots %d to %d",
280 num_slots, ls->ls_num_slots);
281 rc->rc_result = 0;
282 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0);
283 goto do_send;
284 }
285
286 dlm_slots_copy_out(ls, rc);
287 spin_unlock(&ls->ls_recover_lock);
288
289 do_send:
Alexander Aring8f2dc782021-05-21 15:08:42 -0400290 send_rcom_stateless(ls, msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000291}
292
David Teigland4a99c3d2006-08-09 11:20:15 -0500293static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
David Teiglande7fd4172006-01-18 09:30:29 +0000294{
David Teigland98f176f2006-11-27 13:19:28 -0600295 spin_lock(&ls->ls_rcom_spin);
296 if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
297 rc_in->rc_id != ls->ls_rcom_seq) {
298 log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
299 rc_in->rc_type, rc_in->rc_header.h_nodeid,
Ryusuke Konishi57adf7e2006-11-29 09:33:48 -0500300 (unsigned long long)rc_in->rc_id,
301 (unsigned long long)ls->ls_rcom_seq);
David Teigland98f176f2006-11-27 13:19:28 -0600302 goto out;
David Teigland4a99c3d2006-08-09 11:20:15 -0500303 }
David Teiglande7fd4172006-01-18 09:30:29 +0000304 memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
305 set_bit(LSFL_RCOM_READY, &ls->ls_flags);
David Teigland98f176f2006-11-27 13:19:28 -0600306 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
David Teiglande7fd4172006-01-18 09:30:29 +0000307 wake_up(&ls->ls_wait_general);
David Teigland98f176f2006-11-27 13:19:28 -0600308 out:
309 spin_unlock(&ls->ls_rcom_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000310}
311
312int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
313{
314 struct dlm_rcom *rc;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400315 struct dlm_msg *msg;
Al Viro40076852008-01-25 03:01:51 -0500316 int error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000317
David Teiglandfaa0f262006-08-08 17:08:42 -0500318 ls->ls_recover_nodeid = nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000319
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000320retry:
Alexander Aringa070a912021-05-21 15:08:41 -0400321 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES, last_len,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400322 &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000323 if (error)
324 goto out;
325 memcpy(rc->rc_buf, last_name, last_len);
David Teigland98f176f2006-11-27 13:19:28 -0600326
327 allow_sync_reply(ls, &rc->rc_id);
Alexander Aring9f8f9c72020-11-02 20:04:17 -0500328 memset(ls->ls_recover_buf, 0, LOWCOMMS_MAX_TX_BUFFER_LEN);
David Teiglande7fd4172006-01-18 09:30:29 +0000329
Alexander Aring8f2dc782021-05-21 15:08:42 -0400330 send_rcom_stateless(ls, msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000331
332 error = dlm_wait_function(ls, &rcom_response);
David Teigland98f176f2006-11-27 13:19:28 -0600333 disallow_sync_reply(ls);
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000334 if (error == -ETIMEDOUT)
335 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000336 out:
337 return error;
338}
339
340static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
341{
342 struct dlm_rcom *rc;
David Teigland38aa8b02006-12-13 10:37:16 -0600343 int error, inlen, outlen, nodeid;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400344 struct dlm_msg *msg;
David Teiglande7fd4172006-01-18 09:30:29 +0000345
346 nodeid = rc_in->rc_header.h_nodeid;
347 inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
Alexander Aring9f8f9c72020-11-02 20:04:17 -0500348 outlen = LOWCOMMS_MAX_TX_BUFFER_LEN - sizeof(struct dlm_rcom);
David Teiglande7fd4172006-01-18 09:30:29 +0000349
Alexander Aringa070a912021-05-21 15:08:41 -0400350 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400351 &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000352 if (error)
353 return;
David Teigland4a99c3d2006-08-09 11:20:15 -0500354 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600355 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000356
357 dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
358 nodeid);
Alexander Aring8f2dc782021-05-21 15:08:42 -0400359 send_rcom_stateless(ls, msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000360}
361
David Teiglande7fd4172006-01-18 09:30:29 +0000362int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
363{
364 struct dlm_rcom *rc;
365 struct dlm_mhandle *mh;
366 struct dlm_ls *ls = r->res_ls;
367 int error;
368
369 error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
370 &rc, &mh);
371 if (error)
372 goto out;
373 memcpy(rc->rc_buf, r->res_name, r->res_length);
David Teigland1d7c4842012-05-15 16:07:49 -0500374 rc->rc_id = (unsigned long) r->res_id;
David Teiglande7fd4172006-01-18 09:30:29 +0000375
376 send_rcom(ls, mh, rc);
377 out:
378 return error;
379}
380
381static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
382{
383 struct dlm_rcom *rc;
384 struct dlm_mhandle *mh;
385 int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
386 int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
387
388 error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
389 if (error)
390 return;
391
David Teigland9250e522017-10-09 09:29:31 -0500392 /* Old code would send this special id to trigger a debug dump. */
David Teiglandc04fecb2012-05-10 10:18:07 -0500393 if (rc_in->rc_id == 0xFFFFFFFF) {
394 log_error(ls, "receive_rcom_lookup dump from %d", nodeid);
395 dlm_dump_rsb_name(ls, rc_in->rc_buf, len);
396 return;
397 }
398
399 error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len,
400 DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL);
David Teiglande7fd4172006-01-18 09:30:29 +0000401 if (error)
402 ret_nodeid = error;
403 rc->rc_result = ret_nodeid;
404 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600405 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000406
407 send_rcom(ls, mh, rc);
408}
409
410static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
411{
412 dlm_recover_master_reply(ls, rc_in);
413}
414
415static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
416 struct rcom_lock *rl)
417{
418 memset(rl, 0, sizeof(*rl));
419
Al Viro163a1852008-01-25 02:08:26 -0500420 rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
421 rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
422 rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
423 rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
424 rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
David Teiglande7fd4172006-01-18 09:30:29 +0000425 rl->rl_rqmode = lkb->lkb_rqmode;
426 rl->rl_grmode = lkb->lkb_grmode;
427 rl->rl_status = lkb->lkb_status;
Al Viro163a1852008-01-25 02:08:26 -0500428 rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000429
David Teiglande5dae542008-02-06 00:35:45 -0600430 if (lkb->lkb_bastfn)
David Teigland8304d6f2011-02-21 14:58:21 -0600431 rl->rl_asts |= DLM_CB_BAST;
David Teiglande5dae542008-02-06 00:35:45 -0600432 if (lkb->lkb_astfn)
David Teigland8304d6f2011-02-21 14:58:21 -0600433 rl->rl_asts |= DLM_CB_CAST;
David Teiglande7fd4172006-01-18 09:30:29 +0000434
Al Viro163a1852008-01-25 02:08:26 -0500435 rl->rl_namelen = cpu_to_le16(r->res_length);
David Teiglande7fd4172006-01-18 09:30:29 +0000436 memcpy(rl->rl_name, r->res_name, r->res_length);
437
438 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
439 If so, receive_rcom_lock_args() won't take this copy. */
440
441 if (lkb->lkb_lvbptr)
442 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
443}
444
445int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
446{
447 struct dlm_ls *ls = r->res_ls;
448 struct dlm_rcom *rc;
449 struct dlm_mhandle *mh;
450 struct rcom_lock *rl;
451 int error, len = sizeof(struct rcom_lock);
452
453 if (lkb->lkb_lvbptr)
454 len += ls->ls_lvblen;
455
456 error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
457 if (error)
458 goto out;
459
460 rl = (struct rcom_lock *) rc->rc_buf;
461 pack_rcom_lock(r, lkb, rl);
462 rc->rc_id = (unsigned long) r;
463
464 send_rcom(ls, mh, rc);
465 out:
466 return error;
467}
468
Al Viroae773d02008-01-25 19:55:09 -0500469/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +0000470static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
471{
472 struct dlm_rcom *rc;
473 struct dlm_mhandle *mh;
474 int error, nodeid = rc_in->rc_header.h_nodeid;
475
476 dlm_recover_master_copy(ls, rc_in);
477
478 error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
479 sizeof(struct rcom_lock), &rc, &mh);
480 if (error)
481 return;
482
483 /* We send back the same rcom_lock struct we received, but
484 dlm_recover_master_copy() has filled in rl_remid and rl_result */
485
486 memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
487 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600488 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000489
490 send_rcom(ls, mh, rc);
491}
492
David Teiglandc36258b2007-09-27 15:53:38 -0500493/* If the lockspace doesn't exist then still send a status message
494 back; it's possible that it just doesn't have its global_id yet. */
495
496int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
David Teiglande7fd4172006-01-18 09:30:29 +0000497{
498 struct dlm_rcom *rc;
David Teigland1babdb42006-11-27 13:18:41 -0600499 struct rcom_config *rf;
David Teiglande7fd4172006-01-18 09:30:29 +0000500 struct dlm_mhandle *mh;
501 char *mb;
David Teigland1babdb42006-11-27 13:18:41 -0600502 int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
David Teiglande7fd4172006-01-18 09:30:29 +0000503
Alexander Aringa070a912021-05-21 15:08:41 -0400504 mh = dlm_midcomms_get_mhandle(nodeid, mb_len, GFP_NOFS, &mb);
David Teiglande7fd4172006-01-18 09:30:29 +0000505 if (!mh)
506 return -ENOBUFS;
David Teiglande7fd4172006-01-18 09:30:29 +0000507
508 rc = (struct dlm_rcom *) mb;
509
510 rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
511 rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
512 rc->rc_header.h_nodeid = dlm_our_nodeid();
513 rc->rc_header.h_length = mb_len;
514 rc->rc_header.h_cmd = DLM_RCOM;
515
516 rc->rc_type = DLM_RCOM_STATUS_REPLY;
David Teiglandf5888752006-08-23 12:50:54 -0500517 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600518 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000519 rc->rc_result = -ESRCH;
520
David Teigland1babdb42006-11-27 13:18:41 -0600521 rf = (struct rcom_config *) rc->rc_buf;
Al Viro93ff2972008-01-25 02:34:00 -0500522 rf->rf_lvblen = cpu_to_le32(~0U);
David Teigland1babdb42006-11-27 13:18:41 -0600523
David Teiglande7fd4172006-01-18 09:30:29 +0000524 dlm_rcom_out(rc);
Alexander Aringa070a912021-05-21 15:08:41 -0400525 dlm_midcomms_commit_mhandle(mh);
David Teiglande7fd4172006-01-18 09:30:29 +0000526
527 return 0;
528}
529
David Teiglandc04fecb2012-05-10 10:18:07 -0500530/*
531 * Ignore messages for stage Y before we set
532 * recover_status bit for stage X:
533 *
534 * recover_status = 0
535 *
536 * dlm_recover_members()
537 * - send nothing
538 * - recv nothing
539 * - ignore NAMES, NAMES_REPLY
540 * - ignore LOOKUP, LOOKUP_REPLY
541 * - ignore LOCK, LOCK_REPLY
542 *
543 * recover_status |= NODES
544 *
545 * dlm_recover_members_wait()
546 *
547 * dlm_recover_directory()
548 * - send NAMES
549 * - recv NAMES_REPLY
550 * - ignore LOOKUP, LOOKUP_REPLY
551 * - ignore LOCK, LOCK_REPLY
552 *
553 * recover_status |= DIR
554 *
555 * dlm_recover_directory_wait()
556 *
557 * dlm_recover_masters()
558 * - send LOOKUP
559 * - recv LOOKUP_REPLY
560 *
561 * dlm_recover_locks()
562 * - send LOCKS
563 * - recv LOCKS_REPLY
564 *
565 * recover_status |= LOCKS
566 *
567 * dlm_recover_locks_wait()
568 *
569 * recover_status |= DONE
570 */
571
David Teiglandc36258b2007-09-27 15:53:38 -0500572/* Called by dlm_recv; corresponds to dlm_receive_message() but special
David Teiglande7fd4172006-01-18 09:30:29 +0000573 recovery-only comms are sent through here. */
574
David Teiglandc36258b2007-09-27 15:53:38 -0500575void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000576{
Al Viroae773d02008-01-25 19:55:09 -0500577 int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
David Teiglandc04fecb2012-05-10 10:18:07 -0500578 int stop, reply = 0, names = 0, lookup = 0, lock = 0;
David Teigland48756472012-04-26 15:54:29 -0500579 uint32_t status;
David Teiglandd6e24782012-04-23 13:58:42 -0500580 uint64_t seq;
Al Viroae773d02008-01-25 19:55:09 -0500581
David Teiglandd6e24782012-04-23 13:58:42 -0500582 switch (rc->rc_type) {
David Teiglandc04fecb2012-05-10 10:18:07 -0500583 case DLM_RCOM_STATUS_REPLY:
584 reply = 1;
585 break;
586 case DLM_RCOM_NAMES:
587 names = 1;
588 break;
589 case DLM_RCOM_NAMES_REPLY:
590 names = 1;
591 reply = 1;
592 break;
593 case DLM_RCOM_LOOKUP:
594 lookup = 1;
595 break;
596 case DLM_RCOM_LOOKUP_REPLY:
597 lookup = 1;
598 reply = 1;
599 break;
David Teigland48756472012-04-26 15:54:29 -0500600 case DLM_RCOM_LOCK:
601 lock = 1;
602 break;
603 case DLM_RCOM_LOCK_REPLY:
604 lock = 1;
605 reply = 1;
606 break;
Wu Bo90db4f82020-04-22 14:59:27 +0800607 }
David Teiglandd6e24782012-04-23 13:58:42 -0500608
609 spin_lock(&ls->ls_recover_lock);
David Teigland48756472012-04-26 15:54:29 -0500610 status = ls->ls_recover_status;
David Teigland475f2302012-08-02 11:08:21 -0500611 stop = test_bit(LSFL_RECOVER_STOP, &ls->ls_flags);
David Teiglandd6e24782012-04-23 13:58:42 -0500612 seq = ls->ls_recover_seq;
613 spin_unlock(&ls->ls_recover_lock);
614
David Teiglandc04fecb2012-05-10 10:18:07 -0500615 if (stop && (rc->rc_type != DLM_RCOM_STATUS))
616 goto ignore;
617
618 if (reply && (rc->rc_seq_reply != seq))
619 goto ignore;
620
621 if (!(status & DLM_RS_NODES) && (names || lookup || lock))
622 goto ignore;
623
624 if (!(status & DLM_RS_DIR) && (lookup || lock))
625 goto ignore;
David Teiglande7fd4172006-01-18 09:30:29 +0000626
David Teiglande7fd4172006-01-18 09:30:29 +0000627 switch (rc->rc_type) {
628 case DLM_RCOM_STATUS:
629 receive_rcom_status(ls, rc);
630 break;
631
632 case DLM_RCOM_NAMES:
633 receive_rcom_names(ls, rc);
634 break;
635
636 case DLM_RCOM_LOOKUP:
637 receive_rcom_lookup(ls, rc);
638 break;
639
640 case DLM_RCOM_LOCK:
Al Viroae773d02008-01-25 19:55:09 -0500641 if (rc->rc_header.h_length < lock_size)
642 goto Eshort;
David Teiglande7fd4172006-01-18 09:30:29 +0000643 receive_rcom_lock(ls, rc);
644 break;
645
646 case DLM_RCOM_STATUS_REPLY:
David Teiglanddbcfc342008-01-29 14:52:10 -0600647 receive_sync_reply(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000648 break;
649
650 case DLM_RCOM_NAMES_REPLY:
David Teiglanddbcfc342008-01-29 14:52:10 -0600651 receive_sync_reply(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000652 break;
653
654 case DLM_RCOM_LOOKUP_REPLY:
655 receive_rcom_lookup_reply(ls, rc);
656 break;
657
658 case DLM_RCOM_LOCK_REPLY:
Al Viroae773d02008-01-25 19:55:09 -0500659 if (rc->rc_header.h_length < lock_size)
660 goto Eshort;
David Teiglanddbcfc342008-01-29 14:52:10 -0600661 dlm_recover_process_copy(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000662 break;
663
664 default:
David Teiglanddbcfc342008-01-29 14:52:10 -0600665 log_error(ls, "receive_rcom bad type %d", rc->rc_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000666 }
David Teiglandc04fecb2012-05-10 10:18:07 -0500667 return;
668
669ignore:
670 log_limit(ls, "dlm_receive_rcom ignore msg %d "
671 "from %d %llu %llu recover seq %llu sts %x gen %u",
672 rc->rc_type,
673 nodeid,
674 (unsigned long long)rc->rc_seq,
675 (unsigned long long)rc->rc_seq_reply,
676 (unsigned long long)seq,
677 status, ls->ls_generation);
David Teiglandc36258b2007-09-27 15:53:38 -0500678 return;
Al Viroae773d02008-01-25 19:55:09 -0500679Eshort:
David Teiglandc04fecb2012-05-10 10:18:07 -0500680 log_error(ls, "recovery message %d from %d is too short",
681 rc->rc_type, nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000682}
683