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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/pinger.c
34 * Portal-RPC reconnection and replay operations, for use in recovery.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <linux/kthread.h>
40 #include <linux/workqueue.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include "ptlrpc_internal.h"
45 static int suppress_pings;
46 module_param(suppress_pings, int, 0644);
47 MODULE_PARM_DESC(suppress_pings, "Suppress pings");
49 struct mutex pinger_mutex;
50 static struct list_head pinger_imports =
51 LIST_HEAD_INIT(pinger_imports);
52 static struct list_head timeout_list =
53 LIST_HEAD_INIT(timeout_list);
55 int ptlrpc_pinger_suppress_pings()
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;
81 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
85 req->rq_send_state = LUSTRE_IMP_FULL;
87 rc = ptlrpc_queue_wait(req);
89 ptlrpc_req_finished(req);
93 EXPORT_SYMBOL(ptlrpc_obd_ping);
95 static bool ptlrpc_check_import_is_idle(struct obd_import *imp)
97 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
100 if (!imp->imp_idle_timeout)
104 * - client_obd_setup() - hashed import
105 * - ptlrpcd_alloc_work()
106 * - ptlrpcd_alloc_work()
107 * - ptlrpc_pinger_add_import
109 if (atomic_read(&imp->imp_refcount) > 4)
112 /* any lock increases ns_bref being a resource holder */
113 if (ns && atomic_read(&ns->ns_bref) > 0)
116 now = ktime_get_real_seconds();
117 if (now - imp->imp_last_reply_time < imp->imp_idle_timeout)
123 static int ptlrpc_ping(struct obd_import *imp)
125 struct ptlrpc_request *req;
128 if (ptlrpc_check_import_is_idle(imp))
129 RETURN(ptlrpc_disconnect_and_idle_import(imp));
131 req = ptlrpc_prep_ping(imp);
133 CERROR("OOM trying to ping %s->%s\n",
134 imp->imp_obd->obd_uuid.uuid,
135 obd2cli_tgt(imp->imp_obd));
139 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
140 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
141 ptlrpcd_add_req(req);
146 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
149 time64_t time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
151 if (imp->imp_state == LUSTRE_IMP_DISCON) {
152 time64_t dtime = max_t(time64_t, CONNECTION_SWITCH_MIN,
154 at_get(&imp->imp_at.iat_net_latency));
155 time = min(time, dtime);
157 imp->imp_next_ping = ktime_get_seconds() + time;
158 #endif /* ENABLE_PINGER */
161 void ptlrpc_ping_import_soon(struct obd_import *imp)
163 imp->imp_next_ping = ktime_get_seconds();
166 static inline int imp_is_deactive(struct obd_import *imp)
168 return (imp->imp_deactive ||
169 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
172 static inline time64_t ptlrpc_next_reconnect(struct obd_import *imp)
174 if (imp->imp_server_timeout)
175 return ktime_get_seconds() + (obd_timeout >> 1);
177 return ktime_get_seconds() + obd_timeout;
180 static time64_t pinger_check_timeout(time64_t time)
182 struct timeout_item *item;
183 time64_t timeout = PING_INTERVAL;
185 /* This list is sorted in increasing timeout order */
186 mutex_lock(&pinger_mutex);
187 list_for_each_entry(item, &timeout_list, ti_chain) {
188 time64_t ti_timeout = item->ti_timeout;
190 if (timeout > ti_timeout)
191 timeout = ti_timeout;
194 mutex_unlock(&pinger_mutex);
196 return time + timeout - ktime_get_seconds();
201 void ptlrpc_pinger_ir_up(void)
203 CDEBUG(D_HA, "IR up\n");
206 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
208 void ptlrpc_pinger_ir_down(void)
210 CDEBUG(D_HA, "IR down\n");
213 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
215 static void ptlrpc_pinger_process_import(struct obd_import *imp,
223 spin_lock(&imp->imp_lock);
225 level = imp->imp_state;
226 force = imp->imp_force_verify;
227 force_next = imp->imp_force_next_verify;
229 * This will be used below only if the import is "FULL".
231 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
233 imp->imp_force_verify = 0;
235 if (imp->imp_next_ping - 5 >= this_ping && !force) {
236 spin_unlock(&imp->imp_lock);
240 imp->imp_force_next_verify = 0;
242 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u "
243 "force %u force_next %u deactive %u pingable %u suppress %u\n",
244 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
245 ptlrpc_import_state_name(level), level, force, force_next,
246 imp->imp_deactive, imp->imp_pingable, suppress);
248 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
249 /* wait for a while before trying recovery again */
250 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
251 spin_unlock(&imp->imp_lock);
252 if (!imp->imp_no_pinger_recover)
253 ptlrpc_initiate_recovery(imp);
254 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov ||
255 imp_is_deactive(imp)) {
256 CDEBUG(D_HA, "%s->%s: not pinging (in recovery "
257 "or recovery disabled: %s)\n",
258 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
259 ptlrpc_import_state_name(level));
261 imp->imp_force_verify = 1;
262 spin_unlock(&imp->imp_lock);
263 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
264 spin_unlock(&imp->imp_lock);
269 static struct workqueue_struct *pinger_wq;
270 static void ptlrpc_pinger_main(struct work_struct *ws);
271 static DECLARE_DELAYED_WORK(ping_work, ptlrpc_pinger_main);
273 static void ptlrpc_pinger_main(struct work_struct *ws)
275 time64_t this_ping = ktime_get_seconds();
276 time64_t time_to_next_wake;
277 struct timeout_item *item;
278 struct obd_import *imp;
279 struct list_head *iter;
282 mutex_lock(&pinger_mutex);
283 list_for_each_entry(item, &timeout_list, ti_chain)
284 item->ti_cb(item, item->ti_cb_data);
286 list_for_each(iter, &pinger_imports) {
287 imp = list_entry(iter, struct obd_import,
290 ptlrpc_pinger_process_import(imp, this_ping);
291 /* obd_timeout might have changed */
292 if (imp->imp_pingable && imp->imp_next_ping &&
293 imp->imp_next_ping > this_ping + PING_INTERVAL)
294 ptlrpc_update_next_ping(imp, 0);
296 mutex_unlock(&pinger_mutex);
297 /* update memory usage info */
298 obd_update_maxusage();
300 /* Wait until the next ping time, or until we're stopped. */
301 time_to_next_wake = pinger_check_timeout(this_ping);
302 /* The ping sent by ptlrpc_send_rpc may get sent out
303 * say .01 second after this.
304 * ptlrpc_pinger_sending_on_import will then set the
305 * next ping time to next_ping + .01 sec, which means
306 * we will SKIP the next ping at next_ping, and the
307 * ping will get sent 2 timeouts from now! Beware. */
308 CDEBUG(D_INFO, "next wakeup in %lld (%lld)\n",
309 time_to_next_wake, this_ping + PING_INTERVAL);
310 } while (time_to_next_wake <= 0);
312 queue_delayed_work(pinger_wq, &ping_work,
313 cfs_time_seconds(max(time_to_next_wake, 1LL)));
316 int ptlrpc_start_pinger(void)
322 pinger_wq = alloc_workqueue("ptlrpc_pinger", 0, 1);
324 CERROR("cannot start pinger workqueue\n");
328 queue_delayed_work(pinger_wq, &ping_work, 0);
331 CWARN("Pings will be suppressed at the request of the "
332 "administrator. The configuration shall meet the "
333 "additional requirements described in the manual. "
334 "(Search for the \"suppress_pings\" kernel module "
340 int ptlrpc_pinger_remove_timeouts(void);
342 int ptlrpc_stop_pinger(void)
348 ptlrpc_pinger_remove_timeouts();
350 cancel_delayed_work_sync(&ping_work);
351 destroy_workqueue(pinger_wq);
357 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
359 ptlrpc_update_next_ping(imp, 0);
362 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
364 ptlrpc_update_next_ping(imp, 1);
365 assert_spin_locked(&imp->imp_lock);
367 * Avoid reading stale imp_connect_data. When not sure if pings are
368 * expected or not on next connection, we assume they are not and force
369 * one anyway to guarantee the chance of updating
370 * imp_peer_committed_transno.
372 if (imp->imp_state != LUSTRE_IMP_FULL ||
373 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
374 imp->imp_force_next_verify = 1;
377 int ptlrpc_pinger_add_import(struct obd_import *imp)
380 if (!list_empty(&imp->imp_pinger_chain))
383 mutex_lock(&pinger_mutex);
384 CDEBUG(D_HA, "adding pingable import %s->%s\n",
385 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
386 /* if we add to pinger we want recovery on this import */
387 imp->imp_obd->obd_no_recov = 0;
388 ptlrpc_update_next_ping(imp, 0);
389 /* XXX sort, blah blah */
390 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
391 class_import_get(imp);
393 ptlrpc_pinger_wake_up();
394 mutex_unlock(&pinger_mutex);
398 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
400 int ptlrpc_pinger_del_import(struct obd_import *imp)
404 if (list_empty(&imp->imp_pinger_chain))
407 mutex_lock(&pinger_mutex);
408 list_del_init(&imp->imp_pinger_chain);
409 CDEBUG(D_HA, "removing pingable import %s->%s\n",
410 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
411 /* if we remove from pinger we don't want recovery on this import */
412 imp->imp_obd->obd_no_recov = 1;
413 class_import_put(imp);
414 mutex_unlock(&pinger_mutex);
417 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
420 * Register a timeout callback to the pinger list, and the callback will
421 * be called when timeout happens.
423 static struct timeout_item *ptlrpc_new_timeout(time64_t time,
424 enum timeout_event event,
425 timeout_cb_t cb, void *data)
427 struct timeout_item *ti;
433 INIT_LIST_HEAD(&ti->ti_obd_list);
434 INIT_LIST_HEAD(&ti->ti_chain);
435 ti->ti_timeout = time;
436 ti->ti_event = event;
438 ti->ti_cb_data = data;
444 * Register timeout event on the the pinger thread.
445 * Note: the timeout list is an sorted list with increased timeout value.
447 static struct timeout_item*
448 ptlrpc_pinger_register_timeout(time64_t time, enum timeout_event event,
449 timeout_cb_t cb, void *data)
451 struct timeout_item *item, *tmp;
453 LASSERT(mutex_is_locked(&pinger_mutex));
455 list_for_each_entry(item, &timeout_list, ti_chain)
456 if (item->ti_event == event)
459 item = ptlrpc_new_timeout(time, event, cb, data);
461 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
462 if (tmp->ti_timeout < time) {
463 list_add(&item->ti_chain, &tmp->ti_chain);
467 list_add(&item->ti_chain, &timeout_list);
473 /* Add a client_obd to the timeout event list, when timeout(@time)
474 * happens, the callback(@cb) will be called.
476 int ptlrpc_add_timeout_client(time64_t time, enum timeout_event event,
477 timeout_cb_t cb, void *data,
478 struct list_head *obd_list)
480 struct timeout_item *ti;
482 mutex_lock(&pinger_mutex);
483 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
485 mutex_unlock(&pinger_mutex);
488 list_add(obd_list, &ti->ti_obd_list);
489 mutex_unlock(&pinger_mutex);
492 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
494 int ptlrpc_del_timeout_client(struct list_head *obd_list,
495 enum timeout_event event)
497 struct timeout_item *ti = NULL, *item;
499 if (list_empty(obd_list))
501 mutex_lock(&pinger_mutex);
502 list_del_init(obd_list);
504 * If there are no obd attached to the timeout event
505 * list, remove this timeout event from the pinger
507 list_for_each_entry(item, &timeout_list, ti_chain) {
508 if (item->ti_event == event) {
513 LASSERTF(ti != NULL, "ti is NULL !\n");
514 if (list_empty(&ti->ti_obd_list)) {
515 list_del(&ti->ti_chain);
518 mutex_unlock(&pinger_mutex);
521 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
523 int ptlrpc_pinger_remove_timeouts(void)
525 struct timeout_item *item, *tmp;
527 mutex_lock(&pinger_mutex);
528 list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
529 LASSERT(list_empty(&item->ti_obd_list));
530 list_del(&item->ti_chain);
533 mutex_unlock(&pinger_mutex);
537 void ptlrpc_pinger_wake_up()
540 mod_delayed_work(pinger_wq, &ping_work, 0);
544 /* Ping evictor thread */
546 #define PET_TERMINATE 2
548 static int pet_refcount = 0;
549 static int pet_state;
550 static wait_queue_head_t pet_waitq;
551 static struct list_head pet_list;
552 static DEFINE_SPINLOCK(pet_lock);
554 int ping_evictor_wake(struct obd_export *exp)
556 struct obd_device *obd;
558 spin_lock(&pet_lock);
559 if (pet_state != PET_READY) {
560 /* eventually the new obd will call here again. */
561 spin_unlock(&pet_lock);
565 obd = class_exp2obd(exp);
566 if (list_empty(&obd->obd_evict_list)) {
567 class_incref(obd, "evictor", obd);
568 list_add(&obd->obd_evict_list, &pet_list);
570 spin_unlock(&pet_lock);
576 static int ping_evictor_main(void *arg)
578 struct obd_device *obd;
579 struct obd_export *exp;
580 struct l_wait_info lwi = { 0 };
581 time64_t expire_time;
586 CDEBUG(D_HA, "Starting Ping Evictor\n");
587 pet_state = PET_READY;
589 l_wait_event(pet_waitq, (!list_empty(&pet_list)) ||
590 (pet_state == PET_TERMINATE), &lwi);
592 /* loop until all obd's will be removed */
593 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
596 /* we only get here if pet_exp != NULL, and the end of this
597 * loop is the only place which sets it NULL again, so lock
598 * is not strictly necessary. */
599 spin_lock(&pet_lock);
600 obd = list_entry(pet_list.next, struct obd_device,
602 spin_unlock(&pet_lock);
604 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
606 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
607 obd->obd_name, expire_time);
609 /* Exports can't be deleted out of the list while we hold
610 * the obd lock (class_unlink_export), which means we can't
611 * lose the last ref on the export. If they've already been
612 * removed from the list, we won't find them here. */
613 spin_lock(&obd->obd_dev_lock);
614 while (!list_empty(&obd->obd_exports_timed)) {
615 exp = list_entry(obd->obd_exports_timed.next,
617 exp_obd_chain_timed);
618 if (expire_time > exp->exp_last_request_time) {
619 class_export_get(exp);
620 spin_unlock(&obd->obd_dev_lock);
621 LCONSOLE_WARN("%s: haven't heard from client %s"
622 " (at %s) in %lld seconds. I think"
623 " it's dead, and I am evicting"
624 " it. exp %p, cur %lld expire %lld"
627 obd_uuid2str(&exp->exp_client_uuid),
628 obd_export_nid2str(exp),
629 ktime_get_real_seconds() -
630 exp->exp_last_request_time,
631 exp, ktime_get_real_seconds(),
633 exp->exp_last_request_time);
634 CDEBUG(D_HA, "Last request was at %lld\n",
635 exp->exp_last_request_time);
636 class_fail_export(exp);
637 class_export_put(exp);
638 spin_lock(&obd->obd_dev_lock);
640 /* List is sorted, so everyone below is ok */
644 spin_unlock(&obd->obd_dev_lock);
646 spin_lock(&pet_lock);
647 list_del_init(&obd->obd_evict_list);
648 spin_unlock(&pet_lock);
650 class_decref(obd, "evictor", obd);
652 CDEBUG(D_HA, "Exiting Ping Evictor\n");
657 void ping_evictor_start(void)
659 struct task_struct *task;
661 if (++pet_refcount > 1)
664 INIT_LIST_HEAD(&pet_list);
665 init_waitqueue_head(&pet_waitq);
667 task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
670 CERROR("Cannot start ping evictor thread: %ld\n",
674 EXPORT_SYMBOL(ping_evictor_start);
676 void ping_evictor_stop(void)
678 if (--pet_refcount > 0)
681 pet_state = PET_TERMINATE;
684 EXPORT_SYMBOL(ping_evictor_stop);