Whamcloud - gitweb
b=4019
[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 Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24 #ifdef __KERNEL__
25 # include <linux/config.h>
26 # include <linux/module.h>
27 # include <linux/kmod.h>
28 #else
29 # include <liblustre.h>
30 #endif
31
32 #include <linux/obd_support.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_net.h>
35 #include <linux/lustre_import.h>
36 #include <linux/lustre_export.h>
37 #include <linux/obd.h>
38 #include <linux/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, imp->imp_target_uuid.uuid,                          \
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         unsigned long flags;                            \
62                                                         \
63         spin_lock_irqsave(&imp->imp_lock, flags);       \
64         IMPORT_SET_STATE_NOLOCK(imp, state);            \
65         spin_unlock_irqrestore(&imp->imp_lock, flags);  \
66 } while(0)
67
68
69 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
70                                     void * data, int rc);
71 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
72
73 /* Only this function is allowed to change the import state when it is
74  * CLOSED. I would rather refcount the import and free it after
75  * disconnection like we do with exports. To do that, the client_obd
76  * will need to save the peer info somewhere other than in the import,
77  * though. */
78 int ptlrpc_init_import(struct obd_import *imp)
79 {
80         unsigned long flags;
81
82         spin_lock_irqsave(&imp->imp_lock, flags);
83
84         imp->imp_generation++;
85         imp->imp_state =  LUSTRE_IMP_NEW;
86
87         spin_unlock_irqrestore(&imp->imp_lock, flags);
88
89         return 0;
90 }
91
92 /* Returns true if import was FULL, false if import was already not
93  * connected.
94  */
95 int ptlrpc_set_import_discon(struct obd_import *imp)
96 {
97         unsigned long flags;
98         int rc = 0;
99
100         spin_lock_irqsave(&imp->imp_lock, flags);
101
102         if (imp->imp_state == LUSTRE_IMP_FULL) {
103                 CERROR("%s: connection lost to %s@%s\n",
104                        imp->imp_obd->obd_name, 
105                        imp->imp_target_uuid.uuid,
106                        imp->imp_connection->c_remote_uuid.uuid);
107                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
108                 spin_unlock_irqrestore(&imp->imp_lock, flags);
109                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
110                 rc = 1;
111         } else {
112                 spin_unlock_irqrestore(&imp->imp_lock, flags);
113                 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
114                        imp,imp->imp_client->cli_name,
115                        ptlrpc_import_state_name(imp->imp_state));
116         }
117
118         return rc;
119 }
120
121 /*
122  * This acts as a barrier; all existing requests are rejected, and
123  * no new requests will be accepted until the import is valid again.
124  */
125 void ptlrpc_deactivate_import(struct obd_import *imp)
126 {
127         unsigned long flags;
128         ENTRY;
129
130         spin_lock_irqsave(&imp->imp_lock, flags);
131         CDEBUG(D_HA, "setting import %s INVALID\n",
132                imp->imp_target_uuid.uuid);
133         imp->imp_invalid = 1;
134         imp->imp_generation++;
135         spin_unlock_irqrestore(&imp->imp_lock, flags);
136
137         ptlrpc_abort_inflight(imp);
138         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
139 }
140
141 /*
142  * This function will invalidate the import, if necessary, then block
143  * for all the RPC completions, and finally notify the obd to
144  * invalidate its state (ie cancel locks, clear pending requests,
145  * etc).
146  *
147  * in_rpc: true if this is called while processing an rpc, like
148  *    CONNECT. It will allow for one RPC to be inflight while
149  *    waiting for requests to complete. Ugly, yes, but I don't see an
150  *    cleaner way right now.
151  */
152 void ptlrpc_invalidate_import(struct obd_import *imp, int in_rpc)
153 {
154         struct l_wait_info lwi;
155         unsigned long timeout;
156         int inflight = 0;
157         int rc;
158
159         if (!imp->imp_invalid)
160                 ptlrpc_deactivate_import(imp);
161
162         LASSERT(imp->imp_invalid);
163
164         if (in_rpc)
165                 inflight = 1;
166
167         /* wait for all requests to error out and call completion 
168            callbacks */
169         if (imp->imp_server_timeout)
170                 timeout = obd_timeout / 2;
171         else
172                 timeout = obd_timeout;
173         timeout = MAX(timeout * HZ, 1);
174         lwi = LWI_TIMEOUT_INTR(timeout, NULL, NULL, NULL);
175         rc = l_wait_event(imp->imp_recovery_waitq, 
176                           (atomic_read(&imp->imp_inflight) == inflight), 
177                           &lwi);
178
179         if (rc)
180                 CERROR("%s: rc = %d waiting for callback (%d != %d)\n",
181                        imp->imp_target_uuid.uuid, rc,
182                        atomic_read(&imp->imp_inflight), inflight);
183
184         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
185 }
186
187 void ptlrpc_activate_import(struct obd_import *imp)
188 {
189         struct obd_device *obd = imp->imp_obd;
190         unsigned long flags;
191
192         spin_lock_irqsave(&imp->imp_lock, flags);
193         imp->imp_invalid = 0;
194         spin_unlock_irqrestore(&imp->imp_lock, flags);
195
196         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
197 }
198
199 void ptlrpc_fail_import(struct obd_import *imp, int generation)
200 {
201         ENTRY;
202
203         LASSERT (!imp->imp_dlm_fake);
204
205         if (ptlrpc_set_import_discon(imp)) {
206                 unsigned long flags;
207
208                 if (!imp->imp_replayable) {
209                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
210                                "auto-deactivating\n",
211                                imp->imp_target_uuid.uuid,
212                                imp->imp_connection->c_remote_uuid.uuid,
213                                imp->imp_obd->obd_name);
214                         ptlrpc_deactivate_import(imp);
215                 }
216
217                 CDEBUG(D_HA, "%s: waking up pinger\n",
218                        imp->imp_target_uuid.uuid);
219
220                 spin_lock_irqsave(&imp->imp_lock, flags);
221                 imp->imp_force_verify = 1;
222                 spin_unlock_irqrestore(&imp->imp_lock, flags);
223
224                 ptlrpc_pinger_wake_up();
225         }
226         EXIT;
227 }
228
229 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
230 {
231         struct obd_device *obd = imp->imp_obd;
232         int initial_connect = 0;
233         int rc;
234         __u64 committed_before_reconnect = 0;
235         struct ptlrpc_request *request;
236         int size[] = {sizeof(imp->imp_target_uuid),
237                                  sizeof(obd->obd_uuid),
238                                  sizeof(imp->imp_dlm_handle),
239                                  sizeof(unsigned long)};
240         char *tmp[] = {imp->imp_target_uuid.uuid,
241                        obd->obd_uuid.uuid,
242                        (char *)&imp->imp_dlm_handle,
243                        (char *)&imp->imp_connect_flags}; /* XXX: make this portable! */
244         struct ptlrpc_connect_async_args *aa;
245         unsigned long flags;
246
247         spin_lock_irqsave(&imp->imp_lock, flags);
248         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
249                 spin_unlock_irqrestore(&imp->imp_lock, flags);
250                 CERROR("can't connect to a closed import\n");
251                 RETURN(-EINVAL);
252         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
253                 spin_unlock_irqrestore(&imp->imp_lock, flags);
254                 CERROR("already connected\n");
255                 RETURN(0);
256         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
257                 spin_unlock_irqrestore(&imp->imp_lock, flags);
258                 CERROR("already connecting\n");
259                 RETURN(-EALREADY);
260         }
261
262         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
263
264         imp->imp_resend_replay = 0;
265
266         if (imp->imp_remote_handle.cookie == 0) {
267                 initial_connect = 1;
268         } else {
269                 committed_before_reconnect = imp->imp_peer_committed_transno;;
270                 imp->imp_conn_cnt++;
271         }
272
273
274         spin_unlock_irqrestore(&imp->imp_lock, flags);
275
276         if (new_uuid) {
277                 struct ptlrpc_connection *conn;
278                 struct obd_uuid uuid;
279                 struct obd_export *dlmexp;
280
281                 obd_str2uuid(&uuid, new_uuid);
282
283                 conn = ptlrpc_uuid_to_connection(&uuid);
284                 if (!conn)
285                         GOTO(out, rc = -ENOENT);
286
287                 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
288                        imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
289                        imp->imp_connection->c_remote_uuid.uuid,
290                        conn->c_remote_uuid.uuid);
291
292                 /* Switch the import's connection and the DLM export's
293                  * connection (which are almost certainly the same, but we
294                  * keep distinct refs just to make things clearer. I think. */
295                 if (imp->imp_connection)
296                         ptlrpc_put_connection(imp->imp_connection);
297                 /* We hand off the ref from ptlrpc_get_connection. */
298                 imp->imp_connection = conn;
299
300                 dlmexp = class_conn2export(&imp->imp_dlm_handle);
301
302                 LASSERT(dlmexp != NULL);
303
304                 if (dlmexp->exp_connection)
305                         ptlrpc_put_connection(dlmexp->exp_connection);
306                 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
307                 class_export_put(dlmexp);
308
309         }
310
311         request = ptlrpc_prep_req(imp, imp->imp_connect_op, 4, size, tmp);
312         if (!request)
313                 GOTO(out, rc = -ENOMEM);
314
315 #ifndef __KERNEL__
316         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
317 #endif
318
319         request->rq_send_state = LUSTRE_IMP_CONNECTING;
320         request->rq_replen = lustre_msg_size(0, NULL);
321         request->rq_interpret_reply = ptlrpc_connect_interpret;
322
323         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
324         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
325         memset(aa, 0, sizeof *aa);
326
327         aa->pcaa_peer_committed = committed_before_reconnect;
328         aa->pcaa_initial_connect = initial_connect;
329
330         if (aa->pcaa_initial_connect) {
331                 lustre_msg_add_op_flags(request->rq_reqmsg, 
332                                         MSG_CONNECT_INITIAL);
333                 imp->imp_replayable = 1; 
334         }
335
336         ptlrpcd_add_req(request);
337         rc = 0;
338         imp->imp_connect_start = jiffies;
339 out:
340         if (rc != 0) {
341                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
342         }
343
344         RETURN(rc);
345 }
346
347 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
348                                     void * data, int rc)
349 {
350         struct ptlrpc_connect_async_args *aa = data;
351         struct obd_import *imp = request->rq_import;
352         struct lustre_handle old_hdl;
353         unsigned long flags;
354         int msg_flags;
355         ENTRY;
356
357         spin_lock_irqsave(&imp->imp_lock, flags);
358         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
359                 spin_unlock_irqrestore(&imp->imp_lock, flags);
360                 RETURN(0);
361         }
362         spin_unlock_irqrestore(&imp->imp_lock, flags);
363
364         if (rc)
365                 GOTO(out, rc);
366
367         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
368
369         if (aa->pcaa_initial_connect) {
370                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
371                         CDEBUG(D_HA, "connected to replayable target: %s\n",
372                                imp->imp_target_uuid.uuid);
373                         imp->imp_pingable = imp->imp_replayable = 1;
374                 } else {
375                         imp->imp_replayable = 0;
376                 }
377                 LASSERTF(imp->imp_conn_cnt < request->rq_repmsg->conn_cnt,
378                          "imp conn_cnt %d req conn_cnt %d", 
379                          imp->imp_conn_cnt, request->rq_repmsg->conn_cnt);
380                 imp->imp_conn_cnt = request->rq_repmsg->conn_cnt;
381                 imp->imp_remote_handle = request->rq_repmsg->handle;
382                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
383                 GOTO(finish, rc = 0);
384         }
385
386         /* Determine what recovery state to move the import to. */
387         if (MSG_CONNECT_RECONNECT & msg_flags) {
388                 memset(&old_hdl, 0, sizeof(old_hdl));
389                 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
390                             sizeof (old_hdl))) {
391                         CERROR("%s@%s didn't like our handle "LPX64
392                                ", failed\n", imp->imp_target_uuid.uuid,
393                                imp->imp_connection->c_remote_uuid.uuid,
394                                imp->imp_dlm_handle.cookie);
395                         GOTO(out, rc = -ENOTCONN);
396                 }
397
398                 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
399                            sizeof(imp->imp_remote_handle))) {
400                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
401                                "; copying, but this may foreshadow disaster\n",
402                                imp->imp_target_uuid.uuid,
403                                imp->imp_connection->c_remote_uuid.uuid,
404                                imp->imp_remote_handle.cookie,
405                                request->rq_repmsg->handle.cookie);
406                         imp->imp_remote_handle = request->rq_repmsg->handle;
407                 } else {
408                         CERROR("reconnected to %s@%s after partition\n",
409                                imp->imp_target_uuid.uuid,
410                                imp->imp_connection->c_remote_uuid.uuid);
411                 }
412
413                 if (imp->imp_invalid) {
414                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
415                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
416                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
417                                imp->imp_obd->obd_name, 
418                                imp->imp_target_uuid.uuid);
419                         imp->imp_resend_replay = 1;
420                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
421                 } else {
422                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
423                 }
424         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
425                 LASSERT(imp->imp_replayable);
426                 imp->imp_remote_handle = request->rq_repmsg->handle;
427                 imp->imp_last_replay_transno = 0;
428                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
429         } else {
430                 CWARN("oops! we get evicted from %s\n", imp->imp_target_uuid.uuid);
431                 imp->imp_remote_handle = request->rq_repmsg->handle;
432                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
433         }
434
435         /* Sanity checks for a reconnected import. */
436         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
437                 CERROR("imp_replayable flag does not match server "
438                        "after reconnect. We should LBUG right here.\n");
439         }
440
441         if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
442                 CERROR("%s went back in time (transno "LPD64
443                        " was previously committed, server now claims "LPD64
444                        ")! is shared storage not coherent?\n",
445                        imp->imp_target_uuid.uuid,
446                        aa->pcaa_peer_committed,
447                        request->rq_repmsg->last_committed);
448         }
449
450 finish:
451         rc = ptlrpc_import_recovery_state_machine(imp);
452         if (rc != 0) {
453                 if (rc == -ENOTCONN) {
454                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
455                                "invalidating and reconnecting\n",
456                                imp->imp_target_uuid.uuid,
457                                imp->imp_connection->c_remote_uuid.uuid);
458                         ptlrpc_connect_import(imp, NULL);
459                         RETURN(0);
460                 }
461         }
462  out:
463         if (rc != 0) {
464                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
465                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
466                         ptlrpc_deactivate_import(imp);
467                 }
468                 if (rc == -ETIMEDOUT && (jiffies - imp->imp_connect_start) > HZ) {
469                         CDEBUG(D_ERROR, "recovery of %s on %s failed (timeout)\n",
470                                imp->imp_target_uuid.uuid,
471                                (char *)imp->imp_connection->c_remote_uuid.uuid);
472                         ptlrpc_connect_import(imp, NULL);
473                         RETURN(0);
474                 }
475                 CDEBUG(D_ERROR, "recovery of %s on %s failed (%d)\n",
476                        imp->imp_target_uuid.uuid,
477                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
478         }
479
480         wake_up(&imp->imp_recovery_waitq);
481         RETURN(rc);
482 }
483
484 static int completed_replay_interpret(struct ptlrpc_request *req,
485                                     void * data, int rc)
486 {
487         atomic_dec(&req->rq_import->imp_replay_inflight);
488         if (req->rq_status == 0) {
489                 ptlrpc_import_recovery_state_machine(req->rq_import);
490         } else {
491                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
492                        "reconnecting\n", 
493                        req->rq_import->imp_obd->obd_name, req->rq_status);
494                 ptlrpc_connect_import(req->rq_import, NULL);
495         }
496
497         RETURN(0);
498 }
499
500 static int signal_completed_replay(struct obd_import *imp)
501  {
502         struct ptlrpc_request *req;
503         ENTRY;
504
505         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
506         atomic_inc(&imp->imp_replay_inflight);
507
508         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
509         if (!req)
510                 RETURN(-ENOMEM);
511
512         req->rq_replen = lustre_msg_size(0, NULL);
513         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
514         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
515         req->rq_timeout *= 3;
516         req->rq_interpret_reply = completed_replay_interpret;
517
518         ptlrpcd_add_req(req);
519         RETURN(0);
520 }
521
522 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
523 {
524         int rc = 0;
525         int inflight;
526
527         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
528                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
529                        imp->imp_target_uuid.uuid,
530                        imp->imp_connection->c_remote_uuid.uuid);
531
532                 ptlrpc_invalidate_import(imp, 1);
533
534                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
535         }
536
537         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
538                 CDEBUG(D_HA, "replay requested by %s\n",
539                        imp->imp_target_uuid.uuid);
540                 rc = ptlrpc_replay_next(imp, &inflight);
541                 if (inflight == 0 &&
542                     atomic_read(&imp->imp_replay_inflight) == 0) {
543                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
544                         rc = ldlm_replay_locks(imp);
545                         if (rc)
546                                 GOTO(out, rc);
547                 }
548                 rc = 0;
549         }
550
551         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
552                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
553                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
554                         rc = signal_completed_replay(imp);
555                         if (rc)
556                                 GOTO(out, rc);
557                 }
558
559         }
560
561         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
562                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
563                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
564                 }
565         }
566
567         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
568                 CDEBUG(D_HA, "reconnected to %s@%s\n",
569                        imp->imp_target_uuid.uuid,
570                        imp->imp_connection->c_remote_uuid.uuid);
571
572                 rc = ptlrpc_resend(imp);
573                 if (rc)
574                         GOTO(out, rc);
575                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
576                 ptlrpc_activate_import(imp);
577                 CERROR("%s: connection restored to %s@%s\n",
578                        imp->imp_obd->obd_name, 
579                        imp->imp_target_uuid.uuid,
580                        imp->imp_connection->c_remote_uuid.uuid);
581         }
582
583         if (imp->imp_state == LUSTRE_IMP_FULL) {
584                 wake_up(&imp->imp_recovery_waitq);
585                 ptlrpc_wake_delayed(imp);
586         }
587
588  out:
589         RETURN(rc);
590 }
591
592 static int back_to_sleep(void *unused)
593 {
594         return 0;
595 }
596
597 int ptlrpc_disconnect_import(struct obd_import *imp)
598 {
599         struct ptlrpc_request *request;
600         int rq_opc;
601         int rc = 0;
602         unsigned long flags;
603         ENTRY;
604
605         switch (imp->imp_connect_op) {
606         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
607         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
608         case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
609         default:
610                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
611                        imp->imp_target_uuid.uuid, imp->imp_connect_op);
612                 RETURN(-EINVAL);
613         }
614
615
616         if (ptlrpc_import_in_recovery(imp)) {
617                 struct l_wait_info lwi;
618                 unsigned long timeout;
619                 if (imp->imp_server_timeout)
620                         timeout = obd_timeout / 2;
621                 else
622                         timeout = obd_timeout;
623                 timeout = MAX(timeout * HZ, 1);
624                 lwi = LWI_TIMEOUT_INTR(obd_timeout, back_to_sleep, NULL, NULL);
625                 rc = l_wait_event(imp->imp_recovery_waitq, 
626                                   !ptlrpc_import_in_recovery(imp), &lwi);
627
628         }
629
630         spin_lock_irqsave(&imp->imp_lock, flags);
631         if (imp->imp_state != LUSTRE_IMP_FULL) {
632                 GOTO(out, 0);
633         }
634         spin_unlock_irqrestore(&imp->imp_lock, flags);
635
636         request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
637         if (request) {
638                 /* For non-replayable connections, don't attempt
639                    reconnect if this fails */
640                 if (!imp->imp_replayable) {
641                         request->rq_no_resend = 1;
642                         IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
643                         request->rq_send_state =  LUSTRE_IMP_CONNECTING;
644                 }
645                 request->rq_replen = lustre_msg_size(0, NULL);
646                 rc = ptlrpc_queue_wait(request);
647                 ptlrpc_req_finished(request);
648         }
649
650         spin_lock_irqsave(&imp->imp_lock, flags);
651 out:
652         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
653         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
654         imp->imp_conn_cnt = 0;
655         spin_unlock_irqrestore(&imp->imp_lock, flags);
656
657         RETURN(rc);
658 }
659