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