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, 0, NULL, NULL);
50 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
51 imp->imp_obd->obd_uuid.uuid,
52 obd2cli_tgt(imp->imp_obd));
53 req->rq_no_resend = req->rq_no_delay = 1;
54 req->rq_replen = lustre_msg_size(0, NULL);
57 CERROR("OOM trying to ping %s->%s\n",
58 imp->imp_obd->obd_uuid.uuid,
59 obd2cli_tgt(imp->imp_obd));
66 static void ptlrpc_update_next_ping(struct obd_import *imp)
68 imp->imp_next_ping = cfs_time_shift(
69 (imp->imp_state == LUSTRE_IMP_DISCON ?
70 RECONNECT_INTERVAL : PING_INTERVAL));
73 void ptlrpc_ping_import_soon(struct obd_import *imp)
75 imp->imp_next_ping = cfs_time_current();
79 static int ptlrpc_pinger_main(void *arg)
81 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
82 struct ptlrpc_thread *thread = data->thread;
85 cfs_daemonize(data->name);
87 /* Record that the thread is running */
88 thread->t_flags = SVC_RUNNING;
89 cfs_waitq_signal(&thread->t_ctl_waitq);
91 /* And now, loop forever, pinging as needed. */
93 cfs_time_t this_ping = cfs_time_current();
94 struct l_wait_info lwi;
95 cfs_duration_t time_to_next_ping;
96 struct list_head *iter;
98 mutex_down(&pinger_sem);
99 list_for_each(iter, &pinger_imports) {
100 struct obd_import *imp =
101 list_entry(iter, struct obd_import,
106 spin_lock_irqsave(&imp->imp_lock, flags);
107 level = imp->imp_state;
108 force = imp->imp_force_verify;
109 imp->imp_force_verify = 0;
110 spin_unlock_irqrestore(&imp->imp_lock, flags);
112 CDEBUG_EX(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
113 "level %s/%u force %u deactive %u pingable %u\n",
114 ptlrpc_import_state_name(level), level,
115 force, imp->imp_deactive, imp->imp_pingable);
118 /* if the next ping is within, say, 5 jiffies from
119 now, go ahead and ping. See note below. */
120 cfs_time_aftereq(this_ping,
121 imp->imp_next_ping - 5 * CFS_TICK)) {
122 if (level == LUSTRE_IMP_DISCON &&
123 !imp->imp_deactive) {
124 /* wait at least a timeout before
125 trying recovery again. */
126 imp->imp_next_ping = cfs_time_shift(obd_timeout);
127 ptlrpc_initiate_recovery(imp);
128 } else if (level != LUSTRE_IMP_FULL ||
129 imp->imp_obd->obd_no_recov ||
131 CDEBUG(D_HA, "not pinging %s "
132 "(in recovery: %s or recovery "
133 "disabled: %u/%u)\n",
134 obd2cli_tgt(imp->imp_obd),
135 ptlrpc_import_state_name(level),
137 imp->imp_obd->obd_no_recov);
138 } else if (imp->imp_pingable || force) {
142 if (!imp->imp_pingable)
145 "don't need to ping %s ("CFS_TIME_T
146 " > "CFS_TIME_T")\n",
147 obd2cli_tgt(imp->imp_obd),
148 imp->imp_next_ping, this_ping);
151 /* obd_timeout might have changed */
152 if (cfs_time_after(imp->imp_next_ping,
153 cfs_time_add(this_ping,
154 cfs_time_seconds(PING_INTERVAL))))
155 ptlrpc_update_next_ping(imp);
157 mutex_up(&pinger_sem);
159 /* Wait until the next ping time, or until we're stopped. */
160 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
161 cfs_time_seconds(PING_INTERVAL)),
164 /* The ping sent by ptlrpc_send_rpc may get sent out
165 say .01 second after this.
166 ptlrpc_pinger_eending_on_import will then set the
167 next ping time to next_ping + .01 sec, which means
168 we will SKIP the next ping at next_ping, and the
169 ping will get sent 2 timeouts from now! Beware. */
170 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
172 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
173 if (time_to_next_ping > 0) {
174 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
176 l_wait_event(thread->t_ctl_waitq,
177 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
179 if (thread->t_flags & SVC_STOPPING) {
180 thread->t_flags &= ~SVC_STOPPING;
183 } else if (thread->t_flags & SVC_EVENT) {
184 /* woken after adding import to reset timer */
185 thread->t_flags &= ~SVC_EVENT;
190 thread->t_flags = SVC_STOPPED;
191 cfs_waitq_signal(&thread->t_ctl_waitq);
193 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
197 static struct ptlrpc_thread *pinger_thread = NULL;
199 int ptlrpc_start_pinger(void)
201 struct l_wait_info lwi = { 0 };
202 struct ptlrpc_svc_data d;
204 #ifndef ENABLE_PINGER
209 if (pinger_thread != NULL)
212 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
213 if (pinger_thread == NULL)
215 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
218 d.thread = pinger_thread;
220 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
221 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
222 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
224 CERROR("cannot start thread: %d\n", rc);
225 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
226 pinger_thread = NULL;
229 l_wait_event(pinger_thread->t_ctl_waitq,
230 pinger_thread->t_flags & SVC_RUNNING, &lwi);
235 int ptlrpc_stop_pinger(void)
237 struct l_wait_info lwi = { 0 };
239 #ifndef ENABLE_PINGER
244 if (pinger_thread == NULL)
246 mutex_down(&pinger_sem);
247 pinger_thread->t_flags = SVC_STOPPING;
248 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
249 mutex_up(&pinger_sem);
251 l_wait_event(pinger_thread->t_ctl_waitq,
252 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
254 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
255 pinger_thread = NULL;
259 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
261 ptlrpc_update_next_ping(imp);
264 int ptlrpc_pinger_add_import(struct obd_import *imp)
267 if (!list_empty(&imp->imp_pinger_chain))
270 mutex_down(&pinger_sem);
271 CDEBUG(D_HA, "adding pingable import %s->%s\n",
272 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
273 ptlrpc_update_next_ping(imp);
274 /* XXX sort, blah blah */
275 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
276 class_import_get(imp);
278 ptlrpc_pinger_wake_up();
279 mutex_up(&pinger_sem);
284 int ptlrpc_pinger_del_import(struct obd_import *imp)
287 if (list_empty(&imp->imp_pinger_chain))
290 mutex_down(&pinger_sem);
291 list_del_init(&imp->imp_pinger_chain);
292 CDEBUG(D_HA, "removing pingable import %s->%s\n",
293 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
294 class_import_put(imp);
295 mutex_up(&pinger_sem);
299 void ptlrpc_pinger_wake_up()
302 pinger_thread->t_flags |= SVC_EVENT;
303 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
307 /* Ping evictor thread */
309 #define PET_TERMINATE 2
311 static int pet_refcount = 0;
312 static int pet_state;
313 static wait_queue_head_t pet_waitq;
314 static struct obd_export *pet_exp = NULL;
315 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
317 int ping_evictor_wake(struct obd_export *exp)
319 spin_lock(&pet_lock);
321 /* eventually the new obd will call here again. */
322 spin_unlock(&pet_lock);
326 /* We have to make sure the obd isn't destroyed between now and when
327 * the ping evictor runs. We'll take a reference here, and drop it
328 * when we finish in the evictor. We don't really care about this
329 * export in particular; we just need one to keep the obd alive. */
330 pet_exp = class_export_get(exp);
331 spin_unlock(&pet_lock);
337 static int ping_evictor_main(void *arg)
339 struct obd_device *obd;
340 struct obd_export *exp;
341 struct l_wait_info lwi = { 0 };
345 ptlrpc_daemonize("ll_evictor");
347 CDEBUG(D_HA, "Starting Ping Evictor\n");
349 pet_state = PET_READY;
351 l_wait_event(pet_waitq, pet_exp ||
352 (pet_state == PET_TERMINATE), &lwi);
353 if (pet_state == PET_TERMINATE)
356 /* we only get here if pet_exp != NULL, and the end of this
357 * loop is the only place which sets it NULL again, so lock
358 * is not strictly necessary. */
359 spin_lock(&pet_lock);
360 obd = pet_exp->exp_obd;
361 spin_unlock(&pet_lock);
363 expire_time = CURRENT_SECONDS - (3 * obd_timeout / 2);
365 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
366 obd->obd_name, expire_time);
368 /* Exports can't be deleted out of the list while we hold
369 * the obd lock (class_unlink_export), which means we can't
370 * lose the last ref on the export. If they've already been
371 * removed from the list, we won't find them here. */
372 spin_lock(&obd->obd_dev_lock);
373 while (!list_empty(&obd->obd_exports_timed)) {
374 exp = list_entry(obd->obd_exports_timed.next,
375 struct obd_export,exp_obd_chain_timed);
377 if (expire_time > exp->exp_last_request_time) {
378 class_export_get(exp);
379 spin_unlock(&obd->obd_dev_lock);
380 LCONSOLE_WARN("%s: haven't heard from %s in %ld"
381 " seconds. Last request was at %ld. "
382 "I think it's dead, and I am evicting "
383 "it.\n", obd->obd_name,
384 obd_export_nid2str(exp),
385 (long)(CURRENT_SECONDS -
386 exp->exp_last_request_time),
387 exp->exp_last_request_time);
390 class_fail_export(exp);
391 class_export_put(exp);
393 spin_lock(&obd->obd_dev_lock);
395 /* List is sorted, so everyone below is ok */
399 spin_unlock(&obd->obd_dev_lock);
401 class_export_put(pet_exp);
403 spin_lock(&pet_lock);
405 spin_unlock(&pet_lock);
407 CDEBUG(D_HA, "Exiting Ping Evictor\n");
412 void ping_evictor_start(void)
416 if (++pet_refcount > 1)
419 init_waitqueue_head(&pet_waitq);
421 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
424 CERROR("Cannot start ping evictor thread: %d\n", rc);
427 EXPORT_SYMBOL(ping_evictor_start);
429 void ping_evictor_stop(void)
431 if (--pet_refcount > 0)
434 pet_state = PET_TERMINATE;
437 EXPORT_SYMBOL(ping_evictor_stop);
438 #else /* !__KERNEL__ */
441 * the current implementation of pinger in liblustre is not optimized
445 static struct pinger_data {
447 cfs_time_t pd_this_ping; /* jiffies */
448 cfs_time_t pd_next_ping; /* jiffies */
449 struct ptlrpc_request_set *pd_set;
452 static int pinger_check_rpcs(void *arg)
454 cfs_time_t curtime = cfs_time_current();
455 struct ptlrpc_request *req;
456 struct ptlrpc_request_set *set;
457 struct list_head *iter;
458 struct pinger_data *pd = &pinger_args;
461 /* prevent recursion */
462 if (pd->pd_recursion++) {
463 CDEBUG(D_HA, "pinger: recursion! quit\n");
469 /* have we reached ping point? */
470 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
475 /* if we have rpc_set already, continue processing it */
477 LASSERT(pd->pd_this_ping);
482 pd->pd_this_ping = curtime;
483 pd->pd_set = ptlrpc_prep_set();
484 if (pd->pd_set == NULL)
488 /* add rpcs into set */
489 mutex_down(&pinger_sem);
490 list_for_each(iter, &pinger_imports) {
491 struct obd_import *imp =
492 list_entry(iter, struct obd_import, imp_pinger_chain);
493 int generation, level;
496 if (cfs_time_aftereq(pd->pd_this_ping,
497 imp->imp_next_ping - 5 * CFS_TICK)) {
499 spin_lock_irqsave(&imp->imp_lock, flags);
500 generation = imp->imp_generation;
501 level = imp->imp_state;
502 spin_unlock_irqrestore(&imp->imp_lock, flags);
504 if (level != LUSTRE_IMP_FULL) {
506 "not pinging %s (in recovery)\n",
507 obd2cli_tgt(imp->imp_obd));
511 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
514 CERROR("out of memory\n");
517 req->rq_no_resend = 1;
518 req->rq_replen = lustre_msg_size(0, NULL);
519 req->rq_send_state = LUSTRE_IMP_FULL;
520 req->rq_phase = RQ_PHASE_RPC;
521 req->rq_import_generation = generation;
522 ptlrpc_set_add_req(set, req);
524 CDEBUG(D_HA, "don't need to ping %s ("CFS_TIME_T" > "
525 CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
526 imp->imp_next_ping, pd->pd_this_ping);
529 pd->pd_this_ping = curtime;
530 mutex_up(&pinger_sem);
532 /* Might be empty, that's OK. */
533 if (set->set_remaining == 0)
534 CDEBUG(D_HA, "nothing to ping\n");
536 list_for_each(iter, &set->set_requests) {
537 struct ptlrpc_request *req =
538 list_entry(iter, struct ptlrpc_request,
540 DEBUG_REQ(D_HA, req, "pinging %s->%s",
541 req->rq_import->imp_obd->obd_uuid.uuid,
542 obd2cli_tgt(req->rq_import->imp_obd));
543 (void)ptl_send_rpc(req, 0);
547 rc = ptlrpc_check_set(set);
549 /* not finished, and we are not expired, simply return */
550 if (!rc && cfs_time_before(curtime,
551 cfs_time_add(pd->pd_this_ping,
552 cfs_time_seconds(PING_INTERVAL)))) {
553 CDEBUG(D_HA, "not finished, but also not expired\n");
558 /* Expire all the requests that didn't come back. */
559 mutex_down(&pinger_sem);
560 list_for_each(iter, &set->set_requests) {
561 req = list_entry(iter, struct ptlrpc_request,
567 req->rq_phase = RQ_PHASE_COMPLETE;
568 set->set_remaining--;
569 /* If it was disconnected, don't sweat it. */
570 if (list_empty(&req->rq_import->imp_pinger_chain)) {
571 ptlrpc_unregister_reply(req);
575 CDEBUG(D_HA, "pinger initiate expire_one_request\n");
576 ptlrpc_expire_one_request(req);
578 mutex_up(&pinger_sem);
580 ptlrpc_set_destroy(set);
584 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
585 cfs_time_seconds(PING_INTERVAL));
586 pd->pd_this_ping = 0; /* XXX for debug */
588 CDEBUG(D_HA, "finished a round ping\n");
593 static void *pinger_callback = NULL;
594 #endif /* ENABLE_PINGER */
596 int ptlrpc_start_pinger(void)
599 memset(&pinger_args, 0, sizeof(pinger_args));
600 pinger_callback = liblustre_register_wait_callback(&pinger_check_rpcs,
606 int ptlrpc_stop_pinger(void)
610 liblustre_deregister_wait_callback(pinger_callback);
615 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
618 mutex_down(&pinger_sem);
619 ptlrpc_update_next_ping(imp);
620 if (pinger_args.pd_set == NULL &&
621 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
622 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
623 imp->imp_next_ping, cfs_time_current());
624 pinger_args.pd_next_ping = imp->imp_next_ping;
626 mutex_up(&pinger_sem);
630 int ptlrpc_pinger_add_import(struct obd_import *imp)
633 if (!list_empty(&imp->imp_pinger_chain))
636 CDEBUG(D_HA, "adding pingable import %s->%s\n",
637 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
638 ptlrpc_pinger_sending_on_import(imp);
640 mutex_down(&pinger_sem);
641 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
642 class_import_get(imp);
643 mutex_up(&pinger_sem);
648 int ptlrpc_pinger_del_import(struct obd_import *imp)
651 if (list_empty(&imp->imp_pinger_chain))
654 mutex_down(&pinger_sem);
655 list_del_init(&imp->imp_pinger_chain);
656 CDEBUG(D_HA, "removing pingable import %s->%s\n",
657 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
658 class_import_put(imp);
659 mutex_up(&pinger_sem);
663 void ptlrpc_pinger_wake_up()
666 /* XXX force pinger to run, if needed */
669 #endif /* !__KERNEL__ */