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 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,
105 spin_lock(&imp->imp_lock);
106 level = imp->imp_state;
107 force = imp->imp_force_verify;
108 imp->imp_force_verify = 0;
109 spin_unlock(&imp->imp_lock);
111 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
112 "level %s/%u force %u deactive %u pingable %u\n",
113 ptlrpc_import_state_name(level), level,
114 force, imp->imp_deactive, imp->imp_pingable);
117 /* if the next ping is within, say, 5 jiffies from
118 now, go ahead and ping. See note below. */
119 cfs_time_aftereq(this_ping,
120 imp->imp_next_ping - 5 * CFS_TICK)) {
121 if (level == LUSTRE_IMP_DISCON &&
122 !imp->imp_deactive) {
123 /* wait at least a timeout before
124 trying recovery again. */
125 imp->imp_next_ping = cfs_time_shift(obd_timeout);
126 ptlrpc_initiate_recovery(imp);
127 } else if (level != LUSTRE_IMP_FULL ||
128 imp->imp_obd->obd_no_recov ||
130 CDEBUG(D_HA, "not pinging %s "
131 "(in recovery: %s or recovery "
132 "disabled: %u/%u)\n",
133 obd2cli_tgt(imp->imp_obd),
134 ptlrpc_import_state_name(level),
136 imp->imp_obd->obd_no_recov);
137 } else if (imp->imp_pingable || force) {
141 if (!imp->imp_pingable)
144 "don't need to ping %s ("CFS_TIME_T
145 " > "CFS_TIME_T")\n",
146 obd2cli_tgt(imp->imp_obd),
147 imp->imp_next_ping, this_ping);
150 /* obd_timeout might have changed */
151 if (cfs_time_after(imp->imp_next_ping,
152 cfs_time_add(this_ping,
153 cfs_time_seconds(PING_INTERVAL))))
154 ptlrpc_update_next_ping(imp);
156 mutex_up(&pinger_sem);
158 /* Wait until the next ping time, or until we're stopped. */
159 time_to_next_ping = cfs_time_sub(cfs_time_add(this_ping,
160 cfs_time_seconds(PING_INTERVAL)),
163 /* The ping sent by ptlrpc_send_rpc may get sent out
164 say .01 second after this.
165 ptlrpc_pinger_eending_on_import will then set the
166 next ping time to next_ping + .01 sec, which means
167 we will SKIP the next ping at next_ping, and the
168 ping will get sent 2 timeouts from now! Beware. */
169 CDEBUG(D_INFO, "next ping in "CFS_DURATION_T" ("CFS_TIME_T")\n",
171 cfs_time_add(this_ping, cfs_time_seconds(PING_INTERVAL)));
172 if (time_to_next_ping > 0) {
173 lwi = LWI_TIMEOUT(max_t(cfs_duration_t, time_to_next_ping, cfs_time_seconds(1)),
175 l_wait_event(thread->t_ctl_waitq,
176 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
178 if (thread->t_flags & SVC_STOPPING) {
179 thread->t_flags &= ~SVC_STOPPING;
182 } else if (thread->t_flags & SVC_EVENT) {
183 /* woken after adding import to reset timer */
184 thread->t_flags &= ~SVC_EVENT;
189 thread->t_flags = SVC_STOPPED;
190 cfs_waitq_signal(&thread->t_ctl_waitq);
192 CDEBUG(D_NET, "pinger thread exiting, process %d\n", cfs_curproc_pid());
196 static struct ptlrpc_thread *pinger_thread = NULL;
198 int ptlrpc_start_pinger(void)
200 struct l_wait_info lwi = { 0 };
201 struct ptlrpc_svc_data d;
203 #ifndef ENABLE_PINGER
208 if (pinger_thread != NULL)
211 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
212 if (pinger_thread == NULL)
214 cfs_waitq_init(&pinger_thread->t_ctl_waitq);
217 d.thread = pinger_thread;
219 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
220 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
221 rc = cfs_kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
223 CERROR("cannot start thread: %d\n", rc);
224 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
225 pinger_thread = NULL;
228 l_wait_event(pinger_thread->t_ctl_waitq,
229 pinger_thread->t_flags & SVC_RUNNING, &lwi);
234 int ptlrpc_stop_pinger(void)
236 struct l_wait_info lwi = { 0 };
238 #ifndef ENABLE_PINGER
243 if (pinger_thread == NULL)
245 mutex_down(&pinger_sem);
246 pinger_thread->t_flags = SVC_STOPPING;
247 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
248 mutex_up(&pinger_sem);
250 l_wait_event(pinger_thread->t_ctl_waitq,
251 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
253 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
254 pinger_thread = NULL;
258 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
260 ptlrpc_update_next_ping(imp);
263 int ptlrpc_pinger_add_import(struct obd_import *imp)
266 if (!list_empty(&imp->imp_pinger_chain))
269 mutex_down(&pinger_sem);
270 CDEBUG(D_HA, "adding pingable import %s->%s\n",
271 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
272 ptlrpc_update_next_ping(imp);
273 /* XXX sort, blah blah */
274 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
275 class_import_get(imp);
277 ptlrpc_pinger_wake_up();
278 mutex_up(&pinger_sem);
283 int ptlrpc_pinger_del_import(struct obd_import *imp)
286 if (list_empty(&imp->imp_pinger_chain))
289 mutex_down(&pinger_sem);
290 list_del_init(&imp->imp_pinger_chain);
291 CDEBUG(D_HA, "removing pingable import %s->%s\n",
292 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
293 class_import_put(imp);
294 mutex_up(&pinger_sem);
298 void ptlrpc_pinger_wake_up()
301 pinger_thread->t_flags |= SVC_EVENT;
302 cfs_waitq_signal(&pinger_thread->t_ctl_waitq);
306 /* Ping evictor thread */
308 #define PET_TERMINATE 2
310 static int pet_refcount = 0;
311 static int pet_state;
312 static wait_queue_head_t pet_waitq;
313 static struct obd_export *pet_exp = NULL;
314 static spinlock_t pet_lock = SPIN_LOCK_UNLOCKED;
316 int ping_evictor_wake(struct obd_export *exp)
318 spin_lock(&pet_lock);
320 /* eventually the new obd will call here again. */
321 spin_unlock(&pet_lock);
325 /* We have to make sure the obd isn't destroyed between now and when
326 * the ping evictor runs. We'll take a reference here, and drop it
327 * when we finish in the evictor. We don't really care about this
328 * export in particular; we just need one to keep the obd alive. */
329 pet_exp = class_export_get(exp);
330 spin_unlock(&pet_lock);
336 static int ping_evictor_main(void *arg)
338 struct obd_device *obd;
339 struct obd_export *exp;
340 struct l_wait_info lwi = { 0 };
344 ptlrpc_daemonize("ll_evictor");
346 CDEBUG(D_HA, "Starting Ping Evictor\n");
348 pet_state = PET_READY;
350 l_wait_event(pet_waitq, pet_exp ||
351 (pet_state == PET_TERMINATE), &lwi);
352 if (pet_state == PET_TERMINATE)
355 /* we only get here if pet_exp != NULL, and the end of this
356 * loop is the only place which sets it NULL again, so lock
357 * is not strictly necessary. */
358 spin_lock(&pet_lock);
359 obd = pet_exp->exp_obd;
360 spin_unlock(&pet_lock);
362 expire_time = CURRENT_SECONDS - (3 * obd_timeout / 2);
364 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
365 obd->obd_name, expire_time);
367 /* Exports can't be deleted out of the list while we hold
368 * the obd lock (class_unlink_export), which means we can't
369 * lose the last ref on the export. If they've already been
370 * removed from the list, we won't find them here. */
371 spin_lock(&obd->obd_dev_lock);
372 while (!list_empty(&obd->obd_exports_timed)) {
373 exp = list_entry(obd->obd_exports_timed.next,
374 struct obd_export,exp_obd_chain_timed);
375 if (expire_time > exp->exp_last_request_time) {
376 class_export_get(exp);
377 spin_unlock(&obd->obd_dev_lock);
378 LCONSOLE_WARN("%s: haven't heard from client %s"
379 " (at %s) in %ld seconds. I think"
380 " it's dead, and I am evicting"
381 " it.\n", obd->obd_name,
382 obd_uuid2str(&exp->exp_client_uuid),
383 obd_export_nid2str(exp),
384 (long)(CURRENT_SECONDS -
385 exp->exp_last_request_time));
386 CDEBUG(D_HA, "Last request was at %ld\n",
387 exp->exp_last_request_time);
388 class_fail_export(exp);
389 class_export_put(exp);
390 spin_lock(&obd->obd_dev_lock);
392 /* List is sorted, so everyone below is ok */
396 spin_unlock(&obd->obd_dev_lock);
398 class_export_put(pet_exp);
400 spin_lock(&pet_lock);
402 spin_unlock(&pet_lock);
404 CDEBUG(D_HA, "Exiting Ping Evictor\n");
409 void ping_evictor_start(void)
413 if (++pet_refcount > 1)
416 init_waitqueue_head(&pet_waitq);
418 rc = cfs_kernel_thread(ping_evictor_main, NULL, CLONE_VM | CLONE_FILES);
421 CERROR("Cannot start ping evictor thread: %d\n", rc);
424 EXPORT_SYMBOL(ping_evictor_start);
426 void ping_evictor_stop(void)
428 if (--pet_refcount > 0)
431 pet_state = PET_TERMINATE;
434 EXPORT_SYMBOL(ping_evictor_stop);
435 #else /* !__KERNEL__ */
438 * the current implementation of pinger in liblustre is not optimized
442 static struct pinger_data {
444 cfs_time_t pd_this_ping; /* jiffies */
445 cfs_time_t pd_next_ping; /* jiffies */
446 struct ptlrpc_request_set *pd_set;
449 static int pinger_check_rpcs(void *arg)
451 cfs_time_t curtime = cfs_time_current();
452 struct ptlrpc_request *req;
453 struct ptlrpc_request_set *set;
454 struct list_head *iter;
455 struct pinger_data *pd = &pinger_args;
458 /* prevent recursion */
459 if (pd->pd_recursion++) {
460 CDEBUG(D_HA, "pinger: recursion! quit\n");
466 /* have we reached ping point? */
467 if (!pd->pd_set && time_before(curtime, pd->pd_next_ping)) {
472 /* if we have rpc_set already, continue processing it */
474 LASSERT(pd->pd_this_ping);
479 pd->pd_this_ping = curtime;
480 pd->pd_set = ptlrpc_prep_set();
481 if (pd->pd_set == NULL)
485 /* add rpcs into set */
486 mutex_down(&pinger_sem);
487 list_for_each(iter, &pinger_imports) {
488 struct obd_import *imp =
489 list_entry(iter, struct obd_import, imp_pinger_chain);
490 int generation, level;
492 if (cfs_time_aftereq(pd->pd_this_ping,
493 imp->imp_next_ping - 5 * CFS_TICK)) {
495 spin_lock(&imp->imp_lock);
496 generation = imp->imp_generation;
497 level = imp->imp_state;
498 spin_unlock(&imp->imp_lock);
500 if (level != LUSTRE_IMP_FULL) {
502 "not pinging %s (in recovery)\n",
503 obd2cli_tgt(imp->imp_obd));
507 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING,
510 CERROR("out of memory\n");
513 req->rq_no_resend = 1;
514 ptlrpc_req_set_repsize(req, 1, NULL);
515 req->rq_send_state = LUSTRE_IMP_FULL;
516 req->rq_phase = RQ_PHASE_RPC;
517 req->rq_import_generation = generation;
518 ptlrpc_set_add_req(set, req);
520 CDEBUG(D_HA, "don't need to ping %s ("CFS_TIME_T" > "
521 CFS_TIME_T")\n", obd2cli_tgt(imp->imp_obd),
522 imp->imp_next_ping, pd->pd_this_ping);
525 pd->pd_this_ping = curtime;
526 mutex_up(&pinger_sem);
528 /* Might be empty, that's OK. */
529 if (set->set_remaining == 0)
530 CDEBUG(D_HA, "nothing to ping\n");
532 list_for_each(iter, &set->set_requests) {
533 struct ptlrpc_request *req =
534 list_entry(iter, struct ptlrpc_request,
536 DEBUG_REQ(D_HA, req, "pinging %s->%s",
537 req->rq_import->imp_obd->obd_uuid.uuid,
538 obd2cli_tgt(req->rq_import->imp_obd));
539 (void)ptl_send_rpc(req, 0);
543 rc = ptlrpc_check_set(set);
545 /* not finished, and we are not expired, simply return */
546 if (!rc && cfs_time_before(curtime,
547 cfs_time_add(pd->pd_this_ping,
548 cfs_time_seconds(PING_INTERVAL)))) {
549 CDEBUG(D_HA, "not finished, but also not expired\n");
554 /* Expire all the requests that didn't come back. */
555 mutex_down(&pinger_sem);
556 list_for_each(iter, &set->set_requests) {
557 req = list_entry(iter, struct ptlrpc_request,
563 req->rq_phase = RQ_PHASE_COMPLETE;
564 atomic_dec(&req->rq_import->imp_inflight);
565 set->set_remaining--;
566 /* If it was disconnected, don't sweat it. */
567 if (list_empty(&req->rq_import->imp_pinger_chain)) {
568 ptlrpc_unregister_reply(req);
572 CDEBUG(D_HA, "pinger initiate expire_one_request\n");
573 ptlrpc_expire_one_request(req);
575 mutex_up(&pinger_sem);
577 ptlrpc_set_destroy(set);
581 pd->pd_next_ping = cfs_time_add(pd->pd_this_ping,
582 cfs_time_seconds(PING_INTERVAL));
583 pd->pd_this_ping = 0; /* XXX for debug */
585 CDEBUG(D_HA, "finished a round ping\n");
590 static void *pinger_callback = NULL;
591 #endif /* ENABLE_PINGER */
593 int ptlrpc_start_pinger(void)
596 memset(&pinger_args, 0, sizeof(pinger_args));
597 pinger_callback = liblustre_register_wait_callback(&pinger_check_rpcs,
603 int ptlrpc_stop_pinger(void)
607 liblustre_deregister_wait_callback(pinger_callback);
612 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
615 mutex_down(&pinger_sem);
616 ptlrpc_update_next_ping(imp);
617 if (pinger_args.pd_set == NULL &&
618 time_before(imp->imp_next_ping, pinger_args.pd_next_ping)) {
619 CDEBUG(D_HA, "set next ping to "CFS_TIME_T"(cur "CFS_TIME_T")\n",
620 imp->imp_next_ping, cfs_time_current());
621 pinger_args.pd_next_ping = imp->imp_next_ping;
623 mutex_up(&pinger_sem);
627 int ptlrpc_pinger_add_import(struct obd_import *imp)
630 if (!list_empty(&imp->imp_pinger_chain))
633 CDEBUG(D_HA, "adding pingable import %s->%s\n",
634 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
635 ptlrpc_pinger_sending_on_import(imp);
637 mutex_down(&pinger_sem);
638 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
639 class_import_get(imp);
640 mutex_up(&pinger_sem);
645 int ptlrpc_pinger_del_import(struct obd_import *imp)
648 if (list_empty(&imp->imp_pinger_chain))
651 mutex_down(&pinger_sem);
652 list_del_init(&imp->imp_pinger_chain);
653 CDEBUG(D_HA, "removing pingable import %s->%s\n",
654 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
655 class_import_put(imp);
656 mutex_up(&pinger_sem);
660 void ptlrpc_pinger_wake_up()
664 /* XXX force pinger to run, if needed */
665 struct obd_import *imp;
666 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
667 CDEBUG(D_HA, "Checking that we need to do anything about import"
668 " %s->%s\n", imp->imp_obd->obd_uuid.uuid,
669 obd2cli_tgt(imp->imp_obd));
670 #ifdef ENABLE_LIBLUSTRE_RECOVERY
671 if (imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
673 /*XXX only recover for the initial connection */
674 if (!lustre_handle_is_used(&imp->imp_remote_handle) &&
675 imp->imp_state == LUSTRE_IMP_DISCON && !imp->imp_deactive)
677 ptlrpc_initiate_recovery(imp);
678 else if (imp->imp_state != LUSTRE_IMP_FULL)
679 CDEBUG(D_HA, "Refused to recover import %s->%s "
680 "state %d, deactive %d\n",
681 imp->imp_obd->obd_uuid.uuid,
682 obd2cli_tgt(imp->imp_obd), imp->imp_state,
688 #endif /* !__KERNEL__ */