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