Whamcloud - gitweb
b=6233
[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 #define UUID_STR "_UUID"
93 static void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
94 {
95         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
96                 ? uuid : uuid + strlen(prefix);
97
98         *uuid_len = strlen(*uuid_start);
99
100         if (*uuid_len < strlen(UUID_STR))
101                 return;
102         
103         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
104                     UUID_STR, strlen(UUID_STR)))
105                 *uuid_len -= strlen(UUID_STR);
106 }
107
108 /* Returns true if import was FULL, false if import was already not
109  * connected.
110  */
111 int ptlrpc_set_import_discon(struct obd_import *imp)
112 {
113         unsigned long flags;
114         int rc = 0;
115
116         spin_lock_irqsave(&imp->imp_lock, flags);
117
118         if (imp->imp_state == LUSTRE_IMP_FULL) {
119                 char nidbuf[PTL_NALFMT_SIZE];
120                 char *target_start;
121                 int   target_len;
122
123                 deuuidify(imp->imp_target_uuid.uuid, NULL,
124                           &target_start, &target_len);
125
126                 LCONSOLE_ERROR("Connection to service %.*s via nid %s was "
127                                "lost; in progress operations using this "
128                                "service will %s.\n",
129                                target_len, target_start,
130                                ptlrpc_peernid2str(&imp->imp_connection->c_peer,
131                                                   nidbuf),
132                                imp->imp_replayable 
133                                ? "wait for recovery to complete"
134                                : "fail");
135
136                 CWARN("%s: connection lost to %s@%s\n",
137                       imp->imp_obd->obd_name,
138                       imp->imp_target_uuid.uuid,
139                       imp->imp_connection->c_remote_uuid.uuid);
140                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
141                 spin_unlock_irqrestore(&imp->imp_lock, flags);
142                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
143                 rc = 1;
144         } else {
145                 spin_unlock_irqrestore(&imp->imp_lock, flags);
146                 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
147                        imp,imp->imp_client->cli_name,
148                        ptlrpc_import_state_name(imp->imp_state));
149         }
150
151         return rc;
152 }
153
154 /*
155  * This acts as a barrier; all existing requests are rejected, and
156  * no new requests will be accepted until the import is valid again.
157  */
158 void ptlrpc_deactivate_import(struct obd_import *imp)
159 {
160         unsigned long flags;
161         ENTRY;
162
163         spin_lock_irqsave(&imp->imp_lock, flags);
164         CDEBUG(D_HA, "setting import %s INVALID\n", imp->imp_target_uuid.uuid);
165         imp->imp_invalid = 1;
166         imp->imp_generation++;
167         spin_unlock_irqrestore(&imp->imp_lock, flags);
168
169         ptlrpc_abort_inflight(imp);
170         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
171 }
172
173 /*
174  * This function will invalidate the import, if necessary, then block
175  * for all the RPC completions, and finally notify the obd to
176  * invalidate its state (ie cancel locks, clear pending requests,
177  * etc).
178  */
179 void ptlrpc_invalidate_import(struct obd_import *imp)
180 {
181         struct l_wait_info lwi;
182         int rc;
183
184         if (!imp->imp_invalid)
185                 ptlrpc_deactivate_import(imp);
186
187         LASSERT(imp->imp_invalid);
188
189         /* wait for all requests to error out and call completion callbacks */
190         lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), NULL,
191                                NULL, NULL);
192         rc = l_wait_event(imp->imp_recovery_waitq,
193                           (atomic_read(&imp->imp_inflight) == 0),
194                           &lwi);
195
196         if (rc)
197                 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
198                        imp->imp_target_uuid.uuid, rc,
199                        atomic_read(&imp->imp_inflight));
200
201         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
202 }
203
204 void ptlrpc_activate_import(struct obd_import *imp)
205 {
206         struct obd_device *obd = imp->imp_obd;
207         unsigned long flags;
208
209         spin_lock_irqsave(&imp->imp_lock, flags);
210         imp->imp_invalid = 0;
211         spin_unlock_irqrestore(&imp->imp_lock, flags);
212
213         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
214 }
215
216 void ptlrpc_fail_import(struct obd_import *imp, int generation)
217 {
218         ENTRY;
219
220         LASSERT (!imp->imp_dlm_fake);
221
222         if (ptlrpc_set_import_discon(imp)) {
223                 unsigned long flags;
224
225                 if (!imp->imp_replayable) {
226                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
227                                "auto-deactivating\n",
228                                imp->imp_target_uuid.uuid,
229                                imp->imp_connection->c_remote_uuid.uuid,
230                                imp->imp_obd->obd_name);
231                         ptlrpc_deactivate_import(imp);
232                 }
233
234                 CDEBUG(D_HA, "%s: waking up pinger\n",
235                        imp->imp_target_uuid.uuid);
236
237                 spin_lock_irqsave(&imp->imp_lock, flags);
238                 imp->imp_force_verify = 1;
239                 spin_unlock_irqrestore(&imp->imp_lock, flags);
240
241                 ptlrpc_pinger_wake_up();
242         }
243         EXIT;
244 }
245
246 #define ATTEMPT_TOO_SOON(last)  \
247         ((last) && ((long)(jiffies - (last)) <= (long)(obd_timeout * 2 * HZ)))
248
249 static int import_select_connection(struct obd_import *imp)
250 {
251         struct obd_import_conn *imp_conn, *tmp;
252         struct obd_export *dlmexp;
253         int found = 0;
254         ENTRY;
255
256         spin_lock(&imp->imp_lock);
257
258         if (list_empty(&imp->imp_conn_list)) {
259                 CERROR("%s: no connections available\n",
260                         imp->imp_obd->obd_name);
261                 spin_unlock(&imp->imp_lock);
262                 RETURN(-EINVAL);
263         }
264
265         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
266                 if (!imp_conn->oic_last_attempt ||
267                     time_after(jiffies, imp_conn->oic_last_attempt + 
268                                obd_timeout * 2 * HZ)) {
269                         found = 1;
270                         break;
271                 }
272         }
273
274         /* if not found, simply choose the current one */
275         if (!found) {
276                 CWARN("%s: continuing with current connection\n",
277                       imp->imp_obd->obd_name);
278                 LASSERT(imp->imp_conn_current);
279                 imp_conn = imp->imp_conn_current;
280         }
281         LASSERT(imp_conn->oic_conn);
282
283         imp_conn->oic_last_attempt = jiffies;
284
285         /* move the items ahead of the selected one to list tail */
286         while (1) {
287                 tmp= list_entry(imp->imp_conn_list.next,
288                                 struct obd_import_conn, oic_item);
289                 if (tmp == imp_conn)
290                         break;
291                 list_del(&tmp->oic_item);
292                 list_add_tail(&tmp->oic_item, &imp->imp_conn_list);
293         }
294
295         /* switch connection, don't mind if it's same as the current one */
296         if (imp->imp_connection)
297                 ptlrpc_put_connection(imp->imp_connection);
298         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
299
300         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
301         LASSERT(dlmexp != NULL);
302         if (dlmexp->exp_connection)
303                 ptlrpc_put_connection(dlmexp->exp_connection);
304         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
305         class_export_put(dlmexp);
306
307         imp->imp_conn_current = imp_conn;
308         CWARN("%s: Using connection %s\n",
309                imp->imp_obd->obd_name,
310                imp_conn->oic_uuid.uuid);
311         spin_unlock(&imp->imp_lock);
312
313         RETURN(0);
314 }
315
316 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
317 {
318         struct obd_device *obd = imp->imp_obd;
319         int initial_connect = 0;
320         int rc;
321         __u64 committed_before_reconnect = 0;
322         struct ptlrpc_request *request;
323         int size[] = {sizeof(imp->imp_target_uuid),
324                       sizeof(obd->obd_uuid),
325                       sizeof(imp->imp_dlm_handle),
326                       sizeof(imp->imp_connect_data)};
327         char *tmp[] = {imp->imp_target_uuid.uuid,
328                        obd->obd_uuid.uuid,
329                        (char *)&imp->imp_dlm_handle,
330                        (char *)&imp->imp_connect_data};
331         struct ptlrpc_connect_async_args *aa;
332         unsigned long flags;
333
334         spin_lock_irqsave(&imp->imp_lock, flags);
335         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
336                 spin_unlock_irqrestore(&imp->imp_lock, flags);
337                 CERROR("can't connect to a closed import\n");
338                 RETURN(-EINVAL);
339         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
340                 spin_unlock_irqrestore(&imp->imp_lock, flags);
341                 CERROR("already connected\n");
342                 RETURN(0);
343         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
344                 spin_unlock_irqrestore(&imp->imp_lock, flags);
345                 CERROR("already connecting\n");
346                 RETURN(-EALREADY);
347         }
348
349         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
350
351         imp->imp_conn_cnt++;
352         imp->imp_resend_replay = 0;
353
354         if (imp->imp_remote_handle.cookie == 0) {
355                 initial_connect = 1;
356         } else {
357                 committed_before_reconnect = imp->imp_peer_committed_transno;
358         }
359
360         spin_unlock_irqrestore(&imp->imp_lock, flags);
361
362         if (new_uuid) {
363                 struct obd_uuid uuid;
364
365                 obd_str2uuid(&uuid, new_uuid);
366                 rc = import_set_conn_priority(imp, &uuid);
367                 if (rc)
368                         GOTO(out, rc);
369         }
370
371         rc = import_select_connection(imp);
372         if (rc)
373                 GOTO(out, rc);
374
375         request = ptlrpc_prep_req(imp, imp->imp_connect_op, 4, size, tmp);
376         if (!request)
377                 GOTO(out, rc = -ENOMEM);
378
379 #ifndef __KERNEL__
380         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
381 #endif
382
383         request->rq_send_state = LUSTRE_IMP_CONNECTING;
384         size[0] = sizeof(struct obd_connect_data);
385         request->rq_replen = lustre_msg_size(1, size);
386         request->rq_interpret_reply = ptlrpc_connect_interpret;
387
388         LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
389         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
390         memset(aa, 0, sizeof *aa);
391
392         aa->pcaa_peer_committed = committed_before_reconnect;
393         aa->pcaa_initial_connect = initial_connect;
394
395         if (aa->pcaa_initial_connect)
396                 imp->imp_replayable = 1;
397
398         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
399         ptlrpcd_add_req(request);
400         rc = 0;
401 out:
402         if (rc != 0) {
403                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
404         }
405
406         RETURN(rc);
407 }
408
409 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
410                                     void * data, int rc)
411 {
412         struct ptlrpc_connect_async_args *aa = data;
413         struct obd_import *imp = request->rq_import;
414         struct lustre_handle old_hdl;
415         unsigned long flags;
416         int msg_flags;
417         ENTRY;
418
419         spin_lock_irqsave(&imp->imp_lock, flags);
420         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
421                 spin_unlock_irqrestore(&imp->imp_lock, flags);
422                 RETURN(0);
423         }
424         spin_unlock_irqrestore(&imp->imp_lock, flags);
425
426         if (rc)
427                 GOTO(out, rc);
428
429         LASSERT(imp->imp_conn_current);
430         imp->imp_conn_current->oic_last_attempt = 0;
431
432         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
433
434         /* All imports are pingable */
435         imp->imp_pingable = 1;
436         
437         if (aa->pcaa_initial_connect) {
438                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
439                         CDEBUG(D_HA, "connected to replayable target: %s\n",
440                                imp->imp_target_uuid.uuid);
441                         imp->imp_replayable = 1;
442                 } else {
443                         imp->imp_replayable = 0;
444                 }
445                 imp->imp_remote_handle = request->rq_repmsg->handle;
446                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
447                 GOTO(finish, rc = 0);
448         }
449
450         /* Determine what recovery state to move the import to. */
451         if (MSG_CONNECT_RECONNECT & msg_flags) {
452                 memset(&old_hdl, 0, sizeof(old_hdl));
453                 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
454                             sizeof (old_hdl))) {
455                         CERROR("%s@%s didn't like our handle "LPX64
456                                ", failed\n", imp->imp_target_uuid.uuid,
457                                imp->imp_connection->c_remote_uuid.uuid,
458                                imp->imp_dlm_handle.cookie);
459                         GOTO(out, rc = -ENOTCONN);
460                 }
461
462                 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
463                            sizeof(imp->imp_remote_handle))) {
464                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
465                                "; copying, but this may foreshadow disaster\n",
466                                imp->imp_target_uuid.uuid,
467                                imp->imp_connection->c_remote_uuid.uuid,
468                                imp->imp_remote_handle.cookie,
469                                request->rq_repmsg->handle.cookie);
470                         imp->imp_remote_handle = request->rq_repmsg->handle;
471                 } else {
472                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
473                                imp->imp_target_uuid.uuid,
474                                imp->imp_connection->c_remote_uuid.uuid);
475                 }
476
477                 if (imp->imp_invalid) {
478                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
479                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
480                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
481                                imp->imp_obd->obd_name,
482                                imp->imp_target_uuid.uuid);
483                         imp->imp_resend_replay = 1;
484                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
485                 } else {
486                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
487                 }
488         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
489                 LASSERT(imp->imp_replayable);
490                 imp->imp_remote_handle = request->rq_repmsg->handle;
491                 imp->imp_last_replay_transno = 0;
492                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
493         } else {
494                 imp->imp_remote_handle = request->rq_repmsg->handle;
495                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
496         }
497
498         /* Sanity checks for a reconnected import. */
499         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
500                 CERROR("imp_replayable flag does not match server "
501                        "after reconnect. We should LBUG right here.\n");
502         }
503
504         if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
505                 CERROR("%s went back in time (transno "LPD64
506                        " was previously committed, server now claims "LPD64
507                        ")! is shared storage not coherent?\n",
508                        imp->imp_target_uuid.uuid,
509                        aa->pcaa_peer_committed,
510                        request->rq_repmsg->last_committed);
511         }
512
513 finish:
514         rc = ptlrpc_import_recovery_state_machine(imp);
515         if (rc != 0) {
516                 if (rc == -ENOTCONN) {
517                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
518                                "invalidating and reconnecting\n",
519                                imp->imp_target_uuid.uuid,
520                                imp->imp_connection->c_remote_uuid.uuid);
521                         ptlrpc_connect_import(imp, NULL);
522                         RETURN(0);
523                 }
524         }
525  out:
526         if (rc != 0) {
527                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
528                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
529                         ptlrpc_deactivate_import(imp);
530                 }
531                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
532                        imp->imp_target_uuid.uuid,
533                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
534         }
535
536         wake_up(&imp->imp_recovery_waitq);
537         RETURN(rc);
538 }
539
540 static int completed_replay_interpret(struct ptlrpc_request *req,
541                                     void * data, int rc)
542 {
543         atomic_dec(&req->rq_import->imp_replay_inflight);
544         if (req->rq_status == 0) {
545                 ptlrpc_import_recovery_state_machine(req->rq_import);
546         } else {
547                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
548                        "reconnecting\n", 
549                        req->rq_import->imp_obd->obd_name, req->rq_status);
550                 ptlrpc_connect_import(req->rq_import, NULL);
551         }
552
553         RETURN(0);
554 }
555
556 static int signal_completed_replay(struct obd_import *imp)
557 {
558         struct ptlrpc_request *req;
559         ENTRY;
560
561         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
562         atomic_inc(&imp->imp_replay_inflight);
563
564         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
565         if (!req) {
566                 atomic_dec(&imp->imp_replay_inflight);
567                 RETURN(-ENOMEM);
568         }
569
570         req->rq_replen = lustre_msg_size(0, NULL);
571         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
572         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
573         req->rq_timeout *= 3;
574         req->rq_interpret_reply = completed_replay_interpret;
575
576         ptlrpcd_add_req(req);
577         RETURN(0);
578 }
579
580 static int ptlrpc_invalidate_import_thread(void *data)
581 {
582         struct obd_import *imp = data;
583         unsigned long flags;
584
585         ENTRY;
586
587         lock_kernel();
588         ptlrpc_daemonize();
589
590         SIGNAL_MASK_LOCK(current, flags);
591         sigfillset(&current->blocked);
592         RECALC_SIGPENDING;
593         SIGNAL_MASK_UNLOCK(current, flags);
594         THREAD_NAME(current->comm, sizeof(current->comm), "ll_imp_inval");
595         unlock_kernel();
596
597         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
598                imp->imp_obd->obd_name, imp->imp_target_uuid.uuid,
599                imp->imp_connection->c_remote_uuid.uuid);
600
601         ptlrpc_invalidate_import(imp);
602
603         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
604         ptlrpc_import_recovery_state_machine(imp);
605
606         RETURN(0);
607 }
608
609 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
610 {
611         int rc = 0;
612         int inflight;
613         char *target_start;
614         int target_len;
615
616         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
617                 deuuidify(imp->imp_target_uuid.uuid, NULL,
618                           &target_start, &target_len);
619                 LCONSOLE_ERROR("This client was evicted by %.*s; in progress "
620                                "operations using this service will %s.\n",
621                                target_len, target_start,
622                                imp->imp_replayable
623                                ? "be reattempted"
624                                : "fail");
625                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
626                        imp->imp_target_uuid.uuid,
627                        imp->imp_connection->c_remote_uuid.uuid);
628
629                 rc = kernel_thread(ptlrpc_invalidate_import_thread, imp,
630                                    CLONE_VM | CLONE_FILES);
631                 if (rc < 0)
632                         CERROR("error starting invalidate thread: %d\n", rc);
633                 RETURN(rc);
634         }
635
636         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
637                 CDEBUG(D_HA, "replay requested by %s\n",
638                        imp->imp_target_uuid.uuid);
639                 rc = ptlrpc_replay_next(imp, &inflight);
640                 if (inflight == 0 &&
641                     atomic_read(&imp->imp_replay_inflight) == 0) {
642                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
643                         rc = ldlm_replay_locks(imp);
644                         if (rc)
645                                 GOTO(out, rc);
646                 }
647                 rc = 0;
648         }
649
650         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
651                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
652                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
653                         rc = signal_completed_replay(imp);
654                         if (rc)
655                                 GOTO(out, rc);
656                 }
657
658         }
659
660         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
661                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
662                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
663                 }
664         }
665
666         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
667                 char nidbuf[PTL_NALFMT_SIZE];
668
669                 CDEBUG(D_HA, "reconnected to %s@%s\n",
670                        imp->imp_target_uuid.uuid,
671                        imp->imp_connection->c_remote_uuid.uuid);
672
673                 rc = ptlrpc_resend(imp);
674                 if (rc)
675                         GOTO(out, rc);
676                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
677                 ptlrpc_activate_import(imp);
678
679                 deuuidify(imp->imp_target_uuid.uuid, NULL,
680                           &target_start, &target_len);
681                 ptlrpc_peernid2str(&imp->imp_connection->c_peer,
682                                    nidbuf);
683
684                 LCONSOLE_INFO("Connection restored to service %.*s using nid "
685                               "%s.\n",
686                               target_len, target_start, nidbuf);
687
688                 CWARN("%s: connection restored to %s@%s\n",
689                       imp->imp_obd->obd_name,
690                       imp->imp_target_uuid.uuid,
691                       imp->imp_connection->c_remote_uuid.uuid);
692         }
693
694         if (imp->imp_state == LUSTRE_IMP_FULL) {
695                 wake_up(&imp->imp_recovery_waitq);
696                 ptlrpc_wake_delayed(imp);
697         }
698
699  out:
700         RETURN(rc);
701 }
702
703 static int back_to_sleep(void *unused)
704 {
705         return 0;
706 }
707
708 int ptlrpc_disconnect_import(struct obd_import *imp)
709 {
710         struct ptlrpc_request *request;
711         int rq_opc;
712         int rc = 0;
713         unsigned long flags;
714         ENTRY;
715
716         switch (imp->imp_connect_op) {
717         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
718         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
719         case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
720         default:
721                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
722                        imp->imp_target_uuid.uuid, imp->imp_connect_op);
723                 RETURN(-EINVAL);
724         }
725
726
727         if (ptlrpc_import_in_recovery(imp)) {
728                 struct l_wait_info lwi;
729                 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep,
730                                        NULL, NULL);
731                 rc = l_wait_event(imp->imp_recovery_waitq,
732                                   !ptlrpc_import_in_recovery(imp), &lwi);
733
734         }
735
736         spin_lock_irqsave(&imp->imp_lock, flags);
737         if (imp->imp_state != LUSTRE_IMP_FULL) {
738                 GOTO(out, 0);
739         }
740         spin_unlock_irqrestore(&imp->imp_lock, flags);
741
742         request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
743         if (request) {
744                 /* For non-replayable connections, don't attempt
745                    reconnect if this fails */
746                 if (!imp->imp_replayable) {
747                         request->rq_no_resend = 1;
748                         IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
749                         request->rq_send_state =  LUSTRE_IMP_CONNECTING;
750                 }
751                 request->rq_replen = lustre_msg_size(0, NULL);
752                 rc = ptlrpc_queue_wait(request);
753                 ptlrpc_req_finished(request);
754         }
755
756         spin_lock_irqsave(&imp->imp_lock, flags);
757 out:
758         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
759         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
760         spin_unlock_irqrestore(&imp->imp_lock, flags);
761
762         RETURN(rc);
763 }
764