1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
12 #include "dlm_internal.h"
13 #include "lockspace.h"
25 static int rcom_response(struct dlm_ls *ls)
27 return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
30 static 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)
35 rc = (struct dlm_rcom *) mb;
37 rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
38 rc->rc_header.u.h_lockspace = cpu_to_le32(ls->ls_global_id);
39 rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
40 rc->rc_header.h_length = cpu_to_le16(mb_len);
41 rc->rc_header.h_cmd = DLM_RCOM;
43 rc->rc_type = cpu_to_le32(type);
45 spin_lock(&ls->ls_recover_lock);
46 rc->rc_seq = cpu_to_le64(ls->ls_recover_seq);
47 spin_unlock(&ls->ls_recover_lock);
52 static 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)
55 int mb_len = sizeof(struct dlm_rcom) + len;
56 struct dlm_mhandle *mh;
59 mh = dlm_midcomms_get_mhandle(to_nodeid, mb_len, GFP_NOFS, &mb);
61 log_print("%s to %d type %d len %d ENOBUFS",
62 __func__, to_nodeid, type, len);
66 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len);
71 static int create_rcom_stateless(struct dlm_ls *ls, int to_nodeid, int type,
72 int len, struct dlm_rcom **rc_ret,
73 struct dlm_msg **msg_ret)
75 int mb_len = sizeof(struct dlm_rcom) + len;
79 msg = dlm_lowcomms_new_msg(to_nodeid, mb_len, GFP_NOFS, &mb,
82 log_print("create_rcom to %d type %d len %d ENOBUFS",
83 to_nodeid, type, len);
87 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len);
92 static void send_rcom(struct dlm_mhandle *mh, struct dlm_rcom *rc)
94 dlm_midcomms_commit_mhandle(mh);
97 static void send_rcom_stateless(struct dlm_msg *msg, struct dlm_rcom *rc)
99 dlm_lowcomms_commit_msg(msg);
100 dlm_lowcomms_put_msg(msg);
103 static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs,
106 rs->rs_flags = cpu_to_le32(flags);
109 /* When replying to a status request, a node also sends back its
110 configuration values. The requesting node then checks that the remote
111 node is configured the same way as itself. */
113 static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf,
116 rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
117 rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
119 rf->rf_our_slot = cpu_to_le16(ls->ls_slot);
120 rf->rf_num_slots = cpu_to_le16(num_slots);
121 rf->rf_generation = cpu_to_le32(ls->ls_generation);
124 static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
126 struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
128 if ((le32_to_cpu(rc->rc_header.h_version) & 0xFFFF0000) != DLM_HEADER_MAJOR) {
129 log_error(ls, "version mismatch: %x nodeid %d: %x",
130 DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
131 le32_to_cpu(rc->rc_header.h_version));
135 if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
136 le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
137 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
138 ls->ls_lvblen, ls->ls_exflags, nodeid,
139 le32_to_cpu(rf->rf_lvblen),
140 le32_to_cpu(rf->rf_lsflags));
146 static void allow_sync_reply(struct dlm_ls *ls, __le64 *new_seq)
148 spin_lock(&ls->ls_rcom_spin);
149 *new_seq = cpu_to_le64(++ls->ls_rcom_seq);
150 set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
151 spin_unlock(&ls->ls_rcom_spin);
154 static void disallow_sync_reply(struct dlm_ls *ls)
156 spin_lock(&ls->ls_rcom_spin);
157 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
158 clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
159 spin_unlock(&ls->ls_rcom_spin);
163 * low nodeid gathers one slot value at a time from each node.
164 * it sets need_slots=0, and saves rf_our_slot returned from each
167 * other nodes gather all slot values at once from the low nodeid.
168 * they set need_slots=1, and ignore the rf_our_slot returned from each
169 * rcom_config. they use the rf_num_slots returned from the low
170 * node's rcom_config.
173 int dlm_rcom_status(struct dlm_ls *ls, int nodeid, uint32_t status_flags)
179 ls->ls_recover_nodeid = nodeid;
181 if (nodeid == dlm_our_nodeid()) {
182 rc = ls->ls_recover_buf;
183 rc->rc_result = cpu_to_le32(dlm_recover_status(ls));
188 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS,
189 sizeof(struct rcom_status), &rc, &msg);
193 set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags);
195 allow_sync_reply(ls, &rc->rc_id);
196 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
198 send_rcom_stateless(msg, rc);
200 error = dlm_wait_function(ls, &rcom_response);
201 disallow_sync_reply(ls);
202 if (error == -ETIMEDOUT)
207 rc = ls->ls_recover_buf;
209 if (rc->rc_result == cpu_to_le32(-ESRCH)) {
210 /* we pretend the remote lockspace exists with 0 status */
211 log_debug(ls, "remote node %d not ready", nodeid);
215 error = check_rcom_config(ls, rc, nodeid);
218 /* the caller looks at rc_result for the remote recovery status */
223 static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
226 struct rcom_status *rs;
228 int nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
229 int len = sizeof(struct rcom_config);
234 if (!dlm_slots_version(&rc_in->rc_header)) {
235 status = dlm_recover_status(ls);
239 rs = (struct rcom_status *)rc_in->rc_buf;
241 if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) {
242 status = dlm_recover_status(ls);
246 spin_lock(&ls->ls_recover_lock);
247 status = ls->ls_recover_status;
248 num_slots = ls->ls_num_slots;
249 spin_unlock(&ls->ls_recover_lock);
250 len += num_slots * sizeof(struct rcom_slot);
253 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS_REPLY,
258 rc->rc_id = rc_in->rc_id;
259 rc->rc_seq_reply = rc_in->rc_seq;
260 rc->rc_result = cpu_to_le32(status);
262 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots);
267 spin_lock(&ls->ls_recover_lock);
268 if (ls->ls_num_slots != num_slots) {
269 spin_unlock(&ls->ls_recover_lock);
270 log_debug(ls, "receive_rcom_status num_slots %d to %d",
271 num_slots, ls->ls_num_slots);
273 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0);
277 dlm_slots_copy_out(ls, rc);
278 spin_unlock(&ls->ls_recover_lock);
281 send_rcom_stateless(msg, rc);
284 static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
286 spin_lock(&ls->ls_rcom_spin);
287 if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
288 le64_to_cpu(rc_in->rc_id) != ls->ls_rcom_seq) {
289 log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
290 le32_to_cpu(rc_in->rc_type),
291 le32_to_cpu(rc_in->rc_header.h_nodeid),
292 (unsigned long long)le64_to_cpu(rc_in->rc_id),
293 (unsigned long long)ls->ls_rcom_seq);
296 memcpy(ls->ls_recover_buf, rc_in,
297 le16_to_cpu(rc_in->rc_header.h_length));
298 set_bit(LSFL_RCOM_READY, &ls->ls_flags);
299 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
300 wake_up(&ls->ls_wait_general);
302 spin_unlock(&ls->ls_rcom_spin);
305 int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
311 ls->ls_recover_nodeid = nodeid;
314 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES, last_len,
318 memcpy(rc->rc_buf, last_name, last_len);
320 allow_sync_reply(ls, &rc->rc_id);
321 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
323 send_rcom_stateless(msg, rc);
325 error = dlm_wait_function(ls, &rcom_response);
326 disallow_sync_reply(ls);
327 if (error == -ETIMEDOUT)
333 static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
336 int error, inlen, outlen, nodeid;
339 nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
340 inlen = le16_to_cpu(rc_in->rc_header.h_length) -
341 sizeof(struct dlm_rcom);
342 outlen = DLM_MAX_APP_BUFSIZE - sizeof(struct dlm_rcom);
344 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen,
348 rc->rc_id = rc_in->rc_id;
349 rc->rc_seq_reply = rc_in->rc_seq;
351 dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
353 send_rcom_stateless(msg, rc);
356 int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
359 struct dlm_mhandle *mh;
360 struct dlm_ls *ls = r->res_ls;
363 error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
367 memcpy(rc->rc_buf, r->res_name, r->res_length);
368 rc->rc_id = cpu_to_le64(r->res_id);
375 static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
378 struct dlm_mhandle *mh;
379 int error, ret_nodeid, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
380 int len = le16_to_cpu(rc_in->rc_header.h_length) -
381 sizeof(struct dlm_rcom);
383 /* Old code would send this special id to trigger a debug dump. */
384 if (rc_in->rc_id == cpu_to_le64(0xFFFFFFFF)) {
385 log_error(ls, "receive_rcom_lookup dump from %d", nodeid);
386 dlm_dump_rsb_name(ls, rc_in->rc_buf, len);
390 error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
394 error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len,
395 DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL);
398 rc->rc_result = cpu_to_le32(ret_nodeid);
399 rc->rc_id = rc_in->rc_id;
400 rc->rc_seq_reply = rc_in->rc_seq;
405 static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
407 dlm_recover_master_reply(ls, rc_in);
410 static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
411 struct rcom_lock *rl)
413 memset(rl, 0, sizeof(*rl));
415 rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
416 rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
417 rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
418 rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
419 rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
420 rl->rl_rqmode = lkb->lkb_rqmode;
421 rl->rl_grmode = lkb->lkb_grmode;
422 rl->rl_status = lkb->lkb_status;
423 rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
426 rl->rl_asts |= DLM_CB_BAST;
428 rl->rl_asts |= DLM_CB_CAST;
430 rl->rl_namelen = cpu_to_le16(r->res_length);
431 memcpy(rl->rl_name, r->res_name, r->res_length);
433 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
434 If so, receive_rcom_lock_args() won't take this copy. */
437 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
440 int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
442 struct dlm_ls *ls = r->res_ls;
444 struct dlm_mhandle *mh;
445 struct rcom_lock *rl;
446 int error, len = sizeof(struct rcom_lock);
449 len += ls->ls_lvblen;
451 error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
455 rl = (struct rcom_lock *) rc->rc_buf;
456 pack_rcom_lock(r, lkb, rl);
457 rc->rc_id = cpu_to_le64((uintptr_t)r);
464 /* needs at least dlm_rcom + rcom_lock */
465 static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
468 struct dlm_mhandle *mh;
469 int error, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
471 dlm_recover_master_copy(ls, rc_in);
473 error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
474 sizeof(struct rcom_lock), &rc, &mh);
478 /* We send back the same rcom_lock struct we received, but
479 dlm_recover_master_copy() has filled in rl_remid and rl_result */
481 memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
482 rc->rc_id = rc_in->rc_id;
483 rc->rc_seq_reply = rc_in->rc_seq;
488 /* If the lockspace doesn't exist then still send a status message
489 back; it's possible that it just doesn't have its global_id yet. */
491 int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
494 struct rcom_config *rf;
495 struct dlm_mhandle *mh;
497 int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
499 mh = dlm_midcomms_get_mhandle(nodeid, mb_len, GFP_NOFS, &mb);
503 rc = (struct dlm_rcom *) mb;
505 rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
506 rc->rc_header.u.h_lockspace = rc_in->rc_header.u.h_lockspace;
507 rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
508 rc->rc_header.h_length = cpu_to_le16(mb_len);
509 rc->rc_header.h_cmd = DLM_RCOM;
511 rc->rc_type = cpu_to_le32(DLM_RCOM_STATUS_REPLY);
512 rc->rc_id = rc_in->rc_id;
513 rc->rc_seq_reply = rc_in->rc_seq;
514 rc->rc_result = cpu_to_le32(-ESRCH);
516 rf = (struct rcom_config *) rc->rc_buf;
517 rf->rf_lvblen = cpu_to_le32(~0U);
519 dlm_midcomms_commit_mhandle(mh);
525 * Ignore messages for stage Y before we set
526 * recover_status bit for stage X:
530 * dlm_recover_members()
533 * - ignore NAMES, NAMES_REPLY
534 * - ignore LOOKUP, LOOKUP_REPLY
535 * - ignore LOCK, LOCK_REPLY
537 * recover_status |= NODES
539 * dlm_recover_members_wait()
541 * dlm_recover_directory()
544 * - ignore LOOKUP, LOOKUP_REPLY
545 * - ignore LOCK, LOCK_REPLY
547 * recover_status |= DIR
549 * dlm_recover_directory_wait()
551 * dlm_recover_masters()
553 * - recv LOOKUP_REPLY
555 * dlm_recover_locks()
559 * recover_status |= LOCKS
561 * dlm_recover_locks_wait()
563 * recover_status |= DONE
566 /* Called by dlm_recv; corresponds to dlm_receive_message() but special
567 recovery-only comms are sent through here. */
569 void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
571 int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
572 int stop, reply = 0, names = 0, lookup = 0, lock = 0;
576 switch (rc->rc_type) {
577 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
580 case cpu_to_le32(DLM_RCOM_NAMES):
583 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
587 case cpu_to_le32(DLM_RCOM_LOOKUP):
590 case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY):
594 case cpu_to_le32(DLM_RCOM_LOCK):
597 case cpu_to_le32(DLM_RCOM_LOCK_REPLY):
603 spin_lock(&ls->ls_recover_lock);
604 status = ls->ls_recover_status;
605 stop = dlm_recovery_stopped(ls);
606 seq = ls->ls_recover_seq;
607 spin_unlock(&ls->ls_recover_lock);
609 if (stop && (rc->rc_type != cpu_to_le32(DLM_RCOM_STATUS)))
612 if (reply && (le64_to_cpu(rc->rc_seq_reply) != seq))
615 if (!(status & DLM_RS_NODES) && (names || lookup || lock))
618 if (!(status & DLM_RS_DIR) && (lookup || lock))
621 switch (rc->rc_type) {
622 case cpu_to_le32(DLM_RCOM_STATUS):
623 receive_rcom_status(ls, rc);
626 case cpu_to_le32(DLM_RCOM_NAMES):
627 receive_rcom_names(ls, rc);
630 case cpu_to_le32(DLM_RCOM_LOOKUP):
631 receive_rcom_lookup(ls, rc);
634 case cpu_to_le32(DLM_RCOM_LOCK):
635 if (le16_to_cpu(rc->rc_header.h_length) < lock_size)
637 receive_rcom_lock(ls, rc);
640 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
641 receive_sync_reply(ls, rc);
644 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
645 receive_sync_reply(ls, rc);
648 case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY):
649 receive_rcom_lookup_reply(ls, rc);
652 case cpu_to_le32(DLM_RCOM_LOCK_REPLY):
653 if (le16_to_cpu(rc->rc_header.h_length) < lock_size)
655 dlm_recover_process_copy(ls, rc);
659 log_error(ls, "receive_rcom bad type %d",
660 le32_to_cpu(rc->rc_type));
665 log_limit(ls, "dlm_receive_rcom ignore msg %d "
666 "from %d %llu %llu recover seq %llu sts %x gen %u",
667 le32_to_cpu(rc->rc_type),
669 (unsigned long long)le64_to_cpu(rc->rc_seq),
670 (unsigned long long)le64_to_cpu(rc->rc_seq_reply),
671 (unsigned long long)seq,
672 status, ls->ls_generation);
675 log_error(ls, "recovery message %d from %d is too short",
676 le32_to_cpu(rc->rc_type), nodeid);