1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/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_semaphore_t pinger_sem;
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, PSCOPE_OTHER);
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_down(&pinger_sem);
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_up(&pinger_sem);
159 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
164 static cfs_waitq_t suspend_timeouts_waitq;
167 cfs_time_t ptlrpc_suspend_wakeup_time(void)
169 return suspend_wakeup_time;
172 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
174 /*XXX: disabled for now, will be replaced by adaptive timeouts */
176 if (imp->imp_no_timeout)
178 imp->imp_no_timeout = 1;
179 cfs_atomic_inc(&suspend_timeouts);
180 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n",
181 cfs_atomic_read(&suspend_timeouts));
185 void ptlrpc_activate_timeouts(struct obd_import *imp)
187 /*XXX: disabled for now, will be replaced by adaptive timeouts */
189 if (!imp->imp_no_timeout)
191 imp->imp_no_timeout = 0;
192 LASSERT(cfs_atomic_read(&suspend_timeouts) > 0);
193 if (cfs_atomic_dec_and_test(&suspend_timeouts)) {
194 suspend_wakeup_time = cfs_time_current();
195 cfs_waitq_signal(&suspend_timeouts_waitq);
197 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n",
198 cfs_atomic_read(&suspend_timeouts));
202 int ptlrpc_check_suspend(void)
204 if (cfs_atomic_read(&suspend_timeouts))
209 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
211 struct l_wait_info lwi;
213 if (cfs_atomic_read(&suspend_timeouts)) {
214 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
215 cfs_atomic_read(&suspend_timeouts));
216 lwi = LWI_INTR(NULL, NULL);
217 l_wait_event(suspend_timeouts_waitq,
218 cfs_atomic_read(&suspend_timeouts) == 0, &lwi);
219 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
227 static void ptlrpc_pinger_process_import(struct obd_import *imp,
228 unsigned long this_ping)
232 cfs_spin_lock(&imp->imp_lock);
233 level = imp->imp_state;
234 force = imp->imp_force_verify;
236 imp->imp_force_verify = 0;
237 cfs_spin_unlock(&imp->imp_lock);
239 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
240 "level %s/%u force %u deactive %u pingable %u\n",
241 ptlrpc_import_state_name(level), level,
242 force, imp->imp_deactive, imp->imp_pingable);
244 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
245 this_ping) && force == 0)
248 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
249 /* wait at least a timeout before trying recovery again */
250 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
251 ptlrpc_initiate_recovery(imp);
252 } else if (level != LUSTRE_IMP_FULL ||
253 imp->imp_obd->obd_no_recov ||
254 imp_is_deactive(imp)) {
255 CDEBUG(D_HA, "not pinging %s (in recovery "
256 " or recovery disabled: %s)\n",
257 obd2cli_tgt(imp->imp_obd),
258 ptlrpc_import_state_name(level));
259 } else if (imp->imp_pingable || force) {
264 static int ptlrpc_pinger_main(void *arg)
266 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
267 struct ptlrpc_thread *thread = data->thread;
270 cfs_daemonize(data->name);
272 /* Record that the thread is running */
273 thread->t_flags = SVC_RUNNING;
274 cfs_waitq_signal(&thread->t_ctl_waitq);
276 /* And now, loop forever, pinging as needed. */
278 cfs_time_t this_ping = cfs_time_current();
279 struct l_wait_info lwi;
280 cfs_duration_t time_to_next_wake;
281 struct timeout_item *item;
284 cfs_mutex_down(&pinger_sem);
285 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
286 item->ti_cb(item, item->ti_cb_data);
288 cfs_list_for_each(iter, &pinger_imports) {
289 struct obd_import *imp =
290 cfs_list_entry(iter, struct obd_import,
293 ptlrpc_pinger_process_import(imp, this_ping);
294 /* obd_timeout might have changed */
295 if (imp->imp_pingable && imp->imp_next_ping &&
296 cfs_time_after(imp->imp_next_ping,
297 cfs_time_add(this_ping,
298 cfs_time_seconds(PING_INTERVAL))))
299 ptlrpc_update_next_ping(imp, 0);
301 cfs_mutex_up(&pinger_sem);
302 /* update memory usage info */
303 obd_update_maxusage();
305 /* Wait until the next ping time, or until we're stopped. */
306 time_to_next_wake = pinger_check_timeout(this_ping);
307 /* The ping sent by ptlrpc_send_rpc may get sent out
308 say .01 second after this.
309 ptlrpc_pinger_sending_on_import will then set the
310 next ping time to next_ping + .01 sec, which means
311 we will SKIP the next ping at next_ping, and the
312 ping will get sent 2 timeouts from now! Beware. */
313 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("CFS_TIME_T")\n",
315 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
316 if (time_to_next_wake > 0) {
317 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_wake, cfs_time_seconds(1)),
319 l_wait_event(thread->t_ctl_waitq,
320 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
322 if (thread->t_flags & SVC_STOPPING) {
323 thread->t_flags &= ~SVC_STOPPING;
326 } else if (thread->t_flags & SVC_EVENT) {
327 /* woken after adding import to reset timer */
328 thread->t_flags &= ~SVC_EVENT;
333 thread->t_flags = 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 };
345 struct ptlrpc_svc_data d;
347 #ifndef ENABLE_PINGER
352 if (pinger_thread != NULL)
355 OBD_ALLOC_PTR(pinger_thread);
356 if (pinger_thread == NULL)
358 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
359 cfs_waitq_init(&suspend_timeouts_waitq);
362 d.thread = pinger_thread;
364 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
365 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
366 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
368 CERROR("cannot start thread: %d\n", rc);
369 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
370 pinger_thread = NULL;
373 l_wait_event(pinger_thread->t_ctl_waitq,
374 pinger_thread->t_flags & SVC_RUNNING, &lwi);
379 int ptlrpc_pinger_remove_timeouts(void);
381 int ptlrpc_stop_pinger(void)
383 struct l_wait_info lwi = { 0 };
385 #ifndef ENABLE_PINGER
390 if (pinger_thread == NULL)
393 ptlrpc_pinger_remove_timeouts();
394 cfs_mutex_down(&pinger_sem);
395 pinger_thread->t_flags = SVC_STOPPING;
396 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
397 cfs_mutex_up(&pinger_sem);
399 l_wait_event(pinger_thread->t_ctl_waitq,
400 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
402 OBD_FREE_PTR(pinger_thread);
403 pinger_thread = NULL;
407 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
409 ptlrpc_update_next_ping(imp, 0);
412 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
414 ptlrpc_update_next_ping(imp, 1);
417 int ptlrpc_pinger_add_import(struct obd_import *imp)
420 if (!cfs_list_empty(&imp->imp_pinger_chain))
423 cfs_mutex_down(&pinger_sem);
424 CDEBUG(D_HA, "adding pingable import %s->%s\n",
425 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
426 /* if we add to pinger we want recovery on this import */
427 imp->imp_obd->obd_no_recov = 0;
428 ptlrpc_update_next_ping(imp, 0);
429 /* XXX sort, blah blah */
430 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
431 class_import_get(imp);
433 ptlrpc_pinger_wake_up();
434 cfs_mutex_up(&pinger_sem);
439 int ptlrpc_pinger_del_import(struct obd_import *imp)
442 if (cfs_list_empty(&imp->imp_pinger_chain))
445 cfs_mutex_down(&pinger_sem);
446 cfs_list_del_init(&imp->imp_pinger_chain);
447 CDEBUG(D_HA, "removing pingable import %s->%s\n",
448 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
449 /* if we remove from pinger we don't want recovery on this import */
450 imp->imp_obd->obd_no_recov = 1;
451 class_import_put(imp);
452 cfs_mutex_up(&pinger_sem);
457 * Register a timeout callback to the pinger list, and the callback will
458 * be called when timeout happens.
460 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
461 timeout_cb_t cb, void *data)
463 struct timeout_item *ti;
469 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
470 CFS_INIT_LIST_HEAD(&ti->ti_chain);
471 ti->ti_timeout = time;
472 ti->ti_event = event;
474 ti->ti_cb_data = data;
480 * Register timeout event on the the pinger thread.
481 * Note: the timeout list is an sorted list with increased timeout value.
483 static struct timeout_item*
484 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
485 timeout_cb_t cb, void *data)
487 struct timeout_item *item, *tmp;
489 LASSERT_SEM_LOCKED(&pinger_sem);
491 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
492 if (item->ti_event == event)
495 item = ptlrpc_new_timeout(time, event, cb, data);
497 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
498 if (tmp->ti_timeout < time) {
499 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
503 cfs_list_add(&item->ti_chain, &timeout_list);
509 /* Add a client_obd to the timeout event list, when timeout(@time)
510 * happens, the callback(@cb) will be called.
512 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
513 timeout_cb_t cb, void *data,
514 cfs_list_t *obd_list)
516 struct timeout_item *ti;
518 cfs_mutex_down(&pinger_sem);
519 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
521 cfs_mutex_up(&pinger_sem);
524 cfs_list_add(obd_list, &ti->ti_obd_list);
525 cfs_mutex_up(&pinger_sem);
529 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
530 enum timeout_event event)
532 struct timeout_item *ti = NULL, *item;
534 if (cfs_list_empty(obd_list))
536 cfs_mutex_down(&pinger_sem);
537 cfs_list_del_init(obd_list);
539 * If there are no obd attached to the timeout event
540 * list, remove this timeout event from the pinger
542 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
543 if (item->ti_event == event) {
548 LASSERTF(ti != NULL, "ti is NULL ! \n");
549 if (cfs_list_empty(&ti->ti_obd_list)) {
550 cfs_list_del(&ti->ti_chain);
553 cfs_mutex_up(&pinger_sem);
557 int ptlrpc_pinger_remove_timeouts(void)
559 struct timeout_item *item, *tmp;
561 cfs_mutex_down(&pinger_sem);
562 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
563 LASSERT(cfs_list_empty(&item->ti_obd_list));
564 cfs_list_del(&item->ti_chain);
567 cfs_mutex_up(&pinger_sem);
571 void ptlrpc_pinger_wake_up()
574 pinger_thread->t_flags |= SVC_EVENT;
575 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
579 /* Ping evictor thread */
581 #define PET_TERMINATE 2
583 static int pet_refcount = 0;
584 static int pet_state;
585 static cfs_waitq_t pet_waitq;
586 CFS_LIST_HEAD(pet_list);
587 static cfs_spinlock_t pet_lock = CFS_SPIN_LOCK_UNLOCKED;
589 int ping_evictor_wake(struct obd_export *exp)
591 struct obd_device *obd;
593 cfs_spin_lock(&pet_lock);
594 if (pet_state != PET_READY) {
595 /* eventually the new obd will call here again. */
596 cfs_spin_unlock(&pet_lock);
600 obd = class_exp2obd(exp);
601 if (cfs_list_empty(&obd->obd_evict_list)) {
602 class_incref(obd, "evictor", obd);
603 cfs_list_add(&obd->obd_evict_list, &pet_list);
605 cfs_spin_unlock(&pet_lock);
607 cfs_waitq_signal(&pet_waitq);
611 static int ping_evictor_main(void *arg)
613 struct obd_device *obd;
614 struct obd_export *exp;
615 struct l_wait_info lwi = { 0 };
619 cfs_daemonize_ctxt("ll_evictor");
621 CDEBUG(D_HA, "Starting Ping Evictor\n");
622 pet_state = PET_READY;
624 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
625 (pet_state == PET_TERMINATE), &lwi);
627 /* loop until all obd's will be removed */
628 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
631 /* we only get here if pet_exp != NULL, and the end of this
632 * loop is the only place which sets it NULL again, so lock
633 * is not strictly necessary. */
634 cfs_spin_lock(&pet_lock);
635 obd = cfs_list_entry(pet_list.next, struct obd_device,
637 cfs_spin_unlock(&pet_lock);
639 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
641 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
642 obd->obd_name, expire_time);
644 /* Exports can't be deleted out of the list while we hold
645 * the obd lock (class_unlink_export), which means we can't
646 * lose the last ref on the export. If they've already been
647 * removed from the list, we won't find them here. */
648 cfs_spin_lock(&obd->obd_dev_lock);
649 while (!cfs_list_empty(&obd->obd_exports_timed)) {
650 exp = cfs_list_entry(obd->obd_exports_timed.next,
652 exp_obd_chain_timed);
653 if (expire_time > exp->exp_last_request_time) {
654 class_export_get(exp);
655 cfs_spin_unlock(&obd->obd_dev_lock);
656 LCONSOLE_WARN("%s: haven't heard from client %s"
657 " (at %s) in %ld seconds. I think"
658 " it's dead, and I am evicting"
659 " it. exp %p, cur %ld expire %ld"
662 obd_uuid2str(&exp->exp_client_uuid),
663 obd_export_nid2str(exp),
664 (long)(cfs_time_current_sec() -
665 exp->exp_last_request_time),
666 exp, (long)cfs_time_current_sec(),
668 (long)exp->exp_last_request_time);
669 CDEBUG(D_HA, "Last request was at %ld\n",
670 exp->exp_last_request_time);
671 class_fail_export(exp);
672 class_export_put(exp);
673 cfs_spin_lock(&obd->obd_dev_lock);
675 /* List is sorted, so everyone below is ok */
679 cfs_spin_unlock(&obd->obd_dev_lock);
681 cfs_spin_lock(&pet_lock);
682 cfs_list_del_init(&obd->obd_evict_list);
683 cfs_spin_unlock(&pet_lock);
685 class_decref(obd, "evictor", obd);
687 CDEBUG(D_HA, "Exiting Ping Evictor\n");
692 void ping_evictor_start(void)
696 if (++pet_refcount > 1)
699 cfs_waitq_init(&pet_waitq);
701 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
704 CERROR("Cannot start ping evictor thread: %d\n", rc);
707 EXPORT_SYMBOL(ping_evictor_start);
709 void ping_evictor_stop(void)
711 if (--pet_refcount > 0)
714 pet_state = PET_TERMINATE;
715 cfs_waitq_signal(&pet_waitq);
717 EXPORT_SYMBOL(ping_evictor_stop);
718 #else /* !__KERNEL__ */
721 * the current implementation of pinger in liblustre is not optimized
725 static struct pinger_data {
727 cfs_time_t pd_this_ping; /* jiffies */
728 cfs_time_t pd_next_ping; /* jiffies */
729 struct ptlrpc_request_set *pd_set;
732 static int pinger_check_rpcs(void *arg)
734 cfs_time_t curtime = cfs_time_current();
735 struct ptlrpc_request *req;
736 struct ptlrpc_request_set *set;
738 struct obd_import *imp;
739 struct pinger_data *pd = &pinger_args;
742 /* prevent recursion */
743 if (pd->pd_recursion++) {
744 CDEBUG(D_HA, "pinger: recursion! quit\n");
750 /* have we reached ping point? */
751 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
756 /* if we have rpc_set already, continue processing it */
758 LASSERT(pd->pd_this_ping);
763 pd->pd_this_ping = curtime;
764 pd->pd_set = ptlrpc_prep_set();
765 if (pd->pd_set == NULL)
769 /* add rpcs into set */
770 cfs_mutex_down(&pinger_sem);
771 cfs_list_for_each(iter, &pinger_imports) {
772 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
774 int generation, level;
776 if (cfs_time_aftereq(pd->pd_this_ping,
777 imp->imp_next_ping - 5 * CFS_TICK)) {
779 cfs_spin_lock(&imp->imp_lock);
780 generation = imp->imp_generation;
781 level = imp->imp_state;
782 cfs_spin_unlock(&imp->imp_lock);
784 if (level != LUSTRE_IMP_FULL) {
786 "not pinging %s (in recovery)\n",
787 obd2cli_tgt(imp->imp_obd));
791 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
795 CERROR("OOM trying to ping %s->%s\n",
796 imp->imp_obd->obd_uuid.uuid,
797 obd2cli_tgt(imp->imp_obd));
801 req->rq_no_resend = 1;
802 ptlrpc_request_set_replen(req);
803 req->rq_send_state = LUSTRE_IMP_FULL;
804 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
805 req->rq_import_generation = generation;
806 ptlrpc_set_add_req(set, req);
808 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
809 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
810 imp->imp_next_ping, pd->pd_this_ping);
813 pd->pd_this_ping = curtime;
814 cfs_mutex_up(&pinger_sem);
816 /* Might be empty, that's OK. */
817 if (cfs_atomic_read(&set->set_remaining) == 0)
818 CDEBUG(D_RPCTRACE, "nothing to ping\n");
820 cfs_list_for_each(iter, &set->set_requests) {
821 struct ptlrpc_request *req =
822 cfs_list_entry(iter, struct ptlrpc_request,
824 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
825 req->rq_import->imp_obd->obd_uuid.uuid,
826 obd2cli_tgt(req->rq_import->imp_obd));
827 (void)ptl_send_rpc(req, 0);
831 rc = ptlrpc_check_set(NULL, set);
833 /* not finished, and we are not expired, simply return */
834 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
835 cfs_time_seconds(PING_INTERVAL)))) {
836 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
841 /* Expire all the requests that didn't come back. */
842 cfs_mutex_down(&pinger_sem);
843 cfs_list_for_each(iter, &set->set_requests) {
844 req = cfs_list_entry(iter, struct ptlrpc_request,
847 if (req->rq_phase == RQ_PHASE_COMPLETE)
850 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
853 /* This will also unregister reply. */
854 ptlrpc_expire_one_request(req, 0);
856 /* We're done with this req, let's finally move it to complete
857 * phase and take care of inflights. */
858 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
859 imp = req->rq_import;
860 cfs_spin_lock(&imp->imp_lock);
861 if (!cfs_list_empty(&req->rq_list)) {
862 cfs_list_del_init(&req->rq_list);
863 cfs_atomic_dec(&imp->imp_inflight);
865 cfs_spin_unlock(&imp->imp_lock);
866 cfs_atomic_dec(&set->set_remaining);
868 cfs_mutex_up(&pinger_sem);
870 ptlrpc_set_destroy(set);
874 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
875 cfs_time_seconds(PING_INTERVAL));
876 pd->pd_this_ping = 0; /* XXX for debug */
878 CDEBUG(D_INFO, "finished a round ping\n");
883 static void *pinger_callback = NULL;
884 #endif /* ENABLE_PINGER */
886 int ptlrpc_start_pinger(void)
889 memset(&pinger_args, 0, sizeof(pinger_args));
890 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
897 int ptlrpc_stop_pinger(void)
901 liblustre_deregister_wait_callback(pinger_callback);
906 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
909 cfs_mutex_down(&pinger_sem);
910 ptlrpc_update_next_ping(imp, 0);
911 if (pinger_args.pd_set == NULL &&
912 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
913 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
914 imp->imp_next_ping, cfs_time_current());
915 pinger_args.pd_next_ping = imp->imp_next_ping;
917 cfs_mutex_up(&pinger_sem);
921 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
924 cfs_mutex_down(&pinger_sem);
925 ptlrpc_update_next_ping(imp, 1);
926 if (pinger_args.pd_set == NULL &&
927 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
928 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
929 imp->imp_next_ping, cfs_time_current());
930 pinger_args.pd_next_ping = imp->imp_next_ping;
932 cfs_mutex_up(&pinger_sem);
936 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
937 timeout_cb_t cb, void *data,
938 cfs_list_t *obd_list)
943 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
944 enum timeout_event event)
949 int ptlrpc_pinger_add_import(struct obd_import *imp)
952 if (!cfs_list_empty(&imp->imp_pinger_chain))
955 CDEBUG(D_HA, "adding pingable import %s->%s\n",
956 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
957 ptlrpc_pinger_sending_on_import(imp);
959 cfs_mutex_down(&pinger_sem);
960 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
961 class_import_get(imp);
962 cfs_mutex_up(&pinger_sem);
967 int ptlrpc_pinger_del_import(struct obd_import *imp)
970 if (cfs_list_empty(&imp->imp_pinger_chain))
973 cfs_mutex_down(&pinger_sem);
974 cfs_list_del_init(&imp->imp_pinger_chain);
975 CDEBUG(D_HA, "removing pingable import %s->%s\n",
976 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
977 class_import_put(imp);
978 cfs_mutex_up(&pinger_sem);
982 void ptlrpc_pinger_wake_up()
985 /* XXX force pinger to run, if needed */
986 struct obd_import *imp;
988 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
989 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
990 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
991 #ifdef ENABLE_LIBLUSTRE_RECOVERY
992 if (imp->imp_state == LUSTRE_IMP_DISCON &&
993 !imp_is_deactive(imp))
995 /*XXX only recover for the initial connection */
996 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
997 imp->imp_state == LUSTRE_IMP_DISCON &&
998 !imp_is_deactive(imp))
1000 ptlrpc_initiate_recovery(imp);
1001 else if (imp->imp_state != LUSTRE_IMP_FULL)
1002 CDEBUG(D_HA, "Refused to recover import %s->%s "
1003 "state %d, deactive %d\n",
1004 imp->imp_obd->obd_uuid.uuid,
1005 obd2cli_tgt(imp->imp_obd), imp->imp_state,
1006 imp_is_deactive(imp));
1011 #endif /* !__KERNEL__ */