blob: 3292e6ea0102aa5ad9d28e842abe642fe3c0b377 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * GPL HEADER END
21 */
22/*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
25 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2011, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
28/*
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
31 *
32 * lustre/ptlrpc/import.c
33 *
34 * Author: Mike Shaver <shaver@clusterfs.com>
35 */
36
37#define DEBUG_SUBSYSTEM S_RPC
38
Greg Kroah-Hartmane27db142014-07-11 22:29:36 -070039#include "../include/obd_support.h"
40#include "../include/lustre_ha.h"
41#include "../include/lustre_net.h"
42#include "../include/lustre_import.h"
43#include "../include/lustre_export.h"
44#include "../include/obd.h"
45#include "../include/obd_cksum.h"
46#include "../include/obd_class.h"
Peng Taod7e09d02013-05-02 16:46:55 +080047
48#include "ptlrpc_internal.h"
49
50struct ptlrpc_connect_async_args {
51 __u64 pcaa_peer_committed;
52 int pcaa_initial_connect;
53};
54
55/**
56 * Updates import \a imp current state to provided \a state value
57 * Helper function. Must be called under imp_lock.
58 */
59static void __import_set_state(struct obd_import *imp,
60 enum lustre_imp_state state)
61{
Andriy Skulysh502cb582015-03-25 22:07:07 -040062 switch (state) {
63 case LUSTRE_IMP_CLOSED:
64 case LUSTRE_IMP_NEW:
65 case LUSTRE_IMP_DISCON:
66 case LUSTRE_IMP_CONNECTING:
67 break;
68 case LUSTRE_IMP_REPLAY_WAIT:
69 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
70 break;
71 default:
72 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
73 }
74
Peng Taod7e09d02013-05-02 16:46:55 +080075 imp->imp_state = state;
76 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
77 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
Arnd Bergmann74e489a2015-09-27 16:45:18 -040078 ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +080079 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
80 IMP_STATE_HIST_LEN;
81}
82
83/* A CLOSED import should remain so. */
Kristina Martsenko532118c2013-11-11 21:35:03 +020084#define IMPORT_SET_STATE_NOLOCK(imp, state) \
85do { \
86 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
87 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
88 imp, obd2cli_tgt(imp->imp_obd), \
89 ptlrpc_import_state_name(imp->imp_state), \
90 ptlrpc_import_state_name(state)); \
91 __import_set_state(imp, state); \
92 } \
Kristina Martsenko3949015e2013-11-11 21:34:58 +020093} while (0)
Peng Taod7e09d02013-05-02 16:46:55 +080094
95#define IMPORT_SET_STATE(imp, state) \
96do { \
97 spin_lock(&imp->imp_lock); \
98 IMPORT_SET_STATE_NOLOCK(imp, state); \
99 spin_unlock(&imp->imp_lock); \
Kristina Martsenko3949015e2013-11-11 21:34:58 +0200100} while (0)
Peng Taod7e09d02013-05-02 16:46:55 +0800101
Peng Taod7e09d02013-05-02 16:46:55 +0800102static int ptlrpc_connect_interpret(const struct lu_env *env,
103 struct ptlrpc_request *request,
Greg Donaldaff9d8e2014-08-21 11:07:42 -0500104 void *data, int rc);
Peng Taod7e09d02013-05-02 16:46:55 +0800105int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
106
107/* Only this function is allowed to change the import state when it is
108 * CLOSED. I would rather refcount the import and free it after
109 * disconnection like we do with exports. To do that, the client_obd
110 * will need to save the peer info somewhere other than in the import,
Oleg Drokindadfcda2016-02-24 22:00:38 -0500111 * though.
112 */
Peng Taod7e09d02013-05-02 16:46:55 +0800113int ptlrpc_init_import(struct obd_import *imp)
114{
115 spin_lock(&imp->imp_lock);
116
117 imp->imp_generation++;
Chris Hannad0bfef32015-06-03 10:28:26 -0400118 imp->imp_state = LUSTRE_IMP_NEW;
Peng Taod7e09d02013-05-02 16:46:55 +0800119
120 spin_unlock(&imp->imp_lock);
121
122 return 0;
123}
124EXPORT_SYMBOL(ptlrpc_init_import);
125
126#define UUID_STR "_UUID"
Shraddha Barke9dd7d422015-10-08 01:36:06 +0530127static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
128 int *uuid_len)
Peng Taod7e09d02013-05-02 16:46:55 +0800129{
130 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
131 ? uuid : uuid + strlen(prefix);
132
133 *uuid_len = strlen(*uuid_start);
134
135 if (*uuid_len < strlen(UUID_STR))
136 return;
137
138 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500139 UUID_STR, strlen(UUID_STR)))
Peng Taod7e09d02013-05-02 16:46:55 +0800140 *uuid_len -= strlen(UUID_STR);
141}
Peng Taod7e09d02013-05-02 16:46:55 +0800142
143/**
144 * Returns true if import was FULL, false if import was already not
145 * connected.
146 * @imp - import to be disconnected
147 * @conn_cnt - connection count (epoch) of the request that timed out
148 * and caused the disconnection. In some cases, multiple
149 * inflight requests can fail to a single target (e.g. OST
150 * bulk requests) and if one has already caused a reconnection
151 * (increasing the import->conn_cnt) the older failure should
152 * not also cause a reconnection. If zero it forces a reconnect.
153 */
154int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
155{
156 int rc = 0;
157
158 spin_lock(&imp->imp_lock);
159
160 if (imp->imp_state == LUSTRE_IMP_FULL &&
161 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
162 char *target_start;
163 int target_len;
164
165 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
166 &target_start, &target_len);
167
168 if (imp->imp_replayable) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800169 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was lost; in progress operations using this service will wait for recovery to complete\n",
170 imp->imp_obd->obd_name, target_len, target_start,
171 libcfs_nid2str(imp->imp_connection->c_peer.nid));
Peng Taod7e09d02013-05-02 16:46:55 +0800172 } else {
Joe Perches2d00bd12014-11-23 11:28:50 -0800173 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to %.*s (at %s) was lost; in progress operations using this service will fail\n",
174 imp->imp_obd->obd_name,
175 target_len, target_start,
176 libcfs_nid2str(imp->imp_connection->c_peer.nid));
Peng Taod7e09d02013-05-02 16:46:55 +0800177 }
Peng Taod7e09d02013-05-02 16:46:55 +0800178 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
179 spin_unlock(&imp->imp_lock);
180
181 if (obd_dump_on_timeout)
182 libcfs_debug_dumplog();
183
184 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
185 rc = 1;
186 } else {
187 spin_unlock(&imp->imp_lock);
188 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
189 imp->imp_client->cli_name, imp,
190 (imp->imp_state == LUSTRE_IMP_FULL &&
191 imp->imp_conn_cnt > conn_cnt) ?
192 "reconnected" : "not connected", imp->imp_conn_cnt,
193 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
194 }
195
196 return rc;
197}
198
Peng Taod7e09d02013-05-02 16:46:55 +0800199/*
200 * This acts as a barrier; all existing requests are rejected, and
201 * no new requests will be accepted until the import is valid again.
202 */
203void ptlrpc_deactivate_import(struct obd_import *imp)
204{
Maxime Lorrillere441fda82015-08-17 14:04:49 +0200205 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
206
Peng Taod7e09d02013-05-02 16:46:55 +0800207 spin_lock(&imp->imp_lock);
Maxime Lorrillere441fda82015-08-17 14:04:49 +0200208 imp->imp_invalid = 1;
209 imp->imp_generation++;
210 spin_unlock(&imp->imp_lock);
211
212 ptlrpc_abort_inflight(imp);
213 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
Peng Taod7e09d02013-05-02 16:46:55 +0800214}
215EXPORT_SYMBOL(ptlrpc_deactivate_import);
216
217static unsigned int
Arnd Bergmann219e6de2015-09-27 16:45:30 -0400218ptlrpc_inflight_deadline(struct ptlrpc_request *req, time64_t now)
Peng Taod7e09d02013-05-02 16:46:55 +0800219{
220 long dl;
221
222 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
223 (req->rq_phase == RQ_PHASE_BULK) ||
224 (req->rq_phase == RQ_PHASE_NEW)))
225 return 0;
226
227 if (req->rq_timedout)
228 return 0;
229
230 if (req->rq_phase == RQ_PHASE_NEW)
231 dl = req->rq_sent;
232 else
233 dl = req->rq_deadline;
234
235 if (dl <= now)
236 return 0;
237
238 return dl - now;
239}
240
241static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
242{
Arnd Bergmann219e6de2015-09-27 16:45:30 -0400243 time64_t now = ktime_get_real_seconds();
Peng Taod7e09d02013-05-02 16:46:55 +0800244 struct list_head *tmp, *n;
245 struct ptlrpc_request *req;
246 unsigned int timeout = 0;
247
248 spin_lock(&imp->imp_lock);
249 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
250 req = list_entry(tmp, struct ptlrpc_request, rq_list);
251 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
252 }
253 spin_unlock(&imp->imp_lock);
254 return timeout;
255}
256
257/**
258 * This function will invalidate the import, if necessary, then block
259 * for all the RPC completions, and finally notify the obd to
260 * invalidate its state (ie cancel locks, clear pending requests,
261 * etc).
262 */
263void ptlrpc_invalidate_import(struct obd_import *imp)
264{
265 struct list_head *tmp, *n;
266 struct ptlrpc_request *req;
267 struct l_wait_info lwi;
268 unsigned int timeout;
269 int rc;
270
271 atomic_inc(&imp->imp_inval_count);
272
273 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
274 ptlrpc_deactivate_import(imp);
275
Andriy Skulyshcca8fca2014-06-22 21:32:10 -0400276 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
Peng Taod7e09d02013-05-02 16:46:55 +0800277 LASSERT(imp->imp_invalid);
278
279 /* Wait forever until inflight == 0. We really can't do it another
280 * way because in some cases we need to wait for very long reply
281 * unlink. We can't do anything before that because there is really
Oleg Drokindadfcda2016-02-24 22:00:38 -0500282 * no guarantee that some rdma transfer is not in progress right now.
283 */
Peng Taod7e09d02013-05-02 16:46:55 +0800284 do {
285 /* Calculate max timeout for waiting on rpcs to error
286 * out. Use obd_timeout if calculated value is smaller
Oleg Drokindadfcda2016-02-24 22:00:38 -0500287 * than it.
288 */
Peng Taod7e09d02013-05-02 16:46:55 +0800289 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
Greg Donald1d8cb702014-08-25 20:07:19 -0500300 CDEBUG(D_RPCTRACE,
301 "Sleeping %d sec for inflight to error out\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800302 timeout);
303
304 /* Wait for all requests to error out and call completion
305 * callbacks. Cap it at obd_timeout -- these should all
Oleg Drokindadfcda2016-02-24 22:00:38 -0500306 * have been locally cancelled by ptlrpc_abort_inflight.
307 */
Peng Taod7e09d02013-05-02 16:46:55 +0800308 lwi = LWI_TIMEOUT_INTERVAL(
309 cfs_timeout_cap(cfs_time_seconds(timeout)),
310 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
311 NULL, NULL);
312 rc = l_wait_event(imp->imp_recovery_waitq,
313 (atomic_read(&imp->imp_inflight) == 0),
314 &lwi);
315 if (rc) {
316 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
317
318 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
319 cli_tgt, rc,
320 atomic_read(&imp->imp_inflight));
321
322 spin_lock(&imp->imp_lock);
323 if (atomic_read(&imp->imp_inflight) == 0) {
324 int count = atomic_read(&imp->imp_unregistering);
325
326 /* We know that "unregistering" rpcs only can
327 * survive in sending or delaying lists (they
328 * maybe waiting for long reply unlink in
329 * sluggish nets). Let's check this. If there
330 * is no inflight and unregistering != 0, this
Oleg Drokindadfcda2016-02-24 22:00:38 -0500331 * is bug.
332 */
Joe Perches2d00bd12014-11-23 11:28:50 -0800333 LASSERTF(count == 0, "Some RPCs are still unregistering: %d\n",
334 count);
Peng Taod7e09d02013-05-02 16:46:55 +0800335
336 /* Let's save one loop as soon as inflight have
337 * dropped to zero. No new inflights possible at
Oleg Drokindadfcda2016-02-24 22:00:38 -0500338 * this point.
339 */
Peng Taod7e09d02013-05-02 16:46:55 +0800340 rc = 0;
341 } else {
342 list_for_each_safe(tmp, n,
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500343 &imp->imp_sending_list) {
Peng Taod7e09d02013-05-02 16:46:55 +0800344 req = list_entry(tmp,
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500345 struct ptlrpc_request,
346 rq_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800347 DEBUG_REQ(D_ERROR, req,
348 "still on sending list");
349 }
350 list_for_each_safe(tmp, n,
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500351 &imp->imp_delayed_list) {
Peng Taod7e09d02013-05-02 16:46:55 +0800352 req = list_entry(tmp,
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500353 struct ptlrpc_request,
354 rq_list);
Peng Taod7e09d02013-05-02 16:46:55 +0800355 DEBUG_REQ(D_ERROR, req,
356 "still on delayed list");
357 }
358
Vitaly Fertman81ea39e2016-06-20 16:55:34 -0400359 CERROR("%s: Unregistering RPCs found (%d). Network is sluggish? Waiting them to error out.\n",
Joe Perches2d00bd12014-11-23 11:28:50 -0800360 cli_tgt,
Peng Taod7e09d02013-05-02 16:46:55 +0800361 atomic_read(&imp->
Joe Perches2d00bd12014-11-23 11:28:50 -0800362 imp_unregistering));
Peng Taod7e09d02013-05-02 16:46:55 +0800363 }
364 spin_unlock(&imp->imp_lock);
Chris Hannad0bfef32015-06-03 10:28:26 -0400365 }
Peng Taod7e09d02013-05-02 16:46:55 +0800366 } while (rc != 0);
367
368 /*
369 * Let's additionally check that no new rpcs added to import in
370 * "invalidate" state.
371 */
372 LASSERT(atomic_read(&imp->imp_inflight) == 0);
373 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
374 sptlrpc_import_flush_all_ctx(imp);
375
376 atomic_dec(&imp->imp_inval_count);
377 wake_up_all(&imp->imp_recovery_waitq);
378}
379EXPORT_SYMBOL(ptlrpc_invalidate_import);
380
381/* unset imp_invalid */
382void ptlrpc_activate_import(struct obd_import *imp)
383{
384 struct obd_device *obd = imp->imp_obd;
385
386 spin_lock(&imp->imp_lock);
Hongchao Zhang0b291b92014-04-27 13:06:29 -0400387 if (imp->imp_deactive != 0) {
388 spin_unlock(&imp->imp_lock);
389 return;
390 }
391
Peng Taod7e09d02013-05-02 16:46:55 +0800392 imp->imp_invalid = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800393 spin_unlock(&imp->imp_lock);
394 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
395}
396EXPORT_SYMBOL(ptlrpc_activate_import);
397
Andriy Skulyshcca8fca2014-06-22 21:32:10 -0400398static void ptlrpc_pinger_force(struct obd_import *imp)
399{
400 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
401 ptlrpc_import_state_name(imp->imp_state));
402
403 spin_lock(&imp->imp_lock);
404 imp->imp_force_verify = 1;
405 spin_unlock(&imp->imp_lock);
406
407 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
408 ptlrpc_pinger_wake_up();
409}
410
Peng Taod7e09d02013-05-02 16:46:55 +0800411void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
412{
Peng Taod7e09d02013-05-02 16:46:55 +0800413 LASSERT(!imp->imp_dlm_fake);
414
415 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
416 if (!imp->imp_replayable) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800417 CDEBUG(D_HA, "import %s@%s for %s not replayable, auto-deactivating\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800418 obd2cli_tgt(imp->imp_obd),
419 imp->imp_connection->c_remote_uuid.uuid,
420 imp->imp_obd->obd_name);
421 ptlrpc_deactivate_import(imp);
422 }
423
Andriy Skulyshcca8fca2014-06-22 21:32:10 -0400424 ptlrpc_pinger_force(imp);
Peng Taod7e09d02013-05-02 16:46:55 +0800425 }
Peng Taod7e09d02013-05-02 16:46:55 +0800426}
427EXPORT_SYMBOL(ptlrpc_fail_import);
428
429int ptlrpc_reconnect_import(struct obd_import *imp)
430{
Andriy Skulyshcca8fca2014-06-22 21:32:10 -0400431 struct l_wait_info lwi;
432 int secs = cfs_time_seconds(obd_timeout);
433 int rc;
434
435 ptlrpc_pinger_force(imp);
436
437 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
438 obd2cli_tgt(imp->imp_obd), secs);
439
440 lwi = LWI_TIMEOUT(secs, NULL, NULL);
441 rc = l_wait_event(imp->imp_recovery_waitq,
442 !ptlrpc_import_in_recovery(imp), &lwi);
443 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
444 ptlrpc_import_state_name(imp->imp_state));
445 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800446}
447EXPORT_SYMBOL(ptlrpc_reconnect_import);
448
449/**
450 * Connection on import \a imp is changed to another one (if more than one is
451 * present). We typically chose connection that we have not tried to connect to
452 * the longest
453 */
454static int import_select_connection(struct obd_import *imp)
455{
456 struct obd_import_conn *imp_conn = NULL, *conn;
457 struct obd_export *dlmexp;
458 char *target_start;
459 int target_len, tried_all = 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800460
461 spin_lock(&imp->imp_lock);
462
463 if (list_empty(&imp->imp_conn_list)) {
464 CERROR("%s: no connections available\n",
465 imp->imp_obd->obd_name);
466 spin_unlock(&imp->imp_lock);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800467 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800468 }
469
470 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700471 CDEBUG(D_HA, "%s: connect to NID %s last attempt %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800472 imp->imp_obd->obd_name,
473 libcfs_nid2str(conn->oic_conn->c_peer.nid),
474 conn->oic_last_attempt);
475
476 /* If we have not tried this connection since
Oleg Drokindadfcda2016-02-24 22:00:38 -0500477 * the last successful attempt, go with this one
478 */
Peng Taod7e09d02013-05-02 16:46:55 +0800479 if ((conn->oic_last_attempt == 0) ||
480 cfs_time_beforeq_64(conn->oic_last_attempt,
Oleg Drokin30c0aa32016-02-26 01:50:02 -0500481 imp->imp_last_success_conn)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800482 imp_conn = conn;
483 tried_all = 0;
484 break;
485 }
486
487 /* If all of the connections have already been tried
Oleg Drokindadfcda2016-02-24 22:00:38 -0500488 * since the last successful connection; just choose the
489 * least recently used
490 */
Peng Taod7e09d02013-05-02 16:46:55 +0800491 if (!imp_conn)
492 imp_conn = conn;
493 else if (cfs_time_before_64(conn->oic_last_attempt,
494 imp_conn->oic_last_attempt))
495 imp_conn = conn;
496 }
497
498 /* if not found, simply choose the current one */
499 if (!imp_conn || imp->imp_force_reconnect) {
500 LASSERT(imp->imp_conn_current);
501 imp_conn = imp->imp_conn_current;
502 tried_all = 0;
503 }
504 LASSERT(imp_conn->oic_conn);
505
506 /* If we've tried everything, and we're back to the beginning of the
Oleg Drokindadfcda2016-02-24 22:00:38 -0500507 * list, increase our timeout and try again. It will be reset when
508 * we do finally connect. (FIXME: really we should wait for all network
509 * state associated with the last connection attempt to drain before
510 * trying to reconnect on it.)
511 */
Peng Taod7e09d02013-05-02 16:46:55 +0800512 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
513 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300514
Peng Taod7e09d02013-05-02 16:46:55 +0800515 if (at_get(at) < CONNECTION_SWITCH_MAX) {
516 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
517 if (at_get(at) > CONNECTION_SWITCH_MAX)
518 at_reset(at, CONNECTION_SWITCH_MAX);
519 }
520 LASSERT(imp_conn->oic_last_attempt);
Joe Perches2d00bd12014-11-23 11:28:50 -0800521 CDEBUG(D_HA, "%s: tried all connections, increasing latency to %ds\n",
522 imp->imp_obd->obd_name, at_get(at));
Peng Taod7e09d02013-05-02 16:46:55 +0800523 }
524
525 imp_conn->oic_last_attempt = cfs_time_current_64();
526
527 /* switch connection, don't mind if it's same as the current one */
Markus Elfringa5cb8882015-07-14 09:35:42 +0200528 ptlrpc_connection_put(imp->imp_connection);
Peng Taod7e09d02013-05-02 16:46:55 +0800529 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
530
Chris Hannad0bfef32015-06-03 10:28:26 -0400531 dlmexp = class_conn2export(&imp->imp_dlm_handle);
Markus Elfringa5cb8882015-07-14 09:35:42 +0200532 ptlrpc_connection_put(dlmexp->exp_connection);
Peng Taod7e09d02013-05-02 16:46:55 +0800533 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
534 class_export_put(dlmexp);
535
536 if (imp->imp_conn_current != imp_conn) {
537 if (imp->imp_conn_current) {
538 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
539 &target_start, &target_len);
540
Joe Perches2d00bd12014-11-23 11:28:50 -0800541 CDEBUG(D_HA, "%s: Connection changing to %.*s (at %s)\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800542 imp->imp_obd->obd_name,
543 target_len, target_start,
544 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
545 }
546
547 imp->imp_conn_current = imp_conn;
548 }
549
550 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
551 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
552 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
553
554 spin_unlock(&imp->imp_lock);
555
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800556 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800557}
558
559/*
560 * must be called under imp_lock
561 */
562static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
563{
564 struct ptlrpc_request *req;
565 struct list_head *tmp;
566
Hongchao Zhang63d42572014-02-28 21:16:37 -0500567 /* The requests in committed_list always have smaller transnos than
Oleg Drokindadfcda2016-02-24 22:00:38 -0500568 * the requests in replay_list
569 */
Hongchao Zhang63d42572014-02-28 21:16:37 -0500570 if (!list_empty(&imp->imp_committed_list)) {
571 tmp = imp->imp_committed_list.next;
572 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
573 *transno = req->rq_transno;
574 if (req->rq_transno == 0) {
575 DEBUG_REQ(D_ERROR, req,
576 "zero transno in committed_list");
577 LBUG();
578 }
579 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800580 }
Hongchao Zhang63d42572014-02-28 21:16:37 -0500581 if (!list_empty(&imp->imp_replay_list)) {
582 tmp = imp->imp_replay_list.next;
583 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
584 *transno = req->rq_transno;
585 if (req->rq_transno == 0) {
586 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
587 LBUG();
588 }
589 return 1;
590 }
591 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800592}
593
594/**
595 * Attempt to (re)connect import \a imp. This includes all preparations,
596 * initializing CONNECT RPC request and passing it to ptlrpcd for
597 * actual sending.
598 * Returns 0 on success or error code.
599 */
600int ptlrpc_connect_import(struct obd_import *imp)
601{
602 struct obd_device *obd = imp->imp_obd;
603 int initial_connect = 0;
604 int set_transno = 0;
605 __u64 committed_before_reconnect = 0;
606 struct ptlrpc_request *request;
607 char *bufs[] = { NULL,
608 obd2cli_tgt(imp->imp_obd),
609 obd->obd_uuid.uuid,
610 (char *)&imp->imp_dlm_handle,
611 (char *)&imp->imp_connect_data };
612 struct ptlrpc_connect_async_args *aa;
613 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800614
615 spin_lock(&imp->imp_lock);
616 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
617 spin_unlock(&imp->imp_lock);
618 CERROR("can't connect to a closed import\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800619 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800620 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
621 spin_unlock(&imp->imp_lock);
622 CERROR("already connected\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800623 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800624 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
625 spin_unlock(&imp->imp_lock);
626 CERROR("already connecting\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800627 return -EALREADY;
Peng Taod7e09d02013-05-02 16:46:55 +0800628 }
629
630 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
631
632 imp->imp_conn_cnt++;
633 imp->imp_resend_replay = 0;
634
635 if (!lustre_handle_is_used(&imp->imp_remote_handle))
636 initial_connect = 1;
637 else
638 committed_before_reconnect = imp->imp_peer_committed_transno;
639
640 set_transno = ptlrpc_first_transno(imp,
641 &imp->imp_connect_data.ocd_transno);
642 spin_unlock(&imp->imp_lock);
643
644 rc = import_select_connection(imp);
645 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200646 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800647
Alexandr Terekhov5bcfab12014-05-31 18:20:39 +0300648 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +0800649 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200650 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800651
652 /* Reset connect flags to the originally requested flags, in case
Oleg Drokindadfcda2016-02-24 22:00:38 -0500653 * the server is updated on-the-fly we will get the new features.
654 */
Peng Taod7e09d02013-05-02 16:46:55 +0800655 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
656 /* Reset ocd_version each time so the server knows the exact versions */
657 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
658 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
659 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
660
661 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
662 &obd->obd_uuid, &imp->imp_connect_data, NULL);
663 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200664 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800665
666 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
Oleg Drokin8b382082016-02-16 00:46:58 -0500667 if (!request) {
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200668 rc = -ENOMEM;
669 goto out;
670 }
Peng Taod7e09d02013-05-02 16:46:55 +0800671
672 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
673 imp->imp_connect_op, bufs, NULL);
674 if (rc) {
675 ptlrpc_request_free(request);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200676 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800677 }
678
679 /* Report the rpc service time to the server so that it knows how long
Oleg Drokindadfcda2016-02-24 22:00:38 -0500680 * to wait for clients to join recovery
681 */
Peng Taod7e09d02013-05-02 16:46:55 +0800682 lustre_msg_set_service_time(request->rq_reqmsg,
683 at_timeout2est(request->rq_timeout));
684
685 /* The amount of time we give the server to process the connect req.
686 * import_select_connection will increase the net latency on
687 * repeated reconnect attempts to cover slow networks.
688 * We override/ignore the server rpc completion estimate here,
Oleg Drokindadfcda2016-02-24 22:00:38 -0500689 * which may be large if this is a reconnect attempt
690 */
Peng Taod7e09d02013-05-02 16:46:55 +0800691 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
692 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
693
694 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
695
Nathaniel Clark3d2b8f52016-06-09 22:35:17 -0400696 request->rq_no_resend = 1;
697 request->rq_no_delay = 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800698 request->rq_send_state = LUSTRE_IMP_CONNECTING;
699 /* Allow a slightly larger reply for future growth compatibility */
700 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
701 sizeof(struct obd_connect_data)+16*sizeof(__u64));
702 ptlrpc_request_set_replen(request);
703 request->rq_interpret_reply = ptlrpc_connect_interpret;
704
Kristina Martsenko3949015e2013-11-11 21:34:58 +0200705 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
Peng Taod7e09d02013-05-02 16:46:55 +0800706 aa = ptlrpc_req_async_args(request);
Joe Perchesec83e612013-10-13 20:22:03 -0700707 memset(aa, 0, sizeof(*aa));
Peng Taod7e09d02013-05-02 16:46:55 +0800708
709 aa->pcaa_peer_committed = committed_before_reconnect;
710 aa->pcaa_initial_connect = initial_connect;
711
712 if (aa->pcaa_initial_connect) {
713 spin_lock(&imp->imp_lock);
714 imp->imp_replayable = 1;
715 spin_unlock(&imp->imp_lock);
716 lustre_msg_add_op_flags(request->rq_reqmsg,
717 MSG_CONNECT_INITIAL);
718 }
719
720 if (set_transno)
721 lustre_msg_add_op_flags(request->rq_reqmsg,
722 MSG_CONNECT_TRANSNO);
723
724 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
725 request->rq_timeout);
Olaf Weberc5c4c6f2015-09-14 18:41:35 -0400726 ptlrpcd_add_req(request);
Peng Taod7e09d02013-05-02 16:46:55 +0800727 rc = 0;
728out:
Olaf Weberc5c4c6f2015-09-14 18:41:35 -0400729 if (rc != 0)
Peng Taod7e09d02013-05-02 16:46:55 +0800730 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
Peng Taod7e09d02013-05-02 16:46:55 +0800731
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800732 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800733}
734EXPORT_SYMBOL(ptlrpc_connect_import);
735
736static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
737{
738 int force_verify;
739
740 spin_lock(&imp->imp_lock);
741 force_verify = imp->imp_force_verify != 0;
742 spin_unlock(&imp->imp_lock);
743
744 if (force_verify)
745 ptlrpc_pinger_wake_up();
746}
747
748static int ptlrpc_busy_reconnect(int rc)
749{
750 return (rc == -EBUSY) || (rc == -EAGAIN);
751}
752
753/**
754 * interpret_reply callback for connect RPCs.
755 * Looks into returned status of connect operation and decides
756 * what to do with the import - i.e enter recovery, promote it to
757 * full state for normal operations of disconnect it due to an error.
758 */
759static int ptlrpc_connect_interpret(const struct lu_env *env,
760 struct ptlrpc_request *request,
761 void *data, int rc)
762{
763 struct ptlrpc_connect_async_args *aa = data;
764 struct obd_import *imp = request->rq_import;
765 struct client_obd *cli = &imp->imp_obd->u.cli;
766 struct lustre_handle old_hdl;
767 __u64 old_connect_flags;
768 int msg_flags;
769 struct obd_connect_data *ocd;
770 struct obd_export *exp;
771 int ret;
Peng Taod7e09d02013-05-02 16:46:55 +0800772
773 spin_lock(&imp->imp_lock);
774 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
775 imp->imp_connect_tried = 1;
776 spin_unlock(&imp->imp_lock);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800777 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800778 }
779
780 if (rc) {
781 /* if this reconnect to busy export - not need select new target
Oleg Drokindadfcda2016-02-24 22:00:38 -0500782 * for connecting
783 */
Peng Taod7e09d02013-05-02 16:46:55 +0800784 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
785 spin_unlock(&imp->imp_lock);
786 ptlrpc_maybe_ping_import_soon(imp);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200787 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800788 }
789 spin_unlock(&imp->imp_lock);
790
791 LASSERT(imp->imp_conn_current);
792
793 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
794
795 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
796 RCL_SERVER);
797 /* server replied obd_connect_data is always bigger */
798 ocd = req_capsule_server_sized_get(&request->rq_pill,
799 &RMF_CONNECT_DATA, ret);
800
Oleg Drokin8b382082016-02-16 00:46:58 -0500801 if (!ocd) {
Peng Taod7e09d02013-05-02 16:46:55 +0800802 CERROR("%s: no connect data from server\n",
803 imp->imp_obd->obd_name);
804 rc = -EPROTO;
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200805 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800806 }
807
808 spin_lock(&imp->imp_lock);
809
810 /* All imports are pingable */
811 imp->imp_pingable = 1;
812 imp->imp_force_reconnect = 0;
813 imp->imp_force_verify = 0;
814
815 imp->imp_connect_data = *ocd;
816
817 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
818 imp->imp_obd->obd_name, ocd->ocd_instance);
819 exp = class_conn2export(&imp->imp_dlm_handle);
820
821 spin_unlock(&imp->imp_lock);
822
823 /* check that server granted subset of flags we asked for. */
824 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
825 ocd->ocd_connect_flags) {
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700826 CERROR("%s: Server didn't granted asked subset of flags: asked=%#llx grranted=%#llx\n",
Greg Donald1d8cb702014-08-25 20:07:19 -0500827 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
Peng Taod7e09d02013-05-02 16:46:55 +0800828 ocd->ocd_connect_flags);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200829 rc = -EPROTO;
830 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800831 }
832
833 if (!exp) {
834 /* This could happen if export is cleaned during the
Oleg Drokindadfcda2016-02-24 22:00:38 -0500835 * connect attempt
836 */
Peng Taod7e09d02013-05-02 16:46:55 +0800837 CERROR("%s: missing export after connect\n",
838 imp->imp_obd->obd_name);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200839 rc = -ENODEV;
840 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800841 }
842 old_connect_flags = exp_connect_flags(exp);
843 exp->exp_connect_data = *ocd;
844 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
845 class_export_put(exp);
846
847 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
848
849 if (aa->pcaa_initial_connect) {
850 spin_lock(&imp->imp_lock);
851 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
852 imp->imp_replayable = 1;
853 spin_unlock(&imp->imp_lock);
854 CDEBUG(D_HA, "connected to replayable target: %s\n",
855 obd2cli_tgt(imp->imp_obd));
856 } else {
857 imp->imp_replayable = 0;
858 spin_unlock(&imp->imp_lock);
859 }
860
861 /* if applies, adjust the imp->imp_msg_magic here
Oleg Drokindadfcda2016-02-24 22:00:38 -0500862 * according to reply flags
863 */
Peng Taod7e09d02013-05-02 16:46:55 +0800864
865 imp->imp_remote_handle =
866 *lustre_msg_get_handle(request->rq_repmsg);
867
868 /* Initial connects are allowed for clients with non-random
869 * uuids when servers are in recovery. Simply signal the
Oleg Drokindadfcda2016-02-24 22:00:38 -0500870 * servers replay is complete and wait in REPLAY_WAIT.
871 */
Peng Taod7e09d02013-05-02 16:46:55 +0800872 if (msg_flags & MSG_CONNECT_RECOVERING) {
873 CDEBUG(D_HA, "connect to %s during recovery\n",
874 obd2cli_tgt(imp->imp_obd));
875 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
876 } else {
877 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
878 ptlrpc_activate_import(imp);
879 }
880
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200881 rc = 0;
882 goto finish;
Peng Taod7e09d02013-05-02 16:46:55 +0800883 }
884
885 /* Determine what recovery state to move the import to. */
Julia Lawall2b241d32015-08-29 19:30:12 +0200886 if (msg_flags & MSG_CONNECT_RECONNECT) {
Peng Taod7e09d02013-05-02 16:46:55 +0800887 memset(&old_hdl, 0, sizeof(old_hdl));
888 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
Kristina Martsenko3949015e2013-11-11 21:34:58 +0200889 sizeof(old_hdl))) {
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700890 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due bad handle %#llx\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800891 obd2cli_tgt(imp->imp_obd),
892 imp->imp_connection->c_remote_uuid.uuid,
893 imp->imp_dlm_handle.cookie);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200894 rc = -ENOTCONN;
895 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800896 }
897
898 if (memcmp(&imp->imp_remote_handle,
899 lustre_msg_get_handle(request->rq_repmsg),
900 sizeof(imp->imp_remote_handle))) {
901 int level = msg_flags & MSG_CONNECT_RECOVERING ?
902 D_HA : D_WARNING;
903
904 /* Bug 16611/14775: if server handle have changed,
905 * that means some sort of disconnection happened.
906 * If the server is not in recovery, that also means it
907 * already erased all of our state because of previous
908 * eviction. If it is in recovery - we are safe to
909 * participate since we can reestablish all of our state
Oleg Drokindadfcda2016-02-24 22:00:38 -0500910 * with server again
911 */
Julia Lawall2b241d32015-08-29 19:30:12 +0200912 if ((msg_flags & MSG_CONNECT_RECOVERING)) {
Hema Prathabanb533ff42014-08-31 16:30:56 +0530913 CDEBUG(level, "%s@%s changed server handle from %#llx to %#llx but is still in recovery\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800914 obd2cli_tgt(imp->imp_obd),
915 imp->imp_connection->c_remote_uuid.uuid,
916 imp->imp_remote_handle.cookie,
917 lustre_msg_get_handle(
918 request->rq_repmsg)->cookie);
919 } else {
Joe Perches2d00bd12014-11-23 11:28:50 -0800920 LCONSOLE_WARN("Evicted from %s (at %s) after server handle changed from %#llx to %#llx\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800921 obd2cli_tgt(imp->imp_obd),
922 imp->imp_connection-> \
923 c_remote_uuid.uuid,
924 imp->imp_remote_handle.cookie,
925 lustre_msg_get_handle(
Joe Perches2d00bd12014-11-23 11:28:50 -0800926 request->rq_repmsg)->cookie);
Peng Taod7e09d02013-05-02 16:46:55 +0800927 }
928
Peng Taod7e09d02013-05-02 16:46:55 +0800929 imp->imp_remote_handle =
930 *lustre_msg_get_handle(request->rq_repmsg);
931
Julia Lawall2b241d32015-08-29 19:30:12 +0200932 if (!(msg_flags & MSG_CONNECT_RECOVERING)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800933 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +0200934 rc = 0;
935 goto finish;
Peng Taod7e09d02013-05-02 16:46:55 +0800936 }
937
938 } else {
939 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
940 obd2cli_tgt(imp->imp_obd),
941 imp->imp_connection->c_remote_uuid.uuid);
942 }
943
944 if (imp->imp_invalid) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800945 CDEBUG(D_HA, "%s: reconnected but import is invalid; marking evicted\n",
946 imp->imp_obd->obd_name);
Peng Taod7e09d02013-05-02 16:46:55 +0800947 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
Julia Lawall2b241d32015-08-29 19:30:12 +0200948 } else if (msg_flags & MSG_CONNECT_RECOVERING) {
Peng Taod7e09d02013-05-02 16:46:55 +0800949 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
950 imp->imp_obd->obd_name,
951 obd2cli_tgt(imp->imp_obd));
952
953 spin_lock(&imp->imp_lock);
954 imp->imp_resend_replay = 1;
955 spin_unlock(&imp->imp_lock);
956
Andriy Skulysh502cb582015-03-25 22:07:07 -0400957 IMPORT_SET_STATE(imp, imp->imp_replay_state);
Peng Taod7e09d02013-05-02 16:46:55 +0800958 } else {
959 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
960 }
Julia Lawall2b241d32015-08-29 19:30:12 +0200961 } else if ((msg_flags & MSG_CONNECT_RECOVERING) && !imp->imp_invalid) {
Peng Taod7e09d02013-05-02 16:46:55 +0800962 LASSERT(imp->imp_replayable);
963 imp->imp_remote_handle =
964 *lustre_msg_get_handle(request->rq_repmsg);
965 imp->imp_last_replay_transno = 0;
966 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
967 } else {
Joe Perches2d00bd12014-11-23 11:28:50 -0800968 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags not set: %x)",
969 imp->imp_obd->obd_name, msg_flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800970 imp->imp_remote_handle =
971 *lustre_msg_get_handle(request->rq_repmsg);
972 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
973 }
974
975 /* Sanity checks for a reconnected import. */
976 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800977 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
Peng Taod7e09d02013-05-02 16:46:55 +0800978 }
979
980 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
981 lustre_msg_get_last_committed(request->rq_repmsg) <
982 aa->pcaa_peer_committed) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800983 CERROR("%s went back in time (transno %lld was previously committed, server now claims %lld)! See https://bugzilla.lustre.org/show_bug.cgi?id=9646\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800984 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
985 lustre_msg_get_last_committed(request->rq_repmsg));
986 }
987
988finish:
989 rc = ptlrpc_import_recovery_state_machine(imp);
990 if (rc != 0) {
991 if (rc == -ENOTCONN) {
Joe Perches2d00bd12014-11-23 11:28:50 -0800992 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery; invalidating and reconnecting\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800993 obd2cli_tgt(imp->imp_obd),
994 imp->imp_connection->c_remote_uuid.uuid);
995 ptlrpc_connect_import(imp);
996 imp->imp_connect_tried = 1;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800997 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800998 }
999 } else {
Andreas Dilger35e45812016-04-27 18:21:07 -04001000 static bool warned;
1001
Peng Taod7e09d02013-05-02 16:46:55 +08001002 spin_lock(&imp->imp_lock);
1003 list_del(&imp->imp_conn_current->oic_item);
Oleg Drokin30c0aa32016-02-26 01:50:02 -05001004 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
Peng Taod7e09d02013-05-02 16:46:55 +08001005 imp->imp_last_success_conn =
1006 imp->imp_conn_current->oic_last_attempt;
1007
1008 spin_unlock(&imp->imp_lock);
1009
Fan Yongf261f482014-08-15 12:48:09 -04001010 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
1011 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
Joe Perches2d00bd12014-11-23 11:28:50 -08001012 LCONSOLE_WARN("%s: MDS %s does not support ibits lock, either very old or invalid: requested %llx, replied %llx\n",
Fan Yongf261f482014-08-15 12:48:09 -04001013 imp->imp_obd->obd_name,
1014 imp->imp_connection->c_remote_uuid.uuid,
1015 imp->imp_connect_flags_orig,
1016 ocd->ocd_connect_flags);
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001017 rc = -EPROTO;
1018 goto out;
Fan Yongf261f482014-08-15 12:48:09 -04001019 }
Peng Taod7e09d02013-05-02 16:46:55 +08001020
Andreas Dilger35e45812016-04-27 18:21:07 -04001021 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
Peng Taod7e09d02013-05-02 16:46:55 +08001022 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1023 LUSTRE_VERSION_OFFSET_WARN ||
1024 ocd->ocd_version < LUSTRE_VERSION_CODE -
1025 LUSTRE_VERSION_OFFSET_WARN)) {
1026 /* Sigh, some compilers do not like #ifdef in the middle
Oleg Drokindadfcda2016-02-24 22:00:38 -05001027 * of macro arguments
1028 */
Andreas Dilger35e45812016-04-27 18:21:07 -04001029 const char *older = "older than client. Consider upgrading server";
1030 const char *newer = "newer than client. Consider recompiling application";
Peng Taod7e09d02013-05-02 16:46:55 +08001031
Joe Perches2d00bd12014-11-23 11:28:50 -08001032 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) is much %s (%s)\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001033 obd2cli_tgt(imp->imp_obd),
1034 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1035 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1036 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1037 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1038 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1039 newer : older, LUSTRE_VERSION_STRING);
Andreas Dilger35e45812016-04-27 18:21:07 -04001040 warned = true;
Peng Taod7e09d02013-05-02 16:46:55 +08001041 }
1042
1043#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1044 /* Check if server has LU-1252 fix applied to not always swab
1045 * the IR MNE entries. Do this only once per connection. This
1046 * fixup is version-limited, because we don't want to carry the
1047 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
1048 * need interop with unpatched 2.2 servers. For newer servers,
Oleg Drokindadfcda2016-02-24 22:00:38 -05001049 * the client will do MNE swabbing only as needed. LU-1644
1050 */
Peng Taod7e09d02013-05-02 16:46:55 +08001051 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1052 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
1053 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
1054 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
1055 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
1056 strcmp(imp->imp_obd->obd_type->typ_name,
1057 LUSTRE_MGC_NAME) == 0))
1058 imp->imp_need_mne_swab = 1;
1059 else /* clear if server was upgraded since last connect */
1060 imp->imp_need_mne_swab = 0;
1061#else
1062#warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1063#endif
1064
1065 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1066 /* We sent to the server ocd_cksum_types with bits set
1067 * for algorithms we understand. The server masked off
Oleg Drokindadfcda2016-02-24 22:00:38 -05001068 * the checksum types it doesn't support
1069 */
Peng Taod7e09d02013-05-02 16:46:55 +08001070 if ((ocd->ocd_cksum_types &
1071 cksum_types_supported_client()) == 0) {
Joe Perches2d00bd12014-11-23 11:28:50 -08001072 LCONSOLE_WARN("The negotiation of the checksum algorithm to use with server %s failed (%x/%x), disabling checksums\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001073 obd2cli_tgt(imp->imp_obd),
1074 ocd->ocd_cksum_types,
1075 cksum_types_supported_client());
1076 cli->cl_checksum = 0;
1077 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1078 } else {
1079 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1080 }
1081 } else {
1082 /* The server does not support OBD_CONNECT_CKSUM.
Oleg Drokindadfcda2016-02-24 22:00:38 -05001083 * Enforce ADLER for backward compatibility
1084 */
Peng Taod7e09d02013-05-02 16:46:55 +08001085 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1086 }
Hema Prathabanb533ff42014-08-31 16:30:56 +05301087 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
Peng Taod7e09d02013-05-02 16:46:55 +08001088
1089 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
1090 cli->cl_max_pages_per_rpc =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 min(ocd->ocd_brw_size >> PAGE_SHIFT,
Peng Taod7e09d02013-05-02 16:46:55 +08001092 cli->cl_max_pages_per_rpc);
1093 else if (imp->imp_connect_op == MDS_CONNECT ||
1094 imp->imp_connect_op == MGS_CONNECT)
1095 cli->cl_max_pages_per_rpc = 1;
1096
1097 /* Reset ns_connect_flags only for initial connect. It might be
1098 * changed in while using FS and if we reset it in reconnect
1099 * this leads to losing user settings done before such as
Oleg Drokindadfcda2016-02-24 22:00:38 -05001100 * disable lru_resize, etc.
1101 */
Peng Taod7e09d02013-05-02 16:46:55 +08001102 if (old_connect_flags != exp_connect_flags(exp) ||
1103 aa->pcaa_initial_connect) {
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -07001104 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server flags: %#llx\n",
1105 imp->imp_obd->obd_name, ocd->ocd_connect_flags);
Peng Taod7e09d02013-05-02 16:46:55 +08001106 imp->imp_obd->obd_namespace->ns_connect_flags =
1107 ocd->ocd_connect_flags;
1108 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1109 ocd->ocd_connect_flags;
1110 }
1111
1112 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1113 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1114 /* We need a per-message support flag, because
Oleg Drokindadfcda2016-02-24 22:00:38 -05001115 * a. we don't know if the incoming connect reply
1116 * supports AT or not (in reply_in_callback)
1117 * until we unpack it.
1118 * b. failovered server means export and flags are gone
1119 * (in ptlrpc_send_reply).
1120 * Can only be set when we know AT is supported at
1121 * both ends
1122 */
Peng Taod7e09d02013-05-02 16:46:55 +08001123 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1124 else
1125 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1126
1127 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1128 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1129 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1130 else
1131 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1132
1133 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1134 (cli->cl_max_pages_per_rpc > 0));
1135 }
1136
1137out:
1138 imp->imp_connect_tried = 1;
1139
1140 if (rc != 0) {
1141 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1142 if (rc == -EACCES) {
1143 /*
1144 * Give up trying to reconnect
1145 * EACCES means client has no permission for connection
1146 */
1147 imp->imp_obd->obd_no_recov = 1;
1148 ptlrpc_deactivate_import(imp);
1149 }
1150
1151 if (rc == -EPROTO) {
1152 struct obd_connect_data *ocd;
1153
1154 /* reply message might not be ready */
Oleg Drokin8b382082016-02-16 00:46:58 -05001155 if (!request->rq_repmsg)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001156 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08001157
1158 ocd = req_capsule_server_get(&request->rq_pill,
1159 &RMF_CONNECT_DATA);
1160 if (ocd &&
1161 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1162 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
Kristina Martsenko532118c2013-11-11 21:35:03 +02001163 /*
1164 * Actually servers are only supposed to refuse
1165 * connection from liblustre clients, so we
1166 * should never see this from VFS context
1167 */
Joe Perches2d00bd12014-11-23 11:28:50 -08001168 LCONSOLE_ERROR_MSG(0x16a, "Server %s version (%d.%d.%d.%d) refused connection from this client with an incompatible version (%s). Client must be recompiled\n",
1169 obd2cli_tgt(imp->imp_obd),
1170 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1171 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1172 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1173 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1174 LUSTRE_VERSION_STRING);
Peng Taod7e09d02013-05-02 16:46:55 +08001175 ptlrpc_deactivate_import(imp);
1176 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1177 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001178 return -EPROTO;
Peng Taod7e09d02013-05-02 16:46:55 +08001179 }
1180
1181 ptlrpc_maybe_ping_import_soon(imp);
1182
1183 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1184 obd2cli_tgt(imp->imp_obd),
1185 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1186 }
1187
1188 wake_up_all(&imp->imp_recovery_waitq);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001189 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001190}
1191
1192/**
1193 * interpret callback for "completed replay" RPCs.
1194 * \see signal_completed_replay
1195 */
1196static int completed_replay_interpret(const struct lu_env *env,
1197 struct ptlrpc_request *req,
Greg Donaldaff9d8e2014-08-21 11:07:42 -05001198 void *data, int rc)
Peng Taod7e09d02013-05-02 16:46:55 +08001199{
Peng Taod7e09d02013-05-02 16:46:55 +08001200 atomic_dec(&req->rq_import->imp_replay_inflight);
1201 if (req->rq_status == 0 &&
1202 !req->rq_import->imp_vbr_failed) {
1203 ptlrpc_import_recovery_state_machine(req->rq_import);
1204 } else {
1205 if (req->rq_import->imp_vbr_failed) {
1206 CDEBUG(D_WARNING,
1207 "%s: version recovery fails, reconnecting\n",
1208 req->rq_import->imp_obd->obd_name);
1209 } else {
Joe Perches2d00bd12014-11-23 11:28:50 -08001210 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, reconnecting\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001211 req->rq_import->imp_obd->obd_name,
1212 req->rq_status);
1213 }
1214 ptlrpc_connect_import(req->rq_import);
1215 }
1216
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001217 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001218}
1219
1220/**
1221 * Let server know that we have no requests to replay anymore.
1222 * Achieved by just sending a PING request
1223 */
1224static int signal_completed_replay(struct obd_import *imp)
1225{
1226 struct ptlrpc_request *req;
Peng Taod7e09d02013-05-02 16:46:55 +08001227
1228 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001229 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001230
1231 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1232 atomic_inc(&imp->imp_replay_inflight);
1233
1234 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1235 OBD_PING);
Oleg Drokin8b382082016-02-16 00:46:58 -05001236 if (!req) {
Peng Taod7e09d02013-05-02 16:46:55 +08001237 atomic_dec(&imp->imp_replay_inflight);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001238 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08001239 }
1240
1241 ptlrpc_request_set_replen(req);
1242 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1243 lustre_msg_add_flags(req->rq_reqmsg,
1244 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1245 if (AT_OFF)
1246 req->rq_timeout *= 3;
1247 req->rq_interpret_reply = completed_replay_interpret;
1248
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04001249 ptlrpcd_add_req(req);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001250 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001251}
1252
1253/**
1254 * In kernel code all import invalidation happens in its own
1255 * separate thread, so that whatever application happened to encounter
1256 * a problem could still be killed or otherwise continue
1257 */
1258static int ptlrpc_invalidate_import_thread(void *data)
1259{
1260 struct obd_import *imp = data;
1261
Peng Taod7e09d02013-05-02 16:46:55 +08001262 unshare_fs_struct();
1263
1264 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1265 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1266 imp->imp_connection->c_remote_uuid.uuid);
1267
1268 ptlrpc_invalidate_import(imp);
1269
1270 if (obd_dump_on_eviction) {
1271 CERROR("dump the log upon eviction\n");
1272 libcfs_debug_dumplog();
1273 }
1274
1275 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1276 ptlrpc_import_recovery_state_machine(imp);
1277
1278 class_import_put(imp);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001279 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001280}
1281
1282/**
1283 * This is the state machine for client-side recovery on import.
1284 *
Masanari Iidab6da17f2014-02-08 00:30:40 +09001285 * Typically we have two possibly paths. If we came to server and it is not
Peng Taod7e09d02013-05-02 16:46:55 +08001286 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1287 * state and reconnect from scratch.
1288 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1289 * We go through our list of requests to replay and send them to server one by
1290 * one.
1291 * After sending all request from the list we change import state to
1292 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1293 * and also all the locks we don't yet have and wait for server to grant us.
1294 * After that we send a special "replay completed" request and change import
1295 * state to IMP_REPLAY_WAIT.
1296 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1297 * state and resend all requests from sending list.
1298 * After that we promote import to FULL state and send all delayed requests
1299 * and import is fully operational after that.
1300 *
1301 */
1302int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1303{
1304 int rc = 0;
1305 int inflight;
1306 char *target_start;
1307 int target_len;
1308
Peng Taod7e09d02013-05-02 16:46:55 +08001309 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1310 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1311 &target_start, &target_len);
1312 /* Don't care about MGC eviction */
1313 if (strcmp(imp->imp_obd->obd_type->typ_name,
1314 LUSTRE_MGC_NAME) != 0) {
Joe Perches2d00bd12014-11-23 11:28:50 -08001315 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted by %.*s; in progress operations using this service will fail.\n",
Peng Taod7e09d02013-05-02 16:46:55 +08001316 imp->imp_obd->obd_name, target_len,
1317 target_start);
1318 }
1319 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1320 obd2cli_tgt(imp->imp_obd),
1321 imp->imp_connection->c_remote_uuid.uuid);
1322 /* reset vbr_failed flag upon eviction */
1323 spin_lock(&imp->imp_lock);
1324 imp->imp_vbr_failed = 0;
1325 spin_unlock(&imp->imp_lock);
1326
1327 {
Greg Kroah-Hartman68b636b2013-08-04 08:56:42 +08001328 struct task_struct *task;
Peng Taod7e09d02013-05-02 16:46:55 +08001329 /* bug 17802: XXX client_disconnect_export vs connect request
Frank Zago9c379662016-03-02 17:02:02 -05001330 * race. if client is evicted at this time, we start
Peng Taod7e09d02013-05-02 16:46:55 +08001331 * invalidate thread without reference to import and import can
Oleg Drokindadfcda2016-02-24 22:00:38 -05001332 * be freed at same time.
1333 */
Peng Taod7e09d02013-05-02 16:46:55 +08001334 class_import_get(imp);
1335 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
Oleg Drokin30c0aa32016-02-26 01:50:02 -05001336 "ll_imp_inval");
Peng Taod7e09d02013-05-02 16:46:55 +08001337 if (IS_ERR(task)) {
1338 class_import_put(imp);
1339 CERROR("error starting invalidate thread: %d\n", rc);
1340 rc = PTR_ERR(task);
1341 } else {
1342 rc = 0;
1343 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001344 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001345 }
1346 }
1347
1348 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1349 CDEBUG(D_HA, "replay requested by %s\n",
1350 obd2cli_tgt(imp->imp_obd));
1351 rc = ptlrpc_replay_next(imp, &inflight);
1352 if (inflight == 0 &&
1353 atomic_read(&imp->imp_replay_inflight) == 0) {
1354 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1355 rc = ldlm_replay_locks(imp);
1356 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001357 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001358 }
1359 rc = 0;
1360 }
1361
1362 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1363 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1364 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1365 rc = signal_completed_replay(imp);
1366 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001367 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001368 }
Peng Taod7e09d02013-05-02 16:46:55 +08001369 }
1370
1371 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1372 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1373 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1374 }
1375 }
1376
1377 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1378 CDEBUG(D_HA, "reconnected to %s@%s\n",
1379 obd2cli_tgt(imp->imp_obd),
1380 imp->imp_connection->c_remote_uuid.uuid);
1381
1382 rc = ptlrpc_resend(imp);
1383 if (rc)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001384 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001385 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1386 ptlrpc_activate_import(imp);
1387
1388 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1389 &target_start, &target_len);
1390 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1391 imp->imp_obd->obd_name,
1392 target_len, target_start,
1393 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1394 }
1395
1396 if (imp->imp_state == LUSTRE_IMP_FULL) {
1397 wake_up_all(&imp->imp_recovery_waitq);
1398 ptlrpc_wake_delayed(imp);
1399 }
1400
1401out:
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001402 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001403}
1404
1405int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1406{
1407 struct ptlrpc_request *req;
1408 int rq_opc, rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001409
Andreas Dilger88291a72014-04-27 13:06:33 -04001410 if (imp->imp_obd->obd_force)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001411 goto set_state;
Peng Taod7e09d02013-05-02 16:46:55 +08001412
1413 switch (imp->imp_connect_op) {
Andreas Dilger88291a72014-04-27 13:06:33 -04001414 case OST_CONNECT:
1415 rq_opc = OST_DISCONNECT;
1416 break;
1417 case MDS_CONNECT:
1418 rq_opc = MDS_DISCONNECT;
1419 break;
1420 case MGS_CONNECT:
1421 rq_opc = MGS_DISCONNECT;
1422 break;
Peng Taod7e09d02013-05-02 16:46:55 +08001423 default:
Andreas Dilger88291a72014-04-27 13:06:33 -04001424 rc = -EINVAL;
Joe Perches2d00bd12014-11-23 11:28:50 -08001425 CERROR("%s: don't know how to disconnect from %s (connect_op %d): rc = %d\n",
Andreas Dilger88291a72014-04-27 13:06:33 -04001426 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1427 imp->imp_connect_op, rc);
1428 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001429 }
1430
1431 if (ptlrpc_import_in_recovery(imp)) {
1432 struct l_wait_info lwi;
Greg Kroah-Hartmanb2d201b2014-07-12 00:45:51 -07001433 long timeout;
Peng Taod7e09d02013-05-02 16:46:55 +08001434
Peng Taod7e09d02013-05-02 16:46:55 +08001435 if (AT_OFF) {
1436 if (imp->imp_server_timeout)
1437 timeout = cfs_time_seconds(obd_timeout / 2);
1438 else
1439 timeout = cfs_time_seconds(obd_timeout);
1440 } else {
1441 int idx = import_at_get_index(imp,
1442 imp->imp_client->cli_request_portal);
1443 timeout = cfs_time_seconds(
1444 at_get(&imp->imp_at.iat_service_estimate[idx]));
1445 }
1446
1447 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1448 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1449 rc = l_wait_event(imp->imp_recovery_waitq,
1450 !ptlrpc_import_in_recovery(imp), &lwi);
Peng Taod7e09d02013-05-02 16:46:55 +08001451 }
1452
1453 spin_lock(&imp->imp_lock);
1454 if (imp->imp_state != LUSTRE_IMP_FULL)
Julia Lawalla9b3e8f2014-09-07 18:18:29 +02001455 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001456 spin_unlock(&imp->imp_lock);
1457
1458 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1459 LUSTRE_OBD_VERSION, rq_opc);
1460 if (req) {
1461 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1462 * it fails. We can get through the above with a down server
Oleg Drokindadfcda2016-02-24 22:00:38 -05001463 * if the client doesn't know the server is gone yet.
1464 */
Peng Taod7e09d02013-05-02 16:46:55 +08001465 req->rq_no_resend = 1;
1466
1467 /* We want client umounts to happen quickly, no matter the
Oleg Drokindadfcda2016-02-24 22:00:38 -05001468 * server state...
1469 */
Peng Taod7e09d02013-05-02 16:46:55 +08001470 req->rq_timeout = min_t(int, req->rq_timeout,
1471 INITIAL_CONNECT_TIMEOUT);
1472
1473 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
Chris Hannad0bfef32015-06-03 10:28:26 -04001474 req->rq_send_state = LUSTRE_IMP_CONNECTING;
Peng Taod7e09d02013-05-02 16:46:55 +08001475 ptlrpc_request_set_replen(req);
1476 rc = ptlrpc_queue_wait(req);
1477 ptlrpc_req_finished(req);
1478 }
1479
1480set_state:
1481 spin_lock(&imp->imp_lock);
1482out:
1483 if (noclose)
1484 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1485 else
1486 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1487 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1488 spin_unlock(&imp->imp_lock);
1489
Andreas Dilger88291a72014-04-27 13:06:33 -04001490 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1491 rc = 0;
1492
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001493 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001494}
1495EXPORT_SYMBOL(ptlrpc_disconnect_import);
1496
Peng Taod7e09d02013-05-02 16:46:55 +08001497/* Adaptive Timeout utils */
1498extern unsigned int at_min, at_max, at_history;
1499
1500/* Bin into timeslices using AT_BINS bins.
Oleg Drokindadfcda2016-02-24 22:00:38 -05001501 * This gives us a max of the last binlimit*AT_BINS secs without the storage,
1502 * but still smoothing out a return to normalcy from a slow response.
1503 * (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1504 */
Peng Taod7e09d02013-05-02 16:46:55 +08001505int at_measured(struct adaptive_timeout *at, unsigned int val)
1506{
1507 unsigned int old = at->at_current;
Arnd Bergmann0ac04782015-09-27 16:45:19 -04001508 time64_t now = ktime_get_real_seconds();
1509 long binlimit = max_t(long, at_history / AT_BINS, 1);
Peng Taod7e09d02013-05-02 16:46:55 +08001510
1511 LASSERT(at);
1512 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
Arnd Bergmann0ac04782015-09-27 16:45:19 -04001513 val, at, (long)(now - at->at_binstart), at->at_current,
Peng Taod7e09d02013-05-02 16:46:55 +08001514 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1515
1516 if (val == 0)
1517 /* 0's don't count, because we never want our timeout to
Oleg Drokindadfcda2016-02-24 22:00:38 -05001518 * drop to 0, and because 0 could mean an error
1519 */
Peng Taod7e09d02013-05-02 16:46:55 +08001520 return 0;
1521
1522 spin_lock(&at->at_lock);
1523
1524 if (unlikely(at->at_binstart == 0)) {
1525 /* Special case to remove default from history */
1526 at->at_current = val;
1527 at->at_worst_ever = val;
1528 at->at_worst_time = now;
1529 at->at_hist[0] = val;
1530 at->at_binstart = now;
Kristina Martsenko3949015e2013-11-11 21:34:58 +02001531 } else if (now - at->at_binstart < binlimit) {
Peng Taod7e09d02013-05-02 16:46:55 +08001532 /* in bin 0 */
1533 at->at_hist[0] = max(val, at->at_hist[0]);
1534 at->at_current = max(val, at->at_current);
1535 } else {
1536 int i, shift;
1537 unsigned int maxv = val;
1538 /* move bins over */
Arnd Bergmann0ac04782015-09-27 16:45:19 -04001539 shift = (u32)(now - at->at_binstart) / binlimit;
Peng Taod7e09d02013-05-02 16:46:55 +08001540 LASSERT(shift > 0);
Kristina Martsenko3949015e2013-11-11 21:34:58 +02001541 for (i = AT_BINS - 1; i >= 0; i--) {
Peng Taod7e09d02013-05-02 16:46:55 +08001542 if (i >= shift) {
1543 at->at_hist[i] = at->at_hist[i - shift];
1544 maxv = max(maxv, at->at_hist[i]);
1545 } else {
1546 at->at_hist[i] = 0;
1547 }
1548 }
1549 at->at_hist[0] = val;
1550 at->at_current = maxv;
1551 at->at_binstart += shift * binlimit;
1552 }
1553
1554 if (at->at_current > at->at_worst_ever) {
1555 at->at_worst_ever = at->at_current;
1556 at->at_worst_time = now;
1557 }
1558
1559 if (at->at_flags & AT_FLG_NOHIST)
1560 /* Only keep last reported val; keeping the rest of the history
Oleg Drokindadfcda2016-02-24 22:00:38 -05001561 * for debugfs only
1562 */
Peng Taod7e09d02013-05-02 16:46:55 +08001563 at->at_current = val;
1564
1565 if (at_max > 0)
1566 at->at_current = min(at->at_current, at_max);
1567 at->at_current = max(at->at_current, at_min);
1568
1569 if (at->at_current != old)
Joe Perches2d00bd12014-11-23 11:28:50 -08001570 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d (val=%u) hist %u %u %u %u\n",
1571 at,
Peng Taod7e09d02013-05-02 16:46:55 +08001572 old, at->at_current, at->at_current - old, val,
1573 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1574 at->at_hist[3]);
1575
1576 /* if we changed, report the old value */
1577 old = (at->at_current != old) ? old : 0;
1578
1579 spin_unlock(&at->at_lock);
1580 return old;
1581}
1582
1583/* Find the imp_at index for a given portal; assign if space available */
1584int import_at_get_index(struct obd_import *imp, int portal)
1585{
1586 struct imp_at *at = &imp->imp_at;
1587 int i;
1588
1589 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1590 if (at->iat_portal[i] == portal)
1591 return i;
1592 if (at->iat_portal[i] == 0)
1593 /* unused */
1594 break;
1595 }
1596
1597 /* Not found in list, add it under a lock */
1598 spin_lock(&imp->imp_lock);
1599
1600 /* Check unused under lock */
1601 for (; i < IMP_AT_MAX_PORTALS; i++) {
1602 if (at->iat_portal[i] == portal)
1603 goto out;
1604 if (at->iat_portal[i] == 0)
1605 /* unused */
1606 break;
1607 }
1608
1609 /* Not enough portals? */
1610 LASSERT(i < IMP_AT_MAX_PORTALS);
1611
1612 at->iat_portal[i] = portal;
1613out:
1614 spin_unlock(&imp->imp_lock);
1615 return i;
1616}