Whamcloud - gitweb
add ability to resend request if it isn't fit in reply buffer.
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_RPC
38 #ifndef __KERNEL__
39 #include <errno.h>
40 #include <signal.h>
41 #include <liblustre.h>
42 #endif
43
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_lib.h>
47 #include <lustre_ha.h>
48 #include <lustre_import.h>
49
50 #include "ptlrpc_internal.h"
51
52 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
53                         struct ptlrpc_client *cl)
54 {
55         cl->cli_request_portal = req_portal;
56         cl->cli_reply_portal   = rep_portal;
57         cl->cli_name           = name;
58 }
59
60 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
61 {
62         struct ptlrpc_connection *c;
63         lnet_nid_t                self;
64         lnet_process_id_t         peer;
65         int                       err;
66
67         err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
68         if (err != 0) {
69                 CERROR("cannot find peer %s!\n", uuid->uuid);
70                 return NULL;
71         }
72
73         c = ptlrpc_connection_get(peer, self, uuid);
74         if (c) {
75                 memcpy(c->c_remote_uuid.uuid,
76                        uuid->uuid, sizeof(c->c_remote_uuid.uuid));
77         }
78
79         CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
80
81         return c;
82 }
83
84 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
85 {
86         struct ptlrpc_bulk_desc *desc;
87
88         OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
89         if (!desc)
90                 return NULL;
91
92         spin_lock_init(&desc->bd_lock);
93         cfs_waitq_init(&desc->bd_waitq);
94         desc->bd_max_iov = npages;
95         desc->bd_iov_count = 0;
96         desc->bd_md_h = LNET_INVALID_HANDLE;
97         desc->bd_portal = portal;
98         desc->bd_type = type;
99
100         return desc;
101 }
102
103 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
104                                               int npages, int type, int portal)
105 {
106         struct obd_import *imp = req->rq_import;
107         struct ptlrpc_bulk_desc *desc;
108
109         ENTRY;
110         LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
111         desc = new_bulk(npages, type, portal);
112         if (desc == NULL)
113                 RETURN(NULL);
114
115         desc->bd_import_generation = req->rq_import_generation;
116         desc->bd_import = class_import_get(imp);
117         desc->bd_req = req;
118
119         desc->bd_cbid.cbid_fn  = client_bulk_callback;
120         desc->bd_cbid.cbid_arg = desc;
121
122         /* This makes req own desc, and free it when she frees herself */
123         req->rq_bulk = desc;
124
125         return desc;
126 }
127
128 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
129                                               int npages, int type, int portal)
130 {
131         struct obd_export *exp = req->rq_export;
132         struct ptlrpc_bulk_desc *desc;
133
134         ENTRY;
135         LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
136
137         desc = new_bulk(npages, type, portal);
138         if (desc == NULL)
139                 RETURN(NULL);
140
141         desc->bd_export = class_export_get(exp);
142         desc->bd_req = req;
143
144         desc->bd_cbid.cbid_fn  = server_bulk_callback;
145         desc->bd_cbid.cbid_arg = desc;
146
147         /* NB we don't assign rq_bulk here; server-side requests are
148          * re-used, and the handler frees the bulk desc explicitly. */
149
150         return desc;
151 }
152
153 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
154                            cfs_page_t *page, int pageoffset, int len)
155 {
156         LASSERT(desc->bd_iov_count < desc->bd_max_iov);
157         LASSERT(page != NULL);
158         LASSERT(pageoffset >= 0);
159         LASSERT(len > 0);
160         LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
161
162         desc->bd_nob += len;
163
164         cfs_page_pin(page);
165         ptlrpc_add_bulk_page(desc, page, pageoffset, len);
166 }
167
168 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
169 {
170         int i;
171         ENTRY;
172
173         LASSERT(desc != NULL);
174         LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
175         LASSERT(!desc->bd_network_rw);         /* network hands off or */
176         LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
177         if (desc->bd_export)
178                 class_export_put(desc->bd_export);
179         else
180                 class_import_put(desc->bd_import);
181
182         for (i = 0; i < desc->bd_iov_count ; i++)
183                 cfs_page_unpin(desc->bd_iov[i].kiov_page);
184
185         OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
186                                 bd_iov[desc->bd_max_iov]));
187         EXIT;
188 }
189
190 /* Set server timelimit for this req */
191 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
192 {
193         __u32 serv_est;
194         int idx;
195         struct imp_at *at;
196
197         LASSERT(req->rq_import);
198
199         if (AT_OFF) {
200                 /* non-AT settings */
201                 req->rq_timeout = req->rq_import->imp_server_timeout ?
202                         obd_timeout / 2 : obd_timeout;
203                 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
204                 return;
205         }
206
207         at = &req->rq_import->imp_at;
208         idx = import_at_get_index(req->rq_import,
209                                   req->rq_request_portal);
210         serv_est = at_get(&at->iat_service_estimate[idx]);
211         req->rq_timeout = at_est2timeout(serv_est);
212         /* We could get even fancier here, using history to predict increased
213            loading... */
214
215         /* Let the server know what this RPC timeout is by putting it in the
216            reqmsg*/
217         lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
218 }
219
220 /* Adjust max service estimate based on server value */
221 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
222                                   unsigned int serv_est)
223 {
224         int idx;
225         unsigned int oldse;
226         struct imp_at *at;
227
228         /* do estimate only if is not in recovery */
229         if ((req->rq_send_state != LUSTRE_IMP_FULL) &&
230              (req->rq_send_state != LUSTRE_IMP_CONNECTING))
231                 return;
232
233         LASSERT(req->rq_import);
234         at = &req->rq_import->imp_at;
235
236         idx = import_at_get_index(req->rq_import, req->rq_request_portal);
237         /* max service estimates are tracked on the server side,
238            so just keep minimal history here */
239         oldse = at_add(&at->iat_service_estimate[idx], serv_est);
240         if (oldse != 0)
241                 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
242                        "has changed from %d to %d\n",
243                        req->rq_import->imp_obd->obd_name,req->rq_request_portal,
244                        oldse, at_get(&at->iat_service_estimate[idx]));
245 }
246
247 /* Expected network latency per remote node (secs) */
248 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
249 {
250         return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
251 }
252
253 /* Adjust expected network latency */
254 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
255                                       unsigned int service_time)
256 {
257         unsigned int nl, oldnl;
258         struct imp_at *at;
259         time_t now = cfs_time_current_sec();
260
261         LASSERT(req->rq_import);
262         at = &req->rq_import->imp_at;
263
264         /* Network latency is total time less server processing time */
265         nl = max_t(int, now - req->rq_sent - service_time, 0) + 1/*st rounding*/;
266         if (service_time > now - req->rq_sent + 3 /* bz16408 */)
267                 CWARN("Reported service time %u > total measured time %ld\n",
268                       service_time, now - req->rq_sent);
269
270         oldnl = at_add(&at->iat_net_latency, nl);
271         if (oldnl != 0)
272                 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
273                        "has changed from %d to %d\n",
274                        req->rq_import->imp_obd->obd_name,
275                        obd_uuid2str(
276                                &req->rq_import->imp_connection->c_remote_uuid),
277                        oldnl, at_get(&at->iat_net_latency));
278 }
279
280 static int unpack_reply_common(struct ptlrpc_request *req)
281 {
282         int rc;
283
284         req->rq_rep_swab_mask = 0;
285         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
286         if (rc < 0) {
287                 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
288                 return(-EPROTO);
289         }
290
291         if (rc > 0)
292                 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
293
294         return rc;
295 }
296
297 static int unpack_reply(struct ptlrpc_request *req)
298 {
299         int rc;
300
301         rc = unpack_reply_common(req);
302         if (rc < 0)
303                 return rc;
304
305         rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
306         if (rc) {
307                 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
308                 return(-EPROTO);
309         }
310         return 0;
311 }
312
313 static inline void unpack_reply_free_msg(struct lustre_msg *msg, int len)
314 {
315         OBD_FREE(msg, len);
316 }
317
318 static int unpack_reply_copy_msg(struct ptlrpc_request *req,
319                                  struct lustre_msg **msg, int *len)
320 {
321         struct lustre_msg *msgcpy;
322         __u32 csum_calc, csum_get;
323         int lencpy, rc;
324         ENTRY;
325
326         LASSERT_SPIN_LOCKED(&req->rq_lock);
327         *msg = NULL;
328         *len = 0;
329
330         /* Swabbing required when rc == 1 */
331         rc = unpack_reply_common(req);
332         if (rc < 0)
333                 RETURN(rc);
334
335         lencpy = req->rq_replen;
336         spin_unlock(&req->rq_lock);
337
338         OBD_ALLOC(msgcpy, lencpy);
339         if (!msgcpy) {
340                 spin_lock(&req->rq_lock);
341                 RETURN(-ENOMEM);
342         }
343         spin_lock(&req->rq_lock);
344
345         /* Checksum must be calculated before being unswabbed.  If the magic
346          * in the copy is unswabbed discard like the checksum failure case */
347         memcpy(msgcpy, req->rq_repmsg, lencpy);
348         if (lustre_msg_need_swab(msgcpy)) {
349                 DEBUG_REQ(D_NET, req, "incorrect message magic: %08x\n",
350                           msgcpy->lm_magic);
351                 GOTO(err, rc = -EINVAL);
352         }
353
354         csum_calc = lustre_msg_calc_cksum(msgcpy);
355
356         /* Unpack the copy the original rq_repmsg is untouched */
357         rc = lustre_unpack_msg_ptlrpc_body(msgcpy, MSG_PTLRPC_BODY_OFF, rc);
358         if (rc) {
359                 DEBUG_REQ(D_ERROR, req, "unpack msg copy failed: %d", rc);
360                 GOTO(err, rc = -EPROTO);
361         }
362
363         /* For early replies the LND may update repmsg outside req->rq_lock
364          * resulting in a checksum failure which is non-harmful */
365         csum_get = lustre_msg_get_cksum(msgcpy);
366         if (csum_calc != csum_get) {
367                 DEBUG_REQ(D_NET, req, "checksum mismatch: %x != %x\n",
368                           csum_calc, csum_get);
369                 GOTO(err, rc = -EINVAL);
370         }
371
372         *msg = msgcpy;
373         *len = lencpy;
374         return 0;
375 err:
376         unpack_reply_free_msg(msgcpy, lencpy);
377         return rc;
378 }
379
380 /* Handle an early reply message.  To prevent a real reply from arriving
381  * and changing req->rq_repmsg this func is called under the rq_lock */
382 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req) {
383         struct lustre_msg *msg;
384         time_t olddl;
385         int len, rc;
386         ENTRY;
387
388         LASSERT_SPIN_LOCKED(&req->rq_lock);
389         req->rq_early = 0;
390
391         /* All early replys for this request use a single repbuf which can
392          * be updated outside the req->rq_lock.  To prevent racing we create
393          * a copy of the repmsg and verify its checksum before it is used. */
394         rc = unpack_reply_copy_msg(req, &msg, &len);
395         if (rc) {
396                 /* Let's just ignore it - same as if it never got here */
397                 CDEBUG(D_ADAPTTO, "Discarding racing early reply: %d\n", rc);
398                 RETURN(rc);
399         }
400
401         /* Expecting to increase the service time estimate here */
402         ptlrpc_at_adj_service(req, lustre_msg_get_timeout(msg));
403         ptlrpc_at_adj_net_latency(req, lustre_msg_get_service_time(msg));
404
405         /* Adjust the local timeout for this req */
406         ptlrpc_at_set_req_timeout(req);
407
408         olddl = req->rq_deadline;
409         /* Server assumes it now has rq_timeout from when it sent the
410            early reply, so client should give it at least that long. */
411         req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
412                     ptlrpc_at_get_net_latency(req);
413
414         DEBUG_REQ(D_ADAPTTO, req,
415                   "Early reply #%d, new deadline in %lds (%+lds)",
416                   req->rq_early_count, req->rq_deadline -
417                   cfs_time_current_sec(), req->rq_deadline - olddl);
418
419         unpack_reply_free_msg(msg, len);
420         RETURN(rc);
421 }
422
423 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
424 {
425         struct list_head *l, *tmp;
426         struct ptlrpc_request *req;
427
428         LASSERT(pool != NULL);
429
430         spin_lock(&pool->prp_lock);
431         list_for_each_safe(l, tmp, &pool->prp_req_list) {
432                 req = list_entry(l, struct ptlrpc_request, rq_list);
433                 list_del(&req->rq_list);
434                 LASSERT(req->rq_reqmsg);
435                 OBD_FREE(req->rq_reqmsg, pool->prp_rq_size);
436                 OBD_FREE(req, sizeof(*req));
437         }
438         spin_unlock(&pool->prp_lock);
439         OBD_FREE(pool, sizeof(*pool));
440 }
441
442 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
443 {
444         int i;
445         int size = 1;
446
447         while (size < pool->prp_rq_size)
448                 size <<= 1;
449
450         LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
451                  "Trying to change pool size with nonempty pool "
452                  "from %d to %d bytes\n", pool->prp_rq_size, size);
453
454         spin_lock(&pool->prp_lock);
455         pool->prp_rq_size = size;
456         for (i = 0; i < num_rq; i++) {
457                 struct ptlrpc_request *req;
458                 struct lustre_msg *msg;
459
460                 spin_unlock(&pool->prp_lock);
461                 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
462                 if (!req)
463                         return;
464                 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
465                 if (!msg) {
466                         OBD_FREE(req, sizeof(struct ptlrpc_request));
467                         return;
468                 }
469                 req->rq_reqmsg = msg;
470                 req->rq_pool = pool;
471                 spin_lock(&pool->prp_lock);
472                 list_add_tail(&req->rq_list, &pool->prp_req_list);
473         }
474         spin_unlock(&pool->prp_lock);
475         return;
476 }
477
478 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
479                                                 void (*populate_pool)(struct ptlrpc_request_pool *, int))
480 {
481         struct ptlrpc_request_pool *pool;
482
483         OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
484         if (!pool)
485                 return NULL;
486
487         /* Request next power of two for the allocation, because internally
488            kernel would do exactly this */
489
490         spin_lock_init(&pool->prp_lock);
491         CFS_INIT_LIST_HEAD(&pool->prp_req_list);
492         pool->prp_rq_size = msgsize;
493         pool->prp_populate = populate_pool;
494
495         populate_pool(pool, num_rq);
496
497         if (list_empty(&pool->prp_req_list)) {
498                 /* have not allocated a single request for the pool */
499                 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
500                 pool = NULL;
501         }
502         return pool;
503 }
504
505 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
506 {
507         struct ptlrpc_request *request;
508         struct lustre_msg *reqmsg;
509
510         if (!pool)
511                 return NULL;
512
513         spin_lock(&pool->prp_lock);
514
515         /* See if we have anything in a pool, and bail out if nothing,
516          * in writeout path, where this matters, this is safe to do, because
517          * nothing is lost in this case, and when some in-flight requests
518          * complete, this code will be called again. */
519         if (unlikely(list_empty(&pool->prp_req_list))) {
520                 spin_unlock(&pool->prp_lock);
521                 return NULL;
522         }
523
524         request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
525                              rq_list);
526         list_del_init(&request->rq_list);
527         spin_unlock(&pool->prp_lock);
528
529         LASSERT(request->rq_reqmsg);
530         LASSERT(request->rq_pool);
531
532         reqmsg = request->rq_reqmsg;
533         memset(request, 0, sizeof(*request));
534         request->rq_reqmsg = reqmsg;
535         request->rq_pool = pool;
536         request->rq_reqlen = pool->prp_rq_size;
537         return request;
538 }
539
540 struct ptlrpc_request *
541 ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
542                      int count, __u32 *lengths, char **bufs,
543                      struct ptlrpc_request_pool *pool)
544 {
545         struct ptlrpc_request *request = NULL;
546         int rc;
547         ENTRY;
548
549         /* The obd disconnected */
550         if (imp == NULL)
551                 return NULL;
552
553         LASSERT(imp != LP_POISON);
554         LASSERT((unsigned long)imp->imp_client > 0x1000);
555         LASSERT(imp->imp_client != LP_POISON);
556
557         if (pool)
558                 request = ptlrpc_prep_req_from_pool(pool);
559
560         if (!request)
561                 OBD_ALLOC(request, sizeof(*request));
562
563         if (!request) {
564                 CERROR("request allocation out of memory\n");
565                 RETURN(NULL);
566         }
567
568         rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
569                                  bufs);
570         if (rc) {
571                 LASSERT(!request->rq_pool);
572                 OBD_FREE(request, sizeof(*request));
573                 RETURN(NULL);
574         }
575
576         lustre_msg_add_version(request->rq_reqmsg, version);
577         request->rq_send_state = LUSTRE_IMP_FULL;
578         request->rq_type = PTL_RPC_MSG_REQUEST;
579         request->rq_import = class_import_get(imp);
580         request->rq_export = NULL;
581
582         request->rq_req_cbid.cbid_fn  = request_out_callback;
583         request->rq_req_cbid.cbid_arg = request;
584
585         request->rq_reply_cbid.cbid_fn  = reply_in_callback;
586         request->rq_reply_cbid.cbid_arg = request;
587
588         request->rq_reply_deadline = 0;
589         request->rq_phase = RQ_PHASE_NEW;
590         request->rq_next_phase = RQ_PHASE_UNDEFINED;
591
592         request->rq_request_portal = imp->imp_client->cli_request_portal;
593         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
594
595         ptlrpc_at_set_req_timeout(request);
596
597         spin_lock_init(&request->rq_lock);
598         CFS_INIT_LIST_HEAD(&request->rq_list);
599         CFS_INIT_LIST_HEAD(&request->rq_replay_list);
600         CFS_INIT_LIST_HEAD(&request->rq_set_chain);
601         CFS_INIT_LIST_HEAD(&request->rq_history_list);
602         CFS_INIT_LIST_HEAD(&request->rq_exp_list);
603         cfs_waitq_init(&request->rq_reply_waitq);
604         request->rq_xid = ptlrpc_next_xid();
605         atomic_set(&request->rq_refcount, 1);
606
607         lustre_msg_set_opc(request->rq_reqmsg, opcode);
608
609         RETURN(request);
610 }
611
612 struct ptlrpc_request *
613 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
614                 __u32 *lengths, char **bufs)
615 {
616         return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
617                                     NULL);
618 }
619
620 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
621                                            unsigned int timeout,
622                                            int (*interpreter)(struct ptlrpc_request *,
623                                                               void *, int))
624 {
625         struct ptlrpc_request *request = NULL;
626         ENTRY;
627
628         OBD_ALLOC(request, sizeof(*request));
629         if (!request) {
630                 CERROR("request allocation out of memory\n");
631                 RETURN(NULL);
632         }
633
634         request->rq_send_state = LUSTRE_IMP_FULL;
635         request->rq_type = PTL_RPC_MSG_REQUEST;
636         request->rq_import = class_import_get(imp);
637         request->rq_export = NULL;
638
639         request->rq_sent = cfs_time_current_sec();
640         request->rq_reply_deadline = request->rq_sent + timeout;
641         request->rq_interpret_reply = interpreter;
642         request->rq_phase = RQ_PHASE_RPC;
643         request->rq_next_phase = RQ_PHASE_INTERPRET;
644         /* don't want reply */
645         request->rq_receiving_reply = 0;
646         request->rq_must_unlink = 0;
647         request->rq_no_delay = request->rq_no_resend = 1;
648         request->rq_fake = 1;
649
650         spin_lock_init(&request->rq_lock);
651         CFS_INIT_LIST_HEAD(&request->rq_list);
652         CFS_INIT_LIST_HEAD(&request->rq_replay_list);
653         CFS_INIT_LIST_HEAD(&request->rq_set_chain);
654         CFS_INIT_LIST_HEAD(&request->rq_history_list);
655         CFS_INIT_LIST_HEAD(&request->rq_exp_list);
656         cfs_waitq_init(&request->rq_reply_waitq);
657
658         request->rq_xid = ptlrpc_next_xid();
659         atomic_set(&request->rq_refcount, 1);
660
661         RETURN(request);
662 }
663
664 void ptlrpc_fakereq_finished(struct ptlrpc_request *req)
665 {
666         /* if we kill request before timeout - need adjust counter */
667         if (req->rq_phase == RQ_PHASE_RPC) {
668                 struct ptlrpc_request_set *set = req->rq_set;
669
670                 if (set)
671                         set->set_remaining --;
672         }
673
674         ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
675         list_del_init(&req->rq_list);
676 }
677
678
679 struct ptlrpc_request_set *ptlrpc_prep_set(void)
680 {
681         struct ptlrpc_request_set *set;
682
683         ENTRY;
684         OBD_ALLOC(set, sizeof *set);
685         if (!set)
686                 RETURN(NULL);
687         CFS_INIT_LIST_HEAD(&set->set_requests);
688         cfs_waitq_init(&set->set_waitq);
689         set->set_remaining = 0;
690         spin_lock_init(&set->set_new_req_lock);
691         CFS_INIT_LIST_HEAD(&set->set_new_requests);
692         CFS_INIT_LIST_HEAD(&set->set_cblist);
693
694         RETURN(set);
695 }
696
697 /* Finish with this set; opposite of prep_set. */
698 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
699 {
700         struct list_head *tmp;
701         struct list_head *next;
702         int               expected_phase;
703         int               n = 0;
704         ENTRY;
705
706         /* Requests on the set should either all be completed, or all be new */
707         expected_phase = (set->set_remaining == 0) ?
708                          RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
709         list_for_each (tmp, &set->set_requests) {
710                 struct ptlrpc_request *req =
711                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
712
713                 LASSERT(req->rq_phase == expected_phase);
714                 n++;
715         }
716
717         LASSERTF(set->set_remaining == 0 || set->set_remaining == n, "%d / %d\n",
718                  set->set_remaining, n);
719
720         list_for_each_safe(tmp, next, &set->set_requests) {
721                 struct ptlrpc_request *req =
722                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
723                 list_del_init(&req->rq_set_chain);
724
725                 LASSERT(req->rq_phase == expected_phase);
726
727                 if (req->rq_phase == RQ_PHASE_NEW) {
728
729                         if (req->rq_interpret_reply != NULL) {
730                                 int (*interpreter)(struct ptlrpc_request *,
731                                                    void *, int) =
732                                         req->rq_interpret_reply;
733
734                                 /* higher level (i.e. LOV) failed;
735                                  * let the sub reqs clean up */
736                                 req->rq_status = -EBADR;
737                                 interpreter(req, &req->rq_async_args,
738                                             req->rq_status);
739                         }
740                         set->set_remaining--;
741                 }
742
743                 req->rq_set = NULL;
744                 ptlrpc_req_finished (req);
745         }
746
747         LASSERT(set->set_remaining == 0);
748
749         OBD_FREE(set, sizeof(*set));
750         EXIT;
751 }
752
753 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
754                       set_interpreter_func fn, void *data)
755 {
756         struct ptlrpc_set_cbdata *cbdata;
757
758         OBD_SLAB_ALLOC(cbdata, ptlrpc_cbdata_slab,
759                         CFS_ALLOC_STD, sizeof(*cbdata));
760         if (cbdata == NULL)
761                 RETURN(-ENOMEM);
762
763         cbdata->psc_interpret = fn;
764         cbdata->psc_data = data;
765         list_add_tail(&cbdata->psc_item, &set->set_cblist);
766
767         RETURN(0);
768 }
769
770 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
771                         struct ptlrpc_request *req)
772 {
773         /* The set takes over the caller's request reference */
774         list_add_tail(&req->rq_set_chain, &set->set_requests);
775         req->rq_set = set;
776         set->set_remaining++;
777 }
778
779 /** 
780  * Lock so many callers can add things, the context that owns the set
781  * is supposed to notice these and move them into the set proper. 
782  */
783 int ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
784                            struct ptlrpc_request *req)
785 {
786         struct ptlrpc_request_set *set = pc->pc_set;
787
788         /* 
789          * Let caller know that we stopped and will not handle this request.
790          * It needs to take care itself of request.
791          */
792         if (test_bit(LIOD_STOP, &pc->pc_flags))
793                 return -EALREADY;
794
795         spin_lock(&set->set_new_req_lock);
796         /* 
797          * The set takes over the caller's request reference. 
798          */
799         list_add_tail(&req->rq_set_chain, &set->set_new_requests);
800         req->rq_set = set;
801         spin_unlock(&set->set_new_req_lock);
802
803         /*
804          * Let thead know that we added something and better it to wake up 
805          * and process.
806          */
807         cfs_waitq_signal(&set->set_waitq);
808         return 0;
809 }
810
811 /*
812  * Based on the current state of the import, determine if the request
813  * can be sent, is an error, or should be delayed.
814  *
815  * Returns true if this request should be delayed. If false, and
816  * *status is set, then the request can not be sent and *status is the
817  * error code.  If false and status is 0, then request can be sent.
818  *
819  * The imp->imp_lock must be held.
820  */
821 static int ptlrpc_import_delay_req(struct obd_import *imp,
822                                    struct ptlrpc_request *req, int *status)
823 {
824         int delay = 0;
825         ENTRY;
826
827         LASSERT (status != NULL);
828         *status = 0;
829
830         if (imp->imp_state == LUSTRE_IMP_NEW) {
831                 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
832                 *status = -EIO;
833                 LBUG();
834         } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
835                 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
836                 *status = -EIO;
837         } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
838                  imp->imp_state == LUSTRE_IMP_CONNECTING) {
839                 /* allow CONNECT even if import is invalid */ ;
840                 if (atomic_read(&imp->imp_inval_count) != 0) {
841                         DEBUG_REQ(D_ERROR, req, "invalidate in flight");
842                         *status = -EIO;
843                 }
844         } else if ((imp->imp_invalid && (!imp->imp_recon_bk)) ||
845                                          imp->imp_obd->obd_no_recov) {
846                 /* If the import has been invalidated (such as by an OST
847                  * failure), and if the import(MGC) tried all of its connection
848                  * list (Bug 13464), the request must fail with -ESHUTDOWN.
849                  * This indicates the requests should be discarded; an -EIO
850                  * may result in a resend of the request. */
851                 if (!imp->imp_deactive)
852                         DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
853                 *status = -ESHUTDOWN; /* bz 12940 */
854         } else if (req->rq_import_generation != imp->imp_generation) {
855                 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
856                 *status = -EIO;
857         } else if (req->rq_send_state != imp->imp_state) {
858                 /* invalidate in progress - any requests should be drop */
859                 if (atomic_read(&imp->imp_inval_count) != 0) {
860                         DEBUG_REQ(D_ERROR, req, "invalidate in flight");
861                         *status = -EIO;
862                 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
863                         *status = -EWOULDBLOCK;
864                 } else {
865                         delay = 1;
866                 }
867         }
868
869         RETURN(delay);
870 }
871
872 static int ptlrpc_check_reply(struct ptlrpc_request *req)
873 {
874         int rc = 0;
875         const char *what = "";
876         ENTRY;
877
878         /* serialise with network callback */
879         spin_lock(&req->rq_lock);
880
881         if (ptlrpc_client_replied(req)) {
882                 what = "REPLIED: ";
883                 GOTO(out, rc = 1);
884         }
885
886         if (req->rq_net_err && !req->rq_timedout) {
887                 what = "NETERR: ";
888                 spin_unlock(&req->rq_lock);
889                 rc = ptlrpc_expire_one_request(req, 0);
890                 spin_lock(&req->rq_lock);
891                 GOTO(out, rc);
892         }
893
894         if (req->rq_err) {
895                 what = "ABORTED: ";
896                 GOTO(out, rc = 1);
897         }
898
899         if (req->rq_resend) {
900                 what = "RESEND: ";
901                 GOTO(out, rc = 1);
902         }
903
904         if (req->rq_restart) {
905                 what = "RESTART: ";
906                 GOTO(out, rc = 1);
907         }
908
909         if (ptlrpc_client_early(req)) {
910                 what = "EARLYREP: ";
911                 ptlrpc_at_recv_early_reply(req);
912                 GOTO(out, rc = 0); /* keep waiting */
913         }
914
915         EXIT;
916  out:
917         spin_unlock(&req->rq_lock);
918         DEBUG_REQ(D_NET, req, "%src = %d for", what, rc);
919         return rc;
920 }
921
922 /* Conditionally suppress specific console messages */
923 static int ptlrpc_console_allow(struct ptlrpc_request *req)
924 {
925         __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
926         int err;
927
928         /* Suppress particular reconnect errors which are to be expected.  No
929          * errors are suppressed for the initial connection on an import */
930         if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
931             (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
932
933                 /* Suppress timed out reconnect requests */
934                 if (req->rq_timedout)
935                         return 0;
936
937                 /* Suppress unavailable/again reconnect requests */
938                 err = lustre_msg_get_status(req->rq_repmsg);
939                 if (err == -ENODEV || err == -EAGAIN)
940                         return 0;
941         }
942
943         return 1;
944 }
945
946 static int ptlrpc_check_status(struct ptlrpc_request *req)
947 {
948         int err;
949         ENTRY;
950
951         err = lustre_msg_get_status(req->rq_repmsg);
952         if (err < 0) {
953                 DEBUG_REQ(D_INFO, req, "status is %d", err);
954         } else if (err > 0) {
955                 /* XXX: translate this error from net to host */
956                 DEBUG_REQ(D_INFO, req, "status is %d", err);
957         }
958
959         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
960                 struct obd_import *imp = req->rq_import;
961                 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
962
963                 if (ptlrpc_console_allow(req))
964                         LCONSOLE_ERROR_MSG(0x011,"an error occurred while "
965                                            "communicating with %s. The %s "
966                                            "operation failed with %d\n",
967                                            libcfs_nid2str(
968                                            imp->imp_connection->c_peer.nid),
969                                            ll_opcode2str(opc), err);
970
971                 RETURN(err < 0 ? err : -EINVAL);
972         }
973
974         RETURN(err);
975 }
976
977 /* VBR: we should save pre-versions for replay*/
978 static void ptlrpc_save_versions(struct ptlrpc_request *req)
979 {
980         struct lustre_msg *repmsg = req->rq_repmsg;
981         struct lustre_msg *reqmsg = req->rq_reqmsg;
982         __u64 *versions = lustre_msg_get_versions(repmsg);
983         ENTRY;
984         /* Interoperability with 1.6. This should be changed to LASSERT in HEAD */
985         if (versions == NULL)
986                 return;
987
988         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
989                 return;
990
991         lustre_msg_set_versions(reqmsg, versions);
992         CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
993                versions[0], versions[1]);
994
995         EXIT;
996 }
997
998 static int after_reply(struct ptlrpc_request *req)
999 {
1000         struct obd_import *imp = req->rq_import;
1001         struct obd_device *obd = req->rq_import->imp_obd;
1002         int rc;
1003         struct timeval work_start;
1004         long timediff;
1005         ENTRY;
1006
1007         LASSERT(!req->rq_receiving_reply);
1008         LASSERT(obd);
1009
1010         /* NB Until this point, the whole of the incoming message,
1011          * including buflens, status etc is in the sender's byte order. */
1012
1013         if (req->rq_reply_truncate && !req->rq_no_resend) {
1014                 req->rq_resend = 1;
1015                 OBD_FREE(req->rq_repbuf, req->rq_replen);
1016                 req->rq_repbuf = NULL;
1017                 req->rq_replen = req->rq_nob_received;
1018                 RETURN(0);
1019         }
1020
1021         LASSERT (req->rq_nob_received <= req->rq_replen);
1022         rc = unpack_reply(req);
1023         if (rc)
1024                 RETURN(rc);
1025
1026         do_gettimeofday(&work_start);
1027         timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1028         if (obd->obd_svc_stats != NULL) {
1029                 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1030                                     timediff);
1031                 ptlrpc_lprocfs_rpc_sent(req, timediff);
1032         }
1033
1034         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, obd_fail_val);
1035         ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1036         ptlrpc_at_adj_net_latency(req, lustre_msg_get_service_time(req->rq_repmsg));
1037
1038         if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1039             lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1040                 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1041                           lustre_msg_get_type(req->rq_repmsg));
1042                 RETURN(-EPROTO);
1043         }
1044
1045         rc = ptlrpc_check_status(req);
1046         if (rc) {
1047                 /* Either we've been evicted, or the server has failed for
1048                  * some reason. Try to reconnect, and if that fails, punt to
1049                  * the upcall. */
1050                 if (ll_rpc_recoverable_error(rc)) {
1051                         if (req->rq_send_state != LUSTRE_IMP_FULL ||
1052                             imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1053                                 RETURN(rc);
1054                         }
1055                         ptlrpc_request_handle_notconn(req);
1056                         RETURN(rc);
1057                 }
1058         } else {
1059                 /* Let's look if server sent slv. Do it only for RPC with
1060                  * rc == 0. */
1061                 ldlm_cli_update_pool(req);
1062         }
1063
1064         /* Store transno in reqmsg for replay. */
1065         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1066                 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1067                 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1068         }
1069
1070         if (imp->imp_replayable) {
1071                 spin_lock(&imp->imp_lock);
1072                 /* no point in adding already-committed requests to the replay
1073                  * list, we will just remove them immediately. b=9829 */
1074                 if (req->rq_transno != 0 &&
1075                     (req->rq_transno >
1076                      lustre_msg_get_last_committed(req->rq_repmsg) ||
1077                      req->rq_replay)) {
1078                         /* version recovery */
1079                         ptlrpc_save_versions(req);
1080                         ptlrpc_retain_replayable_request(req, imp);
1081                 } else if (req->rq_commit_cb != NULL) {
1082                         spin_unlock(&imp->imp_lock);
1083                         req->rq_commit_cb(req);
1084                         spin_lock(&imp->imp_lock);
1085                 }
1086
1087                 /* Replay-enabled imports return commit-status information. */
1088                 if (lustre_msg_get_last_committed(req->rq_repmsg))
1089                         imp->imp_peer_committed_transno =
1090                                 lustre_msg_get_last_committed(req->rq_repmsg);
1091                 ptlrpc_free_committed(imp);
1092
1093                 if (req->rq_transno > imp->imp_peer_committed_transno)
1094                         ptlrpc_pinger_sending_on_import(imp);
1095                 spin_unlock(&imp->imp_lock);
1096         }
1097
1098         RETURN(rc);
1099 }
1100
1101 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1102 {
1103         struct obd_import     *imp;
1104         int rc;
1105         ENTRY;
1106
1107         LASSERT(req->rq_phase == RQ_PHASE_NEW);
1108         if (req->rq_sent && (req->rq_sent > CURRENT_SECONDS))
1109                 RETURN (0);
1110
1111         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1112
1113         imp = req->rq_import;
1114         spin_lock(&imp->imp_lock);
1115
1116         req->rq_import_generation = imp->imp_generation;
1117
1118         if (ptlrpc_import_delay_req(imp, req, &rc)) {
1119                 spin_lock(&req->rq_lock);
1120                 req->rq_waiting = 1;
1121                 spin_unlock(&req->rq_lock);
1122
1123                 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1124                           "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1125                           ptlrpc_import_state_name(req->rq_send_state),
1126                           ptlrpc_import_state_name(imp->imp_state));
1127                 LASSERT(list_empty(&req->rq_list));
1128                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1129                 atomic_inc(&req->rq_import->imp_inflight);
1130                 spin_unlock(&imp->imp_lock);
1131                 RETURN(0);
1132         }
1133
1134         if (rc != 0) {
1135                 spin_unlock(&imp->imp_lock);
1136                 req->rq_status = rc;
1137                 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1138                 RETURN(rc);
1139         }
1140
1141         LASSERT(list_empty(&req->rq_list));
1142         list_add_tail(&req->rq_list, &imp->imp_sending_list);
1143         atomic_inc(&req->rq_import->imp_inflight);
1144         spin_unlock(&imp->imp_lock);
1145
1146         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1147         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1148                " %s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1149                imp->imp_obd->obd_uuid.uuid,
1150                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1151                libcfs_nid2str(imp->imp_connection->c_peer.nid),
1152                lustre_msg_get_opc(req->rq_reqmsg));
1153
1154         rc = ptl_send_rpc(req, 0);
1155         if (rc) {
1156                 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1157                 req->rq_net_err = 1;
1158                 RETURN(rc);
1159         }
1160         RETURN(0);
1161 }
1162
1163 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
1164 int ptlrpc_check_set(struct ptlrpc_request_set *set)
1165 {
1166         struct list_head *tmp;
1167         int force_timer_recalc = 0;
1168         ENTRY;
1169
1170         if (set->set_remaining == 0)
1171                 RETURN(1);
1172
1173         list_for_each(tmp, &set->set_requests) {
1174                 struct ptlrpc_request *req =
1175                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1176                 struct obd_import *imp = req->rq_import;
1177                 int rc = 0;
1178
1179                 if (req->rq_phase == RQ_PHASE_NEW &&
1180                     ptlrpc_send_new_req(req)) {
1181                         force_timer_recalc = 1;
1182                 }
1183
1184                 /* delayed send - skip */
1185                 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1186                         continue;
1187
1188                 if (!(req->rq_phase == RQ_PHASE_RPC ||
1189                       req->rq_phase == RQ_PHASE_BULK ||
1190                       req->rq_phase == RQ_PHASE_INTERPRET ||
1191                       req->rq_phase == RQ_PHASE_UNREGISTERING ||
1192                       req->rq_phase == RQ_PHASE_COMPLETE)) {
1193                         DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1194                         LBUG();
1195                 }
1196
1197                 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1198                         LASSERT(req->rq_next_phase != req->rq_phase);
1199                         LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1200
1201                         /* Skip processing until reply is unlinked. We
1202                          * can't return to pool before that and we can't
1203                          * call interpret before that. We need to make
1204                          * sure that all rdma transfers finished and will
1205                          * not corrupt any data. */
1206                         if (ptlrpc_client_recv_or_unlink(req) ||
1207                             ptlrpc_client_bulk_active(req))
1208                                 continue;
1209
1210                         /* Turn repl fail_loc off to prevent it from looping
1211                          * forever. */
1212                         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1213                                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK |
1214                                                OBD_FAIL_ONCE);
1215                         }
1216
1217                         /* Turn off bulk fail_loc. */
1218                         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1219                                 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK |
1220                                                OBD_FAIL_ONCE);
1221                         }
1222
1223                         /* Move to next phase if reply was successfully 
1224                          * unlinked. */
1225                         ptlrpc_rqphase_move(req, req->rq_next_phase);
1226                 }
1227
1228                 if (req->rq_phase == RQ_PHASE_COMPLETE)
1229                         continue;
1230
1231                 if (req->rq_phase == RQ_PHASE_INTERPRET)
1232                         GOTO(interpret, req->rq_status);
1233
1234                 /* Note that this also will start async reply unlink */
1235                 if (req->rq_net_err && !req->rq_timedout) {
1236                         ptlrpc_expire_one_request(req, 1);
1237
1238                         /* Check if we still need to wait for unlink. */
1239                         if (ptlrpc_client_recv_or_unlink(req) ||
1240                             ptlrpc_client_bulk_active(req))
1241                                 continue;
1242                 }
1243
1244                 if (req->rq_err) {
1245                         if (req->rq_status == 0)
1246                                 req->rq_status = -EIO;
1247                         GOTO(interpret, req->rq_status);
1248                 }
1249
1250                 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
1251                  * will only be set after rq_timedout, but the oig waiting
1252                  * path sets rq_intr irrespective of whether ptlrpcd has
1253                  * seen a timeout.  our policy is to only interpret
1254                  * interrupted rpcs after they have timed out */
1255                 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
1256                         req->rq_status = -EINTR;
1257                         GOTO(interpret, req->rq_status);
1258                 }
1259
1260                 if (req->rq_phase == RQ_PHASE_RPC) {
1261                         if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
1262                                 int status;
1263
1264                                 if (!ptlrpc_unregister_reply(req, 1))
1265                                         continue;
1266
1267                                 spin_lock(&imp->imp_lock);
1268                                 if (ptlrpc_import_delay_req(imp, req, &status)){
1269                                         /* put on delay list - only if we wait
1270                                          * recovery finished - before send */
1271                                         list_del_init(&req->rq_list);
1272                                         list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1273                                         spin_unlock(&imp->imp_lock);
1274                                         continue;
1275                                 }
1276
1277                                 if (status != 0)  {
1278                                         req->rq_status = status;
1279                                         spin_unlock(&imp->imp_lock);
1280                                         GOTO(interpret, req->rq_status);
1281                                 }
1282                                 if (req->rq_no_resend) {
1283                                         req->rq_status = -ENOTCONN;
1284                                         spin_unlock(&imp->imp_lock);
1285                                         GOTO(interpret, req->rq_status);
1286                                 }
1287
1288                                 list_del_init(&req->rq_list);
1289                                 list_add_tail(&req->rq_list,
1290                                               &imp->imp_sending_list);
1291
1292                                 spin_unlock(&imp->imp_lock);
1293
1294                                 req->rq_waiting = 0;
1295
1296                                 if (req->rq_timedout||req->rq_resend) {
1297                                         /* This is re-sending anyways, 
1298                                          * let's mark req as resend. */
1299                                         req->rq_resend = 1;
1300                                         if (req->rq_bulk) {
1301                                                 __u64 old_xid;
1302
1303                                                 if (!ptlrpc_unregister_bulk(req, 1))
1304                                                         continue;
1305
1306                                                 /* ensure previous bulk fails */
1307                                                 old_xid = req->rq_xid;
1308                                                 req->rq_xid = ptlrpc_next_xid();
1309                                                 CDEBUG(D_HA, "resend bulk "
1310                                                        "old x"LPU64
1311                                                        " new x"LPU64"\n",
1312                                                        old_xid, req->rq_xid);
1313                                         }
1314                                 }
1315
1316                                 rc = ptl_send_rpc(req, 0);
1317                                 if (rc) {
1318                                         DEBUG_REQ(D_HA, req, "send failed (%d)",
1319                                                   rc);
1320                                         force_timer_recalc = 1;
1321                                         req->rq_net_err = 1;
1322                                 }
1323                                 /* need to reset the timeout */
1324                                 force_timer_recalc = 1;
1325                         }
1326
1327                         spin_lock(&req->rq_lock);
1328
1329                         if (ptlrpc_client_early(req)) {
1330                                 ptlrpc_at_recv_early_reply(req);
1331                                 spin_unlock(&req->rq_lock);
1332                                 continue;
1333                         }
1334
1335                         /* Still waiting for a reply? */
1336                         if (ptlrpc_client_recv(req)) {
1337                                 spin_unlock(&req->rq_lock);
1338                                 continue;
1339                         }
1340
1341                         /* Did we actually receive a reply? */
1342                         if (!ptlrpc_client_replied(req)) {
1343                                 spin_unlock(&req->rq_lock);
1344                                 continue;
1345                         }
1346
1347                         spin_unlock(&req->rq_lock);
1348
1349                         req->rq_status = after_reply(req);
1350                         if (req->rq_resend)
1351                                 continue;
1352
1353                         /* If there is no bulk associated with this request,
1354                          * then we're done and should let the interpreter
1355                          * process the reply. Similarly if the RPC returned
1356                          * an error, and therefore the bulk will never arrive.
1357                          */
1358                         if (req->rq_bulk == NULL || req->rq_status != 0)
1359                                 GOTO(interpret, req->rq_status);
1360
1361                         ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1362                 }
1363
1364                 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1365                 if (ptlrpc_client_bulk_active(req))
1366                         continue;
1367
1368                 if (!req->rq_bulk->bd_success) {
1369                         /* The RPC reply arrived OK, but the bulk screwed
1370                          * up!  Dead wierd since the server told us the RPC
1371                          * was good after getting the REPLY for her GET or
1372                          * the ACK for her PUT. */
1373                         DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1374                         req->rq_status = -EIO;
1375                         GOTO(interpret, req->rq_status);
1376                 }
1377       interpret:
1378                 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1379
1380                 /* This moves to "unregistering" phase we need to wait for
1381                  * reply unlink. */
1382                 if (!ptlrpc_unregister_reply(req, 1))
1383                         continue;
1384
1385                 if (!ptlrpc_unregister_bulk(req, 1))
1386                         continue;
1387
1388                 /* When calling interpret receiving already should be
1389                  * finished. */
1390                 LASSERT(!req->rq_receiving_reply);
1391
1392                 ptlrpc_req_interpret(req, req->rq_status);
1393
1394                 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1395
1396                 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1397                        "opc %s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1398                        imp->imp_obd->obd_uuid.uuid,
1399                        req->rq_reqmsg ? lustre_msg_get_status(req->rq_reqmsg):-1,
1400                        req->rq_xid,
1401                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1402                        req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1);
1403
1404                 spin_lock(&imp->imp_lock);
1405                 /* Request already may be not on sending or delaying list. This
1406                  * may happen in the case of marking it errorneous for the case
1407                  * ptlrpc_import_delay_req(req, status) find it impossible to 
1408                  * allow sending this rpc and returns *status != 0. */
1409                 if (!list_empty(&req->rq_list)) {
1410                         list_del_init(&req->rq_list);
1411                         atomic_dec(&imp->imp_inflight);
1412                 }
1413                 spin_unlock(&imp->imp_lock);
1414
1415                 set->set_remaining--;
1416                 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1417         }
1418
1419         /* If we hit an error, we want to recover promptly. */
1420         RETURN(set->set_remaining == 0 || force_timer_recalc);
1421 }
1422
1423 /* Return 1 if we should give up, else 0 */
1424 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1425 {
1426         struct obd_import *imp = req->rq_import;
1427         int rc = 0;
1428         ENTRY;
1429
1430         DEBUG_REQ(D_WARNING, req, "Request x"LPU64" sent from %s to NID %s"
1431                   " %lus ago has %s (limit %lus).\n", req->rq_xid,
1432                   imp ? imp->imp_obd->obd_name : "<?>",
1433                   imp ? libcfs_nid2str(imp->imp_connection->c_peer.nid) : "<?>",
1434                   cfs_time_current_sec() - req->rq_sent,
1435                   req->rq_net_err ? "failed due to network error" : "timed out",
1436                   req->rq_deadline - req->rq_sent);
1437
1438         if (imp != NULL && obd_debug_peer_on_timeout)
1439                 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1440
1441         spin_lock(&req->rq_lock);
1442         req->rq_timedout = 1;
1443         spin_unlock(&req->rq_lock);
1444
1445         ptlrpc_unregister_reply(req, async_unlink);
1446         ptlrpc_unregister_bulk(req, async_unlink);
1447
1448         if (obd_dump_on_timeout)
1449                 libcfs_debug_dumplog();
1450
1451         if (imp == NULL) {
1452                 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1453                 RETURN(1);
1454         }
1455
1456         if (req->rq_fake)
1457                 RETURN(1);
1458
1459         atomic_inc(&imp->imp_timeouts);
1460
1461         /* The DLM server doesn't want recovery run on its imports. */
1462         if (imp->imp_dlm_fake)
1463                 RETURN(1);
1464
1465         /* If this request is for recovery or other primordial tasks,
1466          * then error it out here. */
1467         if (req->rq_send_state != LUSTRE_IMP_FULL ||
1468             imp->imp_obd->obd_no_recov) {
1469                 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1470                           ptlrpc_import_state_name(req->rq_send_state),
1471                           ptlrpc_import_state_name(imp->imp_state));
1472                 spin_lock(&req->rq_lock);
1473                 req->rq_status = -ETIMEDOUT;
1474                 req->rq_err = 1;
1475                 spin_unlock(&req->rq_lock);
1476                 RETURN(1);
1477         }
1478
1479         /* if a request can't be resent we can't wait for an answer after
1480            the timeout */
1481         if (req->rq_no_resend) {
1482                 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1483                 rc = 1;
1484         }
1485
1486         ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1487
1488         RETURN(rc);
1489 }
1490
1491 int ptlrpc_expired_set(void *data)
1492 {
1493         struct ptlrpc_request_set *set = data;
1494         struct list_head          *tmp;
1495         time_t                     now = cfs_time_current_sec();
1496         ENTRY;
1497
1498         LASSERT(set != NULL);
1499
1500         /* A timeout expired; see which reqs it applies to... */
1501         list_for_each(tmp, &set->set_requests) {
1502                 struct ptlrpc_request *req =
1503                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1504
1505                 /* Request in-flight? */
1506                 if (!((req->rq_phase == RQ_PHASE_RPC &&
1507                        !req->rq_waiting && !req->rq_resend) ||
1508                       (req->rq_phase == RQ_PHASE_BULK)))
1509                         continue;
1510
1511                 if (req->rq_timedout ||     /* already dealt with */
1512                     req->rq_deadline > now) /* not expired */
1513                         continue;
1514
1515                 /* Deal with this guy. Do it asynchronously to not block
1516                  * ptlrpcd thread. */
1517                 ptlrpc_expire_one_request(req, 1);
1518         }
1519
1520         /* When waiting for a whole set, we always to break out of the
1521          * sleep so we can recalculate the timeout, or enable interrupts
1522          * if everyone's timed out. */
1523         RETURN(1);
1524 }
1525
1526 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1527 {
1528         spin_lock(&req->rq_lock);
1529         req->rq_intr = 1;
1530         spin_unlock(&req->rq_lock);
1531 }
1532
1533 void ptlrpc_interrupted_set(void *data)
1534 {
1535         struct ptlrpc_request_set *set = data;
1536         struct list_head *tmp;
1537
1538         LASSERT(set != NULL);
1539         CERROR("INTERRUPTED SET %p\n", set);
1540
1541         list_for_each(tmp, &set->set_requests) {
1542                 struct ptlrpc_request *req =
1543                         list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1544
1545                 if (req->rq_phase != RQ_PHASE_RPC &&
1546                     req->rq_phase != RQ_PHASE_UNREGISTERING)
1547                         continue;
1548
1549                 ptlrpc_mark_interrupted(req);
1550         }
1551 }
1552
1553 /* get the smallest timeout in the set; this does NOT set a timeout. */
1554 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1555 {
1556         struct list_head      *tmp;
1557         time_t                 now = cfs_time_current_sec();
1558         int                    timeout = 0;
1559         struct ptlrpc_request *req;
1560         int                    deadline;
1561         ENTRY;
1562
1563         SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1564
1565         list_for_each(tmp, &set->set_requests) {
1566                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1567
1568                 /* request in-flight? */
1569                 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
1570                       (req->rq_phase == RQ_PHASE_BULK) ||
1571                       (req->rq_phase == RQ_PHASE_NEW)))
1572                         continue;
1573
1574                 /* Already timed out. */
1575                 if (req->rq_timedout)
1576                         continue;
1577
1578                 if (req->rq_phase == RQ_PHASE_NEW)
1579                         deadline = req->rq_sent;    /* delayed send */
1580                 else
1581                         deadline = req->rq_deadline;
1582
1583                 if (deadline <= now) {  /* actually expired already */
1584                         timeout = 1;    /* ASAP */
1585                         break;
1586                 }
1587                 if ((timeout == 0) || (timeout > (deadline - now)))
1588                         timeout = deadline - now;
1589         }
1590         RETURN(timeout);
1591 }
1592
1593 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1594 {
1595         struct list_head      *tmp;
1596         struct ptlrpc_request *req;
1597         struct l_wait_info     lwi;
1598         int                    rc, timeout;
1599         ENTRY;
1600
1601         if (list_empty(&set->set_requests))
1602                 RETURN(0);
1603
1604         list_for_each(tmp, &set->set_requests) {
1605                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1606                 if (req->rq_phase == RQ_PHASE_NEW)
1607                         (void)ptlrpc_send_new_req(req);
1608         }
1609
1610         do {
1611                 timeout = ptlrpc_set_next_timeout(set);
1612
1613                 /* wait until all complete, interrupted, or an in-flight
1614                  * req times out */
1615                 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1616                        set, timeout);
1617                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1618                                        ptlrpc_expired_set,
1619                                        ptlrpc_interrupted_set, set);
1620                 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1621
1622                 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1623
1624                 /* -EINTR => all requests have been flagged rq_intr so next
1625                  * check completes.
1626                  * -ETIMEOUTD => someone timed out.  When all reqs have
1627                  * timed out, signals are enabled allowing completion with
1628                  * EINTR.
1629                  * I don't really care if we go once more round the loop in
1630                  * the error cases -eeb. */
1631         } while (rc != 0 || set->set_remaining != 0);
1632
1633         LASSERT(set->set_remaining == 0);
1634
1635         rc = 0;
1636         list_for_each(tmp, &set->set_requests) {
1637                 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1638
1639                 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1640                 if (req->rq_status != 0)
1641                         rc = req->rq_status;
1642         }
1643
1644         if (set->set_interpret != NULL) {
1645                 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1646                         set->set_interpret;
1647                 rc = interpreter (set, set->set_arg, rc);
1648         } else {
1649                 struct ptlrpc_set_cbdata *cbdata, *n;
1650                 int err;
1651
1652                 list_for_each_entry_safe(cbdata, n,
1653                                          &set->set_cblist, psc_item) {
1654                         list_del_init(&cbdata->psc_item);
1655                         err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
1656                         if (err && !rc)
1657                                 rc = err;
1658                         OBD_SLAB_FREE(cbdata, ptlrpc_cbdata_slab,
1659                                         sizeof(*cbdata));
1660                 }
1661         }
1662
1663         RETURN(rc);
1664 }
1665
1666 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
1667 {
1668         struct ptlrpc_request_pool *pool = request->rq_pool;
1669
1670         spin_lock(&pool->prp_lock);
1671         LASSERT(list_empty(&request->rq_list));
1672         LASSERT(!request->rq_receiving_reply);
1673         list_add_tail(&request->rq_list, &pool->prp_req_list);
1674         spin_unlock(&pool->prp_lock);
1675 }
1676
1677 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1678 {
1679         ENTRY;
1680         if (request == NULL) {
1681                 EXIT;
1682                 return;
1683         }
1684
1685         LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1686         LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1687         LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1688         LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1689         LASSERTF(list_empty(&request->rq_exp_list), "req %p\n", request);
1690         LASSERTF(!request->rq_replay, "req %p\n", request);
1691
1692         /* We must take it off the imp_replay_list first.  Otherwise, we'll set
1693          * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1694         if (request->rq_import != NULL) {
1695                 if (!locked)
1696                         spin_lock(&request->rq_import->imp_lock);
1697                 list_del_init(&request->rq_replay_list);
1698                 if (!locked)
1699                         spin_unlock(&request->rq_import->imp_lock);
1700         }
1701         LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1702
1703         if (atomic_read(&request->rq_refcount) != 0) {
1704                 DEBUG_REQ(D_ERROR, request,
1705                           "freeing request with nonzero refcount");
1706                 LBUG();
1707         }
1708
1709         if (request->rq_repbuf != NULL) {
1710                 OBD_FREE(request->rq_repbuf, request->rq_replen);
1711                 request->rq_repbuf = NULL;
1712                 request->rq_repmsg = NULL;
1713         }
1714         if (request->rq_export != NULL) {
1715                 class_export_put(request->rq_export);
1716                 request->rq_export = NULL;
1717         }
1718         if (request->rq_import != NULL) {
1719                 class_import_put(request->rq_import);
1720                 request->rq_import = NULL;
1721         }
1722         if (request->rq_bulk != NULL)
1723                 ptlrpc_free_bulk(request->rq_bulk);
1724
1725         if (request->rq_pool) {
1726                 __ptlrpc_free_req_to_pool(request);
1727         } else {
1728                 if (request->rq_reqmsg != NULL) {
1729                         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
1730                         request->rq_reqmsg = NULL;
1731                 }
1732                 OBD_FREE(request, sizeof(*request));
1733         }
1734         EXIT;
1735 }
1736
1737 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1738 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1739 {
1740         LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1741         (void)__ptlrpc_req_finished(request, 1);
1742 }
1743
1744 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1745 {
1746         ENTRY;
1747         if (request == NULL)
1748                 RETURN(1);
1749
1750         if (request == LP_POISON ||
1751             request->rq_reqmsg == LP_POISON) {
1752                 CERROR("dereferencing freed request (bug 575)\n");
1753                 LBUG();
1754                 RETURN(1);
1755         }
1756
1757         DEBUG_REQ(D_INFO, request, "refcount now %u",
1758                   atomic_read(&request->rq_refcount) - 1);
1759
1760         if (atomic_dec_and_test(&request->rq_refcount)) {
1761                 __ptlrpc_free_req(request, locked);
1762                 RETURN(1);
1763         }
1764
1765         RETURN(0);
1766 }
1767
1768 void ptlrpc_req_finished(struct ptlrpc_request *request)
1769 {
1770         __ptlrpc_req_finished(request, 0);
1771 }
1772
1773 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1774 {
1775         return request->rq_xid;
1776 }
1777 EXPORT_SYMBOL(ptlrpc_req_xid);
1778
1779 /* Disengage the client's reply buffer from the network
1780  * NB does _NOT_ unregister any client-side bulk.
1781  * IDEMPOTENT, but _not_ safe against concurrent callers.
1782  * The request owner (i.e. the thread doing the I/O) must call...
1783  */
1784 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
1785 {
1786         int                rc;
1787         cfs_waitq_t       *wq;
1788         struct l_wait_info lwi;
1789         ENTRY;
1790
1791         /* Might sleep. */
1792         LASSERT(!in_interrupt());
1793
1794         /* Let's setup deadline for reply unlink. */
1795         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) && 
1796             async && request->rq_reply_deadline == 0)
1797                 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
1798
1799         /* Nothing left to do. */
1800         if (!ptlrpc_client_recv_or_unlink(request))
1801                 RETURN(1);
1802
1803         LNetMDUnlink(request->rq_reply_md_h);
1804
1805         /* Let's check it once again. */        
1806         if (!ptlrpc_client_recv_or_unlink(request))
1807                 RETURN(1);
1808
1809         /* Move to "Unregistering" phase as reply was not unlinked yet. */
1810         ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
1811
1812         /* Do not wait for unlink to finish. */
1813         if (async)
1814                 RETURN(0);
1815
1816         /* We have to l_wait_event() whatever the result, to give liblustre
1817          * a chance to run reply_in_callback(), and to make sure we've
1818          * unlinked before returning a req to the pool */
1819         if (request->rq_set != NULL)
1820                 wq = &request->rq_set->set_waitq;
1821         else
1822                 wq = &request->rq_reply_waitq;
1823
1824         for (;;) {
1825                 /* Network access will complete in finite time but the HUGE
1826                  * timeout lets us CWARN for visibility of sluggish NALs */
1827                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
1828                                            cfs_time_seconds(1), NULL, NULL);
1829                 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
1830                                   &lwi);
1831                 if (rc == 0) {
1832                         ptlrpc_rqphase_move(request, request->rq_next_phase);
1833                         RETURN(1);
1834                 }
1835
1836                 LASSERT(rc == -ETIMEDOUT);
1837                 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
1838                           "rvcng=%d unlnk=%d", request->rq_receiving_reply,
1839                           request->rq_must_unlink);
1840         }
1841         RETURN(0);
1842 }
1843
1844 /* caller must hold imp->imp_lock */
1845 void ptlrpc_free_committed(struct obd_import *imp)
1846 {
1847         struct list_head *tmp, *saved;
1848         struct ptlrpc_request *req;
1849         struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1850         ENTRY;
1851
1852         LASSERT(imp != NULL);
1853
1854         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1855
1856
1857         if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1858             imp->imp_generation == imp->imp_last_generation_checked) {
1859                 CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
1860                        imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1861                 EXIT;
1862                 return;
1863         }
1864
1865         CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
1866                imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1867                imp->imp_generation);
1868         imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1869         imp->imp_last_generation_checked = imp->imp_generation;
1870
1871         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1872                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1873
1874                 /* XXX ok to remove when 1357 resolved - rread 05/29/03  */
1875                 LASSERT(req != last_req);
1876                 last_req = req;
1877
1878                 if (req->rq_import_generation < imp->imp_generation) {
1879                         DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
1880                         GOTO(free_req, 0);
1881                 }
1882
1883                 if (req->rq_replay) {
1884                         DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
1885                         continue;
1886                 }
1887
1888                 /* not yet committed */
1889                 if (req->rq_transno > imp->imp_peer_committed_transno) {
1890                         DEBUG_REQ(D_RPCTRACE, req, "stopping search");
1891                         break;
1892                 }
1893
1894                 DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
1895                           imp->imp_peer_committed_transno);
1896 free_req:
1897                 spin_lock(&req->rq_lock);
1898                 req->rq_replay = 0;
1899                 spin_unlock(&req->rq_lock);
1900                 if (req->rq_commit_cb != NULL)
1901                         req->rq_commit_cb(req);
1902                 list_del_init(&req->rq_replay_list);
1903                 __ptlrpc_req_finished(req, 1);
1904         }
1905
1906         EXIT;
1907         return;
1908 }
1909
1910 void ptlrpc_cleanup_client(struct obd_import *imp)
1911 {
1912         ENTRY;
1913         EXIT;
1914         return;
1915 }
1916
1917 void ptlrpc_resend_req(struct ptlrpc_request *req)
1918 {
1919         DEBUG_REQ(D_HA, req, "going to resend");
1920         lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1921         req->rq_status = -EAGAIN;
1922
1923         spin_lock(&req->rq_lock);
1924         req->rq_resend = 1;
1925         req->rq_net_err = 0;
1926         req->rq_timedout = 0;
1927         if (req->rq_bulk) {
1928                 __u64 old_xid = req->rq_xid;
1929
1930                 /* ensure previous bulk fails */
1931                 req->rq_xid = ptlrpc_next_xid();
1932                 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1933                        old_xid, req->rq_xid);
1934         }
1935         ptlrpc_client_wake_req(req);
1936         spin_unlock(&req->rq_lock);
1937 }
1938
1939 /* XXX: this function and rq_status are currently unused */
1940 void ptlrpc_restart_req(struct ptlrpc_request *req)
1941 {
1942         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1943         req->rq_status = -ERESTARTSYS;
1944
1945         spin_lock(&req->rq_lock);
1946         req->rq_restart = 1;
1947         req->rq_timedout = 0;
1948         ptlrpc_client_wake_req(req);
1949         spin_unlock(&req->rq_lock);
1950 }
1951
1952 static void interrupted_request(void *data)
1953 {
1954         struct ptlrpc_request *req = data;
1955         DEBUG_REQ(D_HA, req, "request interrupted");
1956         spin_lock(&req->rq_lock);
1957         req->rq_intr = 1;
1958         spin_unlock(&req->rq_lock);
1959 }
1960
1961 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1962 {
1963         ENTRY;
1964         atomic_inc(&req->rq_refcount);
1965         RETURN(req);
1966 }
1967
1968 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1969                                       struct obd_import *imp)
1970 {
1971         struct list_head *tmp;
1972
1973         LASSERT_SPIN_LOCKED(&imp->imp_lock);
1974
1975         /* clear this for new requests that were resent as well
1976            as resent replayed requests. */
1977         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1978
1979         /* don't re-add requests that have been replayed */
1980         if (!list_empty(&req->rq_replay_list))
1981                 return;
1982
1983         lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1984
1985         LASSERT(imp->imp_replayable);
1986         /* Balanced in ptlrpc_free_committed, usually. */
1987         ptlrpc_request_addref(req);
1988         list_for_each_prev(tmp, &imp->imp_replay_list) {
1989                 struct ptlrpc_request *iter =
1990                         list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1991
1992                 /* We may have duplicate transnos if we create and then
1993                  * open a file, or for closes retained if to match creating
1994                  * opens, so use req->rq_xid as a secondary key.
1995                  * (See bugs 684, 685, and 428.)
1996                  * XXX no longer needed, but all opens need transnos!
1997                  */
1998                 if (iter->rq_transno > req->rq_transno)
1999                         continue;
2000
2001                 if (iter->rq_transno == req->rq_transno) {
2002                         LASSERT(iter->rq_xid != req->rq_xid);
2003                         if (iter->rq_xid > req->rq_xid)
2004                                 continue;
2005                 }
2006
2007                 list_add(&req->rq_replay_list, &iter->rq_replay_list);
2008                 return;
2009         }
2010
2011         list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
2012 }
2013
2014 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2015 {
2016         int rc = 0;
2017         int brc;
2018         struct l_wait_info lwi;
2019         struct obd_import *imp = req->rq_import;
2020         cfs_duration_t timeout = CFS_TICK;
2021         long timeoutl;
2022         ENTRY;
2023
2024         LASSERT(req->rq_set == NULL);
2025         LASSERT(!req->rq_receiving_reply);
2026
2027         /* for distributed debugging */
2028         lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
2029         LASSERT(imp->imp_obd != NULL);
2030         CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
2031                "%s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
2032                imp->imp_obd->obd_uuid.uuid,
2033                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
2034                libcfs_nid2str(imp->imp_connection->c_peer.nid),
2035                lustre_msg_get_opc(req->rq_reqmsg));
2036
2037         /* Mark phase here for a little debug help */
2038         ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
2039
2040         spin_lock(&imp->imp_lock);
2041         req->rq_import_generation = imp->imp_generation;
2042 restart:
2043         if (ptlrpc_import_delay_req(imp, req, &rc)) {
2044                 list_del_init(&req->rq_list);
2045                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
2046                 atomic_inc(&imp->imp_inflight);
2047                 spin_unlock(&imp->imp_lock);
2048
2049                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
2050                           cfs_curproc_comm(),
2051                           ptlrpc_import_state_name(req->rq_send_state),
2052                           ptlrpc_import_state_name(imp->imp_state));
2053                 lwi = LWI_INTR(interrupted_request, req);
2054                 rc = l_wait_event(req->rq_reply_waitq,
2055                                   (req->rq_send_state == imp->imp_state ||
2056                                    req->rq_err || req->rq_intr),
2057                                   &lwi);
2058                 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
2059                           cfs_curproc_comm(),
2060                           ptlrpc_import_state_name(imp->imp_state),
2061                           ptlrpc_import_state_name(req->rq_send_state),
2062                           req->rq_err, req->rq_intr);
2063
2064                 spin_lock(&imp->imp_lock);
2065                 list_del_init(&req->rq_list);
2066                 atomic_dec(&imp->imp_inflight);
2067
2068                 if (req->rq_err) {
2069                         /* rq_status was set locally */
2070                         rc = req->rq_status ? req->rq_status : -EIO;
2071                 }
2072                 else if (req->rq_intr) {
2073                         rc = -EINTR;
2074                 }
2075                 else if (req->rq_no_resend) {
2076                         rc = -ETIMEDOUT;
2077                 }
2078                 else {
2079                         GOTO(restart, rc);
2080                 }
2081         }
2082
2083         if (rc != 0) {
2084                 spin_unlock(&imp->imp_lock);
2085                 req->rq_status = rc; // XXX this ok?
2086                 GOTO(out, rc);
2087         }
2088
2089         if (req->rq_resend) {
2090                 if (req->rq_bulk != NULL) {
2091                         ptlrpc_unregister_bulk(req, 0);
2092
2093                         /* bulk requests are supposed to be
2094                          * idempotent, so we are free to bump the xid
2095                          * here, which we need to do before
2096                          * registering the bulk again (bug 6371).
2097                          * print the old xid first for sanity.
2098                          */
2099                         DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
2100                         req->rq_xid = ptlrpc_next_xid();
2101                 }
2102
2103                 DEBUG_REQ(D_HA, req, "resending: ");
2104         }
2105
2106         /* XXX this is the same as ptlrpc_set_wait */
2107         LASSERT(list_empty(&req->rq_list));
2108         list_add_tail(&req->rq_list, &imp->imp_sending_list);
2109         atomic_inc(&imp->imp_inflight);
2110         spin_unlock(&imp->imp_lock);
2111
2112         rc = ptl_send_rpc(req, 0);
2113         if (rc)
2114                 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
2115         do {
2116                 timeoutl = req->rq_deadline - cfs_time_current_sec();
2117                 timeout = (timeoutl <= 0 || rc) ? CFS_TICK :
2118                         cfs_time_seconds(timeoutl);
2119                 DEBUG_REQ(D_NET, req,
2120                           "-- sleeping for "CFS_DURATION_T" ticks", timeout);
2121                 lwi = LWI_TIMEOUT_INTR(timeout, NULL, interrupted_request, req);
2122                 brc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req),
2123                                   &lwi);
2124                 /* Wait again if we changed deadline */
2125         } while ((brc == -ETIMEDOUT) &&
2126                  (req->rq_deadline > cfs_time_current_sec()));
2127
2128         if ((brc == -ETIMEDOUT) && !ptlrpc_expire_one_request(req, 0)) {
2129                 /* Wait forever for reconnect / replay or failure */
2130                 lwi = LWI_INTR(interrupted_request, req);
2131                 brc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req),
2132                                    &lwi);
2133         }
2134
2135         CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
2136                "%s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
2137                imp->imp_obd->obd_uuid.uuid,
2138                lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
2139                libcfs_nid2str(imp->imp_connection->c_peer.nid),
2140                lustre_msg_get_opc(req->rq_reqmsg));
2141
2142         /* If the reply was received normally, this just grabs the spinlock
2143          * (ensuring the reply callback has returned), sees that
2144          * req->rq_receiving_reply is clear and returns. */
2145         ptlrpc_unregister_reply(req, 0);
2146
2147         spin_lock(&imp->imp_lock);
2148         list_del_init(&req->rq_list);
2149         atomic_dec(&imp->imp_inflight);
2150         spin_unlock(&imp->imp_lock);
2151
2152         if (req->rq_err) {
2153                 DEBUG_REQ(D_RPCTRACE, req, "err rc=%d status=%d",
2154                           rc, req->rq_status);
2155                 rc = rc ? rc : req->rq_status;
2156                 GOTO(out, rc = rc ? rc : -EIO);
2157         }
2158
2159         if (req->rq_intr) {
2160                 /* Should only be interrupted if we timed out. */
2161                 if (!req->rq_timedout)
2162                         DEBUG_REQ(D_ERROR, req,
2163                                   "rq_intr set but rq_timedout not");
2164                 GOTO(out, rc = -EINTR);
2165         }
2166
2167         /* Resend if we need to */
2168         if (req->rq_resend||req->rq_timedout) {
2169                 /* ...unless we were specifically told otherwise. */
2170                 if (req->rq_no_resend)
2171                         GOTO(out, rc = -ETIMEDOUT);
2172                 spin_lock(&imp->imp_lock);
2173                 /* we can have rq_timeout on dlm fake import which not support
2174                  * recovery - but me need resend request on this import instead
2175                  * of return error */
2176                 req->rq_resend = 1;
2177                 goto restart;
2178         }
2179
2180         if (!ptlrpc_client_replied(req)) {
2181                 /* How can this be? -eeb */
2182                 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
2183                 LBUG();
2184                 GOTO(out, rc = req->rq_status);
2185         }
2186
2187         rc = after_reply(req);
2188         /* NB may return +ve success rc */
2189         if (req->rq_resend) {
2190                 spin_lock(&imp->imp_lock);
2191                 goto restart;
2192         }
2193
2194  out:
2195         if (req->rq_bulk != NULL) {
2196                 if (rc >= 0) {
2197                         /* success so far.  Note that anything going wrong
2198                          * with bulk now, is EXTREMELY strange, since the
2199                          * server must have believed that the bulk
2200                          * tranferred OK before she replied with success to
2201                          * me. */
2202                         lwi = LWI_TIMEOUT(timeout, NULL, NULL);
2203                         brc = l_wait_event(req->rq_reply_waitq,
2204                                            !ptlrpc_client_bulk_active(req),
2205                                            &lwi);
2206                         LASSERT(brc == 0 || brc == -ETIMEDOUT);
2207                         if (brc != 0) {
2208                                 LASSERT(brc == -ETIMEDOUT);
2209                                 DEBUG_REQ(D_ERROR, req, "bulk timed out");
2210                                 rc = brc;
2211                         } else if (!req->rq_bulk->bd_success) {
2212                                 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
2213                                 rc = -EIO;
2214                         }
2215                 }
2216                 if (rc < 0)
2217                         ptlrpc_unregister_bulk(req, 0);
2218         }
2219
2220         LASSERT(!req->rq_receiving_reply);
2221         ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
2222         cfs_waitq_broadcast(&imp->imp_recovery_waitq);
2223         RETURN(rc);
2224 }
2225
2226 struct ptlrpc_replay_async_args {
2227         int praa_old_state;
2228         int praa_old_status;
2229 };
2230
2231 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
2232                                     void * data, int rc)
2233 {
2234         struct ptlrpc_replay_async_args *aa = data;
2235         struct obd_import *imp = req->rq_import;
2236
2237         ENTRY;
2238         atomic_dec(&imp->imp_replay_inflight);
2239
2240         if (!ptlrpc_client_replied(req)) {
2241                 CERROR("request replay timed out, restarting recovery\n");
2242                 GOTO(out, rc = -ETIMEDOUT);
2243         }
2244
2245         if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2246             (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2247              lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2248                 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2249
2250         /* VBR: check version failure */
2251         if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2252                 /* replay was failed due to version mismatch */
2253                 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2254                 spin_lock(&imp->imp_lock);
2255                 imp->imp_vbr_failed = 1;
2256                 imp->imp_no_lock_replay = 1;
2257                 spin_unlock(&imp->imp_lock);
2258         } else {
2259                 /* The transno had better not change over replay. */
2260                 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
2261                         lustre_msg_get_transno(req->rq_repmsg) ||
2262                         lustre_msg_get_transno(req->rq_repmsg) == 0);
2263         }
2264
2265         spin_lock(&imp->imp_lock);
2266         /* if replays by version then gap was occur on server, no trust to locks */
2267         if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2268                 imp->imp_no_lock_replay = 1;
2269         imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2270         spin_unlock(&imp->imp_lock);
2271         LASSERT(imp->imp_last_replay_transno);
2272
2273         DEBUG_REQ(D_HA, req, "got rep");
2274
2275         /* let the callback do fixups, possibly including in the request */
2276         if (req->rq_replay_cb)
2277                 req->rq_replay_cb(req);
2278
2279         if (ptlrpc_client_replied(req) &&
2280             lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2281                 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2282                           lustre_msg_get_status(req->rq_repmsg),
2283                           aa->praa_old_status);
2284         } else {
2285                 /* Put it back for re-replay. */
2286                 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2287         }
2288
2289         /* continue with recovery */
2290         rc = ptlrpc_import_recovery_state_machine(imp);
2291  out:
2292         req->rq_send_state = aa->praa_old_state;
2293
2294         if (rc != 0)
2295                 /* this replay failed, so restart recovery */
2296                 ptlrpc_connect_import(imp, NULL);
2297
2298         RETURN(rc);
2299 }
2300
2301
2302 int ptlrpc_replay_req(struct ptlrpc_request *req)
2303 {
2304         struct ptlrpc_replay_async_args *aa;
2305         ENTRY;
2306
2307         LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2308
2309         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2310         aa = ptlrpc_req_async_args(req);
2311         memset(aa, 0, sizeof *aa);
2312
2313         /* Prepare request to be resent with ptlrpcd */
2314         aa->praa_old_state = req->rq_send_state;
2315         req->rq_send_state = LUSTRE_IMP_REPLAY;
2316         req->rq_phase = RQ_PHASE_NEW;
2317         req->rq_next_phase = RQ_PHASE_UNDEFINED;
2318         if (req->rq_repmsg)
2319                 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2320         req->rq_status = 0;
2321         req->rq_interpret_reply = ptlrpc_replay_interpret;
2322         /* Readjust the timeout for current conditions */
2323         ptlrpc_at_set_req_timeout(req);
2324
2325         DEBUG_REQ(D_HA, req, "REPLAY");
2326
2327         atomic_inc(&req->rq_import->imp_replay_inflight);
2328         ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2329
2330         ptlrpcd_add_req(req);
2331         RETURN(0);
2332 }
2333
2334 void ptlrpc_abort_inflight(struct obd_import *imp)
2335 {
2336         struct list_head *tmp, *n;
2337         ENTRY;
2338
2339         /* Make sure that no new requests get processed for this import.
2340          * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2341          * this flag and then putting requests on sending_list or delayed_list.
2342          */
2343         spin_lock(&imp->imp_lock);
2344
2345         /* XXX locking?  Maybe we should remove each request with the list
2346          * locked?  Also, how do we know if the requests on the list are
2347          * being freed at this time?
2348          */
2349         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2350                 struct ptlrpc_request *req =
2351                         list_entry(tmp, struct ptlrpc_request, rq_list);
2352
2353                 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2354
2355                 spin_lock (&req->rq_lock);
2356                 if (req->rq_import_generation < imp->imp_generation) {
2357                         req->rq_err = 1;
2358                         req->rq_status = -EINTR;
2359                         ptlrpc_client_wake_req(req);
2360                 }
2361                 spin_unlock (&req->rq_lock);
2362         }
2363
2364         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2365                 struct ptlrpc_request *req =
2366                         list_entry(tmp, struct ptlrpc_request, rq_list);
2367
2368                 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2369
2370                 spin_lock (&req->rq_lock);
2371                 if (req->rq_import_generation < imp->imp_generation) {
2372                         req->rq_err = 1;
2373                         req->rq_status = -EINTR;
2374                         ptlrpc_client_wake_req(req);
2375                 }
2376                 spin_unlock (&req->rq_lock);
2377         }
2378
2379         /* Last chance to free reqs left on the replay list, but we
2380          * will still leak reqs that haven't committed.  */
2381         if (imp->imp_replayable)
2382                 ptlrpc_free_committed(imp);
2383
2384         spin_unlock(&imp->imp_lock);
2385
2386         EXIT;
2387 }
2388
2389 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2390 {
2391         struct list_head *tmp, *pos;
2392
2393         LASSERT(set != NULL);
2394
2395         list_for_each_safe(pos, tmp, &set->set_requests) {
2396                 struct ptlrpc_request *req =
2397                         list_entry(pos, struct ptlrpc_request, rq_set_chain);
2398
2399                 spin_lock(&req->rq_lock);
2400                 if (req->rq_phase != RQ_PHASE_RPC) {
2401                         spin_unlock(&req->rq_lock);
2402                         continue;
2403                 }
2404
2405                 req->rq_err = 1;
2406                 req->rq_status = -EINTR;
2407                 ptlrpc_client_wake_req(req);
2408                 spin_unlock(&req->rq_lock);
2409         }
2410 }
2411
2412 static __u64 ptlrpc_last_xid;
2413 static spinlock_t ptlrpc_last_xid_lock;
2414
2415 /* Initialize the XID for the node.  This is common among all requests on
2416  * this node, and only requires the property that it is monotonically
2417  * increasing.  It does not need to be sequential.  Since this is also used
2418  * as the RDMA match bits, it is important that a single client NOT have
2419  * the same match bits for two different in-flight requests, hence we do
2420  * NOT want to have an XID per target or similar.
2421  *
2422  * To avoid an unlikely collision between match bits after a client reboot
2423  * (which would deliver old data into the wrong RDMA buffer) we initialize
2424  * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2425  * If the time is clearly incorrect, we instead use a 62-bit random number.
2426  * In the worst case the random number will overflow 1M RPCs per second in
2427  * 9133 years, or permutations thereof.
2428  */
2429 #define YEAR_2004 (1ULL << 30)
2430 void ptlrpc_init_xid(void)
2431 {
2432         time_t now = cfs_time_current_sec();
2433
2434         spin_lock_init(&ptlrpc_last_xid_lock);
2435         if (now < YEAR_2004) {
2436                 ll_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2437                 ptlrpc_last_xid >>= 2;
2438                 ptlrpc_last_xid |= (1ULL << 61);
2439         } else {
2440                 ptlrpc_last_xid = (__u64)now << 20;
2441         }
2442 }
2443
2444 __u64 ptlrpc_next_xid(void)
2445 {
2446         __u64 tmp;
2447         spin_lock(&ptlrpc_last_xid_lock);
2448         tmp = ++ptlrpc_last_xid;
2449         spin_unlock(&ptlrpc_last_xid_lock);
2450         return tmp;
2451 }
2452
2453 __u64 ptlrpc_sample_next_xid(void)
2454 {
2455         if (sizeof(long) < 8) {
2456                 /* need to avoid possible word tearing on 32-bit systems */
2457                 __u64 tmp;
2458                 spin_lock(&ptlrpc_last_xid_lock);
2459                 tmp = ptlrpc_last_xid + 1;
2460                 spin_unlock(&ptlrpc_last_xid_lock);
2461                 return tmp;
2462         }
2463         /* No need to lock, since returned value is racy anyways */
2464         return ptlrpc_last_xid + 1;
2465 }
2466 EXPORT_SYMBOL(ptlrpc_sample_next_xid);