Whamcloud - gitweb
branch: b_new_cmd
[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 #include <lustre_ver.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, obd2cli_tgt(imp->imp_obd),                          \
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         spin_lock(&imp->imp_lock);              \
62         IMPORT_SET_STATE_NOLOCK(imp, state);    \
63         spin_unlock(&imp->imp_lock);            \
64 } while(0)
65
66
67 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
68                                     void * data, int rc);
69 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
70
71 /* Only this function is allowed to change the import state when it is
72  * CLOSED. I would rather refcount the import and free it after
73  * disconnection like we do with exports. To do that, the client_obd
74  * will need to save the peer info somewhere other than in the import,
75  * though. */
76 int ptlrpc_init_import(struct obd_import *imp)
77 {
78         spin_lock(&imp->imp_lock);
79
80         imp->imp_generation++;
81         imp->imp_state =  LUSTRE_IMP_NEW;
82
83         spin_unlock(&imp->imp_lock);
84
85         return 0;
86 }
87 EXPORT_SYMBOL(ptlrpc_init_import);
88
89 #define UUID_STR "_UUID"
90 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
91                       int *uuid_len)
92 {
93         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
94                 ? uuid : uuid + strlen(prefix);
95
96         *uuid_len = strlen(*uuid_start);
97
98         if (*uuid_len < strlen(UUID_STR))
99                 return;
100
101         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
102                     UUID_STR, strlen(UUID_STR)))
103                 *uuid_len -= strlen(UUID_STR);
104 }
105
106 /* Returns true if import was FULL, false if import was already not
107  * connected.
108  * @imp - import to be disconnected
109  * @conn_cnt - connection count (epoch) of the request that timed out
110  *             and caused the disconnection.  In some cases, multiple
111  *             inflight requests can fail to a single target (e.g. OST
112  *             bulk requests) and if one has already caused a reconnection
113  *             (increasing the import->conn_cnt) the older failure should
114  *             not also cause a reconnection.  If zero it forces a reconnect.
115  */
116 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
117 {
118         int rc = 0;
119
120         spin_lock(&imp->imp_lock);
121
122         if (imp->imp_state == LUSTRE_IMP_FULL &&
123             (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
124                 char *target_start;
125                 int   target_len;
126
127                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
128                           &target_start, &target_len);
129
130                 LCONSOLE_ERROR("%s: Connection to service %.*s via nid %s was "
131                                "lost; in progress operations using this "
132                                "service will %s.\n", imp->imp_obd->obd_name,
133                                target_len, target_start,
134                                libcfs_nid2str(imp->imp_connection->c_peer.nid),
135                                imp->imp_replayable ?
136                                       "wait for recovery to complete" : "fail");
137
138                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
139                 spin_unlock(&imp->imp_lock);
140     
141                 if (obd_dump_on_timeout)
142                         libcfs_debug_dumplog();
143
144                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
145                 rc = 1;
146         } else {
147                 spin_unlock(&imp->imp_lock);
148                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
149                        imp->imp_client->cli_name, imp,
150                        (imp->imp_state == LUSTRE_IMP_FULL &&
151                         imp->imp_conn_cnt > conn_cnt) ?
152                        "reconnected" : "not connected", imp->imp_conn_cnt,
153                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
154         }
155
156         return rc;
157 }
158
159 /*
160  * This acts as a barrier; all existing requests are rejected, and
161  * no new requests will be accepted until the import is valid again.
162  */
163 void ptlrpc_deactivate_import(struct obd_import *imp)
164 {
165         ENTRY;
166
167         spin_lock(&imp->imp_lock);
168         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
169         imp->imp_invalid = 1;
170         imp->imp_generation++;
171         spin_unlock(&imp->imp_lock);
172
173         ptlrpc_abort_inflight(imp);
174         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
175 }
176
177 /*
178  * This function will invalidate the import, if necessary, then block
179  * for all the RPC completions, and finally notify the obd to
180  * invalidate its state (ie cancel locks, clear pending requests,
181  * etc).
182  */
183 void ptlrpc_invalidate_import(struct obd_import *imp)
184 {
185         struct l_wait_info lwi;
186         int rc;
187
188         if (!imp->imp_invalid)
189                 ptlrpc_deactivate_import(imp);
190
191         LASSERT(imp->imp_invalid);
192
193         /* wait for all requests to error out and call completion callbacks */
194         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
195                                    HZ, NULL, NULL);
196         rc = l_wait_event(imp->imp_recovery_waitq,
197                           (atomic_read(&imp->imp_inflight) == 0),
198                           &lwi);
199
200         if (rc)
201                 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
202                        obd2cli_tgt(imp->imp_obd), rc,
203                        atomic_read(&imp->imp_inflight));
204
205         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
206 }
207
208 /* unset imp_invalid */
209 void ptlrpc_activate_import(struct obd_import *imp)
210 {
211         struct obd_device *obd = imp->imp_obd;
212
213         spin_lock(&imp->imp_lock);
214         imp->imp_invalid = 0;
215         spin_unlock(&imp->imp_lock);
216
217         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
218 }
219
220 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
221 {
222         ENTRY;
223
224         LASSERT(!imp->imp_dlm_fake);
225
226         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
227                 if (!imp->imp_replayable) {
228                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
229                                "auto-deactivating\n",
230                                obd2cli_tgt(imp->imp_obd),
231                                imp->imp_connection->c_remote_uuid.uuid,
232                                imp->imp_obd->obd_name);
233                         ptlrpc_deactivate_import(imp);
234                 }
235
236                 CDEBUG(D_HA, "%s: waking up pinger\n",
237                        obd2cli_tgt(imp->imp_obd));
238
239                 spin_lock(&imp->imp_lock);
240                 imp->imp_force_verify = 1;
241                 spin_unlock(&imp->imp_lock);
242
243                 ptlrpc_pinger_wake_up();
244         }
245         EXIT;
246 }
247
248 static int import_select_connection(struct obd_import *imp)
249 {
250         struct obd_import_conn *imp_conn = NULL, *conn;
251         struct obd_export *dlmexp;
252         ENTRY;
253
254         spin_lock(&imp->imp_lock);
255
256         if (list_empty(&imp->imp_conn_list)) {
257                 CERROR("%s: no connections available\n",
258                         imp->imp_obd->obd_name);
259                 spin_unlock(&imp->imp_lock);
260                 RETURN(-EINVAL);
261         }
262
263         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
264                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
265                        imp->imp_obd->obd_name,
266                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
267                        conn->oic_last_attempt);
268                 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
269                 if (cfs_time_before_64(conn->oic_last_attempt + 
270                                        RECONNECT_INTERVAL * HZ,
271                                        cfs_time_current_64())) {
272                         /* If we have never tried this connection since the
273                            the last successful attempt, go with this one */
274                         if (cfs_time_before_64(conn->oic_last_attempt,
275                                                imp->imp_last_success_conn)) {
276                                 imp_conn = conn;
277                                 break;
278                         }
279
280                         /* Both of these connections have already been tried
281                            since the last successful connection; just choose the
282                            least recently used */
283                         if (!imp_conn)
284                                 imp_conn = conn;
285                         else if (cfs_time_before_64(conn->oic_last_attempt,
286                                                     imp_conn->oic_last_attempt))
287                                 imp_conn = conn;
288                 }
289         }
290
291         /* if not found, simply choose the current one */
292         if (!imp_conn) {
293                 LASSERT(imp->imp_conn_current);
294                 imp_conn = imp->imp_conn_current;
295         }
296         LASSERT(imp_conn->oic_conn);
297
298         imp_conn->oic_last_attempt = cfs_time_current_64();
299
300         /* switch connection, don't mind if it's same as the current one */
301         if (imp->imp_connection)
302                 ptlrpc_put_connection(imp->imp_connection);
303         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
304
305         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
306         LASSERT(dlmexp != NULL);
307         if (dlmexp->exp_connection)
308                 ptlrpc_put_connection(dlmexp->exp_connection);
309         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
310         class_export_put(dlmexp);
311
312         if (imp->imp_conn_current != imp_conn) {
313                 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
314                               imp->imp_obd->obd_name, imp_conn->oic_uuid.uuid,
315                               libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
316         imp->imp_conn_current = imp_conn;
317         }
318
319         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
320                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
321                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
322
323         spin_unlock(&imp->imp_lock);
324
325         RETURN(0);
326 }
327
328 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
329 {
330         struct obd_device *obd = imp->imp_obd;
331         int initial_connect = 0;
332         int rc;
333         __u64 committed_before_reconnect = 0;
334         struct ptlrpc_request *request;
335         int size[] = { sizeof(struct ptlrpc_body),
336                        sizeof(imp->imp_obd->u.cli.cl_target_uuid),
337                        sizeof(obd->obd_uuid),
338                        sizeof(imp->imp_dlm_handle),
339                        sizeof(imp->imp_connect_data) };
340         char *tmp[] = { NULL,
341                         obd2cli_tgt(imp->imp_obd),
342                         obd->obd_uuid.uuid,
343                         (char *)&imp->imp_dlm_handle,
344                         (char *)&imp->imp_connect_data };
345         struct ptlrpc_connect_async_args *aa;
346
347         ENTRY;
348         spin_lock(&imp->imp_lock);
349         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
350                 spin_unlock(&imp->imp_lock);
351                 CERROR("can't connect to a closed import\n");
352                 RETURN(-EINVAL);
353         } else if (imp->imp_state == LUSTRE_IMP_FULL &&
354                    imp->imp_force_reconnect == 0) {
355                 spin_unlock(&imp->imp_lock);
356                 CERROR("already connected\n");
357                 RETURN(0);
358         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
359                 spin_unlock(&imp->imp_lock);
360                 CERROR("already connecting\n");
361                 RETURN(-EALREADY);
362         }
363
364         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
365
366         imp->imp_conn_cnt++;
367         imp->imp_resend_replay = 0;
368
369         if (!lustre_handle_is_used(&imp->imp_remote_handle))
370                 initial_connect = 1;
371         else
372                 committed_before_reconnect = imp->imp_peer_committed_transno;
373
374         spin_unlock(&imp->imp_lock);
375
376         if (new_uuid) {
377                 struct obd_uuid uuid;
378
379                 obd_str2uuid(&uuid, new_uuid);
380                 rc = import_set_conn_priority(imp, &uuid);
381                 if (rc)
382                         GOTO(out, rc);
383         }
384
385         rc = import_select_connection(imp);
386         if (rc)
387                 GOTO(out, rc);
388
389         /* last in connection list */
390         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
391                 if (imp->imp_initial_recov_bk && initial_connect) {
392                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
393                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
394                         /* Don't retry if connect fails */
395                         rc = 0;
396                         obd_set_info_async(obd->obd_self_export,
397                                            strlen(KEY_INIT_RECOV),
398                                            KEY_INIT_RECOV,
399                                            sizeof(rc), &rc, NULL);
400                 }
401                 if (imp->imp_recon_bk) {
402                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
403                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
404                         imp->imp_last_recon = 1;
405                 }
406         }
407
408         /* Reset connect flags to the originally requested flags, in case
409          * the server is updated on-the-fly we will get the new features. */
410         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
411         rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
412                            &obd->obd_uuid, &imp->imp_connect_data);
413         if (rc)
414                 GOTO(out, rc);
415
416         request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
417                                   5, size, tmp);
418         if (!request)
419                 GOTO(out, rc = -ENOMEM);
420
421 #ifndef __KERNEL__
422         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
423 #endif
424         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
425
426         request->rq_send_state = LUSTRE_IMP_CONNECTING;
427         /* Allow a slightly larger reply for future growth compatibility */
428         size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
429                               16 * sizeof(__u64);
430         ptlrpc_req_set_repsize(request, 2, size);
431         request->rq_interpret_reply = ptlrpc_connect_interpret;
432
433         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
434         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
435         memset(aa, 0, sizeof *aa);
436
437         aa->pcaa_peer_committed = committed_before_reconnect;
438         aa->pcaa_initial_connect = initial_connect;
439
440         if (aa->pcaa_initial_connect) {
441                 imp->imp_replayable = 1;
442                 /* On an initial connect, we don't know which one of a
443                    failover server pair is up.  Don't wait long. */
444                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
445         }
446
447         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
448         ptlrpcd_add_req(request);
449         rc = 0;
450 out:
451         if (rc != 0) {
452                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
453         }
454
455         RETURN(rc);
456 }
457 EXPORT_SYMBOL(ptlrpc_connect_import);
458
459 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
460 {
461         struct obd_import_conn *imp_conn;
462         int wake_pinger = 0;
463
464         ENTRY;
465
466         spin_lock(&imp->imp_lock);
467         if (list_empty(&imp->imp_conn_list))
468                 GOTO(unlock, 0);
469
470         imp_conn = list_entry(imp->imp_conn_list.prev,
471                               struct obd_import_conn,
472                               oic_item);
473
474         if (imp->imp_conn_current != imp_conn) {
475                 ptlrpc_ping_import_soon(imp);
476                 wake_pinger = 1;
477         }
478
479  unlock:
480         spin_unlock(&imp->imp_lock);
481
482         if (wake_pinger)
483                 ptlrpc_pinger_wake_up();
484
485         EXIT;
486 }
487
488 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
489                                     void * data, int rc)
490 {
491         struct ptlrpc_connect_async_args *aa = data;
492         struct obd_import *imp = request->rq_import;
493         struct client_obd *cli = &imp->imp_obd->u.cli;
494         struct lustre_handle old_hdl;
495         int msg_flags;
496         ENTRY;
497
498         spin_lock(&imp->imp_lock);
499         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
500                 spin_unlock(&imp->imp_lock);
501                 RETURN(0);
502         }
503         imp->imp_force_reconnect = 0;
504         spin_unlock(&imp->imp_lock);
505
506         if (rc)
507                 GOTO(out, rc);
508
509         rc = sptlrpc_cli_install_rvs_ctx(imp, request->rq_cli_ctx);
510         if (rc)
511                 GOTO(out, rc);
512
513         LASSERT(imp->imp_conn_current);
514
515         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
516
517         /* All imports are pingable */
518         imp->imp_pingable = 1;
519
520         if (aa->pcaa_initial_connect) {
521                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
522                         CDEBUG(D_HA, "connected to replayable target: %s\n",
523                                obd2cli_tgt(imp->imp_obd));
524                         imp->imp_replayable = 1;
525                 } else {
526                         imp->imp_replayable = 0;
527                 }
528
529                 if (msg_flags & MSG_CONNECT_NEXT_VER) {
530                         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
531                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
532                                obd2cli_tgt(imp->imp_obd));
533                 } else {
534                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
535                                obd2cli_tgt(imp->imp_obd));
536                 }
537
538                 imp->imp_remote_handle =
539                                 *lustre_msg_get_handle(request->rq_repmsg);
540
541                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
542                 GOTO(finish, rc = 0);
543         }
544
545         /* Determine what recovery state to move the import to. */
546         if (MSG_CONNECT_RECONNECT & msg_flags) {
547                 memset(&old_hdl, 0, sizeof(old_hdl));
548                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
549                             sizeof (old_hdl))) {
550                         CERROR("%s@%s didn't like our handle "LPX64
551                                ", failed\n", obd2cli_tgt(imp->imp_obd),
552                                imp->imp_connection->c_remote_uuid.uuid,
553                                imp->imp_dlm_handle.cookie);
554                         GOTO(out, rc = -ENOTCONN);
555                 }
556
557                 if (memcmp(&imp->imp_remote_handle,
558                            lustre_msg_get_handle(request->rq_repmsg),
559                            sizeof(imp->imp_remote_handle))) {
560                         int level = D_ERROR;
561                         /* The MGC does this all the time */
562                         if (strcmp(imp->imp_obd->obd_type->typ_name,
563                                    LUSTRE_MGC_NAME) == 0) {
564                                 level = D_CONFIG;
565                         }
566                         CDEBUG(level, 
567                                "%s@%s changed handle from "LPX64" to "LPX64
568                                "; copying, but this may foreshadow disaster\n",
569                                obd2cli_tgt(imp->imp_obd),
570                                imp->imp_connection->c_remote_uuid.uuid,
571                                imp->imp_remote_handle.cookie,
572                                lustre_msg_get_handle(request->rq_repmsg)->
573                                         cookie);
574                         imp->imp_remote_handle =
575                                      *lustre_msg_get_handle(request->rq_repmsg);
576                 } else {
577                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
578                                obd2cli_tgt(imp->imp_obd),
579                                imp->imp_connection->c_remote_uuid.uuid);
580                 }
581
582                 if (imp->imp_invalid) {
583                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
584                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
585                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
586                                imp->imp_obd->obd_name,
587                                obd2cli_tgt(imp->imp_obd));
588                         imp->imp_resend_replay = 1;
589                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
590                 } else {
591                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
592                 }
593         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
594                 LASSERT(imp->imp_replayable);
595                 imp->imp_remote_handle =
596                                 *lustre_msg_get_handle(request->rq_repmsg);
597                 imp->imp_last_replay_transno = 0;
598                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
599         } else {
600                 imp->imp_remote_handle =
601                                 *lustre_msg_get_handle(request->rq_repmsg);
602                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
603         }
604
605         /* Sanity checks for a reconnected import. */
606         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
607                 CERROR("imp_replayable flag does not match server "
608                        "after reconnect. We should LBUG right here.\n");
609         }
610
611         if (lustre_msg_get_last_committed(request->rq_repmsg) <
612             aa->pcaa_peer_committed) {
613                 CERROR("%s went back in time (transno "LPD64
614                        " was previously committed, server now claims "LPD64
615                        ")!  See https://bugzilla.clusterfs.com/"
616                        "long_list.cgi?buglist=9646\n",
617                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
618                        lustre_msg_get_last_committed(request->rq_repmsg));
619         }
620
621 finish:
622         rc = ptlrpc_import_recovery_state_machine(imp);
623         if (rc != 0) {
624                 if (rc == -ENOTCONN) {
625                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
626                                "invalidating and reconnecting\n",
627                                obd2cli_tgt(imp->imp_obd),
628                                imp->imp_connection->c_remote_uuid.uuid);
629                         ptlrpc_connect_import(imp, NULL);
630                         RETURN(0);
631                 }
632         } else {
633                 struct obd_connect_data *ocd;
634                 struct obd_export *exp;
635
636                 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
637                                          lustre_swab_connect);
638
639                 spin_lock(&imp->imp_lock);
640                 list_del(&imp->imp_conn_current->oic_item);
641                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
642                 imp->imp_last_success_conn =
643                         imp->imp_conn_current->oic_last_attempt;
644
645                 if (ocd == NULL) {
646                         spin_unlock(&imp->imp_lock);
647                         CERROR("Wrong connect data from server\n");
648                         rc = -EPROTO;
649                         GOTO(out, rc);
650                 }
651
652                 imp->imp_connect_data = *ocd;
653
654                 exp = class_conn2export(&imp->imp_dlm_handle);
655                 spin_unlock(&imp->imp_lock);
656
657                 /* check that server granted subset of flags we asked for. */
658                 LASSERTF((ocd->ocd_connect_flags &
659                           imp->imp_connect_flags_orig) ==
660                          ocd->ocd_connect_flags, LPX64" != "LPX64,
661                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
662
663                 if (!exp) {
664                         /* This could happen if export is cleaned during the 
665                            connect attempt */
666                         spin_unlock(&imp->imp_lock);
667                         CERROR("Missing export for %s\n", 
668                                imp->imp_obd->obd_name);
669                         GOTO(out, rc = -ENODEV);
670                 }
671                 exp->exp_connect_flags = ocd->ocd_connect_flags;
672                 class_export_put(exp);
673
674                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
675
676                 if (!ocd->ocd_ibits_known &&
677                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
678                         CERROR("Inodebits aware server returned zero compatible"
679                                " bits?\n");
680
681                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
682                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
683                     LUSTRE_VERSION_OFFSET_WARN)) {
684                         /* Sigh, some compilers do not like #ifdef in the middle
685                            of macro arguments */
686 #ifdef __KERNEL__
687                         const char *action = "upgrading this client";
688 #else
689                         const char *action = "recompiling this application";
690 #endif
691
692                         CWARN("Server %s version (%d.%d.%d.%d) is much newer. "
693                               "Consider %s (%s).\n",
694                               obd2cli_tgt(imp->imp_obd),
695                               OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
696                               OBD_OCD_VERSION_MINOR(ocd->ocd_version),
697                               OBD_OCD_VERSION_PATCH(ocd->ocd_version),
698                               OBD_OCD_VERSION_FIX(ocd->ocd_version),
699                               action, LUSTRE_VERSION_STRING);
700                 }
701
702                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
703                         cli->cl_max_pages_per_rpc = ocd->ocd_brw_size >> PAGE_SHIFT;
704                 } else {
705                         cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
706                                                         (int)(1024 * 1024 >> PAGE_SHIFT));
707                 }
708
709                 LASSERT(cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES &&
710                         cli->cl_max_pages_per_rpc > 0);
711
712         }
713
714  out:
715         if (rc != 0) {
716                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
717                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
718                         ptlrpc_deactivate_import(imp);
719
720                 if (imp->imp_recon_bk && imp->imp_last_recon) {
721                         /* Give up trying to reconnect */
722                         imp->imp_obd->obd_no_recov = 1;
723                         ptlrpc_deactivate_import(imp);
724                 }
725
726                 if (rc == -EPROTO) {
727                         struct obd_connect_data *ocd;
728
729                         /* reply message might not be ready */
730                         if (request->rq_repmsg != NULL)
731                                 RETURN(-EPROTO);
732
733                         ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
734                                                  sizeof *ocd,
735                                                  lustre_swab_connect);
736                         if (ocd &&
737                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
738                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
739                            /* Actually servers are only supposed to refuse
740                               connection from liblustre clients, so we should
741                               never see this from VFS context */
742                                 CERROR("Server %s version (%d.%d.%d.%d) "
743                                        "refused connection from this client "
744                                        "as too old version (%s).  Client must "
745                                        "be recompiled\n",
746                                       obd2cli_tgt(imp->imp_obd),
747                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
748                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
749                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
750                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
751                                       LUSTRE_VERSION_STRING);
752                                 ptlrpc_deactivate_import(imp);
753                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
754                         }
755                         RETURN(-EPROTO);
756                 }
757
758                 ptlrpc_maybe_ping_import_soon(imp);
759
760                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
761                        obd2cli_tgt(imp->imp_obd),
762                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
763         }
764         
765         imp->imp_last_recon = 0;
766
767         cfs_waitq_signal(&imp->imp_recovery_waitq);
768         RETURN(rc);
769 }
770
771 static int completed_replay_interpret(struct ptlrpc_request *req,
772                                     void * data, int rc)
773 {
774         ENTRY;
775         atomic_dec(&req->rq_import->imp_replay_inflight);
776         if (req->rq_status == 0) {
777                 ptlrpc_import_recovery_state_machine(req->rq_import);
778         } else {
779                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
780                        "reconnecting\n",
781                        req->rq_import->imp_obd->obd_name, req->rq_status);
782                 ptlrpc_connect_import(req->rq_import, NULL);
783         }
784
785         RETURN(0);
786 }
787
788 static int signal_completed_replay(struct obd_import *imp)
789 {
790         struct ptlrpc_request *req;
791         ENTRY;
792
793         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
794         atomic_inc(&imp->imp_replay_inflight);
795
796         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
797         if (!req) {
798                 atomic_dec(&imp->imp_replay_inflight);
799                 RETURN(-ENOMEM);
800         }
801
802         ptlrpc_req_set_repsize(req, 1, NULL);
803         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
804         lustre_msg_add_flags(req->rq_reqmsg, MSG_LAST_REPLAY);
805         req->rq_timeout *= 3;
806         req->rq_interpret_reply = completed_replay_interpret;
807
808         ptlrpcd_add_req(req);
809         RETURN(0);
810 }
811
812 #ifdef __KERNEL__
813 static int ptlrpc_invalidate_import_thread(void *data)
814 {
815         struct obd_import *imp = data;
816
817         ENTRY;
818
819         ptlrpc_daemonize("ll_imp_inval");
820         
821         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
822                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
823                imp->imp_connection->c_remote_uuid.uuid);
824
825         ptlrpc_invalidate_import(imp);
826
827         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
828         ptlrpc_import_recovery_state_machine(imp);
829
830         RETURN(0);
831 }
832 #endif
833
834 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
835 {
836         int rc = 0;
837         int inflight;
838         char *target_start;
839         int target_len;
840
841         ENTRY;
842         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
843                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
844                           &target_start, &target_len);
845                 LCONSOLE_ERROR("This client was evicted by %.*s; in progress "
846                                "operations using this service will fail.\n",
847                                target_len, target_start);
848                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
849                        obd2cli_tgt(imp->imp_obd),
850                        imp->imp_connection->c_remote_uuid.uuid);
851
852 #ifdef __KERNEL__
853                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
854                                    CLONE_VM | CLONE_FILES);
855                 if (rc < 0)
856                         CERROR("error starting invalidate thread: %d\n", rc);
857                 else
858                         rc = 0;
859                 RETURN(rc);
860 #else
861                 ptlrpc_invalidate_import(imp);
862
863                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
864 #endif
865         }
866
867         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
868                 CDEBUG(D_HA, "replay requested by %s\n",
869                        obd2cli_tgt(imp->imp_obd));
870                 rc = ptlrpc_replay_next(imp, &inflight);
871                 if (inflight == 0 &&
872                     atomic_read(&imp->imp_replay_inflight) == 0) {
873                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
874                         rc = ldlm_replay_locks(imp);
875                         if (rc)
876                                 GOTO(out, rc);
877                 }
878                 rc = 0;
879         }
880
881         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
882                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
883                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
884                         rc = signal_completed_replay(imp);
885                         if (rc)
886                                 GOTO(out, rc);
887                 }
888
889         }
890
891         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
892                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
893                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
894                 }
895         }
896
897         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
898                 CDEBUG(D_HA, "reconnected to %s@%s\n",
899                        obd2cli_tgt(imp->imp_obd),
900                        imp->imp_connection->c_remote_uuid.uuid);
901
902                 rc = ptlrpc_resend(imp);
903                 if (rc)
904                         GOTO(out, rc);
905                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
906                 ptlrpc_activate_import(imp);
907
908                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
909                           &target_start, &target_len);
910                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
911                               "using nid %s.\n", imp->imp_obd->obd_name,
912                               target_len, target_start,
913                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
914         }
915
916         if (imp->imp_state == LUSTRE_IMP_FULL) {
917                 cfs_waitq_signal(&imp->imp_recovery_waitq);
918                 ptlrpc_wake_delayed(imp);
919         }
920
921  out:
922         RETURN(rc);
923 }
924
925 static int back_to_sleep(void *unused)
926 {
927         return 0;
928 }
929
930 int ptlrpc_disconnect_import(struct obd_import *imp)
931 {
932         struct ptlrpc_request *req;
933         int rq_opc, rc = 0;
934         ENTRY;
935
936         switch (imp->imp_connect_op) {
937         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
938         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
939         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
940         default:
941                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
942                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
943                 RETURN(-EINVAL);
944         }
945
946         if (ptlrpc_import_in_recovery(imp)) {
947                 struct l_wait_info lwi;
948                 cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
949
950                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
951                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
952                 rc = l_wait_event(imp->imp_recovery_waitq,
953                                   !ptlrpc_import_in_recovery(imp), &lwi);
954
955         }
956
957         spin_lock(&imp->imp_lock);
958         if (imp->imp_state != LUSTRE_IMP_FULL)
959                 GOTO(out, 0);
960
961         spin_unlock(&imp->imp_lock);
962
963         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
964         if (req) {
965                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
966                  * it fails.  We can get through the above with a down server
967                  * if the client doesn't know the server is gone yet. */
968                 req->rq_no_resend = 1;
969                 req->rq_timeout = 5;
970                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
971                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
972                 ptlrpc_req_set_repsize(req, 1, NULL);
973                 rc = ptlrpc_queue_wait(req);
974                 ptlrpc_req_finished(req);
975         }
976
977         spin_lock(&imp->imp_lock);
978 out:
979         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
980         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
981         spin_unlock(&imp->imp_lock);
982
983         RETURN(rc);
984 }
985