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 2008 Sun Microsystems, Inc. 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)
111 int time = 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);
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);
412 int ptlrpc_pinger_add_import(struct obd_import *imp)
415 if (!cfs_list_empty(&imp->imp_pinger_chain))
418 cfs_mutex_down(&pinger_sem);
419 CDEBUG(D_HA, "adding pingable import %s->%s\n",
420 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
421 /* if we add to pinger we want recovery on this import */
422 imp->imp_obd->obd_no_recov = 0;
423 ptlrpc_update_next_ping(imp);
424 /* XXX sort, blah blah */
425 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
426 class_import_get(imp);
428 ptlrpc_pinger_wake_up();
429 cfs_mutex_up(&pinger_sem);
434 int ptlrpc_pinger_del_import(struct obd_import *imp)
437 if (cfs_list_empty(&imp->imp_pinger_chain))
440 cfs_mutex_down(&pinger_sem);
441 cfs_list_del_init(&imp->imp_pinger_chain);
442 CDEBUG(D_HA, "removing pingable import %s->%s\n",
443 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
444 /* if we remove from pinger we don't want recovery on this import */
445 imp->imp_obd->obd_no_recov = 1;
446 class_import_put(imp);
447 cfs_mutex_up(&pinger_sem);
452 * Register a timeout callback to the pinger list, and the callback will
453 * be called when timeout happens.
455 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
456 timeout_cb_t cb, void *data)
458 struct timeout_item *ti;
464 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
465 CFS_INIT_LIST_HEAD(&ti->ti_chain);
466 ti->ti_timeout = time;
467 ti->ti_event = event;
469 ti->ti_cb_data = data;
475 * Register timeout event on the the pinger thread.
476 * Note: the timeout list is an sorted list with increased timeout value.
478 static struct timeout_item*
479 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
480 timeout_cb_t cb, void *data)
482 struct timeout_item *item, *tmp;
484 LASSERT_SEM_LOCKED(&pinger_sem);
486 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
487 if (item->ti_event == event)
490 item = ptlrpc_new_timeout(time, event, cb, data);
492 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
493 if (tmp->ti_timeout < time) {
494 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
498 cfs_list_add(&item->ti_chain, &timeout_list);
504 /* Add a client_obd to the timeout event list, when timeout(@time)
505 * happens, the callback(@cb) will be called.
507 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
508 timeout_cb_t cb, void *data,
509 cfs_list_t *obd_list)
511 struct timeout_item *ti;
513 cfs_mutex_down(&pinger_sem);
514 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
516 cfs_mutex_up(&pinger_sem);
519 cfs_list_add(obd_list, &ti->ti_obd_list);
520 cfs_mutex_up(&pinger_sem);
524 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
525 enum timeout_event event)
527 struct timeout_item *ti = NULL, *item;
529 if (cfs_list_empty(obd_list))
531 cfs_mutex_down(&pinger_sem);
532 cfs_list_del_init(obd_list);
534 * If there are no obd attached to the timeout event
535 * list, remove this timeout event from the pinger
537 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
538 if (item->ti_event == event) {
543 LASSERTF(ti != NULL, "ti is NULL ! \n");
544 if (cfs_list_empty(&ti->ti_obd_list)) {
545 cfs_list_del(&ti->ti_chain);
548 cfs_mutex_up(&pinger_sem);
552 int ptlrpc_pinger_remove_timeouts(void)
554 struct timeout_item *item, *tmp;
556 cfs_mutex_down(&pinger_sem);
557 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
558 LASSERT(cfs_list_empty(&item->ti_obd_list));
559 cfs_list_del(&item->ti_chain);
562 cfs_mutex_up(&pinger_sem);
566 void ptlrpc_pinger_wake_up()
569 pinger_thread->t_flags |= SVC_EVENT;
570 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
574 /* Ping evictor thread */
576 #define PET_TERMINATE 2
578 static int pet_refcount = 0;
579 static int pet_state;
580 static cfs_waitq_t pet_waitq;
581 CFS_LIST_HEAD(pet_list);
582 static cfs_spinlock_t pet_lock = CFS_SPIN_LOCK_UNLOCKED;
584 int ping_evictor_wake(struct obd_export *exp)
586 struct obd_device *obd;
588 cfs_spin_lock(&pet_lock);
589 if (pet_state != PET_READY) {
590 /* eventually the new obd will call here again. */
591 cfs_spin_unlock(&pet_lock);
595 obd = class_exp2obd(exp);
596 if (cfs_list_empty(&obd->obd_evict_list)) {
597 class_incref(obd, __FUNCTION__, cfs_current());
598 cfs_list_add(&obd->obd_evict_list, &pet_list);
600 cfs_spin_unlock(&pet_lock);
602 cfs_waitq_signal(&pet_waitq);
606 static int ping_evictor_main(void *arg)
608 struct obd_device *obd;
609 struct obd_export *exp;
610 struct l_wait_info lwi = { 0 };
614 cfs_daemonize_ctxt("ll_evictor");
616 CDEBUG(D_HA, "Starting Ping Evictor\n");
617 pet_state = PET_READY;
619 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
620 (pet_state == PET_TERMINATE), &lwi);
622 /* loop until all obd's will be removed */
623 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
626 /* we only get here if pet_exp != NULL, and the end of this
627 * loop is the only place which sets it NULL again, so lock
628 * is not strictly necessary. */
629 cfs_spin_lock(&pet_lock);
630 obd = cfs_list_entry(pet_list.next, struct obd_device,
632 cfs_spin_unlock(&pet_lock);
634 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
636 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
637 obd->obd_name, expire_time);
639 /* Exports can't be deleted out of the list while we hold
640 * the obd lock (class_unlink_export), which means we can't
641 * lose the last ref on the export. If they've already been
642 * removed from the list, we won't find them here. */
643 cfs_spin_lock(&obd->obd_dev_lock);
644 while (!cfs_list_empty(&obd->obd_exports_timed)) {
645 exp = cfs_list_entry(obd->obd_exports_timed.next,
647 exp_obd_chain_timed);
648 if (expire_time > exp->exp_last_request_time) {
649 class_export_get(exp);
650 cfs_spin_unlock(&obd->obd_dev_lock);
651 LCONSOLE_WARN("%s: haven't heard from client %s"
652 " (at %s) in %ld seconds. I think"
653 " it's dead, and I am evicting"
654 " it. exp %p, cur %ld expire %ld"
657 obd_uuid2str(&exp->exp_client_uuid),
658 obd_export_nid2str(exp),
659 (long)(cfs_time_current_sec() -
660 exp->exp_last_request_time),
661 exp, (long)cfs_time_current_sec(),
663 (long)exp->exp_last_request_time);
664 CDEBUG(D_HA, "Last request was at %ld\n",
665 exp->exp_last_request_time);
666 class_fail_export(exp);
667 class_export_put(exp);
668 cfs_spin_lock(&obd->obd_dev_lock);
670 /* List is sorted, so everyone below is ok */
674 cfs_spin_unlock(&obd->obd_dev_lock);
676 cfs_spin_lock(&pet_lock);
677 cfs_list_del_init(&obd->obd_evict_list);
678 cfs_spin_unlock(&pet_lock);
680 class_decref(obd, __FUNCTION__, cfs_current());
682 CDEBUG(D_HA, "Exiting Ping Evictor\n");
687 void ping_evictor_start(void)
691 if (++pet_refcount > 1)
694 cfs_waitq_init(&pet_waitq);
696 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
699 CERROR("Cannot start ping evictor thread: %d\n", rc);
702 EXPORT_SYMBOL(ping_evictor_start);
704 void ping_evictor_stop(void)
706 if (--pet_refcount > 0)
709 pet_state = PET_TERMINATE;
710 cfs_waitq_signal(&pet_waitq);
712 EXPORT_SYMBOL(ping_evictor_stop);
713 #else /* !__KERNEL__ */
716 * the current implementation of pinger in liblustre is not optimized
720 static struct pinger_data {
722 cfs_time_t pd_this_ping; /* jiffies */
723 cfs_time_t pd_next_ping; /* jiffies */
724 struct ptlrpc_request_set *pd_set;
727 static int pinger_check_rpcs(void *arg)
729 cfs_time_t curtime = cfs_time_current();
730 struct ptlrpc_request *req;
731 struct ptlrpc_request_set *set;
733 struct obd_import *imp;
734 struct pinger_data *pd = &pinger_args;
737 /* prevent recursion */
738 if (pd->pd_recursion++) {
739 CDEBUG(D_HA, "pinger: recursion! quit\n");
745 /* have we reached ping point? */
746 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
751 /* if we have rpc_set already, continue processing it */
753 LASSERT(pd->pd_this_ping);
758 pd->pd_this_ping = curtime;
759 pd->pd_set = ptlrpc_prep_set();
760 if (pd->pd_set == NULL)
764 /* add rpcs into set */
765 cfs_mutex_down(&pinger_sem);
766 cfs_list_for_each(iter, &pinger_imports) {
767 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
769 int generation, level;
771 if (cfs_time_aftereq(pd->pd_this_ping,
772 imp->imp_next_ping - 5 * CFS_TICK)) {
774 cfs_spin_lock(&imp->imp_lock);
775 generation = imp->imp_generation;
776 level = imp->imp_state;
777 cfs_spin_unlock(&imp->imp_lock);
779 if (level != LUSTRE_IMP_FULL) {
781 "not pinging %s (in recovery)\n",
782 obd2cli_tgt(imp->imp_obd));
786 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
790 CERROR("OOM trying to ping %s->%s\n",
791 imp->imp_obd->obd_uuid.uuid,
792 obd2cli_tgt(imp->imp_obd));
796 req->rq_no_resend = 1;
797 ptlrpc_request_set_replen(req);
798 req->rq_send_state = LUSTRE_IMP_FULL;
799 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
800 req->rq_import_generation = generation;
801 ptlrpc_set_add_req(set, req);
803 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
804 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
805 imp->imp_next_ping, pd->pd_this_ping);
808 pd->pd_this_ping = curtime;
809 cfs_mutex_up(&pinger_sem);
811 /* Might be empty, that's OK. */
812 if (cfs_atomic_read(&set->set_remaining) == 0)
813 CDEBUG(D_RPCTRACE, "nothing to ping\n");
815 cfs_list_for_each(iter, &set->set_requests) {
816 struct ptlrpc_request *req =
817 cfs_list_entry(iter, struct ptlrpc_request,
819 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
820 req->rq_import->imp_obd->obd_uuid.uuid,
821 obd2cli_tgt(req->rq_import->imp_obd));
822 (void)ptl_send_rpc(req, 0);
826 rc = ptlrpc_check_set(NULL, set);
828 /* not finished, and we are not expired, simply return */
829 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
830 cfs_time_seconds(PING_INTERVAL)))) {
831 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
836 /* Expire all the requests that didn't come back. */
837 cfs_mutex_down(&pinger_sem);
838 cfs_list_for_each(iter, &set->set_requests) {
839 req = cfs_list_entry(iter, struct ptlrpc_request,
842 if (req->rq_phase == RQ_PHASE_COMPLETE)
845 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
848 /* This will also unregister reply. */
849 ptlrpc_expire_one_request(req, 0);
851 /* We're done with this req, let's finally move it to complete
852 * phase and take care of inflights. */
853 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
854 imp = req->rq_import;
855 cfs_spin_lock(&imp->imp_lock);
856 if (!cfs_list_empty(&req->rq_list)) {
857 cfs_list_del_init(&req->rq_list);
858 cfs_atomic_dec(&imp->imp_inflight);
860 cfs_spin_unlock(&imp->imp_lock);
861 cfs_atomic_dec(&set->set_remaining);
863 cfs_mutex_up(&pinger_sem);
865 ptlrpc_set_destroy(set);
869 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
870 cfs_time_seconds(PING_INTERVAL));
871 pd->pd_this_ping = 0; /* XXX for debug */
873 CDEBUG(D_INFO, "finished a round ping\n");
878 static void *pinger_callback = NULL;
879 #endif /* ENABLE_PINGER */
881 int ptlrpc_start_pinger(void)
884 memset(&pinger_args, 0, sizeof(pinger_args));
885 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
892 int ptlrpc_stop_pinger(void)
896 liblustre_deregister_wait_callback(pinger_callback);
901 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
904 cfs_mutex_down(&pinger_sem);
905 ptlrpc_update_next_ping(imp);
906 if (pinger_args.pd_set == NULL &&
907 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
908 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
909 imp->imp_next_ping, cfs_time_current());
910 pinger_args.pd_next_ping = imp->imp_next_ping;
912 cfs_mutex_up(&pinger_sem);
916 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
917 timeout_cb_t cb, void *data,
918 cfs_list_t *obd_list)
923 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
924 enum timeout_event event)
929 int ptlrpc_pinger_add_import(struct obd_import *imp)
932 if (!cfs_list_empty(&imp->imp_pinger_chain))
935 CDEBUG(D_HA, "adding pingable import %s->%s\n",
936 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
937 ptlrpc_pinger_sending_on_import(imp);
939 cfs_mutex_down(&pinger_sem);
940 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
941 class_import_get(imp);
942 cfs_mutex_up(&pinger_sem);
947 int ptlrpc_pinger_del_import(struct obd_import *imp)
950 if (cfs_list_empty(&imp->imp_pinger_chain))
953 cfs_mutex_down(&pinger_sem);
954 cfs_list_del_init(&imp->imp_pinger_chain);
955 CDEBUG(D_HA, "removing pingable import %s->%s\n",
956 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
957 class_import_put(imp);
958 cfs_mutex_up(&pinger_sem);
962 void ptlrpc_pinger_wake_up()
965 /* XXX force pinger to run, if needed */
966 struct obd_import *imp;
968 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
969 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
970 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
971 #ifdef ENABLE_LIBLUSTRE_RECOVERY
972 if (imp->imp_state == LUSTRE_IMP_DISCON &&
973 !imp_is_deactive(imp))
975 /*XXX only recover for the initial connection */
976 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
977 imp->imp_state == LUSTRE_IMP_DISCON &&
978 !imp_is_deactive(imp))
980 ptlrpc_initiate_recovery(imp);
981 else if (imp->imp_state != LUSTRE_IMP_FULL)
982 CDEBUG(D_HA, "Refused to recover import %s->%s "
983 "state %d, deactive %d\n",
984 imp->imp_obd->obd_uuid.uuid,
985 obd2cli_tgt(imp->imp_obd), imp->imp_state,
986 imp_is_deactive(imp));
991 #endif /* !__KERNEL__ */