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