Whamcloud - gitweb
Merge of b_md to HEAD:
[fs/lustre-release.git] / lustre / ptlrpc / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_lib.h>
28 #include <linux/lustre_ha.h>
29 #include <linux/lustre_import.h>
30
31 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
32                         struct ptlrpc_client *cl)
33 {
34         cl->cli_request_portal = req_portal;
35         cl->cli_reply_portal   = rep_portal;
36         cl->cli_name           = name;
37 }
38
39 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
40 {
41         return req->rq_connection->c_remote_uuid;
42 }
43
44 struct ptlrpc_connection *ptlrpc_uuid_to_connection(obd_uuid_t uuid)
45 {
46         struct ptlrpc_connection *c;
47         struct lustre_peer peer;
48         int err;
49
50         err = kportal_uuid_to_peer(uuid, &peer);
51         if (err != 0) {
52                 CERROR("cannot find peer %s!\n", uuid);
53                 return NULL;
54         }
55
56         c = ptlrpc_get_connection(&peer, uuid);
57         if (c) {
58                 memcpy(c->c_remote_uuid, uuid, sizeof(c->c_remote_uuid));
59                 c->c_epoch++;
60         }
61
62         CDEBUG(D_INFO, "%s -> %p\n", uuid, c);
63
64         return c;
65 }
66
67 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,obd_uuid_t uuid)
68 {
69         struct lustre_peer peer;
70         int err;
71
72         err = kportal_uuid_to_peer(uuid, &peer);
73         if (err != 0) {
74                 CERROR("cannot find peer %s!\n", uuid);
75                 return;
76         }
77
78         memcpy(&conn->c_peer, &peer, sizeof(peer));
79         return;
80 }
81
82 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
83 {
84         struct ptlrpc_bulk_desc *desc;
85
86         OBD_ALLOC(desc, sizeof(*desc));
87         if (desc != NULL) {
88                 desc->bd_connection = ptlrpc_connection_addref(conn);
89                 atomic_set(&desc->bd_refcount, 1);
90                 init_waitqueue_head(&desc->bd_waitq);
91                 INIT_LIST_HEAD(&desc->bd_page_list);
92                 INIT_LIST_HEAD(&desc->bd_set_chain);
93                 ptl_set_inv_handle(&desc->bd_md_h);
94                 ptl_set_inv_handle(&desc->bd_me_h);
95         }
96
97         return desc;
98 }
99
100 int ptlrpc_bulk_error(struct ptlrpc_bulk_desc *desc)
101 {
102         int rc = 0;
103         if (desc->bd_flags & PTL_RPC_FL_TIMEOUT) {
104                 rc = (desc->bd_flags & PTL_RPC_FL_INTR ? -ERESTARTSYS :
105                       -ETIMEDOUT);
106         }
107         return rc;
108 }
109
110 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
111 {
112         struct ptlrpc_bulk_page *bulk;
113
114         OBD_ALLOC(bulk, sizeof(*bulk));
115         if (bulk != NULL) {
116                 bulk->bp_desc = desc;
117                 list_add_tail(&bulk->bp_link, &desc->bd_page_list);
118                 desc->bd_page_count++;
119         }
120         return bulk;
121 }
122
123 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
124 {
125         struct list_head *tmp, *next;
126         ENTRY;
127         if (desc == NULL) {
128                 EXIT;
129                 return;
130         }
131
132         LASSERT(list_empty(&desc->bd_set_chain));
133
134         list_for_each_safe(tmp, next, &desc->bd_page_list) {
135                 struct ptlrpc_bulk_page *bulk;
136                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
137                 ptlrpc_free_bulk_page(bulk);
138         }
139
140         ptlrpc_put_connection(desc->bd_connection);
141
142         OBD_FREE(desc, sizeof(*desc));
143         EXIT;
144 }
145
146 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
147 {
148         ENTRY;
149         if (bulk == NULL) {
150                 EXIT;
151                 return;
152         }
153
154         list_del(&bulk->bp_link);
155         bulk->bp_desc->bd_page_count--;
156         OBD_FREE(bulk, sizeof(*bulk));
157         EXIT;
158 }
159
160 static int ll_sync_brw_timeout(void *data)
161 {
162         struct obd_brw_set *set = data;
163         struct list_head *tmp;
164         int failed = 0;
165         ENTRY;
166
167         LASSERT(set);
168
169         set->brw_flags |= PTL_RPC_FL_TIMEOUT;
170
171         list_for_each(tmp, &set->brw_desc_head) {
172                 struct ptlrpc_bulk_desc *desc =
173                         list_entry(tmp, struct ptlrpc_bulk_desc, bd_set_chain);
174
175                 /* Skip descriptors that were completed successfully. */
176                 if (desc->bd_flags & (PTL_BULK_FL_RCVD | PTL_BULK_FL_SENT))
177                         continue;
178
179                 LASSERT(desc->bd_connection);
180
181                 /* If PtlMDUnlink succeeds, then it hasn't completed yet.  If it
182                  * fails, the bulk finished _just_ in time (after the timeout
183                  * fired but before we got this far) and we'll let it live.
184                  */
185                 if (PtlMDUnlink(desc->bd_md_h) != 0) {
186                         CERROR("Near-miss on OST %s -- need to adjust "
187                                "obd_timeout?\n",
188                                desc->bd_connection->c_remote_uuid);
189                         continue;
190                 }
191
192                 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
193                        desc->bd_page_count, desc->bd_connection->c_remote_uuid,
194                        desc->bd_portal, desc->bd_connection);
195
196                 /* This one will "never" arrive, don't wait for it. */
197                 if (atomic_dec_and_test(&set->brw_refcount))
198                         wake_up(&set->brw_waitq);
199
200                 if (class_signal_connection_failure)
201                         class_signal_connection_failure(desc->bd_connection);
202                 else
203                         failed = 1;
204         }
205
206         /* 0 = We go back to sleep, until we're resumed or interrupted */
207         /* 1 = We can't be recovered, just abort the syscall with -ETIMEDOUT */
208         RETURN(failed);
209 }
210
211 static int ll_sync_brw_intr(void *data)
212 {
213         struct obd_brw_set *set = data;
214
215         ENTRY;
216         set->brw_flags |= PTL_RPC_FL_INTR;
217         RETURN(1); /* ignored, as of this writing */
218 }
219
220 int ll_brw_sync_wait(struct obd_brw_set *set, int phase)
221 {
222         struct l_wait_info lwi;
223         struct list_head *tmp, *next;
224         int rc = 0;
225         ENTRY;
226
227         switch(phase) {
228         case CB_PHASE_START:
229                 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ll_sync_brw_timeout,
230                                        ll_sync_brw_intr, set);
231                 rc = l_wait_event(set->brw_waitq,
232                                   atomic_read(&set->brw_refcount) == 0, &lwi);
233
234                 list_for_each_safe(tmp, next, &set->brw_desc_head) {
235                         struct ptlrpc_bulk_desc *desc =
236                                 list_entry(tmp, struct ptlrpc_bulk_desc,
237                                            bd_set_chain);
238                         list_del_init(&desc->bd_set_chain);
239                         ptlrpc_bulk_decref(desc);
240                 }
241                 break;
242         case CB_PHASE_FINISH:
243                 if (atomic_dec_and_test(&set->brw_refcount))
244                         wake_up(&set->brw_waitq);
245                 break;
246         default:
247                 LBUG();
248         }
249
250         RETURN(rc);
251 }
252
253 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
254                                        int count, int *lengths, char **bufs)
255 {
256         struct ptlrpc_connection *conn = imp->imp_connection;
257         struct ptlrpc_request *request;
258         int rc;
259         ENTRY;
260
261         OBD_ALLOC(request, sizeof(*request));
262         if (!request) {
263                 CERROR("request allocation out of memory\n");
264                 RETURN(NULL);
265         }
266
267         rc = lustre_pack_msg(count, lengths, bufs,
268                              &request->rq_reqlen, &request->rq_reqmsg);
269         if (rc) {
270                 CERROR("cannot pack request %d\n", rc);
271                 OBD_FREE(request, sizeof(*request));
272                 RETURN(NULL);
273         }
274
275         request->rq_level = LUSTRE_CONN_FULL;
276         request->rq_type = PTL_RPC_MSG_REQUEST;
277         request->rq_import = imp;
278
279         /* XXX FIXME bug 625069 */
280         request->rq_request_portal = imp->imp_client->cli_request_portal;
281         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
282
283         request->rq_connection = ptlrpc_connection_addref(conn);
284
285         INIT_LIST_HEAD(&request->rq_list);
286         atomic_set(&request->rq_refcount, 1);
287
288         spin_lock(&imp->imp_lock);
289         request->rq_xid = HTON__u32(++imp->imp_last_xid);
290         spin_unlock(&imp->imp_lock);
291
292         request->rq_reqmsg->magic = PTLRPC_MSG_MAGIC;
293         request->rq_reqmsg->version = PTLRPC_MSG_VERSION;
294         request->rq_reqmsg->opc = HTON__u32(opcode);
295         request->rq_reqmsg->flags = 0;
296
297         ptlrpc_hdl2req(request, &imp->imp_handle);
298         RETURN(request);
299 }
300
301 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
302 {
303         ENTRY;
304         if (request == NULL) {
305                 EXIT;
306                 return;
307         }
308
309         if (atomic_read(&request->rq_refcount) != 0) {
310                 CERROR("freeing request %p (%d->%s:%d) with refcount %d\n",
311                        request, request->rq_reqmsg->opc,
312                        request->rq_connection->c_remote_uuid,
313                        request->rq_import->imp_client->cli_request_portal,
314                        request->rq_refcount);
315                 /* LBUG(); */
316         }
317
318         if (request->rq_repmsg != NULL) {
319                 OBD_FREE(request->rq_repmsg, request->rq_replen);
320                 request->rq_repmsg = NULL;
321                 request->rq_reply_md.start = NULL;
322         }
323         if (request->rq_reqmsg != NULL) {
324                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
325                 request->rq_reqmsg = NULL;
326         }
327
328         if (request->rq_import) {
329                 if (!locked)
330                         spin_lock(&request->rq_import->imp_lock);
331                 list_del_init(&request->rq_list);
332                 if (!locked)
333                         spin_unlock(&request->rq_import->imp_lock);
334         }
335
336         ptlrpc_put_connection(request->rq_connection);
337         OBD_FREE(request, sizeof(*request));
338         EXIT;
339 }
340
341 void ptlrpc_free_req(struct ptlrpc_request *request)
342 {
343         __ptlrpc_free_req(request, 0);
344 }
345
346 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
347 {
348         ENTRY;
349         if (request == NULL)
350                 RETURN(1);
351
352         if (atomic_dec_and_test(&request->rq_refcount)) {
353                 __ptlrpc_free_req(request, locked);
354                 RETURN(1);
355         }
356
357         DEBUG_REQ(D_INFO, request, "refcount now %u",
358                   atomic_read(&request->rq_refcount));
359         RETURN(0);
360 }
361
362 void ptlrpc_req_finished(struct ptlrpc_request *request)
363 {
364         __ptlrpc_req_finished(request, 0);
365 }
366
367 static int ptlrpc_check_reply(struct ptlrpc_request *req)
368 {
369         int rc = 0;
370
371         ENTRY;
372         if (req->rq_repmsg != NULL) {
373                 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
374                 req->rq_flags |= PTL_RPC_FL_REPLIED;
375                 GOTO(out, rc = 1);
376         }
377
378         if (req->rq_flags & PTL_RPC_FL_RESEND) {
379                 ENTRY;
380                 DEBUG_REQ(D_ERROR, req, "RESEND:");
381                 GOTO(out, rc = 1);
382         }
383
384         if (req->rq_flags & PTL_RPC_FL_ERR) {
385                 ENTRY;
386                 DEBUG_REQ(D_ERROR, req, "ABORTED:");
387                 GOTO(out, rc = 1);
388         }
389
390         if (req->rq_flags & PTL_RPC_FL_RESTART) {
391                 DEBUG_REQ(D_ERROR, req, "RESTART:");
392                 GOTO(out, rc = 1);
393         }
394  out:
395         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
396         return rc;
397 }
398
399 static int ptlrpc_check_status(struct ptlrpc_request *req)
400 {
401         int err;
402         ENTRY;
403
404         err = req->rq_repmsg->status;
405         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
406                 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR");
407                 RETURN(err ? err : -EINVAL);
408         }
409
410         if (err < 0) {
411                 DEBUG_REQ(D_ERROR, req, "status is %d", err);
412         } else if (err > 0) {
413                 /* XXX: translate this error from net to host */
414                 DEBUG_REQ(D_INFO, req, "status is %d", err);
415         }
416
417         RETURN(err);
418 }
419
420 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
421 {
422         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
423         request->rq_reqmsg = NULL;
424         request->rq_reqlen = 0;
425 }
426
427 /* Abort this request and cleanup any resources associated with it. */
428 static int ptlrpc_abort(struct ptlrpc_request *request)
429 {
430         /* First remove the ME for the reply; in theory, this means
431          * that we can tear down the buffer safely. */
432         PtlMEUnlink(request->rq_reply_me_h);
433         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
434         request->rq_repmsg = NULL;
435         request->rq_replen = 0;
436         return 0;
437 }
438
439 /* caller must hold imp->imp_lock */
440 void ptlrpc_free_committed(struct obd_import *imp)
441 {
442         struct list_head *tmp, *saved;
443         struct ptlrpc_request *req;
444         ENTRY;
445
446         CDEBUG(D_HA, "committing for xid "LPU64", last_committed "LPU64"\n",
447                imp->imp_peer_last_xid, imp->imp_peer_committed_transno);
448
449         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
450                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
451
452                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
453                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
454                         continue;
455                 }
456
457                 /* not yet committed */
458                 if (req->rq_transno > imp->imp_peer_committed_transno)
459                         break;
460
461                 DEBUG_REQ(D_HA, req, "committing (last_committed %Lu)",
462                           imp->imp_peer_committed_transno);
463                 __ptlrpc_req_finished(req, 1);
464         }
465
466         EXIT;
467         return;
468 }
469
470 void ptlrpc_cleanup_client(struct obd_import *imp)
471 {
472         struct list_head *tmp, *saved;
473         struct ptlrpc_request *req;
474         struct ptlrpc_connection *conn = imp->imp_connection;
475         ENTRY;
476
477         LASSERT(conn);
478
479         spin_lock(&imp->imp_lock);
480         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
481                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
482
483                 /* XXX we should make sure that nobody's sleeping on these! */
484                 DEBUG_REQ(D_HA, req, "cleaning up from sending list");
485                 list_del_init(&req->rq_list);
486                 req->rq_import = NULL;
487                 __ptlrpc_req_finished(req, 0);
488         }
489         spin_unlock(&imp->imp_lock);
490         
491         EXIT;
492         return;
493 }
494
495 void ptlrpc_continue_req(struct ptlrpc_request *req)
496 {
497         ENTRY;
498         DEBUG_REQ(D_HA, req, "continuing delayed request");
499         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
500         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
501         wake_up(&req->rq_wait_for_rep);
502         EXIT;
503 }
504
505 void ptlrpc_resend_req(struct ptlrpc_request *req)
506 {
507         ENTRY;
508         DEBUG_REQ(D_HA, req, "resending");
509         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
510         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
511         req->rq_status = -EAGAIN;
512         req->rq_level = LUSTRE_CONN_RECOVD;
513         req->rq_flags |= PTL_RPC_FL_RESEND;
514         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
515         wake_up(&req->rq_wait_for_rep);
516         EXIT;
517 }
518
519 void ptlrpc_restart_req(struct ptlrpc_request *req)
520 {
521         ENTRY;
522         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
523         req->rq_status = -ERESTARTSYS;
524         req->rq_flags |= PTL_RPC_FL_RESTART;
525         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
526         wake_up(&req->rq_wait_for_rep);
527         EXIT;
528 }
529
530 static int expired_request(void *data)
531 {
532         struct ptlrpc_request *req = data;
533
534         ENTRY;
535         if (!req) {
536                 CERROR("NULL req!");
537                 LBUG();
538                 RETURN(0);
539         }
540
541         DEBUG_REQ(D_ERROR, req, "timeout");
542         req->rq_flags |= PTL_RPC_FL_TIMEOUT;
543
544         if (!req->rq_import) {
545                 DEBUG_REQ(D_ERROR, req, "NULL import");
546                 LBUG();
547                 RETURN(0);
548         }
549
550         if (!req->rq_import->imp_connection) {
551                 DEBUG_REQ(D_ERROR, req, "NULL connection");
552                 LBUG();
553                 RETURN(0);
554         }
555
556         if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
557                 RETURN(1);
558
559         req->rq_timeout = 0;
560         recovd_conn_fail(req->rq_import->imp_connection);
561
562 #if 0
563         /* If this request is for recovery or other primordial tasks,
564          * don't go back to sleep.
565          */
566         if (req->rq_level < LUSTRE_CONN_FULL)
567                 RETURN(1);
568 #endif
569         RETURN(0);
570 }
571
572 static int interrupted_request(void *data)
573 {
574         struct ptlrpc_request *req = data;
575         ENTRY;
576         req->rq_flags |= PTL_RPC_FL_INTR;
577         RETURN(1); /* ignored, as of this writing */
578 }
579
580 /* If the import has been invalidated (such as by an OST failure), the
581  * request must fail with -EIO.
582  *
583  * Must be called with imp_lock held, will drop it if it returns -EIO.
584  */
585 #define EIO_IF_INVALID(req)                                                   \
586 if (req->rq_import->imp_flags & IMP_INVALID) {                                \
587         DEBUG_REQ(D_ERROR, req, "IMP_INVALID:");                              \
588         spin_unlock(&imp->imp_lock);                                          \
589         RETURN(-EIO);                                                         \
590 }
591
592 int ptlrpc_queue_wait(struct ptlrpc_request *req)
593 {
594         int rc = 0;
595         struct l_wait_info lwi;
596         struct obd_import *imp = req->rq_import;
597         struct ptlrpc_connection *conn = imp->imp_connection;
598         ENTRY;
599
600         init_waitqueue_head(&req->rq_wait_for_rep);
601
602         /* for distributed debugging */
603         req->rq_reqmsg->status = HTON__u32(current->pid); 
604         CDEBUG(D_RPCTRACE, "Sending RPC pid:xid:nid:opc %d:"LPU64":%x:%d\n",
605                NTOH__u32(req->rq_reqmsg->status), req->rq_xid,
606                conn->c_peer.peer_nid, NTOH__u32(req->rq_reqmsg->opc));
607
608         if (req->rq_level > imp->imp_level) {
609                 spin_lock(&imp->imp_lock);
610                 EIO_IF_INVALID(req);
611                 list_del(&req->rq_list);
612                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
613                 spin_unlock(&imp->imp_lock);
614
615                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%d < %d)",
616                           current->comm, req->rq_level, imp->imp_level);
617                 lwi = LWI_INTR(NULL, NULL);
618                 rc = l_wait_event(req->rq_wait_for_rep,
619                                   (req->rq_level <= imp->imp_level) ||
620                                   (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
621
622                 spin_lock(&imp->imp_lock);
623                 list_del_init(&req->rq_list);
624                 spin_unlock(&imp->imp_lock);
625
626                 if (req->rq_flags & PTL_RPC_FL_ERR)
627                         RETURN(-EIO);
628
629                 if (rc)
630                         RETURN(rc);
631
632                 CERROR("process %d resumed\n", current->pid);
633         }
634  resend:
635         req->rq_timeout = obd_timeout;
636         spin_lock(&imp->imp_lock);
637         EIO_IF_INVALID(req);
638
639         LASSERT(list_empty(&req->rq_list));
640         list_add_tail(&req->rq_list, &imp->imp_sending_list);
641         spin_unlock(&imp->imp_lock);
642         rc = ptl_send_rpc(req);
643         if (rc) {
644                 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
645                        req->rq_reqmsg->opc);
646                 /* sleep for a jiffy, then trigger recovery */
647                 lwi = LWI_TIMEOUT_INTR(1, expired_request,
648                                        interrupted_request, req);
649         } else {
650                 DEBUG_REQ(D_NET, req, "-- sleeping");
651                 lwi = LWI_TIMEOUT_INTR(req->rq_timeout * HZ, expired_request,
652                                        interrupted_request, req);
653         }
654         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
655         DEBUG_REQ(D_NET, req, "-- done sleeping");
656
657         spin_lock(&imp->imp_lock);
658         list_del_init(&req->rq_list);
659         spin_unlock(&imp->imp_lock);
660
661         if (req->rq_flags & PTL_RPC_FL_ERR) {
662                 ptlrpc_abort(req);
663                 GOTO(out, rc = -EIO);
664         }
665
666         /* Don't resend if we were interrupted. */
667         if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
668             PTL_RPC_FL_RESEND) {
669                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
670                 DEBUG_REQ(D_HA, req, "resending: ");
671                 goto resend;
672         }
673
674         if (req->rq_flags & PTL_RPC_FL_INTR) {
675                 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
676                         LBUG(); /* should only be interrupted if we timed out */
677                 /* Clean up the dangling reply buffers */
678                 ptlrpc_abort(req);
679                 GOTO(out, rc = -EINTR);
680         }
681
682         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
683                 GOTO(out, rc = -ETIMEDOUT);
684
685         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
686                 GOTO(out, rc = req->rq_status);
687
688         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
689         if (rc) {
690                 CERROR("unpack_rep failed: %d\n", rc);
691                 GOTO(out, rc);
692         }
693 #if 0
694         /* FIXME: Enable when BlueArc makes new release */
695         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
696             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
697                 CERROR("invalid packet type received (type=%u)\n",
698                        req->rq_repmsg->type);
699                 LBUG();
700                 GOTO(out, rc = -EINVAL);
701         }
702 #endif
703         CDEBUG(D_NET, "got rep "LPU64"\n", req->rq_xid);
704         if (req->rq_repmsg->status == 0)
705                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
706                        req->rq_replen, req->rq_repmsg->status);
707
708
709         if (req->rq_import->imp_flags & IMP_REPLAYABLE) {
710                 spin_lock(&imp->imp_lock);
711                 if (req->rq_flags & PTL_RPC_FL_REPLAY || req->rq_transno != 0) {
712                         /* Balanced in ptlrpc_free_committed, usually. */
713                         atomic_inc(&req->rq_refcount);
714                         list_add_tail(&req->rq_list, &imp->imp_replay_list);
715                 }
716
717                 if (req->rq_transno > imp->imp_max_transno) {
718                         imp->imp_max_transno = req->rq_transno;
719                 } else if (req->rq_transno != 0 &&
720                            imp->imp_level == LUSTRE_CONN_FULL) {
721                         CERROR("got transno "LPD64" after "LPD64": recovery "
722                                "may not work\n", req->rq_transno,
723                                imp->imp_max_transno);
724                 }
725
726                 /* Replay-enabled imports return commit-status information. */
727                 imp->imp_peer_last_xid = req->rq_repmsg->last_xid;
728                 imp->imp_peer_committed_transno =
729                         req->rq_repmsg->last_committed;
730                 ptlrpc_free_committed(imp);
731                 spin_unlock(&imp->imp_lock);
732         }
733
734         rc = ptlrpc_check_status(req);
735
736         EXIT;
737  out:
738         return rc;
739 }
740
741 #undef EIO_IF_INVALID
742
743 int ptlrpc_replay_req(struct ptlrpc_request *req)
744 {
745         int rc = 0, old_level, old_status = 0;
746         // struct ptlrpc_client *cli = req->rq_import->imp_client;
747         struct l_wait_info lwi;
748         ENTRY;
749
750         init_waitqueue_head(&req->rq_wait_for_rep);
751         DEBUG_REQ(D_NET, req, "");
752
753         req->rq_timeout = obd_timeout;
754         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
755         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
756
757         /* temporarily set request to RECOVD level (reset at out:) */
758         old_level = req->rq_level;
759         if (req->rq_flags & PTL_RPC_FL_REPLIED)
760                 old_status = req->rq_repmsg->status;
761         req->rq_level = LUSTRE_CONN_RECOVD;
762         rc = ptl_send_rpc(req);
763         if (rc) {
764                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
765                 ptlrpc_cleanup_request_buf(req);
766                 // up(&cli->cli_rpc_sem);
767                 GOTO(out, rc = -rc);
768         }
769
770         CDEBUG(D_OTHER, "-- sleeping\n");
771         lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
772         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
773         CDEBUG(D_OTHER, "-- done\n");
774
775         // up(&cli->cli_rpc_sem);
776
777         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
778                 CERROR("Unknown reason for wakeup\n");
779                 /* XXX Phil - I end up here when I kill obdctl */
780                 ptlrpc_abort(req);
781                 GOTO(out, rc = -EINTR);
782         }
783
784         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
785         if (rc) {
786                 CERROR("unpack_rep failed: %d\n", rc);
787                 GOTO(out, rc);
788         }
789
790         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
791
792         /* let the callback do fixups, possibly including in the request */
793         if (req->rq_replay_cb)
794                 req->rq_replay_cb(req);
795
796         if ((req->rq_flags & PTL_RPC_FL_REPLIED) &&
797             req->rq_repmsg->status != old_status) {
798                 DEBUG_REQ(D_HA, req, "status %d, old was %d",
799                           req->rq_repmsg->status, old_status);
800         }
801
802  out:
803         req->rq_level = old_level;
804         RETURN(rc);
805 }