Whamcloud - gitweb
* merge b_recovery into 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         if (atomic_read(&desc->bd_refcount) != 0)
135                 CERROR("freeing desc %p with refcount %d!\n", desc,
136                        atomic_read(&desc->bd_refcount));
137
138         list_for_each_safe(tmp, next, &desc->bd_page_list) {
139                 struct ptlrpc_bulk_page *bulk;
140                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
141                 ptlrpc_free_bulk_page(bulk);
142         }
143
144         ptlrpc_put_connection(desc->bd_connection);
145
146         OBD_FREE(desc, sizeof(*desc));
147         EXIT;
148 }
149
150 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
151 {
152         ENTRY;
153         if (bulk == NULL) {
154                 EXIT;
155                 return;
156         }
157
158         list_del(&bulk->bp_link);
159         bulk->bp_desc->bd_page_count--;
160         OBD_FREE(bulk, sizeof(*bulk));
161         EXIT;
162 }
163
164 static int ll_sync_brw_timeout(void *data)
165 {
166         struct obd_brw_set *set = data;
167         struct list_head *tmp;
168         int failed = 0;
169         ENTRY;
170
171         LASSERT(set);
172
173         set->brw_flags |= PTL_RPC_FL_TIMEOUT;
174
175         list_for_each(tmp, &set->brw_desc_head) {
176                 struct ptlrpc_bulk_desc *desc =
177                         list_entry(tmp, struct ptlrpc_bulk_desc, bd_set_chain);
178
179                 /* Skip descriptors that were completed successfully. */
180                 if (desc->bd_flags & (PTL_BULK_FL_RCVD | PTL_BULK_FL_SENT))
181                         continue;
182
183                 LASSERT(desc->bd_connection);
184
185                 /* If PtlMDUnlink succeeds, then it hasn't completed yet.  If it
186                  * fails, the bulk finished _just_ in time (after the timeout
187                  * fired but before we got this far) and we'll let it live.
188                  */
189                 if (PtlMDUnlink(desc->bd_md_h) != 0) {
190                         CERROR("Near-miss on OST %s -- need to adjust "
191                                "obd_timeout?\n",
192                                desc->bd_connection->c_remote_uuid);
193                         continue;
194                 }
195
196                 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
197                        desc->bd_page_count, desc->bd_connection->c_remote_uuid,
198                        desc->bd_portal, desc->bd_connection);
199
200                 /* This one will "never" arrive, don't wait for it. */
201                 if (atomic_dec_and_test(&set->brw_refcount))
202                         wake_up(&set->brw_waitq);
203
204                 if (class_signal_connection_failure)
205                         class_signal_connection_failure(desc->bd_connection);
206                 else
207                         failed = 1;
208         }
209
210         /* 0 = We go back to sleep, until we're resumed or interrupted */
211         /* 1 = We can't be recovered, just abort the syscall with -ETIMEDOUT */
212         RETURN(failed);
213 }
214
215 static int ll_sync_brw_intr(void *data)
216 {
217         struct obd_brw_set *set = data;
218
219         ENTRY;
220         set->brw_flags |= PTL_RPC_FL_INTR;
221         RETURN(1); /* ignored, as of this writing */
222 }
223
224 int ll_brw_sync_wait(struct obd_brw_set *set, int phase)
225 {
226         struct l_wait_info lwi;
227         struct list_head *tmp, *next;
228         int rc = 0;
229         ENTRY;
230
231         switch(phase) {
232         case CB_PHASE_START:
233                 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ll_sync_brw_timeout,
234                                        ll_sync_brw_intr, set);
235                 rc = l_wait_event(set->brw_waitq,
236                                   atomic_read(&set->brw_refcount) == 0, &lwi);
237
238                 list_for_each_safe(tmp, next, &set->brw_desc_head) {
239                         struct ptlrpc_bulk_desc *desc =
240                                 list_entry(tmp, struct ptlrpc_bulk_desc,
241                                            bd_set_chain);
242                         list_del_init(&desc->bd_set_chain);
243                         ptlrpc_bulk_decref(desc);
244                 }
245                 break;
246         case CB_PHASE_FINISH:
247                 if (atomic_dec_and_test(&set->brw_refcount))
248                         wake_up(&set->brw_waitq);
249                 break;
250         default:
251                 LBUG();
252         }
253
254         RETURN(rc);
255 }
256
257 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
258                                        int count, int *lengths, char **bufs)
259 {
260         struct ptlrpc_connection *conn;
261         struct ptlrpc_request *request;
262         int rc;
263         ENTRY;
264
265         LASSERT((unsigned long)imp > 0x1000);
266         conn = imp->imp_connection;
267
268         OBD_ALLOC(request, sizeof(*request));
269         if (!request) {
270                 CERROR("request allocation out of memory\n");
271                 RETURN(NULL);
272         }
273
274         rc = lustre_pack_msg(count, lengths, bufs,
275                              &request->rq_reqlen, &request->rq_reqmsg);
276         if (rc) {
277                 CERROR("cannot pack request %d\n", rc);
278                 OBD_FREE(request, sizeof(*request));
279                 RETURN(NULL);
280         }
281
282         request->rq_level = LUSTRE_CONN_FULL;
283         request->rq_type = PTL_RPC_MSG_REQUEST;
284         request->rq_import = imp;
285
286         /* XXX FIXME bug 625069 */
287         request->rq_request_portal = imp->imp_client->cli_request_portal;
288         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
289
290         request->rq_connection = ptlrpc_connection_addref(conn);
291
292         INIT_LIST_HEAD(&request->rq_list);
293         atomic_set(&request->rq_refcount, 1);
294
295         spin_lock(&imp->imp_lock);
296         request->rq_xid = HTON__u32(++imp->imp_last_xid);
297         spin_unlock(&imp->imp_lock);
298
299         request->rq_reqmsg->magic = PTLRPC_MSG_MAGIC;
300         request->rq_reqmsg->version = PTLRPC_MSG_VERSION;
301         request->rq_reqmsg->opc = HTON__u32(opcode);
302         request->rq_reqmsg->flags = 0;
303
304         ptlrpc_hdl2req(request, &imp->imp_handle);
305         RETURN(request);
306 }
307
308 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
309 {
310         ENTRY;
311         if (request == NULL) {
312                 EXIT;
313                 return;
314         }
315
316         if (atomic_read(&request->rq_refcount) != 0) {
317                 CERROR("freeing request %p (%d->%s:%d) with refcount %d\n",
318                        request, request->rq_reqmsg->opc,
319                        request->rq_connection->c_remote_uuid,
320                        request->rq_import->imp_client->cli_request_portal,
321                        request->rq_refcount);
322                 /* LBUG(); */
323         }
324
325         if (request->rq_repmsg != NULL) {
326                 OBD_FREE(request->rq_repmsg, request->rq_replen);
327                 request->rq_repmsg = NULL;
328                 request->rq_reply_md.start = NULL;
329         }
330         if (request->rq_reqmsg != NULL) {
331                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
332                 request->rq_reqmsg = NULL;
333         }
334
335         if (request->rq_import) {
336                 if (!locked)
337                         spin_lock(&request->rq_import->imp_lock);
338                 list_del_init(&request->rq_list);
339                 if (!locked)
340                         spin_unlock(&request->rq_import->imp_lock);
341         }
342
343         ptlrpc_put_connection(request->rq_connection);
344         OBD_FREE(request, sizeof(*request));
345         EXIT;
346 }
347
348 void ptlrpc_free_req(struct ptlrpc_request *request)
349 {
350         __ptlrpc_free_req(request, 0);
351 }
352
353 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
354 {
355         ENTRY;
356         if (request == NULL)
357                 RETURN(1);
358
359         DEBUG_REQ(D_INFO, request, "refcount now %u",
360                   atomic_read(&request->rq_refcount) - 1);
361
362         if (atomic_dec_and_test(&request->rq_refcount)) {
363                 __ptlrpc_free_req(request, locked);
364                 RETURN(1);
365         }
366
367         RETURN(0);
368 }
369
370 void ptlrpc_req_finished(struct ptlrpc_request *request)
371 {
372         __ptlrpc_req_finished(request, 0);
373 }
374
375 static int ptlrpc_check_reply(struct ptlrpc_request *req)
376 {
377         int rc = 0;
378
379         ENTRY;
380         if (req->rq_repmsg != NULL) {
381                 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
382                 req->rq_flags |= PTL_RPC_FL_REPLIED;
383                 GOTO(out, rc = 1);
384         }
385
386         if (req->rq_flags & PTL_RPC_FL_RESEND) {
387                 ENTRY;
388                 DEBUG_REQ(D_ERROR, req, "RESEND:");
389                 GOTO(out, rc = 1);
390         }
391
392         if (req->rq_flags & PTL_RPC_FL_ERR) {
393                 ENTRY;
394                 DEBUG_REQ(D_ERROR, req, "ABORTED:");
395                 GOTO(out, rc = 1);
396         }
397
398         if (req->rq_flags & PTL_RPC_FL_RESTART) {
399                 DEBUG_REQ(D_ERROR, req, "RESTART:");
400                 GOTO(out, rc = 1);
401         }
402         EXIT;
403  out:
404         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
405         return rc;
406 }
407
408 static int ptlrpc_check_status(struct ptlrpc_request *req)
409 {
410         int err;
411         ENTRY;
412
413         err = req->rq_repmsg->status;
414         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
415                 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR (%d)\n", err);
416                 RETURN(err ? err : -EINVAL);
417         }
418
419         if (err < 0) {
420                 DEBUG_REQ(D_ERROR, req, "status is %d", err);
421         } else if (err > 0) {
422                 /* XXX: translate this error from net to host */
423                 DEBUG_REQ(D_INFO, req, "status is %d", err);
424         }
425
426         RETURN(err);
427 }
428
429 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
430 {
431         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
432         request->rq_reqmsg = NULL;
433         request->rq_reqlen = 0;
434 }
435
436 /* Abort this request and cleanup any resources associated with it. */
437 static int ptlrpc_abort(struct ptlrpc_request *request)
438 {
439         /* First remove the ME for the reply; in theory, this means
440          * that we can tear down the buffer safely. */
441         PtlMEUnlink(request->rq_reply_me_h);
442         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
443         request->rq_repmsg = NULL;
444         request->rq_replen = 0;
445         return 0;
446 }
447
448 /* caller must hold imp->imp_lock */
449 void ptlrpc_free_committed(struct obd_import *imp)
450 {
451         struct list_head *tmp, *saved;
452         struct ptlrpc_request *req;
453         ENTRY;
454
455 #ifdef CONFIG_SMP
456         LASSERT(spin_is_locked(&imp->imp_lock));
457 #endif
458
459         CDEBUG(D_HA, "committing for xid "LPU64", last_committed "LPU64"\n",
460                imp->imp_peer_last_xid, imp->imp_peer_committed_transno);
461
462         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
463                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
464
465                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
466                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
467                         continue;
468                 }
469
470                 /* not yet committed */
471                 if (req->rq_transno > imp->imp_peer_committed_transno) {
472                         DEBUG_REQ(D_HA, req, "stopping search");
473                         break;
474                 }
475
476                 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
477                           imp->imp_peer_committed_transno);
478                 __ptlrpc_req_finished(req, 1);
479         }
480
481         EXIT;
482         return;
483 }
484
485 void ptlrpc_cleanup_client(struct obd_import *imp)
486 {
487         struct list_head *tmp, *saved;
488         struct ptlrpc_request *req;
489         struct ptlrpc_connection *conn = imp->imp_connection;
490         ENTRY;
491
492         LASSERT(conn);
493
494         spin_lock(&imp->imp_lock);
495         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
496                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
497
498                 /* XXX we should make sure that nobody's sleeping on these! */
499                 DEBUG_REQ(D_HA, req, "cleaning up from sending list");
500                 list_del_init(&req->rq_list);
501                 req->rq_import = NULL;
502                 __ptlrpc_req_finished(req, 0);
503         }
504         spin_unlock(&imp->imp_lock);
505         
506         EXIT;
507         return;
508 }
509
510 void ptlrpc_continue_req(struct ptlrpc_request *req)
511 {
512         ENTRY;
513         DEBUG_REQ(D_HA, req, "continuing delayed request");
514         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
515         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
516         wake_up(&req->rq_wait_for_rep);
517         EXIT;
518 }
519
520 void ptlrpc_resend_req(struct ptlrpc_request *req)
521 {
522         ENTRY;
523         DEBUG_REQ(D_HA, req, "resending");
524         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
525         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
526         req->rq_status = -EAGAIN;
527         req->rq_level = LUSTRE_CONN_RECOVD;
528         req->rq_flags |= PTL_RPC_FL_RESEND;
529         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
530         wake_up(&req->rq_wait_for_rep);
531         EXIT;
532 }
533
534 void ptlrpc_restart_req(struct ptlrpc_request *req)
535 {
536         ENTRY;
537         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
538         req->rq_status = -ERESTARTSYS;
539         req->rq_flags |= PTL_RPC_FL_RESTART;
540         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
541         wake_up(&req->rq_wait_for_rep);
542         EXIT;
543 }
544
545 static int expired_request(void *data)
546 {
547         struct ptlrpc_request *req = data;
548
549         ENTRY;
550         if (!req) {
551                 CERROR("NULL req!");
552                 LBUG();
553                 RETURN(0);
554         }
555
556         DEBUG_REQ(D_ERROR, req, "timeout");
557         req->rq_flags |= PTL_RPC_FL_TIMEOUT;
558
559         if (!req->rq_import) {
560                 DEBUG_REQ(D_ERROR, req, "NULL import");
561                 LBUG();
562                 RETURN(0);
563         }
564
565         if (!req->rq_import->imp_connection) {
566                 DEBUG_REQ(D_ERROR, req, "NULL connection");
567                 LBUG();
568                 RETURN(0);
569         }
570
571         if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
572                 RETURN(1);
573
574         req->rq_timeout = 0;
575         recovd_conn_fail(req->rq_import->imp_connection);
576
577 #if 0
578         /* If this request is for recovery or other primordial tasks,
579          * don't go back to sleep.
580          */
581         if (req->rq_level < LUSTRE_CONN_FULL)
582                 RETURN(1);
583 #endif
584         RETURN(0);
585 }
586
587 static int interrupted_request(void *data)
588 {
589         struct ptlrpc_request *req = data;
590         ENTRY;
591         req->rq_flags |= PTL_RPC_FL_INTR;
592         RETURN(1); /* ignored, as of this writing */
593 }
594
595 /* If the import has been invalidated (such as by an OST failure), the
596  * request must fail with -EIO.
597  *
598  * Must be called with imp_lock held, will drop it if it returns -EIO.
599  */
600 #define EIO_IF_INVALID(req)                                                   \
601 if (req->rq_import->imp_flags & IMP_INVALID) {                                \
602         DEBUG_REQ(D_ERROR, req, "IMP_INVALID:");                              \
603         spin_unlock(&imp->imp_lock);                                          \
604         RETURN(-EIO);                                                         \
605 }
606
607 int ptlrpc_queue_wait(struct ptlrpc_request *req)
608 {
609         int rc = 0;
610         struct l_wait_info lwi;
611         struct obd_import *imp = req->rq_import;
612         struct ptlrpc_connection *conn = imp->imp_connection;
613         ENTRY;
614
615         init_waitqueue_head(&req->rq_wait_for_rep);
616
617         /* for distributed debugging */
618         req->rq_reqmsg->status = HTON__u32(current->pid); 
619         CDEBUG(D_RPCTRACE, "Sending RPC pid:xid:nid:opc %d:"LPU64":%x:%d\n",
620                NTOH__u32(req->rq_reqmsg->status), req->rq_xid,
621                conn->c_peer.peer_nid, NTOH__u32(req->rq_reqmsg->opc));
622
623         spin_lock(&imp->imp_lock);
624         EIO_IF_INVALID(req);
625         if (req->rq_level > imp->imp_level) {
626                 list_del(&req->rq_list);
627                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
628                 spin_unlock(&imp->imp_lock);
629
630                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%d < %d)",
631                           current->comm, req->rq_level, imp->imp_level);
632                 lwi = LWI_INTR(NULL, NULL);
633                 rc = l_wait_event(req->rq_wait_for_rep,
634                                   (req->rq_level <= imp->imp_level) ||
635                                   (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
636
637                 spin_lock(&imp->imp_lock);
638                 list_del_init(&req->rq_list);
639                 spin_unlock(&imp->imp_lock);
640
641                 if (req->rq_flags & PTL_RPC_FL_ERR)
642                         RETURN(-EIO);
643
644                 if (rc)
645                         RETURN(rc);
646
647                 CERROR("process %d resumed\n", current->pid);
648         }
649  resend:
650
651         LASSERT(list_empty(&req->rq_list));
652         list_add_tail(&req->rq_list, &imp->imp_sending_list);
653         spin_unlock(&imp->imp_lock);
654         rc = ptl_send_rpc(req);
655         if (rc) {
656                 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
657                        req->rq_reqmsg->opc);
658                 /* sleep for a jiffy, then trigger recovery */
659                 lwi = LWI_TIMEOUT_INTR(1, expired_request,
660                                        interrupted_request, req);
661         } else {
662                 DEBUG_REQ(D_NET, req, "-- sleeping");
663                 lwi = LWI_TIMEOUT_INTR(req->rq_timeout * HZ, expired_request,
664                                        interrupted_request, req);
665         }
666         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
667         DEBUG_REQ(D_NET, req, "-- done sleeping");
668
669         spin_lock(&imp->imp_lock);
670         list_del_init(&req->rq_list);
671         spin_unlock(&imp->imp_lock);
672
673         if (req->rq_flags & PTL_RPC_FL_ERR) {
674                 ptlrpc_abort(req);
675                 GOTO(out, rc = -EIO);
676         }
677
678         /* Don't resend if we were interrupted. */
679         if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
680             PTL_RPC_FL_RESEND) {
681                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
682                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
683                 DEBUG_REQ(D_HA, req, "resending: ");
684                 goto resend;
685         }
686
687         if (req->rq_flags & PTL_RPC_FL_INTR) {
688                 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
689                         LBUG(); /* should only be interrupted if we timed out */
690                 /* Clean up the dangling reply buffers */
691                 ptlrpc_abort(req);
692                 GOTO(out, rc = -EINTR);
693         }
694
695         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
696                 GOTO(out, rc = -ETIMEDOUT);
697
698         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
699                 GOTO(out, rc = req->rq_status);
700
701         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
702         if (rc) {
703                 CERROR("unpack_rep failed: %d\n", rc);
704                 GOTO(out, rc);
705         }
706 #if 0
707         /* FIXME: Enable when BlueArc makes new release */
708         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
709             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
710                 CERROR("invalid packet type received (type=%u)\n",
711                        req->rq_repmsg->type);
712                 LBUG();
713                 GOTO(out, rc = -EINVAL);
714         }
715 #endif
716         DEBUG_REQ(D_NET, req, "status %d\n", req->rq_repmsg->status);
717
718         /* We're a rejected connection, need to invalidate and rebuild. */
719         if (req->rq_repmsg->status == -ENOTCONN) {
720                 spin_lock(&imp->imp_lock);
721                 /* If someone else is reconnecting us (CONN_RECOVD) or has
722                  * already completed it (handle mismatch), then we just need
723                  * to get out.
724                  */
725                 if (imp->imp_level == LUSTRE_CONN_RECOVD ||
726                     imp->imp_handle.addr != req->rq_reqmsg->addr ||
727                     imp->imp_handle.cookie != req->rq_reqmsg->cookie) {
728                         spin_unlock(&imp->imp_lock);
729                         GOTO(out, rc = -EIO);
730                 }
731                 imp->imp_level = LUSTRE_CONN_RECOVD;
732                 spin_unlock(&imp->imp_lock);
733                 rc = imp->imp_recover(imp, PTLRPC_RECOVD_PHASE_NOTCONN);
734                 if (rc)
735                         LBUG();
736                 GOTO(out, rc = -EIO);
737         }
738
739         if (req->rq_import->imp_flags & IMP_REPLAYABLE) {
740                 spin_lock(&imp->imp_lock);
741                 if (req->rq_flags & PTL_RPC_FL_REPLAY || req->rq_transno != 0) {
742                         /* Balanced in ptlrpc_free_committed, usually. */
743                         atomic_inc(&req->rq_refcount);
744                         list_add_tail(&req->rq_list, &imp->imp_replay_list);
745                 }
746
747                 if (req->rq_transno > imp->imp_max_transno) {
748                         imp->imp_max_transno = req->rq_transno;
749                 } else if (req->rq_transno != 0 &&
750                            imp->imp_level == LUSTRE_CONN_FULL) {
751                         CDEBUG(D_HA, "got transno "LPD64" after "LPD64
752                                ": recovery may not work\n", req->rq_transno,
753                                imp->imp_max_transno);
754                 }
755
756                 /* Replay-enabled imports return commit-status information. */
757                 imp->imp_peer_last_xid = req->rq_repmsg->last_xid;
758                 imp->imp_peer_committed_transno =
759                         req->rq_repmsg->last_committed;
760                 ptlrpc_free_committed(imp);
761                 spin_unlock(&imp->imp_lock);
762         }
763
764         rc = ptlrpc_check_status(req);
765
766         EXIT;
767  out:
768         return rc;
769 }
770
771 #undef EIO_IF_INVALID
772
773 int ptlrpc_replay_req(struct ptlrpc_request *req)
774 {
775         int rc = 0, old_level, old_status = 0;
776         // struct ptlrpc_client *cli = req->rq_import->imp_client;
777         struct l_wait_info lwi;
778         ENTRY;
779
780         init_waitqueue_head(&req->rq_wait_for_rep);
781         DEBUG_REQ(D_NET, req, "");
782
783         req->rq_timeout = obd_timeout;
784         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
785         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
786
787         /* temporarily set request to RECOVD level (reset at out:) */
788         old_level = req->rq_level;
789         if (req->rq_flags & PTL_RPC_FL_REPLIED)
790                 old_status = req->rq_repmsg->status;
791         req->rq_level = LUSTRE_CONN_RECOVD;
792         rc = ptl_send_rpc(req);
793         if (rc) {
794                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
795                 ptlrpc_cleanup_request_buf(req);
796                 // up(&cli->cli_rpc_sem);
797                 GOTO(out, rc = -rc);
798         }
799
800         CDEBUG(D_OTHER, "-- sleeping\n");
801         lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
802         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
803         CDEBUG(D_OTHER, "-- done\n");
804
805         // up(&cli->cli_rpc_sem);
806
807         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
808                 CERROR("Unknown reason for wakeup\n");
809                 /* XXX Phil - I end up here when I kill obdctl */
810                 ptlrpc_abort(req);
811                 GOTO(out, rc = -EINTR);
812         }
813
814         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
815         if (rc) {
816                 CERROR("unpack_rep failed: %d\n", rc);
817                 GOTO(out, rc);
818         }
819
820         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
821
822         /* let the callback do fixups, possibly including in the request */
823         if (req->rq_replay_cb)
824                 req->rq_replay_cb(req);
825
826         if ((req->rq_flags & PTL_RPC_FL_REPLIED) &&
827             req->rq_repmsg->status != old_status) {
828                 DEBUG_REQ(D_HA, req, "status %d, old was %d",
829                           req->rq_repmsg->status, old_status);
830         }
831
832  out:
833         req->rq_level = old_level;
834         RETURN(rc);
835 }
836
837 /* XXX looks a lot like super.c:invalidate_request_list, don't it? */
838 void ptlrpc_abort_inflight(struct obd_import *imp)
839 {
840         struct list_head *tmp, *n;
841
842         /* Make sure that no new requests get processed for this import.
843          * ptlrpc_queue_wait must (and does) hold imp_lock while testing this
844          * flag and then putting requests on sending_list or delayed_list.
845          */
846         spin_lock(&imp->imp_lock);
847         imp->imp_flags |= IMP_INVALID;
848         spin_unlock(&imp->imp_lock);
849
850         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
851                 struct ptlrpc_request *req =
852                         list_entry(tmp, struct ptlrpc_request, rq_list);
853
854                 DEBUG_REQ(D_HA, req, "inflight");
855                 req->rq_flags |= PTL_RPC_FL_ERR;
856                 wake_up(&req->rq_wait_for_rep);
857         }
858
859         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
860                 struct ptlrpc_request *req =
861                         list_entry(tmp, struct ptlrpc_request, rq_list);
862
863                 DEBUG_REQ(D_HA, req, "aborting waiting req");
864                 req->rq_flags |= PTL_RPC_FL_ERR;
865                 wake_up(&req->rq_wait_for_rep);
866         }
867 }