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 struct mutex pinger_mutex;
48 static LIST_HEAD(pinger_imports);
49 static struct list_head timeout_list = LIST_HEAD_INIT(timeout_list);
51 struct ptlrpc_request *
52 ptlrpc_prep_ping(struct obd_import *imp)
54 struct ptlrpc_request *req;
56 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING,
57 LUSTRE_OBD_VERSION, OBD_PING);
59 ptlrpc_request_set_replen(req);
60 req->rq_no_resend = req->rq_no_delay = 1;
65 int ptlrpc_obd_ping(struct obd_device *obd)
68 struct ptlrpc_request *req;
70 req = ptlrpc_prep_ping(obd->u.cli.cl_import);
74 req->rq_send_state = LUSTRE_IMP_FULL;
76 rc = ptlrpc_queue_wait(req);
78 ptlrpc_req_finished(req);
82 EXPORT_SYMBOL(ptlrpc_obd_ping);
84 static int ptlrpc_ping(struct obd_import *imp)
86 struct ptlrpc_request *req;
88 req = ptlrpc_prep_ping(imp);
90 CERROR("OOM trying to ping %s->%s\n",
91 imp->imp_obd->obd_uuid.uuid,
92 obd2cli_tgt(imp->imp_obd));
96 DEBUG_REQ(D_INFO, req, "pinging %s->%s",
97 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
103 static void ptlrpc_update_next_ping(struct obd_import *imp, int soon)
105 int time = soon ? PING_INTERVAL_SHORT : PING_INTERVAL;
106 if (imp->imp_state == LUSTRE_IMP_DISCON) {
107 int dtime = max_t(int, CONNECTION_SWITCH_MIN,
109 at_get(&imp->imp_at.iat_net_latency));
110 time = min(time, dtime);
112 imp->imp_next_ping = cfs_time_shift(time);
115 static inline int imp_is_deactive(struct obd_import *imp)
117 return (imp->imp_deactive ||
118 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_IMP_DEACTIVE));
121 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
123 if (imp->imp_server_timeout)
124 return cfs_time_shift(obd_timeout / 2);
126 return cfs_time_shift(obd_timeout);
129 static long pinger_check_timeout(unsigned long time)
131 struct timeout_item *item;
132 unsigned long timeout = PING_INTERVAL;
134 /* The timeout list is a increase order sorted list */
135 mutex_lock(&pinger_mutex);
136 list_for_each_entry(item, &timeout_list, ti_chain) {
137 int ti_timeout = item->ti_timeout;
138 if (timeout > ti_timeout)
139 timeout = ti_timeout;
142 mutex_unlock(&pinger_mutex);
144 return cfs_time_sub(cfs_time_add(time, cfs_time_seconds(timeout)),
150 void ptlrpc_pinger_ir_up(void)
152 CDEBUG(D_HA, "IR up\n");
155 EXPORT_SYMBOL(ptlrpc_pinger_ir_up);
157 void ptlrpc_pinger_ir_down(void)
159 CDEBUG(D_HA, "IR down\n");
162 EXPORT_SYMBOL(ptlrpc_pinger_ir_down);
164 static void ptlrpc_pinger_process_import(struct obd_import *imp,
165 unsigned long this_ping)
172 spin_lock(&imp->imp_lock);
174 level = imp->imp_state;
175 force = imp->imp_force_verify;
176 force_next = imp->imp_force_next_verify;
178 * This will be used below only if the import is "FULL".
180 suppress = ir_up && OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS);
182 imp->imp_force_verify = 0;
184 if (cfs_time_aftereq(imp->imp_next_ping - 5 * CFS_TICK, this_ping) &&
186 spin_unlock(&imp->imp_lock);
190 imp->imp_force_next_verify = 0;
192 spin_unlock(&imp->imp_lock);
194 CDEBUG(level == LUSTRE_IMP_FULL ? D_INFO : D_HA, "%s->%s: level %s/%u force %u force_next %u deactive %u pingable %u suppress %u\n",
195 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
196 ptlrpc_import_state_name(level), level, force, force_next,
197 imp->imp_deactive, imp->imp_pingable, suppress);
199 if (level == LUSTRE_IMP_DISCON && !imp_is_deactive(imp)) {
200 /* wait for a while before trying recovery again */
201 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
202 if (!imp->imp_no_pinger_recover)
203 ptlrpc_initiate_recovery(imp);
204 } else if (level != LUSTRE_IMP_FULL ||
205 imp->imp_obd->obd_no_recov ||
206 imp_is_deactive(imp)) {
207 CDEBUG(D_HA, "%s->%s: not pinging (in recovery or recovery disabled: %s)\n",
208 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd),
209 ptlrpc_import_state_name(level));
211 spin_lock(&imp->imp_lock);
212 imp->imp_force_verify = 1;
213 spin_unlock(&imp->imp_lock);
215 } else if ((imp->imp_pingable && !suppress) || force_next || force) {
220 static int ptlrpc_pinger_main(void *arg)
222 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
224 /* Record that the thread is running */
225 thread_set_flags(thread, SVC_RUNNING);
226 wake_up(&thread->t_ctl_waitq);
228 /* And now, loop forever, pinging as needed. */
230 unsigned long this_ping = cfs_time_current();
231 struct l_wait_info lwi;
232 long time_to_next_wake;
233 struct timeout_item *item;
234 struct list_head *iter;
236 mutex_lock(&pinger_mutex);
237 list_for_each_entry(item, &timeout_list, ti_chain) {
238 item->ti_cb(item, item->ti_cb_data);
240 list_for_each(iter, &pinger_imports) {
241 struct obd_import *imp =
242 list_entry(iter, struct obd_import,
245 ptlrpc_pinger_process_import(imp, this_ping);
246 /* obd_timeout might have changed */
247 if (imp->imp_pingable && imp->imp_next_ping &&
248 cfs_time_after(imp->imp_next_ping,
249 cfs_time_add(this_ping,
250 cfs_time_seconds(PING_INTERVAL))))
251 ptlrpc_update_next_ping(imp, 0);
253 mutex_unlock(&pinger_mutex);
255 /* Wait until the next ping time, or until we're stopped. */
256 time_to_next_wake = pinger_check_timeout(this_ping);
257 /* The ping sent by ptlrpc_send_rpc may get sent out
258 say .01 second after this.
259 ptlrpc_pinger_sending_on_import will then set the
260 next ping time to next_ping + .01 sec, which means
261 we will SKIP the next ping at next_ping, and the
262 ping will get sent 2 timeouts from now! Beware. */
263 CDEBUG(D_INFO, "next wakeup in " CFS_DURATION_T " (%ld)\n",
265 cfs_time_add(this_ping,
266 cfs_time_seconds(PING_INTERVAL)));
267 if (time_to_next_wake > 0) {
268 lwi = LWI_TIMEOUT(max_t(long, time_to_next_wake,
269 cfs_time_seconds(1)),
271 l_wait_event(thread->t_ctl_waitq,
272 thread_is_stopping(thread) ||
273 thread_is_event(thread),
275 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
277 /* woken after adding import to reset timer */
278 thread_test_and_clear_flags(thread, SVC_EVENT);
282 thread_set_flags(thread, SVC_STOPPED);
283 wake_up(&thread->t_ctl_waitq);
285 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current_pid());
289 static struct ptlrpc_thread pinger_thread;
291 int ptlrpc_start_pinger(void)
293 struct l_wait_info lwi = { 0 };
296 if (!thread_is_init(&pinger_thread) &&
297 !thread_is_stopped(&pinger_thread))
300 init_waitqueue_head(&pinger_thread.t_ctl_waitq);
302 strcpy(pinger_thread.t_name, "ll_ping");
304 rc = PTR_ERR(kthread_run(ptlrpc_pinger_main, &pinger_thread,
305 "%s", pinger_thread.t_name));
306 if (IS_ERR_VALUE(rc)) {
307 CERROR("cannot start thread: %d\n", rc);
310 l_wait_event(pinger_thread.t_ctl_waitq,
311 thread_is_running(&pinger_thread), &lwi);
316 static int ptlrpc_pinger_remove_timeouts(void);
318 int ptlrpc_stop_pinger(void)
320 struct l_wait_info lwi = { 0 };
323 if (thread_is_init(&pinger_thread) ||
324 thread_is_stopped(&pinger_thread))
327 ptlrpc_pinger_remove_timeouts();
328 thread_set_flags(&pinger_thread, SVC_STOPPING);
329 wake_up(&pinger_thread.t_ctl_waitq);
331 l_wait_event(pinger_thread.t_ctl_waitq,
332 thread_is_stopped(&pinger_thread), &lwi);
337 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
339 ptlrpc_update_next_ping(imp, 0);
341 EXPORT_SYMBOL(ptlrpc_pinger_sending_on_import);
343 void ptlrpc_pinger_commit_expected(struct obd_import *imp)
345 ptlrpc_update_next_ping(imp, 1);
346 assert_spin_locked(&imp->imp_lock);
348 * Avoid reading stale imp_connect_data. When not sure if pings are
349 * expected or not on next connection, we assume they are not and force
350 * one anyway to guarantee the chance of updating
351 * imp_peer_committed_transno.
353 if (imp->imp_state != LUSTRE_IMP_FULL ||
354 OCD_HAS_FLAG(&imp->imp_connect_data, PINGLESS))
355 imp->imp_force_next_verify = 1;
358 int ptlrpc_pinger_add_import(struct obd_import *imp)
360 if (!list_empty(&imp->imp_pinger_chain))
363 mutex_lock(&pinger_mutex);
364 CDEBUG(D_HA, "adding pingable import %s->%s\n",
365 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
366 /* if we add to pinger we want recovery on this import */
367 imp->imp_obd->obd_no_recov = 0;
368 ptlrpc_update_next_ping(imp, 0);
369 /* XXX sort, blah blah */
370 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
371 class_import_get(imp);
373 ptlrpc_pinger_wake_up();
374 mutex_unlock(&pinger_mutex);
378 EXPORT_SYMBOL(ptlrpc_pinger_add_import);
380 int ptlrpc_pinger_del_import(struct obd_import *imp)
382 if (list_empty(&imp->imp_pinger_chain))
385 mutex_lock(&pinger_mutex);
386 list_del_init(&imp->imp_pinger_chain);
387 CDEBUG(D_HA, "removing pingable import %s->%s\n",
388 imp->imp_obd->obd_uuid.uuid, obd2cli_tgt(imp->imp_obd));
389 /* if we remove from pinger we don't want recovery on this import */
390 imp->imp_obd->obd_no_recov = 1;
391 class_import_put(imp);
392 mutex_unlock(&pinger_mutex);
395 EXPORT_SYMBOL(ptlrpc_pinger_del_import);
398 * Register a timeout callback to the pinger list, and the callback will
399 * be called when timeout happens.
401 static struct timeout_item *ptlrpc_new_timeout(int time,
402 enum timeout_event event, timeout_cb_t cb, void *data)
404 struct timeout_item *ti;
406 ti = kzalloc(sizeof(*ti), GFP_NOFS);
410 INIT_LIST_HEAD(&ti->ti_obd_list);
411 INIT_LIST_HEAD(&ti->ti_chain);
412 ti->ti_timeout = time;
413 ti->ti_event = event;
415 ti->ti_cb_data = data;
421 * Register timeout event on the pinger thread.
422 * Note: the timeout list is an sorted list with increased timeout value.
424 static struct timeout_item*
425 ptlrpc_pinger_register_timeout(int time, enum timeout_event event,
426 timeout_cb_t cb, void *data)
428 struct timeout_item *item, *tmp;
430 LASSERT(mutex_is_locked(&pinger_mutex));
432 list_for_each_entry(item, &timeout_list, ti_chain)
433 if (item->ti_event == event)
436 item = ptlrpc_new_timeout(time, event, cb, data);
438 list_for_each_entry_reverse(tmp, &timeout_list, ti_chain) {
439 if (tmp->ti_timeout < time) {
440 list_add(&item->ti_chain, &tmp->ti_chain);
444 list_add(&item->ti_chain, &timeout_list);
450 /* Add a client_obd to the timeout event list, when timeout(@time)
451 * happens, the callback(@cb) will be called.
453 int ptlrpc_add_timeout_client(int time, enum timeout_event event,
454 timeout_cb_t cb, void *data,
455 struct list_head *obd_list)
457 struct timeout_item *ti;
459 mutex_lock(&pinger_mutex);
460 ti = ptlrpc_pinger_register_timeout(time, event, cb, data);
462 mutex_unlock(&pinger_mutex);
465 list_add(obd_list, &ti->ti_obd_list);
466 mutex_unlock(&pinger_mutex);
469 EXPORT_SYMBOL(ptlrpc_add_timeout_client);
471 int ptlrpc_del_timeout_client(struct list_head *obd_list,
472 enum timeout_event event)
474 struct timeout_item *ti = NULL, *item;
476 if (list_empty(obd_list))
478 mutex_lock(&pinger_mutex);
479 list_del_init(obd_list);
481 * If there are no obd attached to the timeout event
482 * list, remove this timeout event from the pinger
484 list_for_each_entry(item, &timeout_list, ti_chain) {
485 if (item->ti_event == event) {
490 LASSERTF(ti != NULL, "ti is NULL !\n");
491 if (list_empty(&ti->ti_obd_list)) {
492 list_del(&ti->ti_chain);
495 mutex_unlock(&pinger_mutex);
498 EXPORT_SYMBOL(ptlrpc_del_timeout_client);
500 static int ptlrpc_pinger_remove_timeouts(void)
502 struct timeout_item *item, *tmp;
504 mutex_lock(&pinger_mutex);
505 list_for_each_entry_safe(item, tmp, &timeout_list, ti_chain) {
506 LASSERT(list_empty(&item->ti_obd_list));
507 list_del(&item->ti_chain);
510 mutex_unlock(&pinger_mutex);
514 void ptlrpc_pinger_wake_up(void)
516 thread_add_flags(&pinger_thread, SVC_EVENT);
517 wake_up(&pinger_thread.t_ctl_waitq);