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