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, 1, 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 ptlrpc_req_set_repsize(req, 1, 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 int time = (imp->imp_state != LUSTRE_IMP_DISCON) ? PING_INTERVAL :
69 /* FIXME should this be limited to LND_TIMEOUT so we don't
70 build up pings in LND output queues? */
71 max_t(int, CONNECTION_SWITCH_MIN,
72 at_get(&imp->imp_at.iat_net_latency));
73 imp->imp_next_ping = cfs_time_shift(time);
76 void ptlrpc_ping_import_soon(struct obd_import *imp)
78 imp->imp_next_ping = cfs_time_current();
82 static int ptlrpc_pinger_main(void *arg)
84 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
85 struct ptlrpc_thread *thread = data->thread;
88 cfs_daemonize(data->name);
90 /* Record that the thread is running */
91 thread->t_flags = SVC_RUNNING;
92 cfs_waitq_signal(&thread->t_ctl_waitq);
94 /* And now, loop forever, pinging as needed. */
96 cfs_time_t this_ping = cfs_time_current();
97 struct l_wait_info lwi;
98 cfs_duration_t time_to_next_ping;
99 struct list_head *iter;
101 mutex_down(&pinger_sem);
102 list_for_each(iter, &pinger_imports) {
103 struct obd_import *imp =
104 list_entry(iter, struct obd_import,
108 spin_lock(&imp->imp_lock);
109 level = imp->imp_state;
110 force = imp->imp_force_verify;
111 imp->imp_force_verify = 0;
112 spin_unlock(&imp->imp_lock);
114 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_RPCTRACE,
115 "level %s/%u force %u deactive %u pingable %u\n",
116 ptlrpc_import_state_name(level), level,
117 force, imp->imp_deactive, imp->imp_pingable);
120 /* if the next ping is within, say, 5 jiffies from
121 now, go ahead and ping. See note below. */
122 cfs_time_aftereq(this_ping,
123 imp->imp_next_ping - 5 * CFS_TICK)) {
124 if (level == LUSTRE_IMP_DISCON &&
125 !imp->imp_deactive) {
126 /* wait at least a timeout before
127 trying recovery again. */
128 imp->imp_next_ping = cfs_time_shift(obd_timeout);
129 ptlrpc_initiate_recovery(imp);
130 } else if (level != LUSTRE_IMP_FULL ||
131 imp->imp_obd->obd_no_recov ||
133 CDEBUG(D_HA, "not pinging %s "
134 "(in recovery: %s or recovery "
135 "disabled: %u/%u)\n",
136 obd2cli_tgt(imp->imp_obd),
137 ptlrpc_import_state_name(level),
139 imp->imp_obd->obd_no_recov);
140 } else if (imp->imp_pingable || force) {
144 if (!imp->imp_pingable)
147 "don't need to ping %s ("CFS_TIME_T
148 " > "CFS_TIME_T")\n",
149 obd2cli_tgt(imp->imp_obd),
150 imp->imp_next_ping, this_ping);
153 /* obd_timeout might have changed */
154 if (cfs_time_after(imp->imp_next_ping,
155 cfs_time_add(this_ping,
156 cfs_time_seconds(PING_INTERVAL))))
157 ptlrpc_update_next_ping(imp);
159 mutex_up(&pinger_sem);
161 /* Wait until the next ping time, or until we're stopped. */
162 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
163 cfs_time_seconds(PING_INTERVAL)),
166 /* The ping sent by ptlrpc_send_rpc may get sent out
167 say .01 second after this.
168 ptlrpc_pinger_eending_on_import will then set the
169 next ping time to next_ping + .01 sec, which means
170 we will SKIP the next ping at next_ping, and the
171 ping will get sent 2 timeouts from now! Beware. */
172 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
174 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
175 if (time_to_next_ping > 0) {
176 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
178 l_wait_event(thread->t_ctl_waitq,
179 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
181 if (thread->t_flags & SVC_STOPPING) {
182 thread->t_flags &= ~SVC_STOPPING;
185 } else if (thread->t_flags & SVC_EVENT) {
186 /* woken after adding import to reset timer */
187 thread->t_flags &= ~SVC_EVENT;
192 thread->t_flags = SVC_STOPPED;
193 cfs_waitq_signal(&thread->t_ctl_waitq);
195 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
199 static struct ptlrpc_thread *pinger_thread = NULL;
201 int ptlrpc_start_pinger(void)
203 struct l_wait_info lwi = { 0 };
204 struct ptlrpc_svc_data d;
206 #ifndef ENABLE_PINGER
211 if (pinger_thread != NULL)
214 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
215 if (pinger_thread == NULL)
217 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
220 d.thread = pinger_thread;
222 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
223 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
224 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
226 CERROR("cannot start thread: %d\n", rc);
227 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
228 pinger_thread = NULL;
231 l_wait_event(pinger_thread->t_ctl_waitq,
232 pinger_thread->t_flags & SVC_RUNNING, &lwi);
237 int ptlrpc_stop_pinger(void)
239 struct l_wait_info lwi = { 0 };
241 #ifndef ENABLE_PINGER
246 if (pinger_thread == NULL)
248 mutex_down(&pinger_sem);
249 pinger_thread->t_flags = SVC_STOPPING;
250 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
251 mutex_up(&pinger_sem);
253 l_wait_event(pinger_thread->t_ctl_waitq,
254 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
256 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
257 pinger_thread = NULL;
261 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
263 ptlrpc_update_next_ping(imp);
266 int ptlrpc_pinger_add_import(struct obd_import *imp)
269 if (!list_empty(&imp->imp_pinger_chain))
272 mutex_down(&pinger_sem);
273 CDEBUG(D_HA, "adding pingable import %s->%s\n",
274 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
275 /* if we add to pinger we want recovery on this import */
276 imp->imp_obd->obd_no_recov = 0;
278 ptlrpc_update_next_ping(imp);
279 /* XXX sort, blah blah */
280 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
281 class_import_get(imp);
283 ptlrpc_pinger_wake_up();
284 mutex_up(&pinger_sem);
289 int ptlrpc_pinger_del_import(struct obd_import *imp)
292 if (list_empty(&imp->imp_pinger_chain))
295 mutex_down(&pinger_sem);
296 list_del_init(&imp->imp_pinger_chain);
297 CDEBUG(D_HA, "removing pingable import %s->%s\n",
298 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
299 /* if we remove from pinger we don't want recovery on this import */
300 imp->imp_obd->obd_no_recov = 1;
301 class_import_put(imp);
302 mutex_up(&pinger_sem);
306 void ptlrpc_pinger_wake_up()
309 pinger_thread->t_flags |= SVC_EVENT;
310 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
314 /* Ping evictor thread */
316 #define PET_TERMINATE 2
318 static int pet_refcount = 0;
319 static int pet_state;
320 static wait_queue_head_t pet_waitq;
321 static struct obd_export *pet_exp = NULL;
322 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
324 int ping_evictor_wake(struct obd_export *exp)
326 spin_lock(&pet_lock);
328 /* eventually the new obd will call here again. */
329 spin_unlock(&pet_lock);
333 /* We have to make sure the obd isn't destroyed between now and when
334 * the ping evictor runs. We'll take a reference here, and drop it
335 * when we finish in the evictor. We don't really care about this
336 * export in particular; we just need one to keep the obd alive. */
337 pet_exp = class_export_get(exp);
338 spin_unlock(&pet_lock);
344 static int ping_evictor_main(void *arg)
346 struct obd_device *obd;
347 struct obd_export *exp;
348 struct l_wait_info lwi = { 0 };
352 ptlrpc_daemonize("ll_evictor");
354 CDEBUG(D_HA, "Starting Ping Evictor\n");
356 pet_state = PET_READY;
358 l_wait_event(pet_waitq, pet_exp ||
359 (pet_state == PET_TERMINATE), &lwi);
360 if (pet_state == PET_TERMINATE)
363 /* we only get here if pet_exp != NULL, and the end of this
364 * loop is the only place which sets it NULL again, so lock
365 * is not strictly necessary. */
366 spin_lock(&pet_lock);
367 obd = pet_exp->exp_obd;
368 spin_unlock(&pet_lock);
370 expire_time = cfs_time_current_sec() - PING_EVICT_TIMEOUT;
372 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
373 obd->obd_name, expire_time);
375 /* Exports can't be deleted out of the list while we hold
376 * the obd lock (class_unlink_export), which means we can't
377 * lose the last ref on the export. If they've already been
378 * removed from the list, we won't find them here. */
379 spin_lock(&obd->obd_dev_lock);
380 while (!list_empty(&obd->obd_exports_timed)) {
381 exp = list_entry(obd->obd_exports_timed.next,
382 struct obd_export,exp_obd_chain_timed);
383 if (expire_time > exp->exp_last_request_time) {
384 class_export_get(exp);
385 spin_unlock(&obd->obd_dev_lock);
386 LCONSOLE_WARN("%s: haven't heard from client %s"
387 " (at %s) in %ld seconds. I think"
388 " it's dead, and I am evicting"
389 " it.\n", obd->obd_name,
390 obd_uuid2str(&exp->exp_client_uuid),
391 obd_export_nid2str(exp),
392 (long)(cfs_time_current_sec() -
393 exp->exp_last_request_time));
394 CDEBUG(D_HA, "Last request was at %ld\n",
395 exp->exp_last_request_time);
396 class_fail_export(exp);
397 class_export_put(exp);
398 spin_lock(&obd->obd_dev_lock);
400 /* List is sorted, so everyone below is ok */
404 spin_unlock(&obd->obd_dev_lock);
406 class_export_put(pet_exp);
408 spin_lock(&pet_lock);
410 spin_unlock(&pet_lock);
412 CDEBUG(D_HA, "Exiting Ping Evictor\n");
417 void ping_evictor_start(void)
421 if (++pet_refcount > 1)
424 init_waitqueue_head(&pet_waitq);
426 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
429 CERROR("Cannot start ping evictor thread: %d\n", rc);
432 EXPORT_SYMBOL(ping_evictor_start);
434 void ping_evictor_stop(void)
436 if (--pet_refcount > 0)
439 pet_state = PET_TERMINATE;
442 EXPORT_SYMBOL(ping_evictor_stop);
443 #else /* !__KERNEL__ */
446 * the current implementation of pinger in liblustre is not optimized
450 static struct pinger_data {
452 cfs_time_t pd_this_ping; /* jiffies */
453 cfs_time_t pd_next_ping; /* jiffies */
454 struct ptlrpc_request_set *pd_set;
457 static int pinger_check_rpcs(void *arg)
459 cfs_time_t curtime = cfs_time_current();
460 struct ptlrpc_request *req;
461 struct ptlrpc_request_set *set;
462 struct list_head *iter;
463 struct pinger_data *pd = &pinger_args;
466 /* prevent recursion */
467 if (pd->pd_recursion++) {
468 CDEBUG(D_HA, "pinger: recursion! quit\n");
474 /* have we reached ping point? */
475 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
480 /* if we have rpc_set already, continue processing it */
482 LASSERT(pd->pd_this_ping);
487 pd->pd_this_ping = curtime;
488 pd->pd_set = ptlrpc_prep_set();
489 if (pd->pd_set == NULL)
493 /* add rpcs into set */
494 mutex_down(&pinger_sem);
495 list_for_each(iter, &pinger_imports) {
496 struct obd_import *imp =
497 list_entry(iter, struct obd_import, imp_pinger_chain);
498 int generation, level;
500 if (cfs_time_aftereq(pd->pd_this_ping,
501 imp->imp_next_ping - 5 * CFS_TICK)) {
503 spin_lock(&imp->imp_lock);
504 generation = imp->imp_generation;
505 level = imp->imp_state;
506 spin_unlock(&imp->imp_lock);
508 if (level != LUSTRE_IMP_FULL) {
510 "not pinging %s (in recovery)\n",
511 obd2cli_tgt(imp->imp_obd));
515 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
518 CERROR("out of memory\n");
521 req->rq_no_resend = 1;
522 ptlrpc_req_set_repsize(req, 1, NULL);
523 req->rq_send_state = LUSTRE_IMP_FULL;
524 req->rq_phase = RQ_PHASE_RPC;
525 req->rq_import_generation = generation;
526 ptlrpc_set_add_req(set, req);
528 CDEBUG(D_INFO, "don't need to ping %s ("CFS_TIME_T
529 " > "CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
530 imp->imp_next_ping, pd->pd_this_ping);
533 pd->pd_this_ping = curtime;
534 mutex_up(&pinger_sem);
536 /* Might be empty, that's OK. */
537 if (set->set_remaining == 0)
538 CDEBUG(D_RPCTRACE, "nothing to ping\n");
540 list_for_each(iter, &set->set_requests) {
541 struct ptlrpc_request *req =
542 list_entry(iter, struct ptlrpc_request,
544 DEBUG_REQ(D_RPCTRACE, req, "pinging %s->%s",
545 req->rq_import->imp_obd->obd_uuid.uuid,
546 obd2cli_tgt(req->rq_import->imp_obd));
547 (void)ptl_send_rpc(req, 0);
551 rc = ptlrpc_check_set(set);
553 /* not finished, and we are not expired, simply return */
554 if (!rc && cfs_time_before(curtime, cfs_time_add(pd->pd_this_ping,
555 cfs_time_seconds(PING_INTERVAL)))) {
556 CDEBUG(D_RPCTRACE, "not finished, but also not expired\n");
561 /* Expire all the requests that didn't come back. */
562 mutex_down(&pinger_sem);
563 list_for_each(iter, &set->set_requests) {
564 req = list_entry(iter, struct ptlrpc_request,
567 if (req->rq_phase == RQ_PHASE_COMPLETE)
570 req->rq_phase = RQ_PHASE_COMPLETE;
571 atomic_dec(&req->rq_import->imp_inflight);
572 set->set_remaining--;
573 /* If it was disconnected, don't sweat it. */
574 if (list_empty(&req->rq_import->imp_pinger_chain)) {
575 ptlrpc_unregister_reply(req);
579 CDEBUG(D_RPCTRACE, "pinger initiate expire_one_request\n");
580 ptlrpc_expire_one_request(req);
582 mutex_up(&pinger_sem);
584 ptlrpc_set_destroy(set);
588 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
589 cfs_time_seconds(PING_INTERVAL));
590 pd->pd_this_ping = 0; /* XXX for debug */
592 CDEBUG(D_INFO, "finished a round ping\n");
597 static void *pinger_callback = NULL;
598 #endif /* ENABLE_PINGER */
600 int ptlrpc_start_pinger(void)
603 memset(&pinger_args, 0, sizeof(pinger_args));
604 pinger_callback = liblustre_register_wait_callback("pinger_check_rpcs",
611 int ptlrpc_stop_pinger(void)
615 liblustre_deregister_wait_callback(pinger_callback);
620 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
623 mutex_down(&pinger_sem);
624 ptlrpc_update_next_ping(imp);
625 if (pinger_args.pd_set == NULL &&
626 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
627 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
628 imp->imp_next_ping, cfs_time_current());
629 pinger_args.pd_next_ping = imp->imp_next_ping;
631 mutex_up(&pinger_sem);
635 int ptlrpc_pinger_add_import(struct obd_import *imp)
638 if (!list_empty(&imp->imp_pinger_chain))
641 CDEBUG(D_HA, "adding pingable import %s->%s\n",
642 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
643 ptlrpc_pinger_sending_on_import(imp);
645 mutex_down(&pinger_sem);
646 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
647 class_import_get(imp);
648 mutex_up(&pinger_sem);
653 int ptlrpc_pinger_del_import(struct obd_import *imp)
656 if (list_empty(&imp->imp_pinger_chain))
659 mutex_down(&pinger_sem);
660 list_del_init(&imp->imp_pinger_chain);
661 CDEBUG(D_HA, "removing pingable import %s->%s\n",
662 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
663 class_import_put(imp);
664 mutex_up(&pinger_sem);
668 void ptlrpc_pinger_wake_up()
672 /* XXX force pinger to run, if needed */
673 struct obd_import *imp;
674 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
675 CDEBUG(D_RPCTRACE, "checking import %s->%s\n",
676 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
677 #ifdef ENABLE_LIBLUSTRE_RECOVERY
678 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
680 /*XXX only recover for the initial connection */
681 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
682 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
684 ptlrpc_initiate_recovery(imp);
685 else if (imp->imp_state != LUSTRE_IMP_FULL)
686 CDEBUG(D_HA, "Refused to recover import %s->%s "
687 "state %d, deactive %d\n",
688 imp->imp_obd->obd_uuid.uuid,
689 obd2cli_tgt(imp->imp_obd), imp->imp_state,
695 #endif /* !__KERNEL__ */