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);
53 int ptlrpc_pinger_suppress_pings(void)
55 return suppress_pings;
57 EXPORT_SYMBOL(ptlrpc_pinger_suppress_pings);
59 struct ptlrpc_request *
60 ptlrpc_prep_ping(struct obd_import *imp)
62 struct ptlrpc_request *req;
64 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
65 LUSTRE_OBD_VERSION, OBD_PING);
67 ptlrpc_request_set_replen(req);
68 req->rq_no_resend = req->rq_no_delay = 1;
73 int ptlrpc_obd_ping(struct obd_device *obd)
76 struct ptlrpc_request *req;
77 struct obd_import *imp;
81 with_imp_locked(obd, imp, rc) {
82 req = ptlrpc_prep_ping(imp);
87 req->rq_send_state = LUSTRE_IMP_FULL;
88 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)
103 if (atomic_read(&imp->imp_reqs) > 0)
106 /* any lock increases ns_bref being a resource holder */
107 if (ns && atomic_read(&ns->ns_bref) > 0)
110 now = ktime_get_real_seconds();
111 if (now - imp->imp_last_reply_time < imp->imp_idle_timeout)
117 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
119 #ifdef CONFIG_LUSTRE_FS_PINGER
120 time64_t time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
122 if (imp->imp_state == LUSTRE_IMP_DISCON) {
123 time64_t dtime = max_t(time64_t, CONNECTION_SWITCH_MIN,
125 at_get(&imp->imp_at.iat_net_latency));
126 time = min(time, dtime);
128 imp->imp_next_ping = ktime_get_seconds() + time;
129 #endif /* CONFIG_LUSTRE_FS_PINGER */
132 static int ptlrpc_ping(struct obd_import *imp)
134 struct ptlrpc_request *req;
138 if (ptlrpc_check_import_is_idle(imp))
139 RETURN(ptlrpc_disconnect_and_idle_import(imp));
141 req = ptlrpc_prep_ping(imp);
143 CERROR("OOM trying to ping %s->%s\n",
144 imp->imp_obd->obd_uuid.uuid,
145 obd2cli_tgt(imp->imp_obd));
149 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
150 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
151 /* Updating imp_next_ping early, it allows pinger_check_timeout to
152 * see an actual time for next awake. request_out_callback update
153 * happens at another thread, and ptlrpc_pinger_main may sleep
156 ptlrpc_update_next_ping(imp, 0);
157 ptlrpcd_add_req(req);
162 void ptlrpc_ping_import_soon(struct obd_import *imp)
164 imp->imp_next_ping = ktime_get_seconds();
167 static inline int imp_is_deactive(struct obd_import *imp)
169 return imp->imp_deactive ||
170 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE);
173 static inline time64_t ptlrpc_next_reconnect(struct obd_import *imp)
175 return ktime_get_seconds() + INITIAL_CONNECT_TIMEOUT;
178 static timeout_t pinger_check_timeout(time64_t time)
180 timeout_t timeout = PING_INTERVAL;
181 timeout_t next_timeout;
183 struct list_head *iter;
184 struct obd_import *imp;
186 mutex_lock(&pinger_mutex);
187 now = ktime_get_seconds();
188 /* Process imports to find a nearest next ping */
189 list_for_each(iter, &pinger_imports) {
190 imp = list_entry(iter, struct obd_import, imp_pinger_chain);
191 if (!imp->imp_pingable || imp->imp_next_ping < now)
193 next_timeout = imp->imp_next_ping - now;
194 /* make sure imp_next_ping in the future from time */
195 if (next_timeout > (now - time) && timeout > next_timeout)
196 timeout = next_timeout;
198 mutex_unlock(&pinger_mutex);
200 return timeout - (now - time);
205 void ptlrpc_pinger_ir_up(void)
207 CDEBUG(D_HA, "IR up\n");
210 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
212 void ptlrpc_pinger_ir_down(void)
214 CDEBUG(D_HA, "IR down\n");
217 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
219 static void ptlrpc_pinger_process_import(struct obd_import *imp,
227 spin_lock(&imp->imp_lock);
229 level = imp->imp_state;
230 force = imp->imp_force_verify;
231 force_next = imp->imp_force_next_verify;
233 * This will be used below only if the import is "FULL".
235 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
237 imp->imp_force_verify = 0;
239 if (imp->imp_next_ping - 5 >= this_ping && !force) {
240 spin_unlock(&imp->imp_lock);
244 imp->imp_force_next_verify = 0;
246 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA,
247 "%s->%s: level %s/%u force %u force_next %u deactive %u pingable %u suppress %u\n",
248 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
249 ptlrpc_import_state_name(level), level, force, force_next,
250 imp->imp_deactive, imp->imp_pingable, suppress);
252 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
253 /* wait for a while before trying recovery again */
254 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
255 spin_unlock(&imp->imp_lock);
256 if (!imp->imp_no_pinger_recover ||
257 imp->imp_connect_error == -EAGAIN)
258 ptlrpc_initiate_recovery(imp);
259 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov ||
260 imp_is_deactive(imp)) {
262 "%s->%s: not pinging (in recovery or recovery disabled: %s)\n",
263 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
264 ptlrpc_import_state_name(level));
266 imp->imp_force_verify = 1;
267 spin_unlock(&imp->imp_lock);
268 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
269 spin_unlock(&imp->imp_lock);
272 spin_unlock(&imp->imp_lock);
276 static struct workqueue_struct *pinger_wq;
277 static void ptlrpc_pinger_main(struct work_struct *ws);
278 static DECLARE_DELAYED_WORK(ping_work, ptlrpc_pinger_main);
280 static void ptlrpc_pinger_main(struct work_struct *ws)
282 time64_t this_ping, time_after_ping;
283 timeout_t time_to_next_wake;
284 struct obd_import *imp;
287 this_ping = ktime_get_seconds();
289 mutex_lock(&pinger_mutex);
291 list_for_each_entry(imp, &pinger_imports, imp_pinger_chain) {
292 ptlrpc_pinger_process_import(imp, this_ping);
293 /* obd_timeout might have changed */
294 if (imp->imp_pingable && imp->imp_next_ping &&
295 imp->imp_next_ping > this_ping + PING_INTERVAL)
296 ptlrpc_update_next_ping(imp, 0);
298 mutex_unlock(&pinger_mutex);
300 time_after_ping = ktime_get_seconds();
301 /* update memory usage info */
302 obd_update_maxusage();
304 if ((ktime_get_seconds() - this_ping - 3) > PING_INTERVAL)
305 CDEBUG(D_HA, "long time to ping: %lld, %lld, %lld\n",
306 this_ping, time_after_ping, ktime_get_seconds());
308 /* Wait until the next ping time, or until we're stopped. */
309 time_to_next_wake = pinger_check_timeout(this_ping);
311 * The ping sent by ptlrpc_send_rpc may get sent out
312 * say .01 second after this.
313 * ptlrpc_pinger_sending_on_import will then set the
314 * next ping time to next_ping + .01 sec, which means
315 * we will SKIP the next ping at next_ping, and the
316 * ping will get sent 2 timeouts from now! Beware.
318 CDEBUG(D_INFO, "next wakeup in %d (%lld)\n",
319 time_to_next_wake, this_ping + PING_INTERVAL);
320 } while (time_to_next_wake <= 0);
322 queue_delayed_work(pinger_wq, &ping_work,
323 cfs_time_seconds(max(time_to_next_wake, 1)));
326 int ptlrpc_start_pinger(void)
328 #ifdef CONFIG_LUSTRE_FS_PINGER
332 pinger_wq = cfs_cpt_bind_workqueue("ptlrpc_pinger", cfs_cpt_tab,
334 if (IS_ERR(pinger_wq)) {
335 CERROR("cannot start pinger workqueue\n");
336 return PTR_ERR(pinger_wq);
339 queue_delayed_work(pinger_wq, &ping_work, 0);
342 CWARN("Pings will be suppressed at the request of the administrator. The configuration shall meet the additional requirements described in the manual. (Search for the \"suppress_pings\" kernel module parameter.)\n");
347 int ptlrpc_stop_pinger(void)
349 #ifdef CONFIG_LUSTRE_FS_PINGER
353 cancel_delayed_work_sync(&ping_work);
354 destroy_workqueue(pinger_wq);
360 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
362 ptlrpc_update_next_ping(imp, 0);
365 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
367 ptlrpc_update_next_ping(imp, 1);
368 assert_spin_locked(&imp->imp_lock);
370 * Avoid reading stale imp_connect_data. When not sure if pings are
371 * expected or not on next connection, we assume they are not and force
372 * one anyway to guarantee the chance of updating
373 * imp_peer_committed_transno.
375 if (imp->imp_state != LUSTRE_IMP_FULL ||
376 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
377 imp->imp_force_next_verify = 1;
380 int ptlrpc_pinger_add_import(struct obd_import *imp)
383 if (!list_empty(&imp->imp_pinger_chain))
386 mutex_lock(&pinger_mutex);
387 CDEBUG(D_HA, "adding pingable import %s->%s\n",
388 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
389 /* if we add to pinger we want recovery on this import */
390 imp->imp_obd->obd_no_recov = 0;
391 ptlrpc_update_next_ping(imp, 0);
392 /* XXX sort, blah blah */
393 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
394 class_import_get(imp);
396 ptlrpc_pinger_wake_up();
397 mutex_unlock(&pinger_mutex);
401 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
403 int ptlrpc_pinger_del_import(struct obd_import *imp)
407 if (list_empty(&imp->imp_pinger_chain))
410 mutex_lock(&pinger_mutex);
411 list_del_init(&imp->imp_pinger_chain);
412 CDEBUG(D_HA, "removing pingable import %s->%s\n",
413 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
414 /* if we remove from pinger we don't want recovery on this import */
415 imp->imp_obd->obd_no_recov = 1;
416 class_import_put(imp);
417 mutex_unlock(&pinger_mutex);
420 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
422 void ptlrpc_pinger_wake_up(void)
424 #ifdef CONFIG_LUSTRE_FS_PINGER
425 mod_delayed_work(pinger_wq, &ping_work, 0);
429 /* Ping evictor thread */
431 #define PET_TERMINATE 2
433 static int pet_refcount;
434 static int pet_state;
435 static wait_queue_head_t pet_waitq;
436 static LIST_HEAD(pet_list);
437 static DEFINE_SPINLOCK(pet_lock);
439 int ping_evictor_wake(struct obd_export *exp)
441 struct obd_device *obd;
443 spin_lock(&pet_lock);
444 if (pet_state != PET_READY) {
445 /* eventually the new obd will call here again. */
446 spin_unlock(&pet_lock);
450 obd = class_exp2obd(exp);
451 if (list_empty(&obd->obd_evict_list)) {
452 class_incref(obd, "evictor", obd);
453 list_add(&obd->obd_evict_list, &pet_list);
455 spin_unlock(&pet_lock);
461 static int ping_evictor_main(void *arg)
463 struct obd_device *obd;
464 struct obd_export *exp;
465 time64_t expire_time;
468 CDEBUG(D_HA, "Starting Ping Evictor\n");
469 pet_state = PET_READY;
471 wait_event_idle(pet_waitq,
472 (!list_empty(&pet_list)) ||
473 (pet_state == PET_TERMINATE));
475 /* loop until all obd's will be removed */
476 if ((pet_state == PET_TERMINATE) && list_empty(&pet_list))
480 * we only get here if pet_exp != NULL, and the end of this
481 * loop is the only place which sets it NULL again, so lock
482 * is not strictly necessary.
484 spin_lock(&pet_lock);
485 obd = list_entry(pet_list.next, struct obd_device,
487 spin_unlock(&pet_lock);
489 expire_time = ktime_get_real_seconds() - PING_EVICT_TIMEOUT;
491 CDEBUG(D_HA, "evicting all exports of obd %s older than %lld\n",
492 obd->obd_name, expire_time);
495 * Exports can't be deleted out of the list while we hold
496 * the obd lock (class_unlink_export), which means we can't
497 * lose the last ref on the export. If they've already been
498 * removed from the list, we won't find them here.
500 spin_lock(&obd->obd_dev_lock);
501 while (!list_empty(&obd->obd_exports_timed)) {
502 exp = list_entry(obd->obd_exports_timed.next,
504 exp_obd_chain_timed);
505 if (expire_time > exp->exp_last_request_time) {
506 struct obd_uuid *client_uuid;
508 class_export_get(exp);
509 client_uuid = &exp->exp_client_uuid;
510 spin_unlock(&obd->obd_dev_lock);
511 LCONSOLE_WARN("%s: haven't heard from client %s (at %s) in %lld seconds. I think it's dead, and I am evicting it. exp %p, cur %lld expire %lld last %lld\n",
513 obd_uuid2str(client_uuid),
514 obd_export_nid2str(exp),
515 ktime_get_real_seconds() -
516 exp->exp_last_request_time,
517 exp, ktime_get_real_seconds(),
519 exp->exp_last_request_time);
520 CDEBUG(D_HA, "Last request was at %lld\n",
521 exp->exp_last_request_time);
522 class_fail_export(exp);
523 class_export_put(exp);
524 spin_lock(&obd->obd_dev_lock);
526 /* List is sorted, so everyone below is ok */
530 spin_unlock(&obd->obd_dev_lock);
532 spin_lock(&pet_lock);
533 list_del_init(&obd->obd_evict_list);
534 spin_unlock(&pet_lock);
536 class_decref(obd, "evictor", obd);
538 CDEBUG(D_HA, "Exiting Ping Evictor\n");
543 void ping_evictor_start(void)
545 struct task_struct *task;
547 if (++pet_refcount > 1)
550 init_waitqueue_head(&pet_waitq);
552 task = kthread_run(ping_evictor_main, NULL, "ll_evictor");
555 CERROR("Cannot start ping evictor thread: %ld\n",
559 EXPORT_SYMBOL(ping_evictor_start);
561 void ping_evictor_stop(void)
563 if (--pet_refcount > 0)
566 pet_state = PET_TERMINATE;
569 EXPORT_SYMBOL(ping_evictor_stop);