Whamcloud - gitweb
b=3048
[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 static 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(imp->imp_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         ptlrpcd_add_req(request);
399         rc = 0;
400 out:
401         if (rc != 0) {
402                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
403         }
404
405         RETURN(rc);
406 }
407
408 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
409                                     void * data, int rc)
410 {
411         struct ptlrpc_connect_async_args *aa = data;
412         struct obd_import *imp = request->rq_import;
413         struct lustre_handle old_hdl;
414         unsigned long flags;
415         int msg_flags;
416         ENTRY;
417
418         spin_lock_irqsave(&imp->imp_lock, flags);
419         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
420                 spin_unlock_irqrestore(&imp->imp_lock, flags);
421                 RETURN(0);
422         }
423         spin_unlock_irqrestore(&imp->imp_lock, flags);
424
425         if (rc)
426                 GOTO(out, rc);
427
428         LASSERT(imp->imp_conn_current);
429         imp->imp_conn_current->oic_last_attempt = 0;
430
431         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
432
433         if (aa->pcaa_initial_connect) {
434                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
435                         CDEBUG(D_HA, "connected to replayable target: %s\n",
436                                imp->imp_target_uuid.uuid);
437                         imp->imp_pingable = imp->imp_replayable = 1;
438                 } else {
439                         imp->imp_replayable = 0;
440                 }
441                 imp->imp_remote_handle = request->rq_repmsg->handle;
442                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
443                 GOTO(finish, rc = 0);
444         }
445
446         /* Determine what recovery state to move the import to. */
447         if (MSG_CONNECT_RECONNECT & msg_flags) {
448                 memset(&old_hdl, 0, sizeof(old_hdl));
449                 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
450                             sizeof (old_hdl))) {
451                         CERROR("%s@%s didn't like our handle "LPX64
452                                ", failed\n", imp->imp_target_uuid.uuid,
453                                imp->imp_connection->c_remote_uuid.uuid,
454                                imp->imp_dlm_handle.cookie);
455                         GOTO(out, rc = -ENOTCONN);
456                 }
457
458                 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
459                            sizeof(imp->imp_remote_handle))) {
460                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
461                                "; copying, but this may foreshadow disaster\n",
462                                imp->imp_target_uuid.uuid,
463                                imp->imp_connection->c_remote_uuid.uuid,
464                                imp->imp_remote_handle.cookie,
465                                request->rq_repmsg->handle.cookie);
466                         imp->imp_remote_handle = request->rq_repmsg->handle;
467                 } else {
468                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
469                                imp->imp_target_uuid.uuid,
470                                imp->imp_connection->c_remote_uuid.uuid);
471                 }
472
473                 if (imp->imp_invalid) {
474                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
475                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
476                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
477                                imp->imp_obd->obd_name,
478                                imp->imp_target_uuid.uuid);
479                         imp->imp_resend_replay = 1;
480                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
481                 } else {
482                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
483                 }
484         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
485                 LASSERT(imp->imp_replayable);
486                 imp->imp_remote_handle = request->rq_repmsg->handle;
487                 imp->imp_last_replay_transno = 0;
488                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
489         } else {
490                 imp->imp_remote_handle = request->rq_repmsg->handle;
491                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
492         }
493
494         /* Sanity checks for a reconnected import. */
495         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
496                 CERROR("imp_replayable flag does not match server "
497                        "after reconnect. We should LBUG right here.\n");
498         }
499
500         if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
501                 CERROR("%s went back in time (transno "LPD64
502                        " was previously committed, server now claims "LPD64
503                        ")! is shared storage not coherent?\n",
504                        imp->imp_target_uuid.uuid,
505                        aa->pcaa_peer_committed,
506                        request->rq_repmsg->last_committed);
507         }
508
509 finish:
510         rc = ptlrpc_import_recovery_state_machine(imp);
511         if (rc != 0) {
512                 if (rc == -ENOTCONN) {
513                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
514                                "invalidating and reconnecting\n",
515                                imp->imp_target_uuid.uuid,
516                                imp->imp_connection->c_remote_uuid.uuid);
517                         ptlrpc_connect_import(imp, NULL);
518                         RETURN(0);
519                 }
520         }
521  out:
522         if (rc != 0) {
523                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
524                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
525                         ptlrpc_deactivate_import(imp);
526                 }
527                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
528                        imp->imp_target_uuid.uuid,
529                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
530         }
531
532         wake_up(&imp->imp_recovery_waitq);
533         RETURN(rc);
534 }
535
536 static int completed_replay_interpret(struct ptlrpc_request *req,
537                                     void * data, int rc)
538 {
539         atomic_dec(&req->rq_import->imp_replay_inflight);
540         if (req->rq_status == 0) {
541                 ptlrpc_import_recovery_state_machine(req->rq_import);
542         } else {
543                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
544                        "reconnecting\n", 
545                        req->rq_import->imp_obd->obd_name, req->rq_status);
546                 ptlrpc_connect_import(req->rq_import, NULL);
547         }
548
549         RETURN(0);
550 }
551
552 static int signal_completed_replay(struct obd_import *imp)
553 {
554         struct ptlrpc_request *req;
555         ENTRY;
556
557         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
558         atomic_inc(&imp->imp_replay_inflight);
559
560         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
561         if (!req) {
562                 atomic_dec(&imp->imp_replay_inflight);
563                 RETURN(-ENOMEM);
564         }
565
566         req->rq_replen = lustre_msg_size(0, NULL);
567         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
568         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
569         req->rq_timeout *= 3;
570         req->rq_interpret_reply = completed_replay_interpret;
571
572         ptlrpcd_add_req(req);
573         RETURN(0);
574 }
575
576 static int ptlrpc_invalidate_import_thread(void *data)
577 {
578         struct obd_import *imp = data;
579         unsigned long flags;
580
581         ENTRY;
582
583         lock_kernel();
584         ptlrpc_daemonize();
585
586         SIGNAL_MASK_LOCK(current, flags);
587         sigfillset(&current->blocked);
588         RECALC_SIGPENDING;
589         SIGNAL_MASK_UNLOCK(current, flags);
590         THREAD_NAME(current->comm, sizeof(current->comm), "ll_imp_inval");
591         unlock_kernel();
592
593         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
594                imp->imp_obd->obd_name, imp->imp_target_uuid.uuid,
595                imp->imp_connection->c_remote_uuid.uuid);
596
597         ptlrpc_invalidate_import(imp);
598
599         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
600         ptlrpc_import_recovery_state_machine(imp);
601
602         RETURN(0);
603 }
604
605 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
606 {
607         int rc = 0;
608         int inflight;
609         char *target_start;
610         int target_len;
611
612         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
613                 deuuidify(imp->imp_target_uuid.uuid, NULL,
614                           &target_start, &target_len);
615                 LCONSOLE_ERROR("This client was evicted by %.*s; in progress "
616                                "operations using this service will %s.\n",
617                                target_len, target_start,
618                                imp->imp_replayable
619                                ? "be reattempted"
620                                : "fail");
621                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
622                        imp->imp_target_uuid.uuid,
623                        imp->imp_connection->c_remote_uuid.uuid);
624
625                 rc = kernel_thread(ptlrpc_invalidate_import_thread, imp,
626                                    CLONE_VM | CLONE_FILES);
627                 if (rc < 0)
628                         CERROR("error starting invalidate thread: %d\n", rc);
629                 RETURN(rc);
630         }
631
632         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
633                 CDEBUG(D_HA, "replay requested by %s\n",
634                        imp->imp_target_uuid.uuid);
635                 rc = ptlrpc_replay_next(imp, &inflight);
636                 if (inflight == 0 &&
637                     atomic_read(&imp->imp_replay_inflight) == 0) {
638                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
639                         rc = ldlm_replay_locks(imp);
640                         if (rc)
641                                 GOTO(out, rc);
642                 }
643                 rc = 0;
644         }
645
646         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
647                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
648                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
649                         rc = signal_completed_replay(imp);
650                         if (rc)
651                                 GOTO(out, rc);
652                 }
653
654         }
655
656         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
657                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
658                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
659                 }
660         }
661
662         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
663                 char nidbuf[PTL_NALFMT_SIZE];
664
665                 CDEBUG(D_HA, "reconnected to %s@%s\n",
666                        imp->imp_target_uuid.uuid,
667                        imp->imp_connection->c_remote_uuid.uuid);
668
669                 rc = ptlrpc_resend(imp);
670                 if (rc)
671                         GOTO(out, rc);
672                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
673                 ptlrpc_activate_import(imp);
674
675                 deuuidify(imp->imp_target_uuid.uuid, NULL,
676                           &target_start, &target_len);
677                 ptlrpc_peernid2str(&imp->imp_connection->c_peer,
678                                    nidbuf);
679
680                 LCONSOLE_INFO("Connection restored to service %.*s using nid "
681                               "%s.\n",
682                               target_len, target_start, nidbuf);
683
684                 CWARN("%s: connection restored to %s@%s\n",
685                       imp->imp_obd->obd_name,
686                       imp->imp_target_uuid.uuid,
687                       imp->imp_connection->c_remote_uuid.uuid);
688         }
689
690         if (imp->imp_state == LUSTRE_IMP_FULL) {
691                 wake_up(&imp->imp_recovery_waitq);
692                 ptlrpc_wake_delayed(imp);
693         }
694
695  out:
696         RETURN(rc);
697 }
698
699 static int back_to_sleep(void *unused)
700 {
701         return 0;
702 }
703
704 int ptlrpc_disconnect_import(struct obd_import *imp)
705 {
706         struct ptlrpc_request *request;
707         int rq_opc;
708         int rc = 0;
709         unsigned long flags;
710         ENTRY;
711
712         switch (imp->imp_connect_op) {
713         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
714         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
715         case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
716         default:
717                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
718                        imp->imp_target_uuid.uuid, imp->imp_connect_op);
719                 RETURN(-EINVAL);
720         }
721
722
723         if (ptlrpc_import_in_recovery(imp)) {
724                 struct l_wait_info lwi;
725                 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep,
726                                        NULL, NULL);
727                 rc = l_wait_event(imp->imp_recovery_waitq,
728                                   !ptlrpc_import_in_recovery(imp), &lwi);
729
730         }
731
732         spin_lock_irqsave(&imp->imp_lock, flags);
733         if (imp->imp_state != LUSTRE_IMP_FULL) {
734                 GOTO(out, 0);
735         }
736         spin_unlock_irqrestore(&imp->imp_lock, flags);
737
738         request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
739         if (request) {
740                 /* For non-replayable connections, don't attempt
741                    reconnect if this fails */
742                 if (!imp->imp_replayable) {
743                         request->rq_no_resend = 1;
744                         IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
745                         request->rq_send_state =  LUSTRE_IMP_CONNECTING;
746                 }
747                 request->rq_replen = lustre_msg_size(0, NULL);
748                 rc = ptlrpc_queue_wait(request);
749                 ptlrpc_req_finished(request);
750         }
751
752         spin_lock_irqsave(&imp->imp_lock, flags);
753 out:
754         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
755         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
756         spin_unlock_irqrestore(&imp->imp_lock, flags);
757
758         RETURN(rc);
759 }
760