1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/import.c
38 * Author: Mike Shaver <shaver@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_RPC
43 # include <liblustre.h>
46 #include <obd_support.h>
47 #include <lustre_ha.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_export.h>
52 #include <obd_cksum.h>
53 #include <obd_class.h>
55 #include "ptlrpc_internal.h"
57 struct ptlrpc_connect_async_args {
58 __u64 pcaa_peer_committed;
59 int pcaa_initial_connect;
62 /* A CLOSED import should remain so. */
63 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
65 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
66 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
67 imp, obd2cli_tgt(imp->imp_obd), \
68 ptlrpc_import_state_name(imp->imp_state), \
69 ptlrpc_import_state_name(state)); \
70 imp->imp_state = state; \
74 #define IMPORT_SET_STATE(imp, state) \
76 spin_lock(&imp->imp_lock); \
77 IMPORT_SET_STATE_NOLOCK(imp, state); \
78 spin_unlock(&imp->imp_lock); \
82 static int ptlrpc_connect_interpret(const struct lu_env *env,
83 struct ptlrpc_request *request,
85 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
87 /* Only this function is allowed to change the import state when it is
88 * CLOSED. I would rather refcount the import and free it after
89 * disconnection like we do with exports. To do that, the client_obd
90 * will need to save the peer info somewhere other than in the import,
92 int ptlrpc_init_import(struct obd_import *imp)
94 spin_lock(&imp->imp_lock);
96 imp->imp_generation++;
97 imp->imp_state = LUSTRE_IMP_NEW;
99 spin_unlock(&imp->imp_lock);
103 EXPORT_SYMBOL(ptlrpc_init_import);
105 #define UUID_STR "_UUID"
106 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
109 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
110 ? uuid : uuid + strlen(prefix);
112 *uuid_len = strlen(*uuid_start);
114 if (*uuid_len < strlen(UUID_STR))
117 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
118 UUID_STR, strlen(UUID_STR)))
119 *uuid_len -= strlen(UUID_STR);
122 /* Returns true if import was FULL, false if import was already not
124 * @imp - import to be disconnected
125 * @conn_cnt - connection count (epoch) of the request that timed out
126 * and caused the disconnection. In some cases, multiple
127 * inflight requests can fail to a single target (e.g. OST
128 * bulk requests) and if one has already caused a reconnection
129 * (increasing the import->conn_cnt) the older failure should
130 * not also cause a reconnection. If zero it forces a reconnect.
132 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
136 spin_lock(&imp->imp_lock);
138 if (imp->imp_state == LUSTRE_IMP_FULL &&
139 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
143 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
144 &target_start, &target_len);
146 if (imp->imp_replayable) {
147 LCONSOLE_WARN("%s: Connection to service %.*s via nid "
148 "%s was lost; in progress operations using this "
149 "service will wait for recovery to complete.\n",
150 imp->imp_obd->obd_name, target_len, target_start,
151 libcfs_nid2str(imp->imp_connection->c_peer.nid));
153 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
154 "%.*s via nid %s was lost; in progress "
155 "operations using this service will fail.\n",
156 imp->imp_obd->obd_name,
157 target_len, target_start,
158 libcfs_nid2str(imp->imp_connection->c_peer.nid));
160 ptlrpc_deactivate_timeouts(imp);
161 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
162 spin_unlock(&imp->imp_lock);
164 if (obd_dump_on_timeout)
165 libcfs_debug_dumplog();
167 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
170 spin_unlock(&imp->imp_lock);
171 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
172 imp->imp_client->cli_name, imp,
173 (imp->imp_state == LUSTRE_IMP_FULL &&
174 imp->imp_conn_cnt > conn_cnt) ?
175 "reconnected" : "not connected", imp->imp_conn_cnt,
176 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
182 /* Must be called with imp_lock held! */
183 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
186 LASSERT_SPIN_LOCKED(&imp->imp_lock);
188 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
189 imp->imp_invalid = 1;
190 imp->imp_generation++;
191 spin_unlock(&imp->imp_lock);
193 ptlrpc_abort_inflight(imp);
194 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
200 * This acts as a barrier; all existing requests are rejected, and
201 * no new requests will be accepted until the import is valid again.
203 void ptlrpc_deactivate_import(struct obd_import *imp)
205 spin_lock(&imp->imp_lock);
206 ptlrpc_deactivate_and_unlock_import(imp);
210 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
214 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
215 (req->rq_phase == RQ_PHASE_BULK) ||
216 (req->rq_phase == RQ_PHASE_NEW)))
219 if (req->rq_timedout)
222 if (req->rq_phase == RQ_PHASE_NEW)
225 dl = req->rq_deadline;
233 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
235 time_t now = cfs_time_current_sec();
236 struct list_head *tmp, *n;
237 struct ptlrpc_request *req;
238 unsigned int timeout = 0;
240 spin_lock(&imp->imp_lock);
241 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
242 req = list_entry(tmp, struct ptlrpc_request, rq_list);
243 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
245 spin_unlock(&imp->imp_lock);
250 * This function will invalidate the import, if necessary, then block
251 * for all the RPC completions, and finally notify the obd to
252 * invalidate its state (ie cancel locks, clear pending requests,
255 void ptlrpc_invalidate_import(struct obd_import *imp)
257 struct list_head *tmp, *n;
258 struct ptlrpc_request *req;
259 struct l_wait_info lwi;
260 unsigned int timeout;
263 atomic_inc(&imp->imp_inval_count);
266 * If this is an invalid MGC connection, then don't bother
267 * waiting for imp_inflight to drop to 0.
269 if (imp->imp_invalid && imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
272 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
273 ptlrpc_deactivate_import(imp);
275 LASSERT(imp->imp_invalid);
277 /* Wait forever until inflight == 0. We really can't do it another
278 * way because in some cases we need to wait for very long reply
279 * unlink. We can't do anything before that because there is really
280 * no guarantee that some rdma transfer is not in progress right now. */
282 /* Calculate max timeout for waiting on rpcs to error
283 * out. Use obd_timeout if calculated value is smaller
285 timeout = ptlrpc_inflight_timeout(imp);
286 timeout += timeout / 3;
289 timeout = obd_timeout;
291 CDEBUG(D_RPCTRACE, "Sleeping %d sec for inflight to error out\n",
294 /* Wait for all requests to error out and call completion
295 * callbacks. Cap it at obd_timeout -- these should all
296 * have been locally cancelled by ptlrpc_abort_inflight. */
297 lwi = LWI_TIMEOUT_INTERVAL(
298 cfs_timeout_cap(cfs_time_seconds(timeout)),
299 cfs_time_seconds(1), NULL, NULL);
300 rc = l_wait_event(imp->imp_recovery_waitq,
301 (atomic_read(&imp->imp_inflight) == 0), &lwi);
303 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
305 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
306 cli_tgt, rc, atomic_read(&imp->imp_inflight));
308 spin_lock(&imp->imp_lock);
309 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
310 req = list_entry(tmp, struct ptlrpc_request,
312 DEBUG_REQ(D_ERROR, req, "still on sending list");
314 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
315 req = list_entry(tmp, struct ptlrpc_request,
317 DEBUG_REQ(D_ERROR, req, "still on delayed list");
320 if (atomic_read(&imp->imp_unregistering) == 0) {
321 /* We know that only "unregistering" rpcs may
322 * still survive in sending or delaying lists
323 * (They are waiting for long reply unlink in
324 * sluggish nets). Let's check this. If there
325 * is no unregistering and inflight != 0 this
327 LASSERT(atomic_read(&imp->imp_inflight) == 0);
329 /* Let's save one loop as soon as inflight have
330 * dropped to zero. No new inflights possible at
334 CERROR("%s: RPCs in \"%s\" phase found (%d). "
335 "Network is sluggish? Waiting them "
336 "to error out.\n", cli_tgt,
337 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
338 atomic_read(&imp->imp_unregistering));
340 spin_unlock(&imp->imp_lock);
345 * Let's additionally check that no new rpcs added to import in
346 * "invalidate" state.
348 LASSERT(atomic_read(&imp->imp_inflight) == 0);
350 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
351 sptlrpc_import_flush_all_ctx(imp);
353 atomic_dec(&imp->imp_inval_count);
354 cfs_waitq_signal(&imp->imp_recovery_waitq);
357 /* unset imp_invalid */
358 void ptlrpc_activate_import(struct obd_import *imp)
360 struct obd_device *obd = imp->imp_obd;
362 spin_lock(&imp->imp_lock);
363 imp->imp_invalid = 0;
364 ptlrpc_activate_timeouts(imp);
365 spin_unlock(&imp->imp_lock);
366 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
369 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
373 LASSERT(!imp->imp_dlm_fake);
375 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
376 if (!imp->imp_replayable) {
377 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
378 "auto-deactivating\n",
379 obd2cli_tgt(imp->imp_obd),
380 imp->imp_connection->c_remote_uuid.uuid,
381 imp->imp_obd->obd_name);
382 ptlrpc_deactivate_import(imp);
385 CDEBUG(D_HA, "%s: waking up pinger\n",
386 obd2cli_tgt(imp->imp_obd));
388 spin_lock(&imp->imp_lock);
389 imp->imp_force_verify = 1;
390 spin_unlock(&imp->imp_lock);
392 ptlrpc_pinger_wake_up();
397 int ptlrpc_reconnect_import(struct obd_import *imp)
399 ptlrpc_set_import_discon(imp, 0);
400 /* Force a new connect attempt */
401 ptlrpc_invalidate_import(imp);
402 /* Do a fresh connect next time by zeroing the handle */
403 ptlrpc_disconnect_import(imp, 1);
404 /* Wait for all invalidate calls to finish */
405 if (atomic_read(&imp->imp_inval_count) > 0) {
407 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
408 rc = l_wait_event(imp->imp_recovery_waitq,
409 (atomic_read(&imp->imp_inval_count) == 0),
412 CERROR("Interrupted, inval=%d\n",
413 atomic_read(&imp->imp_inval_count));
416 /* Allow reconnect attempts */
417 imp->imp_obd->obd_no_recov = 0;
418 /* Remove 'invalid' flag */
419 ptlrpc_activate_import(imp);
420 /* Attempt a new connect */
421 ptlrpc_recover_import(imp, NULL);
425 EXPORT_SYMBOL(ptlrpc_reconnect_import);
427 static int import_select_connection(struct obd_import *imp)
429 struct obd_import_conn *imp_conn = NULL, *conn;
430 struct obd_export *dlmexp;
434 spin_lock(&imp->imp_lock);
436 if (list_empty(&imp->imp_conn_list)) {
437 CERROR("%s: no connections available\n",
438 imp->imp_obd->obd_name);
439 spin_unlock(&imp->imp_lock);
443 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
444 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
445 imp->imp_obd->obd_name,
446 libcfs_nid2str(conn->oic_conn->c_peer.nid),
447 conn->oic_last_attempt);
448 /* Don't thrash connections */
449 if (cfs_time_before_64(cfs_time_current_64(),
450 conn->oic_last_attempt +
451 cfs_time_seconds(CONNECTION_SWITCH_MIN))) {
455 /* If we have not tried this connection since the
456 the last successful attempt, go with this one */
457 if ((conn->oic_last_attempt == 0) ||
458 cfs_time_beforeq_64(conn->oic_last_attempt,
459 imp->imp_last_success_conn)) {
465 /* If all of the connections have already been tried
466 since the last successful connection; just choose the
467 least recently used */
470 else if (cfs_time_before_64(conn->oic_last_attempt,
471 imp_conn->oic_last_attempt))
475 /* if not found, simply choose the current one */
477 LASSERT(imp->imp_conn_current);
478 imp_conn = imp->imp_conn_current;
481 LASSERT(imp_conn->oic_conn);
483 /* If we've tried everything, and we're back to the beginning of the
484 list, increase our timeout and try again. It will be reset when
485 we do finally connect. (FIXME: really we should wait for all network
486 state associated with the last connection attempt to drain before
487 trying to reconnect on it.) */
488 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item) &&
489 !imp->imp_recon_bk /* not retrying */) {
490 if (at_get(&imp->imp_at.iat_net_latency) <
491 CONNECTION_SWITCH_MAX) {
492 at_add(&imp->imp_at.iat_net_latency,
493 at_get(&imp->imp_at.iat_net_latency) +
494 CONNECTION_SWITCH_INC);
496 LASSERT(imp_conn->oic_last_attempt);
497 CWARN("%s: tried all connections, increasing latency to %ds\n",
498 imp->imp_obd->obd_name,
499 at_get(&imp->imp_at.iat_net_latency));
502 imp_conn->oic_last_attempt = cfs_time_current_64();
504 /* switch connection, don't mind if it's same as the current one */
505 if (imp->imp_connection)
506 ptlrpc_connection_put(imp->imp_connection);
507 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
509 dlmexp = class_conn2export(&imp->imp_dlm_handle);
510 LASSERT(dlmexp != NULL);
511 if (dlmexp->exp_connection)
512 ptlrpc_connection_put(dlmexp->exp_connection);
513 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
514 class_export_put(dlmexp);
516 if (imp->imp_conn_current != imp_conn) {
517 if (imp->imp_conn_current)
518 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
519 imp->imp_obd->obd_name,
520 imp_conn->oic_uuid.uuid,
521 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
522 imp->imp_conn_current = imp_conn;
525 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
526 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
527 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
529 spin_unlock(&imp->imp_lock);
535 * must be called under imp_lock
537 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
539 struct ptlrpc_request *req;
540 struct list_head *tmp;
542 if (list_empty(&imp->imp_replay_list))
544 tmp = imp->imp_replay_list.next;
545 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
546 *transno = req->rq_transno;
547 if (req->rq_transno == 0) {
548 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
555 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
557 struct obd_device *obd = imp->imp_obd;
558 int initial_connect = 0;
560 __u64 committed_before_reconnect = 0;
561 struct ptlrpc_request *request;
562 char *bufs[] = { NULL,
563 obd2cli_tgt(imp->imp_obd),
565 (char *)&imp->imp_dlm_handle,
566 (char *)&imp->imp_connect_data };
567 struct ptlrpc_connect_async_args *aa;
571 spin_lock(&imp->imp_lock);
572 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
573 spin_unlock(&imp->imp_lock);
574 CERROR("can't connect to a closed import\n");
576 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
577 spin_unlock(&imp->imp_lock);
578 CERROR("already connected\n");
580 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
581 spin_unlock(&imp->imp_lock);
582 CERROR("already connecting\n");
586 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
589 imp->imp_resend_replay = 0;
591 if (!lustre_handle_is_used(&imp->imp_remote_handle))
594 committed_before_reconnect = imp->imp_peer_committed_transno;
596 set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
597 spin_unlock(&imp->imp_lock);
600 struct obd_uuid uuid;
602 obd_str2uuid(&uuid, new_uuid);
603 rc = import_set_conn_priority(imp, &uuid);
608 rc = import_select_connection(imp);
612 /* last in connection list */
613 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
614 if (imp->imp_initial_recov_bk && initial_connect) {
615 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
616 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
617 /* Don't retry if connect fails */
619 obd_set_info_async(obd->obd_self_export,
620 sizeof(KEY_INIT_RECOV),
622 sizeof(rc), &rc, NULL);
624 if (imp->imp_recon_bk) {
625 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
626 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
627 spin_lock(&imp->imp_lock);
628 imp->imp_last_recon = 1;
629 spin_unlock(&imp->imp_lock);
633 rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
637 /* Reset connect flags to the originally requested flags, in case
638 * the server is updated on-the-fly we will get the new features. */
639 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
640 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
642 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
643 &obd->obd_uuid, &imp->imp_connect_data, NULL);
647 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
649 GOTO(out, rc = -ENOMEM);
651 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
652 imp->imp_connect_op, bufs, NULL);
654 ptlrpc_request_free(request);
659 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
661 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
663 request->rq_no_resend = request->rq_no_delay = 1;
664 request->rq_send_state = LUSTRE_IMP_CONNECTING;
665 /* Allow a slightly larger reply for future growth compatibility */
666 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
667 sizeof(struct obd_connect_data)+16*sizeof(__u64));
668 ptlrpc_request_set_replen(request);
669 request->rq_interpret_reply = ptlrpc_connect_interpret;
671 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
672 aa = ptlrpc_req_async_args(request);
673 memset(aa, 0, sizeof *aa);
675 aa->pcaa_peer_committed = committed_before_reconnect;
676 aa->pcaa_initial_connect = initial_connect;
678 if (aa->pcaa_initial_connect) {
679 spin_lock(&imp->imp_lock);
680 imp->imp_replayable = 1;
681 spin_unlock(&imp->imp_lock);
682 lustre_msg_add_op_flags(request->rq_reqmsg,
683 MSG_CONNECT_INITIAL);
685 /* AT will use INITIAL_CONNECT_TIMEOUT the first
686 time, adaptive after that. */
687 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
691 lustre_msg_add_op_flags(request->rq_reqmsg,
692 MSG_CONNECT_TRANSNO);
694 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
695 ptlrpcd_add_req(request, PSCOPE_OTHER);
699 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
704 EXPORT_SYMBOL(ptlrpc_connect_import);
706 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
709 struct obd_import_conn *imp_conn;
715 spin_lock(&imp->imp_lock);
716 if (list_empty(&imp->imp_conn_list))
720 imp_conn = list_entry(imp->imp_conn_list.prev,
721 struct obd_import_conn,
724 /* XXX: When the failover node is the primary node, it is possible
725 * to have two identical connections in imp_conn_list. We must
726 * compare not conn's pointers but NIDs, otherwise we can defeat
727 * connection throttling. (See bug 14774.) */
728 if (imp->imp_conn_current->oic_conn->c_peer.nid !=
729 imp_conn->oic_conn->c_peer.nid) {
730 ptlrpc_ping_import_soon(imp);
734 /* liblustre has no pinger thead, so we wakup pinger anyway */
739 spin_unlock(&imp->imp_lock);
742 ptlrpc_pinger_wake_up();
747 static int ptlrpc_connect_interpret(const struct lu_env *env,
748 struct ptlrpc_request *request,
751 struct ptlrpc_connect_async_args *aa = data;
752 struct obd_import *imp = request->rq_import;
753 struct client_obd *cli = &imp->imp_obd->u.cli;
754 struct lustre_handle old_hdl;
758 spin_lock(&imp->imp_lock);
759 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
760 spin_unlock(&imp->imp_lock);
763 spin_unlock(&imp->imp_lock);
768 LASSERT(imp->imp_conn_current);
770 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
772 /* All imports are pingable */
773 spin_lock(&imp->imp_lock);
774 imp->imp_pingable = 1;
776 if (aa->pcaa_initial_connect) {
777 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
778 imp->imp_replayable = 1;
779 spin_unlock(&imp->imp_lock);
780 CDEBUG(D_HA, "connected to replayable target: %s\n",
781 obd2cli_tgt(imp->imp_obd));
783 imp->imp_replayable = 0;
784 spin_unlock(&imp->imp_lock);
787 /* if applies, adjust the imp->imp_msg_magic here
788 * according to reply flags */
790 imp->imp_remote_handle =
791 *lustre_msg_get_handle(request->rq_repmsg);
793 /* Initial connects are allowed for clients with non-random
794 * uuids when servers are in recovery. Simply signal the
795 * servers replay is complete and wait in REPLAY_WAIT. */
796 if (msg_flags & MSG_CONNECT_RECOVERING) {
797 CDEBUG(D_HA, "connect to %s during recovery\n",
798 obd2cli_tgt(imp->imp_obd));
799 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
801 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
804 spin_lock(&imp->imp_lock);
805 if (imp->imp_invalid) {
806 spin_unlock(&imp->imp_lock);
807 ptlrpc_activate_import(imp);
809 spin_unlock(&imp->imp_lock);
812 GOTO(finish, rc = 0);
814 spin_unlock(&imp->imp_lock);
817 /* Determine what recovery state to move the import to. */
818 if (MSG_CONNECT_RECONNECT & msg_flags) {
819 memset(&old_hdl, 0, sizeof(old_hdl));
820 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
822 CERROR("%s@%s didn't like our handle "LPX64
823 ", failed\n", obd2cli_tgt(imp->imp_obd),
824 imp->imp_connection->c_remote_uuid.uuid,
825 imp->imp_dlm_handle.cookie);
826 GOTO(out, rc = -ENOTCONN);
829 if (memcmp(&imp->imp_remote_handle,
830 lustre_msg_get_handle(request->rq_repmsg),
831 sizeof(imp->imp_remote_handle))) {
832 int level = msg_flags & MSG_CONNECT_RECOVERING ? D_HA :
835 /* Bug 16611/14775: if server handle have changed,
836 * that means some sort of disconnection happened.
837 * If the server is not in recovery, that also means it
838 * already erased all of our state because of previous
839 * eviction. If it is in recovery - we are safe to
840 * participate since we can reestablish all of our state
841 * with server again */
842 CDEBUG(level,"%s@%s changed server handle from "
843 LPX64" to "LPX64"%s \n" "but is still in recovery \n",
844 obd2cli_tgt(imp->imp_obd),
845 imp->imp_connection->c_remote_uuid.uuid,
846 imp->imp_remote_handle.cookie,
847 lustre_msg_get_handle(request->rq_repmsg)->
849 (MSG_CONNECT_RECOVERING & msg_flags) ?
850 "but is still in recovery" : "");
852 imp->imp_remote_handle =
853 *lustre_msg_get_handle(request->rq_repmsg);
855 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
856 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
857 GOTO(finish, rc = 0);
861 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
862 obd2cli_tgt(imp->imp_obd),
863 imp->imp_connection->c_remote_uuid.uuid);
866 if (imp->imp_invalid) {
867 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
868 "marking evicted\n", imp->imp_obd->obd_name);
869 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
870 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
871 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
872 imp->imp_obd->obd_name,
873 obd2cli_tgt(imp->imp_obd));
875 spin_lock(&imp->imp_lock);
876 imp->imp_resend_replay = 1;
877 spin_unlock(&imp->imp_lock);
879 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
881 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
883 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
884 LASSERT(imp->imp_replayable);
885 imp->imp_remote_handle =
886 *lustre_msg_get_handle(request->rq_repmsg);
887 imp->imp_last_replay_transno = 0;
888 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
890 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
891 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
892 imp->imp_remote_handle =
893 *lustre_msg_get_handle(request->rq_repmsg);
894 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
897 /* Sanity checks for a reconnected import. */
898 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
899 CERROR("imp_replayable flag does not match server "
900 "after reconnect. We should LBUG right here.\n");
903 if (lustre_msg_get_last_committed(request->rq_repmsg) <
904 aa->pcaa_peer_committed) {
905 CERROR("%s went back in time (transno "LPD64
906 " was previously committed, server now claims "LPD64
907 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
909 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
910 lustre_msg_get_last_committed(request->rq_repmsg));
914 rc = ptlrpc_import_recovery_state_machine(imp);
916 if (rc == -ENOTCONN) {
917 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
918 "invalidating and reconnecting\n",
919 obd2cli_tgt(imp->imp_obd),
920 imp->imp_connection->c_remote_uuid.uuid);
921 ptlrpc_connect_import(imp, NULL);
925 struct obd_connect_data *ocd;
926 struct obd_export *exp;
928 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
930 /* server replied obd_connect_data is always bigger */
931 ocd = req_capsule_server_sized_get(&request->rq_pill,
932 &RMF_CONNECT_DATA, ret);
934 spin_lock(&imp->imp_lock);
935 list_del(&imp->imp_conn_current->oic_item);
936 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
937 imp->imp_last_success_conn =
938 imp->imp_conn_current->oic_last_attempt;
941 spin_unlock(&imp->imp_lock);
942 CERROR("Wrong connect data from server\n");
947 imp->imp_connect_data = *ocd;
949 exp = class_conn2export(&imp->imp_dlm_handle);
950 spin_unlock(&imp->imp_lock);
952 /* check that server granted subset of flags we asked for. */
953 LASSERTF((ocd->ocd_connect_flags &
954 imp->imp_connect_flags_orig) ==
955 ocd->ocd_connect_flags, LPX64" != "LPX64,
956 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
959 /* This could happen if export is cleaned during the
961 CERROR("Missing export for %s\n",
962 imp->imp_obd->obd_name);
963 GOTO(out, rc = -ENODEV);
965 exp->exp_connect_flags = ocd->ocd_connect_flags;
966 imp->imp_obd->obd_self_export->exp_connect_flags =
967 ocd->ocd_connect_flags;
968 class_export_put(exp);
970 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
972 if (!ocd->ocd_ibits_known &&
973 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
974 CERROR("Inodebits aware server returned zero compatible"
977 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
978 (ocd->ocd_version > LUSTRE_VERSION_CODE +
979 LUSTRE_VERSION_OFFSET_WARN ||
980 ocd->ocd_version < LUSTRE_VERSION_CODE -
981 LUSTRE_VERSION_OFFSET_WARN)) {
982 /* Sigh, some compilers do not like #ifdef in the middle
983 of macro arguments */
986 "older. Consider upgrading this client";
989 "older. Consider recompiling this application";
991 const char *newer = "newer than client version";
993 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
995 obd2cli_tgt(imp->imp_obd),
996 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
997 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
998 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
999 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1000 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1001 newer : older, LUSTRE_VERSION_STRING);
1004 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1005 /* We sent to the server ocd_cksum_types with bits set
1006 * for algorithms we understand. The server masked off
1007 * the checksum types it doesn't support */
1008 if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
1009 LCONSOLE_WARN("The negotiation of the checksum "
1010 "alogrithm to use with server %s "
1011 "failed (%x/%x), disabling "
1013 obd2cli_tgt(imp->imp_obd),
1014 ocd->ocd_cksum_types,
1016 cli->cl_checksum = 0;
1017 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
1018 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1020 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1022 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
1023 cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
1024 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
1025 cli->cl_cksum_type = OBD_CKSUM_ADLER;
1027 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1030 /* The server does not support OBD_CONNECT_CKSUM.
1031 * Enforce CRC32 for backward compatibility*/
1032 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
1033 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1036 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
1037 cli->cl_max_pages_per_rpc =
1038 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
1041 imp->imp_obd->obd_namespace->ns_connect_flags =
1042 ocd->ocd_connect_flags;
1043 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1044 ocd->ocd_connect_flags;
1046 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1047 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1048 /* We need a per-message support flag, because
1049 a. we don't know if the incoming connect reply
1050 supports AT or not (in reply_in_callback)
1052 b. failovered server means export and flags are gone
1053 (in ptlrpc_send_reply).
1054 Can only be set when we know AT is supported at
1056 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1058 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1060 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1061 (cli->cl_max_pages_per_rpc > 0));
1066 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1067 spin_lock(&imp->imp_lock);
1068 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
1069 (request->rq_import_generation == imp->imp_generation))
1070 ptlrpc_deactivate_and_unlock_import(imp);
1072 spin_unlock(&imp->imp_lock);
1074 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
1077 * Give up trying to reconnect
1078 * EACCES means client has no permission for connection
1080 imp->imp_obd->obd_no_recov = 1;
1081 ptlrpc_deactivate_import(imp);
1084 if (rc == -EPROTO) {
1085 struct obd_connect_data *ocd;
1087 /* reply message might not be ready */
1088 if (request->rq_repmsg == NULL)
1091 ocd = req_capsule_server_get(&request->rq_pill,
1094 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1095 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1096 /* Actually servers are only supposed to refuse
1097 connection from liblustre clients, so we should
1098 never see this from VFS context */
1099 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1101 " refused connection from this client "
1102 "with an incompatible version (%s). "
1103 "Client must be recompiled\n",
1104 obd2cli_tgt(imp->imp_obd),
1105 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1106 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1107 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1108 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1109 LUSTRE_VERSION_STRING);
1110 ptlrpc_deactivate_import(imp);
1111 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1116 ptlrpc_maybe_ping_import_soon(imp);
1118 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1119 obd2cli_tgt(imp->imp_obd),
1120 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1123 spin_lock(&imp->imp_lock);
1124 imp->imp_last_recon = 0;
1125 spin_unlock(&imp->imp_lock);
1127 cfs_waitq_signal(&imp->imp_recovery_waitq);
1131 static int completed_replay_interpret(const struct lu_env *env,
1132 struct ptlrpc_request *req,
1133 void * data, int rc)
1136 atomic_dec(&req->rq_import->imp_replay_inflight);
1137 if (req->rq_status == 0) {
1138 ptlrpc_import_recovery_state_machine(req->rq_import);
1140 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1142 req->rq_import->imp_obd->obd_name, req->rq_status);
1143 ptlrpc_connect_import(req->rq_import, NULL);
1149 static int signal_completed_replay(struct obd_import *imp)
1151 struct ptlrpc_request *req;
1154 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1155 atomic_inc(&imp->imp_replay_inflight);
1157 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1160 atomic_dec(&imp->imp_replay_inflight);
1164 ptlrpc_request_set_replen(req);
1165 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1166 lustre_msg_add_flags(req->rq_reqmsg,
1167 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1168 req->rq_timeout *= 3;
1169 req->rq_interpret_reply = completed_replay_interpret;
1171 ptlrpcd_add_req(req, PSCOPE_OTHER);
1176 static int ptlrpc_invalidate_import_thread(void *data)
1178 struct obd_import *imp = data;
1182 ptlrpc_daemonize("ll_imp_inval");
1184 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1185 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1186 imp->imp_connection->c_remote_uuid.uuid);
1188 ptlrpc_invalidate_import(imp);
1190 if (obd_dump_on_eviction) {
1191 CERROR("dump the log upon eviction\n");
1192 libcfs_debug_dumplog();
1195 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1196 ptlrpc_import_recovery_state_machine(imp);
1202 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1210 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1211 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1212 &target_start, &target_len);
1213 /* Don't care about MGC eviction */
1214 if (strcmp(imp->imp_obd->obd_type->typ_name,
1215 LUSTRE_MGC_NAME) != 0) {
1216 LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1217 "%.*s; in progress operations using "
1218 "this service will fail.\n",
1219 target_len, target_start);
1221 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1222 obd2cli_tgt(imp->imp_obd),
1223 imp->imp_connection->c_remote_uuid.uuid);
1226 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1227 CLONE_VM | CLONE_FILES);
1229 CERROR("error starting invalidate thread: %d\n", rc);
1234 ptlrpc_invalidate_import(imp);
1236 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1240 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1241 CDEBUG(D_HA, "replay requested by %s\n",
1242 obd2cli_tgt(imp->imp_obd));
1243 rc = ptlrpc_replay_next(imp, &inflight);
1244 if (inflight == 0 &&
1245 atomic_read(&imp->imp_replay_inflight) == 0) {
1246 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1247 rc = ldlm_replay_locks(imp);
1254 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1255 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1256 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1257 rc = signal_completed_replay(imp);
1264 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1265 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1266 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1270 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1271 CDEBUG(D_HA, "reconnected to %s@%s\n",
1272 obd2cli_tgt(imp->imp_obd),
1273 imp->imp_connection->c_remote_uuid.uuid);
1275 rc = ptlrpc_resend(imp);
1278 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1279 ptlrpc_activate_import(imp);
1281 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1282 &target_start, &target_len);
1283 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1284 "using nid %s.\n", imp->imp_obd->obd_name,
1285 target_len, target_start,
1286 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1289 if (imp->imp_state == LUSTRE_IMP_FULL) {
1290 cfs_waitq_signal(&imp->imp_recovery_waitq);
1291 ptlrpc_wake_delayed(imp);
1298 static int back_to_sleep(void *unused)
1303 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1305 struct ptlrpc_request *req;
1307 int nowait = imp->imp_obd->obd_force;
1311 GOTO(set_state, rc);
1313 switch (imp->imp_connect_op) {
1314 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1315 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1316 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1318 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1319 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1323 if (ptlrpc_import_in_recovery(imp)) {
1324 struct l_wait_info lwi;
1325 cfs_duration_t timeout;
1329 if (imp->imp_server_timeout)
1330 timeout = cfs_time_seconds(obd_timeout / 2);
1332 timeout = cfs_time_seconds(obd_timeout);
1334 int idx = import_at_get_index(imp,
1335 imp->imp_client->cli_request_portal);
1336 timeout = cfs_time_seconds(
1337 at_get(&imp->imp_at.iat_service_estimate[idx]));
1340 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1341 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1342 rc = l_wait_event(imp->imp_recovery_waitq,
1343 !ptlrpc_import_in_recovery(imp), &lwi);
1347 spin_lock(&imp->imp_lock);
1348 if (imp->imp_state != LUSTRE_IMP_FULL)
1351 spin_unlock(&imp->imp_lock);
1353 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1354 LUSTRE_OBD_VERSION, rq_opc);
1356 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1357 * it fails. We can get through the above with a down server
1358 * if the client doesn't know the server is gone yet. */
1359 req->rq_no_resend = 1;
1362 /* We want client umounts to happen quickly, no matter the
1364 req->rq_timeout = min_t(int, req->rq_timeout,
1365 INITIAL_CONNECT_TIMEOUT);
1367 /* ... but we always want liblustre clients to nicely
1368 disconnect, so only use the adaptive value. */
1370 req->rq_timeout = obd_timeout / 3;
1373 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1374 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1375 ptlrpc_request_set_replen(req);
1376 rc = ptlrpc_queue_wait(req);
1377 ptlrpc_req_finished(req);
1381 spin_lock(&imp->imp_lock);
1384 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1386 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1387 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1388 imp->imp_conn_cnt = 0;
1389 /* Try all connections in the future - bz 12758 */
1390 imp->imp_last_recon = 0;
1391 spin_unlock(&imp->imp_lock);
1397 /* Adaptive Timeout utils */
1398 extern unsigned int at_min, at_max, at_history;
1400 /* Bin into timeslices using AT_BINS bins.
1401 This gives us a max of the last binlimit*AT_BINS secs without the storage,
1402 but still smoothing out a return to normalcy from a slow response.
1403 (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1404 int at_add(struct adaptive_timeout *at, unsigned int val)
1406 unsigned int old = at->at_current;
1407 time_t now = cfs_time_current_sec();
1408 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1412 CDEBUG(D_INFO, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1413 val, at, now - at->at_binstart, at->at_current,
1414 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1417 /* 0's don't count, because we never want our timeout to
1418 drop to 0, and because 0 could mean an error */
1421 spin_lock(&at->at_lock);
1423 if (unlikely(at->at_binstart == 0)) {
1424 /* Special case to remove default from history */
1425 at->at_current = val;
1426 at->at_worst_ever = val;
1427 at->at_worst_time = now;
1428 at->at_hist[0] = val;
1429 at->at_binstart = now;
1430 } else if (now - at->at_binstart < binlimit ) {
1432 at->at_hist[0] = max(val, at->at_hist[0]);
1433 at->at_current = max(val, at->at_current);
1436 unsigned int maxv = val;
1437 /* move bins over */
1438 shift = (now - at->at_binstart) / binlimit;
1440 for(i = AT_BINS - 1; i >= 0; i--) {
1442 at->at_hist[i] = at->at_hist[i - shift];
1443 maxv = max(maxv, at->at_hist[i]);
1448 at->at_hist[0] = val;
1449 at->at_current = maxv;
1450 at->at_binstart += shift * binlimit;
1453 if (at->at_current > at->at_worst_ever) {
1454 at->at_worst_ever = at->at_current;
1455 at->at_worst_time = now;
1458 if (at->at_flags & AT_FLG_NOHIST)
1459 /* Only keep last reported val; keeping the rest of the history
1461 at->at_current = val;
1464 at->at_current = min(at->at_current, at_max);
1465 at->at_current = max(at->at_current, at_min);
1468 if (at->at_current != old)
1469 CDEBUG(D_ADAPTTO, "AT %p change: old=%u new=%u delta=%d "
1470 "(val=%u) hist %u %u %u %u\n", at,
1471 old, at->at_current, at->at_current - old, val,
1472 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1476 /* if we changed, report the old value */
1477 old = (at->at_current != old) ? old : 0;
1479 spin_unlock(&at->at_lock);
1483 /* Find the imp_at index for a given portal; assign if space available */
1484 int import_at_get_index(struct obd_import *imp, int portal)
1486 struct imp_at *at = &imp->imp_at;
1489 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1490 if (at->iat_portal[i] == portal)
1492 if (at->iat_portal[i] == 0)
1497 /* Not found in list, add it under a lock */
1498 spin_lock(&imp->imp_lock);
1500 /* Check unused under lock */
1501 for (; i < IMP_AT_MAX_PORTALS; i++) {
1502 if (at->iat_portal[i] == portal)
1504 if (at->iat_portal[i] == 0)
1509 /* Not enough portals? */
1510 LASSERT(i < IMP_AT_MAX_PORTALS);
1512 at->iat_portal[i] = portal;
1514 spin_unlock(&imp->imp_lock);