blob: 5821b777a1a74da8eb59b8bab65841bb79828dad [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);
Alexander Aring8e2e4082021-05-21 15:08:45 -040038 rc->rc_header.u.h_lockspace = ls->ls_global_id;
David Teiglande7fd4172006-01-18 09:30:29 +000039 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
Alexander Aring88aa0232021-07-16 16:22:38 -040092static void send_rcom(struct dlm_mhandle *mh, struct dlm_rcom *rc)
Alexander Aringa070a912021-05-21 15:08:41 -040093{
94 dlm_rcom_out(rc);
Alexander Aringa070a912021-05-21 15:08:41 -040095 dlm_midcomms_commit_mhandle(mh);
96}
97
Alexander Aring88aa0232021-07-16 16:22:38 -040098static void send_rcom_stateless(struct dlm_msg *msg, struct dlm_rcom *rc)
Alexander Aringa070a912021-05-21 15:08:41 -040099{
Alexander Aring88aa0232021-07-16 16:22:38 -0400100 dlm_rcom_out(rc);
Alexander Aring8f2dc782021-05-21 15:08:42 -0400101 dlm_lowcomms_commit_msg(msg);
102 dlm_lowcomms_put_msg(msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000103}
104
David Teigland757a4272011-10-20 13:26:28 -0500105static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs,
106 uint32_t flags)
107{
108 rs->rs_flags = cpu_to_le32(flags);
109}
110
David Teiglande7fd4172006-01-18 09:30:29 +0000111/* When replying to a status request, a node also sends back its
112 configuration values. The requesting node then checks that the remote
113 node is configured the same way as itself. */
114
David Teigland757a4272011-10-20 13:26:28 -0500115static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf,
116 uint32_t num_slots)
David Teiglande7fd4172006-01-18 09:30:29 +0000117{
Al Viro93ff2972008-01-25 02:34:00 -0500118 rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
119 rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
David Teigland757a4272011-10-20 13:26:28 -0500120
121 rf->rf_our_slot = cpu_to_le16(ls->ls_slot);
122 rf->rf_num_slots = cpu_to_le16(num_slots);
123 rf->rf_generation = cpu_to_le32(ls->ls_generation);
David Teiglande7fd4172006-01-18 09:30:29 +0000124}
125
David Teigland757a4272011-10-20 13:26:28 -0500126static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000127{
David Teigland9e971b72006-12-13 10:37:55 -0600128 struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
129
130 if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
131 log_error(ls, "version mismatch: %x nodeid %d: %x",
132 DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
133 rc->rc_header.h_version);
David Teigland8b0e7b22007-05-18 09:03:35 -0500134 return -EPROTO;
David Teigland9e971b72006-12-13 10:37:55 -0600135 }
136
Al Viro93ff2972008-01-25 02:34:00 -0500137 if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
138 le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
David Teiglande7fd4172006-01-18 09:30:29 +0000139 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
Al Viro93ff2972008-01-25 02:34:00 -0500140 ls->ls_lvblen, ls->ls_exflags, nodeid,
141 le32_to_cpu(rf->rf_lvblen),
142 le32_to_cpu(rf->rf_lsflags));
David Teigland8b0e7b22007-05-18 09:03:35 -0500143 return -EPROTO;
David Teiglande7fd4172006-01-18 09:30:29 +0000144 }
145 return 0;
146}
147
David Teigland98f176f2006-11-27 13:19:28 -0600148static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
149{
150 spin_lock(&ls->ls_rcom_spin);
151 *new_seq = ++ls->ls_rcom_seq;
152 set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
153 spin_unlock(&ls->ls_rcom_spin);
154}
155
156static void disallow_sync_reply(struct dlm_ls *ls)
157{
158 spin_lock(&ls->ls_rcom_spin);
159 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
160 clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
161 spin_unlock(&ls->ls_rcom_spin);
162}
163
David Teigland757a4272011-10-20 13:26:28 -0500164/*
165 * low nodeid gathers one slot value at a time from each node.
166 * it sets need_slots=0, and saves rf_our_slot returned from each
167 * rcom_config.
168 *
169 * other nodes gather all slot values at once from the low nodeid.
170 * they set need_slots=1, and ignore the rf_our_slot returned from each
171 * rcom_config. they use the rf_num_slots returned from the low
172 * node's rcom_config.
173 */
174
175int dlm_rcom_status(struct dlm_ls *ls, int nodeid, uint32_t status_flags)
David Teiglande7fd4172006-01-18 09:30:29 +0000176{
177 struct dlm_rcom *rc;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400178 struct dlm_msg *msg;
David Teiglande7fd4172006-01-18 09:30:29 +0000179 int error = 0;
180
David Teiglandfaa0f262006-08-08 17:08:42 -0500181 ls->ls_recover_nodeid = nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000182
183 if (nodeid == dlm_our_nodeid()) {
Al Viro40076852008-01-25 03:01:51 -0500184 rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000185 rc->rc_result = dlm_recover_status(ls);
186 goto out;
187 }
188
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000189retry:
Alexander Aringa070a912021-05-21 15:08:41 -0400190 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400191 sizeof(struct rcom_status), &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000192 if (error)
193 goto out;
David Teigland98f176f2006-11-27 13:19:28 -0600194
David Teigland757a4272011-10-20 13:26:28 -0500195 set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags);
196
David Teigland98f176f2006-11-27 13:19:28 -0600197 allow_sync_reply(ls, &rc->rc_id);
Alexander Aringd10a0b82021-06-02 09:45:20 -0400198 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
David Teiglande7fd4172006-01-18 09:30:29 +0000199
Alexander Aring88aa0232021-07-16 16:22:38 -0400200 send_rcom_stateless(msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000201
202 error = dlm_wait_function(ls, &rcom_response);
David Teigland98f176f2006-11-27 13:19:28 -0600203 disallow_sync_reply(ls);
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000204 if (error == -ETIMEDOUT)
205 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000206 if (error)
207 goto out;
208
Al Viro40076852008-01-25 03:01:51 -0500209 rc = ls->ls_recover_buf;
David Teiglande7fd4172006-01-18 09:30:29 +0000210
211 if (rc->rc_result == -ESRCH) {
212 /* we pretend the remote lockspace exists with 0 status */
213 log_debug(ls, "remote node %d not ready", nodeid);
214 rc->rc_result = 0;
David Teigland757a4272011-10-20 13:26:28 -0500215 error = 0;
216 } else {
217 error = check_rcom_config(ls, rc, nodeid);
218 }
219
David Teiglande7fd4172006-01-18 09:30:29 +0000220 /* the caller looks at rc_result for the remote recovery status */
221 out:
222 return error;
223}
224
225static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
226{
227 struct dlm_rcom *rc;
David Teigland757a4272011-10-20 13:26:28 -0500228 struct rcom_status *rs;
229 uint32_t status;
230 int nodeid = rc_in->rc_header.h_nodeid;
231 int len = sizeof(struct rcom_config);
Alexander Aring8f2dc782021-05-21 15:08:42 -0400232 struct dlm_msg *msg;
David Teigland757a4272011-10-20 13:26:28 -0500233 int num_slots = 0;
234 int error;
David Teiglande7fd4172006-01-18 09:30:29 +0000235
David Teigland757a4272011-10-20 13:26:28 -0500236 if (!dlm_slots_version(&rc_in->rc_header)) {
237 status = dlm_recover_status(ls);
238 goto do_create;
239 }
240
241 rs = (struct rcom_status *)rc_in->rc_buf;
242
Neale Fergusonc07127b2014-10-14 15:10:48 -0500243 if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) {
David Teigland757a4272011-10-20 13:26:28 -0500244 status = dlm_recover_status(ls);
245 goto do_create;
246 }
247
248 spin_lock(&ls->ls_recover_lock);
249 status = ls->ls_recover_status;
250 num_slots = ls->ls_num_slots;
251 spin_unlock(&ls->ls_recover_lock);
252 len += num_slots * sizeof(struct rcom_slot);
253
254 do_create:
Alexander Aringa070a912021-05-21 15:08:41 -0400255 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS_REPLY,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400256 len, &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000257 if (error)
258 return;
David Teigland757a4272011-10-20 13:26:28 -0500259
David Teigland4a99c3d2006-08-09 11:20:15 -0500260 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600261 rc->rc_seq_reply = rc_in->rc_seq;
David Teigland757a4272011-10-20 13:26:28 -0500262 rc->rc_result = status;
David Teiglande7fd4172006-01-18 09:30:29 +0000263
David Teigland757a4272011-10-20 13:26:28 -0500264 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots);
265
266 if (!num_slots)
267 goto do_send;
268
269 spin_lock(&ls->ls_recover_lock);
270 if (ls->ls_num_slots != num_slots) {
271 spin_unlock(&ls->ls_recover_lock);
272 log_debug(ls, "receive_rcom_status num_slots %d to %d",
273 num_slots, ls->ls_num_slots);
274 rc->rc_result = 0;
275 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0);
276 goto do_send;
277 }
278
279 dlm_slots_copy_out(ls, rc);
280 spin_unlock(&ls->ls_recover_lock);
281
282 do_send:
Alexander Aring88aa0232021-07-16 16:22:38 -0400283 send_rcom_stateless(msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000284}
285
David Teigland4a99c3d2006-08-09 11:20:15 -0500286static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
David Teiglande7fd4172006-01-18 09:30:29 +0000287{
David Teigland98f176f2006-11-27 13:19:28 -0600288 spin_lock(&ls->ls_rcom_spin);
289 if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
290 rc_in->rc_id != ls->ls_rcom_seq) {
291 log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
292 rc_in->rc_type, rc_in->rc_header.h_nodeid,
Ryusuke Konishi57adf7e2006-11-29 09:33:48 -0500293 (unsigned long long)rc_in->rc_id,
294 (unsigned long long)ls->ls_rcom_seq);
David Teigland98f176f2006-11-27 13:19:28 -0600295 goto out;
David Teigland4a99c3d2006-08-09 11:20:15 -0500296 }
David Teiglande7fd4172006-01-18 09:30:29 +0000297 memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
298 set_bit(LSFL_RCOM_READY, &ls->ls_flags);
David Teigland98f176f2006-11-27 13:19:28 -0600299 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
David Teiglande7fd4172006-01-18 09:30:29 +0000300 wake_up(&ls->ls_wait_general);
David Teigland98f176f2006-11-27 13:19:28 -0600301 out:
302 spin_unlock(&ls->ls_rcom_spin);
David Teiglande7fd4172006-01-18 09:30:29 +0000303}
304
305int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
306{
307 struct dlm_rcom *rc;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400308 struct dlm_msg *msg;
Al Viro40076852008-01-25 03:01:51 -0500309 int error = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000310
David Teiglandfaa0f262006-08-08 17:08:42 -0500311 ls->ls_recover_nodeid = nodeid;
David Teiglande7fd4172006-01-18 09:30:29 +0000312
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000313retry:
Alexander Aringa070a912021-05-21 15:08:41 -0400314 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES, last_len,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400315 &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000316 if (error)
317 goto out;
318 memcpy(rc->rc_buf, last_name, last_len);
David Teigland98f176f2006-11-27 13:19:28 -0600319
320 allow_sync_reply(ls, &rc->rc_id);
Alexander Aringd10a0b82021-06-02 09:45:20 -0400321 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
David Teiglande7fd4172006-01-18 09:30:29 +0000322
Alexander Aring88aa0232021-07-16 16:22:38 -0400323 send_rcom_stateless(msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000324
325 error = dlm_wait_function(ls, &rcom_response);
David Teigland98f176f2006-11-27 13:19:28 -0600326 disallow_sync_reply(ls);
tsutomu.owa@toshiba.co.jp59661212017-09-12 08:56:08 +0000327 if (error == -ETIMEDOUT)
328 goto retry;
David Teiglande7fd4172006-01-18 09:30:29 +0000329 out:
330 return error;
331}
332
333static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
334{
335 struct dlm_rcom *rc;
David Teigland38aa8b02006-12-13 10:37:16 -0600336 int error, inlen, outlen, nodeid;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400337 struct dlm_msg *msg;
David Teiglande7fd4172006-01-18 09:30:29 +0000338
339 nodeid = rc_in->rc_header.h_nodeid;
340 inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
Alexander Aringd10a0b82021-06-02 09:45:20 -0400341 outlen = DLM_MAX_APP_BUFSIZE - sizeof(struct dlm_rcom);
David Teiglande7fd4172006-01-18 09:30:29 +0000342
Alexander Aringa070a912021-05-21 15:08:41 -0400343 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen,
Alexander Aring8f2dc782021-05-21 15:08:42 -0400344 &rc, &msg);
David Teiglande7fd4172006-01-18 09:30:29 +0000345 if (error)
346 return;
David Teigland4a99c3d2006-08-09 11:20:15 -0500347 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600348 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000349
350 dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
351 nodeid);
Alexander Aring88aa0232021-07-16 16:22:38 -0400352 send_rcom_stateless(msg, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000353}
354
David Teiglande7fd4172006-01-18 09:30:29 +0000355int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
356{
357 struct dlm_rcom *rc;
358 struct dlm_mhandle *mh;
359 struct dlm_ls *ls = r->res_ls;
360 int error;
361
362 error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
363 &rc, &mh);
364 if (error)
365 goto out;
366 memcpy(rc->rc_buf, r->res_name, r->res_length);
David Teigland1d7c4842012-05-15 16:07:49 -0500367 rc->rc_id = (unsigned long) r->res_id;
David Teiglande7fd4172006-01-18 09:30:29 +0000368
Alexander Aring88aa0232021-07-16 16:22:38 -0400369 send_rcom(mh, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000370 out:
371 return error;
372}
373
374static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
375{
376 struct dlm_rcom *rc;
377 struct dlm_mhandle *mh;
378 int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
379 int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
380
David Teigland9250e522017-10-09 09:29:31 -0500381 /* Old code would send this special id to trigger a debug dump. */
David Teiglandc04fecb2012-05-10 10:18:07 -0500382 if (rc_in->rc_id == 0xFFFFFFFF) {
383 log_error(ls, "receive_rcom_lookup dump from %d", nodeid);
384 dlm_dump_rsb_name(ls, rc_in->rc_buf, len);
385 return;
386 }
387
Colin Ian Kingf6089982021-05-26 15:46:05 +0100388 error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
389 if (error)
390 return;
391
David Teiglandc04fecb2012-05-10 10:18:07 -0500392 error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len,
393 DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL);
David Teiglande7fd4172006-01-18 09:30:29 +0000394 if (error)
395 ret_nodeid = error;
396 rc->rc_result = ret_nodeid;
397 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600398 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000399
Alexander Aring88aa0232021-07-16 16:22:38 -0400400 send_rcom(mh, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000401}
402
403static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
404{
405 dlm_recover_master_reply(ls, rc_in);
406}
407
408static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
409 struct rcom_lock *rl)
410{
411 memset(rl, 0, sizeof(*rl));
412
Al Viro163a1852008-01-25 02:08:26 -0500413 rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
414 rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
415 rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
416 rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
417 rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
David Teiglande7fd4172006-01-18 09:30:29 +0000418 rl->rl_rqmode = lkb->lkb_rqmode;
419 rl->rl_grmode = lkb->lkb_grmode;
420 rl->rl_status = lkb->lkb_status;
Al Viro163a1852008-01-25 02:08:26 -0500421 rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000422
David Teiglande5dae542008-02-06 00:35:45 -0600423 if (lkb->lkb_bastfn)
David Teigland8304d6f2011-02-21 14:58:21 -0600424 rl->rl_asts |= DLM_CB_BAST;
David Teiglande5dae542008-02-06 00:35:45 -0600425 if (lkb->lkb_astfn)
David Teigland8304d6f2011-02-21 14:58:21 -0600426 rl->rl_asts |= DLM_CB_CAST;
David Teiglande7fd4172006-01-18 09:30:29 +0000427
Al Viro163a1852008-01-25 02:08:26 -0500428 rl->rl_namelen = cpu_to_le16(r->res_length);
David Teiglande7fd4172006-01-18 09:30:29 +0000429 memcpy(rl->rl_name, r->res_name, r->res_length);
430
431 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
432 If so, receive_rcom_lock_args() won't take this copy. */
433
434 if (lkb->lkb_lvbptr)
435 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
436}
437
438int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
439{
440 struct dlm_ls *ls = r->res_ls;
441 struct dlm_rcom *rc;
442 struct dlm_mhandle *mh;
443 struct rcom_lock *rl;
444 int error, len = sizeof(struct rcom_lock);
445
446 if (lkb->lkb_lvbptr)
447 len += ls->ls_lvblen;
448
449 error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
450 if (error)
451 goto out;
452
453 rl = (struct rcom_lock *) rc->rc_buf;
454 pack_rcom_lock(r, lkb, rl);
455 rc->rc_id = (unsigned long) r;
456
Alexander Aring88aa0232021-07-16 16:22:38 -0400457 send_rcom(mh, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000458 out:
459 return error;
460}
461
Al Viroae773d02008-01-25 19:55:09 -0500462/* needs at least dlm_rcom + rcom_lock */
David Teiglande7fd4172006-01-18 09:30:29 +0000463static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
464{
465 struct dlm_rcom *rc;
466 struct dlm_mhandle *mh;
467 int error, nodeid = rc_in->rc_header.h_nodeid;
468
469 dlm_recover_master_copy(ls, rc_in);
470
471 error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
472 sizeof(struct rcom_lock), &rc, &mh);
473 if (error)
474 return;
475
476 /* We send back the same rcom_lock struct we received, but
477 dlm_recover_master_copy() has filled in rl_remid and rl_result */
478
479 memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
480 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600481 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000482
Alexander Aring88aa0232021-07-16 16:22:38 -0400483 send_rcom(mh, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000484}
485
David Teiglandc36258b2007-09-27 15:53:38 -0500486/* If the lockspace doesn't exist then still send a status message
487 back; it's possible that it just doesn't have its global_id yet. */
488
489int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
David Teiglande7fd4172006-01-18 09:30:29 +0000490{
491 struct dlm_rcom *rc;
David Teigland1babdb42006-11-27 13:18:41 -0600492 struct rcom_config *rf;
David Teiglande7fd4172006-01-18 09:30:29 +0000493 struct dlm_mhandle *mh;
494 char *mb;
David Teigland1babdb42006-11-27 13:18:41 -0600495 int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
David Teiglande7fd4172006-01-18 09:30:29 +0000496
Alexander Aringa070a912021-05-21 15:08:41 -0400497 mh = dlm_midcomms_get_mhandle(nodeid, mb_len, GFP_NOFS, &mb);
David Teiglande7fd4172006-01-18 09:30:29 +0000498 if (!mh)
499 return -ENOBUFS;
David Teiglande7fd4172006-01-18 09:30:29 +0000500
501 rc = (struct dlm_rcom *) mb;
502
503 rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
Alexander Aring8e2e4082021-05-21 15:08:45 -0400504 rc->rc_header.u.h_lockspace = rc_in->rc_header.u.h_lockspace;
David Teiglande7fd4172006-01-18 09:30:29 +0000505 rc->rc_header.h_nodeid = dlm_our_nodeid();
506 rc->rc_header.h_length = mb_len;
507 rc->rc_header.h_cmd = DLM_RCOM;
508
509 rc->rc_type = DLM_RCOM_STATUS_REPLY;
David Teiglandf5888752006-08-23 12:50:54 -0500510 rc->rc_id = rc_in->rc_id;
David Teigland38aa8b02006-12-13 10:37:16 -0600511 rc->rc_seq_reply = rc_in->rc_seq;
David Teiglande7fd4172006-01-18 09:30:29 +0000512 rc->rc_result = -ESRCH;
513
David Teigland1babdb42006-11-27 13:18:41 -0600514 rf = (struct rcom_config *) rc->rc_buf;
Al Viro93ff2972008-01-25 02:34:00 -0500515 rf->rf_lvblen = cpu_to_le32(~0U);
David Teigland1babdb42006-11-27 13:18:41 -0600516
David Teiglande7fd4172006-01-18 09:30:29 +0000517 dlm_rcom_out(rc);
Alexander Aringa070a912021-05-21 15:08:41 -0400518 dlm_midcomms_commit_mhandle(mh);
David Teiglande7fd4172006-01-18 09:30:29 +0000519
520 return 0;
521}
522
David Teiglandc04fecb2012-05-10 10:18:07 -0500523/*
524 * Ignore messages for stage Y before we set
525 * recover_status bit for stage X:
526 *
527 * recover_status = 0
528 *
529 * dlm_recover_members()
530 * - send nothing
531 * - recv nothing
532 * - ignore NAMES, NAMES_REPLY
533 * - ignore LOOKUP, LOOKUP_REPLY
534 * - ignore LOCK, LOCK_REPLY
535 *
536 * recover_status |= NODES
537 *
538 * dlm_recover_members_wait()
539 *
540 * dlm_recover_directory()
541 * - send NAMES
542 * - recv NAMES_REPLY
543 * - ignore LOOKUP, LOOKUP_REPLY
544 * - ignore LOCK, LOCK_REPLY
545 *
546 * recover_status |= DIR
547 *
548 * dlm_recover_directory_wait()
549 *
550 * dlm_recover_masters()
551 * - send LOOKUP
552 * - recv LOOKUP_REPLY
553 *
554 * dlm_recover_locks()
555 * - send LOCKS
556 * - recv LOCKS_REPLY
557 *
558 * recover_status |= LOCKS
559 *
560 * dlm_recover_locks_wait()
561 *
562 * recover_status |= DONE
563 */
564
David Teiglandc36258b2007-09-27 15:53:38 -0500565/* Called by dlm_recv; corresponds to dlm_receive_message() but special
David Teiglande7fd4172006-01-18 09:30:29 +0000566 recovery-only comms are sent through here. */
567
David Teiglandc36258b2007-09-27 15:53:38 -0500568void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
David Teiglande7fd4172006-01-18 09:30:29 +0000569{
Al Viroae773d02008-01-25 19:55:09 -0500570 int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
David Teiglandc04fecb2012-05-10 10:18:07 -0500571 int stop, reply = 0, names = 0, lookup = 0, lock = 0;
David Teigland48756472012-04-26 15:54:29 -0500572 uint32_t status;
David Teiglandd6e24782012-04-23 13:58:42 -0500573 uint64_t seq;
Al Viroae773d02008-01-25 19:55:09 -0500574
David Teiglandd6e24782012-04-23 13:58:42 -0500575 switch (rc->rc_type) {
David Teiglandc04fecb2012-05-10 10:18:07 -0500576 case DLM_RCOM_STATUS_REPLY:
577 reply = 1;
578 break;
579 case DLM_RCOM_NAMES:
580 names = 1;
581 break;
582 case DLM_RCOM_NAMES_REPLY:
583 names = 1;
584 reply = 1;
585 break;
586 case DLM_RCOM_LOOKUP:
587 lookup = 1;
588 break;
589 case DLM_RCOM_LOOKUP_REPLY:
590 lookup = 1;
591 reply = 1;
592 break;
David Teigland48756472012-04-26 15:54:29 -0500593 case DLM_RCOM_LOCK:
594 lock = 1;
595 break;
596 case DLM_RCOM_LOCK_REPLY:
597 lock = 1;
598 reply = 1;
599 break;
Wu Bo90db4f82020-04-22 14:59:27 +0800600 }
David Teiglandd6e24782012-04-23 13:58:42 -0500601
602 spin_lock(&ls->ls_recover_lock);
David Teigland48756472012-04-26 15:54:29 -0500603 status = ls->ls_recover_status;
Alexander Aring3e973672021-11-02 15:17:12 -0400604 stop = dlm_recovery_stopped(ls);
David Teiglandd6e24782012-04-23 13:58:42 -0500605 seq = ls->ls_recover_seq;
606 spin_unlock(&ls->ls_recover_lock);
607
David Teiglandc04fecb2012-05-10 10:18:07 -0500608 if (stop && (rc->rc_type != DLM_RCOM_STATUS))
609 goto ignore;
610
611 if (reply && (rc->rc_seq_reply != seq))
612 goto ignore;
613
614 if (!(status & DLM_RS_NODES) && (names || lookup || lock))
615 goto ignore;
616
617 if (!(status & DLM_RS_DIR) && (lookup || lock))
618 goto ignore;
David Teiglande7fd4172006-01-18 09:30:29 +0000619
David Teiglande7fd4172006-01-18 09:30:29 +0000620 switch (rc->rc_type) {
621 case DLM_RCOM_STATUS:
622 receive_rcom_status(ls, rc);
623 break;
624
625 case DLM_RCOM_NAMES:
626 receive_rcom_names(ls, rc);
627 break;
628
629 case DLM_RCOM_LOOKUP:
630 receive_rcom_lookup(ls, rc);
631 break;
632
633 case DLM_RCOM_LOCK:
Al Viroae773d02008-01-25 19:55:09 -0500634 if (rc->rc_header.h_length < lock_size)
635 goto Eshort;
David Teiglande7fd4172006-01-18 09:30:29 +0000636 receive_rcom_lock(ls, rc);
637 break;
638
639 case DLM_RCOM_STATUS_REPLY:
David Teiglanddbcfc342008-01-29 14:52:10 -0600640 receive_sync_reply(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000641 break;
642
643 case DLM_RCOM_NAMES_REPLY:
David Teiglanddbcfc342008-01-29 14:52:10 -0600644 receive_sync_reply(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000645 break;
646
647 case DLM_RCOM_LOOKUP_REPLY:
648 receive_rcom_lookup_reply(ls, rc);
649 break;
650
651 case DLM_RCOM_LOCK_REPLY:
Al Viroae773d02008-01-25 19:55:09 -0500652 if (rc->rc_header.h_length < lock_size)
653 goto Eshort;
David Teiglanddbcfc342008-01-29 14:52:10 -0600654 dlm_recover_process_copy(ls, rc);
David Teiglande7fd4172006-01-18 09:30:29 +0000655 break;
656
657 default:
David Teiglanddbcfc342008-01-29 14:52:10 -0600658 log_error(ls, "receive_rcom bad type %d", rc->rc_type);
David Teiglande7fd4172006-01-18 09:30:29 +0000659 }
David Teiglandc04fecb2012-05-10 10:18:07 -0500660 return;
661
662ignore:
663 log_limit(ls, "dlm_receive_rcom ignore msg %d "
664 "from %d %llu %llu recover seq %llu sts %x gen %u",
665 rc->rc_type,
666 nodeid,
667 (unsigned long long)rc->rc_seq,
668 (unsigned long long)rc->rc_seq_reply,
669 (unsigned long long)seq,
670 status, ls->ls_generation);
David Teiglandc36258b2007-09-27 15:53:38 -0500671 return;
Al Viroae773d02008-01-25 19:55:09 -0500672Eshort:
David Teiglandc04fecb2012-05-10 10:18:07 -0500673 log_error(ls, "recovery message %d from %d is too short",
674 rc->rc_type, nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +0000675}
676