Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27 #ifndef __KERNEL__
28 #include <errno.h>
29 #include <signal.h>
30 #include <liblustre.h>
31 #endif
32
33 #include <obd_support.h>
34 #include <obd_class.h>
35 #include <lustre_lib.h>
36 #include <lustre_ha.h>
37 #include <lustre_import.h>
38
39 #include "ptlrpc_internal.h"
40
41 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
42                         struct ptlrpc_client *cl)
43 {
44         cl->cli_request_portal = req_portal;
45         cl->cli_reply_portal   = rep_portal;
46         cl->cli_name           = name;
47 }
48
49 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
50 {
51         struct ptlrpc_connection *c;
52         lnet_nid_t                self;
53         lnet_process_id_t         peer;
54         int                       err;
55
56         err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
57         if (err != 0) {
58                 CERROR("cannot find peer %s!\n", uuid->uuid);
59                 return NULL;
60         }
61
62         c = ptlrpc_get_connection(peer, self, uuid);
63         if (c) {
64                 memcpy(c->c_remote_uuid.uuid,
65                        uuid->uuid, sizeof(c->c_remote_uuid.uuid));
66         }
67
68         CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
69
70         return c;
71 }
72
73 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
74                                  struct obd_uuid *uuid)
75 {
76         lnet_nid_t        self;
77         lnet_process_id_t peer;
78         int               err;
79
80         err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
81         if (err != 0) {
82                 CERROR("cannot find peer %s!\n", uuid->uuid);
83                 return;
84         }
85
86         conn->c_peer = peer;
87         conn->c_self = self;
88         return;
89 }
90
91 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
92 {
93         struct ptlrpc_bulk_desc *desc;
94
95         OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
96         if (!desc)
97                 return NULL;
98
99         spin_lock_init(&desc->bd_lock);
100         cfs_waitq_init(&desc->bd_waitq);
101         desc->bd_max_iov = npages;
102         desc->bd_iov_count = 0;
103         desc->bd_md_h = LNET_INVALID_HANDLE;
104         desc->bd_portal = portal;
105         desc->bd_type = type;
106
107         return desc;
108 }
109
110 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
111                                                int npages, int type, int portal)
112 {
113         struct obd_import *imp = req->rq_import;
114         struct ptlrpc_bulk_desc *desc;
115
116         ENTRY;
117         LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
118         desc = new_bulk(npages, type, portal);
119         if (desc == NULL)
120                 RETURN(NULL);
121
122         desc->bd_import_generation = req->rq_import_generation;
123         desc->bd_import = class_import_get(imp);
124         desc->bd_req = req;
125
126         desc->bd_cbid.cbid_fn  = client_bulk_callback;
127         desc->bd_cbid.cbid_arg = desc;
128
129         /* This makes req own desc, and free it when she frees herself */
130         req->rq_bulk = desc;
131
132         return desc;
133 }
134
135 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
136                                                int npages, int type, int portal)
137 {
138         struct obd_export *exp = req->rq_export;
139         struct ptlrpc_bulk_desc *desc;
140
141         ENTRY;
142         LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
143
144         desc = new_bulk(npages, type, portal);
145         if (desc == NULL)
146                 RETURN(NULL);
147
148         desc->bd_export = class_export_get(exp);
149         desc->bd_req = req;
150
151         desc->bd_cbid.cbid_fn  = server_bulk_callback;
152         desc->bd_cbid.cbid_arg = desc;
153
154         /* NB we don't assign rq_bulk here; server-side requests are
155          * re-used, and the handler frees the bulk desc explicitly. */
156
157         return desc;
158 }
159
160 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
161                            cfs_page_t *page, int pageoffset, int len)
162 {
163         LASSERT(desc->bd_iov_count < desc->bd_max_iov);
164         LASSERT(page != NULL);
165         LASSERT(pageoffset >= 0);
166         LASSERT(len > 0);
167         LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
168
169         desc->bd_nob += len;
170
171         ptlrpc_add_bulk_page(desc, page, pageoffset, len);
172 }
173
174 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
175 {
176         ENTRY;
177
178         LASSERT(desc != NULL);
179         LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
180         LASSERT(!desc->bd_network_rw);         /* network hands off or */
181         LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
182
183         sptlrpc_enc_pool_put_pages(desc);
184
185         if (desc->bd_export)
186                 class_export_put(desc->bd_export);
187         else
188                 class_import_put(desc->bd_import);
189
190         OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
191                                 bd_iov[desc->bd_max_iov]));
192         EXIT;
193 }
194
195 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
196 {
197         struct list_head *l, *tmp;
198         struct ptlrpc_request *req;
199
200         if (!pool)
201                 return;
202
203         list_for_each_safe(l, tmp, &pool->prp_req_list) {
204                 req = list_entry(l, struct ptlrpc_request, rq_list);
205                 list_del(&req->rq_list);
206                 LASSERT(req->rq_reqbuf);
207                 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
208                 OBD_FREE(req->rq_reqbuf, pool->prp_rq_size);
209                 OBD_FREE(req, sizeof(*req));
210         }
211         OBD_FREE(pool, sizeof(*pool));
212 }
213
214 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
215 {
216         int i;
217         int size = 1;
218
219         while (size < pool->prp_rq_size + SPTLRPC_MAX_PAYLOAD)
220                 size <<= 1;
221
222         LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
223                  "Trying to change pool size with nonempty pool "
224                  "from %d to %d bytes\n", pool->prp_rq_size, size);
225
226         spin_lock(&pool->prp_lock);
227         pool->prp_rq_size = size;
228         for (i = 0; i < num_rq; i++) {
229                 struct ptlrpc_request *req;
230                 struct lustre_msg *msg;
231
232                 spin_unlock(&pool->prp_lock);
233                 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
234                 if (!req)
235                         return;
236                 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
237                 if (!msg) {
238                         OBD_FREE(req, sizeof(struct ptlrpc_request));
239                         return;
240                 }
241                 req->rq_reqbuf = msg;
242                 req->rq_reqbuf_len = size;
243                 req->rq_pool = pool;
244                 spin_lock(&pool->prp_lock);
245                 list_add_tail(&req->rq_list, &pool->prp_req_list);
246         }
247         spin_unlock(&pool->prp_lock);
248         return;
249 }
250
251 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
252                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int))
253 {
254         struct ptlrpc_request_pool *pool;
255
256         OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
257         if (!pool)
258                 return NULL;
259
260         /* Request next power of two for the allocation, because internally
261            kernel would do exactly this */
262
263         spin_lock_init(&pool->prp_lock);
264         CFS_INIT_LIST_HEAD(&pool->prp_req_list);
265         pool->prp_rq_size = msgsize;
266         pool->prp_populate = populate_pool;
267
268         populate_pool(pool, num_rq);
269
270         if (list_empty(&pool->prp_req_list)) {
271                 /* have not allocated a single request for the pool */
272                 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
273                 pool = NULL;
274         }
275         return pool;
276 }
277
278 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
279 {
280         struct ptlrpc_request *request;
281         struct lustre_msg *reqbuf;
282
283         if (!pool)
284                 return NULL;
285
286         spin_lock(&pool->prp_lock);
287
288         /* See if we have anything in a pool, and bail out if nothing,
289          * in writeout path, where this matters, this is safe to do, because
290          * nothing is lost in this case, and when some in-flight requests
291          * complete, this code will be called again. */
292         if (unlikely(list_empty(&pool->prp_req_list))) {
293                 spin_unlock(&pool->prp_lock);
294                 return NULL;
295         }
296
297         request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
298                              rq_list);
299         list_del(&request->rq_list);
300         spin_unlock(&pool->prp_lock);
301
302         LASSERT(request->rq_reqbuf);
303         LASSERT(request->rq_pool);
304
305         reqbuf = request->rq_reqbuf;
306         memset(request, 0, sizeof(*request));
307         request->rq_reqbuf = reqbuf;
308         request->rq_reqbuf_len = pool->prp_rq_size;
309         request->rq_pool = pool;
310         return request;
311 }
312
313 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
314 {
315         struct ptlrpc_request_pool *pool = request->rq_pool;
316
317         spin_lock(&pool->prp_lock);
318         LASSERT(list_empty(&request->rq_list));
319         list_add_tail(&request->rq_list, &pool->prp_req_list);
320         spin_unlock(&pool->prp_lock);
321 }
322
323 struct ptlrpc_request *
324 ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
325                      int count, int *lengths, char **bufs,
326                      struct ptlrpc_request_pool *pool,
327                      struct ptlrpc_cli_ctx *ctx)
328 {
329         struct ptlrpc_request *request = NULL;
330         int rc;
331         ENTRY;
332
333         /* The obd disconnected */
334         if (imp == NULL)
335                 return NULL;
336
337         LASSERT(imp != LP_POISON);
338         LASSERT((unsigned long)imp->imp_client > 0x1000);
339         LASSERT(imp->imp_client != LP_POISON);
340
341         if (pool)
342                 request = ptlrpc_prep_req_from_pool(pool);
343
344         if (!request)
345                 OBD_ALLOC(request, sizeof(*request));
346
347         if (!request) {
348                 CERROR("request allocation out of memory\n");
349                 RETURN(NULL);
350         }
351
352         request->rq_import = class_import_get(imp);
353
354         if (unlikely(ctx))
355                 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
356         else {
357                 rc = sptlrpc_req_get_ctx(request);
358                 if (rc)
359                         GOTO(out_free, rc);
360         }
361
362         sptlrpc_req_set_flavor(request, opcode);
363
364         rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
365                                  bufs);
366         if (rc) {
367                 LASSERT(!request->rq_pool);
368                 GOTO(out_ctx, rc);
369         }
370
371         lustre_msg_add_version(request->rq_reqmsg, version);
372
373         if (imp->imp_server_timeout)
374                 request->rq_timeout = obd_timeout / 2;
375         else
376                 request->rq_timeout = obd_timeout;
377         request->rq_send_state = LUSTRE_IMP_FULL;
378         request->rq_type = PTL_RPC_MSG_REQUEST;
379         request->rq_export = NULL;
380
381         request->rq_req_cbid.cbid_fn  = request_out_callback;
382         request->rq_req_cbid.cbid_arg = request;
383
384         request->rq_reply_cbid.cbid_fn  = reply_in_callback;
385         request->rq_reply_cbid.cbid_arg = request;
386
387         request->rq_phase = RQ_PHASE_NEW;
388
389         /* XXX FIXME bug 249 */
390         request->rq_request_portal = imp->imp_client->cli_request_portal;
391         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
392
393         spin_lock_init(&request->rq_lock);
394         CFS_INIT_LIST_HEAD(&request->rq_list);
395         CFS_INIT_LIST_HEAD(&request->rq_replay_list);
396         CFS_INIT_LIST_HEAD(&request->rq_mod_list);
397         CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
398         CFS_INIT_LIST_HEAD(&request->rq_set_chain);
399         cfs_waitq_init(&request->rq_reply_waitq);
400         request->rq_xid = ptlrpc_next_xid();
401         atomic_set(&request->rq_refcount, 1);
402
403         lustre_msg_set_opc(request->rq_reqmsg, opcode);
404         lustre_msg_set_flags(request->rq_reqmsg, 0);
405
406         RETURN(request);
407 out_ctx:
408         sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
409 out_free:
410         class_import_put(imp);
411         if (request->rq_pool)
412                 __ptlrpc_free_req_to_pool(request);
413         else
414                 OBD_FREE(request, sizeof(*request));
415         return NULL;
416 }
417
418 struct ptlrpc_request *
419 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
420                 int *lengths, char **bufs)
421 {
422         return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
423                                     NULL, NULL);
424 }
425
426 struct ptlrpc_request_set *ptlrpc_prep_set(void)
427 {
428         struct ptlrpc_request_set *set;
429
430         ENTRY;
431         OBD_ALLOC(set, sizeof *set);
432         if (!set)
433                 RETURN(NULL);
434         CFS_INIT_LIST_HEAD(&set->set_requests);
435         cfs_waitq_init(&set->set_waitq);
436         set->set_remaining = 0;
437         spin_lock_init(&set->set_new_req_lock);
438         CFS_INIT_LIST_HEAD(&set->set_new_requests);
439
440         RETURN(set);
441 }
442
443 /* Finish with this set; opposite of prep_set. */
444 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
445 {
446         struct list_head *tmp;
447         struct list_head *next;
448         int               expected_phase;
449         int               n = 0;
450         ENTRY;
451
452         /* Requests on the set should either all be completed, or all be new */
453         expected_phase = (set->set_remaining == 0) ?
454                          RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
455         list_for_each (tmp, &set->set_requests) {
456                 struct ptlrpc_request *req =
457                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
458
459                 LASSERT(req->rq_phase == expected_phase);
460                 n++;
461         }
462
463         LASSERT(set->set_remaining == 0 || set->set_remaining == n);
464
465         list_for_each_safe(tmp, next, &set->set_requests) {
466                 struct ptlrpc_request *req =
467                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
468                 list_del_init(&req->rq_set_chain);
469
470                 LASSERT(req->rq_phase == expected_phase);
471
472                 if (req->rq_phase == RQ_PHASE_NEW) {
473
474                         if (req->rq_interpret_reply != NULL) {
475                                 int (*interpreter)(struct ptlrpc_request *,
476                                                    void *, int) =
477                                         req->rq_interpret_reply;
478
479                                 /* higher level (i.e. LOV) failed;
480                                  * let the sub reqs clean up */
481                                 req->rq_status = -EBADR;
482                                 interpreter(req, &req->rq_async_args,
483                                             req->rq_status);
484                         }
485                         set->set_remaining--;
486                 }
487
488                 req->rq_set = NULL;
489                 ptlrpc_req_finished (req);
490         }
491
492         LASSERT(set->set_remaining == 0);
493
494         OBD_FREE(set, sizeof(*set));
495         EXIT;
496 }
497
498 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
499                         struct ptlrpc_request *req)
500 {
501         /* The set takes over the caller's request reference */
502         list_add_tail(&req->rq_set_chain, &set->set_requests);
503         req->rq_set = set;
504         set->set_remaining++;
505
506         atomic_inc(&req->rq_import->imp_inflight);
507 }
508
509 /* lock so many callers can add things, the context that owns the set
510  * is supposed to notice these and move them into the set proper. */
511 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
512                             struct ptlrpc_request *req)
513 {
514         spin_lock(&set->set_new_req_lock);
515         /* The set takes over the caller's request reference */
516         list_add_tail(&req->rq_set_chain, &set->set_new_requests);
517         req->rq_set = set;
518         spin_unlock(&set->set_new_req_lock);
519 }
520
521 /*
522  * Based on the current state of the import, determine if the request
523  * can be sent, is an error, or should be delayed.
524  *
525  * Returns true if this request should be delayed. If false, and
526  * *status is set, then the request can not be sent and *status is the
527  * error code.  If false and status is 0, then request can be sent.
528  *
529  * The imp->imp_lock must be held.
530  */
531 static int ptlrpc_import_delay_req(struct obd_import *imp,
532                                    struct ptlrpc_request *req, int *status)
533 {
534         int delay = 0;
535         ENTRY;
536
537         LASSERT (status != NULL);
538         *status = 0;
539
540         if (req->rq_ctx_init || req->rq_ctx_fini) {
541                 /* always allow ctx init/fini rpc go through */
542         } else if (imp->imp_state == LUSTRE_IMP_NEW) {
543                 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
544                 *status = -EIO;
545                 LBUG();
546         } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
547                 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
548                 *status = -EIO;
549         } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
550                  imp->imp_state == LUSTRE_IMP_CONNECTING) {
551                 /* allow CONNECT even if import is invalid */ ;
552         } else if (imp->imp_invalid) {
553                 /* if it is mgc, wait for recovry. b=13464 */
554                 if (imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
555                         delay = 1;
556                 /* If the import has been invalidated (such as by an OST
557                  * failure) the request must fail with -ESHUTDOWN.  This
558                  * indicates the requests should be discarded; an -EIO
559                  * may result in a resend of the request. */
560                 if (!imp->imp_deactive)
561                         DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
562                 *status = -ESHUTDOWN; /* bz 12940 */
563         } else if (req->rq_import_generation != imp->imp_generation) {
564                 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
565                 *status = -EIO;
566         } else if (req->rq_send_state != imp->imp_state) {
567                 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
568                     req->rq_no_delay)
569                         *status = -EWOULDBLOCK;
570                 else
571                         delay = 1;
572         }
573
574         RETURN(delay);
575 }
576
577 static int ptlrpc_check_reply(struct ptlrpc_request *req)
578 {
579         int rc = 0;
580         ENTRY;
581
582         /* serialise with network callback */
583         spin_lock(&req->rq_lock);
584
585         if (req->rq_replied)
586                 GOTO(out, rc = 1);
587
588         if (req->rq_net_err && !req->rq_timedout) {
589                 spin_unlock(&req->rq_lock);
590                 rc = ptlrpc_expire_one_request(req);
591                 spin_lock(&req->rq_lock);
592                 GOTO(out, rc);
593         }
594
595         if (req->rq_err)
596                 GOTO(out, rc = 1);
597
598         if (req->rq_resend)
599                 GOTO(out, rc = 1);
600
601         if (req->rq_restart)
602                 GOTO(out, rc = 1);
603         EXIT;
604  out:
605         spin_unlock(&req->rq_lock);
606         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
607         return rc;
608 }
609
610 static int ptlrpc_check_status(struct ptlrpc_request *req)
611 {
612         int err;
613         ENTRY;
614
615         err = lustre_msg_get_status(req->rq_repmsg);
616         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
617                 struct obd_import *imp = req->rq_import;
618                 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
619                 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
620                                 " with %s. The %s operation failed with %d\n",
621                                 libcfs_nid2str(imp->imp_connection->c_peer.nid),
622                                 ll_opcode2str(opc), err);
623                 RETURN(err < 0 ? err : -EINVAL);
624         }
625
626         if (err < 0) {
627                 DEBUG_REQ(D_INFO, req, "status is %d", err);
628         } else if (err > 0) {
629                 /* XXX: translate this error from net to host */
630                 DEBUG_REQ(D_INFO, req, "status is %d", err);
631         }
632
633         RETURN(err);
634 }
635
636 static int after_reply(struct ptlrpc_request *req)
637 {
638         struct obd_import *imp = req->rq_import;
639         int rc;
640         ENTRY;
641
642         LASSERT(!req->rq_receiving_reply);
643         LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
644
645         /* NB Until this point, the whole of the incoming message,
646          * including buflens, status etc is in the sender's byte order. */
647
648         /* Clear reply swab mask; this is a new reply in sender's byte order */
649         req->rq_rep_swab_mask = 0;
650
651         rc = sptlrpc_cli_unwrap_reply(req);
652         if (rc) {
653                 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
654                 RETURN(rc);
655         }
656
657         /* security layer unwrap might ask resend this request */
658         if (req->rq_resend)
659                 RETURN(0);
660
661         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
662         if (rc) {
663                 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
664                 RETURN(-EPROTO);
665         }
666
667         rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
668         if (rc) {
669                 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
670                 RETURN(-EPROTO);
671         }
672
673         if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
674             lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
675                 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
676                           lustre_msg_get_type(req->rq_repmsg));
677                 RETURN(-EPROTO);
678         }
679
680         rc = ptlrpc_check_status(req);
681         imp->imp_connect_error = rc;
682
683         if (rc) {
684                 /* Either we've been evicted, or the server has failed for
685                  * some reason. Try to reconnect, and if that fails, punt to
686                  * the upcall. */
687                 if (ll_rpc_recoverable_error(rc)) {
688                         if (req->rq_send_state != LUSTRE_IMP_FULL ||
689                             imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
690                                 RETURN(rc);
691                         }
692                         ptlrpc_request_handle_notconn(req);
693                         RETURN(rc);
694                 }
695         } else {
696                 /* Let's look if server send slv. Do it only for RPC with 
697                  * rc == 0. */
698                 if (imp->imp_obd->obd_namespace) {
699                         /* Disconnect rpc is sent when namespace is already 
700                          * destroyed. Let's check this and will not try update
701                          * pool. */
702                         ldlm_cli_update_pool(req);
703                 }
704         }
705
706         /* Store transno in reqmsg for replay. */
707         req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
708         lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
709
710         if (req->rq_import->imp_replayable) {
711                 spin_lock(&imp->imp_lock);
712                 /* no point in adding already-committed requests to the replay
713                  * list, we will just remove them immediately. b=9829 */
714                 if (req->rq_transno != 0 && 
715                     (req->rq_transno > 
716                      lustre_msg_get_last_committed(req->rq_repmsg) ||
717                      req->rq_replay))
718                         ptlrpc_retain_replayable_request(req, imp);
719                 else if (req->rq_commit_cb != NULL) {
720                         spin_unlock(&imp->imp_lock);
721                         req->rq_commit_cb(req);
722                         spin_lock(&imp->imp_lock);
723                 }
724
725                 /* Replay-enabled imports return commit-status information. */
726                 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
727                         imp->imp_peer_committed_transno =
728                                 lustre_msg_get_last_committed(req->rq_repmsg);
729                 }
730                 ptlrpc_free_committed(imp);
731                 spin_unlock(&imp->imp_lock);
732         }
733
734         RETURN(rc);
735 }
736
737 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
738 {
739         struct obd_import     *imp;
740         int rc;
741         ENTRY;
742
743         LASSERT(req->rq_phase == RQ_PHASE_NEW);
744         if (req->rq_sent && (req->rq_sent > CURRENT_SECONDS))
745                 RETURN (0);
746         
747         req->rq_phase = RQ_PHASE_RPC;
748
749         imp = req->rq_import;
750         spin_lock(&imp->imp_lock);
751
752         req->rq_import_generation = imp->imp_generation;
753
754         if (ptlrpc_import_delay_req(imp, req, &rc)) {
755                 spin_lock (&req->rq_lock);
756                 req->rq_waiting = 1;
757                 spin_unlock (&req->rq_lock);
758
759                 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
760                           "(%s != %s)",
761                           lustre_msg_get_status(req->rq_reqmsg) ,
762                           ptlrpc_import_state_name(req->rq_send_state),
763                           ptlrpc_import_state_name(imp->imp_state));
764                 LASSERT(list_empty (&req->rq_list));
765
766                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
767                 spin_unlock(&imp->imp_lock);
768                 RETURN(0);
769         }
770
771         if (rc != 0) {
772                 spin_unlock(&imp->imp_lock);
773                 req->rq_status = rc;
774                 req->rq_phase = RQ_PHASE_INTERPRET;
775                 RETURN(rc);
776         }
777
778         /* XXX this is the same as ptlrpc_queue_wait */
779         LASSERT(list_empty(&req->rq_list));
780         list_add_tail(&req->rq_list, &imp->imp_sending_list);
781         spin_unlock(&imp->imp_lock);
782
783         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
784
785         rc = sptlrpc_req_refresh_ctx(req, -1);
786         if (rc) {
787                 if (req->rq_err) {
788                         req->rq_status = rc;
789                         RETURN(1);
790                 } else {
791                         /* here begins timeout counting */
792                         req->rq_sent = CURRENT_SECONDS;
793                         req->rq_wait_ctx = 1;
794                         RETURN(0);
795                 }
796         }
797
798         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
799                " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
800                imp->imp_obd->obd_uuid.uuid,
801                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
802                libcfs_nid2str(imp->imp_connection->c_peer.nid),
803                lustre_msg_get_opc(req->rq_reqmsg));
804
805         rc = ptl_send_rpc(req, 0);
806         if (rc) {
807                 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
808                 req->rq_net_err = 1;
809                 RETURN(rc);
810         }
811         RETURN(0);
812 }
813
814 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
815 int ptlrpc_check_set(struct ptlrpc_request_set *set)
816 {
817         struct list_head *tmp;
818         int force_timer_recalc = 0;
819         ENTRY;
820
821         if (set->set_remaining == 0)
822                 RETURN(1);
823
824         list_for_each(tmp, &set->set_requests) {
825                 struct ptlrpc_request *req =
826                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
827                 struct obd_import *imp = req->rq_import;
828                 int rc = 0;
829
830                 if (req->rq_phase == RQ_PHASE_NEW &&
831                     ptlrpc_send_new_req(req)) {
832                         force_timer_recalc = 1;
833                 }
834                 /* delayed send - skip */
835                 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
836                         continue;
837
838                 if (!(req->rq_phase == RQ_PHASE_RPC ||
839                       req->rq_phase == RQ_PHASE_BULK ||
840                       req->rq_phase == RQ_PHASE_INTERPRET ||
841                       req->rq_phase == RQ_PHASE_COMPLETE)) {
842                         DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
843                         LBUG();
844                 }
845
846                 if (req->rq_phase == RQ_PHASE_COMPLETE)
847                         continue;
848
849                 if (req->rq_phase == RQ_PHASE_INTERPRET)
850                         GOTO(interpret, req->rq_status);
851
852                 if (req->rq_net_err && !req->rq_timedout)
853                         ptlrpc_expire_one_request(req);
854
855                 if (req->rq_err) {
856                         ptlrpc_unregister_reply(req);
857                         req->rq_replied = 0;
858                         if (req->rq_status == 0)
859                                 req->rq_status = -EIO;
860                         req->rq_phase = RQ_PHASE_INTERPRET;
861
862                         spin_lock(&imp->imp_lock);
863                         list_del_init(&req->rq_list);
864                         spin_unlock(&imp->imp_lock);
865
866                         GOTO(interpret, req->rq_status);
867                 }
868
869                 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
870                  * will only be set after rq_timedout, but the oig waiting
871                  * path sets rq_intr irrespective of whether ptlrpcd has
872                  * seen a timeout.  our policy is to only interpret
873                  * interrupted rpcs after they have timed out */
874                 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
875                                      req->rq_wait_ctx)) {
876                         /* NB could be on delayed list */
877                         ptlrpc_unregister_reply(req);
878                         req->rq_status = -EINTR;
879                         req->rq_phase = RQ_PHASE_INTERPRET;
880
881                         spin_lock(&imp->imp_lock);
882                         list_del_init(&req->rq_list);
883                         spin_unlock(&imp->imp_lock);
884
885                         GOTO(interpret, req->rq_status);
886                 }
887
888                 if (req->rq_phase == RQ_PHASE_RPC) {
889                         if (req->rq_timedout || req->rq_resend ||
890                             req->rq_waiting || req->rq_wait_ctx) {
891                                 int status;
892
893                                 /* rq_wait_ctx is only touched in ptlrpcd,
894                                  * no lock needed here.
895                                  */
896                                 if (req->rq_wait_ctx)
897                                         goto check_ctx;
898
899                                 ptlrpc_unregister_reply(req);
900
901                                 spin_lock(&imp->imp_lock);
902
903                                 if (ptlrpc_import_delay_req(imp, req, &status)){
904                                         spin_unlock(&imp->imp_lock);
905                                         continue;
906                                 }
907
908                                 list_del_init(&req->rq_list);
909                                 if (status != 0)  {
910                                         req->rq_status = status;
911                                         req->rq_phase = RQ_PHASE_INTERPRET;
912                                         spin_unlock(&imp->imp_lock);
913                                         GOTO(interpret, req->rq_status);
914                                 }
915                                 if (req->rq_no_resend && !req->rq_wait_ctx) {
916                                         req->rq_status = -ENOTCONN;
917                                         req->rq_phase = RQ_PHASE_INTERPRET;
918                                         spin_unlock(&imp->imp_lock);
919                                         GOTO(interpret, req->rq_status);
920                                 }
921                                 list_add_tail(&req->rq_list,
922                                               &imp->imp_sending_list);
923
924                                 spin_unlock(&imp->imp_lock);
925
926                                 req->rq_waiting = 0;
927                                 if (req->rq_resend) {
928                                         lustre_msg_add_flags(req->rq_reqmsg,
929                                                              MSG_RESENT);
930                                         if (req->rq_bulk) {
931                                                 __u64 old_xid = req->rq_xid;
932
933                                                 ptlrpc_unregister_bulk (req);
934
935                                                 /* ensure previous bulk fails */
936                                                 req->rq_xid = ptlrpc_next_xid();
937                                                 CDEBUG(D_HA, "resend bulk "
938                                                        "old x"LPU64
939                                                        " new x"LPU64"\n",
940                                                        old_xid, req->rq_xid);
941                                         }
942                                 }
943 check_ctx:
944                                 status = sptlrpc_req_refresh_ctx(req, -1);
945                                 if (status) {
946                                         if (req->rq_err) {
947                                                 req->rq_status = status;
948                                                 force_timer_recalc = 1;
949                                         }
950                                         if (!req->rq_wait_ctx) {
951                                                 /* begins timeout counting */
952                                                 req->rq_sent = CURRENT_SECONDS;
953                                                 req->rq_wait_ctx = 1;
954                                         }
955                                         continue;
956                                 } else {
957                                         req->rq_sent = 0;
958                                         req->rq_wait_ctx = 0;
959                                 }
960
961                                 rc = ptl_send_rpc(req, 0);
962                                 if (rc) {
963                                         DEBUG_REQ(D_HA, req, "send failed (%d)",
964                                                   rc);
965                                         force_timer_recalc = 1;
966                                         req->rq_net_err = 1;
967                                 }
968                                 /* need to reset the timeout */
969                                 force_timer_recalc = 1;
970                         }
971
972                         /* Still waiting for a reply? */
973                         if (ptlrpc_client_receiving_reply(req))
974                                 continue;
975
976                         /* Did we actually receive a reply? */
977                         if (!ptlrpc_client_replied(req))
978                                 continue;
979
980                         spin_lock(&imp->imp_lock);
981                         list_del_init(&req->rq_list);
982                         spin_unlock(&imp->imp_lock);
983
984                         req->rq_status = after_reply(req);
985                         if (req->rq_resend) {
986                                 /* Add this req to the delayed list so
987                                    it can be errored if the import is
988                                    evicted after recovery. */
989                                 spin_lock(&imp->imp_lock);
990                                 list_add_tail(&req->rq_list,
991                                               &imp->imp_delayed_list);
992                                 spin_unlock(&imp->imp_lock);
993                                 continue;
994                         }
995
996                         /* If there is no bulk associated with this request,
997                          * then we're done and should let the interpreter
998                          * process the reply.  Similarly if the RPC returned
999                          * an error, and therefore the bulk will never arrive.
1000                          */
1001                         if (req->rq_bulk == NULL || req->rq_status != 0) {
1002                                 req->rq_phase = RQ_PHASE_INTERPRET;
1003                                 GOTO(interpret, req->rq_status);
1004                         }
1005
1006                         req->rq_phase = RQ_PHASE_BULK;
1007                 }
1008
1009                 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1010                 if (ptlrpc_bulk_active(req->rq_bulk))
1011                         continue;
1012
1013                 if (!req->rq_bulk->bd_success) {
1014                         /* The RPC reply arrived OK, but the bulk screwed
1015                          * up!  Dead wierd since the server told us the RPC
1016                          * was good after getting the REPLY for her GET or
1017                          * the ACK for her PUT. */
1018                         DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1019                         LBUG();
1020                 }
1021
1022                 req->rq_phase = RQ_PHASE_INTERPRET;
1023
1024         interpret:
1025                 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1026                 LASSERT(!req->rq_receiving_reply);
1027
1028                 ptlrpc_unregister_reply(req);
1029                 if (req->rq_bulk != NULL)
1030                         ptlrpc_unregister_bulk (req);
1031
1032                 if (req->rq_interpret_reply != NULL) {
1033                         int (*interpreter)(struct ptlrpc_request *,void *,int) =
1034                                 req->rq_interpret_reply;
1035                         req->rq_status = interpreter(req, &req->rq_async_args,
1036                                                      req->rq_status);
1037                 }
1038                 req->rq_phase = RQ_PHASE_COMPLETE;
1039
1040                 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1041                        "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1042                        imp->imp_obd->obd_uuid.uuid,
1043                        lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1044                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1045                        lustre_msg_get_opc(req->rq_reqmsg));
1046
1047                 set->set_remaining--;
1048
1049                 atomic_dec(&imp->imp_inflight);
1050                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1051         }
1052
1053         /* If we hit an error, we want to recover promptly. */
1054         RETURN(set->set_remaining == 0 || force_timer_recalc);
1055 }
1056
1057 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
1058 {
1059         struct obd_import *imp = req->rq_import;
1060         int rc = 0;
1061         ENTRY;
1062
1063         DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, %lus ago)",
1064                   req->rq_net_err ? "network error" : "timeout",
1065                   (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
1066
1067         if (imp != NULL && obd_debug_peer_on_timeout)
1068                 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1069
1070         spin_lock(&req->rq_lock);
1071         req->rq_timedout = 1;
1072         req->rq_wait_ctx = 0;
1073         spin_unlock(&req->rq_lock);
1074
1075         ptlrpc_unregister_reply (req);
1076
1077         if (obd_dump_on_timeout)
1078                 libcfs_debug_dumplog();
1079
1080         if (req->rq_bulk != NULL)
1081                 ptlrpc_unregister_bulk (req);
1082
1083         if (imp == NULL) {
1084                 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1085                 RETURN(1);
1086         }
1087
1088         /* The DLM server doesn't want recovery run on its imports. */
1089         if (imp->imp_dlm_fake)
1090                 RETURN(1);
1091
1092         /* If this request is for recovery or other primordial tasks,
1093          * then error it out here. */
1094         if (req->rq_ctx_init || req->rq_ctx_fini ||
1095             req->rq_send_state != LUSTRE_IMP_FULL ||
1096             imp->imp_obd->obd_no_recov) {
1097                 spin_lock(&req->rq_lock);
1098                 req->rq_status = -ETIMEDOUT;
1099                 req->rq_err = 1;
1100                 spin_unlock(&req->rq_lock);
1101                 RETURN(1);
1102         }
1103         
1104         /* if request can't be resend we can't wait answer after timeout */
1105         if (req->rq_no_resend) {
1106                 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1107                 rc = 1;
1108         }
1109
1110         ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1111
1112         RETURN(rc);
1113 }
1114
1115 int ptlrpc_expired_set(void *data)
1116 {
1117         struct ptlrpc_request_set *set = data;
1118         struct list_head          *tmp;
1119         time_t                     now = CURRENT_SECONDS;
1120         ENTRY;
1121
1122         LASSERT(set != NULL);
1123
1124         /* A timeout expired; see which reqs it applies to... */
1125         list_for_each (tmp, &set->set_requests) {
1126                 struct ptlrpc_request *req =
1127                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1128
1129                 /* request in-flight? */
1130                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1131                        !req->rq_resend) ||
1132                       (req->rq_phase == RQ_PHASE_BULK)))
1133                         continue;
1134
1135                 if (req->rq_timedout ||           /* already dealt with */
1136                     req->rq_sent + req->rq_timeout > now) /* not expired */
1137                         continue;
1138
1139                 /* deal with this guy */
1140                 ptlrpc_expire_one_request (req);
1141         }
1142
1143         /* When waiting for a whole set, we always to break out of the
1144          * sleep so we can recalculate the timeout, or enable interrupts
1145          * iff everyone's timed out.
1146          */
1147         RETURN(1);
1148 }
1149
1150 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1151 {
1152         spin_lock(&req->rq_lock);
1153         req->rq_intr = 1;
1154         spin_unlock(&req->rq_lock);
1155 }
1156
1157 void ptlrpc_interrupted_set(void *data)
1158 {
1159         struct ptlrpc_request_set *set = data;
1160         struct list_head *tmp;
1161
1162         LASSERT(set != NULL);
1163         CERROR("INTERRUPTED SET %p\n", set);
1164
1165         list_for_each(tmp, &set->set_requests) {
1166                 struct ptlrpc_request *req =
1167                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1168
1169                 if (req->rq_phase != RQ_PHASE_RPC)
1170                         continue;
1171
1172                 ptlrpc_mark_interrupted(req);
1173         }
1174 }
1175
1176 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1177 {
1178         struct list_head      *tmp;
1179         time_t                 now = CURRENT_SECONDS;
1180         time_t                 deadline;
1181         int                    timeout = 0;
1182         struct ptlrpc_request *req;
1183         ENTRY;
1184
1185         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1186
1187         list_for_each(tmp, &set->set_requests) {
1188                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1189
1190                 /* request in-flight? */
1191                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1192                       (req->rq_phase == RQ_PHASE_BULK) ||
1193                       (req->rq_phase == RQ_PHASE_NEW)))
1194                         continue;
1195
1196                 if (req->rq_timedout)   /* already timed out */
1197                         continue;
1198
1199                 if (req->rq_phase == RQ_PHASE_NEW)
1200                         deadline = req->rq_sent;
1201                 else
1202                         deadline = req->rq_sent + req->rq_timeout;
1203
1204                 if (deadline <= now)    /* actually expired already */
1205                         timeout = 1;    /* ASAP */
1206                 else if (timeout == 0 || timeout > deadline - now)
1207                         timeout = deadline - now;
1208         }
1209         RETURN(timeout);
1210 }
1211
1212 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1213 {
1214         struct list_head      *tmp;
1215         struct ptlrpc_request *req;
1216         struct l_wait_info     lwi;
1217         int                    rc, timeout;
1218         ENTRY;
1219
1220         if (list_empty(&set->set_requests))
1221                 RETURN(0);
1222
1223         list_for_each(tmp, &set->set_requests) {
1224                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1225                 if (req->rq_phase == RQ_PHASE_NEW)
1226                         (void)ptlrpc_send_new_req(req);
1227         }
1228
1229         do {
1230                 timeout = ptlrpc_set_next_timeout(set);
1231
1232                 /* wait until all complete, interrupted, or an in-flight
1233                  * req times out */
1234                 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1235                        set, timeout);
1236                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1237                                        ptlrpc_expired_set,
1238                                        ptlrpc_interrupted_set, set);
1239                 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1240
1241                 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1242
1243                 /* -EINTR => all requests have been flagged rq_intr so next
1244                  * check completes.
1245                  * -ETIMEOUTD => someone timed out.  When all reqs have
1246                  * timed out, signals are enabled allowing completion with
1247                  * EINTR.
1248                  * I don't really care if we go once more round the loop in
1249                  * the error cases -eeb. */
1250         } while (rc != 0 || set->set_remaining != 0);
1251
1252         LASSERT(set->set_remaining == 0);
1253
1254         rc = 0;
1255         list_for_each(tmp, &set->set_requests) {
1256                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1257
1258                 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1259                 if (req->rq_status != 0)
1260                         rc = req->rq_status;
1261         }
1262
1263         if (set->set_interpret != NULL) {
1264                 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1265                         set->set_interpret;
1266                 rc = interpreter (set, set->set_arg, rc);
1267         }
1268
1269         RETURN(rc);
1270 }
1271
1272 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1273 {
1274         ENTRY;
1275         if (request == NULL) {
1276                 EXIT;
1277                 return;
1278         }
1279
1280         LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1281         LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1282         LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1283         LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1284         LASSERT(request->rq_cli_ctx);
1285
1286         /* We must take it off the imp_replay_list first.  Otherwise, we'll set
1287          * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1288         if (request->rq_import != NULL) {
1289                 if (!locked)
1290                         spin_lock(&request->rq_import->imp_lock);
1291                 list_del_init(&request->rq_mod_list);
1292                 list_del_init(&request->rq_replay_list);
1293                 if (!locked)
1294                         spin_unlock(&request->rq_import->imp_lock);
1295         }
1296         LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1297
1298         if (atomic_read(&request->rq_refcount) != 0) {
1299                 DEBUG_REQ(D_ERROR, request,
1300                           "freeing request with nonzero refcount");
1301                 LBUG();
1302         }
1303
1304         if (request->rq_repbuf != NULL)
1305                 sptlrpc_cli_free_repbuf(request);
1306         if (request->rq_export != NULL) {
1307                 class_export_put(request->rq_export);
1308                 request->rq_export = NULL;
1309         }
1310         if (request->rq_import != NULL) {
1311                 class_import_put(request->rq_import);
1312                 request->rq_import = NULL;
1313         }
1314         if (request->rq_bulk != NULL)
1315                 ptlrpc_free_bulk(request->rq_bulk);
1316
1317         if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
1318                 sptlrpc_cli_free_reqbuf(request);
1319
1320         sptlrpc_req_put_ctx(request, !locked);
1321
1322         if (request->rq_pool)
1323                 __ptlrpc_free_req_to_pool(request);
1324         else
1325                 OBD_FREE(request, sizeof(*request));
1326         EXIT;
1327 }
1328
1329 void ptlrpc_free_req(struct ptlrpc_request *request)
1330 {
1331         __ptlrpc_free_req(request, 0);
1332 }
1333
1334 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1335 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1336 {
1337         LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1338         (void)__ptlrpc_req_finished(request, 1);
1339 }
1340
1341 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1342 {
1343         ENTRY;
1344         if (request == NULL)
1345                 RETURN(1);
1346
1347         if (request == LP_POISON ||
1348             request->rq_reqmsg == LP_POISON) {
1349                 CERROR("dereferencing freed request (bug 575)\n");
1350                 LBUG();
1351                 RETURN(1);
1352         }
1353
1354         DEBUG_REQ(D_INFO, request, "refcount now %u",
1355                   atomic_read(&request->rq_refcount) - 1);
1356
1357         if (atomic_dec_and_test(&request->rq_refcount)) {
1358                 __ptlrpc_free_req(request, locked);
1359                 RETURN(1);
1360         }
1361
1362         RETURN(0);
1363 }
1364
1365 void ptlrpc_req_finished(struct ptlrpc_request *request)
1366 {
1367         __ptlrpc_req_finished(request, 0);
1368 }
1369
1370 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1371 {
1372         return request->rq_xid;
1373 }
1374 EXPORT_SYMBOL(ptlrpc_req_xid);
1375
1376 /* Disengage the client's reply buffer from the network
1377  * NB does _NOT_ unregister any client-side bulk.
1378  * IDEMPOTENT, but _not_ safe against concurrent callers.
1379  * The request owner (i.e. the thread doing the I/O) must call...
1380  */
1381 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1382 {
1383         int                rc;
1384         cfs_waitq_t       *wq;
1385         struct l_wait_info lwi;
1386
1387         LASSERT(!in_interrupt ());             /* might sleep */
1388
1389         if (!ptlrpc_client_receiving_reply(request))
1390                 return;
1391
1392         LNetMDUnlink (request->rq_reply_md_h);
1393
1394         /* We have to l_wait_event() whatever the result, to give liblustre
1395          * a chance to run reply_in_callback() */
1396
1397         if (request->rq_set != NULL)
1398                 wq = &request->rq_set->set_waitq;
1399         else
1400                 wq = &request->rq_reply_waitq;
1401
1402         for (;;) {
1403                 /* Network access will complete in finite time but the HUGE
1404                  * timeout lets us CWARN for visibility of sluggish NALs */
1405                 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1406                 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1407                 if (rc == 0)
1408                         return;
1409
1410                 LASSERT (rc == -ETIMEDOUT);
1411                 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1412         }
1413 }
1414
1415 /* caller must hold imp->imp_lock */
1416 void ptlrpc_free_committed(struct obd_import *imp)
1417 {
1418         struct list_head *tmp, *saved;
1419         struct ptlrpc_request *req;
1420         struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1421         ENTRY;
1422
1423         LASSERT(imp != NULL);
1424
1425         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1426
1427
1428         if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1429             imp->imp_generation == imp->imp_last_generation_checked) {
1430                 CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
1431                        imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1432                 return;
1433         }
1434
1435         CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
1436                imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1437                imp->imp_generation);
1438         imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1439         imp->imp_last_generation_checked = imp->imp_generation;
1440
1441         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1442                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1443
1444                 /* XXX ok to remove when 1357 resolved - rread 05/29/03  */
1445                 LASSERT(req != last_req);
1446                 last_req = req;
1447
1448                 if (req->rq_import_generation < imp->imp_generation) {
1449                         DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
1450                         GOTO(free_req, 0);
1451                 }
1452
1453                 if (req->rq_replay) {
1454                         DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
1455                         continue;
1456                 }
1457
1458                 /* not yet committed */
1459                 if (req->rq_transno > imp->imp_peer_committed_transno) {
1460                         DEBUG_REQ(D_RPCTRACE, req, "stopping search");
1461                         break;
1462                 }
1463
1464                 DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
1465                           imp->imp_peer_committed_transno);
1466 free_req:
1467                 spin_lock(&req->rq_lock);
1468                 req->rq_replay = 0;
1469                 spin_unlock(&req->rq_lock);
1470                 if (req->rq_commit_cb != NULL)
1471                         req->rq_commit_cb(req);
1472                 list_del_init(&req->rq_replay_list);
1473                 __ptlrpc_req_finished(req, 1);
1474         }
1475
1476         EXIT;
1477         return;
1478 }
1479
1480 void ptlrpc_cleanup_client(struct obd_import *imp)
1481 {
1482         ENTRY;
1483         EXIT;
1484         return;
1485 }
1486
1487 void ptlrpc_resend_req(struct ptlrpc_request *req)
1488 {
1489         DEBUG_REQ(D_HA, req, "going to resend");
1490         lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1491         req->rq_status = -EAGAIN;
1492
1493         spin_lock(&req->rq_lock);
1494         req->rq_resend = 1;
1495         req->rq_net_err = 0;
1496         req->rq_timedout = 0;
1497         if (req->rq_bulk) {
1498                 __u64 old_xid = req->rq_xid;
1499
1500                 /* ensure previous bulk fails */
1501                 req->rq_xid = ptlrpc_next_xid();
1502                 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1503                        old_xid, req->rq_xid);
1504         }
1505         ptlrpc_wake_client_req(req);
1506         spin_unlock(&req->rq_lock);
1507 }
1508
1509 /* XXX: this function and rq_status are currently unused */
1510 void ptlrpc_restart_req(struct ptlrpc_request *req)
1511 {
1512         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1513         req->rq_status = -ERESTARTSYS;
1514
1515         spin_lock(&req->rq_lock);
1516         req->rq_restart = 1;
1517         req->rq_timedout = 0;
1518         ptlrpc_wake_client_req(req);
1519         spin_unlock(&req->rq_lock);
1520 }
1521
1522 static int expired_request(void *data)
1523 {
1524         struct ptlrpc_request *req = data;
1525         ENTRY;
1526
1527         /* some failure can suspend regular timeouts */
1528         if (ptlrpc_check_suspend())
1529                 RETURN(1);
1530
1531         RETURN(ptlrpc_expire_one_request(req));
1532 }
1533
1534 static void interrupted_request(void *data)
1535 {
1536         struct ptlrpc_request *req = data;
1537         DEBUG_REQ(D_HA, req, "request interrupted");
1538         spin_lock(&req->rq_lock);
1539         req->rq_intr = 1;
1540         spin_unlock(&req->rq_lock);
1541 }
1542
1543 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1544 {
1545         ENTRY;
1546         atomic_inc(&req->rq_refcount);
1547         RETURN(req);
1548 }
1549
1550 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1551                                       struct obd_import *imp)
1552 {
1553         struct list_head *tmp;
1554
1555         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1556
1557         /* clear this for new requests that were resent as well
1558            as resent replayed requests. */
1559         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1560
1561         /* don't re-add requests that have been replayed */
1562         if (!list_empty(&req->rq_replay_list))
1563                 return;
1564
1565         lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1566
1567         LASSERT(imp->imp_replayable);
1568         /* Balanced in ptlrpc_free_committed, usually. */
1569         ptlrpc_request_addref(req);
1570         list_for_each_prev(tmp, &imp->imp_replay_list) {
1571                 struct ptlrpc_request *iter =
1572                         list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1573
1574                 /* We may have duplicate transnos if we create and then
1575                  * open a file, or for closes retained if to match creating
1576                  * opens, so use req->rq_xid as a secondary key.
1577                  * (See bugs 684, 685, and 428.)
1578                  * XXX no longer needed, but all opens need transnos!
1579                  */
1580                 if (iter->rq_transno > req->rq_transno)
1581                         continue;
1582
1583                 if (iter->rq_transno == req->rq_transno) {
1584                         LASSERT(iter->rq_xid != req->rq_xid);
1585                         if (iter->rq_xid > req->rq_xid)
1586                                 continue;
1587                 }
1588
1589                 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1590                 return;
1591         }
1592
1593         list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1594 }
1595
1596 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1597 {
1598         int rc = 0;
1599         int brc;
1600         struct l_wait_info lwi;
1601         struct obd_import *imp = req->rq_import;
1602         cfs_duration_t timeout = 0;
1603         ENTRY;
1604
1605         LASSERT(req->rq_set == NULL);
1606         LASSERT(!req->rq_receiving_reply);
1607         atomic_inc(&imp->imp_inflight);
1608
1609         /* for distributed debugging */
1610         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1611         LASSERT(imp->imp_obd != NULL);
1612         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1613                "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1614                imp->imp_obd->obd_uuid.uuid,
1615                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1616                libcfs_nid2str(imp->imp_connection->c_peer.nid),
1617                lustre_msg_get_opc(req->rq_reqmsg));
1618
1619         /* Mark phase here for a little debug help */
1620         req->rq_phase = RQ_PHASE_RPC;
1621
1622         spin_lock(&imp->imp_lock);
1623 restart:
1624         req->rq_import_generation = imp->imp_generation;
1625         if (ptlrpc_import_delay_req(imp, req, &rc)) {
1626                 list_del(&req->rq_list);
1627
1628                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1629                 spin_unlock(&imp->imp_lock);
1630
1631                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1632                           cfs_curproc_comm(),
1633                           ptlrpc_import_state_name(req->rq_send_state),
1634                           ptlrpc_import_state_name(imp->imp_state));
1635                 lwi = LWI_INTR(interrupted_request, req);
1636                 rc = l_wait_event(req->rq_reply_waitq,
1637                                   (req->rq_send_state == imp->imp_state ||
1638                                    req->rq_err || req->rq_intr),
1639                                   &lwi);
1640                 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1641                           cfs_curproc_comm(),
1642                           ptlrpc_import_state_name(imp->imp_state),
1643                           ptlrpc_import_state_name(req->rq_send_state),
1644                           req->rq_err, req->rq_intr);
1645
1646                 spin_lock(&imp->imp_lock);
1647                 list_del_init(&req->rq_list);
1648
1649                 if (req->rq_err) {
1650                         rc = -EIO;
1651                 }
1652                 else if (req->rq_intr) {
1653                         rc = -EINTR;
1654                 }
1655                 else if (req->rq_no_resend) {
1656                         spin_unlock(&imp->imp_lock);
1657                         GOTO(out, rc = -ETIMEDOUT);
1658                 }
1659                 else {
1660                         GOTO(restart, rc);
1661                 }
1662         }
1663
1664         if (rc != 0) {
1665                 list_del_init(&req->rq_list);
1666                 spin_unlock(&imp->imp_lock);
1667                 req->rq_status = rc; // XXX this ok?
1668                 GOTO(out, rc);
1669         }
1670
1671         if (req->rq_resend) {
1672                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1673
1674                 if (req->rq_bulk != NULL) {
1675                         ptlrpc_unregister_bulk (req);
1676
1677                         /* bulk requests are supposed to be
1678                          * idempotent, so we are free to bump the xid
1679                          * here, which we need to do before
1680                          * registering the bulk again (bug 6371).
1681                          * print the old xid first for sanity.
1682                          */
1683                         DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1684                         req->rq_xid = ptlrpc_next_xid();
1685                 }
1686
1687                 DEBUG_REQ(D_HA, req, "resending: ");
1688         }
1689
1690         /* XXX this is the same as ptlrpc_set_wait */
1691         LASSERT(list_empty(&req->rq_list));
1692         list_add_tail(&req->rq_list, &imp->imp_sending_list);
1693         spin_unlock(&imp->imp_lock);
1694
1695         rc = sptlrpc_req_refresh_ctx(req, 0);
1696         if (rc) {
1697                 if (req->rq_err) {
1698                         /* we got fatal ctx refresh error, directly jump out
1699                          * thus we can pass back the actual error code.
1700                          */
1701                         spin_lock(&imp->imp_lock);
1702                         list_del_init(&req->rq_list);
1703                         spin_unlock(&imp->imp_lock);
1704
1705                         CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
1706                         GOTO(out, rc);
1707                 }
1708                 /* simulating we got error during send rpc */
1709                 goto after_send;
1710         }
1711
1712         rc = ptl_send_rpc(req, 0);
1713         if (rc) {
1714                 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1715                 timeout = CFS_TICK;
1716         } else {
1717                 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1718                 DEBUG_REQ(D_NET, req, 
1719                           "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1720         }
1721 repeat:
1722         lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1723                                req);
1724         rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1725         if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1726                 goto repeat;
1727
1728 after_send:
1729         CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1730                "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1731                imp->imp_obd->obd_uuid.uuid,
1732                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1733                libcfs_nid2str(imp->imp_connection->c_peer.nid),
1734                lustre_msg_get_opc(req->rq_reqmsg));
1735
1736         spin_lock(&imp->imp_lock);
1737         list_del_init(&req->rq_list);
1738         spin_unlock(&imp->imp_lock);
1739
1740         /* If the reply was received normally, this just grabs the spinlock
1741          * (ensuring the reply callback has returned), sees that
1742          * req->rq_receiving_reply is clear and returns. */
1743         ptlrpc_unregister_reply (req);
1744
1745         if (req->rq_err)
1746                 GOTO(out, rc = -EIO);
1747
1748         /* Resend if we need to, unless we were interrupted. */
1749         if (req->rq_resend && !req->rq_intr) {
1750                 /* ...unless we were specifically told otherwise. */
1751                 if (req->rq_no_resend)
1752                         GOTO(out, rc = -ETIMEDOUT);
1753                 spin_lock(&imp->imp_lock);
1754                 goto restart;
1755         }
1756
1757         if (req->rq_intr) {
1758                 /* Should only be interrupted if we timed out. */
1759                 if (!req->rq_timedout)
1760                         DEBUG_REQ(D_ERROR, req,
1761                                   "rq_intr set but rq_timedout not");
1762                 GOTO(out, rc = -EINTR);
1763         }
1764
1765         if (req->rq_timedout) {                 /* non-recoverable timeout */
1766                 GOTO(out, rc = -ETIMEDOUT);
1767         }
1768
1769         if (!req->rq_replied) {
1770                 /* How can this be? -eeb */
1771                 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1772                 LBUG();
1773                 GOTO(out, rc = req->rq_status);
1774         }
1775
1776         rc = after_reply(req);
1777         /* NB may return +ve success rc */
1778         if (req->rq_resend) {
1779                 spin_lock(&imp->imp_lock);
1780                 goto restart;
1781         }
1782
1783  out:
1784         if (req->rq_bulk != NULL) {
1785                 if (rc >= 0) {
1786                         /* success so far.  Note that anything going wrong
1787                          * with bulk now, is EXTREMELY strange, since the
1788                          * server must have believed that the bulk
1789                          * tranferred OK before she replied with success to
1790                          * me. */
1791                         lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1792                         brc = l_wait_event(req->rq_reply_waitq,
1793                                            !ptlrpc_bulk_active(req->rq_bulk),
1794                                            &lwi);
1795                         LASSERT(brc == 0 || brc == -ETIMEDOUT);
1796                         if (brc != 0) {
1797                                 LASSERT(brc == -ETIMEDOUT);
1798                                 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1799                                 rc = brc;
1800                         } else if (!req->rq_bulk->bd_success) {
1801                                 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1802                                 rc = -EIO;
1803                         }
1804                 }
1805                 if (rc < 0)
1806                         ptlrpc_unregister_bulk (req);
1807         }
1808
1809         LASSERT(!req->rq_receiving_reply);
1810         req->rq_phase = RQ_PHASE_INTERPRET;
1811
1812         atomic_dec(&imp->imp_inflight);
1813         cfs_waitq_signal(&imp->imp_recovery_waitq);
1814         RETURN(rc);
1815 }
1816
1817 struct ptlrpc_replay_async_args {
1818         int praa_old_state;
1819         int praa_old_status;
1820 };
1821
1822 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1823                                     void * data, int rc)
1824 {
1825         struct ptlrpc_replay_async_args *aa = data;
1826         struct obd_import *imp = req->rq_import;
1827
1828         ENTRY;
1829         atomic_dec(&imp->imp_replay_inflight);
1830
1831         if (!req->rq_replied) {
1832                 CERROR("request replay timed out, restarting recovery\n");
1833                 GOTO(out, rc = -ETIMEDOUT);
1834         }
1835
1836         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1837             (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
1838              lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
1839                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1840
1841         /* The transno had better not change over replay. */
1842         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1843                 lustre_msg_get_transno(req->rq_repmsg));
1844
1845         DEBUG_REQ(D_HA, req, "got rep");
1846
1847         /* let the callback do fixups, possibly including in the request */
1848         if (req->rq_replay_cb)
1849                 req->rq_replay_cb(req);
1850
1851         if (req->rq_replied &&
1852             lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1853                 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1854                           lustre_msg_get_status(req->rq_repmsg),
1855                           aa->praa_old_status);
1856         } else {
1857                 /* Put it back for re-replay. */
1858                 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
1859         }
1860
1861         /*
1862          * Errors while replay can set transno to 0, but
1863          * imp_last_replay_transno shouldn't be set to 0 anyway
1864          */
1865         if (req->rq_transno > 0) {
1866                 spin_lock(&imp->imp_lock);
1867                 LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
1868                 imp->imp_last_replay_transno = req->rq_transno;
1869                 spin_unlock(&imp->imp_lock);
1870         } else
1871                 CERROR("Transno is 0 during replay!\n");
1872         /* continue with recovery */
1873         rc = ptlrpc_import_recovery_state_machine(imp);
1874  out:
1875         req->rq_send_state = aa->praa_old_state;
1876
1877         if (rc != 0)
1878                 /* this replay failed, so restart recovery */
1879                 ptlrpc_connect_import(imp, NULL);
1880
1881         RETURN(rc);
1882 }
1883
1884 int ptlrpc_replay_req(struct ptlrpc_request *req)
1885 {
1886         struct ptlrpc_replay_async_args *aa;
1887         ENTRY;
1888
1889         LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1890
1891         /* Not handling automatic bulk replay yet (or ever?) */
1892         LASSERT(req->rq_bulk == NULL);
1893
1894         DEBUG_REQ(D_HA, req, "REPLAY");
1895
1896         LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1897         aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1898         memset(aa, 0, sizeof *aa);
1899
1900         /* Prepare request to be resent with ptlrpcd */
1901         aa->praa_old_state = req->rq_send_state;
1902         req->rq_send_state = LUSTRE_IMP_REPLAY;
1903         req->rq_phase = RQ_PHASE_NEW;
1904         aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
1905         req->rq_status = 0;
1906
1907         req->rq_interpret_reply = ptlrpc_replay_interpret;
1908         atomic_inc(&req->rq_import->imp_replay_inflight);
1909         ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1910
1911         ptlrpcd_add_req(req);
1912         RETURN(0);
1913 }
1914
1915 void ptlrpc_abort_inflight(struct obd_import *imp)
1916 {
1917         struct list_head *tmp, *n;
1918         ENTRY;
1919
1920         /* Make sure that no new requests get processed for this import.
1921          * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1922          * this flag and then putting requests on sending_list or delayed_list.
1923          */
1924         spin_lock(&imp->imp_lock);
1925
1926         /* XXX locking?  Maybe we should remove each request with the list
1927          * locked?  Also, how do we know if the requests on the list are
1928          * being freed at this time?
1929          */
1930         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1931                 struct ptlrpc_request *req =
1932                         list_entry(tmp, struct ptlrpc_request, rq_list);
1933
1934                 DEBUG_REQ(D_RPCTRACE, req, "inflight");
1935
1936                 spin_lock (&req->rq_lock);
1937                 if (req->rq_import_generation < imp->imp_generation) {
1938                         req->rq_err = 1;
1939                         ptlrpc_wake_client_req(req);
1940                 }
1941                 spin_unlock (&req->rq_lock);
1942         }
1943
1944         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1945                 struct ptlrpc_request *req =
1946                         list_entry(tmp, struct ptlrpc_request, rq_list);
1947
1948                 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
1949
1950                 spin_lock (&req->rq_lock);
1951                 if (req->rq_import_generation < imp->imp_generation) {
1952                         req->rq_err = 1;
1953                         ptlrpc_wake_client_req(req);
1954                 }
1955                 spin_unlock (&req->rq_lock);
1956         }
1957
1958         /* Last chance to free reqs left on the replay list, but we
1959          * will still leak reqs that haven't committed.  */
1960         if (imp->imp_replayable)
1961                 ptlrpc_free_committed(imp);
1962
1963         spin_unlock(&imp->imp_lock);
1964
1965         EXIT;
1966 }
1967
1968 static __u64 ptlrpc_last_xid = 0;
1969 spinlock_t ptlrpc_last_xid_lock;
1970
1971 __u64 ptlrpc_next_xid(void)
1972 {
1973         __u64 tmp;
1974         spin_lock(&ptlrpc_last_xid_lock);
1975         tmp = ++ptlrpc_last_xid;
1976         spin_unlock(&ptlrpc_last_xid_lock);
1977         return tmp;
1978 }
1979
1980 __u64 ptlrpc_sample_next_xid(void)
1981 {
1982         __u64 tmp;
1983         spin_lock(&ptlrpc_last_xid_lock);
1984         tmp = ptlrpc_last_xid + 1;
1985         spin_unlock(&ptlrpc_last_xid_lock);
1986         return tmp;
1987 }
1988 EXPORT_SYMBOL(ptlrpc_sample_next_xid);