1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
7 ** This copyrighted material is made available to anyone wishing to use,
8 ** modify, copy, or redistribute it subject to the terms and conditions
9 ** of the GNU General Public License v.2.
11 *******************************************************************************
12 ******************************************************************************/
14 #include "dlm_internal.h"
15 #include "lockspace.h"
27 static int rcom_response(struct dlm_ls
*ls
)
29 return test_bit(LSFL_RCOM_READY
, &ls
->ls_flags
);
32 static int create_rcom(struct dlm_ls
*ls
, int to_nodeid
, int type
, int len
,
33 struct dlm_rcom
**rc_ret
, struct dlm_mhandle
**mh_ret
)
36 struct dlm_mhandle
*mh
;
38 int mb_len
= sizeof(struct dlm_rcom
) + len
;
40 mh
= dlm_lowcomms_get_buffer(to_nodeid
, mb_len
, GFP_NOFS
, &mb
);
42 log_print("create_rcom to %d type %d len %d ENOBUFS",
43 to_nodeid
, type
, len
);
46 memset(mb
, 0, mb_len
);
48 rc
= (struct dlm_rcom
*) mb
;
50 rc
->rc_header
.h_version
= (DLM_HEADER_MAJOR
| DLM_HEADER_MINOR
);
51 rc
->rc_header
.h_lockspace
= ls
->ls_global_id
;
52 rc
->rc_header
.h_nodeid
= dlm_our_nodeid();
53 rc
->rc_header
.h_length
= mb_len
;
54 rc
->rc_header
.h_cmd
= DLM_RCOM
;
58 spin_lock(&ls
->ls_recover_lock
);
59 rc
->rc_seq
= ls
->ls_recover_seq
;
60 spin_unlock(&ls
->ls_recover_lock
);
67 static void send_rcom(struct dlm_ls
*ls
, struct dlm_mhandle
*mh
,
71 dlm_lowcomms_commit_buffer(mh
);
74 static void set_rcom_status(struct dlm_ls
*ls
, struct rcom_status
*rs
,
77 rs
->rs_flags
= cpu_to_le32(flags
);
80 /* When replying to a status request, a node also sends back its
81 configuration values. The requesting node then checks that the remote
82 node is configured the same way as itself. */
84 static void set_rcom_config(struct dlm_ls
*ls
, struct rcom_config
*rf
,
87 rf
->rf_lvblen
= cpu_to_le32(ls
->ls_lvblen
);
88 rf
->rf_lsflags
= cpu_to_le32(ls
->ls_exflags
);
90 rf
->rf_our_slot
= cpu_to_le16(ls
->ls_slot
);
91 rf
->rf_num_slots
= cpu_to_le16(num_slots
);
92 rf
->rf_generation
= cpu_to_le32(ls
->ls_generation
);
95 static int check_rcom_config(struct dlm_ls
*ls
, struct dlm_rcom
*rc
, int nodeid
)
97 struct rcom_config
*rf
= (struct rcom_config
*) rc
->rc_buf
;
99 if ((rc
->rc_header
.h_version
& 0xFFFF0000) != DLM_HEADER_MAJOR
) {
100 log_error(ls
, "version mismatch: %x nodeid %d: %x",
101 DLM_HEADER_MAJOR
| DLM_HEADER_MINOR
, nodeid
,
102 rc
->rc_header
.h_version
);
106 if (le32_to_cpu(rf
->rf_lvblen
) != ls
->ls_lvblen
||
107 le32_to_cpu(rf
->rf_lsflags
) != ls
->ls_exflags
) {
108 log_error(ls
, "config mismatch: %d,%x nodeid %d: %d,%x",
109 ls
->ls_lvblen
, ls
->ls_exflags
, nodeid
,
110 le32_to_cpu(rf
->rf_lvblen
),
111 le32_to_cpu(rf
->rf_lsflags
));
117 static void allow_sync_reply(struct dlm_ls
*ls
, uint64_t *new_seq
)
119 spin_lock(&ls
->ls_rcom_spin
);
120 *new_seq
= ++ls
->ls_rcom_seq
;
121 set_bit(LSFL_RCOM_WAIT
, &ls
->ls_flags
);
122 spin_unlock(&ls
->ls_rcom_spin
);
125 static void disallow_sync_reply(struct dlm_ls
*ls
)
127 spin_lock(&ls
->ls_rcom_spin
);
128 clear_bit(LSFL_RCOM_WAIT
, &ls
->ls_flags
);
129 clear_bit(LSFL_RCOM_READY
, &ls
->ls_flags
);
130 spin_unlock(&ls
->ls_rcom_spin
);
134 * low nodeid gathers one slot value at a time from each node.
135 * it sets need_slots=0, and saves rf_our_slot returned from each
138 * other nodes gather all slot values at once from the low nodeid.
139 * they set need_slots=1, and ignore the rf_our_slot returned from each
140 * rcom_config. they use the rf_num_slots returned from the low
141 * node's rcom_config.
144 int dlm_rcom_status(struct dlm_ls
*ls
, int nodeid
, uint32_t status_flags
)
147 struct dlm_mhandle
*mh
;
150 ls
->ls_recover_nodeid
= nodeid
;
152 if (nodeid
== dlm_our_nodeid()) {
153 rc
= ls
->ls_recover_buf
;
154 rc
->rc_result
= dlm_recover_status(ls
);
159 error
= create_rcom(ls
, nodeid
, DLM_RCOM_STATUS
,
160 sizeof(struct rcom_status
), &rc
, &mh
);
164 set_rcom_status(ls
, (struct rcom_status
*)rc
->rc_buf
, status_flags
);
166 allow_sync_reply(ls
, &rc
->rc_id
);
167 memset(ls
->ls_recover_buf
, 0, dlm_config
.ci_buffer_size
);
169 send_rcom(ls
, mh
, rc
);
171 error
= dlm_wait_function(ls
, &rcom_response
);
172 disallow_sync_reply(ls
);
173 if (error
== -ETIMEDOUT
)
178 rc
= ls
->ls_recover_buf
;
180 if (rc
->rc_result
== -ESRCH
) {
181 /* we pretend the remote lockspace exists with 0 status */
182 log_debug(ls
, "remote node %d not ready", nodeid
);
186 error
= check_rcom_config(ls
, rc
, nodeid
);
189 /* the caller looks at rc_result for the remote recovery status */
194 static void receive_rcom_status(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
197 struct dlm_mhandle
*mh
;
198 struct rcom_status
*rs
;
200 int nodeid
= rc_in
->rc_header
.h_nodeid
;
201 int len
= sizeof(struct rcom_config
);
205 if (!dlm_slots_version(&rc_in
->rc_header
)) {
206 status
= dlm_recover_status(ls
);
210 rs
= (struct rcom_status
*)rc_in
->rc_buf
;
212 if (!(le32_to_cpu(rs
->rs_flags
) & DLM_RSF_NEED_SLOTS
)) {
213 status
= dlm_recover_status(ls
);
217 spin_lock(&ls
->ls_recover_lock
);
218 status
= ls
->ls_recover_status
;
219 num_slots
= ls
->ls_num_slots
;
220 spin_unlock(&ls
->ls_recover_lock
);
221 len
+= num_slots
* sizeof(struct rcom_slot
);
224 error
= create_rcom(ls
, nodeid
, DLM_RCOM_STATUS_REPLY
,
229 rc
->rc_id
= rc_in
->rc_id
;
230 rc
->rc_seq_reply
= rc_in
->rc_seq
;
231 rc
->rc_result
= status
;
233 set_rcom_config(ls
, (struct rcom_config
*)rc
->rc_buf
, num_slots
);
238 spin_lock(&ls
->ls_recover_lock
);
239 if (ls
->ls_num_slots
!= num_slots
) {
240 spin_unlock(&ls
->ls_recover_lock
);
241 log_debug(ls
, "receive_rcom_status num_slots %d to %d",
242 num_slots
, ls
->ls_num_slots
);
244 set_rcom_config(ls
, (struct rcom_config
*)rc
->rc_buf
, 0);
248 dlm_slots_copy_out(ls
, rc
);
249 spin_unlock(&ls
->ls_recover_lock
);
252 send_rcom(ls
, mh
, rc
);
255 static void receive_sync_reply(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
257 spin_lock(&ls
->ls_rcom_spin
);
258 if (!test_bit(LSFL_RCOM_WAIT
, &ls
->ls_flags
) ||
259 rc_in
->rc_id
!= ls
->ls_rcom_seq
) {
260 log_debug(ls
, "reject reply %d from %d seq %llx expect %llx",
261 rc_in
->rc_type
, rc_in
->rc_header
.h_nodeid
,
262 (unsigned long long)rc_in
->rc_id
,
263 (unsigned long long)ls
->ls_rcom_seq
);
266 memcpy(ls
->ls_recover_buf
, rc_in
, rc_in
->rc_header
.h_length
);
267 set_bit(LSFL_RCOM_READY
, &ls
->ls_flags
);
268 clear_bit(LSFL_RCOM_WAIT
, &ls
->ls_flags
);
269 wake_up(&ls
->ls_wait_general
);
271 spin_unlock(&ls
->ls_rcom_spin
);
274 int dlm_rcom_names(struct dlm_ls
*ls
, int nodeid
, char *last_name
, int last_len
)
277 struct dlm_mhandle
*mh
;
280 ls
->ls_recover_nodeid
= nodeid
;
283 error
= create_rcom(ls
, nodeid
, DLM_RCOM_NAMES
, last_len
, &rc
, &mh
);
286 memcpy(rc
->rc_buf
, last_name
, last_len
);
288 allow_sync_reply(ls
, &rc
->rc_id
);
289 memset(ls
->ls_recover_buf
, 0, dlm_config
.ci_buffer_size
);
291 send_rcom(ls
, mh
, rc
);
293 error
= dlm_wait_function(ls
, &rcom_response
);
294 disallow_sync_reply(ls
);
295 if (error
== -ETIMEDOUT
)
301 static void receive_rcom_names(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
304 struct dlm_mhandle
*mh
;
305 int error
, inlen
, outlen
, nodeid
;
307 nodeid
= rc_in
->rc_header
.h_nodeid
;
308 inlen
= rc_in
->rc_header
.h_length
- sizeof(struct dlm_rcom
);
309 outlen
= dlm_config
.ci_buffer_size
- sizeof(struct dlm_rcom
);
311 error
= create_rcom(ls
, nodeid
, DLM_RCOM_NAMES_REPLY
, outlen
, &rc
, &mh
);
314 rc
->rc_id
= rc_in
->rc_id
;
315 rc
->rc_seq_reply
= rc_in
->rc_seq
;
317 dlm_copy_master_names(ls
, rc_in
->rc_buf
, inlen
, rc
->rc_buf
, outlen
,
319 send_rcom(ls
, mh
, rc
);
322 int dlm_send_rcom_lookup(struct dlm_rsb
*r
, int dir_nodeid
)
325 struct dlm_mhandle
*mh
;
326 struct dlm_ls
*ls
= r
->res_ls
;
329 error
= create_rcom(ls
, dir_nodeid
, DLM_RCOM_LOOKUP
, r
->res_length
,
333 memcpy(rc
->rc_buf
, r
->res_name
, r
->res_length
);
334 rc
->rc_id
= (unsigned long) r
->res_id
;
336 send_rcom(ls
, mh
, rc
);
341 static void receive_rcom_lookup(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
344 struct dlm_mhandle
*mh
;
345 int error
, ret_nodeid
, nodeid
= rc_in
->rc_header
.h_nodeid
;
346 int len
= rc_in
->rc_header
.h_length
- sizeof(struct dlm_rcom
);
348 error
= create_rcom(ls
, nodeid
, DLM_RCOM_LOOKUP_REPLY
, 0, &rc
, &mh
);
352 /* Old code would send this special id to trigger a debug dump. */
353 if (rc_in
->rc_id
== 0xFFFFFFFF) {
354 log_error(ls
, "receive_rcom_lookup dump from %d", nodeid
);
355 dlm_dump_rsb_name(ls
, rc_in
->rc_buf
, len
);
359 error
= dlm_master_lookup(ls
, nodeid
, rc_in
->rc_buf
, len
,
360 DLM_LU_RECOVER_MASTER
, &ret_nodeid
, NULL
);
363 rc
->rc_result
= ret_nodeid
;
364 rc
->rc_id
= rc_in
->rc_id
;
365 rc
->rc_seq_reply
= rc_in
->rc_seq
;
367 send_rcom(ls
, mh
, rc
);
370 static void receive_rcom_lookup_reply(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
372 dlm_recover_master_reply(ls
, rc_in
);
375 static void pack_rcom_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
,
376 struct rcom_lock
*rl
)
378 memset(rl
, 0, sizeof(*rl
));
380 rl
->rl_ownpid
= cpu_to_le32(lkb
->lkb_ownpid
);
381 rl
->rl_lkid
= cpu_to_le32(lkb
->lkb_id
);
382 rl
->rl_exflags
= cpu_to_le32(lkb
->lkb_exflags
);
383 rl
->rl_flags
= cpu_to_le32(lkb
->lkb_flags
);
384 rl
->rl_lvbseq
= cpu_to_le32(lkb
->lkb_lvbseq
);
385 rl
->rl_rqmode
= lkb
->lkb_rqmode
;
386 rl
->rl_grmode
= lkb
->lkb_grmode
;
387 rl
->rl_status
= lkb
->lkb_status
;
388 rl
->rl_wait_type
= cpu_to_le16(lkb
->lkb_wait_type
);
391 rl
->rl_asts
|= DLM_CB_BAST
;
393 rl
->rl_asts
|= DLM_CB_CAST
;
395 rl
->rl_namelen
= cpu_to_le16(r
->res_length
);
396 memcpy(rl
->rl_name
, r
->res_name
, r
->res_length
);
398 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
399 If so, receive_rcom_lock_args() won't take this copy. */
402 memcpy(rl
->rl_lvb
, lkb
->lkb_lvbptr
, r
->res_ls
->ls_lvblen
);
405 int dlm_send_rcom_lock(struct dlm_rsb
*r
, struct dlm_lkb
*lkb
)
407 struct dlm_ls
*ls
= r
->res_ls
;
409 struct dlm_mhandle
*mh
;
410 struct rcom_lock
*rl
;
411 int error
, len
= sizeof(struct rcom_lock
);
414 len
+= ls
->ls_lvblen
;
416 error
= create_rcom(ls
, r
->res_nodeid
, DLM_RCOM_LOCK
, len
, &rc
, &mh
);
420 rl
= (struct rcom_lock
*) rc
->rc_buf
;
421 pack_rcom_lock(r
, lkb
, rl
);
422 rc
->rc_id
= (unsigned long) r
;
424 send_rcom(ls
, mh
, rc
);
429 /* needs at least dlm_rcom + rcom_lock */
430 static void receive_rcom_lock(struct dlm_ls
*ls
, struct dlm_rcom
*rc_in
)
433 struct dlm_mhandle
*mh
;
434 int error
, nodeid
= rc_in
->rc_header
.h_nodeid
;
436 dlm_recover_master_copy(ls
, rc_in
);
438 error
= create_rcom(ls
, nodeid
, DLM_RCOM_LOCK_REPLY
,
439 sizeof(struct rcom_lock
), &rc
, &mh
);
443 /* We send back the same rcom_lock struct we received, but
444 dlm_recover_master_copy() has filled in rl_remid and rl_result */
446 memcpy(rc
->rc_buf
, rc_in
->rc_buf
, sizeof(struct rcom_lock
));
447 rc
->rc_id
= rc_in
->rc_id
;
448 rc
->rc_seq_reply
= rc_in
->rc_seq
;
450 send_rcom(ls
, mh
, rc
);
453 /* If the lockspace doesn't exist then still send a status message
454 back; it's possible that it just doesn't have its global_id yet. */
456 int dlm_send_ls_not_ready(int nodeid
, struct dlm_rcom
*rc_in
)
459 struct rcom_config
*rf
;
460 struct dlm_mhandle
*mh
;
462 int mb_len
= sizeof(struct dlm_rcom
) + sizeof(struct rcom_config
);
464 mh
= dlm_lowcomms_get_buffer(nodeid
, mb_len
, GFP_NOFS
, &mb
);
467 memset(mb
, 0, mb_len
);
469 rc
= (struct dlm_rcom
*) mb
;
471 rc
->rc_header
.h_version
= (DLM_HEADER_MAJOR
| DLM_HEADER_MINOR
);
472 rc
->rc_header
.h_lockspace
= rc_in
->rc_header
.h_lockspace
;
473 rc
->rc_header
.h_nodeid
= dlm_our_nodeid();
474 rc
->rc_header
.h_length
= mb_len
;
475 rc
->rc_header
.h_cmd
= DLM_RCOM
;
477 rc
->rc_type
= DLM_RCOM_STATUS_REPLY
;
478 rc
->rc_id
= rc_in
->rc_id
;
479 rc
->rc_seq_reply
= rc_in
->rc_seq
;
480 rc
->rc_result
= -ESRCH
;
482 rf
= (struct rcom_config
*) rc
->rc_buf
;
483 rf
->rf_lvblen
= cpu_to_le32(~0U);
486 dlm_lowcomms_commit_buffer(mh
);
492 * Ignore messages for stage Y before we set
493 * recover_status bit for stage X:
497 * dlm_recover_members()
500 * - ignore NAMES, NAMES_REPLY
501 * - ignore LOOKUP, LOOKUP_REPLY
502 * - ignore LOCK, LOCK_REPLY
504 * recover_status |= NODES
506 * dlm_recover_members_wait()
508 * dlm_recover_directory()
511 * - ignore LOOKUP, LOOKUP_REPLY
512 * - ignore LOCK, LOCK_REPLY
514 * recover_status |= DIR
516 * dlm_recover_directory_wait()
518 * dlm_recover_masters()
520 * - recv LOOKUP_REPLY
522 * dlm_recover_locks()
526 * recover_status |= LOCKS
528 * dlm_recover_locks_wait()
530 * recover_status |= DONE
533 /* Called by dlm_recv; corresponds to dlm_receive_message() but special
534 recovery-only comms are sent through here. */
536 void dlm_receive_rcom(struct dlm_ls
*ls
, struct dlm_rcom
*rc
, int nodeid
)
538 int lock_size
= sizeof(struct dlm_rcom
) + sizeof(struct rcom_lock
);
539 int stop
, reply
= 0, names
= 0, lookup
= 0, lock
= 0;
543 switch (rc
->rc_type
) {
544 case DLM_RCOM_STATUS_REPLY
:
550 case DLM_RCOM_NAMES_REPLY
:
554 case DLM_RCOM_LOOKUP
:
557 case DLM_RCOM_LOOKUP_REPLY
:
564 case DLM_RCOM_LOCK_REPLY
:
570 spin_lock(&ls
->ls_recover_lock
);
571 status
= ls
->ls_recover_status
;
572 stop
= test_bit(LSFL_RECOVER_STOP
, &ls
->ls_flags
);
573 seq
= ls
->ls_recover_seq
;
574 spin_unlock(&ls
->ls_recover_lock
);
576 if (stop
&& (rc
->rc_type
!= DLM_RCOM_STATUS
))
579 if (reply
&& (rc
->rc_seq_reply
!= seq
))
582 if (!(status
& DLM_RS_NODES
) && (names
|| lookup
|| lock
))
585 if (!(status
& DLM_RS_DIR
) && (lookup
|| lock
))
588 switch (rc
->rc_type
) {
589 case DLM_RCOM_STATUS
:
590 receive_rcom_status(ls
, rc
);
594 receive_rcom_names(ls
, rc
);
597 case DLM_RCOM_LOOKUP
:
598 receive_rcom_lookup(ls
, rc
);
602 if (rc
->rc_header
.h_length
< lock_size
)
604 receive_rcom_lock(ls
, rc
);
607 case DLM_RCOM_STATUS_REPLY
:
608 receive_sync_reply(ls
, rc
);
611 case DLM_RCOM_NAMES_REPLY
:
612 receive_sync_reply(ls
, rc
);
615 case DLM_RCOM_LOOKUP_REPLY
:
616 receive_rcom_lookup_reply(ls
, rc
);
619 case DLM_RCOM_LOCK_REPLY
:
620 if (rc
->rc_header
.h_length
< lock_size
)
622 dlm_recover_process_copy(ls
, rc
);
626 log_error(ls
, "receive_rcom bad type %d", rc
->rc_type
);
631 log_limit(ls
, "dlm_receive_rcom ignore msg %d "
632 "from %d %llu %llu recover seq %llu sts %x gen %u",
635 (unsigned long long)rc
->rc_seq
,
636 (unsigned long long)rc
->rc_seq_reply
,
637 (unsigned long long)seq
,
638 status
, ls
->ls_generation
);
641 log_error(ls
, "recovery message %d from %d is too short",
642 rc
->rc_type
, nodeid
);