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