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