4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/pinger.c
34 * Portal-RPC reconnection and replay operations, for use in recovery.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <linux/kthread.h>
40 #include <obd_support.h>
41 #include <obd_class.h>
42 #include "ptlrpc_internal.h"
44 static int suppress_pings;
45 module_param(suppress_pings, int, 0644);
46 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
48 struct mutex pinger_mutex;
49 static struct list_head pinger_imports =
50 LIST_HEAD_INIT(pinger_imports);
51 static struct list_head timeout_list =
52 LIST_HEAD_INIT(timeout_list);
54 int ptlrpc_pinger_suppress_pings()
56 return suppress_pings;
58 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
60 struct ptlrpc_request *
61 ptlrpc_prep_ping(struct obd_import *imp)
63 struct ptlrpc_request *req;
65 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
66 LUSTRE_OBD_VERSION, OBD_PING);
68 ptlrpc_request_set_replen(req);
69 req->rq_no_resend = req->rq_no_delay = 1;
74 int ptlrpc_obd_ping(struct obd_device *obd)
77 struct ptlrpc_request *req;
80 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
84 req->rq_send_state = LUSTRE_IMP_FULL;
86 rc = ptlrpc_queue_wait(req);
88 ptlrpc_req_finished(req);
92 EXPORT_SYMBOL(ptlrpc_obd_ping);
94 static int ptlrpc_ping(struct obd_import *imp)
96 struct ptlrpc_request *req;
99 req = ptlrpc_prep_ping(imp);
101 CERROR("OOM trying to ping %s->%s\n",
102 imp->imp_obd->obd_uuid.uuid,
103 obd2cli_tgt(imp->imp_obd));
107 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
108 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
109 ptlrpcd_add_req(req);
114 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
117 time64_t time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
119 if (imp->imp_state == LUSTRE_IMP_DISCON) {
120 time64_t dtime = max_t(time64_t, CONNECTION_SWITCH_MIN,
122 at_get(&imp->imp_at.iat_net_latency));
123 time = min(time, dtime);
125 imp->imp_next_ping = ktime_get_seconds() + time;
126 #endif /* ENABLE_PINGER */
129 void ptlrpc_ping_import_soon(struct obd_import *imp)
131 imp->imp_next_ping = ktime_get_seconds();
134 static inline int imp_is_deactive(struct obd_import *imp)
136 return (imp->imp_deactive ||
137 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
140 static inline time64_t ptlrpc_next_reconnect(struct obd_import *imp)
142 if (imp->imp_server_timeout)
143 return ktime_get_seconds() + (obd_timeout >> 1);
145 return ktime_get_seconds() + obd_timeout;
148 static time64_t pinger_check_timeout(time64_t time)
150 struct timeout_item *item;
151 time64_t timeout = PING_INTERVAL;
153 /* This list is sorted in increasing timeout order */
154 mutex_lock(&pinger_mutex);
155 list_for_each_entry(item, &timeout_list, ti_chain) {
156 time64_t ti_timeout = item->ti_timeout;
158 if (timeout > ti_timeout)
159 timeout = ti_timeout;
162 mutex_unlock(&pinger_mutex);
164 return time + timeout - ktime_get_seconds();
169 void ptlrpc_pinger_ir_up(void)
171 CDEBUG(D_HA, "IR up\n");
174 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
176 void ptlrpc_pinger_ir_down(void)
178 CDEBUG(D_HA, "IR down\n");
181 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
183 static void ptlrpc_pinger_process_import(struct obd_import *imp,
191 spin_lock(&imp->imp_lock);
193 level = imp->imp_state;
194 force = imp->imp_force_verify;
195 force_next = imp->imp_force_next_verify;
197 * This will be used below only if the import is "FULL".
199 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
201 imp->imp_force_verify = 0;
203 if (imp->imp_next_ping - 5 >= this_ping && !force) {
204 spin_unlock(&imp->imp_lock);
208 imp->imp_force_next_verify = 0;
210 spin_unlock(&imp->imp_lock);
212 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
213 "force %u force_next %u deactive %u pingable %u suppress %u\n",
214 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
215 ptlrpc_import_state_name(level), level, force, force_next,
216 imp->imp_deactive, imp->imp_pingable, suppress);
218 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
219 /* wait for a while before trying recovery again */
220 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
221 if (!imp->imp_no_pinger_recover)
222 ptlrpc_initiate_recovery(imp);
223 } else if (level != LUSTRE_IMP_FULL ||
224 imp->imp_obd->obd_no_recov ||
225 imp_is_deactive(imp)) {
226 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
227 "or recovery disabled: %s)\n",
228 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
229 ptlrpc_import_state_name(level));
231 spin_lock(&imp->imp_lock);
232 imp->imp_force_verify = 1;
233 spin_unlock(&imp->imp_lock);
235 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
240 static int ptlrpc_pinger_main(void *arg)
242 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
245 /* Record that the thread is running */
246 thread_set_flags(thread, SVC_RUNNING);
247 wake_up(&thread->t_ctl_waitq);
249 /* And now, loop forever, pinging as needed. */
251 time64_t this_ping = ktime_get_seconds();
252 struct l_wait_info lwi;
253 time64_t time_to_next_wake;
254 struct timeout_item *item;
255 struct list_head *iter;
257 mutex_lock(&pinger_mutex);
258 list_for_each_entry(item, &timeout_list, ti_chain)
259 item->ti_cb(item, item->ti_cb_data);
261 list_for_each(iter, &pinger_imports) {
262 struct obd_import *imp = list_entry(iter,
266 ptlrpc_pinger_process_import(imp, this_ping);
267 /* obd_timeout might have changed */
268 if (imp->imp_pingable && imp->imp_next_ping &&
269 imp->imp_next_ping > this_ping + PING_INTERVAL)
270 ptlrpc_update_next_ping(imp, 0);
272 mutex_unlock(&pinger_mutex);
273 /* update memory usage info */
274 obd_update_maxusage();
276 /* Wait until the next ping time, or until we're stopped. */
277 time_to_next_wake = pinger_check_timeout(this_ping);
278 /* The ping sent by ptlrpc_send_rpc may get sent out
279 say .01 second after this.
280 ptlrpc_pinger_sending_on_import will then set the
281 next ping time to next_ping + .01 sec, which means
282 we will SKIP the next ping at next_ping, and the
283 ping will get sent 2 timeouts from now! Beware. */
284 CDEBUG(D_INFO, "next wakeup in %lld (%lld)\n",
285 time_to_next_wake, this_ping + PING_INTERVAL);
286 if (time_to_next_wake > 0) {
287 time64_t time_max = max(time_to_next_wake, 1LL);
289 lwi = LWI_TIMEOUT(cfs_time_seconds(time_max),
291 l_wait_event(thread->t_ctl_waitq,
292 thread_is_stopping(thread) ||
293 thread_is_event(thread),
295 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
299 /* woken after adding import to reset timer */
300 thread_test_and_clear_flags(thread, SVC_EVENT);
305 thread_set_flags(thread, SVC_STOPPED);
306 wake_up(&thread->t_ctl_waitq);
308 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
312 static struct ptlrpc_thread pinger_thread;
314 int ptlrpc_start_pinger(void)
316 struct l_wait_info lwi = { 0 };
317 struct task_struct *task;
319 #ifndef ENABLE_PINGER
324 if (!thread_is_init(&pinger_thread) &&
325 !thread_is_stopped(&pinger_thread))
328 init_waitqueue_head(&pinger_thread.t_ctl_waitq);
330 strcpy(pinger_thread.t_name, "ll_ping");
332 task = kthread_run(ptlrpc_pinger_main, &pinger_thread,
333 pinger_thread.t_name);
336 CERROR("cannot start pinger thread: rc = %d\n", rc);
340 l_wait_event(pinger_thread.t_ctl_waitq,
341 thread_is_running(&pinger_thread), &lwi);
344 CWARN("Pings will be suppressed at the request of the "
345 "administrator. The configuration shall meet the "
346 "additional requirements described in the manual. "
347 "(Search for the \"suppress_pings\" kernel module "
353 int ptlrpc_pinger_remove_timeouts(void);
355 int ptlrpc_stop_pinger(void)
357 struct l_wait_info lwi = { 0 };
358 #ifndef ENABLE_PINGER
363 if (thread_is_init(&pinger_thread) ||
364 thread_is_stopped(&pinger_thread))
367 ptlrpc_pinger_remove_timeouts();
369 thread_set_flags(&pinger_thread, SVC_STOPPING);
370 wake_up(&pinger_thread.t_ctl_waitq);
372 l_wait_event(pinger_thread.t_ctl_waitq,
373 thread_is_stopped(&pinger_thread), &lwi);
377 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
379 ptlrpc_update_next_ping(imp, 0);
382 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
384 ptlrpc_update_next_ping(imp, 1);
385 assert_spin_locked(&imp->imp_lock);
387 * Avoid reading stale imp_connect_data. When not sure if pings are
388 * expected or not on next connection, we assume they are not and force
389 * one anyway to guarantee the chance of updating
390 * imp_peer_committed_transno.
392 if (imp->imp_state != LUSTRE_IMP_FULL ||
393 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
394 imp->imp_force_next_verify = 1;
397 int ptlrpc_pinger_add_import(struct obd_import *imp)
400 if (!list_empty(&imp->imp_pinger_chain))
403 mutex_lock(&pinger_mutex);
404 CDEBUG(D_HA, "adding pingable import %s->%s\n",
405 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
406 /* if we add to pinger we want recovery on this import */
407 imp->imp_obd->obd_no_recov = 0;
408 ptlrpc_update_next_ping(imp, 0);
409 /* XXX sort, blah blah */
410 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
411 class_import_get(imp);
413 ptlrpc_pinger_wake_up();
414 mutex_unlock(&pinger_mutex);
418 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
420 int ptlrpc_pinger_del_import(struct obd_import *imp)
424 if (list_empty(&imp->imp_pinger_chain))
427 mutex_lock(&pinger_mutex);
428 list_del_init(&imp->imp_pinger_chain);
429 CDEBUG(D_HA, "removing pingable import %s->%s\n",
430 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
431 /* if we remove from pinger we don't want recovery on this import */
432 imp->imp_obd->obd_no_recov = 1;
433 class_import_put(imp);
434 mutex_unlock(&pinger_mutex);
437 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
440 * Register a timeout callback to the pinger list, and the callback will
441 * be called when timeout happens.
443 static struct timeout_item *ptlrpc_new_timeout(time64_t time,
444 enum timeout_event event,
445 timeout_cb_t cb, void *data)
447 struct timeout_item *ti;
453 INIT_LIST_HEAD(&ti->ti_obd_list);
454 INIT_LIST_HEAD(&ti->ti_chain);
455 ti->ti_timeout = time;
456 ti->ti_event = event;
458 ti->ti_cb_data = data;
464 * Register timeout event on the the pinger thread.
465 * Note: the timeout list is an sorted list with increased timeout value.
467 static struct timeout_item*
468 ptlrpc_pinger_register_timeout(time64_t time, enum timeout_event event,
469 timeout_cb_t cb, void *data)
471 struct timeout_item *item, *tmp;
473 LASSERT(mutex_is_locked(&pinger_mutex));
475 list_for_each_entry(item, &timeout_list, ti_chain)
476 if (item->ti_event == event)
479 item = ptlrpc_new_timeout(time, event, cb, data);
481 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
482 if (tmp->ti_timeout < time) {
483 list_add(&item->ti_chain, &tmp->ti_chain);
487 list_add(&item->ti_chain, &timeout_list);
493 /* Add a client_obd to the timeout event list, when timeout(@time)
494 * happens, the callback(@cb) will be called.
496 int ptlrpc_add_timeout_client(time64_t time, enum timeout_event event,
497 timeout_cb_t cb, void *data,
498 struct list_head *obd_list)
500 struct timeout_item *ti;
502 mutex_lock(&pinger_mutex);
503 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
505 mutex_unlock(&pinger_mutex);
508 list_add(obd_list, &ti->ti_obd_list);
509 mutex_unlock(&pinger_mutex);
512 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
514 int ptlrpc_del_timeout_client(struct list_head *obd_list,
515 enum timeout_event event)
517 struct timeout_item *ti = NULL, *item;
519 if (list_empty(obd_list))
521 mutex_lock(&pinger_mutex);
522 list_del_init(obd_list);
524 * If there are no obd attached to the timeout event
525 * list, remove this timeout event from the pinger
527 list_for_each_entry(item, &timeout_list, ti_chain) {
528 if (item->ti_event == event) {
533 LASSERTF(ti != NULL, "ti is NULL !\n");
534 if (list_empty(&ti->ti_obd_list)) {
535 list_del(&ti->ti_chain);
538 mutex_unlock(&pinger_mutex);
541 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
543 int ptlrpc_pinger_remove_timeouts(void)
545 struct timeout_item *item, *tmp;
547 mutex_lock(&pinger_mutex);
548 list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
549 LASSERT(list_empty(&item->ti_obd_list));
550 list_del(&item->ti_chain);
553 mutex_unlock(&pinger_mutex);
557 void ptlrpc_pinger_wake_up()
560 thread_add_flags(&pinger_thread, SVC_EVENT);
561 wake_up(&pinger_thread.t_ctl_waitq);
565 /* Ping evictor thread */
567 #define PET_TERMINATE 2
569 static int pet_refcount = 0;
570 static int pet_state;
571 static wait_queue_head_t pet_waitq;
572 static struct list_head pet_list;
573 static DEFINE_SPINLOCK(pet_lock);
575 int ping_evictor_wake(struct obd_export *exp)
577 struct obd_device *obd;
579 spin_lock(&pet_lock);
580 if (pet_state != PET_READY) {
581 /* eventually the new obd will call here again. */
582 spin_unlock(&pet_lock);
586 obd = class_exp2obd(exp);
587 if (list_empty(&obd->obd_evict_list)) {
588 class_incref(obd, "evictor", obd);
589 list_add(&obd->obd_evict_list, &pet_list);
591 spin_unlock(&pet_lock);
597 static int ping_evictor_main(void *arg)
599 struct obd_device *obd;
600 struct obd_export *exp;
601 struct l_wait_info lwi = { 0 };
602 time64_t expire_time;
607 CDEBUG(D_HA, "Starting Ping Evictor\n");
608 pet_state = PET_READY;
610 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
611 (pet_state == PET_TERMINATE), &lwi);
613 /* loop until all obd's will be removed */
614 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
617 /* we only get here if pet_exp != NULL, and the end of this
618 * loop is the only place which sets it NULL again, so lock
619 * is not strictly necessary. */
620 spin_lock(&pet_lock);
621 obd = list_entry(pet_list.next, struct obd_device,
623 spin_unlock(&pet_lock);
625 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
627 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
628 obd->obd_name, expire_time);
630 /* Exports can't be deleted out of the list while we hold
631 * the obd lock (class_unlink_export), which means we can't
632 * lose the last ref on the export. If they've already been
633 * removed from the list, we won't find them here. */
634 spin_lock(&obd->obd_dev_lock);
635 while (!list_empty(&obd->obd_exports_timed)) {
636 exp = list_entry(obd->obd_exports_timed.next,
638 exp_obd_chain_timed);
639 if (expire_time > exp->exp_last_request_time) {
640 class_export_get(exp);
641 spin_unlock(&obd->obd_dev_lock);
642 LCONSOLE_WARN("%s: haven't heard from client %s"
643 " (at %s) in %lld seconds. I think"
644 " it's dead, and I am evicting"
645 " it. exp %p, cur %lld expire %lld"
648 obd_uuid2str(&exp->exp_client_uuid),
649 obd_export_nid2str(exp),
650 ktime_get_real_seconds() -
651 exp->exp_last_request_time,
652 exp, ktime_get_real_seconds(),
654 exp->exp_last_request_time);
655 CDEBUG(D_HA, "Last request was at %lld\n",
656 exp->exp_last_request_time);
657 class_fail_export(exp);
658 class_export_put(exp);
659 spin_lock(&obd->obd_dev_lock);
661 /* List is sorted, so everyone below is ok */
665 spin_unlock(&obd->obd_dev_lock);
667 spin_lock(&pet_lock);
668 list_del_init(&obd->obd_evict_list);
669 spin_unlock(&pet_lock);
671 class_decref(obd, "evictor", obd);
673 CDEBUG(D_HA, "Exiting Ping Evictor\n");
678 void ping_evictor_start(void)
680 struct task_struct *task;
682 if (++pet_refcount > 1)
685 INIT_LIST_HEAD(&pet_list);
686 init_waitqueue_head(&pet_waitq);
688 task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
691 CERROR("Cannot start ping evictor thread: %ld\n",
695 EXPORT_SYMBOL(ping_evictor_start);
697 void ping_evictor_stop(void)
699 if (--pet_refcount > 0)
702 pet_state = PET_TERMINATE;
705 EXPORT_SYMBOL(ping_evictor_stop);