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