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);
65 void ptlrpc_update_next_ping(struct obd_import *imp)
68 int time = PING_INTERVAL;
69 if (imp->imp_state == LUSTRE_IMP_DISCON) {
70 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
72 at_get(&imp->imp_at.iat_net_latency));
73 time = min(time, dtime);
75 imp->imp_next_ping = cfs_time_shift(time);
76 #endif /* ENABLE_PINGER */
79 void ptlrpc_ping_import_soon(struct obd_import *imp)
81 imp->imp_next_ping = cfs_time_current();
84 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
86 if (imp->imp_server_timeout)
87 return cfs_time_shift(obd_timeout / 2);
89 return cfs_time_shift(obd_timeout);
92 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
93 static cfs_time_t suspend_wakeup_time = 0;
96 static wait_queue_head_t suspend_timeouts_waitq;
99 cfs_time_t ptlrpc_suspend_wakeup_time(void)
101 return suspend_wakeup_time;
104 void ptlrpc_deactivate_timeouts(struct obd_import *imp)
106 /*XXX: disabled for now, will be replaced by adaptive timeouts */
108 if (imp->imp_no_timeout)
110 imp->imp_no_timeout = 1;
111 atomic_inc(&suspend_timeouts);
112 CDEBUG(D_HA|D_WARNING, "deactivate timeouts %u\n", atomic_read(&suspend_timeouts));
116 void ptlrpc_activate_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 = 0;
123 LASSERT(atomic_read(&suspend_timeouts) > 0);
124 if (atomic_dec_and_test(&suspend_timeouts)) {
125 suspend_wakeup_time = cfs_time_current();
126 wake_up(&suspend_timeouts_waitq);
128 CDEBUG(D_HA|D_WARNING, "activate timeouts %u\n", atomic_read(&suspend_timeouts));
132 int ptlrpc_check_suspend(void)
134 if (atomic_read(&suspend_timeouts))
139 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
141 struct l_wait_info lwi;
143 if (atomic_read(&suspend_timeouts)) {
144 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
145 atomic_read(&suspend_timeouts));
146 lwi = LWI_INTR(NULL, NULL);
147 l_wait_event(suspend_timeouts_waitq,
148 atomic_read(&suspend_timeouts) == 0, &lwi);
149 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
157 static void ptlrpc_pinger_process_import(struct obd_import *imp,
158 unsigned long this_ping)
162 spin_lock(&imp->imp_lock);
163 level = imp->imp_state;
164 force = imp->imp_force_verify;
166 imp->imp_force_verify = 0;
167 spin_unlock(&imp->imp_lock);
169 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
170 "level %s/%u force %u deactive %u pingable %u\n",
171 ptlrpc_import_state_name(level), level,
172 force, imp->imp_deactive, imp->imp_pingable);
174 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK,
175 this_ping) && force == 0)
178 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
179 /* wait at least a timeout before trying recovery again */
180 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
181 ptlrpc_initiate_recovery(imp);
182 } else if (level != LUSTRE_IMP_FULL ||
183 imp->imp_obd->obd_no_recov ||
185 CDEBUG(D_HA, "not pinging %s (in recovery "
186 " or recovery disabled: %s)\n",
187 obd2cli_tgt(imp->imp_obd),
188 ptlrpc_import_state_name(level));
189 } else if (imp->imp_pingable || force) {
194 static int ptlrpc_pinger_main(void *arg)
196 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
197 struct ptlrpc_thread *thread = data->thread;
200 cfs_daemonize(data->name);
202 /* Record that the thread is running */
203 thread->t_flags = SVC_RUNNING;
204 cfs_waitq_signal(&thread->t_ctl_waitq);
206 /* And now, loop forever, pinging as needed. */
208 cfs_time_t this_ping = cfs_time_current();
209 struct l_wait_info lwi;
210 cfs_duration_t time_to_next_ping;
211 struct list_head *iter;
213 mutex_down(&pinger_sem);
214 list_for_each(iter, &pinger_imports) {
215 struct obd_import *imp =
216 list_entry(iter, struct obd_import,
219 ptlrpc_pinger_process_import(imp, this_ping);
220 /* obd_timeout might have changed */
221 if (imp->imp_pingable && imp->imp_next_ping &&
222 cfs_time_after(imp->imp_next_ping,
223 cfs_time_add(this_ping,
224 cfs_time_seconds(PING_INTERVAL))))
225 ptlrpc_update_next_ping(imp);
227 mutex_up(&pinger_sem);
228 /* update memory usage info */
229 obd_update_maxusage();
231 /* Wait until the next ping time, or until we're stopped. */
232 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
233 cfs_time_seconds(PING_INTERVAL)),
236 /* The ping sent by ptlrpc_send_rpc may get sent out
237 say .01 second after this.
238 ptlrpc_pinger_sending_on_import will then set the
239 next ping time to next_ping + .01 sec, which means
240 we will SKIP the next ping at next_ping, and the
241 ping will get sent 2 timeouts from now! Beware. */
242 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
244 cfs_time_add(this_ping,
245 cfs_time_seconds(PING_INTERVAL)));
246 if (time_to_next_ping > 0) {
247 lwi = LWI_TIMEOUT(max_t(cfs_duration_t,
249 cfs_time_seconds(1)),
251 l_wait_event(thread->t_ctl_waitq,
252 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
254 if (thread->t_flags & SVC_STOPPING) {
255 thread->t_flags &= ~SVC_STOPPING;
258 } else if (thread->t_flags & SVC_EVENT) {
259 /* woken after adding import to reset timer */
260 thread->t_flags &= ~SVC_EVENT;
265 thread->t_flags = SVC_STOPPED;
266 cfs_waitq_signal(&thread->t_ctl_waitq);
268 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
272 static struct ptlrpc_thread *pinger_thread = NULL;
274 int ptlrpc_start_pinger(void)
276 struct l_wait_info lwi = { 0 };
277 struct ptlrpc_svc_data d;
279 #ifndef ENABLE_PINGER
284 if (pinger_thread != NULL)
287 OBD_ALLOC_PTR(pinger_thread);
288 if (pinger_thread == NULL)
290 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
291 cfs_waitq_init(&suspend_timeouts_waitq);
294 d.thread = pinger_thread;
296 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
297 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
298 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
300 CERROR("cannot start thread: %d\n", rc);
301 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
302 pinger_thread = NULL;
305 l_wait_event(pinger_thread->t_ctl_waitq,
306 pinger_thread->t_flags & SVC_RUNNING, &lwi);
311 int ptlrpc_stop_pinger(void)
313 struct l_wait_info lwi = { 0 };
315 #ifndef ENABLE_PINGER
320 if (pinger_thread == NULL)
322 mutex_down(&pinger_sem);
323 pinger_thread->t_flags = SVC_STOPPING;
324 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
325 mutex_up(&pinger_sem);
327 l_wait_event(pinger_thread->t_ctl_waitq,
328 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
330 OBD_FREE_PTR(pinger_thread);
331 pinger_thread = NULL;
335 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
337 ptlrpc_update_next_ping(imp);
340 int ptlrpc_pinger_add_import(struct obd_import *imp)
343 if (!list_empty(&imp->imp_pinger_chain))
346 mutex_down(&pinger_sem);
347 CDEBUG(D_HA, "adding pingable import %s->%s\n",
348 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
349 /* if we add to pinger we want recovery on this import */
350 imp->imp_obd->obd_no_recov = 0;
351 ptlrpc_update_next_ping(imp);
352 /* XXX sort, blah blah */
353 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
354 class_import_get(imp);
356 ptlrpc_pinger_wake_up();
357 mutex_up(&pinger_sem);
362 int ptlrpc_pinger_del_import(struct obd_import *imp)
365 if (list_empty(&imp->imp_pinger_chain))
368 mutex_down(&pinger_sem);
369 list_del_init(&imp->imp_pinger_chain);
370 CDEBUG(D_HA, "removing pingable import %s->%s\n",
371 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
372 /* if we remove from pinger we don't want recovery on this import */
373 imp->imp_obd->obd_no_recov = 1;
374 class_import_put(imp);
375 mutex_up(&pinger_sem);
379 void ptlrpc_pinger_wake_up()
382 pinger_thread->t_flags |= SVC_EVENT;
383 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
387 /* Ping evictor thread */
389 #define PET_TERMINATE 2
391 static int pet_refcount = 0;
392 static int pet_state;
393 static wait_queue_head_t pet_waitq;
394 static struct obd_export *pet_exp = NULL;
395 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
397 int ping_evictor_wake(struct obd_export *exp)
399 spin_lock(&pet_lock);
400 if (pet_exp || (pet_state != PET_READY)) {
401 /* eventually the new obd will call here again. */
402 spin_unlock(&pet_lock);
406 /* We have to make sure the obd isn't destroyed between now and when
407 * the ping evictor runs. We'll take a reference here, and drop it
408 * when we finish in the evictor. We don't really care about this
409 * export in particular; we just need one to keep the obd alive. */
410 pet_exp = class_export_get(exp);
411 spin_unlock(&pet_lock);
417 static int ping_evictor_main(void *arg)
419 struct obd_device *obd;
420 struct obd_export *exp;
421 struct l_wait_info lwi = { 0 };
425 ptlrpc_daemonize("ll_evictor");
427 CDEBUG(D_HA, "Starting Ping Evictor\n");
429 pet_state = PET_READY;
431 l_wait_event(pet_waitq, pet_exp ||
432 (pet_state == PET_TERMINATE), &lwi);
433 if (pet_state == PET_TERMINATE)
436 /* we only get here if pet_exp != NULL, and the end of this
437 * loop is the only place which sets it NULL again, so lock
438 * is not strictly necessary. */
439 spin_lock(&pet_lock);
440 obd = pet_exp->exp_obd;
441 spin_unlock(&pet_lock);
443 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
445 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
446 obd->obd_name, expire_time);
448 /* Exports can't be deleted out of the list while we hold
449 * the obd lock (class_unlink_export), which means we can't
450 * lose the last ref on the export. If they've already been
451 * removed from the list, we won't find them here. */
452 spin_lock(&obd->obd_dev_lock);
453 while (!list_empty(&obd->obd_exports_timed)) {
454 exp = list_entry(obd->obd_exports_timed.next,
455 struct obd_export,exp_obd_chain_timed);
456 if (expire_time > exp->exp_last_request_time) {
457 class_export_get(exp);
458 spin_unlock(&obd->obd_dev_lock);
459 LCONSOLE_WARN("%s: haven't heard from client %s"
460 " (at %s) in %ld seconds. I think"
461 " it's dead, and I am evicting"
462 " it. exp %p, cur %ld expire %ld"
465 obd_uuid2str(&exp->exp_client_uuid),
466 obd_export_nid2str(exp),
467 (long)(cfs_time_current_sec() -
468 exp->exp_last_request_time),
469 exp, (long)cfs_time_current_sec(),
471 (long)exp->exp_last_request_time);
472 CDEBUG(D_HA, "Last request was at %ld\n",
473 exp->exp_last_request_time);
474 class_fail_export(exp);
475 class_export_put(exp);
476 spin_lock(&obd->obd_dev_lock);
478 /* List is sorted, so everyone below is ok */
482 spin_unlock(&obd->obd_dev_lock);
484 class_export_put(pet_exp);
486 spin_lock(&pet_lock);
488 spin_unlock(&pet_lock);
490 CDEBUG(D_HA, "Exiting Ping Evictor\n");
495 void ping_evictor_start(void)
499 if (++pet_refcount > 1)
502 init_waitqueue_head(&pet_waitq);
504 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
507 CERROR("Cannot start ping evictor thread: %d\n", rc);
510 EXPORT_SYMBOL(ping_evictor_start);
512 void ping_evictor_stop(void)
514 if (--pet_refcount > 0)
517 pet_state = PET_TERMINATE;
520 EXPORT_SYMBOL(ping_evictor_stop);
521 #else /* !__KERNEL__ */
524 * the current implementation of pinger in liblustre is not optimized
528 static struct pinger_data {
530 cfs_time_t pd_this_ping; /* jiffies */
531 cfs_time_t pd_next_ping; /* jiffies */
532 struct ptlrpc_request_set *pd_set;
535 static int pinger_check_rpcs(void *arg)
537 cfs_time_t curtime = cfs_time_current();
538 struct ptlrpc_request *req;
539 struct ptlrpc_request_set *set;
540 struct list_head *iter;
541 struct pinger_data *pd = &pinger_args;
544 /* prevent recursion */
545 if (pd->pd_recursion++) {
546 CDEBUG(D_HA, "pinger: recursion! quit\n");
552 /* have we reached ping point? */
553 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
558 /* if we have rpc_set already, continue processing it */
560 LASSERT(pd->pd_this_ping);
565 pd->pd_this_ping = curtime;
566 pd->pd_set = ptlrpc_prep_set();
567 if (pd->pd_set == NULL)
571 /* add rpcs into set */
572 mutex_down(&pinger_sem);
573 list_for_each(iter, &pinger_imports) {
574 struct obd_import *imp =
575 list_entry(iter, struct obd_import, imp_pinger_chain);
576 int generation, level;
578 if (cfs_time_aftereq(pd->pd_this_ping,
579 imp->imp_next_ping - 5 * CFS_TICK)) {
581 spin_lock(&imp->imp_lock);
582 generation = imp->imp_generation;
583 level = imp->imp_state;
584 spin_unlock(&imp->imp_lock);
586 if (level != LUSTRE_IMP_FULL) {
588 "not pinging %s (in recovery)\n",
589 obd2cli_tgt(imp->imp_obd));
593 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
597 CERROR("OOM trying to ping %s->%s\n",
598 imp->imp_obd->obd_uuid.uuid,
599 obd2cli_tgt(imp->imp_obd));
603 req->rq_no_resend = 1;
604 ptlrpc_request_set_replen(req);
605 req->rq_send_state = LUSTRE_IMP_FULL;
606 req->rq_phase = RQ_PHASE_RPC;
607 req->rq_import_generation = generation;
608 ptlrpc_set_add_req(set, req);
610 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
611 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
612 imp->imp_next_ping, pd->pd_this_ping);
615 pd->pd_this_ping = curtime;
616 mutex_up(&pinger_sem);
618 /* Might be empty, that's OK. */
619 if (set->set_remaining == 0)
620 CDEBUG(D_RPCTRACE, "nothing to ping\n");
622 list_for_each(iter, &set->set_requests) {
623 struct ptlrpc_request *req =
624 list_entry(iter, struct ptlrpc_request,
626 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
627 req->rq_import->imp_obd->obd_uuid.uuid,
628 obd2cli_tgt(req->rq_import->imp_obd));
629 (void)ptl_send_rpc(req, 0);
633 rc = ptlrpc_check_set(set);
635 /* not finished, and we are not expired, simply return */
636 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
637 cfs_time_seconds(PING_INTERVAL)))) {
638 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
643 /* Expire all the requests that didn't come back. */
644 mutex_down(&pinger_sem);
645 list_for_each(iter, &set->set_requests) {
646 req = list_entry(iter, struct ptlrpc_request,
649 if (req->rq_phase == RQ_PHASE_COMPLETE)
652 req->rq_phase = RQ_PHASE_COMPLETE;
653 atomic_dec(&req->rq_import->imp_inflight);
654 set->set_remaining--;
655 /* If it was disconnected, don't sweat it. */
656 if (list_empty(&req->rq_import->imp_pinger_chain)) {
657 ptlrpc_unregister_reply(req);
661 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
662 ptlrpc_expire_one_request(req);
664 mutex_up(&pinger_sem);
666 ptlrpc_set_destroy(set);
670 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
671 cfs_time_seconds(PING_INTERVAL));
672 pd->pd_this_ping = 0; /* XXX for debug */
674 CDEBUG(D_INFO, "finished a round ping\n");
679 static void *pinger_callback = NULL;
680 #endif /* ENABLE_PINGER */
682 int ptlrpc_start_pinger(void)
685 memset(&pinger_args, 0, sizeof(pinger_args));
686 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
693 int ptlrpc_stop_pinger(void)
697 liblustre_deregister_wait_callback(pinger_callback);
702 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
705 mutex_down(&pinger_sem);
706 ptlrpc_update_next_ping(imp);
707 if (pinger_args.pd_set == NULL &&
708 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
709 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
710 imp->imp_next_ping, cfs_time_current());
711 pinger_args.pd_next_ping = imp->imp_next_ping;
713 mutex_up(&pinger_sem);
717 int ptlrpc_pinger_add_import(struct obd_import *imp)
720 if (!list_empty(&imp->imp_pinger_chain))
723 CDEBUG(D_HA, "adding pingable import %s->%s\n",
724 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
725 ptlrpc_pinger_sending_on_import(imp);
727 mutex_down(&pinger_sem);
728 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
729 class_import_get(imp);
730 mutex_up(&pinger_sem);
735 int ptlrpc_pinger_del_import(struct obd_import *imp)
738 if (list_empty(&imp->imp_pinger_chain))
741 mutex_down(&pinger_sem);
742 list_del_init(&imp->imp_pinger_chain);
743 CDEBUG(D_HA, "removing pingable import %s->%s\n",
744 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
745 class_import_put(imp);
746 mutex_up(&pinger_sem);
750 void ptlrpc_pinger_wake_up()
753 /* XXX force pinger to run, if needed */
754 struct obd_import *imp;
756 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
757 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
758 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
759 #ifdef ENABLE_LIBLUSTRE_RECOVERY
760 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
762 /*XXX only recover for the initial connection */
763 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
764 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
766 ptlrpc_initiate_recovery(imp);
767 else if (imp->imp_state != LUSTRE_IMP_FULL)
768 CDEBUG(D_HA, "Refused to recover import %s->%s "
769 "state %d, deactive %d\n",
770 imp->imp_obd->obd_uuid.uuid,
771 obd2cli_tgt(imp->imp_obd), imp->imp_state,
777 #endif /* !__KERNEL__ */