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_eending_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, cfs_time_seconds(PING_INTERVAL)));
241 if (time_to_next_ping > 0) {
242 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
244 l_wait_event(thread->t_ctl_waitq,
245 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
247 if (thread->t_flags & SVC_STOPPING) {
248 thread->t_flags &= ~SVC_STOPPING;
251 } else if (thread->t_flags & SVC_EVENT) {
252 /* woken after adding import to reset timer */
253 thread->t_flags &= ~SVC_EVENT;
258 thread->t_flags = SVC_STOPPED;
259 cfs_waitq_signal(&thread->t_ctl_waitq);
261 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
265 static struct ptlrpc_thread *pinger_thread = NULL;
267 int ptlrpc_start_pinger(void)
269 struct l_wait_info lwi = { 0 };
270 struct ptlrpc_svc_data d;
272 #ifndef ENABLE_PINGER
277 if (pinger_thread != NULL)
280 OBD_ALLOC_PTR(pinger_thread);
281 if (pinger_thread == NULL)
283 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
284 cfs_waitq_init(&suspend_timeouts_waitq);
287 d.thread = pinger_thread;
289 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
290 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
291 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
293 CERROR("cannot start thread: %d\n", rc);
294 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
295 pinger_thread = NULL;
298 l_wait_event(pinger_thread->t_ctl_waitq,
299 pinger_thread->t_flags & SVC_RUNNING, &lwi);
304 int ptlrpc_stop_pinger(void)
306 struct l_wait_info lwi = { 0 };
308 #ifndef ENABLE_PINGER
313 if (pinger_thread == NULL)
315 mutex_down(&pinger_sem);
316 pinger_thread->t_flags = SVC_STOPPING;
317 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
318 mutex_up(&pinger_sem);
320 l_wait_event(pinger_thread->t_ctl_waitq,
321 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
323 OBD_FREE_PTR(pinger_thread);
324 pinger_thread = NULL;
328 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
330 ptlrpc_update_next_ping(imp);
333 int ptlrpc_pinger_add_import(struct obd_import *imp)
336 if (!list_empty(&imp->imp_pinger_chain))
339 mutex_down(&pinger_sem);
340 CDEBUG(D_HA, "adding pingable import %s->%s\n",
341 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
342 /* if we add to pinger we want recovery on this import */
343 imp->imp_obd->obd_no_recov = 0;
344 ptlrpc_update_next_ping(imp);
345 /* XXX sort, blah blah */
346 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
347 class_import_get(imp);
349 ptlrpc_pinger_wake_up();
350 mutex_up(&pinger_sem);
355 int ptlrpc_pinger_del_import(struct obd_import *imp)
358 if (list_empty(&imp->imp_pinger_chain))
361 mutex_down(&pinger_sem);
362 list_del_init(&imp->imp_pinger_chain);
363 CDEBUG(D_HA, "removing pingable import %s->%s\n",
364 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
365 /* if we remove from pinger we don't want recovery on this import */
366 imp->imp_obd->obd_no_recov = 1;
367 class_import_put(imp);
368 mutex_up(&pinger_sem);
372 void ptlrpc_pinger_wake_up()
375 pinger_thread->t_flags |= SVC_EVENT;
376 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
380 /* Ping evictor thread */
382 #define PET_TERMINATE 2
384 static int pet_refcount = 0;
385 static int pet_state;
386 static wait_queue_head_t pet_waitq;
387 static struct obd_export *pet_exp = NULL;
388 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
390 int ping_evictor_wake(struct obd_export *exp)
392 spin_lock(&pet_lock);
393 if (pet_exp || (pet_state != PET_READY)) {
394 /* eventually the new obd will call here again. */
395 spin_unlock(&pet_lock);
399 /* We have to make sure the obd isn't destroyed between now and when
400 * the ping evictor runs. We'll take a reference here, and drop it
401 * when we finish in the evictor. We don't really care about this
402 * export in particular; we just need one to keep the obd alive. */
403 pet_exp = class_export_get(exp);
404 spin_unlock(&pet_lock);
410 static int ping_evictor_main(void *arg)
412 struct obd_device *obd;
413 struct obd_export *exp;
414 struct l_wait_info lwi = { 0 };
418 ptlrpc_daemonize("ll_evictor");
420 CDEBUG(D_HA, "Starting Ping Evictor\n");
422 pet_state = PET_READY;
424 l_wait_event(pet_waitq, pet_exp ||
425 (pet_state == PET_TERMINATE), &lwi);
426 if (pet_state == PET_TERMINATE)
429 /* we only get here if pet_exp != NULL, and the end of this
430 * loop is the only place which sets it NULL again, so lock
431 * is not strictly necessary. */
432 spin_lock(&pet_lock);
433 obd = pet_exp->exp_obd;
434 spin_unlock(&pet_lock);
436 expire_time = cfs_time_current_sec() - (3 * obd_timeout / 2);
438 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
439 obd->obd_name, expire_time);
441 /* Exports can't be deleted out of the list while we hold
442 * the obd lock (class_unlink_export), which means we can't
443 * lose the last ref on the export. If they've already been
444 * removed from the list, we won't find them here. */
445 spin_lock(&obd->obd_dev_lock);
446 while (!list_empty(&obd->obd_exports_timed)) {
447 exp = list_entry(obd->obd_exports_timed.next,
448 struct obd_export,exp_obd_chain_timed);
449 if (expire_time > exp->exp_last_request_time) {
450 class_export_get(exp);
451 spin_unlock(&obd->obd_dev_lock);
452 LCONSOLE_WARN("%s: haven't heard from client %s"
453 " (at %s) in %ld seconds. I think"
454 " it's dead, and I am evicting"
455 " it. exp %p, cur %ld expire %ld"
458 obd_uuid2str(&exp->exp_client_uuid),
459 obd_export_nid2str(exp),
460 (long)(cfs_time_current_sec() -
461 exp->exp_last_request_time),
462 exp, (long)cfs_time_current_sec(),
464 (long)exp->exp_last_request_time);
465 CDEBUG(D_HA, "Last request was at %ld\n",
466 exp->exp_last_request_time);
467 class_fail_export(exp);
468 class_export_put(exp);
469 spin_lock(&obd->obd_dev_lock);
471 /* List is sorted, so everyone below is ok */
475 spin_unlock(&obd->obd_dev_lock);
477 class_export_put(pet_exp);
479 spin_lock(&pet_lock);
481 spin_unlock(&pet_lock);
483 CDEBUG(D_HA, "Exiting Ping Evictor\n");
488 void ping_evictor_start(void)
492 if (++pet_refcount > 1)
495 init_waitqueue_head(&pet_waitq);
497 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
500 CERROR("Cannot start ping evictor thread: %d\n", rc);
503 EXPORT_SYMBOL(ping_evictor_start);
505 void ping_evictor_stop(void)
507 if (--pet_refcount > 0)
510 pet_state = PET_TERMINATE;
513 EXPORT_SYMBOL(ping_evictor_stop);
514 #else /* !__KERNEL__ */
517 * the current implementation of pinger in liblustre is not optimized
521 static struct pinger_data {
523 cfs_time_t pd_this_ping; /* jiffies */
524 cfs_time_t pd_next_ping; /* jiffies */
525 struct ptlrpc_request_set *pd_set;
528 static int pinger_check_rpcs(void *arg)
530 cfs_time_t curtime = cfs_time_current();
531 struct ptlrpc_request *req;
532 struct ptlrpc_request_set *set;
533 struct list_head *iter;
534 struct pinger_data *pd = &pinger_args;
537 /* prevent recursion */
538 if (pd->pd_recursion++) {
539 CDEBUG(D_HA, "pinger: recursion! quit\n");
545 /* have we reached ping point? */
546 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
551 /* if we have rpc_set already, continue processing it */
553 LASSERT(pd->pd_this_ping);
558 pd->pd_this_ping = curtime;
559 pd->pd_set = ptlrpc_prep_set();
560 if (pd->pd_set == NULL)
564 /* add rpcs into set */
565 mutex_down(&pinger_sem);
566 list_for_each(iter, &pinger_imports) {
567 struct obd_import *imp =
568 list_entry(iter, struct obd_import, imp_pinger_chain);
569 int generation, level;
571 if (cfs_time_aftereq(pd->pd_this_ping,
572 imp->imp_next_ping - 5 * CFS_TICK)) {
574 spin_lock(&imp->imp_lock);
575 generation = imp->imp_generation;
576 level = imp->imp_state;
577 spin_unlock(&imp->imp_lock);
579 if (level != LUSTRE_IMP_FULL) {
581 "not pinging %s (in recovery)\n",
582 obd2cli_tgt(imp->imp_obd));
586 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
590 CERROR("OOM trying to ping %s->%s\n",
591 imp->imp_obd->obd_uuid.uuid,
592 obd2cli_tgt(imp->imp_obd));
596 req->rq_no_resend = 1;
597 ptlrpc_request_set_replen(req);
598 req->rq_send_state = LUSTRE_IMP_FULL;
599 req->rq_phase = RQ_PHASE_RPC;
600 req->rq_import_generation = generation;
601 ptlrpc_set_add_req(set, req);
603 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
604 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
605 imp->imp_next_ping, pd->pd_this_ping);
608 pd->pd_this_ping = curtime;
609 mutex_up(&pinger_sem);
611 /* Might be empty, that's OK. */
612 if (set->set_remaining == 0)
613 CDEBUG(D_RPCTRACE, "nothing to ping\n");
615 list_for_each(iter, &set->set_requests) {
616 struct ptlrpc_request *req =
617 list_entry(iter, struct ptlrpc_request,
619 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
620 req->rq_import->imp_obd->obd_uuid.uuid,
621 obd2cli_tgt(req->rq_import->imp_obd));
622 (void)ptl_send_rpc(req, 0);
626 rc = ptlrpc_check_set(set);
628 /* not finished, and we are not expired, simply return */
629 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
630 cfs_time_seconds(PING_INTERVAL)))) {
631 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
636 /* Expire all the requests that didn't come back. */
637 mutex_down(&pinger_sem);
638 list_for_each(iter, &set->set_requests) {
639 req = list_entry(iter, struct ptlrpc_request,
642 if (req->rq_phase == RQ_PHASE_COMPLETE)
645 req->rq_phase = RQ_PHASE_COMPLETE;
646 atomic_dec(&req->rq_import->imp_inflight);
647 set->set_remaining--;
648 /* If it was disconnected, don't sweat it. */
649 if (list_empty(&req->rq_import->imp_pinger_chain)) {
650 ptlrpc_unregister_reply(req);
654 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
655 ptlrpc_expire_one_request(req);
657 mutex_up(&pinger_sem);
659 ptlrpc_set_destroy(set);
663 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
664 cfs_time_seconds(PING_INTERVAL));
665 pd->pd_this_ping = 0; /* XXX for debug */
667 CDEBUG(D_INFO, "finished a round ping\n");
672 static void *pinger_callback = NULL;
673 #endif /* ENABLE_PINGER */
675 int ptlrpc_start_pinger(void)
678 memset(&pinger_args, 0, sizeof(pinger_args));
679 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
686 int ptlrpc_stop_pinger(void)
690 liblustre_deregister_wait_callback(pinger_callback);
695 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
698 mutex_down(&pinger_sem);
699 ptlrpc_update_next_ping(imp);
700 if (pinger_args.pd_set == NULL &&
701 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
702 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
703 imp->imp_next_ping, cfs_time_current());
704 pinger_args.pd_next_ping = imp->imp_next_ping;
706 mutex_up(&pinger_sem);
710 int ptlrpc_pinger_add_import(struct obd_import *imp)
713 if (!list_empty(&imp->imp_pinger_chain))
716 CDEBUG(D_HA, "adding pingable import %s->%s\n",
717 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
718 ptlrpc_pinger_sending_on_import(imp);
720 mutex_down(&pinger_sem);
721 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
722 class_import_get(imp);
723 mutex_up(&pinger_sem);
728 int ptlrpc_pinger_del_import(struct obd_import *imp)
731 if (list_empty(&imp->imp_pinger_chain))
734 mutex_down(&pinger_sem);
735 list_del_init(&imp->imp_pinger_chain);
736 CDEBUG(D_HA, "removing pingable import %s->%s\n",
737 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
738 class_import_put(imp);
739 mutex_up(&pinger_sem);
743 void ptlrpc_pinger_wake_up()
746 /* XXX force pinger to run, if needed */
747 struct obd_import *imp;
749 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
750 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
751 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
752 #ifdef ENABLE_LIBLUSTRE_RECOVERY
753 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
755 /*XXX only recover for the initial connection */
756 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
757 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
759 ptlrpc_initiate_recovery(imp);
760 else if (imp->imp_state != LUSTRE_IMP_FULL)
761 CDEBUG(D_HA, "Refused to recover import %s->%s "
762 "state %d, deactive %d\n",
763 imp->imp_obd->obd_uuid.uuid,
764 obd2cli_tgt(imp->imp_obd), imp->imp_state,
770 #endif /* !__KERNEL__ */