Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / ptlrpc / import.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Mike Shaver <shaver@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <lustre_ha.h>
33 #include <lustre_net.h>
34 #include <lustre_import.h>
35 #include <lustre_export.h>
36 #include <obd.h>
37 #include <obd_class.h>
38
39 #include "ptlrpc_internal.h"
40
41 struct ptlrpc_connect_async_args {
42          __u64 pcaa_peer_committed;
43         int pcaa_initial_connect;
44 };
45
46 /* A CLOSED import should remain so. */
47 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
48 do {                                                                           \
49         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
50                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
51                       imp, obd2cli_tgt(imp->imp_obd),                          \
52                       ptlrpc_import_state_name(imp->imp_state),                \
53                       ptlrpc_import_state_name(state));                        \
54                imp->imp_state = state;                                         \
55         }                                                                      \
56 } while(0)
57
58 #define IMPORT_SET_STATE(imp, state)            \
59 do {                                            \
60         spin_lock(&imp->imp_lock);              \
61         IMPORT_SET_STATE_NOLOCK(imp, state);    \
62         spin_unlock(&imp->imp_lock);            \
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         spin_lock(&imp->imp_lock);
78
79         imp->imp_generation++;
80         imp->imp_state =  LUSTRE_IMP_NEW;
81
82         spin_unlock(&imp->imp_lock);
83
84         return 0;
85 }
86 EXPORT_SYMBOL(ptlrpc_init_import);
87
88 #define UUID_STR "_UUID"
89 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
90                       int *uuid_len)
91 {
92         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
93                 ? uuid : uuid + strlen(prefix);
94
95         *uuid_len = strlen(*uuid_start);
96
97         if (*uuid_len < strlen(UUID_STR))
98                 return;
99
100         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
101                     UUID_STR, strlen(UUID_STR)))
102                 *uuid_len -= strlen(UUID_STR);
103 }
104
105 /* Returns true if import was FULL, false if import was already not
106  * connected.
107  * @imp - import to be disconnected
108  * @conn_cnt - connection count (epoch) of the request that timed out
109  *             and caused the disconnection.  In some cases, multiple
110  *             inflight requests can fail to a single target (e.g. OST
111  *             bulk requests) and if one has already caused a reconnection
112  *             (increasing the import->conn_cnt) the older failure should
113  *             not also cause a reconnection.  If zero it forces a reconnect.
114  */
115 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
116 {
117         int rc = 0;
118
119         spin_lock(&imp->imp_lock);
120
121         if (imp->imp_state == LUSTRE_IMP_FULL &&
122             (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
123                 char *target_start;
124                 int   target_len;
125
126                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
127                           &target_start, &target_len);
128
129                 if (imp->imp_replayable) {
130                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
131                                "%s was lost; in progress operations using this "
132                                "service will wait for recovery to complete.\n",
133                                imp->imp_obd->obd_name, target_len, target_start,
134                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
135                 } else {
136                         LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
137                                            "%.*s via nid %s was lost; in progress"
138                                            "operations using this service will"
139                                            "fail.\n",
140                                            imp->imp_obd->obd_name,
141                                            target_len, target_start,
142                                  libcfs_nid2str(imp->imp_connection->c_peer.nid));
143                 }
144                 ptlrpc_deactivate_timeouts(imp);
145                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
146                 spin_unlock(&imp->imp_lock);
147     
148                 if (obd_dump_on_timeout)
149                         libcfs_debug_dumplog();
150
151                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
152                 rc = 1;
153         } else {
154                 spin_unlock(&imp->imp_lock);
155                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
156                        imp->imp_client->cli_name, imp,
157                        (imp->imp_state == LUSTRE_IMP_FULL &&
158                         imp->imp_conn_cnt > conn_cnt) ?
159                        "reconnected" : "not connected", imp->imp_conn_cnt,
160                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
161         }
162
163         return rc;
164 }
165
166 /* Must be called with imp_lock held! */
167 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
168 {
169         ENTRY;
170         LASSERT_SPIN_LOCKED(&imp->imp_lock);
171
172         if (imp->imp_invalid) {
173                 spin_unlock(&imp->imp_lock);
174                 EXIT;
175                 return;
176         }
177
178         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
179         imp->imp_invalid = 1;
180         imp->imp_generation++;
181         spin_unlock(&imp->imp_lock);
182
183         ptlrpc_abort_inflight(imp);
184         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
185
186         EXIT;
187 }
188
189 /*
190  * This acts as a barrier; all existing requests are rejected, and
191  * no new requests will be accepted until the import is valid again.
192  */
193 void ptlrpc_deactivate_import(struct obd_import *imp)
194 {
195         spin_lock(&imp->imp_lock);
196         ptlrpc_deactivate_and_unlock_import(imp);
197 }
198
199 /*
200  * This function will invalidate the import, if necessary, then block
201  * for all the RPC completions, and finally notify the obd to
202  * invalidate its state (ie cancel locks, clear pending requests,
203  * etc).
204  */
205 void ptlrpc_invalidate_import(struct obd_import *imp)
206 {
207         struct l_wait_info lwi;
208         int rc;
209
210         atomic_inc(&imp->imp_inval_count);
211
212         ptlrpc_deactivate_import(imp);
213
214         LASSERT(imp->imp_invalid);
215
216         /* wait for all requests to error out and call completion callbacks */
217         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
218                                    HZ, NULL, NULL);
219         rc = l_wait_event(imp->imp_recovery_waitq,
220                           (atomic_read(&imp->imp_inflight) == 0), &lwi);
221
222         if (rc)
223                 CDEBUG(D_HA, "%s: rc = %d waiting for callback (%d != 0)\n",
224                        obd2cli_tgt(imp->imp_obd), rc,
225                        atomic_read(&imp->imp_inflight));
226
227         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
228         sptlrpc_import_flush_all_ctx(imp);
229
230         atomic_dec(&imp->imp_inval_count);
231         cfs_waitq_signal(&imp->imp_recovery_waitq);
232 }
233
234 /* unset imp_invalid */
235 void ptlrpc_activate_import(struct obd_import *imp)
236 {
237         struct obd_device *obd = imp->imp_obd;
238
239         spin_lock(&imp->imp_lock);
240         imp->imp_invalid = 0;
241         ptlrpc_activate_timeouts(imp);
242         spin_unlock(&imp->imp_lock);
243         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
244 }
245
246 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
247 {
248         ENTRY;
249
250         LASSERT(!imp->imp_dlm_fake);
251
252         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
253                 if (!imp->imp_replayable) {
254                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
255                                "auto-deactivating\n",
256                                obd2cli_tgt(imp->imp_obd),
257                                imp->imp_connection->c_remote_uuid.uuid,
258                                imp->imp_obd->obd_name);
259                         ptlrpc_deactivate_import(imp);
260                 }
261
262                 CDEBUG(D_HA, "%s: waking up pinger\n",
263                        obd2cli_tgt(imp->imp_obd));
264
265                 spin_lock(&imp->imp_lock);
266                 imp->imp_force_verify = 1;
267                 spin_unlock(&imp->imp_lock);
268
269                 ptlrpc_pinger_wake_up();
270         }
271         EXIT;
272 }
273
274 int ptlrpc_reconnect_import(struct obd_import *imp)
275 {
276         
277         ptlrpc_set_import_discon(imp, 0); 
278         /* Force a new connect attempt */
279         ptlrpc_invalidate_import(imp);
280         /* Do a fresh connect next time by zeroing the handle */
281         ptlrpc_disconnect_import(imp, 1);
282         /* Wait for all invalidate calls to finish */
283         if (atomic_read(&imp->imp_inval_count) > 0) {
284                 int rc;
285                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
286                 rc = l_wait_event(imp->imp_recovery_waitq,
287                                   (atomic_read(&imp->imp_inval_count) == 0),
288                                   &lwi);
289                 if (rc)
290                         CERROR("Interrupted, inval=%d\n", 
291                                atomic_read(&imp->imp_inval_count));
292         }
293
294         /* Allow reconnect attempts */
295         imp->imp_obd->obd_no_recov = 0;
296         /* Remove 'invalid' flag */
297         ptlrpc_activate_import(imp);
298         /* Attempt a new connect */
299         ptlrpc_recover_import(imp, NULL);
300         return 0;
301 }
302
303 EXPORT_SYMBOL(ptlrpc_reconnect_import);
304
305 static int import_select_connection(struct obd_import *imp)
306 {
307         struct obd_import_conn *imp_conn = NULL, *conn;
308         struct obd_export *dlmexp;
309         ENTRY;
310
311         spin_lock(&imp->imp_lock);
312
313         if (list_empty(&imp->imp_conn_list)) {
314                 CERROR("%s: no connections available\n",
315                         imp->imp_obd->obd_name);
316                 spin_unlock(&imp->imp_lock);
317                 RETURN(-EINVAL);
318         }
319
320         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
321                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
322                        imp->imp_obd->obd_name,
323                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
324                        conn->oic_last_attempt);
325                 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
326                 if (cfs_time_before_64(conn->oic_last_attempt + 
327                                        RECONNECT_INTERVAL * HZ,
328                                        cfs_time_current_64())) {
329                         /* If we have never tried this connection since the
330                            the last successful attempt, go with this one */
331                         if (cfs_time_beforeq_64(conn->oic_last_attempt,
332                                                imp->imp_last_success_conn)) {
333                                 imp_conn = conn;
334                                 break;
335                         }
336
337                         /* Both of these connections have already been tried
338                            since the last successful connection; just choose the
339                            least recently used */
340                         if (!imp_conn)
341                                 imp_conn = conn;
342                         else if (cfs_time_before_64(conn->oic_last_attempt,
343                                                     imp_conn->oic_last_attempt))
344                                 imp_conn = conn;
345                 }
346         }
347
348         /* if not found, simply choose the current one */
349         if (!imp_conn) {
350                 LASSERT(imp->imp_conn_current);
351                 imp_conn = imp->imp_conn_current;
352         }
353         LASSERT(imp_conn->oic_conn);
354
355         imp_conn->oic_last_attempt = cfs_time_current_64();
356
357         /* switch connection, don't mind if it's same as the current one */
358         if (imp->imp_connection)
359                 ptlrpc_put_connection(imp->imp_connection);
360         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
361
362         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
363         LASSERT(dlmexp != NULL);
364         if (dlmexp->exp_connection)
365                 ptlrpc_put_connection(dlmexp->exp_connection);
366         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
367         class_export_put(dlmexp);
368
369         if (imp->imp_conn_current != imp_conn) {
370                 if (imp->imp_conn_current)
371                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
372                                       imp->imp_obd->obd_name,
373                                       imp_conn->oic_uuid.uuid,
374                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
375                 imp->imp_conn_current = imp_conn;
376         }
377
378         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
379                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
380                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
381
382         spin_unlock(&imp->imp_lock);
383
384         RETURN(0);
385 }
386
387 /*
388  * must be called under imp_lock
389  */
390 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
391 {
392         struct ptlrpc_request *req;
393         struct list_head *tmp;
394
395         if (list_empty(&imp->imp_replay_list))
396                 return 0;
397         tmp = imp->imp_replay_list.next;
398         req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
399         *transno = req->rq_transno;
400         if (req->rq_transno == 0) {
401                 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
402                 LBUG();
403         }
404
405         return 1;
406 }
407
408 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
409 {
410         struct obd_device *obd = imp->imp_obd;
411         int initial_connect = 0;
412         int set_transno = 0;
413         __u64 committed_before_reconnect = 0;
414         struct ptlrpc_request *request;
415         char *bufs[] = { NULL,
416                          obd2cli_tgt(imp->imp_obd),
417                          obd->obd_uuid.uuid,
418                          (char *)&imp->imp_dlm_handle,
419                          (char *)&imp->imp_connect_data };
420         struct ptlrpc_connect_async_args *aa;
421         int rc;
422         ENTRY;
423
424         spin_lock(&imp->imp_lock);
425         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
426                 spin_unlock(&imp->imp_lock);
427                 CERROR("can't connect to a closed import\n");
428                 RETURN(-EINVAL);
429         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
430                 spin_unlock(&imp->imp_lock);
431                 CERROR("already connected\n");
432                 RETURN(0);
433         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
434                 spin_unlock(&imp->imp_lock);
435                 CERROR("already connecting\n");
436                 RETURN(-EALREADY);
437         }
438
439         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
440
441         imp->imp_conn_cnt++;
442         imp->imp_resend_replay = 0;
443
444         if (!lustre_handle_is_used(&imp->imp_remote_handle))
445                 initial_connect = 1;
446         else
447                 committed_before_reconnect = imp->imp_peer_committed_transno;
448
449         set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
450         spin_unlock(&imp->imp_lock);
451
452         if (new_uuid) {
453                 struct obd_uuid uuid;
454
455                 obd_str2uuid(&uuid, new_uuid);
456                 rc = import_set_conn_priority(imp, &uuid);
457                 if (rc)
458                         GOTO(out, rc);
459         }
460
461         rc = import_select_connection(imp);
462         if (rc)
463                 GOTO(out, rc);
464
465         /* last in connection list */
466         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
467                 if (imp->imp_initial_recov_bk && initial_connect) {
468                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
469                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
470                         /* Don't retry if connect fails */
471                         rc = 0;
472                         obd_set_info_async(obd->obd_self_export,
473                                            strlen(KEY_INIT_RECOV),
474                                            KEY_INIT_RECOV,
475                                            sizeof(rc), &rc, NULL);
476                 }
477                 if (imp->imp_recon_bk) {
478                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
479                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
480                         spin_lock(&imp->imp_lock);
481                         imp->imp_last_recon = 1;
482                         spin_unlock(&imp->imp_lock);
483                 }
484         }
485
486         rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
487         if (rc)
488                 GOTO(out, rc);
489
490         /* Reset connect flags to the originally requested flags, in case
491          * the server is updated on-the-fly we will get the new features. */
492         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
493         rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
494                            &obd->obd_uuid, &imp->imp_connect_data);
495         if (rc)
496                 GOTO(out, rc);
497
498         request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
499         if (request == NULL)
500                 GOTO(out, rc = -ENOMEM);
501
502         rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
503                                       imp->imp_connect_op, bufs, NULL);
504         if (rc) {
505                 ptlrpc_request_free(request);
506                 GOTO(out, rc);
507         }
508
509 #ifndef __KERNEL__
510         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
511 #endif
512         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
513
514         request->rq_send_state = LUSTRE_IMP_CONNECTING;
515         /* Allow a slightly larger reply for future growth compatibility */
516         req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
517                              sizeof(struct obd_connect_data)+16*sizeof(__u64));
518         ptlrpc_request_set_replen(request);
519         request->rq_interpret_reply = ptlrpc_connect_interpret;
520
521         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
522         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
523         memset(aa, 0, sizeof *aa);
524
525         aa->pcaa_peer_committed = committed_before_reconnect;
526         aa->pcaa_initial_connect = initial_connect;
527
528         if (aa->pcaa_initial_connect) {
529                 spin_lock(&imp->imp_lock);
530                 imp->imp_replayable = 1;
531                 spin_unlock(&imp->imp_lock);
532                 /* On an initial connect, we don't know which one of a
533                    failover server pair is up.  Don't wait long. */
534 #ifdef CRAY_XT3
535                 request->rq_timeout = max((int)(obd_timeout / 2), 5);
536 #else
537                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
538 #endif
539                 lustre_msg_add_op_flags(request->rq_reqmsg, 
540                                         MSG_CONNECT_INITIAL);
541         }
542
543         if (set_transno)
544                 lustre_msg_add_op_flags(request->rq_reqmsg, 
545                                         MSG_CONNECT_TRANSNO);
546
547         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
548         ptlrpcd_add_req(request);
549         rc = 0;
550 out:
551         if (rc != 0) {
552                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
553         }
554
555         RETURN(rc);
556 }
557 EXPORT_SYMBOL(ptlrpc_connect_import);
558
559 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
560 {
561 #ifdef __KERNEL__
562         struct obd_import_conn *imp_conn;
563 #endif
564         int wake_pinger = 0;
565
566         ENTRY;
567
568         spin_lock(&imp->imp_lock);
569         if (list_empty(&imp->imp_conn_list))
570                 GOTO(unlock, 0);
571
572 #ifdef __KERNEL__
573         imp_conn = list_entry(imp->imp_conn_list.prev,
574                               struct obd_import_conn,
575                               oic_item);
576
577         /* XXX: When the failover node is the primary node, it is possible
578          * to have two identical connections in imp_conn_list. We must 
579          * compare not conn's pointers but NIDs, otherwise we can defeat
580          * connection throttling. (See bug 14774.) */
581         if (imp->imp_conn_current->oic_conn->c_self != 
582                                 imp_conn->oic_conn->c_self) {
583                 ptlrpc_ping_import_soon(imp);
584                 wake_pinger = 1;
585         }
586 #else
587         /* liblustre has no pinger thead, so we wakup pinger anyway */
588         wake_pinger = 1;
589 #endif 
590
591  unlock:
592         spin_unlock(&imp->imp_lock);
593
594         if (wake_pinger)
595                 ptlrpc_pinger_wake_up();
596
597         EXIT;
598 }
599
600 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
601                                     void * data, int rc)
602 {
603         struct ptlrpc_connect_async_args *aa = data;
604         struct obd_import *imp = request->rq_import;
605         struct client_obd *cli = &imp->imp_obd->u.cli;
606         struct lustre_handle old_hdl;
607         int msg_flags;
608         ENTRY;
609
610         spin_lock(&imp->imp_lock);
611         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
612                 spin_unlock(&imp->imp_lock);
613                 RETURN(0);
614         }
615         spin_unlock(&imp->imp_lock);
616
617         if (rc)
618                 GOTO(out, rc);
619
620         LASSERT(imp->imp_conn_current);
621
622         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
623
624         /* All imports are pingable */
625         spin_lock(&imp->imp_lock);
626         imp->imp_pingable = 1;
627
628         if (aa->pcaa_initial_connect) {
629                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
630                         imp->imp_replayable = 1;
631                         spin_unlock(&imp->imp_lock);
632                         CDEBUG(D_HA, "connected to replayable target: %s\n",
633                                obd2cli_tgt(imp->imp_obd));
634                 } else {
635                         imp->imp_replayable = 0;
636                         spin_unlock(&imp->imp_lock);
637                 }
638
639                 /* if applies, adjust the imp->imp_msg_magic here
640                  * according to reply flags */
641
642                 imp->imp_remote_handle =
643                                 *lustre_msg_get_handle(request->rq_repmsg);
644
645                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
646                 ptlrpc_activate_import(imp);
647                 GOTO(finish, rc = 0);
648         } else {
649                 spin_unlock(&imp->imp_lock);
650         }
651
652         /* Determine what recovery state to move the import to. */
653         if (MSG_CONNECT_RECONNECT & msg_flags) {
654                 memset(&old_hdl, 0, sizeof(old_hdl));
655                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
656                             sizeof (old_hdl))) {
657                         CERROR("%s@%s didn't like our handle "LPX64
658                                ", failed\n", obd2cli_tgt(imp->imp_obd),
659                                imp->imp_connection->c_remote_uuid.uuid,
660                                imp->imp_dlm_handle.cookie);
661                         GOTO(out, rc = -ENOTCONN);
662                 }
663
664                 if (memcmp(&imp->imp_remote_handle,
665                            lustre_msg_get_handle(request->rq_repmsg),
666                            sizeof(imp->imp_remote_handle))) {
667
668                         CWARN("%s@%s changed server handle from "
669                                LPX64" to "LPX64" - evicting.\n",
670                                obd2cli_tgt(imp->imp_obd),
671                                imp->imp_connection->c_remote_uuid.uuid,
672                                imp->imp_remote_handle.cookie,
673                                lustre_msg_get_handle(request->rq_repmsg)->
674                                          cookie);
675                         imp->imp_remote_handle =
676                                      *lustre_msg_get_handle(request->rq_repmsg);
677
678                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
679                         GOTO(finish, rc = 0);
680                 } else {
681                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
682                                obd2cli_tgt(imp->imp_obd),
683                                imp->imp_connection->c_remote_uuid.uuid);
684                 }
685
686                 if (imp->imp_invalid) {
687                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
688                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
689                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
690                                imp->imp_obd->obd_name,
691                                obd2cli_tgt(imp->imp_obd));
692
693                         spin_lock(&imp->imp_lock);
694                         imp->imp_resend_replay = 1;
695                         spin_unlock(&imp->imp_lock);
696
697                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
698                 } else {
699                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
700                 }
701         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
702                 LASSERT(imp->imp_replayable);
703                 imp->imp_remote_handle =
704                                 *lustre_msg_get_handle(request->rq_repmsg);
705                 imp->imp_last_replay_transno = 0;
706                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
707         } else {
708                 imp->imp_remote_handle =
709                                 *lustre_msg_get_handle(request->rq_repmsg);
710                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
711         }
712
713         /* Sanity checks for a reconnected import. */
714         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
715                 CERROR("imp_replayable flag does not match server "
716                        "after reconnect. We should LBUG right here.\n");
717         }
718
719         if (lustre_msg_get_last_committed(request->rq_repmsg) <
720             aa->pcaa_peer_committed) {
721                 CERROR("%s went back in time (transno "LPD64
722                        " was previously committed, server now claims "LPD64
723                        ")!  See https://bugzilla.clusterfs.com/"
724                        "long_list.cgi?buglist=9646\n",
725                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
726                        lustre_msg_get_last_committed(request->rq_repmsg));
727         }
728
729 finish:
730         rc = ptlrpc_import_recovery_state_machine(imp);
731         if (rc != 0) {
732                 if (rc == -ENOTCONN) {
733                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
734                                "invalidating and reconnecting\n",
735                                obd2cli_tgt(imp->imp_obd),
736                                imp->imp_connection->c_remote_uuid.uuid);
737                         ptlrpc_connect_import(imp, NULL);
738                         RETURN(0);
739                 }
740         } else {
741                 struct obd_connect_data *ocd;
742                 struct obd_export *exp;
743                 int ret;
744                 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
745                                            RCL_SERVER);
746                 /* server replied obd_connect_data is always bigger */
747                 ocd = req_capsule_server_sized_get(&request->rq_pill,
748                                                    &RMF_CONNECT_DATA, ret);
749
750                 spin_lock(&imp->imp_lock);
751                 list_del(&imp->imp_conn_current->oic_item);
752                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
753                 imp->imp_last_success_conn =
754                         imp->imp_conn_current->oic_last_attempt;
755
756                 if (ocd == NULL) {
757                         spin_unlock(&imp->imp_lock);
758                         CERROR("Wrong connect data from server\n");
759                         rc = -EPROTO;
760                         GOTO(out, rc);
761                 }
762
763                 imp->imp_connect_data = *ocd;
764
765                 exp = class_conn2export(&imp->imp_dlm_handle);
766                 spin_unlock(&imp->imp_lock);
767
768                 /* check that server granted subset of flags we asked for. */
769                 LASSERTF((ocd->ocd_connect_flags &
770                           imp->imp_connect_flags_orig) ==
771                          ocd->ocd_connect_flags, LPX64" != "LPX64,
772                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
773
774                 if (!exp) {
775                         /* This could happen if export is cleaned during the 
776                            connect attempt */
777                         CERROR("Missing export for %s\n", 
778                                imp->imp_obd->obd_name);
779                         GOTO(out, rc = -ENODEV);
780                 }
781                 exp->exp_connect_flags = ocd->ocd_connect_flags;
782                 imp->imp_obd->obd_self_export->exp_connect_flags = 
783                                                         ocd->ocd_connect_flags;
784                 class_export_put(exp);
785
786                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
787
788                 if (!ocd->ocd_ibits_known &&
789                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
790                         CERROR("Inodebits aware server returned zero compatible"
791                                " bits?\n");
792
793                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
794                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
795                                         LUSTRE_VERSION_OFFSET_WARN ||
796                      ocd->ocd_version < LUSTRE_VERSION_CODE -
797                                         LUSTRE_VERSION_OFFSET_WARN)) {
798                         /* Sigh, some compilers do not like #ifdef in the middle
799                            of macro arguments */
800 #ifdef __KERNEL__
801                         const char *older =
802                                 "older. Consider upgrading this client";
803 #else
804                         const char *older =
805                                 "older. Consider recompiling this application";
806 #endif
807                         const char *newer = "newer than client version";
808
809                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
810                                       "is much %s (%s)\n",
811                                       obd2cli_tgt(imp->imp_obd),
812                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
813                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
814                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
815                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
816                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
817                                       newer : older, LUSTRE_VERSION_STRING);
818                 }
819
820                 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
821                         /* We sent to the server ocd_cksum_types with bits set
822                          * for algorithms we understand. The server masked off
823                          * the checksum types it doesn't support */
824                         if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
825                                 LCONSOLE_WARN("The negotiation of the checksum "
826                                               "alogrithm to use with server %s "
827                                               "failed (%x/%x), disabling "
828                                               "checksums\n",
829                                               obd2cli_tgt(imp->imp_obd),
830                                               ocd->ocd_cksum_types,
831                                               OBD_CKSUM_ALL);
832                                 cli->cl_checksum = 0;
833                                 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
834                                 cli->cl_cksum_type = OBD_CKSUM_CRC32;
835                         } else {
836                                 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
837
838                                 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
839                                         cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
840                                 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
841                                         cli->cl_cksum_type = OBD_CKSUM_ADLER;
842                                 else
843                                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
844                         }
845                 } else {
846                         /* The server does not support OBD_CONNECT_CKSUM.
847                          * Enforce CRC32 for backward compatibility*/
848                         cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
849                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
850                 }
851
852                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
853                         cli->cl_max_pages_per_rpc = 
854                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
855                 }
856
857                 imp->imp_obd->obd_namespace->ns_connect_flags = 
858                                                         ocd->ocd_connect_flags;
859                 imp->imp_obd->obd_namespace->ns_orig_connect_flags = 
860                                                         ocd->ocd_connect_flags;
861
862                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
863                         (cli->cl_max_pages_per_rpc > 0));
864         }
865
866 out:
867         if (rc != 0) {
868                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
869                 spin_lock(&imp->imp_lock);
870                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
871                     (request->rq_import_generation == imp->imp_generation))
872                         ptlrpc_deactivate_and_unlock_import(imp);
873                 else
874                         spin_unlock(&imp->imp_lock);
875
876                 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
877                     (rc == -EACCES)) {
878                         /*
879                          * Give up trying to reconnect
880                          * EACCES means client has no permission for connection
881                          */
882                         imp->imp_obd->obd_no_recov = 1;
883                         ptlrpc_deactivate_import(imp);
884                 }
885
886                 if (rc == -EPROTO) {
887                         struct obd_connect_data *ocd;
888
889                         /* reply message might not be ready */
890                         if (request->rq_repmsg == NULL)
891                                 RETURN(-EPROTO);
892
893                         ocd = req_capsule_server_get(&request->rq_pill,
894                                                      &RMF_CONNECT_DATA);
895                         if (ocd &&
896                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
897                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
898                            /* Actually servers are only supposed to refuse
899                               connection from liblustre clients, so we should
900                               never see this from VFS context */
901                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
902                                         "(%d.%d.%d.%d)"
903                                         " refused connection from this client "
904                                         "with an incompatible version (%s).  "
905                                         "Client must be recompiled\n",
906                                         obd2cli_tgt(imp->imp_obd),
907                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
908                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
909                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
910                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
911                                         LUSTRE_VERSION_STRING);
912                                 ptlrpc_deactivate_import(imp);
913                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
914                         }
915                         RETURN(-EPROTO);
916                 }
917
918                 ptlrpc_maybe_ping_import_soon(imp);
919
920                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
921                        obd2cli_tgt(imp->imp_obd),
922                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
923         }
924         
925         spin_lock(&imp->imp_lock);
926         imp->imp_last_recon = 0;
927         spin_unlock(&imp->imp_lock);
928
929         cfs_waitq_signal(&imp->imp_recovery_waitq);
930         RETURN(rc);
931 }
932
933 static int completed_replay_interpret(struct ptlrpc_request *req,
934                                     void * data, int rc)
935 {
936         ENTRY;
937         atomic_dec(&req->rq_import->imp_replay_inflight);
938         if (req->rq_status == 0) {
939                 ptlrpc_import_recovery_state_machine(req->rq_import);
940         } else {
941                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
942                        "reconnecting\n",
943                        req->rq_import->imp_obd->obd_name, req->rq_status);
944                 ptlrpc_connect_import(req->rq_import, NULL);
945         }
946
947         RETURN(0);
948 }
949
950 static int signal_completed_replay(struct obd_import *imp)
951 {
952         struct ptlrpc_request *req;
953         ENTRY;
954
955         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
956         atomic_inc(&imp->imp_replay_inflight);
957
958         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
959                                         OBD_PING);
960         if (req == NULL) {
961                 atomic_dec(&imp->imp_replay_inflight);
962                 RETURN(-ENOMEM);
963         }
964
965         ptlrpc_request_set_replen(req);
966         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
967         lustre_msg_add_flags(req->rq_reqmsg, 
968                              MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
969         req->rq_timeout *= 3;
970         req->rq_interpret_reply = completed_replay_interpret;
971
972         ptlrpcd_add_req(req);
973         RETURN(0);
974 }
975
976 #ifdef __KERNEL__
977 static int ptlrpc_invalidate_import_thread(void *data)
978 {
979         struct obd_import *imp = data;
980
981         ENTRY;
982
983         ptlrpc_daemonize("ll_imp_inval");
984         
985         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
986                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
987                imp->imp_connection->c_remote_uuid.uuid);
988
989         ptlrpc_invalidate_import(imp);
990
991         if (obd_dump_on_eviction) {
992                 CERROR("dump the log upon eviction\n");
993                 libcfs_debug_dumplog();
994         }
995
996         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
997         ptlrpc_import_recovery_state_machine(imp);
998
999         RETURN(0);
1000 }
1001 #endif
1002
1003 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1004 {
1005         int rc = 0;
1006         int inflight;
1007         char *target_start;
1008         int target_len;
1009
1010         ENTRY;
1011         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1012                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1013                           &target_start, &target_len);
1014                 /* Don't care about MGC eviction */
1015                 if (strcmp(imp->imp_obd->obd_type->typ_name,
1016                            LUSTRE_MGC_NAME) != 0) {
1017                         LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1018                                            "%.*s; in progress operations using "
1019                                            "this service will fail.\n",
1020                                            target_len, target_start);
1021                 }
1022                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1023                        obd2cli_tgt(imp->imp_obd),
1024                        imp->imp_connection->c_remote_uuid.uuid);
1025
1026 #ifdef __KERNEL__
1027                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1028                                        CLONE_VM | CLONE_FILES);
1029                 if (rc < 0)
1030                         CERROR("error starting invalidate thread: %d\n", rc);
1031                 else
1032                         rc = 0;
1033                 RETURN(rc);
1034 #else
1035                 ptlrpc_invalidate_import(imp);
1036
1037                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1038 #endif
1039         }
1040
1041         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1042                 CDEBUG(D_HA, "replay requested by %s\n",
1043                        obd2cli_tgt(imp->imp_obd));
1044                 rc = ptlrpc_replay_next(imp, &inflight);
1045                 if (inflight == 0 &&
1046                     atomic_read(&imp->imp_replay_inflight) == 0) {
1047                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1048                         rc = ldlm_replay_locks(imp);
1049                         if (rc)
1050                                 GOTO(out, rc);
1051                 }
1052                 rc = 0;
1053         }
1054
1055         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1056                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1057                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1058                         rc = signal_completed_replay(imp);
1059                         if (rc)
1060                                 GOTO(out, rc);
1061                 }
1062
1063         }
1064
1065         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1066                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1067                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1068                 }
1069         }
1070
1071         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1072                 CDEBUG(D_HA, "reconnected to %s@%s\n",
1073                        obd2cli_tgt(imp->imp_obd),
1074                        imp->imp_connection->c_remote_uuid.uuid);
1075
1076                 rc = ptlrpc_resend(imp);
1077                 if (rc)
1078                         GOTO(out, rc);
1079                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1080                 ptlrpc_activate_import(imp);
1081
1082                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1083                           &target_start, &target_len);
1084                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1085                               "using nid %s.\n", imp->imp_obd->obd_name,
1086                               target_len, target_start,
1087                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
1088         }
1089
1090         if (imp->imp_state == LUSTRE_IMP_FULL) {
1091                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1092                 ptlrpc_wake_delayed(imp);
1093         }
1094
1095 out:
1096         RETURN(rc);
1097 }
1098
1099 static int back_to_sleep(void *unused)
1100 {
1101         return 0;
1102 }
1103
1104 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1105 {
1106         struct ptlrpc_request *req;
1107         int rq_opc, rc = 0;
1108         int nowait = imp->imp_obd->obd_force;
1109         ENTRY;
1110
1111         if (nowait)
1112                 GOTO(set_state, rc);
1113
1114         switch (imp->imp_connect_op) {
1115         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1116         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1117         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1118         default:
1119                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1120                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1121                 RETURN(-EINVAL);
1122         }
1123
1124         if (ptlrpc_import_in_recovery(imp)) {
1125                 struct l_wait_info lwi;
1126                 cfs_duration_t timeout;
1127                 if (imp->imp_server_timeout)
1128                         timeout = cfs_time_seconds(obd_timeout / 2);
1129                 else
1130                         timeout = cfs_time_seconds(obd_timeout);
1131                 
1132                 timeout = MAX(timeout * HZ, 1);
1133                 
1134                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
1135                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1136                 rc = l_wait_event(imp->imp_recovery_waitq,
1137                                   !ptlrpc_import_in_recovery(imp), &lwi);
1138
1139         }
1140
1141         spin_lock(&imp->imp_lock);
1142         if (imp->imp_state != LUSTRE_IMP_FULL)
1143                 GOTO(out, 0);
1144
1145         spin_unlock(&imp->imp_lock);
1146
1147         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1148                                         LUSTRE_OBD_VERSION, rq_opc);
1149         if (req) {
1150                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1151                  * it fails.  We can get through the above with a down server
1152                  * if the client doesn't know the server is gone yet. */
1153                 req->rq_no_resend = 1;
1154 #ifdef CRAY_XT3
1155                 req->rq_timeout = obd_timeout / 3;
1156 #else
1157                 req->rq_timeout = 5;
1158 #endif
1159                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1160                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1161                 ptlrpc_request_set_replen(req);
1162                 rc = ptlrpc_queue_wait(req);
1163                 ptlrpc_req_finished(req);
1164         }
1165
1166 set_state:
1167         spin_lock(&imp->imp_lock);
1168 out:
1169         if (noclose) 
1170                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1171         else
1172                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1173         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1174         imp->imp_conn_cnt = 0;
1175         imp->imp_last_recon = 0;
1176         spin_unlock(&imp->imp_lock);
1177
1178         RETURN(rc);
1179 }
1180