Whamcloud - gitweb
cae29ec64016670bb3c487a39910072a8923f26f
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/import.c
37  *
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #ifndef __KERNEL__
43 # include <liblustre.h>
44 #endif
45
46 #include <obd_support.h>
47 #include <lustre_ha.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_export.h>
51 #include <obd.h>
52 #include <obd_cksum.h>
53 #include <obd_class.h>
54
55 #include "ptlrpc_internal.h"
56
57 struct ptlrpc_connect_async_args {
58          __u64 pcaa_peer_committed;
59         int pcaa_initial_connect;
60 };
61
62 /* A CLOSED import should remain so. */
63 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
64 do {                                                                           \
65         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
66                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
67                       imp, obd2cli_tgt(imp->imp_obd),                          \
68                       ptlrpc_import_state_name(imp->imp_state),                \
69                       ptlrpc_import_state_name(state));                        \
70                imp->imp_state = state;                                         \
71         }                                                                      \
72 } while(0)
73
74 #define IMPORT_SET_STATE(imp, state)            \
75 do {                                            \
76         spin_lock(&imp->imp_lock);              \
77         IMPORT_SET_STATE_NOLOCK(imp, state);    \
78         spin_unlock(&imp->imp_lock);            \
79 } while(0)
80
81
82 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
83                                     void * data, int rc);
84 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
85
86 /* Only this function is allowed to change the import state when it is
87  * CLOSED. I would rather refcount the import and free it after
88  * disconnection like we do with exports. To do that, the client_obd
89  * will need to save the peer info somewhere other than in the import,
90  * though. */
91 int ptlrpc_init_import(struct obd_import *imp)
92 {
93         spin_lock(&imp->imp_lock);
94
95         imp->imp_generation++;
96         imp->imp_state =  LUSTRE_IMP_NEW;
97
98         spin_unlock(&imp->imp_lock);
99
100         return 0;
101 }
102 EXPORT_SYMBOL(ptlrpc_init_import);
103
104 #define UUID_STR "_UUID"
105 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
106                       int *uuid_len)
107 {
108         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
109                 ? uuid : uuid + strlen(prefix);
110
111         *uuid_len = strlen(*uuid_start);
112
113         if (*uuid_len < strlen(UUID_STR))
114                 return;
115
116         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
117                     UUID_STR, strlen(UUID_STR)))
118                 *uuid_len -= strlen(UUID_STR);
119 }
120
121 /* Returns true if import was FULL, false if import was already not
122  * connected.
123  * @imp - import to be disconnected
124  * @conn_cnt - connection count (epoch) of the request that timed out
125  *             and caused the disconnection.  In some cases, multiple
126  *             inflight requests can fail to a single target (e.g. OST
127  *             bulk requests) and if one has already caused a reconnection
128  *             (increasing the import->conn_cnt) the older failure should
129  *             not also cause a reconnection.  If zero it forces a reconnect.
130  */
131 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
132 {
133         int rc = 0;
134
135         spin_lock(&imp->imp_lock);
136
137         if (imp->imp_state == LUSTRE_IMP_FULL &&
138             (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
139                 char *target_start;
140                 int   target_len;
141
142                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
143                           &target_start, &target_len);
144
145                 if (imp->imp_replayable) {
146                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
147                                "%s was lost; in progress operations using this "
148                                "service will wait for recovery to complete.\n",
149                                imp->imp_obd->obd_name, target_len, target_start,
150                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
151                 } else {
152                         LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
153                                            "%.*s via nid %s was lost; in progress"
154                                            "operations using this service will"
155                                            "fail.\n",
156                                            imp->imp_obd->obd_name,
157                                            target_len, target_start,
158                                  libcfs_nid2str(imp->imp_connection->c_peer.nid));
159                 }
160                 ptlrpc_deactivate_timeouts(imp);
161                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
162                 spin_unlock(&imp->imp_lock);
163
164                 if (obd_dump_on_timeout)
165                         libcfs_debug_dumplog();
166
167                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
168                 rc = 1;
169         } else {
170                 spin_unlock(&imp->imp_lock);
171                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
172                        imp->imp_client->cli_name, imp,
173                        (imp->imp_state == LUSTRE_IMP_FULL &&
174                         imp->imp_conn_cnt > conn_cnt) ?
175                        "reconnected" : "not connected", imp->imp_conn_cnt,
176                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
177         }
178
179         return rc;
180 }
181
182 /* Must be called with imp_lock held! */
183 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
184 {
185         ENTRY;
186         LASSERT_SPIN_LOCKED(&imp->imp_lock);
187
188         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
189         imp->imp_invalid = 1;
190         imp->imp_generation++;
191         spin_unlock(&imp->imp_lock);
192
193         ptlrpc_abort_inflight(imp);
194         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
195
196         EXIT;
197 }
198
199 /*
200  * This acts as a barrier; all existing requests are rejected, and
201  * no new requests will be accepted until the import is valid again.
202  */
203 void ptlrpc_deactivate_import(struct obd_import *imp)
204 {
205         spin_lock(&imp->imp_lock);
206         ptlrpc_deactivate_and_unlock_import(imp);
207 }
208
209 /*
210  * This function will invalidate the import, if necessary, then block
211  * for all the RPC completions, and finally notify the obd to
212  * invalidate its state (ie cancel locks, clear pending requests,
213  * etc).
214  */
215 void ptlrpc_invalidate_import(struct obd_import *imp)
216 {
217         struct list_head *tmp, *n;
218         struct ptlrpc_request *req;
219         struct l_wait_info lwi;
220         int rc;
221
222         atomic_inc(&imp->imp_inval_count);
223
224         /*
225          * If this is an invalid MGC connection, then don't bother
226          * waiting for imp_inflight to drop to 0.
227          */
228         if (imp->imp_invalid && imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
229                 goto out;
230
231         if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
232                 ptlrpc_deactivate_import(imp);
233
234         LASSERT(imp->imp_invalid);
235
236         /* wait for all requests to error out and call completion callbacks.
237            Cap it at obd_timeout -- these should all have been locally
238            cancelled by ptlrpc_abort_inflight. */
239         lwi = LWI_TIMEOUT_INTERVAL(
240                 cfs_timeout_cap(cfs_time_seconds(obd_timeout)),
241                 cfs_time_seconds(1), NULL, NULL);
242         rc = l_wait_event(imp->imp_recovery_waitq,
243                           (atomic_read(&imp->imp_inflight) == 0), &lwi);
244
245         if (rc) {
246                 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
247                          obd2cli_tgt(imp->imp_obd), rc,
248                          atomic_read(&imp->imp_inflight));
249                 spin_lock(&imp->imp_lock);
250                 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
251                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
252                         DEBUG_REQ(D_ERROR, req, "still on sending list");
253                 }
254                 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
255                         req = list_entry(tmp, struct ptlrpc_request, rq_list);
256                         DEBUG_REQ(D_ERROR, req, "still on delayed list");
257                 }
258                 spin_unlock(&imp->imp_lock);
259                 LASSERT(atomic_read(&imp->imp_inflight) == 0);
260         }
261
262 out:
263         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
264         sptlrpc_import_flush_all_ctx(imp);
265
266         atomic_dec(&imp->imp_inval_count);
267         cfs_waitq_signal(&imp->imp_recovery_waitq);
268 }
269
270 /* unset imp_invalid */
271 void ptlrpc_activate_import(struct obd_import *imp)
272 {
273         struct obd_device *obd = imp->imp_obd;
274
275         spin_lock(&imp->imp_lock);
276         imp->imp_invalid = 0;
277         ptlrpc_activate_timeouts(imp);
278         spin_unlock(&imp->imp_lock);
279         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
280 }
281
282 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
283 {
284         ENTRY;
285
286         LASSERT(!imp->imp_dlm_fake);
287
288         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
289                 if (!imp->imp_replayable) {
290                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
291                                "auto-deactivating\n",
292                                obd2cli_tgt(imp->imp_obd),
293                                imp->imp_connection->c_remote_uuid.uuid,
294                                imp->imp_obd->obd_name);
295                         ptlrpc_deactivate_import(imp);
296                 }
297
298                 CDEBUG(D_HA, "%s: waking up pinger\n",
299                        obd2cli_tgt(imp->imp_obd));
300
301                 spin_lock(&imp->imp_lock);
302                 imp->imp_force_verify = 1;
303                 spin_unlock(&imp->imp_lock);
304
305                 ptlrpc_pinger_wake_up();
306         }
307         EXIT;
308 }
309
310 int ptlrpc_reconnect_import(struct obd_import *imp)
311 {
312         ptlrpc_set_import_discon(imp, 0);
313         /* Force a new connect attempt */
314         ptlrpc_invalidate_import(imp);
315         /* Do a fresh connect next time by zeroing the handle */
316         ptlrpc_disconnect_import(imp, 1);
317         /* Wait for all invalidate calls to finish */
318         if (atomic_read(&imp->imp_inval_count) > 0) {
319                 int rc;
320                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
321                 rc = l_wait_event(imp->imp_recovery_waitq,
322                                   (atomic_read(&imp->imp_inval_count) == 0),
323                                   &lwi);
324                 if (rc)
325                         CERROR("Interrupted, inval=%d\n",
326                                atomic_read(&imp->imp_inval_count));
327         }
328
329         /* Allow reconnect attempts */
330         imp->imp_obd->obd_no_recov = 0;
331         /* Remove 'invalid' flag */
332         ptlrpc_activate_import(imp);
333         /* Attempt a new connect */
334         ptlrpc_recover_import(imp, NULL);
335         return 0;
336 }
337
338 EXPORT_SYMBOL(ptlrpc_reconnect_import);
339
340 static int import_select_connection(struct obd_import *imp)
341 {
342         struct obd_import_conn *imp_conn = NULL, *conn;
343         struct obd_export *dlmexp;
344         int tried_all = 1;
345         ENTRY;
346
347         spin_lock(&imp->imp_lock);
348
349         if (list_empty(&imp->imp_conn_list)) {
350                 CERROR("%s: no connections available\n",
351                         imp->imp_obd->obd_name);
352                 spin_unlock(&imp->imp_lock);
353                 RETURN(-EINVAL);
354         }
355
356         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
357                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
358                        imp->imp_obd->obd_name,
359                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
360                        conn->oic_last_attempt);
361                 /* Don't thrash connections */
362                 if (cfs_time_before_64(cfs_time_current_64(),
363                                      conn->oic_last_attempt +
364                                      cfs_time_seconds(CONNECTION_SWITCH_MIN))) {
365                         continue;
366                 }
367
368                 /* If we have not tried this connection since the
369                    the last successful attempt, go with this one */
370                 if ((conn->oic_last_attempt == 0) ||
371                     cfs_time_beforeq_64(conn->oic_last_attempt,
372                                        imp->imp_last_success_conn)) {
373                         imp_conn = conn;
374                         tried_all = 0;
375                         break;
376                 }
377
378                 /* If all of the connections have already been tried
379                    since the last successful connection; just choose the
380                    least recently used */
381                 if (!imp_conn)
382                         imp_conn = conn;
383                 else if (cfs_time_before_64(conn->oic_last_attempt,
384                                             imp_conn->oic_last_attempt))
385                         imp_conn = conn;
386         }
387
388         /* if not found, simply choose the current one */
389         if (!imp_conn) {
390                 LASSERT(imp->imp_conn_current);
391                 imp_conn = imp->imp_conn_current;
392                 tried_all = 0;
393         }
394         LASSERT(imp_conn->oic_conn);
395
396         /* If we've tried everything, and we're back to the beginning of the
397            list, increase our timeout and try again. It will be reset when
398            we do finally connect. (FIXME: really we should wait for all network
399            state associated with the last connection attempt to drain before
400            trying to reconnect on it.) */
401         if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item) &&
402             !imp->imp_recon_bk /* not retrying */) {
403                 if (at_get(&imp->imp_at.iat_net_latency) <
404                     CONNECTION_SWITCH_MAX) {
405                         at_add(&imp->imp_at.iat_net_latency,
406                                at_get(&imp->imp_at.iat_net_latency) +
407                                CONNECTION_SWITCH_INC);
408                 }
409                 LASSERT(imp_conn->oic_last_attempt);
410                 CWARN("%s: tried all connections, increasing latency to %ds\n",
411                       imp->imp_obd->obd_name,
412                       at_get(&imp->imp_at.iat_net_latency));
413         }
414
415         imp_conn->oic_last_attempt = cfs_time_current_64();
416
417         /* switch connection, don't mind if it's same as the current one */
418         if (imp->imp_connection)
419                 ptlrpc_connection_put(imp->imp_connection);
420         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
421
422         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
423         LASSERT(dlmexp != NULL);
424         if (dlmexp->exp_connection)
425                 ptlrpc_connection_put(dlmexp->exp_connection);
426         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
427         class_export_put(dlmexp);
428
429         if (imp->imp_conn_current != imp_conn) {
430                 if (imp->imp_conn_current)
431                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
432                                       imp->imp_obd->obd_name,
433                                       imp_conn->oic_uuid.uuid,
434                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
435                 imp->imp_conn_current = imp_conn;
436         }
437
438         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
439                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
440                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
441
442         spin_unlock(&imp->imp_lock);
443
444         RETURN(0);
445 }
446
447 /*
448  * must be called under imp_lock
449  */
450 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
451 {
452         struct ptlrpc_request *req;
453         struct list_head *tmp;
454
455         if (list_empty(&imp->imp_replay_list))
456                 return 0;
457         tmp = imp->imp_replay_list.next;
458         req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
459         *transno = req->rq_transno;
460         if (req->rq_transno == 0) {
461                 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
462                 LBUG();
463         }
464
465         return 1;
466 }
467
468 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
469 {
470         struct obd_device *obd = imp->imp_obd;
471         int initial_connect = 0;
472         int set_transno = 0;
473         __u64 committed_before_reconnect = 0;
474         struct ptlrpc_request *request;
475         char *bufs[] = { NULL,
476                          obd2cli_tgt(imp->imp_obd),
477                          obd->obd_uuid.uuid,
478                          (char *)&imp->imp_dlm_handle,
479                          (char *)&imp->imp_connect_data };
480         struct ptlrpc_connect_async_args *aa;
481         int rc;
482         ENTRY;
483
484         spin_lock(&imp->imp_lock);
485         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
486                 spin_unlock(&imp->imp_lock);
487                 CERROR("can't connect to a closed import\n");
488                 RETURN(-EINVAL);
489         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
490                 spin_unlock(&imp->imp_lock);
491                 CERROR("already connected\n");
492                 RETURN(0);
493         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
494                 spin_unlock(&imp->imp_lock);
495                 CERROR("already connecting\n");
496                 RETURN(-EALREADY);
497         }
498
499         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
500
501         imp->imp_conn_cnt++;
502         imp->imp_resend_replay = 0;
503
504         if (!lustre_handle_is_used(&imp->imp_remote_handle))
505                 initial_connect = 1;
506         else
507                 committed_before_reconnect = imp->imp_peer_committed_transno;
508
509         set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
510         spin_unlock(&imp->imp_lock);
511
512         if (new_uuid) {
513                 struct obd_uuid uuid;
514
515                 obd_str2uuid(&uuid, new_uuid);
516                 rc = import_set_conn_priority(imp, &uuid);
517                 if (rc)
518                         GOTO(out, rc);
519         }
520
521         rc = import_select_connection(imp);
522         if (rc)
523                 GOTO(out, rc);
524
525         /* last in connection list */
526         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
527                 if (imp->imp_initial_recov_bk && initial_connect) {
528                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
529                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
530                         /* Don't retry if connect fails */
531                         rc = 0;
532                         obd_set_info_async(obd->obd_self_export,
533                                            sizeof(KEY_INIT_RECOV),
534                                            KEY_INIT_RECOV,
535                                            sizeof(rc), &rc, NULL);
536                 }
537                 if (imp->imp_recon_bk) {
538                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
539                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
540                         spin_lock(&imp->imp_lock);
541                         imp->imp_last_recon = 1;
542                         spin_unlock(&imp->imp_lock);
543                 }
544         }
545
546         rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
547         if (rc)
548                 GOTO(out, rc);
549
550         /* Reset connect flags to the originally requested flags, in case
551          * the server is updated on-the-fly we will get the new features. */
552         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
553         imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
554
555         rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
556                            &obd->obd_uuid, &imp->imp_connect_data);
557         if (rc)
558                 GOTO(out, rc);
559
560         request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
561         if (request == NULL)
562                 GOTO(out, rc = -ENOMEM);
563
564         rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
565                                       imp->imp_connect_op, bufs, NULL);
566         if (rc) {
567                 ptlrpc_request_free(request);
568                 GOTO(out, rc);
569         }
570
571 #ifndef __KERNEL__
572         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
573 #endif
574         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
575
576         request->rq_send_state = LUSTRE_IMP_CONNECTING;
577         /* Allow a slightly larger reply for future growth compatibility */
578         req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
579                              sizeof(struct obd_connect_data)+16*sizeof(__u64));
580         ptlrpc_request_set_replen(request);
581         request->rq_interpret_reply = ptlrpc_connect_interpret;
582
583         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
584         aa = ptlrpc_req_async_args(request);
585         memset(aa, 0, sizeof *aa);
586
587         aa->pcaa_peer_committed = committed_before_reconnect;
588         aa->pcaa_initial_connect = initial_connect;
589
590         if (aa->pcaa_initial_connect) {
591                 spin_lock(&imp->imp_lock);
592                 imp->imp_replayable = 1;
593                 spin_unlock(&imp->imp_lock);
594                 lustre_msg_add_op_flags(request->rq_reqmsg,
595                                         MSG_CONNECT_INITIAL);
596                 if (AT_OFF)
597                         /* AT will use INITIAL_CONNECT_TIMEOUT the first
598                            time, adaptive after that. */
599                         request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
600         }
601
602         if (set_transno)
603                 lustre_msg_add_op_flags(request->rq_reqmsg,
604                                         MSG_CONNECT_TRANSNO);
605
606         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
607         ptlrpcd_add_req(request);
608         rc = 0;
609 out:
610         if (rc != 0) {
611                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
612         }
613
614         RETURN(rc);
615 }
616 EXPORT_SYMBOL(ptlrpc_connect_import);
617
618 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
619 {
620 #ifdef __KERNEL__
621         struct obd_import_conn *imp_conn;
622 #endif
623         int wake_pinger = 0;
624
625         ENTRY;
626
627         spin_lock(&imp->imp_lock);
628         if (list_empty(&imp->imp_conn_list))
629                 GOTO(unlock, 0);
630
631 #ifdef __KERNEL__
632         imp_conn = list_entry(imp->imp_conn_list.prev,
633                               struct obd_import_conn,
634                               oic_item);
635
636         /* XXX: When the failover node is the primary node, it is possible
637          * to have two identical connections in imp_conn_list. We must
638          * compare not conn's pointers but NIDs, otherwise we can defeat
639          * connection throttling. (See bug 14774.) */
640         if (imp->imp_conn_current->oic_conn->c_self !=
641                                 imp_conn->oic_conn->c_self) {
642                 ptlrpc_ping_import_soon(imp);
643                 wake_pinger = 1;
644         }
645 #else
646         /* liblustre has no pinger thead, so we wakup pinger anyway */
647         wake_pinger = 1;
648 #endif
649
650  unlock:
651         spin_unlock(&imp->imp_lock);
652
653         if (wake_pinger)
654                 ptlrpc_pinger_wake_up();
655
656         EXIT;
657 }
658
659 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
660                                     void * data, int rc)
661 {
662         struct ptlrpc_connect_async_args *aa = data;
663         struct obd_import *imp = request->rq_import;
664         struct client_obd *cli = &imp->imp_obd->u.cli;
665         struct lustre_handle old_hdl;
666         int msg_flags;
667         ENTRY;
668
669         spin_lock(&imp->imp_lock);
670         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
671                 spin_unlock(&imp->imp_lock);
672                 RETURN(0);
673         }
674         spin_unlock(&imp->imp_lock);
675
676         if (rc)
677                 GOTO(out, rc);
678
679         LASSERT(imp->imp_conn_current);
680
681         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
682
683         /* All imports are pingable */
684         spin_lock(&imp->imp_lock);
685         imp->imp_pingable = 1;
686
687         if (aa->pcaa_initial_connect) {
688                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
689                         imp->imp_replayable = 1;
690                         spin_unlock(&imp->imp_lock);
691                         CDEBUG(D_HA, "connected to replayable target: %s\n",
692                                obd2cli_tgt(imp->imp_obd));
693                 } else {
694                         imp->imp_replayable = 0;
695                         spin_unlock(&imp->imp_lock);
696                 }
697
698                 /* if applies, adjust the imp->imp_msg_magic here
699                  * according to reply flags */
700
701                 imp->imp_remote_handle =
702                                 *lustre_msg_get_handle(request->rq_repmsg);
703
704                 /* Initial connects are allowed for clients with non-random
705                  * uuids when servers are in recovery.  Simply signal the
706                  * servers replay is complete and wait in REPLAY_WAIT. */
707                 if (msg_flags & MSG_CONNECT_RECOVERING) {
708                         CDEBUG(D_HA, "connect to %s during recovery\n",
709                                obd2cli_tgt(imp->imp_obd));
710                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
711                 } else {
712                         IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
713                 }
714
715                 spin_lock(&imp->imp_lock);
716                 if (imp->imp_invalid) {
717                         spin_unlock(&imp->imp_lock);
718                         ptlrpc_activate_import(imp);
719                 } else {
720                         spin_unlock(&imp->imp_lock);
721                 }
722
723                 GOTO(finish, rc = 0);
724         } else {
725                 spin_unlock(&imp->imp_lock);
726         }
727
728         /* Determine what recovery state to move the import to. */
729         if (MSG_CONNECT_RECONNECT & msg_flags) {
730                 memset(&old_hdl, 0, sizeof(old_hdl));
731                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
732                             sizeof (old_hdl))) {
733                         CERROR("%s@%s didn't like our handle "LPX64
734                                ", failed\n", obd2cli_tgt(imp->imp_obd),
735                                imp->imp_connection->c_remote_uuid.uuid,
736                                imp->imp_dlm_handle.cookie);
737                         GOTO(out, rc = -ENOTCONN);
738                 }
739
740                 if (memcmp(&imp->imp_remote_handle,
741                            lustre_msg_get_handle(request->rq_repmsg),
742                            sizeof(imp->imp_remote_handle))) {
743                         int level = msg_flags & MSG_CONNECT_RECOVERING ? D_HA :
744                                                                          D_WARNING;
745
746                         /* Bug 16611/14775: if server handle have changed,
747                          * that means some sort of disconnection happened.
748                          * If the server is not in recovery, that also means it
749                          * already erased all of our state because of previous
750                          * eviction. If it is in recovery - we are safe to
751                          * participate since we can reestablish all of our state
752                          * with server again */
753                         CDEBUG(level,"%s@%s changed server handle from "
754                                      LPX64" to "LPX64"%s \n" "but is still in recovery \n",
755                                      obd2cli_tgt(imp->imp_obd),
756                                      imp->imp_connection->c_remote_uuid.uuid,
757                                      imp->imp_remote_handle.cookie,
758                                      lustre_msg_get_handle(request->rq_repmsg)->
759                                                                         cookie,
760                                      (MSG_CONNECT_RECOVERING & msg_flags) ?
761                                          "but is still in recovery" : "");
762
763                         imp->imp_remote_handle =
764                                      *lustre_msg_get_handle(request->rq_repmsg);
765
766                         if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
767                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
768                                 GOTO(finish, rc = 0);
769                         }
770
771                 } else {
772                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
773                                obd2cli_tgt(imp->imp_obd),
774                                imp->imp_connection->c_remote_uuid.uuid);
775                 }
776
777                 if (imp->imp_invalid) {
778                         CDEBUG(D_HA, "%s: reconnected but import is invalid; "
779                                "marking evicted\n", imp->imp_obd->obd_name);
780                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
781                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
782                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
783                                imp->imp_obd->obd_name,
784                                obd2cli_tgt(imp->imp_obd));
785
786                         spin_lock(&imp->imp_lock);
787                         imp->imp_resend_replay = 1;
788                         spin_unlock(&imp->imp_lock);
789
790                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
791                 } else {
792                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
793                 }
794         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
795                 LASSERT(imp->imp_replayable);
796                 imp->imp_remote_handle =
797                                 *lustre_msg_get_handle(request->rq_repmsg);
798                 imp->imp_last_replay_transno = 0;
799                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
800         } else {
801                 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
802                           " not set: %x)", imp->imp_obd->obd_name, msg_flags);
803                 imp->imp_remote_handle =
804                                 *lustre_msg_get_handle(request->rq_repmsg);
805                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
806         }
807
808         /* Sanity checks for a reconnected import. */
809         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
810                 CERROR("imp_replayable flag does not match server "
811                        "after reconnect. We should LBUG right here.\n");
812         }
813
814         if (lustre_msg_get_last_committed(request->rq_repmsg) <
815             aa->pcaa_peer_committed) {
816                 CERROR("%s went back in time (transno "LPD64
817                        " was previously committed, server now claims "LPD64
818                        ")!  See https://bugzilla.lustre.org/show_bug.cgi?"
819                        "id=9646\n",
820                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
821                        lustre_msg_get_last_committed(request->rq_repmsg));
822         }
823
824 finish:
825         rc = ptlrpc_import_recovery_state_machine(imp);
826         if (rc != 0) {
827                 if (rc == -ENOTCONN) {
828                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
829                                "invalidating and reconnecting\n",
830                                obd2cli_tgt(imp->imp_obd),
831                                imp->imp_connection->c_remote_uuid.uuid);
832                         ptlrpc_connect_import(imp, NULL);
833                         RETURN(0);
834                 }
835         } else {
836                 struct obd_connect_data *ocd;
837                 struct obd_export *exp;
838                 int ret;
839                 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
840                                            RCL_SERVER);
841                 /* server replied obd_connect_data is always bigger */
842                 ocd = req_capsule_server_sized_get(&request->rq_pill,
843                                                    &RMF_CONNECT_DATA, ret);
844
845                 spin_lock(&imp->imp_lock);
846                 list_del(&imp->imp_conn_current->oic_item);
847                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
848                 imp->imp_last_success_conn =
849                         imp->imp_conn_current->oic_last_attempt;
850
851                 if (ocd == NULL) {
852                         spin_unlock(&imp->imp_lock);
853                         CERROR("Wrong connect data from server\n");
854                         rc = -EPROTO;
855                         GOTO(out, rc);
856                 }
857
858                 imp->imp_connect_data = *ocd;
859
860                 exp = class_conn2export(&imp->imp_dlm_handle);
861                 spin_unlock(&imp->imp_lock);
862
863                 /* check that server granted subset of flags we asked for. */
864                 LASSERTF((ocd->ocd_connect_flags &
865                           imp->imp_connect_flags_orig) ==
866                          ocd->ocd_connect_flags, LPX64" != "LPX64,
867                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
868
869                 if (!exp) {
870                         /* This could happen if export is cleaned during the
871                            connect attempt */
872                         CERROR("Missing export for %s\n",
873                                imp->imp_obd->obd_name);
874                         GOTO(out, rc = -ENODEV);
875                 }
876                 exp->exp_connect_flags = ocd->ocd_connect_flags;
877                 imp->imp_obd->obd_self_export->exp_connect_flags =
878                                                         ocd->ocd_connect_flags;
879                 class_export_put(exp);
880
881                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
882
883                 if (!ocd->ocd_ibits_known &&
884                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
885                         CERROR("Inodebits aware server returned zero compatible"
886                                " bits?\n");
887
888                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
889                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
890                                         LUSTRE_VERSION_OFFSET_WARN ||
891                      ocd->ocd_version < LUSTRE_VERSION_CODE -
892                                         LUSTRE_VERSION_OFFSET_WARN)) {
893                         /* Sigh, some compilers do not like #ifdef in the middle
894                            of macro arguments */
895 #ifdef __KERNEL__
896                         const char *older =
897                                 "older. Consider upgrading this client";
898 #else
899                         const char *older =
900                                 "older. Consider recompiling this application";
901 #endif
902                         const char *newer = "newer than client version";
903
904                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
905                                       "is much %s (%s)\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                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
912                                       newer : older, LUSTRE_VERSION_STRING);
913                 }
914
915                 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
916                         /* We sent to the server ocd_cksum_types with bits set
917                          * for algorithms we understand. The server masked off
918                          * the checksum types it doesn't support */
919                         if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
920                                 LCONSOLE_WARN("The negotiation of the checksum "
921                                               "alogrithm to use with server %s "
922                                               "failed (%x/%x), disabling "
923                                               "checksums\n",
924                                               obd2cli_tgt(imp->imp_obd),
925                                               ocd->ocd_cksum_types,
926                                               OBD_CKSUM_ALL);
927                                 cli->cl_checksum = 0;
928                                 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
929                                 cli->cl_cksum_type = OBD_CKSUM_CRC32;
930                         } else {
931                                 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
932
933                                 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
934                                         cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
935                                 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
936                                         cli->cl_cksum_type = OBD_CKSUM_ADLER;
937                                 else
938                                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
939                         }
940                 } else {
941                         /* The server does not support OBD_CONNECT_CKSUM.
942                          * Enforce CRC32 for backward compatibility*/
943                         cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
944                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
945                 }
946
947                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
948                         cli->cl_max_pages_per_rpc =
949                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
950                 }
951
952                 imp->imp_obd->obd_namespace->ns_connect_flags =
953                                                         ocd->ocd_connect_flags;
954                 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
955                                                         ocd->ocd_connect_flags;
956
957                 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
958                     (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
959                         /* We need a per-message support flag, because
960                            a. we don't know if the incoming connect reply
961                               supports AT or not (in reply_in_callback)
962                               until we unpack it.
963                            b. failovered server means export and flags are gone
964                               (in ptlrpc_send_reply).
965                            Can only be set when we know AT is supported at
966                            both ends */
967                         imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
968                 else
969                         imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
970
971                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
972                         (cli->cl_max_pages_per_rpc > 0));
973         }
974
975 out:
976         if (rc != 0) {
977                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
978                 spin_lock(&imp->imp_lock);
979                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
980                     (request->rq_import_generation == imp->imp_generation))
981                         ptlrpc_deactivate_and_unlock_import(imp);
982                 else
983                         spin_unlock(&imp->imp_lock);
984
985                 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
986                     (rc == -EACCES)) {
987                         /*
988                          * Give up trying to reconnect
989                          * EACCES means client has no permission for connection
990                          */
991                         imp->imp_obd->obd_no_recov = 1;
992                         ptlrpc_deactivate_import(imp);
993                 }
994
995                 if (rc == -EPROTO) {
996                         struct obd_connect_data *ocd;
997
998                         /* reply message might not be ready */
999                         if (request->rq_repmsg == NULL)
1000                                 RETURN(-EPROTO);
1001
1002                         ocd = req_capsule_server_get(&request->rq_pill,
1003                                                      &RMF_CONNECT_DATA);
1004                         if (ocd &&
1005                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1006                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1007                            /* Actually servers are only supposed to refuse
1008                               connection from liblustre clients, so we should
1009                               never see this from VFS context */
1010                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1011                                         "(%d.%d.%d.%d)"
1012                                         " refused connection from this client "
1013                                         "with an incompatible version (%s).  "
1014                                         "Client must be recompiled\n",
1015                                         obd2cli_tgt(imp->imp_obd),
1016                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1017                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1018                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1019                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
1020                                         LUSTRE_VERSION_STRING);
1021                                 ptlrpc_deactivate_import(imp);
1022                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1023                         }
1024                         RETURN(-EPROTO);
1025                 }
1026
1027                 ptlrpc_maybe_ping_import_soon(imp);
1028
1029                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1030                        obd2cli_tgt(imp->imp_obd),
1031                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1032         }
1033
1034         spin_lock(&imp->imp_lock);
1035         imp->imp_last_recon = 0;
1036         spin_unlock(&imp->imp_lock);
1037
1038         cfs_waitq_signal(&imp->imp_recovery_waitq);
1039         RETURN(rc);
1040 }
1041
1042 static int completed_replay_interpret(struct ptlrpc_request *req,
1043                                     void * data, int rc)
1044 {
1045         ENTRY;
1046         atomic_dec(&req->rq_import->imp_replay_inflight);
1047         if (req->rq_status == 0) {
1048                 ptlrpc_import_recovery_state_machine(req->rq_import);
1049         } else {
1050                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1051                        "reconnecting\n",
1052                        req->rq_import->imp_obd->obd_name, req->rq_status);
1053                 ptlrpc_connect_import(req->rq_import, NULL);
1054         }
1055
1056         RETURN(0);
1057 }
1058
1059 static int signal_completed_replay(struct obd_import *imp)
1060 {
1061         struct ptlrpc_request *req;
1062         ENTRY;
1063
1064         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1065         atomic_inc(&imp->imp_replay_inflight);
1066
1067         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1068                                         OBD_PING);
1069         if (req == NULL) {
1070                 atomic_dec(&imp->imp_replay_inflight);
1071                 RETURN(-ENOMEM);
1072         }
1073
1074         ptlrpc_request_set_replen(req);
1075         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1076         lustre_msg_add_flags(req->rq_reqmsg,
1077                              MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1078         req->rq_timeout *= 3;
1079         req->rq_interpret_reply = completed_replay_interpret;
1080
1081         ptlrpcd_add_req(req);
1082         RETURN(0);
1083 }
1084
1085 #ifdef __KERNEL__
1086 static int ptlrpc_invalidate_import_thread(void *data)
1087 {
1088         struct obd_import *imp = data;
1089
1090         ENTRY;
1091
1092         ptlrpc_daemonize("ll_imp_inval");
1093
1094         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1095                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1096                imp->imp_connection->c_remote_uuid.uuid);
1097
1098         ptlrpc_invalidate_import(imp);
1099
1100         if (obd_dump_on_eviction) {
1101                 CERROR("dump the log upon eviction\n");
1102                 libcfs_debug_dumplog();
1103         }
1104
1105         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1106         ptlrpc_import_recovery_state_machine(imp);
1107
1108         RETURN(0);
1109 }
1110 #endif
1111
1112 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1113 {
1114         int rc = 0;
1115         int inflight;
1116         char *target_start;
1117         int target_len;
1118
1119         ENTRY;
1120         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1121                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1122                           &target_start, &target_len);
1123                 /* Don't care about MGC eviction */
1124                 if (strcmp(imp->imp_obd->obd_type->typ_name,
1125                            LUSTRE_MGC_NAME) != 0) {
1126                         LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1127                                            "%.*s; in progress operations using "
1128                                            "this service will fail.\n",
1129                                            target_len, target_start);
1130                 }
1131                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1132                        obd2cli_tgt(imp->imp_obd),
1133                        imp->imp_connection->c_remote_uuid.uuid);
1134
1135 #ifdef __KERNEL__
1136                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1137                                        CLONE_VM | CLONE_FILES);
1138                 if (rc < 0)
1139                         CERROR("error starting invalidate thread: %d\n", rc);
1140                 else
1141                         rc = 0;
1142                 RETURN(rc);
1143 #else
1144                 ptlrpc_invalidate_import(imp);
1145
1146                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1147 #endif
1148         }
1149
1150         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1151                 CDEBUG(D_HA, "replay requested by %s\n",
1152                        obd2cli_tgt(imp->imp_obd));
1153                 rc = ptlrpc_replay_next(imp, &inflight);
1154                 if (inflight == 0 &&
1155                     atomic_read(&imp->imp_replay_inflight) == 0) {
1156                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1157                         rc = ldlm_replay_locks(imp);
1158                         if (rc)
1159                                 GOTO(out, rc);
1160                 }
1161                 rc = 0;
1162         }
1163
1164         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1165                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1166                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1167                         rc = signal_completed_replay(imp);
1168                         if (rc)
1169                                 GOTO(out, rc);
1170                 }
1171
1172         }
1173
1174         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1175                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1176                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1177                 }
1178         }
1179
1180         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1181                 CDEBUG(D_HA, "reconnected to %s@%s\n",
1182                        obd2cli_tgt(imp->imp_obd),
1183                        imp->imp_connection->c_remote_uuid.uuid);
1184
1185                 rc = ptlrpc_resend(imp);
1186                 if (rc)
1187                         GOTO(out, rc);
1188                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1189                 ptlrpc_activate_import(imp);
1190
1191                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1192                           &target_start, &target_len);
1193                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1194                               "using nid %s.\n", imp->imp_obd->obd_name,
1195                               target_len, target_start,
1196                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
1197         }
1198
1199         if (imp->imp_state == LUSTRE_IMP_FULL) {
1200                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1201                 ptlrpc_wake_delayed(imp);
1202         }
1203
1204 out:
1205         RETURN(rc);
1206 }
1207
1208 static int back_to_sleep(void *unused)
1209 {
1210         return 0;
1211 }
1212
1213 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1214 {
1215         struct ptlrpc_request *req;
1216         int rq_opc, rc = 0;
1217         int nowait = imp->imp_obd->obd_force;
1218         ENTRY;
1219
1220         if (nowait)
1221                 GOTO(set_state, rc);
1222
1223         switch (imp->imp_connect_op) {
1224         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1225         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1226         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1227         default:
1228                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1229                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1230                 RETURN(-EINVAL);
1231         }
1232
1233         if (ptlrpc_import_in_recovery(imp)) {
1234                 struct l_wait_info lwi;
1235                 cfs_duration_t timeout;
1236
1237
1238                 if (AT_OFF) {
1239                         if (imp->imp_server_timeout)
1240                                 timeout = cfs_time_seconds(obd_timeout / 2);
1241                         else
1242                                 timeout = cfs_time_seconds(obd_timeout);
1243                 } else {
1244                         int idx = import_at_get_index(imp,
1245                                 imp->imp_client->cli_request_portal);
1246                         timeout = cfs_time_seconds(
1247                                 at_get(&imp->imp_at.iat_service_estimate[idx]));
1248                 }
1249
1250                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1251                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1252                 rc = l_wait_event(imp->imp_recovery_waitq,
1253                                   !ptlrpc_import_in_recovery(imp), &lwi);
1254
1255         }
1256
1257         spin_lock(&imp->imp_lock);
1258         if (imp->imp_state != LUSTRE_IMP_FULL)
1259                 GOTO(out, 0);
1260
1261         spin_unlock(&imp->imp_lock);
1262
1263         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1264                                         LUSTRE_OBD_VERSION, rq_opc);
1265         if (req) {
1266                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1267                  * it fails.  We can get through the above with a down server
1268                  * if the client doesn't know the server is gone yet. */
1269                 req->rq_no_resend = 1;
1270
1271 #ifndef CRAY_XT3
1272                 /* We want client umounts to happen quickly, no matter the
1273                    server state... */
1274                 req->rq_timeout = min_t(int, req->rq_timeout,
1275                                         INITIAL_CONNECT_TIMEOUT);
1276 #else
1277                 /* ... but we always want liblustre clients to nicely
1278                    disconnect, so only use the adaptive value. */
1279                 if (AT_OFF)
1280                         req->rq_timeout = obd_timeout / 3;
1281 #endif
1282
1283                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1284                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1285                 ptlrpc_request_set_replen(req);
1286                 rc = ptlrpc_queue_wait(req);
1287                 ptlrpc_req_finished(req);
1288         }
1289
1290 set_state:
1291         spin_lock(&imp->imp_lock);
1292 out:
1293         if (noclose)
1294                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1295         else
1296                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1297         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1298         imp->imp_conn_cnt = 0;
1299         /* Try all connections in the future - bz 12758 */
1300         imp->imp_last_recon = 0;
1301         spin_unlock(&imp->imp_lock);
1302
1303         RETURN(rc);
1304 }
1305
1306
1307 /* Adaptive Timeout utils */
1308 extern unsigned int at_min, at_max, at_history;
1309
1310 /* Bin into timeslices using AT_BINS bins.
1311    This gives us a max of the last binlimit*AT_BINS secs without the storage,
1312    but still smoothing out a return to normalcy from a slow response.
1313    (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1314 int at_add(struct adaptive_timeout *at, unsigned int val)
1315 {
1316         unsigned int old = at->at_current;
1317         time_t now = cfs_time_current_sec();
1318         time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1319
1320         LASSERT(at);
1321 #if 0
1322         CDEBUG(D_INFO, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1323                val, at, now - at->at_binstart, at->at_current,
1324                at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1325 #endif
1326         if (val == 0)
1327                 /* 0's don't count, because we never want our timeout to
1328                    drop to 0, and because 0 could mean an error */
1329                 return 0;
1330
1331         spin_lock(&at->at_lock);
1332
1333         if (unlikely(at->at_binstart == 0)) {
1334                 /* Special case to remove default from history */
1335                 at->at_current = val;
1336                 at->at_worst_ever = val;
1337                 at->at_worst_time = now;
1338                 at->at_hist[0] = val;
1339                 at->at_binstart = now;
1340         } else if (now - at->at_binstart < binlimit ) {
1341                 /* in bin 0 */
1342                 at->at_hist[0] = max(val, at->at_hist[0]);
1343                 at->at_current = max(val, at->at_current);
1344         } else {
1345                 int i, shift;
1346                 unsigned int maxv = val;
1347                 /* move bins over */
1348                 shift = (now - at->at_binstart) / binlimit;
1349                 LASSERT(shift > 0);
1350                 for(i = AT_BINS - 1; i >= 0; i--) {
1351                         if (i >= shift) {
1352                                 at->at_hist[i] = at->at_hist[i - shift];
1353                                 maxv = max(maxv, at->at_hist[i]);
1354                         } else {
1355                                 at->at_hist[i] = 0;
1356                         }
1357                 }
1358                 at->at_hist[0] = val;
1359                 at->at_current = maxv;
1360                 at->at_binstart += shift * binlimit;
1361         }
1362
1363         if (at->at_current > at->at_worst_ever) {
1364                 at->at_worst_ever = at->at_current;
1365                 at->at_worst_time = now;
1366         }
1367
1368         if (at->at_flags & AT_FLG_NOHIST)
1369                 /* Only keep last reported val; keeping the rest of the history
1370                    for proc only */
1371                 at->at_current = val;
1372
1373         if (at_max > 0)
1374                 at->at_current =  min(at->at_current, at_max);
1375         at->at_current =  max(at->at_current, at_min);
1376
1377 #if 0
1378         if (at->at_current != old)
1379                 CDEBUG(D_ADAPTTO, "AT %p change: old=%u new=%u delta=%d "
1380                        "(val=%u) hist %u %u %u %u\n", at,
1381                        old, at->at_current, at->at_current - old, val,
1382                        at->at_hist[0], at->at_hist[1], at->at_hist[2],
1383                        at->at_hist[3]);
1384 #endif
1385
1386         /* if we changed, report the old value */
1387         old = (at->at_current != old) ? old : 0;
1388
1389         spin_unlock(&at->at_lock);
1390         return old;
1391 }
1392
1393 /* Find the imp_at index for a given portal; assign if space available */
1394 int import_at_get_index(struct obd_import *imp, int portal)
1395 {
1396         struct imp_at *at = &imp->imp_at;
1397         int i;
1398
1399         for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1400                 if (at->iat_portal[i] == portal)
1401                         return i;
1402                 if (at->iat_portal[i] == 0)
1403                         /* unused */
1404                         break;
1405         }
1406
1407         /* Not found in list, add it under a lock */
1408         spin_lock(&imp->imp_lock);
1409
1410         /* Check unused under lock */
1411         for (; i < IMP_AT_MAX_PORTALS; i++) {
1412                 if (at->iat_portal[i] == portal)
1413                         goto out;
1414                 if (at->iat_portal[i] == 0)
1415                         /* unused */
1416                         break;
1417         }
1418
1419         /* Not enough portals? */
1420         LASSERT(i < IMP_AT_MAX_PORTALS);
1421
1422         at->iat_portal[i] = portal;
1423 out:
1424         spin_unlock(&imp->imp_lock);
1425         return i;
1426 }