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 struct list_head pinger_imports = CFS_LIST_HEAD_INIT(pinger_imports);
42 int ptlrpc_ping(struct obd_import *imp)
44 struct ptlrpc_request *req;
48 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
51 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
52 imp->imp_obd->obd_uuid.uuid,
53 obd2cli_tgt(imp->imp_obd));
54 req->rq_no_resend = req->rq_no_delay = 1;
55 ptlrpc_req_set_repsize(req, 1, NULL);
58 CERROR("OOM trying to ping %s->%s\n",
59 imp->imp_obd->obd_uuid.uuid,
60 obd2cli_tgt(imp->imp_obd));
67 void ptlrpc_update_next_ping(struct obd_import *imp)
70 int time = PING_INTERVAL;
71 if (imp->imp_state == LUSTRE_IMP_DISCON) {
72 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
73 at_get(&imp->imp_at.iat_net_latency));
74 time = min(time, dtime);
76 imp->imp_next_ping = cfs_time_shift(time);
77 #endif /* ENABLE_PINGER */
80 void ptlrpc_ping_import_soon(struct obd_import *imp)
82 imp->imp_next_ping = cfs_time_current();
86 static int ptlrpc_pinger_main(void *arg)
88 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
89 struct ptlrpc_thread *thread = data->thread;
92 cfs_daemonize(data->name);
94 /* Record that the thread is running */
95 thread->t_flags = SVC_RUNNING;
96 cfs_waitq_signal(&thread->t_ctl_waitq);
98 /* And now, loop forever, pinging as needed. */
100 cfs_time_t this_ping = cfs_time_current();
101 struct l_wait_info lwi;
102 cfs_duration_t time_to_next_ping;
103 struct list_head *iter;
105 mutex_down(&pinger_sem);
106 list_for_each(iter, &pinger_imports) {
107 struct obd_import *imp =
108 list_entry(iter, struct obd_import,
112 spin_lock(&imp->imp_lock);
113 level = imp->imp_state;
114 force = imp->imp_force_verify;
115 imp->imp_force_verify = 0;
116 spin_unlock(&imp->imp_lock);
118 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_RPCTRACE,
119 "level %s/%u force %u deactive %u pingable %u\n",
120 ptlrpc_import_state_name(level), level,
121 force, imp->imp_deactive, imp->imp_pingable);
124 /* if the next ping is within, say, 5 jiffies from
125 now, go ahead and ping. See note below. */
126 cfs_time_aftereq(this_ping,
127 imp->imp_next_ping - 5 * CFS_TICK)) {
128 if (level == LUSTRE_IMP_DISCON &&
129 !imp->imp_deactive) {
130 /* wait at least a timeout before
131 trying recovery again. */
132 imp->imp_next_ping = cfs_time_shift(obd_timeout);
133 ptlrpc_initiate_recovery(imp);
134 } else if (level != LUSTRE_IMP_FULL ||
135 imp->imp_obd->obd_no_recov ||
137 CDEBUG(D_HA, "not pinging %s "
138 "(in recovery: %s or recovery "
139 "disabled: %u/%u)\n",
140 obd2cli_tgt(imp->imp_obd),
141 ptlrpc_import_state_name(level),
143 imp->imp_obd->obd_no_recov);
144 } else if (imp->imp_pingable || force) {
148 if (!imp->imp_pingable)
151 "don't need to ping %s ("CFS_TIME_T
152 " > "CFS_TIME_T")\n",
153 obd2cli_tgt(imp->imp_obd),
154 imp->imp_next_ping, this_ping);
157 /* obd_timeout might have changed */
158 if (cfs_time_after(imp->imp_next_ping,
159 cfs_time_add(this_ping,
160 cfs_time_seconds(PING_INTERVAL))))
161 ptlrpc_update_next_ping(imp);
163 mutex_up(&pinger_sem);
164 /* update memory usage info */
165 obd_update_maxusage();
167 /* Wait until the next ping time, or until we're stopped. */
168 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
169 cfs_time_seconds(PING_INTERVAL)),
172 /* The ping sent by ptlrpc_send_rpc may get sent out
173 say .01 second after this.
174 ptlrpc_pinger_eending_on_import will then set the
175 next ping time to next_ping + .01 sec, which means
176 we will SKIP the next ping at next_ping, and the
177 ping will get sent 2 timeouts from now! Beware. */
178 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
180 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
181 if (time_to_next_ping > 0) {
182 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
184 l_wait_event(thread->t_ctl_waitq,
185 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
187 if (thread->t_flags & SVC_STOPPING) {
188 thread->t_flags &= ~SVC_STOPPING;
191 } else if (thread->t_flags & SVC_EVENT) {
192 /* woken after adding import to reset timer */
193 thread->t_flags &= ~SVC_EVENT;
198 thread->t_flags = SVC_STOPPED;
199 cfs_waitq_signal(&thread->t_ctl_waitq);
201 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
205 static struct ptlrpc_thread *pinger_thread = NULL;
207 int ptlrpc_start_pinger(void)
209 struct l_wait_info lwi = { 0 };
210 struct ptlrpc_svc_data d;
212 #ifndef ENABLE_PINGER
217 if (pinger_thread != NULL)
220 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
221 if (pinger_thread == NULL)
223 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
226 d.thread = pinger_thread;
228 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
229 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
230 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
232 CERROR("cannot start thread: %d\n", rc);
233 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
234 pinger_thread = NULL;
237 l_wait_event(pinger_thread->t_ctl_waitq,
238 pinger_thread->t_flags & SVC_RUNNING, &lwi);
243 int ptlrpc_stop_pinger(void)
245 struct l_wait_info lwi = { 0 };
247 #ifndef ENABLE_PINGER
252 if (pinger_thread == NULL)
254 mutex_down(&pinger_sem);
255 pinger_thread->t_flags = SVC_STOPPING;
256 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
257 mutex_up(&pinger_sem);
259 l_wait_event(pinger_thread->t_ctl_waitq,
260 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
262 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
263 pinger_thread = NULL;
267 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
269 ptlrpc_update_next_ping(imp);
272 int ptlrpc_pinger_add_import(struct obd_import *imp)
275 if (!list_empty(&imp->imp_pinger_chain))
278 mutex_down(&pinger_sem);
279 CDEBUG(D_HA, "adding pingable import %s->%s\n",
280 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
281 /* if we add to pinger we want recovery on this import */
282 imp->imp_obd->obd_no_recov = 0;
284 ptlrpc_update_next_ping(imp);
285 /* XXX sort, blah blah */
286 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
287 class_import_get(imp);
289 ptlrpc_pinger_wake_up();
290 mutex_up(&pinger_sem);
295 int ptlrpc_pinger_del_import(struct obd_import *imp)
298 if (list_empty(&imp->imp_pinger_chain))
301 mutex_down(&pinger_sem);
302 list_del_init(&imp->imp_pinger_chain);
303 CDEBUG(D_HA, "removing pingable import %s->%s\n",
304 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
305 /* if we remove from pinger we don't want recovery on this import */
306 imp->imp_obd->obd_no_recov = 1;
307 class_import_put(imp);
308 mutex_up(&pinger_sem);
312 void ptlrpc_pinger_wake_up()
315 pinger_thread->t_flags |= SVC_EVENT;
316 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
320 /* Ping evictor thread */
322 #define PET_TERMINATE 2
324 static int pet_refcount = 0;
325 static int pet_state;
326 static wait_queue_head_t pet_waitq;
327 static struct obd_export *pet_exp = NULL;
328 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
330 int ping_evictor_wake(struct obd_export *exp)
332 spin_lock(&pet_lock);
334 /* eventually the new obd will call here again. */
335 spin_unlock(&pet_lock);
339 /* We have to make sure the obd isn't destroyed between now and when
340 * the ping evictor runs. We'll take a reference here, and drop it
341 * when we finish in the evictor. We don't really care about this
342 * export in particular; we just need one to keep the obd alive. */
343 pet_exp = class_export_get(exp);
344 spin_unlock(&pet_lock);
350 static int ping_evictor_main(void *arg)
352 struct obd_device *obd;
353 struct obd_export *exp;
354 struct l_wait_info lwi = { 0 };
358 ptlrpc_daemonize("ll_evictor");
360 CDEBUG(D_HA, "Starting Ping Evictor\n");
362 pet_state = PET_READY;
364 l_wait_event(pet_waitq, pet_exp ||
365 (pet_state == PET_TERMINATE), &lwi);
366 if (pet_state == PET_TERMINATE)
369 /* we only get here if pet_exp != NULL, and the end of this
370 * loop is the only place which sets it NULL again, so lock
371 * is not strictly necessary. */
372 spin_lock(&pet_lock);
373 obd = pet_exp->exp_obd;
374 spin_unlock(&pet_lock);
376 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
378 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
379 obd->obd_name, expire_time);
381 /* Exports can't be deleted out of the list while we hold
382 * the obd lock (class_unlink_export), which means we can't
383 * lose the last ref on the export. If they've already been
384 * removed from the list, we won't find them here. */
385 spin_lock(&obd->obd_dev_lock);
386 while (!list_empty(&obd->obd_exports_timed)) {
387 exp = list_entry(obd->obd_exports_timed.next,
388 struct obd_export,exp_obd_chain_timed);
389 if (expire_time > exp->exp_last_request_time) {
390 class_export_get(exp);
391 spin_unlock(&obd->obd_dev_lock);
392 LCONSOLE_WARN("%s: haven't heard from client %s"
393 " (at %s) in %ld seconds. I think"
394 " it's dead, and I am evicting"
395 " it.\n", obd->obd_name,
396 obd_uuid2str(&exp->exp_client_uuid),
397 obd_export_nid2str(exp),
398 (long)(cfs_time_current_sec() -
399 exp->exp_last_request_time));
400 CDEBUG(D_HA, "Last request was at %ld\n",
401 exp->exp_last_request_time);
402 class_fail_export(exp);
403 class_export_put(exp);
404 spin_lock(&obd->obd_dev_lock);
406 /* List is sorted, so everyone below is ok */
410 spin_unlock(&obd->obd_dev_lock);
412 class_export_put(pet_exp);
414 spin_lock(&pet_lock);
416 spin_unlock(&pet_lock);
418 CDEBUG(D_HA, "Exiting Ping Evictor\n");
423 void ping_evictor_start(void)
427 if (++pet_refcount > 1)
430 init_waitqueue_head(&pet_waitq);
432 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
435 CERROR("Cannot start ping evictor thread: %d\n", rc);
438 EXPORT_SYMBOL(ping_evictor_start);
440 void ping_evictor_stop(void)
442 if (--pet_refcount > 0)
445 pet_state = PET_TERMINATE;
448 EXPORT_SYMBOL(ping_evictor_stop);
449 #else /* !__KERNEL__ */
452 * the current implementation of pinger in liblustre is not optimized
456 static struct pinger_data {
458 cfs_time_t pd_this_ping; /* jiffies */
459 cfs_time_t pd_next_ping; /* jiffies */
460 struct ptlrpc_request_set *pd_set;
463 static int pinger_check_rpcs(void *arg)
465 cfs_time_t curtime = cfs_time_current();
466 struct ptlrpc_request *req;
467 struct ptlrpc_request_set *set;
468 struct list_head *iter;
469 struct pinger_data *pd = &pinger_args;
472 /* prevent recursion */
473 if (pd->pd_recursion++) {
474 CDEBUG(D_HA, "pinger: recursion! quit\n");
480 /* have we reached ping point? */
481 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
486 /* if we have rpc_set already, continue processing it */
488 LASSERT(pd->pd_this_ping);
493 pd->pd_this_ping = curtime;
494 pd->pd_set = ptlrpc_prep_set();
495 if (pd->pd_set == NULL)
499 /* add rpcs into set */
500 mutex_down(&pinger_sem);
501 list_for_each(iter, &pinger_imports) {
502 struct obd_import *imp =
503 list_entry(iter, struct obd_import, imp_pinger_chain);
504 int generation, level;
506 if (cfs_time_aftereq(pd->pd_this_ping,
507 imp->imp_next_ping - 5 * CFS_TICK)) {
509 spin_lock(&imp->imp_lock);
510 generation = imp->imp_generation;
511 level = imp->imp_state;
512 spin_unlock(&imp->imp_lock);
514 if (level != LUSTRE_IMP_FULL) {
516 "not pinging %s (in recovery)\n",
517 obd2cli_tgt(imp->imp_obd));
521 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
524 CERROR("out of memory\n");
527 req->rq_no_resend = 1;
528 ptlrpc_req_set_repsize(req, 1, NULL);
529 req->rq_send_state = LUSTRE_IMP_FULL;
530 req->rq_phase = RQ_PHASE_RPC;
531 req->rq_import_generation = generation;
532 ptlrpc_set_add_req(set, req);
534 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
535 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
536 imp->imp_next_ping, pd->pd_this_ping);
539 pd->pd_this_ping = curtime;
540 mutex_up(&pinger_sem);
542 /* Might be empty, that's OK. */
543 if (set->set_remaining == 0)
544 CDEBUG(D_RPCTRACE, "nothing to ping\n");
546 list_for_each(iter, &set->set_requests) {
547 struct ptlrpc_request *req =
548 list_entry(iter, struct ptlrpc_request,
550 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
551 req->rq_import->imp_obd->obd_uuid.uuid,
552 obd2cli_tgt(req->rq_import->imp_obd));
553 (void)ptl_send_rpc(req, 0);
557 rc = ptlrpc_check_set(set);
559 /* not finished, and we are not expired, simply return */
560 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
561 cfs_time_seconds(PING_INTERVAL)))) {
562 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
567 /* Expire all the requests that didn't come back. */
568 mutex_down(&pinger_sem);
569 list_for_each(iter, &set->set_requests) {
570 req = list_entry(iter, struct ptlrpc_request,
573 if (req->rq_phase == RQ_PHASE_COMPLETE)
576 req->rq_phase = RQ_PHASE_COMPLETE;
577 atomic_dec(&req->rq_import->imp_inflight);
578 set->set_remaining--;
579 /* If it was disconnected, don't sweat it. */
580 if (list_empty(&req->rq_import->imp_pinger_chain)) {
581 ptlrpc_unregister_reply(req);
585 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
586 ptlrpc_expire_one_request(req);
588 mutex_up(&pinger_sem);
590 ptlrpc_set_destroy(set);
594 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
595 cfs_time_seconds(PING_INTERVAL));
596 pd->pd_this_ping = 0; /* XXX for debug */
598 CDEBUG(D_INFO, "finished a round ping\n");
603 static void *pinger_callback = NULL;
604 #endif /* ENABLE_PINGER */
606 int ptlrpc_start_pinger(void)
609 memset(&pinger_args, 0, sizeof(pinger_args));
610 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
617 int ptlrpc_stop_pinger(void)
621 liblustre_deregister_wait_callback(pinger_callback);
626 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
629 mutex_down(&pinger_sem);
630 ptlrpc_update_next_ping(imp);
631 if (pinger_args.pd_set == NULL &&
632 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
633 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
634 imp->imp_next_ping, cfs_time_current());
635 pinger_args.pd_next_ping = imp->imp_next_ping;
637 mutex_up(&pinger_sem);
641 int ptlrpc_pinger_add_import(struct obd_import *imp)
644 if (!list_empty(&imp->imp_pinger_chain))
647 CDEBUG(D_HA, "adding pingable import %s->%s\n",
648 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
649 ptlrpc_pinger_sending_on_import(imp);
651 mutex_down(&pinger_sem);
652 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
653 class_import_get(imp);
654 mutex_up(&pinger_sem);
659 int ptlrpc_pinger_del_import(struct obd_import *imp)
662 if (list_empty(&imp->imp_pinger_chain))
665 mutex_down(&pinger_sem);
666 list_del_init(&imp->imp_pinger_chain);
667 CDEBUG(D_HA, "removing pingable import %s->%s\n",
668 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
669 class_import_put(imp);
670 mutex_up(&pinger_sem);
674 void ptlrpc_pinger_wake_up()
678 /* XXX force pinger to run, if needed */
679 struct obd_import *imp;
680 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
681 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
682 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
683 #ifdef ENABLE_LIBLUSTRE_RECOVERY
684 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
686 /*XXX only recover for the initial connection */
687 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
688 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
690 ptlrpc_initiate_recovery(imp);
691 else if (imp->imp_state != LUSTRE_IMP_FULL)
692 CDEBUG(D_HA, "Refused to recover import %s->%s "
693 "state %d, deactive %d\n",
694 imp->imp_obd->obd_uuid.uuid,
695 obd2cli_tgt(imp->imp_obd), imp->imp_state,
701 #endif /* !__KERNEL__ */