Whamcloud - gitweb
Demote the 'sending list' CERRORs to CDEBUGs, so that it's easier to spot bugs
[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                 ptl_set_inv_handle(&desc->bd_md_h);
93                 ptl_set_inv_handle(&desc->bd_me_h);
94         }
95
96         return desc;
97 }
98
99 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
100 {
101         struct ptlrpc_bulk_page *bulk;
102
103         OBD_ALLOC(bulk, sizeof(*bulk));
104         if (bulk != NULL) {
105                 bulk->bp_desc = desc;
106                 list_add_tail(&bulk->bp_link, &desc->bd_page_list);
107                 desc->bd_page_count++;
108         }
109         return bulk;
110 }
111
112 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
113 {
114         struct list_head *tmp, *next;
115         ENTRY;
116         if (desc == NULL) {
117                 EXIT;
118                 return;
119         }
120
121         list_for_each_safe(tmp, next, &desc->bd_page_list) {
122                 struct ptlrpc_bulk_page *bulk;
123                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
124                 ptlrpc_free_bulk_page(bulk);
125         }
126
127         ptlrpc_put_connection(desc->bd_connection);
128
129         OBD_FREE(desc, sizeof(*desc));
130         EXIT;
131 }
132
133 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
134 {
135         ENTRY;
136         if (bulk == NULL) {
137                 EXIT;
138                 return;
139         }
140
141         list_del(&bulk->bp_link);
142         bulk->bp_desc->bd_page_count--;
143         OBD_FREE(bulk, sizeof(*bulk));
144         EXIT;
145 }
146
147 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
148                                        int count, int *lengths, char **bufs)
149 {
150         struct ptlrpc_connection *conn = imp->imp_connection;
151         struct ptlrpc_request *request;
152         int rc;
153         ENTRY;
154
155         OBD_ALLOC(request, sizeof(*request));
156         if (!request) {
157                 CERROR("request allocation out of memory\n");
158                 RETURN(NULL);
159         }
160
161         rc = lustre_pack_msg(count, lengths, bufs,
162                              &request->rq_reqlen, &request->rq_reqmsg);
163         if (rc) {
164                 CERROR("cannot pack request %d\n", rc);
165                 OBD_FREE(request, sizeof(*request));
166                 RETURN(NULL);
167         }
168
169         request->rq_level = LUSTRE_CONN_FULL;
170         request->rq_type = PTL_RPC_MSG_REQUEST;
171         request->rq_import = imp;
172
173         /* XXX FIXME bug 625069 */
174         request->rq_request_portal = imp->imp_client->cli_request_portal;
175         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
176
177         request->rq_connection = ptlrpc_connection_addref(conn);
178
179         INIT_LIST_HEAD(&request->rq_list);
180         /*
181          * This will be reduced once when the sender is finished (waiting for
182          * reply, f.e.), and once when the request has been committed and is
183          * removed from the to-be-committed list.
184          *
185          * Also, the refcount will be increased in ptl_send_rpc immediately
186          * before we hand it off to portals, and there will be a corresponding
187          * decrease in request_out_cb (which is called to indicate that portals
188          * is finished with the request, and it can be safely freed).
189          *
190          * (Except in the DLM server case, where it will be dropped twice
191          * by the sender, and then the last time by request_out_callback.)
192          */
193         atomic_set(&request->rq_refcount, 2);
194
195         spin_lock(&conn->c_lock);
196         request->rq_xid = HTON__u32(++conn->c_xid_out);
197         spin_unlock(&conn->c_lock);
198
199         request->rq_reqmsg->magic = PTLRPC_MSG_MAGIC; 
200         request->rq_reqmsg->version = PTLRPC_MSG_VERSION;
201         request->rq_reqmsg->opc = HTON__u32(opcode);
202         request->rq_reqmsg->flags = 0;
203
204         ptlrpc_hdl2req(request, &imp->imp_handle);
205         RETURN(request);
206 }
207
208 void ptlrpc_req_finished(struct ptlrpc_request *request)
209 {
210         if (request == NULL)
211                 return;
212
213         if (atomic_dec_and_test(&request->rq_refcount))
214                 ptlrpc_free_req(request);
215 }
216
217 void ptlrpc_free_req(struct ptlrpc_request *request)
218 {
219         ENTRY;
220         if (request == NULL) {
221                 EXIT;
222                 return;
223         }
224
225         if (atomic_read(&request->rq_refcount) != 0) {
226                 CERROR("freeing request %p (%d->%s:%d) with refcount %d\n",
227                        request, request->rq_reqmsg->opc,
228                        request->rq_connection->c_remote_uuid,
229                        request->rq_import->imp_client->cli_request_portal,
230                        request->rq_refcount);
231                 /* LBUG(); */
232         }
233
234         if (request->rq_repmsg != NULL) { 
235                 OBD_FREE(request->rq_repmsg, request->rq_replen);
236                 request->rq_repmsg = NULL;
237                 request->rq_reply_md.start = NULL; 
238         }
239         if (request->rq_reqmsg != NULL) {
240                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
241                 request->rq_reqmsg = NULL;
242         }
243
244         if (request->rq_connection) {
245                 spin_lock(&request->rq_connection->c_lock);
246                 list_del_init(&request->rq_list);
247                 spin_unlock(&request->rq_connection->c_lock);
248         }
249
250         ptlrpc_put_connection(request->rq_connection);
251         OBD_FREE(request, sizeof(*request));
252         EXIT;
253 }
254
255 static int ptlrpc_check_reply(struct ptlrpc_request *req)
256 {
257         int rc = 0;
258
259         if (req->rq_repmsg != NULL) {
260                 struct ptlrpc_connection *conn = req->rq_import->imp_connection;
261                 if (req->rq_level > conn->c_level) {
262                         CDEBUG(D_HA,
263                                "rep to xid "LPD64" op %d to %s:%d: "
264                                "recovery started, ignoring (%d > %d)\n",
265                                (unsigned long long)req->rq_xid,
266                                req->rq_reqmsg->opc, conn->c_remote_uuid,
267                                req->rq_import->imp_client->cli_request_portal,
268                                req->rq_level, conn->c_level);
269                         req->rq_repmsg = NULL;
270                         GOTO(out, rc = 0);
271                 }
272                 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
273                 req->rq_flags |= PTL_RPC_FL_REPLIED;
274                 GOTO(out, rc = 1);
275         }
276
277         if (req->rq_flags & PTL_RPC_FL_RESEND) { 
278                 CERROR("-- RESTART --\n");
279                 GOTO(out, rc = 1);
280         }
281
282         if (req->rq_flags & PTL_RPC_FL_ERR) {
283                 CERROR("-- ABORTED --\n");
284                 GOTO(out, rc = 1);
285         }
286
287  out:
288         CDEBUG(D_NET, "req = %p, rc = %d\n", req, rc);
289         return rc;
290 }
291
292 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
293 {
294         ENTRY;
295
296         if (err != 0) {
297                 CERROR("err is %d\n", err);
298                 RETURN(err);
299         }
300
301         if (req == NULL) {
302                 CERROR("req == NULL\n");
303                 RETURN(-ENOMEM);
304         }
305
306         if (req->rq_repmsg == NULL) {
307                 CERROR("req->rq_repmsg == NULL\n");
308                 RETURN(-ENOMEM);
309         }
310
311         err = req->rq_repmsg->status;
312         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
313                 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
314                 RETURN(err ? err : -EINVAL);
315         }
316
317         if (err != 0) {
318                 if (err < 0)
319                         CERROR("req->rq_repmsg->status is %d\n", err);
320                 else
321                         CDEBUG(D_INFO, "req->rq_repmsg->status is %d\n", err);
322                 /* XXX: translate this error from net to host */
323                 RETURN(err);
324         }
325
326         RETURN(0);
327 }
328
329 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
330 {
331         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
332         request->rq_reqmsg = NULL;
333         request->rq_reqlen = 0;
334 }
335
336 /* Abort this request and cleanup any resources associated with it. */
337 static int ptlrpc_abort(struct ptlrpc_request *request)
338 {
339         /* First remove the ME for the reply; in theory, this means
340          * that we can tear down the buffer safely. */
341         PtlMEUnlink(request->rq_reply_me_h);
342         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
343         request->rq_repmsg = NULL;
344         request->rq_replen = 0;
345         return 0;
346 }
347
348 /* caller must hold conn->c_lock */
349 void ptlrpc_free_committed(struct ptlrpc_connection *conn)
350 {
351         struct list_head *tmp, *saved;
352         struct ptlrpc_request *req;
353
354 restart:
355         list_for_each_safe(tmp, saved, &conn->c_sending_head) {
356                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
357
358                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
359                         CDEBUG(D_INFO, "Keeping req %p xid "LPD64" for replay\n",
360                                req, req->rq_xid);
361                         continue;
362                 }
363
364                 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
365                         CDEBUG(D_INFO, "Keeping in-flight req %p xid "LPD64
366                                " for replay\n", req, req->rq_xid);
367                         continue;
368                 }
369
370                 /* not yet committed */
371                 if (req->rq_transno > conn->c_last_committed)
372                         break;
373                 
374                 CDEBUG(D_INFO, "Marking request %p xid %Ld as committed "
375                        "transno=%Lu, last_committed=%Lu\n", req,
376                        (long long)req->rq_xid, (long long)req->rq_transno,
377                        (long long)conn->c_last_committed);
378                 if (atomic_dec_and_test(&req->rq_refcount)) {
379                         /* We do this to prevent free_req deadlock.
380                          * Restarting after each removal is not so bad, as we are
381                          * almost always deleting the first item in the list.
382                          *
383                          * If we use a recursive lock here, we can skip the
384                          * unlock/lock/restart sequence.
385                          */
386                         spin_unlock(&conn->c_lock);
387                         ptlrpc_free_req(req);
388                         spin_lock(&conn->c_lock);
389                         goto restart;
390                 } else {
391                         list_del(&req->rq_list);
392                         list_add(&req->rq_list, &conn->c_dying_head);
393                 }
394         }
395
396         EXIT;
397         return;
398 }
399
400 void ptlrpc_cleanup_client(struct obd_import *imp)
401 {
402         struct list_head *tmp, *saved;
403         struct ptlrpc_request *req;
404         struct ptlrpc_connection *conn = imp->imp_connection;
405         ENTRY;
406
407         LASSERT(conn);
408
409 restart1:
410         spin_lock(&conn->c_lock);
411         list_for_each_safe(tmp, saved, &conn->c_sending_head) {
412                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
413                 if (req->rq_import != imp)
414                         continue;
415                 /* XXX we should make sure that nobody's sleeping on these! */
416                 DEBUG_REQ(D_HA, req, "cleaning up from sending list");
417                 list_del_init(&req->rq_list);
418                 req->rq_import = NULL;
419                 spin_unlock(&conn->c_lock);
420                 ptlrpc_req_finished(req);
421                 goto restart1;
422         }
423 restart2:
424         list_for_each_safe(tmp, saved, &conn->c_dying_head) {
425                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
426                 if (req->rq_import != imp)
427                         continue;
428                 DEBUG_REQ(D_ERROR, req, "on dying list at cleanup");
429                 list_del_init(&req->rq_list);
430                 req->rq_import = NULL;
431                 spin_unlock(&conn->c_lock);
432                 ptlrpc_req_finished(req); 
433                 spin_lock(&conn->c_lock);
434                 goto restart2;
435         }
436         spin_unlock(&conn->c_lock);
437
438         EXIT;
439         return;
440 }
441
442 void ptlrpc_continue_req(struct ptlrpc_request *req)
443 {
444         ENTRY;
445         CDEBUG(D_HA, "continue delayed request "LPD64" opc %d\n", 
446                req->rq_xid, req->rq_reqmsg->opc); 
447         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
448         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
449         wake_up(&req->rq_wait_for_rep); 
450         EXIT;
451 }
452
453 void ptlrpc_resend_req(struct ptlrpc_request *req)
454 {
455         ENTRY;
456         CDEBUG(D_HA, "resend request "LPD64", opc %d\n", 
457                req->rq_xid, req->rq_reqmsg->opc);
458         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
459         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
460         req->rq_status = -EAGAIN;
461         req->rq_level = LUSTRE_CONN_RECOVD;
462         req->rq_flags |= PTL_RPC_FL_RESEND;
463         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
464         wake_up(&req->rq_wait_for_rep);
465         EXIT;
466 }
467
468 void ptlrpc_restart_req(struct ptlrpc_request *req)
469 {
470         ENTRY;
471         CDEBUG(D_HA, "restart completed request "LPD64", opc %d\n", 
472                req->rq_xid, req->rq_reqmsg->opc);
473         req->rq_status = -ERESTARTSYS;
474         req->rq_flags |= PTL_RPC_FL_RECOVERY;
475         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
476         wake_up(&req->rq_wait_for_rep);
477         EXIT;
478 }
479
480 static int expired_request(void *data)
481 {
482         struct ptlrpc_request *req = data;
483
484         ENTRY;
485         if (!req) {
486                 CERROR("NULL req!");
487                 LBUG();
488                 RETURN(0);
489         }
490
491         DEBUG_REQ(D_ERROR, req, "timeout");
492         req->rq_flags |= PTL_RPC_FL_TIMEOUT;
493
494         if (!req->rq_import) {
495                 DEBUG_REQ(D_ERROR, req, "NULL import");
496                 LBUG();
497                 RETURN(0);
498         }
499
500         if (!req->rq_import->imp_connection) {
501                 DEBUG_REQ(D_ERROR, req, "NULL connection");
502                 LBUG();
503                 RETURN(0);
504         }
505
506         if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
507                 RETURN(1);
508
509         req->rq_timeout = 0;
510         req->rq_connection->c_level = LUSTRE_CONN_RECOVD;
511         recovd_conn_fail(req->rq_import->imp_connection);
512
513         /* If this request is for recovery or other primordial tasks,
514          * don't go back to sleep.
515          */
516         if (req->rq_level < LUSTRE_CONN_FULL)
517                 RETURN(1);
518         RETURN(0);
519 }
520
521 static int interrupted_request(void *data)
522 {
523         struct ptlrpc_request *req = data;
524         ENTRY;
525         req->rq_flags |= PTL_RPC_FL_INTR;
526         RETURN(1); /* ignored, as of this writing */
527 }
528
529 /* If we're being torn down by umount -f, or the import has been
530  * invalidated (such as by an OST failure), the request must fail with
531  * -EIO.
532  *
533  * Must be called with conn->c_lock held, will drop it if it returns -EIO.
534  *
535  * XXX this should just be testing the import, and umount_begin shouldn't touch
536  * XXX the connection.
537  */
538 #define EIO_IF_INVALID(conn, req)                                             \
539 if ((conn->c_flags & CONN_INVALID) ||                                         \
540     (req->rq_import->imp_flags & IMP_INVALID)) {                              \
541         DEBUG_REQ(D_ERROR, req, "%s_INVALID:",                                \
542                   (conn->c_flags & CONN_INVALID) ? "CONN" : "IMP");           \
543         spin_unlock(&conn->c_lock);                                           \
544         RETURN(-EIO);                                                         \
545 }        
546
547 int ptlrpc_queue_wait(struct ptlrpc_request *req)
548 {
549         int rc = 0;
550         struct l_wait_info lwi;
551         struct ptlrpc_client *cli = req->rq_import->imp_client;
552         struct ptlrpc_connection *conn = req->rq_import->imp_connection;
553         ENTRY;
554
555         init_waitqueue_head(&req->rq_wait_for_rep);
556         DEBUG_REQ(D_HA, req, "subsys: %s:", cli->cli_name);
557
558         /* XXX probably both an import and connection level are needed */
559         if (req->rq_level > conn->c_level) { 
560                 spin_lock(&conn->c_lock);
561                 EIO_IF_INVALID(conn, req);
562                 list_del(&req->rq_list);
563                 list_add_tail(&req->rq_list, &conn->c_delayed_head);
564                 spin_unlock(&conn->c_lock);
565
566                 DEBUG_REQ(D_HA, req, "waiting for recovery: (%d < %d)",
567                           req->rq_level, conn->c_level);
568                 lwi = LWI_INTR(NULL, NULL);
569                 rc = l_wait_event(req->rq_wait_for_rep,
570                                   (req->rq_level <= conn->c_level) ||
571                                   (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
572
573                 spin_lock(&conn->c_lock);
574                 list_del_init(&req->rq_list);
575                 spin_unlock(&conn->c_lock);
576
577                 if (req->rq_flags & PTL_RPC_FL_ERR)
578                         RETURN(-EIO);
579
580                 if (rc)
581                         RETURN(rc);
582                 
583                 CERROR("process %d resumed\n", current->pid);
584         }
585  resend:
586         req->rq_timeout = obd_timeout;
587         spin_lock(&conn->c_lock);
588         EIO_IF_INVALID(conn, req);
589         
590         list_del(&req->rq_list);
591         list_add_tail(&req->rq_list, &conn->c_sending_head);
592         spin_unlock(&conn->c_lock);
593         rc = ptl_send_rpc(req);
594         if (rc) {
595                 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
596                        req->rq_reqmsg->opc);
597                 /* the sleep below will time out, triggering recovery */
598         }
599
600         DEBUG_REQ(D_NET, req, "-- sleeping");
601         lwi = LWI_TIMEOUT_INTR(req->rq_timeout * HZ, expired_request,
602                                interrupted_request, req);
603         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
604         DEBUG_REQ(D_NET, req, "-- done sleeping");
605
606         if (req->rq_flags & PTL_RPC_FL_ERR) {
607                 ptlrpc_abort(req);
608                 GOTO(out, rc = -EIO);
609         }
610
611         /* Don't resend if we were interrupted. */
612         if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
613             PTL_RPC_FL_RESEND) {
614                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
615                 DEBUG_REQ(D_HA, req, "resending: ");
616                 goto resend;
617         }
618
619         // up(&cli->cli_rpc_sem);
620         if (req->rq_flags & PTL_RPC_FL_INTR) {
621                 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
622                         LBUG(); /* should only be interrupted if we timed out */
623                 /* Clean up the dangling reply buffers */
624                 ptlrpc_abort(req);
625                 GOTO(out, rc = -EINTR);
626         }
627
628         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
629                 GOTO(out, rc = -ETIMEDOUT);
630
631         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
632                 GOTO(out, rc = req->rq_status);
633
634         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
635         if (rc) {
636                 CERROR("unpack_rep failed: %d\n", rc);
637                 GOTO(out, rc);
638         }
639 #if 0
640         /* FIXME: Enable when BlueArc makes new release */
641         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
642             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
643                 CERROR("invalid packet type received (type=%u)\n",
644                        req->rq_repmsg->type);
645                 LBUG();
646                 GOTO(out, rc = -EINVAL);
647         }
648 #endif
649         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
650         if (req->rq_repmsg->status == 0)
651                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
652                        req->rq_replen, req->rq_repmsg->status);
653
654         spin_lock(&conn->c_lock);
655         conn->c_last_xid = req->rq_repmsg->last_xid;
656         conn->c_last_committed = req->rq_repmsg->last_committed;
657         ptlrpc_free_committed(conn);
658         spin_unlock(&conn->c_lock);
659
660         EXIT;
661  out:
662         return rc;
663 }
664
665 #undef EIO_IF_INVALID
666
667 int ptlrpc_replay_req(struct ptlrpc_request *req)
668 {
669         int rc = 0, old_level, old_status;
670         // struct ptlrpc_client *cli = req->rq_import->imp_client;
671         struct l_wait_info lwi;
672         ENTRY;
673
674         init_waitqueue_head(&req->rq_wait_for_rep);
675         DEBUG_REQ(D_NET, req, "");
676
677         req->rq_timeout = obd_timeout;
678         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
679         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
680
681         /* temporarily set request to RECOVD level (reset at out:) */
682         old_level = req->rq_level;
683         old_status = req->rq_repmsg->status;
684         req->rq_level = LUSTRE_CONN_RECOVD;
685         rc = ptl_send_rpc(req);
686         if (rc) {
687                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
688                 ptlrpc_cleanup_request_buf(req);
689                 // up(&cli->cli_rpc_sem);
690                 GOTO(out, rc = -rc);
691         }
692
693         CDEBUG(D_OTHER, "-- sleeping\n");
694         lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
695         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
696         CDEBUG(D_OTHER, "-- done\n");
697
698         // up(&cli->cli_rpc_sem);
699
700         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
701                 CERROR("Unknown reason for wakeup\n");
702                 /* XXX Phil - I end up here when I kill obdctl */
703                 ptlrpc_abort(req);
704                 GOTO(out, rc = -EINTR);
705         }
706
707         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
708         if (rc) {
709                 CERROR("unpack_rep failed: %d\n", rc);
710                 GOTO(out, rc);
711         }
712
713         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
714
715         /* let the callback do fixups, possibly including in the request */
716         if (req->rq_replay_cb)
717                 req->rq_replay_cb(req);
718
719         if (req->rq_repmsg->status != old_status) {
720                 DEBUG_REQ(D_HA, req, "status %d, old was %d",
721                           req->rq_repmsg->status, old_status);
722         }
723
724  out:
725         req->rq_level = old_level;
726         RETURN(rc);
727 }