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