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,
36 rc = (struct dlm_rcom *) mb;
38 rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
39 rc->rc_header.u.h_lockspace = cpu_to_le32(ls->ls_global_id);
40 rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
41 rc->rc_header.h_length = cpu_to_le16(mb_len);
42 rc->rc_header.h_cmd = DLM_RCOM;
44 rc->rc_type = cpu_to_le32(type);
45 rc->rc_seq = cpu_to_le64(seq);
50 static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
51 struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret,
54 int mb_len = sizeof(struct dlm_rcom) + len;
55 struct dlm_mhandle *mh;
58 mh = dlm_midcomms_get_mhandle(to_nodeid, mb_len, GFP_NOFS, &mb);
60 log_print("%s to %d type %d len %d ENOBUFS",
61 __func__, to_nodeid, type, len);
65 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len, seq);
70 static int create_rcom_stateless(struct dlm_ls *ls, int to_nodeid, int type,
71 int len, struct dlm_rcom **rc_ret,
72 struct dlm_msg **msg_ret, uint64_t seq)
74 int mb_len = sizeof(struct dlm_rcom) + len;
78 msg = dlm_lowcomms_new_msg(to_nodeid, mb_len, GFP_NOFS, &mb,
81 log_print("create_rcom to %d type %d len %d ENOBUFS",
82 to_nodeid, type, len);
86 _create_rcom(ls, to_nodeid, type, len, rc_ret, mb, mb_len, seq);
91 static void send_rcom(struct dlm_mhandle *mh, struct dlm_rcom *rc)
93 dlm_midcomms_commit_mhandle(mh, NULL, 0);
96 static void send_rcom_stateless(struct dlm_msg *msg, struct dlm_rcom *rc)
98 dlm_lowcomms_commit_msg(msg);
99 dlm_lowcomms_put_msg(msg);
102 static void set_rcom_status(struct dlm_ls *ls, struct rcom_status *rs,
105 rs->rs_flags = cpu_to_le32(flags);
108 /* When replying to a status request, a node also sends back its
109 configuration values. The requesting node then checks that the remote
110 node is configured the same way as itself. */
112 static void set_rcom_config(struct dlm_ls *ls, struct rcom_config *rf,
115 rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
116 rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
118 rf->rf_our_slot = cpu_to_le16(ls->ls_slot);
119 rf->rf_num_slots = cpu_to_le16(num_slots);
120 rf->rf_generation = cpu_to_le32(ls->ls_generation);
123 static int check_rcom_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
125 struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
127 if ((le32_to_cpu(rc->rc_header.h_version) & 0xFFFF0000) != DLM_HEADER_MAJOR) {
128 log_error(ls, "version mismatch: %x nodeid %d: %x",
129 DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
130 le32_to_cpu(rc->rc_header.h_version));
134 if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
135 le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
136 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
137 ls->ls_lvblen, ls->ls_exflags, nodeid,
138 le32_to_cpu(rf->rf_lvblen),
139 le32_to_cpu(rf->rf_lsflags));
145 static void allow_sync_reply(struct dlm_ls *ls, __le64 *new_seq)
147 spin_lock(&ls->ls_rcom_spin);
148 *new_seq = cpu_to_le64(++ls->ls_rcom_seq);
149 set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
150 spin_unlock(&ls->ls_rcom_spin);
153 static void disallow_sync_reply(struct dlm_ls *ls)
155 spin_lock(&ls->ls_rcom_spin);
156 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
157 clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
158 spin_unlock(&ls->ls_rcom_spin);
162 * low nodeid gathers one slot value at a time from each node.
163 * it sets need_slots=0, and saves rf_our_slot returned from each
166 * other nodes gather all slot values at once from the low nodeid.
167 * they set need_slots=1, and ignore the rf_our_slot returned from each
168 * rcom_config. they use the rf_num_slots returned from the low
169 * node's rcom_config.
172 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,
194 set_rcom_status(ls, (struct rcom_status *)rc->rc_buf, status_flags);
196 allow_sync_reply(ls, &rc->rc_id);
197 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
199 send_rcom_stateless(msg, rc);
201 error = dlm_wait_function(ls, &rcom_response);
202 disallow_sync_reply(ls);
203 if (error == -ETIMEDOUT)
208 rc = ls->ls_recover_buf;
210 if (rc->rc_result == cpu_to_le32(-ESRCH)) {
211 /* we pretend the remote lockspace exists with 0 status */
212 log_debug(ls, "remote node %d not ready", nodeid);
216 error = check_rcom_config(ls, rc, nodeid);
219 /* the caller looks at rc_result for the remote recovery status */
224 static void receive_rcom_status(struct dlm_ls *ls,
225 const struct dlm_rcom *rc_in,
229 struct rcom_status *rs;
231 int nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
232 int len = sizeof(struct rcom_config);
237 if (!dlm_slots_version(&rc_in->rc_header)) {
238 status = dlm_recover_status(ls);
242 rs = (struct rcom_status *)rc_in->rc_buf;
244 if (!(le32_to_cpu(rs->rs_flags) & DLM_RSF_NEED_SLOTS)) {
245 status = dlm_recover_status(ls);
249 spin_lock(&ls->ls_recover_lock);
250 status = ls->ls_recover_status;
251 num_slots = ls->ls_num_slots;
252 spin_unlock(&ls->ls_recover_lock);
253 len += num_slots * sizeof(struct rcom_slot);
256 error = create_rcom_stateless(ls, nodeid, DLM_RCOM_STATUS_REPLY,
257 len, &rc, &msg, seq);
261 rc->rc_id = rc_in->rc_id;
262 rc->rc_seq_reply = rc_in->rc_seq;
263 rc->rc_result = cpu_to_le32(status);
265 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, num_slots);
270 spin_lock(&ls->ls_recover_lock);
271 if (ls->ls_num_slots != num_slots) {
272 spin_unlock(&ls->ls_recover_lock);
273 log_debug(ls, "receive_rcom_status num_slots %d to %d",
274 num_slots, ls->ls_num_slots);
276 set_rcom_config(ls, (struct rcom_config *)rc->rc_buf, 0);
280 dlm_slots_copy_out(ls, rc);
281 spin_unlock(&ls->ls_recover_lock);
284 send_rcom_stateless(msg, rc);
287 static void receive_sync_reply(struct dlm_ls *ls, const struct dlm_rcom *rc_in)
289 spin_lock(&ls->ls_rcom_spin);
290 if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
291 le64_to_cpu(rc_in->rc_id) != ls->ls_rcom_seq) {
292 log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
293 le32_to_cpu(rc_in->rc_type),
294 le32_to_cpu(rc_in->rc_header.h_nodeid),
295 (unsigned long long)le64_to_cpu(rc_in->rc_id),
296 (unsigned long long)ls->ls_rcom_seq);
299 memcpy(ls->ls_recover_buf, rc_in,
300 le16_to_cpu(rc_in->rc_header.h_length));
301 set_bit(LSFL_RCOM_READY, &ls->ls_flags);
302 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
303 wake_up(&ls->ls_wait_general);
305 spin_unlock(&ls->ls_rcom_spin);
308 int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name,
309 int last_len, uint64_t seq)
311 struct dlm_mhandle *mh;
315 ls->ls_recover_nodeid = nodeid;
318 error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len,
322 memcpy(rc->rc_buf, last_name, last_len);
324 allow_sync_reply(ls, &rc->rc_id);
325 memset(ls->ls_recover_buf, 0, DLM_MAX_SOCKET_BUFSIZE);
329 error = dlm_wait_function(ls, &rcom_response);
330 disallow_sync_reply(ls);
331 if (error == -ETIMEDOUT)
337 static void receive_rcom_names(struct dlm_ls *ls, const struct dlm_rcom *rc_in,
340 struct dlm_mhandle *mh;
342 int error, inlen, outlen, nodeid;
344 nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
345 inlen = le16_to_cpu(rc_in->rc_header.h_length) -
346 sizeof(struct dlm_rcom);
347 outlen = DLM_MAX_APP_BUFSIZE - sizeof(struct dlm_rcom);
349 error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen,
353 rc->rc_id = rc_in->rc_id;
354 rc->rc_seq_reply = rc_in->rc_seq;
356 dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
361 int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid, uint64_t seq)
364 struct dlm_mhandle *mh;
365 struct dlm_ls *ls = r->res_ls;
368 error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
372 memcpy(rc->rc_buf, r->res_name, r->res_length);
373 rc->rc_id = cpu_to_le64(r->res_id);
380 static void receive_rcom_lookup(struct dlm_ls *ls,
381 const struct dlm_rcom *rc_in, uint64_t seq)
384 struct dlm_mhandle *mh;
385 int error, ret_nodeid, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
386 int len = le16_to_cpu(rc_in->rc_header.h_length) -
387 sizeof(struct dlm_rcom);
389 /* Old code would send this special id to trigger a debug dump. */
390 if (rc_in->rc_id == cpu_to_le64(0xFFFFFFFF)) {
391 log_error(ls, "receive_rcom_lookup dump from %d", nodeid);
392 dlm_dump_rsb_name(ls, rc_in->rc_buf, len);
396 error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh,
401 error = dlm_master_lookup(ls, nodeid, rc_in->rc_buf, len,
402 DLM_LU_RECOVER_MASTER, &ret_nodeid, NULL);
405 rc->rc_result = cpu_to_le32(ret_nodeid);
406 rc->rc_id = rc_in->rc_id;
407 rc->rc_seq_reply = rc_in->rc_seq;
412 static void receive_rcom_lookup_reply(struct dlm_ls *ls,
413 const struct dlm_rcom *rc_in)
415 dlm_recover_master_reply(ls, rc_in);
418 static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
419 struct rcom_lock *rl)
421 memset(rl, 0, sizeof(*rl));
423 rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
424 rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
425 rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
426 rl->rl_flags = cpu_to_le32(dlm_dflags_val(lkb));
427 rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
428 rl->rl_rqmode = lkb->lkb_rqmode;
429 rl->rl_grmode = lkb->lkb_grmode;
430 rl->rl_status = lkb->lkb_status;
431 rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
434 rl->rl_asts |= DLM_CB_BAST;
436 rl->rl_asts |= DLM_CB_CAST;
438 rl->rl_namelen = cpu_to_le16(r->res_length);
439 memcpy(rl->rl_name, r->res_name, r->res_length);
441 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
442 If so, receive_rcom_lock_args() won't take this copy. */
445 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
448 int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb, uint64_t seq)
450 struct dlm_ls *ls = r->res_ls;
452 struct dlm_mhandle *mh;
453 struct rcom_lock *rl;
454 int error, len = sizeof(struct rcom_lock);
457 len += ls->ls_lvblen;
459 error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh,
464 rl = (struct rcom_lock *) rc->rc_buf;
465 pack_rcom_lock(r, lkb, rl);
466 rc->rc_id = cpu_to_le64((uintptr_t)r);
473 /* needs at least dlm_rcom + rcom_lock */
474 static void receive_rcom_lock(struct dlm_ls *ls, const struct dlm_rcom *rc_in,
477 __le32 rl_remid, rl_result;
478 struct rcom_lock *rl;
480 struct dlm_mhandle *mh;
481 int error, nodeid = le32_to_cpu(rc_in->rc_header.h_nodeid);
483 dlm_recover_master_copy(ls, rc_in, &rl_remid, &rl_result);
485 error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
486 sizeof(struct rcom_lock), &rc, &mh, seq);
490 memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
491 rl = (struct rcom_lock *)rc->rc_buf;
492 /* set rl_remid and rl_result from dlm_recover_master_copy() */
493 rl->rl_remid = rl_remid;
494 rl->rl_result = rl_result;
496 rc->rc_id = rc_in->rc_id;
497 rc->rc_seq_reply = rc_in->rc_seq;
502 /* If the lockspace doesn't exist then still send a status message
503 back; it's possible that it just doesn't have its global_id yet. */
505 int dlm_send_ls_not_ready(int nodeid, const struct dlm_rcom *rc_in)
508 struct rcom_config *rf;
509 struct dlm_mhandle *mh;
511 int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
513 mh = dlm_midcomms_get_mhandle(nodeid, mb_len, GFP_NOFS, &mb);
517 rc = (struct dlm_rcom *) mb;
519 rc->rc_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
520 rc->rc_header.u.h_lockspace = rc_in->rc_header.u.h_lockspace;
521 rc->rc_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
522 rc->rc_header.h_length = cpu_to_le16(mb_len);
523 rc->rc_header.h_cmd = DLM_RCOM;
525 rc->rc_type = cpu_to_le32(DLM_RCOM_STATUS_REPLY);
526 rc->rc_id = rc_in->rc_id;
527 rc->rc_seq_reply = rc_in->rc_seq;
528 rc->rc_result = cpu_to_le32(-ESRCH);
530 rf = (struct rcom_config *) rc->rc_buf;
531 rf->rf_lvblen = cpu_to_le32(~0U);
533 dlm_midcomms_commit_mhandle(mh, NULL, 0);
539 * Ignore messages for stage Y before we set
540 * recover_status bit for stage X:
544 * dlm_recover_members()
547 * - ignore NAMES, NAMES_REPLY
548 * - ignore LOOKUP, LOOKUP_REPLY
549 * - ignore LOCK, LOCK_REPLY
551 * recover_status |= NODES
553 * dlm_recover_members_wait()
555 * dlm_recover_directory()
558 * - ignore LOOKUP, LOOKUP_REPLY
559 * - ignore LOCK, LOCK_REPLY
561 * recover_status |= DIR
563 * dlm_recover_directory_wait()
565 * dlm_recover_masters()
567 * - recv LOOKUP_REPLY
569 * dlm_recover_locks()
573 * recover_status |= LOCKS
575 * dlm_recover_locks_wait()
577 * recover_status |= DONE
580 /* Called by dlm_recv; corresponds to dlm_receive_message() but special
581 recovery-only comms are sent through here. */
583 void dlm_receive_rcom(struct dlm_ls *ls, const struct dlm_rcom *rc, int nodeid)
585 int lock_size = sizeof(struct dlm_rcom) + sizeof(struct rcom_lock);
586 int stop, reply = 0, names = 0, lookup = 0, lock = 0;
590 switch (rc->rc_type) {
591 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
594 case cpu_to_le32(DLM_RCOM_NAMES):
597 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
601 case cpu_to_le32(DLM_RCOM_LOOKUP):
604 case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY):
608 case cpu_to_le32(DLM_RCOM_LOCK):
611 case cpu_to_le32(DLM_RCOM_LOCK_REPLY):
617 spin_lock(&ls->ls_recover_lock);
618 status = ls->ls_recover_status;
619 stop = dlm_recovery_stopped(ls);
620 seq = ls->ls_recover_seq;
621 spin_unlock(&ls->ls_recover_lock);
623 if (stop && (rc->rc_type != cpu_to_le32(DLM_RCOM_STATUS)))
626 if (reply && (le64_to_cpu(rc->rc_seq_reply) != seq))
629 if (!(status & DLM_RS_NODES) && (names || lookup || lock))
632 if (!(status & DLM_RS_DIR) && (lookup || lock))
635 switch (rc->rc_type) {
636 case cpu_to_le32(DLM_RCOM_STATUS):
637 receive_rcom_status(ls, rc, seq);
640 case cpu_to_le32(DLM_RCOM_NAMES):
641 receive_rcom_names(ls, rc, seq);
644 case cpu_to_le32(DLM_RCOM_LOOKUP):
645 receive_rcom_lookup(ls, rc, seq);
648 case cpu_to_le32(DLM_RCOM_LOCK):
649 if (le16_to_cpu(rc->rc_header.h_length) < lock_size)
651 receive_rcom_lock(ls, rc, seq);
654 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
655 receive_sync_reply(ls, rc);
658 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
659 receive_sync_reply(ls, rc);
662 case cpu_to_le32(DLM_RCOM_LOOKUP_REPLY):
663 receive_rcom_lookup_reply(ls, rc);
666 case cpu_to_le32(DLM_RCOM_LOCK_REPLY):
667 if (le16_to_cpu(rc->rc_header.h_length) < lock_size)
669 dlm_recover_process_copy(ls, rc, seq);
673 log_error(ls, "receive_rcom bad type %d",
674 le32_to_cpu(rc->rc_type));
679 log_limit(ls, "dlm_receive_rcom ignore msg %d "
680 "from %d %llu %llu recover seq %llu sts %x gen %u",
681 le32_to_cpu(rc->rc_type),
683 (unsigned long long)le64_to_cpu(rc->rc_seq),
684 (unsigned long long)le64_to_cpu(rc->rc_seq_reply),
685 (unsigned long long)seq,
686 status, ls->ls_generation);
689 log_error(ls, "recovery message %d from %d is too short",
690 le32_to_cpu(rc->rc_type), nodeid);