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