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 struct semaphore pinger_sem;
52 static CFS_LIST_HEAD(pinger_imports);
54 int ptlrpc_ping(struct obd_import *imp)
56 struct ptlrpc_request *req;
59 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
62 CERROR("OOM trying to ping %s->%s\n",
63 imp->imp_obd->obd_uuid.uuid,
64 obd2cli_tgt(imp->imp_obd));
68 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
69 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
70 req->rq_no_resend = req->rq_no_delay = 1;
71 ptlrpc_request_set_replen(req);
77 void ptlrpc_update_next_ping(struct obd_import *imp)
80 int time = PING_INTERVAL;
81 if (imp->imp_state == LUSTRE_IMP_DISCON) {
82 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
84 at_get(&imp->imp_at.iat_net_latency));
85 time = min(time, dtime);
87 imp->imp_next_ping = cfs_time_shift(time);
88 #endif /* ENABLE_PINGER */
91 void ptlrpc_ping_import_soon(struct obd_import *imp)
93 imp->imp_next_ping = cfs_time_current();
96 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
98 if (imp->imp_server_timeout)
99 return cfs_time_shift(obd_timeout / 2);
101 return cfs_time_shift(obd_timeout);
104 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
105 static cfs_time_t suspend_wakeup_time = 0;
108 static wait_queue_head_t suspend_timeouts_waitq;
111 cfs_time_t ptlrpc_suspend_wakeup_time(void)
113 return suspend_wakeup_time;
116 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
118 /*XXX: disabled for now, will be replaced by adaptive timeouts */
120 if (imp->imp_no_timeout)
122 imp->imp_no_timeout = 1;
123 atomic_inc(&suspend_timeouts);
124 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
128 void ptlrpc_activate_timeouts(struct obd_import *imp)
130 /*XXX: disabled for now, will be replaced by adaptive timeouts */
132 if (!imp->imp_no_timeout)
134 imp->imp_no_timeout = 0;
135 LASSERT(atomic_read(&suspend_timeouts) > 0);
136 if (atomic_dec_and_test(&suspend_timeouts)) {
137 suspend_wakeup_time = cfs_time_current();
138 wake_up(&suspend_timeouts_waitq);
140 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
144 int ptlrpc_check_suspend(void)
146 if (atomic_read(&suspend_timeouts))
151 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
153 struct l_wait_info lwi;
155 if (atomic_read(&suspend_timeouts)) {
156 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
157 atomic_read(&suspend_timeouts));
158 lwi = LWI_INTR(NULL, NULL);
159 l_wait_event(suspend_timeouts_waitq,
160 atomic_read(&suspend_timeouts) == 0, &lwi);
161 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
169 static void ptlrpc_pinger_process_import(struct obd_import *imp,
170 unsigned long this_ping)
174 spin_lock(&imp->imp_lock);
175 level = imp->imp_state;
176 force = imp->imp_force_verify;
178 imp->imp_force_verify = 0;
179 spin_unlock(&imp->imp_lock);
181 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
182 "level %s/%u force %u deactive %u pingable %u\n",
183 ptlrpc_import_state_name(level), level,
184 force, imp->imp_deactive, imp->imp_pingable);
186 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
187 this_ping) && force == 0)
190 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
191 /* wait at least a timeout before trying recovery again */
192 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
193 ptlrpc_initiate_recovery(imp);
194 } else if (level != LUSTRE_IMP_FULL ||
195 imp->imp_obd->obd_no_recov ||
197 CDEBUG(D_HA, "not pinging %s (in recovery "
198 " or recovery disabled: %s)\n",
199 obd2cli_tgt(imp->imp_obd),
200 ptlrpc_import_state_name(level));
201 } else if (imp->imp_pingable || force) {
206 static int ptlrpc_pinger_main(void *arg)
208 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
209 struct ptlrpc_thread *thread = data->thread;
212 cfs_daemonize(data->name);
214 /* Record that the thread is running */
215 thread->t_flags = SVC_RUNNING;
216 cfs_waitq_signal(&thread->t_ctl_waitq);
218 /* And now, loop forever, pinging as needed. */
220 cfs_time_t this_ping = cfs_time_current();
221 struct l_wait_info lwi;
222 cfs_duration_t time_to_next_ping;
223 struct list_head *iter;
225 mutex_down(&pinger_sem);
226 list_for_each(iter, &pinger_imports) {
227 struct obd_import *imp =
228 list_entry(iter, struct obd_import,
231 ptlrpc_pinger_process_import(imp, this_ping);
232 /* obd_timeout might have changed */
233 if (imp->imp_pingable && imp->imp_next_ping &&
234 cfs_time_after(imp->imp_next_ping,
235 cfs_time_add(this_ping,
236 cfs_time_seconds(PING_INTERVAL))))
237 ptlrpc_update_next_ping(imp);
239 mutex_up(&pinger_sem);
240 /* update memory usage info */
241 obd_update_maxusage();
243 /* Wait until the next ping time, or until we're stopped. */
244 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
245 cfs_time_seconds(PING_INTERVAL)),
248 /* The ping sent by ptlrpc_send_rpc may get sent out
249 say .01 second after this.
250 ptlrpc_pinger_sending_on_import will then set the
251 next ping time to next_ping + .01 sec, which means
252 we will SKIP the next ping at next_ping, and the
253 ping will get sent 2 timeouts from now! Beware. */
254 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
256 cfs_time_add(this_ping,
257 cfs_time_seconds(PING_INTERVAL)));
258 if (time_to_next_ping > 0) {
259 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
261 cfs_time_seconds(1)),
263 l_wait_event(thread->t_ctl_waitq,
264 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
266 if (thread->t_flags & SVC_STOPPING) {
267 thread->t_flags &= ~SVC_STOPPING;
270 } else if (thread->t_flags & SVC_EVENT) {
271 /* woken after adding import to reset timer */
272 thread->t_flags &= ~SVC_EVENT;
277 thread->t_flags = SVC_STOPPED;
278 cfs_waitq_signal(&thread->t_ctl_waitq);
280 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
284 static struct ptlrpc_thread *pinger_thread = NULL;
286 int ptlrpc_start_pinger(void)
288 struct l_wait_info lwi = { 0 };
289 struct ptlrpc_svc_data d;
291 #ifndef ENABLE_PINGER
296 if (pinger_thread != NULL)
299 OBD_ALLOC_PTR(pinger_thread);
300 if (pinger_thread == NULL)
302 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
303 cfs_waitq_init(&suspend_timeouts_waitq);
306 d.thread = pinger_thread;
308 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
309 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
310 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
312 CERROR("cannot start thread: %d\n", rc);
313 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
314 pinger_thread = NULL;
317 l_wait_event(pinger_thread->t_ctl_waitq,
318 pinger_thread->t_flags & SVC_RUNNING, &lwi);
323 int ptlrpc_stop_pinger(void)
325 struct l_wait_info lwi = { 0 };
327 #ifndef ENABLE_PINGER
332 if (pinger_thread == NULL)
334 mutex_down(&pinger_sem);
335 pinger_thread->t_flags = SVC_STOPPING;
336 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
337 mutex_up(&pinger_sem);
339 l_wait_event(pinger_thread->t_ctl_waitq,
340 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
342 OBD_FREE_PTR(pinger_thread);
343 pinger_thread = NULL;
347 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
349 ptlrpc_update_next_ping(imp);
352 int ptlrpc_pinger_add_import(struct obd_import *imp)
355 if (!list_empty(&imp->imp_pinger_chain))
358 mutex_down(&pinger_sem);
359 CDEBUG(D_HA, "adding pingable import %s->%s\n",
360 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
361 /* if we add to pinger we want recovery on this import */
362 imp->imp_obd->obd_no_recov = 0;
363 ptlrpc_update_next_ping(imp);
364 /* XXX sort, blah blah */
365 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
366 class_import_get(imp);
368 ptlrpc_pinger_wake_up();
369 mutex_up(&pinger_sem);
374 int ptlrpc_pinger_del_import(struct obd_import *imp)
377 if (list_empty(&imp->imp_pinger_chain))
380 mutex_down(&pinger_sem);
381 list_del_init(&imp->imp_pinger_chain);
382 CDEBUG(D_HA, "removing pingable import %s->%s\n",
383 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
384 /* if we remove from pinger we don't want recovery on this import */
385 imp->imp_obd->obd_no_recov = 1;
386 class_import_put(imp);
387 mutex_up(&pinger_sem);
391 void ptlrpc_pinger_wake_up()
394 pinger_thread->t_flags |= SVC_EVENT;
395 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
399 /* Ping evictor thread */
401 #define PET_TERMINATE 2
403 static int pet_refcount = 0;
404 static int pet_state;
405 static wait_queue_head_t pet_waitq;
406 static struct obd_export *pet_exp = NULL;
407 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
409 int ping_evictor_wake(struct obd_export *exp)
411 spin_lock(&pet_lock);
412 if (pet_exp || (pet_state != PET_READY)) {
413 /* eventually the new obd will call here again. */
414 spin_unlock(&pet_lock);
418 /* We have to make sure the obd isn't destroyed between now and when
419 * the ping evictor runs. We'll take a reference here, and drop it
420 * when we finish in the evictor. We don't really care about this
421 * export in particular; we just need one to keep the obd alive. */
422 pet_exp = class_export_get(exp);
423 spin_unlock(&pet_lock);
429 static int ping_evictor_main(void *arg)
431 struct obd_device *obd;
432 struct obd_export *exp;
433 struct l_wait_info lwi = { 0 };
437 ptlrpc_daemonize("ll_evictor");
439 CDEBUG(D_HA, "Starting Ping Evictor\n");
441 pet_state = PET_READY;
443 l_wait_event(pet_waitq, pet_exp ||
444 (pet_state == PET_TERMINATE), &lwi);
445 if (pet_state == PET_TERMINATE)
448 /* we only get here if pet_exp != NULL, and the end of this
449 * loop is the only place which sets it NULL again, so lock
450 * is not strictly necessary. */
451 spin_lock(&pet_lock);
452 obd = pet_exp->exp_obd;
453 spin_unlock(&pet_lock);
455 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
457 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
458 obd->obd_name, expire_time);
460 /* Exports can't be deleted out of the list while we hold
461 * the obd lock (class_unlink_export), which means we can't
462 * lose the last ref on the export. If they've already been
463 * removed from the list, we won't find them here. */
464 spin_lock(&obd->obd_dev_lock);
465 while (!list_empty(&obd->obd_exports_timed)) {
466 exp = list_entry(obd->obd_exports_timed.next,
467 struct obd_export,exp_obd_chain_timed);
468 if (expire_time > exp->exp_last_request_time) {
469 class_export_get(exp);
470 spin_unlock(&obd->obd_dev_lock);
471 LCONSOLE_WARN("%s: haven't heard from client %s"
472 " (at %s) in %ld seconds. I think"
473 " it's dead, and I am evicting"
474 " it. exp %p, cur %ld expire %ld"
477 obd_uuid2str(&exp->exp_client_uuid),
478 obd_export_nid2str(exp),
479 (long)(cfs_time_current_sec() -
480 exp->exp_last_request_time),
481 exp, (long)cfs_time_current_sec(),
483 (long)exp->exp_last_request_time);
484 CDEBUG(D_HA, "Last request was at %ld\n",
485 exp->exp_last_request_time);
486 class_fail_export(exp);
487 class_export_put(exp);
488 spin_lock(&obd->obd_dev_lock);
490 /* List is sorted, so everyone below is ok */
494 spin_unlock(&obd->obd_dev_lock);
496 class_export_put(pet_exp);
498 spin_lock(&pet_lock);
500 spin_unlock(&pet_lock);
502 CDEBUG(D_HA, "Exiting Ping Evictor\n");
507 void ping_evictor_start(void)
511 if (++pet_refcount > 1)
514 init_waitqueue_head(&pet_waitq);
516 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
519 CERROR("Cannot start ping evictor thread: %d\n", rc);
522 EXPORT_SYMBOL(ping_evictor_start);
524 void ping_evictor_stop(void)
526 if (--pet_refcount > 0)
529 pet_state = PET_TERMINATE;
532 EXPORT_SYMBOL(ping_evictor_stop);
533 #else /* !__KERNEL__ */
536 * the current implementation of pinger in liblustre is not optimized
540 static struct pinger_data {
542 cfs_time_t pd_this_ping; /* jiffies */
543 cfs_time_t pd_next_ping; /* jiffies */
544 struct ptlrpc_request_set *pd_set;
547 static int pinger_check_rpcs(void *arg)
549 cfs_time_t curtime = cfs_time_current();
550 struct ptlrpc_request *req;
551 struct ptlrpc_request_set *set;
552 struct list_head *iter;
553 struct pinger_data *pd = &pinger_args;
556 /* prevent recursion */
557 if (pd->pd_recursion++) {
558 CDEBUG(D_HA, "pinger: recursion! quit\n");
564 /* have we reached ping point? */
565 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
570 /* if we have rpc_set already, continue processing it */
572 LASSERT(pd->pd_this_ping);
577 pd->pd_this_ping = curtime;
578 pd->pd_set = ptlrpc_prep_set();
579 if (pd->pd_set == NULL)
583 /* add rpcs into set */
584 mutex_down(&pinger_sem);
585 list_for_each(iter, &pinger_imports) {
586 struct obd_import *imp =
587 list_entry(iter, struct obd_import, imp_pinger_chain);
588 int generation, level;
590 if (cfs_time_aftereq(pd->pd_this_ping,
591 imp->imp_next_ping - 5 * CFS_TICK)) {
593 spin_lock(&imp->imp_lock);
594 generation = imp->imp_generation;
595 level = imp->imp_state;
596 spin_unlock(&imp->imp_lock);
598 if (level != LUSTRE_IMP_FULL) {
600 "not pinging %s (in recovery)\n",
601 obd2cli_tgt(imp->imp_obd));
605 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
609 CERROR("OOM trying to ping %s->%s\n",
610 imp->imp_obd->obd_uuid.uuid,
611 obd2cli_tgt(imp->imp_obd));
615 req->rq_no_resend = 1;
616 ptlrpc_request_set_replen(req);
617 req->rq_send_state = LUSTRE_IMP_FULL;
618 req->rq_phase = RQ_PHASE_RPC;
619 req->rq_import_generation = generation;
620 ptlrpc_set_add_req(set, req);
622 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
623 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
624 imp->imp_next_ping, pd->pd_this_ping);
627 pd->pd_this_ping = curtime;
628 mutex_up(&pinger_sem);
630 /* Might be empty, that's OK. */
631 if (set->set_remaining == 0)
632 CDEBUG(D_RPCTRACE, "nothing to ping\n");
634 list_for_each(iter, &set->set_requests) {
635 struct ptlrpc_request *req =
636 list_entry(iter, struct ptlrpc_request,
638 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
639 req->rq_import->imp_obd->obd_uuid.uuid,
640 obd2cli_tgt(req->rq_import->imp_obd));
641 (void)ptl_send_rpc(req, 0);
645 rc = ptlrpc_check_set(set);
647 /* not finished, and we are not expired, simply return */
648 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
649 cfs_time_seconds(PING_INTERVAL)))) {
650 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
655 /* Expire all the requests that didn't come back. */
656 mutex_down(&pinger_sem);
657 list_for_each(iter, &set->set_requests) {
658 req = list_entry(iter, struct ptlrpc_request,
661 if (req->rq_phase == RQ_PHASE_COMPLETE)
664 req->rq_phase = RQ_PHASE_COMPLETE;
665 atomic_dec(&req->rq_import->imp_inflight);
666 set->set_remaining--;
667 /* If it was disconnected, don't sweat it. */
668 if (list_empty(&req->rq_import->imp_pinger_chain)) {
669 ptlrpc_unregister_reply(req);
673 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
674 ptlrpc_expire_one_request(req);
676 mutex_up(&pinger_sem);
678 ptlrpc_set_destroy(set);
682 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
683 cfs_time_seconds(PING_INTERVAL));
684 pd->pd_this_ping = 0; /* XXX for debug */
686 CDEBUG(D_INFO, "finished a round ping\n");
691 static void *pinger_callback = NULL;
692 #endif /* ENABLE_PINGER */
694 int ptlrpc_start_pinger(void)
697 memset(&pinger_args, 0, sizeof(pinger_args));
698 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
705 int ptlrpc_stop_pinger(void)
709 liblustre_deregister_wait_callback(pinger_callback);
714 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
717 mutex_down(&pinger_sem);
718 ptlrpc_update_next_ping(imp);
719 if (pinger_args.pd_set == NULL &&
720 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
721 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
722 imp->imp_next_ping, cfs_time_current());
723 pinger_args.pd_next_ping = imp->imp_next_ping;
725 mutex_up(&pinger_sem);
729 int ptlrpc_pinger_add_import(struct obd_import *imp)
732 if (!list_empty(&imp->imp_pinger_chain))
735 CDEBUG(D_HA, "adding pingable import %s->%s\n",
736 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
737 ptlrpc_pinger_sending_on_import(imp);
739 mutex_down(&pinger_sem);
740 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
741 class_import_get(imp);
742 mutex_up(&pinger_sem);
747 int ptlrpc_pinger_del_import(struct obd_import *imp)
750 if (list_empty(&imp->imp_pinger_chain))
753 mutex_down(&pinger_sem);
754 list_del_init(&imp->imp_pinger_chain);
755 CDEBUG(D_HA, "removing pingable import %s->%s\n",
756 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
757 class_import_put(imp);
758 mutex_up(&pinger_sem);
762 void ptlrpc_pinger_wake_up()
765 /* XXX force pinger to run, if needed */
766 struct obd_import *imp;
768 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
769 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
770 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
771 #ifdef ENABLE_LIBLUSTRE_RECOVERY
772 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
774 /*XXX only recover for the initial connection */
775 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
776 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
778 ptlrpc_initiate_recovery(imp);
779 else if (imp->imp_state != LUSTRE_IMP_FULL)
780 CDEBUG(D_HA, "Refused to recover import %s->%s "
781 "state %d, deactive %d\n",
782 imp->imp_obd->obd_uuid.uuid,
783 obd2cli_tgt(imp->imp_obd), imp->imp_state,
789 #endif /* !__KERNEL__ */