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.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ptlrpc/pinger.c
40 * Portal-RPC reconnection and replay operations, for use in recovery.
44 #include <liblustre.h>
46 #define DEBUG_SUBSYSTEM S_RPC
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include "ptlrpc_internal.h"
53 cfs_semaphore_t pinger_sem;
54 static CFS_LIST_HEAD(pinger_imports);
55 static cfs_list_t timeout_list = CFS_LIST_HEAD_INIT(timeout_list);
56 struct ptlrpc_request *
57 ptlrpc_prep_ping(struct obd_import *imp)
59 struct ptlrpc_request *req;
61 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
62 LUSTRE_OBD_VERSION, OBD_PING);
64 ptlrpc_request_set_replen(req);
65 req->rq_no_resend = req->rq_no_delay = 1;
70 int ptlrpc_obd_ping(struct obd_device *obd)
73 struct ptlrpc_request *req;
76 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
80 req->rq_send_state = LUSTRE_IMP_FULL;
82 rc = ptlrpc_queue_wait(req);
84 ptlrpc_req_finished(req);
88 EXPORT_SYMBOL(ptlrpc_obd_ping);
90 int ptlrpc_ping(struct obd_import *imp)
92 struct ptlrpc_request *req;
95 req = ptlrpc_prep_ping(imp);
97 CERROR("OOM trying to ping %s->%s\n",
98 imp->imp_obd->obd_uuid.uuid,
99 obd2cli_tgt(imp->imp_obd));
103 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
104 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
105 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
110 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
113 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
114 if (imp->imp_state == LUSTRE_IMP_DISCON) {
115 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
117 at_get(&imp->imp_at.iat_net_latency));
118 time = min(time, dtime);
120 imp->imp_next_ping = cfs_time_shift(time);
121 #endif /* ENABLE_PINGER */
124 void ptlrpc_ping_import_soon(struct obd_import *imp)
126 imp->imp_next_ping = cfs_time_current();
129 static inline int imp_is_deactive(struct obd_import *imp)
131 return (imp->imp_deactive ||
132 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
135 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
137 if (imp->imp_server_timeout)
138 return cfs_time_shift(obd_timeout / 2);
140 return cfs_time_shift(obd_timeout);
143 static cfs_atomic_t suspend_timeouts = CFS_ATOMIC_INIT(0);
144 static cfs_time_t suspend_wakeup_time = 0;
146 cfs_duration_t pinger_check_timeout(cfs_time_t time)
148 struct timeout_item *item;
149 cfs_time_t timeout = PING_INTERVAL;
151 /* The timeout list is a increase order sorted list */
152 cfs_mutex_down(&pinger_sem);
153 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
154 int ti_timeout = item->ti_timeout;
155 if (timeout > ti_timeout)
156 timeout = ti_timeout;
159 cfs_mutex_up(&pinger_sem);
161 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
165 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 for a while before trying recovery again */
250 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
251 if (!imp->imp_no_pinger_recover)
252 ptlrpc_initiate_recovery(imp);
253 } else if (level != LUSTRE_IMP_FULL ||
254 imp->imp_obd->obd_no_recov ||
255 imp_is_deactive(imp)) {
256 CDEBUG(D_HA, "not pinging %s (in recovery "
257 " or recovery disabled: %s)\n",
258 obd2cli_tgt(imp->imp_obd),
259 ptlrpc_import_state_name(level));
260 } else if (imp->imp_pingable || force) {
265 static int ptlrpc_pinger_main(void *arg)
267 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
268 struct ptlrpc_thread *thread = data->thread;
271 cfs_daemonize(data->name);
273 /* Record that the thread is running */
274 thread_set_flags(thread, SVC_RUNNING);
275 cfs_waitq_signal(&thread->t_ctl_waitq);
277 /* And now, loop forever, pinging as needed. */
279 cfs_time_t this_ping = cfs_time_current();
280 struct l_wait_info lwi;
281 cfs_duration_t time_to_next_wake;
282 struct timeout_item *item;
285 cfs_mutex_down(&pinger_sem);
286 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
287 item->ti_cb(item, item->ti_cb_data);
289 cfs_list_for_each(iter, &pinger_imports) {
290 struct obd_import *imp =
291 cfs_list_entry(iter, struct obd_import,
294 ptlrpc_pinger_process_import(imp, this_ping);
295 /* obd_timeout might have changed */
296 if (imp->imp_pingable && imp->imp_next_ping &&
297 cfs_time_after(imp->imp_next_ping,
298 cfs_time_add(this_ping,
299 cfs_time_seconds(PING_INTERVAL))))
300 ptlrpc_update_next_ping(imp, 0);
302 cfs_mutex_up(&pinger_sem);
303 /* update memory usage info */
304 obd_update_maxusage();
306 /* Wait until the next ping time, or until we're stopped. */
307 time_to_next_wake = pinger_check_timeout(this_ping);
308 /* The ping sent by ptlrpc_send_rpc may get sent out
309 say .01 second after this.
310 ptlrpc_pinger_sending_on_import will then set the
311 next ping time to next_ping + .01 sec, which means
312 we will SKIP the next ping at next_ping, and the
313 ping will get sent 2 timeouts from now! Beware. */
314 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
315 CFS_TIME_T")\n", time_to_next_wake,
316 cfs_time_add(this_ping,cfs_time_seconds(PING_INTERVAL)));
317 if (time_to_next_wake > 0) {
318 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
320 cfs_time_seconds(1)),
322 l_wait_event(thread->t_ctl_waitq,
323 thread_is_stopping(thread) ||
324 thread_is_event(thread),
326 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
330 /* woken after adding import to reset timer */
331 thread_test_and_clear_flags(thread, SVC_EVENT);
336 thread_set_flags(thread, SVC_STOPPED);
337 cfs_waitq_signal(&thread->t_ctl_waitq);
339 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
343 static struct ptlrpc_thread *pinger_thread = NULL;
345 int ptlrpc_start_pinger(void)
347 struct l_wait_info lwi = { 0 };
348 struct ptlrpc_svc_data d;
350 #ifndef ENABLE_PINGER
355 if (pinger_thread != NULL)
358 OBD_ALLOC_PTR(pinger_thread);
359 if (pinger_thread == NULL)
361 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
362 cfs_waitq_init(&suspend_timeouts_waitq);
365 d.thread = pinger_thread;
367 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
368 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
369 rc = cfs_create_thread(ptlrpc_pinger_main, &d, CFS_DAEMON_FLAGS);
371 CERROR("cannot start thread: %d\n", rc);
372 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
373 pinger_thread = NULL;
376 l_wait_event(pinger_thread->t_ctl_waitq,
377 thread_is_running(pinger_thread), &lwi);
382 int ptlrpc_pinger_remove_timeouts(void);
384 int ptlrpc_stop_pinger(void)
386 struct l_wait_info lwi = { 0 };
388 #ifndef ENABLE_PINGER
393 if (pinger_thread == NULL)
396 ptlrpc_pinger_remove_timeouts();
397 cfs_mutex_down(&pinger_sem);
398 thread_set_flags(pinger_thread, SVC_STOPPING);
399 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
400 cfs_mutex_up(&pinger_sem);
402 l_wait_event(pinger_thread->t_ctl_waitq,
403 thread_is_stopped(pinger_thread), &lwi);
405 OBD_FREE_PTR(pinger_thread);
406 pinger_thread = NULL;
410 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
412 ptlrpc_update_next_ping(imp, 0);
415 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
417 ptlrpc_update_next_ping(imp, 1);
420 int ptlrpc_pinger_add_import(struct obd_import *imp)
423 if (!cfs_list_empty(&imp->imp_pinger_chain))
426 cfs_mutex_down(&pinger_sem);
427 CDEBUG(D_HA, "adding pingable import %s->%s\n",
428 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
429 /* if we add to pinger we want recovery on this import */
430 imp->imp_obd->obd_no_recov = 0;
431 ptlrpc_update_next_ping(imp, 0);
432 /* XXX sort, blah blah */
433 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
434 class_import_get(imp);
436 ptlrpc_pinger_wake_up();
437 cfs_mutex_up(&pinger_sem);
442 int ptlrpc_pinger_del_import(struct obd_import *imp)
445 if (cfs_list_empty(&imp->imp_pinger_chain))
448 cfs_mutex_down(&pinger_sem);
449 cfs_list_del_init(&imp->imp_pinger_chain);
450 CDEBUG(D_HA, "removing pingable import %s->%s\n",
451 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
452 /* if we remove from pinger we don't want recovery on this import */
453 imp->imp_obd->obd_no_recov = 1;
454 class_import_put(imp);
455 cfs_mutex_up(&pinger_sem);
460 * Register a timeout callback to the pinger list, and the callback will
461 * be called when timeout happens.
463 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
464 timeout_cb_t cb, void *data)
466 struct timeout_item *ti;
472 CFS_INIT_LIST_HEAD(&ti->ti_obd_list);
473 CFS_INIT_LIST_HEAD(&ti->ti_chain);
474 ti->ti_timeout = time;
475 ti->ti_event = event;
477 ti->ti_cb_data = data;
483 * Register timeout event on the the pinger thread.
484 * Note: the timeout list is an sorted list with increased timeout value.
486 static struct timeout_item*
487 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
488 timeout_cb_t cb, void *data)
490 struct timeout_item *item, *tmp;
492 LASSERT_SEM_LOCKED(&pinger_sem);
494 cfs_list_for_each_entry(item, &timeout_list, ti_chain)
495 if (item->ti_event == event)
498 item = ptlrpc_new_timeout(time, event, cb, data);
500 cfs_list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
501 if (tmp->ti_timeout < time) {
502 cfs_list_add(&item->ti_chain, &tmp->ti_chain);
506 cfs_list_add(&item->ti_chain, &timeout_list);
512 /* Add a client_obd to the timeout event list, when timeout(@time)
513 * happens, the callback(@cb) will be called.
515 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
516 timeout_cb_t cb, void *data,
517 cfs_list_t *obd_list)
519 struct timeout_item *ti;
521 cfs_mutex_down(&pinger_sem);
522 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
524 cfs_mutex_up(&pinger_sem);
527 cfs_list_add(obd_list, &ti->ti_obd_list);
528 cfs_mutex_up(&pinger_sem);
532 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
533 enum timeout_event event)
535 struct timeout_item *ti = NULL, *item;
537 if (cfs_list_empty(obd_list))
539 cfs_mutex_down(&pinger_sem);
540 cfs_list_del_init(obd_list);
542 * If there are no obd attached to the timeout event
543 * list, remove this timeout event from the pinger
545 cfs_list_for_each_entry(item, &timeout_list, ti_chain) {
546 if (item->ti_event == event) {
551 LASSERTF(ti != NULL, "ti is NULL ! \n");
552 if (cfs_list_empty(&ti->ti_obd_list)) {
553 cfs_list_del(&ti->ti_chain);
556 cfs_mutex_up(&pinger_sem);
560 int ptlrpc_pinger_remove_timeouts(void)
562 struct timeout_item *item, *tmp;
564 cfs_mutex_down(&pinger_sem);
565 cfs_list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
566 LASSERT(cfs_list_empty(&item->ti_obd_list));
567 cfs_list_del(&item->ti_chain);
570 cfs_mutex_up(&pinger_sem);
574 void ptlrpc_pinger_wake_up()
577 thread_add_flags(pinger_thread, SVC_EVENT);
578 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
582 /* Ping evictor thread */
584 #define PET_TERMINATE 2
586 static int pet_refcount = 0;
587 static int pet_state;
588 static cfs_waitq_t pet_waitq;
589 CFS_LIST_HEAD(pet_list);
590 static cfs_spinlock_t pet_lock = CFS_SPIN_LOCK_UNLOCKED;
592 int ping_evictor_wake(struct obd_export *exp)
594 struct obd_device *obd;
596 cfs_spin_lock(&pet_lock);
597 if (pet_state != PET_READY) {
598 /* eventually the new obd will call here again. */
599 cfs_spin_unlock(&pet_lock);
603 obd = class_exp2obd(exp);
604 if (cfs_list_empty(&obd->obd_evict_list)) {
605 class_incref(obd, "evictor", obd);
606 cfs_list_add(&obd->obd_evict_list, &pet_list);
608 cfs_spin_unlock(&pet_lock);
610 cfs_waitq_signal(&pet_waitq);
614 static int ping_evictor_main(void *arg)
616 struct obd_device *obd;
617 struct obd_export *exp;
618 struct l_wait_info lwi = { 0 };
622 cfs_daemonize_ctxt("ll_evictor");
624 CDEBUG(D_HA, "Starting Ping Evictor\n");
625 pet_state = PET_READY;
627 l_wait_event(pet_waitq, (!cfs_list_empty(&pet_list)) ||
628 (pet_state == PET_TERMINATE), &lwi);
630 /* loop until all obd's will be removed */
631 if ((pet_state == PET_TERMINATE) && cfs_list_empty(&pet_list))
634 /* we only get here if pet_exp != NULL, and the end of this
635 * loop is the only place which sets it NULL again, so lock
636 * is not strictly necessary. */
637 cfs_spin_lock(&pet_lock);
638 obd = cfs_list_entry(pet_list.next, struct obd_device,
640 cfs_spin_unlock(&pet_lock);
642 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
644 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
645 obd->obd_name, expire_time);
647 /* Exports can't be deleted out of the list while we hold
648 * the obd lock (class_unlink_export), which means we can't
649 * lose the last ref on the export. If they've already been
650 * removed from the list, we won't find them here. */
651 cfs_spin_lock(&obd->obd_dev_lock);
652 while (!cfs_list_empty(&obd->obd_exports_timed)) {
653 exp = cfs_list_entry(obd->obd_exports_timed.next,
655 exp_obd_chain_timed);
656 if (expire_time > exp->exp_last_request_time) {
657 class_export_get(exp);
658 cfs_spin_unlock(&obd->obd_dev_lock);
659 LCONSOLE_WARN("%s: haven't heard from client %s"
660 " (at %s) in %ld seconds. I think"
661 " it's dead, and I am evicting"
662 " it. exp %p, cur %ld expire %ld"
665 obd_uuid2str(&exp->exp_client_uuid),
666 obd_export_nid2str(exp),
667 (long)(cfs_time_current_sec() -
668 exp->exp_last_request_time),
669 exp, (long)cfs_time_current_sec(),
671 (long)exp->exp_last_request_time);
672 CDEBUG(D_HA, "Last request was at %ld\n",
673 exp->exp_last_request_time);
674 class_fail_export(exp);
675 class_export_put(exp);
676 cfs_spin_lock(&obd->obd_dev_lock);
678 /* List is sorted, so everyone below is ok */
682 cfs_spin_unlock(&obd->obd_dev_lock);
684 cfs_spin_lock(&pet_lock);
685 cfs_list_del_init(&obd->obd_evict_list);
686 cfs_spin_unlock(&pet_lock);
688 class_decref(obd, "evictor", obd);
690 CDEBUG(D_HA, "Exiting Ping Evictor\n");
695 void ping_evictor_start(void)
699 if (++pet_refcount > 1)
702 cfs_waitq_init(&pet_waitq);
704 rc = cfs_create_thread(ping_evictor_main, NULL, CFS_DAEMON_FLAGS);
707 CERROR("Cannot start ping evictor thread: %d\n", rc);
710 EXPORT_SYMBOL(ping_evictor_start);
712 void ping_evictor_stop(void)
714 if (--pet_refcount > 0)
717 pet_state = PET_TERMINATE;
718 cfs_waitq_signal(&pet_waitq);
720 EXPORT_SYMBOL(ping_evictor_stop);
721 #else /* !__KERNEL__ */
724 * the current implementation of pinger in liblustre is not optimized
728 static struct pinger_data {
730 cfs_time_t pd_this_ping; /* jiffies */
731 cfs_time_t pd_next_ping; /* jiffies */
732 struct ptlrpc_request_set *pd_set;
735 static int pinger_check_rpcs(void *arg)
737 cfs_time_t curtime = cfs_time_current();
738 struct ptlrpc_request *req;
739 struct ptlrpc_request_set *set;
741 struct obd_import *imp;
742 struct pinger_data *pd = &pinger_args;
745 /* prevent recursion */
746 if (pd->pd_recursion++) {
747 CDEBUG(D_HA, "pinger: recursion! quit\n");
753 /* have we reached ping point? */
754 if (!pd->pd_set && cfs_time_before(curtime, pd->pd_next_ping)) {
759 /* if we have rpc_set already, continue processing it */
761 LASSERT(pd->pd_this_ping);
766 pd->pd_this_ping = curtime;
767 pd->pd_set = ptlrpc_prep_set();
768 if (pd->pd_set == NULL)
772 /* add rpcs into set */
773 cfs_mutex_down(&pinger_sem);
774 cfs_list_for_each(iter, &pinger_imports) {
775 struct obd_import *imp = cfs_list_entry(iter, struct obd_import,
777 int generation, level;
779 if (cfs_time_aftereq(pd->pd_this_ping,
780 imp->imp_next_ping - 5 * CFS_TICK)) {
782 cfs_spin_lock(&imp->imp_lock);
783 generation = imp->imp_generation;
784 level = imp->imp_state;
785 cfs_spin_unlock(&imp->imp_lock);
787 if (level != LUSTRE_IMP_FULL) {
789 "not pinging %s (in recovery)\n",
790 obd2cli_tgt(imp->imp_obd));
794 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
798 CERROR("OOM trying to ping %s->%s\n",
799 imp->imp_obd->obd_uuid.uuid,
800 obd2cli_tgt(imp->imp_obd));
804 req->rq_no_resend = 1;
805 ptlrpc_request_set_replen(req);
806 req->rq_send_state = LUSTRE_IMP_FULL;
807 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
808 req->rq_import_generation = generation;
809 ptlrpc_set_add_req(set, req);
811 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
812 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
813 imp->imp_next_ping, pd->pd_this_ping);
816 pd->pd_this_ping = curtime;
817 cfs_mutex_up(&pinger_sem);
819 /* Might be empty, that's OK. */
820 if (cfs_atomic_read(&set->set_remaining) == 0)
821 CDEBUG(D_RPCTRACE, "nothing to ping\n");
823 cfs_list_for_each(iter, &set->set_requests) {
824 struct ptlrpc_request *req =
825 cfs_list_entry(iter, struct ptlrpc_request,
827 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
828 req->rq_import->imp_obd->obd_uuid.uuid,
829 obd2cli_tgt(req->rq_import->imp_obd));
830 (void)ptl_send_rpc(req, 0);
834 rc = ptlrpc_check_set(NULL, set);
836 /* not finished, and we are not expired, simply return */
837 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
838 cfs_time_seconds(PING_INTERVAL)))) {
839 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
844 /* Expire all the requests that didn't come back. */
845 cfs_mutex_down(&pinger_sem);
846 cfs_list_for_each(iter, &set->set_requests) {
847 req = cfs_list_entry(iter, struct ptlrpc_request,
850 if (req->rq_phase == RQ_PHASE_COMPLETE)
853 CDEBUG(D_RPCTRACE, "Pinger initiate expire request(%p)\n",
856 /* This will also unregister reply. */
857 ptlrpc_expire_one_request(req, 0);
859 /* We're done with this req, let's finally move it to complete
860 * phase and take care of inflights. */
861 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
862 imp = req->rq_import;
863 cfs_spin_lock(&imp->imp_lock);
864 if (!cfs_list_empty(&req->rq_list)) {
865 cfs_list_del_init(&req->rq_list);
866 cfs_atomic_dec(&imp->imp_inflight);
868 cfs_spin_unlock(&imp->imp_lock);
869 cfs_atomic_dec(&set->set_remaining);
871 cfs_mutex_up(&pinger_sem);
873 ptlrpc_set_destroy(set);
877 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
878 cfs_time_seconds(PING_INTERVAL));
879 pd->pd_this_ping = 0; /* XXX for debug */
881 CDEBUG(D_INFO, "finished a round ping\n");
886 static void *pinger_callback = NULL;
887 #endif /* ENABLE_PINGER */
889 int ptlrpc_start_pinger(void)
892 memset(&pinger_args, 0, sizeof(pinger_args));
893 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
900 int ptlrpc_stop_pinger(void)
904 liblustre_deregister_wait_callback(pinger_callback);
909 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
912 cfs_mutex_down(&pinger_sem);
913 ptlrpc_update_next_ping(imp, 0);
914 if (pinger_args.pd_set == NULL &&
915 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
916 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
917 imp->imp_next_ping, cfs_time_current());
918 pinger_args.pd_next_ping = imp->imp_next_ping;
920 cfs_mutex_up(&pinger_sem);
924 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
927 cfs_mutex_down(&pinger_sem);
928 ptlrpc_update_next_ping(imp, 1);
929 if (pinger_args.pd_set == NULL &&
930 cfs_time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
931 CDEBUG(D_HA,"set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
932 imp->imp_next_ping, cfs_time_current());
933 pinger_args.pd_next_ping = imp->imp_next_ping;
935 cfs_mutex_up(&pinger_sem);
939 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
940 timeout_cb_t cb, void *data,
941 cfs_list_t *obd_list)
946 int ptlrpc_del_timeout_client(cfs_list_t *obd_list,
947 enum timeout_event event)
952 int ptlrpc_pinger_add_import(struct obd_import *imp)
955 if (!cfs_list_empty(&imp->imp_pinger_chain))
958 CDEBUG(D_HA, "adding pingable import %s->%s\n",
959 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
960 ptlrpc_pinger_sending_on_import(imp);
962 cfs_mutex_down(&pinger_sem);
963 cfs_list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
964 class_import_get(imp);
965 cfs_mutex_up(&pinger_sem);
970 int ptlrpc_pinger_del_import(struct obd_import *imp)
973 if (cfs_list_empty(&imp->imp_pinger_chain))
976 cfs_mutex_down(&pinger_sem);
977 cfs_list_del_init(&imp->imp_pinger_chain);
978 CDEBUG(D_HA, "removing pingable import %s->%s\n",
979 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
980 class_import_put(imp);
981 cfs_mutex_up(&pinger_sem);
985 void ptlrpc_pinger_wake_up()
988 /* XXX force pinger to run, if needed */
989 struct obd_import *imp;
991 cfs_list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
992 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
993 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
994 #ifdef ENABLE_LIBLUSTRE_RECOVERY
995 if (imp->imp_state == LUSTRE_IMP_DISCON &&
996 !imp_is_deactive(imp))
998 /*XXX only recover for the initial connection */
999 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
1000 imp->imp_state == LUSTRE_IMP_DISCON &&
1001 !imp_is_deactive(imp))
1003 ptlrpc_initiate_recovery(imp);
1004 else if (imp->imp_state != LUSTRE_IMP_FULL)
1005 CDEBUG(D_HA, "Refused to recover import %s->%s "
1006 "state %d, deactive %d\n",
1007 imp->imp_obd->obd_uuid.uuid,
1008 obd2cli_tgt(imp->imp_obd), imp->imp_state,
1009 imp_is_deactive(imp));
1014 #endif /* !__KERNEL__ */