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, Whamcloud, Inc.
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 cfs_mutex_t pinger_mutex;
52 static CFS_LIST_HEAD(pinger_imports);
53 static cfs_list_t timeout_list = CFS_LIST_HEAD_INIT(timeout_list);
54 struct ptlrpc_request *
55 ptlrpc_prep_ping(struct obd_import *imp)
57 struct ptlrpc_request *req;
59 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
60 LUSTRE_OBD_VERSION, OBD_PING);
62 ptlrpc_request_set_replen(req);
63 req->rq_no_resend = req->rq_no_delay = 1;
68 int ptlrpc_obd_ping(struct obd_device *obd)
71 struct ptlrpc_request *req;
74 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
78 req->rq_send_state = LUSTRE_IMP_FULL;
80 rc = ptlrpc_queue_wait(req);
82 ptlrpc_req_finished(req);
86 EXPORT_SYMBOL(ptlrpc_obd_ping);
88 int ptlrpc_ping(struct obd_import *imp)
90 struct ptlrpc_request *req;
93 req = ptlrpc_prep_ping(imp);
95 CERROR("OOM trying to ping %s->%s\n",
96 imp->imp_obd->obd_uuid.uuid,
97 obd2cli_tgt(imp->imp_obd));
101 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
102 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
103 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
108 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
111 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
112 if (imp->imp_state == LUSTRE_IMP_DISCON) {
113 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
115 at_get(&imp->imp_at.iat_net_latency));
116 time = min(time, dtime);
118 imp->imp_next_ping = cfs_time_shift(time);
119 #endif /* ENABLE_PINGER */
122 void ptlrpc_ping_import_soon(struct obd_import *imp)
124 imp->imp_next_ping = cfs_time_current();
127 static inline int imp_is_deactive(struct obd_import *imp)
129 return (imp->imp_deactive ||
130 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
133 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
135 if (imp->imp_server_timeout)
136 return cfs_time_shift(obd_timeout / 2);
138 return cfs_time_shift(obd_timeout);
141 static cfs_atomic_t suspend_timeouts = CFS_ATOMIC_INIT(0);
142 static cfs_time_t suspend_wakeup_time = 0;
144 cfs_duration_t pinger_check_timeout(cfs_time_t time)
146 struct timeout_item *item;
147 cfs_time_t timeout = PING_INTERVAL;
149 /* The timeout list is a increase order sorted list */
150 cfs_mutex_lock(&pinger_mutex);
151 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
152 int ti_timeout = item->ti_timeout;
153 if (timeout > ti_timeout)
154 timeout = ti_timeout;
157 cfs_mutex_unlock(&pinger_mutex);
159 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
163 static cfs_waitq_t suspend_timeouts_waitq;
165 cfs_time_t ptlrpc_suspend_wakeup_time(void)
167 return suspend_wakeup_time;
170 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
172 /*XXX: disabled for now, will be replaced by adaptive timeouts */
174 if (imp->imp_no_timeout)
176 imp->imp_no_timeout = 1;
177 cfs_atomic_inc(&suspend_timeouts);
178 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n",
179 cfs_atomic_read(&suspend_timeouts));
183 void ptlrpc_activate_timeouts(struct obd_import *imp)
185 /*XXX: disabled for now, will be replaced by adaptive timeouts */
187 if (!imp->imp_no_timeout)
189 imp->imp_no_timeout = 0;
190 LASSERT(cfs_atomic_read(&suspend_timeouts) > 0);
191 if (cfs_atomic_dec_and_test(&suspend_timeouts)) {
192 suspend_wakeup_time = cfs_time_current();
193 cfs_waitq_signal(&suspend_timeouts_waitq);
195 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n",
196 cfs_atomic_read(&suspend_timeouts));
200 int ptlrpc_check_suspend(void)
202 if (cfs_atomic_read(&suspend_timeouts))
207 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
209 struct l_wait_info lwi;
211 if (cfs_atomic_read(&suspend_timeouts)) {
212 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
213 cfs_atomic_read(&suspend_timeouts));
214 lwi = LWI_INTR(NULL, NULL);
215 l_wait_event(suspend_timeouts_waitq,
216 cfs_atomic_read(&suspend_timeouts) == 0, &lwi);
217 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
225 static void ptlrpc_pinger_process_import(struct obd_import *imp,
226 unsigned long this_ping)
230 cfs_spin_lock(&imp->imp_lock);
231 level = imp->imp_state;
232 force = imp->imp_force_verify;
234 imp->imp_force_verify = 0;
235 cfs_spin_unlock(&imp->imp_lock);
237 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
238 "level %s/%u force %u deactive %u pingable %u\n",
239 ptlrpc_import_state_name(level), level,
240 force, imp->imp_deactive, imp->imp_pingable);
242 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
243 this_ping) && force == 0)
246 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
247 /* wait for a while before trying recovery again */
248 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
249 if (!imp->imp_no_pinger_recover)
250 ptlrpc_initiate_recovery(imp);
251 } else if (level != LUSTRE_IMP_FULL ||
252 imp->imp_obd->obd_no_recov ||
253 imp_is_deactive(imp)) {
254 CDEBUG(D_HA, "not pinging %s (in recovery "
255 " or recovery disabled: %s)\n",
256 obd2cli_tgt(imp->imp_obd),
257 ptlrpc_import_state_name(level));
258 } else if (imp->imp_pingable || force) {
263 static int ptlrpc_pinger_main(void *arg)
265 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
268 cfs_daemonize(thread->t_name);
270 /* Record that the thread is running */
271 thread_set_flags(thread, SVC_RUNNING);
272 cfs_waitq_signal(&thread->t_ctl_waitq);
274 /* And now, loop forever, pinging as needed. */
276 cfs_time_t this_ping = cfs_time_current();
277 struct l_wait_info lwi;
278 cfs_duration_t time_to_next_wake;
279 struct timeout_item *item;
282 cfs_mutex_lock(&pinger_mutex);
283 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
284 item->ti_cb(item, item->ti_cb_data);
286 cfs_list_for_each(iter, &pinger_imports) {
287 struct obd_import *imp =
288 cfs_list_entry(iter, struct obd_import,
291 ptlrpc_pinger_process_import(imp, this_ping);
292 /* obd_timeout might have changed */
293 if (imp->imp_pingable && imp->imp_next_ping &&
294 cfs_time_after(imp->imp_next_ping,
295 cfs_time_add(this_ping,
296 cfs_time_seconds(PING_INTERVAL))))
297 ptlrpc_update_next_ping(imp, 0);
299 cfs_mutex_unlock(&pinger_mutex);
300 /* update memory usage info */
301 obd_update_maxusage();
303 /* Wait until the next ping time, or until we're stopped. */
304 time_to_next_wake = pinger_check_timeout(this_ping);
305 /* The ping sent by ptlrpc_send_rpc may get sent out
306 say .01 second after this.
307 ptlrpc_pinger_sending_on_import will then set the
308 next ping time to next_ping + .01 sec, which means
309 we will SKIP the next ping at next_ping, and the
310 ping will get sent 2 timeouts from now! Beware. */
311 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
312 CFS_TIME_T")\n", time_to_next_wake,
313 cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
314 if (time_to_next_wake > 0) {
315 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
317 cfs_time_seconds(1)),
319 l_wait_event(thread->t_ctl_waitq,
320 thread_is_stopping(thread) ||
321 thread_is_event(thread),
323 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
327 /* woken after adding import to reset timer */
328 thread_test_and_clear_flags(thread, SVC_EVENT);
333 thread_set_flags(thread, SVC_STOPPED);
334 cfs_waitq_signal(&thread->t_ctl_waitq);
336 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
340 static struct ptlrpc_thread *pinger_thread = NULL;
342 int ptlrpc_start_pinger(void)
344 struct l_wait_info lwi = { 0 };
346 #ifndef ENABLE_PINGER
351 if (pinger_thread != NULL)
354 OBD_ALLOC_PTR(pinger_thread);
355 if (pinger_thread == NULL)
357 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
358 cfs_waitq_init(&suspend_timeouts_waitq);
360 strcpy(pinger_thread->t_name, "ll_ping");
362 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
363 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
364 rc = cfs_create_thread(ptlrpc_pinger_main,
365 pinger_thread, CFS_DAEMON_FLAGS);
367 CERROR("cannot start thread: %d\n", rc);
368 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
369 pinger_thread = NULL;
372 l_wait_event(pinger_thread->t_ctl_waitq,
373 thread_is_running(pinger_thread), &lwi);
378 int ptlrpc_pinger_remove_timeouts(void);
380 int ptlrpc_stop_pinger(void)
382 struct l_wait_info lwi = { 0 };
384 #ifndef ENABLE_PINGER
389 if (pinger_thread == NULL)
392 ptlrpc_pinger_remove_timeouts();
393 cfs_mutex_lock(&pinger_mutex);
394 thread_set_flags(pinger_thread, SVC_STOPPING);
395 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
396 cfs_mutex_unlock(&pinger_mutex);
398 l_wait_event(pinger_thread->t_ctl_waitq,
399 thread_is_stopped(pinger_thread), &lwi);
401 OBD_FREE_PTR(pinger_thread);
402 pinger_thread = NULL;
406 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
408 ptlrpc_update_next_ping(imp, 0);
411 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
413 ptlrpc_update_next_ping(imp, 1);
416 int ptlrpc_pinger_add_import(struct obd_import *imp)
419 if (!cfs_list_empty(&imp->imp_pinger_chain))
422 cfs_mutex_lock(&pinger_mutex);
423 CDEBUG(D_HA, "adding pingable import %s->%s\n",
424 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
425 /* if we add to pinger we want recovery on this import */
426 imp->imp_obd->obd_no_recov = 0;
427 ptlrpc_update_next_ping(imp, 0);
428 /* XXX sort, blah blah */
429 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
430 class_import_get(imp);
432 ptlrpc_pinger_wake_up();
433 cfs_mutex_unlock(&pinger_mutex);
438 int ptlrpc_pinger_del_import(struct obd_import *imp)
441 if (cfs_list_empty(&imp->imp_pinger_chain))
444 cfs_mutex_lock(&pinger_mutex);
445 cfs_list_del_init(&imp->imp_pinger_chain);
446 CDEBUG(D_HA, "removing pingable import %s->%s\n",
447 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
448 /* if we remove from pinger we don't want recovery on this import */
449 imp->imp_obd->obd_no_recov = 1;
450 class_import_put(imp);
451 cfs_mutex_unlock(&pinger_mutex);
456 * Register a timeout callback to the pinger list, and the callback will
457 * be called when timeout happens.
459 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
460 timeout_cb_t cb, void *data)
462 struct timeout_item *ti;
468 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
469 CFS_INIT_LIST_HEAD(&ti->ti_chain);
470 ti->ti_timeout = time;
471 ti->ti_event = event;
473 ti->ti_cb_data = data;
479 * Register timeout event on the the pinger thread.
480 * Note: the timeout list is an sorted list with increased timeout value.
482 static struct timeout_item*
483 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
484 timeout_cb_t cb, void *data)
486 struct timeout_item *item, *tmp;
488 LASSERT_MUTEX_LOCKED(&pinger_mutex);
490 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
491 if (item->ti_event == event)
494 item = ptlrpc_new_timeout(time, event, cb, data);
496 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
497 if (tmp->ti_timeout < time) {
498 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
502 cfs_list_add(&item->ti_chain, &timeout_list);
508 /* Add a client_obd to the timeout event list, when timeout(@time)
509 * happens, the callback(@cb) will be called.
511 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
512 timeout_cb_t cb, void *data,
513 cfs_list_t *obd_list)
515 struct timeout_item *ti;
517 cfs_mutex_lock(&pinger_mutex);
518 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
520 cfs_mutex_unlock(&pinger_mutex);
523 cfs_list_add(obd_list, &ti->ti_obd_list);
524 cfs_mutex_unlock(&pinger_mutex);
528 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
529 enum timeout_event event)
531 struct timeout_item *ti = NULL, *item;
533 if (cfs_list_empty(obd_list))
535 cfs_mutex_lock(&pinger_mutex);
536 cfs_list_del_init(obd_list);
538 * If there are no obd attached to the timeout event
539 * list, remove this timeout event from the pinger
541 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
542 if (item->ti_event == event) {
547 LASSERTF(ti != NULL, "ti is NULL ! \n");
548 if (cfs_list_empty(&ti->ti_obd_list)) {
549 cfs_list_del(&ti->ti_chain);
552 cfs_mutex_unlock(&pinger_mutex);
556 int ptlrpc_pinger_remove_timeouts(void)
558 struct timeout_item *item, *tmp;
560 cfs_mutex_lock(&pinger_mutex);
561 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
562 LASSERT(cfs_list_empty(&item->ti_obd_list));
563 cfs_list_del(&item->ti_chain);
566 cfs_mutex_unlock(&pinger_mutex);
570 void ptlrpc_pinger_wake_up()
573 thread_add_flags(pinger_thread, SVC_EVENT);
574 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
578 /* Ping evictor thread */
580 #define PET_TERMINATE 2
582 static int pet_refcount = 0;
583 static int pet_state;
584 static cfs_waitq_t pet_waitq;
585 CFS_LIST_HEAD(pet_list);
586 static cfs_spinlock_t pet_lock = CFS_SPIN_LOCK_UNLOCKED;
588 int ping_evictor_wake(struct obd_export *exp)
590 struct obd_device *obd;
592 cfs_spin_lock(&pet_lock);
593 if (pet_state != PET_READY) {
594 /* eventually the new obd will call here again. */
595 cfs_spin_unlock(&pet_lock);
599 obd = class_exp2obd(exp);
600 if (cfs_list_empty(&obd->obd_evict_list)) {
601 class_incref(obd, "evictor", obd);
602 cfs_list_add(&obd->obd_evict_list, &pet_list);
604 cfs_spin_unlock(&pet_lock);
606 cfs_waitq_signal(&pet_waitq);
610 static int ping_evictor_main(void *arg)
612 struct obd_device *obd;
613 struct obd_export *exp;
614 struct l_wait_info lwi = { 0 };
618 cfs_daemonize_ctxt("ll_evictor");
620 CDEBUG(D_HA, "Starting Ping Evictor\n");
621 pet_state = PET_READY;
623 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
624 (pet_state == PET_TERMINATE), &lwi);
626 /* loop until all obd's will be removed */
627 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
630 /* we only get here if pet_exp != NULL, and the end of this
631 * loop is the only place which sets it NULL again, so lock
632 * is not strictly necessary. */
633 cfs_spin_lock(&pet_lock);
634 obd = cfs_list_entry(pet_list.next, struct obd_device,
636 cfs_spin_unlock(&pet_lock);
638 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
640 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
641 obd->obd_name, expire_time);
643 /* Exports can't be deleted out of the list while we hold
644 * the obd lock (class_unlink_export), which means we can't
645 * lose the last ref on the export. If they've already been
646 * removed from the list, we won't find them here. */
647 cfs_spin_lock(&obd->obd_dev_lock);
648 while (!cfs_list_empty(&obd->obd_exports_timed)) {
649 exp = cfs_list_entry(obd->obd_exports_timed.next,
651 exp_obd_chain_timed);
652 if (expire_time > exp->exp_last_request_time) {
653 class_export_get(exp);
654 cfs_spin_unlock(&obd->obd_dev_lock);
655 LCONSOLE_WARN("%s: haven't heard from client %s"
656 " (at %s) in %ld seconds. I think"
657 " it's dead, and I am evicting"
658 " it. exp %p, cur %ld expire %ld"
661 obd_uuid2str(&exp->exp_client_uuid),
662 obd_export_nid2str(exp),
663 (long)(cfs_time_current_sec() -
664 exp->exp_last_request_time),
665 exp, (long)cfs_time_current_sec(),
667 (long)exp->exp_last_request_time);
668 CDEBUG(D_HA, "Last request was at %ld\n",
669 exp->exp_last_request_time);
670 class_fail_export(exp);
671 class_export_put(exp);
672 cfs_spin_lock(&obd->obd_dev_lock);
674 /* List is sorted, so everyone below is ok */
678 cfs_spin_unlock(&obd->obd_dev_lock);
680 cfs_spin_lock(&pet_lock);
681 cfs_list_del_init(&obd->obd_evict_list);
682 cfs_spin_unlock(&pet_lock);
684 class_decref(obd, "evictor", obd);
686 CDEBUG(D_HA, "Exiting Ping Evictor\n");
691 void ping_evictor_start(void)
695 if (++pet_refcount > 1)
698 cfs_waitq_init(&pet_waitq);
700 rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
703 CERROR("Cannot start ping evictor thread: %d\n", rc);
706 EXPORT_SYMBOL(ping_evictor_start);
708 void ping_evictor_stop(void)
710 if (--pet_refcount > 0)
713 pet_state = PET_TERMINATE;
714 cfs_waitq_signal(&pet_waitq);
716 EXPORT_SYMBOL(ping_evictor_stop);
717 #else /* !__KERNEL__ */
720 * the current implementation of pinger in liblustre is not optimized
724 static struct pinger_data {
726 cfs_time_t pd_this_ping; /* jiffies */
727 cfs_time_t pd_next_ping; /* jiffies */
728 struct ptlrpc_request_set *pd_set;
731 static int pinger_check_rpcs(void *arg)
733 cfs_time_t curtime = cfs_time_current();
734 struct ptlrpc_request *req;
735 struct ptlrpc_request_set *set;
737 struct obd_import *imp;
738 struct pinger_data *pd = &pinger_args;
741 /* prevent recursion */
742 if (pd->pd_recursion++) {
743 CDEBUG(D_HA, "pinger: recursion! quit\n");
749 /* have we reached ping point? */
750 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
755 /* if we have rpc_set already, continue processing it */
757 LASSERT(pd->pd_this_ping);
762 pd->pd_this_ping = curtime;
763 pd->pd_set = ptlrpc_prep_set();
764 if (pd->pd_set == NULL)
768 /* add rpcs into set */
769 cfs_mutex_lock(&pinger_mutex);
770 cfs_list_for_each(iter, &pinger_imports) {
771 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
773 int generation, level;
775 if (cfs_time_aftereq(pd->pd_this_ping,
776 imp->imp_next_ping - 5 * CFS_TICK)) {
778 cfs_spin_lock(&imp->imp_lock);
779 generation = imp->imp_generation;
780 level = imp->imp_state;
781 cfs_spin_unlock(&imp->imp_lock);
783 if (level != LUSTRE_IMP_FULL) {
785 "not pinging %s (in recovery)\n",
786 obd2cli_tgt(imp->imp_obd));
790 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
794 CERROR("OOM trying to ping %s->%s\n",
795 imp->imp_obd->obd_uuid.uuid,
796 obd2cli_tgt(imp->imp_obd));
800 req->rq_no_resend = 1;
801 ptlrpc_request_set_replen(req);
802 req->rq_send_state = LUSTRE_IMP_FULL;
803 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
804 req->rq_import_generation = generation;
805 ptlrpc_set_add_req(set, req);
807 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
808 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
809 imp->imp_next_ping, pd->pd_this_ping);
812 pd->pd_this_ping = curtime;
813 cfs_mutex_unlock(&pinger_mutex);
815 /* Might be empty, that's OK. */
816 if (cfs_atomic_read(&set->set_remaining) == 0)
817 CDEBUG(D_RPCTRACE, "nothing to ping\n");
819 cfs_list_for_each(iter, &set->set_requests) {
820 struct ptlrpc_request *req =
821 cfs_list_entry(iter, struct ptlrpc_request,
823 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
824 req->rq_import->imp_obd->obd_uuid.uuid,
825 obd2cli_tgt(req->rq_import->imp_obd));
826 (void)ptl_send_rpc(req, 0);
830 rc = ptlrpc_check_set(NULL, set);
832 /* not finished, and we are not expired, simply return */
833 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
834 cfs_time_seconds(PING_INTERVAL)))) {
835 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
840 /* Expire all the requests that didn't come back. */
841 cfs_mutex_lock(&pinger_mutex);
842 cfs_list_for_each(iter, &set->set_requests) {
843 req = cfs_list_entry(iter, struct ptlrpc_request,
846 if (req->rq_phase == RQ_PHASE_COMPLETE)
849 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
852 /* This will also unregister reply. */
853 ptlrpc_expire_one_request(req, 0);
855 /* We're done with this req, let's finally move it to complete
856 * phase and take care of inflights. */
857 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
858 imp = req->rq_import;
859 cfs_spin_lock(&imp->imp_lock);
860 if (!cfs_list_empty(&req->rq_list)) {
861 cfs_list_del_init(&req->rq_list);
862 cfs_atomic_dec(&imp->imp_inflight);
864 cfs_spin_unlock(&imp->imp_lock);
865 cfs_atomic_dec(&set->set_remaining);
867 cfs_mutex_unlock(&pinger_mutex);
869 ptlrpc_set_destroy(set);
873 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
874 cfs_time_seconds(PING_INTERVAL));
875 pd->pd_this_ping = 0; /* XXX for debug */
877 CDEBUG(D_INFO, "finished a round ping\n");
882 static void *pinger_callback = NULL;
883 #endif /* ENABLE_PINGER */
885 int ptlrpc_start_pinger(void)
888 memset(&pinger_args, 0, sizeof(pinger_args));
889 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
896 int ptlrpc_stop_pinger(void)
900 liblustre_deregister_wait_callback(pinger_callback);
905 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
908 cfs_mutex_lock(&pinger_mutex);
909 ptlrpc_update_next_ping(imp, 0);
910 if (pinger_args.pd_set == NULL &&
911 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
912 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
913 imp->imp_next_ping, cfs_time_current());
914 pinger_args.pd_next_ping = imp->imp_next_ping;
916 cfs_mutex_unlock(&pinger_mutex);
920 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
923 cfs_mutex_lock(&pinger_mutex);
924 ptlrpc_update_next_ping(imp, 1);
925 if (pinger_args.pd_set == NULL &&
926 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
927 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
928 imp->imp_next_ping, cfs_time_current());
929 pinger_args.pd_next_ping = imp->imp_next_ping;
931 cfs_mutex_unlock(&pinger_mutex);
935 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
936 timeout_cb_t cb, void *data,
937 cfs_list_t *obd_list)
942 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
943 enum timeout_event event)
948 int ptlrpc_pinger_add_import(struct obd_import *imp)
951 if (!cfs_list_empty(&imp->imp_pinger_chain))
954 CDEBUG(D_HA, "adding pingable import %s->%s\n",
955 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
956 ptlrpc_pinger_sending_on_import(imp);
958 cfs_mutex_lock(&pinger_mutex);
959 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
960 class_import_get(imp);
961 cfs_mutex_unlock(&pinger_mutex);
966 int ptlrpc_pinger_del_import(struct obd_import *imp)
969 if (cfs_list_empty(&imp->imp_pinger_chain))
972 cfs_mutex_lock(&pinger_mutex);
973 cfs_list_del_init(&imp->imp_pinger_chain);
974 CDEBUG(D_HA, "removing pingable import %s->%s\n",
975 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
976 class_import_put(imp);
977 cfs_mutex_unlock(&pinger_mutex);
981 void ptlrpc_pinger_wake_up()
984 /* XXX force pinger to run, if needed */
985 struct obd_import *imp;
987 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
988 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
989 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
990 #ifdef ENABLE_LIBLUSTRE_RECOVERY
991 if (imp->imp_state == LUSTRE_IMP_DISCON &&
992 !imp_is_deactive(imp))
994 /*XXX only recover for the initial connection */
995 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
996 imp->imp_state == LUSTRE_IMP_DISCON &&
997 !imp_is_deactive(imp))
999 ptlrpc_initiate_recovery(imp);
1000 else if (imp->imp_state != LUSTRE_IMP_FULL)
1001 CDEBUG(D_HA, "Refused to recover import %s->%s "
1002 "state %d, deactive %d\n",
1003 imp->imp_obd->obd_uuid.uuid,
1004 obd2cli_tgt(imp->imp_obd), imp->imp_state,
1005 imp_is_deactive(imp));
1010 #endif /* !__KERNEL__ */