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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/pinger.c
38 * Portal-RPC reconnection and replay operations, for use in recovery.
42 #include <liblustre.h>
44 #define DEBUG_SUBSYSTEM S_RPC
47 #include <obd_support.h>
48 #include <obd_class.h>
49 #include "ptlrpc_internal.h"
51 static int suppress_pings;
52 CFS_MODULE_PARM(suppress_pings, "i", int, 0644, "Suppress pings");
54 struct mutex pinger_mutex;
55 static CFS_LIST_HEAD(pinger_imports);
56 static cfs_list_t timeout_list = CFS_LIST_HEAD_INIT(timeout_list);
58 int ptlrpc_pinger_suppress_pings()
60 return suppress_pings;
62 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
64 struct ptlrpc_request *
65 ptlrpc_prep_ping(struct obd_import *imp)
67 struct ptlrpc_request *req;
69 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
70 LUSTRE_OBD_VERSION, OBD_PING);
72 ptlrpc_request_set_replen(req);
73 req->rq_no_resend = req->rq_no_delay = 1;
78 int ptlrpc_obd_ping(struct obd_device *obd)
81 struct ptlrpc_request *req;
84 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
88 req->rq_send_state = LUSTRE_IMP_FULL;
90 rc = ptlrpc_queue_wait(req);
92 ptlrpc_req_finished(req);
96 EXPORT_SYMBOL(ptlrpc_obd_ping);
98 int ptlrpc_ping(struct obd_import *imp)
100 struct ptlrpc_request *req;
103 req = ptlrpc_prep_ping(imp);
105 CERROR("OOM trying to ping %s->%s\n",
106 imp->imp_obd->obd_uuid.uuid,
107 obd2cli_tgt(imp->imp_obd));
111 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
112 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
113 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
118 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
121 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
122 if (imp->imp_state == LUSTRE_IMP_DISCON) {
123 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
125 at_get(&imp->imp_at.iat_net_latency));
126 time = min(time, dtime);
128 imp->imp_next_ping = cfs_time_shift(time);
129 #endif /* ENABLE_PINGER */
132 void ptlrpc_ping_import_soon(struct obd_import *imp)
134 imp->imp_next_ping = cfs_time_current();
137 static inline int imp_is_deactive(struct obd_import *imp)
139 return (imp->imp_deactive ||
140 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
143 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
145 if (imp->imp_server_timeout)
146 return cfs_time_shift(obd_timeout / 2);
148 return cfs_time_shift(obd_timeout);
151 static cfs_atomic_t suspend_timeouts = CFS_ATOMIC_INIT(0);
152 static cfs_time_t suspend_wakeup_time = 0;
154 cfs_duration_t pinger_check_timeout(cfs_time_t time)
156 struct timeout_item *item;
157 cfs_time_t timeout = PING_INTERVAL;
159 /* The timeout list is a increase order sorted list */
160 mutex_lock(&pinger_mutex);
161 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
162 int ti_timeout = item->ti_timeout;
163 if (timeout > ti_timeout)
164 timeout = ti_timeout;
167 mutex_unlock(&pinger_mutex);
169 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
173 static cfs_waitq_t suspend_timeouts_waitq;
175 cfs_time_t ptlrpc_suspend_wakeup_time(void)
177 return suspend_wakeup_time;
180 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
182 /*XXX: disabled for now, will be replaced by adaptive timeouts */
184 if (imp->imp_no_timeout)
186 imp->imp_no_timeout = 1;
187 cfs_atomic_inc(&suspend_timeouts);
188 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n",
189 cfs_atomic_read(&suspend_timeouts));
193 void ptlrpc_activate_timeouts(struct obd_import *imp)
195 /*XXX: disabled for now, will be replaced by adaptive timeouts */
197 if (!imp->imp_no_timeout)
199 imp->imp_no_timeout = 0;
200 LASSERT(cfs_atomic_read(&suspend_timeouts) > 0);
201 if (cfs_atomic_dec_and_test(&suspend_timeouts)) {
202 suspend_wakeup_time = cfs_time_current();
203 cfs_waitq_signal(&suspend_timeouts_waitq);
205 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n",
206 cfs_atomic_read(&suspend_timeouts));
210 int ptlrpc_check_suspend(void)
212 if (cfs_atomic_read(&suspend_timeouts))
217 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
219 struct l_wait_info lwi;
221 if (cfs_atomic_read(&suspend_timeouts)) {
222 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
223 cfs_atomic_read(&suspend_timeouts));
224 lwi = LWI_INTR(NULL, NULL);
225 l_wait_event(suspend_timeouts_waitq,
226 cfs_atomic_read(&suspend_timeouts) == 0, &lwi);
227 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
235 static void ptlrpc_pinger_process_import(struct obd_import *imp,
236 unsigned long this_ping)
243 spin_lock(&imp->imp_lock);
245 level = imp->imp_state;
246 force = imp->imp_force_verify;
247 force_next = imp->imp_force_next_verify;
249 * This will be used below only if the import is "FULL".
251 suppress = !!(imp->imp_connect_data.ocd_connect_flags &
252 OBD_CONNECT_PINGLESS);
254 imp->imp_force_verify = 0;
256 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
258 spin_unlock(&imp->imp_lock);
262 imp->imp_force_next_verify = 0;
264 spin_unlock(&imp->imp_lock);
266 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
267 "force %u force_next %u deactive %u pingable %u suppress %u\n",
268 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
269 ptlrpc_import_state_name(level), level, force, force_next,
270 imp->imp_deactive, imp->imp_pingable, suppress);
272 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
273 /* wait for a while before trying recovery again */
274 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
275 if (!imp->imp_no_pinger_recover)
276 ptlrpc_initiate_recovery(imp);
277 } else if (level != LUSTRE_IMP_FULL ||
278 imp->imp_obd->obd_no_recov ||
279 imp_is_deactive(imp)) {
280 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
281 "or recovery disabled: %s)\n",
282 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
283 ptlrpc_import_state_name(level));
284 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
289 static int ptlrpc_pinger_main(void *arg)
291 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
294 cfs_daemonize(thread->t_name);
296 /* Record that the thread is running */
297 thread_set_flags(thread, SVC_RUNNING);
298 cfs_waitq_signal(&thread->t_ctl_waitq);
300 /* And now, loop forever, pinging as needed. */
302 cfs_time_t this_ping = cfs_time_current();
303 struct l_wait_info lwi;
304 cfs_duration_t time_to_next_wake;
305 struct timeout_item *item;
308 mutex_lock(&pinger_mutex);
309 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
310 item->ti_cb(item, item->ti_cb_data);
312 cfs_list_for_each(iter, &pinger_imports) {
313 struct obd_import *imp =
314 cfs_list_entry(iter, struct obd_import,
317 ptlrpc_pinger_process_import(imp, this_ping);
318 /* obd_timeout might have changed */
319 if (imp->imp_pingable && imp->imp_next_ping &&
320 cfs_time_after(imp->imp_next_ping,
321 cfs_time_add(this_ping,
322 cfs_time_seconds(PING_INTERVAL))))
323 ptlrpc_update_next_ping(imp, 0);
325 mutex_unlock(&pinger_mutex);
326 /* update memory usage info */
327 obd_update_maxusage();
329 /* Wait until the next ping time, or until we're stopped. */
330 time_to_next_wake = pinger_check_timeout(this_ping);
331 /* The ping sent by ptlrpc_send_rpc may get sent out
332 say .01 second after this.
333 ptlrpc_pinger_sending_on_import will then set the
334 next ping time to next_ping + .01 sec, which means
335 we will SKIP the next ping at next_ping, and the
336 ping will get sent 2 timeouts from now! Beware. */
337 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
338 CFS_TIME_T")\n", time_to_next_wake,
339 cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
340 if (time_to_next_wake > 0) {
341 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
343 cfs_time_seconds(1)),
345 l_wait_event(thread->t_ctl_waitq,
346 thread_is_stopping(thread) ||
347 thread_is_event(thread),
349 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
353 /* woken after adding import to reset timer */
354 thread_test_and_clear_flags(thread, SVC_EVENT);
359 thread_set_flags(thread, SVC_STOPPED);
360 cfs_waitq_signal(&thread->t_ctl_waitq);
362 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
366 static struct ptlrpc_thread *pinger_thread = NULL;
368 int ptlrpc_start_pinger(void)
370 struct l_wait_info lwi = { 0 };
372 #ifndef ENABLE_PINGER
377 if (pinger_thread != NULL)
380 OBD_ALLOC_PTR(pinger_thread);
381 if (pinger_thread == NULL)
383 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
384 cfs_waitq_init(&suspend_timeouts_waitq);
386 strcpy(pinger_thread->t_name, "ll_ping");
388 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
389 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
390 rc = cfs_create_thread(ptlrpc_pinger_main,
391 pinger_thread, CFS_DAEMON_FLAGS);
393 CERROR("cannot start thread: %d\n", rc);
394 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
395 pinger_thread = NULL;
398 l_wait_event(pinger_thread->t_ctl_waitq,
399 thread_is_running(pinger_thread), &lwi);
402 CWARN("Pings will be suppressed at the request of the "
403 "administrator. The configuration shall meet the "
404 "additional requirements described in the manual. "
405 "(Search for the \"suppress_pings\" kernel module "
411 int ptlrpc_pinger_remove_timeouts(void);
413 int ptlrpc_stop_pinger(void)
415 struct l_wait_info lwi = { 0 };
417 #ifndef ENABLE_PINGER
422 if (pinger_thread == NULL)
425 ptlrpc_pinger_remove_timeouts();
426 mutex_lock(&pinger_mutex);
427 thread_set_flags(pinger_thread, SVC_STOPPING);
428 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
429 mutex_unlock(&pinger_mutex);
431 l_wait_event(pinger_thread->t_ctl_waitq,
432 thread_is_stopped(pinger_thread), &lwi);
434 OBD_FREE_PTR(pinger_thread);
435 pinger_thread = NULL;
439 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
441 ptlrpc_update_next_ping(imp, 0);
443 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
445 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
447 ptlrpc_update_next_ping(imp, 1);
448 LASSERT_SPIN_LOCKED(&imp->imp_lock);
450 * Avoid reading stale imp_connect_data. When not sure if pings are
451 * expected or not on next connection, we assume they are not and force
452 * one anyway to guarantee the chance of updating
453 * imp_peer_committed_transno.
455 if (imp->imp_state != LUSTRE_IMP_FULL ||
456 imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_PINGLESS)
457 imp->imp_force_next_verify = 1;
460 int ptlrpc_pinger_add_import(struct obd_import *imp)
463 if (!cfs_list_empty(&imp->imp_pinger_chain))
466 mutex_lock(&pinger_mutex);
467 CDEBUG(D_HA, "adding pingable import %s->%s\n",
468 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
469 /* if we add to pinger we want recovery on this import */
470 imp->imp_obd->obd_no_recov = 0;
471 ptlrpc_update_next_ping(imp, 0);
472 /* XXX sort, blah blah */
473 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
474 class_import_get(imp);
476 ptlrpc_pinger_wake_up();
477 mutex_unlock(&pinger_mutex);
481 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
483 int ptlrpc_pinger_del_import(struct obd_import *imp)
486 if (cfs_list_empty(&imp->imp_pinger_chain))
489 mutex_lock(&pinger_mutex);
490 cfs_list_del_init(&imp->imp_pinger_chain);
491 CDEBUG(D_HA, "removing pingable import %s->%s\n",
492 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
493 /* if we remove from pinger we don't want recovery on this import */
494 imp->imp_obd->obd_no_recov = 1;
495 class_import_put(imp);
496 mutex_unlock(&pinger_mutex);
499 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
502 * Register a timeout callback to the pinger list, and the callback will
503 * be called when timeout happens.
505 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
506 timeout_cb_t cb, void *data)
508 struct timeout_item *ti;
514 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
515 CFS_INIT_LIST_HEAD(&ti->ti_chain);
516 ti->ti_timeout = time;
517 ti->ti_event = event;
519 ti->ti_cb_data = data;
525 * Register timeout event on the the pinger thread.
526 * Note: the timeout list is an sorted list with increased timeout value.
528 static struct timeout_item*
529 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
530 timeout_cb_t cb, void *data)
532 struct timeout_item *item, *tmp;
534 LASSERT_MUTEX_LOCKED(&pinger_mutex);
536 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
537 if (item->ti_event == event)
540 item = ptlrpc_new_timeout(time, event, cb, data);
542 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
543 if (tmp->ti_timeout < time) {
544 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
548 cfs_list_add(&item->ti_chain, &timeout_list);
554 /* Add a client_obd to the timeout event list, when timeout(@time)
555 * happens, the callback(@cb) will be called.
557 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
558 timeout_cb_t cb, void *data,
559 cfs_list_t *obd_list)
561 struct timeout_item *ti;
563 mutex_lock(&pinger_mutex);
564 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
566 mutex_unlock(&pinger_mutex);
569 cfs_list_add(obd_list, &ti->ti_obd_list);
570 mutex_unlock(&pinger_mutex);
573 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
575 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
576 enum timeout_event event)
578 struct timeout_item *ti = NULL, *item;
580 if (cfs_list_empty(obd_list))
582 mutex_lock(&pinger_mutex);
583 cfs_list_del_init(obd_list);
585 * If there are no obd attached to the timeout event
586 * list, remove this timeout event from the pinger
588 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
589 if (item->ti_event == event) {
594 LASSERTF(ti != NULL, "ti is NULL ! \n");
595 if (cfs_list_empty(&ti->ti_obd_list)) {
596 cfs_list_del(&ti->ti_chain);
599 mutex_unlock(&pinger_mutex);
602 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
604 int ptlrpc_pinger_remove_timeouts(void)
606 struct timeout_item *item, *tmp;
608 mutex_lock(&pinger_mutex);
609 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
610 LASSERT(cfs_list_empty(&item->ti_obd_list));
611 cfs_list_del(&item->ti_chain);
614 mutex_unlock(&pinger_mutex);
618 void ptlrpc_pinger_wake_up()
621 thread_add_flags(pinger_thread, SVC_EVENT);
622 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
626 /* Ping evictor thread */
628 #define PET_TERMINATE 2
630 static int pet_refcount = 0;
631 static int pet_state;
632 static cfs_waitq_t pet_waitq;
633 CFS_LIST_HEAD(pet_list);
634 static DEFINE_SPINLOCK(pet_lock);
636 int ping_evictor_wake(struct obd_export *exp)
638 struct obd_device *obd;
640 spin_lock(&pet_lock);
641 if (pet_state != PET_READY) {
642 /* eventually the new obd will call here again. */
643 spin_unlock(&pet_lock);
647 obd = class_exp2obd(exp);
648 if (cfs_list_empty(&obd->obd_evict_list)) {
649 class_incref(obd, "evictor", obd);
650 cfs_list_add(&obd->obd_evict_list, &pet_list);
652 spin_unlock(&pet_lock);
654 cfs_waitq_signal(&pet_waitq);
658 static int ping_evictor_main(void *arg)
660 struct obd_device *obd;
661 struct obd_export *exp;
662 struct l_wait_info lwi = { 0 };
666 cfs_daemonize_ctxt("ll_evictor");
668 CDEBUG(D_HA, "Starting Ping Evictor\n");
669 pet_state = PET_READY;
671 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
672 (pet_state == PET_TERMINATE), &lwi);
674 /* loop until all obd's will be removed */
675 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
678 /* we only get here if pet_exp != NULL, and the end of this
679 * loop is the only place which sets it NULL again, so lock
680 * is not strictly necessary. */
681 spin_lock(&pet_lock);
682 obd = cfs_list_entry(pet_list.next, struct obd_device,
684 spin_unlock(&pet_lock);
686 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
688 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
689 obd->obd_name, expire_time);
691 /* Exports can't be deleted out of the list while we hold
692 * the obd lock (class_unlink_export), which means we can't
693 * lose the last ref on the export. If they've already been
694 * removed from the list, we won't find them here. */
695 spin_lock(&obd->obd_dev_lock);
696 while (!cfs_list_empty(&obd->obd_exports_timed)) {
697 exp = cfs_list_entry(obd->obd_exports_timed.next,
699 exp_obd_chain_timed);
700 if (expire_time > exp->exp_last_request_time) {
701 class_export_get(exp);
702 spin_unlock(&obd->obd_dev_lock);
703 LCONSOLE_WARN("%s: haven't heard from client %s"
704 " (at %s) in %ld seconds. I think"
705 " it's dead, and I am evicting"
706 " it. exp %p, cur %ld expire %ld"
709 obd_uuid2str(&exp->exp_client_uuid),
710 obd_export_nid2str(exp),
711 (long)(cfs_time_current_sec() -
712 exp->exp_last_request_time),
713 exp, (long)cfs_time_current_sec(),
715 (long)exp->exp_last_request_time);
716 CDEBUG(D_HA, "Last request was at %ld\n",
717 exp->exp_last_request_time);
718 class_fail_export(exp);
719 class_export_put(exp);
720 spin_lock(&obd->obd_dev_lock);
722 /* List is sorted, so everyone below is ok */
726 spin_unlock(&obd->obd_dev_lock);
728 spin_lock(&pet_lock);
729 cfs_list_del_init(&obd->obd_evict_list);
730 spin_unlock(&pet_lock);
732 class_decref(obd, "evictor", obd);
734 CDEBUG(D_HA, "Exiting Ping Evictor\n");
739 void ping_evictor_start(void)
743 if (++pet_refcount > 1)
746 cfs_waitq_init(&pet_waitq);
748 rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
751 CERROR("Cannot start ping evictor thread: %d\n", rc);
754 EXPORT_SYMBOL(ping_evictor_start);
756 void ping_evictor_stop(void)
758 if (--pet_refcount > 0)
761 pet_state = PET_TERMINATE;
762 cfs_waitq_signal(&pet_waitq);
764 EXPORT_SYMBOL(ping_evictor_stop);
765 #else /* !__KERNEL__ */
768 * the current implementation of pinger in liblustre is not optimized
772 static struct pinger_data {
774 cfs_time_t pd_this_ping; /* jiffies */
775 cfs_time_t pd_next_ping; /* jiffies */
776 struct ptlrpc_request_set *pd_set;
779 static int pinger_check_rpcs(void *arg)
781 cfs_time_t curtime = cfs_time_current();
782 struct ptlrpc_request *req;
783 struct ptlrpc_request_set *set;
785 struct obd_import *imp;
786 struct pinger_data *pd = &pinger_args;
789 /* prevent recursion */
790 if (pd->pd_recursion++) {
791 CDEBUG(D_HA, "pinger: recursion! quit\n");
797 /* have we reached ping point? */
798 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
803 /* if we have rpc_set already, continue processing it */
805 LASSERT(pd->pd_this_ping);
810 pd->pd_this_ping = curtime;
811 pd->pd_set = ptlrpc_prep_set();
812 if (pd->pd_set == NULL)
816 /* add rpcs into set */
817 mutex_lock(&pinger_mutex);
818 cfs_list_for_each(iter, &pinger_imports) {
819 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
821 int generation, level;
823 if (cfs_time_aftereq(pd->pd_this_ping,
824 imp->imp_next_ping - 5 * CFS_TICK)) {
826 spin_lock(&imp->imp_lock);
827 generation = imp->imp_generation;
828 level = imp->imp_state;
829 spin_unlock(&imp->imp_lock);
831 if (level != LUSTRE_IMP_FULL) {
833 "not pinging %s (in recovery)\n",
834 obd2cli_tgt(imp->imp_obd));
838 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
842 CERROR("OOM trying to ping %s->%s\n",
843 imp->imp_obd->obd_uuid.uuid,
844 obd2cli_tgt(imp->imp_obd));
848 req->rq_no_resend = 1;
849 ptlrpc_request_set_replen(req);
850 req->rq_send_state = LUSTRE_IMP_FULL;
851 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
852 req->rq_import_generation = generation;
853 ptlrpc_set_add_req(set, req);
855 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
856 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
857 imp->imp_next_ping, pd->pd_this_ping);
860 pd->pd_this_ping = curtime;
861 mutex_unlock(&pinger_mutex);
863 /* Might be empty, that's OK. */
864 if (cfs_atomic_read(&set->set_remaining) == 0)
865 CDEBUG(D_RPCTRACE, "nothing to ping\n");
867 cfs_list_for_each(iter, &set->set_requests) {
868 struct ptlrpc_request *req =
869 cfs_list_entry(iter, struct ptlrpc_request,
871 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
872 req->rq_import->imp_obd->obd_uuid.uuid,
873 obd2cli_tgt(req->rq_import->imp_obd));
874 (void)ptl_send_rpc(req, 0);
878 rc = ptlrpc_check_set(NULL, set);
880 /* not finished, and we are not expired, simply return */
881 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
882 cfs_time_seconds(PING_INTERVAL)))) {
883 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
888 /* Expire all the requests that didn't come back. */
889 mutex_lock(&pinger_mutex);
890 cfs_list_for_each(iter, &set->set_requests) {
891 req = cfs_list_entry(iter, struct ptlrpc_request,
894 if (req->rq_phase == RQ_PHASE_COMPLETE)
897 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
900 /* This will also unregister reply. */
901 ptlrpc_expire_one_request(req, 0);
903 /* We're done with this req, let's finally move it to complete
904 * phase and take care of inflights. */
905 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
906 imp = req->rq_import;
907 spin_lock(&imp->imp_lock);
908 if (!cfs_list_empty(&req->rq_list)) {
909 cfs_list_del_init(&req->rq_list);
910 cfs_atomic_dec(&imp->imp_inflight);
912 spin_unlock(&imp->imp_lock);
913 cfs_atomic_dec(&set->set_remaining);
915 mutex_unlock(&pinger_mutex);
917 ptlrpc_set_destroy(set);
921 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
922 cfs_time_seconds(PING_INTERVAL));
923 pd->pd_this_ping = 0; /* XXX for debug */
925 CDEBUG(D_INFO, "finished a round ping\n");
930 static void *pinger_callback = NULL;
931 #endif /* ENABLE_PINGER */
933 int ptlrpc_start_pinger(void)
936 memset(&pinger_args, 0, sizeof(pinger_args));
937 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
944 int ptlrpc_stop_pinger(void)
948 liblustre_deregister_wait_callback(pinger_callback);
953 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
956 mutex_lock(&pinger_mutex);
957 ptlrpc_update_next_ping(imp, 0);
958 if (pinger_args.pd_set == NULL &&
959 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
960 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
961 imp->imp_next_ping, cfs_time_current());
962 pinger_args.pd_next_ping = imp->imp_next_ping;
964 mutex_unlock(&pinger_mutex);
968 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
971 mutex_lock(&pinger_mutex);
972 ptlrpc_update_next_ping(imp, 1);
973 if (pinger_args.pd_set == NULL &&
974 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
975 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
976 imp->imp_next_ping, cfs_time_current());
977 pinger_args.pd_next_ping = imp->imp_next_ping;
979 mutex_unlock(&pinger_mutex);
983 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
984 timeout_cb_t cb, void *data,
985 cfs_list_t *obd_list)
990 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
991 enum timeout_event event)
996 int ptlrpc_pinger_add_import(struct obd_import *imp)
999 if (!cfs_list_empty(&imp->imp_pinger_chain))
1002 CDEBUG(D_HA, "adding pingable import %s->%s\n",
1003 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1004 ptlrpc_pinger_sending_on_import(imp);
1006 mutex_lock(&pinger_mutex);
1007 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
1008 class_import_get(imp);
1009 mutex_unlock(&pinger_mutex);
1014 int ptlrpc_pinger_del_import(struct obd_import *imp)
1017 if (cfs_list_empty(&imp->imp_pinger_chain))
1020 mutex_lock(&pinger_mutex);
1021 cfs_list_del_init(&imp->imp_pinger_chain);
1022 CDEBUG(D_HA, "removing pingable import %s->%s\n",
1023 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1024 class_import_put(imp);
1025 mutex_unlock(&pinger_mutex);
1029 void ptlrpc_pinger_wake_up()
1031 #ifdef ENABLE_PINGER
1032 /* XXX force pinger to run, if needed */
1033 struct obd_import *imp;
1035 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
1036 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
1037 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1038 #ifdef ENABLE_LIBLUSTRE_RECOVERY
1039 if (imp->imp_state == LUSTRE_IMP_DISCON &&
1040 !imp_is_deactive(imp))
1042 /*XXX only recover for the initial connection */
1043 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
1044 imp->imp_state == LUSTRE_IMP_DISCON &&
1045 !imp_is_deactive(imp))
1047 ptlrpc_initiate_recovery(imp);
1048 else if (imp->imp_state != LUSTRE_IMP_FULL)
1049 CDEBUG(D_HA, "Refused to recover import %s->%s "
1050 "state %d, deactive %d\n",
1051 imp->imp_obd->obd_uuid.uuid,
1052 obd2cli_tgt(imp->imp_obd), imp->imp_state,
1053 imp_is_deactive(imp));
1058 #endif /* !__KERNEL__ */