Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / ptlrpc / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_lib.h>
28 #include <linux/lustre_ha.h>
29 #include <linux/lustre_import.h>
30
31 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
32                         struct ptlrpc_client *cl)
33 {
34         cl->cli_request_portal = req_portal;
35         cl->cli_reply_portal   = rep_portal;
36         cl->cli_name           = name;
37 }
38
39 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
40 {
41         return req->rq_connection->c_remote_uuid;
42 }
43
44 struct ptlrpc_connection *ptlrpc_uuid_to_connection(obd_uuid_t uuid)
45 {
46         struct ptlrpc_connection *c;
47         struct lustre_peer peer;
48         int err;
49
50         err = kportal_uuid_to_peer(uuid, &peer);
51         if (err != 0) {
52                 CERROR("cannot find peer %s!\n", uuid);
53                 return NULL;
54         }
55
56         c = ptlrpc_get_connection(&peer, uuid);
57         if (c) {
58                 memcpy(c->c_remote_uuid, uuid, sizeof(c->c_remote_uuid));
59                 c->c_epoch++;
60         }
61
62         CDEBUG(D_INFO, "%s -> %p\n", uuid, c);
63
64         return c;
65 }
66
67 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,obd_uuid_t uuid)
68 {
69         struct lustre_peer peer;
70         int err;
71
72         err = kportal_uuid_to_peer(uuid, &peer);
73         if (err != 0) {
74                 CERROR("cannot find peer %s!\n", uuid);
75                 return;
76         }
77
78         memcpy(&conn->c_peer, &peer, sizeof(peer));
79         return;
80 }
81
82 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
83 {
84         struct ptlrpc_bulk_desc *desc;
85
86         OBD_ALLOC(desc, sizeof(*desc));
87         if (desc != NULL) {
88                 desc->bd_connection = ptlrpc_connection_addref(conn);
89                 atomic_set(&desc->bd_refcount, 1);
90                 init_waitqueue_head(&desc->bd_waitq);
91                 INIT_LIST_HEAD(&desc->bd_page_list);
92                 INIT_LIST_HEAD(&desc->bd_set_chain);
93                 ptl_set_inv_handle(&desc->bd_md_h);
94                 ptl_set_inv_handle(&desc->bd_me_h);
95         }
96
97         return desc;
98 }
99
100 int ptlrpc_bulk_error(struct ptlrpc_bulk_desc *desc)
101 {
102         int rc = 0;
103         if (desc->bd_flags & PTL_RPC_FL_TIMEOUT) {
104                 rc = (desc->bd_flags & PTL_RPC_FL_INTR ? -ERESTARTSYS :
105                       -ETIMEDOUT);
106         }
107         return rc;
108 }
109
110 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
111 {
112         struct ptlrpc_bulk_page *bulk;
113
114         OBD_ALLOC(bulk, sizeof(*bulk));
115         if (bulk != NULL) {
116                 bulk->bp_desc = desc;
117                 list_add_tail(&bulk->bp_link, &desc->bd_page_list);
118                 desc->bd_page_count++;
119         }
120         return bulk;
121 }
122
123 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
124 {
125         struct list_head *tmp, *next;
126         ENTRY;
127         if (desc == NULL) {
128                 EXIT;
129                 return;
130         }
131
132         LASSERT(list_empty(&desc->bd_set_chain));
133
134         if (atomic_read(&desc->bd_refcount) != 0)
135                 CERROR("freeing desc %p with refcount %d!\n", desc,
136                        atomic_read(&desc->bd_refcount));
137
138         list_for_each_safe(tmp, next, &desc->bd_page_list) {
139                 struct ptlrpc_bulk_page *bulk;
140                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
141                 ptlrpc_free_bulk_page(bulk);
142         }
143
144         ptlrpc_put_connection(desc->bd_connection);
145
146         OBD_FREE(desc, sizeof(*desc));
147         EXIT;
148 }
149
150 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
151 {
152         ENTRY;
153         if (bulk == NULL) {
154                 EXIT;
155                 return;
156         }
157
158         list_del(&bulk->bp_link);
159         bulk->bp_desc->bd_page_count--;
160         OBD_FREE(bulk, sizeof(*bulk));
161         EXIT;
162 }
163
164 static int ll_sync_brw_timeout(void *data)
165 {
166         struct obd_brw_set *set = data;
167         struct list_head *tmp;
168         int failed = 0;
169         ENTRY;
170
171         LASSERT(set);
172
173         set->brw_flags |= PTL_RPC_FL_TIMEOUT;
174
175         list_for_each(tmp, &set->brw_desc_head) {
176                 struct ptlrpc_bulk_desc *desc =
177                         list_entry(tmp, struct ptlrpc_bulk_desc, bd_set_chain);
178
179                 /* Skip descriptors that were completed successfully. */
180                 if (desc->bd_flags & (PTL_BULK_FL_RCVD | PTL_BULK_FL_SENT))
181                         continue;
182
183                 LASSERT(desc->bd_connection);
184
185                 /* If PtlMDUnlink succeeds, then it hasn't completed yet.  If it
186                  * fails, the bulk finished _just_ in time (after the timeout
187                  * fired but before we got this far) and we'll let it live.
188                  */
189                 if (PtlMDUnlink(desc->bd_md_h) != 0) {
190                         CERROR("Near-miss on OST %s -- need to adjust "
191                                "obd_timeout?\n",
192                                desc->bd_connection->c_remote_uuid);
193                         continue;
194                 }
195
196                 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
197                        desc->bd_page_count, desc->bd_connection->c_remote_uuid,
198                        desc->bd_portal, desc->bd_connection);
199
200                 /* This one will "never" arrive, don't wait for it. */
201                 if (atomic_dec_and_test(&set->brw_refcount))
202                         wake_up(&set->brw_waitq);
203
204                 if (class_signal_connection_failure)
205                         class_signal_connection_failure(desc->bd_connection);
206                 else
207                         failed = 1;
208         }
209
210         /* 0 = We go back to sleep, until we're resumed or interrupted */
211         /* 1 = We can't be recovered, just abort the syscall with -ETIMEDOUT */
212         RETURN(failed);
213 }
214
215 static int ll_sync_brw_intr(void *data)
216 {
217         struct obd_brw_set *set = data;
218
219         ENTRY;
220         set->brw_flags |= PTL_RPC_FL_INTR;
221         RETURN(1); /* ignored, as of this writing */
222 }
223
224 int ll_brw_sync_wait(struct obd_brw_set *set, int phase)
225 {
226         struct l_wait_info lwi;
227         struct list_head *tmp, *next;
228         int rc = 0;
229         ENTRY;
230
231         switch(phase) {
232         case CB_PHASE_START:
233                 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ll_sync_brw_timeout,
234                                        ll_sync_brw_intr, set);
235                 rc = l_wait_event(set->brw_waitq,
236                                   atomic_read(&set->brw_refcount) == 0, &lwi);
237
238                 list_for_each_safe(tmp, next, &set->brw_desc_head) {
239                         struct ptlrpc_bulk_desc *desc =
240                                 list_entry(tmp, struct ptlrpc_bulk_desc,
241                                            bd_set_chain);
242                         list_del_init(&desc->bd_set_chain);
243                         ptlrpc_bulk_decref(desc);
244                 }
245                 break;
246         case CB_PHASE_FINISH:
247                 if (atomic_dec_and_test(&set->brw_refcount))
248                         wake_up(&set->brw_waitq);
249                 break;
250         default:
251                 LBUG();
252         }
253
254         RETURN(rc);
255 }
256
257 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
258                                        int count, int *lengths, char **bufs)
259 {
260         struct ptlrpc_connection *conn;
261         struct ptlrpc_request *request;
262         unsigned long flags;
263         int rc;
264         ENTRY;
265
266         LASSERT((unsigned long)imp > 0x1000);
267         conn = imp->imp_connection;
268
269         OBD_ALLOC(request, sizeof(*request));
270         if (!request) {
271                 CERROR("request allocation out of memory\n");
272                 RETURN(NULL);
273         }
274
275         rc = lustre_pack_msg(count, lengths, bufs,
276                              &request->rq_reqlen, &request->rq_reqmsg);
277         if (rc) {
278                 CERROR("cannot pack request %d\n", rc);
279                 OBD_FREE(request, sizeof(*request));
280                 RETURN(NULL);
281         }
282
283         request->rq_level = LUSTRE_CONN_FULL;
284         request->rq_type = PTL_RPC_MSG_REQUEST;
285         request->rq_import = imp;
286
287         /* XXX FIXME bug 625069 */
288         request->rq_request_portal = imp->imp_client->cli_request_portal;
289         request->rq_reply_portal = imp->imp_client->cli_reply_portal;
290
291         request->rq_connection = ptlrpc_connection_addref(conn);
292
293         INIT_LIST_HEAD(&request->rq_list);
294         atomic_set(&request->rq_refcount, 1);
295
296         spin_lock_irqsave(&imp->imp_lock, flags);
297         request->rq_xid = HTON__u32(++imp->imp_last_xid);
298         spin_unlock_irqrestore(&imp->imp_lock, flags);
299
300         request->rq_reqmsg->magic = PTLRPC_MSG_MAGIC;
301         request->rq_reqmsg->version = PTLRPC_MSG_VERSION;
302         request->rq_reqmsg->opc = HTON__u32(opcode);
303         request->rq_reqmsg->flags = 0;
304
305         ptlrpc_hdl2req(request, &imp->imp_handle);
306         RETURN(request);
307 }
308
309 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
310 {
311         ENTRY;
312         if (request == NULL) {
313                 EXIT;
314                 return;
315         }
316
317         if (atomic_read(&request->rq_refcount) != 0) {
318                 CERROR("freeing request %p (%d->%s:%d) with refcount %d\n",
319                        request, request->rq_reqmsg->opc,
320                        request->rq_connection->c_remote_uuid,
321                        request->rq_import->imp_client->cli_request_portal,
322                        atomic_read (&request->rq_refcount));
323                 /* LBUG(); */
324         }
325
326         if (request->rq_repmsg != NULL) {
327                 OBD_FREE(request->rq_repmsg, request->rq_replen);
328                 request->rq_repmsg = NULL;
329                 request->rq_reply_md.start = NULL;
330         }
331         if (request->rq_reqmsg != NULL) {
332                 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
333                 request->rq_reqmsg = NULL;
334         }
335
336         if (request->rq_import) {
337                 unsigned long flags = 0;
338                 if (!locked)
339                         spin_lock_irqsave(&request->rq_import->imp_lock, flags);
340                 list_del_init(&request->rq_list);
341                 if (!locked)
342                         spin_unlock_irqrestore(&request->rq_import->imp_lock,
343                                                flags);
344         }
345
346         ptlrpc_put_connection(request->rq_connection);
347         OBD_FREE(request, sizeof(*request));
348         EXIT;
349 }
350
351 void ptlrpc_free_req(struct ptlrpc_request *request)
352 {
353         __ptlrpc_free_req(request, 0);
354 }
355
356 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
357 {
358         ENTRY;
359         if (request == NULL)
360                 RETURN(1);
361
362         if (request == (void *)(long)(0x5a5a5a5a5a5a5a5a)) {
363                 CERROR("dereferencing freed request (bug 575)\n");
364                 LBUG();
365                 RETURN(1);
366         }
367
368         DEBUG_REQ(D_INFO, request, "refcount now %u",
369                   atomic_read(&request->rq_refcount) - 1);
370
371         if (atomic_dec_and_test(&request->rq_refcount)) {
372                 __ptlrpc_free_req(request, locked);
373                 RETURN(1);
374         }
375
376         RETURN(0);
377 }
378
379 void ptlrpc_req_finished(struct ptlrpc_request *request)
380 {
381         __ptlrpc_req_finished(request, 0);
382 }
383
384 static int ptlrpc_check_reply(struct ptlrpc_request *req)
385 {
386         int rc = 0;
387
388         ENTRY;
389         if (req->rq_repmsg != NULL) {
390                 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
391                 /* Store transno in reqmsg for replay. */
392                 req->rq_reqmsg->transno = req->rq_repmsg->transno;
393                 req->rq_flags |= PTL_RPC_FL_REPLIED;
394                 GOTO(out, rc = 1);
395         }
396
397         if (req->rq_flags & PTL_RPC_FL_RESEND) {
398                 ENTRY;
399                 DEBUG_REQ(D_ERROR, req, "RESEND:");
400                 GOTO(out, rc = 1);
401         }
402
403         if (req->rq_flags & PTL_RPC_FL_ERR) {
404                 ENTRY;
405                 DEBUG_REQ(D_ERROR, req, "ABORTED:");
406                 GOTO(out, rc = 1);
407         }
408
409         if (req->rq_flags & PTL_RPC_FL_RESTART) {
410                 DEBUG_REQ(D_ERROR, req, "RESTART:");
411                 GOTO(out, rc = 1);
412         }
413         EXIT;
414  out:
415         DEBUG_REQ(D_NET, req, "rc = %d for", rc);
416         return rc;
417 }
418
419 static int ptlrpc_check_status(struct ptlrpc_request *req)
420 {
421         int err;
422         ENTRY;
423
424         err = req->rq_repmsg->status;
425         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
426                 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR (%d)", err);
427                 RETURN(err ? err : -EINVAL);
428         }
429
430         if (err < 0) {
431                 DEBUG_REQ(D_ERROR, req, "status is %d", err);
432         } else if (err > 0) {
433                 /* XXX: translate this error from net to host */
434                 DEBUG_REQ(D_INFO, req, "status is %d", err);
435         }
436
437         RETURN(err);
438 }
439
440 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
441 {
442         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
443         request->rq_reqmsg = NULL;
444         request->rq_reqlen = 0;
445 }
446
447 /* Abort this request and cleanup any resources associated with it. */
448 static int ptlrpc_abort(struct ptlrpc_request *request)
449 {
450         /* First remove the ME for the reply; in theory, this means
451          * that we can tear down the buffer safely. */
452         if (PtlMEUnlink(request->rq_reply_me_h) != PTL_OK)
453                 RETURN(0);
454         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
455
456         memset(&request->rq_reply_me_h, 0, sizeof(request->rq_reply_me_h));
457         request->rq_reply_md.start = NULL;
458         request->rq_repmsg = NULL;
459         return 0;
460 }
461
462 /* caller must hold imp->imp_lock */
463 void ptlrpc_free_committed(struct obd_import *imp)
464 {
465         struct list_head *tmp, *saved;
466         struct ptlrpc_request *req;
467         ENTRY;
468
469 #ifdef CONFIG_SMP
470         LASSERT(spin_is_locked(&imp->imp_lock));
471 #endif
472
473         CDEBUG(D_HA, "committing for xid "LPU64", last_committed "LPU64"\n",
474                imp->imp_peer_last_xid, imp->imp_peer_committed_transno);
475
476         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
477                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
478
479                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
480                         DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
481                         continue;
482                 }
483
484                 /* not yet committed */
485                 if (req->rq_transno > imp->imp_peer_committed_transno) {
486                         DEBUG_REQ(D_HA, req, "stopping search");
487                         break;
488                 }
489
490                 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
491                           imp->imp_peer_committed_transno);
492                 __ptlrpc_req_finished(req, 1);
493         }
494
495         EXIT;
496         return;
497 }
498
499 void ptlrpc_cleanup_client(struct obd_import *imp)
500 {
501         struct list_head *tmp, *saved;
502         struct ptlrpc_request *req;
503         struct ptlrpc_connection *conn = imp->imp_connection;
504         unsigned long flags;
505         ENTRY;
506
507         LASSERT(conn);
508
509         spin_lock_irqsave(&imp->imp_lock, flags);
510         list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
511                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
512
513                 /* XXX we should make sure that nobody's sleeping on these! */
514                 DEBUG_REQ(D_HA, req, "cleaning up from sending list");
515                 list_del_init(&req->rq_list);
516                 req->rq_import = NULL;
517                 __ptlrpc_req_finished(req, 0);
518         }
519         spin_unlock_irqrestore(&imp->imp_lock, flags);
520         
521         EXIT;
522         return;
523 }
524
525 void ptlrpc_continue_req(struct ptlrpc_request *req)
526 {
527         ENTRY;
528         DEBUG_REQ(D_HA, req, "continuing delayed request");
529         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
530         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
531         wake_up(&req->rq_wait_for_rep);
532         EXIT;
533 }
534
535 void ptlrpc_resend_req(struct ptlrpc_request *req)
536 {
537         ENTRY;
538         DEBUG_REQ(D_HA, req, "resending");
539         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
540         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
541         req->rq_status = -EAGAIN;
542         req->rq_level = LUSTRE_CONN_RECOVD;
543         req->rq_flags |= PTL_RPC_FL_RESEND;
544         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
545         wake_up(&req->rq_wait_for_rep);
546         EXIT;
547 }
548
549 void ptlrpc_restart_req(struct ptlrpc_request *req)
550 {
551         ENTRY;
552         DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
553         req->rq_status = -ERESTARTSYS;
554         req->rq_flags |= PTL_RPC_FL_RESTART;
555         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
556         wake_up(&req->rq_wait_for_rep);
557         EXIT;
558 }
559
560 static int expired_request(void *data)
561 {
562         struct ptlrpc_request *req = data;
563
564         ENTRY;
565         if (!req) {
566                 CERROR("NULL req!");
567                 LBUG();
568                 RETURN(0);
569         }
570
571         DEBUG_REQ(D_ERROR, req, "timeout");
572         ptlrpc_abort(req);
573         req->rq_flags |= PTL_RPC_FL_TIMEOUT;
574
575         if (!req->rq_import) {
576                 DEBUG_REQ(D_ERROR, req, "NULL import");
577                 LBUG();
578                 RETURN(0);
579         }
580
581         if (!req->rq_import->imp_connection) {
582                 DEBUG_REQ(D_ERROR, req, "NULL connection");
583                 LBUG();
584                 RETURN(0);
585         }
586
587         if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
588                 RETURN(1);
589
590         recovd_conn_fail(req->rq_import->imp_connection);
591
592         /* If this request is for recovery or other primordial tasks,
593          * don't go back to sleep.
594          */
595         if (req->rq_level < LUSTRE_CONN_FULL)
596                 RETURN(1);
597         RETURN(0);
598 }
599
600 static int interrupted_request(void *data)
601 {
602         struct ptlrpc_request *req = data;
603         ENTRY;
604         req->rq_flags |= PTL_RPC_FL_INTR;
605         RETURN(1); /* ignored, as of this writing */
606 }
607
608 int ptlrpc_queue_wait(struct ptlrpc_request *req)
609 {
610         int rc = 0;
611         struct l_wait_info lwi;
612         struct obd_import *imp = req->rq_import;
613         struct ptlrpc_connection *conn = imp->imp_connection;
614         unsigned int flags;
615         ENTRY;
616
617         init_waitqueue_head(&req->rq_wait_for_rep);
618
619         /* for distributed debugging */
620         req->rq_reqmsg->status = HTON__u32(current->pid); 
621         CDEBUG(D_RPCTRACE, "Sending RPC pid:xid:nid:opc %d:"LPU64":%x:%d\n",
622                NTOH__u32(req->rq_reqmsg->status), req->rq_xid,
623                conn->c_peer.peer_nid, NTOH__u32(req->rq_reqmsg->opc));
624
625         spin_lock_irqsave(&imp->imp_lock, flags);
626
627         /* 
628          * If the import has been invalidated (such as by an OST failure), the
629          * request must fail with -EIO.
630          */
631         if (req->rq_import->imp_flags & IMP_INVALID) {
632                 DEBUG_REQ(D_ERROR, req, "IMP_INVALID:");
633                 spin_unlock_irqrestore(&imp->imp_lock, flags);
634                 RETURN(-EIO);
635         }
636
637         if (req->rq_level > imp->imp_level) {
638                 list_del(&req->rq_list);
639                 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
640                 spin_unlock_irqrestore(&imp->imp_lock, flags);
641
642                 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%d < %d)",
643                           current->comm, req->rq_level, imp->imp_level);
644                 lwi = LWI_INTR(NULL, NULL);
645                 rc = l_wait_event(req->rq_wait_for_rep,
646                                   (req->rq_level <= imp->imp_level) ||
647                                   (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
648
649                 spin_lock_irqsave(&imp->imp_lock, flags);
650                 list_del_init(&req->rq_list);
651
652                 if (req->rq_flags & PTL_RPC_FL_ERR)
653                         rc = -EIO;
654
655                 if (rc) {
656                         spin_unlock_irqrestore(&imp->imp_lock, flags);
657                         RETURN(rc);
658                 }
659
660                 CERROR("process %d resumed\n", current->pid);
661         }
662  resend:
663
664         LASSERT(list_empty(&req->rq_list));
665         list_add_tail(&req->rq_list, &imp->imp_sending_list);
666         spin_unlock_irqrestore(&imp->imp_lock, flags);
667         rc = ptl_send_rpc(req);
668         if (rc) {
669                 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
670                        req->rq_reqmsg->opc);
671                 /* sleep for a jiffy, then trigger recovery */
672                 lwi = LWI_TIMEOUT_INTR(1, expired_request,
673                                        interrupted_request, req);
674         } else {
675                 DEBUG_REQ(D_NET, req, "-- sleeping");
676                 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_request,
677                                        interrupted_request, req);
678         }
679         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
680         DEBUG_REQ(D_NET, req, "-- done sleeping");
681
682         spin_lock_irqsave(&imp->imp_lock, flags);
683         list_del_init(&req->rq_list);
684         spin_unlock_irqrestore(&imp->imp_lock, flags);
685
686         if (req->rq_flags & PTL_RPC_FL_ERR) {
687                 ptlrpc_abort(req);
688                 GOTO(out, rc = -EIO);
689         }
690
691         /* Don't resend if we were interrupted. */
692         if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
693             PTL_RPC_FL_RESEND) {
694                 req->rq_flags &= ~PTL_RPC_FL_RESEND;
695                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
696                 DEBUG_REQ(D_HA, req, "resending: ");
697                 spin_lock_irqsave(&imp->imp_lock, flags);
698                 goto resend;
699         }
700
701         if (req->rq_flags & PTL_RPC_FL_INTR) {
702                 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
703                         LBUG(); /* should only be interrupted if we timed out */
704                 /* Clean up the dangling reply buffers */
705                 ptlrpc_abort(req);
706                 GOTO(out, rc = -EINTR);
707         }
708
709         if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
710                 GOTO(out, rc = -ETIMEDOUT);
711
712         if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
713                 GOTO(out, rc = req->rq_status);
714
715         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
716         if (rc) {
717                 CERROR("unpack_rep failed: %d\n", rc);
718                 GOTO(out, rc);
719         }
720 #if 0
721         /* FIXME: Enable when BlueArc makes new release */
722         if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
723             req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
724                 CERROR("invalid packet type received (type=%u)\n",
725                        req->rq_repmsg->type);
726                 LBUG();
727                 GOTO(out, rc = -EINVAL);
728         }
729 #endif
730         DEBUG_REQ(D_NET, req, "status %d", req->rq_repmsg->status);
731
732         /* We're a rejected connection, need to invalidate and rebuild. */
733         if (req->rq_repmsg->status == -ENOTCONN) {
734                 spin_lock_irqsave(&imp->imp_lock, flags);
735                 /* If someone else is reconnecting us (CONN_RECOVD) or has
736                  * already completed it (handle mismatch), then we just need
737                  * to get out.
738                  */
739                 if (imp->imp_level == LUSTRE_CONN_RECOVD ||
740                     imp->imp_handle.addr != req->rq_reqmsg->addr ||
741                     imp->imp_handle.cookie != req->rq_reqmsg->cookie) {
742                         spin_unlock_irqrestore(&imp->imp_lock, flags);
743                         GOTO(out, rc = -EIO);
744                 }
745                 imp->imp_level = LUSTRE_CONN_RECOVD;
746                 spin_unlock_irqrestore(&imp->imp_lock, flags);
747                 rc = imp->imp_recover(imp, PTLRPC_RECOVD_PHASE_NOTCONN);
748                 if (rc)
749                         LBUG();
750                 GOTO(out, rc = -EIO);
751         }
752
753         rc = ptlrpc_check_status(req);
754
755         if (req->rq_import->imp_flags & IMP_REPLAYABLE) {
756                 spin_lock_irqsave(&imp->imp_lock, flags);
757                 if ((req->rq_flags & PTL_RPC_FL_REPLAY || req->rq_transno != 0)
758                     && rc >= 0) {
759                         /* Balanced in ptlrpc_free_committed, usually. */
760                         atomic_inc(&req->rq_refcount);
761                         list_add_tail(&req->rq_list, &imp->imp_replay_list);
762                 }
763
764                 if (req->rq_transno > imp->imp_max_transno) {
765                         imp->imp_max_transno = req->rq_transno;
766                 } else if (req->rq_transno != 0 &&
767                            imp->imp_level == LUSTRE_CONN_FULL) {
768                         CDEBUG(D_HA, "got transno "LPD64" after "LPD64
769                                ": recovery may not work\n", req->rq_transno,
770                                imp->imp_max_transno);
771                 }
772
773                 /* Replay-enabled imports return commit-status information. */
774                 imp->imp_peer_last_xid = req->rq_repmsg->last_xid;
775                 imp->imp_peer_committed_transno =
776                         req->rq_repmsg->last_committed;
777                 ptlrpc_free_committed(imp);
778                 spin_unlock_irqrestore(&imp->imp_lock, flags);
779         }
780
781         EXIT;
782  out:
783         return rc;
784 }
785
786 int ptlrpc_replay_req(struct ptlrpc_request *req)
787 {
788         int rc = 0, old_level, old_status = 0;
789         // struct ptlrpc_client *cli = req->rq_import->imp_client;
790         struct l_wait_info lwi;
791         ENTRY;
792
793         init_waitqueue_head(&req->rq_wait_for_rep);
794         DEBUG_REQ(D_NET, req, "");
795
796         req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
797         req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
798
799         /* temporarily set request to RECOVD level (reset at out:) */
800         old_level = req->rq_level;
801         if (req->rq_flags & PTL_RPC_FL_REPLIED)
802                 old_status = req->rq_repmsg->status;
803         req->rq_level = LUSTRE_CONN_RECOVD;
804         rc = ptl_send_rpc(req);
805         if (rc) {
806                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
807                 ptlrpc_cleanup_request_buf(req);
808                 // up(&cli->cli_rpc_sem);
809                 GOTO(out, rc = -rc);
810         }
811
812         CDEBUG(D_OTHER, "-- sleeping\n");
813         lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
814         l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
815         CDEBUG(D_OTHER, "-- done\n");
816
817         // up(&cli->cli_rpc_sem);
818
819         if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
820                 CERROR("Unknown reason for wakeup\n");
821                 /* XXX Phil - I end up here when I kill obdctl */
822                 ptlrpc_abort(req);
823                 GOTO(out, rc = -EINTR);
824         }
825
826         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
827         if (rc) {
828                 CERROR("unpack_rep failed: %d\n", rc);
829                 GOTO(out, rc);
830         }
831
832         CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
833
834         /* let the callback do fixups, possibly including in the request */
835         if (req->rq_replay_cb)
836                 req->rq_replay_cb(req);
837
838         if ((req->rq_flags & PTL_RPC_FL_REPLIED) &&
839             req->rq_repmsg->status != old_status) {
840                 DEBUG_REQ(D_HA, req, "status %d, old was %d",
841                           req->rq_repmsg->status, old_status);
842         }
843
844  out:
845         req->rq_level = old_level;
846         RETURN(rc);
847 }
848
849 /* XXX looks a lot like super.c:invalidate_request_list, don't it? */
850 void ptlrpc_abort_inflight(struct obd_import *imp)
851 {
852         unsigned long flags;
853         struct list_head *tmp, *n;
854
855         /* Make sure that no new requests get processed for this import.
856          * ptlrpc_queue_wait must (and does) hold imp_lock while testing this
857          * flag and then putting requests on sending_list or delayed_list.
858          */
859         spin_lock_irqsave(&imp->imp_lock, flags);
860         imp->imp_flags |= IMP_INVALID;
861         spin_unlock_irqrestore(&imp->imp_lock, flags);
862
863         list_for_each_safe(tmp, n, &imp->imp_sending_list) {
864                 struct ptlrpc_request *req =
865                         list_entry(tmp, struct ptlrpc_request, rq_list);
866
867                 DEBUG_REQ(D_HA, req, "inflight");
868                 req->rq_flags |= PTL_RPC_FL_ERR;
869                 wake_up(&req->rq_wait_for_rep);
870         }
871
872         list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
873                 struct ptlrpc_request *req =
874                         list_entry(tmp, struct ptlrpc_request, rq_list);
875
876                 DEBUG_REQ(D_HA, req, "aborting waiting req");
877                 req->rq_flags |= PTL_RPC_FL_ERR;
878                 wake_up(&req->rq_wait_for_rep);
879         }
880 }