Whamcloud - gitweb
b=2262
[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_req_put_ctx(request, 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 the import has been invalidated (such as by an OST
554                  * failure), the request must fail with -EIO. */
555                 if (!imp->imp_deactive)
556                         DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
557                 *status = -EIO;
558         } else if (req->rq_import_generation != imp->imp_generation) {
559                 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
560                 *status = -EIO;
561         } else if (req->rq_send_state != imp->imp_state) {
562                 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
563                     req->rq_no_delay)
564                         *status = -EWOULDBLOCK;
565                 else
566                         delay = 1;
567         }
568
569         RETURN(delay);
570 }
571
572 static int ptlrpc_check_reply(struct ptlrpc_request *req)
573 {
574         int rc = 0;
575         ENTRY;
576
577         /* serialise with network callback */
578         spin_lock(&req->rq_lock);
579
580         if (req->rq_replied)
581                 GOTO(out, rc = 1);
582
583         if (req->rq_net_err && !req->rq_timedout) {
584                 spin_unlock(&req->rq_lock);
585                 rc = ptlrpc_expire_one_request(req);
586                 spin_lock(&req->rq_lock);
587                 GOTO(out, rc);
588         }
589
590         if (req->rq_err)
591                 GOTO(out, rc = 1);
592
593         if (req->rq_resend)
594                 GOTO(out, rc = 1);
595
596         if (req->rq_restart)
597                 GOTO(out, rc = 1);
598         EXIT;
599  out:
600         spin_unlock(&req->rq_lock);
601         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
602         return rc;
603 }
604
605 static int ptlrpc_check_status(struct ptlrpc_request *req)
606 {
607         int err;
608         ENTRY;
609
610         err = lustre_msg_get_status(req->rq_repmsg);
611         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
612                 struct obd_import *imp = req->rq_import;
613                 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
614                 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
615                                 " with %s. The %s operation failed with %d\n",
616                                 libcfs_nid2str(imp->imp_connection->c_peer.nid),
617                                 ll_opcode2str(opc), err);
618                 RETURN(err < 0 ? err : -EINVAL);
619         }
620
621         if (err < 0) {
622                 DEBUG_REQ(D_INFO, req, "status is %d", err);
623         } else if (err > 0) {
624                 /* XXX: translate this error from net to host */
625                 DEBUG_REQ(D_INFO, req, "status is %d", err);
626         }
627
628         RETURN(err);
629 }
630
631 static int after_reply(struct ptlrpc_request *req)
632 {
633         struct obd_import *imp = req->rq_import;
634         int rc;
635         ENTRY;
636
637         LASSERT(!req->rq_receiving_reply);
638         LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
639
640         /* NB Until this point, the whole of the incoming message,
641          * including buflens, status etc is in the sender's byte order. */
642
643 #if SWAB_PARANOIA
644         /* Clear reply swab mask; this is a new reply in sender's byte order */
645         req->rq_rep_swab_mask = 0;
646 #endif
647         rc = sptlrpc_cli_unwrap_reply(req);
648         if (rc) {
649                 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
650                 RETURN(rc);
651         }
652
653         /* security layer unwrap might ask resend this request */
654         if (req->rq_resend)
655                 RETURN(0);
656
657         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
658         if (rc) {
659                 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
660                 RETURN(-EPROTO);
661         }
662
663         rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
664         if (rc) {
665                 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
666                 RETURN(-EPROTO);
667         }
668
669         if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
670             lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
671                 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
672                           lustre_msg_get_type(req->rq_repmsg));
673                 RETURN(-EPROTO);
674         }
675
676         rc = ptlrpc_check_status(req);
677         imp->imp_connect_error = rc;
678
679         if (rc) {
680                 /* Either we've been evicted, or the server has failed for
681                  * some reason. Try to reconnect, and if that fails, punt to
682                  * the upcall. */
683                 if (rc == -ENOTCONN || rc == -ENODEV) {
684                         if (req->rq_send_state != LUSTRE_IMP_FULL ||
685                             imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
686                                 RETURN(-ENOTCONN);
687                         }
688                         ptlrpc_request_handle_notconn(req);
689                         RETURN(rc);
690                 }
691         } else {
692                 /* Let's look if server send slv. Do it only for RPC with 
693                  * rc == 0. */
694                 if (imp->imp_obd->obd_namespace) {
695                         /* Disconnect rpc is sent when namespace is already 
696                          * destroyed. Let's check this and will not try update
697                          * pool. */
698                         ldlm_cli_update_pool(req);
699                 }
700         }
701
702         /* Store transno in reqmsg for replay. */
703         req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
704         lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
705
706         if (req->rq_import->imp_replayable) {
707                 spin_lock(&imp->imp_lock);
708                 /* no point in adding already-committed requests to the replay
709                  * list, we will just remove them immediately. b=9829 */
710                 if (req->rq_transno != 0 && 
711                     (req->rq_transno > 
712                      lustre_msg_get_last_committed(req->rq_repmsg) ||
713                      req->rq_replay))
714                         ptlrpc_retain_replayable_request(req, imp);
715                 else if (req->rq_commit_cb != NULL) {
716                         spin_unlock(&imp->imp_lock);
717                         req->rq_commit_cb(req);
718                         spin_lock(&imp->imp_lock);
719                 }
720
721                 /* Replay-enabled imports return commit-status information. */
722                 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
723                         imp->imp_peer_committed_transno =
724                                 lustre_msg_get_last_committed(req->rq_repmsg);
725                 }
726                 ptlrpc_free_committed(imp);
727                 spin_unlock(&imp->imp_lock);
728         }
729
730         RETURN(rc);
731 }
732
733 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
734 {
735         struct obd_import     *imp;
736         int rc;
737         ENTRY;
738
739         LASSERT(req->rq_phase == RQ_PHASE_NEW);
740         req->rq_phase = RQ_PHASE_RPC;
741
742         imp = req->rq_import;
743         spin_lock(&imp->imp_lock);
744
745         req->rq_import_generation = imp->imp_generation;
746
747         if (ptlrpc_import_delay_req(imp, req, &rc)) {
748                 spin_lock (&req->rq_lock);
749                 req->rq_waiting = 1;
750                 spin_unlock (&req->rq_lock);
751
752                 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
753                           "(%s != %s)",
754                           lustre_msg_get_status(req->rq_reqmsg) ,
755                           ptlrpc_import_state_name(req->rq_send_state),
756                           ptlrpc_import_state_name(imp->imp_state));
757                 LASSERT(list_empty (&req->rq_list));
758
759                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
760                 spin_unlock(&imp->imp_lock);
761                 RETURN(0);
762         }
763
764         if (rc != 0) {
765                 spin_unlock(&imp->imp_lock);
766                 req->rq_status = rc;
767                 req->rq_phase = RQ_PHASE_INTERPRET;
768                 RETURN(rc);
769         }
770
771         /* XXX this is the same as ptlrpc_queue_wait */
772         LASSERT(list_empty(&req->rq_list));
773         list_add_tail(&req->rq_list, &imp->imp_sending_list);
774         spin_unlock(&imp->imp_lock);
775
776         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
777
778         rc = sptlrpc_req_refresh_ctx(req, -1);
779         if (rc) {
780                 if (req->rq_err) {
781                         req->rq_status = rc;
782                         RETURN(1);
783                 } else {
784                         /* here begins timeout counting */
785                         req->rq_sent = CURRENT_SECONDS;
786                         req->rq_wait_ctx = 1;
787                         RETURN(0);
788                 }
789         }
790
791         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
792                " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
793                imp->imp_obd->obd_uuid.uuid,
794                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
795                libcfs_nid2str(imp->imp_connection->c_peer.nid),
796                lustre_msg_get_opc(req->rq_reqmsg));
797
798         rc = ptl_send_rpc(req, 0);
799         if (rc) {
800                 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
801                 req->rq_net_err = 1;
802                 RETURN(rc);
803         }
804         RETURN(0);
805 }
806
807 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
808 int ptlrpc_check_set(struct ptlrpc_request_set *set)
809 {
810         struct list_head *tmp;
811         int force_timer_recalc = 0;
812         ENTRY;
813
814         if (set->set_remaining == 0)
815                 RETURN(1);
816
817         list_for_each(tmp, &set->set_requests) {
818                 struct ptlrpc_request *req =
819                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
820                 struct obd_import *imp = req->rq_import;
821                 int rc = 0;
822
823                 if (req->rq_phase == RQ_PHASE_NEW &&
824                     ptlrpc_send_new_req(req)) {
825                         force_timer_recalc = 1;
826                 }
827
828                 if (!(req->rq_phase == RQ_PHASE_RPC ||
829                       req->rq_phase == RQ_PHASE_BULK ||
830                       req->rq_phase == RQ_PHASE_INTERPRET ||
831                       req->rq_phase == RQ_PHASE_COMPLETE)) {
832                         DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
833                         LBUG();
834                 }
835
836                 if (req->rq_phase == RQ_PHASE_COMPLETE)
837                         continue;
838
839                 if (req->rq_phase == RQ_PHASE_INTERPRET)
840                         GOTO(interpret, req->rq_status);
841
842                 if (req->rq_net_err && !req->rq_timedout)
843                         ptlrpc_expire_one_request(req);
844
845                 if (req->rq_err) {
846                         ptlrpc_unregister_reply(req);
847                         req->rq_replied = 0;
848                         if (req->rq_status == 0)
849                                 req->rq_status = -EIO;
850                         req->rq_phase = RQ_PHASE_INTERPRET;
851
852                         spin_lock(&imp->imp_lock);
853                         list_del_init(&req->rq_list);
854                         spin_unlock(&imp->imp_lock);
855
856                         GOTO(interpret, req->rq_status);
857                 }
858
859                 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
860                  * will only be set after rq_timedout, but the oig waiting
861                  * path sets rq_intr irrespective of whether ptlrpcd has
862                  * seen a timeout.  our policy is to only interpret
863                  * interrupted rpcs after they have timed out */
864                 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
865                                      req->rq_wait_ctx)) {
866                         /* NB could be on delayed list */
867                         ptlrpc_unregister_reply(req);
868                         req->rq_status = -EINTR;
869                         req->rq_phase = RQ_PHASE_INTERPRET;
870
871                         spin_lock(&imp->imp_lock);
872                         list_del_init(&req->rq_list);
873                         spin_unlock(&imp->imp_lock);
874
875                         GOTO(interpret, req->rq_status);
876                 }
877
878                 if (req->rq_phase == RQ_PHASE_RPC) {
879                         if (req->rq_timedout || req->rq_resend ||
880                             req->rq_waiting || req->rq_wait_ctx) {
881                                 int status;
882
883                                 /* rq_wait_ctx is only touched in ptlrpcd,
884                                  * no lock needed here.
885                                  */
886                                 if (req->rq_wait_ctx)
887                                         goto check_ctx;
888
889                                 ptlrpc_unregister_reply(req);
890
891                                 spin_lock(&imp->imp_lock);
892
893                                 if (ptlrpc_import_delay_req(imp, req, &status)){
894                                         spin_unlock(&imp->imp_lock);
895                                         continue;
896                                 }
897
898                                 list_del_init(&req->rq_list);
899                                 if (status != 0)  {
900                                         req->rq_status = status;
901                                         req->rq_phase = RQ_PHASE_INTERPRET;
902                                         spin_unlock(&imp->imp_lock);
903                                         GOTO(interpret, req->rq_status);
904                                 }
905                                 if (req->rq_no_resend && !req->rq_wait_ctx) {
906                                         req->rq_status = -ENOTCONN;
907                                         req->rq_phase = RQ_PHASE_INTERPRET;
908                                         spin_unlock(&imp->imp_lock);
909                                         GOTO(interpret, req->rq_status);
910                                 }
911                                 list_add_tail(&req->rq_list,
912                                               &imp->imp_sending_list);
913
914                                 spin_unlock(&imp->imp_lock);
915
916                                 req->rq_waiting = 0;
917                                 if (req->rq_resend) {
918                                         lustre_msg_add_flags(req->rq_reqmsg,
919                                                              MSG_RESENT);
920                                         if (req->rq_bulk) {
921                                                 __u64 old_xid = req->rq_xid;
922
923                                                 ptlrpc_unregister_bulk (req);
924
925                                                 /* ensure previous bulk fails */
926                                                 req->rq_xid = ptlrpc_next_xid();
927                                                 CDEBUG(D_HA, "resend bulk "
928                                                        "old x"LPU64
929                                                        " new x"LPU64"\n",
930                                                        old_xid, req->rq_xid);
931                                         }
932                                 }
933 check_ctx:
934                                 status = sptlrpc_req_refresh_ctx(req, -1);
935                                 if (status) {
936                                         if (req->rq_err) {
937                                                 req->rq_status = status;
938                                                 force_timer_recalc = 1;
939                                         }
940                                         if (!req->rq_wait_ctx) {
941                                                 /* begins timeout counting */
942                                                 req->rq_sent = CURRENT_SECONDS;
943                                                 req->rq_wait_ctx = 1;
944                                         }
945                                         continue;
946                                 } else {
947                                         req->rq_sent = 0;
948                                         req->rq_wait_ctx = 0;
949                                 }
950
951                                 rc = ptl_send_rpc(req, 0);
952                                 if (rc) {
953                                         DEBUG_REQ(D_HA, req, "send failed (%d)",
954                                                   rc);
955                                         force_timer_recalc = 1;
956                                         req->rq_net_err = 1;
957                                 }
958                                 /* need to reset the timeout */
959                                 force_timer_recalc = 1;
960                         }
961
962                         /* Still waiting for a reply? */
963                         if (ptlrpc_client_receiving_reply(req))
964                                 continue;
965
966                         /* Did we actually receive a reply? */
967                         if (!ptlrpc_client_replied(req))
968                                 continue;
969
970                         spin_lock(&imp->imp_lock);
971                         list_del_init(&req->rq_list);
972                         spin_unlock(&imp->imp_lock);
973
974                         req->rq_status = after_reply(req);
975                         if (req->rq_resend) {
976                                 /* Add this req to the delayed list so
977                                    it can be errored if the import is
978                                    evicted after recovery. */
979                                 spin_lock(&imp->imp_lock);
980                                 list_add_tail(&req->rq_list,
981                                               &imp->imp_delayed_list);
982                                 spin_unlock(&imp->imp_lock);
983                                 continue;
984                         }
985
986                         /* If there is no bulk associated with this request,
987                          * then we're done and should let the interpreter
988                          * process the reply.  Similarly if the RPC returned
989                          * an error, and therefore the bulk will never arrive.
990                          */
991                         if (req->rq_bulk == NULL || req->rq_status != 0) {
992                                 req->rq_phase = RQ_PHASE_INTERPRET;
993                                 GOTO(interpret, req->rq_status);
994                         }
995
996                         req->rq_phase = RQ_PHASE_BULK;
997                 }
998
999                 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1000                 if (ptlrpc_bulk_active(req->rq_bulk))
1001                         continue;
1002
1003                 if (!req->rq_bulk->bd_success) {
1004                         /* The RPC reply arrived OK, but the bulk screwed
1005                          * up!  Dead wierd since the server told us the RPC
1006                          * was good after getting the REPLY for her GET or
1007                          * the ACK for her PUT. */
1008                         DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1009                         LBUG();
1010                 }
1011
1012                 req->rq_phase = RQ_PHASE_INTERPRET;
1013
1014         interpret:
1015                 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1016                 LASSERT(!req->rq_receiving_reply);
1017
1018                 ptlrpc_unregister_reply(req);
1019                 if (req->rq_bulk != NULL)
1020                         ptlrpc_unregister_bulk (req);
1021
1022                 req->rq_phase = RQ_PHASE_COMPLETE;
1023
1024                 if (req->rq_interpret_reply != NULL) {
1025                         int (*interpreter)(struct ptlrpc_request *,void *,int) =
1026                                 req->rq_interpret_reply;
1027                         req->rq_status = interpreter(req, &req->rq_async_args,
1028                                                      req->rq_status);
1029                 }
1030
1031                 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1032                        "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1033                        imp->imp_obd->obd_uuid.uuid,
1034                        lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1035                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1036                        lustre_msg_get_opc(req->rq_reqmsg));
1037
1038                 set->set_remaining--;
1039
1040                 atomic_dec(&imp->imp_inflight);
1041                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1042         }
1043
1044         /* If we hit an error, we want to recover promptly. */
1045         RETURN(set->set_remaining == 0 || force_timer_recalc);
1046 }
1047
1048 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
1049 {
1050         struct obd_import *imp = req->rq_import;
1051         int rc = 0;
1052         ENTRY;
1053
1054         DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, %lus ago)",
1055                   req->rq_net_err ? "network error" : "timeout",
1056                   (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
1057
1058         if (imp != NULL && obd_debug_peer_on_timeout)
1059                 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1060
1061         spin_lock(&req->rq_lock);
1062         req->rq_timedout = 1;
1063         req->rq_wait_ctx = 0;
1064         spin_unlock(&req->rq_lock);
1065
1066         ptlrpc_unregister_reply (req);
1067
1068         if (obd_dump_on_timeout)
1069                 libcfs_debug_dumplog();
1070
1071         if (req->rq_bulk != NULL)
1072                 ptlrpc_unregister_bulk (req);
1073
1074         if (imp == NULL) {
1075                 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1076                 RETURN(1);
1077         }
1078
1079         /* The DLM server doesn't want recovery run on its imports. */
1080         if (imp->imp_dlm_fake)
1081                 RETURN(1);
1082
1083         /* If this request is for recovery or other primordial tasks,
1084          * then error it out here. */
1085         if (req->rq_ctx_init || req->rq_ctx_fini ||
1086             req->rq_send_state != LUSTRE_IMP_FULL ||
1087             imp->imp_obd->obd_no_recov) {
1088                 spin_lock(&req->rq_lock);
1089                 req->rq_status = -ETIMEDOUT;
1090                 req->rq_err = 1;
1091                 spin_unlock(&req->rq_lock);
1092                 RETURN(1);
1093         }
1094         
1095         /* if request can't be resend we can't wait answer after timeout */
1096         if (req->rq_no_resend) {
1097                 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1098                 rc = 1;
1099         }
1100
1101         ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1102
1103         RETURN(rc);
1104 }
1105
1106 int ptlrpc_expired_set(void *data)
1107 {
1108         struct ptlrpc_request_set *set = data;
1109         struct list_head          *tmp;
1110         time_t                     now = CURRENT_SECONDS;
1111         ENTRY;
1112
1113         LASSERT(set != NULL);
1114
1115         /* A timeout expired; see which reqs it applies to... */
1116         list_for_each (tmp, &set->set_requests) {
1117                 struct ptlrpc_request *req =
1118                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1119
1120                 /* request in-flight? */
1121                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1122                        !req->rq_resend) ||
1123                       (req->rq_phase == RQ_PHASE_BULK)))
1124                         continue;
1125
1126                 if (req->rq_timedout ||           /* already dealt with */
1127                     req->rq_sent + req->rq_timeout > now) /* not expired */
1128                         continue;
1129
1130                 /* deal with this guy */
1131                 ptlrpc_expire_one_request (req);
1132         }
1133
1134         /* When waiting for a whole set, we always to break out of the
1135          * sleep so we can recalculate the timeout, or enable interrupts
1136          * iff everyone's timed out.
1137          */
1138         RETURN(1);
1139 }
1140
1141 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1142 {
1143         spin_lock(&req->rq_lock);
1144         req->rq_intr = 1;
1145         spin_unlock(&req->rq_lock);
1146 }
1147
1148 void ptlrpc_interrupted_set(void *data)
1149 {
1150         struct ptlrpc_request_set *set = data;
1151         struct list_head *tmp;
1152
1153         LASSERT(set != NULL);
1154         CERROR("INTERRUPTED SET %p\n", set);
1155
1156         list_for_each(tmp, &set->set_requests) {
1157                 struct ptlrpc_request *req =
1158                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1159
1160                 if (req->rq_phase != RQ_PHASE_RPC)
1161                         continue;
1162
1163                 ptlrpc_mark_interrupted(req);
1164         }
1165 }
1166
1167 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1168 {
1169         struct list_head      *tmp;
1170         time_t                 now = CURRENT_SECONDS;
1171         time_t                 deadline;
1172         int                    timeout = 0;
1173         struct ptlrpc_request *req;
1174         ENTRY;
1175
1176         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1177
1178         list_for_each(tmp, &set->set_requests) {
1179                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1180
1181                 /* request in-flight? */
1182                 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1183                       (req->rq_phase == RQ_PHASE_BULK)))
1184                         continue;
1185
1186                 if (req->rq_timedout)   /* already timed out */
1187                         continue;
1188
1189                 deadline = req->rq_sent + req->rq_timeout;
1190                 if (deadline <= now)    /* actually expired already */
1191                         timeout = 1;    /* ASAP */
1192                 else if (timeout == 0 || timeout > deadline - now)
1193                         timeout = deadline - now;
1194         }
1195         RETURN(timeout);
1196 }
1197
1198 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1199 {
1200         struct list_head      *tmp;
1201         struct ptlrpc_request *req;
1202         struct l_wait_info     lwi;
1203         int                    rc, timeout;
1204         ENTRY;
1205
1206         if (list_empty(&set->set_requests))
1207                 RETURN(0);
1208
1209         list_for_each(tmp, &set->set_requests) {
1210                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1211                 if (req->rq_phase == RQ_PHASE_NEW)
1212                         (void)ptlrpc_send_new_req(req);
1213         }
1214
1215         do {
1216                 timeout = ptlrpc_set_next_timeout(set);
1217
1218                 /* wait until all complete, interrupted, or an in-flight
1219                  * req times out */
1220                 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
1221                        set, timeout);
1222                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1223                                        ptlrpc_expired_set,
1224                                        ptlrpc_interrupted_set, set);
1225                 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1226
1227                 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1228
1229                 /* -EINTR => all requests have been flagged rq_intr so next
1230                  * check completes.
1231                  * -ETIMEOUTD => someone timed out.  When all reqs have
1232                  * timed out, signals are enabled allowing completion with
1233                  * EINTR.
1234                  * I don't really care if we go once more round the loop in
1235                  * the error cases -eeb. */
1236         } while (rc != 0 || set->set_remaining != 0);
1237
1238         LASSERT(set->set_remaining == 0);
1239
1240         rc = 0;
1241         list_for_each(tmp, &set->set_requests) {
1242                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1243
1244                 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1245                 if (req->rq_status != 0)
1246                         rc = req->rq_status;
1247         }
1248
1249         if (set->set_interpret != NULL) {
1250                 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1251                         set->set_interpret;
1252                 rc = interpreter (set, set->set_arg, rc);
1253         }
1254
1255         RETURN(rc);
1256 }
1257
1258 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1259 {
1260         ENTRY;
1261         if (request == NULL) {
1262                 EXIT;
1263                 return;
1264         }
1265
1266         LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1267         LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1268         LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1269         LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1270         LASSERT(request->rq_cli_ctx);
1271
1272         /* We must take it off the imp_replay_list first.  Otherwise, we'll set
1273          * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1274         if (request->rq_import != NULL) {
1275                 if (!locked)
1276                         spin_lock(&request->rq_import->imp_lock);
1277                 list_del_init(&request->rq_mod_list);
1278                 list_del_init(&request->rq_replay_list);
1279                 if (!locked)
1280                         spin_unlock(&request->rq_import->imp_lock);
1281         }
1282         LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1283
1284         if (atomic_read(&request->rq_refcount) != 0) {
1285                 DEBUG_REQ(D_ERROR, request,
1286                           "freeing request with nonzero refcount");
1287                 LBUG();
1288         }
1289
1290         if (request->rq_repbuf != NULL)
1291                 sptlrpc_cli_free_repbuf(request);
1292         if (request->rq_export != NULL) {
1293                 class_export_put(request->rq_export);
1294                 request->rq_export = NULL;
1295         }
1296         if (request->rq_import != NULL) {
1297                 class_import_put(request->rq_import);
1298                 request->rq_import = NULL;
1299         }
1300         if (request->rq_bulk != NULL)
1301                 ptlrpc_free_bulk(request->rq_bulk);
1302
1303         if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
1304                 sptlrpc_cli_free_reqbuf(request);
1305
1306         sptlrpc_req_put_ctx(request, !locked);
1307
1308         if (request->rq_pool)
1309                 __ptlrpc_free_req_to_pool(request);
1310         else
1311                 OBD_FREE(request, sizeof(*request));
1312         EXIT;
1313 }
1314
1315 void ptlrpc_free_req(struct ptlrpc_request *request)
1316 {
1317         __ptlrpc_free_req(request, 0);
1318 }
1319
1320 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1321 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1322 {
1323         LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1324         (void)__ptlrpc_req_finished(request, 1);
1325 }
1326
1327 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1328 {
1329         ENTRY;
1330         if (request == NULL)
1331                 RETURN(1);
1332
1333         if (request == LP_POISON ||
1334             request->rq_reqmsg == LP_POISON) {
1335                 CERROR("dereferencing freed request (bug 575)\n");
1336                 LBUG();
1337                 RETURN(1);
1338         }
1339
1340         DEBUG_REQ(D_INFO, request, "refcount now %u",
1341                   atomic_read(&request->rq_refcount) - 1);
1342
1343         if (atomic_dec_and_test(&request->rq_refcount)) {
1344                 __ptlrpc_free_req(request, locked);
1345                 RETURN(1);
1346         }
1347
1348         RETURN(0);
1349 }
1350
1351 void ptlrpc_req_finished(struct ptlrpc_request *request)
1352 {
1353         __ptlrpc_req_finished(request, 0);
1354 }
1355
1356 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1357 {
1358         return request->rq_xid;
1359 }
1360 EXPORT_SYMBOL(ptlrpc_req_xid);
1361
1362 /* Disengage the client's reply buffer from the network
1363  * NB does _NOT_ unregister any client-side bulk.
1364  * IDEMPOTENT, but _not_ safe against concurrent callers.
1365  * The request owner (i.e. the thread doing the I/O) must call...
1366  */
1367 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1368 {
1369         int                rc;
1370         cfs_waitq_t       *wq;
1371         struct l_wait_info lwi;
1372
1373         LASSERT(!in_interrupt ());             /* might sleep */
1374
1375         if (!ptlrpc_client_receiving_reply(request))
1376                 return;
1377
1378         LNetMDUnlink (request->rq_reply_md_h);
1379
1380         /* We have to l_wait_event() whatever the result, to give liblustre
1381          * a chance to run reply_in_callback() */
1382
1383         if (request->rq_set != NULL)
1384                 wq = &request->rq_set->set_waitq;
1385         else
1386                 wq = &request->rq_reply_waitq;
1387
1388         for (;;) {
1389                 /* Network access will complete in finite time but the HUGE
1390                  * timeout lets us CWARN for visibility of sluggish NALs */
1391                 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1392                 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1393                 if (rc == 0)
1394                         return;
1395
1396                 LASSERT (rc == -ETIMEDOUT);
1397                 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1398         }
1399 }
1400
1401 /* caller must hold imp->imp_lock */
1402 void ptlrpc_free_committed(struct obd_import *imp)
1403 {
1404         struct list_head *tmp, *saved;
1405         struct ptlrpc_request *req;
1406         struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1407         ENTRY;
1408
1409         LASSERT(imp != NULL);
1410
1411         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1412
1413
1414         if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1415             imp->imp_generation == imp->imp_last_generation_checked) {
1416                 CDEBUG(D_HA, "%s: skip recheck for last_committed "LPU64"\n",
1417                        imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1418                 return;
1419         }
1420         
1421         CDEBUG(D_HA, "%s: committing for last_committed "LPU64" gen %d\n",
1422                imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1423                imp->imp_generation);
1424         imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1425         imp->imp_last_generation_checked = imp->imp_generation;
1426
1427         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1428                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1429
1430                 /* XXX ok to remove when 1357 resolved - rread 05/29/03  */
1431                 LASSERT(req != last_req);
1432                 last_req = req;
1433
1434                 if (req->rq_import_generation < imp->imp_generation) {
1435                         DEBUG_REQ(D_HA, req, "freeing request with old gen");
1436                         GOTO(free_req, 0);
1437                 }
1438
1439                 if (req->rq_replay) {
1440                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1441                         continue;
1442                 }
1443
1444                 /* not yet committed */
1445                 if (req->rq_transno > imp->imp_peer_committed_transno) {
1446                         DEBUG_REQ(D_HA, req, "stopping search");
1447                         break;
1448                 }
1449
1450                 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1451                           imp->imp_peer_committed_transno);
1452 free_req:
1453                 spin_lock(&req->rq_lock);
1454                 req->rq_replay = 0;
1455                 spin_unlock(&req->rq_lock);
1456                 if (req->rq_commit_cb != NULL)
1457                         req->rq_commit_cb(req);
1458                 list_del_init(&req->rq_replay_list);
1459                 __ptlrpc_req_finished(req, 1);
1460         }
1461
1462         EXIT;
1463         return;
1464 }
1465
1466 void ptlrpc_cleanup_client(struct obd_import *imp)
1467 {
1468         ENTRY;
1469         EXIT;
1470         return;
1471 }
1472
1473 void ptlrpc_resend_req(struct ptlrpc_request *req)
1474 {
1475         DEBUG_REQ(D_HA, req, "going to resend");
1476         lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1477         req->rq_status = -EAGAIN;
1478
1479         spin_lock(&req->rq_lock);
1480         req->rq_resend = 1;
1481         req->rq_net_err = 0;
1482         req->rq_timedout = 0;
1483         if (req->rq_bulk) {
1484                 __u64 old_xid = req->rq_xid;
1485
1486                 /* ensure previous bulk fails */
1487                 req->rq_xid = ptlrpc_next_xid();
1488                 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1489                        old_xid, req->rq_xid);
1490         }
1491         ptlrpc_wake_client_req(req);
1492         spin_unlock(&req->rq_lock);
1493 }
1494
1495 /* XXX: this function and rq_status are currently unused */
1496 void ptlrpc_restart_req(struct ptlrpc_request *req)
1497 {
1498         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1499         req->rq_status = -ERESTARTSYS;
1500
1501         spin_lock(&req->rq_lock);
1502         req->rq_restart = 1;
1503         req->rq_timedout = 0;
1504         ptlrpc_wake_client_req(req);
1505         spin_unlock(&req->rq_lock);
1506 }
1507
1508 static int expired_request(void *data)
1509 {
1510         struct ptlrpc_request *req = data;
1511         ENTRY;
1512
1513         /* some failure can suspend regular timeouts */
1514         if (ptlrpc_check_suspend())
1515                 RETURN(1);
1516
1517         RETURN(ptlrpc_expire_one_request(req));
1518 }
1519
1520 static void interrupted_request(void *data)
1521 {
1522         struct ptlrpc_request *req = data;
1523         DEBUG_REQ(D_HA, req, "request interrupted");
1524         spin_lock(&req->rq_lock);
1525         req->rq_intr = 1;
1526         spin_unlock(&req->rq_lock);
1527 }
1528
1529 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1530 {
1531         ENTRY;
1532         atomic_inc(&req->rq_refcount);
1533         RETURN(req);
1534 }
1535
1536 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1537                                       struct obd_import *imp)
1538 {
1539         struct list_head *tmp;
1540
1541         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1542
1543         /* clear this for new requests that were resent as well
1544            as resent replayed requests. */
1545         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1546
1547         /* don't re-add requests that have been replayed */
1548         if (!list_empty(&req->rq_replay_list))
1549                 return;
1550
1551         lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1552
1553         LASSERT(imp->imp_replayable);
1554         /* Balanced in ptlrpc_free_committed, usually. */
1555         ptlrpc_request_addref(req);
1556         list_for_each_prev(tmp, &imp->imp_replay_list) {
1557                 struct ptlrpc_request *iter =
1558                         list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1559
1560                 /* We may have duplicate transnos if we create and then
1561                  * open a file, or for closes retained if to match creating
1562                  * opens, so use req->rq_xid as a secondary key.
1563                  * (See bugs 684, 685, and 428.)
1564                  * XXX no longer needed, but all opens need transnos!
1565                  */
1566                 if (iter->rq_transno > req->rq_transno)
1567                         continue;
1568
1569                 if (iter->rq_transno == req->rq_transno) {
1570                         LASSERT(iter->rq_xid != req->rq_xid);
1571                         if (iter->rq_xid > req->rq_xid)
1572                                 continue;
1573                 }
1574
1575                 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1576                 return;
1577         }
1578
1579         list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1580 }
1581
1582 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1583 {
1584         int rc = 0;
1585         int brc;
1586         struct l_wait_info lwi;
1587         struct obd_import *imp = req->rq_import;
1588         cfs_duration_t timeout = 0;
1589         ENTRY;
1590
1591         LASSERT(req->rq_set == NULL);
1592         LASSERT(!req->rq_receiving_reply);
1593         atomic_inc(&imp->imp_inflight);
1594
1595         /* for distributed debugging */
1596         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1597         LASSERT(imp->imp_obd != NULL);
1598         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1599                "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1600                imp->imp_obd->obd_uuid.uuid,
1601                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1602                libcfs_nid2str(imp->imp_connection->c_peer.nid),
1603                lustre_msg_get_opc(req->rq_reqmsg));
1604
1605         /* Mark phase here for a little debug help */
1606         req->rq_phase = RQ_PHASE_RPC;
1607
1608         spin_lock(&imp->imp_lock);
1609         req->rq_import_generation = imp->imp_generation;
1610 restart:
1611         if (ptlrpc_import_delay_req(imp, req, &rc)) {
1612                 list_del(&req->rq_list);
1613
1614                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1615                 spin_unlock(&imp->imp_lock);
1616
1617                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1618                           cfs_curproc_comm(),
1619                           ptlrpc_import_state_name(req->rq_send_state),
1620                           ptlrpc_import_state_name(imp->imp_state));
1621                 lwi = LWI_INTR(interrupted_request, req);
1622                 rc = l_wait_event(req->rq_reply_waitq,
1623                                   (req->rq_send_state == imp->imp_state ||
1624                                    req->rq_err || req->rq_intr),
1625                                   &lwi);
1626                 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1627                           cfs_curproc_comm(),
1628                           ptlrpc_import_state_name(imp->imp_state),
1629                           ptlrpc_import_state_name(req->rq_send_state),
1630                           req->rq_err, req->rq_intr);
1631
1632                 spin_lock(&imp->imp_lock);
1633                 list_del_init(&req->rq_list);
1634
1635                 if (req->rq_err) {
1636                         rc = -EIO;
1637                 }
1638                 else if (req->rq_intr) {
1639                         rc = -EINTR;
1640                 }
1641                 else if (req->rq_no_resend) {
1642                         spin_unlock(&imp->imp_lock);
1643                         GOTO(out, rc = -ETIMEDOUT);
1644                 }
1645                 else {
1646                         GOTO(restart, rc);
1647                 }
1648         }
1649
1650         if (rc != 0) {
1651                 list_del_init(&req->rq_list);
1652                 spin_unlock(&imp->imp_lock);
1653                 req->rq_status = rc; // XXX this ok?
1654                 GOTO(out, rc);
1655         }
1656
1657         if (req->rq_resend) {
1658                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1659
1660                 if (req->rq_bulk != NULL) {
1661                         ptlrpc_unregister_bulk (req);
1662
1663                         /* bulk requests are supposed to be
1664                          * idempotent, so we are free to bump the xid
1665                          * here, which we need to do before
1666                          * registering the bulk again (bug 6371).
1667                          * print the old xid first for sanity.
1668                          */
1669                         DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1670                         req->rq_xid = ptlrpc_next_xid();
1671                 }
1672
1673                 DEBUG_REQ(D_HA, req, "resending: ");
1674         }
1675
1676         /* XXX this is the same as ptlrpc_set_wait */
1677         LASSERT(list_empty(&req->rq_list));
1678         list_add_tail(&req->rq_list, &imp->imp_sending_list);
1679         spin_unlock(&imp->imp_lock);
1680
1681         rc = sptlrpc_req_refresh_ctx(req, 0);
1682         if (rc) {
1683                 if (req->rq_err) {
1684                         /* we got fatal ctx refresh error, directly jump out
1685                          * thus we can pass back the actual error code.
1686                          */
1687                         spin_lock(&imp->imp_lock);
1688                         list_del_init(&req->rq_list);
1689                         spin_unlock(&imp->imp_lock);
1690
1691                         CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
1692                         GOTO(out, rc);
1693                 }
1694                 /* simulating we got error during send rpc */
1695                 goto after_send;
1696         }
1697
1698         rc = ptl_send_rpc(req, 0);
1699         if (rc) {
1700                 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1701                 timeout = CFS_TICK;
1702         } else {
1703                 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1704                 DEBUG_REQ(D_NET, req, 
1705                           "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1706         }
1707 repeat:
1708         lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1709                                req);
1710         rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1711         if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1712                 goto repeat;
1713
1714 after_send:
1715         CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1716                "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1717                imp->imp_obd->obd_uuid.uuid,
1718                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1719                libcfs_nid2str(imp->imp_connection->c_peer.nid),
1720                lustre_msg_get_opc(req->rq_reqmsg));
1721
1722         spin_lock(&imp->imp_lock);
1723         list_del_init(&req->rq_list);
1724         spin_unlock(&imp->imp_lock);
1725
1726         /* If the reply was received normally, this just grabs the spinlock
1727          * (ensuring the reply callback has returned), sees that
1728          * req->rq_receiving_reply is clear and returns. */
1729         ptlrpc_unregister_reply (req);
1730
1731         if (req->rq_err)
1732                 GOTO(out, rc = -EIO);
1733
1734         /* Resend if we need to, unless we were interrupted. */
1735         if (req->rq_resend && !req->rq_intr) {
1736                 /* ...unless we were specifically told otherwise. */
1737                 if (req->rq_no_resend)
1738                         GOTO(out, rc = -ETIMEDOUT);
1739                 spin_lock(&imp->imp_lock);
1740                 goto restart;
1741         }
1742
1743         if (req->rq_intr) {
1744                 /* Should only be interrupted if we timed out. */
1745                 if (!req->rq_timedout)
1746                         DEBUG_REQ(D_ERROR, req,
1747                                   "rq_intr set but rq_timedout not");
1748                 GOTO(out, rc = -EINTR);
1749         }
1750
1751         if (req->rq_timedout) {                 /* non-recoverable timeout */
1752                 GOTO(out, rc = -ETIMEDOUT);
1753         }
1754
1755         if (!req->rq_replied) {
1756                 /* How can this be? -eeb */
1757                 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1758                 LBUG();
1759                 GOTO(out, rc = req->rq_status);
1760         }
1761
1762         rc = after_reply(req);
1763         /* NB may return +ve success rc */
1764         if (req->rq_resend) {
1765                 spin_lock(&imp->imp_lock);
1766                 goto restart;
1767         }
1768
1769  out:
1770         if (req->rq_bulk != NULL) {
1771                 if (rc >= 0) {
1772                         /* success so far.  Note that anything going wrong
1773                          * with bulk now, is EXTREMELY strange, since the
1774                          * server must have believed that the bulk
1775                          * tranferred OK before she replied with success to
1776                          * me. */
1777                         lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1778                         brc = l_wait_event(req->rq_reply_waitq,
1779                                            !ptlrpc_bulk_active(req->rq_bulk),
1780                                            &lwi);
1781                         LASSERT(brc == 0 || brc == -ETIMEDOUT);
1782                         if (brc != 0) {
1783                                 LASSERT(brc == -ETIMEDOUT);
1784                                 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1785                                 rc = brc;
1786                         } else if (!req->rq_bulk->bd_success) {
1787                                 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1788                                 rc = -EIO;
1789                         }
1790                 }
1791                 if (rc < 0)
1792                         ptlrpc_unregister_bulk (req);
1793         }
1794
1795         LASSERT(!req->rq_receiving_reply);
1796         req->rq_phase = RQ_PHASE_INTERPRET;
1797
1798         atomic_dec(&imp->imp_inflight);
1799         cfs_waitq_signal(&imp->imp_recovery_waitq);
1800         RETURN(rc);
1801 }
1802
1803 struct ptlrpc_replay_async_args {
1804         int praa_old_state;
1805         int praa_old_status;
1806 };
1807
1808 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1809                                     void * data, int rc)
1810 {
1811         struct ptlrpc_replay_async_args *aa = data;
1812         struct obd_import *imp = req->rq_import;
1813
1814         ENTRY;
1815         atomic_dec(&imp->imp_replay_inflight);
1816
1817         if (!req->rq_replied) {
1818                 CERROR("request replay timed out, restarting recovery\n");
1819                 GOTO(out, rc = -ETIMEDOUT);
1820         }
1821
1822         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1823             lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN)
1824                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1825
1826         /* The transno had better not change over replay. */
1827         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1828                 lustre_msg_get_transno(req->rq_repmsg));
1829
1830         DEBUG_REQ(D_HA, req, "got rep");
1831
1832         /* let the callback do fixups, possibly including in the request */
1833         if (req->rq_replay_cb)
1834                 req->rq_replay_cb(req);
1835
1836         if (req->rq_replied &&
1837             lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1838                 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1839                           lustre_msg_get_status(req->rq_repmsg),
1840                           aa->praa_old_status);
1841         } else {
1842                 /* Put it back for re-replay. */
1843                 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
1844         }
1845
1846         /*
1847          * Errors while replay can set transno to 0, but
1848          * imp_last_replay_transno shouldn't be set to 0 anyway
1849          */
1850         if (req->rq_transno > 0) {
1851                 spin_lock(&imp->imp_lock);
1852                 LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
1853                 imp->imp_last_replay_transno = req->rq_transno;
1854                 spin_unlock(&imp->imp_lock);
1855         } else
1856                 CERROR("Transno is 0 during replay!\n");
1857         /* continue with recovery */
1858         rc = ptlrpc_import_recovery_state_machine(imp);
1859  out:
1860         req->rq_send_state = aa->praa_old_state;
1861
1862         if (rc != 0)
1863                 /* this replay failed, so restart recovery */
1864                 ptlrpc_connect_import(imp, NULL);
1865
1866         RETURN(rc);
1867 }
1868
1869 int ptlrpc_replay_req(struct ptlrpc_request *req)
1870 {
1871         struct ptlrpc_replay_async_args *aa;
1872         ENTRY;
1873
1874         LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1875
1876         /* Not handling automatic bulk replay yet (or ever?) */
1877         LASSERT(req->rq_bulk == NULL);
1878
1879         DEBUG_REQ(D_HA, req, "REPLAY");
1880
1881         LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1882         aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1883         memset(aa, 0, sizeof *aa);
1884
1885         /* Prepare request to be resent with ptlrpcd */
1886         aa->praa_old_state = req->rq_send_state;
1887         req->rq_send_state = LUSTRE_IMP_REPLAY;
1888         req->rq_phase = RQ_PHASE_NEW;
1889         aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
1890         req->rq_status = 0;
1891
1892         req->rq_interpret_reply = ptlrpc_replay_interpret;
1893         atomic_inc(&req->rq_import->imp_replay_inflight);
1894         ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1895
1896         ptlrpcd_add_req(req);
1897         RETURN(0);
1898 }
1899
1900 void ptlrpc_abort_inflight(struct obd_import *imp)
1901 {
1902         struct list_head *tmp, *n;
1903         ENTRY;
1904
1905         /* Make sure that no new requests get processed for this import.
1906          * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1907          * this flag and then putting requests on sending_list or delayed_list.
1908          */
1909         spin_lock(&imp->imp_lock);
1910
1911         /* XXX locking?  Maybe we should remove each request with the list
1912          * locked?  Also, how do we know if the requests on the list are
1913          * being freed at this time?
1914          */
1915         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1916                 struct ptlrpc_request *req =
1917                         list_entry(tmp, struct ptlrpc_request, rq_list);
1918
1919                 DEBUG_REQ(D_HA, req, "inflight");
1920
1921                 spin_lock (&req->rq_lock);
1922                 if (req->rq_import_generation < imp->imp_generation) {
1923                         req->rq_err = 1;
1924                         ptlrpc_wake_client_req(req);
1925                 }
1926                 spin_unlock (&req->rq_lock);
1927         }
1928
1929         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1930                 struct ptlrpc_request *req =
1931                         list_entry(tmp, struct ptlrpc_request, rq_list);
1932
1933                 DEBUG_REQ(D_HA, req, "aborting waiting req");
1934
1935                 spin_lock (&req->rq_lock);
1936                 if (req->rq_import_generation < imp->imp_generation) {
1937                         req->rq_err = 1;
1938                         ptlrpc_wake_client_req(req);
1939                 }
1940                 spin_unlock (&req->rq_lock);
1941         }
1942
1943         /* Last chance to free reqs left on the replay list, but we
1944          * will still leak reqs that haven't committed.  */
1945         if (imp->imp_replayable)
1946                 ptlrpc_free_committed(imp);
1947
1948         spin_unlock(&imp->imp_lock);
1949
1950         EXIT;
1951 }
1952
1953 static __u64 ptlrpc_last_xid = 0;
1954 spinlock_t ptlrpc_last_xid_lock;
1955
1956 __u64 ptlrpc_next_xid(void)
1957 {
1958         __u64 tmp;
1959         spin_lock(&ptlrpc_last_xid_lock);
1960         tmp = ++ptlrpc_last_xid;
1961         spin_unlock(&ptlrpc_last_xid_lock);
1962         return tmp;
1963 }
1964
1965 __u64 ptlrpc_sample_next_xid(void)
1966 {
1967         __u64 tmp;
1968         spin_lock(&ptlrpc_last_xid_lock);
1969         tmp = ptlrpc_last_xid + 1;
1970         spin_unlock(&ptlrpc_last_xid_lock);
1971         return tmp;
1972 }
1973 EXPORT_SYMBOL(ptlrpc_sample_next_xid);