Whamcloud - gitweb
Landing b_lock_replay so that Phil can use my ldlm iterators and whatnot for his
[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                 DEBUG_REQ(D_ERROR, req, "RESEND:");
380                 GOTO(out, rc = 1);
381         }
382
383         if (req->rq_flags & PTL_RPC_FL_ERR) {
384                 DEBUG_REQ(D_ERROR, req, "ABORTED:");
385                 GOTO(out, rc = 1);
386         }
387
388         if (req->rq_flags & PTL_RPC_FL_RESTART) {
389                 DEBUG_REQ(D_ERROR, req, "RESTART:");
390                 GOTO(out, rc = 1);
391         }
392  out:
393         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
394         return rc;
395 }
396
397 static int ptlrpc_check_status(struct ptlrpc_request *req)
398 {
399         int err;
400         ENTRY;
401
402         err = req->rq_repmsg->status;
403         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
404                 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
405                 RETURN(err ? err : -EINVAL);
406         }
407
408         if (err != 0) {
409                 if (err < 0)
410                         CDEBUG(D_INFO, "req->rq_repmsg->status is %d\n", err);
411                 else
412                         CDEBUG(D_INFO, "req->rq_repmsg->status is %d\n", err);
413         }
414
415         RETURN(err);
416 }
417
418 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
419 {
420         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
421         request->rq_reqmsg = NULL;
422         request->rq_reqlen = 0;
423 }
424
425 /* Abort this request and cleanup any resources associated with it. */
426 static int ptlrpc_abort(struct ptlrpc_request *request)
427 {
428         /* First remove the ME for the reply; in theory, this means
429          * that we can tear down the buffer safely. */
430         PtlMEUnlink(request->rq_reply_me_h);
431         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
432         request->rq_repmsg = NULL;
433         request->rq_replen = 0;
434         return 0;
435 }
436
437 /* caller must hold imp->imp_lock */
438 void ptlrpc_free_committed(struct obd_import *imp)
439 {
440         struct list_head *tmp, *saved;
441         struct ptlrpc_request *req;
442
443         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
444                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
445
446                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
447                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
448                         continue;
449                 }
450
451                 /* not yet committed */
452                 if (req->rq_transno > imp->imp_peer_committed_transno)
453                         break;
454
455                 DEBUG_REQ(D_HA, req, "committing (last_committed %Lu)",
456                           imp->imp_peer_committed_transno);
457                 __ptlrpc_req_finished(req, 1);
458         }
459
460         EXIT;
461         return;
462 }
463
464 void ptlrpc_cleanup_client(struct obd_import *imp)
465 {
466         struct list_head *tmp, *saved;
467         struct ptlrpc_request *req;
468         struct ptlrpc_connection *conn = imp->imp_connection;
469         ENTRY;
470
471         LASSERT(conn);
472
473         spin_lock(&imp->imp_lock);
474         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
475                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
476
477                 /* XXX we should make sure that nobody's sleeping on these! */
478                 DEBUG_REQ(D_HA, req, "cleaning up from sending list");
479                 list_del_init(&req->rq_list);
480                 req->rq_import = NULL;
481                 __ptlrpc_req_finished(req, 0);
482         }
483         spin_unlock(&imp->imp_lock);
484         
485         EXIT;
486         return;
487 }
488
489 void ptlrpc_continue_req(struct ptlrpc_request *req)
490 {
491         ENTRY;
492         DEBUG_REQ(D_HA, req, "continuing delayed request");
493         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
494         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
495         wake_up(&req->rq_wait_for_rep);
496         EXIT;
497 }
498
499 void ptlrpc_resend_req(struct ptlrpc_request *req)
500 {
501         ENTRY;
502         DEBUG_REQ(D_HA, req, "resending");
503         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
504         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
505         req->rq_status = -EAGAIN;
506         req->rq_level = LUSTRE_CONN_RECOVD;
507         req->rq_flags |= PTL_RPC_FL_RESEND;
508         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
509         wake_up(&req->rq_wait_for_rep);
510         EXIT;
511 }
512
513 void ptlrpc_restart_req(struct ptlrpc_request *req)
514 {
515         ENTRY;
516         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
517         req->rq_status = -ERESTARTSYS;
518         req->rq_flags |= PTL_RPC_FL_RESTART;
519         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
520         wake_up(&req->rq_wait_for_rep);
521         EXIT;
522 }
523
524 static int expired_request(void *data)
525 {
526         struct ptlrpc_request *req = data;
527
528         ENTRY;
529         if (!req) {
530                 CERROR("NULL req!");
531                 LBUG();
532                 RETURN(0);
533         }
534
535         DEBUG_REQ(D_ERROR, req, "timeout");
536         req->rq_flags |= PTL_RPC_FL_TIMEOUT;
537
538         if (!req->rq_import) {
539                 DEBUG_REQ(D_ERROR, req, "NULL import");
540                 LBUG();
541                 RETURN(0);
542         }
543
544         if (!req->rq_import->imp_connection) {
545                 DEBUG_REQ(D_ERROR, req, "NULL connection");
546                 LBUG();
547                 RETURN(0);
548         }
549
550         if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
551                 RETURN(1);
552
553         req->rq_timeout = 0;
554         recovd_conn_fail(req->rq_import->imp_connection);
555
556 #if 0
557         /* If this request is for recovery or other primordial tasks,
558          * don't go back to sleep.
559          */
560         if (req->rq_level < LUSTRE_CONN_FULL)
561                 RETURN(1);
562 #endif
563         RETURN(0);
564 }
565
566 static int interrupted_request(void *data)
567 {
568         struct ptlrpc_request *req = data;
569         ENTRY;
570         req->rq_flags |= PTL_RPC_FL_INTR;
571         RETURN(1); /* ignored, as of this writing */
572 }
573
574 /* If the import has been invalidated (such as by an OST failure), the
575  * request must fail with -EIO.
576  *
577  * Must be called with imp_lock held, will drop it if it returns -EIO.
578  */
579 #define EIO_IF_INVALID(req)                                                   \
580 if (req->rq_import->imp_flags & IMP_INVALID) {                                \
581         DEBUG_REQ(D_ERROR, req, "IMP_INVALID:");                              \
582         spin_unlock(&imp->imp_lock);                                          \
583         RETURN(-EIO);                                                         \
584 }
585
586 int ptlrpc_queue_wait(struct ptlrpc_request *req)
587 {
588         int rc = 0;
589         struct l_wait_info lwi;
590         struct obd_import *imp = req->rq_import;
591         struct ptlrpc_connection *conn = imp->imp_connection;
592         ENTRY;
593
594         init_waitqueue_head(&req->rq_wait_for_rep);
595
596         /* for distributed debugging */
597         req->rq_reqmsg->status = HTON__u32(current->pid); 
598         CDEBUG(D_RPCTRACE, "Sending RPC pid:xid:nid:opc %d:"LPU64":%x:%d\n",
599                NTOH__u32(req->rq_reqmsg->status), req->rq_xid,
600                conn->c_peer.peer_nid, NTOH__u32(req->rq_reqmsg->opc));
601
602         if (req->rq_level > imp->imp_level) {
603                 spin_lock(&imp->imp_lock);
604                 EIO_IF_INVALID(req);
605                 list_del(&req->rq_list);
606                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
607                 spin_unlock(&imp->imp_lock);
608
609                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%d < %d)",
610                           current->comm, req->rq_level, imp->imp_level);
611                 lwi = LWI_INTR(NULL, NULL);
612                 rc = l_wait_event(req->rq_wait_for_rep,
613                                   (req->rq_level <= imp->imp_level) ||
614                                   (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
615
616                 spin_lock(&imp->imp_lock);
617                 list_del_init(&req->rq_list);
618                 spin_unlock(&imp->imp_lock);
619
620                 if (req->rq_flags & PTL_RPC_FL_ERR)
621                         RETURN(-EIO);
622
623                 if (rc)
624                         RETURN(rc);
625
626                 CERROR("process %d resumed\n", current->pid);
627         }
628  resend:
629         req->rq_timeout = obd_timeout;
630         spin_lock(&imp->imp_lock);
631         EIO_IF_INVALID(req);
632
633         LASSERT(list_empty(&req->rq_list));
634         list_add_tail(&req->rq_list, &imp->imp_sending_list);
635         spin_unlock(&imp->imp_lock);
636         rc = ptl_send_rpc(req);
637         if (rc) {
638                 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
639                        req->rq_reqmsg->opc);
640                 /* sleep for a jiffy, then trigger recovery */
641                 lwi = LWI_TIMEOUT_INTR(1, expired_request,
642                                        interrupted_request, req);
643         } else {
644                 DEBUG_REQ(D_NET, req, "-- sleeping");
645                 lwi = LWI_TIMEOUT_INTR(req->rq_timeout * HZ, expired_request,
646                                        interrupted_request, req);
647         }
648         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
649         DEBUG_REQ(D_NET, req, "-- done sleeping");
650
651         spin_lock(&imp->imp_lock);
652         list_del_init(&req->rq_list);
653         spin_unlock(&imp->imp_lock);
654
655         if (req->rq_flags & PTL_RPC_FL_ERR) {
656                 ptlrpc_abort(req);
657                 GOTO(out, rc = -EIO);
658         }
659
660         /* Don't resend if we were interrupted. */
661         if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
662             PTL_RPC_FL_RESEND) {
663                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
664                 DEBUG_REQ(D_HA, req, "resending: ");
665                 goto resend;
666         }
667
668         if (req->rq_flags & PTL_RPC_FL_INTR) {
669                 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
670                         LBUG(); /* should only be interrupted if we timed out */
671                 /* Clean up the dangling reply buffers */
672                 ptlrpc_abort(req);
673                 GOTO(out, rc = -EINTR);
674         }
675
676         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
677                 GOTO(out, rc = -ETIMEDOUT);
678
679         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
680                 GOTO(out, rc = req->rq_status);
681
682         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
683         if (rc) {
684                 CERROR("unpack_rep failed: %d\n", rc);
685                 GOTO(out, rc);
686         }
687 #if 0
688         /* FIXME: Enable when BlueArc makes new release */
689         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
690             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
691                 CERROR("invalid packet type received (type=%u)\n",
692                        req->rq_repmsg->type);
693                 LBUG();
694                 GOTO(out, rc = -EINVAL);
695         }
696 #endif
697         CDEBUG(D_NET, "got rep "LPU64"\n", req->rq_xid);
698         if (req->rq_repmsg->status == 0)
699                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
700                        req->rq_replen, req->rq_repmsg->status);
701
702
703         if (req->rq_import->imp_flags & IMP_REPLAYABLE) {
704                 spin_lock(&imp->imp_lock);
705                 if (req->rq_flags & PTL_RPC_FL_REPLAY || req->rq_transno != 0) {
706                         /* Balanced in ptlrpc_free_committed, usually. */
707                         atomic_inc(&req->rq_refcount);
708                         list_add_tail(&req->rq_list, &imp->imp_replay_list);
709                 }
710
711                 if (req->rq_transno > imp->imp_max_transno) {
712                         imp->imp_max_transno = req->rq_transno;
713                 } else if (req->rq_transno != 0 &&
714                            imp->imp_level == LUSTRE_CONN_FULL) {
715                         CERROR("got transno "LPD64" after "LPD64": recovery "
716                                "may not work\n", req->rq_transno,
717                                imp->imp_max_transno);
718                 }
719
720                 /* Replay-enabled imports return commit-status information. */
721                 imp->imp_peer_last_xid = req->rq_repmsg->last_xid;
722                 imp->imp_peer_committed_transno =
723                         req->rq_repmsg->last_committed;
724                 spin_unlock(&imp->imp_lock);
725                 ptlrpc_free_committed(imp);
726         }
727
728         rc = ptlrpc_check_status(req);
729
730         EXIT;
731  out:
732         return rc;
733 }
734
735 #undef EIO_IF_INVALID
736
737 int ptlrpc_replay_req(struct ptlrpc_request *req)
738 {
739         int rc = 0, old_level, old_status = 0;
740         // struct ptlrpc_client *cli = req->rq_import->imp_client;
741         struct l_wait_info lwi;
742         ENTRY;
743
744         init_waitqueue_head(&req->rq_wait_for_rep);
745         DEBUG_REQ(D_NET, req, "");
746
747         req->rq_timeout = obd_timeout;
748         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
749         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
750
751         /* temporarily set request to RECOVD level (reset at out:) */
752         old_level = req->rq_level;
753         if (req->rq_flags & PTL_RPC_FL_REPLIED)
754                 old_status = req->rq_repmsg->status;
755         req->rq_level = LUSTRE_CONN_RECOVD;
756         rc = ptl_send_rpc(req);
757         if (rc) {
758                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
759                 ptlrpc_cleanup_request_buf(req);
760                 // up(&cli->cli_rpc_sem);
761                 GOTO(out, rc = -rc);
762         }
763
764         CDEBUG(D_OTHER, "-- sleeping\n");
765         lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
766         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
767         CDEBUG(D_OTHER, "-- done\n");
768
769         // up(&cli->cli_rpc_sem);
770
771         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
772                 CERROR("Unknown reason for wakeup\n");
773                 /* XXX Phil - I end up here when I kill obdctl */
774                 ptlrpc_abort(req);
775                 GOTO(out, rc = -EINTR);
776         }
777
778         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
779         if (rc) {
780                 CERROR("unpack_rep failed: %d\n", rc);
781                 GOTO(out, rc);
782         }
783
784         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
785
786         /* let the callback do fixups, possibly including in the request */
787         if (req->rq_replay_cb)
788                 req->rq_replay_cb(req);
789
790         if ((req->rq_flags & PTL_RPC_FL_REPLIED) &&
791             req->rq_repmsg->status != old_status) {
792                 DEBUG_REQ(D_HA, req, "status %d, old was %d",
793                           req->rq_repmsg->status, old_status);
794         }
795
796  out:
797         req->rq_level = old_level;
798         RETURN(rc);
799 }