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 Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <liblustre.h>
29 #include <linux/version.h>
30 #include <asm/semaphore.h>
31 #define DEBUG_SUBSYSTEM S_RPC
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include "ptlrpc_internal.h"
38 #define PINGER_RATE 3 /* how many pings we'll do in obd_timeout period */
40 static DECLARE_MUTEX(pinger_sem);
41 static struct list_head pinger_imports = LIST_HEAD_INIT(pinger_imports);
43 int ptlrpc_ping(struct obd_import *imp)
45 struct ptlrpc_request *req;
49 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
51 DEBUG_REQ(D_HA, req, "pinging %s->%s",
52 imp->imp_obd->obd_uuid.uuid,
53 imp->imp_target_uuid.uuid);
54 req->rq_no_resend = req->rq_no_delay = 1;
55 req->rq_replen = lustre_msg_size(0, NULL);
56 req->rq_timeout = obd_timeout / PINGER_RATE;
59 CERROR("OOM trying to ping %s->%s\n",
60 imp->imp_obd->obd_uuid.uuid,
61 imp->imp_target_uuid.uuid);
69 static inline int ptlrpc_next_ping(struct obd_import *imp)
71 return jiffies + (obd_timeout / PINGER_RATE * HZ);
74 static inline int ptlrpc_next_reconnect(struct obd_import *imp)
76 if (imp->imp_server_timeout)
77 return jiffies + (obd_timeout / 2 * HZ);
79 return jiffies + (obd_timeout * HZ);
82 static atomic_t suspend_timeouts = ATOMIC_INIT(0);
83 static wait_queue_head_t suspend_timeouts_waitq;
85 void ptlrpc_deactivate_timeouts(void)
87 CDEBUG(D_HA, "deactivate timeouts\n");
88 atomic_inc(&suspend_timeouts);
91 void ptlrpc_activate_timeouts(void)
93 CDEBUG(D_HA, "activate timeouts\n");
94 LASSERT(atomic_read(&suspend_timeouts) > 0);
95 if (atomic_dec_and_test(&suspend_timeouts))
96 wake_up(&suspend_timeouts_waitq);
99 int ptlrpc_check_suspend(void)
101 if (atomic_read(&suspend_timeouts))
106 int ptlrpc_check_and_wait_suspend(struct ptlrpc_request *req)
108 struct l_wait_info lwi;
110 if (atomic_read(&suspend_timeouts)) {
111 DEBUG_REQ(D_NET, req, "-- suspend %d regular timeout",
112 atomic_read(&suspend_timeouts));
113 lwi = LWI_INTR(NULL, NULL);
114 l_wait_event(suspend_timeouts_waitq,
115 atomic_read(&suspend_timeouts) == 0, &lwi);
116 DEBUG_REQ(D_NET, req, "-- recharge regular timeout");
122 static void ptlrpc_pinger_process_import(struct obd_import *imp,
123 unsigned long this_ping)
128 spin_lock_irqsave(&imp->imp_lock, flags);
129 level = imp->imp_state;
130 force = imp->imp_force_verify;
132 imp->imp_force_verify = 0;
133 spin_unlock_irqrestore(&imp->imp_lock, flags);
135 if (imp->imp_next_ping - this_ping > obd_timeout * HZ) {
136 CWARN("wrong ping time %lu (current %lu)\n",
137 imp->imp_next_ping, this_ping);
138 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
141 if (imp->imp_next_ping > this_ping && force == 0)
144 if (level == LUSTRE_IMP_DISCON && !imp->imp_deactive) {
145 /* wait at least a timeout before trying recovery again */
146 imp->imp_next_ping = ptlrpc_next_reconnect(imp);
147 ptlrpc_initiate_recovery(imp);
148 } else if (level != LUSTRE_IMP_FULL || imp->imp_obd->obd_no_recov) {
149 CDEBUG(D_HA, "not pinging %s (in recovery "
150 " or recovery disabled: %s)\n",
151 imp->imp_target_uuid.uuid,
152 ptlrpc_import_state_name(level));
153 } else if (imp->imp_pingable || force) {
154 imp->imp_next_ping = ptlrpc_next_ping(imp);
159 static int ptlrpc_pinger_main(void *arg)
161 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
162 struct ptlrpc_thread *thread = data->thread;
169 SIGNAL_MASK_LOCK(current, flags);
170 sigfillset(¤t->blocked);
172 SIGNAL_MASK_UNLOCK(current, flags);
174 LASSERTF(strlen(data->name) < sizeof(current->comm),
175 "name %d > len %d\n",
176 (int)strlen(data->name), (int)sizeof(current->comm));
177 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", data->name);
180 /* Record that the thread is running */
181 thread->t_flags = SVC_RUNNING;
182 wake_up(&thread->t_ctl_waitq);
184 /* And now, loop forever, pinging as needed. */
186 unsigned long this_ping = jiffies;
187 long time_to_next_ping;
188 struct l_wait_info lwi = LWI_TIMEOUT(obd_timeout * HZ,
190 struct list_head *iter;
192 time_to_next_ping = this_ping + (obd_timeout * HZ) - jiffies;
194 list_for_each(iter, &pinger_imports) {
195 struct obd_import *imp =
196 list_entry(iter, struct obd_import,
199 ptlrpc_pinger_process_import(imp, this_ping);
201 CDEBUG(D_OTHER, "%s: pingable %d, next_ping %lu(%lu)\n",
202 imp->imp_target_uuid.uuid,
203 imp->imp_pingable, imp->imp_next_ping, jiffies);
205 if (imp->imp_pingable && imp->imp_next_ping &&
206 imp->imp_next_ping - jiffies < time_to_next_ping &&
207 imp->imp_next_ping > jiffies)
208 time_to_next_ping = imp->imp_next_ping - jiffies;
212 /* Wait until the next ping time, or until we're stopped. */
213 CDEBUG(D_HA, "next ping in %lu (%lu)\n", time_to_next_ping,
214 this_ping + (obd_timeout * HZ));
215 if (time_to_next_ping > 0) {
216 lwi = LWI_TIMEOUT(time_to_next_ping, NULL, NULL);
217 l_wait_event(thread->t_ctl_waitq,
218 thread->t_flags & (SVC_STOPPING|SVC_EVENT),
220 if (thread->t_flags & SVC_STOPPING) {
221 thread->t_flags &= ~SVC_STOPPING;
224 } else if (thread->t_flags & SVC_EVENT) {
225 /* woken after adding import to reset timer */
226 thread->t_flags &= ~SVC_EVENT;
231 thread->t_flags = SVC_STOPPED;
232 wake_up(&thread->t_ctl_waitq);
234 CDEBUG(D_NET, "pinger thread exiting, process %d\n", current->pid);
238 static struct ptlrpc_thread *pinger_thread = NULL;
240 int ptlrpc_start_pinger(void)
242 struct l_wait_info lwi = { 0 };
243 struct ptlrpc_svc_data d;
245 #ifndef ENABLE_PINGER
250 LASSERT(obd_timeout > PINGER_RATE);
252 if (pinger_thread != NULL)
255 OBD_ALLOC(pinger_thread, sizeof(*pinger_thread));
256 if (pinger_thread == NULL)
258 init_waitqueue_head(&pinger_thread->t_ctl_waitq);
259 init_waitqueue_head(&suspend_timeouts_waitq);
262 d.thread = pinger_thread;
264 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
265 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
266 rc = kernel_thread(ptlrpc_pinger_main, &d, CLONE_VM | CLONE_FILES);
268 CERROR("cannot start thread: %d\n", rc);
269 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
272 l_wait_event(pinger_thread->t_ctl_waitq,
273 pinger_thread->t_flags & SVC_RUNNING, &lwi);
278 int ptlrpc_stop_pinger(void)
280 struct l_wait_info lwi = { 0 };
282 #ifndef ENABLE_PINGER
287 if (pinger_thread == NULL)
290 pinger_thread->t_flags = SVC_STOPPING;
291 wake_up(&pinger_thread->t_ctl_waitq);
294 l_wait_event(pinger_thread->t_ctl_waitq,
295 (pinger_thread->t_flags & SVC_STOPPED), &lwi);
297 OBD_FREE(pinger_thread, sizeof(*pinger_thread));
298 pinger_thread = NULL;
302 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
305 imp->imp_next_ping = ptlrpc_next_ping(imp);
309 int ptlrpc_pinger_add_import(struct obd_import *imp)
312 if (!list_empty(&imp->imp_pinger_chain))
316 CDEBUG(D_HA, "adding pingable import %s->%s\n",
317 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
318 imp->imp_next_ping = jiffies + (obd_timeout * HZ);
319 /* XXX sort, blah blah */
320 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
321 class_import_get(imp);
323 ptlrpc_pinger_wake_up();
329 int ptlrpc_pinger_del_import(struct obd_import *imp)
332 if (list_empty(&imp->imp_pinger_chain))
336 list_del_init(&imp->imp_pinger_chain);
337 CDEBUG(D_HA, "removing pingable import %s->%s\n",
338 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
339 class_import_put(imp);
344 void ptlrpc_pinger_wake_up()
347 pinger_thread->t_flags |= SVC_EVENT;
348 wake_up(&pinger_thread->t_ctl_waitq);
352 #else /* !__KERNEL__ */
355 * the current implementation of pinger in liblustre is not optimized
358 static struct pinger_data {
360 unsigned long pd_this_ping;
361 unsigned long pd_next_ping;
365 static int pinger_check_rpcs(void *arg)
367 unsigned long curtime = time(NULL);
368 struct list_head *iter;
369 struct pinger_data *pd = &pinger_args;
371 /* prevent recursion */
372 if (pd->pd_recursion++) {
373 CDEBUG(D_HA, "pinger: recursion! quit\n");
378 /* have we reached ping point? */
379 if (pd->pd_next_ping > curtime && !pd->pd_force_check) {
384 if (pd->pd_force_check)
385 pd->pd_force_check = 0;
387 pd->pd_this_ping = curtime;
389 /* add rpcs into set */
391 list_for_each(iter, &pinger_imports) {
392 struct obd_import *imp =
393 list_entry(iter, struct obd_import,
399 spin_lock_irqsave(&imp->imp_lock, flags);
400 level = imp->imp_state;
401 force = imp->imp_force_verify;
403 imp->imp_force_verify = 0;
404 spin_unlock_irqrestore(&imp->imp_lock, flags);
406 if (imp->imp_next_ping <= pd->pd_this_ping || force) {
407 if (level == LUSTRE_IMP_DISCON) {
408 /* wait at least a timeout before
409 trying recovery again. */
410 unsigned long timeout = obd_timeout;
411 if (imp->imp_server_timeout)
412 timeout = obd_timeout / 2;
413 imp->imp_next_ping = time(NULL) +
415 ptlrpc_initiate_recovery(imp);
417 else if (level != LUSTRE_IMP_FULL ||
418 imp->imp_obd->obd_no_recov) {
420 "not pinging %s (in recovery "
421 " or recovery disabled: %s)\n",
422 imp->imp_target_uuid.uuid,
423 ptlrpc_import_state_name(level));
425 else if (imp->imp_pingable || force) {
430 if (imp->imp_pingable) {
431 CDEBUG(D_HA, "don't need to ping %s "
433 imp->imp_target_uuid.uuid,
434 imp->imp_next_ping, pd->pd_this_ping);
441 pd->pd_next_ping = pd->pd_this_ping + (obd_timeout * HZ);
443 CDEBUG(D_HA, "finished a round ping\n");
448 static void *pinger_callback = NULL;
450 int ptlrpc_start_pinger(void)
452 memset(&pinger_args, 0, sizeof(pinger_args));
455 liblustre_register_wait_callback(&pinger_check_rpcs, &pinger_args);
460 int ptlrpc_stop_pinger(void)
464 liblustre_deregister_wait_callback(pinger_callback);
469 void ptlrpc_pinger_sending_on_import(struct obd_import *imp)
472 imp->imp_next_ping = time(NULL) + obd_timeout;
473 if (pinger_args.pd_next_ping > imp->imp_next_ping) {
474 CDEBUG(D_HA, "set next ping to %ld(cur %ld)\n",
475 imp->imp_next_ping, time(NULL));
476 pinger_args.pd_next_ping = imp->imp_next_ping;
481 int ptlrpc_pinger_add_import(struct obd_import *imp)
484 if (!list_empty(&imp->imp_pinger_chain))
487 CDEBUG(D_HA, "adding pingable import %s->%s\n",
488 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
489 ptlrpc_pinger_sending_on_import(imp);
492 list_add_tail(&imp->imp_pinger_chain, &pinger_imports);
493 class_import_get(imp);
499 int ptlrpc_pinger_del_import(struct obd_import *imp)
502 if (list_empty(&imp->imp_pinger_chain))
506 list_del_init(&imp->imp_pinger_chain);
507 CDEBUG(D_HA, "removing pingable import %s->%s\n",
508 imp->imp_obd->obd_uuid.uuid, imp->imp_target_uuid.uuid);
509 class_import_put(imp);
514 void ptlrpc_pinger_wake_up()
516 pinger_args.pd_force_check = 1;
518 #endif /* !__KERNEL__ */