dlm: do not byteswap rcom_config
[deliverable/linux.git] / fs / dlm / rcom.c
CommitLineData
e7fd4179
DT
1/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
dbcfc347 5** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
e7fd4179
DT
6**
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.
10**
11*******************************************************************************
12******************************************************************************/
13
14#include "dlm_internal.h"
15#include "lockspace.h"
16#include "member.h"
17#include "lowcomms.h"
18#include "midcomms.h"
19#include "rcom.h"
20#include "recover.h"
21#include "dir.h"
22#include "config.h"
23#include "memory.h"
24#include "lock.h"
25#include "util.h"
26
27
28static int rcom_response(struct dlm_ls *ls)
29{
30 return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
31}
32
33static int create_rcom(struct dlm_ls *ls, int to_nodeid, int type, int len,
34 struct dlm_rcom **rc_ret, struct dlm_mhandle **mh_ret)
35{
36 struct dlm_rcom *rc;
37 struct dlm_mhandle *mh;
38 char *mb;
39 int mb_len = sizeof(struct dlm_rcom) + len;
40
44f487a5 41 mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, ls->ls_allocation, &mb);
e7fd4179
DT
42 if (!mh) {
43 log_print("create_rcom to %d type %d len %d ENOBUFS",
44 to_nodeid, type, len);
45 return -ENOBUFS;
46 }
47 memset(mb, 0, mb_len);
48
49 rc = (struct dlm_rcom *) mb;
50
51 rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
52 rc->rc_header.h_lockspace = ls->ls_global_id;
53 rc->rc_header.h_nodeid = dlm_our_nodeid();
54 rc->rc_header.h_length = mb_len;
55 rc->rc_header.h_cmd = DLM_RCOM;
56
57 rc->rc_type = type;
58
38aa8b0c
DT
59 spin_lock(&ls->ls_recover_lock);
60 rc->rc_seq = ls->ls_recover_seq;
61 spin_unlock(&ls->ls_recover_lock);
62
e7fd4179
DT
63 *mh_ret = mh;
64 *rc_ret = rc;
65 return 0;
66}
67
68static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
69 struct dlm_rcom *rc)
70{
71 dlm_rcom_out(rc);
72 dlm_lowcomms_commit_buffer(mh);
73}
74
75/* When replying to a status request, a node also sends back its
76 configuration values. The requesting node then checks that the remote
77 node is configured the same way as itself. */
78
79static void make_config(struct dlm_ls *ls, struct rcom_config *rf)
80{
93ff2971
AV
81 rf->rf_lvblen = cpu_to_le32(ls->ls_lvblen);
82 rf->rf_lsflags = cpu_to_le32(ls->ls_exflags);
e7fd4179
DT
83}
84
9e971b71 85static int check_config(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
e7fd4179 86{
9e971b71
DT
87 struct rcom_config *rf = (struct rcom_config *) rc->rc_buf;
88
89 if ((rc->rc_header.h_version & 0xFFFF0000) != DLM_HEADER_MAJOR) {
90 log_error(ls, "version mismatch: %x nodeid %d: %x",
91 DLM_HEADER_MAJOR | DLM_HEADER_MINOR, nodeid,
92 rc->rc_header.h_version);
8b0e7b2c 93 return -EPROTO;
9e971b71
DT
94 }
95
93ff2971
AV
96 if (le32_to_cpu(rf->rf_lvblen) != ls->ls_lvblen ||
97 le32_to_cpu(rf->rf_lsflags) != ls->ls_exflags) {
e7fd4179 98 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
93ff2971
AV
99 ls->ls_lvblen, ls->ls_exflags, nodeid,
100 le32_to_cpu(rf->rf_lvblen),
101 le32_to_cpu(rf->rf_lsflags));
8b0e7b2c 102 return -EPROTO;
e7fd4179
DT
103 }
104 return 0;
105}
106
98f176fb
DT
107static void allow_sync_reply(struct dlm_ls *ls, uint64_t *new_seq)
108{
109 spin_lock(&ls->ls_rcom_spin);
110 *new_seq = ++ls->ls_rcom_seq;
111 set_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
112 spin_unlock(&ls->ls_rcom_spin);
113}
114
115static void disallow_sync_reply(struct dlm_ls *ls)
116{
117 spin_lock(&ls->ls_rcom_spin);
118 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
119 clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
120 spin_unlock(&ls->ls_rcom_spin);
121}
122
e7fd4179
DT
123int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
124{
125 struct dlm_rcom *rc;
126 struct dlm_mhandle *mh;
127 int error = 0;
128
faa0f267 129 ls->ls_recover_nodeid = nodeid;
e7fd4179
DT
130
131 if (nodeid == dlm_our_nodeid()) {
132 rc = (struct dlm_rcom *) ls->ls_recover_buf;
133 rc->rc_result = dlm_recover_status(ls);
134 goto out;
135 }
136
137 error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
138 if (error)
139 goto out;
98f176fb
DT
140
141 allow_sync_reply(ls, &rc->rc_id);
68c817a1 142 memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
e7fd4179
DT
143
144 send_rcom(ls, mh, rc);
145
146 error = dlm_wait_function(ls, &rcom_response);
98f176fb 147 disallow_sync_reply(ls);
e7fd4179
DT
148 if (error)
149 goto out;
150
151 rc = (struct dlm_rcom *) ls->ls_recover_buf;
152
153 if (rc->rc_result == -ESRCH) {
154 /* we pretend the remote lockspace exists with 0 status */
155 log_debug(ls, "remote node %d not ready", nodeid);
156 rc->rc_result = 0;
157 } else
9e971b71 158 error = check_config(ls, rc, nodeid);
e7fd4179
DT
159 /* the caller looks at rc_result for the remote recovery status */
160 out:
161 return error;
162}
163
164static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
165{
166 struct dlm_rcom *rc;
167 struct dlm_mhandle *mh;
168 int error, nodeid = rc_in->rc_header.h_nodeid;
169
170 error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
171 sizeof(struct rcom_config), &rc, &mh);
172 if (error)
173 return;
4a99c3d9 174 rc->rc_id = rc_in->rc_id;
38aa8b0c 175 rc->rc_seq_reply = rc_in->rc_seq;
e7fd4179
DT
176 rc->rc_result = dlm_recover_status(ls);
177 make_config(ls, (struct rcom_config *) rc->rc_buf);
178
179 send_rcom(ls, mh, rc);
180}
181
4a99c3d9 182static void receive_sync_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
e7fd4179 183{
98f176fb
DT
184 spin_lock(&ls->ls_rcom_spin);
185 if (!test_bit(LSFL_RCOM_WAIT, &ls->ls_flags) ||
186 rc_in->rc_id != ls->ls_rcom_seq) {
187 log_debug(ls, "reject reply %d from %d seq %llx expect %llx",
188 rc_in->rc_type, rc_in->rc_header.h_nodeid,
57adf7ee
RK
189 (unsigned long long)rc_in->rc_id,
190 (unsigned long long)ls->ls_rcom_seq);
98f176fb 191 goto out;
4a99c3d9 192 }
e7fd4179
DT
193 memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
194 set_bit(LSFL_RCOM_READY, &ls->ls_flags);
98f176fb 195 clear_bit(LSFL_RCOM_WAIT, &ls->ls_flags);
e7fd4179 196 wake_up(&ls->ls_wait_general);
98f176fb
DT
197 out:
198 spin_unlock(&ls->ls_rcom_spin);
e7fd4179
DT
199}
200
201int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
202{
203 struct dlm_rcom *rc;
204 struct dlm_mhandle *mh;
205 int error = 0, len = sizeof(struct dlm_rcom);
206
faa0f267 207 ls->ls_recover_nodeid = nodeid;
e7fd4179
DT
208
209 if (nodeid == dlm_our_nodeid()) {
210 dlm_copy_master_names(ls, last_name, last_len,
211 ls->ls_recover_buf + len,
68c817a1 212 dlm_config.ci_buffer_size - len, nodeid);
e7fd4179
DT
213 goto out;
214 }
215
216 error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
217 if (error)
218 goto out;
219 memcpy(rc->rc_buf, last_name, last_len);
98f176fb
DT
220
221 allow_sync_reply(ls, &rc->rc_id);
68c817a1 222 memset(ls->ls_recover_buf, 0, dlm_config.ci_buffer_size);
e7fd4179
DT
223
224 send_rcom(ls, mh, rc);
225
226 error = dlm_wait_function(ls, &rcom_response);
98f176fb 227 disallow_sync_reply(ls);
e7fd4179
DT
228 out:
229 return error;
230}
231
232static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
233{
234 struct dlm_rcom *rc;
235 struct dlm_mhandle *mh;
38aa8b0c 236 int error, inlen, outlen, nodeid;
e7fd4179
DT
237
238 nodeid = rc_in->rc_header.h_nodeid;
239 inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
68c817a1 240 outlen = dlm_config.ci_buffer_size - sizeof(struct dlm_rcom);
e7fd4179
DT
241
242 error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
243 if (error)
244 return;
4a99c3d9 245 rc->rc_id = rc_in->rc_id;
38aa8b0c 246 rc->rc_seq_reply = rc_in->rc_seq;
e7fd4179
DT
247
248 dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
249 nodeid);
250 send_rcom(ls, mh, rc);
251}
252
e7fd4179
DT
253int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
254{
255 struct dlm_rcom *rc;
256 struct dlm_mhandle *mh;
257 struct dlm_ls *ls = r->res_ls;
258 int error;
259
260 error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
261 &rc, &mh);
262 if (error)
263 goto out;
264 memcpy(rc->rc_buf, r->res_name, r->res_length);
265 rc->rc_id = (unsigned long) r;
266
267 send_rcom(ls, mh, rc);
268 out:
269 return error;
270}
271
272static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
273{
274 struct dlm_rcom *rc;
275 struct dlm_mhandle *mh;
276 int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
277 int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
278
279 error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
280 if (error)
281 return;
282
283 error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
284 if (error)
285 ret_nodeid = error;
286 rc->rc_result = ret_nodeid;
287 rc->rc_id = rc_in->rc_id;
38aa8b0c 288 rc->rc_seq_reply = rc_in->rc_seq;
e7fd4179
DT
289
290 send_rcom(ls, mh, rc);
291}
292
293static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
294{
295 dlm_recover_master_reply(ls, rc_in);
296}
297
298static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
299 struct rcom_lock *rl)
300{
301 memset(rl, 0, sizeof(*rl));
302
163a1859
AV
303 rl->rl_ownpid = cpu_to_le32(lkb->lkb_ownpid);
304 rl->rl_lkid = cpu_to_le32(lkb->lkb_id);
305 rl->rl_exflags = cpu_to_le32(lkb->lkb_exflags);
306 rl->rl_flags = cpu_to_le32(lkb->lkb_flags);
307 rl->rl_lvbseq = cpu_to_le32(lkb->lkb_lvbseq);
e7fd4179
DT
308 rl->rl_rqmode = lkb->lkb_rqmode;
309 rl->rl_grmode = lkb->lkb_grmode;
310 rl->rl_status = lkb->lkb_status;
163a1859 311 rl->rl_wait_type = cpu_to_le16(lkb->lkb_wait_type);
e7fd4179
DT
312
313 if (lkb->lkb_bastaddr)
314 rl->rl_asts |= AST_BAST;
315 if (lkb->lkb_astaddr)
316 rl->rl_asts |= AST_COMP;
317
163a1859 318 rl->rl_namelen = cpu_to_le16(r->res_length);
e7fd4179
DT
319 memcpy(rl->rl_name, r->res_name, r->res_length);
320
321 /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
322 If so, receive_rcom_lock_args() won't take this copy. */
323
324 if (lkb->lkb_lvbptr)
325 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
326}
327
328int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
329{
330 struct dlm_ls *ls = r->res_ls;
331 struct dlm_rcom *rc;
332 struct dlm_mhandle *mh;
333 struct rcom_lock *rl;
334 int error, len = sizeof(struct rcom_lock);
335
336 if (lkb->lkb_lvbptr)
337 len += ls->ls_lvblen;
338
339 error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
340 if (error)
341 goto out;
342
343 rl = (struct rcom_lock *) rc->rc_buf;
344 pack_rcom_lock(r, lkb, rl);
345 rc->rc_id = (unsigned long) r;
346
347 send_rcom(ls, mh, rc);
348 out:
349 return error;
350}
351
352static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
353{
354 struct dlm_rcom *rc;
355 struct dlm_mhandle *mh;
356 int error, nodeid = rc_in->rc_header.h_nodeid;
357
358 dlm_recover_master_copy(ls, rc_in);
359
360 error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
361 sizeof(struct rcom_lock), &rc, &mh);
362 if (error)
363 return;
364
365 /* We send back the same rcom_lock struct we received, but
366 dlm_recover_master_copy() has filled in rl_remid and rl_result */
367
368 memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
369 rc->rc_id = rc_in->rc_id;
38aa8b0c 370 rc->rc_seq_reply = rc_in->rc_seq;
e7fd4179
DT
371
372 send_rcom(ls, mh, rc);
373}
374
c36258b5
DT
375/* If the lockspace doesn't exist then still send a status message
376 back; it's possible that it just doesn't have its global_id yet. */
377
378int dlm_send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
e7fd4179
DT
379{
380 struct dlm_rcom *rc;
1babdb45 381 struct rcom_config *rf;
e7fd4179
DT
382 struct dlm_mhandle *mh;
383 char *mb;
1babdb45 384 int mb_len = sizeof(struct dlm_rcom) + sizeof(struct rcom_config);
e7fd4179 385
41684f95 386 mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_NOFS, &mb);
e7fd4179
DT
387 if (!mh)
388 return -ENOBUFS;
389 memset(mb, 0, mb_len);
390
391 rc = (struct dlm_rcom *) mb;
392
393 rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
394 rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
395 rc->rc_header.h_nodeid = dlm_our_nodeid();
396 rc->rc_header.h_length = mb_len;
397 rc->rc_header.h_cmd = DLM_RCOM;
398
399 rc->rc_type = DLM_RCOM_STATUS_REPLY;
f5888750 400 rc->rc_id = rc_in->rc_id;
38aa8b0c 401 rc->rc_seq_reply = rc_in->rc_seq;
e7fd4179
DT
402 rc->rc_result = -ESRCH;
403
1babdb45 404 rf = (struct rcom_config *) rc->rc_buf;
93ff2971 405 rf->rf_lvblen = cpu_to_le32(~0U);
1babdb45 406
e7fd4179
DT
407 dlm_rcom_out(rc);
408 dlm_lowcomms_commit_buffer(mh);
409
410 return 0;
411}
412
38aa8b0c
DT
413static int is_old_reply(struct dlm_ls *ls, struct dlm_rcom *rc)
414{
415 uint64_t seq;
416 int rv = 0;
417
418 switch (rc->rc_type) {
419 case DLM_RCOM_STATUS_REPLY:
420 case DLM_RCOM_NAMES_REPLY:
421 case DLM_RCOM_LOOKUP_REPLY:
422 case DLM_RCOM_LOCK_REPLY:
423 spin_lock(&ls->ls_recover_lock);
424 seq = ls->ls_recover_seq;
425 spin_unlock(&ls->ls_recover_lock);
426 if (rc->rc_seq_reply != seq) {
8ec68867 427 log_debug(ls, "ignoring old reply %x from %d "
38aa8b0c
DT
428 "seq_reply %llx expect %llx",
429 rc->rc_type, rc->rc_header.h_nodeid,
430 (unsigned long long)rc->rc_seq_reply,
431 (unsigned long long)seq);
432 rv = 1;
433 }
434 }
435 return rv;
436}
437
c36258b5 438/* Called by dlm_recv; corresponds to dlm_receive_message() but special
e7fd4179
DT
439 recovery-only comms are sent through here. */
440
c36258b5 441void dlm_receive_rcom(struct dlm_ls *ls, struct dlm_rcom *rc, int nodeid)
e7fd4179 442{
e7fd4179 443 if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
8ec68867 444 log_debug(ls, "ignoring recovery message %x from %d",
e7fd4179
DT
445 rc->rc_type, nodeid);
446 goto out;
447 }
448
38aa8b0c
DT
449 if (is_old_reply(ls, rc))
450 goto out;
451
e7fd4179
DT
452 switch (rc->rc_type) {
453 case DLM_RCOM_STATUS:
454 receive_rcom_status(ls, rc);
455 break;
456
457 case DLM_RCOM_NAMES:
458 receive_rcom_names(ls, rc);
459 break;
460
461 case DLM_RCOM_LOOKUP:
462 receive_rcom_lookup(ls, rc);
463 break;
464
465 case DLM_RCOM_LOCK:
466 receive_rcom_lock(ls, rc);
467 break;
468
469 case DLM_RCOM_STATUS_REPLY:
dbcfc347 470 receive_sync_reply(ls, rc);
e7fd4179
DT
471 break;
472
473 case DLM_RCOM_NAMES_REPLY:
dbcfc347 474 receive_sync_reply(ls, rc);
e7fd4179
DT
475 break;
476
477 case DLM_RCOM_LOOKUP_REPLY:
478 receive_rcom_lookup_reply(ls, rc);
479 break;
480
481 case DLM_RCOM_LOCK_REPLY:
dbcfc347 482 dlm_recover_process_copy(ls, rc);
e7fd4179
DT
483 break;
484
485 default:
dbcfc347 486 log_error(ls, "receive_rcom bad type %d", rc->rc_type);
e7fd4179
DT
487 }
488 out:
c36258b5 489 return;
e7fd4179
DT
490}
491
This page took 0.259417 seconds and 5 git commands to generate.