Whamcloud - gitweb
- reverting part of 15716 as it causes constant assert and stops all testing. Shadow...
[fs/lustre-release.git] / lustre / ptlrpc / import.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Mike Shaver <shaver@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <lustre_ha.h>
33 #include <lustre_net.h>
34 #include <lustre_import.h>
35 #include <lustre_export.h>
36 #include <obd.h>
37 #include <obd_cksum.h>
38 #include <obd_class.h>
39
40 #include "ptlrpc_internal.h"
41
42 struct ptlrpc_connect_async_args {
43          __u64 pcaa_peer_committed;
44         int pcaa_initial_connect;
45 };
46
47 /* A CLOSED import should remain so. */
48 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
49 do {                                                                           \
50         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
51                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
52                       imp, obd2cli_tgt(imp->imp_obd),                          \
53                       ptlrpc_import_state_name(imp->imp_state),                \
54                       ptlrpc_import_state_name(state));                        \
55                imp->imp_state = state;                                         \
56         }                                                                      \
57 } while(0)
58
59 #define IMPORT_SET_STATE(imp, state)            \
60 do {                                            \
61         spin_lock(&imp->imp_lock);              \
62         IMPORT_SET_STATE_NOLOCK(imp, state);    \
63         spin_unlock(&imp->imp_lock);            \
64 } while(0)
65
66
67 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
68                                     void * data, int rc);
69 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
70
71 /* Only this function is allowed to change the import state when it is
72  * CLOSED. I would rather refcount the import and free it after
73  * disconnection like we do with exports. To do that, the client_obd
74  * will need to save the peer info somewhere other than in the import,
75  * though. */
76 int ptlrpc_init_import(struct obd_import *imp)
77 {
78         spin_lock(&imp->imp_lock);
79
80         imp->imp_generation++;
81         imp->imp_state =  LUSTRE_IMP_NEW;
82
83         spin_unlock(&imp->imp_lock);
84
85         return 0;
86 }
87 EXPORT_SYMBOL(ptlrpc_init_import);
88
89 #define UUID_STR "_UUID"
90 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
91                       int *uuid_len)
92 {
93         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
94                 ? uuid : uuid + strlen(prefix);
95
96         *uuid_len = strlen(*uuid_start);
97
98         if (*uuid_len < strlen(UUID_STR))
99                 return;
100
101         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
102                     UUID_STR, strlen(UUID_STR)))
103                 *uuid_len -= strlen(UUID_STR);
104 }
105
106 /* Returns true if import was FULL, false if import was already not
107  * connected.
108  * @imp - import to be disconnected
109  * @conn_cnt - connection count (epoch) of the request that timed out
110  *             and caused the disconnection.  In some cases, multiple
111  *             inflight requests can fail to a single target (e.g. OST
112  *             bulk requests) and if one has already caused a reconnection
113  *             (increasing the import->conn_cnt) the older failure should
114  *             not also cause a reconnection.  If zero it forces a reconnect.
115  */
116 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
117 {
118         int rc = 0;
119
120         spin_lock(&imp->imp_lock);
121
122         if (imp->imp_state == LUSTRE_IMP_FULL &&
123             (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
124                 char *target_start;
125                 int   target_len;
126
127                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
128                           &target_start, &target_len);
129
130                 if (imp->imp_replayable) {
131                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
132                                "%s was lost; in progress operations using this "
133                                "service will wait for recovery to complete.\n",
134                                imp->imp_obd->obd_name, target_len, target_start,
135                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
136                 } else {
137                         LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
138                                            "%.*s via nid %s was lost; in progress"
139                                            "operations using this service will"
140                                            "fail.\n",
141                                            imp->imp_obd->obd_name,
142                                            target_len, target_start,
143                                  libcfs_nid2str(imp->imp_connection->c_peer.nid));
144                 }
145                 ptlrpc_deactivate_timeouts(imp);
146                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
147                 spin_unlock(&imp->imp_lock);
148     
149                 if (obd_dump_on_timeout)
150                         libcfs_debug_dumplog();
151
152                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
153                 rc = 1;
154         } else {
155                 spin_unlock(&imp->imp_lock);
156                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
157                        imp->imp_client->cli_name, imp,
158                        (imp->imp_state == LUSTRE_IMP_FULL &&
159                         imp->imp_conn_cnt > conn_cnt) ?
160                        "reconnected" : "not connected", imp->imp_conn_cnt,
161                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
162         }
163
164         return rc;
165 }
166
167 /* Must be called with imp_lock held! */
168 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
169 {
170         ENTRY;
171         LASSERT_SPIN_LOCKED(&imp->imp_lock);
172
173         if (imp->imp_invalid) {
174                 spin_unlock(&imp->imp_lock);
175                 EXIT;
176                 return;
177         }
178
179         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
180         imp->imp_invalid = 1;
181         imp->imp_generation++;
182         spin_unlock(&imp->imp_lock);
183
184         ptlrpc_abort_inflight(imp);
185         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
186
187         EXIT;
188 }
189
190 /*
191  * This acts as a barrier; all existing requests are rejected, and
192  * no new requests will be accepted until the import is valid again.
193  */
194 void ptlrpc_deactivate_import(struct obd_import *imp)
195 {
196         spin_lock(&imp->imp_lock);
197         ptlrpc_deactivate_and_unlock_import(imp);
198 }
199
200 /*
201  * This function will invalidate the import, if necessary, then block
202  * for all the RPC completions, and finally notify the obd to
203  * invalidate its state (ie cancel locks, clear pending requests,
204  * etc).
205  */
206 void ptlrpc_invalidate_import(struct obd_import *imp)
207 {
208         struct l_wait_info lwi;
209         int rc;
210
211         atomic_inc(&imp->imp_inval_count);
212
213         /*
214          * If this is an invalid MGC connection, then don't bother
215          * waiting for imp_inflight to drop to 0.
216          */
217         if (imp->imp_invalid && imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
218                 goto out;
219
220         if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
221                 ptlrpc_deactivate_import(imp);
222
223         LASSERT(imp->imp_invalid);
224
225         /* wait for all requests to error out and call completion callbacks */
226         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
227                                    HZ, NULL, NULL);
228         rc = l_wait_event(imp->imp_recovery_waitq,
229                           (atomic_read(&imp->imp_inflight) == 0), &lwi);
230
231         if (rc) {
232                 struct list_head *tmp, *n;
233                 struct ptlrpc_request *req;
234
235                 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
236                        obd2cli_tgt(imp->imp_obd), rc,
237                        atomic_read(&imp->imp_inflight));
238                 spin_lock(&imp->imp_lock);
239                 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
240                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
241                         DEBUG_REQ(D_ERROR, req, "still on sending list");
242                 }
243                 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
244                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
245                         DEBUG_REQ(D_ERROR, req, "still on delayed list");
246                 }
247                 spin_unlock(&imp->imp_lock);
248 //                LASSERT(atomic_read(&imp->imp_inflight) == 0);
249         }
250
251 out:
252         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
253         sptlrpc_import_flush_all_ctx(imp);
254
255         atomic_dec(&imp->imp_inval_count);
256         cfs_waitq_signal(&imp->imp_recovery_waitq);
257 }
258
259 /* unset imp_invalid */
260 void ptlrpc_activate_import(struct obd_import *imp)
261 {
262         struct obd_device *obd = imp->imp_obd;
263
264         spin_lock(&imp->imp_lock);
265         imp->imp_invalid = 0;
266         ptlrpc_activate_timeouts(imp);
267         spin_unlock(&imp->imp_lock);
268         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
269 }
270
271 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
272 {
273         ENTRY;
274
275         LASSERT(!imp->imp_dlm_fake);
276
277         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
278                 if (!imp->imp_replayable) {
279                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
280                                "auto-deactivating\n",
281                                obd2cli_tgt(imp->imp_obd),
282                                imp->imp_connection->c_remote_uuid.uuid,
283                                imp->imp_obd->obd_name);
284                         ptlrpc_deactivate_import(imp);
285                 }
286
287                 CDEBUG(D_HA, "%s: waking up pinger\n",
288                        obd2cli_tgt(imp->imp_obd));
289
290                 spin_lock(&imp->imp_lock);
291                 imp->imp_force_verify = 1;
292                 spin_unlock(&imp->imp_lock);
293
294                 ptlrpc_pinger_wake_up();
295         }
296         EXIT;
297 }
298
299 int ptlrpc_reconnect_import(struct obd_import *imp)
300 {
301         
302         ptlrpc_set_import_discon(imp, 0); 
303         /* Force a new connect attempt */
304         ptlrpc_invalidate_import(imp);
305         /* Do a fresh connect next time by zeroing the handle */
306         ptlrpc_disconnect_import(imp, 1);
307         /* Wait for all invalidate calls to finish */
308         if (atomic_read(&imp->imp_inval_count) > 0) {
309                 int rc;
310                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
311                 rc = l_wait_event(imp->imp_recovery_waitq,
312                                   (atomic_read(&imp->imp_inval_count) == 0),
313                                   &lwi);
314                 if (rc)
315                         CERROR("Interrupted, inval=%d\n", 
316                                atomic_read(&imp->imp_inval_count));
317         }
318
319         /* Allow reconnect attempts */
320         imp->imp_obd->obd_no_recov = 0;
321         /* Remove 'invalid' flag */
322         ptlrpc_activate_import(imp);
323         /* Attempt a new connect */
324         ptlrpc_recover_import(imp, NULL);
325         return 0;
326 }
327
328 EXPORT_SYMBOL(ptlrpc_reconnect_import);
329
330 static int import_select_connection(struct obd_import *imp)
331 {
332         struct obd_import_conn *imp_conn = NULL, *conn;
333         struct obd_export *dlmexp;
334         ENTRY;
335
336         spin_lock(&imp->imp_lock);
337
338         if (list_empty(&imp->imp_conn_list)) {
339                 CERROR("%s: no connections available\n",
340                         imp->imp_obd->obd_name);
341                 spin_unlock(&imp->imp_lock);
342                 RETURN(-EINVAL);
343         }
344
345         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
346                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
347                        imp->imp_obd->obd_name,
348                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
349                        conn->oic_last_attempt);
350                 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
351                 if (cfs_time_before_64(conn->oic_last_attempt + 
352                                        RECONNECT_INTERVAL * HZ,
353                                        cfs_time_current_64())) {
354                         /* If we have never tried this connection since the
355                            the last successful attempt, go with this one */
356                         if (cfs_time_beforeq_64(conn->oic_last_attempt,
357                                                imp->imp_last_success_conn)) {
358                                 imp_conn = conn;
359                                 break;
360                         }
361
362                         /* Both of these connections have already been tried
363                            since the last successful connection; just choose the
364                            least recently used */
365                         if (!imp_conn)
366                                 imp_conn = conn;
367                         else if (cfs_time_before_64(conn->oic_last_attempt,
368                                                     imp_conn->oic_last_attempt))
369                                 imp_conn = conn;
370                 }
371         }
372
373         /* if not found, simply choose the current one */
374         if (!imp_conn) {
375                 LASSERT(imp->imp_conn_current);
376                 imp_conn = imp->imp_conn_current;
377         }
378         LASSERT(imp_conn->oic_conn);
379
380         imp_conn->oic_last_attempt = cfs_time_current_64();
381
382         /* switch connection, don't mind if it's same as the current one */
383         if (imp->imp_connection)
384                 ptlrpc_put_connection(imp->imp_connection);
385         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
386
387         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
388         LASSERT(dlmexp != NULL);
389         if (dlmexp->exp_connection)
390                 ptlrpc_put_connection(dlmexp->exp_connection);
391         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
392         class_export_put(dlmexp);
393
394         if (imp->imp_conn_current != imp_conn) {
395                 if (imp->imp_conn_current)
396                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
397                                       imp->imp_obd->obd_name,
398                                       imp_conn->oic_uuid.uuid,
399                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
400                 imp->imp_conn_current = imp_conn;
401         }
402
403         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
404                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
405                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
406
407         spin_unlock(&imp->imp_lock);
408
409         RETURN(0);
410 }
411
412 /*
413  * must be called under imp_lock
414  */
415 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
416 {
417         struct ptlrpc_request *req;
418         struct list_head *tmp;
419
420         if (list_empty(&imp->imp_replay_list))
421                 return 0;
422         tmp = imp->imp_replay_list.next;
423         req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
424         *transno = req->rq_transno;
425         if (req->rq_transno == 0) {
426                 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
427                 LBUG();
428         }
429
430         return 1;
431 }
432
433 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
434 {
435         struct obd_device *obd = imp->imp_obd;
436         int initial_connect = 0;
437         int set_transno = 0;
438         __u64 committed_before_reconnect = 0;
439         struct ptlrpc_request *request;
440         char *bufs[] = { NULL,
441                          obd2cli_tgt(imp->imp_obd),
442                          obd->obd_uuid.uuid,
443                          (char *)&imp->imp_dlm_handle,
444                          (char *)&imp->imp_connect_data };
445         struct ptlrpc_connect_async_args *aa;
446         int rc;
447         ENTRY;
448
449         spin_lock(&imp->imp_lock);
450         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
451                 spin_unlock(&imp->imp_lock);
452                 CERROR("can't connect to a closed import\n");
453                 RETURN(-EINVAL);
454         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
455                 spin_unlock(&imp->imp_lock);
456                 CERROR("already connected\n");
457                 RETURN(0);
458         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
459                 spin_unlock(&imp->imp_lock);
460                 CERROR("already connecting\n");
461                 RETURN(-EALREADY);
462         }
463
464         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
465
466         imp->imp_conn_cnt++;
467         imp->imp_resend_replay = 0;
468
469         if (!lustre_handle_is_used(&imp->imp_remote_handle))
470                 initial_connect = 1;
471         else
472                 committed_before_reconnect = imp->imp_peer_committed_transno;
473
474         set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
475         spin_unlock(&imp->imp_lock);
476
477         if (new_uuid) {
478                 struct obd_uuid uuid;
479
480                 obd_str2uuid(&uuid, new_uuid);
481                 rc = import_set_conn_priority(imp, &uuid);
482                 if (rc)
483                         GOTO(out, rc);
484         }
485
486         rc = import_select_connection(imp);
487         if (rc)
488                 GOTO(out, rc);
489
490         /* last in connection list */
491         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
492                 if (imp->imp_initial_recov_bk && initial_connect) {
493                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
494                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
495                         /* Don't retry if connect fails */
496                         rc = 0;
497                         obd_set_info_async(obd->obd_self_export,
498                                            strlen(KEY_INIT_RECOV),
499                                            KEY_INIT_RECOV,
500                                            sizeof(rc), &rc, NULL);
501                 }
502                 if (imp->imp_recon_bk) {
503                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
504                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
505                         spin_lock(&imp->imp_lock);
506                         imp->imp_last_recon = 1;
507                         spin_unlock(&imp->imp_lock);
508                 }
509         }
510
511         rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
512         if (rc)
513                 GOTO(out, rc);
514
515         /* Reset connect flags to the originally requested flags, in case
516          * the server is updated on-the-fly we will get the new features. */
517         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
518         rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
519                            &obd->obd_uuid, &imp->imp_connect_data);
520         if (rc)
521                 GOTO(out, rc);
522
523         request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
524         if (request == NULL)
525                 GOTO(out, rc = -ENOMEM);
526
527         rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
528                                       imp->imp_connect_op, bufs, NULL);
529         if (rc) {
530                 ptlrpc_request_free(request);
531                 GOTO(out, rc);
532         }
533
534 #ifndef __KERNEL__
535         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
536 #endif
537         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
538
539         request->rq_send_state = LUSTRE_IMP_CONNECTING;
540         /* Allow a slightly larger reply for future growth compatibility */
541         req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
542                              sizeof(struct obd_connect_data)+16*sizeof(__u64));
543         ptlrpc_request_set_replen(request);
544         request->rq_interpret_reply = ptlrpc_connect_interpret;
545
546         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
547         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
548         memset(aa, 0, sizeof *aa);
549
550         aa->pcaa_peer_committed = committed_before_reconnect;
551         aa->pcaa_initial_connect = initial_connect;
552
553         if (aa->pcaa_initial_connect) {
554                 spin_lock(&imp->imp_lock);
555                 imp->imp_replayable = 1;
556                 spin_unlock(&imp->imp_lock);
557                 /* On an initial connect, we don't know which one of a
558                    failover server pair is up.  Don't wait long. */
559 #ifdef CRAY_XT3
560                 request->rq_timeout = max((int)(obd_timeout / 2), 5);
561 #else
562                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
563 #endif
564                 lustre_msg_add_op_flags(request->rq_reqmsg, 
565                                         MSG_CONNECT_INITIAL);
566         }
567
568         if (set_transno)
569                 lustre_msg_add_op_flags(request->rq_reqmsg, 
570                                         MSG_CONNECT_TRANSNO);
571
572         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
573         ptlrpcd_add_req(request);
574         rc = 0;
575 out:
576         if (rc != 0) {
577                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
578         }
579
580         RETURN(rc);
581 }
582 EXPORT_SYMBOL(ptlrpc_connect_import);
583
584 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
585 {
586 #ifdef __KERNEL__
587         struct obd_import_conn *imp_conn;
588 #endif
589         int wake_pinger = 0;
590
591         ENTRY;
592
593         spin_lock(&imp->imp_lock);
594         if (list_empty(&imp->imp_conn_list))
595                 GOTO(unlock, 0);
596
597 #ifdef __KERNEL__
598         imp_conn = list_entry(imp->imp_conn_list.prev,
599                               struct obd_import_conn,
600                               oic_item);
601
602         /* XXX: When the failover node is the primary node, it is possible
603          * to have two identical connections in imp_conn_list. We must 
604          * compare not conn's pointers but NIDs, otherwise we can defeat
605          * connection throttling. (See bug 14774.) */
606         if (imp->imp_conn_current->oic_conn->c_self != 
607                                 imp_conn->oic_conn->c_self) {
608                 ptlrpc_ping_import_soon(imp);
609                 wake_pinger = 1;
610         }
611 #else
612         /* liblustre has no pinger thead, so we wakup pinger anyway */
613         wake_pinger = 1;
614 #endif 
615
616  unlock:
617         spin_unlock(&imp->imp_lock);
618
619         if (wake_pinger)
620                 ptlrpc_pinger_wake_up();
621
622         EXIT;
623 }
624
625 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
626                                     void * data, int rc)
627 {
628         struct ptlrpc_connect_async_args *aa = data;
629         struct obd_import *imp = request->rq_import;
630         struct client_obd *cli = &imp->imp_obd->u.cli;
631         struct lustre_handle old_hdl;
632         int msg_flags;
633         ENTRY;
634
635         spin_lock(&imp->imp_lock);
636         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
637                 spin_unlock(&imp->imp_lock);
638                 RETURN(0);
639         }
640         spin_unlock(&imp->imp_lock);
641
642         if (rc)
643                 GOTO(out, rc);
644
645         LASSERT(imp->imp_conn_current);
646
647         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
648
649         /* All imports are pingable */
650         spin_lock(&imp->imp_lock);
651         imp->imp_pingable = 1;
652
653         if (aa->pcaa_initial_connect) {
654                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
655                         imp->imp_replayable = 1;
656                         spin_unlock(&imp->imp_lock);
657                         CDEBUG(D_HA, "connected to replayable target: %s\n",
658                                obd2cli_tgt(imp->imp_obd));
659                 } else {
660                         imp->imp_replayable = 0;
661                         spin_unlock(&imp->imp_lock);
662                 }
663
664                 /* if applies, adjust the imp->imp_msg_magic here
665                  * according to reply flags */
666
667                 imp->imp_remote_handle =
668                                 *lustre_msg_get_handle(request->rq_repmsg);
669
670                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
671                 spin_lock(&imp->imp_lock);
672                 if (imp->imp_invalid) {
673                         spin_unlock(&imp->imp_lock);
674                         ptlrpc_activate_import(imp);
675                 } else {
676                         spin_unlock(&imp->imp_lock);
677                 }
678
679                 GOTO(finish, rc = 0);
680         } else {
681                 spin_unlock(&imp->imp_lock);
682         }
683
684         /* Determine what recovery state to move the import to. */
685         if (MSG_CONNECT_RECONNECT & msg_flags) {
686                 memset(&old_hdl, 0, sizeof(old_hdl));
687                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
688                             sizeof (old_hdl))) {
689                         CERROR("%s@%s didn't like our handle "LPX64
690                                ", failed\n", obd2cli_tgt(imp->imp_obd),
691                                imp->imp_connection->c_remote_uuid.uuid,
692                                imp->imp_dlm_handle.cookie);
693                         GOTO(out, rc = -ENOTCONN);
694                 }
695
696                 if (memcmp(&imp->imp_remote_handle,
697                            lustre_msg_get_handle(request->rq_repmsg),
698                            sizeof(imp->imp_remote_handle))) {
699
700                         CWARN("%s@%s changed server handle from "
701                                LPX64" to "LPX64" - evicting.\n",
702                                obd2cli_tgt(imp->imp_obd),
703                                imp->imp_connection->c_remote_uuid.uuid,
704                                imp->imp_remote_handle.cookie,
705                                lustre_msg_get_handle(request->rq_repmsg)->
706                                          cookie);
707                         imp->imp_remote_handle =
708                                      *lustre_msg_get_handle(request->rq_repmsg);
709
710                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
711                         GOTO(finish, rc = 0);
712                 } else {
713                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
714                                obd2cli_tgt(imp->imp_obd),
715                                imp->imp_connection->c_remote_uuid.uuid);
716                 }
717
718                 if (imp->imp_invalid) {
719                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
720                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
721                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
722                                imp->imp_obd->obd_name,
723                                obd2cli_tgt(imp->imp_obd));
724
725                         spin_lock(&imp->imp_lock);
726                         imp->imp_resend_replay = 1;
727                         spin_unlock(&imp->imp_lock);
728
729                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
730                 } else {
731                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
732                 }
733         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
734                 LASSERT(imp->imp_replayable);
735                 imp->imp_remote_handle =
736                                 *lustre_msg_get_handle(request->rq_repmsg);
737                 imp->imp_last_replay_transno = 0;
738                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
739         } else {
740                 imp->imp_remote_handle =
741                                 *lustre_msg_get_handle(request->rq_repmsg);
742                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
743         }
744
745         /* Sanity checks for a reconnected import. */
746         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
747                 CERROR("imp_replayable flag does not match server "
748                        "after reconnect. We should LBUG right here.\n");
749         }
750
751         if (lustre_msg_get_last_committed(request->rq_repmsg) <
752             aa->pcaa_peer_committed) {
753                 CERROR("%s went back in time (transno "LPD64
754                        " was previously committed, server now claims "LPD64
755                        ")!  See https://bugzilla.clusterfs.com/"
756                        "long_list.cgi?buglist=9646\n",
757                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
758                        lustre_msg_get_last_committed(request->rq_repmsg));
759         }
760
761 finish:
762         rc = ptlrpc_import_recovery_state_machine(imp);
763         if (rc != 0) {
764                 if (rc == -ENOTCONN) {
765                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
766                                "invalidating and reconnecting\n",
767                                obd2cli_tgt(imp->imp_obd),
768                                imp->imp_connection->c_remote_uuid.uuid);
769                         ptlrpc_connect_import(imp, NULL);
770                         RETURN(0);
771                 }
772         } else {
773                 struct obd_connect_data *ocd;
774                 struct obd_export *exp;
775                 int ret;
776                 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
777                                            RCL_SERVER);
778                 /* server replied obd_connect_data is always bigger */
779                 ocd = req_capsule_server_sized_get(&request->rq_pill,
780                                                    &RMF_CONNECT_DATA, ret);
781
782                 spin_lock(&imp->imp_lock);
783                 list_del(&imp->imp_conn_current->oic_item);
784                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
785                 imp->imp_last_success_conn =
786                         imp->imp_conn_current->oic_last_attempt;
787
788                 if (ocd == NULL) {
789                         spin_unlock(&imp->imp_lock);
790                         CERROR("Wrong connect data from server\n");
791                         rc = -EPROTO;
792                         GOTO(out, rc);
793                 }
794
795                 imp->imp_connect_data = *ocd;
796
797                 exp = class_conn2export(&imp->imp_dlm_handle);
798                 spin_unlock(&imp->imp_lock);
799
800                 /* check that server granted subset of flags we asked for. */
801                 LASSERTF((ocd->ocd_connect_flags &
802                           imp->imp_connect_flags_orig) ==
803                          ocd->ocd_connect_flags, LPX64" != "LPX64,
804                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
805
806                 if (!exp) {
807                         /* This could happen if export is cleaned during the 
808                            connect attempt */
809                         CERROR("Missing export for %s\n", 
810                                imp->imp_obd->obd_name);
811                         GOTO(out, rc = -ENODEV);
812                 }
813                 exp->exp_connect_flags = ocd->ocd_connect_flags;
814                 imp->imp_obd->obd_self_export->exp_connect_flags = 
815                                                         ocd->ocd_connect_flags;
816                 class_export_put(exp);
817
818                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
819
820                 if (!ocd->ocd_ibits_known &&
821                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
822                         CERROR("Inodebits aware server returned zero compatible"
823                                " bits?\n");
824
825                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
826                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
827                                         LUSTRE_VERSION_OFFSET_WARN ||
828                      ocd->ocd_version < LUSTRE_VERSION_CODE -
829                                         LUSTRE_VERSION_OFFSET_WARN)) {
830                         /* Sigh, some compilers do not like #ifdef in the middle
831                            of macro arguments */
832 #ifdef __KERNEL__
833                         const char *older =
834                                 "older. Consider upgrading this client";
835 #else
836                         const char *older =
837                                 "older. Consider recompiling this application";
838 #endif
839                         const char *newer = "newer than client version";
840
841                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
842                                       "is much %s (%s)\n",
843                                       obd2cli_tgt(imp->imp_obd),
844                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
845                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
846                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
847                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
848                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
849                                       newer : older, LUSTRE_VERSION_STRING);
850                 }
851
852                 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
853                         /* We sent to the server ocd_cksum_types with bits set
854                          * for algorithms we understand. The server masked off
855                          * the checksum types it doesn't support */
856                         if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
857                                 LCONSOLE_WARN("The negotiation of the checksum "
858                                               "alogrithm to use with server %s "
859                                               "failed (%x/%x), disabling "
860                                               "checksums\n",
861                                               obd2cli_tgt(imp->imp_obd),
862                                               ocd->ocd_cksum_types,
863                                               OBD_CKSUM_ALL);
864                                 cli->cl_checksum = 0;
865                                 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
866                                 cli->cl_cksum_type = OBD_CKSUM_CRC32;
867                         } else {
868                                 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
869
870                                 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
871                                         cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
872                                 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
873                                         cli->cl_cksum_type = OBD_CKSUM_ADLER;
874                                 else
875                                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
876                         }
877                 } else {
878                         /* The server does not support OBD_CONNECT_CKSUM.
879                          * Enforce CRC32 for backward compatibility*/
880                         cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
881                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
882                 }
883
884                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
885                         cli->cl_max_pages_per_rpc = 
886                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
887                 }
888
889                 imp->imp_obd->obd_namespace->ns_connect_flags = 
890                                                         ocd->ocd_connect_flags;
891                 imp->imp_obd->obd_namespace->ns_orig_connect_flags = 
892                                                         ocd->ocd_connect_flags;
893
894                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
895                         (cli->cl_max_pages_per_rpc > 0));
896         }
897
898 out:
899         if (rc != 0) {
900                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
901                 spin_lock(&imp->imp_lock);
902                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
903                     (request->rq_import_generation == imp->imp_generation))
904                         ptlrpc_deactivate_and_unlock_import(imp);
905                 else
906                         spin_unlock(&imp->imp_lock);
907
908                 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
909                     (rc == -EACCES)) {
910                         /*
911                          * Give up trying to reconnect
912                          * EACCES means client has no permission for connection
913                          */
914                         imp->imp_obd->obd_no_recov = 1;
915                         ptlrpc_deactivate_import(imp);
916                 }
917
918                 if (rc == -EPROTO) {
919                         struct obd_connect_data *ocd;
920
921                         /* reply message might not be ready */
922                         if (request->rq_repmsg == NULL)
923                                 RETURN(-EPROTO);
924
925                         ocd = req_capsule_server_get(&request->rq_pill,
926                                                      &RMF_CONNECT_DATA);
927                         if (ocd &&
928                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
929                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
930                            /* Actually servers are only supposed to refuse
931                               connection from liblustre clients, so we should
932                               never see this from VFS context */
933                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
934                                         "(%d.%d.%d.%d)"
935                                         " refused connection from this client "
936                                         "with an incompatible version (%s).  "
937                                         "Client must be recompiled\n",
938                                         obd2cli_tgt(imp->imp_obd),
939                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
940                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
941                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
942                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
943                                         LUSTRE_VERSION_STRING);
944                                 ptlrpc_deactivate_import(imp);
945                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
946                         }
947                         RETURN(-EPROTO);
948                 }
949
950                 ptlrpc_maybe_ping_import_soon(imp);
951
952                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
953                        obd2cli_tgt(imp->imp_obd),
954                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
955         }
956         
957         spin_lock(&imp->imp_lock);
958         imp->imp_last_recon = 0;
959         spin_unlock(&imp->imp_lock);
960
961         cfs_waitq_signal(&imp->imp_recovery_waitq);
962         RETURN(rc);
963 }
964
965 static int completed_replay_interpret(struct ptlrpc_request *req,
966                                     void * data, int rc)
967 {
968         ENTRY;
969         atomic_dec(&req->rq_import->imp_replay_inflight);
970         if (req->rq_status == 0) {
971                 ptlrpc_import_recovery_state_machine(req->rq_import);
972         } else {
973                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
974                        "reconnecting\n",
975                        req->rq_import->imp_obd->obd_name, req->rq_status);
976                 ptlrpc_connect_import(req->rq_import, NULL);
977         }
978
979         RETURN(0);
980 }
981
982 static int signal_completed_replay(struct obd_import *imp)
983 {
984         struct ptlrpc_request *req;
985         ENTRY;
986
987         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
988         atomic_inc(&imp->imp_replay_inflight);
989
990         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
991                                         OBD_PING);
992         if (req == NULL) {
993                 atomic_dec(&imp->imp_replay_inflight);
994                 RETURN(-ENOMEM);
995         }
996
997         ptlrpc_request_set_replen(req);
998         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
999         lustre_msg_add_flags(req->rq_reqmsg, 
1000                              MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1001         req->rq_timeout *= 3;
1002         req->rq_interpret_reply = completed_replay_interpret;
1003
1004         ptlrpcd_add_req(req);
1005         RETURN(0);
1006 }
1007
1008 #ifdef __KERNEL__
1009 static int ptlrpc_invalidate_import_thread(void *data)
1010 {
1011         struct obd_import *imp = data;
1012
1013         ENTRY;
1014
1015         ptlrpc_daemonize("ll_imp_inval");
1016         
1017         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1018                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1019                imp->imp_connection->c_remote_uuid.uuid);
1020
1021         ptlrpc_invalidate_import(imp);
1022
1023         if (obd_dump_on_eviction) {
1024                 CERROR("dump the log upon eviction\n");
1025                 libcfs_debug_dumplog();
1026         }
1027
1028         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1029         ptlrpc_import_recovery_state_machine(imp);
1030
1031         RETURN(0);
1032 }
1033 #endif
1034
1035 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1036 {
1037         int rc = 0;
1038         int inflight;
1039         char *target_start;
1040         int target_len;
1041
1042         ENTRY;
1043         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1044                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1045                           &target_start, &target_len);
1046                 /* Don't care about MGC eviction */
1047                 if (strcmp(imp->imp_obd->obd_type->typ_name,
1048                            LUSTRE_MGC_NAME) != 0) {
1049                         LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1050                                            "%.*s; in progress operations using "
1051                                            "this service will fail.\n",
1052                                            target_len, target_start);
1053                 }
1054                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1055                        obd2cli_tgt(imp->imp_obd),
1056                        imp->imp_connection->c_remote_uuid.uuid);
1057
1058 #ifdef __KERNEL__
1059                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1060                                        CLONE_VM | CLONE_FILES);
1061                 if (rc < 0)
1062                         CERROR("error starting invalidate thread: %d\n", rc);
1063                 else
1064                         rc = 0;
1065                 RETURN(rc);
1066 #else
1067                 ptlrpc_invalidate_import(imp);
1068
1069                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1070 #endif
1071         }
1072
1073         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1074                 CDEBUG(D_HA, "replay requested by %s\n",
1075                        obd2cli_tgt(imp->imp_obd));
1076                 rc = ptlrpc_replay_next(imp, &inflight);
1077                 if (inflight == 0 &&
1078                     atomic_read(&imp->imp_replay_inflight) == 0) {
1079                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1080                         rc = ldlm_replay_locks(imp);
1081                         if (rc)
1082                                 GOTO(out, rc);
1083                 }
1084                 rc = 0;
1085         }
1086
1087         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1088                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1089                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1090                         rc = signal_completed_replay(imp);
1091                         if (rc)
1092                                 GOTO(out, rc);
1093                 }
1094
1095         }
1096
1097         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1098                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1099                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1100                 }
1101         }
1102
1103         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1104                 CDEBUG(D_HA, "reconnected to %s@%s\n",
1105                        obd2cli_tgt(imp->imp_obd),
1106                        imp->imp_connection->c_remote_uuid.uuid);
1107
1108                 rc = ptlrpc_resend(imp);
1109                 if (rc)
1110                         GOTO(out, rc);
1111                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1112                 ptlrpc_activate_import(imp);
1113
1114                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1115                           &target_start, &target_len);
1116                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1117                               "using nid %s.\n", imp->imp_obd->obd_name,
1118                               target_len, target_start,
1119                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
1120         }
1121
1122         if (imp->imp_state == LUSTRE_IMP_FULL) {
1123                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1124                 ptlrpc_wake_delayed(imp);
1125         }
1126
1127 out:
1128         RETURN(rc);
1129 }
1130
1131 static int back_to_sleep(void *unused)
1132 {
1133         return 0;
1134 }
1135
1136 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1137 {
1138         struct ptlrpc_request *req;
1139         int rq_opc, rc = 0;
1140         int nowait = imp->imp_obd->obd_force;
1141         ENTRY;
1142
1143         if (nowait)
1144                 GOTO(set_state, rc);
1145
1146         switch (imp->imp_connect_op) {
1147         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1148         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1149         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1150         default:
1151                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1152                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1153                 RETURN(-EINVAL);
1154         }
1155
1156         if (ptlrpc_import_in_recovery(imp)) {
1157                 struct l_wait_info lwi;
1158                 cfs_duration_t timeout;
1159                 if (imp->imp_server_timeout)
1160                         timeout = cfs_time_seconds(obd_timeout / 2);
1161                 else
1162                         timeout = cfs_time_seconds(obd_timeout);
1163                 
1164                 timeout = MAX(timeout * HZ, 1);
1165                 
1166                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
1167                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1168                 rc = l_wait_event(imp->imp_recovery_waitq,
1169                                   !ptlrpc_import_in_recovery(imp), &lwi);
1170
1171         }
1172
1173         spin_lock(&imp->imp_lock);
1174         if (imp->imp_state != LUSTRE_IMP_FULL)
1175                 GOTO(out, 0);
1176
1177         spin_unlock(&imp->imp_lock);
1178
1179         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1180                                         LUSTRE_OBD_VERSION, rq_opc);
1181         if (req) {
1182                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1183                  * it fails.  We can get through the above with a down server
1184                  * if the client doesn't know the server is gone yet. */
1185                 req->rq_no_resend = 1;
1186 #ifdef CRAY_XT3
1187                 req->rq_timeout = obd_timeout / 3;
1188 #else
1189                 req->rq_timeout = 5;
1190 #endif
1191                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1192                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1193                 ptlrpc_request_set_replen(req);
1194                 rc = ptlrpc_queue_wait(req);
1195                 ptlrpc_req_finished(req);
1196         }
1197
1198 set_state:
1199         spin_lock(&imp->imp_lock);
1200 out:
1201         if (noclose) 
1202                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1203         else
1204                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1205         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1206         imp->imp_conn_cnt = 0;
1207         imp->imp_last_recon = 0;
1208         spin_unlock(&imp->imp_lock);
1209
1210         RETURN(rc);
1211 }
1212