Whamcloud - gitweb
b=3063
[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, 2003 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 #ifndef __KERNEL__
25 #include <errno.h>
26 #include <signal.h>
27 #include <liblustre.h>
28 #endif
29
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
32 #include <linux/lustre_lib.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_import.h>
35
36 #include "ptlrpc_internal.h"
37
38 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
39                         struct ptlrpc_client *cl)
40 {
41         cl->cli_request_portal = req_portal;
42         cl->cli_reply_portal   = rep_portal;
43         cl->cli_name           = name;
44 }
45
46 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
47 {
48         struct ptlrpc_connection *c;
49         struct ptlrpc_peer peer;
50         int err;
51
52         err = ptlrpc_uuid_to_peer(uuid, &peer);
53         if (err != 0) {
54                 CERROR("cannot find peer %s!\n", uuid->uuid);
55                 return NULL;
56         }
57
58         c = ptlrpc_get_connection(&peer, uuid);
59         if (c) {
60                 memcpy(c->c_remote_uuid.uuid,
61                        uuid->uuid, sizeof(c->c_remote_uuid.uuid));
62         }
63
64         CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
65
66         return c;
67 }
68
69 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
70                                  struct obd_uuid *uuid)
71 {
72         struct ptlrpc_peer peer;
73         int err;
74
75         err = ptlrpc_uuid_to_peer(uuid, &peer);
76         if (err != 0) {
77                 CERROR("cannot find peer %s!\n", uuid->uuid);
78                 return;
79         }
80
81         memcpy(&conn->c_peer, &peer, sizeof (peer));
82         return;
83 }
84
85 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
86 {
87         struct ptlrpc_bulk_desc *desc;
88
89         OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
90         if (!desc)
91                 return NULL;
92
93         spin_lock_init(&desc->bd_lock);
94         init_waitqueue_head(&desc->bd_waitq);
95         desc->bd_max_iov = npages;
96         desc->bd_iov_count = 0;
97         desc->bd_md_h = PTL_INVALID_HANDLE;
98         desc->bd_portal = portal;
99         desc->bd_type = type;
100         
101         return desc;
102 }
103
104 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
105                                                int npages, int type, int portal)
106 {
107         struct obd_import *imp = req->rq_import;
108         struct ptlrpc_bulk_desc *desc;
109
110         LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
111         desc = new_bulk(npages, type, portal);
112         if (desc == NULL)
113                 RETURN(NULL);
114
115         desc->bd_import_generation = req->rq_import_generation;
116         desc->bd_import = class_import_get(imp);
117         desc->bd_req = req;
118
119         desc->bd_cbid.cbid_fn  = client_bulk_callback;
120         desc->bd_cbid.cbid_arg = desc;
121
122         /* This makes req own desc, and free it when she frees herself */
123         req->rq_bulk = desc;
124
125         return desc;
126 }
127
128 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
129                                                int npages, int type, int portal)
130 {
131         struct obd_export *exp = req->rq_export;
132         struct ptlrpc_bulk_desc *desc;
133
134         LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
135
136         desc = new_bulk(npages, type, portal);
137         if (desc == NULL)
138                 RETURN(NULL);
139
140         desc->bd_export = class_export_get(exp);
141         desc->bd_req = req;
142
143         desc->bd_cbid.cbid_fn  = server_bulk_callback;
144         desc->bd_cbid.cbid_arg = desc;
145
146         /* NB we don't assign rq_bulk here; server-side requests are
147          * re-used, and the handler frees the bulk desc explicitly. */
148
149         return desc;
150 }
151
152 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
153                            struct page *page, int pageoffset, int len)
154 {
155         LASSERT(desc->bd_iov_count < desc->bd_max_iov);
156         LASSERT(page != NULL);
157         LASSERT(pageoffset >= 0);
158         LASSERT(len > 0);
159         LASSERT(pageoffset + len <= PAGE_SIZE);
160
161         desc->bd_nob += len;
162
163         pers_bulk_add_page(desc, page, pageoffset, len);
164 }
165
166 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
167 {
168         ENTRY;
169
170         LASSERT(desc != NULL);
171         LASSERT(desc->bd_iov_count != 0x5a5a5a5a); /* not freed already */
172         LASSERT(!desc->bd_network_rw);         /* network hands off or */
173         LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
174         if (desc->bd_export)
175                 class_export_put(desc->bd_export);
176         else
177                 class_import_put(desc->bd_import);
178
179         OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, 
180                                 bd_iov[desc->bd_max_iov]));
181         EXIT;
182 }
183
184 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
185                                        int count, int *lengths, char **bufs)
186 {
187         struct ptlrpc_request *request;
188         int rc;
189         ENTRY;
190
191         LASSERT((unsigned long)imp > 0x1000);
192
193         OBD_ALLOC(request, sizeof(*request));
194         if (!request) {
195                 CERROR("request allocation out of memory\n");
196                 RETURN(NULL);
197         }
198
199         rc = lustre_pack_request(request, count, lengths, bufs);
200         if (rc) {
201                 CERROR("cannot pack request %d\n", rc);
202                 OBD_FREE(request, sizeof(*request));
203                 RETURN(NULL);
204         }
205
206         if (imp->imp_server_timeout)
207                 request->rq_timeout = obd_timeout / 2;
208         else
209                 request->rq_timeout = obd_timeout;
210         request->rq_send_state = LUSTRE_IMP_FULL;
211         request->rq_type = PTL_RPC_MSG_REQUEST;
212         request->rq_import = class_import_get(imp);
213
214         request->rq_req_cbid.cbid_fn  = request_out_callback;
215         request->rq_req_cbid.cbid_arg = request;
216
217         request->rq_reply_cbid.cbid_fn  = reply_in_callback;
218         request->rq_reply_cbid.cbid_arg = request;
219         
220         request->rq_phase = RQ_PHASE_NEW;
221
222         /* XXX FIXME bug 249 */
223         request->rq_request_portal = imp->imp_client->cli_request_portal;
224         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
225
226         spin_lock_init(&request->rq_lock);
227         INIT_LIST_HEAD(&request->rq_list);
228         INIT_LIST_HEAD(&request->rq_replay_list);
229         INIT_LIST_HEAD(&request->rq_set_chain);
230         init_waitqueue_head(&request->rq_reply_waitq);
231         request->rq_xid = ptlrpc_next_xid();
232         atomic_set(&request->rq_refcount, 1);
233
234         request->rq_reqmsg->opc = opcode;
235         request->rq_reqmsg->flags = 0;
236
237         RETURN(request);
238 }
239
240 struct ptlrpc_request_set *ptlrpc_prep_set(void)
241 {
242         struct ptlrpc_request_set *set;
243
244         OBD_ALLOC(set, sizeof *set);
245         if (!set)
246                 RETURN(NULL);
247         INIT_LIST_HEAD(&set->set_requests);
248         init_waitqueue_head(&set->set_waitq);
249         set->set_remaining = 0;
250         spin_lock_init(&set->set_new_req_lock);
251         INIT_LIST_HEAD(&set->set_new_requests);
252
253         RETURN(set);
254 }
255
256 /* Finish with this set; opposite of prep_set. */
257 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
258 {
259         struct list_head *tmp;
260         struct list_head *next;
261         int               expected_phase;
262         int               n = 0;
263         ENTRY;
264
265         /* Requests on the set should either all be completed, or all be new */
266         expected_phase = (set->set_remaining == 0) ?
267                          RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
268         list_for_each (tmp, &set->set_requests) {
269                 struct ptlrpc_request *req =
270                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
271
272                 LASSERT(req->rq_phase == expected_phase);
273                 n++;
274         }
275
276         LASSERT(set->set_remaining == 0 || set->set_remaining == n);
277
278         list_for_each_safe(tmp, next, &set->set_requests) {
279                 struct ptlrpc_request *req =
280                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
281                 list_del_init(&req->rq_set_chain);
282
283                 LASSERT(req->rq_phase == expected_phase);
284
285                 if (req->rq_phase == RQ_PHASE_NEW) {
286
287                         if (req->rq_interpret_reply != NULL) {
288                                 int (*interpreter)(struct ptlrpc_request *,
289                                                    void *, int) =
290                                         req->rq_interpret_reply;
291
292                                 /* higher level (i.e. LOV) failed;
293                                  * let the sub reqs clean up */
294                                 req->rq_status = -EBADR;
295                                 interpreter(req, &req->rq_async_args,
296                                             req->rq_status);
297                         }
298                         set->set_remaining--;
299                 }
300
301                 req->rq_set = NULL;
302                 ptlrpc_req_finished (req);
303         }
304
305         LASSERT(set->set_remaining == 0);
306
307         OBD_FREE(set, sizeof(*set));
308         EXIT;
309 }
310
311 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
312                         struct ptlrpc_request *req)
313 {
314         /* The set takes over the caller's request reference */
315         list_add_tail(&req->rq_set_chain, &set->set_requests);
316         req->rq_set = set;
317         set->set_remaining++;
318         atomic_inc(&req->rq_import->imp_inflight);
319 }
320
321 /* lock so many callers can add things, the context that owns the set
322  * is supposed to notice these and move them into the set proper. */
323 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
324                             struct ptlrpc_request *req)
325 {
326         unsigned long flags;
327         spin_lock_irqsave(&set->set_new_req_lock, flags);
328         /* The set takes over the caller's request reference */
329         list_add_tail(&req->rq_set_chain, &set->set_new_requests);
330         req->rq_set = set;
331         spin_unlock_irqrestore(&set->set_new_req_lock, flags);
332 }
333
334 /*
335  * Based on the current state of the import, determine if the request
336  * can be sent, is an error, or should be delayed.
337  *
338  * Returns true if this request should be delayed. If false, and
339  * *status is set, then the request can not be sent and *status is the
340  * error code.  If false and status is 0, then request can be sent.
341  *
342  * The imp->imp_lock must be held.
343  */
344 static int ptlrpc_import_delay_req(struct obd_import *imp, 
345                                    struct ptlrpc_request *req, int *status)
346 {
347         int delay = 0;
348         ENTRY;
349
350         LASSERT (status != NULL);
351         *status = 0;
352
353         if (imp->imp_state == LUSTRE_IMP_NEW) {
354                 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
355                 *status = -EIO;
356                 LBUG();
357         }
358         else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
359                 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
360                 *status = -EIO;
361         }
362         /* allow CONNECT even if import is invalid */
363         else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
364                  imp->imp_state == LUSTRE_IMP_CONNECTING) {
365                 ;
366         }
367         /*
368          * If the import has been invalidated (such as by an OST failure), the
369          * request must fail with -EIO.  
370          */
371         else if (imp->imp_invalid) {
372                 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
373                 *status = -EIO;
374         } 
375         else if (req->rq_import_generation != imp->imp_generation) {
376                 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
377                 *status = -EIO;
378         } 
379         else if (req->rq_send_state != imp->imp_state) {
380                 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake 
381                     || req->rq_no_delay) 
382                         *status = -EWOULDBLOCK;
383                 else
384                         delay = 1;
385         }
386
387         RETURN(delay);
388 }
389
390 static int ptlrpc_check_reply(struct ptlrpc_request *req)
391 {
392         unsigned long flags;
393         int rc = 0;
394         ENTRY;
395
396         /* serialise with network callback */
397         spin_lock_irqsave (&req->rq_lock, flags);
398
399         if (req->rq_replied) {
400                 DEBUG_REQ(D_NET, req, "REPLIED:");
401                 GOTO(out, rc = 1);
402         }
403
404         if (req->rq_err) {
405                 DEBUG_REQ(D_ERROR, req, "ABORTED:");
406                 GOTO(out, rc = 1);
407         }
408
409         if (req->rq_resend) {
410                 DEBUG_REQ(D_ERROR, req, "RESEND:");
411                 GOTO(out, rc = 1);
412         }
413
414         if (req->rq_restart) {
415                 DEBUG_REQ(D_ERROR, req, "RESTART:");
416                 GOTO(out, rc = 1);
417         }
418         EXIT;
419  out:
420         spin_unlock_irqrestore (&req->rq_lock, flags);
421         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
422         return rc;
423 }
424
425 static int ptlrpc_check_status(struct ptlrpc_request *req)
426 {
427         int err;
428         ENTRY;
429
430         err = req->rq_repmsg->status;
431         if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
432                 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err == %d", 
433                           err);
434                 RETURN(err < 0 ? err : -EINVAL);
435         }
436
437         if (err < 0) {
438                 DEBUG_REQ(D_INFO, req, "status is %d", err);
439         } else if (err > 0) {
440                 /* XXX: translate this error from net to host */
441                 DEBUG_REQ(D_INFO, req, "status is %d", err);
442         }
443
444         RETURN(err);
445 }
446
447 static int after_reply(struct ptlrpc_request *req)
448 {
449         unsigned long flags;
450         struct obd_import *imp = req->rq_import;
451         int rc;
452         ENTRY;
453
454         LASSERT(!req->rq_receiving_reply);
455
456         /* NB Until this point, the whole of the incoming message,
457          * including buflens, status etc is in the sender's byte order. */
458
459 #if SWAB_PARANOIA
460         /* Clear reply swab mask; this is a new reply in sender's byte order */
461         req->rq_rep_swab_mask = 0;
462 #endif
463         LASSERT (req->rq_nob_received <= req->rq_replen);
464         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
465         if (rc) {
466                 CERROR("unpack_rep failed: %d\n", rc);
467                 RETURN(-EPROTO);
468         }
469
470         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
471             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
472                 CERROR("invalid packet type received (type=%u)\n",
473                        req->rq_repmsg->type);
474                 RETURN(-EPROTO);
475         }
476
477         /* Store transno in reqmsg for replay. */
478         req->rq_reqmsg->transno = req->rq_transno = req->rq_repmsg->transno;
479
480         rc = ptlrpc_check_status(req);
481
482         /* Either we've been evicted, or the server has failed for
483          * some reason. Try to reconnect, and if that fails, punt to the
484          * upcall. */
485         if (rc == -ENOTCONN) {
486                 if (req->rq_send_state != LUSTRE_IMP_FULL ||
487                     imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
488                         RETURN(-ENOTCONN);
489                 }
490
491                 ptlrpc_request_handle_notconn(req);
492
493                 RETURN(rc);
494         }
495
496         if (req->rq_import->imp_replayable) {
497                 spin_lock_irqsave(&imp->imp_lock, flags);
498                 if (req->rq_replay || req->rq_transno != 0)
499                         ptlrpc_retain_replayable_request(req, imp);
500                 else if (req->rq_commit_cb != NULL)
501                         req->rq_commit_cb(req);
502
503                 if (req->rq_transno > imp->imp_max_transno)
504                         imp->imp_max_transno = req->rq_transno;
505
506                 /* Replay-enabled imports return commit-status information. */
507                 if (req->rq_repmsg->last_committed)
508                         imp->imp_peer_committed_transno =
509                                 req->rq_repmsg->last_committed;
510                 ptlrpc_free_committed(imp);
511                 spin_unlock_irqrestore(&imp->imp_lock, flags);
512         }
513
514         RETURN(rc);
515 }
516
517 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
518 {
519         struct obd_import     *imp;
520         unsigned long          flags;
521         int rc;
522         ENTRY;
523
524         LASSERT(req->rq_phase == RQ_PHASE_NEW);
525         req->rq_phase = RQ_PHASE_RPC;
526
527         imp = req->rq_import;
528         spin_lock_irqsave(&imp->imp_lock, flags);
529
530         req->rq_import_generation = imp->imp_generation;
531
532         if (ptlrpc_import_delay_req(imp, req, &rc)) {
533                 spin_lock (&req->rq_lock);
534                 req->rq_waiting = 1;
535                 spin_unlock (&req->rq_lock);
536
537                 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
538                           "(%s != %s)",
539                           req->rq_reqmsg->status, 
540                           ptlrpc_import_state_name(req->rq_send_state),
541                           ptlrpc_import_state_name(imp->imp_state));
542                 LASSERT(list_empty (&req->rq_list));
543
544                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
545                 spin_unlock_irqrestore(&imp->imp_lock, flags);
546                 RETURN(0);
547         }
548
549         if (rc != 0) {
550                 spin_unlock_irqrestore(&imp->imp_lock, flags);
551                 req->rq_status = rc;
552                 req->rq_phase = RQ_PHASE_INTERPRET;
553                 RETURN(rc);
554         }
555
556         /* XXX this is the same as ptlrpc_queue_wait */
557         LASSERT(list_empty(&req->rq_list));
558         list_add_tail(&req->rq_list, &imp->imp_sending_list);
559         spin_unlock_irqrestore(&imp->imp_lock, flags);
560
561         req->rq_reqmsg->status = current->pid;
562         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc"
563                " %s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
564                imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
565                req->rq_xid,
566                imp->imp_connection->c_peer.peer_ni->pni_name,
567                imp->imp_connection->c_peer.peer_nid,
568                req->rq_reqmsg->opc);
569
570         rc = ptl_send_rpc(req);
571         if (rc) {
572                 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
573                 req->rq_timeout = 1;
574                 RETURN(rc);
575         }
576         RETURN(0);
577 }
578
579 int ptlrpc_check_set(struct ptlrpc_request_set *set)
580 {
581         unsigned long flags;
582         struct list_head *tmp;
583         int force_timer_recalc = 0;
584         ENTRY;
585
586         if (set->set_remaining == 0)
587                 RETURN(1);
588
589         list_for_each(tmp, &set->set_requests) {
590                 struct ptlrpc_request *req =
591                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
592                 struct obd_import *imp = req->rq_import;
593                 int rc = 0;
594
595                 if (req->rq_phase == RQ_PHASE_NEW &&
596                     ptlrpc_send_new_req(req)) {
597                         force_timer_recalc = 1;
598                 }
599
600                 if (!(req->rq_phase == RQ_PHASE_RPC ||
601                       req->rq_phase == RQ_PHASE_BULK ||
602                       req->rq_phase == RQ_PHASE_INTERPRET ||
603                       req->rq_phase == RQ_PHASE_COMPLETE)) {
604                         DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
605                         LBUG();
606                 }
607
608                 if (req->rq_phase == RQ_PHASE_COMPLETE)
609                         continue;
610
611                 if (req->rq_phase == RQ_PHASE_INTERPRET)
612                         GOTO(interpret, req->rq_status);
613
614                 if (req->rq_err) {
615                         ptlrpc_unregister_reply(req);
616                         if (req->rq_status == 0)
617                                 req->rq_status = -EIO;
618                         req->rq_phase = RQ_PHASE_INTERPRET;
619
620                         spin_lock_irqsave(&imp->imp_lock, flags);
621                         list_del_init(&req->rq_list);
622                         spin_unlock_irqrestore(&imp->imp_lock, flags);
623
624                         GOTO(interpret, req->rq_status);
625                 }
626
627                 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
628                  * will only be set after rq_timedout, but the oig waiting
629                  * path sets rq_intr irrespective of whether ptlrpcd has
630                  * seen a timeout.  our policy is to only interpret 
631                  * interrupted rpcs after they have timed out */
632                 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
633                         /* NB could be on delayed list */
634                         ptlrpc_unregister_reply(req);
635                         req->rq_status = -EINTR;
636                         req->rq_phase = RQ_PHASE_INTERPRET;
637
638                         spin_lock_irqsave(&imp->imp_lock, flags);
639                         list_del_init(&req->rq_list);
640                         spin_unlock_irqrestore(&imp->imp_lock, flags);
641
642                         GOTO(interpret, req->rq_status);
643                 }
644
645                 if (req->rq_phase == RQ_PHASE_RPC) {
646                         if (req->rq_waiting || req->rq_resend) {
647                                 int status;
648
649                                 spin_lock_irqsave(&imp->imp_lock, flags);
650
651                                 if (ptlrpc_import_delay_req(imp, req, &status)){
652                                         spin_unlock_irqrestore(&imp->imp_lock,
653                                                                flags);
654                                         continue;
655                                 } 
656
657                                 list_del_init(&req->rq_list);
658                                 if (status != 0)  {
659                                         req->rq_status = status;
660                                         req->rq_phase = RQ_PHASE_INTERPRET;
661                                         spin_unlock_irqrestore(&imp->imp_lock,
662                                                                flags);
663                                         GOTO(interpret, req->rq_status);
664                                 }
665                                 if (req->rq_no_resend) {
666                                         req->rq_status = -ENOTCONN;
667                                         req->rq_phase = RQ_PHASE_INTERPRET;
668                                         spin_unlock_irqrestore(&imp->imp_lock,
669                                                                flags);
670                                         GOTO(interpret, req->rq_status);
671                                 }
672                                 list_add_tail(&req->rq_list,
673                                               &imp->imp_sending_list);
674
675                                 spin_unlock_irqrestore(&imp->imp_lock, flags);
676
677                                 req->rq_waiting = 0;
678                                 if (req->rq_resend) {
679                                         lustre_msg_add_flags(req->rq_reqmsg,
680                                                              MSG_RESENT);
681                                         ptlrpc_unregister_reply(req);
682                                         if (req->rq_bulk) {
683                                                 __u64 old_xid = req->rq_xid;
684
685                                                 ptlrpc_unregister_bulk (req);
686
687                                                 /* ensure previous bulk fails */
688                                                 req->rq_xid = ptlrpc_next_xid();
689                                                 CDEBUG(D_HA, "resend bulk "
690                                                        "old x"LPU64
691                                                        " new x"LPU64"\n",
692                                                        old_xid, req->rq_xid);
693                                         }
694                                 }
695
696                                 rc = ptl_send_rpc(req);
697                                 if (rc) {
698                                         DEBUG_REQ(D_HA, req, "send failed (%d)",
699                                                   rc);
700                                         force_timer_recalc = 1;
701                                         req->rq_timeout = 0;
702                                 }
703                                 /* need to reset the timeout */
704                                 force_timer_recalc = 1;
705                         }
706
707                         /* Still waiting for a reply? */
708                         if (ptlrpc_client_receiving_reply(req))
709                                 continue;
710
711                         /* Did we actually receive a reply? */
712                         if (!ptlrpc_client_replied(req))
713                                 continue;
714
715                         spin_lock_irqsave(&imp->imp_lock, flags);
716                         list_del_init(&req->rq_list);
717                         spin_unlock_irqrestore(&imp->imp_lock, flags);
718
719                         req->rq_status = after_reply(req);
720                         if (req->rq_resend) {
721                                 /* Add this req to the delayed list so
722                                    it can be errored if the import is
723                                    evicted after recovery. */
724                                 spin_lock_irqsave (&req->rq_lock, flags);
725                                 list_add_tail(&req->rq_list, 
726                                               &imp->imp_delayed_list);
727                                 spin_unlock_irqrestore(&req->rq_lock, flags);
728                                 continue;
729                         }
730
731                         /* If there is no bulk associated with this request,
732                          * then we're done and should let the interpreter
733                          * process the reply.  Similarly if the RPC returned
734                          * an error, and therefore the bulk will never arrive.
735                          */
736                         if (req->rq_bulk == NULL || req->rq_status != 0) {
737                                 req->rq_phase = RQ_PHASE_INTERPRET;
738                                 GOTO(interpret, req->rq_status);
739                         }
740
741                         req->rq_phase = RQ_PHASE_BULK;
742                 }
743
744                 LASSERT(req->rq_phase == RQ_PHASE_BULK);
745                 if (ptlrpc_bulk_active(req->rq_bulk))
746                         continue;
747
748                 if (!req->rq_bulk->bd_success) {
749                         /* The RPC reply arrived OK, but the bulk screwed
750                          * up!  Dead wierd since the server told us the RPC
751                          * was good after getting the REPLY for her GET or
752                          * the ACK for her PUT. */
753                         DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
754                         LBUG();
755                 }
756
757                 req->rq_phase = RQ_PHASE_INTERPRET;
758
759         interpret:
760                 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
761                 LASSERT(!req->rq_receiving_reply);
762
763                 ptlrpc_unregister_reply(req);
764                 if (req->rq_bulk != NULL)
765                         ptlrpc_unregister_bulk (req);
766
767                 req->rq_phase = RQ_PHASE_COMPLETE;
768
769                 if (req->rq_interpret_reply != NULL) {
770                         int (*interpreter)(struct ptlrpc_request *,void *,int) =
771                                 req->rq_interpret_reply;
772                         req->rq_status = interpreter(req, &req->rq_async_args,
773                                                      req->rq_status);
774                 }
775
776                 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:"
777                        "opc %s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
778                        imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
779                        req->rq_xid,
780                        imp->imp_connection->c_peer.peer_ni->pni_name,
781                        imp->imp_connection->c_peer.peer_nid,
782                        req->rq_reqmsg->opc);
783
784                 set->set_remaining--;
785
786                 atomic_dec(&imp->imp_inflight);
787                 wake_up(&imp->imp_recovery_waitq);
788         }
789
790         /* If we hit an error, we want to recover promptly. */
791         RETURN(set->set_remaining == 0 || force_timer_recalc);
792 }
793
794 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
795 {
796         unsigned long      flags;
797         struct obd_import *imp = req->rq_import;
798         ENTRY;
799
800         DEBUG_REQ(D_ERROR, req, "timeout");
801
802         spin_lock_irqsave (&req->rq_lock, flags);
803         req->rq_timedout = 1;
804         spin_unlock_irqrestore (&req->rq_lock, flags);
805
806         ptlrpc_unregister_reply (req);
807
808         if (req->rq_bulk != NULL)
809                 ptlrpc_unregister_bulk (req);
810
811         if (imp == NULL) {
812                 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
813                 RETURN(1);
814         }
815
816         /* The DLM server doesn't want recovery run on its imports. */
817         if (imp->imp_dlm_fake)
818                 RETURN(1);
819
820         /* If this request is for recovery or other primordial tasks,
821          * then error it out here. */
822         if (req->rq_send_state != LUSTRE_IMP_FULL || 
823             imp->imp_obd->obd_no_recov) {
824                 spin_lock_irqsave (&req->rq_lock, flags);
825                 req->rq_status = -ETIMEDOUT;
826                 req->rq_err = 1;
827                 spin_unlock_irqrestore (&req->rq_lock, flags);
828                 RETURN(1);
829         }
830
831         ptlrpc_fail_import(imp, req->rq_import_generation);
832
833         RETURN(0);
834 }
835
836 int ptlrpc_expired_set(void *data)
837 {
838         struct ptlrpc_request_set *set = data;
839         struct list_head          *tmp;
840         time_t                     now = LTIME_S (CURRENT_TIME);
841         ENTRY;
842
843         LASSERT(set != NULL);
844
845         /* A timeout expired; see which reqs it applies to... */
846         list_for_each (tmp, &set->set_requests) {
847                 struct ptlrpc_request *req =
848                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
849
850                 /* request in-flight? */
851                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting 
852                        && !req->rq_resend) ||
853                       (req->rq_phase == RQ_PHASE_BULK)))
854                         continue;
855
856                 if (req->rq_timedout ||           /* already dealt with */
857                     req->rq_sent + req->rq_timeout > now) /* not expired */
858                         continue;
859
860                 /* deal with this guy */
861                 ptlrpc_expire_one_request (req);
862         }
863
864         /* When waiting for a whole set, we always to break out of the
865          * sleep so we can recalculate the timeout, or enable interrupts
866          * iff everyone's timed out.
867          */
868         RETURN(1);
869 }
870
871 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
872 {
873         unsigned long flags;
874         spin_lock_irqsave(&req->rq_lock, flags);
875         req->rq_intr = 1;
876         spin_unlock_irqrestore(&req->rq_lock, flags);
877 }
878
879 void ptlrpc_interrupted_set(void *data)
880 {
881         struct ptlrpc_request_set *set = data;
882         struct list_head *tmp;
883
884         LASSERT(set != NULL);
885         CERROR("INTERRUPTED SET %p\n", set);
886
887         list_for_each(tmp, &set->set_requests) {
888                 struct ptlrpc_request *req =
889                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
890
891                 if (req->rq_phase != RQ_PHASE_RPC)
892                         continue;
893
894                 ptlrpc_mark_interrupted(req);
895         }
896 }
897
898 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
899 {
900         struct list_head      *tmp;
901         time_t                 now = LTIME_S(CURRENT_TIME);
902         time_t                 deadline;
903         int                    timeout = 0;
904         struct ptlrpc_request *req;
905         ENTRY;
906
907         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
908
909         list_for_each(tmp, &set->set_requests) {
910                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
911
912                 /* request in-flight? */
913                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
914                       (req->rq_phase == RQ_PHASE_BULK)))
915                         continue;
916
917                 if (req->rq_timedout)   /* already timed out */
918                         continue;
919
920                 deadline = req->rq_sent + req->rq_timeout;
921                 if (deadline <= now)    /* actually expired already */
922                         timeout = 1;    /* ASAP */
923                 else if (timeout == 0 || timeout > deadline - now)
924                         timeout = deadline - now;
925         }
926         RETURN(timeout);
927 }
928                 
929
930 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
931 {
932         struct list_head      *tmp;
933         struct ptlrpc_request *req;
934         struct l_wait_info     lwi;
935         int                    rc, timeout;
936         ENTRY;
937
938         LASSERT(!list_empty(&set->set_requests));
939         list_for_each(tmp, &set->set_requests) {
940                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
941                 if (req->rq_phase == RQ_PHASE_NEW)
942                         (void)ptlrpc_send_new_req(req);
943         }
944
945         do {
946                 timeout = ptlrpc_set_next_timeout(set);
947
948                 /* wait until all complete, interrupted, or an in-flight
949                  * req times out */
950                 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
951                        set, timeout);
952                 lwi = LWI_TIMEOUT_INTR((timeout ? timeout : 1) * HZ,
953                                        ptlrpc_expired_set, 
954                                        ptlrpc_interrupted_set, set);
955                 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
956
957                 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
958
959                 /* -EINTR => all requests have been flagged rq_intr so next
960                  * check completes.
961                  * -ETIMEOUTD => someone timed out.  When all reqs have
962                  * timed out, signals are enabled allowing completion with
963                  * EINTR.
964                  * I don't really care if we go once more round the loop in
965                  * the error cases -eeb. */
966         } while (rc != 0);
967
968         LASSERT(set->set_remaining == 0);
969
970         rc = 0;
971         list_for_each(tmp, &set->set_requests) {
972                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
973
974                 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
975                 if (req->rq_status != 0)
976                         rc = req->rq_status;
977         }
978
979         if (set->set_interpret != NULL) {
980                 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
981                         set->set_interpret;
982                 rc = interpreter (set, &set->set_args, rc);
983         }
984
985         RETURN(rc);
986 }
987
988 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
989 {
990         ENTRY;
991         if (request == NULL) {
992                 EXIT;
993                 return;
994         }
995
996         LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
997         LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
998         LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
999         LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1000
1001         /* We must take it off the imp_replay_list first.  Otherwise, we'll set
1002          * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1003         if (request->rq_import != NULL) {
1004                 unsigned long flags = 0;
1005                 if (!locked)
1006                         spin_lock_irqsave(&request->rq_import->imp_lock, flags);
1007                 list_del_init(&request->rq_replay_list);
1008                 if (!locked)
1009                         spin_unlock_irqrestore(&request->rq_import->imp_lock,
1010                                                flags);
1011         }
1012         LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1013
1014         if (atomic_read(&request->rq_refcount) != 0) {
1015                 DEBUG_REQ(D_ERROR, request,
1016                           "freeing request with nonzero refcount");
1017                 LBUG();
1018         }
1019
1020         if (request->rq_repmsg != NULL) {
1021                 OBD_FREE(request->rq_repmsg, request->rq_replen);
1022                 request->rq_repmsg = NULL;
1023         }
1024         if (request->rq_reqmsg != NULL) {
1025                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
1026                 request->rq_reqmsg = NULL;
1027         }
1028         if (request->rq_export != NULL) {
1029                 class_export_put(request->rq_export);
1030                 request->rq_export = NULL;
1031         }
1032         if (request->rq_import != NULL) {
1033                 class_import_put(request->rq_import);
1034                 request->rq_import = NULL;
1035         }
1036         if (request->rq_bulk != NULL)
1037                 ptlrpc_free_bulk(request->rq_bulk);
1038
1039         OBD_FREE(request, sizeof(*request));
1040         EXIT;
1041 }
1042
1043 void ptlrpc_free_req(struct ptlrpc_request *request)
1044 {
1045         __ptlrpc_free_req(request, 0);
1046 }
1047
1048 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1049 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1050 {
1051         LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1052         (void)__ptlrpc_req_finished(request, 1);
1053 }
1054
1055 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1056 {
1057         ENTRY;
1058         if (request == NULL)
1059                 RETURN(1);
1060
1061         if (request == (void *)(unsigned long)(0x5a5a5a5a5a5a5a5a) ||
1062             request->rq_reqmsg == (void *)(unsigned long)(0x5a5a5a5a5a5a5a5a)) {
1063                 CERROR("dereferencing freed request (bug 575)\n");
1064                 LBUG();
1065                 RETURN(1);
1066         }
1067
1068         DEBUG_REQ(D_INFO, request, "refcount now %u",
1069                   atomic_read(&request->rq_refcount) - 1);
1070
1071         if (atomic_dec_and_test(&request->rq_refcount)) {
1072                 __ptlrpc_free_req(request, locked);
1073                 RETURN(1);
1074         }
1075
1076         RETURN(0);
1077 }
1078
1079 void ptlrpc_req_finished(struct ptlrpc_request *request)
1080 {
1081         __ptlrpc_req_finished(request, 0);
1082 }
1083
1084 /* Disengage the client's reply buffer from the network
1085  * NB does _NOT_ unregister any client-side bulk.
1086  * IDEMPOTENT, but _not_ safe against concurrent callers.
1087  * The request owner (i.e. the thread doing the I/O) must call...
1088  */
1089 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1090 {
1091         int                rc;
1092         wait_queue_head_t *wq;
1093         struct l_wait_info lwi;
1094
1095         LASSERT(!in_interrupt ());             /* might sleep */
1096
1097         if (!ptlrpc_client_receiving_reply(request))
1098                 return;
1099
1100         rc = PtlMDUnlink (request->rq_reply_md_h);
1101         if (rc == PTL_MD_INVALID) {
1102                 LASSERT (!ptlrpc_client_receiving_reply(request));
1103                 return;
1104         }
1105         
1106         LASSERT (rc == PTL_OK);
1107
1108         if (request->rq_set == NULL)
1109                 wq = &request->rq_set->set_waitq;
1110         else
1111                 wq = &request->rq_reply_waitq;
1112
1113         for (;;) {
1114                 /* Network access will complete in finite time but the HUGE
1115                  * timeout lets us CWARN for visibility of sluggish NALs */
1116                 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
1117                 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1118                 if (rc == 0)
1119                         return;
1120
1121                 LASSERT (rc == -ETIMEDOUT);
1122                 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1123         }
1124 }
1125
1126 /* caller must hold imp->imp_lock */
1127 void ptlrpc_free_committed(struct obd_import *imp)
1128 {
1129         struct list_head *tmp, *saved;
1130         struct ptlrpc_request *req;
1131         struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1132         ENTRY;
1133
1134         LASSERT(imp != NULL);
1135
1136         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1137
1138         CDEBUG(D_HA, "%s: committing for last_committed "LPU64"\n",
1139                imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1140
1141         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1142                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1143
1144                 /* XXX ok to remove when 1357 resolved - rread 05/29/03  */
1145                 LASSERT(req != last_req);
1146                 last_req = req;
1147
1148                 if (req->rq_import_generation < imp->imp_generation) {
1149                         DEBUG_REQ(D_HA, req, "freeing request with old gen");
1150                         GOTO(free_req, 0);
1151                 }
1152
1153                 if (req->rq_replay) {
1154                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1155                         continue;
1156                 }
1157
1158                 /* not yet committed */
1159                 if (req->rq_transno > imp->imp_peer_committed_transno) {
1160                         DEBUG_REQ(D_HA, req, "stopping search");
1161                         break;
1162                 }
1163
1164                 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1165                           imp->imp_peer_committed_transno);
1166 free_req:
1167                 if (req->rq_commit_cb != NULL)
1168                         req->rq_commit_cb(req);
1169                 list_del_init(&req->rq_replay_list);
1170                 __ptlrpc_req_finished(req, 1);
1171         }
1172
1173         EXIT;
1174         return;
1175 }
1176
1177 void ptlrpc_cleanup_client(struct obd_import *imp)
1178 {
1179         ENTRY;
1180         EXIT;
1181         return;
1182 }
1183
1184 void ptlrpc_resend_req(struct ptlrpc_request *req)
1185 {
1186         unsigned long flags;
1187
1188         DEBUG_REQ(D_HA, req, "going to resend");
1189         req->rq_reqmsg->handle.cookie = 0;
1190         req->rq_status = -EAGAIN;
1191
1192         spin_lock_irqsave (&req->rq_lock, flags);
1193         req->rq_resend = 1;
1194         req->rq_timedout = 0;
1195         if (req->rq_bulk) {
1196                 __u64 old_xid = req->rq_xid;
1197                 
1198                 /* ensure previous bulk fails */
1199                 req->rq_xid = ptlrpc_next_xid();
1200                 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1201                        old_xid, req->rq_xid);
1202         }
1203         ptlrpc_wake_client_req(req);
1204         spin_unlock_irqrestore (&req->rq_lock, flags);
1205 }
1206
1207 /* XXX: this function and rq_status are currently unused */
1208 void ptlrpc_restart_req(struct ptlrpc_request *req)
1209 {
1210         unsigned long flags;
1211
1212         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1213         req->rq_status = -ERESTARTSYS;
1214
1215         spin_lock_irqsave (&req->rq_lock, flags);
1216         req->rq_restart = 1;
1217         req->rq_timedout = 0;
1218         ptlrpc_wake_client_req(req);
1219         spin_unlock_irqrestore (&req->rq_lock, flags);
1220 }
1221
1222 static int expired_request(void *data)
1223 {
1224         struct ptlrpc_request *req = data;
1225         ENTRY;
1226
1227         RETURN(ptlrpc_expire_one_request(req));
1228 }
1229
1230 static void interrupted_request(void *data)
1231 {
1232         unsigned long flags;
1233
1234         struct ptlrpc_request *req = data;
1235         DEBUG_REQ(D_HA, req, "request interrupted");
1236         spin_lock_irqsave (&req->rq_lock, flags);
1237         req->rq_intr = 1;
1238         spin_unlock_irqrestore (&req->rq_lock, flags);
1239 }
1240
1241 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1242 {
1243         ENTRY;
1244         atomic_inc(&req->rq_refcount);
1245         RETURN(req);
1246 }
1247
1248 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1249                                       struct obd_import *imp)
1250 {
1251         struct list_head *tmp;
1252
1253         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1254
1255         /* don't re-add requests that have been replayed */
1256         if (!list_empty(&req->rq_replay_list))
1257                 return;
1258
1259         lustre_msg_add_flags(req->rq_reqmsg,
1260                              MSG_REPLAY);
1261
1262         LASSERT(imp->imp_replayable);
1263         /* Balanced in ptlrpc_free_committed, usually. */
1264         ptlrpc_request_addref(req);
1265         list_for_each_prev(tmp, &imp->imp_replay_list) {
1266                 struct ptlrpc_request *iter =
1267                         list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1268
1269                 /* We may have duplicate transnos if we create and then
1270                  * open a file, or for closes retained if to match creating
1271                  * opens, so use req->rq_xid as a secondary key.
1272                  * (See bugs 684, 685, and 428.)
1273                  * XXX no longer needed, but all opens need transnos!
1274                  */
1275                 if (iter->rq_transno > req->rq_transno)
1276                         continue;
1277
1278                 if (iter->rq_transno == req->rq_transno) {
1279                         LASSERT(iter->rq_xid != req->rq_xid);
1280                         if (iter->rq_xid > req->rq_xid)
1281                                 continue;
1282                 }
1283
1284                 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1285                 return;
1286         }
1287
1288         list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1289 }
1290
1291 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1292 {
1293         int rc = 0;
1294         int brc;
1295         struct l_wait_info lwi;
1296         struct obd_import *imp = req->rq_import;
1297         unsigned long flags;
1298         int timeout = 0;
1299         ENTRY;
1300
1301         LASSERT(req->rq_set == NULL);
1302         LASSERT(!req->rq_receiving_reply);
1303         atomic_inc(&imp->imp_inflight);
1304
1305         /* for distributed debugging */
1306         req->rq_reqmsg->status = current->pid;
1307         LASSERT(imp->imp_obd != NULL);
1308         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc "
1309                "%s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
1310                imp->imp_obd->obd_uuid.uuid,
1311                req->rq_reqmsg->status, req->rq_xid,
1312                imp->imp_connection->c_peer.peer_ni->pni_name,
1313                imp->imp_connection->c_peer.peer_nid,
1314                req->rq_reqmsg->opc);
1315
1316         /* Mark phase here for a little debug help */
1317         req->rq_phase = RQ_PHASE_RPC;
1318
1319         spin_lock_irqsave(&imp->imp_lock, flags);
1320         req->rq_import_generation = imp->imp_generation;
1321 restart:
1322         if (ptlrpc_import_delay_req(imp, req, &rc)) {
1323                 list_del(&req->rq_list);
1324
1325                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1326                 spin_unlock_irqrestore(&imp->imp_lock, flags);
1327
1328                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1329                           current->comm, 
1330                           ptlrpc_import_state_name(req->rq_send_state), 
1331                           ptlrpc_import_state_name(imp->imp_state));
1332                 lwi = LWI_INTR(interrupted_request, req);
1333                 rc = l_wait_event(req->rq_reply_waitq,
1334                                   (req->rq_send_state == imp->imp_state ||
1335                                    req->rq_err),
1336                                   &lwi);
1337                 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d == 1)",
1338                           current->comm, 
1339                           ptlrpc_import_state_name(imp->imp_state), 
1340                           ptlrpc_import_state_name(req->rq_send_state),
1341                           req->rq_err);
1342
1343                 spin_lock_irqsave(&imp->imp_lock, flags);
1344                 list_del_init(&req->rq_list);
1345
1346                 if (req->rq_err) {
1347                         rc = -EIO;
1348                 } 
1349                 else if (req->rq_intr) {
1350                         rc = -EINTR;
1351                 }
1352                 else if (req->rq_no_resend) {
1353                         spin_unlock_irqrestore(&imp->imp_lock, flags);
1354                         GOTO(out, rc = -ETIMEDOUT);
1355                 }
1356                 else {
1357                         GOTO(restart, rc);
1358                 }
1359         } 
1360
1361         if (rc != 0) {
1362                 list_del_init(&req->rq_list);
1363                 spin_unlock_irqrestore(&imp->imp_lock, flags);
1364                 req->rq_status = rc; // XXX this ok?
1365                 GOTO(out, rc);
1366         }
1367
1368         if (req->rq_resend) {
1369                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1370
1371                 if (req->rq_bulk != NULL)
1372                         ptlrpc_unregister_bulk (req);
1373
1374                 DEBUG_REQ(D_HA, req, "resending: ");
1375         }
1376
1377         /* XXX this is the same as ptlrpc_set_wait */
1378         LASSERT(list_empty(&req->rq_list));
1379         list_add_tail(&req->rq_list, &imp->imp_sending_list);
1380         spin_unlock_irqrestore(&imp->imp_lock, flags);
1381
1382         rc = ptl_send_rpc(req);
1383         if (rc) {
1384                 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1385                 timeout = 1;
1386         } else {
1387                 timeout = MAX(req->rq_timeout * HZ, 1);
1388                 DEBUG_REQ(D_NET, req, "-- sleeping");
1389         }
1390         lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1391                                req);
1392         l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1393         DEBUG_REQ(D_NET, req, "-- done sleeping");
1394
1395         CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:opc "
1396                "%s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
1397                imp->imp_obd->obd_uuid.uuid,
1398                req->rq_reqmsg->status, req->rq_xid,
1399                imp->imp_connection->c_peer.peer_ni->pni_name,
1400                imp->imp_connection->c_peer.peer_nid,
1401                req->rq_reqmsg->opc);
1402
1403         spin_lock_irqsave(&imp->imp_lock, flags);
1404         list_del_init(&req->rq_list);
1405         spin_unlock_irqrestore(&imp->imp_lock, flags);
1406
1407         /* If the reply was received normally, this just grabs the spinlock
1408          * (ensuring the reply callback has returned), sees that
1409          * req->rq_receiving_reply is clear and returns. */
1410         ptlrpc_unregister_reply (req);
1411
1412         if (req->rq_err)
1413                 GOTO(out, rc = -EIO);
1414
1415         /* Resend if we need to, unless we were interrupted. */
1416         if (req->rq_resend && !req->rq_intr) {
1417                 /* ...unless we were specifically told otherwise. */
1418                 if (req->rq_no_resend)
1419                         GOTO(out, rc = -ETIMEDOUT);
1420                 spin_lock_irqsave(&imp->imp_lock, flags);
1421                 goto restart;
1422         }
1423
1424         if (req->rq_intr) {
1425                 /* Should only be interrupted if we timed out. */
1426                 if (!req->rq_timedout)
1427                         DEBUG_REQ(D_ERROR, req,
1428                                   "rq_intr set but rq_timedout not");
1429                 GOTO(out, rc = -EINTR);
1430         }
1431
1432         if (req->rq_timedout) {                 /* non-recoverable timeout */
1433                 GOTO(out, rc = -ETIMEDOUT);
1434         }
1435
1436         if (!req->rq_replied) {
1437                 /* How can this be? -eeb */
1438                 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1439                 LBUG();
1440                 GOTO(out, rc = req->rq_status);
1441         }
1442
1443         rc = after_reply (req);
1444         /* NB may return +ve success rc */
1445         if (req->rq_resend) {
1446                 spin_lock_irqsave(&imp->imp_lock, flags);
1447                 goto restart;
1448         }
1449
1450  out:
1451         if (req->rq_bulk != NULL) {
1452                 if (rc >= 0) {                  
1453                         /* success so far.  Note that anything going wrong
1454                          * with bulk now, is EXTREMELY strange, since the
1455                          * server must have believed that the bulk
1456                          * tranferred OK before she replied with success to
1457                          * me. */
1458                         lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1459                         brc = l_wait_event(req->rq_reply_waitq,
1460                                            !ptlrpc_bulk_active(req->rq_bulk),
1461                                            &lwi);
1462                         LASSERT(brc == 0 || brc == -ETIMEDOUT);
1463                         if (brc != 0) {
1464                                 LASSERT(brc == -ETIMEDOUT);
1465                                 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1466                                 rc = brc;
1467                         } else if (!req->rq_bulk->bd_success) {
1468                                 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1469                                 rc = -EIO;
1470                         }
1471                 }
1472                 if (rc < 0)
1473                         ptlrpc_unregister_bulk (req);
1474         }
1475
1476         LASSERT(!req->rq_receiving_reply);
1477         req->rq_phase = RQ_PHASE_INTERPRET;
1478
1479         atomic_dec(&imp->imp_inflight);
1480         wake_up(&imp->imp_recovery_waitq);
1481         RETURN(rc);
1482 }
1483
1484 struct ptlrpc_replay_async_args {
1485         int praa_old_state;
1486         int praa_old_status;
1487 };
1488
1489 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1490                                     void * data, int rc)
1491 {
1492         struct ptlrpc_replay_async_args *aa = data;
1493         struct obd_import *imp = req->rq_import;
1494         unsigned long flags;
1495
1496         atomic_dec(&imp->imp_replay_inflight);
1497         
1498         if (!req->rq_replied) {
1499                 CERROR("request replay timed out, restarting recovery\n");
1500                 GOTO(out, rc = -ETIMEDOUT);
1501         }
1502
1503 #if SWAB_PARANOIA
1504         /* Clear reply swab mask; this is a new reply in sender's byte order */
1505         req->rq_rep_swab_mask = 0;
1506 #endif
1507         LASSERT (req->rq_nob_received <= req->rq_replen);
1508         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
1509         if (rc) {
1510                 CERROR("unpack_rep failed: %d\n", rc);
1511                 GOTO(out, rc = -EPROTO);
1512         }
1513
1514         if (req->rq_repmsg->type == PTL_RPC_MSG_ERR && 
1515             req->rq_repmsg->status == -ENOTCONN) 
1516                 GOTO(out, rc = req->rq_repmsg->status);
1517
1518         /* The transno had better not change over replay. */
1519         LASSERT(req->rq_reqmsg->transno == req->rq_repmsg->transno);
1520
1521         DEBUG_REQ(D_HA, req, "got rep");
1522
1523         /* let the callback do fixups, possibly including in the request */
1524         if (req->rq_replay_cb)
1525                 req->rq_replay_cb(req);
1526
1527         if (req->rq_replied && req->rq_repmsg->status != aa->praa_old_status) {
1528                 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1529                           req->rq_repmsg->status, aa->praa_old_status);
1530         } else {
1531                 /* Put it back for re-replay. */
1532                 req->rq_repmsg->status = aa->praa_old_status;
1533         }
1534
1535         spin_lock_irqsave(&imp->imp_lock, flags);
1536         imp->imp_last_replay_transno = req->rq_transno;
1537         spin_unlock_irqrestore(&imp->imp_lock, flags);
1538
1539         /* continue with recovery */
1540         rc = ptlrpc_import_recovery_state_machine(imp);
1541  out:
1542         req->rq_send_state = aa->praa_old_state;
1543         
1544         if (rc != 0)
1545                 /* this replay failed, so restart recovery */
1546                 ptlrpc_connect_import(imp, NULL);
1547
1548         RETURN(rc);
1549 }
1550
1551
1552 int ptlrpc_replay_req(struct ptlrpc_request *req)
1553 {
1554         struct ptlrpc_replay_async_args *aa;
1555         ENTRY;
1556
1557         LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1558
1559         /* Not handling automatic bulk replay yet (or ever?) */
1560         LASSERT(req->rq_bulk == NULL);
1561
1562         DEBUG_REQ(D_HA, req, "REPLAY");
1563
1564         LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1565         aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1566         memset(aa, 0, sizeof *aa);
1567
1568         /* Prepare request to be resent with ptlrpcd */
1569         aa->praa_old_state = req->rq_send_state;
1570         req->rq_send_state = LUSTRE_IMP_REPLAY;
1571         req->rq_phase = RQ_PHASE_NEW;
1572         /*
1573          * Q: "How can a req get on the replay list if it wasn't replied?"
1574          * A: "If we failed during the replay of this request, it will still
1575          *     be on the list, but rq_replied will have been reset to 0."
1576          */
1577         if (req->rq_replied) {
1578                 aa->praa_old_status = req->rq_repmsg->status;
1579                 req->rq_status = 0;
1580                 req->rq_replied = 0;
1581         }
1582
1583         req->rq_interpret_reply = ptlrpc_replay_interpret;
1584         atomic_inc(&req->rq_import->imp_replay_inflight);
1585         ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1586
1587         ptlrpcd_add_req(req);
1588         RETURN(0);
1589 }
1590
1591 void ptlrpc_abort_inflight(struct obd_import *imp)
1592 {
1593         unsigned long flags;
1594         struct list_head *tmp, *n;
1595         ENTRY;
1596
1597         /* Make sure that no new requests get processed for this import.
1598          * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1599          * this flag and then putting requests on sending_list or delayed_list.
1600          */
1601         spin_lock_irqsave(&imp->imp_lock, flags);
1602
1603         /* XXX locking?  Maybe we should remove each request with the list
1604          * locked?  Also, how do we know if the requests on the list are
1605          * being freed at this time?
1606          */
1607         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1608                 struct ptlrpc_request *req =
1609                         list_entry(tmp, struct ptlrpc_request, rq_list);
1610
1611                 DEBUG_REQ(D_HA, req, "inflight");
1612
1613                 spin_lock (&req->rq_lock);
1614                 if (req->rq_import_generation < imp->imp_generation) {
1615                         req->rq_err = 1;
1616                         ptlrpc_wake_client_req(req);
1617                 }
1618                 spin_unlock (&req->rq_lock);
1619         }
1620
1621         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1622                 struct ptlrpc_request *req =
1623                         list_entry(tmp, struct ptlrpc_request, rq_list);
1624
1625                 DEBUG_REQ(D_HA, req, "aborting waiting req");
1626
1627                 spin_lock (&req->rq_lock);
1628                 if (req->rq_import_generation < imp->imp_generation) {
1629                         req->rq_err = 1;
1630                         ptlrpc_wake_client_req(req);
1631                 }
1632                 spin_unlock (&req->rq_lock);
1633         }
1634
1635         /* Last chance to free reqs left on the replay list, but we
1636          * will still leak reqs that haven't comitted.  */
1637         if (imp->imp_replayable)
1638                 ptlrpc_free_committed(imp);
1639
1640         spin_unlock_irqrestore(&imp->imp_lock, flags);
1641
1642         EXIT;
1643 }
1644
1645 static __u64 ptlrpc_last_xid = 0;
1646 static spinlock_t ptlrpc_last_xid_lock = SPIN_LOCK_UNLOCKED;
1647
1648 __u64 ptlrpc_next_xid(void)
1649 {
1650         __u64 tmp;
1651         spin_lock(&ptlrpc_last_xid_lock);
1652         tmp = ++ptlrpc_last_xid;
1653         spin_unlock(&ptlrpc_last_xid_lock);
1654         return tmp;
1655 }
1656
1657