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