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