4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/pinger.c
38 * Portal-RPC reconnection and replay operations, for use in recovery.
41 #define DEBUG_SUBSYSTEM S_RPC
43 #include "../include/obd_support.h"
44 #include "../include/obd_class.h"
45 #include "ptlrpc_internal.h"
47 static int suppress_pings;
48 module_param(suppress_pings, int, 0644);
49 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
51 struct mutex pinger_mutex;
52 static LIST_HEAD(pinger_imports);
53 static struct list_head timeout_list = LIST_HEAD_INIT(timeout_list);
55 int ptlrpc_pinger_suppress_pings(void)
57 return suppress_pings;
59 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
61 struct ptlrpc_request *
62 ptlrpc_prep_ping(struct obd_import *imp)
64 struct ptlrpc_request *req;
66 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
67 LUSTRE_OBD_VERSION, OBD_PING);
69 ptlrpc_request_set_replen(req);
70 req->rq_no_resend = req->rq_no_delay = 1;
75 int ptlrpc_obd_ping(struct obd_device *obd)
78 struct ptlrpc_request *req;
80 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
84 req->rq_send_state = LUSTRE_IMP_FULL;
86 rc = ptlrpc_queue_wait(req);
88 ptlrpc_req_finished(req);
92 EXPORT_SYMBOL(ptlrpc_obd_ping);
94 int ptlrpc_ping(struct obd_import *imp)
96 struct ptlrpc_request *req;
98 req = ptlrpc_prep_ping(imp);
100 CERROR("OOM trying to ping %s->%s\n",
101 imp->imp_obd->obd_uuid.uuid,
102 obd2cli_tgt(imp->imp_obd));
106 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
107 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
108 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
113 void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
115 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
116 if (imp->imp_state == LUSTRE_IMP_DISCON) {
117 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
119 at_get(&imp->imp_at.iat_net_latency));
120 time = min(time, dtime);
122 imp->imp_next_ping = cfs_time_shift(time);
125 void ptlrpc_ping_import_soon(struct obd_import *imp)
127 imp->imp_next_ping = cfs_time_current();
130 static inline int imp_is_deactive(struct obd_import *imp)
132 return (imp->imp_deactive ||
133 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
136 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
138 if (imp->imp_server_timeout)
139 return cfs_time_shift(obd_timeout / 2);
141 return cfs_time_shift(obd_timeout);
144 long pinger_check_timeout(unsigned long time)
146 struct timeout_item *item;
147 unsigned long timeout = PING_INTERVAL;
149 /* The timeout list is a increase order sorted list */
150 mutex_lock(&pinger_mutex);
151 list_for_each_entry(item, &timeout_list, ti_chain) {
152 int ti_timeout = item->ti_timeout;
153 if (timeout > ti_timeout)
154 timeout = ti_timeout;
157 mutex_unlock(&pinger_mutex);
159 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
165 void ptlrpc_pinger_ir_up(void)
167 CDEBUG(D_HA, "IR up\n");
170 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
172 void ptlrpc_pinger_ir_down(void)
174 CDEBUG(D_HA, "IR down\n");
177 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
179 static void ptlrpc_pinger_process_import(struct obd_import *imp,
180 unsigned long this_ping)
187 spin_lock(&imp->imp_lock);
189 level = imp->imp_state;
190 force = imp->imp_force_verify;
191 force_next = imp->imp_force_next_verify;
193 * This will be used below only if the import is "FULL".
195 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
197 imp->imp_force_verify = 0;
199 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
201 spin_unlock(&imp->imp_lock);
205 imp->imp_force_next_verify = 0;
207 spin_unlock(&imp->imp_lock);
209 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
210 "force %u force_next %u deactive %u pingable %u suppress %u\n",
211 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
212 ptlrpc_import_state_name(level), level, force, force_next,
213 imp->imp_deactive, imp->imp_pingable, suppress);
215 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
216 /* wait for a while before trying recovery again */
217 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
218 if (!imp->imp_no_pinger_recover)
219 ptlrpc_initiate_recovery(imp);
220 } else if (level != LUSTRE_IMP_FULL ||
221 imp->imp_obd->obd_no_recov ||
222 imp_is_deactive(imp)) {
223 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
224 "or recovery disabled: %s)\n",
225 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
226 ptlrpc_import_state_name(level));
228 spin_lock(&imp->imp_lock);
229 imp->imp_force_verify = 1;
230 spin_unlock(&imp->imp_lock);
232 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
237 static int ptlrpc_pinger_main(void *arg)
239 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
241 /* Record that the thread is running */
242 thread_set_flags(thread, SVC_RUNNING);
243 wake_up(&thread->t_ctl_waitq);
245 /* And now, loop forever, pinging as needed. */
247 unsigned long this_ping = cfs_time_current();
248 struct l_wait_info lwi;
249 long time_to_next_wake;
250 struct timeout_item *item;
251 struct list_head *iter;
253 mutex_lock(&pinger_mutex);
254 list_for_each_entry(item, &timeout_list, ti_chain) {
255 item->ti_cb(item, item->ti_cb_data);
257 list_for_each(iter, &pinger_imports) {
258 struct obd_import *imp =
259 list_entry(iter, struct obd_import,
262 ptlrpc_pinger_process_import(imp, this_ping);
263 /* obd_timeout might have changed */
264 if (imp->imp_pingable && imp->imp_next_ping &&
265 cfs_time_after(imp->imp_next_ping,
266 cfs_time_add(this_ping,
267 cfs_time_seconds(PING_INTERVAL))))
268 ptlrpc_update_next_ping(imp, 0);
270 mutex_unlock(&pinger_mutex);
271 /* update memory usage info */
272 obd_update_maxusage();
274 /* Wait until the next ping time, or until we're stopped. */
275 time_to_next_wake = pinger_check_timeout(this_ping);
276 /* The ping sent by ptlrpc_send_rpc may get sent out
277 say .01 second after this.
278 ptlrpc_pinger_sending_on_import will then set the
279 next ping time to next_ping + .01 sec, which means
280 we will SKIP the next ping at next_ping, and the
281 ping will get sent 2 timeouts from now! Beware. */
282 CDEBUG(D_INFO, "next wakeup in "CFS_DURATION_T" ("
283 CFS_TIME_T")\n", time_to_next_wake,
284 cfs_time_add(this_ping,
285 cfs_time_seconds(PING_INTERVAL)));
286 if (time_to_next_wake > 0) {
287 lwi = LWI_TIMEOUT(max_t(long, time_to_next_wake,
288 cfs_time_seconds(1)),
290 l_wait_event(thread->t_ctl_waitq,
291 thread_is_stopping(thread) ||
292 thread_is_event(thread),
294 if (thread_test_and_clear_flags(thread, SVC_STOPPING)) {
297 /* woken after adding import to reset timer */
298 thread_test_and_clear_flags(thread, SVC_EVENT);
303 thread_set_flags(thread, SVC_STOPPED);
304 wake_up(&thread->t_ctl_waitq);
306 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
310 static struct ptlrpc_thread pinger_thread;
312 int ptlrpc_start_pinger(void)
314 struct l_wait_info lwi = { 0 };
317 if (!thread_is_init(&pinger_thread) &&
318 !thread_is_stopped(&pinger_thread))
321 init_waitqueue_head(&pinger_thread.t_ctl_waitq);
323 strcpy(pinger_thread.t_name, "ll_ping");
325 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
326 * just drop the VM and FILES in cfs_daemonize_ctxt() right away. */
327 rc = PTR_ERR(kthread_run(ptlrpc_pinger_main, &pinger_thread,
328 "%s", pinger_thread.t_name));
329 if (IS_ERR_VALUE(rc)) {
330 CERROR("cannot start thread: %d\n", rc);
333 l_wait_event(pinger_thread.t_ctl_waitq,
334 thread_is_running(&pinger_thread), &lwi);
337 CWARN("Pings will be suppressed at the request of the "
338 "administrator. The configuration shall meet the "
339 "additional requirements described in the manual. "
340 "(Search for the \"suppress_pings\" kernel module "
346 int ptlrpc_pinger_remove_timeouts(void);
348 int ptlrpc_stop_pinger(void)
350 struct l_wait_info lwi = { 0 };
353 if (thread_is_init(&pinger_thread) ||
354 thread_is_stopped(&pinger_thread))
357 ptlrpc_pinger_remove_timeouts();
358 thread_set_flags(&pinger_thread, SVC_STOPPING);
359 wake_up(&pinger_thread.t_ctl_waitq);
361 l_wait_event(pinger_thread.t_ctl_waitq,
362 thread_is_stopped(&pinger_thread), &lwi);
367 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
369 ptlrpc_update_next_ping(imp, 0);
371 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
373 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
375 ptlrpc_update_next_ping(imp, 1);
376 assert_spin_locked(&imp->imp_lock);
378 * Avoid reading stale imp_connect_data. When not sure if pings are
379 * expected or not on next connection, we assume they are not and force
380 * one anyway to guarantee the chance of updating
381 * imp_peer_committed_transno.
383 if (imp->imp_state != LUSTRE_IMP_FULL ||
384 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
385 imp->imp_force_next_verify = 1;
388 int ptlrpc_pinger_add_import(struct obd_import *imp)
390 if (!list_empty(&imp->imp_pinger_chain))
393 mutex_lock(&pinger_mutex);
394 CDEBUG(D_HA, "adding pingable import %s->%s\n",
395 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
396 /* if we add to pinger we want recovery on this import */
397 imp->imp_obd->obd_no_recov = 0;
398 ptlrpc_update_next_ping(imp, 0);
399 /* XXX sort, blah blah */
400 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
401 class_import_get(imp);
403 ptlrpc_pinger_wake_up();
404 mutex_unlock(&pinger_mutex);
408 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
410 int ptlrpc_pinger_del_import(struct obd_import *imp)
412 if (list_empty(&imp->imp_pinger_chain))
415 mutex_lock(&pinger_mutex);
416 list_del_init(&imp->imp_pinger_chain);
417 CDEBUG(D_HA, "removing pingable import %s->%s\n",
418 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
419 /* if we remove from pinger we don't want recovery on this import */
420 imp->imp_obd->obd_no_recov = 1;
421 class_import_put(imp);
422 mutex_unlock(&pinger_mutex);
425 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
428 * Register a timeout callback to the pinger list, and the callback will
429 * be called when timeout happens.
431 struct timeout_item* ptlrpc_new_timeout(int time, enum timeout_event event,
432 timeout_cb_t cb, void *data)
434 struct timeout_item *ti;
440 INIT_LIST_HEAD(&ti->ti_obd_list);
441 INIT_LIST_HEAD(&ti->ti_chain);
442 ti->ti_timeout = time;
443 ti->ti_event = event;
445 ti->ti_cb_data = data;
451 * Register timeout event on the the pinger thread.
452 * Note: the timeout list is an sorted list with increased timeout value.
454 static struct timeout_item*
455 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
456 timeout_cb_t cb, void *data)
458 struct timeout_item *item, *tmp;
460 LASSERT(mutex_is_locked(&pinger_mutex));
462 list_for_each_entry(item, &timeout_list, ti_chain)
463 if (item->ti_event == event)
466 item = ptlrpc_new_timeout(time, event, cb, data);
468 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
469 if (tmp->ti_timeout < time) {
470 list_add(&item->ti_chain, &tmp->ti_chain);
474 list_add(&item->ti_chain, &timeout_list);
480 /* Add a client_obd to the timeout event list, when timeout(@time)
481 * happens, the callback(@cb) will be called.
483 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
484 timeout_cb_t cb, void *data,
485 struct list_head *obd_list)
487 struct timeout_item *ti;
489 mutex_lock(&pinger_mutex);
490 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
492 mutex_unlock(&pinger_mutex);
495 list_add(obd_list, &ti->ti_obd_list);
496 mutex_unlock(&pinger_mutex);
499 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
501 int ptlrpc_del_timeout_client(struct list_head *obd_list,
502 enum timeout_event event)
504 struct timeout_item *ti = NULL, *item;
506 if (list_empty(obd_list))
508 mutex_lock(&pinger_mutex);
509 list_del_init(obd_list);
511 * If there are no obd attached to the timeout event
512 * list, remove this timeout event from the pinger
514 list_for_each_entry(item, &timeout_list, ti_chain) {
515 if (item->ti_event == event) {
520 LASSERTF(ti != NULL, "ti is NULL !\n");
521 if (list_empty(&ti->ti_obd_list)) {
522 list_del(&ti->ti_chain);
525 mutex_unlock(&pinger_mutex);
528 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
530 int ptlrpc_pinger_remove_timeouts(void)
532 struct timeout_item *item, *tmp;
534 mutex_lock(&pinger_mutex);
535 list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
536 LASSERT(list_empty(&item->ti_obd_list));
537 list_del(&item->ti_chain);
540 mutex_unlock(&pinger_mutex);
544 void ptlrpc_pinger_wake_up(void)
546 thread_add_flags(&pinger_thread, SVC_EVENT);
547 wake_up(&pinger_thread.t_ctl_waitq);
550 /* Ping evictor thread */
552 #define PET_TERMINATE 2
554 static int pet_refcount = 0;
555 static int pet_state;
556 static wait_queue_head_t pet_waitq;
558 static DEFINE_SPINLOCK(pet_lock);
560 int ping_evictor_wake(struct obd_export *exp)
562 struct obd_device *obd;
564 spin_lock(&pet_lock);
565 if (pet_state != PET_READY) {
566 /* eventually the new obd will call here again. */
567 spin_unlock(&pet_lock);
571 obd = class_exp2obd(exp);
572 if (list_empty(&obd->obd_evict_list)) {
573 class_incref(obd, "evictor", obd);
574 list_add(&obd->obd_evict_list, &pet_list);
576 spin_unlock(&pet_lock);
582 static int ping_evictor_main(void *arg)
584 struct obd_device *obd;
585 struct obd_export *exp;
586 struct l_wait_info lwi = { 0 };
591 CDEBUG(D_HA, "Starting Ping Evictor\n");
592 pet_state = PET_READY;
594 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
595 (pet_state == PET_TERMINATE), &lwi);
597 /* loop until all obd's will be removed */
598 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
601 /* we only get here if pet_exp != NULL, and the end of this
602 * loop is the only place which sets it NULL again, so lock
603 * is not strictly necessary. */
604 spin_lock(&pet_lock);
605 obd = list_entry(pet_list.next, struct obd_device,
607 spin_unlock(&pet_lock);
609 expire_time = get_seconds() - PING_EVICT_TIMEOUT;
611 CDEBUG(D_HA, "evicting all exports of obd %s older than %ld\n",
612 obd->obd_name, expire_time);
614 /* Exports can't be deleted out of the list while we hold
615 * the obd lock (class_unlink_export), which means we can't
616 * lose the last ref on the export. If they've already been
617 * removed from the list, we won't find them here. */
618 spin_lock(&obd->obd_dev_lock);
619 while (!list_empty(&obd->obd_exports_timed)) {
620 exp = list_entry(obd->obd_exports_timed.next,
622 exp_obd_chain_timed);
623 if (expire_time > exp->exp_last_request_time) {
624 class_export_get(exp);
625 spin_unlock(&obd->obd_dev_lock);
626 LCONSOLE_WARN("%s: haven't heard from client %s"
627 " (at %s) in %ld seconds. I think"
628 " it's dead, and I am evicting"
629 " it. exp %p, cur %ld expire %ld"
632 obd_uuid2str(&exp->exp_client_uuid),
633 obd_export_nid2str(exp),
634 (long)(get_seconds() -
635 exp->exp_last_request_time),
636 exp, (long)get_seconds(),
638 (long)exp->exp_last_request_time);
639 CDEBUG(D_HA, "Last request was at %ld\n",
640 exp->exp_last_request_time);
641 class_fail_export(exp);
642 class_export_put(exp);
643 spin_lock(&obd->obd_dev_lock);
645 /* List is sorted, so everyone below is ok */
649 spin_unlock(&obd->obd_dev_lock);
651 spin_lock(&pet_lock);
652 list_del_init(&obd->obd_evict_list);
653 spin_unlock(&pet_lock);
655 class_decref(obd, "evictor", obd);
657 CDEBUG(D_HA, "Exiting Ping Evictor\n");
662 void ping_evictor_start(void)
664 struct task_struct *task;
666 if (++pet_refcount > 1)
669 init_waitqueue_head(&pet_waitq);
671 task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
674 CERROR("Cannot start ping evictor thread: %ld\n",
678 EXPORT_SYMBOL(ping_evictor_start);
680 void ping_evictor_stop(void)
682 if (--pet_refcount > 0)
685 pet_state = PET_TERMINATE;
688 EXPORT_SYMBOL(ping_evictor_stop);