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