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