1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Portal-RPC reconnection and replay operations, for use in recovery.
6 * Copyright (c) 2003 Cluster File Systems, Inc.
7 * Authors: Phil Schwan <phil@clusterfs.com>
8 * Mike Shaver <shaver@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
30 #include <liblustre.h>
32 #define DEBUG_SUBSYSTEM S_RPC
35 #include <obd_support.h>
36 #include <obd_class.h>
37 #include "ptlrpc_internal.h"
39 struct semaphore pinger_sem;
40 static CFS_LIST_HEAD(pinger_imports);
42 int ptlrpc_ping(struct obd_import *imp)
44 struct ptlrpc_request *req;
47 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
50 CERROR("OOM trying to ping %s->%s\n",
51 imp->imp_obd->obd_uuid.uuid,
52 obd2cli_tgt(imp->imp_obd));
56 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
57 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
58 req->rq_no_resend = req->rq_no_delay = 1;
59 ptlrpc_request_set_replen(req);
60 req->rq_timeout = PING_INTERVAL;
66 void ptlrpc_update_next_ping(struct obd_import *imp)
69 imp->imp_next_ping = cfs_time_shift(
70 (imp->imp_state == LUSTRE_IMP_DISCON ?
71 RECONNECT_INTERVAL : PING_INTERVAL));
72 #endif /* ENABLE_PINGER */
75 void ptlrpc_ping_import_soon(struct obd_import *imp)
77 imp->imp_next_ping = cfs_time_current();
80 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
82 if (imp->imp_server_timeout)
83 return cfs_time_shift(obd_timeout / 2);
85 return cfs_time_shift(obd_timeout);
88 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
89 static cfs_time_t suspend_wakeup_time = 0;
92 static wait_queue_head_t suspend_timeouts_waitq;
95 cfs_time_t ptlrpc_suspend_wakeup_time(void)
97 return suspend_wakeup_time;
100 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
102 /*XXX: disabled for now, will be replaced by adaptive timeouts */
104 if (imp->imp_no_timeout)
106 imp->imp_no_timeout = 1;
107 atomic_inc(&suspend_timeouts);
108 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
112 void ptlrpc_activate_timeouts(struct obd_import *imp)
114 /*XXX: disabled for now, will be replaced by adaptive timeouts */
116 if (!imp->imp_no_timeout)
118 imp->imp_no_timeout = 0;
119 LASSERT(atomic_read(&suspend_timeouts) > 0);
120 if (atomic_dec_and_test(&suspend_timeouts)) {
121 suspend_wakeup_time = cfs_time_current();
122 wake_up(&suspend_timeouts_waitq);
124 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
128 int ptlrpc_check_suspend(void)
130 if (atomic_read(&suspend_timeouts))
135 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
137 struct l_wait_info lwi;
139 if (atomic_read(&suspend_timeouts)) {
140 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
141 atomic_read(&suspend_timeouts));
142 lwi = LWI_INTR(NULL, NULL);
143 l_wait_event(suspend_timeouts_waitq,
144 atomic_read(&suspend_timeouts) == 0, &lwi);
145 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
153 static void ptlrpc_pinger_process_import(struct obd_import *imp,
154 unsigned long this_ping)
158 spin_lock(&imp->imp_lock);
159 level = imp->imp_state;
160 force = imp->imp_force_verify;
162 imp->imp_force_verify = 0;
163 spin_unlock(&imp->imp_lock);
165 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
166 "level %s/%u force %u deactive %u pingable %u\n",
167 ptlrpc_import_state_name(level), level,
168 force, imp->imp_deactive, imp->imp_pingable);
170 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
171 this_ping) && force == 0)
174 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
175 /* wait at least a timeout before trying recovery again */
176 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
177 ptlrpc_initiate_recovery(imp);
178 } else if (level != LUSTRE_IMP_FULL ||
179 imp->imp_obd->obd_no_recov ||
181 CDEBUG(D_HA, "not pinging %s (in recovery "
182 " or recovery disabled: %s)\n",
183 obd2cli_tgt(imp->imp_obd),
184 ptlrpc_import_state_name(level));
185 } else if (imp->imp_pingable || force) {
190 static int ptlrpc_pinger_main(void *arg)
192 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
193 struct ptlrpc_thread *thread = data->thread;
196 cfs_daemonize(data->name);
198 /* Record that the thread is running */
199 thread->t_flags = SVC_RUNNING;
200 cfs_waitq_signal(&thread->t_ctl_waitq);
202 /* And now, loop forever, pinging as needed. */
204 cfs_time_t this_ping = cfs_time_current();
205 struct l_wait_info lwi;
206 cfs_duration_t time_to_next_ping;
207 struct list_head *iter;
209 mutex_down(&pinger_sem);
210 list_for_each(iter, &pinger_imports) {
211 struct obd_import *imp =
212 list_entry(iter, struct obd_import,
215 ptlrpc_pinger_process_import(imp, this_ping);
216 /* obd_timeout might have changed */
217 if (imp->imp_pingable && imp->imp_next_ping &&
218 cfs_time_after(imp->imp_next_ping,
219 cfs_time_add(this_ping,
220 cfs_time_seconds(PING_INTERVAL))))
221 ptlrpc_update_next_ping(imp);
223 mutex_up(&pinger_sem);
224 /* update memory usage info */
225 obd_update_maxusage();
227 /* Wait until the next ping time, or until we're stopped. */
228 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
229 cfs_time_seconds(PING_INTERVAL)),
232 /* The ping sent by ptlrpc_send_rpc may get sent out
233 say .01 second after this.
234 ptlrpc_pinger_sending_on_import will then set the
235 next ping time to next_ping + .01 sec, which means
236 we will SKIP the next ping at next_ping, and the
237 ping will get sent 2 timeouts from now! Beware. */
238 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
240 cfs_time_add(this_ping,
241 cfs_time_seconds(PING_INTERVAL)));
242 if (time_to_next_ping > 0) {
243 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
245 cfs_time_seconds(1)),
247 l_wait_event(thread->t_ctl_waitq,
248 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
250 if (thread->t_flags & SVC_STOPPING) {
251 thread->t_flags &= ~SVC_STOPPING;
254 } else if (thread->t_flags & SVC_EVENT) {
255 /* woken after adding import to reset timer */
256 thread->t_flags &= ~SVC_EVENT;
261 thread->t_flags = SVC_STOPPED;
262 cfs_waitq_signal(&thread->t_ctl_waitq);
264 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
268 static struct ptlrpc_thread *pinger_thread = NULL;
270 int ptlrpc_start_pinger(void)
272 struct l_wait_info lwi = { 0 };
273 struct ptlrpc_svc_data d;
275 #ifndef ENABLE_PINGER
280 if (pinger_thread != NULL)
283 OBD_ALLOC_PTR(pinger_thread);
284 if (pinger_thread == NULL)
286 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
287 cfs_waitq_init(&suspend_timeouts_waitq);
290 d.thread = pinger_thread;
292 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
293 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
294 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
296 CERROR("cannot start thread: %d\n", rc);
297 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
298 pinger_thread = NULL;
301 l_wait_event(pinger_thread->t_ctl_waitq,
302 pinger_thread->t_flags & SVC_RUNNING, &lwi);
307 int ptlrpc_stop_pinger(void)
309 struct l_wait_info lwi = { 0 };
311 #ifndef ENABLE_PINGER
316 if (pinger_thread == NULL)
318 mutex_down(&pinger_sem);
319 pinger_thread->t_flags = SVC_STOPPING;
320 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
321 mutex_up(&pinger_sem);
323 l_wait_event(pinger_thread->t_ctl_waitq,
324 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
326 OBD_FREE_PTR(pinger_thread);
327 pinger_thread = NULL;
331 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
333 ptlrpc_update_next_ping(imp);
336 int ptlrpc_pinger_add_import(struct obd_import *imp)
339 if (!list_empty(&imp->imp_pinger_chain))
342 mutex_down(&pinger_sem);
343 CDEBUG(D_HA, "adding pingable import %s->%s\n",
344 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
345 /* if we add to pinger we want recovery on this import */
346 imp->imp_obd->obd_no_recov = 0;
347 ptlrpc_update_next_ping(imp);
348 /* XXX sort, blah blah */
349 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
350 class_import_get(imp);
352 ptlrpc_pinger_wake_up();
353 mutex_up(&pinger_sem);
358 int ptlrpc_pinger_del_import(struct obd_import *imp)
361 if (list_empty(&imp->imp_pinger_chain))
364 mutex_down(&pinger_sem);
365 list_del_init(&imp->imp_pinger_chain);
366 CDEBUG(D_HA, "removing pingable import %s->%s\n",
367 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
368 /* if we remove from pinger we don't want recovery on this import */
369 imp->imp_obd->obd_no_recov = 1;
370 class_import_put(imp);
371 mutex_up(&pinger_sem);
375 void ptlrpc_pinger_wake_up()
378 pinger_thread->t_flags |= SVC_EVENT;
379 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
383 /* Ping evictor thread */
385 #define PET_TERMINATE 2
387 static int pet_refcount = 0;
388 static int pet_state;
389 static wait_queue_head_t pet_waitq;
390 static struct obd_export *pet_exp = NULL;
391 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
393 int ping_evictor_wake(struct obd_export *exp)
395 spin_lock(&pet_lock);
396 if (pet_exp || (pet_state != PET_READY)) {
397 /* eventually the new obd will call here again. */
398 spin_unlock(&pet_lock);
402 /* We have to make sure the obd isn't destroyed between now and when
403 * the ping evictor runs. We'll take a reference here, and drop it
404 * when we finish in the evictor. We don't really care about this
405 * export in particular; we just need one to keep the obd alive. */
406 pet_exp = class_export_get(exp);
407 spin_unlock(&pet_lock);
413 static int ping_evictor_main(void *arg)
415 struct obd_device *obd;
416 struct obd_export *exp;
417 struct l_wait_info lwi = { 0 };
421 ptlrpc_daemonize("ll_evictor");
423 CDEBUG(D_HA, "Starting Ping Evictor\n");
425 pet_state = PET_READY;
427 l_wait_event(pet_waitq, pet_exp ||
428 (pet_state == PET_TERMINATE), &lwi);
429 if (pet_state == PET_TERMINATE)
432 /* we only get here if pet_exp != NULL, and the end of this
433 * loop is the only place which sets it NULL again, so lock
434 * is not strictly necessary. */
435 spin_lock(&pet_lock);
436 obd = pet_exp->exp_obd;
437 spin_unlock(&pet_lock);
439 expire_time = cfs_time_current_sec() - (3 * obd_timeout / 2);
441 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
442 obd->obd_name, expire_time);
444 /* Exports can't be deleted out of the list while we hold
445 * the obd lock (class_unlink_export), which means we can't
446 * lose the last ref on the export. If they've already been
447 * removed from the list, we won't find them here. */
448 spin_lock(&obd->obd_dev_lock);
449 while (!list_empty(&obd->obd_exports_timed)) {
450 exp = list_entry(obd->obd_exports_timed.next,
451 struct obd_export,exp_obd_chain_timed);
452 if (expire_time > exp->exp_last_request_time) {
453 class_export_get(exp);
454 spin_unlock(&obd->obd_dev_lock);
455 LCONSOLE_WARN("%s: haven't heard from client %s"
456 " (at %s) in %ld seconds. I think"
457 " it's dead, and I am evicting"
458 " it. exp %p, cur %ld expire %ld"
461 obd_uuid2str(&exp->exp_client_uuid),
462 obd_export_nid2str(exp),
463 (long)(cfs_time_current_sec() -
464 exp->exp_last_request_time),
465 exp, (long)cfs_time_current_sec(),
467 (long)exp->exp_last_request_time);
468 CDEBUG(D_HA, "Last request was at %ld\n",
469 exp->exp_last_request_time);
470 class_fail_export(exp);
471 class_export_put(exp);
472 spin_lock(&obd->obd_dev_lock);
474 /* List is sorted, so everyone below is ok */
478 spin_unlock(&obd->obd_dev_lock);
480 class_export_put(pet_exp);
482 spin_lock(&pet_lock);
484 spin_unlock(&pet_lock);
486 CDEBUG(D_HA, "Exiting Ping Evictor\n");
491 void ping_evictor_start(void)
495 if (++pet_refcount > 1)
498 init_waitqueue_head(&pet_waitq);
500 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
503 CERROR("Cannot start ping evictor thread: %d\n", rc);
506 EXPORT_SYMBOL(ping_evictor_start);
508 void ping_evictor_stop(void)
510 if (--pet_refcount > 0)
513 pet_state = PET_TERMINATE;
516 EXPORT_SYMBOL(ping_evictor_stop);
517 #else /* !__KERNEL__ */
520 * the current implementation of pinger in liblustre is not optimized
524 static struct pinger_data {
526 cfs_time_t pd_this_ping; /* jiffies */
527 cfs_time_t pd_next_ping; /* jiffies */
528 struct ptlrpc_request_set *pd_set;
531 static int pinger_check_rpcs(void *arg)
533 cfs_time_t curtime = cfs_time_current();
534 struct ptlrpc_request *req;
535 struct ptlrpc_request_set *set;
536 struct list_head *iter;
537 struct pinger_data *pd = &pinger_args;
540 /* prevent recursion */
541 if (pd->pd_recursion++) {
542 CDEBUG(D_HA, "pinger: recursion! quit\n");
548 /* have we reached ping point? */
549 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
554 /* if we have rpc_set already, continue processing it */
556 LASSERT(pd->pd_this_ping);
561 pd->pd_this_ping = curtime;
562 pd->pd_set = ptlrpc_prep_set();
563 if (pd->pd_set == NULL)
567 /* add rpcs into set */
568 mutex_down(&pinger_sem);
569 list_for_each(iter, &pinger_imports) {
570 struct obd_import *imp =
571 list_entry(iter, struct obd_import, imp_pinger_chain);
572 int generation, level;
574 if (cfs_time_aftereq(pd->pd_this_ping,
575 imp->imp_next_ping - 5 * CFS_TICK)) {
577 spin_lock(&imp->imp_lock);
578 generation = imp->imp_generation;
579 level = imp->imp_state;
580 spin_unlock(&imp->imp_lock);
582 if (level != LUSTRE_IMP_FULL) {
584 "not pinging %s (in recovery)\n",
585 obd2cli_tgt(imp->imp_obd));
589 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
593 CERROR("OOM trying to ping %s->%s\n",
594 imp->imp_obd->obd_uuid.uuid,
595 obd2cli_tgt(imp->imp_obd));
599 req->rq_no_resend = 1;
600 ptlrpc_request_set_replen(req);
601 req->rq_send_state = LUSTRE_IMP_FULL;
602 req->rq_phase = RQ_PHASE_RPC;
603 req->rq_import_generation = generation;
604 ptlrpc_set_add_req(set, req);
606 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
607 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
608 imp->imp_next_ping, pd->pd_this_ping);
611 pd->pd_this_ping = curtime;
612 mutex_up(&pinger_sem);
614 /* Might be empty, that's OK. */
615 if (set->set_remaining == 0)
616 CDEBUG(D_RPCTRACE, "nothing to ping\n");
618 list_for_each(iter, &set->set_requests) {
619 struct ptlrpc_request *req =
620 list_entry(iter, struct ptlrpc_request,
622 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
623 req->rq_import->imp_obd->obd_uuid.uuid,
624 obd2cli_tgt(req->rq_import->imp_obd));
625 (void)ptl_send_rpc(req, 0);
629 rc = ptlrpc_check_set(set);
631 /* not finished, and we are not expired, simply return */
632 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
633 cfs_time_seconds(PING_INTERVAL)))) {
634 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
639 /* Expire all the requests that didn't come back. */
640 mutex_down(&pinger_sem);
641 list_for_each(iter, &set->set_requests) {
642 req = list_entry(iter, struct ptlrpc_request,
645 if (req->rq_phase == RQ_PHASE_COMPLETE)
648 req->rq_phase = RQ_PHASE_COMPLETE;
649 atomic_dec(&req->rq_import->imp_inflight);
650 set->set_remaining--;
651 /* If it was disconnected, don't sweat it. */
652 if (list_empty(&req->rq_import->imp_pinger_chain)) {
653 ptlrpc_unregister_reply(req);
657 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
658 ptlrpc_expire_one_request(req);
660 mutex_up(&pinger_sem);
662 ptlrpc_set_destroy(set);
666 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
667 cfs_time_seconds(PING_INTERVAL));
668 pd->pd_this_ping = 0; /* XXX for debug */
670 CDEBUG(D_INFO, "finished a round ping\n");
675 static void *pinger_callback = NULL;
676 #endif /* ENABLE_PINGER */
678 int ptlrpc_start_pinger(void)
681 memset(&pinger_args, 0, sizeof(pinger_args));
682 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
689 int ptlrpc_stop_pinger(void)
693 liblustre_deregister_wait_callback(pinger_callback);
698 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
701 mutex_down(&pinger_sem);
702 ptlrpc_update_next_ping(imp);
703 if (pinger_args.pd_set == NULL &&
704 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
705 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
706 imp->imp_next_ping, cfs_time_current());
707 pinger_args.pd_next_ping = imp->imp_next_ping;
709 mutex_up(&pinger_sem);
713 int ptlrpc_pinger_add_import(struct obd_import *imp)
716 if (!list_empty(&imp->imp_pinger_chain))
719 CDEBUG(D_HA, "adding pingable import %s->%s\n",
720 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
721 ptlrpc_pinger_sending_on_import(imp);
723 mutex_down(&pinger_sem);
724 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
725 class_import_get(imp);
726 mutex_up(&pinger_sem);
731 int ptlrpc_pinger_del_import(struct obd_import *imp)
734 if (list_empty(&imp->imp_pinger_chain))
737 mutex_down(&pinger_sem);
738 list_del_init(&imp->imp_pinger_chain);
739 CDEBUG(D_HA, "removing pingable import %s->%s\n",
740 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
741 class_import_put(imp);
742 mutex_up(&pinger_sem);
746 void ptlrpc_pinger_wake_up()
749 /* XXX force pinger to run, if needed */
750 struct obd_import *imp;
752 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
753 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
754 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
755 #ifdef ENABLE_LIBLUSTRE_RECOVERY
756 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
758 /*XXX only recover for the initial connection */
759 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
760 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
762 ptlrpc_initiate_recovery(imp);
763 else if (imp->imp_state != LUSTRE_IMP_FULL)
764 CDEBUG(D_HA, "Refused to recover import %s->%s "
765 "state %d, deactive %d\n",
766 imp->imp_obd->obd_uuid.uuid,
767 obd2cli_tgt(imp->imp_obd), imp->imp_state,
773 #endif /* !__KERNEL__ */