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