[GFS2] Make the new argument to gfs2_trans_add_bh() actually do something
[linux-2.6] / fs / dlm / rcom.c
1 /******************************************************************************
2 *******************************************************************************
3 **
4 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5 **  Copyright (C) 2005 Red Hat, Inc.  All rights reserved.
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
28 static int rcom_response(struct dlm_ls *ls)
29 {
30         return test_bit(LSFL_RCOM_READY, &ls->ls_flags);
31 }
32
33 static 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
41         mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_KERNEL, &mb);
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
59         *mh_ret = mh;
60         *rc_ret = rc;
61         return 0;
62 }
63
64 static void send_rcom(struct dlm_ls *ls, struct dlm_mhandle *mh,
65                       struct dlm_rcom *rc)
66 {
67         dlm_rcom_out(rc);
68         dlm_lowcomms_commit_buffer(mh);
69 }
70
71 /* When replying to a status request, a node also sends back its
72    configuration values.  The requesting node then checks that the remote
73    node is configured the same way as itself. */
74
75 static void make_config(struct dlm_ls *ls, struct rcom_config *rf)
76 {
77         rf->rf_lvblen = ls->ls_lvblen;
78         rf->rf_lsflags = ls->ls_exflags;
79 }
80
81 static int check_config(struct dlm_ls *ls, struct rcom_config *rf, int nodeid)
82 {
83         if (rf->rf_lvblen != ls->ls_lvblen ||
84             rf->rf_lsflags != ls->ls_exflags) {
85                 log_error(ls, "config mismatch: %d,%x nodeid %d: %d,%x",
86                           ls->ls_lvblen, ls->ls_exflags,
87                           nodeid, rf->rf_lvblen, rf->rf_lsflags);
88                 return -EINVAL;
89         }
90         return 0;
91 }
92
93 int dlm_rcom_status(struct dlm_ls *ls, int nodeid)
94 {
95         struct dlm_rcom *rc;
96         struct dlm_mhandle *mh;
97         int error = 0;
98
99         memset(ls->ls_recover_buf, 0, dlm_config.buffer_size);
100
101         if (nodeid == dlm_our_nodeid()) {
102                 rc = (struct dlm_rcom *) ls->ls_recover_buf;
103                 rc->rc_result = dlm_recover_status(ls);
104                 goto out;
105         }
106
107         error = create_rcom(ls, nodeid, DLM_RCOM_STATUS, 0, &rc, &mh);
108         if (error)
109                 goto out;
110
111         send_rcom(ls, mh, rc);
112
113         error = dlm_wait_function(ls, &rcom_response);
114         clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
115         if (error)
116                 goto out;
117
118         rc = (struct dlm_rcom *) ls->ls_recover_buf;
119
120         if (rc->rc_result == -ESRCH) {
121                 /* we pretend the remote lockspace exists with 0 status */
122                 log_debug(ls, "remote node %d not ready", nodeid);
123                 rc->rc_result = 0;
124         } else
125                 error = check_config(ls, (struct rcom_config *) rc->rc_buf,
126                                      nodeid);
127         /* the caller looks at rc_result for the remote recovery status */
128  out:
129         return error;
130 }
131
132 static void receive_rcom_status(struct dlm_ls *ls, struct dlm_rcom *rc_in)
133 {
134         struct dlm_rcom *rc;
135         struct dlm_mhandle *mh;
136         int error, nodeid = rc_in->rc_header.h_nodeid;
137
138         error = create_rcom(ls, nodeid, DLM_RCOM_STATUS_REPLY,
139                             sizeof(struct rcom_config), &rc, &mh);
140         if (error)
141                 return;
142         rc->rc_result = dlm_recover_status(ls);
143         make_config(ls, (struct rcom_config *) rc->rc_buf);
144
145         send_rcom(ls, mh, rc);
146 }
147
148 static void receive_rcom_status_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
149 {
150         memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
151         set_bit(LSFL_RCOM_READY, &ls->ls_flags);
152         wake_up(&ls->ls_wait_general);
153 }
154
155 int dlm_rcom_names(struct dlm_ls *ls, int nodeid, char *last_name, int last_len)
156 {
157         struct dlm_rcom *rc;
158         struct dlm_mhandle *mh;
159         int error = 0, len = sizeof(struct dlm_rcom);
160
161         memset(ls->ls_recover_buf, 0, dlm_config.buffer_size);
162
163         if (nodeid == dlm_our_nodeid()) {
164                 dlm_copy_master_names(ls, last_name, last_len,
165                                       ls->ls_recover_buf + len,
166                                       dlm_config.buffer_size - len, nodeid);
167                 goto out;
168         }
169
170         error = create_rcom(ls, nodeid, DLM_RCOM_NAMES, last_len, &rc, &mh);
171         if (error)
172                 goto out;
173         memcpy(rc->rc_buf, last_name, last_len);
174
175         send_rcom(ls, mh, rc);
176
177         error = dlm_wait_function(ls, &rcom_response);
178         clear_bit(LSFL_RCOM_READY, &ls->ls_flags);
179  out:
180         return error;
181 }
182
183 static void receive_rcom_names(struct dlm_ls *ls, struct dlm_rcom *rc_in)
184 {
185         struct dlm_rcom *rc;
186         struct dlm_mhandle *mh;
187         int error, inlen, outlen;
188         int nodeid = rc_in->rc_header.h_nodeid;
189         uint32_t status = dlm_recover_status(ls);
190
191         /*
192          * We can't run dlm_dir_rebuild_send (which uses ls_nodes) while
193          * dlm_recoverd is running ls_nodes_reconfig (which changes ls_nodes).
194          * It could only happen in rare cases where we get a late NAMES
195          * message from a previous instance of recovery.
196          */
197
198         if (!(status & DLM_RS_NODES)) {
199                 log_debug(ls, "ignoring RCOM_NAMES from %u", nodeid);
200                 return;
201         }
202
203         nodeid = rc_in->rc_header.h_nodeid;
204         inlen = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
205         outlen = dlm_config.buffer_size - sizeof(struct dlm_rcom);
206
207         error = create_rcom(ls, nodeid, DLM_RCOM_NAMES_REPLY, outlen, &rc, &mh);
208         if (error)
209                 return;
210
211         dlm_copy_master_names(ls, rc_in->rc_buf, inlen, rc->rc_buf, outlen,
212                               nodeid);
213         send_rcom(ls, mh, rc);
214 }
215
216 static void receive_rcom_names_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
217 {
218         memcpy(ls->ls_recover_buf, rc_in, rc_in->rc_header.h_length);
219         set_bit(LSFL_RCOM_READY, &ls->ls_flags);
220         wake_up(&ls->ls_wait_general);
221 }
222
223 int dlm_send_rcom_lookup(struct dlm_rsb *r, int dir_nodeid)
224 {
225         struct dlm_rcom *rc;
226         struct dlm_mhandle *mh;
227         struct dlm_ls *ls = r->res_ls;
228         int error;
229
230         error = create_rcom(ls, dir_nodeid, DLM_RCOM_LOOKUP, r->res_length,
231                             &rc, &mh);
232         if (error)
233                 goto out;
234         memcpy(rc->rc_buf, r->res_name, r->res_length);
235         rc->rc_id = (unsigned long) r;
236
237         send_rcom(ls, mh, rc);
238  out:
239         return error;
240 }
241
242 static void receive_rcom_lookup(struct dlm_ls *ls, struct dlm_rcom *rc_in)
243 {
244         struct dlm_rcom *rc;
245         struct dlm_mhandle *mh;
246         int error, ret_nodeid, nodeid = rc_in->rc_header.h_nodeid;
247         int len = rc_in->rc_header.h_length - sizeof(struct dlm_rcom);
248
249         error = create_rcom(ls, nodeid, DLM_RCOM_LOOKUP_REPLY, 0, &rc, &mh);
250         if (error)
251                 return;
252
253         error = dlm_dir_lookup(ls, nodeid, rc_in->rc_buf, len, &ret_nodeid);
254         if (error)
255                 ret_nodeid = error;
256         rc->rc_result = ret_nodeid;
257         rc->rc_id = rc_in->rc_id;
258
259         send_rcom(ls, mh, rc);
260 }
261
262 static void receive_rcom_lookup_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
263 {
264         dlm_recover_master_reply(ls, rc_in);
265 }
266
267 static void pack_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb,
268                            struct rcom_lock *rl)
269 {
270         memset(rl, 0, sizeof(*rl));
271
272         rl->rl_ownpid = lkb->lkb_ownpid;
273         rl->rl_lkid = lkb->lkb_id;
274         rl->rl_exflags = lkb->lkb_exflags;
275         rl->rl_flags = lkb->lkb_flags;
276         rl->rl_lvbseq = lkb->lkb_lvbseq;
277         rl->rl_rqmode = lkb->lkb_rqmode;
278         rl->rl_grmode = lkb->lkb_grmode;
279         rl->rl_status = lkb->lkb_status;
280         rl->rl_wait_type = lkb->lkb_wait_type;
281
282         if (lkb->lkb_bastaddr)
283                 rl->rl_asts |= AST_BAST;
284         if (lkb->lkb_astaddr)
285                 rl->rl_asts |= AST_COMP;
286
287         if (lkb->lkb_range)
288                 memcpy(rl->rl_range, lkb->lkb_range, 4*sizeof(uint64_t));
289
290         rl->rl_namelen = r->res_length;
291         memcpy(rl->rl_name, r->res_name, r->res_length);
292
293         /* FIXME: might we have an lvb without DLM_LKF_VALBLK set ?
294            If so, receive_rcom_lock_args() won't take this copy. */
295
296         if (lkb->lkb_lvbptr)
297                 memcpy(rl->rl_lvb, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
298 }
299
300 int dlm_send_rcom_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
301 {
302         struct dlm_ls *ls = r->res_ls;
303         struct dlm_rcom *rc;
304         struct dlm_mhandle *mh;
305         struct rcom_lock *rl;
306         int error, len = sizeof(struct rcom_lock);
307
308         if (lkb->lkb_lvbptr)
309                 len += ls->ls_lvblen;
310
311         error = create_rcom(ls, r->res_nodeid, DLM_RCOM_LOCK, len, &rc, &mh);
312         if (error)
313                 goto out;
314
315         rl = (struct rcom_lock *) rc->rc_buf;
316         pack_rcom_lock(r, lkb, rl);
317         rc->rc_id = (unsigned long) r;
318
319         send_rcom(ls, mh, rc);
320  out:
321         return error;
322 }
323
324 static void receive_rcom_lock(struct dlm_ls *ls, struct dlm_rcom *rc_in)
325 {
326         struct dlm_rcom *rc;
327         struct dlm_mhandle *mh;
328         int error, nodeid = rc_in->rc_header.h_nodeid;
329
330         dlm_recover_master_copy(ls, rc_in);
331
332         error = create_rcom(ls, nodeid, DLM_RCOM_LOCK_REPLY,
333                             sizeof(struct rcom_lock), &rc, &mh);
334         if (error)
335                 return;
336
337         /* We send back the same rcom_lock struct we received, but
338            dlm_recover_master_copy() has filled in rl_remid and rl_result */
339
340         memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
341         rc->rc_id = rc_in->rc_id;
342
343         send_rcom(ls, mh, rc);
344 }
345
346 static void receive_rcom_lock_reply(struct dlm_ls *ls, struct dlm_rcom *rc_in)
347 {
348         uint32_t status = dlm_recover_status(ls);
349
350         if (!(status & DLM_RS_DIR)) {
351                 log_debug(ls, "ignoring RCOM_LOCK_REPLY from %u",
352                           rc_in->rc_header.h_nodeid);
353                 return;
354         }
355
356         dlm_recover_process_copy(ls, rc_in);
357 }
358
359 static int send_ls_not_ready(int nodeid, struct dlm_rcom *rc_in)
360 {
361         struct dlm_rcom *rc;
362         struct dlm_mhandle *mh;
363         char *mb;
364         int mb_len = sizeof(struct dlm_rcom);
365
366         mh = dlm_lowcomms_get_buffer(nodeid, mb_len, GFP_KERNEL, &mb);
367         if (!mh)
368                 return -ENOBUFS;
369         memset(mb, 0, mb_len);
370
371         rc = (struct dlm_rcom *) mb;
372
373         rc->rc_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
374         rc->rc_header.h_lockspace = rc_in->rc_header.h_lockspace;
375         rc->rc_header.h_nodeid = dlm_our_nodeid();
376         rc->rc_header.h_length = mb_len;
377         rc->rc_header.h_cmd = DLM_RCOM;
378
379         rc->rc_type = DLM_RCOM_STATUS_REPLY;
380         rc->rc_result = -ESRCH;
381
382         dlm_rcom_out(rc);
383         dlm_lowcomms_commit_buffer(mh);
384
385         return 0;
386 }
387
388 /* Called by dlm_recvd; corresponds to dlm_receive_message() but special
389    recovery-only comms are sent through here. */
390
391 void dlm_receive_rcom(struct dlm_header *hd, int nodeid)
392 {
393         struct dlm_rcom *rc = (struct dlm_rcom *) hd;
394         struct dlm_ls *ls;
395
396         dlm_rcom_in(rc);
397
398         /* If the lockspace doesn't exist then still send a status message
399            back; it's possible that it just doesn't have its global_id yet. */
400
401         ls = dlm_find_lockspace_global(hd->h_lockspace);
402         if (!ls) {
403                 log_print("lockspace %x from %d not found",
404                           hd->h_lockspace, nodeid);
405                 send_ls_not_ready(nodeid, rc);
406                 return;
407         }
408
409         if (dlm_recovery_stopped(ls) && (rc->rc_type != DLM_RCOM_STATUS)) {
410                 log_error(ls, "ignoring recovery message %x from %d",
411                           rc->rc_type, nodeid);
412                 goto out;
413         }
414
415         if (nodeid != rc->rc_header.h_nodeid) {
416                 log_error(ls, "bad rcom nodeid %d from %d",
417                           rc->rc_header.h_nodeid, nodeid);
418                 goto out;
419         }
420
421         switch (rc->rc_type) {
422         case DLM_RCOM_STATUS:
423                 receive_rcom_status(ls, rc);
424                 break;
425
426         case DLM_RCOM_NAMES:
427                 receive_rcom_names(ls, rc);
428                 break;
429
430         case DLM_RCOM_LOOKUP:
431                 receive_rcom_lookup(ls, rc);
432                 break;
433
434         case DLM_RCOM_LOCK:
435                 receive_rcom_lock(ls, rc);
436                 break;
437
438         case DLM_RCOM_STATUS_REPLY:
439                 receive_rcom_status_reply(ls, rc);
440                 break;
441
442         case DLM_RCOM_NAMES_REPLY:
443                 receive_rcom_names_reply(ls, rc);
444                 break;
445
446         case DLM_RCOM_LOOKUP_REPLY:
447                 receive_rcom_lookup_reply(ls, rc);
448                 break;
449
450         case DLM_RCOM_LOCK_REPLY:
451                 receive_rcom_lock_reply(ls, rc);
452                 break;
453
454         default:
455                 DLM_ASSERT(0, printk("rc_type=%x\n", rc->rc_type););
456         }
457  out:
458         dlm_put_lockspace(ls);
459 }
460