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