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, 2013, 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");
237 void ptlrpc_pinger_ir_up(void)
239 CDEBUG(D_HA, "IR up\n");
242 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
244 void ptlrpc_pinger_ir_down(void)
246 CDEBUG(D_HA, "IR down\n");
249 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
251 static void ptlrpc_pinger_process_import(struct obd_import *imp,
252 unsigned long this_ping)
259 spin_lock(&imp->imp_lock);
261 level = imp->imp_state;
262 force = imp->imp_force_verify;
263 force_next = imp->imp_force_next_verify;
265 * This will be used below only if the import is "FULL".
267 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
269 imp->imp_force_verify = 0;
271 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
273 spin_unlock(&imp->imp_lock);
277 imp->imp_force_next_verify = 0;
279 spin_unlock(&imp->imp_lock);
281 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
282 "force %u force_next %u deactive %u pingable %u suppress %u\n",
283 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
284 ptlrpc_import_state_name(level), level, force, force_next,
285 imp->imp_deactive, imp->imp_pingable, suppress);
287 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
288 /* wait for a while before trying recovery again */
289 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
290 if (!imp->imp_no_pinger_recover)
291 ptlrpc_initiate_recovery(imp);
292 } else if (level != LUSTRE_IMP_FULL ||
293 imp->imp_obd->obd_no_recov ||
294 imp_is_deactive(imp)) {
295 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
296 "or recovery disabled: %s)\n",
297 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
298 ptlrpc_import_state_name(level));
299 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
304 static int ptlrpc_pinger_main(void *arg)
306 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
309 cfs_daemonize(thread->t_name);
311 /* Record that the thread is running */
312 thread_set_flags(thread, SVC_RUNNING);
313 cfs_waitq_signal(&thread->t_ctl_waitq);
315 /* And now, loop forever, pinging as needed. */
317 cfs_time_t this_ping = cfs_time_current();
318 struct l_wait_info lwi;
319 cfs_duration_t time_to_next_wake;
320 struct timeout_item *item;
323 mutex_lock(&pinger_mutex);
324 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
325 item->ti_cb(item, item->ti_cb_data);
327 cfs_list_for_each(iter, &pinger_imports) {
328 struct obd_import *imp =
329 cfs_list_entry(iter, struct obd_import,
332 ptlrpc_pinger_process_import(imp, this_ping);
333 /* obd_timeout might have changed */
334 if (imp->imp_pingable && imp->imp_next_ping &&
335 cfs_time_after(imp->imp_next_ping,
336 cfs_time_add(this_ping,
337 cfs_time_seconds(PING_INTERVAL))))
338 ptlrpc_update_next_ping(imp, 0);
340 mutex_unlock(&pinger_mutex);
341 /* update memory usage info */
342 obd_update_maxusage();
344 /* Wait until the next ping time, or until we're stopped. */
345 time_to_next_wake = pinger_check_timeout(this_ping);
346 /* The ping sent by ptlrpc_send_rpc may get sent out
347 say .01 second after this.
348 ptlrpc_pinger_sending_on_import will then set the
349 next ping time to next_ping + .01 sec, which means
350 we will SKIP the next ping at next_ping, and the
351 ping will get sent 2 timeouts from now! Beware. */
352 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
353 CFS_TIME_T")\n", time_to_next_wake,
354 cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
355 if (time_to_next_wake > 0) {
356 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
358 cfs_time_seconds(1)),
360 l_wait_event(thread->t_ctl_waitq,
361 thread_is_stopping(thread) ||
362 thread_is_event(thread),
364 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
368 /* woken after adding import to reset timer */
369 thread_test_and_clear_flags(thread, SVC_EVENT);
374 thread_set_flags(thread, SVC_STOPPED);
375 cfs_waitq_signal(&thread->t_ctl_waitq);
377 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
381 static struct ptlrpc_thread pinger_thread;
383 int ptlrpc_start_pinger(void)
385 struct l_wait_info lwi = { 0 };
387 #ifndef ENABLE_PINGER
392 if (!thread_is_init(&pinger_thread) &&
393 !thread_is_stopped(&pinger_thread))
396 cfs_waitq_init(&pinger_thread.t_ctl_waitq);
397 cfs_waitq_init(&suspend_timeouts_waitq);
399 strcpy(pinger_thread.t_name, "ll_ping");
401 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
402 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
403 rc = cfs_create_thread(ptlrpc_pinger_main,
404 &pinger_thread, CFS_DAEMON_FLAGS);
406 CERROR("cannot start thread: %d\n", rc);
409 l_wait_event(pinger_thread.t_ctl_waitq,
410 thread_is_running(&pinger_thread), &lwi);
413 CWARN("Pings will be suppressed at the request of the "
414 "administrator. The configuration shall meet the "
415 "additional requirements described in the manual. "
416 "(Search for the \"suppress_pings\" kernel module "
422 int ptlrpc_pinger_remove_timeouts(void);
424 int ptlrpc_stop_pinger(void)
426 struct l_wait_info lwi = { 0 };
427 #ifndef ENABLE_PINGER
432 if (thread_is_init(&pinger_thread) ||
433 thread_is_stopped(&pinger_thread))
436 ptlrpc_pinger_remove_timeouts();
438 thread_set_flags(&pinger_thread, SVC_STOPPING);
439 cfs_waitq_signal(&pinger_thread.t_ctl_waitq);
441 l_wait_event(pinger_thread.t_ctl_waitq,
442 thread_is_stopped(&pinger_thread), &lwi);
446 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
448 ptlrpc_update_next_ping(imp, 0);
450 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
452 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
454 ptlrpc_update_next_ping(imp, 1);
455 LASSERT_SPIN_LOCKED(&imp->imp_lock);
457 * Avoid reading stale imp_connect_data. When not sure if pings are
458 * expected or not on next connection, we assume they are not and force
459 * one anyway to guarantee the chance of updating
460 * imp_peer_committed_transno.
462 if (imp->imp_state != LUSTRE_IMP_FULL ||
463 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
464 imp->imp_force_next_verify = 1;
467 int ptlrpc_pinger_add_import(struct obd_import *imp)
470 if (!cfs_list_empty(&imp->imp_pinger_chain))
473 mutex_lock(&pinger_mutex);
474 CDEBUG(D_HA, "adding pingable import %s->%s\n",
475 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
476 /* if we add to pinger we want recovery on this import */
477 imp->imp_obd->obd_no_recov = 0;
478 ptlrpc_update_next_ping(imp, 0);
479 /* XXX sort, blah blah */
480 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
481 class_import_get(imp);
483 ptlrpc_pinger_wake_up();
484 mutex_unlock(&pinger_mutex);
488 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
490 int ptlrpc_pinger_del_import(struct obd_import *imp)
493 if (cfs_list_empty(&imp->imp_pinger_chain))
496 mutex_lock(&pinger_mutex);
497 cfs_list_del_init(&imp->imp_pinger_chain);
498 CDEBUG(D_HA, "removing pingable import %s->%s\n",
499 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
500 /* if we remove from pinger we don't want recovery on this import */
501 imp->imp_obd->obd_no_recov = 1;
502 class_import_put(imp);
503 mutex_unlock(&pinger_mutex);
506 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
509 * Register a timeout callback to the pinger list, and the callback will
510 * be called when timeout happens.
512 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
513 timeout_cb_t cb, void *data)
515 struct timeout_item *ti;
521 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
522 CFS_INIT_LIST_HEAD(&ti->ti_chain);
523 ti->ti_timeout = time;
524 ti->ti_event = event;
526 ti->ti_cb_data = data;
532 * Register timeout event on the the pinger thread.
533 * Note: the timeout list is an sorted list with increased timeout value.
535 static struct timeout_item*
536 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
537 timeout_cb_t cb, void *data)
539 struct timeout_item *item, *tmp;
541 LASSERT_MUTEX_LOCKED(&pinger_mutex);
543 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
544 if (item->ti_event == event)
547 item = ptlrpc_new_timeout(time, event, cb, data);
549 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
550 if (tmp->ti_timeout < time) {
551 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
555 cfs_list_add(&item->ti_chain, &timeout_list);
561 /* Add a client_obd to the timeout event list, when timeout(@time)
562 * happens, the callback(@cb) will be called.
564 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
565 timeout_cb_t cb, void *data,
566 cfs_list_t *obd_list)
568 struct timeout_item *ti;
570 mutex_lock(&pinger_mutex);
571 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
573 mutex_unlock(&pinger_mutex);
576 cfs_list_add(obd_list, &ti->ti_obd_list);
577 mutex_unlock(&pinger_mutex);
580 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
582 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
583 enum timeout_event event)
585 struct timeout_item *ti = NULL, *item;
587 if (cfs_list_empty(obd_list))
589 mutex_lock(&pinger_mutex);
590 cfs_list_del_init(obd_list);
592 * If there are no obd attached to the timeout event
593 * list, remove this timeout event from the pinger
595 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
596 if (item->ti_event == event) {
601 LASSERTF(ti != NULL, "ti is NULL ! \n");
602 if (cfs_list_empty(&ti->ti_obd_list)) {
603 cfs_list_del(&ti->ti_chain);
606 mutex_unlock(&pinger_mutex);
609 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
611 int ptlrpc_pinger_remove_timeouts(void)
613 struct timeout_item *item, *tmp;
615 mutex_lock(&pinger_mutex);
616 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
617 LASSERT(cfs_list_empty(&item->ti_obd_list));
618 cfs_list_del(&item->ti_chain);
621 mutex_unlock(&pinger_mutex);
625 void ptlrpc_pinger_wake_up()
628 thread_add_flags(&pinger_thread, SVC_EVENT);
629 cfs_waitq_signal(&pinger_thread.t_ctl_waitq);
633 /* Ping evictor thread */
635 #define PET_TERMINATE 2
637 static int pet_refcount = 0;
638 static int pet_state;
639 static cfs_waitq_t pet_waitq;
640 CFS_LIST_HEAD(pet_list);
641 static DEFINE_SPINLOCK(pet_lock);
643 int ping_evictor_wake(struct obd_export *exp)
645 struct obd_device *obd;
647 spin_lock(&pet_lock);
648 if (pet_state != PET_READY) {
649 /* eventually the new obd will call here again. */
650 spin_unlock(&pet_lock);
654 obd = class_exp2obd(exp);
655 if (cfs_list_empty(&obd->obd_evict_list)) {
656 class_incref(obd, "evictor", obd);
657 cfs_list_add(&obd->obd_evict_list, &pet_list);
659 spin_unlock(&pet_lock);
661 cfs_waitq_signal(&pet_waitq);
665 static int ping_evictor_main(void *arg)
667 struct obd_device *obd;
668 struct obd_export *exp;
669 struct l_wait_info lwi = { 0 };
673 cfs_daemonize_ctxt("ll_evictor");
675 CDEBUG(D_HA, "Starting Ping Evictor\n");
676 pet_state = PET_READY;
678 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
679 (pet_state == PET_TERMINATE), &lwi);
681 /* loop until all obd's will be removed */
682 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
685 /* we only get here if pet_exp != NULL, and the end of this
686 * loop is the only place which sets it NULL again, so lock
687 * is not strictly necessary. */
688 spin_lock(&pet_lock);
689 obd = cfs_list_entry(pet_list.next, struct obd_device,
691 spin_unlock(&pet_lock);
693 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
695 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
696 obd->obd_name, expire_time);
698 /* Exports can't be deleted out of the list while we hold
699 * the obd lock (class_unlink_export), which means we can't
700 * lose the last ref on the export. If they've already been
701 * removed from the list, we won't find them here. */
702 spin_lock(&obd->obd_dev_lock);
703 while (!cfs_list_empty(&obd->obd_exports_timed)) {
704 exp = cfs_list_entry(obd->obd_exports_timed.next,
706 exp_obd_chain_timed);
707 if (expire_time > exp->exp_last_request_time) {
708 class_export_get(exp);
709 spin_unlock(&obd->obd_dev_lock);
710 LCONSOLE_WARN("%s: haven't heard from client %s"
711 " (at %s) in %ld seconds. I think"
712 " it's dead, and I am evicting"
713 " it. exp %p, cur %ld expire %ld"
716 obd_uuid2str(&exp->exp_client_uuid),
717 obd_export_nid2str(exp),
718 (long)(cfs_time_current_sec() -
719 exp->exp_last_request_time),
720 exp, (long)cfs_time_current_sec(),
722 (long)exp->exp_last_request_time);
723 CDEBUG(D_HA, "Last request was at %ld\n",
724 exp->exp_last_request_time);
725 class_fail_export(exp);
726 class_export_put(exp);
727 spin_lock(&obd->obd_dev_lock);
729 /* List is sorted, so everyone below is ok */
733 spin_unlock(&obd->obd_dev_lock);
735 spin_lock(&pet_lock);
736 cfs_list_del_init(&obd->obd_evict_list);
737 spin_unlock(&pet_lock);
739 class_decref(obd, "evictor", obd);
741 CDEBUG(D_HA, "Exiting Ping Evictor\n");
746 void ping_evictor_start(void)
750 if (++pet_refcount > 1)
753 cfs_waitq_init(&pet_waitq);
755 rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
758 CERROR("Cannot start ping evictor thread: %d\n", rc);
761 EXPORT_SYMBOL(ping_evictor_start);
763 void ping_evictor_stop(void)
765 if (--pet_refcount > 0)
768 pet_state = PET_TERMINATE;
769 cfs_waitq_signal(&pet_waitq);
771 EXPORT_SYMBOL(ping_evictor_stop);
772 #else /* !__KERNEL__ */
775 * the current implementation of pinger in liblustre is not optimized
779 static struct pinger_data {
781 cfs_time_t pd_this_ping; /* jiffies */
782 cfs_time_t pd_next_ping; /* jiffies */
783 struct ptlrpc_request_set *pd_set;
786 static int pinger_check_rpcs(void *arg)
788 cfs_time_t curtime = cfs_time_current();
789 struct ptlrpc_request *req;
790 struct ptlrpc_request_set *set;
792 struct obd_import *imp;
793 struct pinger_data *pd = &pinger_args;
796 /* prevent recursion */
797 if (pd->pd_recursion++) {
798 CDEBUG(D_HA, "pinger: recursion! quit\n");
804 /* have we reached ping point? */
805 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
810 /* if we have rpc_set already, continue processing it */
812 LASSERT(pd->pd_this_ping);
817 pd->pd_this_ping = curtime;
818 pd->pd_set = ptlrpc_prep_set();
819 if (pd->pd_set == NULL)
823 /* add rpcs into set */
824 mutex_lock(&pinger_mutex);
825 cfs_list_for_each(iter, &pinger_imports) {
826 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
828 int generation, level;
830 if (cfs_time_aftereq(pd->pd_this_ping,
831 imp->imp_next_ping - 5 * CFS_TICK)) {
833 spin_lock(&imp->imp_lock);
834 generation = imp->imp_generation;
835 level = imp->imp_state;
836 spin_unlock(&imp->imp_lock);
838 if (level != LUSTRE_IMP_FULL) {
840 "not pinging %s (in recovery)\n",
841 obd2cli_tgt(imp->imp_obd));
845 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
849 CERROR("OOM trying to ping %s->%s\n",
850 imp->imp_obd->obd_uuid.uuid,
851 obd2cli_tgt(imp->imp_obd));
855 req->rq_no_resend = 1;
856 ptlrpc_request_set_replen(req);
857 req->rq_send_state = LUSTRE_IMP_FULL;
858 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
859 req->rq_import_generation = generation;
860 ptlrpc_set_add_req(set, req);
862 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
863 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
864 imp->imp_next_ping, pd->pd_this_ping);
867 pd->pd_this_ping = curtime;
868 mutex_unlock(&pinger_mutex);
870 /* Might be empty, that's OK. */
871 if (cfs_atomic_read(&set->set_remaining) == 0)
872 CDEBUG(D_RPCTRACE, "nothing to ping\n");
874 cfs_list_for_each(iter, &set->set_requests) {
875 struct ptlrpc_request *req =
876 cfs_list_entry(iter, struct ptlrpc_request,
878 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
879 req->rq_import->imp_obd->obd_uuid.uuid,
880 obd2cli_tgt(req->rq_import->imp_obd));
881 (void)ptl_send_rpc(req, 0);
885 rc = ptlrpc_check_set(NULL, set);
887 /* not finished, and we are not expired, simply return */
888 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
889 cfs_time_seconds(PING_INTERVAL)))) {
890 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
895 /* Expire all the requests that didn't come back. */
896 mutex_lock(&pinger_mutex);
897 cfs_list_for_each(iter, &set->set_requests) {
898 req = cfs_list_entry(iter, struct ptlrpc_request,
901 if (req->rq_phase == RQ_PHASE_COMPLETE)
904 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
907 /* This will also unregister reply. */
908 ptlrpc_expire_one_request(req, 0);
910 /* We're done with this req, let's finally move it to complete
911 * phase and take care of inflights. */
912 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
913 imp = req->rq_import;
914 spin_lock(&imp->imp_lock);
915 if (!cfs_list_empty(&req->rq_list)) {
916 cfs_list_del_init(&req->rq_list);
917 cfs_atomic_dec(&imp->imp_inflight);
919 spin_unlock(&imp->imp_lock);
920 cfs_atomic_dec(&set->set_remaining);
922 mutex_unlock(&pinger_mutex);
924 ptlrpc_set_destroy(set);
928 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
929 cfs_time_seconds(PING_INTERVAL));
930 pd->pd_this_ping = 0; /* XXX for debug */
932 CDEBUG(D_INFO, "finished a round ping\n");
937 static void *pinger_callback = NULL;
938 #endif /* ENABLE_PINGER */
940 int ptlrpc_start_pinger(void)
943 memset(&pinger_args, 0, sizeof(pinger_args));
944 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
951 int ptlrpc_stop_pinger(void)
955 liblustre_deregister_wait_callback(pinger_callback);
960 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
963 mutex_lock(&pinger_mutex);
964 ptlrpc_update_next_ping(imp, 0);
965 if (pinger_args.pd_set == NULL &&
966 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
967 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
968 imp->imp_next_ping, cfs_time_current());
969 pinger_args.pd_next_ping = imp->imp_next_ping;
971 mutex_unlock(&pinger_mutex);
975 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
978 mutex_lock(&pinger_mutex);
979 ptlrpc_update_next_ping(imp, 1);
980 if (pinger_args.pd_set == NULL &&
981 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
982 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
983 imp->imp_next_ping, cfs_time_current());
984 pinger_args.pd_next_ping = imp->imp_next_ping;
986 mutex_unlock(&pinger_mutex);
990 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
991 timeout_cb_t cb, void *data,
992 cfs_list_t *obd_list)
997 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
998 enum timeout_event event)
1003 int ptlrpc_pinger_add_import(struct obd_import *imp)
1006 if (!cfs_list_empty(&imp->imp_pinger_chain))
1009 CDEBUG(D_HA, "adding pingable import %s->%s\n",
1010 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1011 ptlrpc_pinger_sending_on_import(imp);
1013 mutex_lock(&pinger_mutex);
1014 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
1015 class_import_get(imp);
1016 mutex_unlock(&pinger_mutex);
1021 int ptlrpc_pinger_del_import(struct obd_import *imp)
1024 if (cfs_list_empty(&imp->imp_pinger_chain))
1027 mutex_lock(&pinger_mutex);
1028 cfs_list_del_init(&imp->imp_pinger_chain);
1029 CDEBUG(D_HA, "removing pingable import %s->%s\n",
1030 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1031 class_import_put(imp);
1032 mutex_unlock(&pinger_mutex);
1036 void ptlrpc_pinger_wake_up()
1038 #ifdef ENABLE_PINGER
1039 /* XXX force pinger to run, if needed */
1040 struct obd_import *imp;
1042 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
1043 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
1044 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
1045 #ifdef ENABLE_LIBLUSTRE_RECOVERY
1046 if (imp->imp_state == LUSTRE_IMP_DISCON &&
1047 !imp_is_deactive(imp))
1049 /*XXX only recover for the initial connection */
1050 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
1051 imp->imp_state == LUSTRE_IMP_DISCON &&
1052 !imp_is_deactive(imp))
1054 ptlrpc_initiate_recovery(imp);
1055 else if (imp->imp_state != LUSTRE_IMP_FULL)
1056 CDEBUG(D_HA, "Refused to recover import %s->%s "
1057 "state %d, deactive %d\n",
1058 imp->imp_obd->obd_uuid.uuid,
1059 obd2cli_tgt(imp->imp_obd), imp->imp_state,
1060 imp_is_deactive(imp));
1065 #endif /* !__KERNEL__ */