Whamcloud - gitweb
- removed unused var "i" left from one of previous commits.
[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         
313         ptlrpc_set_import_discon(imp, 0); 
314         /* Force a new connect attempt */
315         ptlrpc_invalidate_import(imp);
316         /* Do a fresh connect next time by zeroing the handle */
317         ptlrpc_disconnect_import(imp, 1);
318         /* Wait for all invalidate calls to finish */
319         if (atomic_read(&imp->imp_inval_count) > 0) {
320                 int rc;
321                 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
322                 rc = l_wait_event(imp->imp_recovery_waitq,
323                                   (atomic_read(&imp->imp_inval_count) == 0),
324                                   &lwi);
325                 if (rc)
326                         CERROR("Interrupted, inval=%d\n", 
327                                atomic_read(&imp->imp_inval_count));
328         }
329
330         /* Allow reconnect attempts */
331         imp->imp_obd->obd_no_recov = 0;
332         /* Remove 'invalid' flag */
333         ptlrpc_activate_import(imp);
334         /* Attempt a new connect */
335         ptlrpc_recover_import(imp, NULL);
336         return 0;
337 }
338
339 EXPORT_SYMBOL(ptlrpc_reconnect_import);
340
341 static int import_select_connection(struct obd_import *imp)
342 {
343         struct obd_import_conn *imp_conn = NULL, *conn;
344         struct obd_export *dlmexp;
345         int tried_all = 1;
346         ENTRY;
347
348         spin_lock(&imp->imp_lock);
349
350         if (list_empty(&imp->imp_conn_list)) {
351                 CERROR("%s: no connections available\n",
352                         imp->imp_obd->obd_name);
353                 spin_unlock(&imp->imp_lock);
354                 RETURN(-EINVAL);
355         }
356
357         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
358                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
359                        imp->imp_obd->obd_name,
360                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
361                        conn->oic_last_attempt);
362                 /* Don't thrash connections */
363                 if (cfs_time_before_64(cfs_time_current_64(),
364                                      conn->oic_last_attempt +
365                                      cfs_time_seconds(CONNECTION_SWITCH_MIN))) {
366                         continue;
367                 }
368
369                 /* If we have not tried this connection since the
370                    the last successful attempt, go with this one */
371                 if ((conn->oic_last_attempt == 0) ||
372                     cfs_time_beforeq_64(conn->oic_last_attempt,
373                                        imp->imp_last_success_conn)) {
374                         imp_conn = conn;
375                         tried_all = 0;
376                         break;
377                 }
378
379                 /* If all of the connections have already been tried
380                    since the last successful connection; just choose the
381                    least recently used */
382                 if (!imp_conn)
383                         imp_conn = conn;
384                 else if (cfs_time_before_64(conn->oic_last_attempt,
385                                             imp_conn->oic_last_attempt))
386                         imp_conn = conn;
387         }
388
389         /* if not found, simply choose the current one */
390         if (!imp_conn) {
391                 LASSERT(imp->imp_conn_current);
392                 imp_conn = imp->imp_conn_current;
393                 tried_all = 0;
394         }
395         LASSERT(imp_conn->oic_conn);
396
397         /* If we've tried everything, and we're back to the beginning of the
398            list, increase our timeout and try again. It will be reset when
399            we do finally connect. (FIXME: really we should wait for all network
400            state associated with the last connection attempt to drain before
401            trying to reconnect on it.) */
402         if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item) &&
403             !imp->imp_recon_bk /* not retrying */) {
404                 if (at_get(&imp->imp_at.iat_net_latency) <
405                     CONNECTION_SWITCH_MAX) {
406                         at_add(&imp->imp_at.iat_net_latency,
407                                at_get(&imp->imp_at.iat_net_latency) +
408                                CONNECTION_SWITCH_INC);
409                 }
410                 LASSERT(imp_conn->oic_last_attempt);
411                 CWARN("%s: tried all connections, increasing latency to %ds\n",
412                       imp->imp_obd->obd_name,
413                       at_get(&imp->imp_at.iat_net_latency));
414         }
415
416         imp_conn->oic_last_attempt = cfs_time_current_64();
417
418         /* switch connection, don't mind if it's same as the current one */
419         if (imp->imp_connection)
420                 ptlrpc_put_connection(imp->imp_connection);
421         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
422
423         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
424         LASSERT(dlmexp != NULL);
425         if (dlmexp->exp_connection)
426                 ptlrpc_put_connection(dlmexp->exp_connection);
427         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
428         class_export_put(dlmexp);
429
430         if (imp->imp_conn_current != imp_conn) {
431                 if (imp->imp_conn_current)
432                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
433                                       imp->imp_obd->obd_name,
434                                       imp_conn->oic_uuid.uuid,
435                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
436                 imp->imp_conn_current = imp_conn;
437         }
438
439         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
440                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
441                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
442
443         spin_unlock(&imp->imp_lock);
444
445         RETURN(0);
446 }
447
448 /*
449  * must be called under imp_lock
450  */
451 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
452 {
453         struct ptlrpc_request *req;
454         struct list_head *tmp;
455
456         if (list_empty(&imp->imp_replay_list))
457                 return 0;
458         tmp = imp->imp_replay_list.next;
459         req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
460         *transno = req->rq_transno;
461         if (req->rq_transno == 0) {
462                 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
463                 LBUG();
464         }
465
466         return 1;
467 }
468
469 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
470 {
471         struct obd_device *obd = imp->imp_obd;
472         int initial_connect = 0;
473         int set_transno = 0;
474         __u64 committed_before_reconnect = 0;
475         struct ptlrpc_request *request;
476         char *bufs[] = { NULL,
477                          obd2cli_tgt(imp->imp_obd),
478                          obd->obd_uuid.uuid,
479                          (char *)&imp->imp_dlm_handle,
480                          (char *)&imp->imp_connect_data };
481         struct ptlrpc_connect_async_args *aa;
482         int rc;
483         ENTRY;
484
485         spin_lock(&imp->imp_lock);
486         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
487                 spin_unlock(&imp->imp_lock);
488                 CERROR("can't connect to a closed import\n");
489                 RETURN(-EINVAL);
490         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
491                 spin_unlock(&imp->imp_lock);
492                 CERROR("already connected\n");
493                 RETURN(0);
494         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
495                 spin_unlock(&imp->imp_lock);
496                 CERROR("already connecting\n");
497                 RETURN(-EALREADY);
498         }
499
500         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
501
502         imp->imp_conn_cnt++;
503         imp->imp_resend_replay = 0;
504
505         if (!lustre_handle_is_used(&imp->imp_remote_handle))
506                 initial_connect = 1;
507         else
508                 committed_before_reconnect = imp->imp_peer_committed_transno;
509
510         set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
511         spin_unlock(&imp->imp_lock);
512
513         if (new_uuid) {
514                 struct obd_uuid uuid;
515
516                 obd_str2uuid(&uuid, new_uuid);
517                 rc = import_set_conn_priority(imp, &uuid);
518                 if (rc)
519                         GOTO(out, rc);
520         }
521
522         rc = import_select_connection(imp);
523         if (rc)
524                 GOTO(out, rc);
525
526         /* last in connection list */
527         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
528                 if (imp->imp_initial_recov_bk && initial_connect) {
529                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
530                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
531                         /* Don't retry if connect fails */
532                         rc = 0;
533                         obd_set_info_async(obd->obd_self_export,
534                                            sizeof(KEY_INIT_RECOV),
535                                            KEY_INIT_RECOV,
536                                            sizeof(rc), &rc, NULL);
537                 }
538                 if (imp->imp_recon_bk) {
539                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
540                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
541                         spin_lock(&imp->imp_lock);
542                         imp->imp_last_recon = 1;
543                         spin_unlock(&imp->imp_lock);
544                 }
545         }
546
547         rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
548         if (rc)
549                 GOTO(out, rc);
550
551         /* Reset connect flags to the originally requested flags, in case
552          * the server is updated on-the-fly we will get the new features. */
553         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
554         imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
555
556         rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
557                            &obd->obd_uuid, &imp->imp_connect_data);
558         if (rc)
559                 GOTO(out, rc);
560
561         request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
562         if (request == NULL)
563                 GOTO(out, rc = -ENOMEM);
564
565         rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
566                                       imp->imp_connect_op, bufs, NULL);
567         if (rc) {
568                 ptlrpc_request_free(request);
569                 GOTO(out, rc);
570         }
571
572 #ifndef __KERNEL__
573         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
574 #endif
575         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
576
577         request->rq_send_state = LUSTRE_IMP_CONNECTING;
578         /* Allow a slightly larger reply for future growth compatibility */
579         req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
580                              sizeof(struct obd_connect_data)+16*sizeof(__u64));
581         ptlrpc_request_set_replen(request);
582         request->rq_interpret_reply = ptlrpc_connect_interpret;
583
584         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
585         aa = ptlrpc_req_async_args(request);
586         memset(aa, 0, sizeof *aa);
587
588         aa->pcaa_peer_committed = committed_before_reconnect;
589         aa->pcaa_initial_connect = initial_connect;
590
591         if (aa->pcaa_initial_connect) {
592                 spin_lock(&imp->imp_lock);
593                 imp->imp_replayable = 1;
594                 spin_unlock(&imp->imp_lock);
595                 lustre_msg_add_op_flags(request->rq_reqmsg, 
596                                         MSG_CONNECT_INITIAL);
597                 if (AT_OFF)
598                         /* AT will use INITIAL_CONNECT_TIMEOUT the first
599                            time, adaptive after that. */
600                         request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
601         }
602
603         if (set_transno)
604                 lustre_msg_add_op_flags(request->rq_reqmsg, 
605                                         MSG_CONNECT_TRANSNO);
606
607         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
608         ptlrpcd_add_req(request);
609         rc = 0;
610 out:
611         if (rc != 0) {
612                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
613         }
614
615         RETURN(rc);
616 }
617 EXPORT_SYMBOL(ptlrpc_connect_import);
618
619 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
620 {
621 #ifdef __KERNEL__
622         struct obd_import_conn *imp_conn;
623 #endif
624         int wake_pinger = 0;
625
626         ENTRY;
627
628         spin_lock(&imp->imp_lock);
629         if (list_empty(&imp->imp_conn_list))
630                 GOTO(unlock, 0);
631
632 #ifdef __KERNEL__
633         imp_conn = list_entry(imp->imp_conn_list.prev,
634                               struct obd_import_conn,
635                               oic_item);
636
637         /* XXX: When the failover node is the primary node, it is possible
638          * to have two identical connections in imp_conn_list. We must 
639          * compare not conn's pointers but NIDs, otherwise we can defeat
640          * connection throttling. (See bug 14774.) */
641         if (imp->imp_conn_current->oic_conn->c_self != 
642                                 imp_conn->oic_conn->c_self) {
643                 ptlrpc_ping_import_soon(imp);
644                 wake_pinger = 1;
645         }
646 #else
647         /* liblustre has no pinger thead, so we wakup pinger anyway */
648         wake_pinger = 1;
649 #endif 
650
651  unlock:
652         spin_unlock(&imp->imp_lock);
653
654         if (wake_pinger)
655                 ptlrpc_pinger_wake_up();
656
657         EXIT;
658 }
659
660 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
661                                     void * data, int rc)
662 {
663         struct ptlrpc_connect_async_args *aa = data;
664         struct obd_import *imp = request->rq_import;
665         struct client_obd *cli = &imp->imp_obd->u.cli;
666         struct lustre_handle old_hdl;
667         int msg_flags;
668         ENTRY;
669
670         spin_lock(&imp->imp_lock);
671         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
672                 spin_unlock(&imp->imp_lock);
673                 RETURN(0);
674         }
675         spin_unlock(&imp->imp_lock);
676
677         if (rc)
678                 GOTO(out, rc);
679
680         LASSERT(imp->imp_conn_current);
681
682         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
683
684         /* All imports are pingable */
685         spin_lock(&imp->imp_lock);
686         imp->imp_pingable = 1;
687
688         if (aa->pcaa_initial_connect) {
689                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
690                         imp->imp_replayable = 1;
691                         spin_unlock(&imp->imp_lock);
692                         CDEBUG(D_HA, "connected to replayable target: %s\n",
693                                obd2cli_tgt(imp->imp_obd));
694                 } else {
695                         imp->imp_replayable = 0;
696                         spin_unlock(&imp->imp_lock);
697                 }
698
699                 /* if applies, adjust the imp->imp_msg_magic here
700                  * according to reply flags */
701
702                 imp->imp_remote_handle =
703                                 *lustre_msg_get_handle(request->rq_repmsg);
704
705                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
706                 spin_lock(&imp->imp_lock);
707                 if (imp->imp_invalid) {
708                         spin_unlock(&imp->imp_lock);
709                         ptlrpc_activate_import(imp);
710                 } else {
711                         spin_unlock(&imp->imp_lock);
712                 }
713
714                 GOTO(finish, rc = 0);
715         } else {
716                 spin_unlock(&imp->imp_lock);
717         }
718
719         /* Determine what recovery state to move the import to. */
720         if (MSG_CONNECT_RECONNECT & msg_flags) {
721                 memset(&old_hdl, 0, sizeof(old_hdl));
722                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
723                             sizeof (old_hdl))) {
724                         CERROR("%s@%s didn't like our handle "LPX64
725                                ", failed\n", obd2cli_tgt(imp->imp_obd),
726                                imp->imp_connection->c_remote_uuid.uuid,
727                                imp->imp_dlm_handle.cookie);
728                         GOTO(out, rc = -ENOTCONN);
729                 }
730
731                 if (memcmp(&imp->imp_remote_handle,
732                            lustre_msg_get_handle(request->rq_repmsg),
733                            sizeof(imp->imp_remote_handle))) {
734                         int level = msg_flags & MSG_CONNECT_RECOVERING ? D_HA :
735                                                                          D_WARNING;
736
737                         /* Bug 16611/14775: if server handle have changed,
738                          * that means some sort of disconnection happened.
739                          * If the server is not in recovery, that also means it
740                          * already erased all of our state because of previous
741                          * eviction. If it is in recovery - we are safe to
742                          * participate since we can reestablish all of our state
743                          * with server again */
744                         CDEBUG(level,"%s@%s changed server handle from "
745                                      LPX64" to "LPX64"%s \n" "but is still in recovery \n",
746                                      obd2cli_tgt(imp->imp_obd),
747                                      imp->imp_connection->c_remote_uuid.uuid,
748                                      imp->imp_remote_handle.cookie,
749                                      lustre_msg_get_handle(request->rq_repmsg)->
750                                                                         cookie,
751                                      (MSG_CONNECT_RECOVERING & msg_flags) ?
752                                          "but is still in recovery" : "");
753
754                         imp->imp_remote_handle =
755                                      *lustre_msg_get_handle(request->rq_repmsg);
756
757                         if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
758                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
759                                 GOTO(finish, rc = 0);
760                         }
761
762                 } else {
763                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
764                                obd2cli_tgt(imp->imp_obd),
765                                imp->imp_connection->c_remote_uuid.uuid);
766                 }
767
768                 if (imp->imp_invalid) {
769                         CDEBUG(D_HA, "%s: reconnected but import is invalid; "
770                                "marking evicted\n", imp->imp_obd->obd_name);
771                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
772                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
773                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
774                                imp->imp_obd->obd_name,
775                                obd2cli_tgt(imp->imp_obd));
776
777                         spin_lock(&imp->imp_lock);
778                         imp->imp_resend_replay = 1;
779                         spin_unlock(&imp->imp_lock);
780
781                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
782                 } else {
783                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
784                 }
785         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
786                 LASSERT(imp->imp_replayable);
787                 imp->imp_remote_handle =
788                                 *lustre_msg_get_handle(request->rq_repmsg);
789                 imp->imp_last_replay_transno = 0;
790                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
791         } else {
792                 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
793                           " not set: %x)", imp->imp_obd->obd_name, msg_flags);
794                 imp->imp_remote_handle =
795                                 *lustre_msg_get_handle(request->rq_repmsg);
796                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
797         }
798
799         /* Sanity checks for a reconnected import. */
800         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
801                 CERROR("imp_replayable flag does not match server "
802                        "after reconnect. We should LBUG right here.\n");
803         }
804
805         if (lustre_msg_get_last_committed(request->rq_repmsg) <
806             aa->pcaa_peer_committed) {
807                 CERROR("%s went back in time (transno "LPD64
808                        " was previously committed, server now claims "LPD64
809                        ")!  See https://bugzilla.lustre.org/show_bug.cgi?"
810                        "id=9646\n",
811                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
812                        lustre_msg_get_last_committed(request->rq_repmsg));
813         }
814
815 finish:
816         rc = ptlrpc_import_recovery_state_machine(imp);
817         if (rc != 0) {
818                 if (rc == -ENOTCONN) {
819                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
820                                "invalidating and reconnecting\n",
821                                obd2cli_tgt(imp->imp_obd),
822                                imp->imp_connection->c_remote_uuid.uuid);
823                         ptlrpc_connect_import(imp, NULL);
824                         RETURN(0);
825                 }
826         } else {
827                 struct obd_connect_data *ocd;
828                 struct obd_export *exp;
829                 int ret;
830                 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
831                                            RCL_SERVER);
832                 /* server replied obd_connect_data is always bigger */
833                 ocd = req_capsule_server_sized_get(&request->rq_pill,
834                                                    &RMF_CONNECT_DATA, ret);
835
836                 spin_lock(&imp->imp_lock);
837                 list_del(&imp->imp_conn_current->oic_item);
838                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
839                 imp->imp_last_success_conn =
840                         imp->imp_conn_current->oic_last_attempt;
841
842                 if (ocd == NULL) {
843                         spin_unlock(&imp->imp_lock);
844                         CERROR("Wrong connect data from server\n");
845                         rc = -EPROTO;
846                         GOTO(out, rc);
847                 }
848
849                 imp->imp_connect_data = *ocd;
850
851                 exp = class_conn2export(&imp->imp_dlm_handle);
852                 spin_unlock(&imp->imp_lock);
853
854                 /* check that server granted subset of flags we asked for. */
855                 LASSERTF((ocd->ocd_connect_flags &
856                           imp->imp_connect_flags_orig) ==
857                          ocd->ocd_connect_flags, LPX64" != "LPX64,
858                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
859
860                 if (!exp) {
861                         /* This could happen if export is cleaned during the 
862                            connect attempt */
863                         CERROR("Missing export for %s\n", 
864                                imp->imp_obd->obd_name);
865                         GOTO(out, rc = -ENODEV);
866                 }
867                 exp->exp_connect_flags = ocd->ocd_connect_flags;
868                 imp->imp_obd->obd_self_export->exp_connect_flags = 
869                                                         ocd->ocd_connect_flags;
870                 class_export_put(exp);
871
872                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
873
874                 if (!ocd->ocd_ibits_known &&
875                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
876                         CERROR("Inodebits aware server returned zero compatible"
877                                " bits?\n");
878
879                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
880                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
881                                         LUSTRE_VERSION_OFFSET_WARN ||
882                      ocd->ocd_version < LUSTRE_VERSION_CODE -
883                                         LUSTRE_VERSION_OFFSET_WARN)) {
884                         /* Sigh, some compilers do not like #ifdef in the middle
885                            of macro arguments */
886 #ifdef __KERNEL__
887                         const char *older =
888                                 "older. Consider upgrading this client";
889 #else
890                         const char *older =
891                                 "older. Consider recompiling this application";
892 #endif
893                         const char *newer = "newer than client version";
894
895                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
896                                       "is much %s (%s)\n",
897                                       obd2cli_tgt(imp->imp_obd),
898                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
899                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
900                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
901                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
902                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
903                                       newer : older, LUSTRE_VERSION_STRING);
904                 }
905
906                 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
907                         /* We sent to the server ocd_cksum_types with bits set
908                          * for algorithms we understand. The server masked off
909                          * the checksum types it doesn't support */
910                         if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
911                                 LCONSOLE_WARN("The negotiation of the checksum "
912                                               "alogrithm to use with server %s "
913                                               "failed (%x/%x), disabling "
914                                               "checksums\n",
915                                               obd2cli_tgt(imp->imp_obd),
916                                               ocd->ocd_cksum_types,
917                                               OBD_CKSUM_ALL);
918                                 cli->cl_checksum = 0;
919                                 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
920                                 cli->cl_cksum_type = OBD_CKSUM_CRC32;
921                         } else {
922                                 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
923
924                                 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
925                                         cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
926                                 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
927                                         cli->cl_cksum_type = OBD_CKSUM_ADLER;
928                                 else
929                                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
930                         }
931                 } else {
932                         /* The server does not support OBD_CONNECT_CKSUM.
933                          * Enforce CRC32 for backward compatibility*/
934                         cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
935                         cli->cl_cksum_type = OBD_CKSUM_CRC32;
936                 }
937
938                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
939                         cli->cl_max_pages_per_rpc = 
940                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
941                 }
942
943                 imp->imp_obd->obd_namespace->ns_connect_flags = 
944                                                         ocd->ocd_connect_flags;
945                 imp->imp_obd->obd_namespace->ns_orig_connect_flags = 
946                                                         ocd->ocd_connect_flags;
947
948                 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
949                     (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
950                         /* We need a per-message support flag, because
951                            a. we don't know if the incoming connect reply
952                               supports AT or not (in reply_in_callback)
953                               until we unpack it.
954                            b. failovered server means export and flags are gone
955                               (in ptlrpc_send_reply).
956                            Can only be set when we know AT is supported at
957                            both ends */
958                         imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
959                 else
960                         imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
961
962                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
963                         (cli->cl_max_pages_per_rpc > 0));
964         }
965
966 out:
967         if (rc != 0) {
968                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
969                 spin_lock(&imp->imp_lock);
970                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
971                     (request->rq_import_generation == imp->imp_generation))
972                         ptlrpc_deactivate_and_unlock_import(imp);
973                 else
974                         spin_unlock(&imp->imp_lock);
975
976                 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
977                     (rc == -EACCES)) {
978                         /*
979                          * Give up trying to reconnect
980                          * EACCES means client has no permission for connection
981                          */
982                         imp->imp_obd->obd_no_recov = 1;
983                         ptlrpc_deactivate_import(imp);
984                 }
985
986                 if (rc == -EPROTO) {
987                         struct obd_connect_data *ocd;
988
989                         /* reply message might not be ready */
990                         if (request->rq_repmsg == NULL)
991                                 RETURN(-EPROTO);
992
993                         ocd = req_capsule_server_get(&request->rq_pill,
994                                                      &RMF_CONNECT_DATA);
995                         if (ocd &&
996                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
997                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
998                            /* Actually servers are only supposed to refuse
999                               connection from liblustre clients, so we should
1000                               never see this from VFS context */
1001                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1002                                         "(%d.%d.%d.%d)"
1003                                         " refused connection from this client "
1004                                         "with an incompatible version (%s).  "
1005                                         "Client must be recompiled\n",
1006                                         obd2cli_tgt(imp->imp_obd),
1007                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1008                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1009                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1010                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
1011                                         LUSTRE_VERSION_STRING);
1012                                 ptlrpc_deactivate_import(imp);
1013                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1014                         }
1015                         RETURN(-EPROTO);
1016                 }
1017
1018                 ptlrpc_maybe_ping_import_soon(imp);
1019
1020                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1021                        obd2cli_tgt(imp->imp_obd),
1022                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1023         }
1024         
1025         spin_lock(&imp->imp_lock);
1026         imp->imp_last_recon = 0;
1027         spin_unlock(&imp->imp_lock);
1028
1029         cfs_waitq_signal(&imp->imp_recovery_waitq);
1030         RETURN(rc);
1031 }
1032
1033 static int completed_replay_interpret(struct ptlrpc_request *req,
1034                                     void * data, int rc)
1035 {
1036         ENTRY;
1037         atomic_dec(&req->rq_import->imp_replay_inflight);
1038         if (req->rq_status == 0) {
1039                 ptlrpc_import_recovery_state_machine(req->rq_import);
1040         } else {
1041                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1042                        "reconnecting\n",
1043                        req->rq_import->imp_obd->obd_name, req->rq_status);
1044                 ptlrpc_connect_import(req->rq_import, NULL);
1045         }
1046
1047         RETURN(0);
1048 }
1049
1050 static int signal_completed_replay(struct obd_import *imp)
1051 {
1052         struct ptlrpc_request *req;
1053         ENTRY;
1054
1055         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1056         atomic_inc(&imp->imp_replay_inflight);
1057
1058         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1059                                         OBD_PING);
1060         if (req == NULL) {
1061                 atomic_dec(&imp->imp_replay_inflight);
1062                 RETURN(-ENOMEM);
1063         }
1064
1065         ptlrpc_request_set_replen(req);
1066         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1067         lustre_msg_add_flags(req->rq_reqmsg, 
1068                              MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1069         req->rq_timeout *= 3;
1070         req->rq_interpret_reply = completed_replay_interpret;
1071
1072         ptlrpcd_add_req(req);
1073         RETURN(0);
1074 }
1075
1076 #ifdef __KERNEL__
1077 static int ptlrpc_invalidate_import_thread(void *data)
1078 {
1079         struct obd_import *imp = data;
1080
1081         ENTRY;
1082
1083         ptlrpc_daemonize("ll_imp_inval");
1084         
1085         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1086                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1087                imp->imp_connection->c_remote_uuid.uuid);
1088
1089         ptlrpc_invalidate_import(imp);
1090
1091         if (obd_dump_on_eviction) {
1092                 CERROR("dump the log upon eviction\n");
1093                 libcfs_debug_dumplog();
1094         }
1095
1096         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1097         ptlrpc_import_recovery_state_machine(imp);
1098
1099         RETURN(0);
1100 }
1101 #endif
1102
1103 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1104 {
1105         int rc = 0;
1106         int inflight;
1107         char *target_start;
1108         int target_len;
1109
1110         ENTRY;
1111         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1112                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1113                           &target_start, &target_len);
1114                 /* Don't care about MGC eviction */
1115                 if (strcmp(imp->imp_obd->obd_type->typ_name,
1116                            LUSTRE_MGC_NAME) != 0) {
1117                         LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1118                                            "%.*s; in progress operations using "
1119                                            "this service will fail.\n",
1120                                            target_len, target_start);
1121                 }
1122                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1123                        obd2cli_tgt(imp->imp_obd),
1124                        imp->imp_connection->c_remote_uuid.uuid);
1125
1126 #ifdef __KERNEL__
1127                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1128                                        CLONE_VM | CLONE_FILES);
1129                 if (rc < 0)
1130                         CERROR("error starting invalidate thread: %d\n", rc);
1131                 else
1132                         rc = 0;
1133                 RETURN(rc);
1134 #else
1135                 ptlrpc_invalidate_import(imp);
1136
1137                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1138 #endif
1139         }
1140
1141         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1142                 CDEBUG(D_HA, "replay requested by %s\n",
1143                        obd2cli_tgt(imp->imp_obd));
1144                 rc = ptlrpc_replay_next(imp, &inflight);
1145                 if (inflight == 0 &&
1146                     atomic_read(&imp->imp_replay_inflight) == 0) {
1147                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1148                         rc = ldlm_replay_locks(imp);
1149                         if (rc)
1150                                 GOTO(out, rc);
1151                 }
1152                 rc = 0;
1153         }
1154
1155         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1156                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1157                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1158                         rc = signal_completed_replay(imp);
1159                         if (rc)
1160                                 GOTO(out, rc);
1161                 }
1162
1163         }
1164
1165         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1166                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1167                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1168                 }
1169         }
1170
1171         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1172                 CDEBUG(D_HA, "reconnected to %s@%s\n",
1173                        obd2cli_tgt(imp->imp_obd),
1174                        imp->imp_connection->c_remote_uuid.uuid);
1175
1176                 rc = ptlrpc_resend(imp);
1177                 if (rc)
1178                         GOTO(out, rc);
1179                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1180                 ptlrpc_activate_import(imp);
1181
1182                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1183                           &target_start, &target_len);
1184                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1185                               "using nid %s.\n", imp->imp_obd->obd_name,
1186                               target_len, target_start,
1187                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
1188         }
1189
1190         if (imp->imp_state == LUSTRE_IMP_FULL) {
1191                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1192                 ptlrpc_wake_delayed(imp);
1193         }
1194
1195 out:
1196         RETURN(rc);
1197 }
1198
1199 static int back_to_sleep(void *unused)
1200 {
1201         return 0;
1202 }
1203
1204 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1205 {
1206         struct ptlrpc_request *req;
1207         int rq_opc, rc = 0;
1208         int nowait = imp->imp_obd->obd_force;
1209         ENTRY;
1210
1211         if (nowait)
1212                 GOTO(set_state, rc);
1213
1214         switch (imp->imp_connect_op) {
1215         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1216         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1217         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1218         default:
1219                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1220                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1221                 RETURN(-EINVAL);
1222         }
1223
1224         if (ptlrpc_import_in_recovery(imp)) {
1225                 struct l_wait_info lwi;
1226                 cfs_duration_t timeout;
1227
1228
1229                 if (AT_OFF) {
1230                         if (imp->imp_server_timeout)
1231                                 timeout = cfs_time_seconds(obd_timeout / 2);
1232                         else
1233                                 timeout = cfs_time_seconds(obd_timeout);
1234                 } else {
1235                         int idx = import_at_get_index(imp,
1236                                 imp->imp_client->cli_request_portal);
1237                         timeout = cfs_time_seconds(
1238                                 at_get(&imp->imp_at.iat_service_estimate[idx]));
1239                 }
1240                 
1241                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
1242                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1243                 rc = l_wait_event(imp->imp_recovery_waitq,
1244                                   !ptlrpc_import_in_recovery(imp), &lwi);
1245
1246         }
1247
1248         spin_lock(&imp->imp_lock);
1249         if (imp->imp_state != LUSTRE_IMP_FULL)
1250                 GOTO(out, 0);
1251
1252         spin_unlock(&imp->imp_lock);
1253
1254         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1255                                         LUSTRE_OBD_VERSION, rq_opc);
1256         if (req) {
1257                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1258                  * it fails.  We can get through the above with a down server
1259                  * if the client doesn't know the server is gone yet. */
1260                 req->rq_no_resend = 1;
1261
1262 #ifndef CRAY_XT3
1263                 /* We want client umounts to happen quickly, no matter the
1264                    server state... */
1265                 req->rq_timeout = min_t(int, req->rq_timeout,
1266                                         INITIAL_CONNECT_TIMEOUT);
1267 #else
1268                 /* ... but we always want liblustre clients to nicely
1269                    disconnect, so only use the adaptive value. */
1270                 if (AT_OFF)
1271                         req->rq_timeout = obd_timeout / 3;
1272 #endif
1273
1274                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1275                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1276                 ptlrpc_request_set_replen(req);
1277                 rc = ptlrpc_queue_wait(req);
1278                 ptlrpc_req_finished(req);
1279         }
1280
1281 set_state:
1282         spin_lock(&imp->imp_lock);
1283 out:
1284         if (noclose) 
1285                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1286         else
1287                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1288         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1289         imp->imp_conn_cnt = 0;
1290         /* Try all connections in the future - bz 12758 */
1291         imp->imp_last_recon = 0;
1292         spin_unlock(&imp->imp_lock);
1293
1294         RETURN(rc);
1295 }
1296
1297
1298 /* Adaptive Timeout utils */
1299 extern unsigned int at_min, at_max, at_history;
1300
1301 /* Bin into timeslices using AT_BINS bins.
1302    This gives us a max of the last binlimit*AT_BINS secs without the storage,
1303    but still smoothing out a return to normalcy from a slow response.
1304    (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1305 int at_add(struct adaptive_timeout *at, unsigned int val)
1306 {
1307         unsigned int old = at->at_current;
1308         time_t now = cfs_time_current_sec();
1309         time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1310
1311         LASSERT(at);
1312 #if 0
1313         CDEBUG(D_INFO, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1314                val, at, now - at->at_binstart, at->at_current,
1315                at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1316 #endif
1317         if (val == 0)
1318                 /* 0's don't count, because we never want our timeout to
1319                    drop to 0, and because 0 could mean an error */
1320                 return 0;
1321
1322         spin_lock(&at->at_lock);
1323
1324         if (unlikely(at->at_binstart == 0)) {
1325                 /* Special case to remove default from history */
1326                 at->at_current = val;
1327                 at->at_worst_ever = val;
1328                 at->at_worst_time = now;
1329                 at->at_hist[0] = val;
1330                 at->at_binstart = now;
1331         } else if (now - at->at_binstart < binlimit ) {
1332                 /* in bin 0 */
1333                 at->at_hist[0] = max(val, at->at_hist[0]);
1334                 at->at_current = max(val, at->at_current);
1335         } else {
1336                 int i, shift;
1337                 unsigned int maxv = val;
1338                 /* move bins over */
1339                 shift = (now - at->at_binstart) / binlimit;
1340                 LASSERT(shift > 0);
1341                 for(i = AT_BINS - 1; i >= 0; i--) {
1342                         if (i >= shift) {
1343                                 at->at_hist[i] = at->at_hist[i - shift];
1344                                 maxv = max(maxv, at->at_hist[i]);
1345                         } else {
1346                                 at->at_hist[i] = 0;
1347                         }
1348                 }
1349                 at->at_hist[0] = val;
1350                 at->at_current = maxv;
1351                 at->at_binstart += shift * binlimit;
1352         }
1353
1354         if (at->at_current > at->at_worst_ever) {
1355                 at->at_worst_ever = at->at_current;
1356                 at->at_worst_time = now;
1357         }
1358
1359         if (at->at_flags & AT_FLG_NOHIST)
1360                 /* Only keep last reported val; keeping the rest of the history
1361                    for proc only */
1362                 at->at_current = val;
1363
1364         if (at_max > 0)
1365                 at->at_current =  min(at->at_current, at_max);
1366         at->at_current =  max(at->at_current, at_min);
1367
1368 #if 0
1369         if (at->at_current != old)
1370                 CDEBUG(D_ADAPTTO, "AT %p change: old=%u new=%u delta=%d "
1371                        "(val=%u) hist %u %u %u %u\n", at,
1372                        old, at->at_current, at->at_current - old, val,
1373                        at->at_hist[0], at->at_hist[1], at->at_hist[2],
1374                        at->at_hist[3]);
1375 #endif
1376
1377         /* if we changed, report the old value */
1378         old = (at->at_current != old) ? old : 0;
1379
1380         spin_unlock(&at->at_lock);
1381         return old;
1382 }
1383
1384 /* Find the imp_at index for a given portal; assign if space available */
1385 int import_at_get_index(struct obd_import *imp, int portal)
1386 {
1387         struct imp_at *at = &imp->imp_at;
1388         int i;
1389
1390         for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1391                 if (at->iat_portal[i] == portal)
1392                         return i;
1393                 if (at->iat_portal[i] == 0)
1394                         /* unused */
1395                         break;
1396         }
1397
1398         /* Not found in list, add it under a lock */
1399         spin_lock(&imp->imp_lock);
1400
1401         /* Check unused under lock */
1402         for (; i < IMP_AT_MAX_PORTALS; i++) {
1403                 if (at->iat_portal[i] == portal)
1404                         goto out;
1405                 if (at->iat_portal[i] == 0)
1406                         /* unused */
1407                         break;
1408         }
1409
1410         /* Not enough portals? */
1411         LASSERT(i < IMP_AT_MAX_PORTALS);
1412
1413         at->iat_portal[i] = portal;
1414 out:
1415         spin_unlock(&imp->imp_lock);
1416         return i;
1417 }